index.js 467 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _objectWithoutPropertiesLoose(source, excluded) {
  4. if (source == null) return {};
  5. var target = {};
  6. var sourceKeys = Object.keys(source);
  7. var key, i;
  8. for (i = 0; i < sourceKeys.length; i++) {
  9. key = sourceKeys[i];
  10. if (excluded.indexOf(key) >= 0) continue;
  11. target[key] = source[key];
  12. }
  13. return target;
  14. }
  15. class Position {
  16. constructor(line, col, index) {
  17. this.line = void 0;
  18. this.column = void 0;
  19. this.index = void 0;
  20. this.line = line;
  21. this.column = col;
  22. this.index = index;
  23. }
  24. }
  25. class SourceLocation {
  26. constructor(start, end) {
  27. this.start = void 0;
  28. this.end = void 0;
  29. this.filename = void 0;
  30. this.identifierName = void 0;
  31. this.start = start;
  32. this.end = end;
  33. }
  34. }
  35. function createPositionWithColumnOffset(position, columnOffset) {
  36. const {
  37. line,
  38. column,
  39. index
  40. } = position;
  41. return new Position(line, column + columnOffset, index + columnOffset);
  42. }
  43. var ParseErrorCode = {
  44. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  45. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  46. };
  47. const reflect = (keys, last = keys.length - 1) => ({
  48. get() {
  49. return keys.reduce((object, key) => object[key], this);
  50. },
  51. set(value) {
  52. keys.reduce((item, key, i) => i === last ? item[key] = value : item[key], this);
  53. }
  54. });
  55. const instantiate = (constructor, properties, descriptors) => Object.keys(descriptors).map(key => [key, descriptors[key]]).filter(([, descriptor]) => !!descriptor).map(([key, descriptor]) => [key, typeof descriptor === "function" ? {
  56. value: descriptor,
  57. enumerable: false
  58. } : typeof descriptor.reflect === "string" ? Object.assign({}, descriptor, reflect(descriptor.reflect.split("."))) : descriptor]).reduce((instance, [key, descriptor]) => Object.defineProperty(instance, key, Object.assign({
  59. configurable: true
  60. }, descriptor)), Object.assign(new constructor(), properties));
  61. var ModuleErrors = {
  62. ImportMetaOutsideModule: {
  63. message: `import.meta may appear only with 'sourceType: "module"'`,
  64. code: ParseErrorCode.SourceTypeModuleError
  65. },
  66. ImportOutsideModule: {
  67. message: `'import' and 'export' may appear only with 'sourceType: "module"'`,
  68. code: ParseErrorCode.SourceTypeModuleError
  69. }
  70. };
  71. const NodeDescriptions = {
  72. ArrayPattern: "array destructuring pattern",
  73. AssignmentExpression: "assignment expression",
  74. AssignmentPattern: "assignment expression",
  75. ArrowFunctionExpression: "arrow function expression",
  76. ConditionalExpression: "conditional expression",
  77. CatchClause: "catch clause",
  78. ForOfStatement: "for-of statement",
  79. ForInStatement: "for-in statement",
  80. ForStatement: "for-loop",
  81. FormalParameters: "function parameter list",
  82. Identifier: "identifier",
  83. ImportSpecifier: "import specifier",
  84. ImportDefaultSpecifier: "import default specifier",
  85. ImportNamespaceSpecifier: "import namespace specifier",
  86. ObjectPattern: "object destructuring pattern",
  87. ParenthesizedExpression: "parenthesized expression",
  88. RestElement: "rest element",
  89. UpdateExpression: {
  90. true: "prefix operation",
  91. false: "postfix operation"
  92. },
  93. VariableDeclarator: "variable declaration",
  94. YieldExpression: "yield expression"
  95. };
  96. const toNodeDescription = ({
  97. type,
  98. prefix
  99. }) => type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[String(prefix)] : NodeDescriptions[type];
  100. var StandardErrors = {
  101. AccessorIsGenerator: ({
  102. kind
  103. }) => `A ${kind}ter cannot be a generator.`,
  104. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  105. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  106. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  107. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  108. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  109. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  110. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  111. BadGetterArity: "A 'get' accesor must not have any formal parameters.",
  112. BadSetterArity: "A 'set' accesor must have exactly one formal parameter.",
  113. BadSetterRestParameter: "A 'set' accesor function argument must not be a rest parameter.",
  114. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  115. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  116. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  117. ConstructorIsAsync: "Constructor can't be an async function.",
  118. ConstructorIsGenerator: "Constructor can't be a generator.",
  119. DeclarationMissingInitializer: ({
  120. kind
  121. }) => `Missing initializer in ${kind} declaration.`,
  122. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax.",
  123. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  124. DecoratorExportClass: "Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead.",
  125. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  126. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  127. DeletePrivateField: "Deleting a private field is not allowed.",
  128. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  129. DuplicateConstructor: "Duplicate constructor in the same class.",
  130. DuplicateDefaultExport: "Only one default export allowed per module.",
  131. DuplicateExport: ({
  132. exportName
  133. }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`,
  134. DuplicateProto: "Redefinition of __proto__ property.",
  135. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  136. ElementAfterRest: "Rest element must be last element.",
  137. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  138. ExportBindingIsString: ({
  139. localName,
  140. exportName
  141. }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`,
  142. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  143. ForInOfLoopInitializer: ({
  144. type
  145. }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`,
  146. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  147. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  148. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  149. IllegalBreakContinue: ({
  150. type
  151. }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`,
  152. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  153. IllegalReturn: "'return' outside of function.",
  154. ImportBindingIsString: ({
  155. importName
  156. }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`,
  157. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  158. ImportCallArity: ({
  159. maxArgumentCount
  160. }) => `\`import()\` requires exactly ${maxArgumentCount === 1 ? "one argument" : "one or two arguments"}.`,
  161. ImportCallNotNewExpression: "Cannot use new with import(...).",
  162. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  163. ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.",
  164. IncompatibleRegExpUVFlags: "The 'u' and 'v' regular expression flags cannot be enabled at the same time.",
  165. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  166. InvalidCodePoint: "Code point out of bounds.",
  167. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  168. InvalidDecimal: "Invalid decimal.",
  169. InvalidDigit: ({
  170. radix
  171. }) => `Expected number in radix ${radix}.`,
  172. InvalidEscapeSequence: "Bad character escape sequence.",
  173. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  174. InvalidEscapedReservedWord: ({
  175. reservedWord
  176. }) => `Escape sequence in keyword ${reservedWord}.`,
  177. InvalidIdentifier: ({
  178. identifierName
  179. }) => `Invalid identifier ${identifierName}.`,
  180. InvalidLhs: ({
  181. ancestor
  182. }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  183. InvalidLhsBinding: ({
  184. ancestor
  185. }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  186. InvalidNumber: "Invalid number.",
  187. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  188. InvalidOrUnexpectedToken: ({
  189. unexpected
  190. }) => `Unexpected character '${unexpected}'.`,
  191. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  192. InvalidPrivateFieldResolution: ({
  193. identifierName
  194. }) => `Private name #${identifierName} is not defined.`,
  195. InvalidPropertyBindingPattern: "Binding member expression.",
  196. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  197. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  198. LabelRedeclaration: ({
  199. labelName
  200. }) => `Label '${labelName}' is already declared.`,
  201. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  202. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  203. MalformedRegExpFlags: "Invalid regular expression flag.",
  204. MissingClassName: "A class name is required.",
  205. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  206. MissingSemicolon: "Missing semicolon.",
  207. MissingPlugin: ({
  208. missingPlugin
  209. }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  210. MissingOneOfPlugins: ({
  211. missingPlugin
  212. }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  213. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  214. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  215. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  216. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  217. ModuleAttributesWithDuplicateKeys: ({
  218. key
  219. }) => `Duplicate key "${key}" is not allowed in module attributes.`,
  220. ModuleExportNameHasLoneSurrogate: ({
  221. surrogateCharCode
  222. }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`,
  223. ModuleExportUndefined: ({
  224. localName
  225. }) => `Export '${localName}' is not defined.`,
  226. MultipleDefaultsInSwitch: "Multiple default clauses.",
  227. NewlineAfterThrow: "Illegal newline after throw.",
  228. NoCatchOrFinally: "Missing catch or finally clause.",
  229. NumberIdentifier: "Identifier directly after number.",
  230. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  231. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  232. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  233. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  234. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  235. ParamDupe: "Argument name clash.",
  236. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  237. PatternHasMethod: "Object pattern can't contain methods.",
  238. PrivateInExpectedIn: ({
  239. identifierName
  240. }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`,
  241. PrivateNameRedeclaration: ({
  242. identifierName
  243. }) => `Duplicate private name #${identifierName}.`,
  244. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  245. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  246. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  247. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  248. RestTrailingComma: "Unexpected trailing comma after rest element.",
  249. SloppyFunction: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
  250. StaticPrototype: "Classes may not have static property named prototype.",
  251. 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?",
  252. SuperPrivateField: "Private fields can't be accessed on super.",
  253. TrailingDecorator: "Decorators must be attached to a class element.",
  254. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  255. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  256. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  257. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  258. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  259. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  260. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  261. UnexpectedKeyword: ({
  262. keyword
  263. }) => `Unexpected keyword '${keyword}'.`,
  264. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  265. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  266. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  267. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  268. UnexpectedPrivateField: "Unexpected private name.",
  269. UnexpectedReservedWord: ({
  270. reservedWord
  271. }) => `Unexpected reserved word '${reservedWord}'.`,
  272. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  273. UnexpectedToken: ({
  274. expected,
  275. unexpected
  276. }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`,
  277. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  278. UnsupportedBind: "Binding should be performed on object property.",
  279. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  280. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  281. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  282. UnsupportedMetaProperty: ({
  283. target,
  284. onlyValidPropertyName
  285. }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`,
  286. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  287. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  288. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  289. UnterminatedComment: "Unterminated comment.",
  290. UnterminatedRegExp: "Unterminated regular expression.",
  291. UnterminatedString: "Unterminated string constant.",
  292. UnterminatedTemplate: "Unterminated template.",
  293. VarRedeclaration: ({
  294. identifierName
  295. }) => `Identifier '${identifierName}' has already been declared.`,
  296. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  297. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  298. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  299. };
  300. var StrictModeErrors = {
  301. StrictDelete: "Deleting local variable in strict mode.",
  302. StrictEvalArguments: ({
  303. referenceName
  304. }) => `Assigning to '${referenceName}' in strict mode.`,
  305. StrictEvalArgumentsBinding: ({
  306. bindingName
  307. }) => `Binding '${bindingName}' in strict mode.`,
  308. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  309. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  310. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  311. StrictWith: "'with' in strict mode."
  312. };
  313. const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
  314. var PipelineOperatorErrors = {
  315. PipeBodyIsTighter: "Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  316. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  317. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  318. PipeTopicUnconfiguredToken: ({
  319. token
  320. }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`,
  321. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  322. PipeUnparenthesizedBody: ({
  323. type
  324. }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
  325. type
  326. })}; please wrap it in parentheses.`,
  327. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  328. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  329. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  330. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  331. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  332. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'
  333. };
  334. const _excluded$1 = ["toMessage"],
  335. _excluded2$1 = ["message"];
  336. function toParseErrorConstructor(_ref) {
  337. let {
  338. toMessage
  339. } = _ref,
  340. properties = _objectWithoutPropertiesLoose(_ref, _excluded$1);
  341. return function constructor({
  342. loc,
  343. details
  344. }) {
  345. return instantiate(SyntaxError, Object.assign({}, properties, {
  346. loc
  347. }), {
  348. clone(overrides = {}) {
  349. const loc = overrides.loc || {};
  350. return constructor({
  351. 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),
  352. details: Object.assign({}, this.details, overrides.details)
  353. });
  354. },
  355. details: {
  356. value: details,
  357. enumerable: false
  358. },
  359. message: {
  360. get() {
  361. return `${toMessage(this.details)} (${this.loc.line}:${this.loc.column})`;
  362. },
  363. set(value) {
  364. Object.defineProperty(this, "message", {
  365. value
  366. });
  367. }
  368. },
  369. pos: {
  370. reflect: "loc.index",
  371. enumerable: true
  372. },
  373. missingPlugin: "missingPlugin" in details && {
  374. reflect: "details.missingPlugin",
  375. enumerable: true
  376. }
  377. });
  378. };
  379. }
  380. function ParseErrorEnum(argument, syntaxPlugin) {
  381. if (Array.isArray(argument)) {
  382. return parseErrorTemplates => ParseErrorEnum(parseErrorTemplates, argument[0]);
  383. }
  384. const ParseErrorConstructors = {};
  385. for (const reasonCode of Object.keys(argument)) {
  386. const template = argument[reasonCode];
  387. const _ref2 = typeof template === "string" ? {
  388. message: () => template
  389. } : typeof template === "function" ? {
  390. message: template
  391. } : template,
  392. {
  393. message
  394. } = _ref2,
  395. rest = _objectWithoutPropertiesLoose(_ref2, _excluded2$1);
  396. const toMessage = typeof message === "string" ? () => message : message;
  397. ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
  398. code: ParseErrorCode.SyntaxError,
  399. reasonCode,
  400. toMessage
  401. }, syntaxPlugin ? {
  402. syntaxPlugin
  403. } : {}, rest));
  404. }
  405. return ParseErrorConstructors;
  406. }
  407. const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
  408. const {
  409. defineProperty
  410. } = Object;
  411. const toUnenumerable = (object, key) => defineProperty(object, key, {
  412. enumerable: false,
  413. value: object[key]
  414. });
  415. function toESTreeLocation(node) {
  416. node.loc.start && toUnenumerable(node.loc.start, "index");
  417. node.loc.end && toUnenumerable(node.loc.end, "index");
  418. return node;
  419. }
  420. var estree = (superClass => class ESTreeParserMixin extends superClass {
  421. parse() {
  422. const file = toESTreeLocation(super.parse());
  423. if (this.options.tokens) {
  424. file.tokens = file.tokens.map(toESTreeLocation);
  425. }
  426. return file;
  427. }
  428. parseRegExpLiteral({
  429. pattern,
  430. flags
  431. }) {
  432. let regex = null;
  433. try {
  434. regex = new RegExp(pattern, flags);
  435. } catch (e) {}
  436. const node = this.estreeParseLiteral(regex);
  437. node.regex = {
  438. pattern,
  439. flags
  440. };
  441. return node;
  442. }
  443. parseBigIntLiteral(value) {
  444. let bigInt;
  445. try {
  446. bigInt = BigInt(value);
  447. } catch (_unused) {
  448. bigInt = null;
  449. }
  450. const node = this.estreeParseLiteral(bigInt);
  451. node.bigint = String(node.value || value);
  452. return node;
  453. }
  454. parseDecimalLiteral(value) {
  455. const decimal = null;
  456. const node = this.estreeParseLiteral(decimal);
  457. node.decimal = String(node.value || value);
  458. return node;
  459. }
  460. estreeParseLiteral(value) {
  461. return this.parseLiteral(value, "Literal");
  462. }
  463. parseStringLiteral(value) {
  464. return this.estreeParseLiteral(value);
  465. }
  466. parseNumericLiteral(value) {
  467. return this.estreeParseLiteral(value);
  468. }
  469. parseNullLiteral() {
  470. return this.estreeParseLiteral(null);
  471. }
  472. parseBooleanLiteral(value) {
  473. return this.estreeParseLiteral(value);
  474. }
  475. directiveToStmt(directive) {
  476. const directiveLiteral = directive.value;
  477. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  478. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  479. expression.value = directiveLiteral.extra.expressionValue;
  480. expression.raw = directiveLiteral.extra.raw;
  481. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.loc.end);
  482. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  483. return this.finishNodeAt(stmt, "ExpressionStatement", directive.loc.end);
  484. }
  485. initFunction(node, isAsync) {
  486. super.initFunction(node, isAsync);
  487. node.expression = false;
  488. }
  489. checkDeclaration(node) {
  490. if (node != null && this.isObjectProperty(node)) {
  491. this.checkDeclaration(node.value);
  492. } else {
  493. super.checkDeclaration(node);
  494. }
  495. }
  496. getObjectOrClassMethodParams(method) {
  497. return method.value.params;
  498. }
  499. isValidDirective(stmt) {
  500. var _stmt$expression$extr;
  501. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  502. }
  503. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  504. super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
  505. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  506. node.body = directiveStatements.concat(node.body);
  507. delete node.directives;
  508. }
  509. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  510. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  511. if (method.typeParameters) {
  512. method.value.typeParameters = method.typeParameters;
  513. delete method.typeParameters;
  514. }
  515. classBody.body.push(method);
  516. }
  517. parsePrivateName() {
  518. const node = super.parsePrivateName();
  519. {
  520. if (!this.getPluginOption("estree", "classFeatures")) {
  521. return node;
  522. }
  523. }
  524. return this.convertPrivateNameToPrivateIdentifier(node);
  525. }
  526. convertPrivateNameToPrivateIdentifier(node) {
  527. const name = super.getPrivateNameSV(node);
  528. node = node;
  529. delete node.id;
  530. node.name = name;
  531. node.type = "PrivateIdentifier";
  532. return node;
  533. }
  534. isPrivateName(node) {
  535. {
  536. if (!this.getPluginOption("estree", "classFeatures")) {
  537. return super.isPrivateName(node);
  538. }
  539. }
  540. return node.type === "PrivateIdentifier";
  541. }
  542. getPrivateNameSV(node) {
  543. {
  544. if (!this.getPluginOption("estree", "classFeatures")) {
  545. return super.getPrivateNameSV(node);
  546. }
  547. }
  548. return node.name;
  549. }
  550. parseLiteral(value, type) {
  551. const node = super.parseLiteral(value, type);
  552. node.raw = node.extra.raw;
  553. delete node.extra;
  554. return node;
  555. }
  556. parseFunctionBody(node, allowExpression, isMethod = false) {
  557. super.parseFunctionBody(node, allowExpression, isMethod);
  558. node.expression = node.body.type !== "BlockStatement";
  559. }
  560. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  561. let funcNode = this.startNode();
  562. funcNode.kind = node.kind;
  563. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  564. funcNode.type = "FunctionExpression";
  565. delete funcNode.kind;
  566. node.value = funcNode;
  567. if (type === "ClassPrivateMethod") {
  568. node.computed = false;
  569. }
  570. return this.finishNode(node, "MethodDefinition");
  571. }
  572. parseClassProperty(...args) {
  573. const propertyNode = super.parseClassProperty(...args);
  574. {
  575. if (!this.getPluginOption("estree", "classFeatures")) {
  576. return propertyNode;
  577. }
  578. }
  579. propertyNode.type = "PropertyDefinition";
  580. return propertyNode;
  581. }
  582. parseClassPrivateProperty(...args) {
  583. const propertyNode = super.parseClassPrivateProperty(...args);
  584. {
  585. if (!this.getPluginOption("estree", "classFeatures")) {
  586. return propertyNode;
  587. }
  588. }
  589. propertyNode.type = "PropertyDefinition";
  590. propertyNode.computed = false;
  591. return propertyNode;
  592. }
  593. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  594. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  595. if (node) {
  596. node.type = "Property";
  597. if (node.kind === "method") {
  598. node.kind = "init";
  599. }
  600. node.shorthand = false;
  601. }
  602. return node;
  603. }
  604. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  605. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  606. if (node) {
  607. node.kind = "init";
  608. node.type = "Property";
  609. }
  610. return node;
  611. }
  612. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  613. return type === "Property" ? "value" : super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  614. }
  615. isAssignable(node, isBinding) {
  616. if (node != null && this.isObjectProperty(node)) {
  617. return this.isAssignable(node.value, isBinding);
  618. }
  619. return super.isAssignable(node, isBinding);
  620. }
  621. toAssignable(node, isLHS = false) {
  622. if (node != null && this.isObjectProperty(node)) {
  623. const {
  624. key,
  625. value
  626. } = node;
  627. if (this.isPrivateName(key)) {
  628. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  629. }
  630. this.toAssignable(value, isLHS);
  631. } else {
  632. super.toAssignable(node, isLHS);
  633. }
  634. }
  635. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  636. if (prop.kind === "get" || prop.kind === "set") {
  637. this.raise(Errors.PatternHasAccessor, {
  638. at: prop.key
  639. });
  640. } else if (prop.method) {
  641. this.raise(Errors.PatternHasMethod, {
  642. at: prop.key
  643. });
  644. } else {
  645. super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  646. }
  647. }
  648. finishCallExpression(unfinished, optional) {
  649. const node = super.finishCallExpression(unfinished, optional);
  650. if (node.callee.type === "Import") {
  651. node.type = "ImportExpression";
  652. node.source = node.arguments[0];
  653. if (this.hasPlugin("importAssertions")) {
  654. var _node$arguments$;
  655. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  656. }
  657. delete node.arguments;
  658. delete node.callee;
  659. }
  660. return node;
  661. }
  662. toReferencedArguments(node) {
  663. if (node.type === "ImportExpression") {
  664. return;
  665. }
  666. super.toReferencedArguments(node);
  667. }
  668. parseExport(unfinished) {
  669. const node = super.parseExport(unfinished);
  670. switch (node.type) {
  671. case "ExportAllDeclaration":
  672. node.exported = null;
  673. break;
  674. case "ExportNamedDeclaration":
  675. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  676. node.type = "ExportAllDeclaration";
  677. node.exported = node.specifiers[0].exported;
  678. delete node.specifiers;
  679. }
  680. break;
  681. }
  682. return node;
  683. }
  684. parseSubscript(base, startPos, startLoc, noCalls, state) {
  685. const node = super.parseSubscript(base, startPos, startLoc, noCalls, state);
  686. if (state.optionalChainMember) {
  687. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  688. node.type = node.type.substring(8);
  689. }
  690. if (state.stop) {
  691. const chain = this.startNodeAtNode(node);
  692. chain.expression = node;
  693. return this.finishNode(chain, "ChainExpression");
  694. }
  695. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  696. node.optional = false;
  697. }
  698. return node;
  699. }
  700. hasPropertyAsPrivateName(node) {
  701. if (node.type === "ChainExpression") {
  702. node = node.expression;
  703. }
  704. return super.hasPropertyAsPrivateName(node);
  705. }
  706. isOptionalChain(node) {
  707. return node.type === "ChainExpression";
  708. }
  709. isObjectProperty(node) {
  710. return node.type === "Property" && node.kind === "init" && !node.method;
  711. }
  712. isObjectMethod(node) {
  713. return node.method || node.kind === "get" || node.kind === "set";
  714. }
  715. finishNodeAt(node, type, endLoc) {
  716. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  717. }
  718. resetStartLocation(node, start, startLoc) {
  719. super.resetStartLocation(node, start, startLoc);
  720. toESTreeLocation(node);
  721. }
  722. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  723. super.resetEndLocation(node, endLoc);
  724. toESTreeLocation(node);
  725. }
  726. });
  727. class TokContext {
  728. constructor(token, preserveSpace) {
  729. this.token = void 0;
  730. this.preserveSpace = void 0;
  731. this.token = token;
  732. this.preserveSpace = !!preserveSpace;
  733. }
  734. }
  735. const types = {
  736. brace: new TokContext("{"),
  737. j_oTag: new TokContext("<tag"),
  738. j_cTag: new TokContext("</tag"),
  739. j_expr: new TokContext("<tag>...</tag>", true)
  740. };
  741. {
  742. types.template = new TokContext("`", true);
  743. }
  744. const beforeExpr = true;
  745. const startsExpr = true;
  746. const isLoop = true;
  747. const isAssign = true;
  748. const prefix = true;
  749. const postfix = true;
  750. class ExportedTokenType {
  751. constructor(label, conf = {}) {
  752. this.label = void 0;
  753. this.keyword = void 0;
  754. this.beforeExpr = void 0;
  755. this.startsExpr = void 0;
  756. this.rightAssociative = void 0;
  757. this.isLoop = void 0;
  758. this.isAssign = void 0;
  759. this.prefix = void 0;
  760. this.postfix = void 0;
  761. this.binop = void 0;
  762. this.label = label;
  763. this.keyword = conf.keyword;
  764. this.beforeExpr = !!conf.beforeExpr;
  765. this.startsExpr = !!conf.startsExpr;
  766. this.rightAssociative = !!conf.rightAssociative;
  767. this.isLoop = !!conf.isLoop;
  768. this.isAssign = !!conf.isAssign;
  769. this.prefix = !!conf.prefix;
  770. this.postfix = !!conf.postfix;
  771. this.binop = conf.binop != null ? conf.binop : null;
  772. {
  773. this.updateContext = null;
  774. }
  775. }
  776. }
  777. const keywords$1 = new Map();
  778. function createKeyword(name, options = {}) {
  779. options.keyword = name;
  780. const token = createToken(name, options);
  781. keywords$1.set(name, token);
  782. return token;
  783. }
  784. function createBinop(name, binop) {
  785. return createToken(name, {
  786. beforeExpr,
  787. binop
  788. });
  789. }
  790. let tokenTypeCounter = -1;
  791. const tokenTypes = [];
  792. const tokenLabels = [];
  793. const tokenBinops = [];
  794. const tokenBeforeExprs = [];
  795. const tokenStartsExprs = [];
  796. const tokenPrefixes = [];
  797. function createToken(name, options = {}) {
  798. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  799. ++tokenTypeCounter;
  800. tokenLabels.push(name);
  801. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  802. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  803. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  804. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  805. tokenTypes.push(new ExportedTokenType(name, options));
  806. return tokenTypeCounter;
  807. }
  808. function createKeywordLike(name, options = {}) {
  809. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  810. ++tokenTypeCounter;
  811. keywords$1.set(name, tokenTypeCounter);
  812. tokenLabels.push(name);
  813. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  814. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  815. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  816. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  817. tokenTypes.push(new ExportedTokenType("name", options));
  818. return tokenTypeCounter;
  819. }
  820. const tt = {
  821. bracketL: createToken("[", {
  822. beforeExpr,
  823. startsExpr
  824. }),
  825. bracketHashL: createToken("#[", {
  826. beforeExpr,
  827. startsExpr
  828. }),
  829. bracketBarL: createToken("[|", {
  830. beforeExpr,
  831. startsExpr
  832. }),
  833. bracketR: createToken("]"),
  834. bracketBarR: createToken("|]"),
  835. braceL: createToken("{", {
  836. beforeExpr,
  837. startsExpr
  838. }),
  839. braceBarL: createToken("{|", {
  840. beforeExpr,
  841. startsExpr
  842. }),
  843. braceHashL: createToken("#{", {
  844. beforeExpr,
  845. startsExpr
  846. }),
  847. braceR: createToken("}"),
  848. braceBarR: createToken("|}"),
  849. parenL: createToken("(", {
  850. beforeExpr,
  851. startsExpr
  852. }),
  853. parenR: createToken(")"),
  854. comma: createToken(",", {
  855. beforeExpr
  856. }),
  857. semi: createToken(";", {
  858. beforeExpr
  859. }),
  860. colon: createToken(":", {
  861. beforeExpr
  862. }),
  863. doubleColon: createToken("::", {
  864. beforeExpr
  865. }),
  866. dot: createToken("."),
  867. question: createToken("?", {
  868. beforeExpr
  869. }),
  870. questionDot: createToken("?."),
  871. arrow: createToken("=>", {
  872. beforeExpr
  873. }),
  874. template: createToken("template"),
  875. ellipsis: createToken("...", {
  876. beforeExpr
  877. }),
  878. backQuote: createToken("`", {
  879. startsExpr
  880. }),
  881. dollarBraceL: createToken("${", {
  882. beforeExpr,
  883. startsExpr
  884. }),
  885. templateTail: createToken("...`", {
  886. startsExpr
  887. }),
  888. templateNonTail: createToken("...${", {
  889. beforeExpr,
  890. startsExpr
  891. }),
  892. at: createToken("@"),
  893. hash: createToken("#", {
  894. startsExpr
  895. }),
  896. interpreterDirective: createToken("#!..."),
  897. eq: createToken("=", {
  898. beforeExpr,
  899. isAssign
  900. }),
  901. assign: createToken("_=", {
  902. beforeExpr,
  903. isAssign
  904. }),
  905. slashAssign: createToken("_=", {
  906. beforeExpr,
  907. isAssign
  908. }),
  909. xorAssign: createToken("_=", {
  910. beforeExpr,
  911. isAssign
  912. }),
  913. moduloAssign: createToken("_=", {
  914. beforeExpr,
  915. isAssign
  916. }),
  917. incDec: createToken("++/--", {
  918. prefix,
  919. postfix,
  920. startsExpr
  921. }),
  922. bang: createToken("!", {
  923. beforeExpr,
  924. prefix,
  925. startsExpr
  926. }),
  927. tilde: createToken("~", {
  928. beforeExpr,
  929. prefix,
  930. startsExpr
  931. }),
  932. doubleCaret: createToken("^^", {
  933. startsExpr
  934. }),
  935. doubleAt: createToken("@@", {
  936. startsExpr
  937. }),
  938. pipeline: createBinop("|>", 0),
  939. nullishCoalescing: createBinop("??", 1),
  940. logicalOR: createBinop("||", 1),
  941. logicalAND: createBinop("&&", 2),
  942. bitwiseOR: createBinop("|", 3),
  943. bitwiseXOR: createBinop("^", 4),
  944. bitwiseAND: createBinop("&", 5),
  945. equality: createBinop("==/!=/===/!==", 6),
  946. lt: createBinop("</>/<=/>=", 7),
  947. gt: createBinop("</>/<=/>=", 7),
  948. relational: createBinop("</>/<=/>=", 7),
  949. bitShift: createBinop("<</>>/>>>", 8),
  950. bitShiftL: createBinop("<</>>/>>>", 8),
  951. bitShiftR: createBinop("<</>>/>>>", 8),
  952. plusMin: createToken("+/-", {
  953. beforeExpr,
  954. binop: 9,
  955. prefix,
  956. startsExpr
  957. }),
  958. modulo: createToken("%", {
  959. binop: 10,
  960. startsExpr
  961. }),
  962. star: createToken("*", {
  963. binop: 10
  964. }),
  965. slash: createBinop("/", 10),
  966. exponent: createToken("**", {
  967. beforeExpr,
  968. binop: 11,
  969. rightAssociative: true
  970. }),
  971. _in: createKeyword("in", {
  972. beforeExpr,
  973. binop: 7
  974. }),
  975. _instanceof: createKeyword("instanceof", {
  976. beforeExpr,
  977. binop: 7
  978. }),
  979. _break: createKeyword("break"),
  980. _case: createKeyword("case", {
  981. beforeExpr
  982. }),
  983. _catch: createKeyword("catch"),
  984. _continue: createKeyword("continue"),
  985. _debugger: createKeyword("debugger"),
  986. _default: createKeyword("default", {
  987. beforeExpr
  988. }),
  989. _else: createKeyword("else", {
  990. beforeExpr
  991. }),
  992. _finally: createKeyword("finally"),
  993. _function: createKeyword("function", {
  994. startsExpr
  995. }),
  996. _if: createKeyword("if"),
  997. _return: createKeyword("return", {
  998. beforeExpr
  999. }),
  1000. _switch: createKeyword("switch"),
  1001. _throw: createKeyword("throw", {
  1002. beforeExpr,
  1003. prefix,
  1004. startsExpr
  1005. }),
  1006. _try: createKeyword("try"),
  1007. _var: createKeyword("var"),
  1008. _const: createKeyword("const"),
  1009. _with: createKeyword("with"),
  1010. _new: createKeyword("new", {
  1011. beforeExpr,
  1012. startsExpr
  1013. }),
  1014. _this: createKeyword("this", {
  1015. startsExpr
  1016. }),
  1017. _super: createKeyword("super", {
  1018. startsExpr
  1019. }),
  1020. _class: createKeyword("class", {
  1021. startsExpr
  1022. }),
  1023. _extends: createKeyword("extends", {
  1024. beforeExpr
  1025. }),
  1026. _export: createKeyword("export"),
  1027. _import: createKeyword("import", {
  1028. startsExpr
  1029. }),
  1030. _null: createKeyword("null", {
  1031. startsExpr
  1032. }),
  1033. _true: createKeyword("true", {
  1034. startsExpr
  1035. }),
  1036. _false: createKeyword("false", {
  1037. startsExpr
  1038. }),
  1039. _typeof: createKeyword("typeof", {
  1040. beforeExpr,
  1041. prefix,
  1042. startsExpr
  1043. }),
  1044. _void: createKeyword("void", {
  1045. beforeExpr,
  1046. prefix,
  1047. startsExpr
  1048. }),
  1049. _delete: createKeyword("delete", {
  1050. beforeExpr,
  1051. prefix,
  1052. startsExpr
  1053. }),
  1054. _do: createKeyword("do", {
  1055. isLoop,
  1056. beforeExpr
  1057. }),
  1058. _for: createKeyword("for", {
  1059. isLoop
  1060. }),
  1061. _while: createKeyword("while", {
  1062. isLoop
  1063. }),
  1064. _as: createKeywordLike("as", {
  1065. startsExpr
  1066. }),
  1067. _assert: createKeywordLike("assert", {
  1068. startsExpr
  1069. }),
  1070. _async: createKeywordLike("async", {
  1071. startsExpr
  1072. }),
  1073. _await: createKeywordLike("await", {
  1074. startsExpr
  1075. }),
  1076. _from: createKeywordLike("from", {
  1077. startsExpr
  1078. }),
  1079. _get: createKeywordLike("get", {
  1080. startsExpr
  1081. }),
  1082. _let: createKeywordLike("let", {
  1083. startsExpr
  1084. }),
  1085. _meta: createKeywordLike("meta", {
  1086. startsExpr
  1087. }),
  1088. _of: createKeywordLike("of", {
  1089. startsExpr
  1090. }),
  1091. _sent: createKeywordLike("sent", {
  1092. startsExpr
  1093. }),
  1094. _set: createKeywordLike("set", {
  1095. startsExpr
  1096. }),
  1097. _static: createKeywordLike("static", {
  1098. startsExpr
  1099. }),
  1100. _yield: createKeywordLike("yield", {
  1101. startsExpr
  1102. }),
  1103. _asserts: createKeywordLike("asserts", {
  1104. startsExpr
  1105. }),
  1106. _checks: createKeywordLike("checks", {
  1107. startsExpr
  1108. }),
  1109. _exports: createKeywordLike("exports", {
  1110. startsExpr
  1111. }),
  1112. _global: createKeywordLike("global", {
  1113. startsExpr
  1114. }),
  1115. _implements: createKeywordLike("implements", {
  1116. startsExpr
  1117. }),
  1118. _intrinsic: createKeywordLike("intrinsic", {
  1119. startsExpr
  1120. }),
  1121. _infer: createKeywordLike("infer", {
  1122. startsExpr
  1123. }),
  1124. _is: createKeywordLike("is", {
  1125. startsExpr
  1126. }),
  1127. _mixins: createKeywordLike("mixins", {
  1128. startsExpr
  1129. }),
  1130. _proto: createKeywordLike("proto", {
  1131. startsExpr
  1132. }),
  1133. _require: createKeywordLike("require", {
  1134. startsExpr
  1135. }),
  1136. _keyof: createKeywordLike("keyof", {
  1137. startsExpr
  1138. }),
  1139. _readonly: createKeywordLike("readonly", {
  1140. startsExpr
  1141. }),
  1142. _unique: createKeywordLike("unique", {
  1143. startsExpr
  1144. }),
  1145. _abstract: createKeywordLike("abstract", {
  1146. startsExpr
  1147. }),
  1148. _declare: createKeywordLike("declare", {
  1149. startsExpr
  1150. }),
  1151. _enum: createKeywordLike("enum", {
  1152. startsExpr
  1153. }),
  1154. _module: createKeywordLike("module", {
  1155. startsExpr
  1156. }),
  1157. _namespace: createKeywordLike("namespace", {
  1158. startsExpr
  1159. }),
  1160. _interface: createKeywordLike("interface", {
  1161. startsExpr
  1162. }),
  1163. _type: createKeywordLike("type", {
  1164. startsExpr
  1165. }),
  1166. _opaque: createKeywordLike("opaque", {
  1167. startsExpr
  1168. }),
  1169. name: createToken("name", {
  1170. startsExpr
  1171. }),
  1172. string: createToken("string", {
  1173. startsExpr
  1174. }),
  1175. num: createToken("num", {
  1176. startsExpr
  1177. }),
  1178. bigint: createToken("bigint", {
  1179. startsExpr
  1180. }),
  1181. decimal: createToken("decimal", {
  1182. startsExpr
  1183. }),
  1184. regexp: createToken("regexp", {
  1185. startsExpr
  1186. }),
  1187. privateName: createToken("#name", {
  1188. startsExpr
  1189. }),
  1190. eof: createToken("eof"),
  1191. jsxName: createToken("jsxName"),
  1192. jsxText: createToken("jsxText", {
  1193. beforeExpr: true
  1194. }),
  1195. jsxTagStart: createToken("jsxTagStart", {
  1196. startsExpr: true
  1197. }),
  1198. jsxTagEnd: createToken("jsxTagEnd"),
  1199. placeholder: createToken("%%", {
  1200. startsExpr: true
  1201. })
  1202. };
  1203. function tokenIsIdentifier(token) {
  1204. return token >= 93 && token <= 128;
  1205. }
  1206. function tokenKeywordOrIdentifierIsKeyword(token) {
  1207. return token <= 92;
  1208. }
  1209. function tokenIsKeywordOrIdentifier(token) {
  1210. return token >= 58 && token <= 128;
  1211. }
  1212. function tokenIsLiteralPropertyName(token) {
  1213. return token >= 58 && token <= 132;
  1214. }
  1215. function tokenComesBeforeExpression(token) {
  1216. return tokenBeforeExprs[token];
  1217. }
  1218. function tokenCanStartExpression(token) {
  1219. return tokenStartsExprs[token];
  1220. }
  1221. function tokenIsAssignment(token) {
  1222. return token >= 29 && token <= 33;
  1223. }
  1224. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1225. return token >= 125 && token <= 127;
  1226. }
  1227. function tokenIsLoop(token) {
  1228. return token >= 90 && token <= 92;
  1229. }
  1230. function tokenIsKeyword(token) {
  1231. return token >= 58 && token <= 92;
  1232. }
  1233. function tokenIsOperator(token) {
  1234. return token >= 39 && token <= 59;
  1235. }
  1236. function tokenIsPostfix(token) {
  1237. return token === 34;
  1238. }
  1239. function tokenIsPrefix(token) {
  1240. return tokenPrefixes[token];
  1241. }
  1242. function tokenIsTSTypeOperator(token) {
  1243. return token >= 117 && token <= 119;
  1244. }
  1245. function tokenIsTSDeclarationStart(token) {
  1246. return token >= 120 && token <= 126;
  1247. }
  1248. function tokenLabelName(token) {
  1249. return tokenLabels[token];
  1250. }
  1251. function tokenOperatorPrecedence(token) {
  1252. return tokenBinops[token];
  1253. }
  1254. function tokenIsRightAssociative(token) {
  1255. return token === 57;
  1256. }
  1257. function tokenIsTemplate(token) {
  1258. return token >= 24 && token <= 25;
  1259. }
  1260. function getExportedToken(token) {
  1261. return tokenTypes[token];
  1262. }
  1263. {
  1264. tokenTypes[8].updateContext = context => {
  1265. context.pop();
  1266. };
  1267. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1268. context.push(types.brace);
  1269. };
  1270. tokenTypes[22].updateContext = context => {
  1271. if (context[context.length - 1] === types.template) {
  1272. context.pop();
  1273. } else {
  1274. context.push(types.template);
  1275. }
  1276. };
  1277. tokenTypes[138].updateContext = context => {
  1278. context.push(types.j_expr, types.j_oTag);
  1279. };
  1280. }
  1281. 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";
  1282. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0898-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f";
  1283. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1284. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1285. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1286. const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 68, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 85, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 190, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1070, 4050, 582, 8634, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8936, 3, 2, 6, 2, 1, 2, 290, 46, 2, 18, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 482, 44, 11, 6, 17, 0, 322, 29, 19, 43, 1269, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4152, 8, 221, 3, 5761, 15, 7472, 3104, 541, 1507, 4938];
  1287. const astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 154, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 161, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 19306, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 262, 6, 10, 9, 357, 0, 62, 13, 1495, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1288. function isInAstralSet(code, set) {
  1289. let pos = 0x10000;
  1290. for (let i = 0, length = set.length; i < length; i += 2) {
  1291. pos += set[i];
  1292. if (pos > code) return false;
  1293. pos += set[i + 1];
  1294. if (pos >= code) return true;
  1295. }
  1296. return false;
  1297. }
  1298. function isIdentifierStart(code) {
  1299. if (code < 65) return code === 36;
  1300. if (code <= 90) return true;
  1301. if (code < 97) return code === 95;
  1302. if (code <= 122) return true;
  1303. if (code <= 0xffff) {
  1304. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1305. }
  1306. return isInAstralSet(code, astralIdentifierStartCodes);
  1307. }
  1308. function isIdentifierChar(code) {
  1309. if (code < 48) return code === 36;
  1310. if (code < 58) return true;
  1311. if (code < 65) return false;
  1312. if (code <= 90) return true;
  1313. if (code < 97) return code === 95;
  1314. if (code <= 122) return true;
  1315. if (code <= 0xffff) {
  1316. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1317. }
  1318. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1319. }
  1320. const reservedWords = {
  1321. 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"],
  1322. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1323. strictBind: ["eval", "arguments"]
  1324. };
  1325. const keywords = new Set(reservedWords.keyword);
  1326. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1327. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1328. function isReservedWord(word, inModule) {
  1329. return inModule && word === "await" || word === "enum";
  1330. }
  1331. function isStrictReservedWord(word, inModule) {
  1332. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1333. }
  1334. function isStrictBindOnlyReservedWord(word) {
  1335. return reservedWordsStrictBindSet.has(word);
  1336. }
  1337. function isStrictBindReservedWord(word, inModule) {
  1338. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1339. }
  1340. function isKeyword(word) {
  1341. return keywords.has(word);
  1342. }
  1343. function isIteratorStart(current, next, next2) {
  1344. return current === 64 && next === 64 && isIdentifierStart(next2);
  1345. }
  1346. 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"]);
  1347. function canBeReservedWord(word) {
  1348. return reservedWordLikeSet.has(word);
  1349. }
  1350. const SCOPE_OTHER = 0b000000000,
  1351. SCOPE_PROGRAM = 0b000000001,
  1352. SCOPE_FUNCTION = 0b000000010,
  1353. SCOPE_ARROW = 0b000000100,
  1354. SCOPE_SIMPLE_CATCH = 0b000001000,
  1355. SCOPE_SUPER = 0b000010000,
  1356. SCOPE_DIRECT_SUPER = 0b000100000,
  1357. SCOPE_CLASS = 0b001000000,
  1358. SCOPE_STATIC_BLOCK = 0b010000000,
  1359. SCOPE_TS_MODULE = 0b100000000,
  1360. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
  1361. const BIND_KIND_VALUE = 0b000000000001,
  1362. BIND_KIND_TYPE = 0b000000000010,
  1363. BIND_SCOPE_VAR = 0b000000000100,
  1364. BIND_SCOPE_LEXICAL = 0b000000001000,
  1365. BIND_SCOPE_FUNCTION = 0b000000010000,
  1366. BIND_FLAGS_NONE = 0b000001000000,
  1367. BIND_FLAGS_CLASS = 0b000010000000,
  1368. BIND_FLAGS_TS_ENUM = 0b000100000000,
  1369. BIND_FLAGS_TS_CONST_ENUM = 0b001000000000,
  1370. BIND_FLAGS_TS_EXPORT_ONLY = 0b010000000000,
  1371. BIND_FLAGS_FLOW_DECLARE_FN = 0b100000000000;
  1372. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  1373. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1374. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1375. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1376. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1377. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1378. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  1379. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1380. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1381. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1382. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1383. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1384. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1385. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1386. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1387. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1388. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1389. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1390. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1391. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1392. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1393. CLASS_ELEMENT_OTHER = 0;
  1394. class BaseParser {
  1395. constructor() {
  1396. this.sawUnambiguousESM = false;
  1397. this.ambiguousScriptDifferentAst = false;
  1398. }
  1399. hasPlugin(pluginConfig) {
  1400. if (typeof pluginConfig === "string") {
  1401. return this.plugins.has(pluginConfig);
  1402. } else {
  1403. const [pluginName, pluginOptions] = pluginConfig;
  1404. if (!this.hasPlugin(pluginName)) {
  1405. return false;
  1406. }
  1407. const actualOptions = this.plugins.get(pluginName);
  1408. for (const key of Object.keys(pluginOptions)) {
  1409. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  1410. return false;
  1411. }
  1412. }
  1413. return true;
  1414. }
  1415. }
  1416. getPluginOption(plugin, name) {
  1417. var _this$plugins$get;
  1418. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  1419. }
  1420. }
  1421. function setTrailingComments(node, comments) {
  1422. if (node.trailingComments === undefined) {
  1423. node.trailingComments = comments;
  1424. } else {
  1425. node.trailingComments.unshift(...comments);
  1426. }
  1427. }
  1428. function setLeadingComments(node, comments) {
  1429. if (node.leadingComments === undefined) {
  1430. node.leadingComments = comments;
  1431. } else {
  1432. node.leadingComments.unshift(...comments);
  1433. }
  1434. }
  1435. function setInnerComments(node, comments) {
  1436. if (node.innerComments === undefined) {
  1437. node.innerComments = comments;
  1438. } else {
  1439. node.innerComments.unshift(...comments);
  1440. }
  1441. }
  1442. function adjustInnerComments(node, elements, commentWS) {
  1443. let lastElement = null;
  1444. let i = elements.length;
  1445. while (lastElement === null && i > 0) {
  1446. lastElement = elements[--i];
  1447. }
  1448. if (lastElement === null || lastElement.start > commentWS.start) {
  1449. setInnerComments(node, commentWS.comments);
  1450. } else {
  1451. setTrailingComments(lastElement, commentWS.comments);
  1452. }
  1453. }
  1454. class CommentsParser extends BaseParser {
  1455. addComment(comment) {
  1456. if (this.filename) comment.loc.filename = this.filename;
  1457. this.state.comments.push(comment);
  1458. }
  1459. processComment(node) {
  1460. const {
  1461. commentStack
  1462. } = this.state;
  1463. const commentStackLength = commentStack.length;
  1464. if (commentStackLength === 0) return;
  1465. let i = commentStackLength - 1;
  1466. const lastCommentWS = commentStack[i];
  1467. if (lastCommentWS.start === node.end) {
  1468. lastCommentWS.leadingNode = node;
  1469. i--;
  1470. }
  1471. const {
  1472. start: nodeStart
  1473. } = node;
  1474. for (; i >= 0; i--) {
  1475. const commentWS = commentStack[i];
  1476. const commentEnd = commentWS.end;
  1477. if (commentEnd > nodeStart) {
  1478. commentWS.containingNode = node;
  1479. this.finalizeComment(commentWS);
  1480. commentStack.splice(i, 1);
  1481. } else {
  1482. if (commentEnd === nodeStart) {
  1483. commentWS.trailingNode = node;
  1484. }
  1485. break;
  1486. }
  1487. }
  1488. }
  1489. finalizeComment(commentWS) {
  1490. const {
  1491. comments
  1492. } = commentWS;
  1493. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  1494. if (commentWS.leadingNode !== null) {
  1495. setTrailingComments(commentWS.leadingNode, comments);
  1496. }
  1497. if (commentWS.trailingNode !== null) {
  1498. setLeadingComments(commentWS.trailingNode, comments);
  1499. }
  1500. } else {
  1501. const {
  1502. containingNode: node,
  1503. start: commentStart
  1504. } = commentWS;
  1505. if (this.input.charCodeAt(commentStart - 1) === 44) {
  1506. switch (node.type) {
  1507. case "ObjectExpression":
  1508. case "ObjectPattern":
  1509. case "RecordExpression":
  1510. adjustInnerComments(node, node.properties, commentWS);
  1511. break;
  1512. case "CallExpression":
  1513. case "OptionalCallExpression":
  1514. adjustInnerComments(node, node.arguments, commentWS);
  1515. break;
  1516. case "FunctionDeclaration":
  1517. case "FunctionExpression":
  1518. case "ArrowFunctionExpression":
  1519. case "ObjectMethod":
  1520. case "ClassMethod":
  1521. case "ClassPrivateMethod":
  1522. adjustInnerComments(node, node.params, commentWS);
  1523. break;
  1524. case "ArrayExpression":
  1525. case "ArrayPattern":
  1526. case "TupleExpression":
  1527. adjustInnerComments(node, node.elements, commentWS);
  1528. break;
  1529. case "ExportNamedDeclaration":
  1530. case "ImportDeclaration":
  1531. adjustInnerComments(node, node.specifiers, commentWS);
  1532. break;
  1533. default:
  1534. {
  1535. setInnerComments(node, comments);
  1536. }
  1537. }
  1538. } else {
  1539. setInnerComments(node, comments);
  1540. }
  1541. }
  1542. }
  1543. finalizeRemainingComments() {
  1544. const {
  1545. commentStack
  1546. } = this.state;
  1547. for (let i = commentStack.length - 1; i >= 0; i--) {
  1548. this.finalizeComment(commentStack[i]);
  1549. }
  1550. this.state.commentStack = [];
  1551. }
  1552. resetPreviousNodeTrailingComments(node) {
  1553. const {
  1554. commentStack
  1555. } = this.state;
  1556. const {
  1557. length
  1558. } = commentStack;
  1559. if (length === 0) return;
  1560. const commentWS = commentStack[length - 1];
  1561. if (commentWS.leadingNode === node) {
  1562. commentWS.leadingNode = null;
  1563. }
  1564. }
  1565. takeSurroundingComments(node, start, end) {
  1566. const {
  1567. commentStack
  1568. } = this.state;
  1569. const commentStackLength = commentStack.length;
  1570. if (commentStackLength === 0) return;
  1571. let i = commentStackLength - 1;
  1572. for (; i >= 0; i--) {
  1573. const commentWS = commentStack[i];
  1574. const commentEnd = commentWS.end;
  1575. const commentStart = commentWS.start;
  1576. if (commentStart === end) {
  1577. commentWS.leadingNode = node;
  1578. } else if (commentEnd === start) {
  1579. commentWS.trailingNode = node;
  1580. } else if (commentEnd < start) {
  1581. break;
  1582. }
  1583. }
  1584. }
  1585. }
  1586. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  1587. const lineBreakG = new RegExp(lineBreak.source, "g");
  1588. function isNewLine(code) {
  1589. switch (code) {
  1590. case 10:
  1591. case 13:
  1592. case 8232:
  1593. case 8233:
  1594. return true;
  1595. default:
  1596. return false;
  1597. }
  1598. }
  1599. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  1600. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y;
  1601. const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
  1602. function isWhitespace(code) {
  1603. switch (code) {
  1604. case 0x0009:
  1605. case 0x000b:
  1606. case 0x000c:
  1607. case 32:
  1608. case 160:
  1609. case 5760:
  1610. case 0x2000:
  1611. case 0x2001:
  1612. case 0x2002:
  1613. case 0x2003:
  1614. case 0x2004:
  1615. case 0x2005:
  1616. case 0x2006:
  1617. case 0x2007:
  1618. case 0x2008:
  1619. case 0x2009:
  1620. case 0x200a:
  1621. case 0x202f:
  1622. case 0x205f:
  1623. case 0x3000:
  1624. case 0xfeff:
  1625. return true;
  1626. default:
  1627. return false;
  1628. }
  1629. }
  1630. class State {
  1631. constructor() {
  1632. this.strict = void 0;
  1633. this.curLine = void 0;
  1634. this.lineStart = void 0;
  1635. this.startLoc = void 0;
  1636. this.endLoc = void 0;
  1637. this.errors = [];
  1638. this.potentialArrowAt = -1;
  1639. this.noArrowAt = [];
  1640. this.noArrowParamsConversionAt = [];
  1641. this.maybeInArrowParameters = false;
  1642. this.inType = false;
  1643. this.noAnonFunctionType = false;
  1644. this.hasFlowComment = false;
  1645. this.isAmbientContext = false;
  1646. this.inAbstractClass = false;
  1647. this.inDisallowConditionalTypesContext = false;
  1648. this.topicContext = {
  1649. maxNumOfResolvableTopics: 0,
  1650. maxTopicIndex: null
  1651. };
  1652. this.soloAwait = false;
  1653. this.inFSharpPipelineDirectBody = false;
  1654. this.labels = [];
  1655. this.decoratorStack = [[]];
  1656. this.comments = [];
  1657. this.commentStack = [];
  1658. this.pos = 0;
  1659. this.type = 135;
  1660. this.value = null;
  1661. this.start = 0;
  1662. this.end = 0;
  1663. this.lastTokEndLoc = null;
  1664. this.lastTokStartLoc = null;
  1665. this.lastTokStart = 0;
  1666. this.context = [types.brace];
  1667. this.canStartJSXElement = true;
  1668. this.containsEsc = false;
  1669. this.strictErrors = new Map();
  1670. this.tokensLength = 0;
  1671. }
  1672. init({
  1673. strictMode,
  1674. sourceType,
  1675. startLine,
  1676. startColumn
  1677. }) {
  1678. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  1679. this.curLine = startLine;
  1680. this.lineStart = -startColumn;
  1681. this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
  1682. }
  1683. curPosition() {
  1684. return new Position(this.curLine, this.pos - this.lineStart, this.pos);
  1685. }
  1686. clone(skipArrays) {
  1687. const state = new State();
  1688. const keys = Object.keys(this);
  1689. for (let i = 0, length = keys.length; i < length; i++) {
  1690. const key = keys[i];
  1691. let val = this[key];
  1692. if (!skipArrays && Array.isArray(val)) {
  1693. val = val.slice();
  1694. }
  1695. state[key] = val;
  1696. }
  1697. return state;
  1698. }
  1699. }
  1700. var _isDigit = function isDigit(code) {
  1701. return code >= 48 && code <= 57;
  1702. };
  1703. const forbiddenNumericSeparatorSiblings = {
  1704. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  1705. hex: new Set([46, 88, 95, 120])
  1706. };
  1707. const isAllowedNumericSeparatorSibling = {
  1708. bin: ch => ch === 48 || ch === 49,
  1709. oct: ch => ch >= 48 && ch <= 55,
  1710. dec: ch => ch >= 48 && ch <= 57,
  1711. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  1712. };
  1713. function readStringContents(type, input, pos, lineStart, curLine, errors) {
  1714. const initialPos = pos;
  1715. const initialLineStart = lineStart;
  1716. const initialCurLine = curLine;
  1717. let out = "";
  1718. let containsInvalid = false;
  1719. let chunkStart = pos;
  1720. const {
  1721. length
  1722. } = input;
  1723. for (;;) {
  1724. if (pos >= length) {
  1725. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1726. out += input.slice(chunkStart, pos);
  1727. break;
  1728. }
  1729. const ch = input.charCodeAt(pos);
  1730. if (isStringEnd(type, ch, input, pos)) {
  1731. out += input.slice(chunkStart, pos);
  1732. break;
  1733. }
  1734. if (ch === 92) {
  1735. out += input.slice(chunkStart, pos);
  1736. let escaped;
  1737. ({
  1738. ch: escaped,
  1739. pos,
  1740. lineStart,
  1741. curLine
  1742. } = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors));
  1743. if (escaped === null) {
  1744. containsInvalid = true;
  1745. } else {
  1746. out += escaped;
  1747. }
  1748. chunkStart = pos;
  1749. } else if (ch === 8232 || ch === 8233) {
  1750. ++pos;
  1751. ++curLine;
  1752. lineStart = pos;
  1753. } else if (ch === 10 || ch === 13) {
  1754. if (type === "template") {
  1755. out += input.slice(chunkStart, pos) + "\n";
  1756. ++pos;
  1757. if (ch === 13 && input.charCodeAt(pos) === 10) {
  1758. ++pos;
  1759. }
  1760. ++curLine;
  1761. chunkStart = lineStart = pos;
  1762. } else {
  1763. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1764. }
  1765. } else {
  1766. ++pos;
  1767. }
  1768. }
  1769. return {
  1770. pos,
  1771. str: out,
  1772. containsInvalid,
  1773. lineStart,
  1774. curLine
  1775. };
  1776. }
  1777. function isStringEnd(type, ch, input, pos) {
  1778. if (type === "template") {
  1779. return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
  1780. }
  1781. return ch === (type === "double" ? 34 : 39);
  1782. }
  1783. function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
  1784. const throwOnInvalid = !inTemplate;
  1785. pos++;
  1786. const res = ch => ({
  1787. pos,
  1788. ch,
  1789. lineStart,
  1790. curLine
  1791. });
  1792. const ch = input.charCodeAt(pos++);
  1793. switch (ch) {
  1794. case 110:
  1795. return res("\n");
  1796. case 114:
  1797. return res("\r");
  1798. case 120:
  1799. {
  1800. let code;
  1801. ({
  1802. code,
  1803. pos
  1804. } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
  1805. return res(code === null ? null : String.fromCharCode(code));
  1806. }
  1807. case 117:
  1808. {
  1809. let code;
  1810. ({
  1811. code,
  1812. pos
  1813. } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
  1814. return res(code === null ? null : String.fromCodePoint(code));
  1815. }
  1816. case 116:
  1817. return res("\t");
  1818. case 98:
  1819. return res("\b");
  1820. case 118:
  1821. return res("\u000b");
  1822. case 102:
  1823. return res("\f");
  1824. case 13:
  1825. if (input.charCodeAt(pos) === 10) {
  1826. ++pos;
  1827. }
  1828. case 10:
  1829. lineStart = pos;
  1830. ++curLine;
  1831. case 8232:
  1832. case 8233:
  1833. return res("");
  1834. case 56:
  1835. case 57:
  1836. if (inTemplate) {
  1837. return res(null);
  1838. } else {
  1839. errors.strictNumericEscape(pos - 1, lineStart, curLine);
  1840. }
  1841. default:
  1842. if (ch >= 48 && ch <= 55) {
  1843. const startPos = pos - 1;
  1844. const match = input.slice(startPos, pos + 2).match(/^[0-7]+/);
  1845. let octalStr = match[0];
  1846. let octal = parseInt(octalStr, 8);
  1847. if (octal > 255) {
  1848. octalStr = octalStr.slice(0, -1);
  1849. octal = parseInt(octalStr, 8);
  1850. }
  1851. pos += octalStr.length - 1;
  1852. const next = input.charCodeAt(pos);
  1853. if (octalStr !== "0" || next === 56 || next === 57) {
  1854. if (inTemplate) {
  1855. return res(null);
  1856. } else {
  1857. errors.strictNumericEscape(startPos, lineStart, curLine);
  1858. }
  1859. }
  1860. return res(String.fromCharCode(octal));
  1861. }
  1862. return res(String.fromCharCode(ch));
  1863. }
  1864. }
  1865. function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
  1866. const initialPos = pos;
  1867. let n;
  1868. ({
  1869. n,
  1870. pos
  1871. } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors));
  1872. if (n === null) {
  1873. if (throwOnInvalid) {
  1874. errors.invalidEscapeSequence(initialPos, lineStart, curLine);
  1875. } else {
  1876. pos = initialPos - 1;
  1877. }
  1878. }
  1879. return {
  1880. code: n,
  1881. pos
  1882. };
  1883. }
  1884. function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors) {
  1885. const start = pos;
  1886. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  1887. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  1888. let invalid = false;
  1889. let total = 0;
  1890. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  1891. const code = input.charCodeAt(pos);
  1892. let val;
  1893. if (code === 95 && allowNumSeparator !== "bail") {
  1894. const prev = input.charCodeAt(pos - 1);
  1895. const next = input.charCodeAt(pos + 1);
  1896. if (!allowNumSeparator) {
  1897. errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
  1898. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  1899. errors.unexpectedNumericSeparator(pos, lineStart, curLine);
  1900. }
  1901. ++pos;
  1902. continue;
  1903. }
  1904. if (code >= 97) {
  1905. val = code - 97 + 10;
  1906. } else if (code >= 65) {
  1907. val = code - 65 + 10;
  1908. } else if (_isDigit(code)) {
  1909. val = code - 48;
  1910. } else {
  1911. val = Infinity;
  1912. }
  1913. if (val >= radix) {
  1914. if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
  1915. val = 0;
  1916. } else if (forceLen) {
  1917. val = 0;
  1918. invalid = true;
  1919. } else {
  1920. break;
  1921. }
  1922. }
  1923. ++pos;
  1924. total = total * radix + val;
  1925. }
  1926. if (pos === start || len != null && pos - start !== len || invalid) {
  1927. return {
  1928. n: null,
  1929. pos
  1930. };
  1931. }
  1932. return {
  1933. n: total,
  1934. pos
  1935. };
  1936. }
  1937. function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
  1938. const ch = input.charCodeAt(pos);
  1939. let code;
  1940. if (ch === 123) {
  1941. ++pos;
  1942. ({
  1943. code,
  1944. pos
  1945. } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
  1946. ++pos;
  1947. if (code !== null && code > 0x10ffff) {
  1948. if (throwOnInvalid) {
  1949. errors.invalidCodePoint(pos, lineStart, curLine);
  1950. } else {
  1951. return {
  1952. code: null,
  1953. pos
  1954. };
  1955. }
  1956. }
  1957. } else {
  1958. ({
  1959. code,
  1960. pos
  1961. } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
  1962. }
  1963. return {
  1964. code,
  1965. pos
  1966. };
  1967. }
  1968. const _excluded = ["at"],
  1969. _excluded2 = ["at"];
  1970. function buildPosition(pos, lineStart, curLine) {
  1971. return new Position(curLine, pos - lineStart, pos);
  1972. }
  1973. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
  1974. class Token {
  1975. constructor(state) {
  1976. this.type = state.type;
  1977. this.value = state.value;
  1978. this.start = state.start;
  1979. this.end = state.end;
  1980. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  1981. }
  1982. }
  1983. class Tokenizer extends CommentsParser {
  1984. constructor(options, input) {
  1985. super();
  1986. this.isLookahead = void 0;
  1987. this.tokens = [];
  1988. this.errorHandlers_readInt = {
  1989. invalidDigit: (pos, lineStart, curLine, radix) => {
  1990. if (!this.options.errorRecovery) return false;
  1991. this.raise(Errors.InvalidDigit, {
  1992. at: buildPosition(pos, lineStart, curLine),
  1993. radix
  1994. });
  1995. return true;
  1996. },
  1997. numericSeparatorInEscapeSequence: this.errorBuilder(Errors.NumericSeparatorInEscapeSequence),
  1998. unexpectedNumericSeparator: this.errorBuilder(Errors.UnexpectedNumericSeparator)
  1999. };
  2000. this.errorHandlers_readCodePoint = Object.assign({}, this.errorHandlers_readInt, {
  2001. invalidEscapeSequence: this.errorBuilder(Errors.InvalidEscapeSequence),
  2002. invalidCodePoint: this.errorBuilder(Errors.InvalidCodePoint)
  2003. });
  2004. this.errorHandlers_readStringContents_string = Object.assign({}, this.errorHandlers_readCodePoint, {
  2005. strictNumericEscape: (pos, lineStart, curLine) => {
  2006. this.recordStrictModeErrors(Errors.StrictNumericEscape, {
  2007. at: buildPosition(pos, lineStart, curLine)
  2008. });
  2009. },
  2010. unterminated: (pos, lineStart, curLine) => {
  2011. throw this.raise(Errors.UnterminatedString, {
  2012. at: buildPosition(pos - 1, lineStart, curLine)
  2013. });
  2014. }
  2015. });
  2016. this.errorHandlers_readStringContents_template = Object.assign({}, this.errorHandlers_readCodePoint, {
  2017. strictNumericEscape: this.errorBuilder(Errors.StrictNumericEscape),
  2018. unterminated: (pos, lineStart, curLine) => {
  2019. throw this.raise(Errors.UnterminatedTemplate, {
  2020. at: buildPosition(pos, lineStart, curLine)
  2021. });
  2022. }
  2023. });
  2024. this.state = new State();
  2025. this.state.init(options);
  2026. this.input = input;
  2027. this.length = input.length;
  2028. this.isLookahead = false;
  2029. }
  2030. pushToken(token) {
  2031. this.tokens.length = this.state.tokensLength;
  2032. this.tokens.push(token);
  2033. ++this.state.tokensLength;
  2034. }
  2035. next() {
  2036. this.checkKeywordEscapes();
  2037. if (this.options.tokens) {
  2038. this.pushToken(new Token(this.state));
  2039. }
  2040. this.state.lastTokStart = this.state.start;
  2041. this.state.lastTokEndLoc = this.state.endLoc;
  2042. this.state.lastTokStartLoc = this.state.startLoc;
  2043. this.nextToken();
  2044. }
  2045. eat(type) {
  2046. if (this.match(type)) {
  2047. this.next();
  2048. return true;
  2049. } else {
  2050. return false;
  2051. }
  2052. }
  2053. match(type) {
  2054. return this.state.type === type;
  2055. }
  2056. createLookaheadState(state) {
  2057. return {
  2058. pos: state.pos,
  2059. value: null,
  2060. type: state.type,
  2061. start: state.start,
  2062. end: state.end,
  2063. context: [this.curContext()],
  2064. inType: state.inType,
  2065. startLoc: state.startLoc,
  2066. lastTokEndLoc: state.lastTokEndLoc,
  2067. curLine: state.curLine,
  2068. lineStart: state.lineStart,
  2069. curPosition: state.curPosition
  2070. };
  2071. }
  2072. lookahead() {
  2073. const old = this.state;
  2074. this.state = this.createLookaheadState(old);
  2075. this.isLookahead = true;
  2076. this.nextToken();
  2077. this.isLookahead = false;
  2078. const curr = this.state;
  2079. this.state = old;
  2080. return curr;
  2081. }
  2082. nextTokenStart() {
  2083. return this.nextTokenStartSince(this.state.pos);
  2084. }
  2085. nextTokenStartSince(pos) {
  2086. skipWhiteSpace.lastIndex = pos;
  2087. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  2088. }
  2089. lookaheadCharCode() {
  2090. return this.input.charCodeAt(this.nextTokenStart());
  2091. }
  2092. codePointAtPos(pos) {
  2093. let cp = this.input.charCodeAt(pos);
  2094. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  2095. const trail = this.input.charCodeAt(pos);
  2096. if ((trail & 0xfc00) === 0xdc00) {
  2097. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  2098. }
  2099. }
  2100. return cp;
  2101. }
  2102. setStrict(strict) {
  2103. this.state.strict = strict;
  2104. if (strict) {
  2105. this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, {
  2106. at
  2107. }));
  2108. this.state.strictErrors.clear();
  2109. }
  2110. }
  2111. curContext() {
  2112. return this.state.context[this.state.context.length - 1];
  2113. }
  2114. nextToken() {
  2115. this.skipSpace();
  2116. this.state.start = this.state.pos;
  2117. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  2118. if (this.state.pos >= this.length) {
  2119. this.finishToken(135);
  2120. return;
  2121. }
  2122. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  2123. }
  2124. skipBlockComment() {
  2125. let startLoc;
  2126. if (!this.isLookahead) startLoc = this.state.curPosition();
  2127. const start = this.state.pos;
  2128. const end = this.input.indexOf("*/", start + 2);
  2129. if (end === -1) {
  2130. throw this.raise(Errors.UnterminatedComment, {
  2131. at: this.state.curPosition()
  2132. });
  2133. }
  2134. this.state.pos = end + 2;
  2135. lineBreakG.lastIndex = start + 2;
  2136. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  2137. ++this.state.curLine;
  2138. this.state.lineStart = lineBreakG.lastIndex;
  2139. }
  2140. if (this.isLookahead) return;
  2141. const comment = {
  2142. type: "CommentBlock",
  2143. value: this.input.slice(start + 2, end),
  2144. start,
  2145. end: end + 2,
  2146. loc: new SourceLocation(startLoc, this.state.curPosition())
  2147. };
  2148. if (this.options.tokens) this.pushToken(comment);
  2149. return comment;
  2150. }
  2151. skipLineComment(startSkip) {
  2152. const start = this.state.pos;
  2153. let startLoc;
  2154. if (!this.isLookahead) startLoc = this.state.curPosition();
  2155. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  2156. if (this.state.pos < this.length) {
  2157. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2158. ch = this.input.charCodeAt(this.state.pos);
  2159. }
  2160. }
  2161. if (this.isLookahead) return;
  2162. const end = this.state.pos;
  2163. const value = this.input.slice(start + startSkip, end);
  2164. const comment = {
  2165. type: "CommentLine",
  2166. value,
  2167. start,
  2168. end,
  2169. loc: new SourceLocation(startLoc, this.state.curPosition())
  2170. };
  2171. if (this.options.tokens) this.pushToken(comment);
  2172. return comment;
  2173. }
  2174. skipSpace() {
  2175. const spaceStart = this.state.pos;
  2176. const comments = [];
  2177. loop: while (this.state.pos < this.length) {
  2178. const ch = this.input.charCodeAt(this.state.pos);
  2179. switch (ch) {
  2180. case 32:
  2181. case 160:
  2182. case 9:
  2183. ++this.state.pos;
  2184. break;
  2185. case 13:
  2186. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  2187. ++this.state.pos;
  2188. }
  2189. case 10:
  2190. case 8232:
  2191. case 8233:
  2192. ++this.state.pos;
  2193. ++this.state.curLine;
  2194. this.state.lineStart = this.state.pos;
  2195. break;
  2196. case 47:
  2197. switch (this.input.charCodeAt(this.state.pos + 1)) {
  2198. case 42:
  2199. {
  2200. const comment = this.skipBlockComment();
  2201. if (comment !== undefined) {
  2202. this.addComment(comment);
  2203. if (this.options.attachComment) comments.push(comment);
  2204. }
  2205. break;
  2206. }
  2207. case 47:
  2208. {
  2209. const comment = this.skipLineComment(2);
  2210. if (comment !== undefined) {
  2211. this.addComment(comment);
  2212. if (this.options.attachComment) comments.push(comment);
  2213. }
  2214. break;
  2215. }
  2216. default:
  2217. break loop;
  2218. }
  2219. break;
  2220. default:
  2221. if (isWhitespace(ch)) {
  2222. ++this.state.pos;
  2223. } else if (ch === 45 && !this.inModule) {
  2224. const pos = this.state.pos;
  2225. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  2226. const comment = this.skipLineComment(3);
  2227. if (comment !== undefined) {
  2228. this.addComment(comment);
  2229. if (this.options.attachComment) comments.push(comment);
  2230. }
  2231. } else {
  2232. break loop;
  2233. }
  2234. } else if (ch === 60 && !this.inModule) {
  2235. const pos = this.state.pos;
  2236. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  2237. const comment = this.skipLineComment(4);
  2238. if (comment !== undefined) {
  2239. this.addComment(comment);
  2240. if (this.options.attachComment) comments.push(comment);
  2241. }
  2242. } else {
  2243. break loop;
  2244. }
  2245. } else {
  2246. break loop;
  2247. }
  2248. }
  2249. }
  2250. if (comments.length > 0) {
  2251. const end = this.state.pos;
  2252. const commentWhitespace = {
  2253. start: spaceStart,
  2254. end,
  2255. comments,
  2256. leadingNode: null,
  2257. trailingNode: null,
  2258. containingNode: null
  2259. };
  2260. this.state.commentStack.push(commentWhitespace);
  2261. }
  2262. }
  2263. finishToken(type, val) {
  2264. this.state.end = this.state.pos;
  2265. this.state.endLoc = this.state.curPosition();
  2266. const prevType = this.state.type;
  2267. this.state.type = type;
  2268. this.state.value = val;
  2269. if (!this.isLookahead) {
  2270. this.updateContext(prevType);
  2271. }
  2272. }
  2273. replaceToken(type) {
  2274. this.state.type = type;
  2275. this.updateContext();
  2276. }
  2277. readToken_numberSign() {
  2278. if (this.state.pos === 0 && this.readToken_interpreter()) {
  2279. return;
  2280. }
  2281. const nextPos = this.state.pos + 1;
  2282. const next = this.codePointAtPos(nextPos);
  2283. if (next >= 48 && next <= 57) {
  2284. throw this.raise(Errors.UnexpectedDigitAfterHash, {
  2285. at: this.state.curPosition()
  2286. });
  2287. }
  2288. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  2289. this.expectPlugin("recordAndTuple");
  2290. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
  2291. throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, {
  2292. at: this.state.curPosition()
  2293. });
  2294. }
  2295. this.state.pos += 2;
  2296. if (next === 123) {
  2297. this.finishToken(7);
  2298. } else {
  2299. this.finishToken(1);
  2300. }
  2301. } else if (isIdentifierStart(next)) {
  2302. ++this.state.pos;
  2303. this.finishToken(134, this.readWord1(next));
  2304. } else if (next === 92) {
  2305. ++this.state.pos;
  2306. this.finishToken(134, this.readWord1());
  2307. } else {
  2308. this.finishOp(27, 1);
  2309. }
  2310. }
  2311. readToken_dot() {
  2312. const next = this.input.charCodeAt(this.state.pos + 1);
  2313. if (next >= 48 && next <= 57) {
  2314. this.readNumber(true);
  2315. return;
  2316. }
  2317. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  2318. this.state.pos += 3;
  2319. this.finishToken(21);
  2320. } else {
  2321. ++this.state.pos;
  2322. this.finishToken(16);
  2323. }
  2324. }
  2325. readToken_slash() {
  2326. const next = this.input.charCodeAt(this.state.pos + 1);
  2327. if (next === 61) {
  2328. this.finishOp(31, 2);
  2329. } else {
  2330. this.finishOp(56, 1);
  2331. }
  2332. }
  2333. readToken_interpreter() {
  2334. if (this.state.pos !== 0 || this.length < 2) return false;
  2335. let ch = this.input.charCodeAt(this.state.pos + 1);
  2336. if (ch !== 33) return false;
  2337. const start = this.state.pos;
  2338. this.state.pos += 1;
  2339. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2340. ch = this.input.charCodeAt(this.state.pos);
  2341. }
  2342. const value = this.input.slice(start + 2, this.state.pos);
  2343. this.finishToken(28, value);
  2344. return true;
  2345. }
  2346. readToken_mult_modulo(code) {
  2347. let type = code === 42 ? 55 : 54;
  2348. let width = 1;
  2349. let next = this.input.charCodeAt(this.state.pos + 1);
  2350. if (code === 42 && next === 42) {
  2351. width++;
  2352. next = this.input.charCodeAt(this.state.pos + 2);
  2353. type = 57;
  2354. }
  2355. if (next === 61 && !this.state.inType) {
  2356. width++;
  2357. type = code === 37 ? 33 : 30;
  2358. }
  2359. this.finishOp(type, width);
  2360. }
  2361. readToken_pipe_amp(code) {
  2362. const next = this.input.charCodeAt(this.state.pos + 1);
  2363. if (next === code) {
  2364. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  2365. this.finishOp(30, 3);
  2366. } else {
  2367. this.finishOp(code === 124 ? 41 : 42, 2);
  2368. }
  2369. return;
  2370. }
  2371. if (code === 124) {
  2372. if (next === 62) {
  2373. this.finishOp(39, 2);
  2374. return;
  2375. }
  2376. if (this.hasPlugin("recordAndTuple") && next === 125) {
  2377. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2378. throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, {
  2379. at: this.state.curPosition()
  2380. });
  2381. }
  2382. this.state.pos += 2;
  2383. this.finishToken(9);
  2384. return;
  2385. }
  2386. if (this.hasPlugin("recordAndTuple") && next === 93) {
  2387. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2388. throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, {
  2389. at: this.state.curPosition()
  2390. });
  2391. }
  2392. this.state.pos += 2;
  2393. this.finishToken(4);
  2394. return;
  2395. }
  2396. }
  2397. if (next === 61) {
  2398. this.finishOp(30, 2);
  2399. return;
  2400. }
  2401. this.finishOp(code === 124 ? 43 : 45, 1);
  2402. }
  2403. readToken_caret() {
  2404. const next = this.input.charCodeAt(this.state.pos + 1);
  2405. if (next === 61 && !this.state.inType) {
  2406. this.finishOp(32, 2);
  2407. } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
  2408. proposal: "hack",
  2409. topicToken: "^^"
  2410. }])) {
  2411. this.finishOp(37, 2);
  2412. const lookaheadCh = this.input.codePointAt(this.state.pos);
  2413. if (lookaheadCh === 94) {
  2414. throw this.unexpected();
  2415. }
  2416. } else {
  2417. this.finishOp(44, 1);
  2418. }
  2419. }
  2420. readToken_atSign() {
  2421. const next = this.input.charCodeAt(this.state.pos + 1);
  2422. if (next === 64 && this.hasPlugin(["pipelineOperator", {
  2423. proposal: "hack",
  2424. topicToken: "@@"
  2425. }])) {
  2426. this.finishOp(38, 2);
  2427. } else {
  2428. this.finishOp(26, 1);
  2429. }
  2430. }
  2431. readToken_plus_min(code) {
  2432. const next = this.input.charCodeAt(this.state.pos + 1);
  2433. if (next === code) {
  2434. this.finishOp(34, 2);
  2435. return;
  2436. }
  2437. if (next === 61) {
  2438. this.finishOp(30, 2);
  2439. } else {
  2440. this.finishOp(53, 1);
  2441. }
  2442. }
  2443. readToken_lt() {
  2444. const {
  2445. pos
  2446. } = this.state;
  2447. const next = this.input.charCodeAt(pos + 1);
  2448. if (next === 60) {
  2449. if (this.input.charCodeAt(pos + 2) === 61) {
  2450. this.finishOp(30, 3);
  2451. return;
  2452. }
  2453. this.finishOp(51, 2);
  2454. return;
  2455. }
  2456. if (next === 61) {
  2457. this.finishOp(49, 2);
  2458. return;
  2459. }
  2460. this.finishOp(47, 1);
  2461. }
  2462. readToken_gt() {
  2463. const {
  2464. pos
  2465. } = this.state;
  2466. const next = this.input.charCodeAt(pos + 1);
  2467. if (next === 62) {
  2468. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  2469. if (this.input.charCodeAt(pos + size) === 61) {
  2470. this.finishOp(30, size + 1);
  2471. return;
  2472. }
  2473. this.finishOp(52, size);
  2474. return;
  2475. }
  2476. if (next === 61) {
  2477. this.finishOp(49, 2);
  2478. return;
  2479. }
  2480. this.finishOp(48, 1);
  2481. }
  2482. readToken_eq_excl(code) {
  2483. const next = this.input.charCodeAt(this.state.pos + 1);
  2484. if (next === 61) {
  2485. this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  2486. return;
  2487. }
  2488. if (code === 61 && next === 62) {
  2489. this.state.pos += 2;
  2490. this.finishToken(19);
  2491. return;
  2492. }
  2493. this.finishOp(code === 61 ? 29 : 35, 1);
  2494. }
  2495. readToken_question() {
  2496. const next = this.input.charCodeAt(this.state.pos + 1);
  2497. const next2 = this.input.charCodeAt(this.state.pos + 2);
  2498. if (next === 63) {
  2499. if (next2 === 61) {
  2500. this.finishOp(30, 3);
  2501. } else {
  2502. this.finishOp(40, 2);
  2503. }
  2504. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  2505. this.state.pos += 2;
  2506. this.finishToken(18);
  2507. } else {
  2508. ++this.state.pos;
  2509. this.finishToken(17);
  2510. }
  2511. }
  2512. getTokenFromCode(code) {
  2513. switch (code) {
  2514. case 46:
  2515. this.readToken_dot();
  2516. return;
  2517. case 40:
  2518. ++this.state.pos;
  2519. this.finishToken(10);
  2520. return;
  2521. case 41:
  2522. ++this.state.pos;
  2523. this.finishToken(11);
  2524. return;
  2525. case 59:
  2526. ++this.state.pos;
  2527. this.finishToken(13);
  2528. return;
  2529. case 44:
  2530. ++this.state.pos;
  2531. this.finishToken(12);
  2532. return;
  2533. case 91:
  2534. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2535. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2536. throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, {
  2537. at: this.state.curPosition()
  2538. });
  2539. }
  2540. this.state.pos += 2;
  2541. this.finishToken(2);
  2542. } else {
  2543. ++this.state.pos;
  2544. this.finishToken(0);
  2545. }
  2546. return;
  2547. case 93:
  2548. ++this.state.pos;
  2549. this.finishToken(3);
  2550. return;
  2551. case 123:
  2552. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2553. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2554. throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, {
  2555. at: this.state.curPosition()
  2556. });
  2557. }
  2558. this.state.pos += 2;
  2559. this.finishToken(6);
  2560. } else {
  2561. ++this.state.pos;
  2562. this.finishToken(5);
  2563. }
  2564. return;
  2565. case 125:
  2566. ++this.state.pos;
  2567. this.finishToken(8);
  2568. return;
  2569. case 58:
  2570. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  2571. this.finishOp(15, 2);
  2572. } else {
  2573. ++this.state.pos;
  2574. this.finishToken(14);
  2575. }
  2576. return;
  2577. case 63:
  2578. this.readToken_question();
  2579. return;
  2580. case 96:
  2581. this.readTemplateToken();
  2582. return;
  2583. case 48:
  2584. {
  2585. const next = this.input.charCodeAt(this.state.pos + 1);
  2586. if (next === 120 || next === 88) {
  2587. this.readRadixNumber(16);
  2588. return;
  2589. }
  2590. if (next === 111 || next === 79) {
  2591. this.readRadixNumber(8);
  2592. return;
  2593. }
  2594. if (next === 98 || next === 66) {
  2595. this.readRadixNumber(2);
  2596. return;
  2597. }
  2598. }
  2599. case 49:
  2600. case 50:
  2601. case 51:
  2602. case 52:
  2603. case 53:
  2604. case 54:
  2605. case 55:
  2606. case 56:
  2607. case 57:
  2608. this.readNumber(false);
  2609. return;
  2610. case 34:
  2611. case 39:
  2612. this.readString(code);
  2613. return;
  2614. case 47:
  2615. this.readToken_slash();
  2616. return;
  2617. case 37:
  2618. case 42:
  2619. this.readToken_mult_modulo(code);
  2620. return;
  2621. case 124:
  2622. case 38:
  2623. this.readToken_pipe_amp(code);
  2624. return;
  2625. case 94:
  2626. this.readToken_caret();
  2627. return;
  2628. case 43:
  2629. case 45:
  2630. this.readToken_plus_min(code);
  2631. return;
  2632. case 60:
  2633. this.readToken_lt();
  2634. return;
  2635. case 62:
  2636. this.readToken_gt();
  2637. return;
  2638. case 61:
  2639. case 33:
  2640. this.readToken_eq_excl(code);
  2641. return;
  2642. case 126:
  2643. this.finishOp(36, 1);
  2644. return;
  2645. case 64:
  2646. this.readToken_atSign();
  2647. return;
  2648. case 35:
  2649. this.readToken_numberSign();
  2650. return;
  2651. case 92:
  2652. this.readWord();
  2653. return;
  2654. default:
  2655. if (isIdentifierStart(code)) {
  2656. this.readWord(code);
  2657. return;
  2658. }
  2659. }
  2660. throw this.raise(Errors.InvalidOrUnexpectedToken, {
  2661. at: this.state.curPosition(),
  2662. unexpected: String.fromCodePoint(code)
  2663. });
  2664. }
  2665. finishOp(type, size) {
  2666. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2667. this.state.pos += size;
  2668. this.finishToken(type, str);
  2669. }
  2670. readRegexp() {
  2671. const startLoc = this.state.startLoc;
  2672. const start = this.state.start + 1;
  2673. let escaped, inClass;
  2674. let {
  2675. pos
  2676. } = this.state;
  2677. for (;; ++pos) {
  2678. if (pos >= this.length) {
  2679. throw this.raise(Errors.UnterminatedRegExp, {
  2680. at: createPositionWithColumnOffset(startLoc, 1)
  2681. });
  2682. }
  2683. const ch = this.input.charCodeAt(pos);
  2684. if (isNewLine(ch)) {
  2685. throw this.raise(Errors.UnterminatedRegExp, {
  2686. at: createPositionWithColumnOffset(startLoc, 1)
  2687. });
  2688. }
  2689. if (escaped) {
  2690. escaped = false;
  2691. } else {
  2692. if (ch === 91) {
  2693. inClass = true;
  2694. } else if (ch === 93 && inClass) {
  2695. inClass = false;
  2696. } else if (ch === 47 && !inClass) {
  2697. break;
  2698. }
  2699. escaped = ch === 92;
  2700. }
  2701. }
  2702. const content = this.input.slice(start, pos);
  2703. ++pos;
  2704. let mods = "";
  2705. const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
  2706. while (pos < this.length) {
  2707. const cp = this.codePointAtPos(pos);
  2708. const char = String.fromCharCode(cp);
  2709. if (VALID_REGEX_FLAGS.has(cp)) {
  2710. if (cp === 118) {
  2711. this.expectPlugin("regexpUnicodeSets", nextPos());
  2712. if (mods.includes("u")) {
  2713. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2714. at: nextPos()
  2715. });
  2716. }
  2717. } else if (cp === 117) {
  2718. if (mods.includes("v")) {
  2719. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2720. at: nextPos()
  2721. });
  2722. }
  2723. }
  2724. if (mods.includes(char)) {
  2725. this.raise(Errors.DuplicateRegExpFlags, {
  2726. at: nextPos()
  2727. });
  2728. }
  2729. } else if (isIdentifierChar(cp) || cp === 92) {
  2730. this.raise(Errors.MalformedRegExpFlags, {
  2731. at: nextPos()
  2732. });
  2733. } else {
  2734. break;
  2735. }
  2736. ++pos;
  2737. mods += char;
  2738. }
  2739. this.state.pos = pos;
  2740. this.finishToken(133, {
  2741. pattern: content,
  2742. flags: mods
  2743. });
  2744. }
  2745. readInt(radix, len, forceLen = false, allowNumSeparator = true) {
  2746. const {
  2747. n,
  2748. pos
  2749. } = readInt(this.input, this.state.pos, this.state.lineStart, this.state.curLine, radix, len, forceLen, allowNumSeparator, this.errorHandlers_readInt);
  2750. this.state.pos = pos;
  2751. return n;
  2752. }
  2753. readRadixNumber(radix) {
  2754. const startLoc = this.state.curPosition();
  2755. let isBigInt = false;
  2756. this.state.pos += 2;
  2757. const val = this.readInt(radix);
  2758. if (val == null) {
  2759. this.raise(Errors.InvalidDigit, {
  2760. at: createPositionWithColumnOffset(startLoc, 2),
  2761. radix
  2762. });
  2763. }
  2764. const next = this.input.charCodeAt(this.state.pos);
  2765. if (next === 110) {
  2766. ++this.state.pos;
  2767. isBigInt = true;
  2768. } else if (next === 109) {
  2769. throw this.raise(Errors.InvalidDecimal, {
  2770. at: startLoc
  2771. });
  2772. }
  2773. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  2774. throw this.raise(Errors.NumberIdentifier, {
  2775. at: this.state.curPosition()
  2776. });
  2777. }
  2778. if (isBigInt) {
  2779. const str = this.input.slice(startLoc.index, this.state.pos).replace(/[_n]/g, "");
  2780. this.finishToken(131, str);
  2781. return;
  2782. }
  2783. this.finishToken(130, val);
  2784. }
  2785. readNumber(startsWithDot) {
  2786. const start = this.state.pos;
  2787. const startLoc = this.state.curPosition();
  2788. let isFloat = false;
  2789. let isBigInt = false;
  2790. let isDecimal = false;
  2791. let hasExponent = false;
  2792. let isOctal = false;
  2793. if (!startsWithDot && this.readInt(10) === null) {
  2794. this.raise(Errors.InvalidNumber, {
  2795. at: this.state.curPosition()
  2796. });
  2797. }
  2798. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  2799. if (hasLeadingZero) {
  2800. const integer = this.input.slice(start, this.state.pos);
  2801. this.recordStrictModeErrors(Errors.StrictOctalLiteral, {
  2802. at: startLoc
  2803. });
  2804. if (!this.state.strict) {
  2805. const underscorePos = integer.indexOf("_");
  2806. if (underscorePos > 0) {
  2807. this.raise(Errors.ZeroDigitNumericSeparator, {
  2808. at: createPositionWithColumnOffset(startLoc, underscorePos)
  2809. });
  2810. }
  2811. }
  2812. isOctal = hasLeadingZero && !/[89]/.test(integer);
  2813. }
  2814. let next = this.input.charCodeAt(this.state.pos);
  2815. if (next === 46 && !isOctal) {
  2816. ++this.state.pos;
  2817. this.readInt(10);
  2818. isFloat = true;
  2819. next = this.input.charCodeAt(this.state.pos);
  2820. }
  2821. if ((next === 69 || next === 101) && !isOctal) {
  2822. next = this.input.charCodeAt(++this.state.pos);
  2823. if (next === 43 || next === 45) {
  2824. ++this.state.pos;
  2825. }
  2826. if (this.readInt(10) === null) {
  2827. this.raise(Errors.InvalidOrMissingExponent, {
  2828. at: startLoc
  2829. });
  2830. }
  2831. isFloat = true;
  2832. hasExponent = true;
  2833. next = this.input.charCodeAt(this.state.pos);
  2834. }
  2835. if (next === 110) {
  2836. if (isFloat || hasLeadingZero) {
  2837. this.raise(Errors.InvalidBigIntLiteral, {
  2838. at: startLoc
  2839. });
  2840. }
  2841. ++this.state.pos;
  2842. isBigInt = true;
  2843. }
  2844. if (next === 109) {
  2845. this.expectPlugin("decimal", this.state.curPosition());
  2846. if (hasExponent || hasLeadingZero) {
  2847. this.raise(Errors.InvalidDecimal, {
  2848. at: startLoc
  2849. });
  2850. }
  2851. ++this.state.pos;
  2852. isDecimal = true;
  2853. }
  2854. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  2855. throw this.raise(Errors.NumberIdentifier, {
  2856. at: this.state.curPosition()
  2857. });
  2858. }
  2859. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  2860. if (isBigInt) {
  2861. this.finishToken(131, str);
  2862. return;
  2863. }
  2864. if (isDecimal) {
  2865. this.finishToken(132, str);
  2866. return;
  2867. }
  2868. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  2869. this.finishToken(130, val);
  2870. }
  2871. readCodePoint(throwOnInvalid) {
  2872. const {
  2873. code,
  2874. pos
  2875. } = readCodePoint(this.input, this.state.pos, this.state.lineStart, this.state.curLine, throwOnInvalid, this.errorHandlers_readCodePoint);
  2876. this.state.pos = pos;
  2877. return code;
  2878. }
  2879. readString(quote) {
  2880. const {
  2881. str,
  2882. pos,
  2883. curLine,
  2884. lineStart
  2885. } = readStringContents(quote === 34 ? "double" : "single", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_string);
  2886. this.state.pos = pos + 1;
  2887. this.state.lineStart = lineStart;
  2888. this.state.curLine = curLine;
  2889. this.finishToken(129, str);
  2890. }
  2891. readTemplateContinuation() {
  2892. if (!this.match(8)) {
  2893. this.unexpected(null, 8);
  2894. }
  2895. this.state.pos--;
  2896. this.readTemplateToken();
  2897. }
  2898. readTemplateToken() {
  2899. const opening = this.input[this.state.pos];
  2900. const {
  2901. str,
  2902. containsInvalid,
  2903. pos,
  2904. curLine,
  2905. lineStart
  2906. } = readStringContents("template", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_template);
  2907. this.state.pos = pos + 1;
  2908. this.state.lineStart = lineStart;
  2909. this.state.curLine = curLine;
  2910. if (this.input.codePointAt(pos) === 96) {
  2911. this.finishToken(24, containsInvalid ? null : opening + str + "`");
  2912. } else {
  2913. this.state.pos++;
  2914. this.finishToken(25, containsInvalid ? null : opening + str + "${");
  2915. }
  2916. }
  2917. recordStrictModeErrors(toParseError, {
  2918. at
  2919. }) {
  2920. const index = at.index;
  2921. if (this.state.strict && !this.state.strictErrors.has(index)) {
  2922. this.raise(toParseError, {
  2923. at
  2924. });
  2925. } else {
  2926. this.state.strictErrors.set(index, [toParseError, at]);
  2927. }
  2928. }
  2929. readWord1(firstCode) {
  2930. this.state.containsEsc = false;
  2931. let word = "";
  2932. const start = this.state.pos;
  2933. let chunkStart = this.state.pos;
  2934. if (firstCode !== undefined) {
  2935. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  2936. }
  2937. while (this.state.pos < this.length) {
  2938. const ch = this.codePointAtPos(this.state.pos);
  2939. if (isIdentifierChar(ch)) {
  2940. this.state.pos += ch <= 0xffff ? 1 : 2;
  2941. } else if (ch === 92) {
  2942. this.state.containsEsc = true;
  2943. word += this.input.slice(chunkStart, this.state.pos);
  2944. const escStart = this.state.curPosition();
  2945. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  2946. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  2947. this.raise(Errors.MissingUnicodeEscape, {
  2948. at: this.state.curPosition()
  2949. });
  2950. chunkStart = this.state.pos - 1;
  2951. continue;
  2952. }
  2953. ++this.state.pos;
  2954. const esc = this.readCodePoint(true);
  2955. if (esc !== null) {
  2956. if (!identifierCheck(esc)) {
  2957. this.raise(Errors.EscapedCharNotAnIdentifier, {
  2958. at: escStart
  2959. });
  2960. }
  2961. word += String.fromCodePoint(esc);
  2962. }
  2963. chunkStart = this.state.pos;
  2964. } else {
  2965. break;
  2966. }
  2967. }
  2968. return word + this.input.slice(chunkStart, this.state.pos);
  2969. }
  2970. readWord(firstCode) {
  2971. const word = this.readWord1(firstCode);
  2972. const type = keywords$1.get(word);
  2973. if (type !== undefined) {
  2974. this.finishToken(type, tokenLabelName(type));
  2975. } else {
  2976. this.finishToken(128, word);
  2977. }
  2978. }
  2979. checkKeywordEscapes() {
  2980. const {
  2981. type
  2982. } = this.state;
  2983. if (tokenIsKeyword(type) && this.state.containsEsc) {
  2984. this.raise(Errors.InvalidEscapedReservedWord, {
  2985. at: this.state.startLoc,
  2986. reservedWord: tokenLabelName(type)
  2987. });
  2988. }
  2989. }
  2990. raise(toParseError, raiseProperties) {
  2991. const {
  2992. at
  2993. } = raiseProperties,
  2994. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded);
  2995. const loc = at instanceof Position ? at : at.loc.start;
  2996. const error = toParseError({
  2997. loc,
  2998. details
  2999. });
  3000. if (!this.options.errorRecovery) throw error;
  3001. if (!this.isLookahead) this.state.errors.push(error);
  3002. return error;
  3003. }
  3004. raiseOverwrite(toParseError, raiseProperties) {
  3005. const {
  3006. at
  3007. } = raiseProperties,
  3008. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded2);
  3009. const loc = at instanceof Position ? at : at.loc.start;
  3010. const pos = loc.index;
  3011. const errors = this.state.errors;
  3012. for (let i = errors.length - 1; i >= 0; i--) {
  3013. const error = errors[i];
  3014. if (error.loc.index === pos) {
  3015. return errors[i] = toParseError({
  3016. loc,
  3017. details
  3018. });
  3019. }
  3020. if (error.loc.index < pos) break;
  3021. }
  3022. return this.raise(toParseError, raiseProperties);
  3023. }
  3024. updateContext(prevType) {}
  3025. unexpected(loc, type) {
  3026. throw this.raise(Errors.UnexpectedToken, {
  3027. expected: type ? tokenLabelName(type) : null,
  3028. at: loc != null ? loc : this.state.startLoc
  3029. });
  3030. }
  3031. expectPlugin(pluginName, loc) {
  3032. if (this.hasPlugin(pluginName)) {
  3033. return true;
  3034. }
  3035. throw this.raise(Errors.MissingPlugin, {
  3036. at: loc != null ? loc : this.state.startLoc,
  3037. missingPlugin: [pluginName]
  3038. });
  3039. }
  3040. expectOnePlugin(pluginNames) {
  3041. if (!pluginNames.some(name => this.hasPlugin(name))) {
  3042. throw this.raise(Errors.MissingOneOfPlugins, {
  3043. at: this.state.startLoc,
  3044. missingPlugin: pluginNames
  3045. });
  3046. }
  3047. }
  3048. errorBuilder(error) {
  3049. return (pos, lineStart, curLine) => {
  3050. this.raise(error, {
  3051. at: buildPosition(pos, lineStart, curLine)
  3052. });
  3053. };
  3054. }
  3055. }
  3056. class Scope {
  3057. constructor(flags) {
  3058. this.var = new Set();
  3059. this.lexical = new Set();
  3060. this.functions = new Set();
  3061. this.flags = flags;
  3062. }
  3063. }
  3064. class ScopeHandler {
  3065. constructor(parser, inModule) {
  3066. this.parser = void 0;
  3067. this.scopeStack = [];
  3068. this.inModule = void 0;
  3069. this.undefinedExports = new Map();
  3070. this.parser = parser;
  3071. this.inModule = inModule;
  3072. }
  3073. get inFunction() {
  3074. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  3075. }
  3076. get allowSuper() {
  3077. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  3078. }
  3079. get allowDirectSuper() {
  3080. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  3081. }
  3082. get inClass() {
  3083. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  3084. }
  3085. get inClassAndNotInNonArrowFunction() {
  3086. const flags = this.currentThisScopeFlags();
  3087. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  3088. }
  3089. get inStaticBlock() {
  3090. for (let i = this.scopeStack.length - 1;; i--) {
  3091. const {
  3092. flags
  3093. } = this.scopeStack[i];
  3094. if (flags & SCOPE_STATIC_BLOCK) {
  3095. return true;
  3096. }
  3097. if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
  3098. return false;
  3099. }
  3100. }
  3101. }
  3102. get inNonArrowFunction() {
  3103. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  3104. }
  3105. get treatFunctionsAsVar() {
  3106. return this.treatFunctionsAsVarInScope(this.currentScope());
  3107. }
  3108. createScope(flags) {
  3109. return new Scope(flags);
  3110. }
  3111. enter(flags) {
  3112. this.scopeStack.push(this.createScope(flags));
  3113. }
  3114. exit() {
  3115. this.scopeStack.pop();
  3116. }
  3117. treatFunctionsAsVarInScope(scope) {
  3118. return !!(scope.flags & (SCOPE_FUNCTION | SCOPE_STATIC_BLOCK) || !this.parser.inModule && scope.flags & SCOPE_PROGRAM);
  3119. }
  3120. declareName(name, bindingType, loc) {
  3121. let scope = this.currentScope();
  3122. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  3123. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  3124. if (bindingType & BIND_SCOPE_FUNCTION) {
  3125. scope.functions.add(name);
  3126. } else {
  3127. scope.lexical.add(name);
  3128. }
  3129. if (bindingType & BIND_SCOPE_LEXICAL) {
  3130. this.maybeExportDefined(scope, name);
  3131. }
  3132. } else if (bindingType & BIND_SCOPE_VAR) {
  3133. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  3134. scope = this.scopeStack[i];
  3135. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  3136. scope.var.add(name);
  3137. this.maybeExportDefined(scope, name);
  3138. if (scope.flags & SCOPE_VAR) break;
  3139. }
  3140. }
  3141. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  3142. this.undefinedExports.delete(name);
  3143. }
  3144. }
  3145. maybeExportDefined(scope, name) {
  3146. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  3147. this.undefinedExports.delete(name);
  3148. }
  3149. }
  3150. checkRedeclarationInScope(scope, name, bindingType, loc) {
  3151. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  3152. this.parser.raise(Errors.VarRedeclaration, {
  3153. at: loc,
  3154. identifierName: name
  3155. });
  3156. }
  3157. }
  3158. isRedeclaredInScope(scope, name, bindingType) {
  3159. if (!(bindingType & BIND_KIND_VALUE)) return false;
  3160. if (bindingType & BIND_SCOPE_LEXICAL) {
  3161. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  3162. }
  3163. if (bindingType & BIND_SCOPE_FUNCTION) {
  3164. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  3165. }
  3166. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  3167. }
  3168. checkLocalExport(id) {
  3169. const {
  3170. name
  3171. } = id;
  3172. const topLevelScope = this.scopeStack[0];
  3173. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  3174. this.undefinedExports.set(name, id.loc.start);
  3175. }
  3176. }
  3177. currentScope() {
  3178. return this.scopeStack[this.scopeStack.length - 1];
  3179. }
  3180. currentVarScopeFlags() {
  3181. for (let i = this.scopeStack.length - 1;; i--) {
  3182. const {
  3183. flags
  3184. } = this.scopeStack[i];
  3185. if (flags & SCOPE_VAR) {
  3186. return flags;
  3187. }
  3188. }
  3189. }
  3190. currentThisScopeFlags() {
  3191. for (let i = this.scopeStack.length - 1;; i--) {
  3192. const {
  3193. flags
  3194. } = this.scopeStack[i];
  3195. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  3196. return flags;
  3197. }
  3198. }
  3199. }
  3200. }
  3201. class FlowScope extends Scope {
  3202. constructor(...args) {
  3203. super(...args);
  3204. this.declareFunctions = new Set();
  3205. }
  3206. }
  3207. class FlowScopeHandler extends ScopeHandler {
  3208. createScope(flags) {
  3209. return new FlowScope(flags);
  3210. }
  3211. declareName(name, bindingType, loc) {
  3212. const scope = this.currentScope();
  3213. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  3214. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  3215. this.maybeExportDefined(scope, name);
  3216. scope.declareFunctions.add(name);
  3217. return;
  3218. }
  3219. super.declareName(name, bindingType, loc);
  3220. }
  3221. isRedeclaredInScope(scope, name, bindingType) {
  3222. if (super.isRedeclaredInScope(scope, name, bindingType)) return true;
  3223. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  3224. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  3225. }
  3226. return false;
  3227. }
  3228. checkLocalExport(id) {
  3229. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  3230. super.checkLocalExport(id);
  3231. }
  3232. }
  3233. }
  3234. class ClassScope {
  3235. constructor() {
  3236. this.privateNames = new Set();
  3237. this.loneAccessors = new Map();
  3238. this.undefinedPrivateNames = new Map();
  3239. }
  3240. }
  3241. class ClassScopeHandler {
  3242. constructor(parser) {
  3243. this.parser = void 0;
  3244. this.stack = [];
  3245. this.undefinedPrivateNames = new Map();
  3246. this.parser = parser;
  3247. }
  3248. current() {
  3249. return this.stack[this.stack.length - 1];
  3250. }
  3251. enter() {
  3252. this.stack.push(new ClassScope());
  3253. }
  3254. exit() {
  3255. const oldClassScope = this.stack.pop();
  3256. const current = this.current();
  3257. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  3258. if (current) {
  3259. if (!current.undefinedPrivateNames.has(name)) {
  3260. current.undefinedPrivateNames.set(name, loc);
  3261. }
  3262. } else {
  3263. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3264. at: loc,
  3265. identifierName: name
  3266. });
  3267. }
  3268. }
  3269. }
  3270. declarePrivateName(name, elementType, loc) {
  3271. const {
  3272. privateNames,
  3273. loneAccessors,
  3274. undefinedPrivateNames
  3275. } = this.current();
  3276. let redefined = privateNames.has(name);
  3277. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  3278. const accessor = redefined && loneAccessors.get(name);
  3279. if (accessor) {
  3280. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  3281. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  3282. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  3283. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  3284. redefined = oldKind === newKind || oldStatic !== newStatic;
  3285. if (!redefined) loneAccessors.delete(name);
  3286. } else if (!redefined) {
  3287. loneAccessors.set(name, elementType);
  3288. }
  3289. }
  3290. if (redefined) {
  3291. this.parser.raise(Errors.PrivateNameRedeclaration, {
  3292. at: loc,
  3293. identifierName: name
  3294. });
  3295. }
  3296. privateNames.add(name);
  3297. undefinedPrivateNames.delete(name);
  3298. }
  3299. usePrivateName(name, loc) {
  3300. let classScope;
  3301. for (classScope of this.stack) {
  3302. if (classScope.privateNames.has(name)) return;
  3303. }
  3304. if (classScope) {
  3305. classScope.undefinedPrivateNames.set(name, loc);
  3306. } else {
  3307. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3308. at: loc,
  3309. identifierName: name
  3310. });
  3311. }
  3312. }
  3313. }
  3314. const kExpression = 0,
  3315. kMaybeArrowParameterDeclaration = 1,
  3316. kMaybeAsyncArrowParameterDeclaration = 2,
  3317. kParameterDeclaration = 3;
  3318. class ExpressionScope {
  3319. constructor(type = kExpression) {
  3320. this.type = void 0;
  3321. this.type = type;
  3322. }
  3323. canBeArrowParameterDeclaration() {
  3324. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  3325. }
  3326. isCertainlyParameterDeclaration() {
  3327. return this.type === kParameterDeclaration;
  3328. }
  3329. }
  3330. class ArrowHeadParsingScope extends ExpressionScope {
  3331. constructor(type) {
  3332. super(type);
  3333. this.declarationErrors = new Map();
  3334. }
  3335. recordDeclarationError(ParsingErrorClass, {
  3336. at
  3337. }) {
  3338. const index = at.index;
  3339. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  3340. }
  3341. clearDeclarationError(index) {
  3342. this.declarationErrors.delete(index);
  3343. }
  3344. iterateErrors(iterator) {
  3345. this.declarationErrors.forEach(iterator);
  3346. }
  3347. }
  3348. class ExpressionScopeHandler {
  3349. constructor(parser) {
  3350. this.parser = void 0;
  3351. this.stack = [new ExpressionScope()];
  3352. this.parser = parser;
  3353. }
  3354. enter(scope) {
  3355. this.stack.push(scope);
  3356. }
  3357. exit() {
  3358. this.stack.pop();
  3359. }
  3360. recordParameterInitializerError(toParseError, {
  3361. at: node
  3362. }) {
  3363. const origin = {
  3364. at: node.loc.start
  3365. };
  3366. const {
  3367. stack
  3368. } = this;
  3369. let i = stack.length - 1;
  3370. let scope = stack[i];
  3371. while (!scope.isCertainlyParameterDeclaration()) {
  3372. if (scope.canBeArrowParameterDeclaration()) {
  3373. scope.recordDeclarationError(toParseError, origin);
  3374. } else {
  3375. return;
  3376. }
  3377. scope = stack[--i];
  3378. }
  3379. this.parser.raise(toParseError, origin);
  3380. }
  3381. recordArrowParemeterBindingError(error, {
  3382. at: node
  3383. }) {
  3384. const {
  3385. stack
  3386. } = this;
  3387. const scope = stack[stack.length - 1];
  3388. const origin = {
  3389. at: node.loc.start
  3390. };
  3391. if (scope.isCertainlyParameterDeclaration()) {
  3392. this.parser.raise(error, origin);
  3393. } else if (scope.canBeArrowParameterDeclaration()) {
  3394. scope.recordDeclarationError(error, origin);
  3395. } else {
  3396. return;
  3397. }
  3398. }
  3399. recordAsyncArrowParametersError({
  3400. at
  3401. }) {
  3402. const {
  3403. stack
  3404. } = this;
  3405. let i = stack.length - 1;
  3406. let scope = stack[i];
  3407. while (scope.canBeArrowParameterDeclaration()) {
  3408. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  3409. scope.recordDeclarationError(Errors.AwaitBindingIdentifier, {
  3410. at
  3411. });
  3412. }
  3413. scope = stack[--i];
  3414. }
  3415. }
  3416. validateAsPattern() {
  3417. const {
  3418. stack
  3419. } = this;
  3420. const currentScope = stack[stack.length - 1];
  3421. if (!currentScope.canBeArrowParameterDeclaration()) return;
  3422. currentScope.iterateErrors(([toParseError, loc]) => {
  3423. this.parser.raise(toParseError, {
  3424. at: loc
  3425. });
  3426. let i = stack.length - 2;
  3427. let scope = stack[i];
  3428. while (scope.canBeArrowParameterDeclaration()) {
  3429. scope.clearDeclarationError(loc.index);
  3430. scope = stack[--i];
  3431. }
  3432. });
  3433. }
  3434. }
  3435. function newParameterDeclarationScope() {
  3436. return new ExpressionScope(kParameterDeclaration);
  3437. }
  3438. function newArrowHeadScope() {
  3439. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  3440. }
  3441. function newAsyncArrowScope() {
  3442. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  3443. }
  3444. function newExpressionScope() {
  3445. return new ExpressionScope();
  3446. }
  3447. const PARAM = 0b0000,
  3448. PARAM_YIELD = 0b0001,
  3449. PARAM_AWAIT = 0b0010,
  3450. PARAM_RETURN = 0b0100,
  3451. PARAM_IN = 0b1000;
  3452. class ProductionParameterHandler {
  3453. constructor() {
  3454. this.stacks = [];
  3455. }
  3456. enter(flags) {
  3457. this.stacks.push(flags);
  3458. }
  3459. exit() {
  3460. this.stacks.pop();
  3461. }
  3462. currentFlags() {
  3463. return this.stacks[this.stacks.length - 1];
  3464. }
  3465. get hasAwait() {
  3466. return (this.currentFlags() & PARAM_AWAIT) > 0;
  3467. }
  3468. get hasYield() {
  3469. return (this.currentFlags() & PARAM_YIELD) > 0;
  3470. }
  3471. get hasReturn() {
  3472. return (this.currentFlags() & PARAM_RETURN) > 0;
  3473. }
  3474. get hasIn() {
  3475. return (this.currentFlags() & PARAM_IN) > 0;
  3476. }
  3477. }
  3478. function functionFlags(isAsync, isGenerator) {
  3479. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  3480. }
  3481. class UtilParser extends Tokenizer {
  3482. addExtra(node, key, value, enumerable = true) {
  3483. if (!node) return;
  3484. const extra = node.extra = node.extra || {};
  3485. if (enumerable) {
  3486. extra[key] = value;
  3487. } else {
  3488. Object.defineProperty(extra, key, {
  3489. enumerable,
  3490. value
  3491. });
  3492. }
  3493. }
  3494. isContextual(token) {
  3495. return this.state.type === token && !this.state.containsEsc;
  3496. }
  3497. isUnparsedContextual(nameStart, name) {
  3498. const nameEnd = nameStart + name.length;
  3499. if (this.input.slice(nameStart, nameEnd) === name) {
  3500. const nextCh = this.input.charCodeAt(nameEnd);
  3501. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  3502. }
  3503. return false;
  3504. }
  3505. isLookaheadContextual(name) {
  3506. const next = this.nextTokenStart();
  3507. return this.isUnparsedContextual(next, name);
  3508. }
  3509. eatContextual(token) {
  3510. if (this.isContextual(token)) {
  3511. this.next();
  3512. return true;
  3513. }
  3514. return false;
  3515. }
  3516. expectContextual(token, toParseError) {
  3517. if (!this.eatContextual(token)) {
  3518. if (toParseError != null) {
  3519. throw this.raise(toParseError, {
  3520. at: this.state.startLoc
  3521. });
  3522. }
  3523. throw this.unexpected(null, token);
  3524. }
  3525. }
  3526. canInsertSemicolon() {
  3527. return this.match(135) || this.match(8) || this.hasPrecedingLineBreak();
  3528. }
  3529. hasPrecedingLineBreak() {
  3530. return lineBreak.test(this.input.slice(this.state.lastTokEndLoc.index, this.state.start));
  3531. }
  3532. hasFollowingLineBreak() {
  3533. skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
  3534. return skipWhiteSpaceToLineBreak.test(this.input);
  3535. }
  3536. isLineTerminator() {
  3537. return this.eat(13) || this.canInsertSemicolon();
  3538. }
  3539. semicolon(allowAsi = true) {
  3540. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  3541. this.raise(Errors.MissingSemicolon, {
  3542. at: this.state.lastTokEndLoc
  3543. });
  3544. }
  3545. expect(type, loc) {
  3546. this.eat(type) || this.unexpected(loc, type);
  3547. }
  3548. tryParse(fn, oldState = this.state.clone()) {
  3549. const abortSignal = {
  3550. node: null
  3551. };
  3552. try {
  3553. const node = fn((node = null) => {
  3554. abortSignal.node = node;
  3555. throw abortSignal;
  3556. });
  3557. if (this.state.errors.length > oldState.errors.length) {
  3558. const failState = this.state;
  3559. this.state = oldState;
  3560. this.state.tokensLength = failState.tokensLength;
  3561. return {
  3562. node,
  3563. error: failState.errors[oldState.errors.length],
  3564. thrown: false,
  3565. aborted: false,
  3566. failState
  3567. };
  3568. }
  3569. return {
  3570. node,
  3571. error: null,
  3572. thrown: false,
  3573. aborted: false,
  3574. failState: null
  3575. };
  3576. } catch (error) {
  3577. const failState = this.state;
  3578. this.state = oldState;
  3579. if (error instanceof SyntaxError) {
  3580. return {
  3581. node: null,
  3582. error,
  3583. thrown: true,
  3584. aborted: false,
  3585. failState
  3586. };
  3587. }
  3588. if (error === abortSignal) {
  3589. return {
  3590. node: abortSignal.node,
  3591. error: null,
  3592. thrown: false,
  3593. aborted: true,
  3594. failState
  3595. };
  3596. }
  3597. throw error;
  3598. }
  3599. }
  3600. checkExpressionErrors(refExpressionErrors, andThrow) {
  3601. if (!refExpressionErrors) return false;
  3602. const {
  3603. shorthandAssignLoc,
  3604. doubleProtoLoc,
  3605. privateKeyLoc,
  3606. optionalParametersLoc
  3607. } = refExpressionErrors;
  3608. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
  3609. if (!andThrow) {
  3610. return hasErrors;
  3611. }
  3612. if (shorthandAssignLoc != null) {
  3613. this.raise(Errors.InvalidCoverInitializedName, {
  3614. at: shorthandAssignLoc
  3615. });
  3616. }
  3617. if (doubleProtoLoc != null) {
  3618. this.raise(Errors.DuplicateProto, {
  3619. at: doubleProtoLoc
  3620. });
  3621. }
  3622. if (privateKeyLoc != null) {
  3623. this.raise(Errors.UnexpectedPrivateField, {
  3624. at: privateKeyLoc
  3625. });
  3626. }
  3627. if (optionalParametersLoc != null) {
  3628. this.unexpected(optionalParametersLoc);
  3629. }
  3630. }
  3631. isLiteralPropertyName() {
  3632. return tokenIsLiteralPropertyName(this.state.type);
  3633. }
  3634. isPrivateName(node) {
  3635. return node.type === "PrivateName";
  3636. }
  3637. getPrivateNameSV(node) {
  3638. return node.id.name;
  3639. }
  3640. hasPropertyAsPrivateName(node) {
  3641. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  3642. }
  3643. isOptionalChain(node) {
  3644. return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
  3645. }
  3646. isObjectProperty(node) {
  3647. return node.type === "ObjectProperty";
  3648. }
  3649. isObjectMethod(node) {
  3650. return node.type === "ObjectMethod";
  3651. }
  3652. initializeScopes(inModule = this.options.sourceType === "module") {
  3653. const oldLabels = this.state.labels;
  3654. this.state.labels = [];
  3655. const oldExportedIdentifiers = this.exportedIdentifiers;
  3656. this.exportedIdentifiers = new Set();
  3657. const oldInModule = this.inModule;
  3658. this.inModule = inModule;
  3659. const oldScope = this.scope;
  3660. const ScopeHandler = this.getScopeHandler();
  3661. this.scope = new ScopeHandler(this, inModule);
  3662. const oldProdParam = this.prodParam;
  3663. this.prodParam = new ProductionParameterHandler();
  3664. const oldClassScope = this.classScope;
  3665. this.classScope = new ClassScopeHandler(this);
  3666. const oldExpressionScope = this.expressionScope;
  3667. this.expressionScope = new ExpressionScopeHandler(this);
  3668. return () => {
  3669. this.state.labels = oldLabels;
  3670. this.exportedIdentifiers = oldExportedIdentifiers;
  3671. this.inModule = oldInModule;
  3672. this.scope = oldScope;
  3673. this.prodParam = oldProdParam;
  3674. this.classScope = oldClassScope;
  3675. this.expressionScope = oldExpressionScope;
  3676. };
  3677. }
  3678. enterInitialScopes() {
  3679. let paramFlags = PARAM;
  3680. if (this.inModule) {
  3681. paramFlags |= PARAM_AWAIT;
  3682. }
  3683. this.scope.enter(SCOPE_PROGRAM);
  3684. this.prodParam.enter(paramFlags);
  3685. }
  3686. checkDestructuringPrivate(refExpressionErrors) {
  3687. const {
  3688. privateKeyLoc
  3689. } = refExpressionErrors;
  3690. if (privateKeyLoc !== null) {
  3691. this.expectPlugin("destructuringPrivate", privateKeyLoc);
  3692. }
  3693. }
  3694. }
  3695. class ExpressionErrors {
  3696. constructor() {
  3697. this.shorthandAssignLoc = null;
  3698. this.doubleProtoLoc = null;
  3699. this.privateKeyLoc = null;
  3700. this.optionalParametersLoc = null;
  3701. }
  3702. }
  3703. class Node {
  3704. constructor(parser, pos, loc) {
  3705. this.type = "";
  3706. this.start = pos;
  3707. this.end = 0;
  3708. this.loc = new SourceLocation(loc);
  3709. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  3710. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  3711. }
  3712. }
  3713. const NodePrototype = Node.prototype;
  3714. {
  3715. NodePrototype.__clone = function () {
  3716. const newNode = new Node();
  3717. const keys = Object.keys(this);
  3718. for (let i = 0, length = keys.length; i < length; i++) {
  3719. const key = keys[i];
  3720. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  3721. newNode[key] = this[key];
  3722. }
  3723. }
  3724. return newNode;
  3725. };
  3726. }
  3727. function clonePlaceholder(node) {
  3728. return cloneIdentifier(node);
  3729. }
  3730. function cloneIdentifier(node) {
  3731. const {
  3732. type,
  3733. start,
  3734. end,
  3735. loc,
  3736. range,
  3737. extra,
  3738. name
  3739. } = node;
  3740. const cloned = Object.create(NodePrototype);
  3741. cloned.type = type;
  3742. cloned.start = start;
  3743. cloned.end = end;
  3744. cloned.loc = loc;
  3745. cloned.range = range;
  3746. cloned.extra = extra;
  3747. cloned.name = name;
  3748. if (type === "Placeholder") {
  3749. cloned.expectedNode = node.expectedNode;
  3750. }
  3751. return cloned;
  3752. }
  3753. function cloneStringLiteral(node) {
  3754. const {
  3755. type,
  3756. start,
  3757. end,
  3758. loc,
  3759. range,
  3760. extra
  3761. } = node;
  3762. if (type === "Placeholder") {
  3763. return clonePlaceholder(node);
  3764. }
  3765. const cloned = Object.create(NodePrototype);
  3766. cloned.type = type;
  3767. cloned.start = start;
  3768. cloned.end = end;
  3769. cloned.loc = loc;
  3770. cloned.range = range;
  3771. if (node.raw !== undefined) {
  3772. cloned.raw = node.raw;
  3773. } else {
  3774. cloned.extra = extra;
  3775. }
  3776. cloned.value = node.value;
  3777. return cloned;
  3778. }
  3779. class NodeUtils extends UtilParser {
  3780. startNode() {
  3781. return new Node(this, this.state.start, this.state.startLoc);
  3782. }
  3783. startNodeAt(pos, loc) {
  3784. return new Node(this, pos, loc);
  3785. }
  3786. startNodeAtNode(type) {
  3787. return this.startNodeAt(type.start, type.loc.start);
  3788. }
  3789. finishNode(node, type) {
  3790. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  3791. }
  3792. finishNodeAt(node, type, endLoc) {
  3793. node.type = type;
  3794. node.end = endLoc.index;
  3795. node.loc.end = endLoc;
  3796. if (this.options.ranges) node.range[1] = endLoc.index;
  3797. if (this.options.attachComment) this.processComment(node);
  3798. return node;
  3799. }
  3800. resetStartLocation(node, start, startLoc) {
  3801. node.start = start;
  3802. node.loc.start = startLoc;
  3803. if (this.options.ranges) node.range[0] = start;
  3804. }
  3805. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  3806. node.end = endLoc.index;
  3807. node.loc.end = endLoc;
  3808. if (this.options.ranges) node.range[1] = endLoc.index;
  3809. }
  3810. resetStartLocationFromNode(node, locationNode) {
  3811. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  3812. }
  3813. }
  3814. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3815. const FlowErrors = ParseErrorEnum`flow`({
  3816. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  3817. 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.",
  3818. AssignReservedType: ({
  3819. reservedType
  3820. }) => `Cannot overwrite reserved type ${reservedType}.`,
  3821. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  3822. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  3823. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  3824. EnumBooleanMemberNotInitialized: ({
  3825. memberName,
  3826. enumName
  3827. }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`,
  3828. EnumDuplicateMemberName: ({
  3829. memberName,
  3830. enumName
  3831. }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`,
  3832. EnumInconsistentMemberValues: ({
  3833. enumName
  3834. }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`,
  3835. EnumInvalidExplicitType: ({
  3836. invalidEnumType,
  3837. enumName
  3838. }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3839. EnumInvalidExplicitTypeUnknownSupplied: ({
  3840. enumName
  3841. }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3842. EnumInvalidMemberInitializerPrimaryType: ({
  3843. enumName,
  3844. memberName,
  3845. explicitType
  3846. }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`,
  3847. EnumInvalidMemberInitializerSymbolType: ({
  3848. enumName,
  3849. memberName
  3850. }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`,
  3851. EnumInvalidMemberInitializerUnknownType: ({
  3852. enumName,
  3853. memberName
  3854. }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`,
  3855. EnumInvalidMemberName: ({
  3856. enumName,
  3857. memberName,
  3858. suggestion
  3859. }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`,
  3860. EnumNumberMemberNotInitialized: ({
  3861. enumName,
  3862. memberName
  3863. }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`,
  3864. EnumStringMemberInconsistentlyInitailized: ({
  3865. enumName
  3866. }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`,
  3867. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  3868. 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.",
  3869. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  3870. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  3871. InexactVariance: "Explicit inexact syntax cannot have variance.",
  3872. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  3873. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  3874. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  3875. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  3876. PatternIsOptional: Object.assign({
  3877. message: "A binding pattern parameter cannot be optional in an implementation signature."
  3878. }, {
  3879. reasonCode: "OptionalBindingPattern"
  3880. }),
  3881. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  3882. SpreadVariance: "Spread properties cannot have variance.",
  3883. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  3884. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  3885. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  3886. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  3887. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  3888. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  3889. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  3890. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  3891. UnexpectedReservedType: ({
  3892. reservedType
  3893. }) => `Unexpected reserved type ${reservedType}.`,
  3894. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  3895. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  3896. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  3897. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  3898. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  3899. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  3900. UnsupportedDeclareExportKind: ({
  3901. unsupportedExportKind,
  3902. suggestion
  3903. }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`,
  3904. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  3905. UnterminatedFlowComment: "Unterminated flow-comment."
  3906. });
  3907. function isEsModuleType(bodyElement) {
  3908. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3909. }
  3910. function hasTypeImportKind(node) {
  3911. return node.importKind === "type" || node.importKind === "typeof";
  3912. }
  3913. function isMaybeDefaultImport(type) {
  3914. return tokenIsKeywordOrIdentifier(type) && type !== 97;
  3915. }
  3916. const exportSuggestions = {
  3917. const: "declare export var",
  3918. let: "declare export var",
  3919. type: "export type",
  3920. interface: "export interface"
  3921. };
  3922. function partition(list, test) {
  3923. const list1 = [];
  3924. const list2 = [];
  3925. for (let i = 0; i < list.length; i++) {
  3926. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  3927. }
  3928. return [list1, list2];
  3929. }
  3930. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  3931. var flow = (superClass => class FlowParserMixin extends superClass {
  3932. constructor(...args) {
  3933. super(...args);
  3934. this.flowPragma = undefined;
  3935. }
  3936. getScopeHandler() {
  3937. return FlowScopeHandler;
  3938. }
  3939. shouldParseTypes() {
  3940. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  3941. }
  3942. shouldParseEnums() {
  3943. return !!this.getPluginOption("flow", "enums");
  3944. }
  3945. finishToken(type, val) {
  3946. if (type !== 129 && type !== 13 && type !== 28) {
  3947. if (this.flowPragma === undefined) {
  3948. this.flowPragma = null;
  3949. }
  3950. }
  3951. return super.finishToken(type, val);
  3952. }
  3953. addComment(comment) {
  3954. if (this.flowPragma === undefined) {
  3955. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  3956. if (!matches) ; else if (matches[1] === "flow") {
  3957. this.flowPragma = "flow";
  3958. } else if (matches[1] === "noflow") {
  3959. this.flowPragma = "noflow";
  3960. } else {
  3961. throw new Error("Unexpected flow pragma");
  3962. }
  3963. }
  3964. return super.addComment(comment);
  3965. }
  3966. flowParseTypeInitialiser(tok) {
  3967. const oldInType = this.state.inType;
  3968. this.state.inType = true;
  3969. this.expect(tok || 14);
  3970. const type = this.flowParseType();
  3971. this.state.inType = oldInType;
  3972. return type;
  3973. }
  3974. flowParsePredicate() {
  3975. const node = this.startNode();
  3976. const moduloLoc = this.state.startLoc;
  3977. this.next();
  3978. this.expectContextual(107);
  3979. if (this.state.lastTokStart > moduloLoc.index + 1) {
  3980. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, {
  3981. at: moduloLoc
  3982. });
  3983. }
  3984. if (this.eat(10)) {
  3985. node.value = super.parseExpression();
  3986. this.expect(11);
  3987. return this.finishNode(node, "DeclaredPredicate");
  3988. } else {
  3989. return this.finishNode(node, "InferredPredicate");
  3990. }
  3991. }
  3992. flowParseTypeAndPredicateInitialiser() {
  3993. const oldInType = this.state.inType;
  3994. this.state.inType = true;
  3995. this.expect(14);
  3996. let type = null;
  3997. let predicate = null;
  3998. if (this.match(54)) {
  3999. this.state.inType = oldInType;
  4000. predicate = this.flowParsePredicate();
  4001. } else {
  4002. type = this.flowParseType();
  4003. this.state.inType = oldInType;
  4004. if (this.match(54)) {
  4005. predicate = this.flowParsePredicate();
  4006. }
  4007. }
  4008. return [type, predicate];
  4009. }
  4010. flowParseDeclareClass(node) {
  4011. this.next();
  4012. this.flowParseInterfaceish(node, true);
  4013. return this.finishNode(node, "DeclareClass");
  4014. }
  4015. flowParseDeclareFunction(node) {
  4016. this.next();
  4017. const id = node.id = this.parseIdentifier();
  4018. const typeNode = this.startNode();
  4019. const typeContainer = this.startNode();
  4020. if (this.match(47)) {
  4021. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  4022. } else {
  4023. typeNode.typeParameters = null;
  4024. }
  4025. this.expect(10);
  4026. const tmp = this.flowParseFunctionTypeParams();
  4027. typeNode.params = tmp.params;
  4028. typeNode.rest = tmp.rest;
  4029. typeNode.this = tmp._this;
  4030. this.expect(11);
  4031. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4032. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  4033. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  4034. this.resetEndLocation(id);
  4035. this.semicolon();
  4036. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.loc.start);
  4037. return this.finishNode(node, "DeclareFunction");
  4038. }
  4039. flowParseDeclare(node, insideModule) {
  4040. if (this.match(80)) {
  4041. return this.flowParseDeclareClass(node);
  4042. } else if (this.match(68)) {
  4043. return this.flowParseDeclareFunction(node);
  4044. } else if (this.match(74)) {
  4045. return this.flowParseDeclareVariable(node);
  4046. } else if (this.eatContextual(123)) {
  4047. if (this.match(16)) {
  4048. return this.flowParseDeclareModuleExports(node);
  4049. } else {
  4050. if (insideModule) {
  4051. this.raise(FlowErrors.NestedDeclareModule, {
  4052. at: this.state.lastTokStartLoc
  4053. });
  4054. }
  4055. return this.flowParseDeclareModule(node);
  4056. }
  4057. } else if (this.isContextual(126)) {
  4058. return this.flowParseDeclareTypeAlias(node);
  4059. } else if (this.isContextual(127)) {
  4060. return this.flowParseDeclareOpaqueType(node);
  4061. } else if (this.isContextual(125)) {
  4062. return this.flowParseDeclareInterface(node);
  4063. } else if (this.match(82)) {
  4064. return this.flowParseDeclareExportDeclaration(node, insideModule);
  4065. } else {
  4066. throw this.unexpected();
  4067. }
  4068. }
  4069. flowParseDeclareVariable(node) {
  4070. this.next();
  4071. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  4072. this.scope.declareName(node.id.name, BIND_VAR, node.id.loc.start);
  4073. this.semicolon();
  4074. return this.finishNode(node, "DeclareVariable");
  4075. }
  4076. flowParseDeclareModule(node) {
  4077. this.scope.enter(SCOPE_OTHER);
  4078. if (this.match(129)) {
  4079. node.id = super.parseExprAtom();
  4080. } else {
  4081. node.id = this.parseIdentifier();
  4082. }
  4083. const bodyNode = node.body = this.startNode();
  4084. const body = bodyNode.body = [];
  4085. this.expect(5);
  4086. while (!this.match(8)) {
  4087. let bodyNode = this.startNode();
  4088. if (this.match(83)) {
  4089. this.next();
  4090. if (!this.isContextual(126) && !this.match(87)) {
  4091. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, {
  4092. at: this.state.lastTokStartLoc
  4093. });
  4094. }
  4095. super.parseImport(bodyNode);
  4096. } else {
  4097. this.expectContextual(121, FlowErrors.UnsupportedStatementInDeclareModule);
  4098. bodyNode = this.flowParseDeclare(bodyNode, true);
  4099. }
  4100. body.push(bodyNode);
  4101. }
  4102. this.scope.exit();
  4103. this.expect(8);
  4104. this.finishNode(bodyNode, "BlockStatement");
  4105. let kind = null;
  4106. let hasModuleExport = false;
  4107. body.forEach(bodyElement => {
  4108. if (isEsModuleType(bodyElement)) {
  4109. if (kind === "CommonJS") {
  4110. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4111. at: bodyElement
  4112. });
  4113. }
  4114. kind = "ES";
  4115. } else if (bodyElement.type === "DeclareModuleExports") {
  4116. if (hasModuleExport) {
  4117. this.raise(FlowErrors.DuplicateDeclareModuleExports, {
  4118. at: bodyElement
  4119. });
  4120. }
  4121. if (kind === "ES") {
  4122. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4123. at: bodyElement
  4124. });
  4125. }
  4126. kind = "CommonJS";
  4127. hasModuleExport = true;
  4128. }
  4129. });
  4130. node.kind = kind || "CommonJS";
  4131. return this.finishNode(node, "DeclareModule");
  4132. }
  4133. flowParseDeclareExportDeclaration(node, insideModule) {
  4134. this.expect(82);
  4135. if (this.eat(65)) {
  4136. if (this.match(68) || this.match(80)) {
  4137. node.declaration = this.flowParseDeclare(this.startNode());
  4138. } else {
  4139. node.declaration = this.flowParseType();
  4140. this.semicolon();
  4141. }
  4142. node.default = true;
  4143. return this.finishNode(node, "DeclareExportDeclaration");
  4144. } else {
  4145. if (this.match(75) || this.isLet() || (this.isContextual(126) || this.isContextual(125)) && !insideModule) {
  4146. const label = this.state.value;
  4147. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, {
  4148. at: this.state.startLoc,
  4149. unsupportedExportKind: label,
  4150. suggestion: exportSuggestions[label]
  4151. });
  4152. }
  4153. if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(127)) {
  4154. node.declaration = this.flowParseDeclare(this.startNode());
  4155. node.default = false;
  4156. return this.finishNode(node, "DeclareExportDeclaration");
  4157. } else if (this.match(55) || this.match(5) || this.isContextual(125) || this.isContextual(126) || this.isContextual(127)) {
  4158. node = this.parseExport(node);
  4159. if (node.type === "ExportNamedDeclaration") {
  4160. node.type = "ExportDeclaration";
  4161. node.default = false;
  4162. delete node.exportKind;
  4163. }
  4164. node.type = "Declare" + node.type;
  4165. return node;
  4166. }
  4167. }
  4168. throw this.unexpected();
  4169. }
  4170. flowParseDeclareModuleExports(node) {
  4171. this.next();
  4172. this.expectContextual(108);
  4173. node.typeAnnotation = this.flowParseTypeAnnotation();
  4174. this.semicolon();
  4175. return this.finishNode(node, "DeclareModuleExports");
  4176. }
  4177. flowParseDeclareTypeAlias(node) {
  4178. this.next();
  4179. const finished = this.flowParseTypeAlias(node);
  4180. finished.type = "DeclareTypeAlias";
  4181. return finished;
  4182. }
  4183. flowParseDeclareOpaqueType(node) {
  4184. this.next();
  4185. const finished = this.flowParseOpaqueType(node, true);
  4186. finished.type = "DeclareOpaqueType";
  4187. return finished;
  4188. }
  4189. flowParseDeclareInterface(node) {
  4190. this.next();
  4191. this.flowParseInterfaceish(node);
  4192. return this.finishNode(node, "DeclareInterface");
  4193. }
  4194. flowParseInterfaceish(node, isClass = false) {
  4195. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  4196. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.loc.start);
  4197. if (this.match(47)) {
  4198. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4199. } else {
  4200. node.typeParameters = null;
  4201. }
  4202. node.extends = [];
  4203. node.implements = [];
  4204. node.mixins = [];
  4205. if (this.eat(81)) {
  4206. do {
  4207. node.extends.push(this.flowParseInterfaceExtends());
  4208. } while (!isClass && this.eat(12));
  4209. }
  4210. if (this.isContextual(114)) {
  4211. this.next();
  4212. do {
  4213. node.mixins.push(this.flowParseInterfaceExtends());
  4214. } while (this.eat(12));
  4215. }
  4216. if (this.isContextual(110)) {
  4217. this.next();
  4218. do {
  4219. node.implements.push(this.flowParseInterfaceExtends());
  4220. } while (this.eat(12));
  4221. }
  4222. node.body = this.flowParseObjectType({
  4223. allowStatic: isClass,
  4224. allowExact: false,
  4225. allowSpread: false,
  4226. allowProto: isClass,
  4227. allowInexact: false
  4228. });
  4229. }
  4230. flowParseInterfaceExtends() {
  4231. const node = this.startNode();
  4232. node.id = this.flowParseQualifiedTypeIdentifier();
  4233. if (this.match(47)) {
  4234. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4235. } else {
  4236. node.typeParameters = null;
  4237. }
  4238. return this.finishNode(node, "InterfaceExtends");
  4239. }
  4240. flowParseInterface(node) {
  4241. this.flowParseInterfaceish(node);
  4242. return this.finishNode(node, "InterfaceDeclaration");
  4243. }
  4244. checkNotUnderscore(word) {
  4245. if (word === "_") {
  4246. this.raise(FlowErrors.UnexpectedReservedUnderscore, {
  4247. at: this.state.startLoc
  4248. });
  4249. }
  4250. }
  4251. checkReservedType(word, startLoc, declaration) {
  4252. if (!reservedTypes.has(word)) return;
  4253. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, {
  4254. at: startLoc,
  4255. reservedType: word
  4256. });
  4257. }
  4258. flowParseRestrictedIdentifier(liberal, declaration) {
  4259. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  4260. return this.parseIdentifier(liberal);
  4261. }
  4262. flowParseTypeAlias(node) {
  4263. node.id = this.flowParseRestrictedIdentifier(false, true);
  4264. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4265. if (this.match(47)) {
  4266. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4267. } else {
  4268. node.typeParameters = null;
  4269. }
  4270. node.right = this.flowParseTypeInitialiser(29);
  4271. this.semicolon();
  4272. return this.finishNode(node, "TypeAlias");
  4273. }
  4274. flowParseOpaqueType(node, declare) {
  4275. this.expectContextual(126);
  4276. node.id = this.flowParseRestrictedIdentifier(true, true);
  4277. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4278. if (this.match(47)) {
  4279. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4280. } else {
  4281. node.typeParameters = null;
  4282. }
  4283. node.supertype = null;
  4284. if (this.match(14)) {
  4285. node.supertype = this.flowParseTypeInitialiser(14);
  4286. }
  4287. node.impltype = null;
  4288. if (!declare) {
  4289. node.impltype = this.flowParseTypeInitialiser(29);
  4290. }
  4291. this.semicolon();
  4292. return this.finishNode(node, "OpaqueType");
  4293. }
  4294. flowParseTypeParameter(requireDefault = false) {
  4295. const nodeStartLoc = this.state.startLoc;
  4296. const node = this.startNode();
  4297. const variance = this.flowParseVariance();
  4298. const ident = this.flowParseTypeAnnotatableIdentifier();
  4299. node.name = ident.name;
  4300. node.variance = variance;
  4301. node.bound = ident.typeAnnotation;
  4302. if (this.match(29)) {
  4303. this.eat(29);
  4304. node.default = this.flowParseType();
  4305. } else {
  4306. if (requireDefault) {
  4307. this.raise(FlowErrors.MissingTypeParamDefault, {
  4308. at: nodeStartLoc
  4309. });
  4310. }
  4311. }
  4312. return this.finishNode(node, "TypeParameter");
  4313. }
  4314. flowParseTypeParameterDeclaration() {
  4315. const oldInType = this.state.inType;
  4316. const node = this.startNode();
  4317. node.params = [];
  4318. this.state.inType = true;
  4319. if (this.match(47) || this.match(138)) {
  4320. this.next();
  4321. } else {
  4322. this.unexpected();
  4323. }
  4324. let defaultRequired = false;
  4325. do {
  4326. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  4327. node.params.push(typeParameter);
  4328. if (typeParameter.default) {
  4329. defaultRequired = true;
  4330. }
  4331. if (!this.match(48)) {
  4332. this.expect(12);
  4333. }
  4334. } while (!this.match(48));
  4335. this.expect(48);
  4336. this.state.inType = oldInType;
  4337. return this.finishNode(node, "TypeParameterDeclaration");
  4338. }
  4339. flowParseTypeParameterInstantiation() {
  4340. const node = this.startNode();
  4341. const oldInType = this.state.inType;
  4342. node.params = [];
  4343. this.state.inType = true;
  4344. this.expect(47);
  4345. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4346. this.state.noAnonFunctionType = false;
  4347. while (!this.match(48)) {
  4348. node.params.push(this.flowParseType());
  4349. if (!this.match(48)) {
  4350. this.expect(12);
  4351. }
  4352. }
  4353. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4354. this.expect(48);
  4355. this.state.inType = oldInType;
  4356. return this.finishNode(node, "TypeParameterInstantiation");
  4357. }
  4358. flowParseTypeParameterInstantiationCallOrNew() {
  4359. const node = this.startNode();
  4360. const oldInType = this.state.inType;
  4361. node.params = [];
  4362. this.state.inType = true;
  4363. this.expect(47);
  4364. while (!this.match(48)) {
  4365. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  4366. if (!this.match(48)) {
  4367. this.expect(12);
  4368. }
  4369. }
  4370. this.expect(48);
  4371. this.state.inType = oldInType;
  4372. return this.finishNode(node, "TypeParameterInstantiation");
  4373. }
  4374. flowParseInterfaceType() {
  4375. const node = this.startNode();
  4376. this.expectContextual(125);
  4377. node.extends = [];
  4378. if (this.eat(81)) {
  4379. do {
  4380. node.extends.push(this.flowParseInterfaceExtends());
  4381. } while (this.eat(12));
  4382. }
  4383. node.body = this.flowParseObjectType({
  4384. allowStatic: false,
  4385. allowExact: false,
  4386. allowSpread: false,
  4387. allowProto: false,
  4388. allowInexact: false
  4389. });
  4390. return this.finishNode(node, "InterfaceTypeAnnotation");
  4391. }
  4392. flowParseObjectPropertyKey() {
  4393. return this.match(130) || this.match(129) ? super.parseExprAtom() : this.parseIdentifier(true);
  4394. }
  4395. flowParseObjectTypeIndexer(node, isStatic, variance) {
  4396. node.static = isStatic;
  4397. if (this.lookahead().type === 14) {
  4398. node.id = this.flowParseObjectPropertyKey();
  4399. node.key = this.flowParseTypeInitialiser();
  4400. } else {
  4401. node.id = null;
  4402. node.key = this.flowParseType();
  4403. }
  4404. this.expect(3);
  4405. node.value = this.flowParseTypeInitialiser();
  4406. node.variance = variance;
  4407. return this.finishNode(node, "ObjectTypeIndexer");
  4408. }
  4409. flowParseObjectTypeInternalSlot(node, isStatic) {
  4410. node.static = isStatic;
  4411. node.id = this.flowParseObjectPropertyKey();
  4412. this.expect(3);
  4413. this.expect(3);
  4414. if (this.match(47) || this.match(10)) {
  4415. node.method = true;
  4416. node.optional = false;
  4417. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  4418. } else {
  4419. node.method = false;
  4420. if (this.eat(17)) {
  4421. node.optional = true;
  4422. }
  4423. node.value = this.flowParseTypeInitialiser();
  4424. }
  4425. return this.finishNode(node, "ObjectTypeInternalSlot");
  4426. }
  4427. flowParseObjectTypeMethodish(node) {
  4428. node.params = [];
  4429. node.rest = null;
  4430. node.typeParameters = null;
  4431. node.this = null;
  4432. if (this.match(47)) {
  4433. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4434. }
  4435. this.expect(10);
  4436. if (this.match(78)) {
  4437. node.this = this.flowParseFunctionTypeParam(true);
  4438. node.this.name = null;
  4439. if (!this.match(11)) {
  4440. this.expect(12);
  4441. }
  4442. }
  4443. while (!this.match(11) && !this.match(21)) {
  4444. node.params.push(this.flowParseFunctionTypeParam(false));
  4445. if (!this.match(11)) {
  4446. this.expect(12);
  4447. }
  4448. }
  4449. if (this.eat(21)) {
  4450. node.rest = this.flowParseFunctionTypeParam(false);
  4451. }
  4452. this.expect(11);
  4453. node.returnType = this.flowParseTypeInitialiser();
  4454. return this.finishNode(node, "FunctionTypeAnnotation");
  4455. }
  4456. flowParseObjectTypeCallProperty(node, isStatic) {
  4457. const valueNode = this.startNode();
  4458. node.static = isStatic;
  4459. node.value = this.flowParseObjectTypeMethodish(valueNode);
  4460. return this.finishNode(node, "ObjectTypeCallProperty");
  4461. }
  4462. flowParseObjectType({
  4463. allowStatic,
  4464. allowExact,
  4465. allowSpread,
  4466. allowProto,
  4467. allowInexact
  4468. }) {
  4469. const oldInType = this.state.inType;
  4470. this.state.inType = true;
  4471. const nodeStart = this.startNode();
  4472. nodeStart.callProperties = [];
  4473. nodeStart.properties = [];
  4474. nodeStart.indexers = [];
  4475. nodeStart.internalSlots = [];
  4476. let endDelim;
  4477. let exact;
  4478. let inexact = false;
  4479. if (allowExact && this.match(6)) {
  4480. this.expect(6);
  4481. endDelim = 9;
  4482. exact = true;
  4483. } else {
  4484. this.expect(5);
  4485. endDelim = 8;
  4486. exact = false;
  4487. }
  4488. nodeStart.exact = exact;
  4489. while (!this.match(endDelim)) {
  4490. let isStatic = false;
  4491. let protoStartLoc = null;
  4492. let inexactStartLoc = null;
  4493. const node = this.startNode();
  4494. if (allowProto && this.isContextual(115)) {
  4495. const lookahead = this.lookahead();
  4496. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4497. this.next();
  4498. protoStartLoc = this.state.startLoc;
  4499. allowStatic = false;
  4500. }
  4501. }
  4502. if (allowStatic && this.isContextual(104)) {
  4503. const lookahead = this.lookahead();
  4504. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4505. this.next();
  4506. isStatic = true;
  4507. }
  4508. }
  4509. const variance = this.flowParseVariance();
  4510. if (this.eat(0)) {
  4511. if (protoStartLoc != null) {
  4512. this.unexpected(protoStartLoc);
  4513. }
  4514. if (this.eat(0)) {
  4515. if (variance) {
  4516. this.unexpected(variance.loc.start);
  4517. }
  4518. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  4519. } else {
  4520. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  4521. }
  4522. } else if (this.match(10) || this.match(47)) {
  4523. if (protoStartLoc != null) {
  4524. this.unexpected(protoStartLoc);
  4525. }
  4526. if (variance) {
  4527. this.unexpected(variance.loc.start);
  4528. }
  4529. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  4530. } else {
  4531. let kind = "init";
  4532. if (this.isContextual(98) || this.isContextual(103)) {
  4533. const lookahead = this.lookahead();
  4534. if (tokenIsLiteralPropertyName(lookahead.type)) {
  4535. kind = this.state.value;
  4536. this.next();
  4537. }
  4538. }
  4539. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  4540. if (propOrInexact === null) {
  4541. inexact = true;
  4542. inexactStartLoc = this.state.lastTokStartLoc;
  4543. } else {
  4544. nodeStart.properties.push(propOrInexact);
  4545. }
  4546. }
  4547. this.flowObjectTypeSemicolon();
  4548. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  4549. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, {
  4550. at: inexactStartLoc
  4551. });
  4552. }
  4553. }
  4554. this.expect(endDelim);
  4555. if (allowSpread) {
  4556. nodeStart.inexact = inexact;
  4557. }
  4558. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  4559. this.state.inType = oldInType;
  4560. return out;
  4561. }
  4562. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  4563. if (this.eat(21)) {
  4564. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  4565. if (isInexactToken) {
  4566. if (!allowSpread) {
  4567. this.raise(FlowErrors.InexactInsideNonObject, {
  4568. at: this.state.lastTokStartLoc
  4569. });
  4570. } else if (!allowInexact) {
  4571. this.raise(FlowErrors.InexactInsideExact, {
  4572. at: this.state.lastTokStartLoc
  4573. });
  4574. }
  4575. if (variance) {
  4576. this.raise(FlowErrors.InexactVariance, {
  4577. at: variance
  4578. });
  4579. }
  4580. return null;
  4581. }
  4582. if (!allowSpread) {
  4583. this.raise(FlowErrors.UnexpectedSpreadType, {
  4584. at: this.state.lastTokStartLoc
  4585. });
  4586. }
  4587. if (protoStartLoc != null) {
  4588. this.unexpected(protoStartLoc);
  4589. }
  4590. if (variance) {
  4591. this.raise(FlowErrors.SpreadVariance, {
  4592. at: variance
  4593. });
  4594. }
  4595. node.argument = this.flowParseType();
  4596. return this.finishNode(node, "ObjectTypeSpreadProperty");
  4597. } else {
  4598. node.key = this.flowParseObjectPropertyKey();
  4599. node.static = isStatic;
  4600. node.proto = protoStartLoc != null;
  4601. node.kind = kind;
  4602. let optional = false;
  4603. if (this.match(47) || this.match(10)) {
  4604. node.method = true;
  4605. if (protoStartLoc != null) {
  4606. this.unexpected(protoStartLoc);
  4607. }
  4608. if (variance) {
  4609. this.unexpected(variance.loc.start);
  4610. }
  4611. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  4612. if (kind === "get" || kind === "set") {
  4613. this.flowCheckGetterSetterParams(node);
  4614. }
  4615. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  4616. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  4617. at: node.value.this
  4618. });
  4619. }
  4620. } else {
  4621. if (kind !== "init") this.unexpected();
  4622. node.method = false;
  4623. if (this.eat(17)) {
  4624. optional = true;
  4625. }
  4626. node.value = this.flowParseTypeInitialiser();
  4627. node.variance = variance;
  4628. }
  4629. node.optional = optional;
  4630. return this.finishNode(node, "ObjectTypeProperty");
  4631. }
  4632. }
  4633. flowCheckGetterSetterParams(property) {
  4634. const paramCount = property.kind === "get" ? 0 : 1;
  4635. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  4636. if (property.value.this) {
  4637. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, {
  4638. at: property.value.this
  4639. });
  4640. }
  4641. if (length !== paramCount) {
  4642. this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  4643. at: property
  4644. });
  4645. }
  4646. if (property.kind === "set" && property.value.rest) {
  4647. this.raise(Errors.BadSetterRestParameter, {
  4648. at: property
  4649. });
  4650. }
  4651. }
  4652. flowObjectTypeSemicolon() {
  4653. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  4654. this.unexpected();
  4655. }
  4656. }
  4657. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  4658. startPos = startPos || this.state.start;
  4659. startLoc = startLoc || this.state.startLoc;
  4660. let node = id || this.flowParseRestrictedIdentifier(true);
  4661. while (this.eat(16)) {
  4662. const node2 = this.startNodeAt(startPos, startLoc);
  4663. node2.qualification = node;
  4664. node2.id = this.flowParseRestrictedIdentifier(true);
  4665. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  4666. }
  4667. return node;
  4668. }
  4669. flowParseGenericType(startPos, startLoc, id) {
  4670. const node = this.startNodeAt(startPos, startLoc);
  4671. node.typeParameters = null;
  4672. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  4673. if (this.match(47)) {
  4674. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4675. }
  4676. return this.finishNode(node, "GenericTypeAnnotation");
  4677. }
  4678. flowParseTypeofType() {
  4679. const node = this.startNode();
  4680. this.expect(87);
  4681. node.argument = this.flowParsePrimaryType();
  4682. return this.finishNode(node, "TypeofTypeAnnotation");
  4683. }
  4684. flowParseTupleType() {
  4685. const node = this.startNode();
  4686. node.types = [];
  4687. this.expect(0);
  4688. while (this.state.pos < this.length && !this.match(3)) {
  4689. node.types.push(this.flowParseType());
  4690. if (this.match(3)) break;
  4691. this.expect(12);
  4692. }
  4693. this.expect(3);
  4694. return this.finishNode(node, "TupleTypeAnnotation");
  4695. }
  4696. flowParseFunctionTypeParam(first) {
  4697. let name = null;
  4698. let optional = false;
  4699. let typeAnnotation = null;
  4700. const node = this.startNode();
  4701. const lh = this.lookahead();
  4702. const isThis = this.state.type === 78;
  4703. if (lh.type === 14 || lh.type === 17) {
  4704. if (isThis && !first) {
  4705. this.raise(FlowErrors.ThisParamMustBeFirst, {
  4706. at: node
  4707. });
  4708. }
  4709. name = this.parseIdentifier(isThis);
  4710. if (this.eat(17)) {
  4711. optional = true;
  4712. if (isThis) {
  4713. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  4714. at: node
  4715. });
  4716. }
  4717. }
  4718. typeAnnotation = this.flowParseTypeInitialiser();
  4719. } else {
  4720. typeAnnotation = this.flowParseType();
  4721. }
  4722. node.name = name;
  4723. node.optional = optional;
  4724. node.typeAnnotation = typeAnnotation;
  4725. return this.finishNode(node, "FunctionTypeParam");
  4726. }
  4727. reinterpretTypeAsFunctionTypeParam(type) {
  4728. const node = this.startNodeAt(type.start, type.loc.start);
  4729. node.name = null;
  4730. node.optional = false;
  4731. node.typeAnnotation = type;
  4732. return this.finishNode(node, "FunctionTypeParam");
  4733. }
  4734. flowParseFunctionTypeParams(params = []) {
  4735. let rest = null;
  4736. let _this = null;
  4737. if (this.match(78)) {
  4738. _this = this.flowParseFunctionTypeParam(true);
  4739. _this.name = null;
  4740. if (!this.match(11)) {
  4741. this.expect(12);
  4742. }
  4743. }
  4744. while (!this.match(11) && !this.match(21)) {
  4745. params.push(this.flowParseFunctionTypeParam(false));
  4746. if (!this.match(11)) {
  4747. this.expect(12);
  4748. }
  4749. }
  4750. if (this.eat(21)) {
  4751. rest = this.flowParseFunctionTypeParam(false);
  4752. }
  4753. return {
  4754. params,
  4755. rest,
  4756. _this
  4757. };
  4758. }
  4759. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  4760. switch (id.name) {
  4761. case "any":
  4762. return this.finishNode(node, "AnyTypeAnnotation");
  4763. case "bool":
  4764. case "boolean":
  4765. return this.finishNode(node, "BooleanTypeAnnotation");
  4766. case "mixed":
  4767. return this.finishNode(node, "MixedTypeAnnotation");
  4768. case "empty":
  4769. return this.finishNode(node, "EmptyTypeAnnotation");
  4770. case "number":
  4771. return this.finishNode(node, "NumberTypeAnnotation");
  4772. case "string":
  4773. return this.finishNode(node, "StringTypeAnnotation");
  4774. case "symbol":
  4775. return this.finishNode(node, "SymbolTypeAnnotation");
  4776. default:
  4777. this.checkNotUnderscore(id.name);
  4778. return this.flowParseGenericType(startPos, startLoc, id);
  4779. }
  4780. }
  4781. flowParsePrimaryType() {
  4782. const startPos = this.state.start;
  4783. const startLoc = this.state.startLoc;
  4784. const node = this.startNode();
  4785. let tmp;
  4786. let type;
  4787. let isGroupedType = false;
  4788. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4789. switch (this.state.type) {
  4790. case 5:
  4791. return this.flowParseObjectType({
  4792. allowStatic: false,
  4793. allowExact: false,
  4794. allowSpread: true,
  4795. allowProto: false,
  4796. allowInexact: true
  4797. });
  4798. case 6:
  4799. return this.flowParseObjectType({
  4800. allowStatic: false,
  4801. allowExact: true,
  4802. allowSpread: true,
  4803. allowProto: false,
  4804. allowInexact: false
  4805. });
  4806. case 0:
  4807. this.state.noAnonFunctionType = false;
  4808. type = this.flowParseTupleType();
  4809. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4810. return type;
  4811. case 47:
  4812. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4813. this.expect(10);
  4814. tmp = this.flowParseFunctionTypeParams();
  4815. node.params = tmp.params;
  4816. node.rest = tmp.rest;
  4817. node.this = tmp._this;
  4818. this.expect(11);
  4819. this.expect(19);
  4820. node.returnType = this.flowParseType();
  4821. return this.finishNode(node, "FunctionTypeAnnotation");
  4822. case 10:
  4823. this.next();
  4824. if (!this.match(11) && !this.match(21)) {
  4825. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  4826. const token = this.lookahead().type;
  4827. isGroupedType = token !== 17 && token !== 14;
  4828. } else {
  4829. isGroupedType = true;
  4830. }
  4831. }
  4832. if (isGroupedType) {
  4833. this.state.noAnonFunctionType = false;
  4834. type = this.flowParseType();
  4835. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4836. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  4837. this.expect(11);
  4838. return type;
  4839. } else {
  4840. this.eat(12);
  4841. }
  4842. }
  4843. if (type) {
  4844. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  4845. } else {
  4846. tmp = this.flowParseFunctionTypeParams();
  4847. }
  4848. node.params = tmp.params;
  4849. node.rest = tmp.rest;
  4850. node.this = tmp._this;
  4851. this.expect(11);
  4852. this.expect(19);
  4853. node.returnType = this.flowParseType();
  4854. node.typeParameters = null;
  4855. return this.finishNode(node, "FunctionTypeAnnotation");
  4856. case 129:
  4857. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  4858. case 85:
  4859. case 86:
  4860. node.value = this.match(85);
  4861. this.next();
  4862. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  4863. case 53:
  4864. if (this.state.value === "-") {
  4865. this.next();
  4866. if (this.match(130)) {
  4867. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  4868. }
  4869. if (this.match(131)) {
  4870. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  4871. }
  4872. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, {
  4873. at: this.state.startLoc
  4874. });
  4875. }
  4876. throw this.unexpected();
  4877. case 130:
  4878. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  4879. case 131:
  4880. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  4881. case 88:
  4882. this.next();
  4883. return this.finishNode(node, "VoidTypeAnnotation");
  4884. case 84:
  4885. this.next();
  4886. return this.finishNode(node, "NullLiteralTypeAnnotation");
  4887. case 78:
  4888. this.next();
  4889. return this.finishNode(node, "ThisTypeAnnotation");
  4890. case 55:
  4891. this.next();
  4892. return this.finishNode(node, "ExistsTypeAnnotation");
  4893. case 87:
  4894. return this.flowParseTypeofType();
  4895. default:
  4896. if (tokenIsKeyword(this.state.type)) {
  4897. const label = tokenLabelName(this.state.type);
  4898. this.next();
  4899. return super.createIdentifier(node, label);
  4900. } else if (tokenIsIdentifier(this.state.type)) {
  4901. if (this.isContextual(125)) {
  4902. return this.flowParseInterfaceType();
  4903. }
  4904. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  4905. }
  4906. }
  4907. throw this.unexpected();
  4908. }
  4909. flowParsePostfixType() {
  4910. const startPos = this.state.start;
  4911. const startLoc = this.state.startLoc;
  4912. let type = this.flowParsePrimaryType();
  4913. let seenOptionalIndexedAccess = false;
  4914. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  4915. const node = this.startNodeAt(startPos, startLoc);
  4916. const optional = this.eat(18);
  4917. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  4918. this.expect(0);
  4919. if (!optional && this.match(3)) {
  4920. node.elementType = type;
  4921. this.next();
  4922. type = this.finishNode(node, "ArrayTypeAnnotation");
  4923. } else {
  4924. node.objectType = type;
  4925. node.indexType = this.flowParseType();
  4926. this.expect(3);
  4927. if (seenOptionalIndexedAccess) {
  4928. node.optional = optional;
  4929. type = this.finishNode(node, "OptionalIndexedAccessType");
  4930. } else {
  4931. type = this.finishNode(node, "IndexedAccessType");
  4932. }
  4933. }
  4934. }
  4935. return type;
  4936. }
  4937. flowParsePrefixType() {
  4938. const node = this.startNode();
  4939. if (this.eat(17)) {
  4940. node.typeAnnotation = this.flowParsePrefixType();
  4941. return this.finishNode(node, "NullableTypeAnnotation");
  4942. } else {
  4943. return this.flowParsePostfixType();
  4944. }
  4945. }
  4946. flowParseAnonFunctionWithoutParens() {
  4947. const param = this.flowParsePrefixType();
  4948. if (!this.state.noAnonFunctionType && this.eat(19)) {
  4949. const node = this.startNodeAt(param.start, param.loc.start);
  4950. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  4951. node.rest = null;
  4952. node.this = null;
  4953. node.returnType = this.flowParseType();
  4954. node.typeParameters = null;
  4955. return this.finishNode(node, "FunctionTypeAnnotation");
  4956. }
  4957. return param;
  4958. }
  4959. flowParseIntersectionType() {
  4960. const node = this.startNode();
  4961. this.eat(45);
  4962. const type = this.flowParseAnonFunctionWithoutParens();
  4963. node.types = [type];
  4964. while (this.eat(45)) {
  4965. node.types.push(this.flowParseAnonFunctionWithoutParens());
  4966. }
  4967. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  4968. }
  4969. flowParseUnionType() {
  4970. const node = this.startNode();
  4971. this.eat(43);
  4972. const type = this.flowParseIntersectionType();
  4973. node.types = [type];
  4974. while (this.eat(43)) {
  4975. node.types.push(this.flowParseIntersectionType());
  4976. }
  4977. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  4978. }
  4979. flowParseType() {
  4980. const oldInType = this.state.inType;
  4981. this.state.inType = true;
  4982. const type = this.flowParseUnionType();
  4983. this.state.inType = oldInType;
  4984. return type;
  4985. }
  4986. flowParseTypeOrImplicitInstantiation() {
  4987. if (this.state.type === 128 && this.state.value === "_") {
  4988. const startPos = this.state.start;
  4989. const startLoc = this.state.startLoc;
  4990. const node = this.parseIdentifier();
  4991. return this.flowParseGenericType(startPos, startLoc, node);
  4992. } else {
  4993. return this.flowParseType();
  4994. }
  4995. }
  4996. flowParseTypeAnnotation() {
  4997. const node = this.startNode();
  4998. node.typeAnnotation = this.flowParseTypeInitialiser();
  4999. return this.finishNode(node, "TypeAnnotation");
  5000. }
  5001. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  5002. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  5003. if (this.match(14)) {
  5004. ident.typeAnnotation = this.flowParseTypeAnnotation();
  5005. this.resetEndLocation(ident);
  5006. }
  5007. return ident;
  5008. }
  5009. typeCastToParameter(node) {
  5010. node.expression.typeAnnotation = node.typeAnnotation;
  5011. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  5012. return node.expression;
  5013. }
  5014. flowParseVariance() {
  5015. let variance = null;
  5016. if (this.match(53)) {
  5017. variance = this.startNode();
  5018. if (this.state.value === "+") {
  5019. variance.kind = "plus";
  5020. } else {
  5021. variance.kind = "minus";
  5022. }
  5023. this.next();
  5024. return this.finishNode(variance, "Variance");
  5025. }
  5026. return variance;
  5027. }
  5028. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  5029. if (allowExpressionBody) {
  5030. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  5031. }
  5032. return super.parseFunctionBody(node, false, isMethod);
  5033. }
  5034. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5035. if (this.match(14)) {
  5036. const typeNode = this.startNode();
  5037. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5038. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  5039. }
  5040. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  5041. }
  5042. parseStatement(context, topLevel) {
  5043. if (this.state.strict && this.isContextual(125)) {
  5044. const lookahead = this.lookahead();
  5045. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  5046. const node = this.startNode();
  5047. this.next();
  5048. return this.flowParseInterface(node);
  5049. }
  5050. } else if (this.shouldParseEnums() && this.isContextual(122)) {
  5051. const node = this.startNode();
  5052. this.next();
  5053. return this.flowParseEnumDeclaration(node);
  5054. }
  5055. const stmt = super.parseStatement(context, topLevel);
  5056. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  5057. this.flowPragma = null;
  5058. }
  5059. return stmt;
  5060. }
  5061. parseExpressionStatement(node, expr) {
  5062. if (expr.type === "Identifier") {
  5063. if (expr.name === "declare") {
  5064. if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
  5065. return this.flowParseDeclare(node);
  5066. }
  5067. } else if (tokenIsIdentifier(this.state.type)) {
  5068. if (expr.name === "interface") {
  5069. return this.flowParseInterface(node);
  5070. } else if (expr.name === "type") {
  5071. return this.flowParseTypeAlias(node);
  5072. } else if (expr.name === "opaque") {
  5073. return this.flowParseOpaqueType(node, false);
  5074. }
  5075. }
  5076. }
  5077. return super.parseExpressionStatement(node, expr);
  5078. }
  5079. shouldParseExportDeclaration() {
  5080. const {
  5081. type
  5082. } = this.state;
  5083. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 122) {
  5084. return !this.state.containsEsc;
  5085. }
  5086. return super.shouldParseExportDeclaration();
  5087. }
  5088. isExportDefaultSpecifier() {
  5089. const {
  5090. type
  5091. } = this.state;
  5092. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 122) {
  5093. return this.state.containsEsc;
  5094. }
  5095. return super.isExportDefaultSpecifier();
  5096. }
  5097. parseExportDefaultExpression() {
  5098. if (this.shouldParseEnums() && this.isContextual(122)) {
  5099. const node = this.startNode();
  5100. this.next();
  5101. return this.flowParseEnumDeclaration(node);
  5102. }
  5103. return super.parseExportDefaultExpression();
  5104. }
  5105. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  5106. if (!this.match(17)) return expr;
  5107. if (this.state.maybeInArrowParameters) {
  5108. const nextCh = this.lookaheadCharCode();
  5109. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  5110. this.setOptionalParametersError(refExpressionErrors);
  5111. return expr;
  5112. }
  5113. }
  5114. this.expect(17);
  5115. const state = this.state.clone();
  5116. const originalNoArrowAt = this.state.noArrowAt;
  5117. const node = this.startNodeAt(startPos, startLoc);
  5118. let {
  5119. consequent,
  5120. failed
  5121. } = this.tryParseConditionalConsequent();
  5122. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5123. if (failed || invalid.length > 0) {
  5124. const noArrowAt = [...originalNoArrowAt];
  5125. if (invalid.length > 0) {
  5126. this.state = state;
  5127. this.state.noArrowAt = noArrowAt;
  5128. for (let i = 0; i < invalid.length; i++) {
  5129. noArrowAt.push(invalid[i].start);
  5130. }
  5131. ({
  5132. consequent,
  5133. failed
  5134. } = this.tryParseConditionalConsequent());
  5135. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5136. }
  5137. if (failed && valid.length > 1) {
  5138. this.raise(FlowErrors.AmbiguousConditionalArrow, {
  5139. at: state.startLoc
  5140. });
  5141. }
  5142. if (failed && valid.length === 1) {
  5143. this.state = state;
  5144. noArrowAt.push(valid[0].start);
  5145. this.state.noArrowAt = noArrowAt;
  5146. ({
  5147. consequent,
  5148. failed
  5149. } = this.tryParseConditionalConsequent());
  5150. }
  5151. }
  5152. this.getArrowLikeExpressions(consequent, true);
  5153. this.state.noArrowAt = originalNoArrowAt;
  5154. this.expect(14);
  5155. node.test = expr;
  5156. node.consequent = consequent;
  5157. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  5158. return this.finishNode(node, "ConditionalExpression");
  5159. }
  5160. tryParseConditionalConsequent() {
  5161. this.state.noArrowParamsConversionAt.push(this.state.start);
  5162. const consequent = this.parseMaybeAssignAllowIn();
  5163. const failed = !this.match(14);
  5164. this.state.noArrowParamsConversionAt.pop();
  5165. return {
  5166. consequent,
  5167. failed
  5168. };
  5169. }
  5170. getArrowLikeExpressions(node, disallowInvalid) {
  5171. const stack = [node];
  5172. const arrows = [];
  5173. while (stack.length !== 0) {
  5174. const node = stack.pop();
  5175. if (node.type === "ArrowFunctionExpression") {
  5176. if (node.typeParameters || !node.returnType) {
  5177. this.finishArrowValidation(node);
  5178. } else {
  5179. arrows.push(node);
  5180. }
  5181. stack.push(node.body);
  5182. } else if (node.type === "ConditionalExpression") {
  5183. stack.push(node.consequent);
  5184. stack.push(node.alternate);
  5185. }
  5186. }
  5187. if (disallowInvalid) {
  5188. arrows.forEach(node => this.finishArrowValidation(node));
  5189. return [arrows, []];
  5190. }
  5191. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  5192. }
  5193. finishArrowValidation(node) {
  5194. var _node$extra;
  5195. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  5196. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  5197. super.checkParams(node, false, true);
  5198. this.scope.exit();
  5199. }
  5200. forwardNoArrowParamsConversionAt(node, parse) {
  5201. let result;
  5202. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5203. this.state.noArrowParamsConversionAt.push(this.state.start);
  5204. result = parse();
  5205. this.state.noArrowParamsConversionAt.pop();
  5206. } else {
  5207. result = parse();
  5208. }
  5209. return result;
  5210. }
  5211. parseParenItem(node, startPos, startLoc) {
  5212. node = super.parseParenItem(node, startPos, startLoc);
  5213. if (this.eat(17)) {
  5214. node.optional = true;
  5215. this.resetEndLocation(node);
  5216. }
  5217. if (this.match(14)) {
  5218. const typeCastNode = this.startNodeAt(startPos, startLoc);
  5219. typeCastNode.expression = node;
  5220. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  5221. return this.finishNode(typeCastNode, "TypeCastExpression");
  5222. }
  5223. return node;
  5224. }
  5225. assertModuleNodeAllowed(node) {
  5226. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  5227. return;
  5228. }
  5229. super.assertModuleNodeAllowed(node);
  5230. }
  5231. parseExport(node) {
  5232. const decl = super.parseExport(node);
  5233. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  5234. decl.exportKind = decl.exportKind || "value";
  5235. }
  5236. return decl;
  5237. }
  5238. parseExportDeclaration(node) {
  5239. if (this.isContextual(126)) {
  5240. node.exportKind = "type";
  5241. const declarationNode = this.startNode();
  5242. this.next();
  5243. if (this.match(5)) {
  5244. node.specifiers = this.parseExportSpecifiers(true);
  5245. super.parseExportFrom(node);
  5246. return null;
  5247. } else {
  5248. return this.flowParseTypeAlias(declarationNode);
  5249. }
  5250. } else if (this.isContextual(127)) {
  5251. node.exportKind = "type";
  5252. const declarationNode = this.startNode();
  5253. this.next();
  5254. return this.flowParseOpaqueType(declarationNode, false);
  5255. } else if (this.isContextual(125)) {
  5256. node.exportKind = "type";
  5257. const declarationNode = this.startNode();
  5258. this.next();
  5259. return this.flowParseInterface(declarationNode);
  5260. } else if (this.shouldParseEnums() && this.isContextual(122)) {
  5261. node.exportKind = "value";
  5262. const declarationNode = this.startNode();
  5263. this.next();
  5264. return this.flowParseEnumDeclaration(declarationNode);
  5265. } else {
  5266. return super.parseExportDeclaration(node);
  5267. }
  5268. }
  5269. eatExportStar(node) {
  5270. if (super.eatExportStar(node)) return true;
  5271. if (this.isContextual(126) && this.lookahead().type === 55) {
  5272. node.exportKind = "type";
  5273. this.next();
  5274. this.next();
  5275. return true;
  5276. }
  5277. return false;
  5278. }
  5279. maybeParseExportNamespaceSpecifier(node) {
  5280. const {
  5281. startLoc
  5282. } = this.state;
  5283. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  5284. if (hasNamespace && node.exportKind === "type") {
  5285. this.unexpected(startLoc);
  5286. }
  5287. return hasNamespace;
  5288. }
  5289. parseClassId(node, isStatement, optionalId) {
  5290. super.parseClassId(node, isStatement, optionalId);
  5291. if (this.match(47)) {
  5292. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5293. }
  5294. }
  5295. parseClassMember(classBody, member, state) {
  5296. const {
  5297. startLoc
  5298. } = this.state;
  5299. if (this.isContextual(121)) {
  5300. if (super.parseClassMemberFromModifier(classBody, member)) {
  5301. return;
  5302. }
  5303. member.declare = true;
  5304. }
  5305. super.parseClassMember(classBody, member, state);
  5306. if (member.declare) {
  5307. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  5308. this.raise(FlowErrors.DeclareClassElement, {
  5309. at: startLoc
  5310. });
  5311. } else if (member.value) {
  5312. this.raise(FlowErrors.DeclareClassFieldInitializer, {
  5313. at: member.value
  5314. });
  5315. }
  5316. }
  5317. }
  5318. isIterator(word) {
  5319. return word === "iterator" || word === "asyncIterator";
  5320. }
  5321. readIterator() {
  5322. const word = super.readWord1();
  5323. const fullWord = "@@" + word;
  5324. if (!this.isIterator(word) || !this.state.inType) {
  5325. this.raise(Errors.InvalidIdentifier, {
  5326. at: this.state.curPosition(),
  5327. identifierName: fullWord
  5328. });
  5329. }
  5330. this.finishToken(128, fullWord);
  5331. }
  5332. getTokenFromCode(code) {
  5333. const next = this.input.charCodeAt(this.state.pos + 1);
  5334. if (code === 123 && next === 124) {
  5335. return this.finishOp(6, 2);
  5336. } else if (this.state.inType && (code === 62 || code === 60)) {
  5337. return this.finishOp(code === 62 ? 48 : 47, 1);
  5338. } else if (this.state.inType && code === 63) {
  5339. if (next === 46) {
  5340. return this.finishOp(18, 2);
  5341. }
  5342. return this.finishOp(17, 1);
  5343. } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
  5344. this.state.pos += 2;
  5345. return this.readIterator();
  5346. } else {
  5347. return super.getTokenFromCode(code);
  5348. }
  5349. }
  5350. isAssignable(node, isBinding) {
  5351. if (node.type === "TypeCastExpression") {
  5352. return this.isAssignable(node.expression, isBinding);
  5353. } else {
  5354. return super.isAssignable(node, isBinding);
  5355. }
  5356. }
  5357. toAssignable(node, isLHS = false) {
  5358. if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
  5359. node.left = this.typeCastToParameter(node.left);
  5360. }
  5361. super.toAssignable(node, isLHS);
  5362. }
  5363. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  5364. for (let i = 0; i < exprList.length; i++) {
  5365. const expr = exprList[i];
  5366. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  5367. exprList[i] = this.typeCastToParameter(expr);
  5368. }
  5369. }
  5370. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  5371. }
  5372. toReferencedList(exprList, isParenthesizedExpr) {
  5373. for (let i = 0; i < exprList.length; i++) {
  5374. var _expr$extra;
  5375. const expr = exprList[i];
  5376. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  5377. this.raise(FlowErrors.TypeCastInPattern, {
  5378. at: expr.typeAnnotation
  5379. });
  5380. }
  5381. }
  5382. return exprList;
  5383. }
  5384. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  5385. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  5386. if (canBePattern && !this.state.maybeInArrowParameters) {
  5387. this.toReferencedList(node.elements);
  5388. }
  5389. return node;
  5390. }
  5391. isValidLVal(type, isParenthesized, binding) {
  5392. return type === "TypeCastExpression" || super.isValidLVal(type, isParenthesized, binding);
  5393. }
  5394. parseClassProperty(node) {
  5395. if (this.match(14)) {
  5396. node.typeAnnotation = this.flowParseTypeAnnotation();
  5397. }
  5398. return super.parseClassProperty(node);
  5399. }
  5400. parseClassPrivateProperty(node) {
  5401. if (this.match(14)) {
  5402. node.typeAnnotation = this.flowParseTypeAnnotation();
  5403. }
  5404. return super.parseClassPrivateProperty(node);
  5405. }
  5406. isClassMethod() {
  5407. return this.match(47) || super.isClassMethod();
  5408. }
  5409. isClassProperty() {
  5410. return this.match(14) || super.isClassProperty();
  5411. }
  5412. isNonstaticConstructor(method) {
  5413. return !this.match(14) && super.isNonstaticConstructor(method);
  5414. }
  5415. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  5416. if (method.variance) {
  5417. this.unexpected(method.variance.loc.start);
  5418. }
  5419. delete method.variance;
  5420. if (this.match(47)) {
  5421. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5422. }
  5423. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  5424. if (method.params && isConstructor) {
  5425. const params = method.params;
  5426. if (params.length > 0 && this.isThisParam(params[0])) {
  5427. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5428. at: method
  5429. });
  5430. }
  5431. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  5432. const params = method.value.params;
  5433. if (params.length > 0 && this.isThisParam(params[0])) {
  5434. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5435. at: method
  5436. });
  5437. }
  5438. }
  5439. }
  5440. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  5441. if (method.variance) {
  5442. this.unexpected(method.variance.loc.start);
  5443. }
  5444. delete method.variance;
  5445. if (this.match(47)) {
  5446. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5447. }
  5448. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  5449. }
  5450. parseClassSuper(node) {
  5451. super.parseClassSuper(node);
  5452. if (node.superClass && this.match(47)) {
  5453. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  5454. }
  5455. if (this.isContextual(110)) {
  5456. this.next();
  5457. const implemented = node.implements = [];
  5458. do {
  5459. const node = this.startNode();
  5460. node.id = this.flowParseRestrictedIdentifier(true);
  5461. if (this.match(47)) {
  5462. node.typeParameters = this.flowParseTypeParameterInstantiation();
  5463. } else {
  5464. node.typeParameters = null;
  5465. }
  5466. implemented.push(this.finishNode(node, "ClassImplements"));
  5467. } while (this.eat(12));
  5468. }
  5469. }
  5470. checkGetterSetterParams(method) {
  5471. super.checkGetterSetterParams(method);
  5472. const params = this.getObjectOrClassMethodParams(method);
  5473. if (params.length > 0) {
  5474. const param = params[0];
  5475. if (this.isThisParam(param) && method.kind === "get") {
  5476. this.raise(FlowErrors.GetterMayNotHaveThisParam, {
  5477. at: param
  5478. });
  5479. } else if (this.isThisParam(param)) {
  5480. this.raise(FlowErrors.SetterMayNotHaveThisParam, {
  5481. at: param
  5482. });
  5483. }
  5484. }
  5485. }
  5486. parsePropertyNamePrefixOperator(node) {
  5487. node.variance = this.flowParseVariance();
  5488. }
  5489. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  5490. if (prop.variance) {
  5491. this.unexpected(prop.variance.loc.start);
  5492. }
  5493. delete prop.variance;
  5494. let typeParameters;
  5495. if (this.match(47) && !isAccessor) {
  5496. typeParameters = this.flowParseTypeParameterDeclaration();
  5497. if (!this.match(10)) this.unexpected();
  5498. }
  5499. const result = super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  5500. if (typeParameters) {
  5501. (result.value || result).typeParameters = typeParameters;
  5502. }
  5503. return result;
  5504. }
  5505. parseAssignableListItemTypes(param) {
  5506. if (this.eat(17)) {
  5507. if (param.type !== "Identifier") {
  5508. this.raise(FlowErrors.PatternIsOptional, {
  5509. at: param
  5510. });
  5511. }
  5512. if (this.isThisParam(param)) {
  5513. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  5514. at: param
  5515. });
  5516. }
  5517. param.optional = true;
  5518. }
  5519. if (this.match(14)) {
  5520. param.typeAnnotation = this.flowParseTypeAnnotation();
  5521. } else if (this.isThisParam(param)) {
  5522. this.raise(FlowErrors.ThisParamAnnotationRequired, {
  5523. at: param
  5524. });
  5525. }
  5526. if (this.match(29) && this.isThisParam(param)) {
  5527. this.raise(FlowErrors.ThisParamNoDefault, {
  5528. at: param
  5529. });
  5530. }
  5531. this.resetEndLocation(param);
  5532. return param;
  5533. }
  5534. parseMaybeDefault(startPos, startLoc, left) {
  5535. const node = super.parseMaybeDefault(startPos, startLoc, left);
  5536. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5537. this.raise(FlowErrors.TypeBeforeInitializer, {
  5538. at: node.typeAnnotation
  5539. });
  5540. }
  5541. return node;
  5542. }
  5543. shouldParseDefaultImport(node) {
  5544. if (!hasTypeImportKind(node)) {
  5545. return super.shouldParseDefaultImport(node);
  5546. }
  5547. return isMaybeDefaultImport(this.state.type);
  5548. }
  5549. parseImportSpecifierLocal(node, specifier, type) {
  5550. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  5551. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  5552. }
  5553. maybeParseDefaultImportSpecifier(node) {
  5554. node.importKind = "value";
  5555. let kind = null;
  5556. if (this.match(87)) {
  5557. kind = "typeof";
  5558. } else if (this.isContextual(126)) {
  5559. kind = "type";
  5560. }
  5561. if (kind) {
  5562. const lh = this.lookahead();
  5563. const {
  5564. type
  5565. } = lh;
  5566. if (kind === "type" && type === 55) {
  5567. this.unexpected(null, lh.type);
  5568. }
  5569. if (isMaybeDefaultImport(type) || type === 5 || type === 55) {
  5570. this.next();
  5571. node.importKind = kind;
  5572. }
  5573. }
  5574. return super.maybeParseDefaultImportSpecifier(node);
  5575. }
  5576. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  5577. const firstIdent = specifier.imported;
  5578. let specifierTypeKind = null;
  5579. if (firstIdent.type === "Identifier") {
  5580. if (firstIdent.name === "type") {
  5581. specifierTypeKind = "type";
  5582. } else if (firstIdent.name === "typeof") {
  5583. specifierTypeKind = "typeof";
  5584. }
  5585. }
  5586. let isBinding = false;
  5587. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  5588. const as_ident = this.parseIdentifier(true);
  5589. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5590. specifier.imported = as_ident;
  5591. specifier.importKind = specifierTypeKind;
  5592. specifier.local = cloneIdentifier(as_ident);
  5593. } else {
  5594. specifier.imported = firstIdent;
  5595. specifier.importKind = null;
  5596. specifier.local = this.parseIdentifier();
  5597. }
  5598. } else {
  5599. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5600. specifier.imported = this.parseIdentifier(true);
  5601. specifier.importKind = specifierTypeKind;
  5602. } else {
  5603. if (importedIsString) {
  5604. throw this.raise(Errors.ImportBindingIsString, {
  5605. at: specifier,
  5606. importName: firstIdent.value
  5607. });
  5608. }
  5609. specifier.imported = firstIdent;
  5610. specifier.importKind = null;
  5611. }
  5612. if (this.eatContextual(93)) {
  5613. specifier.local = this.parseIdentifier();
  5614. } else {
  5615. isBinding = true;
  5616. specifier.local = cloneIdentifier(specifier.imported);
  5617. }
  5618. }
  5619. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5620. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5621. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, {
  5622. at: specifier
  5623. });
  5624. }
  5625. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5626. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  5627. }
  5628. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5629. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  5630. }
  5631. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  5632. }
  5633. parseBindingAtom() {
  5634. switch (this.state.type) {
  5635. case 78:
  5636. return this.parseIdentifier(true);
  5637. default:
  5638. return super.parseBindingAtom();
  5639. }
  5640. }
  5641. parseFunctionParams(node, allowModifiers) {
  5642. const kind = node.kind;
  5643. if (kind !== "get" && kind !== "set" && this.match(47)) {
  5644. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5645. }
  5646. super.parseFunctionParams(node, allowModifiers);
  5647. }
  5648. parseVarId(decl, kind) {
  5649. super.parseVarId(decl, kind);
  5650. if (this.match(14)) {
  5651. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5652. this.resetEndLocation(decl.id);
  5653. }
  5654. }
  5655. parseAsyncArrowFromCallExpression(node, call) {
  5656. if (this.match(14)) {
  5657. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5658. this.state.noAnonFunctionType = true;
  5659. node.returnType = this.flowParseTypeAnnotation();
  5660. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5661. }
  5662. return super.parseAsyncArrowFromCallExpression(node, call);
  5663. }
  5664. shouldParseAsyncArrow() {
  5665. return this.match(14) || super.shouldParseAsyncArrow();
  5666. }
  5667. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5668. var _jsx;
  5669. let state = null;
  5670. let jsx;
  5671. if (this.hasPlugin("jsx") && (this.match(138) || this.match(47))) {
  5672. state = this.state.clone();
  5673. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5674. if (!jsx.error) return jsx.node;
  5675. const {
  5676. context
  5677. } = this.state;
  5678. const currentContext = context[context.length - 1];
  5679. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5680. context.pop();
  5681. }
  5682. }
  5683. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  5684. var _jsx2, _jsx3;
  5685. state = state || this.state.clone();
  5686. let typeParameters;
  5687. const arrow = this.tryParse(abort => {
  5688. var _arrowExpression$extr;
  5689. typeParameters = this.flowParseTypeParameterDeclaration();
  5690. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5691. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5692. this.resetStartLocationFromNode(result, typeParameters);
  5693. return result;
  5694. });
  5695. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5696. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5697. if (expr.type !== "ArrowFunctionExpression") abort();
  5698. expr.typeParameters = typeParameters;
  5699. this.resetStartLocationFromNode(expr, typeParameters);
  5700. return arrowExpression;
  5701. }, state);
  5702. let arrowExpression = null;
  5703. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5704. if (!arrow.error && !arrow.aborted) {
  5705. if (arrow.node.async) {
  5706. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, {
  5707. at: typeParameters
  5708. });
  5709. }
  5710. return arrow.node;
  5711. }
  5712. arrowExpression = arrow.node;
  5713. }
  5714. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5715. this.state = jsx.failState;
  5716. return jsx.node;
  5717. }
  5718. if (arrowExpression) {
  5719. this.state = arrow.failState;
  5720. return arrowExpression;
  5721. }
  5722. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5723. if (arrow.thrown) throw arrow.error;
  5724. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, {
  5725. at: typeParameters
  5726. });
  5727. }
  5728. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5729. }
  5730. parseArrow(node) {
  5731. if (this.match(14)) {
  5732. const result = this.tryParse(() => {
  5733. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5734. this.state.noAnonFunctionType = true;
  5735. const typeNode = this.startNode();
  5736. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5737. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5738. if (this.canInsertSemicolon()) this.unexpected();
  5739. if (!this.match(19)) this.unexpected();
  5740. return typeNode;
  5741. });
  5742. if (result.thrown) return null;
  5743. if (result.error) this.state = result.failState;
  5744. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5745. }
  5746. return super.parseArrow(node);
  5747. }
  5748. shouldParseArrow(params) {
  5749. return this.match(14) || super.shouldParseArrow(params);
  5750. }
  5751. setArrowFunctionParameters(node, params) {
  5752. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5753. node.params = params;
  5754. } else {
  5755. super.setArrowFunctionParameters(node, params);
  5756. }
  5757. }
  5758. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  5759. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5760. return;
  5761. }
  5762. for (let i = 0; i < node.params.length; i++) {
  5763. if (this.isThisParam(node.params[i]) && i > 0) {
  5764. this.raise(FlowErrors.ThisParamMustBeFirst, {
  5765. at: node.params[i]
  5766. });
  5767. }
  5768. }
  5769. return super.checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged);
  5770. }
  5771. parseParenAndDistinguishExpression(canBeArrow) {
  5772. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  5773. }
  5774. parseSubscripts(base, startPos, startLoc, noCalls) {
  5775. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  5776. this.next();
  5777. const node = this.startNodeAt(startPos, startLoc);
  5778. node.callee = base;
  5779. node.arguments = super.parseCallExpressionArguments(11, false);
  5780. base = this.finishNode(node, "CallExpression");
  5781. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  5782. const state = this.state.clone();
  5783. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  5784. if (!arrow.error && !arrow.aborted) return arrow.node;
  5785. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  5786. if (result.node && !result.error) return result.node;
  5787. if (arrow.node) {
  5788. this.state = arrow.failState;
  5789. return arrow.node;
  5790. }
  5791. if (result.node) {
  5792. this.state = result.failState;
  5793. return result.node;
  5794. }
  5795. throw arrow.error || result.error;
  5796. }
  5797. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  5798. }
  5799. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  5800. if (this.match(18) && this.isLookaheadToken_lt()) {
  5801. subscriptState.optionalChainMember = true;
  5802. if (noCalls) {
  5803. subscriptState.stop = true;
  5804. return base;
  5805. }
  5806. this.next();
  5807. const node = this.startNodeAt(startPos, startLoc);
  5808. node.callee = base;
  5809. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5810. this.expect(10);
  5811. node.arguments = this.parseCallExpressionArguments(11, false);
  5812. node.optional = true;
  5813. return this.finishCallExpression(node, true);
  5814. } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
  5815. const node = this.startNodeAt(startPos, startLoc);
  5816. node.callee = base;
  5817. const result = this.tryParse(() => {
  5818. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5819. this.expect(10);
  5820. node.arguments = super.parseCallExpressionArguments(11, false);
  5821. if (subscriptState.optionalChainMember) {
  5822. node.optional = false;
  5823. }
  5824. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5825. });
  5826. if (result.node) {
  5827. if (result.error) this.state = result.failState;
  5828. return result.node;
  5829. }
  5830. }
  5831. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  5832. }
  5833. parseNewCallee(node) {
  5834. super.parseNewCallee(node);
  5835. let targs = null;
  5836. if (this.shouldParseTypes() && this.match(47)) {
  5837. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5838. }
  5839. node.typeArguments = targs;
  5840. }
  5841. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  5842. const node = this.startNodeAt(startPos, startLoc);
  5843. this.parseFunctionParams(node);
  5844. if (!this.parseArrow(node)) return;
  5845. return super.parseArrowExpression(node, undefined, true);
  5846. }
  5847. readToken_mult_modulo(code) {
  5848. const next = this.input.charCodeAt(this.state.pos + 1);
  5849. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5850. this.state.hasFlowComment = false;
  5851. this.state.pos += 2;
  5852. this.nextToken();
  5853. return;
  5854. }
  5855. super.readToken_mult_modulo(code);
  5856. }
  5857. readToken_pipe_amp(code) {
  5858. const next = this.input.charCodeAt(this.state.pos + 1);
  5859. if (code === 124 && next === 125) {
  5860. this.finishOp(9, 2);
  5861. return;
  5862. }
  5863. super.readToken_pipe_amp(code);
  5864. }
  5865. parseTopLevel(file, program) {
  5866. const fileNode = super.parseTopLevel(file, program);
  5867. if (this.state.hasFlowComment) {
  5868. this.raise(FlowErrors.UnterminatedFlowComment, {
  5869. at: this.state.curPosition()
  5870. });
  5871. }
  5872. return fileNode;
  5873. }
  5874. skipBlockComment() {
  5875. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5876. if (this.state.hasFlowComment) {
  5877. throw this.raise(FlowErrors.NestedFlowComment, {
  5878. at: this.state.startLoc
  5879. });
  5880. }
  5881. this.hasFlowCommentCompletion();
  5882. const commentSkip = this.skipFlowComment();
  5883. if (commentSkip) {
  5884. this.state.pos += commentSkip;
  5885. this.state.hasFlowComment = true;
  5886. }
  5887. return;
  5888. }
  5889. if (this.state.hasFlowComment) {
  5890. const end = this.input.indexOf("*-/", this.state.pos + 2);
  5891. if (end === -1) {
  5892. throw this.raise(Errors.UnterminatedComment, {
  5893. at: this.state.curPosition()
  5894. });
  5895. }
  5896. this.state.pos = end + 2 + 3;
  5897. return;
  5898. }
  5899. return super.skipBlockComment();
  5900. }
  5901. skipFlowComment() {
  5902. const {
  5903. pos
  5904. } = this.state;
  5905. let shiftToFirstNonWhiteSpace = 2;
  5906. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5907. shiftToFirstNonWhiteSpace++;
  5908. }
  5909. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5910. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5911. if (ch2 === 58 && ch3 === 58) {
  5912. return shiftToFirstNonWhiteSpace + 2;
  5913. }
  5914. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5915. return shiftToFirstNonWhiteSpace + 12;
  5916. }
  5917. if (ch2 === 58 && ch3 !== 58) {
  5918. return shiftToFirstNonWhiteSpace;
  5919. }
  5920. return false;
  5921. }
  5922. hasFlowCommentCompletion() {
  5923. const end = this.input.indexOf("*/", this.state.pos);
  5924. if (end === -1) {
  5925. throw this.raise(Errors.UnterminatedComment, {
  5926. at: this.state.curPosition()
  5927. });
  5928. }
  5929. }
  5930. flowEnumErrorBooleanMemberNotInitialized(loc, {
  5931. enumName,
  5932. memberName
  5933. }) {
  5934. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, {
  5935. at: loc,
  5936. memberName,
  5937. enumName
  5938. });
  5939. }
  5940. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  5941. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, Object.assign({
  5942. at: loc
  5943. }, enumContext));
  5944. }
  5945. flowEnumErrorNumberMemberNotInitialized(loc, {
  5946. enumName,
  5947. memberName
  5948. }) {
  5949. this.raise(FlowErrors.EnumNumberMemberNotInitialized, {
  5950. at: loc,
  5951. enumName,
  5952. memberName
  5953. });
  5954. }
  5955. flowEnumErrorStringMemberInconsistentlyInitailized(node, {
  5956. enumName
  5957. }) {
  5958. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitailized, {
  5959. at: node,
  5960. enumName
  5961. });
  5962. }
  5963. flowEnumMemberInit() {
  5964. const startLoc = this.state.startLoc;
  5965. const endOfInit = () => this.match(12) || this.match(8);
  5966. switch (this.state.type) {
  5967. case 130:
  5968. {
  5969. const literal = this.parseNumericLiteral(this.state.value);
  5970. if (endOfInit()) {
  5971. return {
  5972. type: "number",
  5973. loc: literal.loc.start,
  5974. value: literal
  5975. };
  5976. }
  5977. return {
  5978. type: "invalid",
  5979. loc: startLoc
  5980. };
  5981. }
  5982. case 129:
  5983. {
  5984. const literal = this.parseStringLiteral(this.state.value);
  5985. if (endOfInit()) {
  5986. return {
  5987. type: "string",
  5988. loc: literal.loc.start,
  5989. value: literal
  5990. };
  5991. }
  5992. return {
  5993. type: "invalid",
  5994. loc: startLoc
  5995. };
  5996. }
  5997. case 85:
  5998. case 86:
  5999. {
  6000. const literal = this.parseBooleanLiteral(this.match(85));
  6001. if (endOfInit()) {
  6002. return {
  6003. type: "boolean",
  6004. loc: literal.loc.start,
  6005. value: literal
  6006. };
  6007. }
  6008. return {
  6009. type: "invalid",
  6010. loc: startLoc
  6011. };
  6012. }
  6013. default:
  6014. return {
  6015. type: "invalid",
  6016. loc: startLoc
  6017. };
  6018. }
  6019. }
  6020. flowEnumMemberRaw() {
  6021. const loc = this.state.startLoc;
  6022. const id = this.parseIdentifier(true);
  6023. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  6024. type: "none",
  6025. loc
  6026. };
  6027. return {
  6028. id,
  6029. init
  6030. };
  6031. }
  6032. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  6033. const {
  6034. explicitType
  6035. } = context;
  6036. if (explicitType === null) {
  6037. return;
  6038. }
  6039. if (explicitType !== expectedType) {
  6040. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  6041. }
  6042. }
  6043. flowEnumMembers({
  6044. enumName,
  6045. explicitType
  6046. }) {
  6047. const seenNames = new Set();
  6048. const members = {
  6049. booleanMembers: [],
  6050. numberMembers: [],
  6051. stringMembers: [],
  6052. defaultedMembers: []
  6053. };
  6054. let hasUnknownMembers = false;
  6055. while (!this.match(8)) {
  6056. if (this.eat(21)) {
  6057. hasUnknownMembers = true;
  6058. break;
  6059. }
  6060. const memberNode = this.startNode();
  6061. const {
  6062. id,
  6063. init
  6064. } = this.flowEnumMemberRaw();
  6065. const memberName = id.name;
  6066. if (memberName === "") {
  6067. continue;
  6068. }
  6069. if (/^[a-z]/.test(memberName)) {
  6070. this.raise(FlowErrors.EnumInvalidMemberName, {
  6071. at: id,
  6072. memberName,
  6073. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  6074. enumName
  6075. });
  6076. }
  6077. if (seenNames.has(memberName)) {
  6078. this.raise(FlowErrors.EnumDuplicateMemberName, {
  6079. at: id,
  6080. memberName,
  6081. enumName
  6082. });
  6083. }
  6084. seenNames.add(memberName);
  6085. const context = {
  6086. enumName,
  6087. explicitType,
  6088. memberName
  6089. };
  6090. memberNode.id = id;
  6091. switch (init.type) {
  6092. case "boolean":
  6093. {
  6094. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  6095. memberNode.init = init.value;
  6096. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  6097. break;
  6098. }
  6099. case "number":
  6100. {
  6101. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  6102. memberNode.init = init.value;
  6103. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  6104. break;
  6105. }
  6106. case "string":
  6107. {
  6108. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  6109. memberNode.init = init.value;
  6110. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  6111. break;
  6112. }
  6113. case "invalid":
  6114. {
  6115. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  6116. }
  6117. case "none":
  6118. {
  6119. switch (explicitType) {
  6120. case "boolean":
  6121. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  6122. break;
  6123. case "number":
  6124. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  6125. break;
  6126. default:
  6127. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  6128. }
  6129. }
  6130. }
  6131. if (!this.match(8)) {
  6132. this.expect(12);
  6133. }
  6134. }
  6135. return {
  6136. members,
  6137. hasUnknownMembers
  6138. };
  6139. }
  6140. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  6141. enumName
  6142. }) {
  6143. if (initializedMembers.length === 0) {
  6144. return defaultedMembers;
  6145. } else if (defaultedMembers.length === 0) {
  6146. return initializedMembers;
  6147. } else if (defaultedMembers.length > initializedMembers.length) {
  6148. for (const member of initializedMembers) {
  6149. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6150. enumName
  6151. });
  6152. }
  6153. return defaultedMembers;
  6154. } else {
  6155. for (const member of defaultedMembers) {
  6156. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6157. enumName
  6158. });
  6159. }
  6160. return initializedMembers;
  6161. }
  6162. }
  6163. flowEnumParseExplicitType({
  6164. enumName
  6165. }) {
  6166. if (!this.eatContextual(101)) return null;
  6167. if (!tokenIsIdentifier(this.state.type)) {
  6168. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, {
  6169. at: this.state.startLoc,
  6170. enumName
  6171. });
  6172. }
  6173. const {
  6174. value
  6175. } = this.state;
  6176. this.next();
  6177. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  6178. this.raise(FlowErrors.EnumInvalidExplicitType, {
  6179. at: this.state.startLoc,
  6180. enumName,
  6181. invalidEnumType: value
  6182. });
  6183. }
  6184. return value;
  6185. }
  6186. flowEnumBody(node, id) {
  6187. const enumName = id.name;
  6188. const nameLoc = id.loc.start;
  6189. const explicitType = this.flowEnumParseExplicitType({
  6190. enumName
  6191. });
  6192. this.expect(5);
  6193. const {
  6194. members,
  6195. hasUnknownMembers
  6196. } = this.flowEnumMembers({
  6197. enumName,
  6198. explicitType
  6199. });
  6200. node.hasUnknownMembers = hasUnknownMembers;
  6201. switch (explicitType) {
  6202. case "boolean":
  6203. node.explicitType = true;
  6204. node.members = members.booleanMembers;
  6205. this.expect(8);
  6206. return this.finishNode(node, "EnumBooleanBody");
  6207. case "number":
  6208. node.explicitType = true;
  6209. node.members = members.numberMembers;
  6210. this.expect(8);
  6211. return this.finishNode(node, "EnumNumberBody");
  6212. case "string":
  6213. node.explicitType = true;
  6214. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6215. enumName
  6216. });
  6217. this.expect(8);
  6218. return this.finishNode(node, "EnumStringBody");
  6219. case "symbol":
  6220. node.members = members.defaultedMembers;
  6221. this.expect(8);
  6222. return this.finishNode(node, "EnumSymbolBody");
  6223. default:
  6224. {
  6225. const empty = () => {
  6226. node.members = [];
  6227. this.expect(8);
  6228. return this.finishNode(node, "EnumStringBody");
  6229. };
  6230. node.explicitType = false;
  6231. const boolsLen = members.booleanMembers.length;
  6232. const numsLen = members.numberMembers.length;
  6233. const strsLen = members.stringMembers.length;
  6234. const defaultedLen = members.defaultedMembers.length;
  6235. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  6236. return empty();
  6237. } else if (!boolsLen && !numsLen) {
  6238. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6239. enumName
  6240. });
  6241. this.expect(8);
  6242. return this.finishNode(node, "EnumStringBody");
  6243. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  6244. for (const member of members.defaultedMembers) {
  6245. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  6246. enumName,
  6247. memberName: member.id.name
  6248. });
  6249. }
  6250. node.members = members.booleanMembers;
  6251. this.expect(8);
  6252. return this.finishNode(node, "EnumBooleanBody");
  6253. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  6254. for (const member of members.defaultedMembers) {
  6255. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  6256. enumName,
  6257. memberName: member.id.name
  6258. });
  6259. }
  6260. node.members = members.numberMembers;
  6261. this.expect(8);
  6262. return this.finishNode(node, "EnumNumberBody");
  6263. } else {
  6264. this.raise(FlowErrors.EnumInconsistentMemberValues, {
  6265. at: nameLoc,
  6266. enumName
  6267. });
  6268. return empty();
  6269. }
  6270. }
  6271. }
  6272. }
  6273. flowParseEnumDeclaration(node) {
  6274. const id = this.parseIdentifier();
  6275. node.id = id;
  6276. node.body = this.flowEnumBody(this.startNode(), id);
  6277. return this.finishNode(node, "EnumDeclaration");
  6278. }
  6279. isLookaheadToken_lt() {
  6280. const next = this.nextTokenStart();
  6281. if (this.input.charCodeAt(next) === 60) {
  6282. const afterNext = this.input.charCodeAt(next + 1);
  6283. return afterNext !== 60 && afterNext !== 61;
  6284. }
  6285. return false;
  6286. }
  6287. maybeUnwrapTypeCastExpression(node) {
  6288. return node.type === "TypeCastExpression" ? node.expression : node;
  6289. }
  6290. });
  6291. const entities = {
  6292. __proto__: null,
  6293. quot: "\u0022",
  6294. amp: "&",
  6295. apos: "\u0027",
  6296. lt: "<",
  6297. gt: ">",
  6298. nbsp: "\u00A0",
  6299. iexcl: "\u00A1",
  6300. cent: "\u00A2",
  6301. pound: "\u00A3",
  6302. curren: "\u00A4",
  6303. yen: "\u00A5",
  6304. brvbar: "\u00A6",
  6305. sect: "\u00A7",
  6306. uml: "\u00A8",
  6307. copy: "\u00A9",
  6308. ordf: "\u00AA",
  6309. laquo: "\u00AB",
  6310. not: "\u00AC",
  6311. shy: "\u00AD",
  6312. reg: "\u00AE",
  6313. macr: "\u00AF",
  6314. deg: "\u00B0",
  6315. plusmn: "\u00B1",
  6316. sup2: "\u00B2",
  6317. sup3: "\u00B3",
  6318. acute: "\u00B4",
  6319. micro: "\u00B5",
  6320. para: "\u00B6",
  6321. middot: "\u00B7",
  6322. cedil: "\u00B8",
  6323. sup1: "\u00B9",
  6324. ordm: "\u00BA",
  6325. raquo: "\u00BB",
  6326. frac14: "\u00BC",
  6327. frac12: "\u00BD",
  6328. frac34: "\u00BE",
  6329. iquest: "\u00BF",
  6330. Agrave: "\u00C0",
  6331. Aacute: "\u00C1",
  6332. Acirc: "\u00C2",
  6333. Atilde: "\u00C3",
  6334. Auml: "\u00C4",
  6335. Aring: "\u00C5",
  6336. AElig: "\u00C6",
  6337. Ccedil: "\u00C7",
  6338. Egrave: "\u00C8",
  6339. Eacute: "\u00C9",
  6340. Ecirc: "\u00CA",
  6341. Euml: "\u00CB",
  6342. Igrave: "\u00CC",
  6343. Iacute: "\u00CD",
  6344. Icirc: "\u00CE",
  6345. Iuml: "\u00CF",
  6346. ETH: "\u00D0",
  6347. Ntilde: "\u00D1",
  6348. Ograve: "\u00D2",
  6349. Oacute: "\u00D3",
  6350. Ocirc: "\u00D4",
  6351. Otilde: "\u00D5",
  6352. Ouml: "\u00D6",
  6353. times: "\u00D7",
  6354. Oslash: "\u00D8",
  6355. Ugrave: "\u00D9",
  6356. Uacute: "\u00DA",
  6357. Ucirc: "\u00DB",
  6358. Uuml: "\u00DC",
  6359. Yacute: "\u00DD",
  6360. THORN: "\u00DE",
  6361. szlig: "\u00DF",
  6362. agrave: "\u00E0",
  6363. aacute: "\u00E1",
  6364. acirc: "\u00E2",
  6365. atilde: "\u00E3",
  6366. auml: "\u00E4",
  6367. aring: "\u00E5",
  6368. aelig: "\u00E6",
  6369. ccedil: "\u00E7",
  6370. egrave: "\u00E8",
  6371. eacute: "\u00E9",
  6372. ecirc: "\u00EA",
  6373. euml: "\u00EB",
  6374. igrave: "\u00EC",
  6375. iacute: "\u00ED",
  6376. icirc: "\u00EE",
  6377. iuml: "\u00EF",
  6378. eth: "\u00F0",
  6379. ntilde: "\u00F1",
  6380. ograve: "\u00F2",
  6381. oacute: "\u00F3",
  6382. ocirc: "\u00F4",
  6383. otilde: "\u00F5",
  6384. ouml: "\u00F6",
  6385. divide: "\u00F7",
  6386. oslash: "\u00F8",
  6387. ugrave: "\u00F9",
  6388. uacute: "\u00FA",
  6389. ucirc: "\u00FB",
  6390. uuml: "\u00FC",
  6391. yacute: "\u00FD",
  6392. thorn: "\u00FE",
  6393. yuml: "\u00FF",
  6394. OElig: "\u0152",
  6395. oelig: "\u0153",
  6396. Scaron: "\u0160",
  6397. scaron: "\u0161",
  6398. Yuml: "\u0178",
  6399. fnof: "\u0192",
  6400. circ: "\u02C6",
  6401. tilde: "\u02DC",
  6402. Alpha: "\u0391",
  6403. Beta: "\u0392",
  6404. Gamma: "\u0393",
  6405. Delta: "\u0394",
  6406. Epsilon: "\u0395",
  6407. Zeta: "\u0396",
  6408. Eta: "\u0397",
  6409. Theta: "\u0398",
  6410. Iota: "\u0399",
  6411. Kappa: "\u039A",
  6412. Lambda: "\u039B",
  6413. Mu: "\u039C",
  6414. Nu: "\u039D",
  6415. Xi: "\u039E",
  6416. Omicron: "\u039F",
  6417. Pi: "\u03A0",
  6418. Rho: "\u03A1",
  6419. Sigma: "\u03A3",
  6420. Tau: "\u03A4",
  6421. Upsilon: "\u03A5",
  6422. Phi: "\u03A6",
  6423. Chi: "\u03A7",
  6424. Psi: "\u03A8",
  6425. Omega: "\u03A9",
  6426. alpha: "\u03B1",
  6427. beta: "\u03B2",
  6428. gamma: "\u03B3",
  6429. delta: "\u03B4",
  6430. epsilon: "\u03B5",
  6431. zeta: "\u03B6",
  6432. eta: "\u03B7",
  6433. theta: "\u03B8",
  6434. iota: "\u03B9",
  6435. kappa: "\u03BA",
  6436. lambda: "\u03BB",
  6437. mu: "\u03BC",
  6438. nu: "\u03BD",
  6439. xi: "\u03BE",
  6440. omicron: "\u03BF",
  6441. pi: "\u03C0",
  6442. rho: "\u03C1",
  6443. sigmaf: "\u03C2",
  6444. sigma: "\u03C3",
  6445. tau: "\u03C4",
  6446. upsilon: "\u03C5",
  6447. phi: "\u03C6",
  6448. chi: "\u03C7",
  6449. psi: "\u03C8",
  6450. omega: "\u03C9",
  6451. thetasym: "\u03D1",
  6452. upsih: "\u03D2",
  6453. piv: "\u03D6",
  6454. ensp: "\u2002",
  6455. emsp: "\u2003",
  6456. thinsp: "\u2009",
  6457. zwnj: "\u200C",
  6458. zwj: "\u200D",
  6459. lrm: "\u200E",
  6460. rlm: "\u200F",
  6461. ndash: "\u2013",
  6462. mdash: "\u2014",
  6463. lsquo: "\u2018",
  6464. rsquo: "\u2019",
  6465. sbquo: "\u201A",
  6466. ldquo: "\u201C",
  6467. rdquo: "\u201D",
  6468. bdquo: "\u201E",
  6469. dagger: "\u2020",
  6470. Dagger: "\u2021",
  6471. bull: "\u2022",
  6472. hellip: "\u2026",
  6473. permil: "\u2030",
  6474. prime: "\u2032",
  6475. Prime: "\u2033",
  6476. lsaquo: "\u2039",
  6477. rsaquo: "\u203A",
  6478. oline: "\u203E",
  6479. frasl: "\u2044",
  6480. euro: "\u20AC",
  6481. image: "\u2111",
  6482. weierp: "\u2118",
  6483. real: "\u211C",
  6484. trade: "\u2122",
  6485. alefsym: "\u2135",
  6486. larr: "\u2190",
  6487. uarr: "\u2191",
  6488. rarr: "\u2192",
  6489. darr: "\u2193",
  6490. harr: "\u2194",
  6491. crarr: "\u21B5",
  6492. lArr: "\u21D0",
  6493. uArr: "\u21D1",
  6494. rArr: "\u21D2",
  6495. dArr: "\u21D3",
  6496. hArr: "\u21D4",
  6497. forall: "\u2200",
  6498. part: "\u2202",
  6499. exist: "\u2203",
  6500. empty: "\u2205",
  6501. nabla: "\u2207",
  6502. isin: "\u2208",
  6503. notin: "\u2209",
  6504. ni: "\u220B",
  6505. prod: "\u220F",
  6506. sum: "\u2211",
  6507. minus: "\u2212",
  6508. lowast: "\u2217",
  6509. radic: "\u221A",
  6510. prop: "\u221D",
  6511. infin: "\u221E",
  6512. ang: "\u2220",
  6513. and: "\u2227",
  6514. or: "\u2228",
  6515. cap: "\u2229",
  6516. cup: "\u222A",
  6517. int: "\u222B",
  6518. there4: "\u2234",
  6519. sim: "\u223C",
  6520. cong: "\u2245",
  6521. asymp: "\u2248",
  6522. ne: "\u2260",
  6523. equiv: "\u2261",
  6524. le: "\u2264",
  6525. ge: "\u2265",
  6526. sub: "\u2282",
  6527. sup: "\u2283",
  6528. nsub: "\u2284",
  6529. sube: "\u2286",
  6530. supe: "\u2287",
  6531. oplus: "\u2295",
  6532. otimes: "\u2297",
  6533. perp: "\u22A5",
  6534. sdot: "\u22C5",
  6535. lceil: "\u2308",
  6536. rceil: "\u2309",
  6537. lfloor: "\u230A",
  6538. rfloor: "\u230B",
  6539. lang: "\u2329",
  6540. rang: "\u232A",
  6541. loz: "\u25CA",
  6542. spades: "\u2660",
  6543. clubs: "\u2663",
  6544. hearts: "\u2665",
  6545. diams: "\u2666"
  6546. };
  6547. const JsxErrors = ParseErrorEnum`jsx`({
  6548. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  6549. MissingClosingTagElement: ({
  6550. openingTagName
  6551. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`,
  6552. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  6553. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  6554. UnexpectedToken: ({
  6555. unexpected,
  6556. HTMLEntity
  6557. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`,
  6558. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  6559. UnterminatedJsxContent: "Unterminated JSX contents.",
  6560. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  6561. });
  6562. function isFragment(object) {
  6563. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  6564. }
  6565. function getQualifiedJSXName(object) {
  6566. if (object.type === "JSXIdentifier") {
  6567. return object.name;
  6568. }
  6569. if (object.type === "JSXNamespacedName") {
  6570. return object.namespace.name + ":" + object.name.name;
  6571. }
  6572. if (object.type === "JSXMemberExpression") {
  6573. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  6574. }
  6575. throw new Error("Node had unexpected type: " + object.type);
  6576. }
  6577. var jsx = (superClass => class JSXParserMixin extends superClass {
  6578. jsxReadToken() {
  6579. let out = "";
  6580. let chunkStart = this.state.pos;
  6581. for (;;) {
  6582. if (this.state.pos >= this.length) {
  6583. throw this.raise(JsxErrors.UnterminatedJsxContent, {
  6584. at: this.state.startLoc
  6585. });
  6586. }
  6587. const ch = this.input.charCodeAt(this.state.pos);
  6588. switch (ch) {
  6589. case 60:
  6590. case 123:
  6591. if (this.state.pos === this.state.start) {
  6592. if (ch === 60 && this.state.canStartJSXElement) {
  6593. ++this.state.pos;
  6594. return this.finishToken(138);
  6595. }
  6596. return super.getTokenFromCode(ch);
  6597. }
  6598. out += this.input.slice(chunkStart, this.state.pos);
  6599. return this.finishToken(137, out);
  6600. case 38:
  6601. out += this.input.slice(chunkStart, this.state.pos);
  6602. out += this.jsxReadEntity();
  6603. chunkStart = this.state.pos;
  6604. break;
  6605. case 62:
  6606. case 125:
  6607. default:
  6608. if (isNewLine(ch)) {
  6609. out += this.input.slice(chunkStart, this.state.pos);
  6610. out += this.jsxReadNewLine(true);
  6611. chunkStart = this.state.pos;
  6612. } else {
  6613. ++this.state.pos;
  6614. }
  6615. }
  6616. }
  6617. }
  6618. jsxReadNewLine(normalizeCRLF) {
  6619. const ch = this.input.charCodeAt(this.state.pos);
  6620. let out;
  6621. ++this.state.pos;
  6622. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6623. ++this.state.pos;
  6624. out = normalizeCRLF ? "\n" : "\r\n";
  6625. } else {
  6626. out = String.fromCharCode(ch);
  6627. }
  6628. ++this.state.curLine;
  6629. this.state.lineStart = this.state.pos;
  6630. return out;
  6631. }
  6632. jsxReadString(quote) {
  6633. let out = "";
  6634. let chunkStart = ++this.state.pos;
  6635. for (;;) {
  6636. if (this.state.pos >= this.length) {
  6637. throw this.raise(Errors.UnterminatedString, {
  6638. at: this.state.startLoc
  6639. });
  6640. }
  6641. const ch = this.input.charCodeAt(this.state.pos);
  6642. if (ch === quote) break;
  6643. if (ch === 38) {
  6644. out += this.input.slice(chunkStart, this.state.pos);
  6645. out += this.jsxReadEntity();
  6646. chunkStart = this.state.pos;
  6647. } else if (isNewLine(ch)) {
  6648. out += this.input.slice(chunkStart, this.state.pos);
  6649. out += this.jsxReadNewLine(false);
  6650. chunkStart = this.state.pos;
  6651. } else {
  6652. ++this.state.pos;
  6653. }
  6654. }
  6655. out += this.input.slice(chunkStart, this.state.pos++);
  6656. return this.finishToken(129, out);
  6657. }
  6658. jsxReadEntity() {
  6659. const startPos = ++this.state.pos;
  6660. if (this.codePointAtPos(this.state.pos) === 35) {
  6661. ++this.state.pos;
  6662. let radix = 10;
  6663. if (this.codePointAtPos(this.state.pos) === 120) {
  6664. radix = 16;
  6665. ++this.state.pos;
  6666. }
  6667. const codePoint = this.readInt(radix, undefined, false, "bail");
  6668. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  6669. ++this.state.pos;
  6670. return String.fromCodePoint(codePoint);
  6671. }
  6672. } else {
  6673. let count = 0;
  6674. let semi = false;
  6675. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) == 59)) {
  6676. ++this.state.pos;
  6677. }
  6678. if (semi) {
  6679. const desc = this.input.slice(startPos, this.state.pos);
  6680. const entity = entities[desc];
  6681. ++this.state.pos;
  6682. if (entity) {
  6683. return entity;
  6684. }
  6685. }
  6686. }
  6687. this.state.pos = startPos;
  6688. return "&";
  6689. }
  6690. jsxReadWord() {
  6691. let ch;
  6692. const start = this.state.pos;
  6693. do {
  6694. ch = this.input.charCodeAt(++this.state.pos);
  6695. } while (isIdentifierChar(ch) || ch === 45);
  6696. return this.finishToken(136, this.input.slice(start, this.state.pos));
  6697. }
  6698. jsxParseIdentifier() {
  6699. const node = this.startNode();
  6700. if (this.match(136)) {
  6701. node.name = this.state.value;
  6702. } else if (tokenIsKeyword(this.state.type)) {
  6703. node.name = tokenLabelName(this.state.type);
  6704. } else {
  6705. this.unexpected();
  6706. }
  6707. this.next();
  6708. return this.finishNode(node, "JSXIdentifier");
  6709. }
  6710. jsxParseNamespacedName() {
  6711. const startPos = this.state.start;
  6712. const startLoc = this.state.startLoc;
  6713. const name = this.jsxParseIdentifier();
  6714. if (!this.eat(14)) return name;
  6715. const node = this.startNodeAt(startPos, startLoc);
  6716. node.namespace = name;
  6717. node.name = this.jsxParseIdentifier();
  6718. return this.finishNode(node, "JSXNamespacedName");
  6719. }
  6720. jsxParseElementName() {
  6721. const startPos = this.state.start;
  6722. const startLoc = this.state.startLoc;
  6723. let node = this.jsxParseNamespacedName();
  6724. if (node.type === "JSXNamespacedName") {
  6725. return node;
  6726. }
  6727. while (this.eat(16)) {
  6728. const newNode = this.startNodeAt(startPos, startLoc);
  6729. newNode.object = node;
  6730. newNode.property = this.jsxParseIdentifier();
  6731. node = this.finishNode(newNode, "JSXMemberExpression");
  6732. }
  6733. return node;
  6734. }
  6735. jsxParseAttributeValue() {
  6736. let node;
  6737. switch (this.state.type) {
  6738. case 5:
  6739. node = this.startNode();
  6740. this.setContext(types.brace);
  6741. this.next();
  6742. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6743. if (node.expression.type === "JSXEmptyExpression") {
  6744. this.raise(JsxErrors.AttributeIsEmpty, {
  6745. at: node
  6746. });
  6747. }
  6748. return node;
  6749. case 138:
  6750. case 129:
  6751. return this.parseExprAtom();
  6752. default:
  6753. throw this.raise(JsxErrors.UnsupportedJsxValue, {
  6754. at: this.state.startLoc
  6755. });
  6756. }
  6757. }
  6758. jsxParseEmptyExpression() {
  6759. const node = this.startNodeAt(this.state.lastTokEndLoc.index, this.state.lastTokEndLoc);
  6760. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  6761. }
  6762. jsxParseSpreadChild(node) {
  6763. this.next();
  6764. node.expression = this.parseExpression();
  6765. this.setContext(types.j_oTag);
  6766. this.state.canStartJSXElement = true;
  6767. this.expect(8);
  6768. return this.finishNode(node, "JSXSpreadChild");
  6769. }
  6770. jsxParseExpressionContainer(node, previousContext) {
  6771. if (this.match(8)) {
  6772. node.expression = this.jsxParseEmptyExpression();
  6773. } else {
  6774. const expression = this.parseExpression();
  6775. node.expression = expression;
  6776. }
  6777. this.setContext(previousContext);
  6778. this.state.canStartJSXElement = true;
  6779. this.expect(8);
  6780. return this.finishNode(node, "JSXExpressionContainer");
  6781. }
  6782. jsxParseAttribute() {
  6783. const node = this.startNode();
  6784. if (this.match(5)) {
  6785. this.setContext(types.brace);
  6786. this.next();
  6787. this.expect(21);
  6788. node.argument = this.parseMaybeAssignAllowIn();
  6789. this.setContext(types.j_oTag);
  6790. this.state.canStartJSXElement = true;
  6791. this.expect(8);
  6792. return this.finishNode(node, "JSXSpreadAttribute");
  6793. }
  6794. node.name = this.jsxParseNamespacedName();
  6795. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6796. return this.finishNode(node, "JSXAttribute");
  6797. }
  6798. jsxParseOpeningElementAt(startPos, startLoc) {
  6799. const node = this.startNodeAt(startPos, startLoc);
  6800. if (this.eat(139)) {
  6801. return this.finishNode(node, "JSXOpeningFragment");
  6802. }
  6803. node.name = this.jsxParseElementName();
  6804. return this.jsxParseOpeningElementAfterName(node);
  6805. }
  6806. jsxParseOpeningElementAfterName(node) {
  6807. const attributes = [];
  6808. while (!this.match(56) && !this.match(139)) {
  6809. attributes.push(this.jsxParseAttribute());
  6810. }
  6811. node.attributes = attributes;
  6812. node.selfClosing = this.eat(56);
  6813. this.expect(139);
  6814. return this.finishNode(node, "JSXOpeningElement");
  6815. }
  6816. jsxParseClosingElementAt(startPos, startLoc) {
  6817. const node = this.startNodeAt(startPos, startLoc);
  6818. if (this.eat(139)) {
  6819. return this.finishNode(node, "JSXClosingFragment");
  6820. }
  6821. node.name = this.jsxParseElementName();
  6822. this.expect(139);
  6823. return this.finishNode(node, "JSXClosingElement");
  6824. }
  6825. jsxParseElementAt(startPos, startLoc) {
  6826. const node = this.startNodeAt(startPos, startLoc);
  6827. const children = [];
  6828. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  6829. let closingElement = null;
  6830. if (!openingElement.selfClosing) {
  6831. contents: for (;;) {
  6832. switch (this.state.type) {
  6833. case 138:
  6834. startPos = this.state.start;
  6835. startLoc = this.state.startLoc;
  6836. this.next();
  6837. if (this.eat(56)) {
  6838. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  6839. break contents;
  6840. }
  6841. children.push(this.jsxParseElementAt(startPos, startLoc));
  6842. break;
  6843. case 137:
  6844. children.push(this.parseExprAtom());
  6845. break;
  6846. case 5:
  6847. {
  6848. const node = this.startNode();
  6849. this.setContext(types.brace);
  6850. this.next();
  6851. if (this.match(21)) {
  6852. children.push(this.jsxParseSpreadChild(node));
  6853. } else {
  6854. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6855. }
  6856. break;
  6857. }
  6858. default:
  6859. throw this.unexpected();
  6860. }
  6861. }
  6862. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  6863. this.raise(JsxErrors.MissingClosingTagFragment, {
  6864. at: closingElement
  6865. });
  6866. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6867. this.raise(JsxErrors.MissingClosingTagElement, {
  6868. at: closingElement,
  6869. openingTagName: getQualifiedJSXName(openingElement.name)
  6870. });
  6871. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6872. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6873. this.raise(JsxErrors.MissingClosingTagElement, {
  6874. at: closingElement,
  6875. openingTagName: getQualifiedJSXName(openingElement.name)
  6876. });
  6877. }
  6878. }
  6879. }
  6880. if (isFragment(openingElement)) {
  6881. node.openingFragment = openingElement;
  6882. node.closingFragment = closingElement;
  6883. } else {
  6884. node.openingElement = openingElement;
  6885. node.closingElement = closingElement;
  6886. }
  6887. node.children = children;
  6888. if (this.match(47)) {
  6889. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, {
  6890. at: this.state.startLoc
  6891. });
  6892. }
  6893. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6894. }
  6895. jsxParseElement() {
  6896. const startPos = this.state.start;
  6897. const startLoc = this.state.startLoc;
  6898. this.next();
  6899. return this.jsxParseElementAt(startPos, startLoc);
  6900. }
  6901. setContext(newContext) {
  6902. const {
  6903. context
  6904. } = this.state;
  6905. context[context.length - 1] = newContext;
  6906. }
  6907. parseExprAtom(refExpressionErrors) {
  6908. if (this.match(137)) {
  6909. return this.parseLiteral(this.state.value, "JSXText");
  6910. } else if (this.match(138)) {
  6911. return this.jsxParseElement();
  6912. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  6913. this.replaceToken(138);
  6914. return this.jsxParseElement();
  6915. } else {
  6916. return super.parseExprAtom(refExpressionErrors);
  6917. }
  6918. }
  6919. skipSpace() {
  6920. const curContext = this.curContext();
  6921. if (!curContext.preserveSpace) super.skipSpace();
  6922. }
  6923. getTokenFromCode(code) {
  6924. const context = this.curContext();
  6925. if (context === types.j_expr) {
  6926. return this.jsxReadToken();
  6927. }
  6928. if (context === types.j_oTag || context === types.j_cTag) {
  6929. if (isIdentifierStart(code)) {
  6930. return this.jsxReadWord();
  6931. }
  6932. if (code === 62) {
  6933. ++this.state.pos;
  6934. return this.finishToken(139);
  6935. }
  6936. if ((code === 34 || code === 39) && context === types.j_oTag) {
  6937. return this.jsxReadString(code);
  6938. }
  6939. }
  6940. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  6941. ++this.state.pos;
  6942. return this.finishToken(138);
  6943. }
  6944. return super.getTokenFromCode(code);
  6945. }
  6946. updateContext(prevType) {
  6947. const {
  6948. context,
  6949. type
  6950. } = this.state;
  6951. if (type === 56 && prevType === 138) {
  6952. context.splice(-2, 2, types.j_cTag);
  6953. this.state.canStartJSXElement = false;
  6954. } else if (type === 138) {
  6955. context.push(types.j_oTag);
  6956. } else if (type === 139) {
  6957. const out = context[context.length - 1];
  6958. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  6959. context.pop();
  6960. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  6961. } else {
  6962. this.setContext(types.j_expr);
  6963. this.state.canStartJSXElement = true;
  6964. }
  6965. } else {
  6966. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  6967. }
  6968. }
  6969. });
  6970. class TypeScriptScope extends Scope {
  6971. constructor(...args) {
  6972. super(...args);
  6973. this.types = new Set();
  6974. this.enums = new Set();
  6975. this.constEnums = new Set();
  6976. this.classes = new Set();
  6977. this.exportOnlyBindings = new Set();
  6978. }
  6979. }
  6980. class TypeScriptScopeHandler extends ScopeHandler {
  6981. createScope(flags) {
  6982. return new TypeScriptScope(flags);
  6983. }
  6984. declareName(name, bindingType, loc) {
  6985. const scope = this.currentScope();
  6986. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  6987. this.maybeExportDefined(scope, name);
  6988. scope.exportOnlyBindings.add(name);
  6989. return;
  6990. }
  6991. super.declareName(name, bindingType, loc);
  6992. if (bindingType & BIND_KIND_TYPE) {
  6993. if (!(bindingType & BIND_KIND_VALUE)) {
  6994. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  6995. this.maybeExportDefined(scope, name);
  6996. }
  6997. scope.types.add(name);
  6998. }
  6999. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  7000. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  7001. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  7002. }
  7003. isRedeclaredInScope(scope, name, bindingType) {
  7004. if (scope.enums.has(name)) {
  7005. if (bindingType & BIND_FLAGS_TS_ENUM) {
  7006. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  7007. const wasConst = scope.constEnums.has(name);
  7008. return isConst !== wasConst;
  7009. }
  7010. return true;
  7011. }
  7012. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  7013. if (scope.lexical.has(name)) {
  7014. return !!(bindingType & BIND_KIND_VALUE);
  7015. } else {
  7016. return false;
  7017. }
  7018. }
  7019. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  7020. return true;
  7021. }
  7022. return super.isRedeclaredInScope(scope, name, bindingType);
  7023. }
  7024. checkLocalExport(id) {
  7025. const topLevelScope = this.scopeStack[0];
  7026. const {
  7027. name
  7028. } = id;
  7029. if (!topLevelScope.types.has(name) && !topLevelScope.exportOnlyBindings.has(name)) {
  7030. super.checkLocalExport(id);
  7031. }
  7032. }
  7033. }
  7034. const getOwn$1 = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  7035. function nonNull(x) {
  7036. if (x == null) {
  7037. throw new Error(`Unexpected ${x} value.`);
  7038. }
  7039. return x;
  7040. }
  7041. function assert(x) {
  7042. if (!x) {
  7043. throw new Error("Assert fail");
  7044. }
  7045. }
  7046. const TSErrors = ParseErrorEnum`typescript`({
  7047. AbstractMethodHasImplementation: ({
  7048. methodName
  7049. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`,
  7050. AbstractPropertyHasInitializer: ({
  7051. propertyName
  7052. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`,
  7053. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  7054. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  7055. CannotFindName: ({
  7056. name
  7057. }) => `Cannot find name '${name}'.`,
  7058. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  7059. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  7060. ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.",
  7061. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  7062. DeclareAccessor: ({
  7063. kind
  7064. }) => `'declare' is not allowed in ${kind}ters.`,
  7065. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  7066. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  7067. DuplicateAccessibilityModifier: ({
  7068. modifier
  7069. }) => `Accessibility modifier already seen.`,
  7070. DuplicateModifier: ({
  7071. modifier
  7072. }) => `Duplicate modifier: '${modifier}'.`,
  7073. EmptyHeritageClauseType: ({
  7074. token
  7075. }) => `'${token}' list cannot be empty.`,
  7076. EmptyTypeArguments: "Type argument list cannot be empty.",
  7077. EmptyTypeParameters: "Type parameter list cannot be empty.",
  7078. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  7079. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  7080. IncompatibleModifiers: ({
  7081. modifiers
  7082. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`,
  7083. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  7084. IndexSignatureHasAccessibility: ({
  7085. modifier
  7086. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`,
  7087. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  7088. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  7089. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  7090. InitializerNotAllowedInAmbientContext: "Initializers are not allowed in ambient contexts.",
  7091. InvalidModifierOnTypeMember: ({
  7092. modifier
  7093. }) => `'${modifier}' modifier cannot appear on a type member.`,
  7094. InvalidModifierOnTypeParameter: ({
  7095. modifier
  7096. }) => `'${modifier}' modifier cannot appear on a type parameter.`,
  7097. InvalidModifierOnTypeParameterPositions: ({
  7098. modifier
  7099. }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`,
  7100. InvalidModifiersOrder: ({
  7101. orderedModifiers
  7102. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
  7103. InvalidPropertyAccessAfterInstantiationExpression: "Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
  7104. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  7105. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  7106. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  7107. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  7108. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  7109. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  7110. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  7111. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  7112. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  7113. PrivateElementHasAccessibility: ({
  7114. modifier
  7115. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`,
  7116. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  7117. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  7118. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  7119. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  7120. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  7121. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  7122. SingleTypeParameterWithoutTrailingComma: ({
  7123. typeParameterName
  7124. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`,
  7125. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  7126. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  7127. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  7128. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  7129. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  7130. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  7131. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  7132. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  7133. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  7134. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  7135. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  7136. UnsupportedSignatureParameterKind: ({
  7137. type
  7138. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`
  7139. });
  7140. function keywordTypeFromName(value) {
  7141. switch (value) {
  7142. case "any":
  7143. return "TSAnyKeyword";
  7144. case "boolean":
  7145. return "TSBooleanKeyword";
  7146. case "bigint":
  7147. return "TSBigIntKeyword";
  7148. case "never":
  7149. return "TSNeverKeyword";
  7150. case "number":
  7151. return "TSNumberKeyword";
  7152. case "object":
  7153. return "TSObjectKeyword";
  7154. case "string":
  7155. return "TSStringKeyword";
  7156. case "symbol":
  7157. return "TSSymbolKeyword";
  7158. case "undefined":
  7159. return "TSUndefinedKeyword";
  7160. case "unknown":
  7161. return "TSUnknownKeyword";
  7162. default:
  7163. return undefined;
  7164. }
  7165. }
  7166. function tsIsAccessModifier(modifier) {
  7167. return modifier === "private" || modifier === "public" || modifier === "protected";
  7168. }
  7169. function tsIsVarianceAnnotations(modifier) {
  7170. return modifier === "in" || modifier === "out";
  7171. }
  7172. var typescript = (superClass => class TypeScriptParserMixin extends superClass {
  7173. getScopeHandler() {
  7174. return TypeScriptScopeHandler;
  7175. }
  7176. tsIsIdentifier() {
  7177. return tokenIsIdentifier(this.state.type);
  7178. }
  7179. tsTokenCanFollowModifier() {
  7180. return (this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(134) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  7181. }
  7182. tsNextTokenCanFollowModifier() {
  7183. this.next();
  7184. return this.tsTokenCanFollowModifier();
  7185. }
  7186. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  7187. if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58) {
  7188. return undefined;
  7189. }
  7190. const modifier = this.state.value;
  7191. if (allowedModifiers.indexOf(modifier) !== -1) {
  7192. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7193. return undefined;
  7194. }
  7195. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7196. return modifier;
  7197. }
  7198. }
  7199. return undefined;
  7200. }
  7201. tsParseModifiers({
  7202. modified,
  7203. allowedModifiers,
  7204. disallowedModifiers,
  7205. stopOnStartOfClassStaticBlock,
  7206. errorTemplate = TSErrors.InvalidModifierOnTypeMember
  7207. }) {
  7208. const enforceOrder = (loc, modifier, before, after) => {
  7209. if (modifier === before && modified[after]) {
  7210. this.raise(TSErrors.InvalidModifiersOrder, {
  7211. at: loc,
  7212. orderedModifiers: [before, after]
  7213. });
  7214. }
  7215. };
  7216. const incompatible = (loc, modifier, mod1, mod2) => {
  7217. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7218. this.raise(TSErrors.IncompatibleModifiers, {
  7219. at: loc,
  7220. modifiers: [mod1, mod2]
  7221. });
  7222. }
  7223. };
  7224. for (;;) {
  7225. const {
  7226. startLoc
  7227. } = this.state;
  7228. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  7229. if (!modifier) break;
  7230. if (tsIsAccessModifier(modifier)) {
  7231. if (modified.accessibility) {
  7232. this.raise(TSErrors.DuplicateAccessibilityModifier, {
  7233. at: startLoc,
  7234. modifier
  7235. });
  7236. } else {
  7237. enforceOrder(startLoc, modifier, modifier, "override");
  7238. enforceOrder(startLoc, modifier, modifier, "static");
  7239. enforceOrder(startLoc, modifier, modifier, "readonly");
  7240. modified.accessibility = modifier;
  7241. }
  7242. } else if (tsIsVarianceAnnotations(modifier)) {
  7243. if (modified[modifier]) {
  7244. this.raise(TSErrors.DuplicateModifier, {
  7245. at: startLoc,
  7246. modifier
  7247. });
  7248. }
  7249. modified[modifier] = true;
  7250. enforceOrder(startLoc, modifier, "in", "out");
  7251. } else {
  7252. if (Object.hasOwnProperty.call(modified, modifier)) {
  7253. this.raise(TSErrors.DuplicateModifier, {
  7254. at: startLoc,
  7255. modifier
  7256. });
  7257. } else {
  7258. enforceOrder(startLoc, modifier, "static", "readonly");
  7259. enforceOrder(startLoc, modifier, "static", "override");
  7260. enforceOrder(startLoc, modifier, "override", "readonly");
  7261. enforceOrder(startLoc, modifier, "abstract", "override");
  7262. incompatible(startLoc, modifier, "declare", "override");
  7263. incompatible(startLoc, modifier, "static", "abstract");
  7264. }
  7265. modified[modifier] = true;
  7266. }
  7267. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7268. this.raise(errorTemplate, {
  7269. at: startLoc,
  7270. modifier
  7271. });
  7272. }
  7273. }
  7274. }
  7275. tsIsListTerminator(kind) {
  7276. switch (kind) {
  7277. case "EnumMembers":
  7278. case "TypeMembers":
  7279. return this.match(8);
  7280. case "HeritageClauseElement":
  7281. return this.match(5);
  7282. case "TupleElementTypes":
  7283. return this.match(3);
  7284. case "TypeParametersOrArguments":
  7285. return this.match(48);
  7286. }
  7287. throw new Error("Unreachable");
  7288. }
  7289. tsParseList(kind, parseElement) {
  7290. const result = [];
  7291. while (!this.tsIsListTerminator(kind)) {
  7292. result.push(parseElement());
  7293. }
  7294. return result;
  7295. }
  7296. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7297. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7298. }
  7299. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7300. const result = [];
  7301. let trailingCommaPos = -1;
  7302. for (;;) {
  7303. if (this.tsIsListTerminator(kind)) {
  7304. break;
  7305. }
  7306. trailingCommaPos = -1;
  7307. const element = parseElement();
  7308. if (element == null) {
  7309. return undefined;
  7310. }
  7311. result.push(element);
  7312. if (this.eat(12)) {
  7313. trailingCommaPos = this.state.lastTokStart;
  7314. continue;
  7315. }
  7316. if (this.tsIsListTerminator(kind)) {
  7317. break;
  7318. }
  7319. if (expectSuccess) {
  7320. this.expect(12);
  7321. }
  7322. return undefined;
  7323. }
  7324. if (refTrailingCommaPos) {
  7325. refTrailingCommaPos.value = trailingCommaPos;
  7326. }
  7327. return result;
  7328. }
  7329. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7330. if (!skipFirstToken) {
  7331. if (bracket) {
  7332. this.expect(0);
  7333. } else {
  7334. this.expect(47);
  7335. }
  7336. }
  7337. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7338. if (bracket) {
  7339. this.expect(3);
  7340. } else {
  7341. this.expect(48);
  7342. }
  7343. return result;
  7344. }
  7345. tsParseImportType() {
  7346. const node = this.startNode();
  7347. this.expect(83);
  7348. this.expect(10);
  7349. if (!this.match(129)) {
  7350. this.raise(TSErrors.UnsupportedImportTypeArgument, {
  7351. at: this.state.startLoc
  7352. });
  7353. }
  7354. node.argument = super.parseExprAtom();
  7355. this.expect(11);
  7356. if (this.eat(16)) {
  7357. node.qualifier = this.tsParseEntityName();
  7358. }
  7359. if (this.match(47)) {
  7360. node.typeParameters = this.tsParseTypeArguments();
  7361. }
  7362. return this.finishNode(node, "TSImportType");
  7363. }
  7364. tsParseEntityName(allowReservedWords = true) {
  7365. let entity = this.parseIdentifier(allowReservedWords);
  7366. while (this.eat(16)) {
  7367. const node = this.startNodeAtNode(entity);
  7368. node.left = entity;
  7369. node.right = this.parseIdentifier(allowReservedWords);
  7370. entity = this.finishNode(node, "TSQualifiedName");
  7371. }
  7372. return entity;
  7373. }
  7374. tsParseTypeReference() {
  7375. const node = this.startNode();
  7376. node.typeName = this.tsParseEntityName();
  7377. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7378. node.typeParameters = this.tsParseTypeArguments();
  7379. }
  7380. return this.finishNode(node, "TSTypeReference");
  7381. }
  7382. tsParseThisTypePredicate(lhs) {
  7383. this.next();
  7384. const node = this.startNodeAtNode(lhs);
  7385. node.parameterName = lhs;
  7386. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  7387. node.asserts = false;
  7388. return this.finishNode(node, "TSTypePredicate");
  7389. }
  7390. tsParseThisTypeNode() {
  7391. const node = this.startNode();
  7392. this.next();
  7393. return this.finishNode(node, "TSThisType");
  7394. }
  7395. tsParseTypeQuery() {
  7396. const node = this.startNode();
  7397. this.expect(87);
  7398. if (this.match(83)) {
  7399. node.exprName = this.tsParseImportType();
  7400. } else {
  7401. node.exprName = this.tsParseEntityName();
  7402. }
  7403. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7404. node.typeParameters = this.tsParseTypeArguments();
  7405. }
  7406. return this.finishNode(node, "TSTypeQuery");
  7407. }
  7408. tsParseInOutModifiers(node) {
  7409. this.tsParseModifiers({
  7410. modified: node,
  7411. allowedModifiers: ["in", "out"],
  7412. disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7413. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7414. });
  7415. }
  7416. tsParseNoneModifiers(node) {
  7417. this.tsParseModifiers({
  7418. modified: node,
  7419. allowedModifiers: [],
  7420. disallowedModifiers: ["in", "out"],
  7421. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  7422. });
  7423. }
  7424. tsParseTypeParameter(parseModifiers = this.tsParseNoneModifiers.bind(this)) {
  7425. const node = this.startNode();
  7426. parseModifiers(node);
  7427. node.name = this.tsParseTypeParameterName();
  7428. node.constraint = this.tsEatThenParseType(81);
  7429. node.default = this.tsEatThenParseType(29);
  7430. return this.finishNode(node, "TSTypeParameter");
  7431. }
  7432. tsTryParseTypeParameters(parseModifiers) {
  7433. if (this.match(47)) {
  7434. return this.tsParseTypeParameters(parseModifiers);
  7435. }
  7436. }
  7437. tsParseTypeParameters(parseModifiers) {
  7438. const node = this.startNode();
  7439. if (this.match(47) || this.match(138)) {
  7440. this.next();
  7441. } else {
  7442. this.unexpected();
  7443. }
  7444. const refTrailingCommaPos = {
  7445. value: -1
  7446. };
  7447. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
  7448. if (node.params.length === 0) {
  7449. this.raise(TSErrors.EmptyTypeParameters, {
  7450. at: node
  7451. });
  7452. }
  7453. if (refTrailingCommaPos.value !== -1) {
  7454. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  7455. }
  7456. return this.finishNode(node, "TSTypeParameterDeclaration");
  7457. }
  7458. tsTryNextParseConstantContext() {
  7459. if (this.lookahead().type !== 75) return null;
  7460. this.next();
  7461. const typeReference = this.tsParseTypeReference();
  7462. if (typeReference.typeParameters) {
  7463. this.raise(TSErrors.CannotFindName, {
  7464. at: typeReference.typeName,
  7465. name: "const"
  7466. });
  7467. }
  7468. return typeReference;
  7469. }
  7470. tsFillSignature(returnToken, signature) {
  7471. const returnTokenRequired = returnToken === 19;
  7472. const paramsKey = "parameters";
  7473. const returnTypeKey = "typeAnnotation";
  7474. signature.typeParameters = this.tsTryParseTypeParameters();
  7475. this.expect(10);
  7476. signature[paramsKey] = this.tsParseBindingListForSignature();
  7477. if (returnTokenRequired) {
  7478. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7479. } else if (this.match(returnToken)) {
  7480. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7481. }
  7482. }
  7483. tsParseBindingListForSignature() {
  7484. return super.parseBindingList(11, 41).map(pattern => {
  7485. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  7486. this.raise(TSErrors.UnsupportedSignatureParameterKind, {
  7487. at: pattern,
  7488. type: pattern.type
  7489. });
  7490. }
  7491. return pattern;
  7492. });
  7493. }
  7494. tsParseTypeMemberSemicolon() {
  7495. if (!this.eat(12) && !this.isLineTerminator()) {
  7496. this.expect(13);
  7497. }
  7498. }
  7499. tsParseSignatureMember(kind, node) {
  7500. this.tsFillSignature(14, node);
  7501. this.tsParseTypeMemberSemicolon();
  7502. return this.finishNode(node, kind);
  7503. }
  7504. tsIsUnambiguouslyIndexSignature() {
  7505. this.next();
  7506. if (tokenIsIdentifier(this.state.type)) {
  7507. this.next();
  7508. return this.match(14);
  7509. }
  7510. return false;
  7511. }
  7512. tsTryParseIndexSignature(node) {
  7513. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  7514. return undefined;
  7515. }
  7516. this.expect(0);
  7517. const id = this.parseIdentifier();
  7518. id.typeAnnotation = this.tsParseTypeAnnotation();
  7519. this.resetEndLocation(id);
  7520. this.expect(3);
  7521. node.parameters = [id];
  7522. const type = this.tsTryParseTypeAnnotation();
  7523. if (type) node.typeAnnotation = type;
  7524. this.tsParseTypeMemberSemicolon();
  7525. return this.finishNode(node, "TSIndexSignature");
  7526. }
  7527. tsParsePropertyOrMethodSignature(node, readonly) {
  7528. if (this.eat(17)) node.optional = true;
  7529. const nodeAny = node;
  7530. if (this.match(10) || this.match(47)) {
  7531. if (readonly) {
  7532. this.raise(TSErrors.ReadonlyForMethodSignature, {
  7533. at: node
  7534. });
  7535. }
  7536. const method = nodeAny;
  7537. if (method.kind && this.match(47)) {
  7538. this.raise(TSErrors.AccesorCannotHaveTypeParameters, {
  7539. at: this.state.curPosition()
  7540. });
  7541. }
  7542. this.tsFillSignature(14, method);
  7543. this.tsParseTypeMemberSemicolon();
  7544. const paramsKey = "parameters";
  7545. const returnTypeKey = "typeAnnotation";
  7546. if (method.kind === "get") {
  7547. if (method[paramsKey].length > 0) {
  7548. this.raise(Errors.BadGetterArity, {
  7549. at: this.state.curPosition()
  7550. });
  7551. if (this.isThisParam(method[paramsKey][0])) {
  7552. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  7553. at: this.state.curPosition()
  7554. });
  7555. }
  7556. }
  7557. } else if (method.kind === "set") {
  7558. if (method[paramsKey].length !== 1) {
  7559. this.raise(Errors.BadSetterArity, {
  7560. at: this.state.curPosition()
  7561. });
  7562. } else {
  7563. const firstParameter = method[paramsKey][0];
  7564. if (this.isThisParam(firstParameter)) {
  7565. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  7566. at: this.state.curPosition()
  7567. });
  7568. }
  7569. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  7570. this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, {
  7571. at: this.state.curPosition()
  7572. });
  7573. }
  7574. if (firstParameter.type === "RestElement") {
  7575. this.raise(TSErrors.SetAccesorCannotHaveRestParameter, {
  7576. at: this.state.curPosition()
  7577. });
  7578. }
  7579. }
  7580. if (method[returnTypeKey]) {
  7581. this.raise(TSErrors.SetAccesorCannotHaveReturnType, {
  7582. at: method[returnTypeKey]
  7583. });
  7584. }
  7585. } else {
  7586. method.kind = "method";
  7587. }
  7588. return this.finishNode(method, "TSMethodSignature");
  7589. } else {
  7590. const property = nodeAny;
  7591. if (readonly) property.readonly = true;
  7592. const type = this.tsTryParseTypeAnnotation();
  7593. if (type) property.typeAnnotation = type;
  7594. this.tsParseTypeMemberSemicolon();
  7595. return this.finishNode(property, "TSPropertySignature");
  7596. }
  7597. }
  7598. tsParseTypeMember() {
  7599. const node = this.startNode();
  7600. if (this.match(10) || this.match(47)) {
  7601. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  7602. }
  7603. if (this.match(77)) {
  7604. const id = this.startNode();
  7605. this.next();
  7606. if (this.match(10) || this.match(47)) {
  7607. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  7608. } else {
  7609. node.key = this.createIdentifier(id, "new");
  7610. return this.tsParsePropertyOrMethodSignature(node, false);
  7611. }
  7612. }
  7613. this.tsParseModifiers({
  7614. modified: node,
  7615. allowedModifiers: ["readonly"],
  7616. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  7617. });
  7618. const idx = this.tsTryParseIndexSignature(node);
  7619. if (idx) {
  7620. return idx;
  7621. }
  7622. super.parsePropertyName(node);
  7623. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  7624. node.kind = node.key.name;
  7625. super.parsePropertyName(node);
  7626. }
  7627. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  7628. }
  7629. tsParseTypeLiteral() {
  7630. const node = this.startNode();
  7631. node.members = this.tsParseObjectTypeMembers();
  7632. return this.finishNode(node, "TSTypeLiteral");
  7633. }
  7634. tsParseObjectTypeMembers() {
  7635. this.expect(5);
  7636. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  7637. this.expect(8);
  7638. return members;
  7639. }
  7640. tsIsStartOfMappedType() {
  7641. this.next();
  7642. if (this.eat(53)) {
  7643. return this.isContextual(118);
  7644. }
  7645. if (this.isContextual(118)) {
  7646. this.next();
  7647. }
  7648. if (!this.match(0)) {
  7649. return false;
  7650. }
  7651. this.next();
  7652. if (!this.tsIsIdentifier()) {
  7653. return false;
  7654. }
  7655. this.next();
  7656. return this.match(58);
  7657. }
  7658. tsParseMappedTypeParameter() {
  7659. const node = this.startNode();
  7660. node.name = this.tsParseTypeParameterName();
  7661. node.constraint = this.tsExpectThenParseType(58);
  7662. return this.finishNode(node, "TSTypeParameter");
  7663. }
  7664. tsParseMappedType() {
  7665. const node = this.startNode();
  7666. this.expect(5);
  7667. if (this.match(53)) {
  7668. node.readonly = this.state.value;
  7669. this.next();
  7670. this.expectContextual(118);
  7671. } else if (this.eatContextual(118)) {
  7672. node.readonly = true;
  7673. }
  7674. this.expect(0);
  7675. node.typeParameter = this.tsParseMappedTypeParameter();
  7676. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  7677. this.expect(3);
  7678. if (this.match(53)) {
  7679. node.optional = this.state.value;
  7680. this.next();
  7681. this.expect(17);
  7682. } else if (this.eat(17)) {
  7683. node.optional = true;
  7684. }
  7685. node.typeAnnotation = this.tsTryParseType();
  7686. this.semicolon();
  7687. this.expect(8);
  7688. return this.finishNode(node, "TSMappedType");
  7689. }
  7690. tsParseTupleType() {
  7691. const node = this.startNode();
  7692. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  7693. let seenOptionalElement = false;
  7694. let labeledElements = null;
  7695. node.elementTypes.forEach(elementNode => {
  7696. var _labeledElements;
  7697. const {
  7698. type
  7699. } = elementNode;
  7700. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  7701. this.raise(TSErrors.OptionalTypeBeforeRequired, {
  7702. at: elementNode
  7703. });
  7704. }
  7705. seenOptionalElement || (seenOptionalElement = type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType");
  7706. let checkType = type;
  7707. if (type === "TSRestType") {
  7708. elementNode = elementNode.typeAnnotation;
  7709. checkType = elementNode.type;
  7710. }
  7711. const isLabeled = checkType === "TSNamedTupleMember";
  7712. (_labeledElements = labeledElements) != null ? _labeledElements : labeledElements = isLabeled;
  7713. if (labeledElements !== isLabeled) {
  7714. this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
  7715. at: elementNode
  7716. });
  7717. }
  7718. });
  7719. return this.finishNode(node, "TSTupleType");
  7720. }
  7721. tsParseTupleElementType() {
  7722. const {
  7723. start: startPos,
  7724. startLoc
  7725. } = this.state;
  7726. const rest = this.eat(21);
  7727. let type = this.tsParseType();
  7728. const optional = this.eat(17);
  7729. const labeled = this.eat(14);
  7730. if (labeled) {
  7731. const labeledNode = this.startNodeAtNode(type);
  7732. labeledNode.optional = optional;
  7733. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  7734. labeledNode.label = type.typeName;
  7735. } else {
  7736. this.raise(TSErrors.InvalidTupleMemberLabel, {
  7737. at: type
  7738. });
  7739. labeledNode.label = type;
  7740. }
  7741. labeledNode.elementType = this.tsParseType();
  7742. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  7743. } else if (optional) {
  7744. const optionalTypeNode = this.startNodeAtNode(type);
  7745. optionalTypeNode.typeAnnotation = type;
  7746. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  7747. }
  7748. if (rest) {
  7749. const restNode = this.startNodeAt(startPos, startLoc);
  7750. restNode.typeAnnotation = type;
  7751. type = this.finishNode(restNode, "TSRestType");
  7752. }
  7753. return type;
  7754. }
  7755. tsParseParenthesizedType() {
  7756. const node = this.startNode();
  7757. this.expect(10);
  7758. node.typeAnnotation = this.tsParseType();
  7759. this.expect(11);
  7760. return this.finishNode(node, "TSParenthesizedType");
  7761. }
  7762. tsParseFunctionOrConstructorType(type, abstract) {
  7763. const node = this.startNode();
  7764. if (type === "TSConstructorType") {
  7765. node.abstract = !!abstract;
  7766. if (abstract) this.next();
  7767. this.next();
  7768. }
  7769. this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
  7770. return this.finishNode(node, type);
  7771. }
  7772. tsParseLiteralTypeNode() {
  7773. const node = this.startNode();
  7774. node.literal = (() => {
  7775. switch (this.state.type) {
  7776. case 130:
  7777. case 131:
  7778. case 129:
  7779. case 85:
  7780. case 86:
  7781. return super.parseExprAtom();
  7782. default:
  7783. throw this.unexpected();
  7784. }
  7785. })();
  7786. return this.finishNode(node, "TSLiteralType");
  7787. }
  7788. tsParseTemplateLiteralType() {
  7789. const node = this.startNode();
  7790. node.literal = super.parseTemplate(false);
  7791. return this.finishNode(node, "TSLiteralType");
  7792. }
  7793. parseTemplateSubstitution() {
  7794. if (this.state.inType) return this.tsParseType();
  7795. return super.parseTemplateSubstitution();
  7796. }
  7797. tsParseThisTypeOrThisTypePredicate() {
  7798. const thisKeyword = this.tsParseThisTypeNode();
  7799. if (this.isContextual(113) && !this.hasPrecedingLineBreak()) {
  7800. return this.tsParseThisTypePredicate(thisKeyword);
  7801. } else {
  7802. return thisKeyword;
  7803. }
  7804. }
  7805. tsParseNonArrayType() {
  7806. switch (this.state.type) {
  7807. case 129:
  7808. case 130:
  7809. case 131:
  7810. case 85:
  7811. case 86:
  7812. return this.tsParseLiteralTypeNode();
  7813. case 53:
  7814. if (this.state.value === "-") {
  7815. const node = this.startNode();
  7816. const nextToken = this.lookahead();
  7817. if (nextToken.type !== 130 && nextToken.type !== 131) {
  7818. throw this.unexpected();
  7819. }
  7820. node.literal = this.parseMaybeUnary();
  7821. return this.finishNode(node, "TSLiteralType");
  7822. }
  7823. break;
  7824. case 78:
  7825. return this.tsParseThisTypeOrThisTypePredicate();
  7826. case 87:
  7827. return this.tsParseTypeQuery();
  7828. case 83:
  7829. return this.tsParseImportType();
  7830. case 5:
  7831. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  7832. case 0:
  7833. return this.tsParseTupleType();
  7834. case 10:
  7835. return this.tsParseParenthesizedType();
  7836. case 25:
  7837. case 24:
  7838. return this.tsParseTemplateLiteralType();
  7839. default:
  7840. {
  7841. const {
  7842. type
  7843. } = this.state;
  7844. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  7845. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  7846. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  7847. const node = this.startNode();
  7848. this.next();
  7849. return this.finishNode(node, nodeType);
  7850. }
  7851. return this.tsParseTypeReference();
  7852. }
  7853. }
  7854. }
  7855. throw this.unexpected();
  7856. }
  7857. tsParseArrayTypeOrHigher() {
  7858. let type = this.tsParseNonArrayType();
  7859. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  7860. if (this.match(3)) {
  7861. const node = this.startNodeAtNode(type);
  7862. node.elementType = type;
  7863. this.expect(3);
  7864. type = this.finishNode(node, "TSArrayType");
  7865. } else {
  7866. const node = this.startNodeAtNode(type);
  7867. node.objectType = type;
  7868. node.indexType = this.tsParseType();
  7869. this.expect(3);
  7870. type = this.finishNode(node, "TSIndexedAccessType");
  7871. }
  7872. }
  7873. return type;
  7874. }
  7875. tsParseTypeOperator() {
  7876. const node = this.startNode();
  7877. const operator = this.state.value;
  7878. this.next();
  7879. node.operator = operator;
  7880. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  7881. if (operator === "readonly") {
  7882. this.tsCheckTypeAnnotationForReadOnly(node);
  7883. }
  7884. return this.finishNode(node, "TSTypeOperator");
  7885. }
  7886. tsCheckTypeAnnotationForReadOnly(node) {
  7887. switch (node.typeAnnotation.type) {
  7888. case "TSTupleType":
  7889. case "TSArrayType":
  7890. return;
  7891. default:
  7892. this.raise(TSErrors.UnexpectedReadonly, {
  7893. at: node
  7894. });
  7895. }
  7896. }
  7897. tsParseInferType() {
  7898. const node = this.startNode();
  7899. this.expectContextual(112);
  7900. const typeParameter = this.startNode();
  7901. typeParameter.name = this.tsParseTypeParameterName();
  7902. typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
  7903. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  7904. return this.finishNode(node, "TSInferType");
  7905. }
  7906. tsParseConstraintForInferType() {
  7907. if (this.eat(81)) {
  7908. const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
  7909. if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
  7910. return constraint;
  7911. }
  7912. }
  7913. }
  7914. tsParseTypeOperatorOrHigher() {
  7915. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  7916. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(112) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
  7917. }
  7918. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  7919. const node = this.startNode();
  7920. const hasLeadingOperator = this.eat(operator);
  7921. const types = [];
  7922. do {
  7923. types.push(parseConstituentType());
  7924. } while (this.eat(operator));
  7925. if (types.length === 1 && !hasLeadingOperator) {
  7926. return types[0];
  7927. }
  7928. node.types = types;
  7929. return this.finishNode(node, kind);
  7930. }
  7931. tsParseIntersectionTypeOrHigher() {
  7932. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  7933. }
  7934. tsParseUnionTypeOrHigher() {
  7935. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  7936. }
  7937. tsIsStartOfFunctionType() {
  7938. if (this.match(47)) {
  7939. return true;
  7940. }
  7941. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  7942. }
  7943. tsSkipParameterStart() {
  7944. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  7945. this.next();
  7946. return true;
  7947. }
  7948. if (this.match(5)) {
  7949. const {
  7950. errors
  7951. } = this.state;
  7952. const previousErrorCount = errors.length;
  7953. try {
  7954. this.parseObjectLike(8, true);
  7955. return errors.length === previousErrorCount;
  7956. } catch (_unused) {
  7957. return false;
  7958. }
  7959. }
  7960. if (this.match(0)) {
  7961. this.next();
  7962. const {
  7963. errors
  7964. } = this.state;
  7965. const previousErrorCount = errors.length;
  7966. try {
  7967. super.parseBindingList(3, 93, true);
  7968. return errors.length === previousErrorCount;
  7969. } catch (_unused2) {
  7970. return false;
  7971. }
  7972. }
  7973. return false;
  7974. }
  7975. tsIsUnambiguouslyStartOfFunctionType() {
  7976. this.next();
  7977. if (this.match(11) || this.match(21)) {
  7978. return true;
  7979. }
  7980. if (this.tsSkipParameterStart()) {
  7981. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  7982. return true;
  7983. }
  7984. if (this.match(11)) {
  7985. this.next();
  7986. if (this.match(19)) {
  7987. return true;
  7988. }
  7989. }
  7990. }
  7991. return false;
  7992. }
  7993. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  7994. return this.tsInType(() => {
  7995. const t = this.startNode();
  7996. this.expect(returnToken);
  7997. const node = this.startNode();
  7998. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  7999. if (asserts && this.match(78)) {
  8000. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  8001. if (thisTypePredicate.type === "TSThisType") {
  8002. node.parameterName = thisTypePredicate;
  8003. node.asserts = true;
  8004. node.typeAnnotation = null;
  8005. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  8006. } else {
  8007. this.resetStartLocationFromNode(thisTypePredicate, node);
  8008. thisTypePredicate.asserts = true;
  8009. }
  8010. t.typeAnnotation = thisTypePredicate;
  8011. return this.finishNode(t, "TSTypeAnnotation");
  8012. }
  8013. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  8014. if (!typePredicateVariable) {
  8015. if (!asserts) {
  8016. return this.tsParseTypeAnnotation(false, t);
  8017. }
  8018. node.parameterName = this.parseIdentifier();
  8019. node.asserts = asserts;
  8020. node.typeAnnotation = null;
  8021. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8022. return this.finishNode(t, "TSTypeAnnotation");
  8023. }
  8024. const type = this.tsParseTypeAnnotation(false);
  8025. node.parameterName = typePredicateVariable;
  8026. node.typeAnnotation = type;
  8027. node.asserts = asserts;
  8028. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8029. return this.finishNode(t, "TSTypeAnnotation");
  8030. });
  8031. }
  8032. tsTryParseTypeOrTypePredicateAnnotation() {
  8033. return this.match(14) ? this.tsParseTypeOrTypePredicateAnnotation(14) : undefined;
  8034. }
  8035. tsTryParseTypeAnnotation() {
  8036. return this.match(14) ? this.tsParseTypeAnnotation() : undefined;
  8037. }
  8038. tsTryParseType() {
  8039. return this.tsEatThenParseType(14);
  8040. }
  8041. tsParseTypePredicatePrefix() {
  8042. const id = this.parseIdentifier();
  8043. if (this.isContextual(113) && !this.hasPrecedingLineBreak()) {
  8044. this.next();
  8045. return id;
  8046. }
  8047. }
  8048. tsParseTypePredicateAsserts() {
  8049. if (this.state.type !== 106) {
  8050. return false;
  8051. }
  8052. const containsEsc = this.state.containsEsc;
  8053. this.next();
  8054. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  8055. return false;
  8056. }
  8057. if (containsEsc) {
  8058. this.raise(Errors.InvalidEscapedReservedWord, {
  8059. at: this.state.lastTokStartLoc,
  8060. reservedWord: "asserts"
  8061. });
  8062. }
  8063. return true;
  8064. }
  8065. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  8066. this.tsInType(() => {
  8067. if (eatColon) this.expect(14);
  8068. t.typeAnnotation = this.tsParseType();
  8069. });
  8070. return this.finishNode(t, "TSTypeAnnotation");
  8071. }
  8072. tsParseType() {
  8073. assert(this.state.inType);
  8074. const type = this.tsParseNonConditionalType();
  8075. if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
  8076. return type;
  8077. }
  8078. const node = this.startNodeAtNode(type);
  8079. node.checkType = type;
  8080. node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
  8081. this.expect(17);
  8082. node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8083. this.expect(14);
  8084. node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8085. return this.finishNode(node, "TSConditionalType");
  8086. }
  8087. isAbstractConstructorSignature() {
  8088. return this.isContextual(120) && this.lookahead().type === 77;
  8089. }
  8090. tsParseNonConditionalType() {
  8091. if (this.tsIsStartOfFunctionType()) {
  8092. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  8093. }
  8094. if (this.match(77)) {
  8095. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  8096. } else if (this.isAbstractConstructorSignature()) {
  8097. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  8098. }
  8099. return this.tsParseUnionTypeOrHigher();
  8100. }
  8101. tsParseTypeAssertion() {
  8102. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8103. this.raise(TSErrors.ReservedTypeAssertion, {
  8104. at: this.state.startLoc
  8105. });
  8106. }
  8107. const node = this.startNode();
  8108. const _const = this.tsTryNextParseConstantContext();
  8109. node.typeAnnotation = _const || this.tsNextThenParseType();
  8110. this.expect(48);
  8111. node.expression = this.parseMaybeUnary();
  8112. return this.finishNode(node, "TSTypeAssertion");
  8113. }
  8114. tsParseHeritageClause(token) {
  8115. const originalStartLoc = this.state.startLoc;
  8116. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
  8117. const node = this.startNode();
  8118. node.expression = this.tsParseEntityName();
  8119. if (this.match(47)) {
  8120. node.typeParameters = this.tsParseTypeArguments();
  8121. }
  8122. return this.finishNode(node, "TSExpressionWithTypeArguments");
  8123. });
  8124. if (!delimitedList.length) {
  8125. this.raise(TSErrors.EmptyHeritageClauseType, {
  8126. at: originalStartLoc,
  8127. token
  8128. });
  8129. }
  8130. return delimitedList;
  8131. }
  8132. tsParseInterfaceDeclaration(node, properties = {}) {
  8133. if (this.hasFollowingLineBreak()) return null;
  8134. this.expectContextual(125);
  8135. if (properties.declare) node.declare = true;
  8136. if (tokenIsIdentifier(this.state.type)) {
  8137. node.id = this.parseIdentifier();
  8138. this.checkIdentifier(node.id, BIND_TS_INTERFACE);
  8139. } else {
  8140. node.id = null;
  8141. this.raise(TSErrors.MissingInterfaceName, {
  8142. at: this.state.startLoc
  8143. });
  8144. }
  8145. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers.bind(this));
  8146. if (this.eat(81)) {
  8147. node.extends = this.tsParseHeritageClause("extends");
  8148. }
  8149. const body = this.startNode();
  8150. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  8151. node.body = this.finishNode(body, "TSInterfaceBody");
  8152. return this.finishNode(node, "TSInterfaceDeclaration");
  8153. }
  8154. tsParseTypeAliasDeclaration(node) {
  8155. node.id = this.parseIdentifier();
  8156. this.checkIdentifier(node.id, BIND_TS_TYPE);
  8157. node.typeAnnotation = this.tsInType(() => {
  8158. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers.bind(this));
  8159. this.expect(29);
  8160. if (this.isContextual(111) && this.lookahead().type !== 16) {
  8161. const node = this.startNode();
  8162. this.next();
  8163. return this.finishNode(node, "TSIntrinsicKeyword");
  8164. }
  8165. return this.tsParseType();
  8166. });
  8167. this.semicolon();
  8168. return this.finishNode(node, "TSTypeAliasDeclaration");
  8169. }
  8170. tsInNoContext(cb) {
  8171. const oldContext = this.state.context;
  8172. this.state.context = [oldContext[0]];
  8173. try {
  8174. return cb();
  8175. } finally {
  8176. this.state.context = oldContext;
  8177. }
  8178. }
  8179. tsInType(cb) {
  8180. const oldInType = this.state.inType;
  8181. this.state.inType = true;
  8182. try {
  8183. return cb();
  8184. } finally {
  8185. this.state.inType = oldInType;
  8186. }
  8187. }
  8188. tsInDisallowConditionalTypesContext(cb) {
  8189. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8190. this.state.inDisallowConditionalTypesContext = true;
  8191. try {
  8192. return cb();
  8193. } finally {
  8194. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8195. }
  8196. }
  8197. tsInAllowConditionalTypesContext(cb) {
  8198. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8199. this.state.inDisallowConditionalTypesContext = false;
  8200. try {
  8201. return cb();
  8202. } finally {
  8203. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8204. }
  8205. }
  8206. tsEatThenParseType(token) {
  8207. return !this.match(token) ? undefined : this.tsNextThenParseType();
  8208. }
  8209. tsExpectThenParseType(token) {
  8210. return this.tsDoThenParseType(() => this.expect(token));
  8211. }
  8212. tsNextThenParseType() {
  8213. return this.tsDoThenParseType(() => this.next());
  8214. }
  8215. tsDoThenParseType(cb) {
  8216. return this.tsInType(() => {
  8217. cb();
  8218. return this.tsParseType();
  8219. });
  8220. }
  8221. tsParseEnumMember() {
  8222. const node = this.startNode();
  8223. node.id = this.match(129) ? super.parseStringLiteral(this.state.value) : this.parseIdentifier(true);
  8224. if (this.eat(29)) {
  8225. node.initializer = super.parseMaybeAssignAllowIn();
  8226. }
  8227. return this.finishNode(node, "TSEnumMember");
  8228. }
  8229. tsParseEnumDeclaration(node, properties = {}) {
  8230. if (properties.const) node.const = true;
  8231. if (properties.declare) node.declare = true;
  8232. this.expectContextual(122);
  8233. node.id = this.parseIdentifier();
  8234. this.checkIdentifier(node.id, node.const ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  8235. this.expect(5);
  8236. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8237. this.expect(8);
  8238. return this.finishNode(node, "TSEnumDeclaration");
  8239. }
  8240. tsParseModuleBlock() {
  8241. const node = this.startNode();
  8242. this.scope.enter(SCOPE_OTHER);
  8243. this.expect(5);
  8244. super.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8245. this.scope.exit();
  8246. return this.finishNode(node, "TSModuleBlock");
  8247. }
  8248. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8249. node.id = this.parseIdentifier();
  8250. if (!nested) {
  8251. this.checkIdentifier(node.id, BIND_TS_NAMESPACE);
  8252. }
  8253. if (this.eat(16)) {
  8254. const inner = this.startNode();
  8255. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8256. node.body = inner;
  8257. } else {
  8258. this.scope.enter(SCOPE_TS_MODULE);
  8259. this.prodParam.enter(PARAM);
  8260. node.body = this.tsParseModuleBlock();
  8261. this.prodParam.exit();
  8262. this.scope.exit();
  8263. }
  8264. return this.finishNode(node, "TSModuleDeclaration");
  8265. }
  8266. tsParseAmbientExternalModuleDeclaration(node) {
  8267. if (this.isContextual(109)) {
  8268. node.global = true;
  8269. node.id = this.parseIdentifier();
  8270. } else if (this.match(129)) {
  8271. node.id = super.parseStringLiteral(this.state.value);
  8272. } else {
  8273. this.unexpected();
  8274. }
  8275. if (this.match(5)) {
  8276. this.scope.enter(SCOPE_TS_MODULE);
  8277. this.prodParam.enter(PARAM);
  8278. node.body = this.tsParseModuleBlock();
  8279. this.prodParam.exit();
  8280. this.scope.exit();
  8281. } else {
  8282. this.semicolon();
  8283. }
  8284. return this.finishNode(node, "TSModuleDeclaration");
  8285. }
  8286. tsParseImportEqualsDeclaration(node, isExport) {
  8287. node.isExport = isExport || false;
  8288. node.id = this.parseIdentifier();
  8289. this.checkIdentifier(node.id, BIND_LEXICAL);
  8290. this.expect(29);
  8291. const moduleReference = this.tsParseModuleReference();
  8292. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8293. this.raise(TSErrors.ImportAliasHasImportType, {
  8294. at: moduleReference
  8295. });
  8296. }
  8297. node.moduleReference = moduleReference;
  8298. this.semicolon();
  8299. return this.finishNode(node, "TSImportEqualsDeclaration");
  8300. }
  8301. tsIsExternalModuleReference() {
  8302. return this.isContextual(116) && this.lookaheadCharCode() === 40;
  8303. }
  8304. tsParseModuleReference() {
  8305. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  8306. }
  8307. tsParseExternalModuleReference() {
  8308. const node = this.startNode();
  8309. this.expectContextual(116);
  8310. this.expect(10);
  8311. if (!this.match(129)) {
  8312. throw this.unexpected();
  8313. }
  8314. node.expression = super.parseExprAtom();
  8315. this.expect(11);
  8316. return this.finishNode(node, "TSExternalModuleReference");
  8317. }
  8318. tsLookAhead(f) {
  8319. const state = this.state.clone();
  8320. const res = f();
  8321. this.state = state;
  8322. return res;
  8323. }
  8324. tsTryParseAndCatch(f) {
  8325. const result = this.tryParse(abort => f() || abort());
  8326. if (result.aborted || !result.node) return undefined;
  8327. if (result.error) this.state = result.failState;
  8328. return result.node;
  8329. }
  8330. tsTryParse(f) {
  8331. const state = this.state.clone();
  8332. const result = f();
  8333. if (result !== undefined && result !== false) {
  8334. return result;
  8335. } else {
  8336. this.state = state;
  8337. return undefined;
  8338. }
  8339. }
  8340. tsTryParseDeclare(nany) {
  8341. if (this.isLineTerminator()) {
  8342. return;
  8343. }
  8344. let starttype = this.state.type;
  8345. let kind;
  8346. if (this.isContextual(99)) {
  8347. starttype = 74;
  8348. kind = "let";
  8349. }
  8350. return this.tsInAmbientContext(() => {
  8351. if (starttype === 68) {
  8352. nany.declare = true;
  8353. return super.parseFunctionStatement(nany, false, true);
  8354. }
  8355. if (starttype === 80) {
  8356. nany.declare = true;
  8357. return this.parseClass(nany, true, false);
  8358. }
  8359. if (starttype === 122) {
  8360. return this.tsParseEnumDeclaration(nany, {
  8361. declare: true
  8362. });
  8363. }
  8364. if (starttype === 109) {
  8365. return this.tsParseAmbientExternalModuleDeclaration(nany);
  8366. }
  8367. if (starttype === 75 || starttype === 74) {
  8368. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  8369. nany.declare = true;
  8370. return this.parseVarStatement(nany, kind || this.state.value, true);
  8371. }
  8372. this.expect(75);
  8373. return this.tsParseEnumDeclaration(nany, {
  8374. const: true,
  8375. declare: true
  8376. });
  8377. }
  8378. if (starttype === 125) {
  8379. const result = this.tsParseInterfaceDeclaration(nany, {
  8380. declare: true
  8381. });
  8382. if (result) return result;
  8383. }
  8384. if (tokenIsIdentifier(starttype)) {
  8385. return this.tsParseDeclaration(nany, this.state.value, true);
  8386. }
  8387. });
  8388. }
  8389. tsTryParseExportDeclaration() {
  8390. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  8391. }
  8392. tsParseExpressionStatement(node, expr) {
  8393. switch (expr.name) {
  8394. case "declare":
  8395. {
  8396. const declaration = this.tsTryParseDeclare(node);
  8397. if (declaration) {
  8398. declaration.declare = true;
  8399. return declaration;
  8400. }
  8401. break;
  8402. }
  8403. case "global":
  8404. if (this.match(5)) {
  8405. this.scope.enter(SCOPE_TS_MODULE);
  8406. this.prodParam.enter(PARAM);
  8407. const mod = node;
  8408. mod.global = true;
  8409. mod.id = expr;
  8410. mod.body = this.tsParseModuleBlock();
  8411. this.scope.exit();
  8412. this.prodParam.exit();
  8413. return this.finishNode(mod, "TSModuleDeclaration");
  8414. }
  8415. break;
  8416. default:
  8417. return this.tsParseDeclaration(node, expr.name, false);
  8418. }
  8419. }
  8420. tsParseDeclaration(node, value, next) {
  8421. switch (value) {
  8422. case "abstract":
  8423. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  8424. return this.tsParseAbstractDeclaration(node);
  8425. }
  8426. break;
  8427. case "module":
  8428. if (this.tsCheckLineTerminator(next)) {
  8429. if (this.match(129)) {
  8430. return this.tsParseAmbientExternalModuleDeclaration(node);
  8431. } else if (tokenIsIdentifier(this.state.type)) {
  8432. return this.tsParseModuleOrNamespaceDeclaration(node);
  8433. }
  8434. }
  8435. break;
  8436. case "namespace":
  8437. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8438. return this.tsParseModuleOrNamespaceDeclaration(node);
  8439. }
  8440. break;
  8441. case "type":
  8442. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8443. return this.tsParseTypeAliasDeclaration(node);
  8444. }
  8445. break;
  8446. }
  8447. }
  8448. tsCheckLineTerminator(next) {
  8449. if (next) {
  8450. if (this.hasFollowingLineBreak()) return false;
  8451. this.next();
  8452. return true;
  8453. }
  8454. return !this.isLineTerminator();
  8455. }
  8456. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  8457. if (!this.match(47)) {
  8458. return undefined;
  8459. }
  8460. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8461. this.state.maybeInArrowParameters = true;
  8462. const res = this.tsTryParseAndCatch(() => {
  8463. const node = this.startNodeAt(startPos, startLoc);
  8464. node.typeParameters = this.tsParseTypeParameters();
  8465. super.parseFunctionParams(node);
  8466. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  8467. this.expect(19);
  8468. return node;
  8469. });
  8470. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  8471. if (!res) {
  8472. return undefined;
  8473. }
  8474. return super.parseArrowExpression(res, null, true);
  8475. }
  8476. tsParseTypeArgumentsInExpression() {
  8477. if (this.reScan_lt() !== 47) {
  8478. return undefined;
  8479. }
  8480. return this.tsParseTypeArguments();
  8481. }
  8482. tsParseTypeArguments() {
  8483. const node = this.startNode();
  8484. node.params = this.tsInType(() => this.tsInNoContext(() => {
  8485. this.expect(47);
  8486. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  8487. }));
  8488. if (node.params.length === 0) {
  8489. this.raise(TSErrors.EmptyTypeArguments, {
  8490. at: node
  8491. });
  8492. }
  8493. this.expect(48);
  8494. return this.finishNode(node, "TSTypeParameterInstantiation");
  8495. }
  8496. tsIsDeclarationStart() {
  8497. return tokenIsTSDeclarationStart(this.state.type);
  8498. }
  8499. isExportDefaultSpecifier() {
  8500. if (this.tsIsDeclarationStart()) return false;
  8501. return super.isExportDefaultSpecifier();
  8502. }
  8503. parseAssignableListItem(allowModifiers, decorators) {
  8504. const startPos = this.state.start;
  8505. const startLoc = this.state.startLoc;
  8506. let accessibility;
  8507. let readonly = false;
  8508. let override = false;
  8509. if (allowModifiers !== undefined) {
  8510. const modified = {};
  8511. this.tsParseModifiers({
  8512. modified,
  8513. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  8514. });
  8515. accessibility = modified.accessibility;
  8516. override = modified.override;
  8517. readonly = modified.readonly;
  8518. if (allowModifiers === false && (accessibility || readonly || override)) {
  8519. this.raise(TSErrors.UnexpectedParameterModifier, {
  8520. at: startLoc
  8521. });
  8522. }
  8523. }
  8524. const left = this.parseMaybeDefault();
  8525. this.parseAssignableListItemTypes(left);
  8526. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  8527. if (accessibility || readonly || override) {
  8528. const pp = this.startNodeAt(startPos, startLoc);
  8529. if (decorators.length) {
  8530. pp.decorators = decorators;
  8531. }
  8532. if (accessibility) pp.accessibility = accessibility;
  8533. if (readonly) pp.readonly = readonly;
  8534. if (override) pp.override = override;
  8535. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  8536. this.raise(TSErrors.UnsupportedParameterPropertyKind, {
  8537. at: pp
  8538. });
  8539. }
  8540. pp.parameter = elt;
  8541. return this.finishNode(pp, "TSParameterProperty");
  8542. }
  8543. if (decorators.length) {
  8544. left.decorators = decorators;
  8545. }
  8546. return elt;
  8547. }
  8548. isSimpleParameter(node) {
  8549. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  8550. }
  8551. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  8552. if (this.match(14)) {
  8553. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  8554. }
  8555. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  8556. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  8557. return this.finishNode(node, bodilessType);
  8558. }
  8559. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  8560. this.raise(TSErrors.DeclareFunctionHasImplementation, {
  8561. at: node
  8562. });
  8563. if (node.declare) {
  8564. return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  8565. }
  8566. }
  8567. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  8568. }
  8569. registerFunctionStatementId(node) {
  8570. if (!node.body && node.id) {
  8571. this.checkIdentifier(node.id, BIND_TS_AMBIENT);
  8572. } else {
  8573. super.registerFunctionStatementId(node);
  8574. }
  8575. }
  8576. tsCheckForInvalidTypeCasts(items) {
  8577. items.forEach(node => {
  8578. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  8579. this.raise(TSErrors.UnexpectedTypeAnnotation, {
  8580. at: node.typeAnnotation
  8581. });
  8582. }
  8583. });
  8584. }
  8585. toReferencedList(exprList, isInParens) {
  8586. this.tsCheckForInvalidTypeCasts(exprList);
  8587. return exprList;
  8588. }
  8589. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  8590. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  8591. if (node.type === "ArrayExpression") {
  8592. this.tsCheckForInvalidTypeCasts(node.elements);
  8593. }
  8594. return node;
  8595. }
  8596. parseSubscript(base, startPos, startLoc, noCalls, state) {
  8597. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  8598. this.state.canStartJSXElement = false;
  8599. this.next();
  8600. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  8601. nonNullExpression.expression = base;
  8602. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  8603. }
  8604. let isOptionalCall = false;
  8605. if (this.match(18) && this.lookaheadCharCode() === 60) {
  8606. if (noCalls) {
  8607. state.stop = true;
  8608. return base;
  8609. }
  8610. state.optionalChainMember = isOptionalCall = true;
  8611. this.next();
  8612. }
  8613. if (this.match(47) || this.match(51)) {
  8614. let missingParenErrorLoc;
  8615. const result = this.tsTryParseAndCatch(() => {
  8616. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  8617. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  8618. if (asyncArrowFn) {
  8619. return asyncArrowFn;
  8620. }
  8621. }
  8622. const typeArguments = this.tsParseTypeArgumentsInExpression();
  8623. if (!typeArguments) return;
  8624. if (isOptionalCall && !this.match(10)) {
  8625. missingParenErrorLoc = this.state.curPosition();
  8626. return;
  8627. }
  8628. if (tokenIsTemplate(this.state.type)) {
  8629. const result = super.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  8630. result.typeParameters = typeArguments;
  8631. return result;
  8632. }
  8633. if (!noCalls && this.eat(10)) {
  8634. const node = this.startNodeAt(startPos, startLoc);
  8635. node.callee = base;
  8636. node.arguments = this.parseCallExpressionArguments(11, false);
  8637. this.tsCheckForInvalidTypeCasts(node.arguments);
  8638. node.typeParameters = typeArguments;
  8639. if (state.optionalChainMember) {
  8640. node.optional = isOptionalCall;
  8641. }
  8642. return this.finishCallExpression(node, state.optionalChainMember);
  8643. }
  8644. const tokenType = this.state.type;
  8645. if (tokenType === 48 || tokenType !== 10 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
  8646. return;
  8647. }
  8648. const node = this.startNodeAt(startPos, startLoc);
  8649. node.expression = base;
  8650. node.typeParameters = typeArguments;
  8651. return this.finishNode(node, "TSInstantiationExpression");
  8652. });
  8653. if (missingParenErrorLoc) {
  8654. this.unexpected(missingParenErrorLoc, 10);
  8655. }
  8656. if (result) {
  8657. if (result.type === "TSInstantiationExpression" && (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40)) {
  8658. this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, {
  8659. at: this.state.startLoc
  8660. });
  8661. }
  8662. return result;
  8663. }
  8664. }
  8665. return super.parseSubscript(base, startPos, startLoc, noCalls, state);
  8666. }
  8667. parseNewCallee(node) {
  8668. var _callee$extra;
  8669. super.parseNewCallee(node);
  8670. const {
  8671. callee
  8672. } = node;
  8673. if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
  8674. node.typeParameters = callee.typeParameters;
  8675. node.callee = callee.expression;
  8676. }
  8677. }
  8678. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  8679. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual(93)) {
  8680. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  8681. node.expression = left;
  8682. const _const = this.tsTryNextParseConstantContext();
  8683. if (_const) {
  8684. node.typeAnnotation = _const;
  8685. } else {
  8686. node.typeAnnotation = this.tsNextThenParseType();
  8687. }
  8688. this.finishNode(node, "TSAsExpression");
  8689. this.reScan_lt_gt();
  8690. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  8691. }
  8692. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec);
  8693. }
  8694. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  8695. if (!this.state.isAmbientContext) {
  8696. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  8697. }
  8698. }
  8699. checkDuplicateExports() {}
  8700. parseImport(node) {
  8701. node.importKind = "value";
  8702. if (tokenIsIdentifier(this.state.type) || this.match(55) || this.match(5)) {
  8703. let ahead = this.lookahead();
  8704. if (this.isContextual(126) && ahead.type !== 12 && ahead.type !== 97 && ahead.type !== 29) {
  8705. node.importKind = "type";
  8706. this.next();
  8707. ahead = this.lookahead();
  8708. }
  8709. if (tokenIsIdentifier(this.state.type) && ahead.type === 29) {
  8710. return this.tsParseImportEqualsDeclaration(node);
  8711. }
  8712. }
  8713. const importNode = super.parseImport(node);
  8714. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  8715. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, {
  8716. at: importNode
  8717. });
  8718. }
  8719. return importNode;
  8720. }
  8721. parseExport(node) {
  8722. if (this.match(83)) {
  8723. this.next();
  8724. if (this.isContextual(126) && this.lookaheadCharCode() !== 61) {
  8725. node.importKind = "type";
  8726. this.next();
  8727. } else {
  8728. node.importKind = "value";
  8729. }
  8730. return this.tsParseImportEqualsDeclaration(node, true);
  8731. } else if (this.eat(29)) {
  8732. const assign = node;
  8733. assign.expression = super.parseExpression();
  8734. this.semicolon();
  8735. return this.finishNode(assign, "TSExportAssignment");
  8736. } else if (this.eatContextual(93)) {
  8737. const decl = node;
  8738. this.expectContextual(124);
  8739. decl.id = this.parseIdentifier();
  8740. this.semicolon();
  8741. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  8742. } else {
  8743. if (this.isContextual(126) && this.lookahead().type === 5) {
  8744. this.next();
  8745. node.exportKind = "type";
  8746. } else {
  8747. node.exportKind = "value";
  8748. }
  8749. return super.parseExport(node);
  8750. }
  8751. }
  8752. isAbstractClass() {
  8753. return this.isContextual(120) && this.lookahead().type === 80;
  8754. }
  8755. parseExportDefaultExpression() {
  8756. if (this.isAbstractClass()) {
  8757. const cls = this.startNode();
  8758. this.next();
  8759. cls.abstract = true;
  8760. return this.parseClass(cls, true, true);
  8761. }
  8762. if (this.match(125)) {
  8763. const result = this.tsParseInterfaceDeclaration(this.startNode());
  8764. if (result) return result;
  8765. }
  8766. return super.parseExportDefaultExpression();
  8767. }
  8768. parseVarStatement(node, kind, allowMissingInitializer = false) {
  8769. const {
  8770. isAmbientContext
  8771. } = this.state;
  8772. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  8773. if (!isAmbientContext) return declaration;
  8774. for (const {
  8775. id,
  8776. init
  8777. } of declaration.declarations) {
  8778. if (!init) continue;
  8779. if (kind !== "const" || !!id.typeAnnotation) {
  8780. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, {
  8781. at: init
  8782. });
  8783. } else if (init.type !== "StringLiteral" && init.type !== "BooleanLiteral" && init.type !== "NumericLiteral" && init.type !== "BigIntLiteral" && (init.type !== "TemplateLiteral" || init.expressions.length > 0) && !isPossiblyLiteralEnum(init)) {
  8784. this.raise(TSErrors.ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference, {
  8785. at: init
  8786. });
  8787. }
  8788. }
  8789. return declaration;
  8790. }
  8791. parseStatementContent(context, topLevel) {
  8792. if (this.match(75) && this.isLookaheadContextual("enum")) {
  8793. const node = this.startNode();
  8794. this.expect(75);
  8795. return this.tsParseEnumDeclaration(node, {
  8796. const: true
  8797. });
  8798. }
  8799. if (this.isContextual(122)) {
  8800. return this.tsParseEnumDeclaration(this.startNode());
  8801. }
  8802. if (this.isContextual(125)) {
  8803. const result = this.tsParseInterfaceDeclaration(this.startNode());
  8804. if (result) return result;
  8805. }
  8806. return super.parseStatementContent(context, topLevel);
  8807. }
  8808. parseAccessModifier() {
  8809. return this.tsParseModifier(["public", "protected", "private"]);
  8810. }
  8811. tsHasSomeModifiers(member, modifiers) {
  8812. return modifiers.some(modifier => {
  8813. if (tsIsAccessModifier(modifier)) {
  8814. return member.accessibility === modifier;
  8815. }
  8816. return !!member[modifier];
  8817. });
  8818. }
  8819. tsIsStartOfStaticBlocks() {
  8820. return this.isContextual(104) && this.lookaheadCharCode() === 123;
  8821. }
  8822. parseClassMember(classBody, member, state) {
  8823. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  8824. this.tsParseModifiers({
  8825. modified: member,
  8826. allowedModifiers: modifiers,
  8827. disallowedModifiers: ["in", "out"],
  8828. stopOnStartOfClassStaticBlock: true,
  8829. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  8830. });
  8831. const callParseClassMemberWithIsStatic = () => {
  8832. if (this.tsIsStartOfStaticBlocks()) {
  8833. this.next();
  8834. this.next();
  8835. if (this.tsHasSomeModifiers(member, modifiers)) {
  8836. this.raise(TSErrors.StaticBlockCannotHaveModifier, {
  8837. at: this.state.curPosition()
  8838. });
  8839. }
  8840. super.parseClassStaticBlock(classBody, member);
  8841. } else {
  8842. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  8843. }
  8844. };
  8845. if (member.declare) {
  8846. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  8847. } else {
  8848. callParseClassMemberWithIsStatic();
  8849. }
  8850. }
  8851. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  8852. const idx = this.tsTryParseIndexSignature(member);
  8853. if (idx) {
  8854. classBody.body.push(idx);
  8855. if (member.abstract) {
  8856. this.raise(TSErrors.IndexSignatureHasAbstract, {
  8857. at: member
  8858. });
  8859. }
  8860. if (member.accessibility) {
  8861. this.raise(TSErrors.IndexSignatureHasAccessibility, {
  8862. at: member,
  8863. modifier: member.accessibility
  8864. });
  8865. }
  8866. if (member.declare) {
  8867. this.raise(TSErrors.IndexSignatureHasDeclare, {
  8868. at: member
  8869. });
  8870. }
  8871. if (member.override) {
  8872. this.raise(TSErrors.IndexSignatureHasOverride, {
  8873. at: member
  8874. });
  8875. }
  8876. return;
  8877. }
  8878. if (!this.state.inAbstractClass && member.abstract) {
  8879. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, {
  8880. at: member
  8881. });
  8882. }
  8883. if (member.override) {
  8884. if (!state.hadSuperClass) {
  8885. this.raise(TSErrors.OverrideNotInSubClass, {
  8886. at: member
  8887. });
  8888. }
  8889. }
  8890. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  8891. }
  8892. parsePostMemberNameModifiers(methodOrProp) {
  8893. const optional = this.eat(17);
  8894. if (optional) methodOrProp.optional = true;
  8895. if (methodOrProp.readonly && this.match(10)) {
  8896. this.raise(TSErrors.ClassMethodHasReadonly, {
  8897. at: methodOrProp
  8898. });
  8899. }
  8900. if (methodOrProp.declare && this.match(10)) {
  8901. this.raise(TSErrors.ClassMethodHasDeclare, {
  8902. at: methodOrProp
  8903. });
  8904. }
  8905. }
  8906. parseExpressionStatement(node, expr) {
  8907. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  8908. return decl || super.parseExpressionStatement(node, expr);
  8909. }
  8910. shouldParseExportDeclaration() {
  8911. if (this.tsIsDeclarationStart()) return true;
  8912. return super.shouldParseExportDeclaration();
  8913. }
  8914. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  8915. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  8916. return super.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  8917. }
  8918. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  8919. if (!result.node) {
  8920. if (result.error) {
  8921. super.setOptionalParametersError(refExpressionErrors, result.error);
  8922. }
  8923. return expr;
  8924. }
  8925. if (result.error) this.state = result.failState;
  8926. return result.node;
  8927. }
  8928. parseParenItem(node, startPos, startLoc) {
  8929. node = super.parseParenItem(node, startPos, startLoc);
  8930. if (this.eat(17)) {
  8931. node.optional = true;
  8932. this.resetEndLocation(node);
  8933. }
  8934. if (this.match(14)) {
  8935. const typeCastNode = this.startNodeAt(startPos, startLoc);
  8936. typeCastNode.expression = node;
  8937. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  8938. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  8939. }
  8940. return node;
  8941. }
  8942. parseExportDeclaration(node) {
  8943. if (!this.state.isAmbientContext && this.isContextual(121)) {
  8944. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  8945. }
  8946. const startPos = this.state.start;
  8947. const startLoc = this.state.startLoc;
  8948. const isDeclare = this.eatContextual(121);
  8949. if (isDeclare && (this.isContextual(121) || !this.shouldParseExportDeclaration())) {
  8950. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, {
  8951. at: this.state.startLoc
  8952. });
  8953. }
  8954. const isIdentifier = tokenIsIdentifier(this.state.type);
  8955. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  8956. if (!declaration) return null;
  8957. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  8958. node.exportKind = "type";
  8959. }
  8960. if (isDeclare) {
  8961. this.resetStartLocation(declaration, startPos, startLoc);
  8962. declaration.declare = true;
  8963. }
  8964. return declaration;
  8965. }
  8966. parseClassId(node, isStatement, optionalId, bindingType) {
  8967. if ((!isStatement || optionalId) && this.isContextual(110)) {
  8968. return;
  8969. }
  8970. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  8971. const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers.bind(this));
  8972. if (typeParameters) node.typeParameters = typeParameters;
  8973. }
  8974. parseClassPropertyAnnotation(node) {
  8975. if (!node.optional && this.eat(35)) {
  8976. node.definite = true;
  8977. }
  8978. const type = this.tsTryParseTypeAnnotation();
  8979. if (type) node.typeAnnotation = type;
  8980. }
  8981. parseClassProperty(node) {
  8982. this.parseClassPropertyAnnotation(node);
  8983. if (this.state.isAmbientContext && !(node.readonly && !node.typeAnnotation) && this.match(29)) {
  8984. this.raise(TSErrors.DeclareClassFieldHasInitializer, {
  8985. at: this.state.startLoc
  8986. });
  8987. }
  8988. if (node.abstract && this.match(29)) {
  8989. const {
  8990. key
  8991. } = node;
  8992. this.raise(TSErrors.AbstractPropertyHasInitializer, {
  8993. at: this.state.startLoc,
  8994. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  8995. });
  8996. }
  8997. return super.parseClassProperty(node);
  8998. }
  8999. parseClassPrivateProperty(node) {
  9000. if (node.abstract) {
  9001. this.raise(TSErrors.PrivateElementHasAbstract, {
  9002. at: node
  9003. });
  9004. }
  9005. if (node.accessibility) {
  9006. this.raise(TSErrors.PrivateElementHasAccessibility, {
  9007. at: node,
  9008. modifier: node.accessibility
  9009. });
  9010. }
  9011. this.parseClassPropertyAnnotation(node);
  9012. return super.parseClassPrivateProperty(node);
  9013. }
  9014. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  9015. const typeParameters = this.tsTryParseTypeParameters();
  9016. if (typeParameters && isConstructor) {
  9017. this.raise(TSErrors.ConstructorHasTypeParameters, {
  9018. at: typeParameters
  9019. });
  9020. }
  9021. const {
  9022. declare = false,
  9023. kind
  9024. } = method;
  9025. if (declare && (kind === "get" || kind === "set")) {
  9026. this.raise(TSErrors.DeclareAccessor, {
  9027. at: method,
  9028. kind
  9029. });
  9030. }
  9031. if (typeParameters) method.typeParameters = typeParameters;
  9032. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  9033. }
  9034. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  9035. const typeParameters = this.tsTryParseTypeParameters();
  9036. if (typeParameters) method.typeParameters = typeParameters;
  9037. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  9038. }
  9039. declareClassPrivateMethodInScope(node, kind) {
  9040. if (node.type === "TSDeclareMethod") return;
  9041. if (node.type === "MethodDefinition" && !node.value.body) return;
  9042. super.declareClassPrivateMethodInScope(node, kind);
  9043. }
  9044. parseClassSuper(node) {
  9045. super.parseClassSuper(node);
  9046. if (node.superClass && (this.match(47) || this.match(51))) {
  9047. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  9048. }
  9049. if (this.eatContextual(110)) {
  9050. node.implements = this.tsParseHeritageClause("implements");
  9051. }
  9052. }
  9053. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9054. const typeParameters = this.tsTryParseTypeParameters();
  9055. if (typeParameters) prop.typeParameters = typeParameters;
  9056. return super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9057. }
  9058. parseFunctionParams(node, allowModifiers) {
  9059. const typeParameters = this.tsTryParseTypeParameters();
  9060. if (typeParameters) node.typeParameters = typeParameters;
  9061. super.parseFunctionParams(node, allowModifiers);
  9062. }
  9063. parseVarId(decl, kind) {
  9064. super.parseVarId(decl, kind);
  9065. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  9066. decl.definite = true;
  9067. }
  9068. const type = this.tsTryParseTypeAnnotation();
  9069. if (type) {
  9070. decl.id.typeAnnotation = type;
  9071. this.resetEndLocation(decl.id);
  9072. }
  9073. }
  9074. parseAsyncArrowFromCallExpression(node, call) {
  9075. if (this.match(14)) {
  9076. node.returnType = this.tsParseTypeAnnotation();
  9077. }
  9078. return super.parseAsyncArrowFromCallExpression(node, call);
  9079. }
  9080. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9081. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  9082. let state;
  9083. let jsx;
  9084. let typeCast;
  9085. if (this.hasPlugin("jsx") && (this.match(138) || this.match(47))) {
  9086. state = this.state.clone();
  9087. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9088. if (!jsx.error) return jsx.node;
  9089. const {
  9090. context
  9091. } = this.state;
  9092. const currentContext = context[context.length - 1];
  9093. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  9094. context.pop();
  9095. }
  9096. }
  9097. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  9098. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9099. }
  9100. if (!state || state === this.state) state = this.state.clone();
  9101. let typeParameters;
  9102. const arrow = this.tryParse(abort => {
  9103. var _expr$extra, _typeParameters;
  9104. typeParameters = this.tsParseTypeParameters();
  9105. const expr = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9106. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9107. abort();
  9108. }
  9109. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  9110. this.resetStartLocationFromNode(expr, typeParameters);
  9111. }
  9112. expr.typeParameters = typeParameters;
  9113. return expr;
  9114. }, state);
  9115. if (!arrow.error && !arrow.aborted) {
  9116. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9117. return arrow.node;
  9118. }
  9119. if (!jsx) {
  9120. assert(!this.hasPlugin("jsx"));
  9121. typeCast = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9122. if (!typeCast.error) return typeCast.node;
  9123. }
  9124. if ((_jsx2 = jsx) != null && _jsx2.node) {
  9125. this.state = jsx.failState;
  9126. return jsx.node;
  9127. }
  9128. if (arrow.node) {
  9129. this.state = arrow.failState;
  9130. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9131. return arrow.node;
  9132. }
  9133. if ((_typeCast = typeCast) != null && _typeCast.node) {
  9134. this.state = typeCast.failState;
  9135. return typeCast.node;
  9136. }
  9137. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  9138. if (arrow.thrown) throw arrow.error;
  9139. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  9140. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  9141. }
  9142. reportReservedArrowTypeParam(node) {
  9143. var _node$extra;
  9144. if (node.params.length === 1 && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  9145. this.raise(TSErrors.ReservedArrowTypeParam, {
  9146. at: node
  9147. });
  9148. }
  9149. }
  9150. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9151. if (!this.hasPlugin("jsx") && this.match(47)) {
  9152. return this.tsParseTypeAssertion();
  9153. } else {
  9154. return super.parseMaybeUnary(refExpressionErrors, sawUnary);
  9155. }
  9156. }
  9157. parseArrow(node) {
  9158. if (this.match(14)) {
  9159. const result = this.tryParse(abort => {
  9160. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9161. if (this.canInsertSemicolon() || !this.match(19)) abort();
  9162. return returnType;
  9163. });
  9164. if (result.aborted) return;
  9165. if (!result.thrown) {
  9166. if (result.error) this.state = result.failState;
  9167. node.returnType = result.node;
  9168. }
  9169. }
  9170. return super.parseArrow(node);
  9171. }
  9172. parseAssignableListItemTypes(param) {
  9173. if (this.eat(17)) {
  9174. if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
  9175. this.raise(TSErrors.PatternIsOptional, {
  9176. at: param
  9177. });
  9178. }
  9179. param.optional = true;
  9180. }
  9181. const type = this.tsTryParseTypeAnnotation();
  9182. if (type) param.typeAnnotation = type;
  9183. this.resetEndLocation(param);
  9184. return param;
  9185. }
  9186. isAssignable(node, isBinding) {
  9187. switch (node.type) {
  9188. case "TSTypeCastExpression":
  9189. return this.isAssignable(node.expression, isBinding);
  9190. case "TSParameterProperty":
  9191. return true;
  9192. default:
  9193. return super.isAssignable(node, isBinding);
  9194. }
  9195. }
  9196. toAssignable(node, isLHS = false) {
  9197. switch (node.type) {
  9198. case "ParenthesizedExpression":
  9199. this.toAssignableParenthesizedExpression(node, isLHS);
  9200. break;
  9201. case "TSAsExpression":
  9202. case "TSNonNullExpression":
  9203. case "TSTypeAssertion":
  9204. if (isLHS) {
  9205. this.expressionScope.recordArrowParemeterBindingError(TSErrors.UnexpectedTypeCastInParameter, {
  9206. at: node
  9207. });
  9208. } else {
  9209. this.raise(TSErrors.UnexpectedTypeCastInParameter, {
  9210. at: node
  9211. });
  9212. }
  9213. this.toAssignable(node.expression, isLHS);
  9214. break;
  9215. case "AssignmentExpression":
  9216. if (!isLHS && node.left.type === "TSTypeCastExpression") {
  9217. node.left = this.typeCastToParameter(node.left);
  9218. }
  9219. default:
  9220. super.toAssignable(node, isLHS);
  9221. }
  9222. }
  9223. toAssignableParenthesizedExpression(node, isLHS) {
  9224. switch (node.expression.type) {
  9225. case "TSAsExpression":
  9226. case "TSNonNullExpression":
  9227. case "TSTypeAssertion":
  9228. case "ParenthesizedExpression":
  9229. this.toAssignable(node.expression, isLHS);
  9230. break;
  9231. default:
  9232. super.toAssignable(node, isLHS);
  9233. }
  9234. }
  9235. checkToRestConversion(node, allowPattern) {
  9236. switch (node.type) {
  9237. case "TSAsExpression":
  9238. case "TSTypeAssertion":
  9239. case "TSNonNullExpression":
  9240. this.checkToRestConversion(node.expression, false);
  9241. break;
  9242. default:
  9243. super.checkToRestConversion(node, allowPattern);
  9244. }
  9245. }
  9246. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  9247. return getOwn$1({
  9248. TSTypeCastExpression: true,
  9249. TSParameterProperty: "parameter",
  9250. TSNonNullExpression: "expression",
  9251. TSAsExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9252. TSTypeAssertion: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true]
  9253. }, type) || super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  9254. }
  9255. parseBindingAtom() {
  9256. switch (this.state.type) {
  9257. case 78:
  9258. return this.parseIdentifier(true);
  9259. default:
  9260. return super.parseBindingAtom();
  9261. }
  9262. }
  9263. parseMaybeDecoratorArguments(expr) {
  9264. if (this.match(47) || this.match(51)) {
  9265. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9266. if (this.match(10)) {
  9267. const call = super.parseMaybeDecoratorArguments(expr);
  9268. call.typeParameters = typeArguments;
  9269. return call;
  9270. }
  9271. this.unexpected(null, 10);
  9272. }
  9273. return super.parseMaybeDecoratorArguments(expr);
  9274. }
  9275. checkCommaAfterRest(close) {
  9276. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9277. this.next();
  9278. return false;
  9279. } else {
  9280. return super.checkCommaAfterRest(close);
  9281. }
  9282. }
  9283. isClassMethod() {
  9284. return this.match(47) || super.isClassMethod();
  9285. }
  9286. isClassProperty() {
  9287. return this.match(35) || this.match(14) || super.isClassProperty();
  9288. }
  9289. parseMaybeDefault(startPos, startLoc, left) {
  9290. const node = super.parseMaybeDefault(startPos, startLoc, left);
  9291. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9292. this.raise(TSErrors.TypeAnnotationAfterAssign, {
  9293. at: node.typeAnnotation
  9294. });
  9295. }
  9296. return node;
  9297. }
  9298. getTokenFromCode(code) {
  9299. if (this.state.inType) {
  9300. if (code === 62) {
  9301. return this.finishOp(48, 1);
  9302. }
  9303. if (code === 60) {
  9304. return this.finishOp(47, 1);
  9305. }
  9306. }
  9307. return super.getTokenFromCode(code);
  9308. }
  9309. reScan_lt_gt() {
  9310. const {
  9311. type
  9312. } = this.state;
  9313. if (type === 47) {
  9314. this.state.pos -= 1;
  9315. this.readToken_lt();
  9316. } else if (type === 48) {
  9317. this.state.pos -= 1;
  9318. this.readToken_gt();
  9319. }
  9320. }
  9321. reScan_lt() {
  9322. const {
  9323. type
  9324. } = this.state;
  9325. if (type === 51) {
  9326. this.state.pos -= 2;
  9327. this.finishOp(47, 1);
  9328. return 47;
  9329. }
  9330. return type;
  9331. }
  9332. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9333. for (let i = 0; i < exprList.length; i++) {
  9334. const expr = exprList[i];
  9335. if ((expr == null ? void 0 : expr.type) === "TSTypeCastExpression") {
  9336. exprList[i] = this.typeCastToParameter(expr);
  9337. }
  9338. }
  9339. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  9340. }
  9341. typeCastToParameter(node) {
  9342. node.expression.typeAnnotation = node.typeAnnotation;
  9343. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  9344. return node.expression;
  9345. }
  9346. shouldParseArrow(params) {
  9347. if (this.match(14)) {
  9348. return params.every(expr => this.isAssignable(expr, true));
  9349. }
  9350. return super.shouldParseArrow(params);
  9351. }
  9352. shouldParseAsyncArrow() {
  9353. return this.match(14) || super.shouldParseAsyncArrow();
  9354. }
  9355. canHaveLeadingDecorator() {
  9356. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  9357. }
  9358. jsxParseOpeningElementAfterName(node) {
  9359. if (this.match(47) || this.match(51)) {
  9360. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  9361. if (typeArguments) node.typeParameters = typeArguments;
  9362. }
  9363. return super.jsxParseOpeningElementAfterName(node);
  9364. }
  9365. getGetterSetterExpectedParamCount(method) {
  9366. const baseCount = super.getGetterSetterExpectedParamCount(method);
  9367. const params = this.getObjectOrClassMethodParams(method);
  9368. const firstParam = params[0];
  9369. const hasContextParam = firstParam && this.isThisParam(firstParam);
  9370. return hasContextParam ? baseCount + 1 : baseCount;
  9371. }
  9372. parseCatchClauseParam() {
  9373. const param = super.parseCatchClauseParam();
  9374. const type = this.tsTryParseTypeAnnotation();
  9375. if (type) {
  9376. param.typeAnnotation = type;
  9377. this.resetEndLocation(param);
  9378. }
  9379. return param;
  9380. }
  9381. tsInAmbientContext(cb) {
  9382. const oldIsAmbientContext = this.state.isAmbientContext;
  9383. this.state.isAmbientContext = true;
  9384. try {
  9385. return cb();
  9386. } finally {
  9387. this.state.isAmbientContext = oldIsAmbientContext;
  9388. }
  9389. }
  9390. parseClass(node, isStatement, optionalId) {
  9391. const oldInAbstractClass = this.state.inAbstractClass;
  9392. this.state.inAbstractClass = !!node.abstract;
  9393. try {
  9394. return super.parseClass(node, isStatement, optionalId);
  9395. } finally {
  9396. this.state.inAbstractClass = oldInAbstractClass;
  9397. }
  9398. }
  9399. tsParseAbstractDeclaration(node) {
  9400. if (this.match(80)) {
  9401. node.abstract = true;
  9402. return this.parseClass(node, true, false);
  9403. } else if (this.isContextual(125)) {
  9404. if (!this.hasFollowingLineBreak()) {
  9405. node.abstract = true;
  9406. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, {
  9407. at: node
  9408. });
  9409. return this.tsParseInterfaceDeclaration(node);
  9410. }
  9411. } else {
  9412. this.unexpected(null, 80);
  9413. }
  9414. }
  9415. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope) {
  9416. const method = super.parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  9417. if (method.abstract) {
  9418. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  9419. if (hasBody) {
  9420. const {
  9421. key
  9422. } = method;
  9423. this.raise(TSErrors.AbstractMethodHasImplementation, {
  9424. at: method,
  9425. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9426. });
  9427. }
  9428. }
  9429. return method;
  9430. }
  9431. tsParseTypeParameterName() {
  9432. const typeName = this.parseIdentifier();
  9433. return typeName.name;
  9434. }
  9435. shouldParseAsAmbientContext() {
  9436. return !!this.getPluginOption("typescript", "dts");
  9437. }
  9438. parse() {
  9439. if (this.shouldParseAsAmbientContext()) {
  9440. this.state.isAmbientContext = true;
  9441. }
  9442. return super.parse();
  9443. }
  9444. getExpression() {
  9445. if (this.shouldParseAsAmbientContext()) {
  9446. this.state.isAmbientContext = true;
  9447. }
  9448. return super.getExpression();
  9449. }
  9450. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  9451. if (!isString && isMaybeTypeOnly) {
  9452. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  9453. return this.finishNode(node, "ExportSpecifier");
  9454. }
  9455. node.exportKind = "value";
  9456. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  9457. }
  9458. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  9459. if (!importedIsString && isMaybeTypeOnly) {
  9460. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  9461. return this.finishNode(specifier, "ImportSpecifier");
  9462. }
  9463. specifier.importKind = "value";
  9464. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly);
  9465. }
  9466. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  9467. const leftOfAsKey = isImport ? "imported" : "local";
  9468. const rightOfAsKey = isImport ? "local" : "exported";
  9469. let leftOfAs = node[leftOfAsKey];
  9470. let rightOfAs;
  9471. let hasTypeSpecifier = false;
  9472. let canParseAsKeyword = true;
  9473. const loc = leftOfAs.loc.start;
  9474. if (this.isContextual(93)) {
  9475. const firstAs = this.parseIdentifier();
  9476. if (this.isContextual(93)) {
  9477. const secondAs = this.parseIdentifier();
  9478. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9479. hasTypeSpecifier = true;
  9480. leftOfAs = firstAs;
  9481. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9482. canParseAsKeyword = false;
  9483. } else {
  9484. rightOfAs = secondAs;
  9485. canParseAsKeyword = false;
  9486. }
  9487. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9488. canParseAsKeyword = false;
  9489. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9490. } else {
  9491. hasTypeSpecifier = true;
  9492. leftOfAs = firstAs;
  9493. }
  9494. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9495. hasTypeSpecifier = true;
  9496. if (isImport) {
  9497. leftOfAs = this.parseIdentifier(true);
  9498. if (!this.isContextual(93)) {
  9499. this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
  9500. }
  9501. } else {
  9502. leftOfAs = this.parseModuleExportName();
  9503. }
  9504. }
  9505. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  9506. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, {
  9507. at: loc
  9508. });
  9509. }
  9510. node[leftOfAsKey] = leftOfAs;
  9511. node[rightOfAsKey] = rightOfAs;
  9512. const kindKey = isImport ? "importKind" : "exportKind";
  9513. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  9514. if (canParseAsKeyword && this.eatContextual(93)) {
  9515. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9516. }
  9517. if (!node[rightOfAsKey]) {
  9518. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  9519. }
  9520. if (isImport) {
  9521. this.checkIdentifier(node[rightOfAsKey], BIND_LEXICAL);
  9522. }
  9523. }
  9524. });
  9525. function isPossiblyLiteralEnum(expression) {
  9526. if (expression.type !== "MemberExpression") return false;
  9527. const {
  9528. computed,
  9529. property
  9530. } = expression;
  9531. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  9532. return false;
  9533. }
  9534. return isUncomputedMemberExpressionChain(expression.object);
  9535. }
  9536. function isUncomputedMemberExpressionChain(expression) {
  9537. if (expression.type === "Identifier") return true;
  9538. if (expression.type !== "MemberExpression") return false;
  9539. if (expression.computed) return false;
  9540. return isUncomputedMemberExpressionChain(expression.object);
  9541. }
  9542. const PlaceholderErrors = ParseErrorEnum`placeholders`({
  9543. ClassNameIsRequired: "A class name is required.",
  9544. UnexpectedSpace: "Unexpected space in placeholder."
  9545. });
  9546. var placeholders = (superClass => class PlaceholdersParserMixin extends superClass {
  9547. parsePlaceholder(expectedNode) {
  9548. if (this.match(140)) {
  9549. const node = this.startNode();
  9550. this.next();
  9551. this.assertNoSpace();
  9552. node.name = super.parseIdentifier(true);
  9553. this.assertNoSpace();
  9554. this.expect(140);
  9555. return this.finishPlaceholder(node, expectedNode);
  9556. }
  9557. }
  9558. finishPlaceholder(node, expectedNode) {
  9559. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  9560. node.expectedNode = expectedNode;
  9561. return isFinished ? node : this.finishNode(node, "Placeholder");
  9562. }
  9563. getTokenFromCode(code) {
  9564. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  9565. return this.finishOp(140, 2);
  9566. }
  9567. return super.getTokenFromCode(code);
  9568. }
  9569. parseExprAtom(refExpressionErrors) {
  9570. return this.parsePlaceholder("Expression") || super.parseExprAtom(refExpressionErrors);
  9571. }
  9572. parseIdentifier(liberal) {
  9573. return this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal);
  9574. }
  9575. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9576. if (word !== undefined) {
  9577. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  9578. }
  9579. }
  9580. parseBindingAtom() {
  9581. return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
  9582. }
  9583. isValidLVal(type, isParenthesized, binding) {
  9584. return type === "Placeholder" || super.isValidLVal(type, isParenthesized, binding);
  9585. }
  9586. toAssignable(node, isLHS) {
  9587. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  9588. node.expectedNode = "Pattern";
  9589. } else {
  9590. super.toAssignable(node, isLHS);
  9591. }
  9592. }
  9593. isLet(context) {
  9594. if (super.isLet(context)) {
  9595. return true;
  9596. }
  9597. if (!this.isContextual(99)) {
  9598. return false;
  9599. }
  9600. if (context) return false;
  9601. const nextToken = this.lookahead();
  9602. if (nextToken.type === 140) {
  9603. return true;
  9604. }
  9605. return false;
  9606. }
  9607. verifyBreakContinue(node, isBreak) {
  9608. if (node.label && node.label.type === "Placeholder") return;
  9609. super.verifyBreakContinue(node, isBreak);
  9610. }
  9611. parseExpressionStatement(node, expr) {
  9612. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  9613. return super.parseExpressionStatement(node, expr);
  9614. }
  9615. if (this.match(14)) {
  9616. const stmt = node;
  9617. stmt.label = this.finishPlaceholder(expr, "Identifier");
  9618. this.next();
  9619. stmt.body = super.parseStatement("label");
  9620. return this.finishNode(stmt, "LabeledStatement");
  9621. }
  9622. this.semicolon();
  9623. node.name = expr.name;
  9624. return this.finishPlaceholder(node, "Statement");
  9625. }
  9626. parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse) {
  9627. return this.parsePlaceholder("BlockStatement") || super.parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse);
  9628. }
  9629. parseFunctionId(requireId) {
  9630. return this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId);
  9631. }
  9632. parseClass(node, isStatement, optionalId) {
  9633. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  9634. this.next();
  9635. this.takeDecorators(node);
  9636. const oldStrict = this.state.strict;
  9637. const placeholder = this.parsePlaceholder("Identifier");
  9638. if (placeholder) {
  9639. if (this.match(81) || this.match(140) || this.match(5)) {
  9640. node.id = placeholder;
  9641. } else if (optionalId || !isStatement) {
  9642. node.id = null;
  9643. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  9644. return this.finishNode(node, type);
  9645. } else {
  9646. throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
  9647. at: this.state.startLoc
  9648. });
  9649. }
  9650. } else {
  9651. this.parseClassId(node, isStatement, optionalId);
  9652. }
  9653. super.parseClassSuper(node);
  9654. node.body = this.parsePlaceholder("ClassBody") || super.parseClassBody(!!node.superClass, oldStrict);
  9655. return this.finishNode(node, type);
  9656. }
  9657. parseExport(node) {
  9658. const placeholder = this.parsePlaceholder("Identifier");
  9659. if (!placeholder) return super.parseExport(node);
  9660. if (!this.isContextual(97) && !this.match(12)) {
  9661. node.specifiers = [];
  9662. node.source = null;
  9663. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  9664. return this.finishNode(node, "ExportNamedDeclaration");
  9665. }
  9666. this.expectPlugin("exportDefaultFrom");
  9667. const specifier = this.startNode();
  9668. specifier.exported = placeholder;
  9669. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  9670. return super.parseExport(node);
  9671. }
  9672. isExportDefaultSpecifier() {
  9673. if (this.match(65)) {
  9674. const next = this.nextTokenStart();
  9675. if (this.isUnparsedContextual(next, "from")) {
  9676. if (this.input.startsWith(tokenLabelName(140), this.nextTokenStartSince(next + 4))) {
  9677. return true;
  9678. }
  9679. }
  9680. }
  9681. return super.isExportDefaultSpecifier();
  9682. }
  9683. maybeParseExportDefaultSpecifier(node) {
  9684. if (node.specifiers && node.specifiers.length > 0) {
  9685. return true;
  9686. }
  9687. return super.maybeParseExportDefaultSpecifier(node);
  9688. }
  9689. checkExport(node) {
  9690. const {
  9691. specifiers
  9692. } = node;
  9693. if (specifiers != null && specifiers.length) {
  9694. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  9695. }
  9696. super.checkExport(node);
  9697. node.specifiers = specifiers;
  9698. }
  9699. parseImport(node) {
  9700. const placeholder = this.parsePlaceholder("Identifier");
  9701. if (!placeholder) return super.parseImport(node);
  9702. node.specifiers = [];
  9703. if (!this.isContextual(97) && !this.match(12)) {
  9704. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  9705. this.semicolon();
  9706. return this.finishNode(node, "ImportDeclaration");
  9707. }
  9708. const specifier = this.startNodeAtNode(placeholder);
  9709. specifier.local = placeholder;
  9710. node.specifiers.push(this.finishNode(specifier, "ImportDefaultSpecifier"));
  9711. if (this.eat(12)) {
  9712. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  9713. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  9714. }
  9715. this.expectContextual(97);
  9716. node.source = this.parseImportSource();
  9717. this.semicolon();
  9718. return this.finishNode(node, "ImportDeclaration");
  9719. }
  9720. parseImportSource() {
  9721. return this.parsePlaceholder("StringLiteral") || super.parseImportSource();
  9722. }
  9723. assertNoSpace() {
  9724. if (this.state.start > this.state.lastTokEndLoc.index) {
  9725. this.raise(PlaceholderErrors.UnexpectedSpace, {
  9726. at: this.state.lastTokEndLoc
  9727. });
  9728. }
  9729. }
  9730. });
  9731. var v8intrinsic = (superClass => class V8IntrinsicMixin extends superClass {
  9732. parseV8Intrinsic() {
  9733. if (this.match(54)) {
  9734. const v8IntrinsicStartLoc = this.state.startLoc;
  9735. const node = this.startNode();
  9736. this.next();
  9737. if (tokenIsIdentifier(this.state.type)) {
  9738. const name = this.parseIdentifierName(this.state.start);
  9739. const identifier = this.createIdentifier(node, name);
  9740. identifier.type = "V8IntrinsicIdentifier";
  9741. if (this.match(10)) {
  9742. return identifier;
  9743. }
  9744. }
  9745. this.unexpected(v8IntrinsicStartLoc);
  9746. }
  9747. }
  9748. parseExprAtom(refExpressionErrors) {
  9749. return this.parseV8Intrinsic() || super.parseExprAtom(refExpressionErrors);
  9750. }
  9751. });
  9752. function hasPlugin(plugins, expectedConfig) {
  9753. const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
  9754. const expectedKeys = Object.keys(expectedOptions);
  9755. const expectedOptionsIsEmpty = expectedKeys.length === 0;
  9756. return plugins.some(p => {
  9757. if (typeof p === "string") {
  9758. return expectedOptionsIsEmpty && p === expectedName;
  9759. } else {
  9760. const [pluginName, pluginOptions] = p;
  9761. if (pluginName !== expectedName) {
  9762. return false;
  9763. }
  9764. for (const key of expectedKeys) {
  9765. if (pluginOptions[key] !== expectedOptions[key]) {
  9766. return false;
  9767. }
  9768. }
  9769. return true;
  9770. }
  9771. });
  9772. }
  9773. function getPluginOption(plugins, name, option) {
  9774. const plugin = plugins.find(plugin => {
  9775. if (Array.isArray(plugin)) {
  9776. return plugin[0] === name;
  9777. } else {
  9778. return plugin === name;
  9779. }
  9780. });
  9781. if (plugin && Array.isArray(plugin) && plugin.length > 1) {
  9782. return plugin[1][option];
  9783. }
  9784. return null;
  9785. }
  9786. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  9787. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  9788. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  9789. function validatePlugins(plugins) {
  9790. if (hasPlugin(plugins, "decorators")) {
  9791. if (hasPlugin(plugins, "decorators-legacy")) {
  9792. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  9793. }
  9794. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  9795. if (decoratorsBeforeExport == null) {
  9796. throw new Error("The 'decorators' plugin requires a 'decoratorsBeforeExport' option," + " whose value must be a boolean. If you are migrating from" + " Babylon/Babel 6 or want to use the old decorators proposal, you" + " should use the 'decorators-legacy' plugin instead of 'decorators'.");
  9797. } else if (typeof decoratorsBeforeExport !== "boolean") {
  9798. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  9799. }
  9800. }
  9801. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  9802. throw new Error("Cannot combine flow and typescript plugins.");
  9803. }
  9804. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  9805. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  9806. }
  9807. if (hasPlugin(plugins, "pipelineOperator")) {
  9808. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  9809. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  9810. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  9811. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  9812. }
  9813. const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
  9814. syntaxType: "hash"
  9815. }]);
  9816. if (proposal === "hack") {
  9817. if (hasPlugin(plugins, "placeholders")) {
  9818. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  9819. }
  9820. if (hasPlugin(plugins, "v8intrinsic")) {
  9821. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  9822. }
  9823. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  9824. if (!TOPIC_TOKENS.includes(topicToken)) {
  9825. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  9826. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  9827. }
  9828. if (topicToken === "#" && tupleSyntaxIsHash) {
  9829. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  9830. }
  9831. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  9832. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  9833. }
  9834. }
  9835. if (hasPlugin(plugins, "moduleAttributes")) {
  9836. {
  9837. if (hasPlugin(plugins, "importAssertions")) {
  9838. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  9839. }
  9840. const moduleAttributesVersionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  9841. if (moduleAttributesVersionPluginOption !== "may-2020") {
  9842. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  9843. }
  9844. }
  9845. }
  9846. if (hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  9847. throw new Error("'recordAndTuple' requires 'syntaxType' option whose value should be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  9848. }
  9849. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  9850. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  9851. error.missingPlugins = "doExpressions";
  9852. throw error;
  9853. }
  9854. }
  9855. const mixinPlugins = {
  9856. estree,
  9857. jsx,
  9858. flow,
  9859. typescript,
  9860. v8intrinsic,
  9861. placeholders
  9862. };
  9863. const mixinPluginNames = Object.keys(mixinPlugins);
  9864. const defaultOptions = {
  9865. sourceType: "script",
  9866. sourceFilename: undefined,
  9867. startColumn: 0,
  9868. startLine: 1,
  9869. allowAwaitOutsideFunction: false,
  9870. allowReturnOutsideFunction: false,
  9871. allowImportExportEverywhere: false,
  9872. allowSuperOutsideMethod: false,
  9873. allowUndeclaredExports: false,
  9874. plugins: [],
  9875. strictMode: null,
  9876. ranges: false,
  9877. tokens: false,
  9878. createParenthesizedExpressions: false,
  9879. errorRecovery: false,
  9880. attachComment: true
  9881. };
  9882. function getOptions(opts) {
  9883. const options = {};
  9884. for (const key of Object.keys(defaultOptions)) {
  9885. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  9886. }
  9887. return options;
  9888. }
  9889. const getOwn = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  9890. const unwrapParenthesizedExpression = node => {
  9891. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  9892. };
  9893. class LValParser extends NodeUtils {
  9894. toAssignable(node, isLHS = false) {
  9895. var _node$extra, _node$extra3;
  9896. let parenthesized = undefined;
  9897. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  9898. parenthesized = unwrapParenthesizedExpression(node);
  9899. if (isLHS) {
  9900. if (parenthesized.type === "Identifier") {
  9901. this.expressionScope.recordArrowParemeterBindingError(Errors.InvalidParenthesizedAssignment, {
  9902. at: node
  9903. });
  9904. } else if (parenthesized.type !== "MemberExpression") {
  9905. this.raise(Errors.InvalidParenthesizedAssignment, {
  9906. at: node
  9907. });
  9908. }
  9909. } else {
  9910. this.raise(Errors.InvalidParenthesizedAssignment, {
  9911. at: node
  9912. });
  9913. }
  9914. }
  9915. switch (node.type) {
  9916. case "Identifier":
  9917. case "ObjectPattern":
  9918. case "ArrayPattern":
  9919. case "AssignmentPattern":
  9920. case "RestElement":
  9921. break;
  9922. case "ObjectExpression":
  9923. node.type = "ObjectPattern";
  9924. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  9925. var _node$extra2;
  9926. const prop = node.properties[i];
  9927. const isLast = i === last;
  9928. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  9929. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  9930. this.raise(Errors.RestTrailingComma, {
  9931. at: node.extra.trailingCommaLoc
  9932. });
  9933. }
  9934. }
  9935. break;
  9936. case "ObjectProperty":
  9937. {
  9938. const {
  9939. key,
  9940. value
  9941. } = node;
  9942. if (this.isPrivateName(key)) {
  9943. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  9944. }
  9945. this.toAssignable(value, isLHS);
  9946. break;
  9947. }
  9948. case "SpreadElement":
  9949. {
  9950. throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
  9951. }
  9952. case "ArrayExpression":
  9953. node.type = "ArrayPattern";
  9954. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  9955. break;
  9956. case "AssignmentExpression":
  9957. if (node.operator !== "=") {
  9958. this.raise(Errors.MissingEqInAssignment, {
  9959. at: node.left.loc.end
  9960. });
  9961. }
  9962. node.type = "AssignmentPattern";
  9963. delete node.operator;
  9964. this.toAssignable(node.left, isLHS);
  9965. break;
  9966. case "ParenthesizedExpression":
  9967. this.toAssignable(parenthesized, isLHS);
  9968. break;
  9969. }
  9970. }
  9971. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  9972. if (prop.type === "ObjectMethod") {
  9973. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, {
  9974. at: prop.key
  9975. });
  9976. } else if (prop.type === "SpreadElement") {
  9977. prop.type = "RestElement";
  9978. const arg = prop.argument;
  9979. this.checkToRestConversion(arg, false);
  9980. this.toAssignable(arg, isLHS);
  9981. if (!isLast) {
  9982. this.raise(Errors.RestTrailingComma, {
  9983. at: prop
  9984. });
  9985. }
  9986. } else {
  9987. this.toAssignable(prop, isLHS);
  9988. }
  9989. }
  9990. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9991. const end = exprList.length - 1;
  9992. for (let i = 0; i <= end; i++) {
  9993. const elt = exprList[i];
  9994. if (!elt) continue;
  9995. if (elt.type === "SpreadElement") {
  9996. elt.type = "RestElement";
  9997. const arg = elt.argument;
  9998. this.checkToRestConversion(arg, true);
  9999. this.toAssignable(arg, isLHS);
  10000. } else {
  10001. this.toAssignable(elt, isLHS);
  10002. }
  10003. if (elt.type === "RestElement") {
  10004. if (i < end) {
  10005. this.raise(Errors.RestTrailingComma, {
  10006. at: elt
  10007. });
  10008. } else if (trailingCommaLoc) {
  10009. this.raise(Errors.RestTrailingComma, {
  10010. at: trailingCommaLoc
  10011. });
  10012. }
  10013. }
  10014. }
  10015. }
  10016. isAssignable(node, isBinding) {
  10017. switch (node.type) {
  10018. case "Identifier":
  10019. case "ObjectPattern":
  10020. case "ArrayPattern":
  10021. case "AssignmentPattern":
  10022. case "RestElement":
  10023. return true;
  10024. case "ObjectExpression":
  10025. {
  10026. const last = node.properties.length - 1;
  10027. return node.properties.every((prop, i) => {
  10028. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  10029. });
  10030. }
  10031. case "ObjectProperty":
  10032. return this.isAssignable(node.value);
  10033. case "SpreadElement":
  10034. return this.isAssignable(node.argument);
  10035. case "ArrayExpression":
  10036. return node.elements.every(element => element === null || this.isAssignable(element));
  10037. case "AssignmentExpression":
  10038. return node.operator === "=";
  10039. case "ParenthesizedExpression":
  10040. return this.isAssignable(node.expression);
  10041. case "MemberExpression":
  10042. case "OptionalMemberExpression":
  10043. return !isBinding;
  10044. default:
  10045. return false;
  10046. }
  10047. }
  10048. toReferencedList(exprList, isParenthesizedExpr) {
  10049. return exprList;
  10050. }
  10051. toReferencedListDeep(exprList, isParenthesizedExpr) {
  10052. this.toReferencedList(exprList, isParenthesizedExpr);
  10053. for (const expr of exprList) {
  10054. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  10055. this.toReferencedListDeep(expr.elements);
  10056. }
  10057. }
  10058. }
  10059. parseSpread(refExpressionErrors) {
  10060. const node = this.startNode();
  10061. this.next();
  10062. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined);
  10063. return this.finishNode(node, "SpreadElement");
  10064. }
  10065. parseRestBinding() {
  10066. const node = this.startNode();
  10067. this.next();
  10068. node.argument = this.parseBindingAtom();
  10069. return this.finishNode(node, "RestElement");
  10070. }
  10071. parseBindingAtom() {
  10072. switch (this.state.type) {
  10073. case 0:
  10074. {
  10075. const node = this.startNode();
  10076. this.next();
  10077. node.elements = this.parseBindingList(3, 93, true);
  10078. return this.finishNode(node, "ArrayPattern");
  10079. }
  10080. case 5:
  10081. return this.parseObjectLike(8, true);
  10082. }
  10083. return this.parseIdentifier();
  10084. }
  10085. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  10086. const elts = [];
  10087. let first = true;
  10088. while (!this.eat(close)) {
  10089. if (first) {
  10090. first = false;
  10091. } else {
  10092. this.expect(12);
  10093. }
  10094. if (allowEmpty && this.match(12)) {
  10095. elts.push(null);
  10096. } else if (this.eat(close)) {
  10097. break;
  10098. } else if (this.match(21)) {
  10099. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  10100. if (!this.checkCommaAfterRest(closeCharCode)) {
  10101. this.expect(close);
  10102. break;
  10103. }
  10104. } else {
  10105. const decorators = [];
  10106. if (this.match(26) && this.hasPlugin("decorators")) {
  10107. this.raise(Errors.UnsupportedParameterDecorator, {
  10108. at: this.state.startLoc
  10109. });
  10110. }
  10111. while (this.match(26)) {
  10112. decorators.push(this.parseDecorator());
  10113. }
  10114. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  10115. }
  10116. }
  10117. return elts;
  10118. }
  10119. parseBindingRestProperty(prop) {
  10120. this.next();
  10121. prop.argument = this.parseIdentifier();
  10122. this.checkCommaAfterRest(125);
  10123. return this.finishNode(prop, "RestElement");
  10124. }
  10125. parseBindingProperty() {
  10126. const prop = this.startNode();
  10127. const {
  10128. type,
  10129. start: startPos,
  10130. startLoc
  10131. } = this.state;
  10132. if (type === 21) {
  10133. return this.parseBindingRestProperty(prop);
  10134. } else if (type === 134) {
  10135. this.expectPlugin("destructuringPrivate", startLoc);
  10136. this.classScope.usePrivateName(this.state.value, startLoc);
  10137. prop.key = this.parsePrivateName();
  10138. } else {
  10139. this.parsePropertyName(prop);
  10140. }
  10141. prop.method = false;
  10142. return this.parseObjPropValue(prop, startPos, startLoc, false, false, true, false);
  10143. }
  10144. parseAssignableListItem(allowModifiers, decorators) {
  10145. const left = this.parseMaybeDefault();
  10146. this.parseAssignableListItemTypes(left);
  10147. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  10148. if (decorators.length) {
  10149. left.decorators = decorators;
  10150. }
  10151. return elt;
  10152. }
  10153. parseAssignableListItemTypes(param) {
  10154. return param;
  10155. }
  10156. parseMaybeDefault(startPos, startLoc, left) {
  10157. var _startLoc, _startPos, _left;
  10158. startLoc = (_startLoc = startLoc) != null ? _startLoc : this.state.startLoc;
  10159. startPos = (_startPos = startPos) != null ? _startPos : this.state.start;
  10160. left = (_left = left) != null ? _left : this.parseBindingAtom();
  10161. if (!this.eat(29)) return left;
  10162. const node = this.startNodeAt(startPos, startLoc);
  10163. node.left = left;
  10164. node.right = this.parseMaybeAssignAllowIn();
  10165. return this.finishNode(node, "AssignmentPattern");
  10166. }
  10167. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  10168. return getOwn({
  10169. AssignmentPattern: "left",
  10170. RestElement: "argument",
  10171. ObjectProperty: "value",
  10172. ParenthesizedExpression: "expression",
  10173. ArrayPattern: "elements",
  10174. ObjectPattern: "properties"
  10175. }, type);
  10176. }
  10177. checkLVal(expression, {
  10178. in: ancestor,
  10179. binding = BIND_NONE,
  10180. checkClashes = false,
  10181. strictModeChanged = false,
  10182. allowingSloppyLetBinding = !(binding & BIND_SCOPE_LEXICAL),
  10183. hasParenthesizedAncestor = false
  10184. }) {
  10185. var _expression$extra;
  10186. const type = expression.type;
  10187. if (this.isObjectMethod(expression)) return;
  10188. if (type === "MemberExpression") {
  10189. if (binding !== BIND_NONE) {
  10190. this.raise(Errors.InvalidPropertyBindingPattern, {
  10191. at: expression
  10192. });
  10193. }
  10194. return;
  10195. }
  10196. if (expression.type === "Identifier") {
  10197. this.checkIdentifier(expression, binding, strictModeChanged, allowingSloppyLetBinding);
  10198. const {
  10199. name
  10200. } = expression;
  10201. if (checkClashes) {
  10202. if (checkClashes.has(name)) {
  10203. this.raise(Errors.ParamDupe, {
  10204. at: expression
  10205. });
  10206. } else {
  10207. checkClashes.add(name);
  10208. }
  10209. }
  10210. return;
  10211. }
  10212. const validity = this.isValidLVal(expression.type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
  10213. if (validity === true) return;
  10214. if (validity === false) {
  10215. const ParseErrorClass = binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  10216. this.raise(ParseErrorClass, {
  10217. at: expression,
  10218. ancestor: ancestor.type === "UpdateExpression" ? {
  10219. type: "UpdateExpression",
  10220. prefix: ancestor.prefix
  10221. } : {
  10222. type: ancestor.type
  10223. }
  10224. });
  10225. return;
  10226. }
  10227. const [key, isParenthesizedExpression] = Array.isArray(validity) ? validity : [validity, type === "ParenthesizedExpression"];
  10228. const nextAncestor = expression.type === "ArrayPattern" || expression.type === "ObjectPattern" || expression.type === "ParenthesizedExpression" ? expression : ancestor;
  10229. for (const child of [].concat(expression[key])) {
  10230. if (child) {
  10231. this.checkLVal(child, {
  10232. in: nextAncestor,
  10233. binding,
  10234. checkClashes,
  10235. allowingSloppyLetBinding,
  10236. strictModeChanged,
  10237. hasParenthesizedAncestor: isParenthesizedExpression
  10238. });
  10239. }
  10240. }
  10241. }
  10242. checkIdentifier(at, bindingType, strictModeChanged = false, allowLetBinding = !(bindingType & BIND_SCOPE_LEXICAL)) {
  10243. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  10244. if (bindingType === BIND_NONE) {
  10245. this.raise(Errors.StrictEvalArguments, {
  10246. at,
  10247. referenceName: at.name
  10248. });
  10249. } else {
  10250. this.raise(Errors.StrictEvalArgumentsBinding, {
  10251. at,
  10252. bindingName: at.name
  10253. });
  10254. }
  10255. }
  10256. if (!allowLetBinding && at.name === "let") {
  10257. this.raise(Errors.LetInLexicalBinding, {
  10258. at
  10259. });
  10260. }
  10261. if (!(bindingType & BIND_NONE)) {
  10262. this.declareNameFromIdentifier(at, bindingType);
  10263. }
  10264. }
  10265. declareNameFromIdentifier(identifier, binding) {
  10266. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  10267. }
  10268. checkToRestConversion(node, allowPattern) {
  10269. switch (node.type) {
  10270. case "ParenthesizedExpression":
  10271. this.checkToRestConversion(node.expression, allowPattern);
  10272. break;
  10273. case "Identifier":
  10274. case "MemberExpression":
  10275. break;
  10276. case "ArrayExpression":
  10277. case "ObjectExpression":
  10278. if (allowPattern) break;
  10279. default:
  10280. this.raise(Errors.InvalidRestAssignmentPattern, {
  10281. at: node
  10282. });
  10283. }
  10284. }
  10285. checkCommaAfterRest(close) {
  10286. if (!this.match(12)) {
  10287. return false;
  10288. }
  10289. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, {
  10290. at: this.state.startLoc
  10291. });
  10292. return true;
  10293. }
  10294. }
  10295. class ExpressionParser extends LValParser {
  10296. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  10297. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10298. return;
  10299. }
  10300. const key = prop.key;
  10301. const name = key.type === "Identifier" ? key.name : key.value;
  10302. if (name === "__proto__") {
  10303. if (isRecord) {
  10304. this.raise(Errors.RecordNoProto, {
  10305. at: key
  10306. });
  10307. return;
  10308. }
  10309. if (protoRef.used) {
  10310. if (refExpressionErrors) {
  10311. if (refExpressionErrors.doubleProtoLoc === null) {
  10312. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10313. }
  10314. } else {
  10315. this.raise(Errors.DuplicateProto, {
  10316. at: key
  10317. });
  10318. }
  10319. }
  10320. protoRef.used = true;
  10321. }
  10322. }
  10323. shouldExitDescending(expr, potentialArrowAt) {
  10324. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  10325. }
  10326. getExpression() {
  10327. this.enterInitialScopes();
  10328. this.nextToken();
  10329. const expr = this.parseExpression();
  10330. if (!this.match(135)) {
  10331. this.unexpected();
  10332. }
  10333. this.finalizeRemainingComments();
  10334. expr.comments = this.state.comments;
  10335. expr.errors = this.state.errors;
  10336. if (this.options.tokens) {
  10337. expr.tokens = this.tokens;
  10338. }
  10339. return expr;
  10340. }
  10341. parseExpression(disallowIn, refExpressionErrors) {
  10342. if (disallowIn) {
  10343. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10344. }
  10345. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10346. }
  10347. parseExpressionBase(refExpressionErrors) {
  10348. const startPos = this.state.start;
  10349. const startLoc = this.state.startLoc;
  10350. const expr = this.parseMaybeAssign(refExpressionErrors);
  10351. if (this.match(12)) {
  10352. const node = this.startNodeAt(startPos, startLoc);
  10353. node.expressions = [expr];
  10354. while (this.eat(12)) {
  10355. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10356. }
  10357. this.toReferencedList(node.expressions);
  10358. return this.finishNode(node, "SequenceExpression");
  10359. }
  10360. return expr;
  10361. }
  10362. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10363. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10364. }
  10365. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10366. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10367. }
  10368. setOptionalParametersError(refExpressionErrors, resultError) {
  10369. var _resultError$loc;
  10370. refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
  10371. }
  10372. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10373. const startPos = this.state.start;
  10374. const startLoc = this.state.startLoc;
  10375. if (this.isContextual(105)) {
  10376. if (this.prodParam.hasYield) {
  10377. let left = this.parseYield();
  10378. if (afterLeftParse) {
  10379. left = afterLeftParse.call(this, left, startPos, startLoc);
  10380. }
  10381. return left;
  10382. }
  10383. }
  10384. let ownExpressionErrors;
  10385. if (refExpressionErrors) {
  10386. ownExpressionErrors = false;
  10387. } else {
  10388. refExpressionErrors = new ExpressionErrors();
  10389. ownExpressionErrors = true;
  10390. }
  10391. const {
  10392. type
  10393. } = this.state;
  10394. if (type === 10 || tokenIsIdentifier(type)) {
  10395. this.state.potentialArrowAt = this.state.start;
  10396. }
  10397. let left = this.parseMaybeConditional(refExpressionErrors);
  10398. if (afterLeftParse) {
  10399. left = afterLeftParse.call(this, left, startPos, startLoc);
  10400. }
  10401. if (tokenIsAssignment(this.state.type)) {
  10402. const node = this.startNodeAt(startPos, startLoc);
  10403. const operator = this.state.value;
  10404. node.operator = operator;
  10405. if (this.match(29)) {
  10406. this.toAssignable(left, true);
  10407. node.left = left;
  10408. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startPos) {
  10409. refExpressionErrors.doubleProtoLoc = null;
  10410. }
  10411. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startPos) {
  10412. refExpressionErrors.shorthandAssignLoc = null;
  10413. }
  10414. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startPos) {
  10415. this.checkDestructuringPrivate(refExpressionErrors);
  10416. refExpressionErrors.privateKeyLoc = null;
  10417. }
  10418. } else {
  10419. node.left = left;
  10420. }
  10421. this.next();
  10422. node.right = this.parseMaybeAssign();
  10423. this.checkLVal(left, {
  10424. in: this.finishNode(node, "AssignmentExpression")
  10425. });
  10426. return node;
  10427. } else if (ownExpressionErrors) {
  10428. this.checkExpressionErrors(refExpressionErrors, true);
  10429. }
  10430. return left;
  10431. }
  10432. parseMaybeConditional(refExpressionErrors) {
  10433. const startPos = this.state.start;
  10434. const startLoc = this.state.startLoc;
  10435. const potentialArrowAt = this.state.potentialArrowAt;
  10436. const expr = this.parseExprOps(refExpressionErrors);
  10437. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10438. return expr;
  10439. }
  10440. return this.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  10441. }
  10442. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  10443. if (this.eat(17)) {
  10444. const node = this.startNodeAt(startPos, startLoc);
  10445. node.test = expr;
  10446. node.consequent = this.parseMaybeAssignAllowIn();
  10447. this.expect(14);
  10448. node.alternate = this.parseMaybeAssign();
  10449. return this.finishNode(node, "ConditionalExpression");
  10450. }
  10451. return expr;
  10452. }
  10453. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10454. return this.match(134) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10455. }
  10456. parseExprOps(refExpressionErrors) {
  10457. const startPos = this.state.start;
  10458. const startLoc = this.state.startLoc;
  10459. const potentialArrowAt = this.state.potentialArrowAt;
  10460. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10461. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10462. return expr;
  10463. }
  10464. return this.parseExprOp(expr, startPos, startLoc, -1);
  10465. }
  10466. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  10467. if (this.isPrivateName(left)) {
  10468. const value = this.getPrivateNameSV(left);
  10469. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10470. this.raise(Errors.PrivateInExpectedIn, {
  10471. at: left,
  10472. identifierName: value
  10473. });
  10474. }
  10475. this.classScope.usePrivateName(value, left.loc.start);
  10476. }
  10477. const op = this.state.type;
  10478. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10479. let prec = tokenOperatorPrecedence(op);
  10480. if (prec > minPrec) {
  10481. if (op === 39) {
  10482. this.expectPlugin("pipelineOperator");
  10483. if (this.state.inFSharpPipelineDirectBody) {
  10484. return left;
  10485. }
  10486. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10487. }
  10488. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  10489. node.left = left;
  10490. node.operator = this.state.value;
  10491. const logical = op === 41 || op === 42;
  10492. const coalesce = op === 40;
  10493. if (coalesce) {
  10494. prec = tokenOperatorPrecedence(42);
  10495. }
  10496. this.next();
  10497. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10498. proposal: "minimal"
  10499. }])) {
  10500. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10501. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, {
  10502. at: this.state.startLoc
  10503. });
  10504. }
  10505. }
  10506. node.right = this.parseExprOpRightExpr(op, prec);
  10507. const finishedNode = this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10508. const nextOp = this.state.type;
  10509. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10510. throw this.raise(Errors.MixingCoalesceWithLogical, {
  10511. at: this.state.startLoc
  10512. });
  10513. }
  10514. return this.parseExprOp(finishedNode, leftStartPos, leftStartLoc, minPrec);
  10515. }
  10516. }
  10517. return left;
  10518. }
  10519. parseExprOpRightExpr(op, prec) {
  10520. const startPos = this.state.start;
  10521. const startLoc = this.state.startLoc;
  10522. switch (op) {
  10523. case 39:
  10524. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10525. case "hack":
  10526. return this.withTopicBindingContext(() => {
  10527. return this.parseHackPipeBody();
  10528. });
  10529. case "smart":
  10530. return this.withTopicBindingContext(() => {
  10531. if (this.prodParam.hasYield && this.isContextual(105)) {
  10532. throw this.raise(Errors.PipeBodyIsTighter, {
  10533. at: this.state.startLoc
  10534. });
  10535. }
  10536. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startPos, startLoc);
  10537. });
  10538. case "fsharp":
  10539. return this.withSoloAwaitPermittingContext(() => {
  10540. return this.parseFSharpPipelineBody(prec);
  10541. });
  10542. }
  10543. default:
  10544. return this.parseExprOpBaseRightExpr(op, prec);
  10545. }
  10546. }
  10547. parseExprOpBaseRightExpr(op, prec) {
  10548. const startPos = this.state.start;
  10549. const startLoc = this.state.startLoc;
  10550. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  10551. }
  10552. parseHackPipeBody() {
  10553. var _body$extra;
  10554. const {
  10555. startLoc
  10556. } = this.state;
  10557. const body = this.parseMaybeAssign();
  10558. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  10559. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  10560. this.raise(Errors.PipeUnparenthesizedBody, {
  10561. at: startLoc,
  10562. type: body.type
  10563. });
  10564. }
  10565. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10566. this.raise(Errors.PipeTopicUnused, {
  10567. at: startLoc
  10568. });
  10569. }
  10570. return body;
  10571. }
  10572. checkExponentialAfterUnary(node) {
  10573. if (this.match(57)) {
  10574. this.raise(Errors.UnexpectedTokenUnaryExponentiation, {
  10575. at: node.argument
  10576. });
  10577. }
  10578. }
  10579. parseMaybeUnary(refExpressionErrors, sawUnary) {
  10580. const startPos = this.state.start;
  10581. const startLoc = this.state.startLoc;
  10582. const isAwait = this.isContextual(96);
  10583. if (isAwait && this.isAwaitAllowed()) {
  10584. this.next();
  10585. const expr = this.parseAwait(startPos, startLoc);
  10586. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  10587. return expr;
  10588. }
  10589. const update = this.match(34);
  10590. const node = this.startNode();
  10591. if (tokenIsPrefix(this.state.type)) {
  10592. node.operator = this.state.value;
  10593. node.prefix = true;
  10594. if (this.match(72)) {
  10595. this.expectPlugin("throwExpressions");
  10596. }
  10597. const isDelete = this.match(89);
  10598. this.next();
  10599. node.argument = this.parseMaybeUnary(null, true);
  10600. this.checkExpressionErrors(refExpressionErrors, true);
  10601. if (this.state.strict && isDelete) {
  10602. const arg = node.argument;
  10603. if (arg.type === "Identifier") {
  10604. this.raise(Errors.StrictDelete, {
  10605. at: node
  10606. });
  10607. } else if (this.hasPropertyAsPrivateName(arg)) {
  10608. this.raise(Errors.DeletePrivateField, {
  10609. at: node
  10610. });
  10611. }
  10612. }
  10613. if (!update) {
  10614. if (!sawUnary) {
  10615. this.checkExponentialAfterUnary(node);
  10616. }
  10617. return this.finishNode(node, "UnaryExpression");
  10618. }
  10619. }
  10620. const expr = this.parseUpdate(node, update, refExpressionErrors);
  10621. if (isAwait) {
  10622. const {
  10623. type
  10624. } = this.state;
  10625. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10626. if (startsExpr && !this.isAmbiguousAwait()) {
  10627. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, {
  10628. at: startLoc
  10629. });
  10630. return this.parseAwait(startPos, startLoc);
  10631. }
  10632. }
  10633. return expr;
  10634. }
  10635. parseUpdate(node, update, refExpressionErrors) {
  10636. if (update) {
  10637. const updateExpressionNode = node;
  10638. this.checkLVal(updateExpressionNode.argument, {
  10639. in: this.finishNode(updateExpressionNode, "UpdateExpression")
  10640. });
  10641. return node;
  10642. }
  10643. const startPos = this.state.start;
  10644. const startLoc = this.state.startLoc;
  10645. let expr = this.parseExprSubscripts(refExpressionErrors);
  10646. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  10647. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  10648. const node = this.startNodeAt(startPos, startLoc);
  10649. node.operator = this.state.value;
  10650. node.prefix = false;
  10651. node.argument = expr;
  10652. this.next();
  10653. this.checkLVal(expr, {
  10654. in: expr = this.finishNode(node, "UpdateExpression")
  10655. });
  10656. }
  10657. return expr;
  10658. }
  10659. parseExprSubscripts(refExpressionErrors) {
  10660. const startPos = this.state.start;
  10661. const startLoc = this.state.startLoc;
  10662. const potentialArrowAt = this.state.potentialArrowAt;
  10663. const expr = this.parseExprAtom(refExpressionErrors);
  10664. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10665. return expr;
  10666. }
  10667. return this.parseSubscripts(expr, startPos, startLoc);
  10668. }
  10669. parseSubscripts(base, startPos, startLoc, noCalls) {
  10670. const state = {
  10671. optionalChainMember: false,
  10672. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  10673. stop: false
  10674. };
  10675. do {
  10676. base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
  10677. state.maybeAsyncArrow = false;
  10678. } while (!state.stop);
  10679. return base;
  10680. }
  10681. parseSubscript(base, startPos, startLoc, noCalls, state) {
  10682. const {
  10683. type
  10684. } = this.state;
  10685. if (!noCalls && type === 15) {
  10686. return this.parseBind(base, startPos, startLoc, noCalls, state);
  10687. } else if (tokenIsTemplate(type)) {
  10688. return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  10689. }
  10690. let optional = false;
  10691. if (type === 18) {
  10692. if (noCalls && this.lookaheadCharCode() === 40) {
  10693. state.stop = true;
  10694. return base;
  10695. }
  10696. state.optionalChainMember = optional = true;
  10697. this.next();
  10698. }
  10699. if (!noCalls && this.match(10)) {
  10700. return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
  10701. } else {
  10702. const computed = this.eat(0);
  10703. if (computed || optional || this.eat(16)) {
  10704. return this.parseMember(base, startPos, startLoc, state, computed, optional);
  10705. } else {
  10706. state.stop = true;
  10707. return base;
  10708. }
  10709. }
  10710. }
  10711. parseMember(base, startPos, startLoc, state, computed, optional) {
  10712. const node = this.startNodeAt(startPos, startLoc);
  10713. node.object = base;
  10714. node.computed = computed;
  10715. if (computed) {
  10716. node.property = this.parseExpression();
  10717. this.expect(3);
  10718. } else if (this.match(134)) {
  10719. if (base.type === "Super") {
  10720. this.raise(Errors.SuperPrivateField, {
  10721. at: startLoc
  10722. });
  10723. }
  10724. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  10725. node.property = this.parsePrivateName();
  10726. } else {
  10727. node.property = this.parseIdentifier(true);
  10728. }
  10729. if (state.optionalChainMember) {
  10730. node.optional = optional;
  10731. return this.finishNode(node, "OptionalMemberExpression");
  10732. } else {
  10733. return this.finishNode(node, "MemberExpression");
  10734. }
  10735. }
  10736. parseBind(base, startPos, startLoc, noCalls, state) {
  10737. const node = this.startNodeAt(startPos, startLoc);
  10738. node.object = base;
  10739. this.next();
  10740. node.callee = this.parseNoCallExpr();
  10741. state.stop = true;
  10742. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  10743. }
  10744. parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
  10745. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10746. let refExpressionErrors = null;
  10747. this.state.maybeInArrowParameters = true;
  10748. this.next();
  10749. const node = this.startNodeAt(startPos, startLoc);
  10750. node.callee = base;
  10751. const {
  10752. maybeAsyncArrow,
  10753. optionalChainMember
  10754. } = state;
  10755. if (maybeAsyncArrow) {
  10756. this.expressionScope.enter(newAsyncArrowScope());
  10757. refExpressionErrors = new ExpressionErrors();
  10758. }
  10759. if (optionalChainMember) {
  10760. node.optional = optional;
  10761. }
  10762. if (optional) {
  10763. node.arguments = this.parseCallExpressionArguments(11);
  10764. } else {
  10765. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  10766. }
  10767. let finishedNode = this.finishCallExpression(node, optionalChainMember);
  10768. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  10769. state.stop = true;
  10770. this.checkDestructuringPrivate(refExpressionErrors);
  10771. this.expressionScope.validateAsPattern();
  10772. this.expressionScope.exit();
  10773. finishedNode = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), finishedNode);
  10774. } else {
  10775. if (maybeAsyncArrow) {
  10776. this.checkExpressionErrors(refExpressionErrors, true);
  10777. this.expressionScope.exit();
  10778. }
  10779. this.toReferencedArguments(finishedNode);
  10780. }
  10781. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10782. return finishedNode;
  10783. }
  10784. toReferencedArguments(node, isParenthesizedExpr) {
  10785. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  10786. }
  10787. parseTaggedTemplateExpression(base, startPos, startLoc, state) {
  10788. const node = this.startNodeAt(startPos, startLoc);
  10789. node.tag = base;
  10790. node.quasi = this.parseTemplate(true);
  10791. if (state.optionalChainMember) {
  10792. this.raise(Errors.OptionalChainingNoTemplate, {
  10793. at: startLoc
  10794. });
  10795. }
  10796. return this.finishNode(node, "TaggedTemplateExpression");
  10797. }
  10798. atPossibleAsyncArrow(base) {
  10799. 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;
  10800. }
  10801. finishCallExpression(node, optional) {
  10802. if (node.callee.type === "Import") {
  10803. if (node.arguments.length === 2) {
  10804. {
  10805. if (!this.hasPlugin("moduleAttributes")) {
  10806. this.expectPlugin("importAssertions");
  10807. }
  10808. }
  10809. }
  10810. if (node.arguments.length === 0 || node.arguments.length > 2) {
  10811. this.raise(Errors.ImportCallArity, {
  10812. at: node,
  10813. maxArgumentCount: this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
  10814. });
  10815. } else {
  10816. for (const arg of node.arguments) {
  10817. if (arg.type === "SpreadElement") {
  10818. this.raise(Errors.ImportCallSpreadArgument, {
  10819. at: arg
  10820. });
  10821. }
  10822. }
  10823. }
  10824. }
  10825. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  10826. }
  10827. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  10828. const elts = [];
  10829. let first = true;
  10830. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10831. this.state.inFSharpPipelineDirectBody = false;
  10832. while (!this.eat(close)) {
  10833. if (first) {
  10834. first = false;
  10835. } else {
  10836. this.expect(12);
  10837. if (this.match(close)) {
  10838. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  10839. this.raise(Errors.ImportCallArgumentTrailingComma, {
  10840. at: this.state.lastTokStartLoc
  10841. });
  10842. }
  10843. if (nodeForExtra) {
  10844. this.addTrailingCommaExtraToNode(nodeForExtra);
  10845. }
  10846. this.next();
  10847. break;
  10848. }
  10849. }
  10850. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  10851. }
  10852. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10853. return elts;
  10854. }
  10855. shouldParseAsyncArrow() {
  10856. return this.match(19) && !this.canInsertSemicolon();
  10857. }
  10858. parseAsyncArrowFromCallExpression(node, call) {
  10859. var _call$extra;
  10860. this.resetPreviousNodeTrailingComments(call);
  10861. this.expect(19);
  10862. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  10863. if (call.innerComments) {
  10864. setInnerComments(node, call.innerComments);
  10865. }
  10866. if (call.callee.trailingComments) {
  10867. setInnerComments(node, call.callee.trailingComments);
  10868. }
  10869. return node;
  10870. }
  10871. parseNoCallExpr() {
  10872. const startPos = this.state.start;
  10873. const startLoc = this.state.startLoc;
  10874. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  10875. }
  10876. parseExprAtom(refExpressionErrors) {
  10877. let node;
  10878. const {
  10879. type
  10880. } = this.state;
  10881. switch (type) {
  10882. case 79:
  10883. return this.parseSuper();
  10884. case 83:
  10885. node = this.startNode();
  10886. this.next();
  10887. if (this.match(16)) {
  10888. return this.parseImportMetaProperty(node);
  10889. }
  10890. if (!this.match(10)) {
  10891. this.raise(Errors.UnsupportedImport, {
  10892. at: this.state.lastTokStartLoc
  10893. });
  10894. }
  10895. return this.finishNode(node, "Import");
  10896. case 78:
  10897. node = this.startNode();
  10898. this.next();
  10899. return this.finishNode(node, "ThisExpression");
  10900. case 90:
  10901. {
  10902. return this.parseDo(this.startNode(), false);
  10903. }
  10904. case 56:
  10905. case 31:
  10906. {
  10907. this.readRegexp();
  10908. return this.parseRegExpLiteral(this.state.value);
  10909. }
  10910. case 130:
  10911. return this.parseNumericLiteral(this.state.value);
  10912. case 131:
  10913. return this.parseBigIntLiteral(this.state.value);
  10914. case 132:
  10915. return this.parseDecimalLiteral(this.state.value);
  10916. case 129:
  10917. return this.parseStringLiteral(this.state.value);
  10918. case 84:
  10919. return this.parseNullLiteral();
  10920. case 85:
  10921. return this.parseBooleanLiteral(true);
  10922. case 86:
  10923. return this.parseBooleanLiteral(false);
  10924. case 10:
  10925. {
  10926. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  10927. return this.parseParenAndDistinguishExpression(canBeArrow);
  10928. }
  10929. case 2:
  10930. case 1:
  10931. {
  10932. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  10933. }
  10934. case 0:
  10935. {
  10936. return this.parseArrayLike(3, true, false, refExpressionErrors);
  10937. }
  10938. case 6:
  10939. case 7:
  10940. {
  10941. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  10942. }
  10943. case 5:
  10944. {
  10945. return this.parseObjectLike(8, false, false, refExpressionErrors);
  10946. }
  10947. case 68:
  10948. return this.parseFunctionOrFunctionSent();
  10949. case 26:
  10950. this.parseDecorators();
  10951. case 80:
  10952. node = this.startNode();
  10953. this.takeDecorators(node);
  10954. return this.parseClass(node, false);
  10955. case 77:
  10956. return this.parseNewOrNewTarget();
  10957. case 25:
  10958. case 24:
  10959. return this.parseTemplate(false);
  10960. case 15:
  10961. {
  10962. node = this.startNode();
  10963. this.next();
  10964. node.object = null;
  10965. const callee = node.callee = this.parseNoCallExpr();
  10966. if (callee.type === "MemberExpression") {
  10967. return this.finishNode(node, "BindExpression");
  10968. } else {
  10969. throw this.raise(Errors.UnsupportedBind, {
  10970. at: callee
  10971. });
  10972. }
  10973. }
  10974. case 134:
  10975. {
  10976. this.raise(Errors.PrivateInExpectedIn, {
  10977. at: this.state.startLoc,
  10978. identifierName: this.state.value
  10979. });
  10980. return this.parsePrivateName();
  10981. }
  10982. case 33:
  10983. {
  10984. return this.parseTopicReferenceThenEqualsSign(54, "%");
  10985. }
  10986. case 32:
  10987. {
  10988. return this.parseTopicReferenceThenEqualsSign(44, "^");
  10989. }
  10990. case 37:
  10991. case 38:
  10992. {
  10993. return this.parseTopicReference("hack");
  10994. }
  10995. case 44:
  10996. case 54:
  10997. case 27:
  10998. {
  10999. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11000. if (pipeProposal) {
  11001. return this.parseTopicReference(pipeProposal);
  11002. } else {
  11003. throw this.unexpected();
  11004. }
  11005. }
  11006. case 47:
  11007. {
  11008. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  11009. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  11010. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  11011. break;
  11012. } else {
  11013. throw this.unexpected();
  11014. }
  11015. }
  11016. default:
  11017. if (tokenIsIdentifier(type)) {
  11018. if (this.isContextual(123) && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  11019. return this.parseModuleExpression();
  11020. }
  11021. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11022. const containsEsc = this.state.containsEsc;
  11023. const id = this.parseIdentifier();
  11024. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  11025. const {
  11026. type
  11027. } = this.state;
  11028. if (type === 68) {
  11029. this.resetPreviousNodeTrailingComments(id);
  11030. this.next();
  11031. return this.parseFunction(this.startNodeAtNode(id), undefined, true);
  11032. } else if (tokenIsIdentifier(type)) {
  11033. if (this.lookaheadCharCode() === 61) {
  11034. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  11035. } else {
  11036. return id;
  11037. }
  11038. } else if (type === 90) {
  11039. this.resetPreviousNodeTrailingComments(id);
  11040. return this.parseDo(this.startNodeAtNode(id), true);
  11041. }
  11042. }
  11043. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  11044. this.next();
  11045. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  11046. }
  11047. return id;
  11048. } else {
  11049. throw this.unexpected();
  11050. }
  11051. }
  11052. }
  11053. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  11054. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11055. if (pipeProposal) {
  11056. this.state.type = topicTokenType;
  11057. this.state.value = topicTokenValue;
  11058. this.state.pos--;
  11059. this.state.end--;
  11060. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  11061. return this.parseTopicReference(pipeProposal);
  11062. } else {
  11063. throw this.unexpected();
  11064. }
  11065. }
  11066. parseTopicReference(pipeProposal) {
  11067. const node = this.startNode();
  11068. const startLoc = this.state.startLoc;
  11069. const tokenType = this.state.type;
  11070. this.next();
  11071. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  11072. }
  11073. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  11074. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  11075. const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
  11076. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  11077. this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, {
  11078. at: startLoc
  11079. });
  11080. }
  11081. this.registerTopicReference();
  11082. return this.finishNode(node, nodeType);
  11083. } else {
  11084. throw this.raise(Errors.PipeTopicUnconfiguredToken, {
  11085. at: startLoc,
  11086. token: tokenLabelName(tokenType)
  11087. });
  11088. }
  11089. }
  11090. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  11091. switch (pipeProposal) {
  11092. case "hack":
  11093. {
  11094. return this.hasPlugin(["pipelineOperator", {
  11095. topicToken: tokenLabelName(tokenType)
  11096. }]);
  11097. }
  11098. case "smart":
  11099. return tokenType === 27;
  11100. default:
  11101. throw this.raise(Errors.PipeTopicRequiresHackPipes, {
  11102. at: startLoc
  11103. });
  11104. }
  11105. }
  11106. parseAsyncArrowUnaryFunction(node) {
  11107. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  11108. const params = [this.parseIdentifier()];
  11109. this.prodParam.exit();
  11110. if (this.hasPrecedingLineBreak()) {
  11111. this.raise(Errors.LineTerminatorBeforeArrow, {
  11112. at: this.state.curPosition()
  11113. });
  11114. }
  11115. this.expect(19);
  11116. return this.parseArrowExpression(node, params, true);
  11117. }
  11118. parseDo(node, isAsync) {
  11119. this.expectPlugin("doExpressions");
  11120. if (isAsync) {
  11121. this.expectPlugin("asyncDoExpressions");
  11122. }
  11123. node.async = isAsync;
  11124. this.next();
  11125. const oldLabels = this.state.labels;
  11126. this.state.labels = [];
  11127. if (isAsync) {
  11128. this.prodParam.enter(PARAM_AWAIT);
  11129. node.body = this.parseBlock();
  11130. this.prodParam.exit();
  11131. } else {
  11132. node.body = this.parseBlock();
  11133. }
  11134. this.state.labels = oldLabels;
  11135. return this.finishNode(node, "DoExpression");
  11136. }
  11137. parseSuper() {
  11138. const node = this.startNode();
  11139. this.next();
  11140. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  11141. this.raise(Errors.SuperNotAllowed, {
  11142. at: node
  11143. });
  11144. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  11145. this.raise(Errors.UnexpectedSuper, {
  11146. at: node
  11147. });
  11148. }
  11149. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  11150. this.raise(Errors.UnsupportedSuper, {
  11151. at: node
  11152. });
  11153. }
  11154. return this.finishNode(node, "Super");
  11155. }
  11156. parsePrivateName() {
  11157. const node = this.startNode();
  11158. const id = this.startNodeAt(this.state.start + 1, new Position(this.state.curLine, this.state.start + 1 - this.state.lineStart, this.state.start + 1));
  11159. const name = this.state.value;
  11160. this.next();
  11161. node.id = this.createIdentifier(id, name);
  11162. return this.finishNode(node, "PrivateName");
  11163. }
  11164. parseFunctionOrFunctionSent() {
  11165. const node = this.startNode();
  11166. this.next();
  11167. if (this.prodParam.hasYield && this.match(16)) {
  11168. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  11169. this.next();
  11170. if (this.match(102)) {
  11171. this.expectPlugin("functionSent");
  11172. } else if (!this.hasPlugin("functionSent")) {
  11173. this.unexpected();
  11174. }
  11175. return this.parseMetaProperty(node, meta, "sent");
  11176. }
  11177. return this.parseFunction(node);
  11178. }
  11179. parseMetaProperty(node, meta, propertyName) {
  11180. node.meta = meta;
  11181. const containsEsc = this.state.containsEsc;
  11182. node.property = this.parseIdentifier(true);
  11183. if (node.property.name !== propertyName || containsEsc) {
  11184. this.raise(Errors.UnsupportedMetaProperty, {
  11185. at: node.property,
  11186. target: meta.name,
  11187. onlyValidPropertyName: propertyName
  11188. });
  11189. }
  11190. return this.finishNode(node, "MetaProperty");
  11191. }
  11192. parseImportMetaProperty(node) {
  11193. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  11194. this.next();
  11195. if (this.isContextual(100)) {
  11196. if (!this.inModule) {
  11197. this.raise(Errors.ImportMetaOutsideModule, {
  11198. at: id
  11199. });
  11200. }
  11201. this.sawUnambiguousESM = true;
  11202. }
  11203. return this.parseMetaProperty(node, id, "meta");
  11204. }
  11205. parseLiteralAtNode(value, type, node) {
  11206. this.addExtra(node, "rawValue", value);
  11207. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  11208. node.value = value;
  11209. this.next();
  11210. return this.finishNode(node, type);
  11211. }
  11212. parseLiteral(value, type) {
  11213. const node = this.startNode();
  11214. return this.parseLiteralAtNode(value, type, node);
  11215. }
  11216. parseStringLiteral(value) {
  11217. return this.parseLiteral(value, "StringLiteral");
  11218. }
  11219. parseNumericLiteral(value) {
  11220. return this.parseLiteral(value, "NumericLiteral");
  11221. }
  11222. parseBigIntLiteral(value) {
  11223. return this.parseLiteral(value, "BigIntLiteral");
  11224. }
  11225. parseDecimalLiteral(value) {
  11226. return this.parseLiteral(value, "DecimalLiteral");
  11227. }
  11228. parseRegExpLiteral(value) {
  11229. const node = this.parseLiteral(value.value, "RegExpLiteral");
  11230. node.pattern = value.pattern;
  11231. node.flags = value.flags;
  11232. return node;
  11233. }
  11234. parseBooleanLiteral(value) {
  11235. const node = this.startNode();
  11236. node.value = value;
  11237. this.next();
  11238. return this.finishNode(node, "BooleanLiteral");
  11239. }
  11240. parseNullLiteral() {
  11241. const node = this.startNode();
  11242. this.next();
  11243. return this.finishNode(node, "NullLiteral");
  11244. }
  11245. parseParenAndDistinguishExpression(canBeArrow) {
  11246. const startPos = this.state.start;
  11247. const startLoc = this.state.startLoc;
  11248. let val;
  11249. this.next();
  11250. this.expressionScope.enter(newArrowHeadScope());
  11251. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11252. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11253. this.state.maybeInArrowParameters = true;
  11254. this.state.inFSharpPipelineDirectBody = false;
  11255. const innerStartPos = this.state.start;
  11256. const innerStartLoc = this.state.startLoc;
  11257. const exprList = [];
  11258. const refExpressionErrors = new ExpressionErrors();
  11259. let first = true;
  11260. let spreadStartLoc;
  11261. let optionalCommaStartLoc;
  11262. while (!this.match(11)) {
  11263. if (first) {
  11264. first = false;
  11265. } else {
  11266. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11267. if (this.match(11)) {
  11268. optionalCommaStartLoc = this.state.startLoc;
  11269. break;
  11270. }
  11271. }
  11272. if (this.match(21)) {
  11273. const spreadNodeStartPos = this.state.start;
  11274. const spreadNodeStartLoc = this.state.startLoc;
  11275. spreadStartLoc = this.state.startLoc;
  11276. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  11277. if (!this.checkCommaAfterRest(41)) {
  11278. break;
  11279. }
  11280. } else {
  11281. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  11282. }
  11283. }
  11284. const innerEndLoc = this.state.lastTokEndLoc;
  11285. this.expect(11);
  11286. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11287. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11288. let arrowNode = this.startNodeAt(startPos, startLoc);
  11289. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11290. this.checkDestructuringPrivate(refExpressionErrors);
  11291. this.expressionScope.validateAsPattern();
  11292. this.expressionScope.exit();
  11293. this.parseArrowExpression(arrowNode, exprList, false);
  11294. return arrowNode;
  11295. }
  11296. this.expressionScope.exit();
  11297. if (!exprList.length) {
  11298. this.unexpected(this.state.lastTokStartLoc);
  11299. }
  11300. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11301. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11302. this.checkExpressionErrors(refExpressionErrors, true);
  11303. this.toReferencedListDeep(exprList, true);
  11304. if (exprList.length > 1) {
  11305. val = this.startNodeAt(innerStartPos, innerStartLoc);
  11306. val.expressions = exprList;
  11307. this.finishNode(val, "SequenceExpression");
  11308. this.resetEndLocation(val, innerEndLoc);
  11309. } else {
  11310. val = exprList[0];
  11311. }
  11312. return this.wrapParenthesis(startPos, startLoc, val);
  11313. }
  11314. wrapParenthesis(startPos, startLoc, expression) {
  11315. if (!this.options.createParenthesizedExpressions) {
  11316. this.addExtra(expression, "parenthesized", true);
  11317. this.addExtra(expression, "parenStart", startPos);
  11318. this.takeSurroundingComments(expression, startPos, this.state.lastTokEndLoc.index);
  11319. return expression;
  11320. }
  11321. const parenExpression = this.startNodeAt(startPos, startLoc);
  11322. parenExpression.expression = expression;
  11323. return this.finishNode(parenExpression, "ParenthesizedExpression");
  11324. }
  11325. shouldParseArrow(params) {
  11326. return !this.canInsertSemicolon();
  11327. }
  11328. parseArrow(node) {
  11329. if (this.eat(19)) {
  11330. return node;
  11331. }
  11332. }
  11333. parseParenItem(node, startPos, startLoc) {
  11334. return node;
  11335. }
  11336. parseNewOrNewTarget() {
  11337. const node = this.startNode();
  11338. this.next();
  11339. if (this.match(16)) {
  11340. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11341. this.next();
  11342. const metaProp = this.parseMetaProperty(node, meta, "target");
  11343. if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
  11344. this.raise(Errors.UnexpectedNewTarget, {
  11345. at: metaProp
  11346. });
  11347. }
  11348. return metaProp;
  11349. }
  11350. return this.parseNew(node);
  11351. }
  11352. parseNew(node) {
  11353. this.parseNewCallee(node);
  11354. if (this.eat(10)) {
  11355. const args = this.parseExprList(11);
  11356. this.toReferencedList(args);
  11357. node.arguments = args;
  11358. } else {
  11359. node.arguments = [];
  11360. }
  11361. return this.finishNode(node, "NewExpression");
  11362. }
  11363. parseNewCallee(node) {
  11364. node.callee = this.parseNoCallExpr();
  11365. if (node.callee.type === "Import") {
  11366. this.raise(Errors.ImportCallNotNewExpression, {
  11367. at: node.callee
  11368. });
  11369. } else if (this.isOptionalChain(node.callee)) {
  11370. this.raise(Errors.OptionalChainingNoNew, {
  11371. at: this.state.lastTokEndLoc
  11372. });
  11373. } else if (this.eat(18)) {
  11374. this.raise(Errors.OptionalChainingNoNew, {
  11375. at: this.state.startLoc
  11376. });
  11377. }
  11378. }
  11379. parseTemplateElement(isTagged) {
  11380. const {
  11381. start,
  11382. startLoc,
  11383. end,
  11384. value
  11385. } = this.state;
  11386. const elemStart = start + 1;
  11387. const elem = this.startNodeAt(elemStart, createPositionWithColumnOffset(startLoc, 1));
  11388. if (value === null) {
  11389. if (!isTagged) {
  11390. this.raise(Errors.InvalidEscapeSequenceTemplate, {
  11391. at: createPositionWithColumnOffset(startLoc, 2)
  11392. });
  11393. }
  11394. }
  11395. const isTail = this.match(24);
  11396. const endOffset = isTail ? -1 : -2;
  11397. const elemEnd = end + endOffset;
  11398. elem.value = {
  11399. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11400. cooked: value === null ? null : value.slice(1, endOffset)
  11401. };
  11402. elem.tail = isTail;
  11403. this.next();
  11404. const finishedNode = this.finishNode(elem, "TemplateElement");
  11405. this.resetEndLocation(finishedNode, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11406. return finishedNode;
  11407. }
  11408. parseTemplate(isTagged) {
  11409. const node = this.startNode();
  11410. node.expressions = [];
  11411. let curElt = this.parseTemplateElement(isTagged);
  11412. node.quasis = [curElt];
  11413. while (!curElt.tail) {
  11414. node.expressions.push(this.parseTemplateSubstitution());
  11415. this.readTemplateContinuation();
  11416. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  11417. }
  11418. return this.finishNode(node, "TemplateLiteral");
  11419. }
  11420. parseTemplateSubstitution() {
  11421. return this.parseExpression();
  11422. }
  11423. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11424. if (isRecord) {
  11425. this.expectPlugin("recordAndTuple");
  11426. }
  11427. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11428. this.state.inFSharpPipelineDirectBody = false;
  11429. const propHash = Object.create(null);
  11430. let first = true;
  11431. const node = this.startNode();
  11432. node.properties = [];
  11433. this.next();
  11434. while (!this.match(close)) {
  11435. if (first) {
  11436. first = false;
  11437. } else {
  11438. this.expect(12);
  11439. if (this.match(close)) {
  11440. this.addTrailingCommaExtraToNode(node);
  11441. break;
  11442. }
  11443. }
  11444. let prop;
  11445. if (isPattern) {
  11446. prop = this.parseBindingProperty();
  11447. } else {
  11448. prop = this.parsePropertyDefinition(refExpressionErrors);
  11449. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  11450. }
  11451. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11452. this.raise(Errors.InvalidRecordProperty, {
  11453. at: prop
  11454. });
  11455. }
  11456. if (prop.shorthand) {
  11457. this.addExtra(prop, "shorthand", true);
  11458. }
  11459. node.properties.push(prop);
  11460. }
  11461. this.next();
  11462. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11463. let type = "ObjectExpression";
  11464. if (isPattern) {
  11465. type = "ObjectPattern";
  11466. } else if (isRecord) {
  11467. type = "RecordExpression";
  11468. }
  11469. return this.finishNode(node, type);
  11470. }
  11471. addTrailingCommaExtraToNode(node) {
  11472. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  11473. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11474. }
  11475. maybeAsyncOrAccessorProp(prop) {
  11476. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11477. }
  11478. parsePropertyDefinition(refExpressionErrors) {
  11479. let decorators = [];
  11480. if (this.match(26)) {
  11481. if (this.hasPlugin("decorators")) {
  11482. this.raise(Errors.UnsupportedPropertyDecorator, {
  11483. at: this.state.startLoc
  11484. });
  11485. }
  11486. while (this.match(26)) {
  11487. decorators.push(this.parseDecorator());
  11488. }
  11489. }
  11490. const prop = this.startNode();
  11491. let isAsync = false;
  11492. let isAccessor = false;
  11493. let startPos;
  11494. let startLoc;
  11495. if (this.match(21)) {
  11496. if (decorators.length) this.unexpected();
  11497. return this.parseSpread();
  11498. }
  11499. if (decorators.length) {
  11500. prop.decorators = decorators;
  11501. decorators = [];
  11502. }
  11503. prop.method = false;
  11504. if (refExpressionErrors) {
  11505. startPos = this.state.start;
  11506. startLoc = this.state.startLoc;
  11507. }
  11508. let isGenerator = this.eat(55);
  11509. this.parsePropertyNamePrefixOperator(prop);
  11510. const containsEsc = this.state.containsEsc;
  11511. const key = this.parsePropertyName(prop, refExpressionErrors);
  11512. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11513. const keyName = key.name;
  11514. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11515. isAsync = true;
  11516. this.resetPreviousNodeTrailingComments(key);
  11517. isGenerator = this.eat(55);
  11518. this.parsePropertyName(prop);
  11519. }
  11520. if (keyName === "get" || keyName === "set") {
  11521. isAccessor = true;
  11522. this.resetPreviousNodeTrailingComments(key);
  11523. prop.kind = keyName;
  11524. if (this.match(55)) {
  11525. isGenerator = true;
  11526. this.raise(Errors.AccessorIsGenerator, {
  11527. at: this.state.curPosition(),
  11528. kind: keyName
  11529. });
  11530. this.next();
  11531. }
  11532. this.parsePropertyName(prop);
  11533. }
  11534. }
  11535. return this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11536. }
  11537. getGetterSetterExpectedParamCount(method) {
  11538. return method.kind === "get" ? 0 : 1;
  11539. }
  11540. getObjectOrClassMethodParams(method) {
  11541. return method.params;
  11542. }
  11543. checkGetterSetterParams(method) {
  11544. var _params;
  11545. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11546. const params = this.getObjectOrClassMethodParams(method);
  11547. if (params.length !== paramCount) {
  11548. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  11549. at: method
  11550. });
  11551. }
  11552. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11553. this.raise(Errors.BadSetterRestParameter, {
  11554. at: method
  11555. });
  11556. }
  11557. }
  11558. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11559. if (isAccessor) {
  11560. const finishedProp = this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11561. this.checkGetterSetterParams(finishedProp);
  11562. return finishedProp;
  11563. }
  11564. if (isAsync || isGenerator || this.match(10)) {
  11565. if (isPattern) this.unexpected();
  11566. prop.kind = "method";
  11567. prop.method = true;
  11568. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11569. }
  11570. }
  11571. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  11572. prop.shorthand = false;
  11573. if (this.eat(14)) {
  11574. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  11575. return this.finishNode(prop, "ObjectProperty");
  11576. }
  11577. if (!prop.computed && prop.key.type === "Identifier") {
  11578. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11579. if (isPattern) {
  11580. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  11581. } else if (this.match(29)) {
  11582. const shorthandAssignLoc = this.state.startLoc;
  11583. if (refExpressionErrors != null) {
  11584. if (refExpressionErrors.shorthandAssignLoc === null) {
  11585. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11586. }
  11587. } else {
  11588. this.raise(Errors.InvalidCoverInitializedName, {
  11589. at: shorthandAssignLoc
  11590. });
  11591. }
  11592. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  11593. } else {
  11594. prop.value = cloneIdentifier(prop.key);
  11595. }
  11596. prop.shorthand = true;
  11597. return this.finishNode(prop, "ObjectProperty");
  11598. }
  11599. }
  11600. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  11601. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  11602. if (!node) this.unexpected();
  11603. return node;
  11604. }
  11605. parsePropertyName(prop, refExpressionErrors) {
  11606. if (this.eat(0)) {
  11607. prop.computed = true;
  11608. prop.key = this.parseMaybeAssignAllowIn();
  11609. this.expect(3);
  11610. } else {
  11611. const {
  11612. type,
  11613. value
  11614. } = this.state;
  11615. let key;
  11616. if (tokenIsKeywordOrIdentifier(type)) {
  11617. key = this.parseIdentifier(true);
  11618. } else {
  11619. switch (type) {
  11620. case 130:
  11621. key = this.parseNumericLiteral(value);
  11622. break;
  11623. case 129:
  11624. key = this.parseStringLiteral(value);
  11625. break;
  11626. case 131:
  11627. key = this.parseBigIntLiteral(value);
  11628. break;
  11629. case 132:
  11630. key = this.parseDecimalLiteral(value);
  11631. break;
  11632. case 134:
  11633. {
  11634. const privateKeyLoc = this.state.startLoc;
  11635. if (refExpressionErrors != null) {
  11636. if (refExpressionErrors.privateKeyLoc === null) {
  11637. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  11638. }
  11639. } else {
  11640. this.raise(Errors.UnexpectedPrivateField, {
  11641. at: privateKeyLoc
  11642. });
  11643. }
  11644. key = this.parsePrivateName();
  11645. break;
  11646. }
  11647. default:
  11648. throw this.unexpected();
  11649. }
  11650. }
  11651. prop.key = key;
  11652. if (type !== 134) {
  11653. prop.computed = false;
  11654. }
  11655. }
  11656. return prop.key;
  11657. }
  11658. initFunction(node, isAsync) {
  11659. node.id = null;
  11660. node.generator = false;
  11661. node.async = !!isAsync;
  11662. }
  11663. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  11664. this.initFunction(node, isAsync);
  11665. node.generator = !!isGenerator;
  11666. const allowModifiers = isConstructor;
  11667. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  11668. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11669. this.parseFunctionParams(node, allowModifiers);
  11670. const finishedNode = this.parseFunctionBodyAndFinish(node, type, true);
  11671. this.prodParam.exit();
  11672. this.scope.exit();
  11673. return finishedNode;
  11674. }
  11675. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  11676. if (isTuple) {
  11677. this.expectPlugin("recordAndTuple");
  11678. }
  11679. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11680. this.state.inFSharpPipelineDirectBody = false;
  11681. const node = this.startNode();
  11682. this.next();
  11683. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  11684. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11685. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  11686. }
  11687. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  11688. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  11689. let flags = functionFlags(isAsync, false);
  11690. if (!this.match(5) && this.prodParam.hasIn) {
  11691. flags |= PARAM_IN;
  11692. }
  11693. this.prodParam.enter(flags);
  11694. this.initFunction(node, isAsync);
  11695. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11696. if (params) {
  11697. this.state.maybeInArrowParameters = true;
  11698. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  11699. }
  11700. this.state.maybeInArrowParameters = false;
  11701. this.parseFunctionBody(node, true);
  11702. this.prodParam.exit();
  11703. this.scope.exit();
  11704. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11705. return this.finishNode(node, "ArrowFunctionExpression");
  11706. }
  11707. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  11708. this.toAssignableList(params, trailingCommaLoc, false);
  11709. node.params = params;
  11710. }
  11711. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  11712. this.parseFunctionBody(node, false, isMethod);
  11713. return this.finishNode(node, type);
  11714. }
  11715. parseFunctionBody(node, allowExpression, isMethod = false) {
  11716. const isExpression = allowExpression && !this.match(5);
  11717. this.expressionScope.enter(newExpressionScope());
  11718. if (isExpression) {
  11719. node.body = this.parseMaybeAssign();
  11720. this.checkParams(node, false, allowExpression, false);
  11721. } else {
  11722. const oldStrict = this.state.strict;
  11723. const oldLabels = this.state.labels;
  11724. this.state.labels = [];
  11725. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  11726. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  11727. const nonSimple = !this.isSimpleParamList(node.params);
  11728. if (hasStrictModeDirective && nonSimple) {
  11729. this.raise(Errors.IllegalLanguageModeDirective, {
  11730. at: (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node
  11731. });
  11732. }
  11733. const strictModeChanged = !oldStrict && this.state.strict;
  11734. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  11735. if (this.state.strict && node.id) {
  11736. this.checkIdentifier(node.id, BIND_OUTSIDE, strictModeChanged);
  11737. }
  11738. });
  11739. this.prodParam.exit();
  11740. this.state.labels = oldLabels;
  11741. }
  11742. this.expressionScope.exit();
  11743. }
  11744. isSimpleParameter(node) {
  11745. return node.type === "Identifier";
  11746. }
  11747. isSimpleParamList(params) {
  11748. for (let i = 0, len = params.length; i < len; i++) {
  11749. if (!this.isSimpleParameter(params[i])) return false;
  11750. }
  11751. return true;
  11752. }
  11753. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  11754. const checkClashes = !allowDuplicates && new Set();
  11755. const formalParameters = {
  11756. type: "FormalParameters"
  11757. };
  11758. for (const param of node.params) {
  11759. this.checkLVal(param, {
  11760. in: formalParameters,
  11761. binding: BIND_VAR,
  11762. checkClashes,
  11763. strictModeChanged
  11764. });
  11765. }
  11766. }
  11767. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  11768. const elts = [];
  11769. let first = true;
  11770. while (!this.eat(close)) {
  11771. if (first) {
  11772. first = false;
  11773. } else {
  11774. this.expect(12);
  11775. if (this.match(close)) {
  11776. if (nodeForExtra) {
  11777. this.addTrailingCommaExtraToNode(nodeForExtra);
  11778. }
  11779. this.next();
  11780. break;
  11781. }
  11782. }
  11783. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  11784. }
  11785. return elts;
  11786. }
  11787. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  11788. let elt;
  11789. if (this.match(12)) {
  11790. if (!allowEmpty) {
  11791. this.raise(Errors.UnexpectedToken, {
  11792. at: this.state.curPosition(),
  11793. unexpected: ","
  11794. });
  11795. }
  11796. elt = null;
  11797. } else if (this.match(21)) {
  11798. const spreadNodeStartPos = this.state.start;
  11799. const spreadNodeStartLoc = this.state.startLoc;
  11800. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartPos, spreadNodeStartLoc);
  11801. } else if (this.match(17)) {
  11802. this.expectPlugin("partialApplication");
  11803. if (!allowPlaceholder) {
  11804. this.raise(Errors.UnexpectedArgumentPlaceholder, {
  11805. at: this.state.startLoc
  11806. });
  11807. }
  11808. const node = this.startNode();
  11809. this.next();
  11810. elt = this.finishNode(node, "ArgumentPlaceholder");
  11811. } else {
  11812. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  11813. }
  11814. return elt;
  11815. }
  11816. parseIdentifier(liberal) {
  11817. const node = this.startNode();
  11818. const name = this.parseIdentifierName(node.start, liberal);
  11819. return this.createIdentifier(node, name);
  11820. }
  11821. createIdentifier(node, name) {
  11822. node.name = name;
  11823. node.loc.identifierName = name;
  11824. return this.finishNode(node, "Identifier");
  11825. }
  11826. parseIdentifierName(pos, liberal) {
  11827. let name;
  11828. const {
  11829. startLoc,
  11830. type
  11831. } = this.state;
  11832. if (tokenIsKeywordOrIdentifier(type)) {
  11833. name = this.state.value;
  11834. } else {
  11835. throw this.unexpected();
  11836. }
  11837. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  11838. if (liberal) {
  11839. if (tokenIsKeyword) {
  11840. this.replaceToken(128);
  11841. }
  11842. } else {
  11843. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  11844. }
  11845. this.next();
  11846. return name;
  11847. }
  11848. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  11849. if (word.length > 10) {
  11850. return;
  11851. }
  11852. if (!canBeReservedWord(word)) {
  11853. return;
  11854. }
  11855. if (word === "yield") {
  11856. if (this.prodParam.hasYield) {
  11857. this.raise(Errors.YieldBindingIdentifier, {
  11858. at: startLoc
  11859. });
  11860. return;
  11861. }
  11862. } else if (word === "await") {
  11863. if (this.prodParam.hasAwait) {
  11864. this.raise(Errors.AwaitBindingIdentifier, {
  11865. at: startLoc
  11866. });
  11867. return;
  11868. }
  11869. if (this.scope.inStaticBlock) {
  11870. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, {
  11871. at: startLoc
  11872. });
  11873. return;
  11874. }
  11875. this.expressionScope.recordAsyncArrowParametersError({
  11876. at: startLoc
  11877. });
  11878. } else if (word === "arguments") {
  11879. if (this.scope.inClassAndNotInNonArrowFunction) {
  11880. this.raise(Errors.ArgumentsInClass, {
  11881. at: startLoc
  11882. });
  11883. return;
  11884. }
  11885. }
  11886. if (checkKeywords && isKeyword(word)) {
  11887. this.raise(Errors.UnexpectedKeyword, {
  11888. at: startLoc,
  11889. keyword: word
  11890. });
  11891. return;
  11892. }
  11893. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  11894. if (reservedTest(word, this.inModule)) {
  11895. this.raise(Errors.UnexpectedReservedWord, {
  11896. at: startLoc,
  11897. reservedWord: word
  11898. });
  11899. }
  11900. }
  11901. isAwaitAllowed() {
  11902. if (this.prodParam.hasAwait) return true;
  11903. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  11904. return true;
  11905. }
  11906. return false;
  11907. }
  11908. parseAwait(startPos, startLoc) {
  11909. const node = this.startNodeAt(startPos, startLoc);
  11910. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, {
  11911. at: node
  11912. });
  11913. if (this.eat(55)) {
  11914. this.raise(Errors.ObsoleteAwaitStar, {
  11915. at: node
  11916. });
  11917. }
  11918. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  11919. if (this.isAmbiguousAwait()) {
  11920. this.ambiguousScriptDifferentAst = true;
  11921. } else {
  11922. this.sawUnambiguousESM = true;
  11923. }
  11924. }
  11925. if (!this.state.soloAwait) {
  11926. node.argument = this.parseMaybeUnary(null, true);
  11927. }
  11928. return this.finishNode(node, "AwaitExpression");
  11929. }
  11930. isAmbiguousAwait() {
  11931. if (this.hasPrecedingLineBreak()) return true;
  11932. const {
  11933. type
  11934. } = this.state;
  11935. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 133 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  11936. }
  11937. parseYield() {
  11938. const node = this.startNode();
  11939. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, {
  11940. at: node
  11941. });
  11942. this.next();
  11943. let delegating = false;
  11944. let argument = null;
  11945. if (!this.hasPrecedingLineBreak()) {
  11946. delegating = this.eat(55);
  11947. switch (this.state.type) {
  11948. case 13:
  11949. case 135:
  11950. case 8:
  11951. case 11:
  11952. case 3:
  11953. case 9:
  11954. case 14:
  11955. case 12:
  11956. if (!delegating) break;
  11957. default:
  11958. argument = this.parseMaybeAssign();
  11959. }
  11960. }
  11961. node.delegate = delegating;
  11962. node.argument = argument;
  11963. return this.finishNode(node, "YieldExpression");
  11964. }
  11965. checkPipelineAtInfixOperator(left, leftStartLoc) {
  11966. if (this.hasPlugin(["pipelineOperator", {
  11967. proposal: "smart"
  11968. }])) {
  11969. if (left.type === "SequenceExpression") {
  11970. this.raise(Errors.PipelineHeadSequenceExpression, {
  11971. at: leftStartLoc
  11972. });
  11973. }
  11974. }
  11975. }
  11976. parseSmartPipelineBodyInStyle(childExpr, startPos, startLoc) {
  11977. if (this.isSimpleReference(childExpr)) {
  11978. const bodyNode = this.startNodeAt(startPos, startLoc);
  11979. bodyNode.callee = childExpr;
  11980. return this.finishNode(bodyNode, "PipelineBareFunction");
  11981. } else {
  11982. const bodyNode = this.startNodeAt(startPos, startLoc);
  11983. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  11984. bodyNode.expression = childExpr;
  11985. return this.finishNode(bodyNode, "PipelineTopicExpression");
  11986. }
  11987. }
  11988. isSimpleReference(expression) {
  11989. switch (expression.type) {
  11990. case "MemberExpression":
  11991. return !expression.computed && this.isSimpleReference(expression.object);
  11992. case "Identifier":
  11993. return true;
  11994. default:
  11995. return false;
  11996. }
  11997. }
  11998. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  11999. if (this.match(19)) {
  12000. throw this.raise(Errors.PipelineBodyNoArrow, {
  12001. at: this.state.startLoc
  12002. });
  12003. }
  12004. if (!this.topicReferenceWasUsedInCurrentContext()) {
  12005. this.raise(Errors.PipelineTopicUnused, {
  12006. at: startLoc
  12007. });
  12008. }
  12009. }
  12010. withTopicBindingContext(callback) {
  12011. const outerContextTopicState = this.state.topicContext;
  12012. this.state.topicContext = {
  12013. maxNumOfResolvableTopics: 1,
  12014. maxTopicIndex: null
  12015. };
  12016. try {
  12017. return callback();
  12018. } finally {
  12019. this.state.topicContext = outerContextTopicState;
  12020. }
  12021. }
  12022. withSmartMixTopicForbiddingContext(callback) {
  12023. if (this.hasPlugin(["pipelineOperator", {
  12024. proposal: "smart"
  12025. }])) {
  12026. const outerContextTopicState = this.state.topicContext;
  12027. this.state.topicContext = {
  12028. maxNumOfResolvableTopics: 0,
  12029. maxTopicIndex: null
  12030. };
  12031. try {
  12032. return callback();
  12033. } finally {
  12034. this.state.topicContext = outerContextTopicState;
  12035. }
  12036. } else {
  12037. return callback();
  12038. }
  12039. }
  12040. withSoloAwaitPermittingContext(callback) {
  12041. const outerContextSoloAwaitState = this.state.soloAwait;
  12042. this.state.soloAwait = true;
  12043. try {
  12044. return callback();
  12045. } finally {
  12046. this.state.soloAwait = outerContextSoloAwaitState;
  12047. }
  12048. }
  12049. allowInAnd(callback) {
  12050. const flags = this.prodParam.currentFlags();
  12051. const prodParamToSet = PARAM_IN & ~flags;
  12052. if (prodParamToSet) {
  12053. this.prodParam.enter(flags | PARAM_IN);
  12054. try {
  12055. return callback();
  12056. } finally {
  12057. this.prodParam.exit();
  12058. }
  12059. }
  12060. return callback();
  12061. }
  12062. disallowInAnd(callback) {
  12063. const flags = this.prodParam.currentFlags();
  12064. const prodParamToClear = PARAM_IN & flags;
  12065. if (prodParamToClear) {
  12066. this.prodParam.enter(flags & ~PARAM_IN);
  12067. try {
  12068. return callback();
  12069. } finally {
  12070. this.prodParam.exit();
  12071. }
  12072. }
  12073. return callback();
  12074. }
  12075. registerTopicReference() {
  12076. this.state.topicContext.maxTopicIndex = 0;
  12077. }
  12078. topicReferenceIsAllowedInCurrentContext() {
  12079. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  12080. }
  12081. topicReferenceWasUsedInCurrentContext() {
  12082. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  12083. }
  12084. parseFSharpPipelineBody(prec) {
  12085. const startPos = this.state.start;
  12086. const startLoc = this.state.startLoc;
  12087. this.state.potentialArrowAt = this.state.start;
  12088. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  12089. this.state.inFSharpPipelineDirectBody = true;
  12090. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, prec);
  12091. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  12092. return ret;
  12093. }
  12094. parseModuleExpression() {
  12095. this.expectPlugin("moduleBlocks");
  12096. const node = this.startNode();
  12097. this.next();
  12098. this.eat(5);
  12099. const revertScopes = this.initializeScopes(true);
  12100. this.enterInitialScopes();
  12101. const program = this.startNode();
  12102. try {
  12103. node.body = this.parseProgram(program, 8, "module");
  12104. } finally {
  12105. revertScopes();
  12106. }
  12107. this.eat(8);
  12108. return this.finishNode(node, "ModuleExpression");
  12109. }
  12110. parsePropertyNamePrefixOperator(prop) {}
  12111. }
  12112. const loopLabel = {
  12113. kind: "loop"
  12114. },
  12115. switchLabel = {
  12116. kind: "switch"
  12117. };
  12118. const FUNC_NO_FLAGS = 0b000,
  12119. FUNC_STATEMENT = 0b001,
  12120. FUNC_HANGING_STATEMENT = 0b010,
  12121. FUNC_NULLABLE_ID = 0b100;
  12122. const loneSurrogate = /[\uD800-\uDFFF]/u;
  12123. const keywordRelationalOperator = /in(?:stanceof)?/y;
  12124. function babel7CompatTokens(tokens, input) {
  12125. for (let i = 0; i < tokens.length; i++) {
  12126. const token = tokens[i];
  12127. const {
  12128. type
  12129. } = token;
  12130. if (typeof type === "number") {
  12131. {
  12132. if (type === 134) {
  12133. const {
  12134. loc,
  12135. start,
  12136. value,
  12137. end
  12138. } = token;
  12139. const hashEndPos = start + 1;
  12140. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12141. tokens.splice(i, 1, new Token({
  12142. type: getExportedToken(27),
  12143. value: "#",
  12144. start: start,
  12145. end: hashEndPos,
  12146. startLoc: loc.start,
  12147. endLoc: hashEndLoc
  12148. }), new Token({
  12149. type: getExportedToken(128),
  12150. value: value,
  12151. start: hashEndPos,
  12152. end: end,
  12153. startLoc: hashEndLoc,
  12154. endLoc: loc.end
  12155. }));
  12156. i++;
  12157. continue;
  12158. }
  12159. if (tokenIsTemplate(type)) {
  12160. const {
  12161. loc,
  12162. start,
  12163. value,
  12164. end
  12165. } = token;
  12166. const backquoteEnd = start + 1;
  12167. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12168. let startToken;
  12169. if (input.charCodeAt(start) === 96) {
  12170. startToken = new Token({
  12171. type: getExportedToken(22),
  12172. value: "`",
  12173. start: start,
  12174. end: backquoteEnd,
  12175. startLoc: loc.start,
  12176. endLoc: backquoteEndLoc
  12177. });
  12178. } else {
  12179. startToken = new Token({
  12180. type: getExportedToken(8),
  12181. value: "}",
  12182. start: start,
  12183. end: backquoteEnd,
  12184. startLoc: loc.start,
  12185. endLoc: backquoteEndLoc
  12186. });
  12187. }
  12188. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  12189. if (type === 24) {
  12190. templateElementEnd = end - 1;
  12191. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  12192. templateValue = value === null ? null : value.slice(1, -1);
  12193. endToken = new Token({
  12194. type: getExportedToken(22),
  12195. value: "`",
  12196. start: templateElementEnd,
  12197. end: end,
  12198. startLoc: templateElementEndLoc,
  12199. endLoc: loc.end
  12200. });
  12201. } else {
  12202. templateElementEnd = end - 2;
  12203. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  12204. templateValue = value === null ? null : value.slice(1, -2);
  12205. endToken = new Token({
  12206. type: getExportedToken(23),
  12207. value: "${",
  12208. start: templateElementEnd,
  12209. end: end,
  12210. startLoc: templateElementEndLoc,
  12211. endLoc: loc.end
  12212. });
  12213. }
  12214. tokens.splice(i, 1, startToken, new Token({
  12215. type: getExportedToken(20),
  12216. value: templateValue,
  12217. start: backquoteEnd,
  12218. end: templateElementEnd,
  12219. startLoc: backquoteEndLoc,
  12220. endLoc: templateElementEndLoc
  12221. }), endToken);
  12222. i += 2;
  12223. continue;
  12224. }
  12225. }
  12226. token.type = getExportedToken(type);
  12227. }
  12228. }
  12229. return tokens;
  12230. }
  12231. class StatementParser extends ExpressionParser {
  12232. parseTopLevel(file, program) {
  12233. file.program = this.parseProgram(program);
  12234. file.comments = this.state.comments;
  12235. if (this.options.tokens) {
  12236. file.tokens = babel7CompatTokens(this.tokens, this.input);
  12237. }
  12238. return this.finishNode(file, "File");
  12239. }
  12240. parseProgram(program, end = 135, sourceType = this.options.sourceType) {
  12241. program.sourceType = sourceType;
  12242. program.interpreter = this.parseInterpreterDirective();
  12243. this.parseBlockBody(program, true, true, end);
  12244. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  12245. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12246. this.raise(Errors.ModuleExportUndefined, {
  12247. at,
  12248. localName
  12249. });
  12250. }
  12251. }
  12252. return this.finishNode(program, "Program");
  12253. }
  12254. stmtToDirective(stmt) {
  12255. const directive = stmt;
  12256. directive.type = "Directive";
  12257. directive.value = directive.expression;
  12258. delete directive.expression;
  12259. const directiveLiteral = directive.value;
  12260. const expressionValue = directiveLiteral.value;
  12261. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  12262. const val = directiveLiteral.value = raw.slice(1, -1);
  12263. this.addExtra(directiveLiteral, "raw", raw);
  12264. this.addExtra(directiveLiteral, "rawValue", val);
  12265. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12266. directiveLiteral.type = "DirectiveLiteral";
  12267. return directive;
  12268. }
  12269. parseInterpreterDirective() {
  12270. if (!this.match(28)) {
  12271. return null;
  12272. }
  12273. const node = this.startNode();
  12274. node.value = this.state.value;
  12275. this.next();
  12276. return this.finishNode(node, "InterpreterDirective");
  12277. }
  12278. isLet(context) {
  12279. if (!this.isContextual(99)) {
  12280. return false;
  12281. }
  12282. return this.isLetKeyword(context);
  12283. }
  12284. isLetKeyword(context) {
  12285. const next = this.nextTokenStart();
  12286. const nextCh = this.codePointAtPos(next);
  12287. if (nextCh === 92 || nextCh === 91) {
  12288. return true;
  12289. }
  12290. if (context) return false;
  12291. if (nextCh === 123) return true;
  12292. if (isIdentifierStart(nextCh)) {
  12293. keywordRelationalOperator.lastIndex = next;
  12294. if (keywordRelationalOperator.test(this.input)) {
  12295. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12296. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12297. return false;
  12298. }
  12299. }
  12300. return true;
  12301. }
  12302. return false;
  12303. }
  12304. parseStatement(context, topLevel) {
  12305. if (this.match(26)) {
  12306. this.parseDecorators(true);
  12307. }
  12308. return this.parseStatementContent(context, topLevel);
  12309. }
  12310. parseStatementContent(context, topLevel) {
  12311. let starttype = this.state.type;
  12312. const node = this.startNode();
  12313. let kind;
  12314. if (this.isLet(context)) {
  12315. starttype = 74;
  12316. kind = "let";
  12317. }
  12318. switch (starttype) {
  12319. case 60:
  12320. return this.parseBreakContinueStatement(node, true);
  12321. case 63:
  12322. return this.parseBreakContinueStatement(node, false);
  12323. case 64:
  12324. return this.parseDebuggerStatement(node);
  12325. case 90:
  12326. return this.parseDoStatement(node);
  12327. case 91:
  12328. return this.parseForStatement(node);
  12329. case 68:
  12330. if (this.lookaheadCharCode() === 46) break;
  12331. if (context) {
  12332. if (this.state.strict) {
  12333. this.raise(Errors.StrictFunction, {
  12334. at: this.state.startLoc
  12335. });
  12336. } else if (context !== "if" && context !== "label") {
  12337. this.raise(Errors.SloppyFunction, {
  12338. at: this.state.startLoc
  12339. });
  12340. }
  12341. }
  12342. return this.parseFunctionStatement(node, false, !context);
  12343. case 80:
  12344. if (context) this.unexpected();
  12345. return this.parseClass(node, true);
  12346. case 69:
  12347. return this.parseIfStatement(node);
  12348. case 70:
  12349. return this.parseReturnStatement(node);
  12350. case 71:
  12351. return this.parseSwitchStatement(node);
  12352. case 72:
  12353. return this.parseThrowStatement(node);
  12354. case 73:
  12355. return this.parseTryStatement(node);
  12356. case 75:
  12357. case 74:
  12358. kind = kind || this.state.value;
  12359. if (context && kind !== "var") {
  12360. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12361. at: this.state.startLoc
  12362. });
  12363. }
  12364. return this.parseVarStatement(node, kind);
  12365. case 92:
  12366. return this.parseWhileStatement(node);
  12367. case 76:
  12368. return this.parseWithStatement(node);
  12369. case 5:
  12370. return this.parseBlock();
  12371. case 13:
  12372. return this.parseEmptyStatement(node);
  12373. case 83:
  12374. {
  12375. const nextTokenCharCode = this.lookaheadCharCode();
  12376. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12377. break;
  12378. }
  12379. }
  12380. case 82:
  12381. {
  12382. if (!this.options.allowImportExportEverywhere && !topLevel) {
  12383. this.raise(Errors.UnexpectedImportExport, {
  12384. at: this.state.startLoc
  12385. });
  12386. }
  12387. this.next();
  12388. let result;
  12389. if (starttype === 83) {
  12390. result = this.parseImport(node);
  12391. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  12392. this.sawUnambiguousESM = true;
  12393. }
  12394. } else {
  12395. result = this.parseExport(node);
  12396. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  12397. this.sawUnambiguousESM = true;
  12398. }
  12399. }
  12400. this.assertModuleNodeAllowed(result);
  12401. return result;
  12402. }
  12403. default:
  12404. {
  12405. if (this.isAsyncFunction()) {
  12406. if (context) {
  12407. this.raise(Errors.AsyncFunctionInSingleStatementContext, {
  12408. at: this.state.startLoc
  12409. });
  12410. }
  12411. this.next();
  12412. return this.parseFunctionStatement(node, true, !context);
  12413. }
  12414. }
  12415. }
  12416. const maybeName = this.state.value;
  12417. const expr = this.parseExpression();
  12418. if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
  12419. return this.parseLabeledStatement(node, maybeName, expr, context);
  12420. } else {
  12421. return this.parseExpressionStatement(node, expr);
  12422. }
  12423. }
  12424. assertModuleNodeAllowed(node) {
  12425. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  12426. this.raise(Errors.ImportOutsideModule, {
  12427. at: node
  12428. });
  12429. }
  12430. }
  12431. takeDecorators(node) {
  12432. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  12433. if (decorators.length) {
  12434. node.decorators = decorators;
  12435. this.resetStartLocationFromNode(node, decorators[0]);
  12436. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  12437. }
  12438. }
  12439. canHaveLeadingDecorator() {
  12440. return this.match(80);
  12441. }
  12442. parseDecorators(allowExport) {
  12443. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  12444. while (this.match(26)) {
  12445. const decorator = this.parseDecorator();
  12446. currentContextDecorators.push(decorator);
  12447. }
  12448. if (this.match(82)) {
  12449. if (!allowExport) {
  12450. this.unexpected();
  12451. }
  12452. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  12453. this.raise(Errors.DecoratorExportClass, {
  12454. at: this.state.startLoc
  12455. });
  12456. }
  12457. } else if (!this.canHaveLeadingDecorator()) {
  12458. throw this.raise(Errors.UnexpectedLeadingDecorator, {
  12459. at: this.state.startLoc
  12460. });
  12461. }
  12462. }
  12463. parseDecorator() {
  12464. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12465. const node = this.startNode();
  12466. this.next();
  12467. if (this.hasPlugin("decorators")) {
  12468. this.state.decoratorStack.push([]);
  12469. const startPos = this.state.start;
  12470. const startLoc = this.state.startLoc;
  12471. let expr;
  12472. if (this.match(10)) {
  12473. const startPos = this.state.start;
  12474. const startLoc = this.state.startLoc;
  12475. this.next();
  12476. expr = this.parseExpression();
  12477. this.expect(11);
  12478. expr = this.wrapParenthesis(startPos, startLoc, expr);
  12479. } else {
  12480. expr = this.parseIdentifier(false);
  12481. while (this.eat(16)) {
  12482. const node = this.startNodeAt(startPos, startLoc);
  12483. node.object = expr;
  12484. node.property = this.parseIdentifier(true);
  12485. node.computed = false;
  12486. expr = this.finishNode(node, "MemberExpression");
  12487. }
  12488. }
  12489. node.expression = this.parseMaybeDecoratorArguments(expr);
  12490. this.state.decoratorStack.pop();
  12491. } else {
  12492. node.expression = this.parseExprSubscripts();
  12493. }
  12494. return this.finishNode(node, "Decorator");
  12495. }
  12496. parseMaybeDecoratorArguments(expr) {
  12497. if (this.eat(10)) {
  12498. const node = this.startNodeAtNode(expr);
  12499. node.callee = expr;
  12500. node.arguments = this.parseCallExpressionArguments(11, false);
  12501. this.toReferencedList(node.arguments);
  12502. return this.finishNode(node, "CallExpression");
  12503. }
  12504. return expr;
  12505. }
  12506. parseBreakContinueStatement(node, isBreak) {
  12507. this.next();
  12508. if (this.isLineTerminator()) {
  12509. node.label = null;
  12510. } else {
  12511. node.label = this.parseIdentifier();
  12512. this.semicolon();
  12513. }
  12514. this.verifyBreakContinue(node, isBreak);
  12515. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  12516. }
  12517. verifyBreakContinue(node, isBreak) {
  12518. let i;
  12519. for (i = 0; i < this.state.labels.length; ++i) {
  12520. const lab = this.state.labels[i];
  12521. if (node.label == null || lab.name === node.label.name) {
  12522. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  12523. if (node.label && isBreak) break;
  12524. }
  12525. }
  12526. if (i === this.state.labels.length) {
  12527. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  12528. this.raise(Errors.IllegalBreakContinue, {
  12529. at: node,
  12530. type
  12531. });
  12532. }
  12533. }
  12534. parseDebuggerStatement(node) {
  12535. this.next();
  12536. this.semicolon();
  12537. return this.finishNode(node, "DebuggerStatement");
  12538. }
  12539. parseHeaderExpression() {
  12540. this.expect(10);
  12541. const val = this.parseExpression();
  12542. this.expect(11);
  12543. return val;
  12544. }
  12545. parseDoStatement(node) {
  12546. this.next();
  12547. this.state.labels.push(loopLabel);
  12548. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("do"));
  12549. this.state.labels.pop();
  12550. this.expect(92);
  12551. node.test = this.parseHeaderExpression();
  12552. this.eat(13);
  12553. return this.finishNode(node, "DoWhileStatement");
  12554. }
  12555. parseForStatement(node) {
  12556. this.next();
  12557. this.state.labels.push(loopLabel);
  12558. let awaitAt = null;
  12559. if (this.isAwaitAllowed() && this.eatContextual(96)) {
  12560. awaitAt = this.state.lastTokStartLoc;
  12561. }
  12562. this.scope.enter(SCOPE_OTHER);
  12563. this.expect(10);
  12564. if (this.match(13)) {
  12565. if (awaitAt !== null) {
  12566. this.unexpected(awaitAt);
  12567. }
  12568. return this.parseFor(node, null);
  12569. }
  12570. const startsWithLet = this.isContextual(99);
  12571. const isLet = startsWithLet && this.isLetKeyword();
  12572. if (this.match(74) || this.match(75) || isLet) {
  12573. const initNode = this.startNode();
  12574. const kind = isLet ? "let" : this.state.value;
  12575. this.next();
  12576. this.parseVar(initNode, true, kind);
  12577. const init = this.finishNode(initNode, "VariableDeclaration");
  12578. if ((this.match(58) || this.isContextual(101)) && init.declarations.length === 1) {
  12579. return this.parseForIn(node, init, awaitAt);
  12580. }
  12581. if (awaitAt !== null) {
  12582. this.unexpected(awaitAt);
  12583. }
  12584. return this.parseFor(node, init);
  12585. }
  12586. const startsWithAsync = this.isContextual(95);
  12587. const refExpressionErrors = new ExpressionErrors();
  12588. const init = this.parseExpression(true, refExpressionErrors);
  12589. const isForOf = this.isContextual(101);
  12590. if (isForOf) {
  12591. if (startsWithLet) {
  12592. this.raise(Errors.ForOfLet, {
  12593. at: init
  12594. });
  12595. }
  12596. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  12597. this.raise(Errors.ForOfAsync, {
  12598. at: init
  12599. });
  12600. }
  12601. }
  12602. if (isForOf || this.match(58)) {
  12603. this.checkDestructuringPrivate(refExpressionErrors);
  12604. this.toAssignable(init, true);
  12605. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  12606. this.checkLVal(init, {
  12607. in: {
  12608. type
  12609. }
  12610. });
  12611. return this.parseForIn(node, init, awaitAt);
  12612. } else {
  12613. this.checkExpressionErrors(refExpressionErrors, true);
  12614. }
  12615. if (awaitAt !== null) {
  12616. this.unexpected(awaitAt);
  12617. }
  12618. return this.parseFor(node, init);
  12619. }
  12620. parseFunctionStatement(node, isAsync, declarationPosition) {
  12621. this.next();
  12622. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  12623. }
  12624. parseIfStatement(node) {
  12625. this.next();
  12626. node.test = this.parseHeaderExpression();
  12627. node.consequent = this.parseStatement("if");
  12628. node.alternate = this.eat(66) ? this.parseStatement("if") : null;
  12629. return this.finishNode(node, "IfStatement");
  12630. }
  12631. parseReturnStatement(node) {
  12632. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  12633. this.raise(Errors.IllegalReturn, {
  12634. at: this.state.startLoc
  12635. });
  12636. }
  12637. this.next();
  12638. if (this.isLineTerminator()) {
  12639. node.argument = null;
  12640. } else {
  12641. node.argument = this.parseExpression();
  12642. this.semicolon();
  12643. }
  12644. return this.finishNode(node, "ReturnStatement");
  12645. }
  12646. parseSwitchStatement(node) {
  12647. this.next();
  12648. node.discriminant = this.parseHeaderExpression();
  12649. const cases = node.cases = [];
  12650. this.expect(5);
  12651. this.state.labels.push(switchLabel);
  12652. this.scope.enter(SCOPE_OTHER);
  12653. let cur;
  12654. for (let sawDefault; !this.match(8);) {
  12655. if (this.match(61) || this.match(65)) {
  12656. const isCase = this.match(61);
  12657. if (cur) this.finishNode(cur, "SwitchCase");
  12658. cases.push(cur = this.startNode());
  12659. cur.consequent = [];
  12660. this.next();
  12661. if (isCase) {
  12662. cur.test = this.parseExpression();
  12663. } else {
  12664. if (sawDefault) {
  12665. this.raise(Errors.MultipleDefaultsInSwitch, {
  12666. at: this.state.lastTokStartLoc
  12667. });
  12668. }
  12669. sawDefault = true;
  12670. cur.test = null;
  12671. }
  12672. this.expect(14);
  12673. } else {
  12674. if (cur) {
  12675. cur.consequent.push(this.parseStatement(null));
  12676. } else {
  12677. this.unexpected();
  12678. }
  12679. }
  12680. }
  12681. this.scope.exit();
  12682. if (cur) this.finishNode(cur, "SwitchCase");
  12683. this.next();
  12684. this.state.labels.pop();
  12685. return this.finishNode(node, "SwitchStatement");
  12686. }
  12687. parseThrowStatement(node) {
  12688. this.next();
  12689. if (this.hasPrecedingLineBreak()) {
  12690. this.raise(Errors.NewlineAfterThrow, {
  12691. at: this.state.lastTokEndLoc
  12692. });
  12693. }
  12694. node.argument = this.parseExpression();
  12695. this.semicolon();
  12696. return this.finishNode(node, "ThrowStatement");
  12697. }
  12698. parseCatchClauseParam() {
  12699. const param = this.parseBindingAtom();
  12700. const simple = param.type === "Identifier";
  12701. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  12702. this.checkLVal(param, {
  12703. in: {
  12704. type: "CatchClause"
  12705. },
  12706. binding: BIND_LEXICAL,
  12707. allowingSloppyLetBinding: true
  12708. });
  12709. return param;
  12710. }
  12711. parseTryStatement(node) {
  12712. this.next();
  12713. node.block = this.parseBlock();
  12714. node.handler = null;
  12715. if (this.match(62)) {
  12716. const clause = this.startNode();
  12717. this.next();
  12718. if (this.match(10)) {
  12719. this.expect(10);
  12720. clause.param = this.parseCatchClauseParam();
  12721. this.expect(11);
  12722. } else {
  12723. clause.param = null;
  12724. this.scope.enter(SCOPE_OTHER);
  12725. }
  12726. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  12727. this.scope.exit();
  12728. node.handler = this.finishNode(clause, "CatchClause");
  12729. }
  12730. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  12731. if (!node.handler && !node.finalizer) {
  12732. this.raise(Errors.NoCatchOrFinally, {
  12733. at: node
  12734. });
  12735. }
  12736. return this.finishNode(node, "TryStatement");
  12737. }
  12738. parseVarStatement(node, kind, allowMissingInitializer = false) {
  12739. this.next();
  12740. this.parseVar(node, false, kind, allowMissingInitializer);
  12741. this.semicolon();
  12742. return this.finishNode(node, "VariableDeclaration");
  12743. }
  12744. parseWhileStatement(node) {
  12745. this.next();
  12746. node.test = this.parseHeaderExpression();
  12747. this.state.labels.push(loopLabel);
  12748. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("while"));
  12749. this.state.labels.pop();
  12750. return this.finishNode(node, "WhileStatement");
  12751. }
  12752. parseWithStatement(node) {
  12753. if (this.state.strict) {
  12754. this.raise(Errors.StrictWith, {
  12755. at: this.state.startLoc
  12756. });
  12757. }
  12758. this.next();
  12759. node.object = this.parseHeaderExpression();
  12760. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("with"));
  12761. return this.finishNode(node, "WithStatement");
  12762. }
  12763. parseEmptyStatement(node) {
  12764. this.next();
  12765. return this.finishNode(node, "EmptyStatement");
  12766. }
  12767. parseLabeledStatement(node, maybeName, expr, context) {
  12768. for (const label of this.state.labels) {
  12769. if (label.name === maybeName) {
  12770. this.raise(Errors.LabelRedeclaration, {
  12771. at: expr,
  12772. labelName: maybeName
  12773. });
  12774. }
  12775. }
  12776. const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(71) ? "switch" : null;
  12777. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  12778. const label = this.state.labels[i];
  12779. if (label.statementStart === node.start) {
  12780. label.statementStart = this.state.start;
  12781. label.kind = kind;
  12782. } else {
  12783. break;
  12784. }
  12785. }
  12786. this.state.labels.push({
  12787. name: maybeName,
  12788. kind: kind,
  12789. statementStart: this.state.start
  12790. });
  12791. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  12792. this.state.labels.pop();
  12793. node.label = expr;
  12794. return this.finishNode(node, "LabeledStatement");
  12795. }
  12796. parseExpressionStatement(node, expr) {
  12797. node.expression = expr;
  12798. this.semicolon();
  12799. return this.finishNode(node, "ExpressionStatement");
  12800. }
  12801. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  12802. const node = this.startNode();
  12803. if (allowDirectives) {
  12804. this.state.strictErrors.clear();
  12805. }
  12806. this.expect(5);
  12807. if (createNewLexicalScope) {
  12808. this.scope.enter(SCOPE_OTHER);
  12809. }
  12810. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  12811. if (createNewLexicalScope) {
  12812. this.scope.exit();
  12813. }
  12814. return this.finishNode(node, "BlockStatement");
  12815. }
  12816. isValidDirective(stmt) {
  12817. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  12818. }
  12819. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  12820. const body = node.body = [];
  12821. const directives = node.directives = [];
  12822. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  12823. }
  12824. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  12825. const oldStrict = this.state.strict;
  12826. let hasStrictModeDirective = false;
  12827. let parsedNonDirective = false;
  12828. while (!this.match(end)) {
  12829. const stmt = this.parseStatement(null, topLevel);
  12830. if (directives && !parsedNonDirective) {
  12831. if (this.isValidDirective(stmt)) {
  12832. const directive = this.stmtToDirective(stmt);
  12833. directives.push(directive);
  12834. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  12835. hasStrictModeDirective = true;
  12836. this.setStrict(true);
  12837. }
  12838. continue;
  12839. }
  12840. parsedNonDirective = true;
  12841. this.state.strictErrors.clear();
  12842. }
  12843. body.push(stmt);
  12844. }
  12845. if (afterBlockParse) {
  12846. afterBlockParse.call(this, hasStrictModeDirective);
  12847. }
  12848. if (!oldStrict) {
  12849. this.setStrict(false);
  12850. }
  12851. this.next();
  12852. }
  12853. parseFor(node, init) {
  12854. node.init = init;
  12855. this.semicolon(false);
  12856. node.test = this.match(13) ? null : this.parseExpression();
  12857. this.semicolon(false);
  12858. node.update = this.match(11) ? null : this.parseExpression();
  12859. this.expect(11);
  12860. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  12861. this.scope.exit();
  12862. this.state.labels.pop();
  12863. return this.finishNode(node, "ForStatement");
  12864. }
  12865. parseForIn(node, init, awaitAt) {
  12866. const isForIn = this.match(58);
  12867. this.next();
  12868. if (isForIn) {
  12869. if (awaitAt !== null) this.unexpected(awaitAt);
  12870. } else {
  12871. node.await = awaitAt !== null;
  12872. }
  12873. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  12874. this.raise(Errors.ForInOfLoopInitializer, {
  12875. at: init,
  12876. type: isForIn ? "ForInStatement" : "ForOfStatement"
  12877. });
  12878. }
  12879. if (init.type === "AssignmentPattern") {
  12880. this.raise(Errors.InvalidLhs, {
  12881. at: init,
  12882. ancestor: {
  12883. type: "ForStatement"
  12884. }
  12885. });
  12886. }
  12887. node.left = init;
  12888. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  12889. this.expect(11);
  12890. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  12891. this.scope.exit();
  12892. this.state.labels.pop();
  12893. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  12894. }
  12895. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  12896. const declarations = node.declarations = [];
  12897. node.kind = kind;
  12898. for (;;) {
  12899. const decl = this.startNode();
  12900. this.parseVarId(decl, kind);
  12901. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  12902. if (decl.init === null && !allowMissingInitializer) {
  12903. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(101)))) {
  12904. this.raise(Errors.DeclarationMissingInitializer, {
  12905. at: this.state.lastTokEndLoc,
  12906. kind: "destructuring"
  12907. });
  12908. } else if (kind === "const" && !(this.match(58) || this.isContextual(101))) {
  12909. this.raise(Errors.DeclarationMissingInitializer, {
  12910. at: this.state.lastTokEndLoc,
  12911. kind: "const"
  12912. });
  12913. }
  12914. }
  12915. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  12916. if (!this.eat(12)) break;
  12917. }
  12918. return node;
  12919. }
  12920. parseVarId(decl, kind) {
  12921. decl.id = this.parseBindingAtom();
  12922. this.checkLVal(decl.id, {
  12923. in: {
  12924. type: "VariableDeclarator"
  12925. },
  12926. binding: kind === "var" ? BIND_VAR : BIND_LEXICAL
  12927. });
  12928. }
  12929. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  12930. const isStatement = statement & FUNC_STATEMENT;
  12931. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  12932. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  12933. this.initFunction(node, isAsync);
  12934. if (this.match(55) && isHangingStatement) {
  12935. this.raise(Errors.GeneratorInSingleStatementContext, {
  12936. at: this.state.startLoc
  12937. });
  12938. }
  12939. node.generator = this.eat(55);
  12940. if (isStatement) {
  12941. node.id = this.parseFunctionId(requireId);
  12942. }
  12943. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  12944. this.state.maybeInArrowParameters = false;
  12945. this.scope.enter(SCOPE_FUNCTION);
  12946. this.prodParam.enter(functionFlags(isAsync, node.generator));
  12947. if (!isStatement) {
  12948. node.id = this.parseFunctionId();
  12949. }
  12950. this.parseFunctionParams(node, false);
  12951. this.withSmartMixTopicForbiddingContext(() => {
  12952. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  12953. });
  12954. this.prodParam.exit();
  12955. this.scope.exit();
  12956. if (isStatement && !isHangingStatement) {
  12957. this.registerFunctionStatementId(node);
  12958. }
  12959. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  12960. return node;
  12961. }
  12962. parseFunctionId(requireId) {
  12963. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  12964. }
  12965. parseFunctionParams(node, allowModifiers) {
  12966. this.expect(10);
  12967. this.expressionScope.enter(newParameterDeclarationScope());
  12968. node.params = this.parseBindingList(11, 41, false, allowModifiers);
  12969. this.expressionScope.exit();
  12970. }
  12971. registerFunctionStatementId(node) {
  12972. if (!node.id) return;
  12973. this.scope.declareName(node.id.name, this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, node.id.loc.start);
  12974. }
  12975. parseClass(node, isStatement, optionalId) {
  12976. this.next();
  12977. this.takeDecorators(node);
  12978. const oldStrict = this.state.strict;
  12979. this.state.strict = true;
  12980. this.parseClassId(node, isStatement, optionalId);
  12981. this.parseClassSuper(node);
  12982. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  12983. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  12984. }
  12985. isClassProperty() {
  12986. return this.match(29) || this.match(13) || this.match(8);
  12987. }
  12988. isClassMethod() {
  12989. return this.match(10);
  12990. }
  12991. isNonstaticConstructor(method) {
  12992. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  12993. }
  12994. parseClassBody(hadSuperClass, oldStrict) {
  12995. this.classScope.enter();
  12996. const state = {
  12997. hadConstructor: false,
  12998. hadSuperClass
  12999. };
  13000. let decorators = [];
  13001. const classBody = this.startNode();
  13002. classBody.body = [];
  13003. this.expect(5);
  13004. this.withSmartMixTopicForbiddingContext(() => {
  13005. while (!this.match(8)) {
  13006. if (this.eat(13)) {
  13007. if (decorators.length > 0) {
  13008. throw this.raise(Errors.DecoratorSemicolon, {
  13009. at: this.state.lastTokEndLoc
  13010. });
  13011. }
  13012. continue;
  13013. }
  13014. if (this.match(26)) {
  13015. decorators.push(this.parseDecorator());
  13016. continue;
  13017. }
  13018. const member = this.startNode();
  13019. if (decorators.length) {
  13020. member.decorators = decorators;
  13021. this.resetStartLocationFromNode(member, decorators[0]);
  13022. decorators = [];
  13023. }
  13024. this.parseClassMember(classBody, member, state);
  13025. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  13026. this.raise(Errors.DecoratorConstructor, {
  13027. at: member
  13028. });
  13029. }
  13030. }
  13031. });
  13032. this.state.strict = oldStrict;
  13033. this.next();
  13034. if (decorators.length) {
  13035. throw this.raise(Errors.TrailingDecorator, {
  13036. at: this.state.startLoc
  13037. });
  13038. }
  13039. this.classScope.exit();
  13040. return this.finishNode(classBody, "ClassBody");
  13041. }
  13042. parseClassMemberFromModifier(classBody, member) {
  13043. const key = this.parseIdentifier(true);
  13044. if (this.isClassMethod()) {
  13045. const method = member;
  13046. method.kind = "method";
  13047. method.computed = false;
  13048. method.key = key;
  13049. method.static = false;
  13050. this.pushClassMethod(classBody, method, false, false, false, false);
  13051. return true;
  13052. } else if (this.isClassProperty()) {
  13053. const prop = member;
  13054. prop.computed = false;
  13055. prop.key = key;
  13056. prop.static = false;
  13057. classBody.body.push(this.parseClassProperty(prop));
  13058. return true;
  13059. }
  13060. this.resetPreviousNodeTrailingComments(key);
  13061. return false;
  13062. }
  13063. parseClassMember(classBody, member, state) {
  13064. const isStatic = this.isContextual(104);
  13065. if (isStatic) {
  13066. if (this.parseClassMemberFromModifier(classBody, member)) {
  13067. return;
  13068. }
  13069. if (this.eat(5)) {
  13070. this.parseClassStaticBlock(classBody, member);
  13071. return;
  13072. }
  13073. }
  13074. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  13075. }
  13076. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  13077. const publicMethod = member;
  13078. const privateMethod = member;
  13079. const publicProp = member;
  13080. const privateProp = member;
  13081. const accessorProp = member;
  13082. const method = publicMethod;
  13083. const publicMember = publicMethod;
  13084. member.static = isStatic;
  13085. this.parsePropertyNamePrefixOperator(member);
  13086. if (this.eat(55)) {
  13087. method.kind = "method";
  13088. const isPrivateName = this.match(134);
  13089. this.parseClassElementName(method);
  13090. if (isPrivateName) {
  13091. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  13092. return;
  13093. }
  13094. if (this.isNonstaticConstructor(publicMethod)) {
  13095. this.raise(Errors.ConstructorIsGenerator, {
  13096. at: publicMethod.key
  13097. });
  13098. }
  13099. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  13100. return;
  13101. }
  13102. const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
  13103. const isPrivate = this.match(134);
  13104. const key = this.parseClassElementName(member);
  13105. const maybeQuestionTokenStartLoc = this.state.startLoc;
  13106. this.parsePostMemberNameModifiers(publicMember);
  13107. if (this.isClassMethod()) {
  13108. method.kind = "method";
  13109. if (isPrivate) {
  13110. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13111. return;
  13112. }
  13113. const isConstructor = this.isNonstaticConstructor(publicMethod);
  13114. let allowsDirectSuper = false;
  13115. if (isConstructor) {
  13116. publicMethod.kind = "constructor";
  13117. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  13118. this.raise(Errors.DuplicateConstructor, {
  13119. at: key
  13120. });
  13121. }
  13122. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  13123. this.raise(Errors.OverrideOnConstructor, {
  13124. at: key
  13125. });
  13126. }
  13127. state.hadConstructor = true;
  13128. allowsDirectSuper = state.hadSuperClass;
  13129. }
  13130. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  13131. } else if (this.isClassProperty()) {
  13132. if (isPrivate) {
  13133. this.pushClassPrivateProperty(classBody, privateProp);
  13134. } else {
  13135. this.pushClassProperty(classBody, publicProp);
  13136. }
  13137. } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
  13138. this.resetPreviousNodeTrailingComments(key);
  13139. const isGenerator = this.eat(55);
  13140. if (publicMember.optional) {
  13141. this.unexpected(maybeQuestionTokenStartLoc);
  13142. }
  13143. method.kind = "method";
  13144. const isPrivate = this.match(134);
  13145. this.parseClassElementName(method);
  13146. this.parsePostMemberNameModifiers(publicMember);
  13147. if (isPrivate) {
  13148. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  13149. } else {
  13150. if (this.isNonstaticConstructor(publicMethod)) {
  13151. this.raise(Errors.ConstructorIsAsync, {
  13152. at: publicMethod.key
  13153. });
  13154. }
  13155. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  13156. }
  13157. } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(55) && this.isLineTerminator())) {
  13158. this.resetPreviousNodeTrailingComments(key);
  13159. method.kind = key.name;
  13160. const isPrivate = this.match(134);
  13161. this.parseClassElementName(publicMethod);
  13162. if (isPrivate) {
  13163. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13164. } else {
  13165. if (this.isNonstaticConstructor(publicMethod)) {
  13166. this.raise(Errors.ConstructorIsAccessor, {
  13167. at: publicMethod.key
  13168. });
  13169. }
  13170. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  13171. }
  13172. this.checkGetterSetterParams(publicMethod);
  13173. } else if (isContextual && key.name === "accessor" && !this.isLineTerminator()) {
  13174. this.expectPlugin("decoratorAutoAccessors");
  13175. this.resetPreviousNodeTrailingComments(key);
  13176. const isPrivate = this.match(134);
  13177. this.parseClassElementName(publicProp);
  13178. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  13179. } else if (this.isLineTerminator()) {
  13180. if (isPrivate) {
  13181. this.pushClassPrivateProperty(classBody, privateProp);
  13182. } else {
  13183. this.pushClassProperty(classBody, publicProp);
  13184. }
  13185. } else {
  13186. this.unexpected();
  13187. }
  13188. }
  13189. parseClassElementName(member) {
  13190. const {
  13191. type,
  13192. value
  13193. } = this.state;
  13194. if ((type === 128 || type === 129) && member.static && value === "prototype") {
  13195. this.raise(Errors.StaticPrototype, {
  13196. at: this.state.startLoc
  13197. });
  13198. }
  13199. if (type === 134) {
  13200. if (value === "constructor") {
  13201. this.raise(Errors.ConstructorClassPrivateField, {
  13202. at: this.state.startLoc
  13203. });
  13204. }
  13205. const key = this.parsePrivateName();
  13206. member.key = key;
  13207. return key;
  13208. }
  13209. return this.parsePropertyName(member);
  13210. }
  13211. parseClassStaticBlock(classBody, member) {
  13212. var _member$decorators;
  13213. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  13214. const oldLabels = this.state.labels;
  13215. this.state.labels = [];
  13216. this.prodParam.enter(PARAM);
  13217. const body = member.body = [];
  13218. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  13219. this.prodParam.exit();
  13220. this.scope.exit();
  13221. this.state.labels = oldLabels;
  13222. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13223. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13224. this.raise(Errors.DecoratorStaticBlock, {
  13225. at: member
  13226. });
  13227. }
  13228. }
  13229. pushClassProperty(classBody, prop) {
  13230. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  13231. this.raise(Errors.ConstructorClassField, {
  13232. at: prop.key
  13233. });
  13234. }
  13235. classBody.body.push(this.parseClassProperty(prop));
  13236. }
  13237. pushClassPrivateProperty(classBody, prop) {
  13238. const node = this.parseClassPrivateProperty(prop);
  13239. classBody.body.push(node);
  13240. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13241. }
  13242. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13243. if (!isPrivate && !prop.computed) {
  13244. const key = prop.key;
  13245. if (key.name === "constructor" || key.value === "constructor") {
  13246. this.raise(Errors.ConstructorClassField, {
  13247. at: key
  13248. });
  13249. }
  13250. }
  13251. const node = this.parseClassAccessorProperty(prop);
  13252. classBody.body.push(node);
  13253. if (isPrivate) {
  13254. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13255. }
  13256. }
  13257. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13258. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13259. }
  13260. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13261. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13262. classBody.body.push(node);
  13263. 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;
  13264. this.declareClassPrivateMethodInScope(node, kind);
  13265. }
  13266. declareClassPrivateMethodInScope(node, kind) {
  13267. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13268. }
  13269. parsePostMemberNameModifiers(methodOrProp) {}
  13270. parseClassPrivateProperty(node) {
  13271. this.parseInitializer(node);
  13272. this.semicolon();
  13273. return this.finishNode(node, "ClassPrivateProperty");
  13274. }
  13275. parseClassProperty(node) {
  13276. this.parseInitializer(node);
  13277. this.semicolon();
  13278. return this.finishNode(node, "ClassProperty");
  13279. }
  13280. parseClassAccessorProperty(node) {
  13281. this.parseInitializer(node);
  13282. this.semicolon();
  13283. return this.finishNode(node, "ClassAccessorProperty");
  13284. }
  13285. parseInitializer(node) {
  13286. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  13287. this.expressionScope.enter(newExpressionScope());
  13288. this.prodParam.enter(PARAM);
  13289. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13290. this.expressionScope.exit();
  13291. this.prodParam.exit();
  13292. this.scope.exit();
  13293. }
  13294. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  13295. if (tokenIsIdentifier(this.state.type)) {
  13296. node.id = this.parseIdentifier();
  13297. if (isStatement) {
  13298. this.declareNameFromIdentifier(node.id, bindingType);
  13299. }
  13300. } else {
  13301. if (optionalId || !isStatement) {
  13302. node.id = null;
  13303. } else {
  13304. throw this.raise(Errors.MissingClassName, {
  13305. at: this.state.startLoc
  13306. });
  13307. }
  13308. }
  13309. }
  13310. parseClassSuper(node) {
  13311. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13312. }
  13313. parseExport(node) {
  13314. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  13315. const parseAfterDefault = !hasDefault || this.eat(12);
  13316. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13317. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13318. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13319. const isFromRequired = hasDefault || hasStar;
  13320. if (hasStar && !hasNamespace) {
  13321. if (hasDefault) this.unexpected();
  13322. this.parseExportFrom(node, true);
  13323. return this.finishNode(node, "ExportAllDeclaration");
  13324. }
  13325. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13326. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  13327. throw this.unexpected(null, 5);
  13328. }
  13329. let hasDeclaration;
  13330. if (isFromRequired || hasSpecifiers) {
  13331. hasDeclaration = false;
  13332. this.parseExportFrom(node, isFromRequired);
  13333. } else {
  13334. hasDeclaration = this.maybeParseExportDeclaration(node);
  13335. }
  13336. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13337. this.checkExport(node, true, false, !!node.source);
  13338. return this.finishNode(node, "ExportNamedDeclaration");
  13339. }
  13340. if (this.eat(65)) {
  13341. node.declaration = this.parseExportDefaultExpression();
  13342. this.checkExport(node, true, true);
  13343. return this.finishNode(node, "ExportDefaultDeclaration");
  13344. }
  13345. throw this.unexpected(null, 5);
  13346. }
  13347. eatExportStar(node) {
  13348. return this.eat(55);
  13349. }
  13350. maybeParseExportDefaultSpecifier(node) {
  13351. if (this.isExportDefaultSpecifier()) {
  13352. this.expectPlugin("exportDefaultFrom");
  13353. const specifier = this.startNode();
  13354. specifier.exported = this.parseIdentifier(true);
  13355. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13356. return true;
  13357. }
  13358. return false;
  13359. }
  13360. maybeParseExportNamespaceSpecifier(node) {
  13361. if (this.isContextual(93)) {
  13362. if (!node.specifiers) node.specifiers = [];
  13363. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  13364. this.next();
  13365. specifier.exported = this.parseModuleExportName();
  13366. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13367. return true;
  13368. }
  13369. return false;
  13370. }
  13371. maybeParseExportNamedSpecifiers(node) {
  13372. if (this.match(5)) {
  13373. if (!node.specifiers) node.specifiers = [];
  13374. const isTypeExport = node.exportKind === "type";
  13375. node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13376. node.source = null;
  13377. node.declaration = null;
  13378. if (this.hasPlugin("importAssertions")) {
  13379. node.assertions = [];
  13380. }
  13381. return true;
  13382. }
  13383. return false;
  13384. }
  13385. maybeParseExportDeclaration(node) {
  13386. if (this.shouldParseExportDeclaration()) {
  13387. node.specifiers = [];
  13388. node.source = null;
  13389. if (this.hasPlugin("importAssertions")) {
  13390. node.assertions = [];
  13391. }
  13392. node.declaration = this.parseExportDeclaration(node);
  13393. return true;
  13394. }
  13395. return false;
  13396. }
  13397. isAsyncFunction() {
  13398. if (!this.isContextual(95)) return false;
  13399. const next = this.nextTokenStart();
  13400. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  13401. }
  13402. parseExportDefaultExpression() {
  13403. const expr = this.startNode();
  13404. const isAsync = this.isAsyncFunction();
  13405. if (this.match(68) || isAsync) {
  13406. this.next();
  13407. if (isAsync) {
  13408. this.next();
  13409. }
  13410. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  13411. }
  13412. if (this.match(80)) {
  13413. return this.parseClass(expr, true, true);
  13414. }
  13415. if (this.match(26)) {
  13416. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  13417. this.raise(Errors.DecoratorBeforeExport, {
  13418. at: this.state.startLoc
  13419. });
  13420. }
  13421. this.parseDecorators(false);
  13422. return this.parseClass(expr, true, true);
  13423. }
  13424. if (this.match(75) || this.match(74) || this.isLet()) {
  13425. throw this.raise(Errors.UnsupportedDefaultExport, {
  13426. at: this.state.startLoc
  13427. });
  13428. }
  13429. const res = this.parseMaybeAssignAllowIn();
  13430. this.semicolon();
  13431. return res;
  13432. }
  13433. parseExportDeclaration(node) {
  13434. return this.parseStatement(null);
  13435. }
  13436. isExportDefaultSpecifier() {
  13437. const {
  13438. type
  13439. } = this.state;
  13440. if (tokenIsIdentifier(type)) {
  13441. if (type === 95 && !this.state.containsEsc || type === 99) {
  13442. return false;
  13443. }
  13444. if ((type === 126 || type === 125) && !this.state.containsEsc) {
  13445. const {
  13446. type: nextType
  13447. } = this.lookahead();
  13448. if (tokenIsIdentifier(nextType) && nextType !== 97 || nextType === 5) {
  13449. this.expectOnePlugin(["flow", "typescript"]);
  13450. return false;
  13451. }
  13452. }
  13453. } else if (!this.match(65)) {
  13454. return false;
  13455. }
  13456. const next = this.nextTokenStart();
  13457. const hasFrom = this.isUnparsedContextual(next, "from");
  13458. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13459. return true;
  13460. }
  13461. if (this.match(65) && hasFrom) {
  13462. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13463. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13464. }
  13465. return false;
  13466. }
  13467. parseExportFrom(node, expect) {
  13468. if (this.eatContextual(97)) {
  13469. node.source = this.parseImportSource();
  13470. this.checkExport(node);
  13471. const assertions = this.maybeParseImportAssertions();
  13472. if (assertions) {
  13473. node.assertions = assertions;
  13474. this.checkJSONModuleImport(node);
  13475. }
  13476. } else if (expect) {
  13477. this.unexpected();
  13478. }
  13479. this.semicolon();
  13480. }
  13481. shouldParseExportDeclaration() {
  13482. const {
  13483. type
  13484. } = this.state;
  13485. if (type === 26) {
  13486. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  13487. if (this.hasPlugin("decorators")) {
  13488. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  13489. throw this.raise(Errors.DecoratorBeforeExport, {
  13490. at: this.state.startLoc
  13491. });
  13492. }
  13493. return true;
  13494. }
  13495. }
  13496. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  13497. }
  13498. checkExport(node, checkNames, isDefault, isFrom) {
  13499. if (checkNames) {
  13500. if (isDefault) {
  13501. this.checkDuplicateExports(node, "default");
  13502. if (this.hasPlugin("exportDefaultFrom")) {
  13503. var _declaration$extra;
  13504. const declaration = node.declaration;
  13505. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  13506. this.raise(Errors.ExportDefaultFromAsIdentifier, {
  13507. at: declaration
  13508. });
  13509. }
  13510. }
  13511. } else if (node.specifiers && node.specifiers.length) {
  13512. for (const specifier of node.specifiers) {
  13513. const {
  13514. exported
  13515. } = specifier;
  13516. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  13517. this.checkDuplicateExports(specifier, exportName);
  13518. if (!isFrom && specifier.local) {
  13519. const {
  13520. local
  13521. } = specifier;
  13522. if (local.type !== "Identifier") {
  13523. this.raise(Errors.ExportBindingIsString, {
  13524. at: specifier,
  13525. localName: local.value,
  13526. exportName
  13527. });
  13528. } else {
  13529. this.checkReservedWord(local.name, local.loc.start, true, false);
  13530. this.scope.checkLocalExport(local);
  13531. }
  13532. }
  13533. }
  13534. } else if (node.declaration) {
  13535. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  13536. const id = node.declaration.id;
  13537. if (!id) throw new Error("Assertion failure");
  13538. this.checkDuplicateExports(node, id.name);
  13539. } else if (node.declaration.type === "VariableDeclaration") {
  13540. for (const declaration of node.declaration.declarations) {
  13541. this.checkDeclaration(declaration.id);
  13542. }
  13543. }
  13544. }
  13545. }
  13546. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  13547. if (currentContextDecorators.length) {
  13548. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13549. at: node
  13550. });
  13551. }
  13552. }
  13553. checkDeclaration(node) {
  13554. if (node.type === "Identifier") {
  13555. this.checkDuplicateExports(node, node.name);
  13556. } else if (node.type === "ObjectPattern") {
  13557. for (const prop of node.properties) {
  13558. this.checkDeclaration(prop);
  13559. }
  13560. } else if (node.type === "ArrayPattern") {
  13561. for (const elem of node.elements) {
  13562. if (elem) {
  13563. this.checkDeclaration(elem);
  13564. }
  13565. }
  13566. } else if (node.type === "ObjectProperty") {
  13567. this.checkDeclaration(node.value);
  13568. } else if (node.type === "RestElement") {
  13569. this.checkDeclaration(node.argument);
  13570. } else if (node.type === "AssignmentPattern") {
  13571. this.checkDeclaration(node.left);
  13572. }
  13573. }
  13574. checkDuplicateExports(node, exportName) {
  13575. if (this.exportedIdentifiers.has(exportName)) {
  13576. if (exportName === "default") {
  13577. this.raise(Errors.DuplicateDefaultExport, {
  13578. at: node
  13579. });
  13580. } else {
  13581. this.raise(Errors.DuplicateExport, {
  13582. at: node,
  13583. exportName
  13584. });
  13585. }
  13586. }
  13587. this.exportedIdentifiers.add(exportName);
  13588. }
  13589. parseExportSpecifiers(isInTypeExport) {
  13590. const nodes = [];
  13591. let first = true;
  13592. this.expect(5);
  13593. while (!this.eat(8)) {
  13594. if (first) {
  13595. first = false;
  13596. } else {
  13597. this.expect(12);
  13598. if (this.eat(8)) break;
  13599. }
  13600. const isMaybeTypeOnly = this.isContextual(126);
  13601. const isString = this.match(129);
  13602. const node = this.startNode();
  13603. node.local = this.parseModuleExportName();
  13604. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  13605. }
  13606. return nodes;
  13607. }
  13608. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  13609. if (this.eatContextual(93)) {
  13610. node.exported = this.parseModuleExportName();
  13611. } else if (isString) {
  13612. node.exported = cloneStringLiteral(node.local);
  13613. } else if (!node.exported) {
  13614. node.exported = cloneIdentifier(node.local);
  13615. }
  13616. return this.finishNode(node, "ExportSpecifier");
  13617. }
  13618. parseModuleExportName() {
  13619. if (this.match(129)) {
  13620. const result = this.parseStringLiteral(this.state.value);
  13621. const surrogate = result.value.match(loneSurrogate);
  13622. if (surrogate) {
  13623. this.raise(Errors.ModuleExportNameHasLoneSurrogate, {
  13624. at: result,
  13625. surrogateCharCode: surrogate[0].charCodeAt(0)
  13626. });
  13627. }
  13628. return result;
  13629. }
  13630. return this.parseIdentifier(true);
  13631. }
  13632. isJSONModuleImport(node) {
  13633. if (node.assertions != null) {
  13634. return node.assertions.some(({
  13635. key,
  13636. value
  13637. }) => {
  13638. return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
  13639. });
  13640. }
  13641. return false;
  13642. }
  13643. checkJSONModuleImport(node) {
  13644. if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
  13645. const {
  13646. specifiers
  13647. } = node;
  13648. if (node.specifiers != null) {
  13649. const nonDefaultNamedSpecifier = specifiers.find(specifier => {
  13650. let imported;
  13651. if (specifier.type === "ExportSpecifier") {
  13652. imported = specifier.local;
  13653. } else if (specifier.type === "ImportSpecifier") {
  13654. imported = specifier.imported;
  13655. }
  13656. if (imported !== undefined) {
  13657. return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
  13658. }
  13659. });
  13660. if (nonDefaultNamedSpecifier !== undefined) {
  13661. this.raise(Errors.ImportJSONBindingNotDefault, {
  13662. at: nonDefaultNamedSpecifier.loc.start
  13663. });
  13664. }
  13665. }
  13666. }
  13667. }
  13668. parseImport(node) {
  13669. node.specifiers = [];
  13670. if (!this.match(129)) {
  13671. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  13672. const parseNext = !hasDefault || this.eat(12);
  13673. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  13674. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  13675. this.expectContextual(97);
  13676. }
  13677. node.source = this.parseImportSource();
  13678. const assertions = this.maybeParseImportAssertions();
  13679. if (assertions) {
  13680. node.assertions = assertions;
  13681. } else {
  13682. const attributes = this.maybeParseModuleAttributes();
  13683. if (attributes) {
  13684. node.attributes = attributes;
  13685. }
  13686. }
  13687. this.checkJSONModuleImport(node);
  13688. this.semicolon();
  13689. return this.finishNode(node, "ImportDeclaration");
  13690. }
  13691. parseImportSource() {
  13692. if (!this.match(129)) this.unexpected();
  13693. return this.parseExprAtom();
  13694. }
  13695. shouldParseDefaultImport(node) {
  13696. return tokenIsIdentifier(this.state.type);
  13697. }
  13698. parseImportSpecifierLocal(node, specifier, type) {
  13699. specifier.local = this.parseIdentifier();
  13700. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  13701. }
  13702. finishImportSpecifier(specifier, type) {
  13703. this.checkLVal(specifier.local, {
  13704. in: specifier,
  13705. binding: BIND_LEXICAL
  13706. });
  13707. return this.finishNode(specifier, type);
  13708. }
  13709. parseAssertEntries() {
  13710. const attrs = [];
  13711. const attrNames = new Set();
  13712. do {
  13713. if (this.match(8)) {
  13714. break;
  13715. }
  13716. const node = this.startNode();
  13717. const keyName = this.state.value;
  13718. if (attrNames.has(keyName)) {
  13719. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  13720. at: this.state.startLoc,
  13721. key: keyName
  13722. });
  13723. }
  13724. attrNames.add(keyName);
  13725. if (this.match(129)) {
  13726. node.key = this.parseStringLiteral(keyName);
  13727. } else {
  13728. node.key = this.parseIdentifier(true);
  13729. }
  13730. this.expect(14);
  13731. if (!this.match(129)) {
  13732. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  13733. at: this.state.startLoc
  13734. });
  13735. }
  13736. node.value = this.parseStringLiteral(this.state.value);
  13737. attrs.push(this.finishNode(node, "ImportAttribute"));
  13738. } while (this.eat(12));
  13739. return attrs;
  13740. }
  13741. maybeParseModuleAttributes() {
  13742. if (this.match(76) && !this.hasPrecedingLineBreak()) {
  13743. this.expectPlugin("moduleAttributes");
  13744. this.next();
  13745. } else {
  13746. if (this.hasPlugin("moduleAttributes")) return [];
  13747. return null;
  13748. }
  13749. const attrs = [];
  13750. const attributes = new Set();
  13751. do {
  13752. const node = this.startNode();
  13753. node.key = this.parseIdentifier(true);
  13754. if (node.key.name !== "type") {
  13755. this.raise(Errors.ModuleAttributeDifferentFromType, {
  13756. at: node.key
  13757. });
  13758. }
  13759. if (attributes.has(node.key.name)) {
  13760. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  13761. at: node.key,
  13762. key: node.key.name
  13763. });
  13764. }
  13765. attributes.add(node.key.name);
  13766. this.expect(14);
  13767. if (!this.match(129)) {
  13768. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  13769. at: this.state.startLoc
  13770. });
  13771. }
  13772. node.value = this.parseStringLiteral(this.state.value);
  13773. this.finishNode(node, "ImportAttribute");
  13774. attrs.push(node);
  13775. } while (this.eat(12));
  13776. return attrs;
  13777. }
  13778. maybeParseImportAssertions() {
  13779. if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  13780. this.expectPlugin("importAssertions");
  13781. this.next();
  13782. } else {
  13783. if (this.hasPlugin("importAssertions")) return [];
  13784. return null;
  13785. }
  13786. this.eat(5);
  13787. const attrs = this.parseAssertEntries();
  13788. this.eat(8);
  13789. return attrs;
  13790. }
  13791. maybeParseDefaultImportSpecifier(node) {
  13792. if (this.shouldParseDefaultImport(node)) {
  13793. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  13794. return true;
  13795. }
  13796. return false;
  13797. }
  13798. maybeParseStarImportSpecifier(node) {
  13799. if (this.match(55)) {
  13800. const specifier = this.startNode();
  13801. this.next();
  13802. this.expectContextual(93);
  13803. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  13804. return true;
  13805. }
  13806. return false;
  13807. }
  13808. parseNamedImportSpecifiers(node) {
  13809. let first = true;
  13810. this.expect(5);
  13811. while (!this.eat(8)) {
  13812. if (first) {
  13813. first = false;
  13814. } else {
  13815. if (this.eat(14)) {
  13816. throw this.raise(Errors.DestructureNamedImport, {
  13817. at: this.state.startLoc
  13818. });
  13819. }
  13820. this.expect(12);
  13821. if (this.eat(8)) break;
  13822. }
  13823. const specifier = this.startNode();
  13824. const importedIsString = this.match(129);
  13825. const isMaybeTypeOnly = this.isContextual(126);
  13826. specifier.imported = this.parseModuleExportName();
  13827. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly);
  13828. node.specifiers.push(importSpecifier);
  13829. }
  13830. }
  13831. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  13832. if (this.eatContextual(93)) {
  13833. specifier.local = this.parseIdentifier();
  13834. } else {
  13835. const {
  13836. imported
  13837. } = specifier;
  13838. if (importedIsString) {
  13839. throw this.raise(Errors.ImportBindingIsString, {
  13840. at: specifier,
  13841. importName: imported.value
  13842. });
  13843. }
  13844. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  13845. if (!specifier.local) {
  13846. specifier.local = cloneIdentifier(imported);
  13847. }
  13848. }
  13849. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  13850. }
  13851. isThisParam(param) {
  13852. return param.type === "Identifier" && param.name === "this";
  13853. }
  13854. }
  13855. class Parser extends StatementParser {
  13856. constructor(options, input) {
  13857. options = getOptions(options);
  13858. super(options, input);
  13859. this.options = options;
  13860. this.initializeScopes();
  13861. this.plugins = pluginsMap(this.options.plugins);
  13862. this.filename = options.sourceFilename;
  13863. }
  13864. getScopeHandler() {
  13865. return ScopeHandler;
  13866. }
  13867. parse() {
  13868. this.enterInitialScopes();
  13869. const file = this.startNode();
  13870. const program = this.startNode();
  13871. this.nextToken();
  13872. file.errors = null;
  13873. this.parseTopLevel(file, program);
  13874. file.errors = this.state.errors;
  13875. return file;
  13876. }
  13877. }
  13878. function pluginsMap(plugins) {
  13879. const pluginMap = new Map();
  13880. for (const plugin of plugins) {
  13881. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  13882. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  13883. }
  13884. return pluginMap;
  13885. }
  13886. function parse(input, options) {
  13887. var _options;
  13888. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  13889. options = Object.assign({}, options);
  13890. try {
  13891. options.sourceType = "module";
  13892. const parser = getParser(options, input);
  13893. const ast = parser.parse();
  13894. if (parser.sawUnambiguousESM) {
  13895. return ast;
  13896. }
  13897. if (parser.ambiguousScriptDifferentAst) {
  13898. try {
  13899. options.sourceType = "script";
  13900. return getParser(options, input).parse();
  13901. } catch (_unused) {}
  13902. } else {
  13903. ast.program.sourceType = "script";
  13904. }
  13905. return ast;
  13906. } catch (moduleError) {
  13907. try {
  13908. options.sourceType = "script";
  13909. return getParser(options, input).parse();
  13910. } catch (_unused2) {}
  13911. throw moduleError;
  13912. }
  13913. } else {
  13914. return getParser(options, input).parse();
  13915. }
  13916. }
  13917. function parseExpression(input, options) {
  13918. const parser = getParser(options, input);
  13919. if (parser.options.strictMode) {
  13920. parser.state.strict = true;
  13921. }
  13922. return parser.getExpression();
  13923. }
  13924. function generateExportedTokenTypes(internalTokenTypes) {
  13925. const tokenTypes = {};
  13926. for (const typeName of Object.keys(internalTokenTypes)) {
  13927. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  13928. }
  13929. return tokenTypes;
  13930. }
  13931. const tokTypes = generateExportedTokenTypes(tt);
  13932. function getParser(options, input) {
  13933. let cls = Parser;
  13934. if (options != null && options.plugins) {
  13935. validatePlugins(options.plugins);
  13936. cls = getParserClass(options.plugins);
  13937. }
  13938. return new cls(options, input);
  13939. }
  13940. const parserClassCache = {};
  13941. function getParserClass(pluginsFromOptions) {
  13942. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  13943. const key = pluginList.join("/");
  13944. let cls = parserClassCache[key];
  13945. if (!cls) {
  13946. cls = Parser;
  13947. for (const plugin of pluginList) {
  13948. cls = mixinPlugins[plugin](cls);
  13949. }
  13950. parserClassCache[key] = cls;
  13951. }
  13952. return cls;
  13953. }
  13954. exports.parse = parse;
  13955. exports.parseExpression = parseExpression;
  13956. exports.tokTypes = tokTypes;
  13957. //# sourceMappingURL=index.js.map