index.js 476 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. const defaultOptions = {
  6. sourceType: "script",
  7. sourceFilename: undefined,
  8. startColumn: 0,
  9. startLine: 1,
  10. allowAwaitOutsideFunction: false,
  11. allowReturnOutsideFunction: false,
  12. allowNewTargetOutsideFunction: false,
  13. allowImportExportEverywhere: false,
  14. allowSuperOutsideMethod: false,
  15. allowUndeclaredExports: false,
  16. plugins: [],
  17. strictMode: null,
  18. ranges: false,
  19. tokens: false,
  20. createParenthesizedExpressions: false,
  21. errorRecovery: false,
  22. attachComment: true,
  23. annexB: true
  24. };
  25. function getOptions(opts) {
  26. if (opts && opts.annexB != null && opts.annexB !== false) {
  27. throw new Error("The `annexB` option can only be set to `false`.");
  28. }
  29. const options = {};
  30. for (const key of Object.keys(defaultOptions)) {
  31. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  32. }
  33. return options;
  34. }
  35. class TokContext {
  36. constructor(token, preserveSpace) {
  37. this.token = void 0;
  38. this.preserveSpace = void 0;
  39. this.token = token;
  40. this.preserveSpace = !!preserveSpace;
  41. }
  42. }
  43. const types = {
  44. brace: new TokContext("{"),
  45. j_oTag: new TokContext("<tag"),
  46. j_cTag: new TokContext("</tag"),
  47. j_expr: new TokContext("<tag>...</tag>", true)
  48. };
  49. {
  50. types.template = new TokContext("`", true);
  51. }
  52. const beforeExpr = true;
  53. const startsExpr = true;
  54. const isLoop = true;
  55. const isAssign = true;
  56. const prefix = true;
  57. const postfix = true;
  58. class ExportedTokenType {
  59. constructor(label, conf = {}) {
  60. this.label = void 0;
  61. this.keyword = void 0;
  62. this.beforeExpr = void 0;
  63. this.startsExpr = void 0;
  64. this.rightAssociative = void 0;
  65. this.isLoop = void 0;
  66. this.isAssign = void 0;
  67. this.prefix = void 0;
  68. this.postfix = void 0;
  69. this.binop = void 0;
  70. this.label = label;
  71. this.keyword = conf.keyword;
  72. this.beforeExpr = !!conf.beforeExpr;
  73. this.startsExpr = !!conf.startsExpr;
  74. this.rightAssociative = !!conf.rightAssociative;
  75. this.isLoop = !!conf.isLoop;
  76. this.isAssign = !!conf.isAssign;
  77. this.prefix = !!conf.prefix;
  78. this.postfix = !!conf.postfix;
  79. this.binop = conf.binop != null ? conf.binop : null;
  80. {
  81. this.updateContext = null;
  82. }
  83. }
  84. }
  85. const keywords$1 = new Map();
  86. function createKeyword(name, options = {}) {
  87. options.keyword = name;
  88. const token = createToken(name, options);
  89. keywords$1.set(name, token);
  90. return token;
  91. }
  92. function createBinop(name, binop) {
  93. return createToken(name, {
  94. beforeExpr,
  95. binop
  96. });
  97. }
  98. let tokenTypeCounter = -1;
  99. const tokenTypes = [];
  100. const tokenLabels = [];
  101. const tokenBinops = [];
  102. const tokenBeforeExprs = [];
  103. const tokenStartsExprs = [];
  104. const tokenPrefixes = [];
  105. function createToken(name, options = {}) {
  106. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  107. ++tokenTypeCounter;
  108. tokenLabels.push(name);
  109. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  110. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  111. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  112. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  113. tokenTypes.push(new ExportedTokenType(name, options));
  114. return tokenTypeCounter;
  115. }
  116. function createKeywordLike(name, options = {}) {
  117. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  118. ++tokenTypeCounter;
  119. keywords$1.set(name, tokenTypeCounter);
  120. tokenLabels.push(name);
  121. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  122. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  123. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  124. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  125. tokenTypes.push(new ExportedTokenType("name", options));
  126. return tokenTypeCounter;
  127. }
  128. const tt = {
  129. bracketL: createToken("[", {
  130. beforeExpr,
  131. startsExpr
  132. }),
  133. bracketHashL: createToken("#[", {
  134. beforeExpr,
  135. startsExpr
  136. }),
  137. bracketBarL: createToken("[|", {
  138. beforeExpr,
  139. startsExpr
  140. }),
  141. bracketR: createToken("]"),
  142. bracketBarR: createToken("|]"),
  143. braceL: createToken("{", {
  144. beforeExpr,
  145. startsExpr
  146. }),
  147. braceBarL: createToken("{|", {
  148. beforeExpr,
  149. startsExpr
  150. }),
  151. braceHashL: createToken("#{", {
  152. beforeExpr,
  153. startsExpr
  154. }),
  155. braceR: createToken("}"),
  156. braceBarR: createToken("|}"),
  157. parenL: createToken("(", {
  158. beforeExpr,
  159. startsExpr
  160. }),
  161. parenR: createToken(")"),
  162. comma: createToken(",", {
  163. beforeExpr
  164. }),
  165. semi: createToken(";", {
  166. beforeExpr
  167. }),
  168. colon: createToken(":", {
  169. beforeExpr
  170. }),
  171. doubleColon: createToken("::", {
  172. beforeExpr
  173. }),
  174. dot: createToken("."),
  175. question: createToken("?", {
  176. beforeExpr
  177. }),
  178. questionDot: createToken("?."),
  179. arrow: createToken("=>", {
  180. beforeExpr
  181. }),
  182. template: createToken("template"),
  183. ellipsis: createToken("...", {
  184. beforeExpr
  185. }),
  186. backQuote: createToken("`", {
  187. startsExpr
  188. }),
  189. dollarBraceL: createToken("${", {
  190. beforeExpr,
  191. startsExpr
  192. }),
  193. templateTail: createToken("...`", {
  194. startsExpr
  195. }),
  196. templateNonTail: createToken("...${", {
  197. beforeExpr,
  198. startsExpr
  199. }),
  200. at: createToken("@"),
  201. hash: createToken("#", {
  202. startsExpr
  203. }),
  204. interpreterDirective: createToken("#!..."),
  205. eq: createToken("=", {
  206. beforeExpr,
  207. isAssign
  208. }),
  209. assign: createToken("_=", {
  210. beforeExpr,
  211. isAssign
  212. }),
  213. slashAssign: createToken("_=", {
  214. beforeExpr,
  215. isAssign
  216. }),
  217. xorAssign: createToken("_=", {
  218. beforeExpr,
  219. isAssign
  220. }),
  221. moduloAssign: createToken("_=", {
  222. beforeExpr,
  223. isAssign
  224. }),
  225. incDec: createToken("++/--", {
  226. prefix,
  227. postfix,
  228. startsExpr
  229. }),
  230. bang: createToken("!", {
  231. beforeExpr,
  232. prefix,
  233. startsExpr
  234. }),
  235. tilde: createToken("~", {
  236. beforeExpr,
  237. prefix,
  238. startsExpr
  239. }),
  240. doubleCaret: createToken("^^", {
  241. startsExpr
  242. }),
  243. doubleAt: createToken("@@", {
  244. startsExpr
  245. }),
  246. pipeline: createBinop("|>", 0),
  247. nullishCoalescing: createBinop("??", 1),
  248. logicalOR: createBinop("||", 1),
  249. logicalAND: createBinop("&&", 2),
  250. bitwiseOR: createBinop("|", 3),
  251. bitwiseXOR: createBinop("^", 4),
  252. bitwiseAND: createBinop("&", 5),
  253. equality: createBinop("==/!=/===/!==", 6),
  254. lt: createBinop("</>/<=/>=", 7),
  255. gt: createBinop("</>/<=/>=", 7),
  256. relational: createBinop("</>/<=/>=", 7),
  257. bitShift: createBinop("<</>>/>>>", 8),
  258. bitShiftL: createBinop("<</>>/>>>", 8),
  259. bitShiftR: createBinop("<</>>/>>>", 8),
  260. plusMin: createToken("+/-", {
  261. beforeExpr,
  262. binop: 9,
  263. prefix,
  264. startsExpr
  265. }),
  266. modulo: createToken("%", {
  267. binop: 10,
  268. startsExpr
  269. }),
  270. star: createToken("*", {
  271. binop: 10
  272. }),
  273. slash: createBinop("/", 10),
  274. exponent: createToken("**", {
  275. beforeExpr,
  276. binop: 11,
  277. rightAssociative: true
  278. }),
  279. _in: createKeyword("in", {
  280. beforeExpr,
  281. binop: 7
  282. }),
  283. _instanceof: createKeyword("instanceof", {
  284. beforeExpr,
  285. binop: 7
  286. }),
  287. _break: createKeyword("break"),
  288. _case: createKeyword("case", {
  289. beforeExpr
  290. }),
  291. _catch: createKeyword("catch"),
  292. _continue: createKeyword("continue"),
  293. _debugger: createKeyword("debugger"),
  294. _default: createKeyword("default", {
  295. beforeExpr
  296. }),
  297. _else: createKeyword("else", {
  298. beforeExpr
  299. }),
  300. _finally: createKeyword("finally"),
  301. _function: createKeyword("function", {
  302. startsExpr
  303. }),
  304. _if: createKeyword("if"),
  305. _return: createKeyword("return", {
  306. beforeExpr
  307. }),
  308. _switch: createKeyword("switch"),
  309. _throw: createKeyword("throw", {
  310. beforeExpr,
  311. prefix,
  312. startsExpr
  313. }),
  314. _try: createKeyword("try"),
  315. _var: createKeyword("var"),
  316. _const: createKeyword("const"),
  317. _with: createKeyword("with"),
  318. _new: createKeyword("new", {
  319. beforeExpr,
  320. startsExpr
  321. }),
  322. _this: createKeyword("this", {
  323. startsExpr
  324. }),
  325. _super: createKeyword("super", {
  326. startsExpr
  327. }),
  328. _class: createKeyword("class", {
  329. startsExpr
  330. }),
  331. _extends: createKeyword("extends", {
  332. beforeExpr
  333. }),
  334. _export: createKeyword("export"),
  335. _import: createKeyword("import", {
  336. startsExpr
  337. }),
  338. _null: createKeyword("null", {
  339. startsExpr
  340. }),
  341. _true: createKeyword("true", {
  342. startsExpr
  343. }),
  344. _false: createKeyword("false", {
  345. startsExpr
  346. }),
  347. _typeof: createKeyword("typeof", {
  348. beforeExpr,
  349. prefix,
  350. startsExpr
  351. }),
  352. _void: createKeyword("void", {
  353. beforeExpr,
  354. prefix,
  355. startsExpr
  356. }),
  357. _delete: createKeyword("delete", {
  358. beforeExpr,
  359. prefix,
  360. startsExpr
  361. }),
  362. _do: createKeyword("do", {
  363. isLoop,
  364. beforeExpr
  365. }),
  366. _for: createKeyword("for", {
  367. isLoop
  368. }),
  369. _while: createKeyword("while", {
  370. isLoop
  371. }),
  372. _as: createKeywordLike("as", {
  373. startsExpr
  374. }),
  375. _assert: createKeywordLike("assert", {
  376. startsExpr
  377. }),
  378. _async: createKeywordLike("async", {
  379. startsExpr
  380. }),
  381. _await: createKeywordLike("await", {
  382. startsExpr
  383. }),
  384. _from: createKeywordLike("from", {
  385. startsExpr
  386. }),
  387. _get: createKeywordLike("get", {
  388. startsExpr
  389. }),
  390. _let: createKeywordLike("let", {
  391. startsExpr
  392. }),
  393. _meta: createKeywordLike("meta", {
  394. startsExpr
  395. }),
  396. _of: createKeywordLike("of", {
  397. startsExpr
  398. }),
  399. _sent: createKeywordLike("sent", {
  400. startsExpr
  401. }),
  402. _set: createKeywordLike("set", {
  403. startsExpr
  404. }),
  405. _static: createKeywordLike("static", {
  406. startsExpr
  407. }),
  408. _using: createKeywordLike("using", {
  409. startsExpr
  410. }),
  411. _yield: createKeywordLike("yield", {
  412. startsExpr
  413. }),
  414. _asserts: createKeywordLike("asserts", {
  415. startsExpr
  416. }),
  417. _checks: createKeywordLike("checks", {
  418. startsExpr
  419. }),
  420. _exports: createKeywordLike("exports", {
  421. startsExpr
  422. }),
  423. _global: createKeywordLike("global", {
  424. startsExpr
  425. }),
  426. _implements: createKeywordLike("implements", {
  427. startsExpr
  428. }),
  429. _intrinsic: createKeywordLike("intrinsic", {
  430. startsExpr
  431. }),
  432. _infer: createKeywordLike("infer", {
  433. startsExpr
  434. }),
  435. _is: createKeywordLike("is", {
  436. startsExpr
  437. }),
  438. _mixins: createKeywordLike("mixins", {
  439. startsExpr
  440. }),
  441. _proto: createKeywordLike("proto", {
  442. startsExpr
  443. }),
  444. _require: createKeywordLike("require", {
  445. startsExpr
  446. }),
  447. _satisfies: createKeywordLike("satisfies", {
  448. startsExpr
  449. }),
  450. _keyof: createKeywordLike("keyof", {
  451. startsExpr
  452. }),
  453. _readonly: createKeywordLike("readonly", {
  454. startsExpr
  455. }),
  456. _unique: createKeywordLike("unique", {
  457. startsExpr
  458. }),
  459. _abstract: createKeywordLike("abstract", {
  460. startsExpr
  461. }),
  462. _declare: createKeywordLike("declare", {
  463. startsExpr
  464. }),
  465. _enum: createKeywordLike("enum", {
  466. startsExpr
  467. }),
  468. _module: createKeywordLike("module", {
  469. startsExpr
  470. }),
  471. _namespace: createKeywordLike("namespace", {
  472. startsExpr
  473. }),
  474. _interface: createKeywordLike("interface", {
  475. startsExpr
  476. }),
  477. _type: createKeywordLike("type", {
  478. startsExpr
  479. }),
  480. _opaque: createKeywordLike("opaque", {
  481. startsExpr
  482. }),
  483. name: createToken("name", {
  484. startsExpr
  485. }),
  486. string: createToken("string", {
  487. startsExpr
  488. }),
  489. num: createToken("num", {
  490. startsExpr
  491. }),
  492. bigint: createToken("bigint", {
  493. startsExpr
  494. }),
  495. decimal: createToken("decimal", {
  496. startsExpr
  497. }),
  498. regexp: createToken("regexp", {
  499. startsExpr
  500. }),
  501. privateName: createToken("#name", {
  502. startsExpr
  503. }),
  504. eof: createToken("eof"),
  505. jsxName: createToken("jsxName"),
  506. jsxText: createToken("jsxText", {
  507. beforeExpr: true
  508. }),
  509. jsxTagStart: createToken("jsxTagStart", {
  510. startsExpr: true
  511. }),
  512. jsxTagEnd: createToken("jsxTagEnd"),
  513. placeholder: createToken("%%", {
  514. startsExpr: true
  515. })
  516. };
  517. function tokenIsIdentifier(token) {
  518. return token >= 93 && token <= 130;
  519. }
  520. function tokenKeywordOrIdentifierIsKeyword(token) {
  521. return token <= 92;
  522. }
  523. function tokenIsKeywordOrIdentifier(token) {
  524. return token >= 58 && token <= 130;
  525. }
  526. function tokenIsLiteralPropertyName(token) {
  527. return token >= 58 && token <= 134;
  528. }
  529. function tokenComesBeforeExpression(token) {
  530. return tokenBeforeExprs[token];
  531. }
  532. function tokenCanStartExpression(token) {
  533. return tokenStartsExprs[token];
  534. }
  535. function tokenIsAssignment(token) {
  536. return token >= 29 && token <= 33;
  537. }
  538. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  539. return token >= 127 && token <= 129;
  540. }
  541. function tokenIsLoop(token) {
  542. return token >= 90 && token <= 92;
  543. }
  544. function tokenIsKeyword(token) {
  545. return token >= 58 && token <= 92;
  546. }
  547. function tokenIsOperator(token) {
  548. return token >= 39 && token <= 59;
  549. }
  550. function tokenIsPostfix(token) {
  551. return token === 34;
  552. }
  553. function tokenIsPrefix(token) {
  554. return tokenPrefixes[token];
  555. }
  556. function tokenIsTSTypeOperator(token) {
  557. return token >= 119 && token <= 121;
  558. }
  559. function tokenIsTSDeclarationStart(token) {
  560. return token >= 122 && token <= 128;
  561. }
  562. function tokenLabelName(token) {
  563. return tokenLabels[token];
  564. }
  565. function tokenOperatorPrecedence(token) {
  566. return tokenBinops[token];
  567. }
  568. function tokenIsRightAssociative(token) {
  569. return token === 57;
  570. }
  571. function tokenIsTemplate(token) {
  572. return token >= 24 && token <= 25;
  573. }
  574. function getExportedToken(token) {
  575. return tokenTypes[token];
  576. }
  577. {
  578. tokenTypes[8].updateContext = context => {
  579. context.pop();
  580. };
  581. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  582. context.push(types.brace);
  583. };
  584. tokenTypes[22].updateContext = context => {
  585. if (context[context.length - 1] === types.template) {
  586. context.pop();
  587. } else {
  588. context.push(types.template);
  589. }
  590. };
  591. tokenTypes[140].updateContext = context => {
  592. context.push(types.j_expr, types.j_oTag);
  593. };
  594. }
  595. function _objectWithoutPropertiesLoose(source, excluded) {
  596. if (source == null) return {};
  597. var target = {};
  598. var sourceKeys = Object.keys(source);
  599. var key, i;
  600. for (i = 0; i < sourceKeys.length; i++) {
  601. key = sourceKeys[i];
  602. if (excluded.indexOf(key) >= 0) continue;
  603. target[key] = source[key];
  604. }
  605. return target;
  606. }
  607. class Position {
  608. constructor(line, col, index) {
  609. this.line = void 0;
  610. this.column = void 0;
  611. this.index = void 0;
  612. this.line = line;
  613. this.column = col;
  614. this.index = index;
  615. }
  616. }
  617. class SourceLocation {
  618. constructor(start, end) {
  619. this.start = void 0;
  620. this.end = void 0;
  621. this.filename = void 0;
  622. this.identifierName = void 0;
  623. this.start = start;
  624. this.end = end;
  625. }
  626. }
  627. function createPositionWithColumnOffset(position, columnOffset) {
  628. const {
  629. line,
  630. column,
  631. index
  632. } = position;
  633. return new Position(line, column + columnOffset, index + columnOffset);
  634. }
  635. var ParseErrorCode = {
  636. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  637. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  638. };
  639. const reflect = (keys, last = keys.length - 1) => ({
  640. get() {
  641. return keys.reduce((object, key) => object[key], this);
  642. },
  643. set(value) {
  644. keys.reduce((item, key, i) => i === last ? item[key] = value : item[key], this);
  645. }
  646. });
  647. const instantiate = (constructor, properties, descriptors) => Object.keys(descriptors).map(key => [key, descriptors[key]]).filter(([, descriptor]) => !!descriptor).map(([key, descriptor]) => [key, typeof descriptor === "function" ? {
  648. value: descriptor,
  649. enumerable: false
  650. } : typeof descriptor.reflect === "string" ? Object.assign({}, descriptor, reflect(descriptor.reflect.split("."))) : descriptor]).reduce((instance, [key, descriptor]) => Object.defineProperty(instance, key, Object.assign({
  651. configurable: true
  652. }, descriptor)), Object.assign(new constructor(), properties));
  653. var ModuleErrors = {
  654. ImportMetaOutsideModule: {
  655. message: `import.meta may appear only with 'sourceType: "module"'`,
  656. code: ParseErrorCode.SourceTypeModuleError
  657. },
  658. ImportOutsideModule: {
  659. message: `'import' and 'export' may appear only with 'sourceType: "module"'`,
  660. code: ParseErrorCode.SourceTypeModuleError
  661. }
  662. };
  663. const NodeDescriptions = {
  664. ArrayPattern: "array destructuring pattern",
  665. AssignmentExpression: "assignment expression",
  666. AssignmentPattern: "assignment expression",
  667. ArrowFunctionExpression: "arrow function expression",
  668. ConditionalExpression: "conditional expression",
  669. CatchClause: "catch clause",
  670. ForOfStatement: "for-of statement",
  671. ForInStatement: "for-in statement",
  672. ForStatement: "for-loop",
  673. FormalParameters: "function parameter list",
  674. Identifier: "identifier",
  675. ImportSpecifier: "import specifier",
  676. ImportDefaultSpecifier: "import default specifier",
  677. ImportNamespaceSpecifier: "import namespace specifier",
  678. ObjectPattern: "object destructuring pattern",
  679. ParenthesizedExpression: "parenthesized expression",
  680. RestElement: "rest element",
  681. UpdateExpression: {
  682. true: "prefix operation",
  683. false: "postfix operation"
  684. },
  685. VariableDeclarator: "variable declaration",
  686. YieldExpression: "yield expression"
  687. };
  688. const toNodeDescription = ({
  689. type,
  690. prefix
  691. }) => type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[String(prefix)] : NodeDescriptions[type];
  692. var StandardErrors = {
  693. AccessorIsGenerator: ({
  694. kind
  695. }) => `A ${kind}ter cannot be a generator.`,
  696. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  697. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  698. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  699. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  700. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  701. AwaitInUsingBinding: "'await' is not allowed to be used as a name in 'using' declarations.",
  702. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  703. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  704. BadGetterArity: "A 'get' accessor must not have any formal parameters.",
  705. BadSetterArity: "A 'set' accessor must have exactly one formal parameter.",
  706. BadSetterRestParameter: "A 'set' accessor function argument must not be a rest parameter.",
  707. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  708. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  709. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  710. ConstructorIsAsync: "Constructor can't be an async function.",
  711. ConstructorIsGenerator: "Constructor can't be a generator.",
  712. DeclarationMissingInitializer: ({
  713. kind
  714. }) => `Missing initializer in ${kind} declaration.`,
  715. DecoratorArgumentsOutsideParentheses: "Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'.",
  716. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. Remove the 'decoratorsBeforeExport: true' option to use the 'export @decorator class {}' syntax.",
  717. DecoratorsBeforeAfterExport: "Decorators can be placed *either* before or after the 'export' keyword, but not in both locations at the same time.",
  718. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  719. DecoratorExportClass: "Decorators must be placed *after* the 'export' keyword. Remove the 'decoratorsBeforeExport: false' option to use the '@decorator export class {}' syntax.",
  720. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  721. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  722. DeletePrivateField: "Deleting a private field is not allowed.",
  723. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  724. DuplicateConstructor: "Duplicate constructor in the same class.",
  725. DuplicateDefaultExport: "Only one default export allowed per module.",
  726. DuplicateExport: ({
  727. exportName
  728. }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`,
  729. DuplicateProto: "Redefinition of __proto__ property.",
  730. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  731. ElementAfterRest: "Rest element must be last element.",
  732. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  733. ExportBindingIsString: ({
  734. localName,
  735. exportName
  736. }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`,
  737. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  738. ForInOfLoopInitializer: ({
  739. type
  740. }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`,
  741. ForInUsing: "For-in loop may not start with 'using' declaration.",
  742. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  743. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  744. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  745. IllegalBreakContinue: ({
  746. type
  747. }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`,
  748. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  749. IllegalReturn: "'return' outside of function.",
  750. ImportBindingIsString: ({
  751. importName
  752. }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`,
  753. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  754. ImportCallArity: ({
  755. maxArgumentCount
  756. }) => `\`import()\` requires exactly ${maxArgumentCount === 1 ? "one argument" : "one or two arguments"}.`,
  757. ImportCallNotNewExpression: "Cannot use new with import(...).",
  758. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  759. ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.",
  760. ImportReflectionHasAssertion: "`import module x` cannot have assertions.",
  761. ImportReflectionNotBinding: 'Only `import module x from "./module"` is valid.',
  762. IncompatibleRegExpUVFlags: "The 'u' and 'v' regular expression flags cannot be enabled at the same time.",
  763. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  764. InvalidCodePoint: "Code point out of bounds.",
  765. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  766. InvalidDecimal: "Invalid decimal.",
  767. InvalidDigit: ({
  768. radix
  769. }) => `Expected number in radix ${radix}.`,
  770. InvalidEscapeSequence: "Bad character escape sequence.",
  771. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  772. InvalidEscapedReservedWord: ({
  773. reservedWord
  774. }) => `Escape sequence in keyword ${reservedWord}.`,
  775. InvalidIdentifier: ({
  776. identifierName
  777. }) => `Invalid identifier ${identifierName}.`,
  778. InvalidLhs: ({
  779. ancestor
  780. }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  781. InvalidLhsBinding: ({
  782. ancestor
  783. }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  784. InvalidNumber: "Invalid number.",
  785. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  786. InvalidOrUnexpectedToken: ({
  787. unexpected
  788. }) => `Unexpected character '${unexpected}'.`,
  789. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  790. InvalidPrivateFieldResolution: ({
  791. identifierName
  792. }) => `Private name #${identifierName} is not defined.`,
  793. InvalidPropertyBindingPattern: "Binding member expression.",
  794. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  795. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  796. LabelRedeclaration: ({
  797. labelName
  798. }) => `Label '${labelName}' is already declared.`,
  799. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  800. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  801. MalformedRegExpFlags: "Invalid regular expression flag.",
  802. MissingClassName: "A class name is required.",
  803. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  804. MissingSemicolon: "Missing semicolon.",
  805. MissingPlugin: ({
  806. missingPlugin
  807. }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  808. MissingOneOfPlugins: ({
  809. missingPlugin
  810. }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  811. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  812. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  813. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  814. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  815. ModuleAttributesWithDuplicateKeys: ({
  816. key
  817. }) => `Duplicate key "${key}" is not allowed in module attributes.`,
  818. ModuleExportNameHasLoneSurrogate: ({
  819. surrogateCharCode
  820. }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`,
  821. ModuleExportUndefined: ({
  822. localName
  823. }) => `Export '${localName}' is not defined.`,
  824. MultipleDefaultsInSwitch: "Multiple default clauses.",
  825. NewlineAfterThrow: "Illegal newline after throw.",
  826. NoCatchOrFinally: "Missing catch or finally clause.",
  827. NumberIdentifier: "Identifier directly after number.",
  828. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  829. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  830. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  831. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  832. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  833. ParamDupe: "Argument name clash.",
  834. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  835. PatternHasMethod: "Object pattern can't contain methods.",
  836. PrivateInExpectedIn: ({
  837. identifierName
  838. }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`,
  839. PrivateNameRedeclaration: ({
  840. identifierName
  841. }) => `Duplicate private name #${identifierName}.`,
  842. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  843. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  844. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  845. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  846. RestTrailingComma: "Unexpected trailing comma after rest element.",
  847. SloppyFunction: "In non-strict mode code, functions can only be declared at top level or inside a block.",
  848. SloppyFunctionAnnexB: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
  849. StaticPrototype: "Classes may not have static property named prototype.",
  850. 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?",
  851. SuperPrivateField: "Private fields can't be accessed on super.",
  852. TrailingDecorator: "Decorators must be attached to a class element.",
  853. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  854. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  855. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  856. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  857. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  858. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  859. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  860. UnexpectedKeyword: ({
  861. keyword
  862. }) => `Unexpected keyword '${keyword}'.`,
  863. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  864. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  865. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  866. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  867. UnexpectedPrivateField: "Unexpected private name.",
  868. UnexpectedReservedWord: ({
  869. reservedWord
  870. }) => `Unexpected reserved word '${reservedWord}'.`,
  871. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  872. UnexpectedToken: ({
  873. expected,
  874. unexpected
  875. }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`,
  876. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  877. UnexpectedUsingDeclaration: "Using declaration cannot appear in the top level when source type is `script`.",
  878. UnsupportedBind: "Binding should be performed on object property.",
  879. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  880. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  881. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  882. UnsupportedMetaProperty: ({
  883. target,
  884. onlyValidPropertyName
  885. }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`,
  886. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  887. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  888. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  889. UnterminatedComment: "Unterminated comment.",
  890. UnterminatedRegExp: "Unterminated regular expression.",
  891. UnterminatedString: "Unterminated string constant.",
  892. UnterminatedTemplate: "Unterminated template.",
  893. UsingDeclarationHasBindingPattern: "Using declaration cannot have destructuring patterns.",
  894. VarRedeclaration: ({
  895. identifierName
  896. }) => `Identifier '${identifierName}' has already been declared.`,
  897. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  898. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  899. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  900. };
  901. var StrictModeErrors = {
  902. StrictDelete: "Deleting local variable in strict mode.",
  903. StrictEvalArguments: ({
  904. referenceName
  905. }) => `Assigning to '${referenceName}' in strict mode.`,
  906. StrictEvalArgumentsBinding: ({
  907. bindingName
  908. }) => `Binding '${bindingName}' in strict mode.`,
  909. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  910. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  911. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  912. StrictWith: "'with' in strict mode."
  913. };
  914. const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
  915. var PipelineOperatorErrors = {
  916. PipeBodyIsTighter: "Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  917. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  918. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  919. PipeTopicUnconfiguredToken: ({
  920. token
  921. }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`,
  922. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  923. PipeUnparenthesizedBody: ({
  924. type
  925. }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
  926. type
  927. })}; please wrap it in parentheses.`,
  928. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  929. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  930. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  931. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  932. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  933. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'
  934. };
  935. const _excluded$1 = ["toMessage"],
  936. _excluded2$1 = ["message"];
  937. function toParseErrorConstructor(_ref) {
  938. let {
  939. toMessage
  940. } = _ref,
  941. properties = _objectWithoutPropertiesLoose(_ref, _excluded$1);
  942. return function constructor({
  943. loc,
  944. details
  945. }) {
  946. return instantiate(SyntaxError, Object.assign({}, properties, {
  947. loc
  948. }), {
  949. clone(overrides = {}) {
  950. const loc = overrides.loc || {};
  951. return constructor({
  952. loc: new Position("line" in loc ? loc.line : this.loc.line, "column" in loc ? loc.column : this.loc.column, "index" in loc ? loc.index : this.loc.index),
  953. details: Object.assign({}, this.details, overrides.details)
  954. });
  955. },
  956. details: {
  957. value: details,
  958. enumerable: false
  959. },
  960. message: {
  961. get() {
  962. return `${toMessage(this.details)} (${this.loc.line}:${this.loc.column})`;
  963. },
  964. set(value) {
  965. Object.defineProperty(this, "message", {
  966. value
  967. });
  968. }
  969. },
  970. pos: {
  971. reflect: "loc.index",
  972. enumerable: true
  973. },
  974. missingPlugin: "missingPlugin" in details && {
  975. reflect: "details.missingPlugin",
  976. enumerable: true
  977. }
  978. });
  979. };
  980. }
  981. function ParseErrorEnum(argument, syntaxPlugin) {
  982. if (Array.isArray(argument)) {
  983. return parseErrorTemplates => ParseErrorEnum(parseErrorTemplates, argument[0]);
  984. }
  985. const ParseErrorConstructors = {};
  986. for (const reasonCode of Object.keys(argument)) {
  987. const template = argument[reasonCode];
  988. const _ref2 = typeof template === "string" ? {
  989. message: () => template
  990. } : typeof template === "function" ? {
  991. message: template
  992. } : template,
  993. {
  994. message
  995. } = _ref2,
  996. rest = _objectWithoutPropertiesLoose(_ref2, _excluded2$1);
  997. const toMessage = typeof message === "string" ? () => message : message;
  998. ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
  999. code: ParseErrorCode.SyntaxError,
  1000. reasonCode,
  1001. toMessage
  1002. }, syntaxPlugin ? {
  1003. syntaxPlugin
  1004. } : {}, rest));
  1005. }
  1006. return ParseErrorConstructors;
  1007. }
  1008. const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
  1009. const {
  1010. defineProperty
  1011. } = Object;
  1012. const toUnenumerable = (object, key) => defineProperty(object, key, {
  1013. enumerable: false,
  1014. value: object[key]
  1015. });
  1016. function toESTreeLocation(node) {
  1017. node.loc.start && toUnenumerable(node.loc.start, "index");
  1018. node.loc.end && toUnenumerable(node.loc.end, "index");
  1019. return node;
  1020. }
  1021. var estree = superClass => class ESTreeParserMixin extends superClass {
  1022. parse() {
  1023. const file = toESTreeLocation(super.parse());
  1024. if (this.options.tokens) {
  1025. file.tokens = file.tokens.map(toESTreeLocation);
  1026. }
  1027. return file;
  1028. }
  1029. parseRegExpLiteral({
  1030. pattern,
  1031. flags
  1032. }) {
  1033. let regex = null;
  1034. try {
  1035. regex = new RegExp(pattern, flags);
  1036. } catch (e) {}
  1037. const node = this.estreeParseLiteral(regex);
  1038. node.regex = {
  1039. pattern,
  1040. flags
  1041. };
  1042. return node;
  1043. }
  1044. parseBigIntLiteral(value) {
  1045. let bigInt;
  1046. try {
  1047. bigInt = BigInt(value);
  1048. } catch (_unused) {
  1049. bigInt = null;
  1050. }
  1051. const node = this.estreeParseLiteral(bigInt);
  1052. node.bigint = String(node.value || value);
  1053. return node;
  1054. }
  1055. parseDecimalLiteral(value) {
  1056. const decimal = null;
  1057. const node = this.estreeParseLiteral(decimal);
  1058. node.decimal = String(node.value || value);
  1059. return node;
  1060. }
  1061. estreeParseLiteral(value) {
  1062. return this.parseLiteral(value, "Literal");
  1063. }
  1064. parseStringLiteral(value) {
  1065. return this.estreeParseLiteral(value);
  1066. }
  1067. parseNumericLiteral(value) {
  1068. return this.estreeParseLiteral(value);
  1069. }
  1070. parseNullLiteral() {
  1071. return this.estreeParseLiteral(null);
  1072. }
  1073. parseBooleanLiteral(value) {
  1074. return this.estreeParseLiteral(value);
  1075. }
  1076. directiveToStmt(directive) {
  1077. const expression = directive.value;
  1078. delete directive.value;
  1079. expression.type = "Literal";
  1080. expression.raw = expression.extra.raw;
  1081. expression.value = expression.extra.expressionValue;
  1082. const stmt = directive;
  1083. stmt.type = "ExpressionStatement";
  1084. stmt.expression = expression;
  1085. stmt.directive = expression.extra.rawValue;
  1086. delete expression.extra;
  1087. return stmt;
  1088. }
  1089. initFunction(node, isAsync) {
  1090. super.initFunction(node, isAsync);
  1091. node.expression = false;
  1092. }
  1093. checkDeclaration(node) {
  1094. if (node != null && this.isObjectProperty(node)) {
  1095. this.checkDeclaration(node.value);
  1096. } else {
  1097. super.checkDeclaration(node);
  1098. }
  1099. }
  1100. getObjectOrClassMethodParams(method) {
  1101. return method.value.params;
  1102. }
  1103. isValidDirective(stmt) {
  1104. var _stmt$expression$extr;
  1105. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  1106. }
  1107. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  1108. super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
  1109. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  1110. node.body = directiveStatements.concat(node.body);
  1111. delete node.directives;
  1112. }
  1113. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  1114. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  1115. if (method.typeParameters) {
  1116. method.value.typeParameters = method.typeParameters;
  1117. delete method.typeParameters;
  1118. }
  1119. classBody.body.push(method);
  1120. }
  1121. parsePrivateName() {
  1122. const node = super.parsePrivateName();
  1123. {
  1124. if (!this.getPluginOption("estree", "classFeatures")) {
  1125. return node;
  1126. }
  1127. }
  1128. return this.convertPrivateNameToPrivateIdentifier(node);
  1129. }
  1130. convertPrivateNameToPrivateIdentifier(node) {
  1131. const name = super.getPrivateNameSV(node);
  1132. node = node;
  1133. delete node.id;
  1134. node.name = name;
  1135. node.type = "PrivateIdentifier";
  1136. return node;
  1137. }
  1138. isPrivateName(node) {
  1139. {
  1140. if (!this.getPluginOption("estree", "classFeatures")) {
  1141. return super.isPrivateName(node);
  1142. }
  1143. }
  1144. return node.type === "PrivateIdentifier";
  1145. }
  1146. getPrivateNameSV(node) {
  1147. {
  1148. if (!this.getPluginOption("estree", "classFeatures")) {
  1149. return super.getPrivateNameSV(node);
  1150. }
  1151. }
  1152. return node.name;
  1153. }
  1154. parseLiteral(value, type) {
  1155. const node = super.parseLiteral(value, type);
  1156. node.raw = node.extra.raw;
  1157. delete node.extra;
  1158. return node;
  1159. }
  1160. parseFunctionBody(node, allowExpression, isMethod = false) {
  1161. super.parseFunctionBody(node, allowExpression, isMethod);
  1162. node.expression = node.body.type !== "BlockStatement";
  1163. }
  1164. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  1165. let funcNode = this.startNode();
  1166. funcNode.kind = node.kind;
  1167. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  1168. funcNode.type = "FunctionExpression";
  1169. delete funcNode.kind;
  1170. node.value = funcNode;
  1171. if (type === "ClassPrivateMethod") {
  1172. node.computed = false;
  1173. }
  1174. return this.finishNode(node, "MethodDefinition");
  1175. }
  1176. parseClassProperty(...args) {
  1177. const propertyNode = super.parseClassProperty(...args);
  1178. {
  1179. if (!this.getPluginOption("estree", "classFeatures")) {
  1180. return propertyNode;
  1181. }
  1182. }
  1183. propertyNode.type = "PropertyDefinition";
  1184. return propertyNode;
  1185. }
  1186. parseClassPrivateProperty(...args) {
  1187. const propertyNode = super.parseClassPrivateProperty(...args);
  1188. {
  1189. if (!this.getPluginOption("estree", "classFeatures")) {
  1190. return propertyNode;
  1191. }
  1192. }
  1193. propertyNode.type = "PropertyDefinition";
  1194. propertyNode.computed = false;
  1195. return propertyNode;
  1196. }
  1197. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  1198. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  1199. if (node) {
  1200. node.type = "Property";
  1201. if (node.kind === "method") {
  1202. node.kind = "init";
  1203. }
  1204. node.shorthand = false;
  1205. }
  1206. return node;
  1207. }
  1208. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  1209. const node = super.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  1210. if (node) {
  1211. node.kind = "init";
  1212. node.type = "Property";
  1213. }
  1214. return node;
  1215. }
  1216. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  1217. return type === "Property" ? "value" : super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  1218. }
  1219. isAssignable(node, isBinding) {
  1220. if (node != null && this.isObjectProperty(node)) {
  1221. return this.isAssignable(node.value, isBinding);
  1222. }
  1223. return super.isAssignable(node, isBinding);
  1224. }
  1225. toAssignable(node, isLHS = false) {
  1226. if (node != null && this.isObjectProperty(node)) {
  1227. const {
  1228. key,
  1229. value
  1230. } = node;
  1231. if (this.isPrivateName(key)) {
  1232. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  1233. }
  1234. this.toAssignable(value, isLHS);
  1235. } else {
  1236. super.toAssignable(node, isLHS);
  1237. }
  1238. }
  1239. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  1240. if (prop.kind === "get" || prop.kind === "set") {
  1241. this.raise(Errors.PatternHasAccessor, {
  1242. at: prop.key
  1243. });
  1244. } else if (prop.method) {
  1245. this.raise(Errors.PatternHasMethod, {
  1246. at: prop.key
  1247. });
  1248. } else {
  1249. super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  1250. }
  1251. }
  1252. finishCallExpression(unfinished, optional) {
  1253. const node = super.finishCallExpression(unfinished, optional);
  1254. if (node.callee.type === "Import") {
  1255. node.type = "ImportExpression";
  1256. node.source = node.arguments[0];
  1257. if (this.hasPlugin("importAssertions")) {
  1258. var _node$arguments$;
  1259. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  1260. }
  1261. delete node.arguments;
  1262. delete node.callee;
  1263. }
  1264. return node;
  1265. }
  1266. toReferencedArguments(node) {
  1267. if (node.type === "ImportExpression") {
  1268. return;
  1269. }
  1270. super.toReferencedArguments(node);
  1271. }
  1272. parseExport(unfinished, decorators) {
  1273. const exportStartLoc = this.state.lastTokStartLoc;
  1274. const node = super.parseExport(unfinished, decorators);
  1275. switch (node.type) {
  1276. case "ExportAllDeclaration":
  1277. node.exported = null;
  1278. break;
  1279. case "ExportNamedDeclaration":
  1280. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  1281. node.type = "ExportAllDeclaration";
  1282. node.exported = node.specifiers[0].exported;
  1283. delete node.specifiers;
  1284. }
  1285. case "ExportDefaultDeclaration":
  1286. {
  1287. var _declaration$decorato;
  1288. const {
  1289. declaration
  1290. } = node;
  1291. if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0 && declaration.start === node.start) {
  1292. this.resetStartLocation(node, exportStartLoc);
  1293. }
  1294. }
  1295. break;
  1296. }
  1297. return node;
  1298. }
  1299. parseSubscript(base, startLoc, noCalls, state) {
  1300. const node = super.parseSubscript(base, startLoc, noCalls, state);
  1301. if (state.optionalChainMember) {
  1302. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  1303. node.type = node.type.substring(8);
  1304. }
  1305. if (state.stop) {
  1306. const chain = this.startNodeAtNode(node);
  1307. chain.expression = node;
  1308. return this.finishNode(chain, "ChainExpression");
  1309. }
  1310. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  1311. node.optional = false;
  1312. }
  1313. return node;
  1314. }
  1315. hasPropertyAsPrivateName(node) {
  1316. if (node.type === "ChainExpression") {
  1317. node = node.expression;
  1318. }
  1319. return super.hasPropertyAsPrivateName(node);
  1320. }
  1321. isObjectProperty(node) {
  1322. return node.type === "Property" && node.kind === "init" && !node.method;
  1323. }
  1324. isObjectMethod(node) {
  1325. return node.method || node.kind === "get" || node.kind === "set";
  1326. }
  1327. finishNodeAt(node, type, endLoc) {
  1328. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  1329. }
  1330. resetStartLocation(node, startLoc) {
  1331. super.resetStartLocation(node, startLoc);
  1332. toESTreeLocation(node);
  1333. }
  1334. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  1335. super.resetEndLocation(node, endLoc);
  1336. toESTreeLocation(node);
  1337. }
  1338. };
  1339. let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u0870-\u0887\u0889-\u088e\u08a0-\u08c9\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u1711\u171f-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4c\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7ca\ua7d0\ua7d1\ua7d3\ua7d5-\ua7d9\ua7f2-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc";
  1340. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0898-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f";
  1341. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1342. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1343. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1344. const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 68, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 4026, 582, 8634, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8936, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 757, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4153, 7, 221, 3, 5761, 15, 7472, 3104, 541, 1507, 4938, 6, 4191];
  1345. const astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 81, 2, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 9, 5351, 0, 7, 14, 13835, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 983, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1346. function isInAstralSet(code, set) {
  1347. let pos = 0x10000;
  1348. for (let i = 0, length = set.length; i < length; i += 2) {
  1349. pos += set[i];
  1350. if (pos > code) return false;
  1351. pos += set[i + 1];
  1352. if (pos >= code) return true;
  1353. }
  1354. return false;
  1355. }
  1356. function isIdentifierStart(code) {
  1357. if (code < 65) return code === 36;
  1358. if (code <= 90) return true;
  1359. if (code < 97) return code === 95;
  1360. if (code <= 122) return true;
  1361. if (code <= 0xffff) {
  1362. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1363. }
  1364. return isInAstralSet(code, astralIdentifierStartCodes);
  1365. }
  1366. function isIdentifierChar(code) {
  1367. if (code < 48) return code === 36;
  1368. if (code < 58) return true;
  1369. if (code < 65) return false;
  1370. if (code <= 90) return true;
  1371. if (code < 97) return code === 95;
  1372. if (code <= 122) return true;
  1373. if (code <= 0xffff) {
  1374. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1375. }
  1376. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1377. }
  1378. const reservedWords = {
  1379. 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"],
  1380. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1381. strictBind: ["eval", "arguments"]
  1382. };
  1383. const keywords = new Set(reservedWords.keyword);
  1384. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1385. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1386. function isReservedWord(word, inModule) {
  1387. return inModule && word === "await" || word === "enum";
  1388. }
  1389. function isStrictReservedWord(word, inModule) {
  1390. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1391. }
  1392. function isStrictBindOnlyReservedWord(word) {
  1393. return reservedWordsStrictBindSet.has(word);
  1394. }
  1395. function isStrictBindReservedWord(word, inModule) {
  1396. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1397. }
  1398. function isKeyword(word) {
  1399. return keywords.has(word);
  1400. }
  1401. function isIteratorStart(current, next, next2) {
  1402. return current === 64 && next === 64 && isIdentifierStart(next2);
  1403. }
  1404. 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"]);
  1405. function canBeReservedWord(word) {
  1406. return reservedWordLikeSet.has(word);
  1407. }
  1408. const SCOPE_OTHER = 0b000000000,
  1409. SCOPE_PROGRAM = 0b000000001,
  1410. SCOPE_FUNCTION = 0b000000010,
  1411. SCOPE_ARROW = 0b000000100,
  1412. SCOPE_SIMPLE_CATCH = 0b000001000,
  1413. SCOPE_SUPER = 0b000010000,
  1414. SCOPE_DIRECT_SUPER = 0b000100000,
  1415. SCOPE_CLASS = 0b001000000,
  1416. SCOPE_STATIC_BLOCK = 0b010000000,
  1417. SCOPE_TS_MODULE = 0b100000000,
  1418. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_STATIC_BLOCK | SCOPE_TS_MODULE;
  1419. const BIND_KIND_VALUE = 0b0000000000001,
  1420. BIND_KIND_TYPE = 0b0000000000010,
  1421. BIND_SCOPE_VAR = 0b0000000000100,
  1422. BIND_SCOPE_LEXICAL = 0b0000000001000,
  1423. BIND_SCOPE_FUNCTION = 0b0000000010000,
  1424. BIND_FLAGS_NONE = 0b00000001000000,
  1425. BIND_FLAGS_CLASS = 0b00000010000000,
  1426. BIND_FLAGS_TS_ENUM = 0b00000100000000,
  1427. BIND_FLAGS_TS_CONST_ENUM = 0b00001000000000,
  1428. BIND_FLAGS_TS_EXPORT_ONLY = 0b00010000000000,
  1429. BIND_FLAGS_FLOW_DECLARE_FN = 0b00100000000000,
  1430. BIND_FLAGS_TS_IMPORT = 0b01000000000000,
  1431. BIND_FLAGS_NO_LET_IN_LEXICAL = 0b10000000000000;
  1432. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1433. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1434. BIND_CATCH_PARAM = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1435. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1436. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1437. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1438. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1439. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1440. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1441. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1442. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1443. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1444. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1445. BIND_TS_TYPE_IMPORT = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_TS_IMPORT,
  1446. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1447. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1448. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1449. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1450. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1451. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1452. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1453. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1454. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1455. CLASS_ELEMENT_OTHER = 0;
  1456. class Scope {
  1457. constructor(flags) {
  1458. this.var = new Set();
  1459. this.lexical = new Set();
  1460. this.functions = new Set();
  1461. this.flags = flags;
  1462. }
  1463. }
  1464. class ScopeHandler {
  1465. constructor(parser, inModule) {
  1466. this.parser = void 0;
  1467. this.scopeStack = [];
  1468. this.inModule = void 0;
  1469. this.undefinedExports = new Map();
  1470. this.parser = parser;
  1471. this.inModule = inModule;
  1472. }
  1473. get inTopLevel() {
  1474. return (this.currentScope().flags & SCOPE_PROGRAM) > 0;
  1475. }
  1476. get inFunction() {
  1477. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  1478. }
  1479. get allowSuper() {
  1480. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  1481. }
  1482. get allowDirectSuper() {
  1483. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  1484. }
  1485. get inClass() {
  1486. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  1487. }
  1488. get inClassAndNotInNonArrowFunction() {
  1489. const flags = this.currentThisScopeFlags();
  1490. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  1491. }
  1492. get inStaticBlock() {
  1493. for (let i = this.scopeStack.length - 1;; i--) {
  1494. const {
  1495. flags
  1496. } = this.scopeStack[i];
  1497. if (flags & SCOPE_STATIC_BLOCK) {
  1498. return true;
  1499. }
  1500. if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
  1501. return false;
  1502. }
  1503. }
  1504. }
  1505. get inNonArrowFunction() {
  1506. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  1507. }
  1508. get treatFunctionsAsVar() {
  1509. return this.treatFunctionsAsVarInScope(this.currentScope());
  1510. }
  1511. createScope(flags) {
  1512. return new Scope(flags);
  1513. }
  1514. enter(flags) {
  1515. this.scopeStack.push(this.createScope(flags));
  1516. }
  1517. exit() {
  1518. const scope = this.scopeStack.pop();
  1519. return scope.flags;
  1520. }
  1521. treatFunctionsAsVarInScope(scope) {
  1522. return !!(scope.flags & (SCOPE_FUNCTION | SCOPE_STATIC_BLOCK) || !this.parser.inModule && scope.flags & SCOPE_PROGRAM);
  1523. }
  1524. declareName(name, bindingType, loc) {
  1525. let scope = this.currentScope();
  1526. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1527. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1528. if (bindingType & BIND_SCOPE_FUNCTION) {
  1529. scope.functions.add(name);
  1530. } else {
  1531. scope.lexical.add(name);
  1532. }
  1533. if (bindingType & BIND_SCOPE_LEXICAL) {
  1534. this.maybeExportDefined(scope, name);
  1535. }
  1536. } else if (bindingType & BIND_SCOPE_VAR) {
  1537. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1538. scope = this.scopeStack[i];
  1539. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1540. scope.var.add(name);
  1541. this.maybeExportDefined(scope, name);
  1542. if (scope.flags & SCOPE_VAR) break;
  1543. }
  1544. }
  1545. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  1546. this.undefinedExports.delete(name);
  1547. }
  1548. }
  1549. maybeExportDefined(scope, name) {
  1550. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  1551. this.undefinedExports.delete(name);
  1552. }
  1553. }
  1554. checkRedeclarationInScope(scope, name, bindingType, loc) {
  1555. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1556. this.parser.raise(Errors.VarRedeclaration, {
  1557. at: loc,
  1558. identifierName: name
  1559. });
  1560. }
  1561. }
  1562. isRedeclaredInScope(scope, name, bindingType) {
  1563. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1564. if (bindingType & BIND_SCOPE_LEXICAL) {
  1565. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  1566. }
  1567. if (bindingType & BIND_SCOPE_FUNCTION) {
  1568. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  1569. }
  1570. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  1571. }
  1572. checkLocalExport(id) {
  1573. const {
  1574. name
  1575. } = id;
  1576. const topLevelScope = this.scopeStack[0];
  1577. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  1578. this.undefinedExports.set(name, id.loc.start);
  1579. }
  1580. }
  1581. currentScope() {
  1582. return this.scopeStack[this.scopeStack.length - 1];
  1583. }
  1584. currentVarScopeFlags() {
  1585. for (let i = this.scopeStack.length - 1;; i--) {
  1586. const {
  1587. flags
  1588. } = this.scopeStack[i];
  1589. if (flags & SCOPE_VAR) {
  1590. return flags;
  1591. }
  1592. }
  1593. }
  1594. currentThisScopeFlags() {
  1595. for (let i = this.scopeStack.length - 1;; i--) {
  1596. const {
  1597. flags
  1598. } = this.scopeStack[i];
  1599. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  1600. return flags;
  1601. }
  1602. }
  1603. }
  1604. }
  1605. class FlowScope extends Scope {
  1606. constructor(...args) {
  1607. super(...args);
  1608. this.declareFunctions = new Set();
  1609. }
  1610. }
  1611. class FlowScopeHandler extends ScopeHandler {
  1612. createScope(flags) {
  1613. return new FlowScope(flags);
  1614. }
  1615. declareName(name, bindingType, loc) {
  1616. const scope = this.currentScope();
  1617. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1618. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1619. this.maybeExportDefined(scope, name);
  1620. scope.declareFunctions.add(name);
  1621. return;
  1622. }
  1623. super.declareName(name, bindingType, loc);
  1624. }
  1625. isRedeclaredInScope(scope, name, bindingType) {
  1626. if (super.isRedeclaredInScope(scope, name, bindingType)) return true;
  1627. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1628. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  1629. }
  1630. return false;
  1631. }
  1632. checkLocalExport(id) {
  1633. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1634. super.checkLocalExport(id);
  1635. }
  1636. }
  1637. }
  1638. class BaseParser {
  1639. constructor() {
  1640. this.sawUnambiguousESM = false;
  1641. this.ambiguousScriptDifferentAst = false;
  1642. }
  1643. hasPlugin(pluginConfig) {
  1644. if (typeof pluginConfig === "string") {
  1645. return this.plugins.has(pluginConfig);
  1646. } else {
  1647. const [pluginName, pluginOptions] = pluginConfig;
  1648. if (!this.hasPlugin(pluginName)) {
  1649. return false;
  1650. }
  1651. const actualOptions = this.plugins.get(pluginName);
  1652. for (const key of Object.keys(pluginOptions)) {
  1653. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  1654. return false;
  1655. }
  1656. }
  1657. return true;
  1658. }
  1659. }
  1660. getPluginOption(plugin, name) {
  1661. var _this$plugins$get;
  1662. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  1663. }
  1664. }
  1665. function setTrailingComments(node, comments) {
  1666. if (node.trailingComments === undefined) {
  1667. node.trailingComments = comments;
  1668. } else {
  1669. node.trailingComments.unshift(...comments);
  1670. }
  1671. }
  1672. function setLeadingComments(node, comments) {
  1673. if (node.leadingComments === undefined) {
  1674. node.leadingComments = comments;
  1675. } else {
  1676. node.leadingComments.unshift(...comments);
  1677. }
  1678. }
  1679. function setInnerComments(node, comments) {
  1680. if (node.innerComments === undefined) {
  1681. node.innerComments = comments;
  1682. } else {
  1683. node.innerComments.unshift(...comments);
  1684. }
  1685. }
  1686. function adjustInnerComments(node, elements, commentWS) {
  1687. let lastElement = null;
  1688. let i = elements.length;
  1689. while (lastElement === null && i > 0) {
  1690. lastElement = elements[--i];
  1691. }
  1692. if (lastElement === null || lastElement.start > commentWS.start) {
  1693. setInnerComments(node, commentWS.comments);
  1694. } else {
  1695. setTrailingComments(lastElement, commentWS.comments);
  1696. }
  1697. }
  1698. class CommentsParser extends BaseParser {
  1699. addComment(comment) {
  1700. if (this.filename) comment.loc.filename = this.filename;
  1701. this.state.comments.push(comment);
  1702. }
  1703. processComment(node) {
  1704. const {
  1705. commentStack
  1706. } = this.state;
  1707. const commentStackLength = commentStack.length;
  1708. if (commentStackLength === 0) return;
  1709. let i = commentStackLength - 1;
  1710. const lastCommentWS = commentStack[i];
  1711. if (lastCommentWS.start === node.end) {
  1712. lastCommentWS.leadingNode = node;
  1713. i--;
  1714. }
  1715. const {
  1716. start: nodeStart
  1717. } = node;
  1718. for (; i >= 0; i--) {
  1719. const commentWS = commentStack[i];
  1720. const commentEnd = commentWS.end;
  1721. if (commentEnd > nodeStart) {
  1722. commentWS.containingNode = node;
  1723. this.finalizeComment(commentWS);
  1724. commentStack.splice(i, 1);
  1725. } else {
  1726. if (commentEnd === nodeStart) {
  1727. commentWS.trailingNode = node;
  1728. }
  1729. break;
  1730. }
  1731. }
  1732. }
  1733. finalizeComment(commentWS) {
  1734. const {
  1735. comments
  1736. } = commentWS;
  1737. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  1738. if (commentWS.leadingNode !== null) {
  1739. setTrailingComments(commentWS.leadingNode, comments);
  1740. }
  1741. if (commentWS.trailingNode !== null) {
  1742. setLeadingComments(commentWS.trailingNode, comments);
  1743. }
  1744. } else {
  1745. const {
  1746. containingNode: node,
  1747. start: commentStart
  1748. } = commentWS;
  1749. if (this.input.charCodeAt(commentStart - 1) === 44) {
  1750. switch (node.type) {
  1751. case "ObjectExpression":
  1752. case "ObjectPattern":
  1753. case "RecordExpression":
  1754. adjustInnerComments(node, node.properties, commentWS);
  1755. break;
  1756. case "CallExpression":
  1757. case "OptionalCallExpression":
  1758. adjustInnerComments(node, node.arguments, commentWS);
  1759. break;
  1760. case "FunctionDeclaration":
  1761. case "FunctionExpression":
  1762. case "ArrowFunctionExpression":
  1763. case "ObjectMethod":
  1764. case "ClassMethod":
  1765. case "ClassPrivateMethod":
  1766. adjustInnerComments(node, node.params, commentWS);
  1767. break;
  1768. case "ArrayExpression":
  1769. case "ArrayPattern":
  1770. case "TupleExpression":
  1771. adjustInnerComments(node, node.elements, commentWS);
  1772. break;
  1773. case "ExportNamedDeclaration":
  1774. case "ImportDeclaration":
  1775. adjustInnerComments(node, node.specifiers, commentWS);
  1776. break;
  1777. default:
  1778. {
  1779. setInnerComments(node, comments);
  1780. }
  1781. }
  1782. } else {
  1783. setInnerComments(node, comments);
  1784. }
  1785. }
  1786. }
  1787. finalizeRemainingComments() {
  1788. const {
  1789. commentStack
  1790. } = this.state;
  1791. for (let i = commentStack.length - 1; i >= 0; i--) {
  1792. this.finalizeComment(commentStack[i]);
  1793. }
  1794. this.state.commentStack = [];
  1795. }
  1796. resetPreviousNodeTrailingComments(node) {
  1797. const {
  1798. commentStack
  1799. } = this.state;
  1800. const {
  1801. length
  1802. } = commentStack;
  1803. if (length === 0) return;
  1804. const commentWS = commentStack[length - 1];
  1805. if (commentWS.leadingNode === node) {
  1806. commentWS.leadingNode = null;
  1807. }
  1808. }
  1809. takeSurroundingComments(node, start, end) {
  1810. const {
  1811. commentStack
  1812. } = this.state;
  1813. const commentStackLength = commentStack.length;
  1814. if (commentStackLength === 0) return;
  1815. let i = commentStackLength - 1;
  1816. for (; i >= 0; i--) {
  1817. const commentWS = commentStack[i];
  1818. const commentEnd = commentWS.end;
  1819. const commentStart = commentWS.start;
  1820. if (commentStart === end) {
  1821. commentWS.leadingNode = node;
  1822. } else if (commentEnd === start) {
  1823. commentWS.trailingNode = node;
  1824. } else if (commentEnd < start) {
  1825. break;
  1826. }
  1827. }
  1828. }
  1829. }
  1830. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  1831. const lineBreakG = new RegExp(lineBreak.source, "g");
  1832. function isNewLine(code) {
  1833. switch (code) {
  1834. case 10:
  1835. case 13:
  1836. case 8232:
  1837. case 8233:
  1838. return true;
  1839. default:
  1840. return false;
  1841. }
  1842. }
  1843. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  1844. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y;
  1845. const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
  1846. function isWhitespace(code) {
  1847. switch (code) {
  1848. case 0x0009:
  1849. case 0x000b:
  1850. case 0x000c:
  1851. case 32:
  1852. case 160:
  1853. case 5760:
  1854. case 0x2000:
  1855. case 0x2001:
  1856. case 0x2002:
  1857. case 0x2003:
  1858. case 0x2004:
  1859. case 0x2005:
  1860. case 0x2006:
  1861. case 0x2007:
  1862. case 0x2008:
  1863. case 0x2009:
  1864. case 0x200a:
  1865. case 0x202f:
  1866. case 0x205f:
  1867. case 0x3000:
  1868. case 0xfeff:
  1869. return true;
  1870. default:
  1871. return false;
  1872. }
  1873. }
  1874. class State {
  1875. constructor() {
  1876. this.strict = void 0;
  1877. this.curLine = void 0;
  1878. this.lineStart = void 0;
  1879. this.startLoc = void 0;
  1880. this.endLoc = void 0;
  1881. this.errors = [];
  1882. this.potentialArrowAt = -1;
  1883. this.noArrowAt = [];
  1884. this.noArrowParamsConversionAt = [];
  1885. this.maybeInArrowParameters = false;
  1886. this.inType = false;
  1887. this.noAnonFunctionType = false;
  1888. this.hasFlowComment = false;
  1889. this.isAmbientContext = false;
  1890. this.inAbstractClass = false;
  1891. this.inDisallowConditionalTypesContext = false;
  1892. this.topicContext = {
  1893. maxNumOfResolvableTopics: 0,
  1894. maxTopicIndex: null
  1895. };
  1896. this.soloAwait = false;
  1897. this.inFSharpPipelineDirectBody = false;
  1898. this.labels = [];
  1899. this.comments = [];
  1900. this.commentStack = [];
  1901. this.pos = 0;
  1902. this.type = 137;
  1903. this.value = null;
  1904. this.start = 0;
  1905. this.end = 0;
  1906. this.lastTokEndLoc = null;
  1907. this.lastTokStartLoc = null;
  1908. this.lastTokStart = 0;
  1909. this.context = [types.brace];
  1910. this.canStartJSXElement = true;
  1911. this.containsEsc = false;
  1912. this.firstInvalidTemplateEscapePos = null;
  1913. this.strictErrors = new Map();
  1914. this.tokensLength = 0;
  1915. }
  1916. init({
  1917. strictMode,
  1918. sourceType,
  1919. startLine,
  1920. startColumn
  1921. }) {
  1922. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  1923. this.curLine = startLine;
  1924. this.lineStart = -startColumn;
  1925. this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
  1926. }
  1927. curPosition() {
  1928. return new Position(this.curLine, this.pos - this.lineStart, this.pos);
  1929. }
  1930. clone(skipArrays) {
  1931. const state = new State();
  1932. const keys = Object.keys(this);
  1933. for (let i = 0, length = keys.length; i < length; i++) {
  1934. const key = keys[i];
  1935. let val = this[key];
  1936. if (!skipArrays && Array.isArray(val)) {
  1937. val = val.slice();
  1938. }
  1939. state[key] = val;
  1940. }
  1941. return state;
  1942. }
  1943. }
  1944. var _isDigit = function isDigit(code) {
  1945. return code >= 48 && code <= 57;
  1946. };
  1947. const forbiddenNumericSeparatorSiblings = {
  1948. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  1949. hex: new Set([46, 88, 95, 120])
  1950. };
  1951. const isAllowedNumericSeparatorSibling = {
  1952. bin: ch => ch === 48 || ch === 49,
  1953. oct: ch => ch >= 48 && ch <= 55,
  1954. dec: ch => ch >= 48 && ch <= 57,
  1955. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  1956. };
  1957. function readStringContents(type, input, pos, lineStart, curLine, errors) {
  1958. const initialPos = pos;
  1959. const initialLineStart = lineStart;
  1960. const initialCurLine = curLine;
  1961. let out = "";
  1962. let firstInvalidLoc = null;
  1963. let chunkStart = pos;
  1964. const {
  1965. length
  1966. } = input;
  1967. for (;;) {
  1968. if (pos >= length) {
  1969. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1970. out += input.slice(chunkStart, pos);
  1971. break;
  1972. }
  1973. const ch = input.charCodeAt(pos);
  1974. if (isStringEnd(type, ch, input, pos)) {
  1975. out += input.slice(chunkStart, pos);
  1976. break;
  1977. }
  1978. if (ch === 92) {
  1979. out += input.slice(chunkStart, pos);
  1980. const res = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors);
  1981. if (res.ch === null && !firstInvalidLoc) {
  1982. firstInvalidLoc = {
  1983. pos,
  1984. lineStart,
  1985. curLine
  1986. };
  1987. } else {
  1988. out += res.ch;
  1989. }
  1990. ({
  1991. pos,
  1992. lineStart,
  1993. curLine
  1994. } = res);
  1995. chunkStart = pos;
  1996. } else if (ch === 8232 || ch === 8233) {
  1997. ++pos;
  1998. ++curLine;
  1999. lineStart = pos;
  2000. } else if (ch === 10 || ch === 13) {
  2001. if (type === "template") {
  2002. out += input.slice(chunkStart, pos) + "\n";
  2003. ++pos;
  2004. if (ch === 13 && input.charCodeAt(pos) === 10) {
  2005. ++pos;
  2006. }
  2007. ++curLine;
  2008. chunkStart = lineStart = pos;
  2009. } else {
  2010. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  2011. }
  2012. } else {
  2013. ++pos;
  2014. }
  2015. }
  2016. return {
  2017. pos,
  2018. str: out,
  2019. firstInvalidLoc,
  2020. lineStart,
  2021. curLine,
  2022. containsInvalid: !!firstInvalidLoc
  2023. };
  2024. }
  2025. function isStringEnd(type, ch, input, pos) {
  2026. if (type === "template") {
  2027. return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
  2028. }
  2029. return ch === (type === "double" ? 34 : 39);
  2030. }
  2031. function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
  2032. const throwOnInvalid = !inTemplate;
  2033. pos++;
  2034. const res = ch => ({
  2035. pos,
  2036. ch,
  2037. lineStart,
  2038. curLine
  2039. });
  2040. const ch = input.charCodeAt(pos++);
  2041. switch (ch) {
  2042. case 110:
  2043. return res("\n");
  2044. case 114:
  2045. return res("\r");
  2046. case 120:
  2047. {
  2048. let code;
  2049. ({
  2050. code,
  2051. pos
  2052. } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
  2053. return res(code === null ? null : String.fromCharCode(code));
  2054. }
  2055. case 117:
  2056. {
  2057. let code;
  2058. ({
  2059. code,
  2060. pos
  2061. } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
  2062. return res(code === null ? null : String.fromCodePoint(code));
  2063. }
  2064. case 116:
  2065. return res("\t");
  2066. case 98:
  2067. return res("\b");
  2068. case 118:
  2069. return res("\u000b");
  2070. case 102:
  2071. return res("\f");
  2072. case 13:
  2073. if (input.charCodeAt(pos) === 10) {
  2074. ++pos;
  2075. }
  2076. case 10:
  2077. lineStart = pos;
  2078. ++curLine;
  2079. case 8232:
  2080. case 8233:
  2081. return res("");
  2082. case 56:
  2083. case 57:
  2084. if (inTemplate) {
  2085. return res(null);
  2086. } else {
  2087. errors.strictNumericEscape(pos - 1, lineStart, curLine);
  2088. }
  2089. default:
  2090. if (ch >= 48 && ch <= 55) {
  2091. const startPos = pos - 1;
  2092. const match = input.slice(startPos, pos + 2).match(/^[0-7]+/);
  2093. let octalStr = match[0];
  2094. let octal = parseInt(octalStr, 8);
  2095. if (octal > 255) {
  2096. octalStr = octalStr.slice(0, -1);
  2097. octal = parseInt(octalStr, 8);
  2098. }
  2099. pos += octalStr.length - 1;
  2100. const next = input.charCodeAt(pos);
  2101. if (octalStr !== "0" || next === 56 || next === 57) {
  2102. if (inTemplate) {
  2103. return res(null);
  2104. } else {
  2105. errors.strictNumericEscape(startPos, lineStart, curLine);
  2106. }
  2107. }
  2108. return res(String.fromCharCode(octal));
  2109. }
  2110. return res(String.fromCharCode(ch));
  2111. }
  2112. }
  2113. function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
  2114. const initialPos = pos;
  2115. let n;
  2116. ({
  2117. n,
  2118. pos
  2119. } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors, !throwOnInvalid));
  2120. if (n === null) {
  2121. if (throwOnInvalid) {
  2122. errors.invalidEscapeSequence(initialPos, lineStart, curLine);
  2123. } else {
  2124. pos = initialPos - 1;
  2125. }
  2126. }
  2127. return {
  2128. code: n,
  2129. pos
  2130. };
  2131. }
  2132. function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors, bailOnError) {
  2133. const start = pos;
  2134. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  2135. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  2136. let invalid = false;
  2137. let total = 0;
  2138. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  2139. const code = input.charCodeAt(pos);
  2140. let val;
  2141. if (code === 95 && allowNumSeparator !== "bail") {
  2142. const prev = input.charCodeAt(pos - 1);
  2143. const next = input.charCodeAt(pos + 1);
  2144. if (!allowNumSeparator) {
  2145. if (bailOnError) return {
  2146. n: null,
  2147. pos
  2148. };
  2149. errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
  2150. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  2151. if (bailOnError) return {
  2152. n: null,
  2153. pos
  2154. };
  2155. errors.unexpectedNumericSeparator(pos, lineStart, curLine);
  2156. }
  2157. ++pos;
  2158. continue;
  2159. }
  2160. if (code >= 97) {
  2161. val = code - 97 + 10;
  2162. } else if (code >= 65) {
  2163. val = code - 65 + 10;
  2164. } else if (_isDigit(code)) {
  2165. val = code - 48;
  2166. } else {
  2167. val = Infinity;
  2168. }
  2169. if (val >= radix) {
  2170. if (val <= 9 && bailOnError) {
  2171. return {
  2172. n: null,
  2173. pos
  2174. };
  2175. } else if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
  2176. val = 0;
  2177. } else if (forceLen) {
  2178. val = 0;
  2179. invalid = true;
  2180. } else {
  2181. break;
  2182. }
  2183. }
  2184. ++pos;
  2185. total = total * radix + val;
  2186. }
  2187. if (pos === start || len != null && pos - start !== len || invalid) {
  2188. return {
  2189. n: null,
  2190. pos
  2191. };
  2192. }
  2193. return {
  2194. n: total,
  2195. pos
  2196. };
  2197. }
  2198. function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
  2199. const ch = input.charCodeAt(pos);
  2200. let code;
  2201. if (ch === 123) {
  2202. ++pos;
  2203. ({
  2204. code,
  2205. pos
  2206. } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
  2207. ++pos;
  2208. if (code !== null && code > 0x10ffff) {
  2209. if (throwOnInvalid) {
  2210. errors.invalidCodePoint(pos, lineStart, curLine);
  2211. } else {
  2212. return {
  2213. code: null,
  2214. pos
  2215. };
  2216. }
  2217. }
  2218. } else {
  2219. ({
  2220. code,
  2221. pos
  2222. } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
  2223. }
  2224. return {
  2225. code,
  2226. pos
  2227. };
  2228. }
  2229. const _excluded = ["at"],
  2230. _excluded2 = ["at"];
  2231. function buildPosition(pos, lineStart, curLine) {
  2232. return new Position(curLine, pos - lineStart, pos);
  2233. }
  2234. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
  2235. class Token {
  2236. constructor(state) {
  2237. this.type = state.type;
  2238. this.value = state.value;
  2239. this.start = state.start;
  2240. this.end = state.end;
  2241. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  2242. }
  2243. }
  2244. class Tokenizer extends CommentsParser {
  2245. constructor(options, input) {
  2246. super();
  2247. this.isLookahead = void 0;
  2248. this.tokens = [];
  2249. this.errorHandlers_readInt = {
  2250. invalidDigit: (pos, lineStart, curLine, radix) => {
  2251. if (!this.options.errorRecovery) return false;
  2252. this.raise(Errors.InvalidDigit, {
  2253. at: buildPosition(pos, lineStart, curLine),
  2254. radix
  2255. });
  2256. return true;
  2257. },
  2258. numericSeparatorInEscapeSequence: this.errorBuilder(Errors.NumericSeparatorInEscapeSequence),
  2259. unexpectedNumericSeparator: this.errorBuilder(Errors.UnexpectedNumericSeparator)
  2260. };
  2261. this.errorHandlers_readCodePoint = Object.assign({}, this.errorHandlers_readInt, {
  2262. invalidEscapeSequence: this.errorBuilder(Errors.InvalidEscapeSequence),
  2263. invalidCodePoint: this.errorBuilder(Errors.InvalidCodePoint)
  2264. });
  2265. this.errorHandlers_readStringContents_string = Object.assign({}, this.errorHandlers_readCodePoint, {
  2266. strictNumericEscape: (pos, lineStart, curLine) => {
  2267. this.recordStrictModeErrors(Errors.StrictNumericEscape, {
  2268. at: buildPosition(pos, lineStart, curLine)
  2269. });
  2270. },
  2271. unterminated: (pos, lineStart, curLine) => {
  2272. throw this.raise(Errors.UnterminatedString, {
  2273. at: buildPosition(pos - 1, lineStart, curLine)
  2274. });
  2275. }
  2276. });
  2277. this.errorHandlers_readStringContents_template = Object.assign({}, this.errorHandlers_readCodePoint, {
  2278. strictNumericEscape: this.errorBuilder(Errors.StrictNumericEscape),
  2279. unterminated: (pos, lineStart, curLine) => {
  2280. throw this.raise(Errors.UnterminatedTemplate, {
  2281. at: buildPosition(pos, lineStart, curLine)
  2282. });
  2283. }
  2284. });
  2285. this.state = new State();
  2286. this.state.init(options);
  2287. this.input = input;
  2288. this.length = input.length;
  2289. this.isLookahead = false;
  2290. }
  2291. pushToken(token) {
  2292. this.tokens.length = this.state.tokensLength;
  2293. this.tokens.push(token);
  2294. ++this.state.tokensLength;
  2295. }
  2296. next() {
  2297. this.checkKeywordEscapes();
  2298. if (this.options.tokens) {
  2299. this.pushToken(new Token(this.state));
  2300. }
  2301. this.state.lastTokStart = this.state.start;
  2302. this.state.lastTokEndLoc = this.state.endLoc;
  2303. this.state.lastTokStartLoc = this.state.startLoc;
  2304. this.nextToken();
  2305. }
  2306. eat(type) {
  2307. if (this.match(type)) {
  2308. this.next();
  2309. return true;
  2310. } else {
  2311. return false;
  2312. }
  2313. }
  2314. match(type) {
  2315. return this.state.type === type;
  2316. }
  2317. createLookaheadState(state) {
  2318. return {
  2319. pos: state.pos,
  2320. value: null,
  2321. type: state.type,
  2322. start: state.start,
  2323. end: state.end,
  2324. context: [this.curContext()],
  2325. inType: state.inType,
  2326. startLoc: state.startLoc,
  2327. lastTokEndLoc: state.lastTokEndLoc,
  2328. curLine: state.curLine,
  2329. lineStart: state.lineStart,
  2330. curPosition: state.curPosition
  2331. };
  2332. }
  2333. lookahead() {
  2334. const old = this.state;
  2335. this.state = this.createLookaheadState(old);
  2336. this.isLookahead = true;
  2337. this.nextToken();
  2338. this.isLookahead = false;
  2339. const curr = this.state;
  2340. this.state = old;
  2341. return curr;
  2342. }
  2343. nextTokenStart() {
  2344. return this.nextTokenStartSince(this.state.pos);
  2345. }
  2346. nextTokenStartSince(pos) {
  2347. skipWhiteSpace.lastIndex = pos;
  2348. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  2349. }
  2350. lookaheadCharCode() {
  2351. return this.input.charCodeAt(this.nextTokenStart());
  2352. }
  2353. codePointAtPos(pos) {
  2354. let cp = this.input.charCodeAt(pos);
  2355. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  2356. const trail = this.input.charCodeAt(pos);
  2357. if ((trail & 0xfc00) === 0xdc00) {
  2358. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  2359. }
  2360. }
  2361. return cp;
  2362. }
  2363. setStrict(strict) {
  2364. this.state.strict = strict;
  2365. if (strict) {
  2366. this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, {
  2367. at
  2368. }));
  2369. this.state.strictErrors.clear();
  2370. }
  2371. }
  2372. curContext() {
  2373. return this.state.context[this.state.context.length - 1];
  2374. }
  2375. nextToken() {
  2376. this.skipSpace();
  2377. this.state.start = this.state.pos;
  2378. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  2379. if (this.state.pos >= this.length) {
  2380. this.finishToken(137);
  2381. return;
  2382. }
  2383. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  2384. }
  2385. skipBlockComment(commentEnd) {
  2386. let startLoc;
  2387. if (!this.isLookahead) startLoc = this.state.curPosition();
  2388. const start = this.state.pos;
  2389. const end = this.input.indexOf(commentEnd, start + 2);
  2390. if (end === -1) {
  2391. throw this.raise(Errors.UnterminatedComment, {
  2392. at: this.state.curPosition()
  2393. });
  2394. }
  2395. this.state.pos = end + commentEnd.length;
  2396. lineBreakG.lastIndex = start + 2;
  2397. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  2398. ++this.state.curLine;
  2399. this.state.lineStart = lineBreakG.lastIndex;
  2400. }
  2401. if (this.isLookahead) return;
  2402. const comment = {
  2403. type: "CommentBlock",
  2404. value: this.input.slice(start + 2, end),
  2405. start,
  2406. end: end + commentEnd.length,
  2407. loc: new SourceLocation(startLoc, this.state.curPosition())
  2408. };
  2409. if (this.options.tokens) this.pushToken(comment);
  2410. return comment;
  2411. }
  2412. skipLineComment(startSkip) {
  2413. const start = this.state.pos;
  2414. let startLoc;
  2415. if (!this.isLookahead) startLoc = this.state.curPosition();
  2416. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  2417. if (this.state.pos < this.length) {
  2418. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2419. ch = this.input.charCodeAt(this.state.pos);
  2420. }
  2421. }
  2422. if (this.isLookahead) return;
  2423. const end = this.state.pos;
  2424. const value = this.input.slice(start + startSkip, end);
  2425. const comment = {
  2426. type: "CommentLine",
  2427. value,
  2428. start,
  2429. end,
  2430. loc: new SourceLocation(startLoc, this.state.curPosition())
  2431. };
  2432. if (this.options.tokens) this.pushToken(comment);
  2433. return comment;
  2434. }
  2435. skipSpace() {
  2436. const spaceStart = this.state.pos;
  2437. const comments = [];
  2438. loop: while (this.state.pos < this.length) {
  2439. const ch = this.input.charCodeAt(this.state.pos);
  2440. switch (ch) {
  2441. case 32:
  2442. case 160:
  2443. case 9:
  2444. ++this.state.pos;
  2445. break;
  2446. case 13:
  2447. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  2448. ++this.state.pos;
  2449. }
  2450. case 10:
  2451. case 8232:
  2452. case 8233:
  2453. ++this.state.pos;
  2454. ++this.state.curLine;
  2455. this.state.lineStart = this.state.pos;
  2456. break;
  2457. case 47:
  2458. switch (this.input.charCodeAt(this.state.pos + 1)) {
  2459. case 42:
  2460. {
  2461. const comment = this.skipBlockComment("*/");
  2462. if (comment !== undefined) {
  2463. this.addComment(comment);
  2464. if (this.options.attachComment) comments.push(comment);
  2465. }
  2466. break;
  2467. }
  2468. case 47:
  2469. {
  2470. const comment = this.skipLineComment(2);
  2471. if (comment !== undefined) {
  2472. this.addComment(comment);
  2473. if (this.options.attachComment) comments.push(comment);
  2474. }
  2475. break;
  2476. }
  2477. default:
  2478. break loop;
  2479. }
  2480. break;
  2481. default:
  2482. if (isWhitespace(ch)) {
  2483. ++this.state.pos;
  2484. } else if (ch === 45 && !this.inModule && this.options.annexB) {
  2485. const pos = this.state.pos;
  2486. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  2487. const comment = this.skipLineComment(3);
  2488. if (comment !== undefined) {
  2489. this.addComment(comment);
  2490. if (this.options.attachComment) comments.push(comment);
  2491. }
  2492. } else {
  2493. break loop;
  2494. }
  2495. } else if (ch === 60 && !this.inModule && this.options.annexB) {
  2496. const pos = this.state.pos;
  2497. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  2498. const comment = this.skipLineComment(4);
  2499. if (comment !== undefined) {
  2500. this.addComment(comment);
  2501. if (this.options.attachComment) comments.push(comment);
  2502. }
  2503. } else {
  2504. break loop;
  2505. }
  2506. } else {
  2507. break loop;
  2508. }
  2509. }
  2510. }
  2511. if (comments.length > 0) {
  2512. const end = this.state.pos;
  2513. const commentWhitespace = {
  2514. start: spaceStart,
  2515. end,
  2516. comments,
  2517. leadingNode: null,
  2518. trailingNode: null,
  2519. containingNode: null
  2520. };
  2521. this.state.commentStack.push(commentWhitespace);
  2522. }
  2523. }
  2524. finishToken(type, val) {
  2525. this.state.end = this.state.pos;
  2526. this.state.endLoc = this.state.curPosition();
  2527. const prevType = this.state.type;
  2528. this.state.type = type;
  2529. this.state.value = val;
  2530. if (!this.isLookahead) {
  2531. this.updateContext(prevType);
  2532. }
  2533. }
  2534. replaceToken(type) {
  2535. this.state.type = type;
  2536. this.updateContext();
  2537. }
  2538. readToken_numberSign() {
  2539. if (this.state.pos === 0 && this.readToken_interpreter()) {
  2540. return;
  2541. }
  2542. const nextPos = this.state.pos + 1;
  2543. const next = this.codePointAtPos(nextPos);
  2544. if (next >= 48 && next <= 57) {
  2545. throw this.raise(Errors.UnexpectedDigitAfterHash, {
  2546. at: this.state.curPosition()
  2547. });
  2548. }
  2549. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  2550. this.expectPlugin("recordAndTuple");
  2551. if (this.getPluginOption("recordAndTuple", "syntaxType") === "bar") {
  2552. throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, {
  2553. at: this.state.curPosition()
  2554. });
  2555. }
  2556. this.state.pos += 2;
  2557. if (next === 123) {
  2558. this.finishToken(7);
  2559. } else {
  2560. this.finishToken(1);
  2561. }
  2562. } else if (isIdentifierStart(next)) {
  2563. ++this.state.pos;
  2564. this.finishToken(136, this.readWord1(next));
  2565. } else if (next === 92) {
  2566. ++this.state.pos;
  2567. this.finishToken(136, this.readWord1());
  2568. } else {
  2569. this.finishOp(27, 1);
  2570. }
  2571. }
  2572. readToken_dot() {
  2573. const next = this.input.charCodeAt(this.state.pos + 1);
  2574. if (next >= 48 && next <= 57) {
  2575. this.readNumber(true);
  2576. return;
  2577. }
  2578. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  2579. this.state.pos += 3;
  2580. this.finishToken(21);
  2581. } else {
  2582. ++this.state.pos;
  2583. this.finishToken(16);
  2584. }
  2585. }
  2586. readToken_slash() {
  2587. const next = this.input.charCodeAt(this.state.pos + 1);
  2588. if (next === 61) {
  2589. this.finishOp(31, 2);
  2590. } else {
  2591. this.finishOp(56, 1);
  2592. }
  2593. }
  2594. readToken_interpreter() {
  2595. if (this.state.pos !== 0 || this.length < 2) return false;
  2596. let ch = this.input.charCodeAt(this.state.pos + 1);
  2597. if (ch !== 33) return false;
  2598. const start = this.state.pos;
  2599. this.state.pos += 1;
  2600. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2601. ch = this.input.charCodeAt(this.state.pos);
  2602. }
  2603. const value = this.input.slice(start + 2, this.state.pos);
  2604. this.finishToken(28, value);
  2605. return true;
  2606. }
  2607. readToken_mult_modulo(code) {
  2608. let type = code === 42 ? 55 : 54;
  2609. let width = 1;
  2610. let next = this.input.charCodeAt(this.state.pos + 1);
  2611. if (code === 42 && next === 42) {
  2612. width++;
  2613. next = this.input.charCodeAt(this.state.pos + 2);
  2614. type = 57;
  2615. }
  2616. if (next === 61 && !this.state.inType) {
  2617. width++;
  2618. type = code === 37 ? 33 : 30;
  2619. }
  2620. this.finishOp(type, width);
  2621. }
  2622. readToken_pipe_amp(code) {
  2623. const next = this.input.charCodeAt(this.state.pos + 1);
  2624. if (next === code) {
  2625. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  2626. this.finishOp(30, 3);
  2627. } else {
  2628. this.finishOp(code === 124 ? 41 : 42, 2);
  2629. }
  2630. return;
  2631. }
  2632. if (code === 124) {
  2633. if (next === 62) {
  2634. this.finishOp(39, 2);
  2635. return;
  2636. }
  2637. if (this.hasPlugin("recordAndTuple") && next === 125) {
  2638. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2639. throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, {
  2640. at: this.state.curPosition()
  2641. });
  2642. }
  2643. this.state.pos += 2;
  2644. this.finishToken(9);
  2645. return;
  2646. }
  2647. if (this.hasPlugin("recordAndTuple") && next === 93) {
  2648. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2649. throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, {
  2650. at: this.state.curPosition()
  2651. });
  2652. }
  2653. this.state.pos += 2;
  2654. this.finishToken(4);
  2655. return;
  2656. }
  2657. }
  2658. if (next === 61) {
  2659. this.finishOp(30, 2);
  2660. return;
  2661. }
  2662. this.finishOp(code === 124 ? 43 : 45, 1);
  2663. }
  2664. readToken_caret() {
  2665. const next = this.input.charCodeAt(this.state.pos + 1);
  2666. if (next === 61 && !this.state.inType) {
  2667. this.finishOp(32, 2);
  2668. } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
  2669. proposal: "hack",
  2670. topicToken: "^^"
  2671. }])) {
  2672. this.finishOp(37, 2);
  2673. const lookaheadCh = this.input.codePointAt(this.state.pos);
  2674. if (lookaheadCh === 94) {
  2675. this.unexpected();
  2676. }
  2677. } else {
  2678. this.finishOp(44, 1);
  2679. }
  2680. }
  2681. readToken_atSign() {
  2682. const next = this.input.charCodeAt(this.state.pos + 1);
  2683. if (next === 64 && this.hasPlugin(["pipelineOperator", {
  2684. proposal: "hack",
  2685. topicToken: "@@"
  2686. }])) {
  2687. this.finishOp(38, 2);
  2688. } else {
  2689. this.finishOp(26, 1);
  2690. }
  2691. }
  2692. readToken_plus_min(code) {
  2693. const next = this.input.charCodeAt(this.state.pos + 1);
  2694. if (next === code) {
  2695. this.finishOp(34, 2);
  2696. return;
  2697. }
  2698. if (next === 61) {
  2699. this.finishOp(30, 2);
  2700. } else {
  2701. this.finishOp(53, 1);
  2702. }
  2703. }
  2704. readToken_lt() {
  2705. const {
  2706. pos
  2707. } = this.state;
  2708. const next = this.input.charCodeAt(pos + 1);
  2709. if (next === 60) {
  2710. if (this.input.charCodeAt(pos + 2) === 61) {
  2711. this.finishOp(30, 3);
  2712. return;
  2713. }
  2714. this.finishOp(51, 2);
  2715. return;
  2716. }
  2717. if (next === 61) {
  2718. this.finishOp(49, 2);
  2719. return;
  2720. }
  2721. this.finishOp(47, 1);
  2722. }
  2723. readToken_gt() {
  2724. const {
  2725. pos
  2726. } = this.state;
  2727. const next = this.input.charCodeAt(pos + 1);
  2728. if (next === 62) {
  2729. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  2730. if (this.input.charCodeAt(pos + size) === 61) {
  2731. this.finishOp(30, size + 1);
  2732. return;
  2733. }
  2734. this.finishOp(52, size);
  2735. return;
  2736. }
  2737. if (next === 61) {
  2738. this.finishOp(49, 2);
  2739. return;
  2740. }
  2741. this.finishOp(48, 1);
  2742. }
  2743. readToken_eq_excl(code) {
  2744. const next = this.input.charCodeAt(this.state.pos + 1);
  2745. if (next === 61) {
  2746. this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  2747. return;
  2748. }
  2749. if (code === 61 && next === 62) {
  2750. this.state.pos += 2;
  2751. this.finishToken(19);
  2752. return;
  2753. }
  2754. this.finishOp(code === 61 ? 29 : 35, 1);
  2755. }
  2756. readToken_question() {
  2757. const next = this.input.charCodeAt(this.state.pos + 1);
  2758. const next2 = this.input.charCodeAt(this.state.pos + 2);
  2759. if (next === 63) {
  2760. if (next2 === 61) {
  2761. this.finishOp(30, 3);
  2762. } else {
  2763. this.finishOp(40, 2);
  2764. }
  2765. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  2766. this.state.pos += 2;
  2767. this.finishToken(18);
  2768. } else {
  2769. ++this.state.pos;
  2770. this.finishToken(17);
  2771. }
  2772. }
  2773. getTokenFromCode(code) {
  2774. switch (code) {
  2775. case 46:
  2776. this.readToken_dot();
  2777. return;
  2778. case 40:
  2779. ++this.state.pos;
  2780. this.finishToken(10);
  2781. return;
  2782. case 41:
  2783. ++this.state.pos;
  2784. this.finishToken(11);
  2785. return;
  2786. case 59:
  2787. ++this.state.pos;
  2788. this.finishToken(13);
  2789. return;
  2790. case 44:
  2791. ++this.state.pos;
  2792. this.finishToken(12);
  2793. return;
  2794. case 91:
  2795. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2796. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2797. throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, {
  2798. at: this.state.curPosition()
  2799. });
  2800. }
  2801. this.state.pos += 2;
  2802. this.finishToken(2);
  2803. } else {
  2804. ++this.state.pos;
  2805. this.finishToken(0);
  2806. }
  2807. return;
  2808. case 93:
  2809. ++this.state.pos;
  2810. this.finishToken(3);
  2811. return;
  2812. case 123:
  2813. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2814. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2815. throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, {
  2816. at: this.state.curPosition()
  2817. });
  2818. }
  2819. this.state.pos += 2;
  2820. this.finishToken(6);
  2821. } else {
  2822. ++this.state.pos;
  2823. this.finishToken(5);
  2824. }
  2825. return;
  2826. case 125:
  2827. ++this.state.pos;
  2828. this.finishToken(8);
  2829. return;
  2830. case 58:
  2831. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  2832. this.finishOp(15, 2);
  2833. } else {
  2834. ++this.state.pos;
  2835. this.finishToken(14);
  2836. }
  2837. return;
  2838. case 63:
  2839. this.readToken_question();
  2840. return;
  2841. case 96:
  2842. this.readTemplateToken();
  2843. return;
  2844. case 48:
  2845. {
  2846. const next = this.input.charCodeAt(this.state.pos + 1);
  2847. if (next === 120 || next === 88) {
  2848. this.readRadixNumber(16);
  2849. return;
  2850. }
  2851. if (next === 111 || next === 79) {
  2852. this.readRadixNumber(8);
  2853. return;
  2854. }
  2855. if (next === 98 || next === 66) {
  2856. this.readRadixNumber(2);
  2857. return;
  2858. }
  2859. }
  2860. case 49:
  2861. case 50:
  2862. case 51:
  2863. case 52:
  2864. case 53:
  2865. case 54:
  2866. case 55:
  2867. case 56:
  2868. case 57:
  2869. this.readNumber(false);
  2870. return;
  2871. case 34:
  2872. case 39:
  2873. this.readString(code);
  2874. return;
  2875. case 47:
  2876. this.readToken_slash();
  2877. return;
  2878. case 37:
  2879. case 42:
  2880. this.readToken_mult_modulo(code);
  2881. return;
  2882. case 124:
  2883. case 38:
  2884. this.readToken_pipe_amp(code);
  2885. return;
  2886. case 94:
  2887. this.readToken_caret();
  2888. return;
  2889. case 43:
  2890. case 45:
  2891. this.readToken_plus_min(code);
  2892. return;
  2893. case 60:
  2894. this.readToken_lt();
  2895. return;
  2896. case 62:
  2897. this.readToken_gt();
  2898. return;
  2899. case 61:
  2900. case 33:
  2901. this.readToken_eq_excl(code);
  2902. return;
  2903. case 126:
  2904. this.finishOp(36, 1);
  2905. return;
  2906. case 64:
  2907. this.readToken_atSign();
  2908. return;
  2909. case 35:
  2910. this.readToken_numberSign();
  2911. return;
  2912. case 92:
  2913. this.readWord();
  2914. return;
  2915. default:
  2916. if (isIdentifierStart(code)) {
  2917. this.readWord(code);
  2918. return;
  2919. }
  2920. }
  2921. throw this.raise(Errors.InvalidOrUnexpectedToken, {
  2922. at: this.state.curPosition(),
  2923. unexpected: String.fromCodePoint(code)
  2924. });
  2925. }
  2926. finishOp(type, size) {
  2927. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2928. this.state.pos += size;
  2929. this.finishToken(type, str);
  2930. }
  2931. readRegexp() {
  2932. const startLoc = this.state.startLoc;
  2933. const start = this.state.start + 1;
  2934. let escaped, inClass;
  2935. let {
  2936. pos
  2937. } = this.state;
  2938. for (;; ++pos) {
  2939. if (pos >= this.length) {
  2940. throw this.raise(Errors.UnterminatedRegExp, {
  2941. at: createPositionWithColumnOffset(startLoc, 1)
  2942. });
  2943. }
  2944. const ch = this.input.charCodeAt(pos);
  2945. if (isNewLine(ch)) {
  2946. throw this.raise(Errors.UnterminatedRegExp, {
  2947. at: createPositionWithColumnOffset(startLoc, 1)
  2948. });
  2949. }
  2950. if (escaped) {
  2951. escaped = false;
  2952. } else {
  2953. if (ch === 91) {
  2954. inClass = true;
  2955. } else if (ch === 93 && inClass) {
  2956. inClass = false;
  2957. } else if (ch === 47 && !inClass) {
  2958. break;
  2959. }
  2960. escaped = ch === 92;
  2961. }
  2962. }
  2963. const content = this.input.slice(start, pos);
  2964. ++pos;
  2965. let mods = "";
  2966. const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
  2967. while (pos < this.length) {
  2968. const cp = this.codePointAtPos(pos);
  2969. const char = String.fromCharCode(cp);
  2970. if (VALID_REGEX_FLAGS.has(cp)) {
  2971. if (cp === 118) {
  2972. this.expectPlugin("regexpUnicodeSets", nextPos());
  2973. if (mods.includes("u")) {
  2974. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2975. at: nextPos()
  2976. });
  2977. }
  2978. } else if (cp === 117) {
  2979. if (mods.includes("v")) {
  2980. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2981. at: nextPos()
  2982. });
  2983. }
  2984. }
  2985. if (mods.includes(char)) {
  2986. this.raise(Errors.DuplicateRegExpFlags, {
  2987. at: nextPos()
  2988. });
  2989. }
  2990. } else if (isIdentifierChar(cp) || cp === 92) {
  2991. this.raise(Errors.MalformedRegExpFlags, {
  2992. at: nextPos()
  2993. });
  2994. } else {
  2995. break;
  2996. }
  2997. ++pos;
  2998. mods += char;
  2999. }
  3000. this.state.pos = pos;
  3001. this.finishToken(135, {
  3002. pattern: content,
  3003. flags: mods
  3004. });
  3005. }
  3006. readInt(radix, len, forceLen = false, allowNumSeparator = true) {
  3007. const {
  3008. n,
  3009. pos
  3010. } = readInt(this.input, this.state.pos, this.state.lineStart, this.state.curLine, radix, len, forceLen, allowNumSeparator, this.errorHandlers_readInt, false);
  3011. this.state.pos = pos;
  3012. return n;
  3013. }
  3014. readRadixNumber(radix) {
  3015. const startLoc = this.state.curPosition();
  3016. let isBigInt = false;
  3017. this.state.pos += 2;
  3018. const val = this.readInt(radix);
  3019. if (val == null) {
  3020. this.raise(Errors.InvalidDigit, {
  3021. at: createPositionWithColumnOffset(startLoc, 2),
  3022. radix
  3023. });
  3024. }
  3025. const next = this.input.charCodeAt(this.state.pos);
  3026. if (next === 110) {
  3027. ++this.state.pos;
  3028. isBigInt = true;
  3029. } else if (next === 109) {
  3030. throw this.raise(Errors.InvalidDecimal, {
  3031. at: startLoc
  3032. });
  3033. }
  3034. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3035. throw this.raise(Errors.NumberIdentifier, {
  3036. at: this.state.curPosition()
  3037. });
  3038. }
  3039. if (isBigInt) {
  3040. const str = this.input.slice(startLoc.index, this.state.pos).replace(/[_n]/g, "");
  3041. this.finishToken(133, str);
  3042. return;
  3043. }
  3044. this.finishToken(132, val);
  3045. }
  3046. readNumber(startsWithDot) {
  3047. const start = this.state.pos;
  3048. const startLoc = this.state.curPosition();
  3049. let isFloat = false;
  3050. let isBigInt = false;
  3051. let isDecimal = false;
  3052. let hasExponent = false;
  3053. let isOctal = false;
  3054. if (!startsWithDot && this.readInt(10) === null) {
  3055. this.raise(Errors.InvalidNumber, {
  3056. at: this.state.curPosition()
  3057. });
  3058. }
  3059. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  3060. if (hasLeadingZero) {
  3061. const integer = this.input.slice(start, this.state.pos);
  3062. this.recordStrictModeErrors(Errors.StrictOctalLiteral, {
  3063. at: startLoc
  3064. });
  3065. if (!this.state.strict) {
  3066. const underscorePos = integer.indexOf("_");
  3067. if (underscorePos > 0) {
  3068. this.raise(Errors.ZeroDigitNumericSeparator, {
  3069. at: createPositionWithColumnOffset(startLoc, underscorePos)
  3070. });
  3071. }
  3072. }
  3073. isOctal = hasLeadingZero && !/[89]/.test(integer);
  3074. }
  3075. let next = this.input.charCodeAt(this.state.pos);
  3076. if (next === 46 && !isOctal) {
  3077. ++this.state.pos;
  3078. this.readInt(10);
  3079. isFloat = true;
  3080. next = this.input.charCodeAt(this.state.pos);
  3081. }
  3082. if ((next === 69 || next === 101) && !isOctal) {
  3083. next = this.input.charCodeAt(++this.state.pos);
  3084. if (next === 43 || next === 45) {
  3085. ++this.state.pos;
  3086. }
  3087. if (this.readInt(10) === null) {
  3088. this.raise(Errors.InvalidOrMissingExponent, {
  3089. at: startLoc
  3090. });
  3091. }
  3092. isFloat = true;
  3093. hasExponent = true;
  3094. next = this.input.charCodeAt(this.state.pos);
  3095. }
  3096. if (next === 110) {
  3097. if (isFloat || hasLeadingZero) {
  3098. this.raise(Errors.InvalidBigIntLiteral, {
  3099. at: startLoc
  3100. });
  3101. }
  3102. ++this.state.pos;
  3103. isBigInt = true;
  3104. }
  3105. if (next === 109) {
  3106. this.expectPlugin("decimal", this.state.curPosition());
  3107. if (hasExponent || hasLeadingZero) {
  3108. this.raise(Errors.InvalidDecimal, {
  3109. at: startLoc
  3110. });
  3111. }
  3112. ++this.state.pos;
  3113. isDecimal = true;
  3114. }
  3115. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3116. throw this.raise(Errors.NumberIdentifier, {
  3117. at: this.state.curPosition()
  3118. });
  3119. }
  3120. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  3121. if (isBigInt) {
  3122. this.finishToken(133, str);
  3123. return;
  3124. }
  3125. if (isDecimal) {
  3126. this.finishToken(134, str);
  3127. return;
  3128. }
  3129. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  3130. this.finishToken(132, val);
  3131. }
  3132. readCodePoint(throwOnInvalid) {
  3133. const {
  3134. code,
  3135. pos
  3136. } = readCodePoint(this.input, this.state.pos, this.state.lineStart, this.state.curLine, throwOnInvalid, this.errorHandlers_readCodePoint);
  3137. this.state.pos = pos;
  3138. return code;
  3139. }
  3140. readString(quote) {
  3141. const {
  3142. str,
  3143. pos,
  3144. curLine,
  3145. lineStart
  3146. } = readStringContents(quote === 34 ? "double" : "single", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_string);
  3147. this.state.pos = pos + 1;
  3148. this.state.lineStart = lineStart;
  3149. this.state.curLine = curLine;
  3150. this.finishToken(131, str);
  3151. }
  3152. readTemplateContinuation() {
  3153. if (!this.match(8)) {
  3154. this.unexpected(null, 8);
  3155. }
  3156. this.state.pos--;
  3157. this.readTemplateToken();
  3158. }
  3159. readTemplateToken() {
  3160. const opening = this.input[this.state.pos];
  3161. const {
  3162. str,
  3163. firstInvalidLoc,
  3164. pos,
  3165. curLine,
  3166. lineStart
  3167. } = readStringContents("template", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_template);
  3168. this.state.pos = pos + 1;
  3169. this.state.lineStart = lineStart;
  3170. this.state.curLine = curLine;
  3171. if (firstInvalidLoc) {
  3172. this.state.firstInvalidTemplateEscapePos = new Position(firstInvalidLoc.curLine, firstInvalidLoc.pos - firstInvalidLoc.lineStart, firstInvalidLoc.pos);
  3173. }
  3174. if (this.input.codePointAt(pos) === 96) {
  3175. this.finishToken(24, firstInvalidLoc ? null : opening + str + "`");
  3176. } else {
  3177. this.state.pos++;
  3178. this.finishToken(25, firstInvalidLoc ? null : opening + str + "${");
  3179. }
  3180. }
  3181. recordStrictModeErrors(toParseError, {
  3182. at
  3183. }) {
  3184. const index = at.index;
  3185. if (this.state.strict && !this.state.strictErrors.has(index)) {
  3186. this.raise(toParseError, {
  3187. at
  3188. });
  3189. } else {
  3190. this.state.strictErrors.set(index, [toParseError, at]);
  3191. }
  3192. }
  3193. readWord1(firstCode) {
  3194. this.state.containsEsc = false;
  3195. let word = "";
  3196. const start = this.state.pos;
  3197. let chunkStart = this.state.pos;
  3198. if (firstCode !== undefined) {
  3199. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  3200. }
  3201. while (this.state.pos < this.length) {
  3202. const ch = this.codePointAtPos(this.state.pos);
  3203. if (isIdentifierChar(ch)) {
  3204. this.state.pos += ch <= 0xffff ? 1 : 2;
  3205. } else if (ch === 92) {
  3206. this.state.containsEsc = true;
  3207. word += this.input.slice(chunkStart, this.state.pos);
  3208. const escStart = this.state.curPosition();
  3209. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  3210. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  3211. this.raise(Errors.MissingUnicodeEscape, {
  3212. at: this.state.curPosition()
  3213. });
  3214. chunkStart = this.state.pos - 1;
  3215. continue;
  3216. }
  3217. ++this.state.pos;
  3218. const esc = this.readCodePoint(true);
  3219. if (esc !== null) {
  3220. if (!identifierCheck(esc)) {
  3221. this.raise(Errors.EscapedCharNotAnIdentifier, {
  3222. at: escStart
  3223. });
  3224. }
  3225. word += String.fromCodePoint(esc);
  3226. }
  3227. chunkStart = this.state.pos;
  3228. } else {
  3229. break;
  3230. }
  3231. }
  3232. return word + this.input.slice(chunkStart, this.state.pos);
  3233. }
  3234. readWord(firstCode) {
  3235. const word = this.readWord1(firstCode);
  3236. const type = keywords$1.get(word);
  3237. if (type !== undefined) {
  3238. this.finishToken(type, tokenLabelName(type));
  3239. } else {
  3240. this.finishToken(130, word);
  3241. }
  3242. }
  3243. checkKeywordEscapes() {
  3244. const {
  3245. type
  3246. } = this.state;
  3247. if (tokenIsKeyword(type) && this.state.containsEsc) {
  3248. this.raise(Errors.InvalidEscapedReservedWord, {
  3249. at: this.state.startLoc,
  3250. reservedWord: tokenLabelName(type)
  3251. });
  3252. }
  3253. }
  3254. raise(toParseError, raiseProperties) {
  3255. const {
  3256. at
  3257. } = raiseProperties,
  3258. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded);
  3259. const loc = at instanceof Position ? at : at.loc.start;
  3260. const error = toParseError({
  3261. loc,
  3262. details
  3263. });
  3264. if (!this.options.errorRecovery) throw error;
  3265. if (!this.isLookahead) this.state.errors.push(error);
  3266. return error;
  3267. }
  3268. raiseOverwrite(toParseError, raiseProperties) {
  3269. const {
  3270. at
  3271. } = raiseProperties,
  3272. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded2);
  3273. const loc = at instanceof Position ? at : at.loc.start;
  3274. const pos = loc.index;
  3275. const errors = this.state.errors;
  3276. for (let i = errors.length - 1; i >= 0; i--) {
  3277. const error = errors[i];
  3278. if (error.loc.index === pos) {
  3279. return errors[i] = toParseError({
  3280. loc,
  3281. details
  3282. });
  3283. }
  3284. if (error.loc.index < pos) break;
  3285. }
  3286. return this.raise(toParseError, raiseProperties);
  3287. }
  3288. updateContext(prevType) {}
  3289. unexpected(loc, type) {
  3290. throw this.raise(Errors.UnexpectedToken, {
  3291. expected: type ? tokenLabelName(type) : null,
  3292. at: loc != null ? loc : this.state.startLoc
  3293. });
  3294. }
  3295. expectPlugin(pluginName, loc) {
  3296. if (this.hasPlugin(pluginName)) {
  3297. return true;
  3298. }
  3299. throw this.raise(Errors.MissingPlugin, {
  3300. at: loc != null ? loc : this.state.startLoc,
  3301. missingPlugin: [pluginName]
  3302. });
  3303. }
  3304. expectOnePlugin(pluginNames) {
  3305. if (!pluginNames.some(name => this.hasPlugin(name))) {
  3306. throw this.raise(Errors.MissingOneOfPlugins, {
  3307. at: this.state.startLoc,
  3308. missingPlugin: pluginNames
  3309. });
  3310. }
  3311. }
  3312. errorBuilder(error) {
  3313. return (pos, lineStart, curLine) => {
  3314. this.raise(error, {
  3315. at: buildPosition(pos, lineStart, curLine)
  3316. });
  3317. };
  3318. }
  3319. }
  3320. class ClassScope {
  3321. constructor() {
  3322. this.privateNames = new Set();
  3323. this.loneAccessors = new Map();
  3324. this.undefinedPrivateNames = new Map();
  3325. }
  3326. }
  3327. class ClassScopeHandler {
  3328. constructor(parser) {
  3329. this.parser = void 0;
  3330. this.stack = [];
  3331. this.undefinedPrivateNames = new Map();
  3332. this.parser = parser;
  3333. }
  3334. current() {
  3335. return this.stack[this.stack.length - 1];
  3336. }
  3337. enter() {
  3338. this.stack.push(new ClassScope());
  3339. }
  3340. exit() {
  3341. const oldClassScope = this.stack.pop();
  3342. const current = this.current();
  3343. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  3344. if (current) {
  3345. if (!current.undefinedPrivateNames.has(name)) {
  3346. current.undefinedPrivateNames.set(name, loc);
  3347. }
  3348. } else {
  3349. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3350. at: loc,
  3351. identifierName: name
  3352. });
  3353. }
  3354. }
  3355. }
  3356. declarePrivateName(name, elementType, loc) {
  3357. const {
  3358. privateNames,
  3359. loneAccessors,
  3360. undefinedPrivateNames
  3361. } = this.current();
  3362. let redefined = privateNames.has(name);
  3363. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  3364. const accessor = redefined && loneAccessors.get(name);
  3365. if (accessor) {
  3366. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  3367. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  3368. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  3369. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  3370. redefined = oldKind === newKind || oldStatic !== newStatic;
  3371. if (!redefined) loneAccessors.delete(name);
  3372. } else if (!redefined) {
  3373. loneAccessors.set(name, elementType);
  3374. }
  3375. }
  3376. if (redefined) {
  3377. this.parser.raise(Errors.PrivateNameRedeclaration, {
  3378. at: loc,
  3379. identifierName: name
  3380. });
  3381. }
  3382. privateNames.add(name);
  3383. undefinedPrivateNames.delete(name);
  3384. }
  3385. usePrivateName(name, loc) {
  3386. let classScope;
  3387. for (classScope of this.stack) {
  3388. if (classScope.privateNames.has(name)) return;
  3389. }
  3390. if (classScope) {
  3391. classScope.undefinedPrivateNames.set(name, loc);
  3392. } else {
  3393. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3394. at: loc,
  3395. identifierName: name
  3396. });
  3397. }
  3398. }
  3399. }
  3400. const kExpression = 0,
  3401. kMaybeArrowParameterDeclaration = 1,
  3402. kMaybeAsyncArrowParameterDeclaration = 2,
  3403. kParameterDeclaration = 3;
  3404. class ExpressionScope {
  3405. constructor(type = kExpression) {
  3406. this.type = void 0;
  3407. this.type = type;
  3408. }
  3409. canBeArrowParameterDeclaration() {
  3410. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  3411. }
  3412. isCertainlyParameterDeclaration() {
  3413. return this.type === kParameterDeclaration;
  3414. }
  3415. }
  3416. class ArrowHeadParsingScope extends ExpressionScope {
  3417. constructor(type) {
  3418. super(type);
  3419. this.declarationErrors = new Map();
  3420. }
  3421. recordDeclarationError(ParsingErrorClass, {
  3422. at
  3423. }) {
  3424. const index = at.index;
  3425. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  3426. }
  3427. clearDeclarationError(index) {
  3428. this.declarationErrors.delete(index);
  3429. }
  3430. iterateErrors(iterator) {
  3431. this.declarationErrors.forEach(iterator);
  3432. }
  3433. }
  3434. class ExpressionScopeHandler {
  3435. constructor(parser) {
  3436. this.parser = void 0;
  3437. this.stack = [new ExpressionScope()];
  3438. this.parser = parser;
  3439. }
  3440. enter(scope) {
  3441. this.stack.push(scope);
  3442. }
  3443. exit() {
  3444. this.stack.pop();
  3445. }
  3446. recordParameterInitializerError(toParseError, {
  3447. at: node
  3448. }) {
  3449. const origin = {
  3450. at: node.loc.start
  3451. };
  3452. const {
  3453. stack
  3454. } = this;
  3455. let i = stack.length - 1;
  3456. let scope = stack[i];
  3457. while (!scope.isCertainlyParameterDeclaration()) {
  3458. if (scope.canBeArrowParameterDeclaration()) {
  3459. scope.recordDeclarationError(toParseError, origin);
  3460. } else {
  3461. return;
  3462. }
  3463. scope = stack[--i];
  3464. }
  3465. this.parser.raise(toParseError, origin);
  3466. }
  3467. recordArrowParameterBindingError(error, {
  3468. at: node
  3469. }) {
  3470. const {
  3471. stack
  3472. } = this;
  3473. const scope = stack[stack.length - 1];
  3474. const origin = {
  3475. at: node.loc.start
  3476. };
  3477. if (scope.isCertainlyParameterDeclaration()) {
  3478. this.parser.raise(error, origin);
  3479. } else if (scope.canBeArrowParameterDeclaration()) {
  3480. scope.recordDeclarationError(error, origin);
  3481. } else {
  3482. return;
  3483. }
  3484. }
  3485. recordAsyncArrowParametersError({
  3486. at
  3487. }) {
  3488. const {
  3489. stack
  3490. } = this;
  3491. let i = stack.length - 1;
  3492. let scope = stack[i];
  3493. while (scope.canBeArrowParameterDeclaration()) {
  3494. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  3495. scope.recordDeclarationError(Errors.AwaitBindingIdentifier, {
  3496. at
  3497. });
  3498. }
  3499. scope = stack[--i];
  3500. }
  3501. }
  3502. validateAsPattern() {
  3503. const {
  3504. stack
  3505. } = this;
  3506. const currentScope = stack[stack.length - 1];
  3507. if (!currentScope.canBeArrowParameterDeclaration()) return;
  3508. currentScope.iterateErrors(([toParseError, loc]) => {
  3509. this.parser.raise(toParseError, {
  3510. at: loc
  3511. });
  3512. let i = stack.length - 2;
  3513. let scope = stack[i];
  3514. while (scope.canBeArrowParameterDeclaration()) {
  3515. scope.clearDeclarationError(loc.index);
  3516. scope = stack[--i];
  3517. }
  3518. });
  3519. }
  3520. }
  3521. function newParameterDeclarationScope() {
  3522. return new ExpressionScope(kParameterDeclaration);
  3523. }
  3524. function newArrowHeadScope() {
  3525. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  3526. }
  3527. function newAsyncArrowScope() {
  3528. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  3529. }
  3530. function newExpressionScope() {
  3531. return new ExpressionScope();
  3532. }
  3533. const PARAM = 0b0000,
  3534. PARAM_YIELD = 0b0001,
  3535. PARAM_AWAIT = 0b0010,
  3536. PARAM_RETURN = 0b0100,
  3537. PARAM_IN = 0b1000;
  3538. class ProductionParameterHandler {
  3539. constructor() {
  3540. this.stacks = [];
  3541. }
  3542. enter(flags) {
  3543. this.stacks.push(flags);
  3544. }
  3545. exit() {
  3546. this.stacks.pop();
  3547. }
  3548. currentFlags() {
  3549. return this.stacks[this.stacks.length - 1];
  3550. }
  3551. get hasAwait() {
  3552. return (this.currentFlags() & PARAM_AWAIT) > 0;
  3553. }
  3554. get hasYield() {
  3555. return (this.currentFlags() & PARAM_YIELD) > 0;
  3556. }
  3557. get hasReturn() {
  3558. return (this.currentFlags() & PARAM_RETURN) > 0;
  3559. }
  3560. get hasIn() {
  3561. return (this.currentFlags() & PARAM_IN) > 0;
  3562. }
  3563. }
  3564. function functionFlags(isAsync, isGenerator) {
  3565. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  3566. }
  3567. class UtilParser extends Tokenizer {
  3568. addExtra(node, key, value, enumerable = true) {
  3569. if (!node) return;
  3570. const extra = node.extra = node.extra || {};
  3571. if (enumerable) {
  3572. extra[key] = value;
  3573. } else {
  3574. Object.defineProperty(extra, key, {
  3575. enumerable,
  3576. value
  3577. });
  3578. }
  3579. }
  3580. isContextual(token) {
  3581. return this.state.type === token && !this.state.containsEsc;
  3582. }
  3583. isUnparsedContextual(nameStart, name) {
  3584. const nameEnd = nameStart + name.length;
  3585. if (this.input.slice(nameStart, nameEnd) === name) {
  3586. const nextCh = this.input.charCodeAt(nameEnd);
  3587. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  3588. }
  3589. return false;
  3590. }
  3591. isLookaheadContextual(name) {
  3592. const next = this.nextTokenStart();
  3593. return this.isUnparsedContextual(next, name);
  3594. }
  3595. eatContextual(token) {
  3596. if (this.isContextual(token)) {
  3597. this.next();
  3598. return true;
  3599. }
  3600. return false;
  3601. }
  3602. expectContextual(token, toParseError) {
  3603. if (!this.eatContextual(token)) {
  3604. if (toParseError != null) {
  3605. throw this.raise(toParseError, {
  3606. at: this.state.startLoc
  3607. });
  3608. }
  3609. this.unexpected(null, token);
  3610. }
  3611. }
  3612. canInsertSemicolon() {
  3613. return this.match(137) || this.match(8) || this.hasPrecedingLineBreak();
  3614. }
  3615. hasPrecedingLineBreak() {
  3616. return lineBreak.test(this.input.slice(this.state.lastTokEndLoc.index, this.state.start));
  3617. }
  3618. hasFollowingLineBreak() {
  3619. skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
  3620. return skipWhiteSpaceToLineBreak.test(this.input);
  3621. }
  3622. isLineTerminator() {
  3623. return this.eat(13) || this.canInsertSemicolon();
  3624. }
  3625. semicolon(allowAsi = true) {
  3626. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  3627. this.raise(Errors.MissingSemicolon, {
  3628. at: this.state.lastTokEndLoc
  3629. });
  3630. }
  3631. expect(type, loc) {
  3632. this.eat(type) || this.unexpected(loc, type);
  3633. }
  3634. tryParse(fn, oldState = this.state.clone()) {
  3635. const abortSignal = {
  3636. node: null
  3637. };
  3638. try {
  3639. const node = fn((node = null) => {
  3640. abortSignal.node = node;
  3641. throw abortSignal;
  3642. });
  3643. if (this.state.errors.length > oldState.errors.length) {
  3644. const failState = this.state;
  3645. this.state = oldState;
  3646. this.state.tokensLength = failState.tokensLength;
  3647. return {
  3648. node,
  3649. error: failState.errors[oldState.errors.length],
  3650. thrown: false,
  3651. aborted: false,
  3652. failState
  3653. };
  3654. }
  3655. return {
  3656. node,
  3657. error: null,
  3658. thrown: false,
  3659. aborted: false,
  3660. failState: null
  3661. };
  3662. } catch (error) {
  3663. const failState = this.state;
  3664. this.state = oldState;
  3665. if (error instanceof SyntaxError) {
  3666. return {
  3667. node: null,
  3668. error,
  3669. thrown: true,
  3670. aborted: false,
  3671. failState
  3672. };
  3673. }
  3674. if (error === abortSignal) {
  3675. return {
  3676. node: abortSignal.node,
  3677. error: null,
  3678. thrown: false,
  3679. aborted: true,
  3680. failState
  3681. };
  3682. }
  3683. throw error;
  3684. }
  3685. }
  3686. checkExpressionErrors(refExpressionErrors, andThrow) {
  3687. if (!refExpressionErrors) return false;
  3688. const {
  3689. shorthandAssignLoc,
  3690. doubleProtoLoc,
  3691. privateKeyLoc,
  3692. optionalParametersLoc
  3693. } = refExpressionErrors;
  3694. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
  3695. if (!andThrow) {
  3696. return hasErrors;
  3697. }
  3698. if (shorthandAssignLoc != null) {
  3699. this.raise(Errors.InvalidCoverInitializedName, {
  3700. at: shorthandAssignLoc
  3701. });
  3702. }
  3703. if (doubleProtoLoc != null) {
  3704. this.raise(Errors.DuplicateProto, {
  3705. at: doubleProtoLoc
  3706. });
  3707. }
  3708. if (privateKeyLoc != null) {
  3709. this.raise(Errors.UnexpectedPrivateField, {
  3710. at: privateKeyLoc
  3711. });
  3712. }
  3713. if (optionalParametersLoc != null) {
  3714. this.unexpected(optionalParametersLoc);
  3715. }
  3716. }
  3717. isLiteralPropertyName() {
  3718. return tokenIsLiteralPropertyName(this.state.type);
  3719. }
  3720. isPrivateName(node) {
  3721. return node.type === "PrivateName";
  3722. }
  3723. getPrivateNameSV(node) {
  3724. return node.id.name;
  3725. }
  3726. hasPropertyAsPrivateName(node) {
  3727. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  3728. }
  3729. isObjectProperty(node) {
  3730. return node.type === "ObjectProperty";
  3731. }
  3732. isObjectMethod(node) {
  3733. return node.type === "ObjectMethod";
  3734. }
  3735. initializeScopes(inModule = this.options.sourceType === "module") {
  3736. const oldLabels = this.state.labels;
  3737. this.state.labels = [];
  3738. const oldExportedIdentifiers = this.exportedIdentifiers;
  3739. this.exportedIdentifiers = new Set();
  3740. const oldInModule = this.inModule;
  3741. this.inModule = inModule;
  3742. const oldScope = this.scope;
  3743. const ScopeHandler = this.getScopeHandler();
  3744. this.scope = new ScopeHandler(this, inModule);
  3745. const oldProdParam = this.prodParam;
  3746. this.prodParam = new ProductionParameterHandler();
  3747. const oldClassScope = this.classScope;
  3748. this.classScope = new ClassScopeHandler(this);
  3749. const oldExpressionScope = this.expressionScope;
  3750. this.expressionScope = new ExpressionScopeHandler(this);
  3751. return () => {
  3752. this.state.labels = oldLabels;
  3753. this.exportedIdentifiers = oldExportedIdentifiers;
  3754. this.inModule = oldInModule;
  3755. this.scope = oldScope;
  3756. this.prodParam = oldProdParam;
  3757. this.classScope = oldClassScope;
  3758. this.expressionScope = oldExpressionScope;
  3759. };
  3760. }
  3761. enterInitialScopes() {
  3762. let paramFlags = PARAM;
  3763. if (this.inModule) {
  3764. paramFlags |= PARAM_AWAIT;
  3765. }
  3766. this.scope.enter(SCOPE_PROGRAM);
  3767. this.prodParam.enter(paramFlags);
  3768. }
  3769. checkDestructuringPrivate(refExpressionErrors) {
  3770. const {
  3771. privateKeyLoc
  3772. } = refExpressionErrors;
  3773. if (privateKeyLoc !== null) {
  3774. this.expectPlugin("destructuringPrivate", privateKeyLoc);
  3775. }
  3776. }
  3777. }
  3778. class ExpressionErrors {
  3779. constructor() {
  3780. this.shorthandAssignLoc = null;
  3781. this.doubleProtoLoc = null;
  3782. this.privateKeyLoc = null;
  3783. this.optionalParametersLoc = null;
  3784. }
  3785. }
  3786. class Node {
  3787. constructor(parser, pos, loc) {
  3788. this.type = "";
  3789. this.start = pos;
  3790. this.end = 0;
  3791. this.loc = new SourceLocation(loc);
  3792. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  3793. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  3794. }
  3795. }
  3796. const NodePrototype = Node.prototype;
  3797. {
  3798. NodePrototype.__clone = function () {
  3799. const newNode = new Node(undefined, this.start, this.loc.start);
  3800. const keys = Object.keys(this);
  3801. for (let i = 0, length = keys.length; i < length; i++) {
  3802. const key = keys[i];
  3803. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  3804. newNode[key] = this[key];
  3805. }
  3806. }
  3807. return newNode;
  3808. };
  3809. }
  3810. function clonePlaceholder(node) {
  3811. return cloneIdentifier(node);
  3812. }
  3813. function cloneIdentifier(node) {
  3814. const {
  3815. type,
  3816. start,
  3817. end,
  3818. loc,
  3819. range,
  3820. extra,
  3821. name
  3822. } = node;
  3823. const cloned = Object.create(NodePrototype);
  3824. cloned.type = type;
  3825. cloned.start = start;
  3826. cloned.end = end;
  3827. cloned.loc = loc;
  3828. cloned.range = range;
  3829. cloned.extra = extra;
  3830. cloned.name = name;
  3831. if (type === "Placeholder") {
  3832. cloned.expectedNode = node.expectedNode;
  3833. }
  3834. return cloned;
  3835. }
  3836. function cloneStringLiteral(node) {
  3837. const {
  3838. type,
  3839. start,
  3840. end,
  3841. loc,
  3842. range,
  3843. extra
  3844. } = node;
  3845. if (type === "Placeholder") {
  3846. return clonePlaceholder(node);
  3847. }
  3848. const cloned = Object.create(NodePrototype);
  3849. cloned.type = type;
  3850. cloned.start = start;
  3851. cloned.end = end;
  3852. cloned.loc = loc;
  3853. cloned.range = range;
  3854. if (node.raw !== undefined) {
  3855. cloned.raw = node.raw;
  3856. } else {
  3857. cloned.extra = extra;
  3858. }
  3859. cloned.value = node.value;
  3860. return cloned;
  3861. }
  3862. class NodeUtils extends UtilParser {
  3863. startNode() {
  3864. return new Node(this, this.state.start, this.state.startLoc);
  3865. }
  3866. startNodeAt(loc) {
  3867. return new Node(this, loc.index, loc);
  3868. }
  3869. startNodeAtNode(type) {
  3870. return this.startNodeAt(type.loc.start);
  3871. }
  3872. finishNode(node, type) {
  3873. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  3874. }
  3875. finishNodeAt(node, type, endLoc) {
  3876. node.type = type;
  3877. node.end = endLoc.index;
  3878. node.loc.end = endLoc;
  3879. if (this.options.ranges) node.range[1] = endLoc.index;
  3880. if (this.options.attachComment) this.processComment(node);
  3881. return node;
  3882. }
  3883. resetStartLocation(node, startLoc) {
  3884. node.start = startLoc.index;
  3885. node.loc.start = startLoc;
  3886. if (this.options.ranges) node.range[0] = startLoc.index;
  3887. }
  3888. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  3889. node.end = endLoc.index;
  3890. node.loc.end = endLoc;
  3891. if (this.options.ranges) node.range[1] = endLoc.index;
  3892. }
  3893. resetStartLocationFromNode(node, locationNode) {
  3894. this.resetStartLocation(node, locationNode.loc.start);
  3895. }
  3896. }
  3897. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3898. const FlowErrors = ParseErrorEnum`flow`({
  3899. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  3900. 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.",
  3901. AssignReservedType: ({
  3902. reservedType
  3903. }) => `Cannot overwrite reserved type ${reservedType}.`,
  3904. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  3905. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  3906. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  3907. EnumBooleanMemberNotInitialized: ({
  3908. memberName,
  3909. enumName
  3910. }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`,
  3911. EnumDuplicateMemberName: ({
  3912. memberName,
  3913. enumName
  3914. }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`,
  3915. EnumInconsistentMemberValues: ({
  3916. enumName
  3917. }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`,
  3918. EnumInvalidExplicitType: ({
  3919. invalidEnumType,
  3920. enumName
  3921. }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3922. EnumInvalidExplicitTypeUnknownSupplied: ({
  3923. enumName
  3924. }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3925. EnumInvalidMemberInitializerPrimaryType: ({
  3926. enumName,
  3927. memberName,
  3928. explicitType
  3929. }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`,
  3930. EnumInvalidMemberInitializerSymbolType: ({
  3931. enumName,
  3932. memberName
  3933. }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`,
  3934. EnumInvalidMemberInitializerUnknownType: ({
  3935. enumName,
  3936. memberName
  3937. }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`,
  3938. EnumInvalidMemberName: ({
  3939. enumName,
  3940. memberName,
  3941. suggestion
  3942. }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`,
  3943. EnumNumberMemberNotInitialized: ({
  3944. enumName,
  3945. memberName
  3946. }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`,
  3947. EnumStringMemberInconsistentlyInitailized: ({
  3948. enumName
  3949. }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`,
  3950. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  3951. ImportReflectionHasImportType: "An `import module` declaration can not use `type` or `typeof` keyword.",
  3952. 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.",
  3953. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  3954. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  3955. InexactVariance: "Explicit inexact syntax cannot have variance.",
  3956. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  3957. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  3958. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  3959. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  3960. PatternIsOptional: Object.assign({
  3961. message: "A binding pattern parameter cannot be optional in an implementation signature."
  3962. }, {
  3963. reasonCode: "OptionalBindingPattern"
  3964. }),
  3965. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  3966. SpreadVariance: "Spread properties cannot have variance.",
  3967. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  3968. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  3969. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  3970. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  3971. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  3972. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  3973. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  3974. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  3975. UnexpectedReservedType: ({
  3976. reservedType
  3977. }) => `Unexpected reserved type ${reservedType}.`,
  3978. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  3979. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  3980. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  3981. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  3982. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  3983. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  3984. UnsupportedDeclareExportKind: ({
  3985. unsupportedExportKind,
  3986. suggestion
  3987. }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`,
  3988. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  3989. UnterminatedFlowComment: "Unterminated flow-comment."
  3990. });
  3991. function isEsModuleType(bodyElement) {
  3992. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3993. }
  3994. function hasTypeImportKind(node) {
  3995. return node.importKind === "type" || node.importKind === "typeof";
  3996. }
  3997. function isMaybeDefaultImport(type) {
  3998. return tokenIsKeywordOrIdentifier(type) && type !== 97;
  3999. }
  4000. const exportSuggestions = {
  4001. const: "declare export var",
  4002. let: "declare export var",
  4003. type: "export type",
  4004. interface: "export interface"
  4005. };
  4006. function partition(list, test) {
  4007. const list1 = [];
  4008. const list2 = [];
  4009. for (let i = 0; i < list.length; i++) {
  4010. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  4011. }
  4012. return [list1, list2];
  4013. }
  4014. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  4015. var flow = superClass => class FlowParserMixin extends superClass {
  4016. constructor(...args) {
  4017. super(...args);
  4018. this.flowPragma = undefined;
  4019. }
  4020. getScopeHandler() {
  4021. return FlowScopeHandler;
  4022. }
  4023. shouldParseTypes() {
  4024. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  4025. }
  4026. shouldParseEnums() {
  4027. return !!this.getPluginOption("flow", "enums");
  4028. }
  4029. finishToken(type, val) {
  4030. if (type !== 131 && type !== 13 && type !== 28) {
  4031. if (this.flowPragma === undefined) {
  4032. this.flowPragma = null;
  4033. }
  4034. }
  4035. super.finishToken(type, val);
  4036. }
  4037. addComment(comment) {
  4038. if (this.flowPragma === undefined) {
  4039. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  4040. if (!matches) ;else if (matches[1] === "flow") {
  4041. this.flowPragma = "flow";
  4042. } else if (matches[1] === "noflow") {
  4043. this.flowPragma = "noflow";
  4044. } else {
  4045. throw new Error("Unexpected flow pragma");
  4046. }
  4047. }
  4048. super.addComment(comment);
  4049. }
  4050. flowParseTypeInitialiser(tok) {
  4051. const oldInType = this.state.inType;
  4052. this.state.inType = true;
  4053. this.expect(tok || 14);
  4054. const type = this.flowParseType();
  4055. this.state.inType = oldInType;
  4056. return type;
  4057. }
  4058. flowParsePredicate() {
  4059. const node = this.startNode();
  4060. const moduloLoc = this.state.startLoc;
  4061. this.next();
  4062. this.expectContextual(108);
  4063. if (this.state.lastTokStart > moduloLoc.index + 1) {
  4064. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, {
  4065. at: moduloLoc
  4066. });
  4067. }
  4068. if (this.eat(10)) {
  4069. node.value = super.parseExpression();
  4070. this.expect(11);
  4071. return this.finishNode(node, "DeclaredPredicate");
  4072. } else {
  4073. return this.finishNode(node, "InferredPredicate");
  4074. }
  4075. }
  4076. flowParseTypeAndPredicateInitialiser() {
  4077. const oldInType = this.state.inType;
  4078. this.state.inType = true;
  4079. this.expect(14);
  4080. let type = null;
  4081. let predicate = null;
  4082. if (this.match(54)) {
  4083. this.state.inType = oldInType;
  4084. predicate = this.flowParsePredicate();
  4085. } else {
  4086. type = this.flowParseType();
  4087. this.state.inType = oldInType;
  4088. if (this.match(54)) {
  4089. predicate = this.flowParsePredicate();
  4090. }
  4091. }
  4092. return [type, predicate];
  4093. }
  4094. flowParseDeclareClass(node) {
  4095. this.next();
  4096. this.flowParseInterfaceish(node, true);
  4097. return this.finishNode(node, "DeclareClass");
  4098. }
  4099. flowParseDeclareFunction(node) {
  4100. this.next();
  4101. const id = node.id = this.parseIdentifier();
  4102. const typeNode = this.startNode();
  4103. const typeContainer = this.startNode();
  4104. if (this.match(47)) {
  4105. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  4106. } else {
  4107. typeNode.typeParameters = null;
  4108. }
  4109. this.expect(10);
  4110. const tmp = this.flowParseFunctionTypeParams();
  4111. typeNode.params = tmp.params;
  4112. typeNode.rest = tmp.rest;
  4113. typeNode.this = tmp._this;
  4114. this.expect(11);
  4115. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4116. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  4117. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  4118. this.resetEndLocation(id);
  4119. this.semicolon();
  4120. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.loc.start);
  4121. return this.finishNode(node, "DeclareFunction");
  4122. }
  4123. flowParseDeclare(node, insideModule) {
  4124. if (this.match(80)) {
  4125. return this.flowParseDeclareClass(node);
  4126. } else if (this.match(68)) {
  4127. return this.flowParseDeclareFunction(node);
  4128. } else if (this.match(74)) {
  4129. return this.flowParseDeclareVariable(node);
  4130. } else if (this.eatContextual(125)) {
  4131. if (this.match(16)) {
  4132. return this.flowParseDeclareModuleExports(node);
  4133. } else {
  4134. if (insideModule) {
  4135. this.raise(FlowErrors.NestedDeclareModule, {
  4136. at: this.state.lastTokStartLoc
  4137. });
  4138. }
  4139. return this.flowParseDeclareModule(node);
  4140. }
  4141. } else if (this.isContextual(128)) {
  4142. return this.flowParseDeclareTypeAlias(node);
  4143. } else if (this.isContextual(129)) {
  4144. return this.flowParseDeclareOpaqueType(node);
  4145. } else if (this.isContextual(127)) {
  4146. return this.flowParseDeclareInterface(node);
  4147. } else if (this.match(82)) {
  4148. return this.flowParseDeclareExportDeclaration(node, insideModule);
  4149. } else {
  4150. this.unexpected();
  4151. }
  4152. }
  4153. flowParseDeclareVariable(node) {
  4154. this.next();
  4155. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  4156. this.scope.declareName(node.id.name, BIND_VAR, node.id.loc.start);
  4157. this.semicolon();
  4158. return this.finishNode(node, "DeclareVariable");
  4159. }
  4160. flowParseDeclareModule(node) {
  4161. this.scope.enter(SCOPE_OTHER);
  4162. if (this.match(131)) {
  4163. node.id = super.parseExprAtom();
  4164. } else {
  4165. node.id = this.parseIdentifier();
  4166. }
  4167. const bodyNode = node.body = this.startNode();
  4168. const body = bodyNode.body = [];
  4169. this.expect(5);
  4170. while (!this.match(8)) {
  4171. let bodyNode = this.startNode();
  4172. if (this.match(83)) {
  4173. this.next();
  4174. if (!this.isContextual(128) && !this.match(87)) {
  4175. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, {
  4176. at: this.state.lastTokStartLoc
  4177. });
  4178. }
  4179. super.parseImport(bodyNode);
  4180. } else {
  4181. this.expectContextual(123, FlowErrors.UnsupportedStatementInDeclareModule);
  4182. bodyNode = this.flowParseDeclare(bodyNode, true);
  4183. }
  4184. body.push(bodyNode);
  4185. }
  4186. this.scope.exit();
  4187. this.expect(8);
  4188. this.finishNode(bodyNode, "BlockStatement");
  4189. let kind = null;
  4190. let hasModuleExport = false;
  4191. body.forEach(bodyElement => {
  4192. if (isEsModuleType(bodyElement)) {
  4193. if (kind === "CommonJS") {
  4194. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4195. at: bodyElement
  4196. });
  4197. }
  4198. kind = "ES";
  4199. } else if (bodyElement.type === "DeclareModuleExports") {
  4200. if (hasModuleExport) {
  4201. this.raise(FlowErrors.DuplicateDeclareModuleExports, {
  4202. at: bodyElement
  4203. });
  4204. }
  4205. if (kind === "ES") {
  4206. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4207. at: bodyElement
  4208. });
  4209. }
  4210. kind = "CommonJS";
  4211. hasModuleExport = true;
  4212. }
  4213. });
  4214. node.kind = kind || "CommonJS";
  4215. return this.finishNode(node, "DeclareModule");
  4216. }
  4217. flowParseDeclareExportDeclaration(node, insideModule) {
  4218. this.expect(82);
  4219. if (this.eat(65)) {
  4220. if (this.match(68) || this.match(80)) {
  4221. node.declaration = this.flowParseDeclare(this.startNode());
  4222. } else {
  4223. node.declaration = this.flowParseType();
  4224. this.semicolon();
  4225. }
  4226. node.default = true;
  4227. return this.finishNode(node, "DeclareExportDeclaration");
  4228. } else {
  4229. if (this.match(75) || this.isLet() || (this.isContextual(128) || this.isContextual(127)) && !insideModule) {
  4230. const label = this.state.value;
  4231. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, {
  4232. at: this.state.startLoc,
  4233. unsupportedExportKind: label,
  4234. suggestion: exportSuggestions[label]
  4235. });
  4236. }
  4237. if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(129)) {
  4238. node.declaration = this.flowParseDeclare(this.startNode());
  4239. node.default = false;
  4240. return this.finishNode(node, "DeclareExportDeclaration");
  4241. } else if (this.match(55) || this.match(5) || this.isContextual(127) || this.isContextual(128) || this.isContextual(129)) {
  4242. node = this.parseExport(node, null);
  4243. if (node.type === "ExportNamedDeclaration") {
  4244. node.type = "ExportDeclaration";
  4245. node.default = false;
  4246. delete node.exportKind;
  4247. }
  4248. node.type = "Declare" + node.type;
  4249. return node;
  4250. }
  4251. }
  4252. this.unexpected();
  4253. }
  4254. flowParseDeclareModuleExports(node) {
  4255. this.next();
  4256. this.expectContextual(109);
  4257. node.typeAnnotation = this.flowParseTypeAnnotation();
  4258. this.semicolon();
  4259. return this.finishNode(node, "DeclareModuleExports");
  4260. }
  4261. flowParseDeclareTypeAlias(node) {
  4262. this.next();
  4263. const finished = this.flowParseTypeAlias(node);
  4264. finished.type = "DeclareTypeAlias";
  4265. return finished;
  4266. }
  4267. flowParseDeclareOpaqueType(node) {
  4268. this.next();
  4269. const finished = this.flowParseOpaqueType(node, true);
  4270. finished.type = "DeclareOpaqueType";
  4271. return finished;
  4272. }
  4273. flowParseDeclareInterface(node) {
  4274. this.next();
  4275. this.flowParseInterfaceish(node, false);
  4276. return this.finishNode(node, "DeclareInterface");
  4277. }
  4278. flowParseInterfaceish(node, isClass) {
  4279. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  4280. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.loc.start);
  4281. if (this.match(47)) {
  4282. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4283. } else {
  4284. node.typeParameters = null;
  4285. }
  4286. node.extends = [];
  4287. node.implements = [];
  4288. node.mixins = [];
  4289. if (this.eat(81)) {
  4290. do {
  4291. node.extends.push(this.flowParseInterfaceExtends());
  4292. } while (!isClass && this.eat(12));
  4293. }
  4294. if (isClass) {
  4295. if (this.eatContextual(115)) {
  4296. do {
  4297. node.mixins.push(this.flowParseInterfaceExtends());
  4298. } while (this.eat(12));
  4299. }
  4300. if (this.eatContextual(111)) {
  4301. do {
  4302. node.implements.push(this.flowParseInterfaceExtends());
  4303. } while (this.eat(12));
  4304. }
  4305. }
  4306. node.body = this.flowParseObjectType({
  4307. allowStatic: isClass,
  4308. allowExact: false,
  4309. allowSpread: false,
  4310. allowProto: isClass,
  4311. allowInexact: false
  4312. });
  4313. }
  4314. flowParseInterfaceExtends() {
  4315. const node = this.startNode();
  4316. node.id = this.flowParseQualifiedTypeIdentifier();
  4317. if (this.match(47)) {
  4318. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4319. } else {
  4320. node.typeParameters = null;
  4321. }
  4322. return this.finishNode(node, "InterfaceExtends");
  4323. }
  4324. flowParseInterface(node) {
  4325. this.flowParseInterfaceish(node, false);
  4326. return this.finishNode(node, "InterfaceDeclaration");
  4327. }
  4328. checkNotUnderscore(word) {
  4329. if (word === "_") {
  4330. this.raise(FlowErrors.UnexpectedReservedUnderscore, {
  4331. at: this.state.startLoc
  4332. });
  4333. }
  4334. }
  4335. checkReservedType(word, startLoc, declaration) {
  4336. if (!reservedTypes.has(word)) return;
  4337. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, {
  4338. at: startLoc,
  4339. reservedType: word
  4340. });
  4341. }
  4342. flowParseRestrictedIdentifier(liberal, declaration) {
  4343. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  4344. return this.parseIdentifier(liberal);
  4345. }
  4346. flowParseTypeAlias(node) {
  4347. node.id = this.flowParseRestrictedIdentifier(false, true);
  4348. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4349. if (this.match(47)) {
  4350. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4351. } else {
  4352. node.typeParameters = null;
  4353. }
  4354. node.right = this.flowParseTypeInitialiser(29);
  4355. this.semicolon();
  4356. return this.finishNode(node, "TypeAlias");
  4357. }
  4358. flowParseOpaqueType(node, declare) {
  4359. this.expectContextual(128);
  4360. node.id = this.flowParseRestrictedIdentifier(true, true);
  4361. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4362. if (this.match(47)) {
  4363. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4364. } else {
  4365. node.typeParameters = null;
  4366. }
  4367. node.supertype = null;
  4368. if (this.match(14)) {
  4369. node.supertype = this.flowParseTypeInitialiser(14);
  4370. }
  4371. node.impltype = null;
  4372. if (!declare) {
  4373. node.impltype = this.flowParseTypeInitialiser(29);
  4374. }
  4375. this.semicolon();
  4376. return this.finishNode(node, "OpaqueType");
  4377. }
  4378. flowParseTypeParameter(requireDefault = false) {
  4379. const nodeStartLoc = this.state.startLoc;
  4380. const node = this.startNode();
  4381. const variance = this.flowParseVariance();
  4382. const ident = this.flowParseTypeAnnotatableIdentifier();
  4383. node.name = ident.name;
  4384. node.variance = variance;
  4385. node.bound = ident.typeAnnotation;
  4386. if (this.match(29)) {
  4387. this.eat(29);
  4388. node.default = this.flowParseType();
  4389. } else {
  4390. if (requireDefault) {
  4391. this.raise(FlowErrors.MissingTypeParamDefault, {
  4392. at: nodeStartLoc
  4393. });
  4394. }
  4395. }
  4396. return this.finishNode(node, "TypeParameter");
  4397. }
  4398. flowParseTypeParameterDeclaration() {
  4399. const oldInType = this.state.inType;
  4400. const node = this.startNode();
  4401. node.params = [];
  4402. this.state.inType = true;
  4403. if (this.match(47) || this.match(140)) {
  4404. this.next();
  4405. } else {
  4406. this.unexpected();
  4407. }
  4408. let defaultRequired = false;
  4409. do {
  4410. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  4411. node.params.push(typeParameter);
  4412. if (typeParameter.default) {
  4413. defaultRequired = true;
  4414. }
  4415. if (!this.match(48)) {
  4416. this.expect(12);
  4417. }
  4418. } while (!this.match(48));
  4419. this.expect(48);
  4420. this.state.inType = oldInType;
  4421. return this.finishNode(node, "TypeParameterDeclaration");
  4422. }
  4423. flowParseTypeParameterInstantiation() {
  4424. const node = this.startNode();
  4425. const oldInType = this.state.inType;
  4426. node.params = [];
  4427. this.state.inType = true;
  4428. this.expect(47);
  4429. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4430. this.state.noAnonFunctionType = false;
  4431. while (!this.match(48)) {
  4432. node.params.push(this.flowParseType());
  4433. if (!this.match(48)) {
  4434. this.expect(12);
  4435. }
  4436. }
  4437. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4438. this.expect(48);
  4439. this.state.inType = oldInType;
  4440. return this.finishNode(node, "TypeParameterInstantiation");
  4441. }
  4442. flowParseTypeParameterInstantiationCallOrNew() {
  4443. const node = this.startNode();
  4444. const oldInType = this.state.inType;
  4445. node.params = [];
  4446. this.state.inType = true;
  4447. this.expect(47);
  4448. while (!this.match(48)) {
  4449. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  4450. if (!this.match(48)) {
  4451. this.expect(12);
  4452. }
  4453. }
  4454. this.expect(48);
  4455. this.state.inType = oldInType;
  4456. return this.finishNode(node, "TypeParameterInstantiation");
  4457. }
  4458. flowParseInterfaceType() {
  4459. const node = this.startNode();
  4460. this.expectContextual(127);
  4461. node.extends = [];
  4462. if (this.eat(81)) {
  4463. do {
  4464. node.extends.push(this.flowParseInterfaceExtends());
  4465. } while (this.eat(12));
  4466. }
  4467. node.body = this.flowParseObjectType({
  4468. allowStatic: false,
  4469. allowExact: false,
  4470. allowSpread: false,
  4471. allowProto: false,
  4472. allowInexact: false
  4473. });
  4474. return this.finishNode(node, "InterfaceTypeAnnotation");
  4475. }
  4476. flowParseObjectPropertyKey() {
  4477. return this.match(132) || this.match(131) ? super.parseExprAtom() : this.parseIdentifier(true);
  4478. }
  4479. flowParseObjectTypeIndexer(node, isStatic, variance) {
  4480. node.static = isStatic;
  4481. if (this.lookahead().type === 14) {
  4482. node.id = this.flowParseObjectPropertyKey();
  4483. node.key = this.flowParseTypeInitialiser();
  4484. } else {
  4485. node.id = null;
  4486. node.key = this.flowParseType();
  4487. }
  4488. this.expect(3);
  4489. node.value = this.flowParseTypeInitialiser();
  4490. node.variance = variance;
  4491. return this.finishNode(node, "ObjectTypeIndexer");
  4492. }
  4493. flowParseObjectTypeInternalSlot(node, isStatic) {
  4494. node.static = isStatic;
  4495. node.id = this.flowParseObjectPropertyKey();
  4496. this.expect(3);
  4497. this.expect(3);
  4498. if (this.match(47) || this.match(10)) {
  4499. node.method = true;
  4500. node.optional = false;
  4501. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4502. } else {
  4503. node.method = false;
  4504. if (this.eat(17)) {
  4505. node.optional = true;
  4506. }
  4507. node.value = this.flowParseTypeInitialiser();
  4508. }
  4509. return this.finishNode(node, "ObjectTypeInternalSlot");
  4510. }
  4511. flowParseObjectTypeMethodish(node) {
  4512. node.params = [];
  4513. node.rest = null;
  4514. node.typeParameters = null;
  4515. node.this = null;
  4516. if (this.match(47)) {
  4517. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4518. }
  4519. this.expect(10);
  4520. if (this.match(78)) {
  4521. node.this = this.flowParseFunctionTypeParam(true);
  4522. node.this.name = null;
  4523. if (!this.match(11)) {
  4524. this.expect(12);
  4525. }
  4526. }
  4527. while (!this.match(11) && !this.match(21)) {
  4528. node.params.push(this.flowParseFunctionTypeParam(false));
  4529. if (!this.match(11)) {
  4530. this.expect(12);
  4531. }
  4532. }
  4533. if (this.eat(21)) {
  4534. node.rest = this.flowParseFunctionTypeParam(false);
  4535. }
  4536. this.expect(11);
  4537. node.returnType = this.flowParseTypeInitialiser();
  4538. return this.finishNode(node, "FunctionTypeAnnotation");
  4539. }
  4540. flowParseObjectTypeCallProperty(node, isStatic) {
  4541. const valueNode = this.startNode();
  4542. node.static = isStatic;
  4543. node.value = this.flowParseObjectTypeMethodish(valueNode);
  4544. return this.finishNode(node, "ObjectTypeCallProperty");
  4545. }
  4546. flowParseObjectType({
  4547. allowStatic,
  4548. allowExact,
  4549. allowSpread,
  4550. allowProto,
  4551. allowInexact
  4552. }) {
  4553. const oldInType = this.state.inType;
  4554. this.state.inType = true;
  4555. const nodeStart = this.startNode();
  4556. nodeStart.callProperties = [];
  4557. nodeStart.properties = [];
  4558. nodeStart.indexers = [];
  4559. nodeStart.internalSlots = [];
  4560. let endDelim;
  4561. let exact;
  4562. let inexact = false;
  4563. if (allowExact && this.match(6)) {
  4564. this.expect(6);
  4565. endDelim = 9;
  4566. exact = true;
  4567. } else {
  4568. this.expect(5);
  4569. endDelim = 8;
  4570. exact = false;
  4571. }
  4572. nodeStart.exact = exact;
  4573. while (!this.match(endDelim)) {
  4574. let isStatic = false;
  4575. let protoStartLoc = null;
  4576. let inexactStartLoc = null;
  4577. const node = this.startNode();
  4578. if (allowProto && this.isContextual(116)) {
  4579. const lookahead = this.lookahead();
  4580. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4581. this.next();
  4582. protoStartLoc = this.state.startLoc;
  4583. allowStatic = false;
  4584. }
  4585. }
  4586. if (allowStatic && this.isContextual(104)) {
  4587. const lookahead = this.lookahead();
  4588. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4589. this.next();
  4590. isStatic = true;
  4591. }
  4592. }
  4593. const variance = this.flowParseVariance();
  4594. if (this.eat(0)) {
  4595. if (protoStartLoc != null) {
  4596. this.unexpected(protoStartLoc);
  4597. }
  4598. if (this.eat(0)) {
  4599. if (variance) {
  4600. this.unexpected(variance.loc.start);
  4601. }
  4602. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  4603. } else {
  4604. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  4605. }
  4606. } else if (this.match(10) || this.match(47)) {
  4607. if (protoStartLoc != null) {
  4608. this.unexpected(protoStartLoc);
  4609. }
  4610. if (variance) {
  4611. this.unexpected(variance.loc.start);
  4612. }
  4613. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  4614. } else {
  4615. let kind = "init";
  4616. if (this.isContextual(98) || this.isContextual(103)) {
  4617. const lookahead = this.lookahead();
  4618. if (tokenIsLiteralPropertyName(lookahead.type)) {
  4619. kind = this.state.value;
  4620. this.next();
  4621. }
  4622. }
  4623. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  4624. if (propOrInexact === null) {
  4625. inexact = true;
  4626. inexactStartLoc = this.state.lastTokStartLoc;
  4627. } else {
  4628. nodeStart.properties.push(propOrInexact);
  4629. }
  4630. }
  4631. this.flowObjectTypeSemicolon();
  4632. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  4633. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, {
  4634. at: inexactStartLoc
  4635. });
  4636. }
  4637. }
  4638. this.expect(endDelim);
  4639. if (allowSpread) {
  4640. nodeStart.inexact = inexact;
  4641. }
  4642. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  4643. this.state.inType = oldInType;
  4644. return out;
  4645. }
  4646. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  4647. if (this.eat(21)) {
  4648. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  4649. if (isInexactToken) {
  4650. if (!allowSpread) {
  4651. this.raise(FlowErrors.InexactInsideNonObject, {
  4652. at: this.state.lastTokStartLoc
  4653. });
  4654. } else if (!allowInexact) {
  4655. this.raise(FlowErrors.InexactInsideExact, {
  4656. at: this.state.lastTokStartLoc
  4657. });
  4658. }
  4659. if (variance) {
  4660. this.raise(FlowErrors.InexactVariance, {
  4661. at: variance
  4662. });
  4663. }
  4664. return null;
  4665. }
  4666. if (!allowSpread) {
  4667. this.raise(FlowErrors.UnexpectedSpreadType, {
  4668. at: this.state.lastTokStartLoc
  4669. });
  4670. }
  4671. if (protoStartLoc != null) {
  4672. this.unexpected(protoStartLoc);
  4673. }
  4674. if (variance) {
  4675. this.raise(FlowErrors.SpreadVariance, {
  4676. at: variance
  4677. });
  4678. }
  4679. node.argument = this.flowParseType();
  4680. return this.finishNode(node, "ObjectTypeSpreadProperty");
  4681. } else {
  4682. node.key = this.flowParseObjectPropertyKey();
  4683. node.static = isStatic;
  4684. node.proto = protoStartLoc != null;
  4685. node.kind = kind;
  4686. let optional = false;
  4687. if (this.match(47) || this.match(10)) {
  4688. node.method = true;
  4689. if (protoStartLoc != null) {
  4690. this.unexpected(protoStartLoc);
  4691. }
  4692. if (variance) {
  4693. this.unexpected(variance.loc.start);
  4694. }
  4695. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4696. if (kind === "get" || kind === "set") {
  4697. this.flowCheckGetterSetterParams(node);
  4698. }
  4699. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  4700. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  4701. at: node.value.this
  4702. });
  4703. }
  4704. } else {
  4705. if (kind !== "init") this.unexpected();
  4706. node.method = false;
  4707. if (this.eat(17)) {
  4708. optional = true;
  4709. }
  4710. node.value = this.flowParseTypeInitialiser();
  4711. node.variance = variance;
  4712. }
  4713. node.optional = optional;
  4714. return this.finishNode(node, "ObjectTypeProperty");
  4715. }
  4716. }
  4717. flowCheckGetterSetterParams(property) {
  4718. const paramCount = property.kind === "get" ? 0 : 1;
  4719. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  4720. if (property.value.this) {
  4721. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, {
  4722. at: property.value.this
  4723. });
  4724. }
  4725. if (length !== paramCount) {
  4726. this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  4727. at: property
  4728. });
  4729. }
  4730. if (property.kind === "set" && property.value.rest) {
  4731. this.raise(Errors.BadSetterRestParameter, {
  4732. at: property
  4733. });
  4734. }
  4735. }
  4736. flowObjectTypeSemicolon() {
  4737. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  4738. this.unexpected();
  4739. }
  4740. }
  4741. flowParseQualifiedTypeIdentifier(startLoc, id) {
  4742. var _startLoc;
  4743. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  4744. let node = id || this.flowParseRestrictedIdentifier(true);
  4745. while (this.eat(16)) {
  4746. const node2 = this.startNodeAt(startLoc);
  4747. node2.qualification = node;
  4748. node2.id = this.flowParseRestrictedIdentifier(true);
  4749. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  4750. }
  4751. return node;
  4752. }
  4753. flowParseGenericType(startLoc, id) {
  4754. const node = this.startNodeAt(startLoc);
  4755. node.typeParameters = null;
  4756. node.id = this.flowParseQualifiedTypeIdentifier(startLoc, id);
  4757. if (this.match(47)) {
  4758. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4759. }
  4760. return this.finishNode(node, "GenericTypeAnnotation");
  4761. }
  4762. flowParseTypeofType() {
  4763. const node = this.startNode();
  4764. this.expect(87);
  4765. node.argument = this.flowParsePrimaryType();
  4766. return this.finishNode(node, "TypeofTypeAnnotation");
  4767. }
  4768. flowParseTupleType() {
  4769. const node = this.startNode();
  4770. node.types = [];
  4771. this.expect(0);
  4772. while (this.state.pos < this.length && !this.match(3)) {
  4773. node.types.push(this.flowParseType());
  4774. if (this.match(3)) break;
  4775. this.expect(12);
  4776. }
  4777. this.expect(3);
  4778. return this.finishNode(node, "TupleTypeAnnotation");
  4779. }
  4780. flowParseFunctionTypeParam(first) {
  4781. let name = null;
  4782. let optional = false;
  4783. let typeAnnotation = null;
  4784. const node = this.startNode();
  4785. const lh = this.lookahead();
  4786. const isThis = this.state.type === 78;
  4787. if (lh.type === 14 || lh.type === 17) {
  4788. if (isThis && !first) {
  4789. this.raise(FlowErrors.ThisParamMustBeFirst, {
  4790. at: node
  4791. });
  4792. }
  4793. name = this.parseIdentifier(isThis);
  4794. if (this.eat(17)) {
  4795. optional = true;
  4796. if (isThis) {
  4797. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  4798. at: node
  4799. });
  4800. }
  4801. }
  4802. typeAnnotation = this.flowParseTypeInitialiser();
  4803. } else {
  4804. typeAnnotation = this.flowParseType();
  4805. }
  4806. node.name = name;
  4807. node.optional = optional;
  4808. node.typeAnnotation = typeAnnotation;
  4809. return this.finishNode(node, "FunctionTypeParam");
  4810. }
  4811. reinterpretTypeAsFunctionTypeParam(type) {
  4812. const node = this.startNodeAt(type.loc.start);
  4813. node.name = null;
  4814. node.optional = false;
  4815. node.typeAnnotation = type;
  4816. return this.finishNode(node, "FunctionTypeParam");
  4817. }
  4818. flowParseFunctionTypeParams(params = []) {
  4819. let rest = null;
  4820. let _this = null;
  4821. if (this.match(78)) {
  4822. _this = this.flowParseFunctionTypeParam(true);
  4823. _this.name = null;
  4824. if (!this.match(11)) {
  4825. this.expect(12);
  4826. }
  4827. }
  4828. while (!this.match(11) && !this.match(21)) {
  4829. params.push(this.flowParseFunctionTypeParam(false));
  4830. if (!this.match(11)) {
  4831. this.expect(12);
  4832. }
  4833. }
  4834. if (this.eat(21)) {
  4835. rest = this.flowParseFunctionTypeParam(false);
  4836. }
  4837. return {
  4838. params,
  4839. rest,
  4840. _this
  4841. };
  4842. }
  4843. flowIdentToTypeAnnotation(startLoc, node, id) {
  4844. switch (id.name) {
  4845. case "any":
  4846. return this.finishNode(node, "AnyTypeAnnotation");
  4847. case "bool":
  4848. case "boolean":
  4849. return this.finishNode(node, "BooleanTypeAnnotation");
  4850. case "mixed":
  4851. return this.finishNode(node, "MixedTypeAnnotation");
  4852. case "empty":
  4853. return this.finishNode(node, "EmptyTypeAnnotation");
  4854. case "number":
  4855. return this.finishNode(node, "NumberTypeAnnotation");
  4856. case "string":
  4857. return this.finishNode(node, "StringTypeAnnotation");
  4858. case "symbol":
  4859. return this.finishNode(node, "SymbolTypeAnnotation");
  4860. default:
  4861. this.checkNotUnderscore(id.name);
  4862. return this.flowParseGenericType(startLoc, id);
  4863. }
  4864. }
  4865. flowParsePrimaryType() {
  4866. const startLoc = this.state.startLoc;
  4867. const node = this.startNode();
  4868. let tmp;
  4869. let type;
  4870. let isGroupedType = false;
  4871. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4872. switch (this.state.type) {
  4873. case 5:
  4874. return this.flowParseObjectType({
  4875. allowStatic: false,
  4876. allowExact: false,
  4877. allowSpread: true,
  4878. allowProto: false,
  4879. allowInexact: true
  4880. });
  4881. case 6:
  4882. return this.flowParseObjectType({
  4883. allowStatic: false,
  4884. allowExact: true,
  4885. allowSpread: true,
  4886. allowProto: false,
  4887. allowInexact: false
  4888. });
  4889. case 0:
  4890. this.state.noAnonFunctionType = false;
  4891. type = this.flowParseTupleType();
  4892. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4893. return type;
  4894. case 47:
  4895. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4896. this.expect(10);
  4897. tmp = this.flowParseFunctionTypeParams();
  4898. node.params = tmp.params;
  4899. node.rest = tmp.rest;
  4900. node.this = tmp._this;
  4901. this.expect(11);
  4902. this.expect(19);
  4903. node.returnType = this.flowParseType();
  4904. return this.finishNode(node, "FunctionTypeAnnotation");
  4905. case 10:
  4906. this.next();
  4907. if (!this.match(11) && !this.match(21)) {
  4908. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  4909. const token = this.lookahead().type;
  4910. isGroupedType = token !== 17 && token !== 14;
  4911. } else {
  4912. isGroupedType = true;
  4913. }
  4914. }
  4915. if (isGroupedType) {
  4916. this.state.noAnonFunctionType = false;
  4917. type = this.flowParseType();
  4918. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4919. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  4920. this.expect(11);
  4921. return type;
  4922. } else {
  4923. this.eat(12);
  4924. }
  4925. }
  4926. if (type) {
  4927. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  4928. } else {
  4929. tmp = this.flowParseFunctionTypeParams();
  4930. }
  4931. node.params = tmp.params;
  4932. node.rest = tmp.rest;
  4933. node.this = tmp._this;
  4934. this.expect(11);
  4935. this.expect(19);
  4936. node.returnType = this.flowParseType();
  4937. node.typeParameters = null;
  4938. return this.finishNode(node, "FunctionTypeAnnotation");
  4939. case 131:
  4940. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  4941. case 85:
  4942. case 86:
  4943. node.value = this.match(85);
  4944. this.next();
  4945. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  4946. case 53:
  4947. if (this.state.value === "-") {
  4948. this.next();
  4949. if (this.match(132)) {
  4950. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  4951. }
  4952. if (this.match(133)) {
  4953. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  4954. }
  4955. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, {
  4956. at: this.state.startLoc
  4957. });
  4958. }
  4959. this.unexpected();
  4960. return;
  4961. case 132:
  4962. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  4963. case 133:
  4964. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  4965. case 88:
  4966. this.next();
  4967. return this.finishNode(node, "VoidTypeAnnotation");
  4968. case 84:
  4969. this.next();
  4970. return this.finishNode(node, "NullLiteralTypeAnnotation");
  4971. case 78:
  4972. this.next();
  4973. return this.finishNode(node, "ThisTypeAnnotation");
  4974. case 55:
  4975. this.next();
  4976. return this.finishNode(node, "ExistsTypeAnnotation");
  4977. case 87:
  4978. return this.flowParseTypeofType();
  4979. default:
  4980. if (tokenIsKeyword(this.state.type)) {
  4981. const label = tokenLabelName(this.state.type);
  4982. this.next();
  4983. return super.createIdentifier(node, label);
  4984. } else if (tokenIsIdentifier(this.state.type)) {
  4985. if (this.isContextual(127)) {
  4986. return this.flowParseInterfaceType();
  4987. }
  4988. return this.flowIdentToTypeAnnotation(startLoc, node, this.parseIdentifier());
  4989. }
  4990. }
  4991. this.unexpected();
  4992. }
  4993. flowParsePostfixType() {
  4994. const startLoc = this.state.startLoc;
  4995. let type = this.flowParsePrimaryType();
  4996. let seenOptionalIndexedAccess = false;
  4997. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  4998. const node = this.startNodeAt(startLoc);
  4999. const optional = this.eat(18);
  5000. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  5001. this.expect(0);
  5002. if (!optional && this.match(3)) {
  5003. node.elementType = type;
  5004. this.next();
  5005. type = this.finishNode(node, "ArrayTypeAnnotation");
  5006. } else {
  5007. node.objectType = type;
  5008. node.indexType = this.flowParseType();
  5009. this.expect(3);
  5010. if (seenOptionalIndexedAccess) {
  5011. node.optional = optional;
  5012. type = this.finishNode(node, "OptionalIndexedAccessType");
  5013. } else {
  5014. type = this.finishNode(node, "IndexedAccessType");
  5015. }
  5016. }
  5017. }
  5018. return type;
  5019. }
  5020. flowParsePrefixType() {
  5021. const node = this.startNode();
  5022. if (this.eat(17)) {
  5023. node.typeAnnotation = this.flowParsePrefixType();
  5024. return this.finishNode(node, "NullableTypeAnnotation");
  5025. } else {
  5026. return this.flowParsePostfixType();
  5027. }
  5028. }
  5029. flowParseAnonFunctionWithoutParens() {
  5030. const param = this.flowParsePrefixType();
  5031. if (!this.state.noAnonFunctionType && this.eat(19)) {
  5032. const node = this.startNodeAt(param.loc.start);
  5033. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  5034. node.rest = null;
  5035. node.this = null;
  5036. node.returnType = this.flowParseType();
  5037. node.typeParameters = null;
  5038. return this.finishNode(node, "FunctionTypeAnnotation");
  5039. }
  5040. return param;
  5041. }
  5042. flowParseIntersectionType() {
  5043. const node = this.startNode();
  5044. this.eat(45);
  5045. const type = this.flowParseAnonFunctionWithoutParens();
  5046. node.types = [type];
  5047. while (this.eat(45)) {
  5048. node.types.push(this.flowParseAnonFunctionWithoutParens());
  5049. }
  5050. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  5051. }
  5052. flowParseUnionType() {
  5053. const node = this.startNode();
  5054. this.eat(43);
  5055. const type = this.flowParseIntersectionType();
  5056. node.types = [type];
  5057. while (this.eat(43)) {
  5058. node.types.push(this.flowParseIntersectionType());
  5059. }
  5060. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  5061. }
  5062. flowParseType() {
  5063. const oldInType = this.state.inType;
  5064. this.state.inType = true;
  5065. const type = this.flowParseUnionType();
  5066. this.state.inType = oldInType;
  5067. return type;
  5068. }
  5069. flowParseTypeOrImplicitInstantiation() {
  5070. if (this.state.type === 130 && this.state.value === "_") {
  5071. const startLoc = this.state.startLoc;
  5072. const node = this.parseIdentifier();
  5073. return this.flowParseGenericType(startLoc, node);
  5074. } else {
  5075. return this.flowParseType();
  5076. }
  5077. }
  5078. flowParseTypeAnnotation() {
  5079. const node = this.startNode();
  5080. node.typeAnnotation = this.flowParseTypeInitialiser();
  5081. return this.finishNode(node, "TypeAnnotation");
  5082. }
  5083. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  5084. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  5085. if (this.match(14)) {
  5086. ident.typeAnnotation = this.flowParseTypeAnnotation();
  5087. this.resetEndLocation(ident);
  5088. }
  5089. return ident;
  5090. }
  5091. typeCastToParameter(node) {
  5092. node.expression.typeAnnotation = node.typeAnnotation;
  5093. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  5094. return node.expression;
  5095. }
  5096. flowParseVariance() {
  5097. let variance = null;
  5098. if (this.match(53)) {
  5099. variance = this.startNode();
  5100. if (this.state.value === "+") {
  5101. variance.kind = "plus";
  5102. } else {
  5103. variance.kind = "minus";
  5104. }
  5105. this.next();
  5106. return this.finishNode(variance, "Variance");
  5107. }
  5108. return variance;
  5109. }
  5110. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  5111. if (allowExpressionBody) {
  5112. this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  5113. return;
  5114. }
  5115. super.parseFunctionBody(node, false, isMethod);
  5116. }
  5117. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5118. if (this.match(14)) {
  5119. const typeNode = this.startNode();
  5120. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5121. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  5122. }
  5123. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  5124. }
  5125. parseStatementLike(flags) {
  5126. if (this.state.strict && this.isContextual(127)) {
  5127. const lookahead = this.lookahead();
  5128. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  5129. const node = this.startNode();
  5130. this.next();
  5131. return this.flowParseInterface(node);
  5132. }
  5133. } else if (this.shouldParseEnums() && this.isContextual(124)) {
  5134. const node = this.startNode();
  5135. this.next();
  5136. return this.flowParseEnumDeclaration(node);
  5137. }
  5138. const stmt = super.parseStatementLike(flags);
  5139. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  5140. this.flowPragma = null;
  5141. }
  5142. return stmt;
  5143. }
  5144. parseExpressionStatement(node, expr, decorators) {
  5145. if (expr.type === "Identifier") {
  5146. if (expr.name === "declare") {
  5147. if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
  5148. return this.flowParseDeclare(node);
  5149. }
  5150. } else if (tokenIsIdentifier(this.state.type)) {
  5151. if (expr.name === "interface") {
  5152. return this.flowParseInterface(node);
  5153. } else if (expr.name === "type") {
  5154. return this.flowParseTypeAlias(node);
  5155. } else if (expr.name === "opaque") {
  5156. return this.flowParseOpaqueType(node, false);
  5157. }
  5158. }
  5159. }
  5160. return super.parseExpressionStatement(node, expr, decorators);
  5161. }
  5162. shouldParseExportDeclaration() {
  5163. const {
  5164. type
  5165. } = this.state;
  5166. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 124) {
  5167. return !this.state.containsEsc;
  5168. }
  5169. return super.shouldParseExportDeclaration();
  5170. }
  5171. isExportDefaultSpecifier() {
  5172. const {
  5173. type
  5174. } = this.state;
  5175. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 124) {
  5176. return this.state.containsEsc;
  5177. }
  5178. return super.isExportDefaultSpecifier();
  5179. }
  5180. parseExportDefaultExpression() {
  5181. if (this.shouldParseEnums() && this.isContextual(124)) {
  5182. const node = this.startNode();
  5183. this.next();
  5184. return this.flowParseEnumDeclaration(node);
  5185. }
  5186. return super.parseExportDefaultExpression();
  5187. }
  5188. parseConditional(expr, startLoc, refExpressionErrors) {
  5189. if (!this.match(17)) return expr;
  5190. if (this.state.maybeInArrowParameters) {
  5191. const nextCh = this.lookaheadCharCode();
  5192. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  5193. this.setOptionalParametersError(refExpressionErrors);
  5194. return expr;
  5195. }
  5196. }
  5197. this.expect(17);
  5198. const state = this.state.clone();
  5199. const originalNoArrowAt = this.state.noArrowAt;
  5200. const node = this.startNodeAt(startLoc);
  5201. let {
  5202. consequent,
  5203. failed
  5204. } = this.tryParseConditionalConsequent();
  5205. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5206. if (failed || invalid.length > 0) {
  5207. const noArrowAt = [...originalNoArrowAt];
  5208. if (invalid.length > 0) {
  5209. this.state = state;
  5210. this.state.noArrowAt = noArrowAt;
  5211. for (let i = 0; i < invalid.length; i++) {
  5212. noArrowAt.push(invalid[i].start);
  5213. }
  5214. ({
  5215. consequent,
  5216. failed
  5217. } = this.tryParseConditionalConsequent());
  5218. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5219. }
  5220. if (failed && valid.length > 1) {
  5221. this.raise(FlowErrors.AmbiguousConditionalArrow, {
  5222. at: state.startLoc
  5223. });
  5224. }
  5225. if (failed && valid.length === 1) {
  5226. this.state = state;
  5227. noArrowAt.push(valid[0].start);
  5228. this.state.noArrowAt = noArrowAt;
  5229. ({
  5230. consequent,
  5231. failed
  5232. } = this.tryParseConditionalConsequent());
  5233. }
  5234. }
  5235. this.getArrowLikeExpressions(consequent, true);
  5236. this.state.noArrowAt = originalNoArrowAt;
  5237. this.expect(14);
  5238. node.test = expr;
  5239. node.consequent = consequent;
  5240. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  5241. return this.finishNode(node, "ConditionalExpression");
  5242. }
  5243. tryParseConditionalConsequent() {
  5244. this.state.noArrowParamsConversionAt.push(this.state.start);
  5245. const consequent = this.parseMaybeAssignAllowIn();
  5246. const failed = !this.match(14);
  5247. this.state.noArrowParamsConversionAt.pop();
  5248. return {
  5249. consequent,
  5250. failed
  5251. };
  5252. }
  5253. getArrowLikeExpressions(node, disallowInvalid) {
  5254. const stack = [node];
  5255. const arrows = [];
  5256. while (stack.length !== 0) {
  5257. const node = stack.pop();
  5258. if (node.type === "ArrowFunctionExpression") {
  5259. if (node.typeParameters || !node.returnType) {
  5260. this.finishArrowValidation(node);
  5261. } else {
  5262. arrows.push(node);
  5263. }
  5264. stack.push(node.body);
  5265. } else if (node.type === "ConditionalExpression") {
  5266. stack.push(node.consequent);
  5267. stack.push(node.alternate);
  5268. }
  5269. }
  5270. if (disallowInvalid) {
  5271. arrows.forEach(node => this.finishArrowValidation(node));
  5272. return [arrows, []];
  5273. }
  5274. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  5275. }
  5276. finishArrowValidation(node) {
  5277. var _node$extra;
  5278. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  5279. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  5280. super.checkParams(node, false, true);
  5281. this.scope.exit();
  5282. }
  5283. forwardNoArrowParamsConversionAt(node, parse) {
  5284. let result;
  5285. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5286. this.state.noArrowParamsConversionAt.push(this.state.start);
  5287. result = parse();
  5288. this.state.noArrowParamsConversionAt.pop();
  5289. } else {
  5290. result = parse();
  5291. }
  5292. return result;
  5293. }
  5294. parseParenItem(node, startLoc) {
  5295. node = super.parseParenItem(node, startLoc);
  5296. if (this.eat(17)) {
  5297. node.optional = true;
  5298. this.resetEndLocation(node);
  5299. }
  5300. if (this.match(14)) {
  5301. const typeCastNode = this.startNodeAt(startLoc);
  5302. typeCastNode.expression = node;
  5303. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  5304. return this.finishNode(typeCastNode, "TypeCastExpression");
  5305. }
  5306. return node;
  5307. }
  5308. assertModuleNodeAllowed(node) {
  5309. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  5310. return;
  5311. }
  5312. super.assertModuleNodeAllowed(node);
  5313. }
  5314. parseExport(node, decorators) {
  5315. const decl = super.parseExport(node, decorators);
  5316. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  5317. decl.exportKind = decl.exportKind || "value";
  5318. }
  5319. return decl;
  5320. }
  5321. parseExportDeclaration(node) {
  5322. if (this.isContextual(128)) {
  5323. node.exportKind = "type";
  5324. const declarationNode = this.startNode();
  5325. this.next();
  5326. if (this.match(5)) {
  5327. node.specifiers = this.parseExportSpecifiers(true);
  5328. super.parseExportFrom(node);
  5329. return null;
  5330. } else {
  5331. return this.flowParseTypeAlias(declarationNode);
  5332. }
  5333. } else if (this.isContextual(129)) {
  5334. node.exportKind = "type";
  5335. const declarationNode = this.startNode();
  5336. this.next();
  5337. return this.flowParseOpaqueType(declarationNode, false);
  5338. } else if (this.isContextual(127)) {
  5339. node.exportKind = "type";
  5340. const declarationNode = this.startNode();
  5341. this.next();
  5342. return this.flowParseInterface(declarationNode);
  5343. } else if (this.shouldParseEnums() && this.isContextual(124)) {
  5344. node.exportKind = "value";
  5345. const declarationNode = this.startNode();
  5346. this.next();
  5347. return this.flowParseEnumDeclaration(declarationNode);
  5348. } else {
  5349. return super.parseExportDeclaration(node);
  5350. }
  5351. }
  5352. eatExportStar(node) {
  5353. if (super.eatExportStar(node)) return true;
  5354. if (this.isContextual(128) && this.lookahead().type === 55) {
  5355. node.exportKind = "type";
  5356. this.next();
  5357. this.next();
  5358. return true;
  5359. }
  5360. return false;
  5361. }
  5362. maybeParseExportNamespaceSpecifier(node) {
  5363. const {
  5364. startLoc
  5365. } = this.state;
  5366. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  5367. if (hasNamespace && node.exportKind === "type") {
  5368. this.unexpected(startLoc);
  5369. }
  5370. return hasNamespace;
  5371. }
  5372. parseClassId(node, isStatement, optionalId) {
  5373. super.parseClassId(node, isStatement, optionalId);
  5374. if (this.match(47)) {
  5375. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5376. }
  5377. }
  5378. parseClassMember(classBody, member, state) {
  5379. const {
  5380. startLoc
  5381. } = this.state;
  5382. if (this.isContextual(123)) {
  5383. if (super.parseClassMemberFromModifier(classBody, member)) {
  5384. return;
  5385. }
  5386. member.declare = true;
  5387. }
  5388. super.parseClassMember(classBody, member, state);
  5389. if (member.declare) {
  5390. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  5391. this.raise(FlowErrors.DeclareClassElement, {
  5392. at: startLoc
  5393. });
  5394. } else if (member.value) {
  5395. this.raise(FlowErrors.DeclareClassFieldInitializer, {
  5396. at: member.value
  5397. });
  5398. }
  5399. }
  5400. }
  5401. isIterator(word) {
  5402. return word === "iterator" || word === "asyncIterator";
  5403. }
  5404. readIterator() {
  5405. const word = super.readWord1();
  5406. const fullWord = "@@" + word;
  5407. if (!this.isIterator(word) || !this.state.inType) {
  5408. this.raise(Errors.InvalidIdentifier, {
  5409. at: this.state.curPosition(),
  5410. identifierName: fullWord
  5411. });
  5412. }
  5413. this.finishToken(130, fullWord);
  5414. }
  5415. getTokenFromCode(code) {
  5416. const next = this.input.charCodeAt(this.state.pos + 1);
  5417. if (code === 123 && next === 124) {
  5418. this.finishOp(6, 2);
  5419. } else if (this.state.inType && (code === 62 || code === 60)) {
  5420. this.finishOp(code === 62 ? 48 : 47, 1);
  5421. } else if (this.state.inType && code === 63) {
  5422. if (next === 46) {
  5423. this.finishOp(18, 2);
  5424. } else {
  5425. this.finishOp(17, 1);
  5426. }
  5427. } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
  5428. this.state.pos += 2;
  5429. this.readIterator();
  5430. } else {
  5431. super.getTokenFromCode(code);
  5432. }
  5433. }
  5434. isAssignable(node, isBinding) {
  5435. if (node.type === "TypeCastExpression") {
  5436. return this.isAssignable(node.expression, isBinding);
  5437. } else {
  5438. return super.isAssignable(node, isBinding);
  5439. }
  5440. }
  5441. toAssignable(node, isLHS = false) {
  5442. if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
  5443. node.left = this.typeCastToParameter(node.left);
  5444. }
  5445. super.toAssignable(node, isLHS);
  5446. }
  5447. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  5448. for (let i = 0; i < exprList.length; i++) {
  5449. const expr = exprList[i];
  5450. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  5451. exprList[i] = this.typeCastToParameter(expr);
  5452. }
  5453. }
  5454. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  5455. }
  5456. toReferencedList(exprList, isParenthesizedExpr) {
  5457. for (let i = 0; i < exprList.length; i++) {
  5458. var _expr$extra;
  5459. const expr = exprList[i];
  5460. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  5461. this.raise(FlowErrors.TypeCastInPattern, {
  5462. at: expr.typeAnnotation
  5463. });
  5464. }
  5465. }
  5466. return exprList;
  5467. }
  5468. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  5469. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  5470. if (canBePattern && !this.state.maybeInArrowParameters) {
  5471. this.toReferencedList(node.elements);
  5472. }
  5473. return node;
  5474. }
  5475. isValidLVal(type, isParenthesized, binding) {
  5476. return type === "TypeCastExpression" || super.isValidLVal(type, isParenthesized, binding);
  5477. }
  5478. parseClassProperty(node) {
  5479. if (this.match(14)) {
  5480. node.typeAnnotation = this.flowParseTypeAnnotation();
  5481. }
  5482. return super.parseClassProperty(node);
  5483. }
  5484. parseClassPrivateProperty(node) {
  5485. if (this.match(14)) {
  5486. node.typeAnnotation = this.flowParseTypeAnnotation();
  5487. }
  5488. return super.parseClassPrivateProperty(node);
  5489. }
  5490. isClassMethod() {
  5491. return this.match(47) || super.isClassMethod();
  5492. }
  5493. isClassProperty() {
  5494. return this.match(14) || super.isClassProperty();
  5495. }
  5496. isNonstaticConstructor(method) {
  5497. return !this.match(14) && super.isNonstaticConstructor(method);
  5498. }
  5499. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  5500. if (method.variance) {
  5501. this.unexpected(method.variance.loc.start);
  5502. }
  5503. delete method.variance;
  5504. if (this.match(47)) {
  5505. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5506. }
  5507. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  5508. if (method.params && isConstructor) {
  5509. const params = method.params;
  5510. if (params.length > 0 && this.isThisParam(params[0])) {
  5511. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5512. at: method
  5513. });
  5514. }
  5515. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  5516. const params = method.value.params;
  5517. if (params.length > 0 && this.isThisParam(params[0])) {
  5518. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5519. at: method
  5520. });
  5521. }
  5522. }
  5523. }
  5524. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  5525. if (method.variance) {
  5526. this.unexpected(method.variance.loc.start);
  5527. }
  5528. delete method.variance;
  5529. if (this.match(47)) {
  5530. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5531. }
  5532. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  5533. }
  5534. parseClassSuper(node) {
  5535. super.parseClassSuper(node);
  5536. if (node.superClass && this.match(47)) {
  5537. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  5538. }
  5539. if (this.isContextual(111)) {
  5540. this.next();
  5541. const implemented = node.implements = [];
  5542. do {
  5543. const node = this.startNode();
  5544. node.id = this.flowParseRestrictedIdentifier(true);
  5545. if (this.match(47)) {
  5546. node.typeParameters = this.flowParseTypeParameterInstantiation();
  5547. } else {
  5548. node.typeParameters = null;
  5549. }
  5550. implemented.push(this.finishNode(node, "ClassImplements"));
  5551. } while (this.eat(12));
  5552. }
  5553. }
  5554. checkGetterSetterParams(method) {
  5555. super.checkGetterSetterParams(method);
  5556. const params = this.getObjectOrClassMethodParams(method);
  5557. if (params.length > 0) {
  5558. const param = params[0];
  5559. if (this.isThisParam(param) && method.kind === "get") {
  5560. this.raise(FlowErrors.GetterMayNotHaveThisParam, {
  5561. at: param
  5562. });
  5563. } else if (this.isThisParam(param)) {
  5564. this.raise(FlowErrors.SetterMayNotHaveThisParam, {
  5565. at: param
  5566. });
  5567. }
  5568. }
  5569. }
  5570. parsePropertyNamePrefixOperator(node) {
  5571. node.variance = this.flowParseVariance();
  5572. }
  5573. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  5574. if (prop.variance) {
  5575. this.unexpected(prop.variance.loc.start);
  5576. }
  5577. delete prop.variance;
  5578. let typeParameters;
  5579. if (this.match(47) && !isAccessor) {
  5580. typeParameters = this.flowParseTypeParameterDeclaration();
  5581. if (!this.match(10)) this.unexpected();
  5582. }
  5583. const result = super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  5584. if (typeParameters) {
  5585. (result.value || result).typeParameters = typeParameters;
  5586. }
  5587. return result;
  5588. }
  5589. parseAssignableListItemTypes(param) {
  5590. if (this.eat(17)) {
  5591. if (param.type !== "Identifier") {
  5592. this.raise(FlowErrors.PatternIsOptional, {
  5593. at: param
  5594. });
  5595. }
  5596. if (this.isThisParam(param)) {
  5597. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  5598. at: param
  5599. });
  5600. }
  5601. param.optional = true;
  5602. }
  5603. if (this.match(14)) {
  5604. param.typeAnnotation = this.flowParseTypeAnnotation();
  5605. } else if (this.isThisParam(param)) {
  5606. this.raise(FlowErrors.ThisParamAnnotationRequired, {
  5607. at: param
  5608. });
  5609. }
  5610. if (this.match(29) && this.isThisParam(param)) {
  5611. this.raise(FlowErrors.ThisParamNoDefault, {
  5612. at: param
  5613. });
  5614. }
  5615. this.resetEndLocation(param);
  5616. return param;
  5617. }
  5618. parseMaybeDefault(startLoc, left) {
  5619. const node = super.parseMaybeDefault(startLoc, left);
  5620. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5621. this.raise(FlowErrors.TypeBeforeInitializer, {
  5622. at: node.typeAnnotation
  5623. });
  5624. }
  5625. return node;
  5626. }
  5627. shouldParseDefaultImport(node) {
  5628. if (!hasTypeImportKind(node)) {
  5629. return super.shouldParseDefaultImport(node);
  5630. }
  5631. return isMaybeDefaultImport(this.state.type);
  5632. }
  5633. checkImportReflection(node) {
  5634. super.checkImportReflection(node);
  5635. if (node.module && node.importKind !== "value") {
  5636. this.raise(FlowErrors.ImportReflectionHasImportType, {
  5637. at: node.specifiers[0].loc.start
  5638. });
  5639. }
  5640. }
  5641. parseImportSpecifierLocal(node, specifier, type) {
  5642. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  5643. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  5644. }
  5645. maybeParseDefaultImportSpecifier(node) {
  5646. node.importKind = "value";
  5647. let kind = null;
  5648. if (this.match(87)) {
  5649. kind = "typeof";
  5650. } else if (this.isContextual(128)) {
  5651. kind = "type";
  5652. }
  5653. if (kind) {
  5654. const lh = this.lookahead();
  5655. const {
  5656. type
  5657. } = lh;
  5658. if (kind === "type" && type === 55) {
  5659. this.unexpected(null, lh.type);
  5660. }
  5661. if (isMaybeDefaultImport(type) || type === 5 || type === 55) {
  5662. this.next();
  5663. node.importKind = kind;
  5664. }
  5665. }
  5666. return super.maybeParseDefaultImportSpecifier(node);
  5667. }
  5668. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  5669. const firstIdent = specifier.imported;
  5670. let specifierTypeKind = null;
  5671. if (firstIdent.type === "Identifier") {
  5672. if (firstIdent.name === "type") {
  5673. specifierTypeKind = "type";
  5674. } else if (firstIdent.name === "typeof") {
  5675. specifierTypeKind = "typeof";
  5676. }
  5677. }
  5678. let isBinding = false;
  5679. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  5680. const as_ident = this.parseIdentifier(true);
  5681. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5682. specifier.imported = as_ident;
  5683. specifier.importKind = specifierTypeKind;
  5684. specifier.local = cloneIdentifier(as_ident);
  5685. } else {
  5686. specifier.imported = firstIdent;
  5687. specifier.importKind = null;
  5688. specifier.local = this.parseIdentifier();
  5689. }
  5690. } else {
  5691. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5692. specifier.imported = this.parseIdentifier(true);
  5693. specifier.importKind = specifierTypeKind;
  5694. } else {
  5695. if (importedIsString) {
  5696. throw this.raise(Errors.ImportBindingIsString, {
  5697. at: specifier,
  5698. importName: firstIdent.value
  5699. });
  5700. }
  5701. specifier.imported = firstIdent;
  5702. specifier.importKind = null;
  5703. }
  5704. if (this.eatContextual(93)) {
  5705. specifier.local = this.parseIdentifier();
  5706. } else {
  5707. isBinding = true;
  5708. specifier.local = cloneIdentifier(specifier.imported);
  5709. }
  5710. }
  5711. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5712. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5713. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, {
  5714. at: specifier
  5715. });
  5716. }
  5717. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5718. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  5719. }
  5720. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5721. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  5722. }
  5723. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  5724. }
  5725. parseBindingAtom() {
  5726. switch (this.state.type) {
  5727. case 78:
  5728. return this.parseIdentifier(true);
  5729. default:
  5730. return super.parseBindingAtom();
  5731. }
  5732. }
  5733. parseFunctionParams(node, isConstructor) {
  5734. const kind = node.kind;
  5735. if (kind !== "get" && kind !== "set" && this.match(47)) {
  5736. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5737. }
  5738. super.parseFunctionParams(node, isConstructor);
  5739. }
  5740. parseVarId(decl, kind) {
  5741. super.parseVarId(decl, kind);
  5742. if (this.match(14)) {
  5743. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5744. this.resetEndLocation(decl.id);
  5745. }
  5746. }
  5747. parseAsyncArrowFromCallExpression(node, call) {
  5748. if (this.match(14)) {
  5749. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5750. this.state.noAnonFunctionType = true;
  5751. node.returnType = this.flowParseTypeAnnotation();
  5752. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5753. }
  5754. return super.parseAsyncArrowFromCallExpression(node, call);
  5755. }
  5756. shouldParseAsyncArrow() {
  5757. return this.match(14) || super.shouldParseAsyncArrow();
  5758. }
  5759. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5760. var _jsx;
  5761. let state = null;
  5762. let jsx;
  5763. if (this.hasPlugin("jsx") && (this.match(140) || this.match(47))) {
  5764. state = this.state.clone();
  5765. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5766. if (!jsx.error) return jsx.node;
  5767. const {
  5768. context
  5769. } = this.state;
  5770. const currentContext = context[context.length - 1];
  5771. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5772. context.pop();
  5773. }
  5774. }
  5775. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  5776. var _jsx2, _jsx3;
  5777. state = state || this.state.clone();
  5778. let typeParameters;
  5779. const arrow = this.tryParse(abort => {
  5780. var _arrowExpression$extr;
  5781. typeParameters = this.flowParseTypeParameterDeclaration();
  5782. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5783. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5784. this.resetStartLocationFromNode(result, typeParameters);
  5785. return result;
  5786. });
  5787. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5788. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5789. if (expr.type !== "ArrowFunctionExpression") abort();
  5790. expr.typeParameters = typeParameters;
  5791. this.resetStartLocationFromNode(expr, typeParameters);
  5792. return arrowExpression;
  5793. }, state);
  5794. let arrowExpression = null;
  5795. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5796. if (!arrow.error && !arrow.aborted) {
  5797. if (arrow.node.async) {
  5798. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, {
  5799. at: typeParameters
  5800. });
  5801. }
  5802. return arrow.node;
  5803. }
  5804. arrowExpression = arrow.node;
  5805. }
  5806. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5807. this.state = jsx.failState;
  5808. return jsx.node;
  5809. }
  5810. if (arrowExpression) {
  5811. this.state = arrow.failState;
  5812. return arrowExpression;
  5813. }
  5814. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5815. if (arrow.thrown) throw arrow.error;
  5816. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, {
  5817. at: typeParameters
  5818. });
  5819. }
  5820. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5821. }
  5822. parseArrow(node) {
  5823. if (this.match(14)) {
  5824. const result = this.tryParse(() => {
  5825. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5826. this.state.noAnonFunctionType = true;
  5827. const typeNode = this.startNode();
  5828. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5829. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5830. if (this.canInsertSemicolon()) this.unexpected();
  5831. if (!this.match(19)) this.unexpected();
  5832. return typeNode;
  5833. });
  5834. if (result.thrown) return null;
  5835. if (result.error) this.state = result.failState;
  5836. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5837. }
  5838. return super.parseArrow(node);
  5839. }
  5840. shouldParseArrow(params) {
  5841. return this.match(14) || super.shouldParseArrow(params);
  5842. }
  5843. setArrowFunctionParameters(node, params) {
  5844. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5845. node.params = params;
  5846. } else {
  5847. super.setArrowFunctionParameters(node, params);
  5848. }
  5849. }
  5850. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  5851. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5852. return;
  5853. }
  5854. for (let i = 0; i < node.params.length; i++) {
  5855. if (this.isThisParam(node.params[i]) && i > 0) {
  5856. this.raise(FlowErrors.ThisParamMustBeFirst, {
  5857. at: node.params[i]
  5858. });
  5859. }
  5860. }
  5861. super.checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged);
  5862. }
  5863. parseParenAndDistinguishExpression(canBeArrow) {
  5864. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  5865. }
  5866. parseSubscripts(base, startLoc, noCalls) {
  5867. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startLoc.index) !== -1) {
  5868. this.next();
  5869. const node = this.startNodeAt(startLoc);
  5870. node.callee = base;
  5871. node.arguments = super.parseCallExpressionArguments(11, false);
  5872. base = this.finishNode(node, "CallExpression");
  5873. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  5874. const state = this.state.clone();
  5875. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startLoc) || abort(), state);
  5876. if (!arrow.error && !arrow.aborted) return arrow.node;
  5877. const result = this.tryParse(() => super.parseSubscripts(base, startLoc, noCalls), state);
  5878. if (result.node && !result.error) return result.node;
  5879. if (arrow.node) {
  5880. this.state = arrow.failState;
  5881. return arrow.node;
  5882. }
  5883. if (result.node) {
  5884. this.state = result.failState;
  5885. return result.node;
  5886. }
  5887. throw arrow.error || result.error;
  5888. }
  5889. return super.parseSubscripts(base, startLoc, noCalls);
  5890. }
  5891. parseSubscript(base, startLoc, noCalls, subscriptState) {
  5892. if (this.match(18) && this.isLookaheadToken_lt()) {
  5893. subscriptState.optionalChainMember = true;
  5894. if (noCalls) {
  5895. subscriptState.stop = true;
  5896. return base;
  5897. }
  5898. this.next();
  5899. const node = this.startNodeAt(startLoc);
  5900. node.callee = base;
  5901. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5902. this.expect(10);
  5903. node.arguments = this.parseCallExpressionArguments(11, false);
  5904. node.optional = true;
  5905. return this.finishCallExpression(node, true);
  5906. } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
  5907. const node = this.startNodeAt(startLoc);
  5908. node.callee = base;
  5909. const result = this.tryParse(() => {
  5910. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5911. this.expect(10);
  5912. node.arguments = super.parseCallExpressionArguments(11, false);
  5913. if (subscriptState.optionalChainMember) {
  5914. node.optional = false;
  5915. }
  5916. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5917. });
  5918. if (result.node) {
  5919. if (result.error) this.state = result.failState;
  5920. return result.node;
  5921. }
  5922. }
  5923. return super.parseSubscript(base, startLoc, noCalls, subscriptState);
  5924. }
  5925. parseNewCallee(node) {
  5926. super.parseNewCallee(node);
  5927. let targs = null;
  5928. if (this.shouldParseTypes() && this.match(47)) {
  5929. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5930. }
  5931. node.typeArguments = targs;
  5932. }
  5933. parseAsyncArrowWithTypeParameters(startLoc) {
  5934. const node = this.startNodeAt(startLoc);
  5935. this.parseFunctionParams(node, false);
  5936. if (!this.parseArrow(node)) return;
  5937. return super.parseArrowExpression(node, undefined, true);
  5938. }
  5939. readToken_mult_modulo(code) {
  5940. const next = this.input.charCodeAt(this.state.pos + 1);
  5941. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5942. this.state.hasFlowComment = false;
  5943. this.state.pos += 2;
  5944. this.nextToken();
  5945. return;
  5946. }
  5947. super.readToken_mult_modulo(code);
  5948. }
  5949. readToken_pipe_amp(code) {
  5950. const next = this.input.charCodeAt(this.state.pos + 1);
  5951. if (code === 124 && next === 125) {
  5952. this.finishOp(9, 2);
  5953. return;
  5954. }
  5955. super.readToken_pipe_amp(code);
  5956. }
  5957. parseTopLevel(file, program) {
  5958. const fileNode = super.parseTopLevel(file, program);
  5959. if (this.state.hasFlowComment) {
  5960. this.raise(FlowErrors.UnterminatedFlowComment, {
  5961. at: this.state.curPosition()
  5962. });
  5963. }
  5964. return fileNode;
  5965. }
  5966. skipBlockComment() {
  5967. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5968. if (this.state.hasFlowComment) {
  5969. throw this.raise(FlowErrors.NestedFlowComment, {
  5970. at: this.state.startLoc
  5971. });
  5972. }
  5973. this.hasFlowCommentCompletion();
  5974. const commentSkip = this.skipFlowComment();
  5975. if (commentSkip) {
  5976. this.state.pos += commentSkip;
  5977. this.state.hasFlowComment = true;
  5978. }
  5979. return;
  5980. }
  5981. return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/");
  5982. }
  5983. skipFlowComment() {
  5984. const {
  5985. pos
  5986. } = this.state;
  5987. let shiftToFirstNonWhiteSpace = 2;
  5988. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5989. shiftToFirstNonWhiteSpace++;
  5990. }
  5991. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5992. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5993. if (ch2 === 58 && ch3 === 58) {
  5994. return shiftToFirstNonWhiteSpace + 2;
  5995. }
  5996. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5997. return shiftToFirstNonWhiteSpace + 12;
  5998. }
  5999. if (ch2 === 58 && ch3 !== 58) {
  6000. return shiftToFirstNonWhiteSpace;
  6001. }
  6002. return false;
  6003. }
  6004. hasFlowCommentCompletion() {
  6005. const end = this.input.indexOf("*/", this.state.pos);
  6006. if (end === -1) {
  6007. throw this.raise(Errors.UnterminatedComment, {
  6008. at: this.state.curPosition()
  6009. });
  6010. }
  6011. }
  6012. flowEnumErrorBooleanMemberNotInitialized(loc, {
  6013. enumName,
  6014. memberName
  6015. }) {
  6016. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, {
  6017. at: loc,
  6018. memberName,
  6019. enumName
  6020. });
  6021. }
  6022. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  6023. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, Object.assign({
  6024. at: loc
  6025. }, enumContext));
  6026. }
  6027. flowEnumErrorNumberMemberNotInitialized(loc, {
  6028. enumName,
  6029. memberName
  6030. }) {
  6031. this.raise(FlowErrors.EnumNumberMemberNotInitialized, {
  6032. at: loc,
  6033. enumName,
  6034. memberName
  6035. });
  6036. }
  6037. flowEnumErrorStringMemberInconsistentlyInitailized(node, {
  6038. enumName
  6039. }) {
  6040. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitailized, {
  6041. at: node,
  6042. enumName
  6043. });
  6044. }
  6045. flowEnumMemberInit() {
  6046. const startLoc = this.state.startLoc;
  6047. const endOfInit = () => this.match(12) || this.match(8);
  6048. switch (this.state.type) {
  6049. case 132:
  6050. {
  6051. const literal = this.parseNumericLiteral(this.state.value);
  6052. if (endOfInit()) {
  6053. return {
  6054. type: "number",
  6055. loc: literal.loc.start,
  6056. value: literal
  6057. };
  6058. }
  6059. return {
  6060. type: "invalid",
  6061. loc: startLoc
  6062. };
  6063. }
  6064. case 131:
  6065. {
  6066. const literal = this.parseStringLiteral(this.state.value);
  6067. if (endOfInit()) {
  6068. return {
  6069. type: "string",
  6070. loc: literal.loc.start,
  6071. value: literal
  6072. };
  6073. }
  6074. return {
  6075. type: "invalid",
  6076. loc: startLoc
  6077. };
  6078. }
  6079. case 85:
  6080. case 86:
  6081. {
  6082. const literal = this.parseBooleanLiteral(this.match(85));
  6083. if (endOfInit()) {
  6084. return {
  6085. type: "boolean",
  6086. loc: literal.loc.start,
  6087. value: literal
  6088. };
  6089. }
  6090. return {
  6091. type: "invalid",
  6092. loc: startLoc
  6093. };
  6094. }
  6095. default:
  6096. return {
  6097. type: "invalid",
  6098. loc: startLoc
  6099. };
  6100. }
  6101. }
  6102. flowEnumMemberRaw() {
  6103. const loc = this.state.startLoc;
  6104. const id = this.parseIdentifier(true);
  6105. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  6106. type: "none",
  6107. loc
  6108. };
  6109. return {
  6110. id,
  6111. init
  6112. };
  6113. }
  6114. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  6115. const {
  6116. explicitType
  6117. } = context;
  6118. if (explicitType === null) {
  6119. return;
  6120. }
  6121. if (explicitType !== expectedType) {
  6122. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  6123. }
  6124. }
  6125. flowEnumMembers({
  6126. enumName,
  6127. explicitType
  6128. }) {
  6129. const seenNames = new Set();
  6130. const members = {
  6131. booleanMembers: [],
  6132. numberMembers: [],
  6133. stringMembers: [],
  6134. defaultedMembers: []
  6135. };
  6136. let hasUnknownMembers = false;
  6137. while (!this.match(8)) {
  6138. if (this.eat(21)) {
  6139. hasUnknownMembers = true;
  6140. break;
  6141. }
  6142. const memberNode = this.startNode();
  6143. const {
  6144. id,
  6145. init
  6146. } = this.flowEnumMemberRaw();
  6147. const memberName = id.name;
  6148. if (memberName === "") {
  6149. continue;
  6150. }
  6151. if (/^[a-z]/.test(memberName)) {
  6152. this.raise(FlowErrors.EnumInvalidMemberName, {
  6153. at: id,
  6154. memberName,
  6155. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  6156. enumName
  6157. });
  6158. }
  6159. if (seenNames.has(memberName)) {
  6160. this.raise(FlowErrors.EnumDuplicateMemberName, {
  6161. at: id,
  6162. memberName,
  6163. enumName
  6164. });
  6165. }
  6166. seenNames.add(memberName);
  6167. const context = {
  6168. enumName,
  6169. explicitType,
  6170. memberName
  6171. };
  6172. memberNode.id = id;
  6173. switch (init.type) {
  6174. case "boolean":
  6175. {
  6176. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  6177. memberNode.init = init.value;
  6178. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  6179. break;
  6180. }
  6181. case "number":
  6182. {
  6183. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  6184. memberNode.init = init.value;
  6185. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  6186. break;
  6187. }
  6188. case "string":
  6189. {
  6190. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  6191. memberNode.init = init.value;
  6192. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  6193. break;
  6194. }
  6195. case "invalid":
  6196. {
  6197. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  6198. }
  6199. case "none":
  6200. {
  6201. switch (explicitType) {
  6202. case "boolean":
  6203. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  6204. break;
  6205. case "number":
  6206. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  6207. break;
  6208. default:
  6209. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  6210. }
  6211. }
  6212. }
  6213. if (!this.match(8)) {
  6214. this.expect(12);
  6215. }
  6216. }
  6217. return {
  6218. members,
  6219. hasUnknownMembers
  6220. };
  6221. }
  6222. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  6223. enumName
  6224. }) {
  6225. if (initializedMembers.length === 0) {
  6226. return defaultedMembers;
  6227. } else if (defaultedMembers.length === 0) {
  6228. return initializedMembers;
  6229. } else if (defaultedMembers.length > initializedMembers.length) {
  6230. for (const member of initializedMembers) {
  6231. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6232. enumName
  6233. });
  6234. }
  6235. return defaultedMembers;
  6236. } else {
  6237. for (const member of defaultedMembers) {
  6238. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6239. enumName
  6240. });
  6241. }
  6242. return initializedMembers;
  6243. }
  6244. }
  6245. flowEnumParseExplicitType({
  6246. enumName
  6247. }) {
  6248. if (!this.eatContextual(101)) return null;
  6249. if (!tokenIsIdentifier(this.state.type)) {
  6250. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, {
  6251. at: this.state.startLoc,
  6252. enumName
  6253. });
  6254. }
  6255. const {
  6256. value
  6257. } = this.state;
  6258. this.next();
  6259. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  6260. this.raise(FlowErrors.EnumInvalidExplicitType, {
  6261. at: this.state.startLoc,
  6262. enumName,
  6263. invalidEnumType: value
  6264. });
  6265. }
  6266. return value;
  6267. }
  6268. flowEnumBody(node, id) {
  6269. const enumName = id.name;
  6270. const nameLoc = id.loc.start;
  6271. const explicitType = this.flowEnumParseExplicitType({
  6272. enumName
  6273. });
  6274. this.expect(5);
  6275. const {
  6276. members,
  6277. hasUnknownMembers
  6278. } = this.flowEnumMembers({
  6279. enumName,
  6280. explicitType
  6281. });
  6282. node.hasUnknownMembers = hasUnknownMembers;
  6283. switch (explicitType) {
  6284. case "boolean":
  6285. node.explicitType = true;
  6286. node.members = members.booleanMembers;
  6287. this.expect(8);
  6288. return this.finishNode(node, "EnumBooleanBody");
  6289. case "number":
  6290. node.explicitType = true;
  6291. node.members = members.numberMembers;
  6292. this.expect(8);
  6293. return this.finishNode(node, "EnumNumberBody");
  6294. case "string":
  6295. node.explicitType = true;
  6296. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6297. enumName
  6298. });
  6299. this.expect(8);
  6300. return this.finishNode(node, "EnumStringBody");
  6301. case "symbol":
  6302. node.members = members.defaultedMembers;
  6303. this.expect(8);
  6304. return this.finishNode(node, "EnumSymbolBody");
  6305. default:
  6306. {
  6307. const empty = () => {
  6308. node.members = [];
  6309. this.expect(8);
  6310. return this.finishNode(node, "EnumStringBody");
  6311. };
  6312. node.explicitType = false;
  6313. const boolsLen = members.booleanMembers.length;
  6314. const numsLen = members.numberMembers.length;
  6315. const strsLen = members.stringMembers.length;
  6316. const defaultedLen = members.defaultedMembers.length;
  6317. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  6318. return empty();
  6319. } else if (!boolsLen && !numsLen) {
  6320. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6321. enumName
  6322. });
  6323. this.expect(8);
  6324. return this.finishNode(node, "EnumStringBody");
  6325. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  6326. for (const member of members.defaultedMembers) {
  6327. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  6328. enumName,
  6329. memberName: member.id.name
  6330. });
  6331. }
  6332. node.members = members.booleanMembers;
  6333. this.expect(8);
  6334. return this.finishNode(node, "EnumBooleanBody");
  6335. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  6336. for (const member of members.defaultedMembers) {
  6337. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  6338. enumName,
  6339. memberName: member.id.name
  6340. });
  6341. }
  6342. node.members = members.numberMembers;
  6343. this.expect(8);
  6344. return this.finishNode(node, "EnumNumberBody");
  6345. } else {
  6346. this.raise(FlowErrors.EnumInconsistentMemberValues, {
  6347. at: nameLoc,
  6348. enumName
  6349. });
  6350. return empty();
  6351. }
  6352. }
  6353. }
  6354. }
  6355. flowParseEnumDeclaration(node) {
  6356. const id = this.parseIdentifier();
  6357. node.id = id;
  6358. node.body = this.flowEnumBody(this.startNode(), id);
  6359. return this.finishNode(node, "EnumDeclaration");
  6360. }
  6361. isLookaheadToken_lt() {
  6362. const next = this.nextTokenStart();
  6363. if (this.input.charCodeAt(next) === 60) {
  6364. const afterNext = this.input.charCodeAt(next + 1);
  6365. return afterNext !== 60 && afterNext !== 61;
  6366. }
  6367. return false;
  6368. }
  6369. maybeUnwrapTypeCastExpression(node) {
  6370. return node.type === "TypeCastExpression" ? node.expression : node;
  6371. }
  6372. };
  6373. const entities = {
  6374. __proto__: null,
  6375. quot: "\u0022",
  6376. amp: "&",
  6377. apos: "\u0027",
  6378. lt: "<",
  6379. gt: ">",
  6380. nbsp: "\u00A0",
  6381. iexcl: "\u00A1",
  6382. cent: "\u00A2",
  6383. pound: "\u00A3",
  6384. curren: "\u00A4",
  6385. yen: "\u00A5",
  6386. brvbar: "\u00A6",
  6387. sect: "\u00A7",
  6388. uml: "\u00A8",
  6389. copy: "\u00A9",
  6390. ordf: "\u00AA",
  6391. laquo: "\u00AB",
  6392. not: "\u00AC",
  6393. shy: "\u00AD",
  6394. reg: "\u00AE",
  6395. macr: "\u00AF",
  6396. deg: "\u00B0",
  6397. plusmn: "\u00B1",
  6398. sup2: "\u00B2",
  6399. sup3: "\u00B3",
  6400. acute: "\u00B4",
  6401. micro: "\u00B5",
  6402. para: "\u00B6",
  6403. middot: "\u00B7",
  6404. cedil: "\u00B8",
  6405. sup1: "\u00B9",
  6406. ordm: "\u00BA",
  6407. raquo: "\u00BB",
  6408. frac14: "\u00BC",
  6409. frac12: "\u00BD",
  6410. frac34: "\u00BE",
  6411. iquest: "\u00BF",
  6412. Agrave: "\u00C0",
  6413. Aacute: "\u00C1",
  6414. Acirc: "\u00C2",
  6415. Atilde: "\u00C3",
  6416. Auml: "\u00C4",
  6417. Aring: "\u00C5",
  6418. AElig: "\u00C6",
  6419. Ccedil: "\u00C7",
  6420. Egrave: "\u00C8",
  6421. Eacute: "\u00C9",
  6422. Ecirc: "\u00CA",
  6423. Euml: "\u00CB",
  6424. Igrave: "\u00CC",
  6425. Iacute: "\u00CD",
  6426. Icirc: "\u00CE",
  6427. Iuml: "\u00CF",
  6428. ETH: "\u00D0",
  6429. Ntilde: "\u00D1",
  6430. Ograve: "\u00D2",
  6431. Oacute: "\u00D3",
  6432. Ocirc: "\u00D4",
  6433. Otilde: "\u00D5",
  6434. Ouml: "\u00D6",
  6435. times: "\u00D7",
  6436. Oslash: "\u00D8",
  6437. Ugrave: "\u00D9",
  6438. Uacute: "\u00DA",
  6439. Ucirc: "\u00DB",
  6440. Uuml: "\u00DC",
  6441. Yacute: "\u00DD",
  6442. THORN: "\u00DE",
  6443. szlig: "\u00DF",
  6444. agrave: "\u00E0",
  6445. aacute: "\u00E1",
  6446. acirc: "\u00E2",
  6447. atilde: "\u00E3",
  6448. auml: "\u00E4",
  6449. aring: "\u00E5",
  6450. aelig: "\u00E6",
  6451. ccedil: "\u00E7",
  6452. egrave: "\u00E8",
  6453. eacute: "\u00E9",
  6454. ecirc: "\u00EA",
  6455. euml: "\u00EB",
  6456. igrave: "\u00EC",
  6457. iacute: "\u00ED",
  6458. icirc: "\u00EE",
  6459. iuml: "\u00EF",
  6460. eth: "\u00F0",
  6461. ntilde: "\u00F1",
  6462. ograve: "\u00F2",
  6463. oacute: "\u00F3",
  6464. ocirc: "\u00F4",
  6465. otilde: "\u00F5",
  6466. ouml: "\u00F6",
  6467. divide: "\u00F7",
  6468. oslash: "\u00F8",
  6469. ugrave: "\u00F9",
  6470. uacute: "\u00FA",
  6471. ucirc: "\u00FB",
  6472. uuml: "\u00FC",
  6473. yacute: "\u00FD",
  6474. thorn: "\u00FE",
  6475. yuml: "\u00FF",
  6476. OElig: "\u0152",
  6477. oelig: "\u0153",
  6478. Scaron: "\u0160",
  6479. scaron: "\u0161",
  6480. Yuml: "\u0178",
  6481. fnof: "\u0192",
  6482. circ: "\u02C6",
  6483. tilde: "\u02DC",
  6484. Alpha: "\u0391",
  6485. Beta: "\u0392",
  6486. Gamma: "\u0393",
  6487. Delta: "\u0394",
  6488. Epsilon: "\u0395",
  6489. Zeta: "\u0396",
  6490. Eta: "\u0397",
  6491. Theta: "\u0398",
  6492. Iota: "\u0399",
  6493. Kappa: "\u039A",
  6494. Lambda: "\u039B",
  6495. Mu: "\u039C",
  6496. Nu: "\u039D",
  6497. Xi: "\u039E",
  6498. Omicron: "\u039F",
  6499. Pi: "\u03A0",
  6500. Rho: "\u03A1",
  6501. Sigma: "\u03A3",
  6502. Tau: "\u03A4",
  6503. Upsilon: "\u03A5",
  6504. Phi: "\u03A6",
  6505. Chi: "\u03A7",
  6506. Psi: "\u03A8",
  6507. Omega: "\u03A9",
  6508. alpha: "\u03B1",
  6509. beta: "\u03B2",
  6510. gamma: "\u03B3",
  6511. delta: "\u03B4",
  6512. epsilon: "\u03B5",
  6513. zeta: "\u03B6",
  6514. eta: "\u03B7",
  6515. theta: "\u03B8",
  6516. iota: "\u03B9",
  6517. kappa: "\u03BA",
  6518. lambda: "\u03BB",
  6519. mu: "\u03BC",
  6520. nu: "\u03BD",
  6521. xi: "\u03BE",
  6522. omicron: "\u03BF",
  6523. pi: "\u03C0",
  6524. rho: "\u03C1",
  6525. sigmaf: "\u03C2",
  6526. sigma: "\u03C3",
  6527. tau: "\u03C4",
  6528. upsilon: "\u03C5",
  6529. phi: "\u03C6",
  6530. chi: "\u03C7",
  6531. psi: "\u03C8",
  6532. omega: "\u03C9",
  6533. thetasym: "\u03D1",
  6534. upsih: "\u03D2",
  6535. piv: "\u03D6",
  6536. ensp: "\u2002",
  6537. emsp: "\u2003",
  6538. thinsp: "\u2009",
  6539. zwnj: "\u200C",
  6540. zwj: "\u200D",
  6541. lrm: "\u200E",
  6542. rlm: "\u200F",
  6543. ndash: "\u2013",
  6544. mdash: "\u2014",
  6545. lsquo: "\u2018",
  6546. rsquo: "\u2019",
  6547. sbquo: "\u201A",
  6548. ldquo: "\u201C",
  6549. rdquo: "\u201D",
  6550. bdquo: "\u201E",
  6551. dagger: "\u2020",
  6552. Dagger: "\u2021",
  6553. bull: "\u2022",
  6554. hellip: "\u2026",
  6555. permil: "\u2030",
  6556. prime: "\u2032",
  6557. Prime: "\u2033",
  6558. lsaquo: "\u2039",
  6559. rsaquo: "\u203A",
  6560. oline: "\u203E",
  6561. frasl: "\u2044",
  6562. euro: "\u20AC",
  6563. image: "\u2111",
  6564. weierp: "\u2118",
  6565. real: "\u211C",
  6566. trade: "\u2122",
  6567. alefsym: "\u2135",
  6568. larr: "\u2190",
  6569. uarr: "\u2191",
  6570. rarr: "\u2192",
  6571. darr: "\u2193",
  6572. harr: "\u2194",
  6573. crarr: "\u21B5",
  6574. lArr: "\u21D0",
  6575. uArr: "\u21D1",
  6576. rArr: "\u21D2",
  6577. dArr: "\u21D3",
  6578. hArr: "\u21D4",
  6579. forall: "\u2200",
  6580. part: "\u2202",
  6581. exist: "\u2203",
  6582. empty: "\u2205",
  6583. nabla: "\u2207",
  6584. isin: "\u2208",
  6585. notin: "\u2209",
  6586. ni: "\u220B",
  6587. prod: "\u220F",
  6588. sum: "\u2211",
  6589. minus: "\u2212",
  6590. lowast: "\u2217",
  6591. radic: "\u221A",
  6592. prop: "\u221D",
  6593. infin: "\u221E",
  6594. ang: "\u2220",
  6595. and: "\u2227",
  6596. or: "\u2228",
  6597. cap: "\u2229",
  6598. cup: "\u222A",
  6599. int: "\u222B",
  6600. there4: "\u2234",
  6601. sim: "\u223C",
  6602. cong: "\u2245",
  6603. asymp: "\u2248",
  6604. ne: "\u2260",
  6605. equiv: "\u2261",
  6606. le: "\u2264",
  6607. ge: "\u2265",
  6608. sub: "\u2282",
  6609. sup: "\u2283",
  6610. nsub: "\u2284",
  6611. sube: "\u2286",
  6612. supe: "\u2287",
  6613. oplus: "\u2295",
  6614. otimes: "\u2297",
  6615. perp: "\u22A5",
  6616. sdot: "\u22C5",
  6617. lceil: "\u2308",
  6618. rceil: "\u2309",
  6619. lfloor: "\u230A",
  6620. rfloor: "\u230B",
  6621. lang: "\u2329",
  6622. rang: "\u232A",
  6623. loz: "\u25CA",
  6624. spades: "\u2660",
  6625. clubs: "\u2663",
  6626. hearts: "\u2665",
  6627. diams: "\u2666"
  6628. };
  6629. const JsxErrors = ParseErrorEnum`jsx`({
  6630. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  6631. MissingClosingTagElement: ({
  6632. openingTagName
  6633. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`,
  6634. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  6635. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  6636. UnexpectedToken: ({
  6637. unexpected,
  6638. HTMLEntity
  6639. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`,
  6640. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  6641. UnterminatedJsxContent: "Unterminated JSX contents.",
  6642. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  6643. });
  6644. function isFragment(object) {
  6645. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  6646. }
  6647. function getQualifiedJSXName(object) {
  6648. if (object.type === "JSXIdentifier") {
  6649. return object.name;
  6650. }
  6651. if (object.type === "JSXNamespacedName") {
  6652. return object.namespace.name + ":" + object.name.name;
  6653. }
  6654. if (object.type === "JSXMemberExpression") {
  6655. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  6656. }
  6657. throw new Error("Node had unexpected type: " + object.type);
  6658. }
  6659. var jsx = superClass => class JSXParserMixin extends superClass {
  6660. jsxReadToken() {
  6661. let out = "";
  6662. let chunkStart = this.state.pos;
  6663. for (;;) {
  6664. if (this.state.pos >= this.length) {
  6665. throw this.raise(JsxErrors.UnterminatedJsxContent, {
  6666. at: this.state.startLoc
  6667. });
  6668. }
  6669. const ch = this.input.charCodeAt(this.state.pos);
  6670. switch (ch) {
  6671. case 60:
  6672. case 123:
  6673. if (this.state.pos === this.state.start) {
  6674. if (ch === 60 && this.state.canStartJSXElement) {
  6675. ++this.state.pos;
  6676. this.finishToken(140);
  6677. } else {
  6678. super.getTokenFromCode(ch);
  6679. }
  6680. return;
  6681. }
  6682. out += this.input.slice(chunkStart, this.state.pos);
  6683. this.finishToken(139, out);
  6684. return;
  6685. case 38:
  6686. out += this.input.slice(chunkStart, this.state.pos);
  6687. out += this.jsxReadEntity();
  6688. chunkStart = this.state.pos;
  6689. break;
  6690. case 62:
  6691. case 125:
  6692. default:
  6693. if (isNewLine(ch)) {
  6694. out += this.input.slice(chunkStart, this.state.pos);
  6695. out += this.jsxReadNewLine(true);
  6696. chunkStart = this.state.pos;
  6697. } else {
  6698. ++this.state.pos;
  6699. }
  6700. }
  6701. }
  6702. }
  6703. jsxReadNewLine(normalizeCRLF) {
  6704. const ch = this.input.charCodeAt(this.state.pos);
  6705. let out;
  6706. ++this.state.pos;
  6707. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6708. ++this.state.pos;
  6709. out = normalizeCRLF ? "\n" : "\r\n";
  6710. } else {
  6711. out = String.fromCharCode(ch);
  6712. }
  6713. ++this.state.curLine;
  6714. this.state.lineStart = this.state.pos;
  6715. return out;
  6716. }
  6717. jsxReadString(quote) {
  6718. let out = "";
  6719. let chunkStart = ++this.state.pos;
  6720. for (;;) {
  6721. if (this.state.pos >= this.length) {
  6722. throw this.raise(Errors.UnterminatedString, {
  6723. at: this.state.startLoc
  6724. });
  6725. }
  6726. const ch = this.input.charCodeAt(this.state.pos);
  6727. if (ch === quote) break;
  6728. if (ch === 38) {
  6729. out += this.input.slice(chunkStart, this.state.pos);
  6730. out += this.jsxReadEntity();
  6731. chunkStart = this.state.pos;
  6732. } else if (isNewLine(ch)) {
  6733. out += this.input.slice(chunkStart, this.state.pos);
  6734. out += this.jsxReadNewLine(false);
  6735. chunkStart = this.state.pos;
  6736. } else {
  6737. ++this.state.pos;
  6738. }
  6739. }
  6740. out += this.input.slice(chunkStart, this.state.pos++);
  6741. this.finishToken(131, out);
  6742. }
  6743. jsxReadEntity() {
  6744. const startPos = ++this.state.pos;
  6745. if (this.codePointAtPos(this.state.pos) === 35) {
  6746. ++this.state.pos;
  6747. let radix = 10;
  6748. if (this.codePointAtPos(this.state.pos) === 120) {
  6749. radix = 16;
  6750. ++this.state.pos;
  6751. }
  6752. const codePoint = this.readInt(radix, undefined, false, "bail");
  6753. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  6754. ++this.state.pos;
  6755. return String.fromCodePoint(codePoint);
  6756. }
  6757. } else {
  6758. let count = 0;
  6759. let semi = false;
  6760. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) == 59)) {
  6761. ++this.state.pos;
  6762. }
  6763. if (semi) {
  6764. const desc = this.input.slice(startPos, this.state.pos);
  6765. const entity = entities[desc];
  6766. ++this.state.pos;
  6767. if (entity) {
  6768. return entity;
  6769. }
  6770. }
  6771. }
  6772. this.state.pos = startPos;
  6773. return "&";
  6774. }
  6775. jsxReadWord() {
  6776. let ch;
  6777. const start = this.state.pos;
  6778. do {
  6779. ch = this.input.charCodeAt(++this.state.pos);
  6780. } while (isIdentifierChar(ch) || ch === 45);
  6781. this.finishToken(138, this.input.slice(start, this.state.pos));
  6782. }
  6783. jsxParseIdentifier() {
  6784. const node = this.startNode();
  6785. if (this.match(138)) {
  6786. node.name = this.state.value;
  6787. } else if (tokenIsKeyword(this.state.type)) {
  6788. node.name = tokenLabelName(this.state.type);
  6789. } else {
  6790. this.unexpected();
  6791. }
  6792. this.next();
  6793. return this.finishNode(node, "JSXIdentifier");
  6794. }
  6795. jsxParseNamespacedName() {
  6796. const startLoc = this.state.startLoc;
  6797. const name = this.jsxParseIdentifier();
  6798. if (!this.eat(14)) return name;
  6799. const node = this.startNodeAt(startLoc);
  6800. node.namespace = name;
  6801. node.name = this.jsxParseIdentifier();
  6802. return this.finishNode(node, "JSXNamespacedName");
  6803. }
  6804. jsxParseElementName() {
  6805. const startLoc = this.state.startLoc;
  6806. let node = this.jsxParseNamespacedName();
  6807. if (node.type === "JSXNamespacedName") {
  6808. return node;
  6809. }
  6810. while (this.eat(16)) {
  6811. const newNode = this.startNodeAt(startLoc);
  6812. newNode.object = node;
  6813. newNode.property = this.jsxParseIdentifier();
  6814. node = this.finishNode(newNode, "JSXMemberExpression");
  6815. }
  6816. return node;
  6817. }
  6818. jsxParseAttributeValue() {
  6819. let node;
  6820. switch (this.state.type) {
  6821. case 5:
  6822. node = this.startNode();
  6823. this.setContext(types.brace);
  6824. this.next();
  6825. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6826. if (node.expression.type === "JSXEmptyExpression") {
  6827. this.raise(JsxErrors.AttributeIsEmpty, {
  6828. at: node
  6829. });
  6830. }
  6831. return node;
  6832. case 140:
  6833. case 131:
  6834. return this.parseExprAtom();
  6835. default:
  6836. throw this.raise(JsxErrors.UnsupportedJsxValue, {
  6837. at: this.state.startLoc
  6838. });
  6839. }
  6840. }
  6841. jsxParseEmptyExpression() {
  6842. const node = this.startNodeAt(this.state.lastTokEndLoc);
  6843. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  6844. }
  6845. jsxParseSpreadChild(node) {
  6846. this.next();
  6847. node.expression = this.parseExpression();
  6848. this.setContext(types.j_expr);
  6849. this.state.canStartJSXElement = true;
  6850. this.expect(8);
  6851. return this.finishNode(node, "JSXSpreadChild");
  6852. }
  6853. jsxParseExpressionContainer(node, previousContext) {
  6854. if (this.match(8)) {
  6855. node.expression = this.jsxParseEmptyExpression();
  6856. } else {
  6857. const expression = this.parseExpression();
  6858. node.expression = expression;
  6859. }
  6860. this.setContext(previousContext);
  6861. this.state.canStartJSXElement = true;
  6862. this.expect(8);
  6863. return this.finishNode(node, "JSXExpressionContainer");
  6864. }
  6865. jsxParseAttribute() {
  6866. const node = this.startNode();
  6867. if (this.match(5)) {
  6868. this.setContext(types.brace);
  6869. this.next();
  6870. this.expect(21);
  6871. node.argument = this.parseMaybeAssignAllowIn();
  6872. this.setContext(types.j_oTag);
  6873. this.state.canStartJSXElement = true;
  6874. this.expect(8);
  6875. return this.finishNode(node, "JSXSpreadAttribute");
  6876. }
  6877. node.name = this.jsxParseNamespacedName();
  6878. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6879. return this.finishNode(node, "JSXAttribute");
  6880. }
  6881. jsxParseOpeningElementAt(startLoc) {
  6882. const node = this.startNodeAt(startLoc);
  6883. if (this.eat(141)) {
  6884. return this.finishNode(node, "JSXOpeningFragment");
  6885. }
  6886. node.name = this.jsxParseElementName();
  6887. return this.jsxParseOpeningElementAfterName(node);
  6888. }
  6889. jsxParseOpeningElementAfterName(node) {
  6890. const attributes = [];
  6891. while (!this.match(56) && !this.match(141)) {
  6892. attributes.push(this.jsxParseAttribute());
  6893. }
  6894. node.attributes = attributes;
  6895. node.selfClosing = this.eat(56);
  6896. this.expect(141);
  6897. return this.finishNode(node, "JSXOpeningElement");
  6898. }
  6899. jsxParseClosingElementAt(startLoc) {
  6900. const node = this.startNodeAt(startLoc);
  6901. if (this.eat(141)) {
  6902. return this.finishNode(node, "JSXClosingFragment");
  6903. }
  6904. node.name = this.jsxParseElementName();
  6905. this.expect(141);
  6906. return this.finishNode(node, "JSXClosingElement");
  6907. }
  6908. jsxParseElementAt(startLoc) {
  6909. const node = this.startNodeAt(startLoc);
  6910. const children = [];
  6911. const openingElement = this.jsxParseOpeningElementAt(startLoc);
  6912. let closingElement = null;
  6913. if (!openingElement.selfClosing) {
  6914. contents: for (;;) {
  6915. switch (this.state.type) {
  6916. case 140:
  6917. startLoc = this.state.startLoc;
  6918. this.next();
  6919. if (this.eat(56)) {
  6920. closingElement = this.jsxParseClosingElementAt(startLoc);
  6921. break contents;
  6922. }
  6923. children.push(this.jsxParseElementAt(startLoc));
  6924. break;
  6925. case 139:
  6926. children.push(this.parseExprAtom());
  6927. break;
  6928. case 5:
  6929. {
  6930. const node = this.startNode();
  6931. this.setContext(types.brace);
  6932. this.next();
  6933. if (this.match(21)) {
  6934. children.push(this.jsxParseSpreadChild(node));
  6935. } else {
  6936. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6937. }
  6938. break;
  6939. }
  6940. default:
  6941. this.unexpected();
  6942. }
  6943. }
  6944. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  6945. this.raise(JsxErrors.MissingClosingTagFragment, {
  6946. at: closingElement
  6947. });
  6948. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6949. this.raise(JsxErrors.MissingClosingTagElement, {
  6950. at: closingElement,
  6951. openingTagName: getQualifiedJSXName(openingElement.name)
  6952. });
  6953. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6954. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6955. this.raise(JsxErrors.MissingClosingTagElement, {
  6956. at: closingElement,
  6957. openingTagName: getQualifiedJSXName(openingElement.name)
  6958. });
  6959. }
  6960. }
  6961. }
  6962. if (isFragment(openingElement)) {
  6963. node.openingFragment = openingElement;
  6964. node.closingFragment = closingElement;
  6965. } else {
  6966. node.openingElement = openingElement;
  6967. node.closingElement = closingElement;
  6968. }
  6969. node.children = children;
  6970. if (this.match(47)) {
  6971. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, {
  6972. at: this.state.startLoc
  6973. });
  6974. }
  6975. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6976. }
  6977. jsxParseElement() {
  6978. const startLoc = this.state.startLoc;
  6979. this.next();
  6980. return this.jsxParseElementAt(startLoc);
  6981. }
  6982. setContext(newContext) {
  6983. const {
  6984. context
  6985. } = this.state;
  6986. context[context.length - 1] = newContext;
  6987. }
  6988. parseExprAtom(refExpressionErrors) {
  6989. if (this.match(139)) {
  6990. return this.parseLiteral(this.state.value, "JSXText");
  6991. } else if (this.match(140)) {
  6992. return this.jsxParseElement();
  6993. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  6994. this.replaceToken(140);
  6995. return this.jsxParseElement();
  6996. } else {
  6997. return super.parseExprAtom(refExpressionErrors);
  6998. }
  6999. }
  7000. skipSpace() {
  7001. const curContext = this.curContext();
  7002. if (!curContext.preserveSpace) super.skipSpace();
  7003. }
  7004. getTokenFromCode(code) {
  7005. const context = this.curContext();
  7006. if (context === types.j_expr) {
  7007. this.jsxReadToken();
  7008. return;
  7009. }
  7010. if (context === types.j_oTag || context === types.j_cTag) {
  7011. if (isIdentifierStart(code)) {
  7012. this.jsxReadWord();
  7013. return;
  7014. }
  7015. if (code === 62) {
  7016. ++this.state.pos;
  7017. this.finishToken(141);
  7018. return;
  7019. }
  7020. if ((code === 34 || code === 39) && context === types.j_oTag) {
  7021. this.jsxReadString(code);
  7022. return;
  7023. }
  7024. }
  7025. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  7026. ++this.state.pos;
  7027. this.finishToken(140);
  7028. return;
  7029. }
  7030. super.getTokenFromCode(code);
  7031. }
  7032. updateContext(prevType) {
  7033. const {
  7034. context,
  7035. type
  7036. } = this.state;
  7037. if (type === 56 && prevType === 140) {
  7038. context.splice(-2, 2, types.j_cTag);
  7039. this.state.canStartJSXElement = false;
  7040. } else if (type === 140) {
  7041. context.push(types.j_oTag);
  7042. } else if (type === 141) {
  7043. const out = context[context.length - 1];
  7044. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  7045. context.pop();
  7046. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  7047. } else {
  7048. this.setContext(types.j_expr);
  7049. this.state.canStartJSXElement = true;
  7050. }
  7051. } else {
  7052. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  7053. }
  7054. }
  7055. };
  7056. class TypeScriptScope extends Scope {
  7057. constructor(...args) {
  7058. super(...args);
  7059. this.types = new Set();
  7060. this.enums = new Set();
  7061. this.constEnums = new Set();
  7062. this.classes = new Set();
  7063. this.exportOnlyBindings = new Set();
  7064. }
  7065. }
  7066. class TypeScriptScopeHandler extends ScopeHandler {
  7067. constructor(...args) {
  7068. super(...args);
  7069. this.importsStack = [];
  7070. }
  7071. createScope(flags) {
  7072. this.importsStack.push(new Set());
  7073. return new TypeScriptScope(flags);
  7074. }
  7075. enter(flags) {
  7076. if (flags == SCOPE_TS_MODULE) {
  7077. this.importsStack.push(new Set());
  7078. }
  7079. super.enter(flags);
  7080. }
  7081. exit() {
  7082. const flags = super.exit();
  7083. if (flags == SCOPE_TS_MODULE) {
  7084. this.importsStack.pop();
  7085. }
  7086. return flags;
  7087. }
  7088. hasImport(name, allowShadow) {
  7089. const len = this.importsStack.length;
  7090. if (this.importsStack[len - 1].has(name)) {
  7091. return true;
  7092. }
  7093. if (!allowShadow && len > 1) {
  7094. for (let i = 0; i < len - 1; i++) {
  7095. if (this.importsStack[i].has(name)) return true;
  7096. }
  7097. }
  7098. return false;
  7099. }
  7100. declareName(name, bindingType, loc) {
  7101. if (bindingType & BIND_FLAGS_TS_IMPORT) {
  7102. if (this.hasImport(name, true)) {
  7103. this.parser.raise(Errors.VarRedeclaration, {
  7104. at: loc,
  7105. identifierName: name
  7106. });
  7107. }
  7108. this.importsStack[this.importsStack.length - 1].add(name);
  7109. return;
  7110. }
  7111. const scope = this.currentScope();
  7112. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  7113. this.maybeExportDefined(scope, name);
  7114. scope.exportOnlyBindings.add(name);
  7115. return;
  7116. }
  7117. super.declareName(name, bindingType, loc);
  7118. if (bindingType & BIND_KIND_TYPE) {
  7119. if (!(bindingType & BIND_KIND_VALUE)) {
  7120. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  7121. this.maybeExportDefined(scope, name);
  7122. }
  7123. scope.types.add(name);
  7124. }
  7125. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  7126. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  7127. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  7128. }
  7129. isRedeclaredInScope(scope, name, bindingType) {
  7130. if (scope.enums.has(name)) {
  7131. if (bindingType & BIND_FLAGS_TS_ENUM) {
  7132. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  7133. const wasConst = scope.constEnums.has(name);
  7134. return isConst !== wasConst;
  7135. }
  7136. return true;
  7137. }
  7138. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  7139. if (scope.lexical.has(name)) {
  7140. return !!(bindingType & BIND_KIND_VALUE);
  7141. } else {
  7142. return false;
  7143. }
  7144. }
  7145. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  7146. return true;
  7147. }
  7148. return super.isRedeclaredInScope(scope, name, bindingType);
  7149. }
  7150. checkLocalExport(id) {
  7151. const {
  7152. name
  7153. } = id;
  7154. if (this.hasImport(name)) return;
  7155. const len = this.scopeStack.length;
  7156. for (let i = len - 1; i >= 0; i--) {
  7157. const scope = this.scopeStack[i];
  7158. if (scope.types.has(name) || scope.exportOnlyBindings.has(name)) return;
  7159. }
  7160. super.checkLocalExport(id);
  7161. }
  7162. }
  7163. const getOwn$1 = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  7164. const unwrapParenthesizedExpression = node => {
  7165. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  7166. };
  7167. class LValParser extends NodeUtils {
  7168. toAssignable(node, isLHS = false) {
  7169. var _node$extra, _node$extra3;
  7170. let parenthesized = undefined;
  7171. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  7172. parenthesized = unwrapParenthesizedExpression(node);
  7173. if (isLHS) {
  7174. if (parenthesized.type === "Identifier") {
  7175. this.expressionScope.recordArrowParameterBindingError(Errors.InvalidParenthesizedAssignment, {
  7176. at: node
  7177. });
  7178. } else if (parenthesized.type !== "MemberExpression") {
  7179. this.raise(Errors.InvalidParenthesizedAssignment, {
  7180. at: node
  7181. });
  7182. }
  7183. } else {
  7184. this.raise(Errors.InvalidParenthesizedAssignment, {
  7185. at: node
  7186. });
  7187. }
  7188. }
  7189. switch (node.type) {
  7190. case "Identifier":
  7191. case "ObjectPattern":
  7192. case "ArrayPattern":
  7193. case "AssignmentPattern":
  7194. case "RestElement":
  7195. break;
  7196. case "ObjectExpression":
  7197. node.type = "ObjectPattern";
  7198. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  7199. var _node$extra2;
  7200. const prop = node.properties[i];
  7201. const isLast = i === last;
  7202. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  7203. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  7204. this.raise(Errors.RestTrailingComma, {
  7205. at: node.extra.trailingCommaLoc
  7206. });
  7207. }
  7208. }
  7209. break;
  7210. case "ObjectProperty":
  7211. {
  7212. const {
  7213. key,
  7214. value
  7215. } = node;
  7216. if (this.isPrivateName(key)) {
  7217. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  7218. }
  7219. this.toAssignable(value, isLHS);
  7220. break;
  7221. }
  7222. case "SpreadElement":
  7223. {
  7224. throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
  7225. }
  7226. case "ArrayExpression":
  7227. node.type = "ArrayPattern";
  7228. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  7229. break;
  7230. case "AssignmentExpression":
  7231. if (node.operator !== "=") {
  7232. this.raise(Errors.MissingEqInAssignment, {
  7233. at: node.left.loc.end
  7234. });
  7235. }
  7236. node.type = "AssignmentPattern";
  7237. delete node.operator;
  7238. this.toAssignable(node.left, isLHS);
  7239. break;
  7240. case "ParenthesizedExpression":
  7241. this.toAssignable(parenthesized, isLHS);
  7242. break;
  7243. }
  7244. }
  7245. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  7246. if (prop.type === "ObjectMethod") {
  7247. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, {
  7248. at: prop.key
  7249. });
  7250. } else if (prop.type === "SpreadElement") {
  7251. prop.type = "RestElement";
  7252. const arg = prop.argument;
  7253. this.checkToRestConversion(arg, false);
  7254. this.toAssignable(arg, isLHS);
  7255. if (!isLast) {
  7256. this.raise(Errors.RestTrailingComma, {
  7257. at: prop
  7258. });
  7259. }
  7260. } else {
  7261. this.toAssignable(prop, isLHS);
  7262. }
  7263. }
  7264. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  7265. const end = exprList.length - 1;
  7266. for (let i = 0; i <= end; i++) {
  7267. const elt = exprList[i];
  7268. if (!elt) continue;
  7269. if (elt.type === "SpreadElement") {
  7270. elt.type = "RestElement";
  7271. const arg = elt.argument;
  7272. this.checkToRestConversion(arg, true);
  7273. this.toAssignable(arg, isLHS);
  7274. } else {
  7275. this.toAssignable(elt, isLHS);
  7276. }
  7277. if (elt.type === "RestElement") {
  7278. if (i < end) {
  7279. this.raise(Errors.RestTrailingComma, {
  7280. at: elt
  7281. });
  7282. } else if (trailingCommaLoc) {
  7283. this.raise(Errors.RestTrailingComma, {
  7284. at: trailingCommaLoc
  7285. });
  7286. }
  7287. }
  7288. }
  7289. }
  7290. isAssignable(node, isBinding) {
  7291. switch (node.type) {
  7292. case "Identifier":
  7293. case "ObjectPattern":
  7294. case "ArrayPattern":
  7295. case "AssignmentPattern":
  7296. case "RestElement":
  7297. return true;
  7298. case "ObjectExpression":
  7299. {
  7300. const last = node.properties.length - 1;
  7301. return node.properties.every((prop, i) => {
  7302. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  7303. });
  7304. }
  7305. case "ObjectProperty":
  7306. return this.isAssignable(node.value);
  7307. case "SpreadElement":
  7308. return this.isAssignable(node.argument);
  7309. case "ArrayExpression":
  7310. return node.elements.every(element => element === null || this.isAssignable(element));
  7311. case "AssignmentExpression":
  7312. return node.operator === "=";
  7313. case "ParenthesizedExpression":
  7314. return this.isAssignable(node.expression);
  7315. case "MemberExpression":
  7316. case "OptionalMemberExpression":
  7317. return !isBinding;
  7318. default:
  7319. return false;
  7320. }
  7321. }
  7322. toReferencedList(exprList, isParenthesizedExpr) {
  7323. return exprList;
  7324. }
  7325. toReferencedListDeep(exprList, isParenthesizedExpr) {
  7326. this.toReferencedList(exprList, isParenthesizedExpr);
  7327. for (const expr of exprList) {
  7328. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  7329. this.toReferencedListDeep(expr.elements);
  7330. }
  7331. }
  7332. }
  7333. parseSpread(refExpressionErrors) {
  7334. const node = this.startNode();
  7335. this.next();
  7336. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined);
  7337. return this.finishNode(node, "SpreadElement");
  7338. }
  7339. parseRestBinding() {
  7340. const node = this.startNode();
  7341. this.next();
  7342. node.argument = this.parseBindingAtom();
  7343. return this.finishNode(node, "RestElement");
  7344. }
  7345. parseBindingAtom() {
  7346. switch (this.state.type) {
  7347. case 0:
  7348. {
  7349. const node = this.startNode();
  7350. this.next();
  7351. node.elements = this.parseBindingList(3, 93, 1);
  7352. return this.finishNode(node, "ArrayPattern");
  7353. }
  7354. case 5:
  7355. return this.parseObjectLike(8, true);
  7356. }
  7357. return this.parseIdentifier();
  7358. }
  7359. parseBindingList(close, closeCharCode, flags) {
  7360. const allowEmpty = flags & 1;
  7361. const elts = [];
  7362. let first = true;
  7363. while (!this.eat(close)) {
  7364. if (first) {
  7365. first = false;
  7366. } else {
  7367. this.expect(12);
  7368. }
  7369. if (allowEmpty && this.match(12)) {
  7370. elts.push(null);
  7371. } else if (this.eat(close)) {
  7372. break;
  7373. } else if (this.match(21)) {
  7374. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding(), flags));
  7375. if (!this.checkCommaAfterRest(closeCharCode)) {
  7376. this.expect(close);
  7377. break;
  7378. }
  7379. } else {
  7380. const decorators = [];
  7381. if (this.match(26) && this.hasPlugin("decorators")) {
  7382. this.raise(Errors.UnsupportedParameterDecorator, {
  7383. at: this.state.startLoc
  7384. });
  7385. }
  7386. while (this.match(26)) {
  7387. decorators.push(this.parseDecorator());
  7388. }
  7389. elts.push(this.parseAssignableListItem(flags, decorators));
  7390. }
  7391. }
  7392. return elts;
  7393. }
  7394. parseBindingRestProperty(prop) {
  7395. this.next();
  7396. prop.argument = this.parseIdentifier();
  7397. this.checkCommaAfterRest(125);
  7398. return this.finishNode(prop, "RestElement");
  7399. }
  7400. parseBindingProperty() {
  7401. const prop = this.startNode();
  7402. const {
  7403. type,
  7404. startLoc
  7405. } = this.state;
  7406. if (type === 21) {
  7407. return this.parseBindingRestProperty(prop);
  7408. } else if (type === 136) {
  7409. this.expectPlugin("destructuringPrivate", startLoc);
  7410. this.classScope.usePrivateName(this.state.value, startLoc);
  7411. prop.key = this.parsePrivateName();
  7412. } else {
  7413. this.parsePropertyName(prop);
  7414. }
  7415. prop.method = false;
  7416. return this.parseObjPropValue(prop, startLoc, false, false, true, false);
  7417. }
  7418. parseAssignableListItem(flags, decorators) {
  7419. const left = this.parseMaybeDefault();
  7420. this.parseAssignableListItemTypes(left, flags);
  7421. const elt = this.parseMaybeDefault(left.loc.start, left);
  7422. if (decorators.length) {
  7423. left.decorators = decorators;
  7424. }
  7425. return elt;
  7426. }
  7427. parseAssignableListItemTypes(param, flags) {
  7428. return param;
  7429. }
  7430. parseMaybeDefault(startLoc, left) {
  7431. var _startLoc, _left;
  7432. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  7433. left = (_left = left) != null ? _left : this.parseBindingAtom();
  7434. if (!this.eat(29)) return left;
  7435. const node = this.startNodeAt(startLoc);
  7436. node.left = left;
  7437. node.right = this.parseMaybeAssignAllowIn();
  7438. return this.finishNode(node, "AssignmentPattern");
  7439. }
  7440. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  7441. return getOwn$1({
  7442. AssignmentPattern: "left",
  7443. RestElement: "argument",
  7444. ObjectProperty: "value",
  7445. ParenthesizedExpression: "expression",
  7446. ArrayPattern: "elements",
  7447. ObjectPattern: "properties"
  7448. }, type);
  7449. }
  7450. checkLVal(expression, {
  7451. in: ancestor,
  7452. binding = BIND_NONE,
  7453. checkClashes = false,
  7454. strictModeChanged = false,
  7455. hasParenthesizedAncestor = false
  7456. }) {
  7457. var _expression$extra;
  7458. const type = expression.type;
  7459. if (this.isObjectMethod(expression)) return;
  7460. if (type === "MemberExpression") {
  7461. if (binding !== BIND_NONE) {
  7462. this.raise(Errors.InvalidPropertyBindingPattern, {
  7463. at: expression
  7464. });
  7465. }
  7466. return;
  7467. }
  7468. if (type === "Identifier") {
  7469. this.checkIdentifier(expression, binding, strictModeChanged);
  7470. const {
  7471. name
  7472. } = expression;
  7473. if (checkClashes) {
  7474. if (checkClashes.has(name)) {
  7475. this.raise(Errors.ParamDupe, {
  7476. at: expression
  7477. });
  7478. } else {
  7479. checkClashes.add(name);
  7480. }
  7481. }
  7482. return;
  7483. }
  7484. const validity = this.isValidLVal(type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
  7485. if (validity === true) return;
  7486. if (validity === false) {
  7487. const ParseErrorClass = binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  7488. this.raise(ParseErrorClass, {
  7489. at: expression,
  7490. ancestor
  7491. });
  7492. return;
  7493. }
  7494. const [key, isParenthesizedExpression] = Array.isArray(validity) ? validity : [validity, type === "ParenthesizedExpression"];
  7495. const nextAncestor = type === "ArrayPattern" || type === "ObjectPattern" || type === "ParenthesizedExpression" ? {
  7496. type
  7497. } : ancestor;
  7498. for (const child of [].concat(expression[key])) {
  7499. if (child) {
  7500. this.checkLVal(child, {
  7501. in: nextAncestor,
  7502. binding,
  7503. checkClashes,
  7504. strictModeChanged,
  7505. hasParenthesizedAncestor: isParenthesizedExpression
  7506. });
  7507. }
  7508. }
  7509. }
  7510. checkIdentifier(at, bindingType, strictModeChanged = false) {
  7511. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  7512. if (bindingType === BIND_NONE) {
  7513. this.raise(Errors.StrictEvalArguments, {
  7514. at,
  7515. referenceName: at.name
  7516. });
  7517. } else {
  7518. this.raise(Errors.StrictEvalArgumentsBinding, {
  7519. at,
  7520. bindingName: at.name
  7521. });
  7522. }
  7523. }
  7524. if (bindingType & BIND_FLAGS_NO_LET_IN_LEXICAL && at.name === "let") {
  7525. this.raise(Errors.LetInLexicalBinding, {
  7526. at
  7527. });
  7528. }
  7529. if (!(bindingType & BIND_NONE)) {
  7530. this.declareNameFromIdentifier(at, bindingType);
  7531. }
  7532. }
  7533. declareNameFromIdentifier(identifier, binding) {
  7534. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  7535. }
  7536. checkToRestConversion(node, allowPattern) {
  7537. switch (node.type) {
  7538. case "ParenthesizedExpression":
  7539. this.checkToRestConversion(node.expression, allowPattern);
  7540. break;
  7541. case "Identifier":
  7542. case "MemberExpression":
  7543. break;
  7544. case "ArrayExpression":
  7545. case "ObjectExpression":
  7546. if (allowPattern) break;
  7547. default:
  7548. this.raise(Errors.InvalidRestAssignmentPattern, {
  7549. at: node
  7550. });
  7551. }
  7552. }
  7553. checkCommaAfterRest(close) {
  7554. if (!this.match(12)) {
  7555. return false;
  7556. }
  7557. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, {
  7558. at: this.state.startLoc
  7559. });
  7560. return true;
  7561. }
  7562. }
  7563. const getOwn = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  7564. function nonNull(x) {
  7565. if (x == null) {
  7566. throw new Error(`Unexpected ${x} value.`);
  7567. }
  7568. return x;
  7569. }
  7570. function assert(x) {
  7571. if (!x) {
  7572. throw new Error("Assert fail");
  7573. }
  7574. }
  7575. const TSErrors = ParseErrorEnum`typescript`({
  7576. AbstractMethodHasImplementation: ({
  7577. methodName
  7578. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`,
  7579. AbstractPropertyHasInitializer: ({
  7580. propertyName
  7581. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`,
  7582. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  7583. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  7584. AccessorCannotBeOptional: "An 'accessor' property cannot be declared optional.",
  7585. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  7586. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  7587. ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.",
  7588. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  7589. DeclareAccessor: ({
  7590. kind
  7591. }) => `'declare' is not allowed in ${kind}ters.`,
  7592. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  7593. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  7594. DuplicateAccessibilityModifier: ({
  7595. modifier
  7596. }) => `Accessibility modifier already seen.`,
  7597. DuplicateModifier: ({
  7598. modifier
  7599. }) => `Duplicate modifier: '${modifier}'.`,
  7600. EmptyHeritageClauseType: ({
  7601. token
  7602. }) => `'${token}' list cannot be empty.`,
  7603. EmptyTypeArguments: "Type argument list cannot be empty.",
  7604. EmptyTypeParameters: "Type parameter list cannot be empty.",
  7605. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  7606. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  7607. ImportReflectionHasImportType: "An `import module` declaration can not use `type` modifier",
  7608. IncompatibleModifiers: ({
  7609. modifiers
  7610. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`,
  7611. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  7612. IndexSignatureHasAccessibility: ({
  7613. modifier
  7614. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`,
  7615. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  7616. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  7617. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  7618. InitializerNotAllowedInAmbientContext: "Initializers are not allowed in ambient contexts.",
  7619. InvalidModifierOnTypeMember: ({
  7620. modifier
  7621. }) => `'${modifier}' modifier cannot appear on a type member.`,
  7622. InvalidModifierOnTypeParameter: ({
  7623. modifier
  7624. }) => `'${modifier}' modifier cannot appear on a type parameter.`,
  7625. InvalidModifierOnTypeParameterPositions: ({
  7626. modifier
  7627. }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`,
  7628. InvalidModifiersOrder: ({
  7629. orderedModifiers
  7630. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
  7631. InvalidPropertyAccessAfterInstantiationExpression: "Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
  7632. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  7633. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  7634. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  7635. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  7636. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  7637. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  7638. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  7639. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  7640. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  7641. PrivateElementHasAccessibility: ({
  7642. modifier
  7643. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`,
  7644. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  7645. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  7646. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  7647. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  7648. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  7649. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  7650. SingleTypeParameterWithoutTrailingComma: ({
  7651. typeParameterName
  7652. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`,
  7653. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  7654. TupleOptionalAfterType: "A labeled tuple optional element must be declared using a question mark after the name and before the colon (`name?: type`), rather than after the type (`name: type?`).",
  7655. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  7656. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  7657. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  7658. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  7659. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  7660. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  7661. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  7662. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  7663. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  7664. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  7665. UnsupportedSignatureParameterKind: ({
  7666. type
  7667. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`
  7668. });
  7669. function keywordTypeFromName(value) {
  7670. switch (value) {
  7671. case "any":
  7672. return "TSAnyKeyword";
  7673. case "boolean":
  7674. return "TSBooleanKeyword";
  7675. case "bigint":
  7676. return "TSBigIntKeyword";
  7677. case "never":
  7678. return "TSNeverKeyword";
  7679. case "number":
  7680. return "TSNumberKeyword";
  7681. case "object":
  7682. return "TSObjectKeyword";
  7683. case "string":
  7684. return "TSStringKeyword";
  7685. case "symbol":
  7686. return "TSSymbolKeyword";
  7687. case "undefined":
  7688. return "TSUndefinedKeyword";
  7689. case "unknown":
  7690. return "TSUnknownKeyword";
  7691. default:
  7692. return undefined;
  7693. }
  7694. }
  7695. function tsIsAccessModifier(modifier) {
  7696. return modifier === "private" || modifier === "public" || modifier === "protected";
  7697. }
  7698. function tsIsVarianceAnnotations(modifier) {
  7699. return modifier === "in" || modifier === "out";
  7700. }
  7701. var typescript = superClass => class TypeScriptParserMixin extends superClass {
  7702. constructor(...args) {
  7703. super(...args);
  7704. this.tsParseInOutModifiers = this.tsParseModifiers.bind(this, {
  7705. allowedModifiers: ["in", "out"],
  7706. disallowedModifiers: ["const", "public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7707. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7708. });
  7709. this.tsParseConstModifier = this.tsParseModifiers.bind(this, {
  7710. allowedModifiers: ["const"],
  7711. disallowedModifiers: ["in", "out"],
  7712. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  7713. });
  7714. this.tsParseInOutConstModifiers = this.tsParseModifiers.bind(this, {
  7715. allowedModifiers: ["in", "out", "const"],
  7716. disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7717. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7718. });
  7719. }
  7720. getScopeHandler() {
  7721. return TypeScriptScopeHandler;
  7722. }
  7723. tsIsIdentifier() {
  7724. return tokenIsIdentifier(this.state.type);
  7725. }
  7726. tsTokenCanFollowModifier() {
  7727. return (this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(136) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  7728. }
  7729. tsNextTokenCanFollowModifier() {
  7730. this.next();
  7731. return this.tsTokenCanFollowModifier();
  7732. }
  7733. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  7734. if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58 && this.state.type !== 75) {
  7735. return undefined;
  7736. }
  7737. const modifier = this.state.value;
  7738. if (allowedModifiers.indexOf(modifier) !== -1) {
  7739. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7740. return undefined;
  7741. }
  7742. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7743. return modifier;
  7744. }
  7745. }
  7746. return undefined;
  7747. }
  7748. tsParseModifiers({
  7749. allowedModifiers,
  7750. disallowedModifiers,
  7751. stopOnStartOfClassStaticBlock,
  7752. errorTemplate = TSErrors.InvalidModifierOnTypeMember
  7753. }, modified) {
  7754. const enforceOrder = (loc, modifier, before, after) => {
  7755. if (modifier === before && modified[after]) {
  7756. this.raise(TSErrors.InvalidModifiersOrder, {
  7757. at: loc,
  7758. orderedModifiers: [before, after]
  7759. });
  7760. }
  7761. };
  7762. const incompatible = (loc, modifier, mod1, mod2) => {
  7763. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7764. this.raise(TSErrors.IncompatibleModifiers, {
  7765. at: loc,
  7766. modifiers: [mod1, mod2]
  7767. });
  7768. }
  7769. };
  7770. for (;;) {
  7771. const {
  7772. startLoc
  7773. } = this.state;
  7774. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  7775. if (!modifier) break;
  7776. if (tsIsAccessModifier(modifier)) {
  7777. if (modified.accessibility) {
  7778. this.raise(TSErrors.DuplicateAccessibilityModifier, {
  7779. at: startLoc,
  7780. modifier
  7781. });
  7782. } else {
  7783. enforceOrder(startLoc, modifier, modifier, "override");
  7784. enforceOrder(startLoc, modifier, modifier, "static");
  7785. enforceOrder(startLoc, modifier, modifier, "readonly");
  7786. modified.accessibility = modifier;
  7787. }
  7788. } else if (tsIsVarianceAnnotations(modifier)) {
  7789. if (modified[modifier]) {
  7790. this.raise(TSErrors.DuplicateModifier, {
  7791. at: startLoc,
  7792. modifier
  7793. });
  7794. }
  7795. modified[modifier] = true;
  7796. enforceOrder(startLoc, modifier, "in", "out");
  7797. } else {
  7798. if (Object.hasOwnProperty.call(modified, modifier)) {
  7799. this.raise(TSErrors.DuplicateModifier, {
  7800. at: startLoc,
  7801. modifier
  7802. });
  7803. } else {
  7804. enforceOrder(startLoc, modifier, "static", "readonly");
  7805. enforceOrder(startLoc, modifier, "static", "override");
  7806. enforceOrder(startLoc, modifier, "override", "readonly");
  7807. enforceOrder(startLoc, modifier, "abstract", "override");
  7808. incompatible(startLoc, modifier, "declare", "override");
  7809. incompatible(startLoc, modifier, "static", "abstract");
  7810. }
  7811. modified[modifier] = true;
  7812. }
  7813. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7814. this.raise(errorTemplate, {
  7815. at: startLoc,
  7816. modifier
  7817. });
  7818. }
  7819. }
  7820. }
  7821. tsIsListTerminator(kind) {
  7822. switch (kind) {
  7823. case "EnumMembers":
  7824. case "TypeMembers":
  7825. return this.match(8);
  7826. case "HeritageClauseElement":
  7827. return this.match(5);
  7828. case "TupleElementTypes":
  7829. return this.match(3);
  7830. case "TypeParametersOrArguments":
  7831. return this.match(48);
  7832. }
  7833. }
  7834. tsParseList(kind, parseElement) {
  7835. const result = [];
  7836. while (!this.tsIsListTerminator(kind)) {
  7837. result.push(parseElement());
  7838. }
  7839. return result;
  7840. }
  7841. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7842. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7843. }
  7844. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7845. const result = [];
  7846. let trailingCommaPos = -1;
  7847. for (;;) {
  7848. if (this.tsIsListTerminator(kind)) {
  7849. break;
  7850. }
  7851. trailingCommaPos = -1;
  7852. const element = parseElement();
  7853. if (element == null) {
  7854. return undefined;
  7855. }
  7856. result.push(element);
  7857. if (this.eat(12)) {
  7858. trailingCommaPos = this.state.lastTokStart;
  7859. continue;
  7860. }
  7861. if (this.tsIsListTerminator(kind)) {
  7862. break;
  7863. }
  7864. if (expectSuccess) {
  7865. this.expect(12);
  7866. }
  7867. return undefined;
  7868. }
  7869. if (refTrailingCommaPos) {
  7870. refTrailingCommaPos.value = trailingCommaPos;
  7871. }
  7872. return result;
  7873. }
  7874. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7875. if (!skipFirstToken) {
  7876. if (bracket) {
  7877. this.expect(0);
  7878. } else {
  7879. this.expect(47);
  7880. }
  7881. }
  7882. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7883. if (bracket) {
  7884. this.expect(3);
  7885. } else {
  7886. this.expect(48);
  7887. }
  7888. return result;
  7889. }
  7890. tsParseImportType() {
  7891. const node = this.startNode();
  7892. this.expect(83);
  7893. this.expect(10);
  7894. if (!this.match(131)) {
  7895. this.raise(TSErrors.UnsupportedImportTypeArgument, {
  7896. at: this.state.startLoc
  7897. });
  7898. }
  7899. node.argument = super.parseExprAtom();
  7900. this.expect(11);
  7901. if (this.eat(16)) {
  7902. node.qualifier = this.tsParseEntityName();
  7903. }
  7904. if (this.match(47)) {
  7905. node.typeParameters = this.tsParseTypeArguments();
  7906. }
  7907. return this.finishNode(node, "TSImportType");
  7908. }
  7909. tsParseEntityName(allowReservedWords = true) {
  7910. let entity = this.parseIdentifier(allowReservedWords);
  7911. while (this.eat(16)) {
  7912. const node = this.startNodeAtNode(entity);
  7913. node.left = entity;
  7914. node.right = this.parseIdentifier(allowReservedWords);
  7915. entity = this.finishNode(node, "TSQualifiedName");
  7916. }
  7917. return entity;
  7918. }
  7919. tsParseTypeReference() {
  7920. const node = this.startNode();
  7921. node.typeName = this.tsParseEntityName();
  7922. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7923. node.typeParameters = this.tsParseTypeArguments();
  7924. }
  7925. return this.finishNode(node, "TSTypeReference");
  7926. }
  7927. tsParseThisTypePredicate(lhs) {
  7928. this.next();
  7929. const node = this.startNodeAtNode(lhs);
  7930. node.parameterName = lhs;
  7931. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  7932. node.asserts = false;
  7933. return this.finishNode(node, "TSTypePredicate");
  7934. }
  7935. tsParseThisTypeNode() {
  7936. const node = this.startNode();
  7937. this.next();
  7938. return this.finishNode(node, "TSThisType");
  7939. }
  7940. tsParseTypeQuery() {
  7941. const node = this.startNode();
  7942. this.expect(87);
  7943. if (this.match(83)) {
  7944. node.exprName = this.tsParseImportType();
  7945. } else {
  7946. node.exprName = this.tsParseEntityName();
  7947. }
  7948. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7949. node.typeParameters = this.tsParseTypeArguments();
  7950. }
  7951. return this.finishNode(node, "TSTypeQuery");
  7952. }
  7953. tsParseTypeParameter(parseModifiers) {
  7954. const node = this.startNode();
  7955. parseModifiers(node);
  7956. node.name = this.tsParseTypeParameterName();
  7957. node.constraint = this.tsEatThenParseType(81);
  7958. node.default = this.tsEatThenParseType(29);
  7959. return this.finishNode(node, "TSTypeParameter");
  7960. }
  7961. tsTryParseTypeParameters(parseModifiers) {
  7962. if (this.match(47)) {
  7963. return this.tsParseTypeParameters(parseModifiers);
  7964. }
  7965. }
  7966. tsParseTypeParameters(parseModifiers) {
  7967. const node = this.startNode();
  7968. if (this.match(47) || this.match(140)) {
  7969. this.next();
  7970. } else {
  7971. this.unexpected();
  7972. }
  7973. const refTrailingCommaPos = {
  7974. value: -1
  7975. };
  7976. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
  7977. if (node.params.length === 0) {
  7978. this.raise(TSErrors.EmptyTypeParameters, {
  7979. at: node
  7980. });
  7981. }
  7982. if (refTrailingCommaPos.value !== -1) {
  7983. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  7984. }
  7985. return this.finishNode(node, "TSTypeParameterDeclaration");
  7986. }
  7987. tsFillSignature(returnToken, signature) {
  7988. const returnTokenRequired = returnToken === 19;
  7989. const paramsKey = "parameters";
  7990. const returnTypeKey = "typeAnnotation";
  7991. signature.typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  7992. this.expect(10);
  7993. signature[paramsKey] = this.tsParseBindingListForSignature();
  7994. if (returnTokenRequired) {
  7995. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7996. } else if (this.match(returnToken)) {
  7997. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7998. }
  7999. }
  8000. tsParseBindingListForSignature() {
  8001. return super.parseBindingList(11, 41, 2).map(pattern => {
  8002. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  8003. this.raise(TSErrors.UnsupportedSignatureParameterKind, {
  8004. at: pattern,
  8005. type: pattern.type
  8006. });
  8007. }
  8008. return pattern;
  8009. });
  8010. }
  8011. tsParseTypeMemberSemicolon() {
  8012. if (!this.eat(12) && !this.isLineTerminator()) {
  8013. this.expect(13);
  8014. }
  8015. }
  8016. tsParseSignatureMember(kind, node) {
  8017. this.tsFillSignature(14, node);
  8018. this.tsParseTypeMemberSemicolon();
  8019. return this.finishNode(node, kind);
  8020. }
  8021. tsIsUnambiguouslyIndexSignature() {
  8022. this.next();
  8023. if (tokenIsIdentifier(this.state.type)) {
  8024. this.next();
  8025. return this.match(14);
  8026. }
  8027. return false;
  8028. }
  8029. tsTryParseIndexSignature(node) {
  8030. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  8031. return undefined;
  8032. }
  8033. this.expect(0);
  8034. const id = this.parseIdentifier();
  8035. id.typeAnnotation = this.tsParseTypeAnnotation();
  8036. this.resetEndLocation(id);
  8037. this.expect(3);
  8038. node.parameters = [id];
  8039. const type = this.tsTryParseTypeAnnotation();
  8040. if (type) node.typeAnnotation = type;
  8041. this.tsParseTypeMemberSemicolon();
  8042. return this.finishNode(node, "TSIndexSignature");
  8043. }
  8044. tsParsePropertyOrMethodSignature(node, readonly) {
  8045. if (this.eat(17)) node.optional = true;
  8046. const nodeAny = node;
  8047. if (this.match(10) || this.match(47)) {
  8048. if (readonly) {
  8049. this.raise(TSErrors.ReadonlyForMethodSignature, {
  8050. at: node
  8051. });
  8052. }
  8053. const method = nodeAny;
  8054. if (method.kind && this.match(47)) {
  8055. this.raise(TSErrors.AccesorCannotHaveTypeParameters, {
  8056. at: this.state.curPosition()
  8057. });
  8058. }
  8059. this.tsFillSignature(14, method);
  8060. this.tsParseTypeMemberSemicolon();
  8061. const paramsKey = "parameters";
  8062. const returnTypeKey = "typeAnnotation";
  8063. if (method.kind === "get") {
  8064. if (method[paramsKey].length > 0) {
  8065. this.raise(Errors.BadGetterArity, {
  8066. at: this.state.curPosition()
  8067. });
  8068. if (this.isThisParam(method[paramsKey][0])) {
  8069. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  8070. at: this.state.curPosition()
  8071. });
  8072. }
  8073. }
  8074. } else if (method.kind === "set") {
  8075. if (method[paramsKey].length !== 1) {
  8076. this.raise(Errors.BadSetterArity, {
  8077. at: this.state.curPosition()
  8078. });
  8079. } else {
  8080. const firstParameter = method[paramsKey][0];
  8081. if (this.isThisParam(firstParameter)) {
  8082. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  8083. at: this.state.curPosition()
  8084. });
  8085. }
  8086. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  8087. this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, {
  8088. at: this.state.curPosition()
  8089. });
  8090. }
  8091. if (firstParameter.type === "RestElement") {
  8092. this.raise(TSErrors.SetAccesorCannotHaveRestParameter, {
  8093. at: this.state.curPosition()
  8094. });
  8095. }
  8096. }
  8097. if (method[returnTypeKey]) {
  8098. this.raise(TSErrors.SetAccesorCannotHaveReturnType, {
  8099. at: method[returnTypeKey]
  8100. });
  8101. }
  8102. } else {
  8103. method.kind = "method";
  8104. }
  8105. return this.finishNode(method, "TSMethodSignature");
  8106. } else {
  8107. const property = nodeAny;
  8108. if (readonly) property.readonly = true;
  8109. const type = this.tsTryParseTypeAnnotation();
  8110. if (type) property.typeAnnotation = type;
  8111. this.tsParseTypeMemberSemicolon();
  8112. return this.finishNode(property, "TSPropertySignature");
  8113. }
  8114. }
  8115. tsParseTypeMember() {
  8116. const node = this.startNode();
  8117. if (this.match(10) || this.match(47)) {
  8118. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  8119. }
  8120. if (this.match(77)) {
  8121. const id = this.startNode();
  8122. this.next();
  8123. if (this.match(10) || this.match(47)) {
  8124. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  8125. } else {
  8126. node.key = this.createIdentifier(id, "new");
  8127. return this.tsParsePropertyOrMethodSignature(node, false);
  8128. }
  8129. }
  8130. this.tsParseModifiers({
  8131. allowedModifiers: ["readonly"],
  8132. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  8133. }, node);
  8134. const idx = this.tsTryParseIndexSignature(node);
  8135. if (idx) {
  8136. return idx;
  8137. }
  8138. super.parsePropertyName(node);
  8139. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  8140. node.kind = node.key.name;
  8141. super.parsePropertyName(node);
  8142. }
  8143. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  8144. }
  8145. tsParseTypeLiteral() {
  8146. const node = this.startNode();
  8147. node.members = this.tsParseObjectTypeMembers();
  8148. return this.finishNode(node, "TSTypeLiteral");
  8149. }
  8150. tsParseObjectTypeMembers() {
  8151. this.expect(5);
  8152. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  8153. this.expect(8);
  8154. return members;
  8155. }
  8156. tsIsStartOfMappedType() {
  8157. this.next();
  8158. if (this.eat(53)) {
  8159. return this.isContextual(120);
  8160. }
  8161. if (this.isContextual(120)) {
  8162. this.next();
  8163. }
  8164. if (!this.match(0)) {
  8165. return false;
  8166. }
  8167. this.next();
  8168. if (!this.tsIsIdentifier()) {
  8169. return false;
  8170. }
  8171. this.next();
  8172. return this.match(58);
  8173. }
  8174. tsParseMappedTypeParameter() {
  8175. const node = this.startNode();
  8176. node.name = this.tsParseTypeParameterName();
  8177. node.constraint = this.tsExpectThenParseType(58);
  8178. return this.finishNode(node, "TSTypeParameter");
  8179. }
  8180. tsParseMappedType() {
  8181. const node = this.startNode();
  8182. this.expect(5);
  8183. if (this.match(53)) {
  8184. node.readonly = this.state.value;
  8185. this.next();
  8186. this.expectContextual(120);
  8187. } else if (this.eatContextual(120)) {
  8188. node.readonly = true;
  8189. }
  8190. this.expect(0);
  8191. node.typeParameter = this.tsParseMappedTypeParameter();
  8192. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  8193. this.expect(3);
  8194. if (this.match(53)) {
  8195. node.optional = this.state.value;
  8196. this.next();
  8197. this.expect(17);
  8198. } else if (this.eat(17)) {
  8199. node.optional = true;
  8200. }
  8201. node.typeAnnotation = this.tsTryParseType();
  8202. this.semicolon();
  8203. this.expect(8);
  8204. return this.finishNode(node, "TSMappedType");
  8205. }
  8206. tsParseTupleType() {
  8207. const node = this.startNode();
  8208. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  8209. let seenOptionalElement = false;
  8210. let labeledElements = null;
  8211. node.elementTypes.forEach(elementNode => {
  8212. var _labeledElements;
  8213. const {
  8214. type
  8215. } = elementNode;
  8216. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  8217. this.raise(TSErrors.OptionalTypeBeforeRequired, {
  8218. at: elementNode
  8219. });
  8220. }
  8221. seenOptionalElement || (seenOptionalElement = type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType");
  8222. let checkType = type;
  8223. if (type === "TSRestType") {
  8224. elementNode = elementNode.typeAnnotation;
  8225. checkType = elementNode.type;
  8226. }
  8227. const isLabeled = checkType === "TSNamedTupleMember";
  8228. (_labeledElements = labeledElements) != null ? _labeledElements : labeledElements = isLabeled;
  8229. if (labeledElements !== isLabeled) {
  8230. this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
  8231. at: elementNode
  8232. });
  8233. }
  8234. });
  8235. return this.finishNode(node, "TSTupleType");
  8236. }
  8237. tsParseTupleElementType() {
  8238. const {
  8239. startLoc
  8240. } = this.state;
  8241. const rest = this.eat(21);
  8242. let labeled;
  8243. let label;
  8244. let optional;
  8245. let type;
  8246. const isWord = tokenIsKeywordOrIdentifier(this.state.type);
  8247. const chAfterWord = isWord ? this.lookaheadCharCode() : null;
  8248. if (chAfterWord === 58) {
  8249. labeled = true;
  8250. optional = false;
  8251. label = this.parseIdentifier(true);
  8252. this.expect(14);
  8253. type = this.tsParseType();
  8254. } else if (chAfterWord === 63) {
  8255. optional = true;
  8256. const startLoc = this.state.startLoc;
  8257. const wordName = this.state.value;
  8258. const typeOrLabel = this.tsParseNonArrayType();
  8259. if (this.lookaheadCharCode() === 58) {
  8260. labeled = true;
  8261. label = this.createIdentifier(this.startNodeAt(startLoc), wordName);
  8262. this.expect(17);
  8263. this.expect(14);
  8264. type = this.tsParseType();
  8265. } else {
  8266. labeled = false;
  8267. type = typeOrLabel;
  8268. this.expect(17);
  8269. }
  8270. } else {
  8271. type = this.tsParseType();
  8272. optional = this.eat(17);
  8273. labeled = this.eat(14);
  8274. }
  8275. if (labeled) {
  8276. let labeledNode;
  8277. if (label) {
  8278. labeledNode = this.startNodeAtNode(label);
  8279. labeledNode.optional = optional;
  8280. labeledNode.label = label;
  8281. labeledNode.elementType = type;
  8282. if (this.eat(17)) {
  8283. labeledNode.optional = true;
  8284. this.raise(TSErrors.TupleOptionalAfterType, {
  8285. at: this.state.lastTokStartLoc
  8286. });
  8287. }
  8288. } else {
  8289. labeledNode = this.startNodeAtNode(type);
  8290. labeledNode.optional = optional;
  8291. this.raise(TSErrors.InvalidTupleMemberLabel, {
  8292. at: type
  8293. });
  8294. labeledNode.label = type;
  8295. labeledNode.elementType = this.tsParseType();
  8296. }
  8297. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  8298. } else if (optional) {
  8299. const optionalTypeNode = this.startNodeAtNode(type);
  8300. optionalTypeNode.typeAnnotation = type;
  8301. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  8302. }
  8303. if (rest) {
  8304. const restNode = this.startNodeAt(startLoc);
  8305. restNode.typeAnnotation = type;
  8306. type = this.finishNode(restNode, "TSRestType");
  8307. }
  8308. return type;
  8309. }
  8310. tsParseParenthesizedType() {
  8311. const node = this.startNode();
  8312. this.expect(10);
  8313. node.typeAnnotation = this.tsParseType();
  8314. this.expect(11);
  8315. return this.finishNode(node, "TSParenthesizedType");
  8316. }
  8317. tsParseFunctionOrConstructorType(type, abstract) {
  8318. const node = this.startNode();
  8319. if (type === "TSConstructorType") {
  8320. node.abstract = !!abstract;
  8321. if (abstract) this.next();
  8322. this.next();
  8323. }
  8324. this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
  8325. return this.finishNode(node, type);
  8326. }
  8327. tsParseLiteralTypeNode() {
  8328. const node = this.startNode();
  8329. node.literal = (() => {
  8330. switch (this.state.type) {
  8331. case 132:
  8332. case 133:
  8333. case 131:
  8334. case 85:
  8335. case 86:
  8336. return super.parseExprAtom();
  8337. default:
  8338. this.unexpected();
  8339. }
  8340. })();
  8341. return this.finishNode(node, "TSLiteralType");
  8342. }
  8343. tsParseTemplateLiteralType() {
  8344. const node = this.startNode();
  8345. node.literal = super.parseTemplate(false);
  8346. return this.finishNode(node, "TSLiteralType");
  8347. }
  8348. parseTemplateSubstitution() {
  8349. if (this.state.inType) return this.tsParseType();
  8350. return super.parseTemplateSubstitution();
  8351. }
  8352. tsParseThisTypeOrThisTypePredicate() {
  8353. const thisKeyword = this.tsParseThisTypeNode();
  8354. if (this.isContextual(114) && !this.hasPrecedingLineBreak()) {
  8355. return this.tsParseThisTypePredicate(thisKeyword);
  8356. } else {
  8357. return thisKeyword;
  8358. }
  8359. }
  8360. tsParseNonArrayType() {
  8361. switch (this.state.type) {
  8362. case 131:
  8363. case 132:
  8364. case 133:
  8365. case 85:
  8366. case 86:
  8367. return this.tsParseLiteralTypeNode();
  8368. case 53:
  8369. if (this.state.value === "-") {
  8370. const node = this.startNode();
  8371. const nextToken = this.lookahead();
  8372. if (nextToken.type !== 132 && nextToken.type !== 133) {
  8373. this.unexpected();
  8374. }
  8375. node.literal = this.parseMaybeUnary();
  8376. return this.finishNode(node, "TSLiteralType");
  8377. }
  8378. break;
  8379. case 78:
  8380. return this.tsParseThisTypeOrThisTypePredicate();
  8381. case 87:
  8382. return this.tsParseTypeQuery();
  8383. case 83:
  8384. return this.tsParseImportType();
  8385. case 5:
  8386. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  8387. case 0:
  8388. return this.tsParseTupleType();
  8389. case 10:
  8390. return this.tsParseParenthesizedType();
  8391. case 25:
  8392. case 24:
  8393. return this.tsParseTemplateLiteralType();
  8394. default:
  8395. {
  8396. const {
  8397. type
  8398. } = this.state;
  8399. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  8400. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  8401. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  8402. const node = this.startNode();
  8403. this.next();
  8404. return this.finishNode(node, nodeType);
  8405. }
  8406. return this.tsParseTypeReference();
  8407. }
  8408. }
  8409. }
  8410. this.unexpected();
  8411. }
  8412. tsParseArrayTypeOrHigher() {
  8413. let type = this.tsParseNonArrayType();
  8414. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  8415. if (this.match(3)) {
  8416. const node = this.startNodeAtNode(type);
  8417. node.elementType = type;
  8418. this.expect(3);
  8419. type = this.finishNode(node, "TSArrayType");
  8420. } else {
  8421. const node = this.startNodeAtNode(type);
  8422. node.objectType = type;
  8423. node.indexType = this.tsParseType();
  8424. this.expect(3);
  8425. type = this.finishNode(node, "TSIndexedAccessType");
  8426. }
  8427. }
  8428. return type;
  8429. }
  8430. tsParseTypeOperator() {
  8431. const node = this.startNode();
  8432. const operator = this.state.value;
  8433. this.next();
  8434. node.operator = operator;
  8435. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  8436. if (operator === "readonly") {
  8437. this.tsCheckTypeAnnotationForReadOnly(node);
  8438. }
  8439. return this.finishNode(node, "TSTypeOperator");
  8440. }
  8441. tsCheckTypeAnnotationForReadOnly(node) {
  8442. switch (node.typeAnnotation.type) {
  8443. case "TSTupleType":
  8444. case "TSArrayType":
  8445. return;
  8446. default:
  8447. this.raise(TSErrors.UnexpectedReadonly, {
  8448. at: node
  8449. });
  8450. }
  8451. }
  8452. tsParseInferType() {
  8453. const node = this.startNode();
  8454. this.expectContextual(113);
  8455. const typeParameter = this.startNode();
  8456. typeParameter.name = this.tsParseTypeParameterName();
  8457. typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
  8458. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  8459. return this.finishNode(node, "TSInferType");
  8460. }
  8461. tsParseConstraintForInferType() {
  8462. if (this.eat(81)) {
  8463. const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
  8464. if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
  8465. return constraint;
  8466. }
  8467. }
  8468. }
  8469. tsParseTypeOperatorOrHigher() {
  8470. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  8471. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(113) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
  8472. }
  8473. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  8474. const node = this.startNode();
  8475. const hasLeadingOperator = this.eat(operator);
  8476. const types = [];
  8477. do {
  8478. types.push(parseConstituentType());
  8479. } while (this.eat(operator));
  8480. if (types.length === 1 && !hasLeadingOperator) {
  8481. return types[0];
  8482. }
  8483. node.types = types;
  8484. return this.finishNode(node, kind);
  8485. }
  8486. tsParseIntersectionTypeOrHigher() {
  8487. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  8488. }
  8489. tsParseUnionTypeOrHigher() {
  8490. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  8491. }
  8492. tsIsStartOfFunctionType() {
  8493. if (this.match(47)) {
  8494. return true;
  8495. }
  8496. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  8497. }
  8498. tsSkipParameterStart() {
  8499. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  8500. this.next();
  8501. return true;
  8502. }
  8503. if (this.match(5)) {
  8504. const {
  8505. errors
  8506. } = this.state;
  8507. const previousErrorCount = errors.length;
  8508. try {
  8509. this.parseObjectLike(8, true);
  8510. return errors.length === previousErrorCount;
  8511. } catch (_unused) {
  8512. return false;
  8513. }
  8514. }
  8515. if (this.match(0)) {
  8516. this.next();
  8517. const {
  8518. errors
  8519. } = this.state;
  8520. const previousErrorCount = errors.length;
  8521. try {
  8522. super.parseBindingList(3, 93, 1);
  8523. return errors.length === previousErrorCount;
  8524. } catch (_unused2) {
  8525. return false;
  8526. }
  8527. }
  8528. return false;
  8529. }
  8530. tsIsUnambiguouslyStartOfFunctionType() {
  8531. this.next();
  8532. if (this.match(11) || this.match(21)) {
  8533. return true;
  8534. }
  8535. if (this.tsSkipParameterStart()) {
  8536. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  8537. return true;
  8538. }
  8539. if (this.match(11)) {
  8540. this.next();
  8541. if (this.match(19)) {
  8542. return true;
  8543. }
  8544. }
  8545. }
  8546. return false;
  8547. }
  8548. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  8549. return this.tsInType(() => {
  8550. const t = this.startNode();
  8551. this.expect(returnToken);
  8552. const node = this.startNode();
  8553. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  8554. if (asserts && this.match(78)) {
  8555. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  8556. if (thisTypePredicate.type === "TSThisType") {
  8557. node.parameterName = thisTypePredicate;
  8558. node.asserts = true;
  8559. node.typeAnnotation = null;
  8560. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  8561. } else {
  8562. this.resetStartLocationFromNode(thisTypePredicate, node);
  8563. thisTypePredicate.asserts = true;
  8564. }
  8565. t.typeAnnotation = thisTypePredicate;
  8566. return this.finishNode(t, "TSTypeAnnotation");
  8567. }
  8568. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  8569. if (!typePredicateVariable) {
  8570. if (!asserts) {
  8571. return this.tsParseTypeAnnotation(false, t);
  8572. }
  8573. node.parameterName = this.parseIdentifier();
  8574. node.asserts = asserts;
  8575. node.typeAnnotation = null;
  8576. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8577. return this.finishNode(t, "TSTypeAnnotation");
  8578. }
  8579. const type = this.tsParseTypeAnnotation(false);
  8580. node.parameterName = typePredicateVariable;
  8581. node.typeAnnotation = type;
  8582. node.asserts = asserts;
  8583. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8584. return this.finishNode(t, "TSTypeAnnotation");
  8585. });
  8586. }
  8587. tsTryParseTypeOrTypePredicateAnnotation() {
  8588. return this.match(14) ? this.tsParseTypeOrTypePredicateAnnotation(14) : undefined;
  8589. }
  8590. tsTryParseTypeAnnotation() {
  8591. return this.match(14) ? this.tsParseTypeAnnotation() : undefined;
  8592. }
  8593. tsTryParseType() {
  8594. return this.tsEatThenParseType(14);
  8595. }
  8596. tsParseTypePredicatePrefix() {
  8597. const id = this.parseIdentifier();
  8598. if (this.isContextual(114) && !this.hasPrecedingLineBreak()) {
  8599. this.next();
  8600. return id;
  8601. }
  8602. }
  8603. tsParseTypePredicateAsserts() {
  8604. if (this.state.type !== 107) {
  8605. return false;
  8606. }
  8607. const containsEsc = this.state.containsEsc;
  8608. this.next();
  8609. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  8610. return false;
  8611. }
  8612. if (containsEsc) {
  8613. this.raise(Errors.InvalidEscapedReservedWord, {
  8614. at: this.state.lastTokStartLoc,
  8615. reservedWord: "asserts"
  8616. });
  8617. }
  8618. return true;
  8619. }
  8620. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  8621. this.tsInType(() => {
  8622. if (eatColon) this.expect(14);
  8623. t.typeAnnotation = this.tsParseType();
  8624. });
  8625. return this.finishNode(t, "TSTypeAnnotation");
  8626. }
  8627. tsParseType() {
  8628. assert(this.state.inType);
  8629. const type = this.tsParseNonConditionalType();
  8630. if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
  8631. return type;
  8632. }
  8633. const node = this.startNodeAtNode(type);
  8634. node.checkType = type;
  8635. node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
  8636. this.expect(17);
  8637. node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8638. this.expect(14);
  8639. node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8640. return this.finishNode(node, "TSConditionalType");
  8641. }
  8642. isAbstractConstructorSignature() {
  8643. return this.isContextual(122) && this.lookahead().type === 77;
  8644. }
  8645. tsParseNonConditionalType() {
  8646. if (this.tsIsStartOfFunctionType()) {
  8647. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  8648. }
  8649. if (this.match(77)) {
  8650. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  8651. } else if (this.isAbstractConstructorSignature()) {
  8652. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  8653. }
  8654. return this.tsParseUnionTypeOrHigher();
  8655. }
  8656. tsParseTypeAssertion() {
  8657. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8658. this.raise(TSErrors.ReservedTypeAssertion, {
  8659. at: this.state.startLoc
  8660. });
  8661. }
  8662. const node = this.startNode();
  8663. node.typeAnnotation = this.tsInType(() => {
  8664. this.next();
  8665. return this.match(75) ? this.tsParseTypeReference() : this.tsParseType();
  8666. });
  8667. this.expect(48);
  8668. node.expression = this.parseMaybeUnary();
  8669. return this.finishNode(node, "TSTypeAssertion");
  8670. }
  8671. tsParseHeritageClause(token) {
  8672. const originalStartLoc = this.state.startLoc;
  8673. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
  8674. const node = this.startNode();
  8675. node.expression = this.tsParseEntityName();
  8676. if (this.match(47)) {
  8677. node.typeParameters = this.tsParseTypeArguments();
  8678. }
  8679. return this.finishNode(node, "TSExpressionWithTypeArguments");
  8680. });
  8681. if (!delimitedList.length) {
  8682. this.raise(TSErrors.EmptyHeritageClauseType, {
  8683. at: originalStartLoc,
  8684. token
  8685. });
  8686. }
  8687. return delimitedList;
  8688. }
  8689. tsParseInterfaceDeclaration(node, properties = {}) {
  8690. if (this.hasFollowingLineBreak()) return null;
  8691. this.expectContextual(127);
  8692. if (properties.declare) node.declare = true;
  8693. if (tokenIsIdentifier(this.state.type)) {
  8694. node.id = this.parseIdentifier();
  8695. this.checkIdentifier(node.id, BIND_TS_INTERFACE);
  8696. } else {
  8697. node.id = null;
  8698. this.raise(TSErrors.MissingInterfaceName, {
  8699. at: this.state.startLoc
  8700. });
  8701. }
  8702. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  8703. if (this.eat(81)) {
  8704. node.extends = this.tsParseHeritageClause("extends");
  8705. }
  8706. const body = this.startNode();
  8707. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  8708. node.body = this.finishNode(body, "TSInterfaceBody");
  8709. return this.finishNode(node, "TSInterfaceDeclaration");
  8710. }
  8711. tsParseTypeAliasDeclaration(node) {
  8712. node.id = this.parseIdentifier();
  8713. this.checkIdentifier(node.id, BIND_TS_TYPE);
  8714. node.typeAnnotation = this.tsInType(() => {
  8715. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers);
  8716. this.expect(29);
  8717. if (this.isContextual(112) && this.lookahead().type !== 16) {
  8718. const node = this.startNode();
  8719. this.next();
  8720. return this.finishNode(node, "TSIntrinsicKeyword");
  8721. }
  8722. return this.tsParseType();
  8723. });
  8724. this.semicolon();
  8725. return this.finishNode(node, "TSTypeAliasDeclaration");
  8726. }
  8727. tsInNoContext(cb) {
  8728. const oldContext = this.state.context;
  8729. this.state.context = [oldContext[0]];
  8730. try {
  8731. return cb();
  8732. } finally {
  8733. this.state.context = oldContext;
  8734. }
  8735. }
  8736. tsInType(cb) {
  8737. const oldInType = this.state.inType;
  8738. this.state.inType = true;
  8739. try {
  8740. return cb();
  8741. } finally {
  8742. this.state.inType = oldInType;
  8743. }
  8744. }
  8745. tsInDisallowConditionalTypesContext(cb) {
  8746. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8747. this.state.inDisallowConditionalTypesContext = true;
  8748. try {
  8749. return cb();
  8750. } finally {
  8751. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8752. }
  8753. }
  8754. tsInAllowConditionalTypesContext(cb) {
  8755. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8756. this.state.inDisallowConditionalTypesContext = false;
  8757. try {
  8758. return cb();
  8759. } finally {
  8760. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8761. }
  8762. }
  8763. tsEatThenParseType(token) {
  8764. return !this.match(token) ? undefined : this.tsNextThenParseType();
  8765. }
  8766. tsExpectThenParseType(token) {
  8767. return this.tsDoThenParseType(() => this.expect(token));
  8768. }
  8769. tsNextThenParseType() {
  8770. return this.tsDoThenParseType(() => this.next());
  8771. }
  8772. tsDoThenParseType(cb) {
  8773. return this.tsInType(() => {
  8774. cb();
  8775. return this.tsParseType();
  8776. });
  8777. }
  8778. tsParseEnumMember() {
  8779. const node = this.startNode();
  8780. node.id = this.match(131) ? super.parseStringLiteral(this.state.value) : this.parseIdentifier(true);
  8781. if (this.eat(29)) {
  8782. node.initializer = super.parseMaybeAssignAllowIn();
  8783. }
  8784. return this.finishNode(node, "TSEnumMember");
  8785. }
  8786. tsParseEnumDeclaration(node, properties = {}) {
  8787. if (properties.const) node.const = true;
  8788. if (properties.declare) node.declare = true;
  8789. this.expectContextual(124);
  8790. node.id = this.parseIdentifier();
  8791. this.checkIdentifier(node.id, node.const ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  8792. this.expect(5);
  8793. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8794. this.expect(8);
  8795. return this.finishNode(node, "TSEnumDeclaration");
  8796. }
  8797. tsParseModuleBlock() {
  8798. const node = this.startNode();
  8799. this.scope.enter(SCOPE_OTHER);
  8800. this.expect(5);
  8801. super.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8802. this.scope.exit();
  8803. return this.finishNode(node, "TSModuleBlock");
  8804. }
  8805. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8806. node.id = this.parseIdentifier();
  8807. if (!nested) {
  8808. this.checkIdentifier(node.id, BIND_TS_NAMESPACE);
  8809. }
  8810. if (this.eat(16)) {
  8811. const inner = this.startNode();
  8812. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8813. node.body = inner;
  8814. } else {
  8815. this.scope.enter(SCOPE_TS_MODULE);
  8816. this.prodParam.enter(PARAM);
  8817. node.body = this.tsParseModuleBlock();
  8818. this.prodParam.exit();
  8819. this.scope.exit();
  8820. }
  8821. return this.finishNode(node, "TSModuleDeclaration");
  8822. }
  8823. tsParseAmbientExternalModuleDeclaration(node) {
  8824. if (this.isContextual(110)) {
  8825. node.global = true;
  8826. node.id = this.parseIdentifier();
  8827. } else if (this.match(131)) {
  8828. node.id = super.parseStringLiteral(this.state.value);
  8829. } else {
  8830. this.unexpected();
  8831. }
  8832. if (this.match(5)) {
  8833. this.scope.enter(SCOPE_TS_MODULE);
  8834. this.prodParam.enter(PARAM);
  8835. node.body = this.tsParseModuleBlock();
  8836. this.prodParam.exit();
  8837. this.scope.exit();
  8838. } else {
  8839. this.semicolon();
  8840. }
  8841. return this.finishNode(node, "TSModuleDeclaration");
  8842. }
  8843. tsParseImportEqualsDeclaration(node, isExport) {
  8844. node.isExport = isExport || false;
  8845. node.id = this.parseIdentifier();
  8846. this.checkIdentifier(node.id, BIND_FLAGS_TS_IMPORT);
  8847. this.expect(29);
  8848. const moduleReference = this.tsParseModuleReference();
  8849. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8850. this.raise(TSErrors.ImportAliasHasImportType, {
  8851. at: moduleReference
  8852. });
  8853. }
  8854. node.moduleReference = moduleReference;
  8855. this.semicolon();
  8856. return this.finishNode(node, "TSImportEqualsDeclaration");
  8857. }
  8858. tsIsExternalModuleReference() {
  8859. return this.isContextual(117) && this.lookaheadCharCode() === 40;
  8860. }
  8861. tsParseModuleReference() {
  8862. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  8863. }
  8864. tsParseExternalModuleReference() {
  8865. const node = this.startNode();
  8866. this.expectContextual(117);
  8867. this.expect(10);
  8868. if (!this.match(131)) {
  8869. this.unexpected();
  8870. }
  8871. node.expression = super.parseExprAtom();
  8872. this.expect(11);
  8873. return this.finishNode(node, "TSExternalModuleReference");
  8874. }
  8875. tsLookAhead(f) {
  8876. const state = this.state.clone();
  8877. const res = f();
  8878. this.state = state;
  8879. return res;
  8880. }
  8881. tsTryParseAndCatch(f) {
  8882. const result = this.tryParse(abort => f() || abort());
  8883. if (result.aborted || !result.node) return undefined;
  8884. if (result.error) this.state = result.failState;
  8885. return result.node;
  8886. }
  8887. tsTryParse(f) {
  8888. const state = this.state.clone();
  8889. const result = f();
  8890. if (result !== undefined && result !== false) {
  8891. return result;
  8892. } else {
  8893. this.state = state;
  8894. return undefined;
  8895. }
  8896. }
  8897. tsTryParseDeclare(nany) {
  8898. if (this.isLineTerminator()) {
  8899. return;
  8900. }
  8901. let starttype = this.state.type;
  8902. let kind;
  8903. if (this.isContextual(99)) {
  8904. starttype = 74;
  8905. kind = "let";
  8906. }
  8907. return this.tsInAmbientContext(() => {
  8908. if (starttype === 68) {
  8909. nany.declare = true;
  8910. return super.parseFunctionStatement(nany, false, false);
  8911. }
  8912. if (starttype === 80) {
  8913. nany.declare = true;
  8914. return this.parseClass(nany, true, false);
  8915. }
  8916. if (starttype === 124) {
  8917. return this.tsParseEnumDeclaration(nany, {
  8918. declare: true
  8919. });
  8920. }
  8921. if (starttype === 110) {
  8922. return this.tsParseAmbientExternalModuleDeclaration(nany);
  8923. }
  8924. if (starttype === 75 || starttype === 74) {
  8925. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  8926. nany.declare = true;
  8927. return this.parseVarStatement(nany, kind || this.state.value, true);
  8928. }
  8929. this.expect(75);
  8930. return this.tsParseEnumDeclaration(nany, {
  8931. const: true,
  8932. declare: true
  8933. });
  8934. }
  8935. if (starttype === 127) {
  8936. const result = this.tsParseInterfaceDeclaration(nany, {
  8937. declare: true
  8938. });
  8939. if (result) return result;
  8940. }
  8941. if (tokenIsIdentifier(starttype)) {
  8942. return this.tsParseDeclaration(nany, this.state.value, true, null);
  8943. }
  8944. });
  8945. }
  8946. tsTryParseExportDeclaration() {
  8947. return this.tsParseDeclaration(this.startNode(), this.state.value, true, null);
  8948. }
  8949. tsParseExpressionStatement(node, expr, decorators) {
  8950. switch (expr.name) {
  8951. case "declare":
  8952. {
  8953. const declaration = this.tsTryParseDeclare(node);
  8954. if (declaration) {
  8955. declaration.declare = true;
  8956. return declaration;
  8957. }
  8958. break;
  8959. }
  8960. case "global":
  8961. if (this.match(5)) {
  8962. this.scope.enter(SCOPE_TS_MODULE);
  8963. this.prodParam.enter(PARAM);
  8964. const mod = node;
  8965. mod.global = true;
  8966. mod.id = expr;
  8967. mod.body = this.tsParseModuleBlock();
  8968. this.scope.exit();
  8969. this.prodParam.exit();
  8970. return this.finishNode(mod, "TSModuleDeclaration");
  8971. }
  8972. break;
  8973. default:
  8974. return this.tsParseDeclaration(node, expr.name, false, decorators);
  8975. }
  8976. }
  8977. tsParseDeclaration(node, value, next, decorators) {
  8978. switch (value) {
  8979. case "abstract":
  8980. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  8981. return this.tsParseAbstractDeclaration(node, decorators);
  8982. }
  8983. break;
  8984. case "module":
  8985. if (this.tsCheckLineTerminator(next)) {
  8986. if (this.match(131)) {
  8987. return this.tsParseAmbientExternalModuleDeclaration(node);
  8988. } else if (tokenIsIdentifier(this.state.type)) {
  8989. return this.tsParseModuleOrNamespaceDeclaration(node);
  8990. }
  8991. }
  8992. break;
  8993. case "namespace":
  8994. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8995. return this.tsParseModuleOrNamespaceDeclaration(node);
  8996. }
  8997. break;
  8998. case "type":
  8999. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  9000. return this.tsParseTypeAliasDeclaration(node);
  9001. }
  9002. break;
  9003. }
  9004. }
  9005. tsCheckLineTerminator(next) {
  9006. if (next) {
  9007. if (this.hasFollowingLineBreak()) return false;
  9008. this.next();
  9009. return true;
  9010. }
  9011. return !this.isLineTerminator();
  9012. }
  9013. tsTryParseGenericAsyncArrowFunction(startLoc) {
  9014. if (!this.match(47)) {
  9015. return undefined;
  9016. }
  9017. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9018. this.state.maybeInArrowParameters = true;
  9019. const res = this.tsTryParseAndCatch(() => {
  9020. const node = this.startNodeAt(startLoc);
  9021. node.typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  9022. super.parseFunctionParams(node);
  9023. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  9024. this.expect(19);
  9025. return node;
  9026. });
  9027. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9028. if (!res) {
  9029. return undefined;
  9030. }
  9031. return super.parseArrowExpression(res, null, true);
  9032. }
  9033. tsParseTypeArgumentsInExpression() {
  9034. if (this.reScan_lt() !== 47) {
  9035. return undefined;
  9036. }
  9037. return this.tsParseTypeArguments();
  9038. }
  9039. tsParseTypeArguments() {
  9040. const node = this.startNode();
  9041. node.params = this.tsInType(() => this.tsInNoContext(() => {
  9042. this.expect(47);
  9043. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  9044. }));
  9045. if (node.params.length === 0) {
  9046. this.raise(TSErrors.EmptyTypeArguments, {
  9047. at: node
  9048. });
  9049. }
  9050. this.expect(48);
  9051. return this.finishNode(node, "TSTypeParameterInstantiation");
  9052. }
  9053. tsIsDeclarationStart() {
  9054. return tokenIsTSDeclarationStart(this.state.type);
  9055. }
  9056. isExportDefaultSpecifier() {
  9057. if (this.tsIsDeclarationStart()) return false;
  9058. return super.isExportDefaultSpecifier();
  9059. }
  9060. parseAssignableListItem(flags, decorators) {
  9061. const startLoc = this.state.startLoc;
  9062. const modified = {};
  9063. this.tsParseModifiers({
  9064. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  9065. }, modified);
  9066. const accessibility = modified.accessibility;
  9067. const override = modified.override;
  9068. const readonly = modified.readonly;
  9069. if (!(flags & 4) && (accessibility || readonly || override)) {
  9070. this.raise(TSErrors.UnexpectedParameterModifier, {
  9071. at: startLoc
  9072. });
  9073. }
  9074. const left = this.parseMaybeDefault();
  9075. this.parseAssignableListItemTypes(left, flags);
  9076. const elt = this.parseMaybeDefault(left.loc.start, left);
  9077. if (accessibility || readonly || override) {
  9078. const pp = this.startNodeAt(startLoc);
  9079. if (decorators.length) {
  9080. pp.decorators = decorators;
  9081. }
  9082. if (accessibility) pp.accessibility = accessibility;
  9083. if (readonly) pp.readonly = readonly;
  9084. if (override) pp.override = override;
  9085. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  9086. this.raise(TSErrors.UnsupportedParameterPropertyKind, {
  9087. at: pp
  9088. });
  9089. }
  9090. pp.parameter = elt;
  9091. return this.finishNode(pp, "TSParameterProperty");
  9092. }
  9093. if (decorators.length) {
  9094. left.decorators = decorators;
  9095. }
  9096. return elt;
  9097. }
  9098. isSimpleParameter(node) {
  9099. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  9100. }
  9101. tsDisallowOptionalPattern(node) {
  9102. for (const param of node.params) {
  9103. if (param.type !== "Identifier" && param.optional && !this.state.isAmbientContext) {
  9104. this.raise(TSErrors.PatternIsOptional, {
  9105. at: param
  9106. });
  9107. }
  9108. }
  9109. }
  9110. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  9111. super.setArrowFunctionParameters(node, params, trailingCommaLoc);
  9112. this.tsDisallowOptionalPattern(node);
  9113. }
  9114. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  9115. if (this.match(14)) {
  9116. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9117. }
  9118. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  9119. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  9120. return this.finishNode(node, bodilessType);
  9121. }
  9122. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  9123. this.raise(TSErrors.DeclareFunctionHasImplementation, {
  9124. at: node
  9125. });
  9126. if (node.declare) {
  9127. return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  9128. }
  9129. }
  9130. this.tsDisallowOptionalPattern(node);
  9131. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  9132. }
  9133. registerFunctionStatementId(node) {
  9134. if (!node.body && node.id) {
  9135. this.checkIdentifier(node.id, BIND_TS_AMBIENT);
  9136. } else {
  9137. super.registerFunctionStatementId(node);
  9138. }
  9139. }
  9140. tsCheckForInvalidTypeCasts(items) {
  9141. items.forEach(node => {
  9142. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  9143. this.raise(TSErrors.UnexpectedTypeAnnotation, {
  9144. at: node.typeAnnotation
  9145. });
  9146. }
  9147. });
  9148. }
  9149. toReferencedList(exprList, isInParens) {
  9150. this.tsCheckForInvalidTypeCasts(exprList);
  9151. return exprList;
  9152. }
  9153. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  9154. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  9155. if (node.type === "ArrayExpression") {
  9156. this.tsCheckForInvalidTypeCasts(node.elements);
  9157. }
  9158. return node;
  9159. }
  9160. parseSubscript(base, startLoc, noCalls, state) {
  9161. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  9162. this.state.canStartJSXElement = false;
  9163. this.next();
  9164. const nonNullExpression = this.startNodeAt(startLoc);
  9165. nonNullExpression.expression = base;
  9166. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  9167. }
  9168. let isOptionalCall = false;
  9169. if (this.match(18) && this.lookaheadCharCode() === 60) {
  9170. if (noCalls) {
  9171. state.stop = true;
  9172. return base;
  9173. }
  9174. state.optionalChainMember = isOptionalCall = true;
  9175. this.next();
  9176. }
  9177. if (this.match(47) || this.match(51)) {
  9178. let missingParenErrorLoc;
  9179. const result = this.tsTryParseAndCatch(() => {
  9180. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  9181. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startLoc);
  9182. if (asyncArrowFn) {
  9183. return asyncArrowFn;
  9184. }
  9185. }
  9186. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9187. if (!typeArguments) return;
  9188. if (isOptionalCall && !this.match(10)) {
  9189. missingParenErrorLoc = this.state.curPosition();
  9190. return;
  9191. }
  9192. if (tokenIsTemplate(this.state.type)) {
  9193. const result = super.parseTaggedTemplateExpression(base, startLoc, state);
  9194. result.typeParameters = typeArguments;
  9195. return result;
  9196. }
  9197. if (!noCalls && this.eat(10)) {
  9198. const node = this.startNodeAt(startLoc);
  9199. node.callee = base;
  9200. node.arguments = this.parseCallExpressionArguments(11, false);
  9201. this.tsCheckForInvalidTypeCasts(node.arguments);
  9202. node.typeParameters = typeArguments;
  9203. if (state.optionalChainMember) {
  9204. node.optional = isOptionalCall;
  9205. }
  9206. return this.finishCallExpression(node, state.optionalChainMember);
  9207. }
  9208. const tokenType = this.state.type;
  9209. if (tokenType === 48 || tokenType === 52 || tokenType !== 10 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
  9210. return;
  9211. }
  9212. const node = this.startNodeAt(startLoc);
  9213. node.expression = base;
  9214. node.typeParameters = typeArguments;
  9215. return this.finishNode(node, "TSInstantiationExpression");
  9216. });
  9217. if (missingParenErrorLoc) {
  9218. this.unexpected(missingParenErrorLoc, 10);
  9219. }
  9220. if (result) {
  9221. if (result.type === "TSInstantiationExpression" && (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40)) {
  9222. this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, {
  9223. at: this.state.startLoc
  9224. });
  9225. }
  9226. return result;
  9227. }
  9228. }
  9229. return super.parseSubscript(base, startLoc, noCalls, state);
  9230. }
  9231. parseNewCallee(node) {
  9232. var _callee$extra;
  9233. super.parseNewCallee(node);
  9234. const {
  9235. callee
  9236. } = node;
  9237. if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
  9238. node.typeParameters = callee.typeParameters;
  9239. node.callee = callee.expression;
  9240. }
  9241. }
  9242. parseExprOp(left, leftStartLoc, minPrec) {
  9243. let isSatisfies;
  9244. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && (this.isContextual(93) || (isSatisfies = this.isContextual(118)))) {
  9245. const node = this.startNodeAt(leftStartLoc);
  9246. node.expression = left;
  9247. node.typeAnnotation = this.tsInType(() => {
  9248. this.next();
  9249. if (this.match(75)) {
  9250. if (isSatisfies) {
  9251. this.raise(Errors.UnexpectedKeyword, {
  9252. at: this.state.startLoc,
  9253. keyword: "const"
  9254. });
  9255. }
  9256. return this.tsParseTypeReference();
  9257. }
  9258. return this.tsParseType();
  9259. });
  9260. this.finishNode(node, isSatisfies ? "TSSatisfiesExpression" : "TSAsExpression");
  9261. this.reScan_lt_gt();
  9262. return this.parseExprOp(node, leftStartLoc, minPrec);
  9263. }
  9264. return super.parseExprOp(left, leftStartLoc, minPrec);
  9265. }
  9266. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9267. if (!this.state.isAmbientContext) {
  9268. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  9269. }
  9270. }
  9271. checkImportReflection(node) {
  9272. super.checkImportReflection(node);
  9273. if (node.module && node.importKind !== "value") {
  9274. this.raise(TSErrors.ImportReflectionHasImportType, {
  9275. at: node.specifiers[0].loc.start
  9276. });
  9277. }
  9278. }
  9279. checkDuplicateExports() {}
  9280. parseImport(node) {
  9281. node.importKind = "value";
  9282. if (tokenIsIdentifier(this.state.type) || this.match(55) || this.match(5)) {
  9283. let ahead = this.lookahead();
  9284. if (this.isContextual(128) && ahead.type !== 12 && ahead.type !== 97 && ahead.type !== 29) {
  9285. node.importKind = "type";
  9286. this.next();
  9287. ahead = this.lookahead();
  9288. }
  9289. if (tokenIsIdentifier(this.state.type) && ahead.type === 29) {
  9290. return this.tsParseImportEqualsDeclaration(node);
  9291. }
  9292. }
  9293. const importNode = super.parseImport(node);
  9294. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  9295. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, {
  9296. at: importNode
  9297. });
  9298. }
  9299. return importNode;
  9300. }
  9301. parseExport(node, decorators) {
  9302. if (this.match(83)) {
  9303. this.next();
  9304. if (this.isContextual(128) && this.lookaheadCharCode() !== 61) {
  9305. node.importKind = "type";
  9306. this.next();
  9307. } else {
  9308. node.importKind = "value";
  9309. }
  9310. return this.tsParseImportEqualsDeclaration(node, true);
  9311. } else if (this.eat(29)) {
  9312. const assign = node;
  9313. assign.expression = super.parseExpression();
  9314. this.semicolon();
  9315. return this.finishNode(assign, "TSExportAssignment");
  9316. } else if (this.eatContextual(93)) {
  9317. const decl = node;
  9318. this.expectContextual(126);
  9319. decl.id = this.parseIdentifier();
  9320. this.semicolon();
  9321. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  9322. } else {
  9323. node.exportKind = "value";
  9324. if (this.isContextual(128)) {
  9325. const ch = this.lookaheadCharCode();
  9326. if (ch === 123 || ch === 42) {
  9327. this.next();
  9328. node.exportKind = "type";
  9329. }
  9330. }
  9331. return super.parseExport(node, decorators);
  9332. }
  9333. }
  9334. isAbstractClass() {
  9335. return this.isContextual(122) && this.lookahead().type === 80;
  9336. }
  9337. parseExportDefaultExpression() {
  9338. if (this.isAbstractClass()) {
  9339. const cls = this.startNode();
  9340. this.next();
  9341. cls.abstract = true;
  9342. return this.parseClass(cls, true, true);
  9343. }
  9344. if (this.match(127)) {
  9345. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9346. if (result) return result;
  9347. }
  9348. return super.parseExportDefaultExpression();
  9349. }
  9350. parseVarStatement(node, kind, allowMissingInitializer = false) {
  9351. const {
  9352. isAmbientContext
  9353. } = this.state;
  9354. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  9355. if (!isAmbientContext) return declaration;
  9356. for (const {
  9357. id,
  9358. init
  9359. } of declaration.declarations) {
  9360. if (!init) continue;
  9361. if (kind !== "const" || !!id.typeAnnotation) {
  9362. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, {
  9363. at: init
  9364. });
  9365. } else if (!isValidAmbientConstInitializer(init, this.hasPlugin("estree"))) {
  9366. this.raise(TSErrors.ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference, {
  9367. at: init
  9368. });
  9369. }
  9370. }
  9371. return declaration;
  9372. }
  9373. parseStatementContent(flags, decorators) {
  9374. if (this.match(75) && this.isLookaheadContextual("enum")) {
  9375. const node = this.startNode();
  9376. this.expect(75);
  9377. return this.tsParseEnumDeclaration(node, {
  9378. const: true
  9379. });
  9380. }
  9381. if (this.isContextual(124)) {
  9382. return this.tsParseEnumDeclaration(this.startNode());
  9383. }
  9384. if (this.isContextual(127)) {
  9385. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9386. if (result) return result;
  9387. }
  9388. return super.parseStatementContent(flags, decorators);
  9389. }
  9390. parseAccessModifier() {
  9391. return this.tsParseModifier(["public", "protected", "private"]);
  9392. }
  9393. tsHasSomeModifiers(member, modifiers) {
  9394. return modifiers.some(modifier => {
  9395. if (tsIsAccessModifier(modifier)) {
  9396. return member.accessibility === modifier;
  9397. }
  9398. return !!member[modifier];
  9399. });
  9400. }
  9401. tsIsStartOfStaticBlocks() {
  9402. return this.isContextual(104) && this.lookaheadCharCode() === 123;
  9403. }
  9404. parseClassMember(classBody, member, state) {
  9405. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  9406. this.tsParseModifiers({
  9407. allowedModifiers: modifiers,
  9408. disallowedModifiers: ["in", "out"],
  9409. stopOnStartOfClassStaticBlock: true,
  9410. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  9411. }, member);
  9412. const callParseClassMemberWithIsStatic = () => {
  9413. if (this.tsIsStartOfStaticBlocks()) {
  9414. this.next();
  9415. this.next();
  9416. if (this.tsHasSomeModifiers(member, modifiers)) {
  9417. this.raise(TSErrors.StaticBlockCannotHaveModifier, {
  9418. at: this.state.curPosition()
  9419. });
  9420. }
  9421. super.parseClassStaticBlock(classBody, member);
  9422. } else {
  9423. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  9424. }
  9425. };
  9426. if (member.declare) {
  9427. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  9428. } else {
  9429. callParseClassMemberWithIsStatic();
  9430. }
  9431. }
  9432. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  9433. const idx = this.tsTryParseIndexSignature(member);
  9434. if (idx) {
  9435. classBody.body.push(idx);
  9436. if (member.abstract) {
  9437. this.raise(TSErrors.IndexSignatureHasAbstract, {
  9438. at: member
  9439. });
  9440. }
  9441. if (member.accessibility) {
  9442. this.raise(TSErrors.IndexSignatureHasAccessibility, {
  9443. at: member,
  9444. modifier: member.accessibility
  9445. });
  9446. }
  9447. if (member.declare) {
  9448. this.raise(TSErrors.IndexSignatureHasDeclare, {
  9449. at: member
  9450. });
  9451. }
  9452. if (member.override) {
  9453. this.raise(TSErrors.IndexSignatureHasOverride, {
  9454. at: member
  9455. });
  9456. }
  9457. return;
  9458. }
  9459. if (!this.state.inAbstractClass && member.abstract) {
  9460. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, {
  9461. at: member
  9462. });
  9463. }
  9464. if (member.override) {
  9465. if (!state.hadSuperClass) {
  9466. this.raise(TSErrors.OverrideNotInSubClass, {
  9467. at: member
  9468. });
  9469. }
  9470. }
  9471. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  9472. }
  9473. parsePostMemberNameModifiers(methodOrProp) {
  9474. const optional = this.eat(17);
  9475. if (optional) methodOrProp.optional = true;
  9476. if (methodOrProp.readonly && this.match(10)) {
  9477. this.raise(TSErrors.ClassMethodHasReadonly, {
  9478. at: methodOrProp
  9479. });
  9480. }
  9481. if (methodOrProp.declare && this.match(10)) {
  9482. this.raise(TSErrors.ClassMethodHasDeclare, {
  9483. at: methodOrProp
  9484. });
  9485. }
  9486. }
  9487. parseExpressionStatement(node, expr, decorators) {
  9488. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr, decorators) : undefined;
  9489. return decl || super.parseExpressionStatement(node, expr, decorators);
  9490. }
  9491. shouldParseExportDeclaration() {
  9492. if (this.tsIsDeclarationStart()) return true;
  9493. return super.shouldParseExportDeclaration();
  9494. }
  9495. parseConditional(expr, startLoc, refExpressionErrors) {
  9496. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  9497. return super.parseConditional(expr, startLoc, refExpressionErrors);
  9498. }
  9499. const result = this.tryParse(() => super.parseConditional(expr, startLoc));
  9500. if (!result.node) {
  9501. if (result.error) {
  9502. super.setOptionalParametersError(refExpressionErrors, result.error);
  9503. }
  9504. return expr;
  9505. }
  9506. if (result.error) this.state = result.failState;
  9507. return result.node;
  9508. }
  9509. parseParenItem(node, startLoc) {
  9510. node = super.parseParenItem(node, startLoc);
  9511. if (this.eat(17)) {
  9512. node.optional = true;
  9513. this.resetEndLocation(node);
  9514. }
  9515. if (this.match(14)) {
  9516. const typeCastNode = this.startNodeAt(startLoc);
  9517. typeCastNode.expression = node;
  9518. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  9519. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  9520. }
  9521. return node;
  9522. }
  9523. parseExportDeclaration(node) {
  9524. if (!this.state.isAmbientContext && this.isContextual(123)) {
  9525. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  9526. }
  9527. const startLoc = this.state.startLoc;
  9528. const isDeclare = this.eatContextual(123);
  9529. if (isDeclare && (this.isContextual(123) || !this.shouldParseExportDeclaration())) {
  9530. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, {
  9531. at: this.state.startLoc
  9532. });
  9533. }
  9534. const isIdentifier = tokenIsIdentifier(this.state.type);
  9535. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  9536. if (!declaration) return null;
  9537. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  9538. node.exportKind = "type";
  9539. }
  9540. if (isDeclare) {
  9541. this.resetStartLocation(declaration, startLoc);
  9542. declaration.declare = true;
  9543. }
  9544. return declaration;
  9545. }
  9546. parseClassId(node, isStatement, optionalId, bindingType) {
  9547. if ((!isStatement || optionalId) && this.isContextual(111)) {
  9548. return;
  9549. }
  9550. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  9551. const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  9552. if (typeParameters) node.typeParameters = typeParameters;
  9553. }
  9554. parseClassPropertyAnnotation(node) {
  9555. if (!node.optional) {
  9556. if (this.eat(35)) {
  9557. node.definite = true;
  9558. } else if (this.eat(17)) {
  9559. node.optional = true;
  9560. }
  9561. }
  9562. const type = this.tsTryParseTypeAnnotation();
  9563. if (type) node.typeAnnotation = type;
  9564. }
  9565. parseClassProperty(node) {
  9566. this.parseClassPropertyAnnotation(node);
  9567. if (this.state.isAmbientContext && !(node.readonly && !node.typeAnnotation) && this.match(29)) {
  9568. this.raise(TSErrors.DeclareClassFieldHasInitializer, {
  9569. at: this.state.startLoc
  9570. });
  9571. }
  9572. if (node.abstract && this.match(29)) {
  9573. const {
  9574. key
  9575. } = node;
  9576. this.raise(TSErrors.AbstractPropertyHasInitializer, {
  9577. at: this.state.startLoc,
  9578. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9579. });
  9580. }
  9581. return super.parseClassProperty(node);
  9582. }
  9583. parseClassPrivateProperty(node) {
  9584. if (node.abstract) {
  9585. this.raise(TSErrors.PrivateElementHasAbstract, {
  9586. at: node
  9587. });
  9588. }
  9589. if (node.accessibility) {
  9590. this.raise(TSErrors.PrivateElementHasAccessibility, {
  9591. at: node,
  9592. modifier: node.accessibility
  9593. });
  9594. }
  9595. this.parseClassPropertyAnnotation(node);
  9596. return super.parseClassPrivateProperty(node);
  9597. }
  9598. parseClassAccessorProperty(node) {
  9599. this.parseClassPropertyAnnotation(node);
  9600. if (node.optional) {
  9601. this.raise(TSErrors.AccessorCannotBeOptional, {
  9602. at: node
  9603. });
  9604. }
  9605. return super.parseClassAccessorProperty(node);
  9606. }
  9607. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  9608. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9609. if (typeParameters && isConstructor) {
  9610. this.raise(TSErrors.ConstructorHasTypeParameters, {
  9611. at: typeParameters
  9612. });
  9613. }
  9614. const {
  9615. declare = false,
  9616. kind
  9617. } = method;
  9618. if (declare && (kind === "get" || kind === "set")) {
  9619. this.raise(TSErrors.DeclareAccessor, {
  9620. at: method,
  9621. kind
  9622. });
  9623. }
  9624. if (typeParameters) method.typeParameters = typeParameters;
  9625. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  9626. }
  9627. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  9628. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9629. if (typeParameters) method.typeParameters = typeParameters;
  9630. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  9631. }
  9632. declareClassPrivateMethodInScope(node, kind) {
  9633. if (node.type === "TSDeclareMethod") return;
  9634. if (node.type === "MethodDefinition" && !node.value.body) return;
  9635. super.declareClassPrivateMethodInScope(node, kind);
  9636. }
  9637. parseClassSuper(node) {
  9638. super.parseClassSuper(node);
  9639. if (node.superClass && (this.match(47) || this.match(51))) {
  9640. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  9641. }
  9642. if (this.eatContextual(111)) {
  9643. node.implements = this.tsParseHeritageClause("implements");
  9644. }
  9645. }
  9646. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9647. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9648. if (typeParameters) prop.typeParameters = typeParameters;
  9649. return super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9650. }
  9651. parseFunctionParams(node, isConstructor) {
  9652. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9653. if (typeParameters) node.typeParameters = typeParameters;
  9654. super.parseFunctionParams(node, isConstructor);
  9655. }
  9656. parseVarId(decl, kind) {
  9657. super.parseVarId(decl, kind);
  9658. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  9659. decl.definite = true;
  9660. }
  9661. const type = this.tsTryParseTypeAnnotation();
  9662. if (type) {
  9663. decl.id.typeAnnotation = type;
  9664. this.resetEndLocation(decl.id);
  9665. }
  9666. }
  9667. parseAsyncArrowFromCallExpression(node, call) {
  9668. if (this.match(14)) {
  9669. node.returnType = this.tsParseTypeAnnotation();
  9670. }
  9671. return super.parseAsyncArrowFromCallExpression(node, call);
  9672. }
  9673. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9674. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  9675. let state;
  9676. let jsx;
  9677. let typeCast;
  9678. if (this.hasPlugin("jsx") && (this.match(140) || this.match(47))) {
  9679. state = this.state.clone();
  9680. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9681. if (!jsx.error) return jsx.node;
  9682. const {
  9683. context
  9684. } = this.state;
  9685. const currentContext = context[context.length - 1];
  9686. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  9687. context.pop();
  9688. }
  9689. }
  9690. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  9691. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9692. }
  9693. if (!state || state === this.state) state = this.state.clone();
  9694. let typeParameters;
  9695. const arrow = this.tryParse(abort => {
  9696. var _expr$extra, _typeParameters;
  9697. typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  9698. const expr = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9699. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9700. abort();
  9701. }
  9702. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  9703. this.resetStartLocationFromNode(expr, typeParameters);
  9704. }
  9705. expr.typeParameters = typeParameters;
  9706. return expr;
  9707. }, state);
  9708. if (!arrow.error && !arrow.aborted) {
  9709. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9710. return arrow.node;
  9711. }
  9712. if (!jsx) {
  9713. assert(!this.hasPlugin("jsx"));
  9714. typeCast = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9715. if (!typeCast.error) return typeCast.node;
  9716. }
  9717. if ((_jsx2 = jsx) != null && _jsx2.node) {
  9718. this.state = jsx.failState;
  9719. return jsx.node;
  9720. }
  9721. if (arrow.node) {
  9722. this.state = arrow.failState;
  9723. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9724. return arrow.node;
  9725. }
  9726. if ((_typeCast = typeCast) != null && _typeCast.node) {
  9727. this.state = typeCast.failState;
  9728. return typeCast.node;
  9729. }
  9730. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  9731. if (arrow.thrown) throw arrow.error;
  9732. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  9733. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  9734. }
  9735. reportReservedArrowTypeParam(node) {
  9736. var _node$extra;
  9737. if (node.params.length === 1 && !node.params[0].constraint && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  9738. this.raise(TSErrors.ReservedArrowTypeParam, {
  9739. at: node
  9740. });
  9741. }
  9742. }
  9743. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9744. if (!this.hasPlugin("jsx") && this.match(47)) {
  9745. return this.tsParseTypeAssertion();
  9746. } else {
  9747. return super.parseMaybeUnary(refExpressionErrors, sawUnary);
  9748. }
  9749. }
  9750. parseArrow(node) {
  9751. if (this.match(14)) {
  9752. const result = this.tryParse(abort => {
  9753. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9754. if (this.canInsertSemicolon() || !this.match(19)) abort();
  9755. return returnType;
  9756. });
  9757. if (result.aborted) return;
  9758. if (!result.thrown) {
  9759. if (result.error) this.state = result.failState;
  9760. node.returnType = result.node;
  9761. }
  9762. }
  9763. return super.parseArrow(node);
  9764. }
  9765. parseAssignableListItemTypes(param, flags) {
  9766. if (!(flags & 2)) return param;
  9767. if (this.eat(17)) {
  9768. param.optional = true;
  9769. }
  9770. const type = this.tsTryParseTypeAnnotation();
  9771. if (type) param.typeAnnotation = type;
  9772. this.resetEndLocation(param);
  9773. return param;
  9774. }
  9775. isAssignable(node, isBinding) {
  9776. switch (node.type) {
  9777. case "TSTypeCastExpression":
  9778. return this.isAssignable(node.expression, isBinding);
  9779. case "TSParameterProperty":
  9780. return true;
  9781. default:
  9782. return super.isAssignable(node, isBinding);
  9783. }
  9784. }
  9785. toAssignable(node, isLHS = false) {
  9786. switch (node.type) {
  9787. case "ParenthesizedExpression":
  9788. this.toAssignableParenthesizedExpression(node, isLHS);
  9789. break;
  9790. case "TSAsExpression":
  9791. case "TSSatisfiesExpression":
  9792. case "TSNonNullExpression":
  9793. case "TSTypeAssertion":
  9794. if (isLHS) {
  9795. this.expressionScope.recordArrowParameterBindingError(TSErrors.UnexpectedTypeCastInParameter, {
  9796. at: node
  9797. });
  9798. } else {
  9799. this.raise(TSErrors.UnexpectedTypeCastInParameter, {
  9800. at: node
  9801. });
  9802. }
  9803. this.toAssignable(node.expression, isLHS);
  9804. break;
  9805. case "AssignmentExpression":
  9806. if (!isLHS && node.left.type === "TSTypeCastExpression") {
  9807. node.left = this.typeCastToParameter(node.left);
  9808. }
  9809. default:
  9810. super.toAssignable(node, isLHS);
  9811. }
  9812. }
  9813. toAssignableParenthesizedExpression(node, isLHS) {
  9814. switch (node.expression.type) {
  9815. case "TSAsExpression":
  9816. case "TSSatisfiesExpression":
  9817. case "TSNonNullExpression":
  9818. case "TSTypeAssertion":
  9819. case "ParenthesizedExpression":
  9820. this.toAssignable(node.expression, isLHS);
  9821. break;
  9822. default:
  9823. super.toAssignable(node, isLHS);
  9824. }
  9825. }
  9826. checkToRestConversion(node, allowPattern) {
  9827. switch (node.type) {
  9828. case "TSAsExpression":
  9829. case "TSSatisfiesExpression":
  9830. case "TSTypeAssertion":
  9831. case "TSNonNullExpression":
  9832. this.checkToRestConversion(node.expression, false);
  9833. break;
  9834. default:
  9835. super.checkToRestConversion(node, allowPattern);
  9836. }
  9837. }
  9838. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  9839. return getOwn({
  9840. TSTypeCastExpression: true,
  9841. TSParameterProperty: "parameter",
  9842. TSNonNullExpression: "expression",
  9843. TSAsExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9844. TSSatisfiesExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9845. TSTypeAssertion: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true]
  9846. }, type) || super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  9847. }
  9848. parseBindingAtom() {
  9849. switch (this.state.type) {
  9850. case 78:
  9851. return this.parseIdentifier(true);
  9852. default:
  9853. return super.parseBindingAtom();
  9854. }
  9855. }
  9856. parseMaybeDecoratorArguments(expr) {
  9857. if (this.match(47) || this.match(51)) {
  9858. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9859. if (this.match(10)) {
  9860. const call = super.parseMaybeDecoratorArguments(expr);
  9861. call.typeParameters = typeArguments;
  9862. return call;
  9863. }
  9864. this.unexpected(null, 10);
  9865. }
  9866. return super.parseMaybeDecoratorArguments(expr);
  9867. }
  9868. checkCommaAfterRest(close) {
  9869. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9870. this.next();
  9871. return false;
  9872. } else {
  9873. return super.checkCommaAfterRest(close);
  9874. }
  9875. }
  9876. isClassMethod() {
  9877. return this.match(47) || super.isClassMethod();
  9878. }
  9879. isClassProperty() {
  9880. return this.match(35) || this.match(14) || super.isClassProperty();
  9881. }
  9882. parseMaybeDefault(startLoc, left) {
  9883. const node = super.parseMaybeDefault(startLoc, left);
  9884. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9885. this.raise(TSErrors.TypeAnnotationAfterAssign, {
  9886. at: node.typeAnnotation
  9887. });
  9888. }
  9889. return node;
  9890. }
  9891. getTokenFromCode(code) {
  9892. if (this.state.inType) {
  9893. if (code === 62) {
  9894. this.finishOp(48, 1);
  9895. return;
  9896. }
  9897. if (code === 60) {
  9898. this.finishOp(47, 1);
  9899. return;
  9900. }
  9901. }
  9902. super.getTokenFromCode(code);
  9903. }
  9904. reScan_lt_gt() {
  9905. const {
  9906. type
  9907. } = this.state;
  9908. if (type === 47) {
  9909. this.state.pos -= 1;
  9910. this.readToken_lt();
  9911. } else if (type === 48) {
  9912. this.state.pos -= 1;
  9913. this.readToken_gt();
  9914. }
  9915. }
  9916. reScan_lt() {
  9917. const {
  9918. type
  9919. } = this.state;
  9920. if (type === 51) {
  9921. this.state.pos -= 2;
  9922. this.finishOp(47, 1);
  9923. return 47;
  9924. }
  9925. return type;
  9926. }
  9927. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9928. for (let i = 0; i < exprList.length; i++) {
  9929. const expr = exprList[i];
  9930. if ((expr == null ? void 0 : expr.type) === "TSTypeCastExpression") {
  9931. exprList[i] = this.typeCastToParameter(expr);
  9932. }
  9933. }
  9934. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  9935. }
  9936. typeCastToParameter(node) {
  9937. node.expression.typeAnnotation = node.typeAnnotation;
  9938. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  9939. return node.expression;
  9940. }
  9941. shouldParseArrow(params) {
  9942. if (this.match(14)) {
  9943. return params.every(expr => this.isAssignable(expr, true));
  9944. }
  9945. return super.shouldParseArrow(params);
  9946. }
  9947. shouldParseAsyncArrow() {
  9948. return this.match(14) || super.shouldParseAsyncArrow();
  9949. }
  9950. canHaveLeadingDecorator() {
  9951. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  9952. }
  9953. jsxParseOpeningElementAfterName(node) {
  9954. if (this.match(47) || this.match(51)) {
  9955. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  9956. if (typeArguments) node.typeParameters = typeArguments;
  9957. }
  9958. return super.jsxParseOpeningElementAfterName(node);
  9959. }
  9960. getGetterSetterExpectedParamCount(method) {
  9961. const baseCount = super.getGetterSetterExpectedParamCount(method);
  9962. const params = this.getObjectOrClassMethodParams(method);
  9963. const firstParam = params[0];
  9964. const hasContextParam = firstParam && this.isThisParam(firstParam);
  9965. return hasContextParam ? baseCount + 1 : baseCount;
  9966. }
  9967. parseCatchClauseParam() {
  9968. const param = super.parseCatchClauseParam();
  9969. const type = this.tsTryParseTypeAnnotation();
  9970. if (type) {
  9971. param.typeAnnotation = type;
  9972. this.resetEndLocation(param);
  9973. }
  9974. return param;
  9975. }
  9976. tsInAmbientContext(cb) {
  9977. const oldIsAmbientContext = this.state.isAmbientContext;
  9978. this.state.isAmbientContext = true;
  9979. try {
  9980. return cb();
  9981. } finally {
  9982. this.state.isAmbientContext = oldIsAmbientContext;
  9983. }
  9984. }
  9985. parseClass(node, isStatement, optionalId) {
  9986. const oldInAbstractClass = this.state.inAbstractClass;
  9987. this.state.inAbstractClass = !!node.abstract;
  9988. try {
  9989. return super.parseClass(node, isStatement, optionalId);
  9990. } finally {
  9991. this.state.inAbstractClass = oldInAbstractClass;
  9992. }
  9993. }
  9994. tsParseAbstractDeclaration(node, decorators) {
  9995. if (this.match(80)) {
  9996. node.abstract = true;
  9997. return this.maybeTakeDecorators(decorators, this.parseClass(node, true, false));
  9998. } else if (this.isContextual(127)) {
  9999. if (!this.hasFollowingLineBreak()) {
  10000. node.abstract = true;
  10001. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, {
  10002. at: node
  10003. });
  10004. return this.tsParseInterfaceDeclaration(node);
  10005. }
  10006. } else {
  10007. this.unexpected(null, 80);
  10008. }
  10009. }
  10010. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope) {
  10011. const method = super.parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  10012. if (method.abstract) {
  10013. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  10014. if (hasBody) {
  10015. const {
  10016. key
  10017. } = method;
  10018. this.raise(TSErrors.AbstractMethodHasImplementation, {
  10019. at: method,
  10020. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  10021. });
  10022. }
  10023. }
  10024. return method;
  10025. }
  10026. tsParseTypeParameterName() {
  10027. const typeName = this.parseIdentifier();
  10028. return typeName.name;
  10029. }
  10030. shouldParseAsAmbientContext() {
  10031. return !!this.getPluginOption("typescript", "dts");
  10032. }
  10033. parse() {
  10034. if (this.shouldParseAsAmbientContext()) {
  10035. this.state.isAmbientContext = true;
  10036. }
  10037. return super.parse();
  10038. }
  10039. getExpression() {
  10040. if (this.shouldParseAsAmbientContext()) {
  10041. this.state.isAmbientContext = true;
  10042. }
  10043. return super.getExpression();
  10044. }
  10045. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  10046. if (!isString && isMaybeTypeOnly) {
  10047. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  10048. return this.finishNode(node, "ExportSpecifier");
  10049. }
  10050. node.exportKind = "value";
  10051. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  10052. }
  10053. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  10054. if (!importedIsString && isMaybeTypeOnly) {
  10055. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  10056. return this.finishNode(specifier, "ImportSpecifier");
  10057. }
  10058. specifier.importKind = "value";
  10059. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, isInTypeOnlyImport ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
  10060. }
  10061. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  10062. const leftOfAsKey = isImport ? "imported" : "local";
  10063. const rightOfAsKey = isImport ? "local" : "exported";
  10064. let leftOfAs = node[leftOfAsKey];
  10065. let rightOfAs;
  10066. let hasTypeSpecifier = false;
  10067. let canParseAsKeyword = true;
  10068. const loc = leftOfAs.loc.start;
  10069. if (this.isContextual(93)) {
  10070. const firstAs = this.parseIdentifier();
  10071. if (this.isContextual(93)) {
  10072. const secondAs = this.parseIdentifier();
  10073. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10074. hasTypeSpecifier = true;
  10075. leftOfAs = firstAs;
  10076. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10077. canParseAsKeyword = false;
  10078. } else {
  10079. rightOfAs = secondAs;
  10080. canParseAsKeyword = false;
  10081. }
  10082. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10083. canParseAsKeyword = false;
  10084. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10085. } else {
  10086. hasTypeSpecifier = true;
  10087. leftOfAs = firstAs;
  10088. }
  10089. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10090. hasTypeSpecifier = true;
  10091. if (isImport) {
  10092. leftOfAs = this.parseIdentifier(true);
  10093. if (!this.isContextual(93)) {
  10094. this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
  10095. }
  10096. } else {
  10097. leftOfAs = this.parseModuleExportName();
  10098. }
  10099. }
  10100. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  10101. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, {
  10102. at: loc
  10103. });
  10104. }
  10105. node[leftOfAsKey] = leftOfAs;
  10106. node[rightOfAsKey] = rightOfAs;
  10107. const kindKey = isImport ? "importKind" : "exportKind";
  10108. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  10109. if (canParseAsKeyword && this.eatContextual(93)) {
  10110. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10111. }
  10112. if (!node[rightOfAsKey]) {
  10113. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  10114. }
  10115. if (isImport) {
  10116. this.checkIdentifier(node[rightOfAsKey], hasTypeSpecifier ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
  10117. }
  10118. }
  10119. };
  10120. function isPossiblyLiteralEnum(expression) {
  10121. if (expression.type !== "MemberExpression") return false;
  10122. const {
  10123. computed,
  10124. property
  10125. } = expression;
  10126. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  10127. return false;
  10128. }
  10129. return isUncomputedMemberExpressionChain(expression.object);
  10130. }
  10131. function isValidAmbientConstInitializer(expression, estree) {
  10132. var _expression$extra;
  10133. const {
  10134. type
  10135. } = expression;
  10136. if ((_expression$extra = expression.extra) != null && _expression$extra.parenthesized) {
  10137. return false;
  10138. }
  10139. if (estree) {
  10140. if (type === "Literal") {
  10141. const {
  10142. value
  10143. } = expression;
  10144. if (typeof value === "string" || typeof value === "boolean") {
  10145. return true;
  10146. }
  10147. }
  10148. } else {
  10149. if (type === "StringLiteral" || type === "BooleanLiteral") {
  10150. return true;
  10151. }
  10152. }
  10153. if (isNumber(expression, estree) || isNegativeNumber(expression, estree)) {
  10154. return true;
  10155. }
  10156. if (type === "TemplateLiteral" && expression.expressions.length === 0) {
  10157. return true;
  10158. }
  10159. if (isPossiblyLiteralEnum(expression)) {
  10160. return true;
  10161. }
  10162. return false;
  10163. }
  10164. function isNumber(expression, estree) {
  10165. if (estree) {
  10166. return expression.type === "Literal" && (typeof expression.value === "number" || "bigint" in expression);
  10167. } else {
  10168. return expression.type === "NumericLiteral" || expression.type === "BigIntLiteral";
  10169. }
  10170. }
  10171. function isNegativeNumber(expression, estree) {
  10172. if (expression.type === "UnaryExpression") {
  10173. const {
  10174. operator,
  10175. argument
  10176. } = expression;
  10177. if (operator === "-" && isNumber(argument, estree)) {
  10178. return true;
  10179. }
  10180. }
  10181. return false;
  10182. }
  10183. function isUncomputedMemberExpressionChain(expression) {
  10184. if (expression.type === "Identifier") return true;
  10185. if (expression.type !== "MemberExpression") return false;
  10186. if (expression.computed) return false;
  10187. return isUncomputedMemberExpressionChain(expression.object);
  10188. }
  10189. const PlaceholderErrors = ParseErrorEnum`placeholders`({
  10190. ClassNameIsRequired: "A class name is required.",
  10191. UnexpectedSpace: "Unexpected space in placeholder."
  10192. });
  10193. var placeholders = superClass => class PlaceholdersParserMixin extends superClass {
  10194. parsePlaceholder(expectedNode) {
  10195. if (this.match(142)) {
  10196. const node = this.startNode();
  10197. this.next();
  10198. this.assertNoSpace();
  10199. node.name = super.parseIdentifier(true);
  10200. this.assertNoSpace();
  10201. this.expect(142);
  10202. return this.finishPlaceholder(node, expectedNode);
  10203. }
  10204. }
  10205. finishPlaceholder(node, expectedNode) {
  10206. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  10207. node.expectedNode = expectedNode;
  10208. return isFinished ? node : this.finishNode(node, "Placeholder");
  10209. }
  10210. getTokenFromCode(code) {
  10211. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  10212. this.finishOp(142, 2);
  10213. } else {
  10214. super.getTokenFromCode(code);
  10215. }
  10216. }
  10217. parseExprAtom(refExpressionErrors) {
  10218. return this.parsePlaceholder("Expression") || super.parseExprAtom(refExpressionErrors);
  10219. }
  10220. parseIdentifier(liberal) {
  10221. return this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal);
  10222. }
  10223. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  10224. if (word !== undefined) {
  10225. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  10226. }
  10227. }
  10228. parseBindingAtom() {
  10229. return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
  10230. }
  10231. isValidLVal(type, isParenthesized, binding) {
  10232. return type === "Placeholder" || super.isValidLVal(type, isParenthesized, binding);
  10233. }
  10234. toAssignable(node, isLHS) {
  10235. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  10236. node.expectedNode = "Pattern";
  10237. } else {
  10238. super.toAssignable(node, isLHS);
  10239. }
  10240. }
  10241. chStartsBindingIdentifier(ch, pos) {
  10242. if (super.chStartsBindingIdentifier(ch, pos)) {
  10243. return true;
  10244. }
  10245. const nextToken = this.lookahead();
  10246. if (nextToken.type === 142) {
  10247. return true;
  10248. }
  10249. return false;
  10250. }
  10251. verifyBreakContinue(node, isBreak) {
  10252. if (node.label && node.label.type === "Placeholder") return;
  10253. super.verifyBreakContinue(node, isBreak);
  10254. }
  10255. parseExpressionStatement(node, expr) {
  10256. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  10257. return super.parseExpressionStatement(node, expr);
  10258. }
  10259. if (this.match(14)) {
  10260. const stmt = node;
  10261. stmt.label = this.finishPlaceholder(expr, "Identifier");
  10262. this.next();
  10263. stmt.body = super.parseStatementOrSloppyAnnexBFunctionDeclaration();
  10264. return this.finishNode(stmt, "LabeledStatement");
  10265. }
  10266. this.semicolon();
  10267. node.name = expr.name;
  10268. return this.finishPlaceholder(node, "Statement");
  10269. }
  10270. parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse) {
  10271. return this.parsePlaceholder("BlockStatement") || super.parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse);
  10272. }
  10273. parseFunctionId(requireId) {
  10274. return this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId);
  10275. }
  10276. parseClass(node, isStatement, optionalId) {
  10277. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  10278. this.next();
  10279. const oldStrict = this.state.strict;
  10280. const placeholder = this.parsePlaceholder("Identifier");
  10281. if (placeholder) {
  10282. if (this.match(81) || this.match(142) || this.match(5)) {
  10283. node.id = placeholder;
  10284. } else if (optionalId || !isStatement) {
  10285. node.id = null;
  10286. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  10287. return this.finishNode(node, type);
  10288. } else {
  10289. throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
  10290. at: this.state.startLoc
  10291. });
  10292. }
  10293. } else {
  10294. this.parseClassId(node, isStatement, optionalId);
  10295. }
  10296. super.parseClassSuper(node);
  10297. node.body = this.parsePlaceholder("ClassBody") || super.parseClassBody(!!node.superClass, oldStrict);
  10298. return this.finishNode(node, type);
  10299. }
  10300. parseExport(node, decorators) {
  10301. const placeholder = this.parsePlaceholder("Identifier");
  10302. if (!placeholder) return super.parseExport(node, decorators);
  10303. if (!this.isContextual(97) && !this.match(12)) {
  10304. node.specifiers = [];
  10305. node.source = null;
  10306. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  10307. return this.finishNode(node, "ExportNamedDeclaration");
  10308. }
  10309. this.expectPlugin("exportDefaultFrom");
  10310. const specifier = this.startNode();
  10311. specifier.exported = placeholder;
  10312. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  10313. return super.parseExport(node, decorators);
  10314. }
  10315. isExportDefaultSpecifier() {
  10316. if (this.match(65)) {
  10317. const next = this.nextTokenStart();
  10318. if (this.isUnparsedContextual(next, "from")) {
  10319. if (this.input.startsWith(tokenLabelName(142), this.nextTokenStartSince(next + 4))) {
  10320. return true;
  10321. }
  10322. }
  10323. }
  10324. return super.isExportDefaultSpecifier();
  10325. }
  10326. maybeParseExportDefaultSpecifier(node) {
  10327. if (node.specifiers && node.specifiers.length > 0) {
  10328. return true;
  10329. }
  10330. return super.maybeParseExportDefaultSpecifier(node);
  10331. }
  10332. checkExport(node) {
  10333. const {
  10334. specifiers
  10335. } = node;
  10336. if (specifiers != null && specifiers.length) {
  10337. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  10338. }
  10339. super.checkExport(node);
  10340. node.specifiers = specifiers;
  10341. }
  10342. parseImport(node) {
  10343. const placeholder = this.parsePlaceholder("Identifier");
  10344. if (!placeholder) return super.parseImport(node);
  10345. node.specifiers = [];
  10346. if (!this.isContextual(97) && !this.match(12)) {
  10347. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  10348. this.semicolon();
  10349. return this.finishNode(node, "ImportDeclaration");
  10350. }
  10351. const specifier = this.startNodeAtNode(placeholder);
  10352. specifier.local = placeholder;
  10353. node.specifiers.push(this.finishNode(specifier, "ImportDefaultSpecifier"));
  10354. if (this.eat(12)) {
  10355. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  10356. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  10357. }
  10358. this.expectContextual(97);
  10359. node.source = this.parseImportSource();
  10360. this.semicolon();
  10361. return this.finishNode(node, "ImportDeclaration");
  10362. }
  10363. parseImportSource() {
  10364. return this.parsePlaceholder("StringLiteral") || super.parseImportSource();
  10365. }
  10366. assertNoSpace() {
  10367. if (this.state.start > this.state.lastTokEndLoc.index) {
  10368. this.raise(PlaceholderErrors.UnexpectedSpace, {
  10369. at: this.state.lastTokEndLoc
  10370. });
  10371. }
  10372. }
  10373. };
  10374. var v8intrinsic = superClass => class V8IntrinsicMixin extends superClass {
  10375. parseV8Intrinsic() {
  10376. if (this.match(54)) {
  10377. const v8IntrinsicStartLoc = this.state.startLoc;
  10378. const node = this.startNode();
  10379. this.next();
  10380. if (tokenIsIdentifier(this.state.type)) {
  10381. const name = this.parseIdentifierName();
  10382. const identifier = this.createIdentifier(node, name);
  10383. identifier.type = "V8IntrinsicIdentifier";
  10384. if (this.match(10)) {
  10385. return identifier;
  10386. }
  10387. }
  10388. this.unexpected(v8IntrinsicStartLoc);
  10389. }
  10390. }
  10391. parseExprAtom(refExpressionErrors) {
  10392. return this.parseV8Intrinsic() || super.parseExprAtom(refExpressionErrors);
  10393. }
  10394. };
  10395. function hasPlugin(plugins, expectedConfig) {
  10396. const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
  10397. const expectedKeys = Object.keys(expectedOptions);
  10398. const expectedOptionsIsEmpty = expectedKeys.length === 0;
  10399. return plugins.some(p => {
  10400. if (typeof p === "string") {
  10401. return expectedOptionsIsEmpty && p === expectedName;
  10402. } else {
  10403. const [pluginName, pluginOptions] = p;
  10404. if (pluginName !== expectedName) {
  10405. return false;
  10406. }
  10407. for (const key of expectedKeys) {
  10408. if (pluginOptions[key] !== expectedOptions[key]) {
  10409. return false;
  10410. }
  10411. }
  10412. return true;
  10413. }
  10414. });
  10415. }
  10416. function getPluginOption(plugins, name, option) {
  10417. const plugin = plugins.find(plugin => {
  10418. if (Array.isArray(plugin)) {
  10419. return plugin[0] === name;
  10420. } else {
  10421. return plugin === name;
  10422. }
  10423. });
  10424. if (plugin && Array.isArray(plugin) && plugin.length > 1) {
  10425. return plugin[1][option];
  10426. }
  10427. return null;
  10428. }
  10429. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  10430. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  10431. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  10432. function validatePlugins(plugins) {
  10433. if (hasPlugin(plugins, "decorators")) {
  10434. if (hasPlugin(plugins, "decorators-legacy")) {
  10435. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  10436. }
  10437. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  10438. if (decoratorsBeforeExport != null && typeof decoratorsBeforeExport !== "boolean") {
  10439. throw new Error("'decoratorsBeforeExport' must be a boolean, if specified.");
  10440. }
  10441. const allowCallParenthesized = getPluginOption(plugins, "decorators", "allowCallParenthesized");
  10442. if (allowCallParenthesized != null && typeof allowCallParenthesized !== "boolean") {
  10443. throw new Error("'allowCallParenthesized' must be a boolean.");
  10444. }
  10445. }
  10446. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  10447. throw new Error("Cannot combine flow and typescript plugins.");
  10448. }
  10449. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  10450. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  10451. }
  10452. if (hasPlugin(plugins, "pipelineOperator")) {
  10453. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  10454. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  10455. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  10456. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  10457. }
  10458. const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
  10459. syntaxType: "hash"
  10460. }]);
  10461. if (proposal === "hack") {
  10462. if (hasPlugin(plugins, "placeholders")) {
  10463. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  10464. }
  10465. if (hasPlugin(plugins, "v8intrinsic")) {
  10466. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  10467. }
  10468. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  10469. if (!TOPIC_TOKENS.includes(topicToken)) {
  10470. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  10471. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  10472. }
  10473. if (topicToken === "#" && tupleSyntaxIsHash) {
  10474. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  10475. }
  10476. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  10477. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  10478. }
  10479. }
  10480. if (hasPlugin(plugins, "moduleAttributes")) {
  10481. {
  10482. if (hasPlugin(plugins, "importAssertions")) {
  10483. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  10484. }
  10485. const moduleAttributesVersionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  10486. if (moduleAttributesVersionPluginOption !== "may-2020") {
  10487. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  10488. }
  10489. }
  10490. }
  10491. if (hasPlugin(plugins, "recordAndTuple") && getPluginOption(plugins, "recordAndTuple", "syntaxType") != null && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  10492. throw new Error("The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  10493. }
  10494. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  10495. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  10496. error.missingPlugins = "doExpressions";
  10497. throw error;
  10498. }
  10499. }
  10500. const mixinPlugins = {
  10501. estree,
  10502. jsx,
  10503. flow,
  10504. typescript,
  10505. v8intrinsic,
  10506. placeholders
  10507. };
  10508. const mixinPluginNames = Object.keys(mixinPlugins);
  10509. class ExpressionParser extends LValParser {
  10510. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  10511. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10512. return;
  10513. }
  10514. const key = prop.key;
  10515. const name = key.type === "Identifier" ? key.name : key.value;
  10516. if (name === "__proto__") {
  10517. if (isRecord) {
  10518. this.raise(Errors.RecordNoProto, {
  10519. at: key
  10520. });
  10521. return;
  10522. }
  10523. if (protoRef.used) {
  10524. if (refExpressionErrors) {
  10525. if (refExpressionErrors.doubleProtoLoc === null) {
  10526. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10527. }
  10528. } else {
  10529. this.raise(Errors.DuplicateProto, {
  10530. at: key
  10531. });
  10532. }
  10533. }
  10534. protoRef.used = true;
  10535. }
  10536. }
  10537. shouldExitDescending(expr, potentialArrowAt) {
  10538. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  10539. }
  10540. getExpression() {
  10541. this.enterInitialScopes();
  10542. this.nextToken();
  10543. const expr = this.parseExpression();
  10544. if (!this.match(137)) {
  10545. this.unexpected();
  10546. }
  10547. this.finalizeRemainingComments();
  10548. expr.comments = this.state.comments;
  10549. expr.errors = this.state.errors;
  10550. if (this.options.tokens) {
  10551. expr.tokens = this.tokens;
  10552. }
  10553. return expr;
  10554. }
  10555. parseExpression(disallowIn, refExpressionErrors) {
  10556. if (disallowIn) {
  10557. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10558. }
  10559. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10560. }
  10561. parseExpressionBase(refExpressionErrors) {
  10562. const startLoc = this.state.startLoc;
  10563. const expr = this.parseMaybeAssign(refExpressionErrors);
  10564. if (this.match(12)) {
  10565. const node = this.startNodeAt(startLoc);
  10566. node.expressions = [expr];
  10567. while (this.eat(12)) {
  10568. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10569. }
  10570. this.toReferencedList(node.expressions);
  10571. return this.finishNode(node, "SequenceExpression");
  10572. }
  10573. return expr;
  10574. }
  10575. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10576. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10577. }
  10578. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10579. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10580. }
  10581. setOptionalParametersError(refExpressionErrors, resultError) {
  10582. var _resultError$loc;
  10583. refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
  10584. }
  10585. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10586. const startLoc = this.state.startLoc;
  10587. if (this.isContextual(106)) {
  10588. if (this.prodParam.hasYield) {
  10589. let left = this.parseYield();
  10590. if (afterLeftParse) {
  10591. left = afterLeftParse.call(this, left, startLoc);
  10592. }
  10593. return left;
  10594. }
  10595. }
  10596. let ownExpressionErrors;
  10597. if (refExpressionErrors) {
  10598. ownExpressionErrors = false;
  10599. } else {
  10600. refExpressionErrors = new ExpressionErrors();
  10601. ownExpressionErrors = true;
  10602. }
  10603. const {
  10604. type
  10605. } = this.state;
  10606. if (type === 10 || tokenIsIdentifier(type)) {
  10607. this.state.potentialArrowAt = this.state.start;
  10608. }
  10609. let left = this.parseMaybeConditional(refExpressionErrors);
  10610. if (afterLeftParse) {
  10611. left = afterLeftParse.call(this, left, startLoc);
  10612. }
  10613. if (tokenIsAssignment(this.state.type)) {
  10614. const node = this.startNodeAt(startLoc);
  10615. const operator = this.state.value;
  10616. node.operator = operator;
  10617. if (this.match(29)) {
  10618. this.toAssignable(left, true);
  10619. node.left = left;
  10620. const startIndex = startLoc.index;
  10621. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startIndex) {
  10622. refExpressionErrors.doubleProtoLoc = null;
  10623. }
  10624. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startIndex) {
  10625. refExpressionErrors.shorthandAssignLoc = null;
  10626. }
  10627. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startIndex) {
  10628. this.checkDestructuringPrivate(refExpressionErrors);
  10629. refExpressionErrors.privateKeyLoc = null;
  10630. }
  10631. } else {
  10632. node.left = left;
  10633. }
  10634. this.next();
  10635. node.right = this.parseMaybeAssign();
  10636. this.checkLVal(left, {
  10637. in: this.finishNode(node, "AssignmentExpression")
  10638. });
  10639. return node;
  10640. } else if (ownExpressionErrors) {
  10641. this.checkExpressionErrors(refExpressionErrors, true);
  10642. }
  10643. return left;
  10644. }
  10645. parseMaybeConditional(refExpressionErrors) {
  10646. const startLoc = this.state.startLoc;
  10647. const potentialArrowAt = this.state.potentialArrowAt;
  10648. const expr = this.parseExprOps(refExpressionErrors);
  10649. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10650. return expr;
  10651. }
  10652. return this.parseConditional(expr, startLoc, refExpressionErrors);
  10653. }
  10654. parseConditional(expr, startLoc, refExpressionErrors) {
  10655. if (this.eat(17)) {
  10656. const node = this.startNodeAt(startLoc);
  10657. node.test = expr;
  10658. node.consequent = this.parseMaybeAssignAllowIn();
  10659. this.expect(14);
  10660. node.alternate = this.parseMaybeAssign();
  10661. return this.finishNode(node, "ConditionalExpression");
  10662. }
  10663. return expr;
  10664. }
  10665. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10666. return this.match(136) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10667. }
  10668. parseExprOps(refExpressionErrors) {
  10669. const startLoc = this.state.startLoc;
  10670. const potentialArrowAt = this.state.potentialArrowAt;
  10671. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10672. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10673. return expr;
  10674. }
  10675. return this.parseExprOp(expr, startLoc, -1);
  10676. }
  10677. parseExprOp(left, leftStartLoc, minPrec) {
  10678. if (this.isPrivateName(left)) {
  10679. const value = this.getPrivateNameSV(left);
  10680. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10681. this.raise(Errors.PrivateInExpectedIn, {
  10682. at: left,
  10683. identifierName: value
  10684. });
  10685. }
  10686. this.classScope.usePrivateName(value, left.loc.start);
  10687. }
  10688. const op = this.state.type;
  10689. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10690. let prec = tokenOperatorPrecedence(op);
  10691. if (prec > minPrec) {
  10692. if (op === 39) {
  10693. this.expectPlugin("pipelineOperator");
  10694. if (this.state.inFSharpPipelineDirectBody) {
  10695. return left;
  10696. }
  10697. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10698. }
  10699. const node = this.startNodeAt(leftStartLoc);
  10700. node.left = left;
  10701. node.operator = this.state.value;
  10702. const logical = op === 41 || op === 42;
  10703. const coalesce = op === 40;
  10704. if (coalesce) {
  10705. prec = tokenOperatorPrecedence(42);
  10706. }
  10707. this.next();
  10708. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10709. proposal: "minimal"
  10710. }])) {
  10711. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10712. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, {
  10713. at: this.state.startLoc
  10714. });
  10715. }
  10716. }
  10717. node.right = this.parseExprOpRightExpr(op, prec);
  10718. const finishedNode = this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10719. const nextOp = this.state.type;
  10720. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10721. throw this.raise(Errors.MixingCoalesceWithLogical, {
  10722. at: this.state.startLoc
  10723. });
  10724. }
  10725. return this.parseExprOp(finishedNode, leftStartLoc, minPrec);
  10726. }
  10727. }
  10728. return left;
  10729. }
  10730. parseExprOpRightExpr(op, prec) {
  10731. const startLoc = this.state.startLoc;
  10732. switch (op) {
  10733. case 39:
  10734. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10735. case "hack":
  10736. return this.withTopicBindingContext(() => {
  10737. return this.parseHackPipeBody();
  10738. });
  10739. case "smart":
  10740. return this.withTopicBindingContext(() => {
  10741. if (this.prodParam.hasYield && this.isContextual(106)) {
  10742. throw this.raise(Errors.PipeBodyIsTighter, {
  10743. at: this.state.startLoc
  10744. });
  10745. }
  10746. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startLoc);
  10747. });
  10748. case "fsharp":
  10749. return this.withSoloAwaitPermittingContext(() => {
  10750. return this.parseFSharpPipelineBody(prec);
  10751. });
  10752. }
  10753. default:
  10754. return this.parseExprOpBaseRightExpr(op, prec);
  10755. }
  10756. }
  10757. parseExprOpBaseRightExpr(op, prec) {
  10758. const startLoc = this.state.startLoc;
  10759. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  10760. }
  10761. parseHackPipeBody() {
  10762. var _body$extra;
  10763. const {
  10764. startLoc
  10765. } = this.state;
  10766. const body = this.parseMaybeAssign();
  10767. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  10768. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  10769. this.raise(Errors.PipeUnparenthesizedBody, {
  10770. at: startLoc,
  10771. type: body.type
  10772. });
  10773. }
  10774. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10775. this.raise(Errors.PipeTopicUnused, {
  10776. at: startLoc
  10777. });
  10778. }
  10779. return body;
  10780. }
  10781. checkExponentialAfterUnary(node) {
  10782. if (this.match(57)) {
  10783. this.raise(Errors.UnexpectedTokenUnaryExponentiation, {
  10784. at: node.argument
  10785. });
  10786. }
  10787. }
  10788. parseMaybeUnary(refExpressionErrors, sawUnary) {
  10789. const startLoc = this.state.startLoc;
  10790. const isAwait = this.isContextual(96);
  10791. if (isAwait && this.isAwaitAllowed()) {
  10792. this.next();
  10793. const expr = this.parseAwait(startLoc);
  10794. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  10795. return expr;
  10796. }
  10797. const update = this.match(34);
  10798. const node = this.startNode();
  10799. if (tokenIsPrefix(this.state.type)) {
  10800. node.operator = this.state.value;
  10801. node.prefix = true;
  10802. if (this.match(72)) {
  10803. this.expectPlugin("throwExpressions");
  10804. }
  10805. const isDelete = this.match(89);
  10806. this.next();
  10807. node.argument = this.parseMaybeUnary(null, true);
  10808. this.checkExpressionErrors(refExpressionErrors, true);
  10809. if (this.state.strict && isDelete) {
  10810. const arg = node.argument;
  10811. if (arg.type === "Identifier") {
  10812. this.raise(Errors.StrictDelete, {
  10813. at: node
  10814. });
  10815. } else if (this.hasPropertyAsPrivateName(arg)) {
  10816. this.raise(Errors.DeletePrivateField, {
  10817. at: node
  10818. });
  10819. }
  10820. }
  10821. if (!update) {
  10822. if (!sawUnary) {
  10823. this.checkExponentialAfterUnary(node);
  10824. }
  10825. return this.finishNode(node, "UnaryExpression");
  10826. }
  10827. }
  10828. const expr = this.parseUpdate(node, update, refExpressionErrors);
  10829. if (isAwait) {
  10830. const {
  10831. type
  10832. } = this.state;
  10833. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10834. if (startsExpr && !this.isAmbiguousAwait()) {
  10835. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, {
  10836. at: startLoc
  10837. });
  10838. return this.parseAwait(startLoc);
  10839. }
  10840. }
  10841. return expr;
  10842. }
  10843. parseUpdate(node, update, refExpressionErrors) {
  10844. if (update) {
  10845. const updateExpressionNode = node;
  10846. this.checkLVal(updateExpressionNode.argument, {
  10847. in: this.finishNode(updateExpressionNode, "UpdateExpression")
  10848. });
  10849. return node;
  10850. }
  10851. const startLoc = this.state.startLoc;
  10852. let expr = this.parseExprSubscripts(refExpressionErrors);
  10853. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  10854. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  10855. const node = this.startNodeAt(startLoc);
  10856. node.operator = this.state.value;
  10857. node.prefix = false;
  10858. node.argument = expr;
  10859. this.next();
  10860. this.checkLVal(expr, {
  10861. in: expr = this.finishNode(node, "UpdateExpression")
  10862. });
  10863. }
  10864. return expr;
  10865. }
  10866. parseExprSubscripts(refExpressionErrors) {
  10867. const startLoc = this.state.startLoc;
  10868. const potentialArrowAt = this.state.potentialArrowAt;
  10869. const expr = this.parseExprAtom(refExpressionErrors);
  10870. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10871. return expr;
  10872. }
  10873. return this.parseSubscripts(expr, startLoc);
  10874. }
  10875. parseSubscripts(base, startLoc, noCalls) {
  10876. const state = {
  10877. optionalChainMember: false,
  10878. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  10879. stop: false
  10880. };
  10881. do {
  10882. base = this.parseSubscript(base, startLoc, noCalls, state);
  10883. state.maybeAsyncArrow = false;
  10884. } while (!state.stop);
  10885. return base;
  10886. }
  10887. parseSubscript(base, startLoc, noCalls, state) {
  10888. const {
  10889. type
  10890. } = this.state;
  10891. if (!noCalls && type === 15) {
  10892. return this.parseBind(base, startLoc, noCalls, state);
  10893. } else if (tokenIsTemplate(type)) {
  10894. return this.parseTaggedTemplateExpression(base, startLoc, state);
  10895. }
  10896. let optional = false;
  10897. if (type === 18) {
  10898. if (noCalls) {
  10899. this.raise(Errors.OptionalChainingNoNew, {
  10900. at: this.state.startLoc
  10901. });
  10902. if (this.lookaheadCharCode() === 40) {
  10903. state.stop = true;
  10904. return base;
  10905. }
  10906. }
  10907. state.optionalChainMember = optional = true;
  10908. this.next();
  10909. }
  10910. if (!noCalls && this.match(10)) {
  10911. return this.parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional);
  10912. } else {
  10913. const computed = this.eat(0);
  10914. if (computed || optional || this.eat(16)) {
  10915. return this.parseMember(base, startLoc, state, computed, optional);
  10916. } else {
  10917. state.stop = true;
  10918. return base;
  10919. }
  10920. }
  10921. }
  10922. parseMember(base, startLoc, state, computed, optional) {
  10923. const node = this.startNodeAt(startLoc);
  10924. node.object = base;
  10925. node.computed = computed;
  10926. if (computed) {
  10927. node.property = this.parseExpression();
  10928. this.expect(3);
  10929. } else if (this.match(136)) {
  10930. if (base.type === "Super") {
  10931. this.raise(Errors.SuperPrivateField, {
  10932. at: startLoc
  10933. });
  10934. }
  10935. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  10936. node.property = this.parsePrivateName();
  10937. } else {
  10938. node.property = this.parseIdentifier(true);
  10939. }
  10940. if (state.optionalChainMember) {
  10941. node.optional = optional;
  10942. return this.finishNode(node, "OptionalMemberExpression");
  10943. } else {
  10944. return this.finishNode(node, "MemberExpression");
  10945. }
  10946. }
  10947. parseBind(base, startLoc, noCalls, state) {
  10948. const node = this.startNodeAt(startLoc);
  10949. node.object = base;
  10950. this.next();
  10951. node.callee = this.parseNoCallExpr();
  10952. state.stop = true;
  10953. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startLoc, noCalls);
  10954. }
  10955. parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional) {
  10956. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10957. let refExpressionErrors = null;
  10958. this.state.maybeInArrowParameters = true;
  10959. this.next();
  10960. const node = this.startNodeAt(startLoc);
  10961. node.callee = base;
  10962. const {
  10963. maybeAsyncArrow,
  10964. optionalChainMember
  10965. } = state;
  10966. if (maybeAsyncArrow) {
  10967. this.expressionScope.enter(newAsyncArrowScope());
  10968. refExpressionErrors = new ExpressionErrors();
  10969. }
  10970. if (optionalChainMember) {
  10971. node.optional = optional;
  10972. }
  10973. if (optional) {
  10974. node.arguments = this.parseCallExpressionArguments(11);
  10975. } else {
  10976. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  10977. }
  10978. let finishedNode = this.finishCallExpression(node, optionalChainMember);
  10979. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  10980. state.stop = true;
  10981. this.checkDestructuringPrivate(refExpressionErrors);
  10982. this.expressionScope.validateAsPattern();
  10983. this.expressionScope.exit();
  10984. finishedNode = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startLoc), finishedNode);
  10985. } else {
  10986. if (maybeAsyncArrow) {
  10987. this.checkExpressionErrors(refExpressionErrors, true);
  10988. this.expressionScope.exit();
  10989. }
  10990. this.toReferencedArguments(finishedNode);
  10991. }
  10992. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10993. return finishedNode;
  10994. }
  10995. toReferencedArguments(node, isParenthesizedExpr) {
  10996. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  10997. }
  10998. parseTaggedTemplateExpression(base, startLoc, state) {
  10999. const node = this.startNodeAt(startLoc);
  11000. node.tag = base;
  11001. node.quasi = this.parseTemplate(true);
  11002. if (state.optionalChainMember) {
  11003. this.raise(Errors.OptionalChainingNoTemplate, {
  11004. at: startLoc
  11005. });
  11006. }
  11007. return this.finishNode(node, "TaggedTemplateExpression");
  11008. }
  11009. atPossibleAsyncArrow(base) {
  11010. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEndLoc.index === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  11011. }
  11012. finishCallExpression(node, optional) {
  11013. if (node.callee.type === "Import") {
  11014. if (node.arguments.length === 2) {
  11015. {
  11016. if (!this.hasPlugin("moduleAttributes")) {
  11017. this.expectPlugin("importAssertions");
  11018. }
  11019. }
  11020. }
  11021. if (node.arguments.length === 0 || node.arguments.length > 2) {
  11022. this.raise(Errors.ImportCallArity, {
  11023. at: node,
  11024. maxArgumentCount: this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
  11025. });
  11026. } else {
  11027. for (const arg of node.arguments) {
  11028. if (arg.type === "SpreadElement") {
  11029. this.raise(Errors.ImportCallSpreadArgument, {
  11030. at: arg
  11031. });
  11032. }
  11033. }
  11034. }
  11035. }
  11036. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  11037. }
  11038. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  11039. const elts = [];
  11040. let first = true;
  11041. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11042. this.state.inFSharpPipelineDirectBody = false;
  11043. while (!this.eat(close)) {
  11044. if (first) {
  11045. first = false;
  11046. } else {
  11047. this.expect(12);
  11048. if (this.match(close)) {
  11049. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  11050. this.raise(Errors.ImportCallArgumentTrailingComma, {
  11051. at: this.state.lastTokStartLoc
  11052. });
  11053. }
  11054. if (nodeForExtra) {
  11055. this.addTrailingCommaExtraToNode(nodeForExtra);
  11056. }
  11057. this.next();
  11058. break;
  11059. }
  11060. }
  11061. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  11062. }
  11063. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11064. return elts;
  11065. }
  11066. shouldParseAsyncArrow() {
  11067. return this.match(19) && !this.canInsertSemicolon();
  11068. }
  11069. parseAsyncArrowFromCallExpression(node, call) {
  11070. var _call$extra;
  11071. this.resetPreviousNodeTrailingComments(call);
  11072. this.expect(19);
  11073. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  11074. if (call.innerComments) {
  11075. setInnerComments(node, call.innerComments);
  11076. }
  11077. if (call.callee.trailingComments) {
  11078. setInnerComments(node, call.callee.trailingComments);
  11079. }
  11080. return node;
  11081. }
  11082. parseNoCallExpr() {
  11083. const startLoc = this.state.startLoc;
  11084. return this.parseSubscripts(this.parseExprAtom(), startLoc, true);
  11085. }
  11086. parseExprAtom(refExpressionErrors) {
  11087. let node;
  11088. let decorators = null;
  11089. const {
  11090. type
  11091. } = this.state;
  11092. switch (type) {
  11093. case 79:
  11094. return this.parseSuper();
  11095. case 83:
  11096. node = this.startNode();
  11097. this.next();
  11098. if (this.match(16)) {
  11099. return this.parseImportMetaProperty(node);
  11100. }
  11101. if (!this.match(10)) {
  11102. this.raise(Errors.UnsupportedImport, {
  11103. at: this.state.lastTokStartLoc
  11104. });
  11105. }
  11106. return this.finishNode(node, "Import");
  11107. case 78:
  11108. node = this.startNode();
  11109. this.next();
  11110. return this.finishNode(node, "ThisExpression");
  11111. case 90:
  11112. {
  11113. return this.parseDo(this.startNode(), false);
  11114. }
  11115. case 56:
  11116. case 31:
  11117. {
  11118. this.readRegexp();
  11119. return this.parseRegExpLiteral(this.state.value);
  11120. }
  11121. case 132:
  11122. return this.parseNumericLiteral(this.state.value);
  11123. case 133:
  11124. return this.parseBigIntLiteral(this.state.value);
  11125. case 134:
  11126. return this.parseDecimalLiteral(this.state.value);
  11127. case 131:
  11128. return this.parseStringLiteral(this.state.value);
  11129. case 84:
  11130. return this.parseNullLiteral();
  11131. case 85:
  11132. return this.parseBooleanLiteral(true);
  11133. case 86:
  11134. return this.parseBooleanLiteral(false);
  11135. case 10:
  11136. {
  11137. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11138. return this.parseParenAndDistinguishExpression(canBeArrow);
  11139. }
  11140. case 2:
  11141. case 1:
  11142. {
  11143. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  11144. }
  11145. case 0:
  11146. {
  11147. return this.parseArrayLike(3, true, false, refExpressionErrors);
  11148. }
  11149. case 6:
  11150. case 7:
  11151. {
  11152. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  11153. }
  11154. case 5:
  11155. {
  11156. return this.parseObjectLike(8, false, false, refExpressionErrors);
  11157. }
  11158. case 68:
  11159. return this.parseFunctionOrFunctionSent();
  11160. case 26:
  11161. decorators = this.parseDecorators();
  11162. case 80:
  11163. return this.parseClass(this.maybeTakeDecorators(decorators, this.startNode()), false);
  11164. case 77:
  11165. return this.parseNewOrNewTarget();
  11166. case 25:
  11167. case 24:
  11168. return this.parseTemplate(false);
  11169. case 15:
  11170. {
  11171. node = this.startNode();
  11172. this.next();
  11173. node.object = null;
  11174. const callee = node.callee = this.parseNoCallExpr();
  11175. if (callee.type === "MemberExpression") {
  11176. return this.finishNode(node, "BindExpression");
  11177. } else {
  11178. throw this.raise(Errors.UnsupportedBind, {
  11179. at: callee
  11180. });
  11181. }
  11182. }
  11183. case 136:
  11184. {
  11185. this.raise(Errors.PrivateInExpectedIn, {
  11186. at: this.state.startLoc,
  11187. identifierName: this.state.value
  11188. });
  11189. return this.parsePrivateName();
  11190. }
  11191. case 33:
  11192. {
  11193. return this.parseTopicReferenceThenEqualsSign(54, "%");
  11194. }
  11195. case 32:
  11196. {
  11197. return this.parseTopicReferenceThenEqualsSign(44, "^");
  11198. }
  11199. case 37:
  11200. case 38:
  11201. {
  11202. return this.parseTopicReference("hack");
  11203. }
  11204. case 44:
  11205. case 54:
  11206. case 27:
  11207. {
  11208. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11209. if (pipeProposal) {
  11210. return this.parseTopicReference(pipeProposal);
  11211. }
  11212. this.unexpected();
  11213. break;
  11214. }
  11215. case 47:
  11216. {
  11217. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  11218. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  11219. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  11220. } else {
  11221. this.unexpected();
  11222. }
  11223. break;
  11224. }
  11225. default:
  11226. if (tokenIsIdentifier(type)) {
  11227. if (this.isContextual(125) && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  11228. return this.parseModuleExpression();
  11229. }
  11230. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11231. const containsEsc = this.state.containsEsc;
  11232. const id = this.parseIdentifier();
  11233. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  11234. const {
  11235. type
  11236. } = this.state;
  11237. if (type === 68) {
  11238. this.resetPreviousNodeTrailingComments(id);
  11239. this.next();
  11240. return this.parseAsyncFunctionExpression(this.startNodeAtNode(id));
  11241. } else if (tokenIsIdentifier(type)) {
  11242. if (this.lookaheadCharCode() === 61) {
  11243. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  11244. } else {
  11245. return id;
  11246. }
  11247. } else if (type === 90) {
  11248. this.resetPreviousNodeTrailingComments(id);
  11249. return this.parseDo(this.startNodeAtNode(id), true);
  11250. }
  11251. }
  11252. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  11253. this.next();
  11254. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  11255. }
  11256. return id;
  11257. } else {
  11258. this.unexpected();
  11259. }
  11260. }
  11261. }
  11262. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  11263. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11264. if (pipeProposal) {
  11265. this.state.type = topicTokenType;
  11266. this.state.value = topicTokenValue;
  11267. this.state.pos--;
  11268. this.state.end--;
  11269. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  11270. return this.parseTopicReference(pipeProposal);
  11271. } else {
  11272. this.unexpected();
  11273. }
  11274. }
  11275. parseTopicReference(pipeProposal) {
  11276. const node = this.startNode();
  11277. const startLoc = this.state.startLoc;
  11278. const tokenType = this.state.type;
  11279. this.next();
  11280. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  11281. }
  11282. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  11283. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  11284. const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
  11285. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  11286. this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, {
  11287. at: startLoc
  11288. });
  11289. }
  11290. this.registerTopicReference();
  11291. return this.finishNode(node, nodeType);
  11292. } else {
  11293. throw this.raise(Errors.PipeTopicUnconfiguredToken, {
  11294. at: startLoc,
  11295. token: tokenLabelName(tokenType)
  11296. });
  11297. }
  11298. }
  11299. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  11300. switch (pipeProposal) {
  11301. case "hack":
  11302. {
  11303. return this.hasPlugin(["pipelineOperator", {
  11304. topicToken: tokenLabelName(tokenType)
  11305. }]);
  11306. }
  11307. case "smart":
  11308. return tokenType === 27;
  11309. default:
  11310. throw this.raise(Errors.PipeTopicRequiresHackPipes, {
  11311. at: startLoc
  11312. });
  11313. }
  11314. }
  11315. parseAsyncArrowUnaryFunction(node) {
  11316. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  11317. const params = [this.parseIdentifier()];
  11318. this.prodParam.exit();
  11319. if (this.hasPrecedingLineBreak()) {
  11320. this.raise(Errors.LineTerminatorBeforeArrow, {
  11321. at: this.state.curPosition()
  11322. });
  11323. }
  11324. this.expect(19);
  11325. return this.parseArrowExpression(node, params, true);
  11326. }
  11327. parseDo(node, isAsync) {
  11328. this.expectPlugin("doExpressions");
  11329. if (isAsync) {
  11330. this.expectPlugin("asyncDoExpressions");
  11331. }
  11332. node.async = isAsync;
  11333. this.next();
  11334. const oldLabels = this.state.labels;
  11335. this.state.labels = [];
  11336. if (isAsync) {
  11337. this.prodParam.enter(PARAM_AWAIT);
  11338. node.body = this.parseBlock();
  11339. this.prodParam.exit();
  11340. } else {
  11341. node.body = this.parseBlock();
  11342. }
  11343. this.state.labels = oldLabels;
  11344. return this.finishNode(node, "DoExpression");
  11345. }
  11346. parseSuper() {
  11347. const node = this.startNode();
  11348. this.next();
  11349. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  11350. this.raise(Errors.SuperNotAllowed, {
  11351. at: node
  11352. });
  11353. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  11354. this.raise(Errors.UnexpectedSuper, {
  11355. at: node
  11356. });
  11357. }
  11358. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  11359. this.raise(Errors.UnsupportedSuper, {
  11360. at: node
  11361. });
  11362. }
  11363. return this.finishNode(node, "Super");
  11364. }
  11365. parsePrivateName() {
  11366. const node = this.startNode();
  11367. const id = this.startNodeAt(createPositionWithColumnOffset(this.state.startLoc, 1));
  11368. const name = this.state.value;
  11369. this.next();
  11370. node.id = this.createIdentifier(id, name);
  11371. return this.finishNode(node, "PrivateName");
  11372. }
  11373. parseFunctionOrFunctionSent() {
  11374. const node = this.startNode();
  11375. this.next();
  11376. if (this.prodParam.hasYield && this.match(16)) {
  11377. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  11378. this.next();
  11379. if (this.match(102)) {
  11380. this.expectPlugin("functionSent");
  11381. } else if (!this.hasPlugin("functionSent")) {
  11382. this.unexpected();
  11383. }
  11384. return this.parseMetaProperty(node, meta, "sent");
  11385. }
  11386. return this.parseFunction(node);
  11387. }
  11388. parseMetaProperty(node, meta, propertyName) {
  11389. node.meta = meta;
  11390. const containsEsc = this.state.containsEsc;
  11391. node.property = this.parseIdentifier(true);
  11392. if (node.property.name !== propertyName || containsEsc) {
  11393. this.raise(Errors.UnsupportedMetaProperty, {
  11394. at: node.property,
  11395. target: meta.name,
  11396. onlyValidPropertyName: propertyName
  11397. });
  11398. }
  11399. return this.finishNode(node, "MetaProperty");
  11400. }
  11401. parseImportMetaProperty(node) {
  11402. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  11403. this.next();
  11404. if (this.isContextual(100)) {
  11405. if (!this.inModule) {
  11406. this.raise(Errors.ImportMetaOutsideModule, {
  11407. at: id
  11408. });
  11409. }
  11410. this.sawUnambiguousESM = true;
  11411. }
  11412. return this.parseMetaProperty(node, id, "meta");
  11413. }
  11414. parseLiteralAtNode(value, type, node) {
  11415. this.addExtra(node, "rawValue", value);
  11416. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  11417. node.value = value;
  11418. this.next();
  11419. return this.finishNode(node, type);
  11420. }
  11421. parseLiteral(value, type) {
  11422. const node = this.startNode();
  11423. return this.parseLiteralAtNode(value, type, node);
  11424. }
  11425. parseStringLiteral(value) {
  11426. return this.parseLiteral(value, "StringLiteral");
  11427. }
  11428. parseNumericLiteral(value) {
  11429. return this.parseLiteral(value, "NumericLiteral");
  11430. }
  11431. parseBigIntLiteral(value) {
  11432. return this.parseLiteral(value, "BigIntLiteral");
  11433. }
  11434. parseDecimalLiteral(value) {
  11435. return this.parseLiteral(value, "DecimalLiteral");
  11436. }
  11437. parseRegExpLiteral(value) {
  11438. const node = this.parseLiteral(value.value, "RegExpLiteral");
  11439. node.pattern = value.pattern;
  11440. node.flags = value.flags;
  11441. return node;
  11442. }
  11443. parseBooleanLiteral(value) {
  11444. const node = this.startNode();
  11445. node.value = value;
  11446. this.next();
  11447. return this.finishNode(node, "BooleanLiteral");
  11448. }
  11449. parseNullLiteral() {
  11450. const node = this.startNode();
  11451. this.next();
  11452. return this.finishNode(node, "NullLiteral");
  11453. }
  11454. parseParenAndDistinguishExpression(canBeArrow) {
  11455. const startLoc = this.state.startLoc;
  11456. let val;
  11457. this.next();
  11458. this.expressionScope.enter(newArrowHeadScope());
  11459. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11460. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11461. this.state.maybeInArrowParameters = true;
  11462. this.state.inFSharpPipelineDirectBody = false;
  11463. const innerStartLoc = this.state.startLoc;
  11464. const exprList = [];
  11465. const refExpressionErrors = new ExpressionErrors();
  11466. let first = true;
  11467. let spreadStartLoc;
  11468. let optionalCommaStartLoc;
  11469. while (!this.match(11)) {
  11470. if (first) {
  11471. first = false;
  11472. } else {
  11473. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11474. if (this.match(11)) {
  11475. optionalCommaStartLoc = this.state.startLoc;
  11476. break;
  11477. }
  11478. }
  11479. if (this.match(21)) {
  11480. const spreadNodeStartLoc = this.state.startLoc;
  11481. spreadStartLoc = this.state.startLoc;
  11482. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartLoc));
  11483. if (!this.checkCommaAfterRest(41)) {
  11484. break;
  11485. }
  11486. } else {
  11487. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  11488. }
  11489. }
  11490. const innerEndLoc = this.state.lastTokEndLoc;
  11491. this.expect(11);
  11492. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11493. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11494. let arrowNode = this.startNodeAt(startLoc);
  11495. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11496. this.checkDestructuringPrivate(refExpressionErrors);
  11497. this.expressionScope.validateAsPattern();
  11498. this.expressionScope.exit();
  11499. this.parseArrowExpression(arrowNode, exprList, false);
  11500. return arrowNode;
  11501. }
  11502. this.expressionScope.exit();
  11503. if (!exprList.length) {
  11504. this.unexpected(this.state.lastTokStartLoc);
  11505. }
  11506. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11507. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11508. this.checkExpressionErrors(refExpressionErrors, true);
  11509. this.toReferencedListDeep(exprList, true);
  11510. if (exprList.length > 1) {
  11511. val = this.startNodeAt(innerStartLoc);
  11512. val.expressions = exprList;
  11513. this.finishNode(val, "SequenceExpression");
  11514. this.resetEndLocation(val, innerEndLoc);
  11515. } else {
  11516. val = exprList[0];
  11517. }
  11518. return this.wrapParenthesis(startLoc, val);
  11519. }
  11520. wrapParenthesis(startLoc, expression) {
  11521. if (!this.options.createParenthesizedExpressions) {
  11522. this.addExtra(expression, "parenthesized", true);
  11523. this.addExtra(expression, "parenStart", startLoc.index);
  11524. this.takeSurroundingComments(expression, startLoc.index, this.state.lastTokEndLoc.index);
  11525. return expression;
  11526. }
  11527. const parenExpression = this.startNodeAt(startLoc);
  11528. parenExpression.expression = expression;
  11529. return this.finishNode(parenExpression, "ParenthesizedExpression");
  11530. }
  11531. shouldParseArrow(params) {
  11532. return !this.canInsertSemicolon();
  11533. }
  11534. parseArrow(node) {
  11535. if (this.eat(19)) {
  11536. return node;
  11537. }
  11538. }
  11539. parseParenItem(node, startLoc) {
  11540. return node;
  11541. }
  11542. parseNewOrNewTarget() {
  11543. const node = this.startNode();
  11544. this.next();
  11545. if (this.match(16)) {
  11546. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11547. this.next();
  11548. const metaProp = this.parseMetaProperty(node, meta, "target");
  11549. if (!this.scope.inNonArrowFunction && !this.scope.inClass && !this.options.allowNewTargetOutsideFunction) {
  11550. this.raise(Errors.UnexpectedNewTarget, {
  11551. at: metaProp
  11552. });
  11553. }
  11554. return metaProp;
  11555. }
  11556. return this.parseNew(node);
  11557. }
  11558. parseNew(node) {
  11559. this.parseNewCallee(node);
  11560. if (this.eat(10)) {
  11561. const args = this.parseExprList(11);
  11562. this.toReferencedList(args);
  11563. node.arguments = args;
  11564. } else {
  11565. node.arguments = [];
  11566. }
  11567. return this.finishNode(node, "NewExpression");
  11568. }
  11569. parseNewCallee(node) {
  11570. node.callee = this.parseNoCallExpr();
  11571. if (node.callee.type === "Import") {
  11572. this.raise(Errors.ImportCallNotNewExpression, {
  11573. at: node.callee
  11574. });
  11575. }
  11576. }
  11577. parseTemplateElement(isTagged) {
  11578. const {
  11579. start,
  11580. startLoc,
  11581. end,
  11582. value
  11583. } = this.state;
  11584. const elemStart = start + 1;
  11585. const elem = this.startNodeAt(createPositionWithColumnOffset(startLoc, 1));
  11586. if (value === null) {
  11587. if (!isTagged) {
  11588. this.raise(Errors.InvalidEscapeSequenceTemplate, {
  11589. at: createPositionWithColumnOffset(this.state.firstInvalidTemplateEscapePos, 1)
  11590. });
  11591. }
  11592. }
  11593. const isTail = this.match(24);
  11594. const endOffset = isTail ? -1 : -2;
  11595. const elemEnd = end + endOffset;
  11596. elem.value = {
  11597. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11598. cooked: value === null ? null : value.slice(1, endOffset)
  11599. };
  11600. elem.tail = isTail;
  11601. this.next();
  11602. const finishedNode = this.finishNode(elem, "TemplateElement");
  11603. this.resetEndLocation(finishedNode, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11604. return finishedNode;
  11605. }
  11606. parseTemplate(isTagged) {
  11607. const node = this.startNode();
  11608. node.expressions = [];
  11609. let curElt = this.parseTemplateElement(isTagged);
  11610. node.quasis = [curElt];
  11611. while (!curElt.tail) {
  11612. node.expressions.push(this.parseTemplateSubstitution());
  11613. this.readTemplateContinuation();
  11614. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  11615. }
  11616. return this.finishNode(node, "TemplateLiteral");
  11617. }
  11618. parseTemplateSubstitution() {
  11619. return this.parseExpression();
  11620. }
  11621. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11622. if (isRecord) {
  11623. this.expectPlugin("recordAndTuple");
  11624. }
  11625. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11626. this.state.inFSharpPipelineDirectBody = false;
  11627. const propHash = Object.create(null);
  11628. let first = true;
  11629. const node = this.startNode();
  11630. node.properties = [];
  11631. this.next();
  11632. while (!this.match(close)) {
  11633. if (first) {
  11634. first = false;
  11635. } else {
  11636. this.expect(12);
  11637. if (this.match(close)) {
  11638. this.addTrailingCommaExtraToNode(node);
  11639. break;
  11640. }
  11641. }
  11642. let prop;
  11643. if (isPattern) {
  11644. prop = this.parseBindingProperty();
  11645. } else {
  11646. prop = this.parsePropertyDefinition(refExpressionErrors);
  11647. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  11648. }
  11649. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11650. this.raise(Errors.InvalidRecordProperty, {
  11651. at: prop
  11652. });
  11653. }
  11654. if (prop.shorthand) {
  11655. this.addExtra(prop, "shorthand", true);
  11656. }
  11657. node.properties.push(prop);
  11658. }
  11659. this.next();
  11660. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11661. let type = "ObjectExpression";
  11662. if (isPattern) {
  11663. type = "ObjectPattern";
  11664. } else if (isRecord) {
  11665. type = "RecordExpression";
  11666. }
  11667. return this.finishNode(node, type);
  11668. }
  11669. addTrailingCommaExtraToNode(node) {
  11670. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  11671. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11672. }
  11673. maybeAsyncOrAccessorProp(prop) {
  11674. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11675. }
  11676. parsePropertyDefinition(refExpressionErrors) {
  11677. let decorators = [];
  11678. if (this.match(26)) {
  11679. if (this.hasPlugin("decorators")) {
  11680. this.raise(Errors.UnsupportedPropertyDecorator, {
  11681. at: this.state.startLoc
  11682. });
  11683. }
  11684. while (this.match(26)) {
  11685. decorators.push(this.parseDecorator());
  11686. }
  11687. }
  11688. const prop = this.startNode();
  11689. let isAsync = false;
  11690. let isAccessor = false;
  11691. let startLoc;
  11692. if (this.match(21)) {
  11693. if (decorators.length) this.unexpected();
  11694. return this.parseSpread();
  11695. }
  11696. if (decorators.length) {
  11697. prop.decorators = decorators;
  11698. decorators = [];
  11699. }
  11700. prop.method = false;
  11701. if (refExpressionErrors) {
  11702. startLoc = this.state.startLoc;
  11703. }
  11704. let isGenerator = this.eat(55);
  11705. this.parsePropertyNamePrefixOperator(prop);
  11706. const containsEsc = this.state.containsEsc;
  11707. const key = this.parsePropertyName(prop, refExpressionErrors);
  11708. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11709. const keyName = key.name;
  11710. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11711. isAsync = true;
  11712. this.resetPreviousNodeTrailingComments(key);
  11713. isGenerator = this.eat(55);
  11714. this.parsePropertyName(prop);
  11715. }
  11716. if (keyName === "get" || keyName === "set") {
  11717. isAccessor = true;
  11718. this.resetPreviousNodeTrailingComments(key);
  11719. prop.kind = keyName;
  11720. if (this.match(55)) {
  11721. isGenerator = true;
  11722. this.raise(Errors.AccessorIsGenerator, {
  11723. at: this.state.curPosition(),
  11724. kind: keyName
  11725. });
  11726. this.next();
  11727. }
  11728. this.parsePropertyName(prop);
  11729. }
  11730. }
  11731. return this.parseObjPropValue(prop, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11732. }
  11733. getGetterSetterExpectedParamCount(method) {
  11734. return method.kind === "get" ? 0 : 1;
  11735. }
  11736. getObjectOrClassMethodParams(method) {
  11737. return method.params;
  11738. }
  11739. checkGetterSetterParams(method) {
  11740. var _params;
  11741. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11742. const params = this.getObjectOrClassMethodParams(method);
  11743. if (params.length !== paramCount) {
  11744. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  11745. at: method
  11746. });
  11747. }
  11748. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11749. this.raise(Errors.BadSetterRestParameter, {
  11750. at: method
  11751. });
  11752. }
  11753. }
  11754. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11755. if (isAccessor) {
  11756. const finishedProp = this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11757. this.checkGetterSetterParams(finishedProp);
  11758. return finishedProp;
  11759. }
  11760. if (isAsync || isGenerator || this.match(10)) {
  11761. if (isPattern) this.unexpected();
  11762. prop.kind = "method";
  11763. prop.method = true;
  11764. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11765. }
  11766. }
  11767. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  11768. prop.shorthand = false;
  11769. if (this.eat(14)) {
  11770. prop.value = isPattern ? this.parseMaybeDefault(this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  11771. return this.finishNode(prop, "ObjectProperty");
  11772. }
  11773. if (!prop.computed && prop.key.type === "Identifier") {
  11774. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11775. if (isPattern) {
  11776. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11777. } else if (this.match(29)) {
  11778. const shorthandAssignLoc = this.state.startLoc;
  11779. if (refExpressionErrors != null) {
  11780. if (refExpressionErrors.shorthandAssignLoc === null) {
  11781. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11782. }
  11783. } else {
  11784. this.raise(Errors.InvalidCoverInitializedName, {
  11785. at: shorthandAssignLoc
  11786. });
  11787. }
  11788. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11789. } else {
  11790. prop.value = cloneIdentifier(prop.key);
  11791. }
  11792. prop.shorthand = true;
  11793. return this.finishNode(prop, "ObjectProperty");
  11794. }
  11795. }
  11796. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  11797. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  11798. if (!node) this.unexpected();
  11799. return node;
  11800. }
  11801. parsePropertyName(prop, refExpressionErrors) {
  11802. if (this.eat(0)) {
  11803. prop.computed = true;
  11804. prop.key = this.parseMaybeAssignAllowIn();
  11805. this.expect(3);
  11806. } else {
  11807. const {
  11808. type,
  11809. value
  11810. } = this.state;
  11811. let key;
  11812. if (tokenIsKeywordOrIdentifier(type)) {
  11813. key = this.parseIdentifier(true);
  11814. } else {
  11815. switch (type) {
  11816. case 132:
  11817. key = this.parseNumericLiteral(value);
  11818. break;
  11819. case 131:
  11820. key = this.parseStringLiteral(value);
  11821. break;
  11822. case 133:
  11823. key = this.parseBigIntLiteral(value);
  11824. break;
  11825. case 134:
  11826. key = this.parseDecimalLiteral(value);
  11827. break;
  11828. case 136:
  11829. {
  11830. const privateKeyLoc = this.state.startLoc;
  11831. if (refExpressionErrors != null) {
  11832. if (refExpressionErrors.privateKeyLoc === null) {
  11833. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  11834. }
  11835. } else {
  11836. this.raise(Errors.UnexpectedPrivateField, {
  11837. at: privateKeyLoc
  11838. });
  11839. }
  11840. key = this.parsePrivateName();
  11841. break;
  11842. }
  11843. default:
  11844. this.unexpected();
  11845. }
  11846. }
  11847. prop.key = key;
  11848. if (type !== 136) {
  11849. prop.computed = false;
  11850. }
  11851. }
  11852. return prop.key;
  11853. }
  11854. initFunction(node, isAsync) {
  11855. node.id = null;
  11856. node.generator = false;
  11857. node.async = isAsync;
  11858. }
  11859. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  11860. this.initFunction(node, isAsync);
  11861. node.generator = isGenerator;
  11862. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  11863. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11864. this.parseFunctionParams(node, isConstructor);
  11865. const finishedNode = this.parseFunctionBodyAndFinish(node, type, true);
  11866. this.prodParam.exit();
  11867. this.scope.exit();
  11868. return finishedNode;
  11869. }
  11870. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  11871. if (isTuple) {
  11872. this.expectPlugin("recordAndTuple");
  11873. }
  11874. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11875. this.state.inFSharpPipelineDirectBody = false;
  11876. const node = this.startNode();
  11877. this.next();
  11878. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  11879. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11880. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  11881. }
  11882. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  11883. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  11884. let flags = functionFlags(isAsync, false);
  11885. if (!this.match(5) && this.prodParam.hasIn) {
  11886. flags |= PARAM_IN;
  11887. }
  11888. this.prodParam.enter(flags);
  11889. this.initFunction(node, isAsync);
  11890. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11891. if (params) {
  11892. this.state.maybeInArrowParameters = true;
  11893. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  11894. }
  11895. this.state.maybeInArrowParameters = false;
  11896. this.parseFunctionBody(node, true);
  11897. this.prodParam.exit();
  11898. this.scope.exit();
  11899. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11900. return this.finishNode(node, "ArrowFunctionExpression");
  11901. }
  11902. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  11903. this.toAssignableList(params, trailingCommaLoc, false);
  11904. node.params = params;
  11905. }
  11906. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  11907. this.parseFunctionBody(node, false, isMethod);
  11908. return this.finishNode(node, type);
  11909. }
  11910. parseFunctionBody(node, allowExpression, isMethod = false) {
  11911. const isExpression = allowExpression && !this.match(5);
  11912. this.expressionScope.enter(newExpressionScope());
  11913. if (isExpression) {
  11914. node.body = this.parseMaybeAssign();
  11915. this.checkParams(node, false, allowExpression, false);
  11916. } else {
  11917. const oldStrict = this.state.strict;
  11918. const oldLabels = this.state.labels;
  11919. this.state.labels = [];
  11920. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  11921. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  11922. const nonSimple = !this.isSimpleParamList(node.params);
  11923. if (hasStrictModeDirective && nonSimple) {
  11924. this.raise(Errors.IllegalLanguageModeDirective, {
  11925. at: (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node
  11926. });
  11927. }
  11928. const strictModeChanged = !oldStrict && this.state.strict;
  11929. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  11930. if (this.state.strict && node.id) {
  11931. this.checkIdentifier(node.id, BIND_OUTSIDE, strictModeChanged);
  11932. }
  11933. });
  11934. this.prodParam.exit();
  11935. this.state.labels = oldLabels;
  11936. }
  11937. this.expressionScope.exit();
  11938. }
  11939. isSimpleParameter(node) {
  11940. return node.type === "Identifier";
  11941. }
  11942. isSimpleParamList(params) {
  11943. for (let i = 0, len = params.length; i < len; i++) {
  11944. if (!this.isSimpleParameter(params[i])) return false;
  11945. }
  11946. return true;
  11947. }
  11948. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  11949. const checkClashes = !allowDuplicates && new Set();
  11950. const formalParameters = {
  11951. type: "FormalParameters"
  11952. };
  11953. for (const param of node.params) {
  11954. this.checkLVal(param, {
  11955. in: formalParameters,
  11956. binding: BIND_VAR,
  11957. checkClashes,
  11958. strictModeChanged
  11959. });
  11960. }
  11961. }
  11962. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  11963. const elts = [];
  11964. let first = true;
  11965. while (!this.eat(close)) {
  11966. if (first) {
  11967. first = false;
  11968. } else {
  11969. this.expect(12);
  11970. if (this.match(close)) {
  11971. if (nodeForExtra) {
  11972. this.addTrailingCommaExtraToNode(nodeForExtra);
  11973. }
  11974. this.next();
  11975. break;
  11976. }
  11977. }
  11978. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  11979. }
  11980. return elts;
  11981. }
  11982. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  11983. let elt;
  11984. if (this.match(12)) {
  11985. if (!allowEmpty) {
  11986. this.raise(Errors.UnexpectedToken, {
  11987. at: this.state.curPosition(),
  11988. unexpected: ","
  11989. });
  11990. }
  11991. elt = null;
  11992. } else if (this.match(21)) {
  11993. const spreadNodeStartLoc = this.state.startLoc;
  11994. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartLoc);
  11995. } else if (this.match(17)) {
  11996. this.expectPlugin("partialApplication");
  11997. if (!allowPlaceholder) {
  11998. this.raise(Errors.UnexpectedArgumentPlaceholder, {
  11999. at: this.state.startLoc
  12000. });
  12001. }
  12002. const node = this.startNode();
  12003. this.next();
  12004. elt = this.finishNode(node, "ArgumentPlaceholder");
  12005. } else {
  12006. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  12007. }
  12008. return elt;
  12009. }
  12010. parseIdentifier(liberal) {
  12011. const node = this.startNode();
  12012. const name = this.parseIdentifierName(liberal);
  12013. return this.createIdentifier(node, name);
  12014. }
  12015. createIdentifier(node, name) {
  12016. node.name = name;
  12017. node.loc.identifierName = name;
  12018. return this.finishNode(node, "Identifier");
  12019. }
  12020. parseIdentifierName(liberal) {
  12021. let name;
  12022. const {
  12023. startLoc,
  12024. type
  12025. } = this.state;
  12026. if (tokenIsKeywordOrIdentifier(type)) {
  12027. name = this.state.value;
  12028. } else {
  12029. this.unexpected();
  12030. }
  12031. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  12032. if (liberal) {
  12033. if (tokenIsKeyword) {
  12034. this.replaceToken(130);
  12035. }
  12036. } else {
  12037. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  12038. }
  12039. this.next();
  12040. return name;
  12041. }
  12042. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  12043. if (word.length > 10) {
  12044. return;
  12045. }
  12046. if (!canBeReservedWord(word)) {
  12047. return;
  12048. }
  12049. if (checkKeywords && isKeyword(word)) {
  12050. this.raise(Errors.UnexpectedKeyword, {
  12051. at: startLoc,
  12052. keyword: word
  12053. });
  12054. return;
  12055. }
  12056. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  12057. if (reservedTest(word, this.inModule)) {
  12058. this.raise(Errors.UnexpectedReservedWord, {
  12059. at: startLoc,
  12060. reservedWord: word
  12061. });
  12062. return;
  12063. } else if (word === "yield") {
  12064. if (this.prodParam.hasYield) {
  12065. this.raise(Errors.YieldBindingIdentifier, {
  12066. at: startLoc
  12067. });
  12068. return;
  12069. }
  12070. } else if (word === "await") {
  12071. if (this.prodParam.hasAwait) {
  12072. this.raise(Errors.AwaitBindingIdentifier, {
  12073. at: startLoc
  12074. });
  12075. return;
  12076. }
  12077. if (this.scope.inStaticBlock) {
  12078. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, {
  12079. at: startLoc
  12080. });
  12081. return;
  12082. }
  12083. this.expressionScope.recordAsyncArrowParametersError({
  12084. at: startLoc
  12085. });
  12086. } else if (word === "arguments") {
  12087. if (this.scope.inClassAndNotInNonArrowFunction) {
  12088. this.raise(Errors.ArgumentsInClass, {
  12089. at: startLoc
  12090. });
  12091. return;
  12092. }
  12093. }
  12094. }
  12095. isAwaitAllowed() {
  12096. if (this.prodParam.hasAwait) return true;
  12097. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  12098. return true;
  12099. }
  12100. return false;
  12101. }
  12102. parseAwait(startLoc) {
  12103. const node = this.startNodeAt(startLoc);
  12104. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, {
  12105. at: node
  12106. });
  12107. if (this.eat(55)) {
  12108. this.raise(Errors.ObsoleteAwaitStar, {
  12109. at: node
  12110. });
  12111. }
  12112. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  12113. if (this.isAmbiguousAwait()) {
  12114. this.ambiguousScriptDifferentAst = true;
  12115. } else {
  12116. this.sawUnambiguousESM = true;
  12117. }
  12118. }
  12119. if (!this.state.soloAwait) {
  12120. node.argument = this.parseMaybeUnary(null, true);
  12121. }
  12122. return this.finishNode(node, "AwaitExpression");
  12123. }
  12124. isAmbiguousAwait() {
  12125. if (this.hasPrecedingLineBreak()) return true;
  12126. const {
  12127. type
  12128. } = this.state;
  12129. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 101 && !this.state.containsEsc || type === 135 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  12130. }
  12131. parseYield() {
  12132. const node = this.startNode();
  12133. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, {
  12134. at: node
  12135. });
  12136. this.next();
  12137. let delegating = false;
  12138. let argument = null;
  12139. if (!this.hasPrecedingLineBreak()) {
  12140. delegating = this.eat(55);
  12141. switch (this.state.type) {
  12142. case 13:
  12143. case 137:
  12144. case 8:
  12145. case 11:
  12146. case 3:
  12147. case 9:
  12148. case 14:
  12149. case 12:
  12150. if (!delegating) break;
  12151. default:
  12152. argument = this.parseMaybeAssign();
  12153. }
  12154. }
  12155. node.delegate = delegating;
  12156. node.argument = argument;
  12157. return this.finishNode(node, "YieldExpression");
  12158. }
  12159. checkPipelineAtInfixOperator(left, leftStartLoc) {
  12160. if (this.hasPlugin(["pipelineOperator", {
  12161. proposal: "smart"
  12162. }])) {
  12163. if (left.type === "SequenceExpression") {
  12164. this.raise(Errors.PipelineHeadSequenceExpression, {
  12165. at: leftStartLoc
  12166. });
  12167. }
  12168. }
  12169. }
  12170. parseSmartPipelineBodyInStyle(childExpr, startLoc) {
  12171. if (this.isSimpleReference(childExpr)) {
  12172. const bodyNode = this.startNodeAt(startLoc);
  12173. bodyNode.callee = childExpr;
  12174. return this.finishNode(bodyNode, "PipelineBareFunction");
  12175. } else {
  12176. const bodyNode = this.startNodeAt(startLoc);
  12177. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  12178. bodyNode.expression = childExpr;
  12179. return this.finishNode(bodyNode, "PipelineTopicExpression");
  12180. }
  12181. }
  12182. isSimpleReference(expression) {
  12183. switch (expression.type) {
  12184. case "MemberExpression":
  12185. return !expression.computed && this.isSimpleReference(expression.object);
  12186. case "Identifier":
  12187. return true;
  12188. default:
  12189. return false;
  12190. }
  12191. }
  12192. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  12193. if (this.match(19)) {
  12194. throw this.raise(Errors.PipelineBodyNoArrow, {
  12195. at: this.state.startLoc
  12196. });
  12197. }
  12198. if (!this.topicReferenceWasUsedInCurrentContext()) {
  12199. this.raise(Errors.PipelineTopicUnused, {
  12200. at: startLoc
  12201. });
  12202. }
  12203. }
  12204. withTopicBindingContext(callback) {
  12205. const outerContextTopicState = this.state.topicContext;
  12206. this.state.topicContext = {
  12207. maxNumOfResolvableTopics: 1,
  12208. maxTopicIndex: null
  12209. };
  12210. try {
  12211. return callback();
  12212. } finally {
  12213. this.state.topicContext = outerContextTopicState;
  12214. }
  12215. }
  12216. withSmartMixTopicForbiddingContext(callback) {
  12217. if (this.hasPlugin(["pipelineOperator", {
  12218. proposal: "smart"
  12219. }])) {
  12220. const outerContextTopicState = this.state.topicContext;
  12221. this.state.topicContext = {
  12222. maxNumOfResolvableTopics: 0,
  12223. maxTopicIndex: null
  12224. };
  12225. try {
  12226. return callback();
  12227. } finally {
  12228. this.state.topicContext = outerContextTopicState;
  12229. }
  12230. } else {
  12231. return callback();
  12232. }
  12233. }
  12234. withSoloAwaitPermittingContext(callback) {
  12235. const outerContextSoloAwaitState = this.state.soloAwait;
  12236. this.state.soloAwait = true;
  12237. try {
  12238. return callback();
  12239. } finally {
  12240. this.state.soloAwait = outerContextSoloAwaitState;
  12241. }
  12242. }
  12243. allowInAnd(callback) {
  12244. const flags = this.prodParam.currentFlags();
  12245. const prodParamToSet = PARAM_IN & ~flags;
  12246. if (prodParamToSet) {
  12247. this.prodParam.enter(flags | PARAM_IN);
  12248. try {
  12249. return callback();
  12250. } finally {
  12251. this.prodParam.exit();
  12252. }
  12253. }
  12254. return callback();
  12255. }
  12256. disallowInAnd(callback) {
  12257. const flags = this.prodParam.currentFlags();
  12258. const prodParamToClear = PARAM_IN & flags;
  12259. if (prodParamToClear) {
  12260. this.prodParam.enter(flags & ~PARAM_IN);
  12261. try {
  12262. return callback();
  12263. } finally {
  12264. this.prodParam.exit();
  12265. }
  12266. }
  12267. return callback();
  12268. }
  12269. registerTopicReference() {
  12270. this.state.topicContext.maxTopicIndex = 0;
  12271. }
  12272. topicReferenceIsAllowedInCurrentContext() {
  12273. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  12274. }
  12275. topicReferenceWasUsedInCurrentContext() {
  12276. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  12277. }
  12278. parseFSharpPipelineBody(prec) {
  12279. const startLoc = this.state.startLoc;
  12280. this.state.potentialArrowAt = this.state.start;
  12281. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  12282. this.state.inFSharpPipelineDirectBody = true;
  12283. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, prec);
  12284. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  12285. return ret;
  12286. }
  12287. parseModuleExpression() {
  12288. this.expectPlugin("moduleBlocks");
  12289. const node = this.startNode();
  12290. this.next();
  12291. if (!this.match(5)) {
  12292. this.unexpected(null, 5);
  12293. }
  12294. const program = this.startNodeAt(this.state.endLoc);
  12295. this.next();
  12296. const revertScopes = this.initializeScopes(true);
  12297. this.enterInitialScopes();
  12298. try {
  12299. node.body = this.parseProgram(program, 8, "module");
  12300. } finally {
  12301. revertScopes();
  12302. }
  12303. return this.finishNode(node, "ModuleExpression");
  12304. }
  12305. parsePropertyNamePrefixOperator(prop) {}
  12306. }
  12307. const loopLabel = {
  12308. kind: "loop"
  12309. },
  12310. switchLabel = {
  12311. kind: "switch"
  12312. };
  12313. const loneSurrogate = /[\uD800-\uDFFF]/u;
  12314. const keywordRelationalOperator = /in(?:stanceof)?/y;
  12315. function babel7CompatTokens(tokens, input) {
  12316. for (let i = 0; i < tokens.length; i++) {
  12317. const token = tokens[i];
  12318. const {
  12319. type
  12320. } = token;
  12321. if (typeof type === "number") {
  12322. {
  12323. if (type === 136) {
  12324. const {
  12325. loc,
  12326. start,
  12327. value,
  12328. end
  12329. } = token;
  12330. const hashEndPos = start + 1;
  12331. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12332. tokens.splice(i, 1, new Token({
  12333. type: getExportedToken(27),
  12334. value: "#",
  12335. start: start,
  12336. end: hashEndPos,
  12337. startLoc: loc.start,
  12338. endLoc: hashEndLoc
  12339. }), new Token({
  12340. type: getExportedToken(130),
  12341. value: value,
  12342. start: hashEndPos,
  12343. end: end,
  12344. startLoc: hashEndLoc,
  12345. endLoc: loc.end
  12346. }));
  12347. i++;
  12348. continue;
  12349. }
  12350. if (tokenIsTemplate(type)) {
  12351. const {
  12352. loc,
  12353. start,
  12354. value,
  12355. end
  12356. } = token;
  12357. const backquoteEnd = start + 1;
  12358. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12359. let startToken;
  12360. if (input.charCodeAt(start) === 96) {
  12361. startToken = new Token({
  12362. type: getExportedToken(22),
  12363. value: "`",
  12364. start: start,
  12365. end: backquoteEnd,
  12366. startLoc: loc.start,
  12367. endLoc: backquoteEndLoc
  12368. });
  12369. } else {
  12370. startToken = new Token({
  12371. type: getExportedToken(8),
  12372. value: "}",
  12373. start: start,
  12374. end: backquoteEnd,
  12375. startLoc: loc.start,
  12376. endLoc: backquoteEndLoc
  12377. });
  12378. }
  12379. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  12380. if (type === 24) {
  12381. templateElementEnd = end - 1;
  12382. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  12383. templateValue = value === null ? null : value.slice(1, -1);
  12384. endToken = new Token({
  12385. type: getExportedToken(22),
  12386. value: "`",
  12387. start: templateElementEnd,
  12388. end: end,
  12389. startLoc: templateElementEndLoc,
  12390. endLoc: loc.end
  12391. });
  12392. } else {
  12393. templateElementEnd = end - 2;
  12394. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  12395. templateValue = value === null ? null : value.slice(1, -2);
  12396. endToken = new Token({
  12397. type: getExportedToken(23),
  12398. value: "${",
  12399. start: templateElementEnd,
  12400. end: end,
  12401. startLoc: templateElementEndLoc,
  12402. endLoc: loc.end
  12403. });
  12404. }
  12405. tokens.splice(i, 1, startToken, new Token({
  12406. type: getExportedToken(20),
  12407. value: templateValue,
  12408. start: backquoteEnd,
  12409. end: templateElementEnd,
  12410. startLoc: backquoteEndLoc,
  12411. endLoc: templateElementEndLoc
  12412. }), endToken);
  12413. i += 2;
  12414. continue;
  12415. }
  12416. }
  12417. token.type = getExportedToken(type);
  12418. }
  12419. }
  12420. return tokens;
  12421. }
  12422. class StatementParser extends ExpressionParser {
  12423. parseTopLevel(file, program) {
  12424. file.program = this.parseProgram(program);
  12425. file.comments = this.state.comments;
  12426. if (this.options.tokens) {
  12427. file.tokens = babel7CompatTokens(this.tokens, this.input);
  12428. }
  12429. return this.finishNode(file, "File");
  12430. }
  12431. parseProgram(program, end = 137, sourceType = this.options.sourceType) {
  12432. program.sourceType = sourceType;
  12433. program.interpreter = this.parseInterpreterDirective();
  12434. this.parseBlockBody(program, true, true, end);
  12435. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  12436. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12437. this.raise(Errors.ModuleExportUndefined, {
  12438. at,
  12439. localName
  12440. });
  12441. }
  12442. }
  12443. let finishedProgram;
  12444. if (end === 137) {
  12445. finishedProgram = this.finishNode(program, "Program");
  12446. } else {
  12447. finishedProgram = this.finishNodeAt(program, "Program", createPositionWithColumnOffset(this.state.startLoc, -1));
  12448. }
  12449. return finishedProgram;
  12450. }
  12451. stmtToDirective(stmt) {
  12452. const directive = stmt;
  12453. directive.type = "Directive";
  12454. directive.value = directive.expression;
  12455. delete directive.expression;
  12456. const directiveLiteral = directive.value;
  12457. const expressionValue = directiveLiteral.value;
  12458. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  12459. const val = directiveLiteral.value = raw.slice(1, -1);
  12460. this.addExtra(directiveLiteral, "raw", raw);
  12461. this.addExtra(directiveLiteral, "rawValue", val);
  12462. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12463. directiveLiteral.type = "DirectiveLiteral";
  12464. return directive;
  12465. }
  12466. parseInterpreterDirective() {
  12467. if (!this.match(28)) {
  12468. return null;
  12469. }
  12470. const node = this.startNode();
  12471. node.value = this.state.value;
  12472. this.next();
  12473. return this.finishNode(node, "InterpreterDirective");
  12474. }
  12475. isLet() {
  12476. if (!this.isContextual(99)) {
  12477. return false;
  12478. }
  12479. return this.hasFollowingBindingAtom();
  12480. }
  12481. chStartsBindingIdentifier(ch, pos) {
  12482. if (isIdentifierStart(ch)) {
  12483. keywordRelationalOperator.lastIndex = pos;
  12484. if (keywordRelationalOperator.test(this.input)) {
  12485. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12486. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12487. return false;
  12488. }
  12489. }
  12490. return true;
  12491. } else if (ch === 92) {
  12492. return true;
  12493. } else {
  12494. return false;
  12495. }
  12496. }
  12497. chStartsBindingPattern(ch) {
  12498. return ch === 91 || ch === 123;
  12499. }
  12500. hasFollowingBindingAtom() {
  12501. const next = this.nextTokenStart();
  12502. const nextCh = this.codePointAtPos(next);
  12503. return this.chStartsBindingPattern(nextCh) || this.chStartsBindingIdentifier(nextCh, next);
  12504. }
  12505. hasFollowingBindingIdentifier() {
  12506. const next = this.nextTokenStart();
  12507. const nextCh = this.codePointAtPos(next);
  12508. return this.chStartsBindingIdentifier(nextCh, next);
  12509. }
  12510. startsUsingForOf() {
  12511. const lookahead = this.lookahead();
  12512. if (lookahead.type === 101 && !lookahead.containsEsc) {
  12513. return false;
  12514. } else {
  12515. this.expectPlugin("explicitResourceManagement");
  12516. return true;
  12517. }
  12518. }
  12519. parseModuleItem() {
  12520. return this.parseStatementLike(1 | 2 | 4 | 8);
  12521. }
  12522. parseStatementListItem() {
  12523. return this.parseStatementLike(2 | 4 | (!this.options.annexB || this.state.strict ? 0 : 8));
  12524. }
  12525. parseStatementOrSloppyAnnexBFunctionDeclaration(allowLabeledFunction = false) {
  12526. let flags = 0;
  12527. if (this.options.annexB && !this.state.strict) {
  12528. flags |= 4;
  12529. if (allowLabeledFunction) {
  12530. flags |= 8;
  12531. }
  12532. }
  12533. return this.parseStatementLike(flags);
  12534. }
  12535. parseStatement() {
  12536. return this.parseStatementLike(0);
  12537. }
  12538. parseStatementLike(flags) {
  12539. let decorators = null;
  12540. if (this.match(26)) {
  12541. decorators = this.parseDecorators(true);
  12542. }
  12543. return this.parseStatementContent(flags, decorators);
  12544. }
  12545. parseStatementContent(flags, decorators) {
  12546. const starttype = this.state.type;
  12547. const node = this.startNode();
  12548. const allowDeclaration = !!(flags & 2);
  12549. const allowFunctionDeclaration = !!(flags & 4);
  12550. const topLevel = flags & 1;
  12551. switch (starttype) {
  12552. case 60:
  12553. return this.parseBreakContinueStatement(node, true);
  12554. case 63:
  12555. return this.parseBreakContinueStatement(node, false);
  12556. case 64:
  12557. return this.parseDebuggerStatement(node);
  12558. case 90:
  12559. return this.parseDoWhileStatement(node);
  12560. case 91:
  12561. return this.parseForStatement(node);
  12562. case 68:
  12563. if (this.lookaheadCharCode() === 46) break;
  12564. if (!allowFunctionDeclaration) {
  12565. this.raise(this.state.strict ? Errors.StrictFunction : this.options.annexB ? Errors.SloppyFunctionAnnexB : Errors.SloppyFunction, {
  12566. at: this.state.startLoc
  12567. });
  12568. }
  12569. return this.parseFunctionStatement(node, false, !allowDeclaration && allowFunctionDeclaration);
  12570. case 80:
  12571. if (!allowDeclaration) this.unexpected();
  12572. return this.parseClass(this.maybeTakeDecorators(decorators, node), true);
  12573. case 69:
  12574. return this.parseIfStatement(node);
  12575. case 70:
  12576. return this.parseReturnStatement(node);
  12577. case 71:
  12578. return this.parseSwitchStatement(node);
  12579. case 72:
  12580. return this.parseThrowStatement(node);
  12581. case 73:
  12582. return this.parseTryStatement(node);
  12583. case 105:
  12584. if (this.hasFollowingLineBreak() || this.state.containsEsc || !this.hasFollowingBindingIdentifier()) {
  12585. break;
  12586. }
  12587. this.expectPlugin("explicitResourceManagement");
  12588. if (!this.scope.inModule && this.scope.inTopLevel) {
  12589. this.raise(Errors.UnexpectedUsingDeclaration, {
  12590. at: this.state.startLoc
  12591. });
  12592. } else if (!allowDeclaration) {
  12593. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12594. at: this.state.startLoc
  12595. });
  12596. }
  12597. return this.parseVarStatement(node, "using");
  12598. case 99:
  12599. {
  12600. if (this.state.containsEsc) {
  12601. break;
  12602. }
  12603. const next = this.nextTokenStart();
  12604. const nextCh = this.codePointAtPos(next);
  12605. if (nextCh !== 91) {
  12606. if (!allowDeclaration && this.hasFollowingLineBreak()) break;
  12607. if (!this.chStartsBindingIdentifier(nextCh, next) && nextCh !== 123) {
  12608. break;
  12609. }
  12610. }
  12611. }
  12612. case 75:
  12613. {
  12614. if (!allowDeclaration) {
  12615. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12616. at: this.state.startLoc
  12617. });
  12618. }
  12619. }
  12620. case 74:
  12621. {
  12622. const kind = this.state.value;
  12623. return this.parseVarStatement(node, kind);
  12624. }
  12625. case 92:
  12626. return this.parseWhileStatement(node);
  12627. case 76:
  12628. return this.parseWithStatement(node);
  12629. case 5:
  12630. return this.parseBlock();
  12631. case 13:
  12632. return this.parseEmptyStatement(node);
  12633. case 83:
  12634. {
  12635. const nextTokenCharCode = this.lookaheadCharCode();
  12636. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12637. break;
  12638. }
  12639. }
  12640. case 82:
  12641. {
  12642. if (!this.options.allowImportExportEverywhere && !topLevel) {
  12643. this.raise(Errors.UnexpectedImportExport, {
  12644. at: this.state.startLoc
  12645. });
  12646. }
  12647. this.next();
  12648. let result;
  12649. if (starttype === 83) {
  12650. result = this.parseImport(node);
  12651. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  12652. this.sawUnambiguousESM = true;
  12653. }
  12654. } else {
  12655. result = this.parseExport(node, decorators);
  12656. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  12657. this.sawUnambiguousESM = true;
  12658. }
  12659. }
  12660. this.assertModuleNodeAllowed(result);
  12661. return result;
  12662. }
  12663. default:
  12664. {
  12665. if (this.isAsyncFunction()) {
  12666. if (!allowDeclaration) {
  12667. this.raise(Errors.AsyncFunctionInSingleStatementContext, {
  12668. at: this.state.startLoc
  12669. });
  12670. }
  12671. this.next();
  12672. return this.parseFunctionStatement(node, true, !allowDeclaration && allowFunctionDeclaration);
  12673. }
  12674. }
  12675. }
  12676. const maybeName = this.state.value;
  12677. const expr = this.parseExpression();
  12678. if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
  12679. return this.parseLabeledStatement(node, maybeName, expr, flags);
  12680. } else {
  12681. return this.parseExpressionStatement(node, expr, decorators);
  12682. }
  12683. }
  12684. assertModuleNodeAllowed(node) {
  12685. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  12686. this.raise(Errors.ImportOutsideModule, {
  12687. at: node
  12688. });
  12689. }
  12690. }
  12691. decoratorsEnabledBeforeExport() {
  12692. if (this.hasPlugin("decorators-legacy")) return true;
  12693. return this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") !== false;
  12694. }
  12695. maybeTakeDecorators(maybeDecorators, classNode, exportNode) {
  12696. if (maybeDecorators) {
  12697. if (classNode.decorators && classNode.decorators.length > 0) {
  12698. if (typeof this.getPluginOption("decorators", "decoratorsBeforeExport") !== "boolean") {
  12699. this.raise(Errors.DecoratorsBeforeAfterExport, {
  12700. at: classNode.decorators[0]
  12701. });
  12702. }
  12703. classNode.decorators.unshift(...maybeDecorators);
  12704. } else {
  12705. classNode.decorators = maybeDecorators;
  12706. }
  12707. this.resetStartLocationFromNode(classNode, maybeDecorators[0]);
  12708. if (exportNode) this.resetStartLocationFromNode(exportNode, classNode);
  12709. }
  12710. return classNode;
  12711. }
  12712. canHaveLeadingDecorator() {
  12713. return this.match(80);
  12714. }
  12715. parseDecorators(allowExport) {
  12716. const decorators = [];
  12717. do {
  12718. decorators.push(this.parseDecorator());
  12719. } while (this.match(26));
  12720. if (this.match(82)) {
  12721. if (!allowExport) {
  12722. this.unexpected();
  12723. }
  12724. if (!this.decoratorsEnabledBeforeExport()) {
  12725. this.raise(Errors.DecoratorExportClass, {
  12726. at: this.state.startLoc
  12727. });
  12728. }
  12729. } else if (!this.canHaveLeadingDecorator()) {
  12730. throw this.raise(Errors.UnexpectedLeadingDecorator, {
  12731. at: this.state.startLoc
  12732. });
  12733. }
  12734. return decorators;
  12735. }
  12736. parseDecorator() {
  12737. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12738. const node = this.startNode();
  12739. this.next();
  12740. if (this.hasPlugin("decorators")) {
  12741. const startLoc = this.state.startLoc;
  12742. let expr;
  12743. if (this.match(10)) {
  12744. const startLoc = this.state.startLoc;
  12745. this.next();
  12746. expr = this.parseExpression();
  12747. this.expect(11);
  12748. expr = this.wrapParenthesis(startLoc, expr);
  12749. const paramsStartLoc = this.state.startLoc;
  12750. node.expression = this.parseMaybeDecoratorArguments(expr);
  12751. if (this.getPluginOption("decorators", "allowCallParenthesized") === false && node.expression !== expr) {
  12752. this.raise(Errors.DecoratorArgumentsOutsideParentheses, {
  12753. at: paramsStartLoc
  12754. });
  12755. }
  12756. } else {
  12757. expr = this.parseIdentifier(false);
  12758. while (this.eat(16)) {
  12759. const node = this.startNodeAt(startLoc);
  12760. node.object = expr;
  12761. if (this.match(136)) {
  12762. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  12763. node.property = this.parsePrivateName();
  12764. } else {
  12765. node.property = this.parseIdentifier(true);
  12766. }
  12767. node.computed = false;
  12768. expr = this.finishNode(node, "MemberExpression");
  12769. }
  12770. node.expression = this.parseMaybeDecoratorArguments(expr);
  12771. }
  12772. } else {
  12773. node.expression = this.parseExprSubscripts();
  12774. }
  12775. return this.finishNode(node, "Decorator");
  12776. }
  12777. parseMaybeDecoratorArguments(expr) {
  12778. if (this.eat(10)) {
  12779. const node = this.startNodeAtNode(expr);
  12780. node.callee = expr;
  12781. node.arguments = this.parseCallExpressionArguments(11, false);
  12782. this.toReferencedList(node.arguments);
  12783. return this.finishNode(node, "CallExpression");
  12784. }
  12785. return expr;
  12786. }
  12787. parseBreakContinueStatement(node, isBreak) {
  12788. this.next();
  12789. if (this.isLineTerminator()) {
  12790. node.label = null;
  12791. } else {
  12792. node.label = this.parseIdentifier();
  12793. this.semicolon();
  12794. }
  12795. this.verifyBreakContinue(node, isBreak);
  12796. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  12797. }
  12798. verifyBreakContinue(node, isBreak) {
  12799. let i;
  12800. for (i = 0; i < this.state.labels.length; ++i) {
  12801. const lab = this.state.labels[i];
  12802. if (node.label == null || lab.name === node.label.name) {
  12803. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  12804. if (node.label && isBreak) break;
  12805. }
  12806. }
  12807. if (i === this.state.labels.length) {
  12808. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  12809. this.raise(Errors.IllegalBreakContinue, {
  12810. at: node,
  12811. type
  12812. });
  12813. }
  12814. }
  12815. parseDebuggerStatement(node) {
  12816. this.next();
  12817. this.semicolon();
  12818. return this.finishNode(node, "DebuggerStatement");
  12819. }
  12820. parseHeaderExpression() {
  12821. this.expect(10);
  12822. const val = this.parseExpression();
  12823. this.expect(11);
  12824. return val;
  12825. }
  12826. parseDoWhileStatement(node) {
  12827. this.next();
  12828. this.state.labels.push(loopLabel);
  12829. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12830. this.state.labels.pop();
  12831. this.expect(92);
  12832. node.test = this.parseHeaderExpression();
  12833. this.eat(13);
  12834. return this.finishNode(node, "DoWhileStatement");
  12835. }
  12836. parseForStatement(node) {
  12837. this.next();
  12838. this.state.labels.push(loopLabel);
  12839. let awaitAt = null;
  12840. if (this.isAwaitAllowed() && this.eatContextual(96)) {
  12841. awaitAt = this.state.lastTokStartLoc;
  12842. }
  12843. this.scope.enter(SCOPE_OTHER);
  12844. this.expect(10);
  12845. if (this.match(13)) {
  12846. if (awaitAt !== null) {
  12847. this.unexpected(awaitAt);
  12848. }
  12849. return this.parseFor(node, null);
  12850. }
  12851. const startsWithLet = this.isContextual(99);
  12852. const startsWithUsing = this.isContextual(105) && !this.hasFollowingLineBreak();
  12853. const isLetOrUsing = startsWithLet && this.hasFollowingBindingAtom() || startsWithUsing && this.hasFollowingBindingIdentifier() && this.startsUsingForOf();
  12854. if (this.match(74) || this.match(75) || isLetOrUsing) {
  12855. const initNode = this.startNode();
  12856. const kind = this.state.value;
  12857. this.next();
  12858. this.parseVar(initNode, true, kind);
  12859. const init = this.finishNode(initNode, "VariableDeclaration");
  12860. const isForIn = this.match(58);
  12861. if (isForIn && startsWithUsing) {
  12862. this.raise(Errors.ForInUsing, {
  12863. at: init
  12864. });
  12865. }
  12866. if ((isForIn || this.isContextual(101)) && init.declarations.length === 1) {
  12867. return this.parseForIn(node, init, awaitAt);
  12868. }
  12869. if (awaitAt !== null) {
  12870. this.unexpected(awaitAt);
  12871. }
  12872. return this.parseFor(node, init);
  12873. }
  12874. const startsWithAsync = this.isContextual(95);
  12875. const refExpressionErrors = new ExpressionErrors();
  12876. const init = this.parseExpression(true, refExpressionErrors);
  12877. const isForOf = this.isContextual(101);
  12878. if (isForOf) {
  12879. if (startsWithLet) {
  12880. this.raise(Errors.ForOfLet, {
  12881. at: init
  12882. });
  12883. }
  12884. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  12885. this.raise(Errors.ForOfAsync, {
  12886. at: init
  12887. });
  12888. }
  12889. }
  12890. if (isForOf || this.match(58)) {
  12891. this.checkDestructuringPrivate(refExpressionErrors);
  12892. this.toAssignable(init, true);
  12893. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  12894. this.checkLVal(init, {
  12895. in: {
  12896. type
  12897. }
  12898. });
  12899. return this.parseForIn(node, init, awaitAt);
  12900. } else {
  12901. this.checkExpressionErrors(refExpressionErrors, true);
  12902. }
  12903. if (awaitAt !== null) {
  12904. this.unexpected(awaitAt);
  12905. }
  12906. return this.parseFor(node, init);
  12907. }
  12908. parseFunctionStatement(node, isAsync, isHangingDeclaration) {
  12909. this.next();
  12910. return this.parseFunction(node, 1 | (isHangingDeclaration ? 2 : 0) | (isAsync ? 8 : 0));
  12911. }
  12912. parseIfStatement(node) {
  12913. this.next();
  12914. node.test = this.parseHeaderExpression();
  12915. node.consequent = this.parseStatementOrSloppyAnnexBFunctionDeclaration();
  12916. node.alternate = this.eat(66) ? this.parseStatementOrSloppyAnnexBFunctionDeclaration() : null;
  12917. return this.finishNode(node, "IfStatement");
  12918. }
  12919. parseReturnStatement(node) {
  12920. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  12921. this.raise(Errors.IllegalReturn, {
  12922. at: this.state.startLoc
  12923. });
  12924. }
  12925. this.next();
  12926. if (this.isLineTerminator()) {
  12927. node.argument = null;
  12928. } else {
  12929. node.argument = this.parseExpression();
  12930. this.semicolon();
  12931. }
  12932. return this.finishNode(node, "ReturnStatement");
  12933. }
  12934. parseSwitchStatement(node) {
  12935. this.next();
  12936. node.discriminant = this.parseHeaderExpression();
  12937. const cases = node.cases = [];
  12938. this.expect(5);
  12939. this.state.labels.push(switchLabel);
  12940. this.scope.enter(SCOPE_OTHER);
  12941. let cur;
  12942. for (let sawDefault; !this.match(8);) {
  12943. if (this.match(61) || this.match(65)) {
  12944. const isCase = this.match(61);
  12945. if (cur) this.finishNode(cur, "SwitchCase");
  12946. cases.push(cur = this.startNode());
  12947. cur.consequent = [];
  12948. this.next();
  12949. if (isCase) {
  12950. cur.test = this.parseExpression();
  12951. } else {
  12952. if (sawDefault) {
  12953. this.raise(Errors.MultipleDefaultsInSwitch, {
  12954. at: this.state.lastTokStartLoc
  12955. });
  12956. }
  12957. sawDefault = true;
  12958. cur.test = null;
  12959. }
  12960. this.expect(14);
  12961. } else {
  12962. if (cur) {
  12963. cur.consequent.push(this.parseStatementListItem());
  12964. } else {
  12965. this.unexpected();
  12966. }
  12967. }
  12968. }
  12969. this.scope.exit();
  12970. if (cur) this.finishNode(cur, "SwitchCase");
  12971. this.next();
  12972. this.state.labels.pop();
  12973. return this.finishNode(node, "SwitchStatement");
  12974. }
  12975. parseThrowStatement(node) {
  12976. this.next();
  12977. if (this.hasPrecedingLineBreak()) {
  12978. this.raise(Errors.NewlineAfterThrow, {
  12979. at: this.state.lastTokEndLoc
  12980. });
  12981. }
  12982. node.argument = this.parseExpression();
  12983. this.semicolon();
  12984. return this.finishNode(node, "ThrowStatement");
  12985. }
  12986. parseCatchClauseParam() {
  12987. const param = this.parseBindingAtom();
  12988. this.scope.enter(this.options.annexB && param.type === "Identifier" ? SCOPE_SIMPLE_CATCH : 0);
  12989. this.checkLVal(param, {
  12990. in: {
  12991. type: "CatchClause"
  12992. },
  12993. binding: BIND_CATCH_PARAM
  12994. });
  12995. return param;
  12996. }
  12997. parseTryStatement(node) {
  12998. this.next();
  12999. node.block = this.parseBlock();
  13000. node.handler = null;
  13001. if (this.match(62)) {
  13002. const clause = this.startNode();
  13003. this.next();
  13004. if (this.match(10)) {
  13005. this.expect(10);
  13006. clause.param = this.parseCatchClauseParam();
  13007. this.expect(11);
  13008. } else {
  13009. clause.param = null;
  13010. this.scope.enter(SCOPE_OTHER);
  13011. }
  13012. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  13013. this.scope.exit();
  13014. node.handler = this.finishNode(clause, "CatchClause");
  13015. }
  13016. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  13017. if (!node.handler && !node.finalizer) {
  13018. this.raise(Errors.NoCatchOrFinally, {
  13019. at: node
  13020. });
  13021. }
  13022. return this.finishNode(node, "TryStatement");
  13023. }
  13024. parseVarStatement(node, kind, allowMissingInitializer = false) {
  13025. this.next();
  13026. this.parseVar(node, false, kind, allowMissingInitializer);
  13027. this.semicolon();
  13028. return this.finishNode(node, "VariableDeclaration");
  13029. }
  13030. parseWhileStatement(node) {
  13031. this.next();
  13032. node.test = this.parseHeaderExpression();
  13033. this.state.labels.push(loopLabel);
  13034. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13035. this.state.labels.pop();
  13036. return this.finishNode(node, "WhileStatement");
  13037. }
  13038. parseWithStatement(node) {
  13039. if (this.state.strict) {
  13040. this.raise(Errors.StrictWith, {
  13041. at: this.state.startLoc
  13042. });
  13043. }
  13044. this.next();
  13045. node.object = this.parseHeaderExpression();
  13046. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13047. return this.finishNode(node, "WithStatement");
  13048. }
  13049. parseEmptyStatement(node) {
  13050. this.next();
  13051. return this.finishNode(node, "EmptyStatement");
  13052. }
  13053. parseLabeledStatement(node, maybeName, expr, flags) {
  13054. for (const label of this.state.labels) {
  13055. if (label.name === maybeName) {
  13056. this.raise(Errors.LabelRedeclaration, {
  13057. at: expr,
  13058. labelName: maybeName
  13059. });
  13060. }
  13061. }
  13062. const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(71) ? "switch" : null;
  13063. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  13064. const label = this.state.labels[i];
  13065. if (label.statementStart === node.start) {
  13066. label.statementStart = this.state.start;
  13067. label.kind = kind;
  13068. } else {
  13069. break;
  13070. }
  13071. }
  13072. this.state.labels.push({
  13073. name: maybeName,
  13074. kind: kind,
  13075. statementStart: this.state.start
  13076. });
  13077. node.body = flags & 8 ? this.parseStatementOrSloppyAnnexBFunctionDeclaration(true) : this.parseStatement();
  13078. this.state.labels.pop();
  13079. node.label = expr;
  13080. return this.finishNode(node, "LabeledStatement");
  13081. }
  13082. parseExpressionStatement(node, expr, decorators) {
  13083. node.expression = expr;
  13084. this.semicolon();
  13085. return this.finishNode(node, "ExpressionStatement");
  13086. }
  13087. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  13088. const node = this.startNode();
  13089. if (allowDirectives) {
  13090. this.state.strictErrors.clear();
  13091. }
  13092. this.expect(5);
  13093. if (createNewLexicalScope) {
  13094. this.scope.enter(SCOPE_OTHER);
  13095. }
  13096. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  13097. if (createNewLexicalScope) {
  13098. this.scope.exit();
  13099. }
  13100. return this.finishNode(node, "BlockStatement");
  13101. }
  13102. isValidDirective(stmt) {
  13103. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  13104. }
  13105. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  13106. const body = node.body = [];
  13107. const directives = node.directives = [];
  13108. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  13109. }
  13110. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  13111. const oldStrict = this.state.strict;
  13112. let hasStrictModeDirective = false;
  13113. let parsedNonDirective = false;
  13114. while (!this.match(end)) {
  13115. const stmt = topLevel ? this.parseModuleItem() : this.parseStatementListItem();
  13116. if (directives && !parsedNonDirective) {
  13117. if (this.isValidDirective(stmt)) {
  13118. const directive = this.stmtToDirective(stmt);
  13119. directives.push(directive);
  13120. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  13121. hasStrictModeDirective = true;
  13122. this.setStrict(true);
  13123. }
  13124. continue;
  13125. }
  13126. parsedNonDirective = true;
  13127. this.state.strictErrors.clear();
  13128. }
  13129. body.push(stmt);
  13130. }
  13131. if (afterBlockParse) {
  13132. afterBlockParse.call(this, hasStrictModeDirective);
  13133. }
  13134. if (!oldStrict) {
  13135. this.setStrict(false);
  13136. }
  13137. this.next();
  13138. }
  13139. parseFor(node, init) {
  13140. node.init = init;
  13141. this.semicolon(false);
  13142. node.test = this.match(13) ? null : this.parseExpression();
  13143. this.semicolon(false);
  13144. node.update = this.match(11) ? null : this.parseExpression();
  13145. this.expect(11);
  13146. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13147. this.scope.exit();
  13148. this.state.labels.pop();
  13149. return this.finishNode(node, "ForStatement");
  13150. }
  13151. parseForIn(node, init, awaitAt) {
  13152. const isForIn = this.match(58);
  13153. this.next();
  13154. if (isForIn) {
  13155. if (awaitAt !== null) this.unexpected(awaitAt);
  13156. } else {
  13157. node.await = awaitAt !== null;
  13158. }
  13159. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || !this.options.annexB || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  13160. this.raise(Errors.ForInOfLoopInitializer, {
  13161. at: init,
  13162. type: isForIn ? "ForInStatement" : "ForOfStatement"
  13163. });
  13164. }
  13165. if (init.type === "AssignmentPattern") {
  13166. this.raise(Errors.InvalidLhs, {
  13167. at: init,
  13168. ancestor: {
  13169. type: "ForStatement"
  13170. }
  13171. });
  13172. }
  13173. node.left = init;
  13174. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  13175. this.expect(11);
  13176. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13177. this.scope.exit();
  13178. this.state.labels.pop();
  13179. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  13180. }
  13181. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  13182. const declarations = node.declarations = [];
  13183. node.kind = kind;
  13184. for (;;) {
  13185. const decl = this.startNode();
  13186. this.parseVarId(decl, kind);
  13187. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  13188. if (decl.init === null && !allowMissingInitializer) {
  13189. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(101)))) {
  13190. this.raise(Errors.DeclarationMissingInitializer, {
  13191. at: this.state.lastTokEndLoc,
  13192. kind: "destructuring"
  13193. });
  13194. } else if (kind === "const" && !(this.match(58) || this.isContextual(101))) {
  13195. this.raise(Errors.DeclarationMissingInitializer, {
  13196. at: this.state.lastTokEndLoc,
  13197. kind: "const"
  13198. });
  13199. }
  13200. }
  13201. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  13202. if (!this.eat(12)) break;
  13203. }
  13204. return node;
  13205. }
  13206. parseVarId(decl, kind) {
  13207. if (kind === "using" && !this.inModule && this.match(96)) {
  13208. this.raise(Errors.AwaitInUsingBinding, {
  13209. at: this.state.startLoc
  13210. });
  13211. }
  13212. const id = this.parseBindingAtom();
  13213. this.checkLVal(id, {
  13214. in: {
  13215. type: "VariableDeclarator"
  13216. },
  13217. binding: kind === "var" ? BIND_VAR : BIND_LEXICAL
  13218. });
  13219. decl.id = id;
  13220. }
  13221. parseAsyncFunctionExpression(node) {
  13222. return this.parseFunction(node, 8);
  13223. }
  13224. parseFunction(node, flags = 0) {
  13225. const hangingDeclaration = flags & 2;
  13226. const isDeclaration = !!(flags & 1);
  13227. const requireId = isDeclaration && !(flags & 4);
  13228. const isAsync = !!(flags & 8);
  13229. this.initFunction(node, isAsync);
  13230. if (this.match(55)) {
  13231. if (hangingDeclaration) {
  13232. this.raise(Errors.GeneratorInSingleStatementContext, {
  13233. at: this.state.startLoc
  13234. });
  13235. }
  13236. this.next();
  13237. node.generator = true;
  13238. }
  13239. if (isDeclaration) {
  13240. node.id = this.parseFunctionId(requireId);
  13241. }
  13242. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  13243. this.state.maybeInArrowParameters = false;
  13244. this.scope.enter(SCOPE_FUNCTION);
  13245. this.prodParam.enter(functionFlags(isAsync, node.generator));
  13246. if (!isDeclaration) {
  13247. node.id = this.parseFunctionId();
  13248. }
  13249. this.parseFunctionParams(node, false);
  13250. this.withSmartMixTopicForbiddingContext(() => {
  13251. this.parseFunctionBodyAndFinish(node, isDeclaration ? "FunctionDeclaration" : "FunctionExpression");
  13252. });
  13253. this.prodParam.exit();
  13254. this.scope.exit();
  13255. if (isDeclaration && !hangingDeclaration) {
  13256. this.registerFunctionStatementId(node);
  13257. }
  13258. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  13259. return node;
  13260. }
  13261. parseFunctionId(requireId) {
  13262. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  13263. }
  13264. parseFunctionParams(node, isConstructor) {
  13265. this.expect(10);
  13266. this.expressionScope.enter(newParameterDeclarationScope());
  13267. node.params = this.parseBindingList(11, 41, 2 | (isConstructor ? 4 : 0));
  13268. this.expressionScope.exit();
  13269. }
  13270. registerFunctionStatementId(node) {
  13271. if (!node.id) return;
  13272. this.scope.declareName(node.id.name, !this.options.annexB || this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, node.id.loc.start);
  13273. }
  13274. parseClass(node, isStatement, optionalId) {
  13275. this.next();
  13276. const oldStrict = this.state.strict;
  13277. this.state.strict = true;
  13278. this.parseClassId(node, isStatement, optionalId);
  13279. this.parseClassSuper(node);
  13280. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  13281. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  13282. }
  13283. isClassProperty() {
  13284. return this.match(29) || this.match(13) || this.match(8);
  13285. }
  13286. isClassMethod() {
  13287. return this.match(10);
  13288. }
  13289. isNonstaticConstructor(method) {
  13290. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  13291. }
  13292. parseClassBody(hadSuperClass, oldStrict) {
  13293. this.classScope.enter();
  13294. const state = {
  13295. hadConstructor: false,
  13296. hadSuperClass
  13297. };
  13298. let decorators = [];
  13299. const classBody = this.startNode();
  13300. classBody.body = [];
  13301. this.expect(5);
  13302. this.withSmartMixTopicForbiddingContext(() => {
  13303. while (!this.match(8)) {
  13304. if (this.eat(13)) {
  13305. if (decorators.length > 0) {
  13306. throw this.raise(Errors.DecoratorSemicolon, {
  13307. at: this.state.lastTokEndLoc
  13308. });
  13309. }
  13310. continue;
  13311. }
  13312. if (this.match(26)) {
  13313. decorators.push(this.parseDecorator());
  13314. continue;
  13315. }
  13316. const member = this.startNode();
  13317. if (decorators.length) {
  13318. member.decorators = decorators;
  13319. this.resetStartLocationFromNode(member, decorators[0]);
  13320. decorators = [];
  13321. }
  13322. this.parseClassMember(classBody, member, state);
  13323. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  13324. this.raise(Errors.DecoratorConstructor, {
  13325. at: member
  13326. });
  13327. }
  13328. }
  13329. });
  13330. this.state.strict = oldStrict;
  13331. this.next();
  13332. if (decorators.length) {
  13333. throw this.raise(Errors.TrailingDecorator, {
  13334. at: this.state.startLoc
  13335. });
  13336. }
  13337. this.classScope.exit();
  13338. return this.finishNode(classBody, "ClassBody");
  13339. }
  13340. parseClassMemberFromModifier(classBody, member) {
  13341. const key = this.parseIdentifier(true);
  13342. if (this.isClassMethod()) {
  13343. const method = member;
  13344. method.kind = "method";
  13345. method.computed = false;
  13346. method.key = key;
  13347. method.static = false;
  13348. this.pushClassMethod(classBody, method, false, false, false, false);
  13349. return true;
  13350. } else if (this.isClassProperty()) {
  13351. const prop = member;
  13352. prop.computed = false;
  13353. prop.key = key;
  13354. prop.static = false;
  13355. classBody.body.push(this.parseClassProperty(prop));
  13356. return true;
  13357. }
  13358. this.resetPreviousNodeTrailingComments(key);
  13359. return false;
  13360. }
  13361. parseClassMember(classBody, member, state) {
  13362. const isStatic = this.isContextual(104);
  13363. if (isStatic) {
  13364. if (this.parseClassMemberFromModifier(classBody, member)) {
  13365. return;
  13366. }
  13367. if (this.eat(5)) {
  13368. this.parseClassStaticBlock(classBody, member);
  13369. return;
  13370. }
  13371. }
  13372. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  13373. }
  13374. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  13375. const publicMethod = member;
  13376. const privateMethod = member;
  13377. const publicProp = member;
  13378. const privateProp = member;
  13379. const accessorProp = member;
  13380. const method = publicMethod;
  13381. const publicMember = publicMethod;
  13382. member.static = isStatic;
  13383. this.parsePropertyNamePrefixOperator(member);
  13384. if (this.eat(55)) {
  13385. method.kind = "method";
  13386. const isPrivateName = this.match(136);
  13387. this.parseClassElementName(method);
  13388. if (isPrivateName) {
  13389. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  13390. return;
  13391. }
  13392. if (this.isNonstaticConstructor(publicMethod)) {
  13393. this.raise(Errors.ConstructorIsGenerator, {
  13394. at: publicMethod.key
  13395. });
  13396. }
  13397. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  13398. return;
  13399. }
  13400. const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
  13401. const isPrivate = this.match(136);
  13402. const key = this.parseClassElementName(member);
  13403. const maybeQuestionTokenStartLoc = this.state.startLoc;
  13404. this.parsePostMemberNameModifiers(publicMember);
  13405. if (this.isClassMethod()) {
  13406. method.kind = "method";
  13407. if (isPrivate) {
  13408. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13409. return;
  13410. }
  13411. const isConstructor = this.isNonstaticConstructor(publicMethod);
  13412. let allowsDirectSuper = false;
  13413. if (isConstructor) {
  13414. publicMethod.kind = "constructor";
  13415. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  13416. this.raise(Errors.DuplicateConstructor, {
  13417. at: key
  13418. });
  13419. }
  13420. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  13421. this.raise(Errors.OverrideOnConstructor, {
  13422. at: key
  13423. });
  13424. }
  13425. state.hadConstructor = true;
  13426. allowsDirectSuper = state.hadSuperClass;
  13427. }
  13428. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  13429. } else if (this.isClassProperty()) {
  13430. if (isPrivate) {
  13431. this.pushClassPrivateProperty(classBody, privateProp);
  13432. } else {
  13433. this.pushClassProperty(classBody, publicProp);
  13434. }
  13435. } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
  13436. this.resetPreviousNodeTrailingComments(key);
  13437. const isGenerator = this.eat(55);
  13438. if (publicMember.optional) {
  13439. this.unexpected(maybeQuestionTokenStartLoc);
  13440. }
  13441. method.kind = "method";
  13442. const isPrivate = this.match(136);
  13443. this.parseClassElementName(method);
  13444. this.parsePostMemberNameModifiers(publicMember);
  13445. if (isPrivate) {
  13446. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  13447. } else {
  13448. if (this.isNonstaticConstructor(publicMethod)) {
  13449. this.raise(Errors.ConstructorIsAsync, {
  13450. at: publicMethod.key
  13451. });
  13452. }
  13453. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  13454. }
  13455. } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(55) && this.isLineTerminator())) {
  13456. this.resetPreviousNodeTrailingComments(key);
  13457. method.kind = key.name;
  13458. const isPrivate = this.match(136);
  13459. this.parseClassElementName(publicMethod);
  13460. if (isPrivate) {
  13461. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13462. } else {
  13463. if (this.isNonstaticConstructor(publicMethod)) {
  13464. this.raise(Errors.ConstructorIsAccessor, {
  13465. at: publicMethod.key
  13466. });
  13467. }
  13468. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  13469. }
  13470. this.checkGetterSetterParams(publicMethod);
  13471. } else if (isContextual && key.name === "accessor" && !this.isLineTerminator()) {
  13472. this.expectPlugin("decoratorAutoAccessors");
  13473. this.resetPreviousNodeTrailingComments(key);
  13474. const isPrivate = this.match(136);
  13475. this.parseClassElementName(publicProp);
  13476. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  13477. } else if (this.isLineTerminator()) {
  13478. if (isPrivate) {
  13479. this.pushClassPrivateProperty(classBody, privateProp);
  13480. } else {
  13481. this.pushClassProperty(classBody, publicProp);
  13482. }
  13483. } else {
  13484. this.unexpected();
  13485. }
  13486. }
  13487. parseClassElementName(member) {
  13488. const {
  13489. type,
  13490. value
  13491. } = this.state;
  13492. if ((type === 130 || type === 131) && member.static && value === "prototype") {
  13493. this.raise(Errors.StaticPrototype, {
  13494. at: this.state.startLoc
  13495. });
  13496. }
  13497. if (type === 136) {
  13498. if (value === "constructor") {
  13499. this.raise(Errors.ConstructorClassPrivateField, {
  13500. at: this.state.startLoc
  13501. });
  13502. }
  13503. const key = this.parsePrivateName();
  13504. member.key = key;
  13505. return key;
  13506. }
  13507. return this.parsePropertyName(member);
  13508. }
  13509. parseClassStaticBlock(classBody, member) {
  13510. var _member$decorators;
  13511. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  13512. const oldLabels = this.state.labels;
  13513. this.state.labels = [];
  13514. this.prodParam.enter(PARAM);
  13515. const body = member.body = [];
  13516. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  13517. this.prodParam.exit();
  13518. this.scope.exit();
  13519. this.state.labels = oldLabels;
  13520. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13521. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13522. this.raise(Errors.DecoratorStaticBlock, {
  13523. at: member
  13524. });
  13525. }
  13526. }
  13527. pushClassProperty(classBody, prop) {
  13528. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  13529. this.raise(Errors.ConstructorClassField, {
  13530. at: prop.key
  13531. });
  13532. }
  13533. classBody.body.push(this.parseClassProperty(prop));
  13534. }
  13535. pushClassPrivateProperty(classBody, prop) {
  13536. const node = this.parseClassPrivateProperty(prop);
  13537. classBody.body.push(node);
  13538. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13539. }
  13540. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13541. if (!isPrivate && !prop.computed) {
  13542. const key = prop.key;
  13543. if (key.name === "constructor" || key.value === "constructor") {
  13544. this.raise(Errors.ConstructorClassField, {
  13545. at: key
  13546. });
  13547. }
  13548. }
  13549. const node = this.parseClassAccessorProperty(prop);
  13550. classBody.body.push(node);
  13551. if (isPrivate) {
  13552. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13553. }
  13554. }
  13555. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13556. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13557. }
  13558. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13559. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13560. classBody.body.push(node);
  13561. 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;
  13562. this.declareClassPrivateMethodInScope(node, kind);
  13563. }
  13564. declareClassPrivateMethodInScope(node, kind) {
  13565. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13566. }
  13567. parsePostMemberNameModifiers(methodOrProp) {}
  13568. parseClassPrivateProperty(node) {
  13569. this.parseInitializer(node);
  13570. this.semicolon();
  13571. return this.finishNode(node, "ClassPrivateProperty");
  13572. }
  13573. parseClassProperty(node) {
  13574. this.parseInitializer(node);
  13575. this.semicolon();
  13576. return this.finishNode(node, "ClassProperty");
  13577. }
  13578. parseClassAccessorProperty(node) {
  13579. this.parseInitializer(node);
  13580. this.semicolon();
  13581. return this.finishNode(node, "ClassAccessorProperty");
  13582. }
  13583. parseInitializer(node) {
  13584. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  13585. this.expressionScope.enter(newExpressionScope());
  13586. this.prodParam.enter(PARAM);
  13587. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13588. this.expressionScope.exit();
  13589. this.prodParam.exit();
  13590. this.scope.exit();
  13591. }
  13592. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  13593. if (tokenIsIdentifier(this.state.type)) {
  13594. node.id = this.parseIdentifier();
  13595. if (isStatement) {
  13596. this.declareNameFromIdentifier(node.id, bindingType);
  13597. }
  13598. } else {
  13599. if (optionalId || !isStatement) {
  13600. node.id = null;
  13601. } else {
  13602. throw this.raise(Errors.MissingClassName, {
  13603. at: this.state.startLoc
  13604. });
  13605. }
  13606. }
  13607. }
  13608. parseClassSuper(node) {
  13609. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13610. }
  13611. parseExport(node, decorators) {
  13612. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  13613. const parseAfterDefault = !hasDefault || this.eat(12);
  13614. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13615. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13616. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13617. const isFromRequired = hasDefault || hasStar;
  13618. if (hasStar && !hasNamespace) {
  13619. if (hasDefault) this.unexpected();
  13620. if (decorators) {
  13621. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13622. at: node
  13623. });
  13624. }
  13625. this.parseExportFrom(node, true);
  13626. return this.finishNode(node, "ExportAllDeclaration");
  13627. }
  13628. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13629. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
  13630. this.unexpected(null, 5);
  13631. }
  13632. if (hasNamespace && parseAfterNamespace) {
  13633. this.unexpected(null, 97);
  13634. }
  13635. let hasDeclaration;
  13636. if (isFromRequired || hasSpecifiers) {
  13637. hasDeclaration = false;
  13638. if (decorators) {
  13639. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13640. at: node
  13641. });
  13642. }
  13643. this.parseExportFrom(node, isFromRequired);
  13644. } else {
  13645. hasDeclaration = this.maybeParseExportDeclaration(node);
  13646. }
  13647. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13648. var _node2$declaration;
  13649. const node2 = node;
  13650. this.checkExport(node2, true, false, !!node2.source);
  13651. if (((_node2$declaration = node2.declaration) == null ? void 0 : _node2$declaration.type) === "ClassDeclaration") {
  13652. this.maybeTakeDecorators(decorators, node2.declaration, node2);
  13653. } else if (decorators) {
  13654. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13655. at: node
  13656. });
  13657. }
  13658. return this.finishNode(node2, "ExportNamedDeclaration");
  13659. }
  13660. if (this.eat(65)) {
  13661. const node2 = node;
  13662. const decl = this.parseExportDefaultExpression();
  13663. node2.declaration = decl;
  13664. if (decl.type === "ClassDeclaration") {
  13665. this.maybeTakeDecorators(decorators, decl, node2);
  13666. } else if (decorators) {
  13667. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13668. at: node
  13669. });
  13670. }
  13671. this.checkExport(node2, true, true);
  13672. return this.finishNode(node2, "ExportDefaultDeclaration");
  13673. }
  13674. this.unexpected(null, 5);
  13675. }
  13676. eatExportStar(node) {
  13677. return this.eat(55);
  13678. }
  13679. maybeParseExportDefaultSpecifier(node) {
  13680. if (this.isExportDefaultSpecifier()) {
  13681. this.expectPlugin("exportDefaultFrom");
  13682. const specifier = this.startNode();
  13683. specifier.exported = this.parseIdentifier(true);
  13684. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13685. return true;
  13686. }
  13687. return false;
  13688. }
  13689. maybeParseExportNamespaceSpecifier(node) {
  13690. if (this.isContextual(93)) {
  13691. if (!node.specifiers) node.specifiers = [];
  13692. const specifier = this.startNodeAt(this.state.lastTokStartLoc);
  13693. this.next();
  13694. specifier.exported = this.parseModuleExportName();
  13695. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13696. return true;
  13697. }
  13698. return false;
  13699. }
  13700. maybeParseExportNamedSpecifiers(node) {
  13701. if (this.match(5)) {
  13702. if (!node.specifiers) node.specifiers = [];
  13703. const isTypeExport = node.exportKind === "type";
  13704. node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13705. node.source = null;
  13706. node.declaration = null;
  13707. if (this.hasPlugin("importAssertions")) {
  13708. node.assertions = [];
  13709. }
  13710. return true;
  13711. }
  13712. return false;
  13713. }
  13714. maybeParseExportDeclaration(node) {
  13715. if (this.shouldParseExportDeclaration()) {
  13716. node.specifiers = [];
  13717. node.source = null;
  13718. if (this.hasPlugin("importAssertions")) {
  13719. node.assertions = [];
  13720. }
  13721. node.declaration = this.parseExportDeclaration(node);
  13722. return true;
  13723. }
  13724. return false;
  13725. }
  13726. isAsyncFunction() {
  13727. if (!this.isContextual(95)) return false;
  13728. const next = this.nextTokenStart();
  13729. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  13730. }
  13731. parseExportDefaultExpression() {
  13732. const expr = this.startNode();
  13733. if (this.match(68)) {
  13734. this.next();
  13735. return this.parseFunction(expr, 1 | 4);
  13736. } else if (this.isAsyncFunction()) {
  13737. this.next();
  13738. this.next();
  13739. return this.parseFunction(expr, 1 | 4 | 8);
  13740. }
  13741. if (this.match(80)) {
  13742. return this.parseClass(expr, true, true);
  13743. }
  13744. if (this.match(26)) {
  13745. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13746. this.raise(Errors.DecoratorBeforeExport, {
  13747. at: this.state.startLoc
  13748. });
  13749. }
  13750. return this.parseClass(this.maybeTakeDecorators(this.parseDecorators(false), this.startNode()), true, true);
  13751. }
  13752. if (this.match(75) || this.match(74) || this.isLet()) {
  13753. throw this.raise(Errors.UnsupportedDefaultExport, {
  13754. at: this.state.startLoc
  13755. });
  13756. }
  13757. const res = this.parseMaybeAssignAllowIn();
  13758. this.semicolon();
  13759. return res;
  13760. }
  13761. parseExportDeclaration(node) {
  13762. if (this.match(80)) {
  13763. const node = this.parseClass(this.startNode(), true, false);
  13764. return node;
  13765. }
  13766. return this.parseStatementListItem();
  13767. }
  13768. isExportDefaultSpecifier() {
  13769. const {
  13770. type
  13771. } = this.state;
  13772. if (tokenIsIdentifier(type)) {
  13773. if (type === 95 && !this.state.containsEsc || type === 99) {
  13774. return false;
  13775. }
  13776. if ((type === 128 || type === 127) && !this.state.containsEsc) {
  13777. const {
  13778. type: nextType
  13779. } = this.lookahead();
  13780. if (tokenIsIdentifier(nextType) && nextType !== 97 || nextType === 5) {
  13781. this.expectOnePlugin(["flow", "typescript"]);
  13782. return false;
  13783. }
  13784. }
  13785. } else if (!this.match(65)) {
  13786. return false;
  13787. }
  13788. const next = this.nextTokenStart();
  13789. const hasFrom = this.isUnparsedContextual(next, "from");
  13790. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13791. return true;
  13792. }
  13793. if (this.match(65) && hasFrom) {
  13794. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13795. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13796. }
  13797. return false;
  13798. }
  13799. parseExportFrom(node, expect) {
  13800. if (this.eatContextual(97)) {
  13801. node.source = this.parseImportSource();
  13802. this.checkExport(node);
  13803. const assertions = this.maybeParseImportAssertions();
  13804. if (assertions) {
  13805. node.assertions = assertions;
  13806. this.checkJSONModuleImport(node);
  13807. }
  13808. } else if (expect) {
  13809. this.unexpected();
  13810. }
  13811. this.semicolon();
  13812. }
  13813. shouldParseExportDeclaration() {
  13814. const {
  13815. type
  13816. } = this.state;
  13817. if (type === 26) {
  13818. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  13819. if (this.hasPlugin("decorators")) {
  13820. if (this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13821. this.raise(Errors.DecoratorBeforeExport, {
  13822. at: this.state.startLoc
  13823. });
  13824. }
  13825. return true;
  13826. }
  13827. }
  13828. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  13829. }
  13830. checkExport(node, checkNames, isDefault, isFrom) {
  13831. if (checkNames) {
  13832. if (isDefault) {
  13833. this.checkDuplicateExports(node, "default");
  13834. if (this.hasPlugin("exportDefaultFrom")) {
  13835. var _declaration$extra;
  13836. const declaration = node.declaration;
  13837. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  13838. this.raise(Errors.ExportDefaultFromAsIdentifier, {
  13839. at: declaration
  13840. });
  13841. }
  13842. }
  13843. } else if (node.specifiers && node.specifiers.length) {
  13844. for (const specifier of node.specifiers) {
  13845. const {
  13846. exported
  13847. } = specifier;
  13848. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  13849. this.checkDuplicateExports(specifier, exportName);
  13850. if (!isFrom && specifier.local) {
  13851. const {
  13852. local
  13853. } = specifier;
  13854. if (local.type !== "Identifier") {
  13855. this.raise(Errors.ExportBindingIsString, {
  13856. at: specifier,
  13857. localName: local.value,
  13858. exportName
  13859. });
  13860. } else {
  13861. this.checkReservedWord(local.name, local.loc.start, true, false);
  13862. this.scope.checkLocalExport(local);
  13863. }
  13864. }
  13865. }
  13866. } else if (node.declaration) {
  13867. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  13868. const id = node.declaration.id;
  13869. if (!id) throw new Error("Assertion failure");
  13870. this.checkDuplicateExports(node, id.name);
  13871. } else if (node.declaration.type === "VariableDeclaration") {
  13872. for (const declaration of node.declaration.declarations) {
  13873. this.checkDeclaration(declaration.id);
  13874. }
  13875. }
  13876. }
  13877. }
  13878. }
  13879. checkDeclaration(node) {
  13880. if (node.type === "Identifier") {
  13881. this.checkDuplicateExports(node, node.name);
  13882. } else if (node.type === "ObjectPattern") {
  13883. for (const prop of node.properties) {
  13884. this.checkDeclaration(prop);
  13885. }
  13886. } else if (node.type === "ArrayPattern") {
  13887. for (const elem of node.elements) {
  13888. if (elem) {
  13889. this.checkDeclaration(elem);
  13890. }
  13891. }
  13892. } else if (node.type === "ObjectProperty") {
  13893. this.checkDeclaration(node.value);
  13894. } else if (node.type === "RestElement") {
  13895. this.checkDeclaration(node.argument);
  13896. } else if (node.type === "AssignmentPattern") {
  13897. this.checkDeclaration(node.left);
  13898. }
  13899. }
  13900. checkDuplicateExports(node, exportName) {
  13901. if (this.exportedIdentifiers.has(exportName)) {
  13902. if (exportName === "default") {
  13903. this.raise(Errors.DuplicateDefaultExport, {
  13904. at: node
  13905. });
  13906. } else {
  13907. this.raise(Errors.DuplicateExport, {
  13908. at: node,
  13909. exportName
  13910. });
  13911. }
  13912. }
  13913. this.exportedIdentifiers.add(exportName);
  13914. }
  13915. parseExportSpecifiers(isInTypeExport) {
  13916. const nodes = [];
  13917. let first = true;
  13918. this.expect(5);
  13919. while (!this.eat(8)) {
  13920. if (first) {
  13921. first = false;
  13922. } else {
  13923. this.expect(12);
  13924. if (this.eat(8)) break;
  13925. }
  13926. const isMaybeTypeOnly = this.isContextual(128);
  13927. const isString = this.match(131);
  13928. const node = this.startNode();
  13929. node.local = this.parseModuleExportName();
  13930. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  13931. }
  13932. return nodes;
  13933. }
  13934. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  13935. if (this.eatContextual(93)) {
  13936. node.exported = this.parseModuleExportName();
  13937. } else if (isString) {
  13938. node.exported = cloneStringLiteral(node.local);
  13939. } else if (!node.exported) {
  13940. node.exported = cloneIdentifier(node.local);
  13941. }
  13942. return this.finishNode(node, "ExportSpecifier");
  13943. }
  13944. parseModuleExportName() {
  13945. if (this.match(131)) {
  13946. const result = this.parseStringLiteral(this.state.value);
  13947. const surrogate = result.value.match(loneSurrogate);
  13948. if (surrogate) {
  13949. this.raise(Errors.ModuleExportNameHasLoneSurrogate, {
  13950. at: result,
  13951. surrogateCharCode: surrogate[0].charCodeAt(0)
  13952. });
  13953. }
  13954. return result;
  13955. }
  13956. return this.parseIdentifier(true);
  13957. }
  13958. isJSONModuleImport(node) {
  13959. if (node.assertions != null) {
  13960. return node.assertions.some(({
  13961. key,
  13962. value
  13963. }) => {
  13964. return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
  13965. });
  13966. }
  13967. return false;
  13968. }
  13969. checkImportReflection(node) {
  13970. if (node.module) {
  13971. var _node$assertions;
  13972. if (node.specifiers.length !== 1 || node.specifiers[0].type !== "ImportDefaultSpecifier") {
  13973. this.raise(Errors.ImportReflectionNotBinding, {
  13974. at: node.specifiers[0].loc.start
  13975. });
  13976. }
  13977. if (((_node$assertions = node.assertions) == null ? void 0 : _node$assertions.length) > 0) {
  13978. this.raise(Errors.ImportReflectionHasAssertion, {
  13979. at: node.specifiers[0].loc.start
  13980. });
  13981. }
  13982. }
  13983. }
  13984. checkJSONModuleImport(node) {
  13985. if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
  13986. const {
  13987. specifiers
  13988. } = node;
  13989. if (specifiers != null) {
  13990. const nonDefaultNamedSpecifier = specifiers.find(specifier => {
  13991. let imported;
  13992. if (specifier.type === "ExportSpecifier") {
  13993. imported = specifier.local;
  13994. } else if (specifier.type === "ImportSpecifier") {
  13995. imported = specifier.imported;
  13996. }
  13997. if (imported !== undefined) {
  13998. return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
  13999. }
  14000. });
  14001. if (nonDefaultNamedSpecifier !== undefined) {
  14002. this.raise(Errors.ImportJSONBindingNotDefault, {
  14003. at: nonDefaultNamedSpecifier.loc.start
  14004. });
  14005. }
  14006. }
  14007. }
  14008. }
  14009. parseMaybeImportReflection(node) {
  14010. let isImportReflection = false;
  14011. if (this.isContextual(125)) {
  14012. const lookahead = this.lookahead();
  14013. const nextType = lookahead.type;
  14014. if (tokenIsIdentifier(nextType)) {
  14015. if (nextType !== 97) {
  14016. isImportReflection = true;
  14017. } else {
  14018. const nextNextTokenFirstChar = this.input.charCodeAt(this.nextTokenStartSince(lookahead.end));
  14019. if (nextNextTokenFirstChar === 102) {
  14020. isImportReflection = true;
  14021. }
  14022. }
  14023. } else if (nextType !== 12) {
  14024. isImportReflection = true;
  14025. }
  14026. }
  14027. if (isImportReflection) {
  14028. this.expectPlugin("importReflection");
  14029. this.next();
  14030. node.module = true;
  14031. } else if (this.hasPlugin("importReflection")) {
  14032. node.module = false;
  14033. }
  14034. }
  14035. parseImport(node) {
  14036. node.specifiers = [];
  14037. if (!this.match(131)) {
  14038. this.parseMaybeImportReflection(node);
  14039. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  14040. const parseNext = !hasDefault || this.eat(12);
  14041. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  14042. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  14043. this.expectContextual(97);
  14044. }
  14045. node.source = this.parseImportSource();
  14046. const assertions = this.maybeParseImportAssertions();
  14047. if (assertions) {
  14048. node.assertions = assertions;
  14049. } else {
  14050. const attributes = this.maybeParseModuleAttributes();
  14051. if (attributes) {
  14052. node.attributes = attributes;
  14053. }
  14054. }
  14055. this.checkImportReflection(node);
  14056. this.checkJSONModuleImport(node);
  14057. this.semicolon();
  14058. return this.finishNode(node, "ImportDeclaration");
  14059. }
  14060. parseImportSource() {
  14061. if (!this.match(131)) this.unexpected();
  14062. return this.parseExprAtom();
  14063. }
  14064. shouldParseDefaultImport(node) {
  14065. return tokenIsIdentifier(this.state.type);
  14066. }
  14067. parseImportSpecifierLocal(node, specifier, type) {
  14068. specifier.local = this.parseIdentifier();
  14069. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  14070. }
  14071. finishImportSpecifier(specifier, type, bindingType = BIND_LEXICAL) {
  14072. this.checkLVal(specifier.local, {
  14073. in: {
  14074. type
  14075. },
  14076. binding: bindingType
  14077. });
  14078. return this.finishNode(specifier, type);
  14079. }
  14080. parseAssertEntries() {
  14081. const attrs = [];
  14082. const attrNames = new Set();
  14083. do {
  14084. if (this.match(8)) {
  14085. break;
  14086. }
  14087. const node = this.startNode();
  14088. const keyName = this.state.value;
  14089. if (attrNames.has(keyName)) {
  14090. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  14091. at: this.state.startLoc,
  14092. key: keyName
  14093. });
  14094. }
  14095. attrNames.add(keyName);
  14096. if (this.match(131)) {
  14097. node.key = this.parseStringLiteral(keyName);
  14098. } else {
  14099. node.key = this.parseIdentifier(true);
  14100. }
  14101. this.expect(14);
  14102. if (!this.match(131)) {
  14103. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  14104. at: this.state.startLoc
  14105. });
  14106. }
  14107. node.value = this.parseStringLiteral(this.state.value);
  14108. attrs.push(this.finishNode(node, "ImportAttribute"));
  14109. } while (this.eat(12));
  14110. return attrs;
  14111. }
  14112. maybeParseModuleAttributes() {
  14113. if (this.match(76) && !this.hasPrecedingLineBreak()) {
  14114. this.expectPlugin("moduleAttributes");
  14115. this.next();
  14116. } else {
  14117. if (this.hasPlugin("moduleAttributes")) return [];
  14118. return null;
  14119. }
  14120. const attrs = [];
  14121. const attributes = new Set();
  14122. do {
  14123. const node = this.startNode();
  14124. node.key = this.parseIdentifier(true);
  14125. if (node.key.name !== "type") {
  14126. this.raise(Errors.ModuleAttributeDifferentFromType, {
  14127. at: node.key
  14128. });
  14129. }
  14130. if (attributes.has(node.key.name)) {
  14131. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  14132. at: node.key,
  14133. key: node.key.name
  14134. });
  14135. }
  14136. attributes.add(node.key.name);
  14137. this.expect(14);
  14138. if (!this.match(131)) {
  14139. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  14140. at: this.state.startLoc
  14141. });
  14142. }
  14143. node.value = this.parseStringLiteral(this.state.value);
  14144. this.finishNode(node, "ImportAttribute");
  14145. attrs.push(node);
  14146. } while (this.eat(12));
  14147. return attrs;
  14148. }
  14149. maybeParseImportAssertions() {
  14150. if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  14151. this.expectPlugin("importAssertions");
  14152. this.next();
  14153. } else {
  14154. if (this.hasPlugin("importAssertions")) return [];
  14155. return null;
  14156. }
  14157. this.eat(5);
  14158. const attrs = this.parseAssertEntries();
  14159. this.eat(8);
  14160. return attrs;
  14161. }
  14162. maybeParseDefaultImportSpecifier(node) {
  14163. if (this.shouldParseDefaultImport(node)) {
  14164. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  14165. return true;
  14166. }
  14167. return false;
  14168. }
  14169. maybeParseStarImportSpecifier(node) {
  14170. if (this.match(55)) {
  14171. const specifier = this.startNode();
  14172. this.next();
  14173. this.expectContextual(93);
  14174. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  14175. return true;
  14176. }
  14177. return false;
  14178. }
  14179. parseNamedImportSpecifiers(node) {
  14180. let first = true;
  14181. this.expect(5);
  14182. while (!this.eat(8)) {
  14183. if (first) {
  14184. first = false;
  14185. } else {
  14186. if (this.eat(14)) {
  14187. throw this.raise(Errors.DestructureNamedImport, {
  14188. at: this.state.startLoc
  14189. });
  14190. }
  14191. this.expect(12);
  14192. if (this.eat(8)) break;
  14193. }
  14194. const specifier = this.startNode();
  14195. const importedIsString = this.match(131);
  14196. const isMaybeTypeOnly = this.isContextual(128);
  14197. specifier.imported = this.parseModuleExportName();
  14198. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly, undefined);
  14199. node.specifiers.push(importSpecifier);
  14200. }
  14201. }
  14202. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  14203. if (this.eatContextual(93)) {
  14204. specifier.local = this.parseIdentifier();
  14205. } else {
  14206. const {
  14207. imported
  14208. } = specifier;
  14209. if (importedIsString) {
  14210. throw this.raise(Errors.ImportBindingIsString, {
  14211. at: specifier,
  14212. importName: imported.value
  14213. });
  14214. }
  14215. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  14216. if (!specifier.local) {
  14217. specifier.local = cloneIdentifier(imported);
  14218. }
  14219. }
  14220. return this.finishImportSpecifier(specifier, "ImportSpecifier", bindingType);
  14221. }
  14222. isThisParam(param) {
  14223. return param.type === "Identifier" && param.name === "this";
  14224. }
  14225. }
  14226. class Parser extends StatementParser {
  14227. constructor(options, input) {
  14228. options = getOptions(options);
  14229. super(options, input);
  14230. this.options = options;
  14231. this.initializeScopes();
  14232. this.plugins = pluginsMap(this.options.plugins);
  14233. this.filename = options.sourceFilename;
  14234. }
  14235. getScopeHandler() {
  14236. return ScopeHandler;
  14237. }
  14238. parse() {
  14239. this.enterInitialScopes();
  14240. const file = this.startNode();
  14241. const program = this.startNode();
  14242. this.nextToken();
  14243. file.errors = null;
  14244. this.parseTopLevel(file, program);
  14245. file.errors = this.state.errors;
  14246. return file;
  14247. }
  14248. }
  14249. function pluginsMap(plugins) {
  14250. const pluginMap = new Map();
  14251. for (const plugin of plugins) {
  14252. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  14253. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  14254. }
  14255. return pluginMap;
  14256. }
  14257. function parse(input, options) {
  14258. var _options;
  14259. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  14260. options = Object.assign({}, options);
  14261. try {
  14262. options.sourceType = "module";
  14263. const parser = getParser(options, input);
  14264. const ast = parser.parse();
  14265. if (parser.sawUnambiguousESM) {
  14266. return ast;
  14267. }
  14268. if (parser.ambiguousScriptDifferentAst) {
  14269. try {
  14270. options.sourceType = "script";
  14271. return getParser(options, input).parse();
  14272. } catch (_unused) {}
  14273. } else {
  14274. ast.program.sourceType = "script";
  14275. }
  14276. return ast;
  14277. } catch (moduleError) {
  14278. try {
  14279. options.sourceType = "script";
  14280. return getParser(options, input).parse();
  14281. } catch (_unused2) {}
  14282. throw moduleError;
  14283. }
  14284. } else {
  14285. return getParser(options, input).parse();
  14286. }
  14287. }
  14288. function parseExpression(input, options) {
  14289. const parser = getParser(options, input);
  14290. if (parser.options.strictMode) {
  14291. parser.state.strict = true;
  14292. }
  14293. return parser.getExpression();
  14294. }
  14295. function generateExportedTokenTypes(internalTokenTypes) {
  14296. const tokenTypes = {};
  14297. for (const typeName of Object.keys(internalTokenTypes)) {
  14298. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  14299. }
  14300. return tokenTypes;
  14301. }
  14302. const tokTypes = generateExportedTokenTypes(tt);
  14303. function getParser(options, input) {
  14304. let cls = Parser;
  14305. if (options != null && options.plugins) {
  14306. validatePlugins(options.plugins);
  14307. cls = getParserClass(options.plugins);
  14308. }
  14309. return new cls(options, input);
  14310. }
  14311. const parserClassCache = {};
  14312. function getParserClass(pluginsFromOptions) {
  14313. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  14314. const key = pluginList.join("/");
  14315. let cls = parserClassCache[key];
  14316. if (!cls) {
  14317. cls = Parser;
  14318. for (const plugin of pluginList) {
  14319. cls = mixinPlugins[plugin](cls);
  14320. }
  14321. parserClassCache[key] = cls;
  14322. }
  14323. return cls;
  14324. }
  14325. exports.parse = parse;
  14326. exports.parseExpression = parseExpression;
  14327. exports.tokTypes = tokTypes;
  14328. //# sourceMappingURL=index.js.map