index.js 435 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  4. const lineBreakG = new RegExp(lineBreak.source, "g");
  5. function isNewLine(code) {
  6. switch (code) {
  7. case 10:
  8. case 13:
  9. case 8232:
  10. case 8233:
  11. return true;
  12. default:
  13. return false;
  14. }
  15. }
  16. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  17. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y;
  18. const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
  19. function isWhitespace(code) {
  20. switch (code) {
  21. case 0x0009:
  22. case 0x000b:
  23. case 0x000c:
  24. case 32:
  25. case 160:
  26. case 5760:
  27. case 0x2000:
  28. case 0x2001:
  29. case 0x2002:
  30. case 0x2003:
  31. case 0x2004:
  32. case 0x2005:
  33. case 0x2006:
  34. case 0x2007:
  35. case 0x2008:
  36. case 0x2009:
  37. case 0x200a:
  38. case 0x202f:
  39. case 0x205f:
  40. case 0x3000:
  41. case 0xfeff:
  42. return true;
  43. default:
  44. return false;
  45. }
  46. }
  47. class Position {
  48. constructor(line, col) {
  49. this.line = void 0;
  50. this.column = void 0;
  51. this.line = line;
  52. this.column = col;
  53. }
  54. }
  55. class SourceLocation {
  56. constructor(start, end) {
  57. this.start = void 0;
  58. this.end = void 0;
  59. this.filename = void 0;
  60. this.identifierName = void 0;
  61. this.start = start;
  62. this.end = end;
  63. }
  64. }
  65. function getLineInfo(input, offset) {
  66. let line = 1;
  67. let lineStart = 0;
  68. let match;
  69. lineBreakG.lastIndex = 0;
  70. while ((match = lineBreakG.exec(input)) && match.index < offset) {
  71. line++;
  72. lineStart = lineBreakG.lastIndex;
  73. }
  74. return new Position(line, offset - lineStart);
  75. }
  76. function createPositionWithColumnOffset(position, columnOffset) {
  77. const {
  78. line,
  79. column
  80. } = position;
  81. return new Position(line, column + columnOffset);
  82. }
  83. class BaseParser {
  84. constructor() {
  85. this.sawUnambiguousESM = false;
  86. this.ambiguousScriptDifferentAst = false;
  87. }
  88. hasPlugin(pluginConfig) {
  89. if (typeof pluginConfig === "string") {
  90. return this.plugins.has(pluginConfig);
  91. } else {
  92. const [pluginName, pluginOptions] = pluginConfig;
  93. if (!this.hasPlugin(pluginName)) {
  94. return false;
  95. }
  96. const actualOptions = this.plugins.get(pluginName);
  97. for (const key of Object.keys(pluginOptions)) {
  98. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. }
  105. getPluginOption(plugin, name) {
  106. var _this$plugins$get;
  107. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  108. }
  109. }
  110. function setTrailingComments(node, comments) {
  111. if (node.trailingComments === undefined) {
  112. node.trailingComments = comments;
  113. } else {
  114. node.trailingComments.unshift(...comments);
  115. }
  116. }
  117. function setLeadingComments(node, comments) {
  118. if (node.leadingComments === undefined) {
  119. node.leadingComments = comments;
  120. } else {
  121. node.leadingComments.unshift(...comments);
  122. }
  123. }
  124. function setInnerComments(node, comments) {
  125. if (node.innerComments === undefined) {
  126. node.innerComments = comments;
  127. } else {
  128. node.innerComments.unshift(...comments);
  129. }
  130. }
  131. function adjustInnerComments(node, elements, commentWS) {
  132. let lastElement = null;
  133. let i = elements.length;
  134. while (lastElement === null && i > 0) {
  135. lastElement = elements[--i];
  136. }
  137. if (lastElement === null || lastElement.start > commentWS.start) {
  138. setInnerComments(node, commentWS.comments);
  139. } else {
  140. setTrailingComments(lastElement, commentWS.comments);
  141. }
  142. }
  143. class CommentsParser extends BaseParser {
  144. addComment(comment) {
  145. if (this.filename) comment.loc.filename = this.filename;
  146. this.state.comments.push(comment);
  147. }
  148. processComment(node) {
  149. const {
  150. commentStack
  151. } = this.state;
  152. const commentStackLength = commentStack.length;
  153. if (commentStackLength === 0) return;
  154. let i = commentStackLength - 1;
  155. const lastCommentWS = commentStack[i];
  156. if (lastCommentWS.start === node.end) {
  157. lastCommentWS.leadingNode = node;
  158. i--;
  159. }
  160. const {
  161. start: nodeStart
  162. } = node;
  163. for (; i >= 0; i--) {
  164. const commentWS = commentStack[i];
  165. const commentEnd = commentWS.end;
  166. if (commentEnd > nodeStart) {
  167. commentWS.containingNode = node;
  168. this.finalizeComment(commentWS);
  169. commentStack.splice(i, 1);
  170. } else {
  171. if (commentEnd === nodeStart) {
  172. commentWS.trailingNode = node;
  173. }
  174. break;
  175. }
  176. }
  177. }
  178. finalizeComment(commentWS) {
  179. const {
  180. comments
  181. } = commentWS;
  182. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  183. if (commentWS.leadingNode !== null) {
  184. setTrailingComments(commentWS.leadingNode, comments);
  185. }
  186. if (commentWS.trailingNode !== null) {
  187. setLeadingComments(commentWS.trailingNode, comments);
  188. }
  189. } else {
  190. const {
  191. containingNode: node,
  192. start: commentStart
  193. } = commentWS;
  194. if (this.input.charCodeAt(commentStart - 1) === 44) {
  195. switch (node.type) {
  196. case "ObjectExpression":
  197. case "ObjectPattern":
  198. case "RecordExpression":
  199. adjustInnerComments(node, node.properties, commentWS);
  200. break;
  201. case "CallExpression":
  202. case "OptionalCallExpression":
  203. adjustInnerComments(node, node.arguments, commentWS);
  204. break;
  205. case "FunctionDeclaration":
  206. case "FunctionExpression":
  207. case "ArrowFunctionExpression":
  208. case "ObjectMethod":
  209. case "ClassMethod":
  210. case "ClassPrivateMethod":
  211. adjustInnerComments(node, node.params, commentWS);
  212. break;
  213. case "ArrayExpression":
  214. case "ArrayPattern":
  215. case "TupleExpression":
  216. adjustInnerComments(node, node.elements, commentWS);
  217. break;
  218. case "ExportNamedDeclaration":
  219. case "ImportDeclaration":
  220. adjustInnerComments(node, node.specifiers, commentWS);
  221. break;
  222. default:
  223. {
  224. setInnerComments(node, comments);
  225. }
  226. }
  227. } else {
  228. setInnerComments(node, comments);
  229. }
  230. }
  231. }
  232. finalizeRemainingComments() {
  233. const {
  234. commentStack
  235. } = this.state;
  236. for (let i = commentStack.length - 1; i >= 0; i--) {
  237. this.finalizeComment(commentStack[i]);
  238. }
  239. this.state.commentStack = [];
  240. }
  241. resetPreviousNodeTrailingComments(node) {
  242. const {
  243. commentStack
  244. } = this.state;
  245. const {
  246. length
  247. } = commentStack;
  248. if (length === 0) return;
  249. const commentWS = commentStack[length - 1];
  250. if (commentWS.leadingNode === node) {
  251. commentWS.leadingNode = null;
  252. }
  253. }
  254. takeSurroundingComments(node, start, end) {
  255. const {
  256. commentStack
  257. } = this.state;
  258. const commentStackLength = commentStack.length;
  259. if (commentStackLength === 0) return;
  260. let i = commentStackLength - 1;
  261. for (; i >= 0; i--) {
  262. const commentWS = commentStack[i];
  263. const commentEnd = commentWS.end;
  264. const commentStart = commentWS.start;
  265. if (commentStart === end) {
  266. commentWS.leadingNode = node;
  267. } else if (commentEnd === start) {
  268. commentWS.trailingNode = node;
  269. } else if (commentEnd < start) {
  270. break;
  271. }
  272. }
  273. }
  274. }
  275. const ErrorCodes = Object.freeze({
  276. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  277. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  278. });
  279. const ErrorMessages = makeErrorTemplates({
  280. AccessorIsGenerator: "A %0ter cannot be a generator.",
  281. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  282. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  283. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  284. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  285. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  286. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  287. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  288. BadGetterArity: "A 'get' accesor must not have any formal parameters.",
  289. BadSetterArity: "A 'set' accesor must have exactly one formal parameter.",
  290. BadSetterRestParameter: "A 'set' accesor function argument must not be a rest parameter.",
  291. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  292. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  293. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  294. ConstructorIsAsync: "Constructor can't be an async function.",
  295. ConstructorIsGenerator: "Constructor can't be a generator.",
  296. DeclarationMissingInitializer: "'%0' require an initialization value.",
  297. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax.",
  298. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  299. DecoratorExportClass: "Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead.",
  300. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  301. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  302. DeletePrivateField: "Deleting a private field is not allowed.",
  303. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  304. DuplicateConstructor: "Duplicate constructor in the same class.",
  305. DuplicateDefaultExport: "Only one default export allowed per module.",
  306. DuplicateExport: "`%0` has already been exported. Exported identifiers must be unique.",
  307. DuplicateProto: "Redefinition of __proto__ property.",
  308. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  309. ElementAfterRest: "Rest element must be last element.",
  310. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  311. ExportBindingIsString: "A string literal cannot be used as an exported binding without `from`.\n- Did you mean `export { '%0' as '%1' } from 'some-module'`?",
  312. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  313. ForInOfLoopInitializer: "'%0' loop variable declaration may not have an initializer.",
  314. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  315. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  316. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  317. IllegalBreakContinue: "Unsyntactic %0.",
  318. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  319. IllegalReturn: "'return' outside of function.",
  320. ImportBindingIsString: 'A string literal cannot be used as an imported binding.\n- Did you mean `import { "%0" as foo }`?',
  321. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  322. ImportCallArity: "`import()` requires exactly %0.",
  323. ImportCallNotNewExpression: "Cannot use new with import(...).",
  324. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  325. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  326. InvalidCodePoint: "Code point out of bounds.",
  327. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  328. InvalidDecimal: "Invalid decimal.",
  329. InvalidDigit: "Expected number in radix %0.",
  330. InvalidEscapeSequence: "Bad character escape sequence.",
  331. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  332. InvalidEscapedReservedWord: "Escape sequence in keyword %0.",
  333. InvalidIdentifier: "Invalid identifier %0.",
  334. InvalidLhs: "Invalid left-hand side in %0.",
  335. InvalidLhsBinding: "Binding invalid left-hand side in %0.",
  336. InvalidNumber: "Invalid number.",
  337. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  338. InvalidOrUnexpectedToken: "Unexpected character '%0'.",
  339. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  340. InvalidPrivateFieldResolution: "Private name #%0 is not defined.",
  341. InvalidPropertyBindingPattern: "Binding member expression.",
  342. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  343. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  344. LabelRedeclaration: "Label '%0' is already declared.",
  345. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  346. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  347. MalformedRegExpFlags: "Invalid regular expression flag.",
  348. MissingClassName: "A class name is required.",
  349. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  350. MissingSemicolon: "Missing semicolon.",
  351. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  352. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  353. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  354. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  355. ModuleAttributesWithDuplicateKeys: 'Duplicate key "%0" is not allowed in module attributes.',
  356. ModuleExportNameHasLoneSurrogate: "An export name cannot include a lone surrogate, found '\\u%0'.",
  357. ModuleExportUndefined: "Export '%0' is not defined.",
  358. MultipleDefaultsInSwitch: "Multiple default clauses.",
  359. NewlineAfterThrow: "Illegal newline after throw.",
  360. NoCatchOrFinally: "Missing catch or finally clause.",
  361. NumberIdentifier: "Identifier directly after number.",
  362. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  363. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  364. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  365. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  366. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  367. ParamDupe: "Argument name clash.",
  368. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  369. PatternHasMethod: "Object pattern can't contain methods.",
  370. PipeBodyIsTighter: "Unexpected %0 after pipeline body; any %0 expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  371. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  372. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  373. PipeTopicUnconfiguredToken: 'Invalid topic token %0. In order to use %0 as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "%0" }.',
  374. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  375. PipeUnparenthesizedBody: "Hack-style pipe body cannot be an unparenthesized %0 expression; please wrap it in parentheses.",
  376. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  377. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  378. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  379. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  380. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  381. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  382. PrivateInExpectedIn: "Private names are only allowed in property accesses (`obj.#%0`) or in `in` expressions (`#%0 in obj`).",
  383. PrivateNameRedeclaration: "Duplicate private name #%0.",
  384. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  385. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  386. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  387. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  388. RestTrailingComma: "Unexpected trailing comma after rest element.",
  389. 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.",
  390. StaticPrototype: "Classes may not have static property named prototype.",
  391. StrictDelete: "Deleting local variable in strict mode.",
  392. StrictEvalArguments: "Assigning to '%0' in strict mode.",
  393. StrictEvalArgumentsBinding: "Binding '%0' in strict mode.",
  394. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  395. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  396. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  397. StrictWith: "'with' in strict mode.",
  398. 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?",
  399. SuperPrivateField: "Private fields can't be accessed on super.",
  400. TrailingDecorator: "Decorators must be attached to a class element.",
  401. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  402. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  403. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  404. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  405. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  406. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  407. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  408. UnexpectedKeyword: "Unexpected keyword '%0'.",
  409. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  410. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  411. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  412. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  413. UnexpectedPrivateField: "Unexpected private name.",
  414. UnexpectedReservedWord: "Unexpected reserved word '%0'.",
  415. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  416. UnexpectedToken: "Unexpected token '%0'.",
  417. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  418. UnsupportedBind: "Binding should be performed on object property.",
  419. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  420. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  421. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  422. UnsupportedMetaProperty: "The only valid meta property for %0 is %0.%1.",
  423. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  424. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  425. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  426. UnterminatedComment: "Unterminated comment.",
  427. UnterminatedRegExp: "Unterminated regular expression.",
  428. UnterminatedString: "Unterminated string constant.",
  429. UnterminatedTemplate: "Unterminated template.",
  430. VarRedeclaration: "Identifier '%0' has already been declared.",
  431. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  432. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  433. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  434. }, ErrorCodes.SyntaxError);
  435. const SourceTypeModuleErrorMessages = makeErrorTemplates({
  436. ImportMetaOutsideModule: `import.meta may appear only with 'sourceType: "module"'`,
  437. ImportOutsideModule: `'import' and 'export' may appear only with 'sourceType: "module"'`
  438. }, ErrorCodes.SourceTypeModuleError);
  439. function keepReasonCodeCompat(reasonCode, syntaxPlugin) {
  440. {
  441. if (syntaxPlugin === "flow" && reasonCode === "PatternIsOptional") {
  442. return "OptionalBindingPattern";
  443. }
  444. }
  445. return reasonCode;
  446. }
  447. function makeErrorTemplates(messages, code, syntaxPlugin) {
  448. const templates = {};
  449. Object.keys(messages).forEach(reasonCode => {
  450. templates[reasonCode] = Object.freeze({
  451. code,
  452. reasonCode: keepReasonCodeCompat(reasonCode, syntaxPlugin),
  453. template: messages[reasonCode]
  454. });
  455. });
  456. return Object.freeze(templates);
  457. }
  458. class ParserError extends CommentsParser {
  459. getLocationForPosition(pos) {
  460. let loc;
  461. if (pos === this.state.start) loc = this.state.startLoc;else if (pos === this.state.lastTokStart) loc = this.state.lastTokStartLoc;else if (pos === this.state.end) loc = this.state.endLoc;else if (pos === this.state.lastTokEnd) loc = this.state.lastTokEndLoc;else loc = getLineInfo(this.input, pos);
  462. return loc;
  463. }
  464. raise(pos, {
  465. code,
  466. reasonCode,
  467. template
  468. }, ...params) {
  469. return this.raiseWithData(pos, {
  470. code,
  471. reasonCode
  472. }, template, ...params);
  473. }
  474. raiseOverwrite(pos, {
  475. code,
  476. template
  477. }, ...params) {
  478. const loc = this.getLocationForPosition(pos);
  479. const message = template.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  480. if (this.options.errorRecovery) {
  481. const errors = this.state.errors;
  482. for (let i = errors.length - 1; i >= 0; i--) {
  483. const error = errors[i];
  484. if (error.pos === pos) {
  485. return Object.assign(error, {
  486. message
  487. });
  488. } else if (error.pos < pos) {
  489. break;
  490. }
  491. }
  492. }
  493. return this._raise({
  494. code,
  495. loc,
  496. pos
  497. }, message);
  498. }
  499. raiseWithData(pos, data, errorTemplate, ...params) {
  500. const loc = this.getLocationForPosition(pos);
  501. const message = errorTemplate.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  502. return this._raise(Object.assign({
  503. loc,
  504. pos
  505. }, data), message);
  506. }
  507. _raise(errorContext, message) {
  508. const err = new SyntaxError(message);
  509. Object.assign(err, errorContext);
  510. if (this.options.errorRecovery) {
  511. if (!this.isLookahead) this.state.errors.push(err);
  512. return err;
  513. } else {
  514. throw err;
  515. }
  516. }
  517. }
  518. var estree = (superClass => class extends superClass {
  519. parseRegExpLiteral({
  520. pattern,
  521. flags
  522. }) {
  523. let regex = null;
  524. try {
  525. regex = new RegExp(pattern, flags);
  526. } catch (e) {}
  527. const node = this.estreeParseLiteral(regex);
  528. node.regex = {
  529. pattern,
  530. flags
  531. };
  532. return node;
  533. }
  534. parseBigIntLiteral(value) {
  535. let bigInt;
  536. try {
  537. bigInt = BigInt(value);
  538. } catch (_unused) {
  539. bigInt = null;
  540. }
  541. const node = this.estreeParseLiteral(bigInt);
  542. node.bigint = String(node.value || value);
  543. return node;
  544. }
  545. parseDecimalLiteral(value) {
  546. const decimal = null;
  547. const node = this.estreeParseLiteral(decimal);
  548. node.decimal = String(node.value || value);
  549. return node;
  550. }
  551. estreeParseLiteral(value) {
  552. return this.parseLiteral(value, "Literal");
  553. }
  554. parseStringLiteral(value) {
  555. return this.estreeParseLiteral(value);
  556. }
  557. parseNumericLiteral(value) {
  558. return this.estreeParseLiteral(value);
  559. }
  560. parseNullLiteral() {
  561. return this.estreeParseLiteral(null);
  562. }
  563. parseBooleanLiteral(value) {
  564. return this.estreeParseLiteral(value);
  565. }
  566. directiveToStmt(directive) {
  567. const directiveLiteral = directive.value;
  568. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  569. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  570. expression.value = directiveLiteral.extra.expressionValue;
  571. expression.raw = directiveLiteral.extra.raw;
  572. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
  573. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  574. return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
  575. }
  576. initFunction(node, isAsync) {
  577. super.initFunction(node, isAsync);
  578. node.expression = false;
  579. }
  580. checkDeclaration(node) {
  581. if (node != null && this.isObjectProperty(node)) {
  582. this.checkDeclaration(node.value);
  583. } else {
  584. super.checkDeclaration(node);
  585. }
  586. }
  587. getObjectOrClassMethodParams(method) {
  588. return method.value.params;
  589. }
  590. isValidDirective(stmt) {
  591. var _stmt$expression$extr;
  592. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  593. }
  594. parseBlockBody(node, ...args) {
  595. super.parseBlockBody(node, ...args);
  596. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  597. node.body = directiveStatements.concat(node.body);
  598. delete node.directives;
  599. }
  600. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  601. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  602. if (method.typeParameters) {
  603. method.value.typeParameters = method.typeParameters;
  604. delete method.typeParameters;
  605. }
  606. classBody.body.push(method);
  607. }
  608. parsePrivateName() {
  609. const node = super.parsePrivateName();
  610. {
  611. if (!this.getPluginOption("estree", "classFeatures")) {
  612. return node;
  613. }
  614. }
  615. return this.convertPrivateNameToPrivateIdentifier(node);
  616. }
  617. convertPrivateNameToPrivateIdentifier(node) {
  618. const name = super.getPrivateNameSV(node);
  619. node = node;
  620. delete node.id;
  621. node.name = name;
  622. node.type = "PrivateIdentifier";
  623. return node;
  624. }
  625. isPrivateName(node) {
  626. {
  627. if (!this.getPluginOption("estree", "classFeatures")) {
  628. return super.isPrivateName(node);
  629. }
  630. }
  631. return node.type === "PrivateIdentifier";
  632. }
  633. getPrivateNameSV(node) {
  634. {
  635. if (!this.getPluginOption("estree", "classFeatures")) {
  636. return super.getPrivateNameSV(node);
  637. }
  638. }
  639. return node.name;
  640. }
  641. parseLiteral(value, type) {
  642. const node = super.parseLiteral(value, type);
  643. node.raw = node.extra.raw;
  644. delete node.extra;
  645. return node;
  646. }
  647. parseFunctionBody(node, allowExpression, isMethod = false) {
  648. super.parseFunctionBody(node, allowExpression, isMethod);
  649. node.expression = node.body.type !== "BlockStatement";
  650. }
  651. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  652. let funcNode = this.startNode();
  653. funcNode.kind = node.kind;
  654. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  655. funcNode.type = "FunctionExpression";
  656. delete funcNode.kind;
  657. node.value = funcNode;
  658. if (type === "ClassPrivateMethod") {
  659. node.computed = false;
  660. }
  661. type = "MethodDefinition";
  662. return this.finishNode(node, type);
  663. }
  664. parseClassProperty(...args) {
  665. const propertyNode = super.parseClassProperty(...args);
  666. {
  667. if (!this.getPluginOption("estree", "classFeatures")) {
  668. return propertyNode;
  669. }
  670. }
  671. propertyNode.type = "PropertyDefinition";
  672. return propertyNode;
  673. }
  674. parseClassPrivateProperty(...args) {
  675. const propertyNode = super.parseClassPrivateProperty(...args);
  676. {
  677. if (!this.getPluginOption("estree", "classFeatures")) {
  678. return propertyNode;
  679. }
  680. }
  681. propertyNode.type = "PropertyDefinition";
  682. propertyNode.computed = false;
  683. return propertyNode;
  684. }
  685. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  686. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  687. if (node) {
  688. node.type = "Property";
  689. if (node.kind === "method") node.kind = "init";
  690. node.shorthand = false;
  691. }
  692. return node;
  693. }
  694. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  695. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  696. if (node) {
  697. node.kind = "init";
  698. node.type = "Property";
  699. }
  700. return node;
  701. }
  702. isAssignable(node, isBinding) {
  703. if (node != null && this.isObjectProperty(node)) {
  704. return this.isAssignable(node.value, isBinding);
  705. }
  706. return super.isAssignable(node, isBinding);
  707. }
  708. toAssignable(node, isLHS = false) {
  709. if (node != null && this.isObjectProperty(node)) {
  710. this.toAssignable(node.value, isLHS);
  711. return node;
  712. }
  713. return super.toAssignable(node, isLHS);
  714. }
  715. toAssignableObjectExpressionProp(prop, ...args) {
  716. if (prop.kind === "get" || prop.kind === "set") {
  717. this.raise(prop.key.start, ErrorMessages.PatternHasAccessor);
  718. } else if (prop.method) {
  719. this.raise(prop.key.start, ErrorMessages.PatternHasMethod);
  720. } else {
  721. super.toAssignableObjectExpressionProp(prop, ...args);
  722. }
  723. }
  724. finishCallExpression(node, optional) {
  725. super.finishCallExpression(node, optional);
  726. if (node.callee.type === "Import") {
  727. node.type = "ImportExpression";
  728. node.source = node.arguments[0];
  729. if (this.hasPlugin("importAssertions")) {
  730. var _node$arguments$;
  731. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  732. }
  733. delete node.arguments;
  734. delete node.callee;
  735. }
  736. return node;
  737. }
  738. toReferencedArguments(node) {
  739. if (node.type === "ImportExpression") {
  740. return;
  741. }
  742. super.toReferencedArguments(node);
  743. }
  744. parseExport(node) {
  745. super.parseExport(node);
  746. switch (node.type) {
  747. case "ExportAllDeclaration":
  748. node.exported = null;
  749. break;
  750. case "ExportNamedDeclaration":
  751. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  752. node.type = "ExportAllDeclaration";
  753. node.exported = node.specifiers[0].exported;
  754. delete node.specifiers;
  755. }
  756. break;
  757. }
  758. return node;
  759. }
  760. parseSubscript(base, startPos, startLoc, noCalls, state) {
  761. const node = super.parseSubscript(base, startPos, startLoc, noCalls, state);
  762. if (state.optionalChainMember) {
  763. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  764. node.type = node.type.substring(8);
  765. }
  766. if (state.stop) {
  767. const chain = this.startNodeAtNode(node);
  768. chain.expression = node;
  769. return this.finishNode(chain, "ChainExpression");
  770. }
  771. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  772. node.optional = false;
  773. }
  774. return node;
  775. }
  776. hasPropertyAsPrivateName(node) {
  777. if (node.type === "ChainExpression") {
  778. node = node.expression;
  779. }
  780. return super.hasPropertyAsPrivateName(node);
  781. }
  782. isOptionalChain(node) {
  783. return node.type === "ChainExpression";
  784. }
  785. isObjectProperty(node) {
  786. return node.type === "Property" && node.kind === "init" && !node.method;
  787. }
  788. isObjectMethod(node) {
  789. return node.method || node.kind === "get" || node.kind === "set";
  790. }
  791. });
  792. class TokContext {
  793. constructor(token, preserveSpace) {
  794. this.token = void 0;
  795. this.preserveSpace = void 0;
  796. this.token = token;
  797. this.preserveSpace = !!preserveSpace;
  798. }
  799. }
  800. const types = {
  801. brace: new TokContext("{"),
  802. j_oTag: new TokContext("<tag"),
  803. j_cTag: new TokContext("</tag"),
  804. j_expr: new TokContext("<tag>...</tag>", true)
  805. };
  806. {
  807. types.template = new TokContext("`", true);
  808. }
  809. const beforeExpr = true;
  810. const startsExpr = true;
  811. const isLoop = true;
  812. const isAssign = true;
  813. const prefix = true;
  814. const postfix = true;
  815. class ExportedTokenType {
  816. constructor(label, conf = {}) {
  817. this.label = void 0;
  818. this.keyword = void 0;
  819. this.beforeExpr = void 0;
  820. this.startsExpr = void 0;
  821. this.rightAssociative = void 0;
  822. this.isLoop = void 0;
  823. this.isAssign = void 0;
  824. this.prefix = void 0;
  825. this.postfix = void 0;
  826. this.binop = void 0;
  827. this.label = label;
  828. this.keyword = conf.keyword;
  829. this.beforeExpr = !!conf.beforeExpr;
  830. this.startsExpr = !!conf.startsExpr;
  831. this.rightAssociative = !!conf.rightAssociative;
  832. this.isLoop = !!conf.isLoop;
  833. this.isAssign = !!conf.isAssign;
  834. this.prefix = !!conf.prefix;
  835. this.postfix = !!conf.postfix;
  836. this.binop = conf.binop != null ? conf.binop : null;
  837. {
  838. this.updateContext = null;
  839. }
  840. }
  841. }
  842. const keywords$1 = new Map();
  843. function createKeyword(name, options = {}) {
  844. options.keyword = name;
  845. const token = createToken(name, options);
  846. keywords$1.set(name, token);
  847. return token;
  848. }
  849. function createBinop(name, binop) {
  850. return createToken(name, {
  851. beforeExpr,
  852. binop
  853. });
  854. }
  855. let tokenTypeCounter = -1;
  856. const tokenTypes = [];
  857. const tokenLabels = [];
  858. const tokenBinops = [];
  859. const tokenBeforeExprs = [];
  860. const tokenStartsExprs = [];
  861. const tokenPrefixes = [];
  862. function createToken(name, options = {}) {
  863. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  864. ++tokenTypeCounter;
  865. tokenLabels.push(name);
  866. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  867. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  868. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  869. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  870. tokenTypes.push(new ExportedTokenType(name, options));
  871. return tokenTypeCounter;
  872. }
  873. function createKeywordLike(name, options = {}) {
  874. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  875. ++tokenTypeCounter;
  876. keywords$1.set(name, tokenTypeCounter);
  877. tokenLabels.push(name);
  878. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  879. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  880. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  881. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  882. tokenTypes.push(new ExportedTokenType("name", options));
  883. return tokenTypeCounter;
  884. }
  885. const tt = {
  886. bracketL: createToken("[", {
  887. beforeExpr,
  888. startsExpr
  889. }),
  890. bracketHashL: createToken("#[", {
  891. beforeExpr,
  892. startsExpr
  893. }),
  894. bracketBarL: createToken("[|", {
  895. beforeExpr,
  896. startsExpr
  897. }),
  898. bracketR: createToken("]"),
  899. bracketBarR: createToken("|]"),
  900. braceL: createToken("{", {
  901. beforeExpr,
  902. startsExpr
  903. }),
  904. braceBarL: createToken("{|", {
  905. beforeExpr,
  906. startsExpr
  907. }),
  908. braceHashL: createToken("#{", {
  909. beforeExpr,
  910. startsExpr
  911. }),
  912. braceR: createToken("}", {
  913. beforeExpr
  914. }),
  915. braceBarR: createToken("|}"),
  916. parenL: createToken("(", {
  917. beforeExpr,
  918. startsExpr
  919. }),
  920. parenR: createToken(")"),
  921. comma: createToken(",", {
  922. beforeExpr
  923. }),
  924. semi: createToken(";", {
  925. beforeExpr
  926. }),
  927. colon: createToken(":", {
  928. beforeExpr
  929. }),
  930. doubleColon: createToken("::", {
  931. beforeExpr
  932. }),
  933. dot: createToken("."),
  934. question: createToken("?", {
  935. beforeExpr
  936. }),
  937. questionDot: createToken("?."),
  938. arrow: createToken("=>", {
  939. beforeExpr
  940. }),
  941. template: createToken("template"),
  942. ellipsis: createToken("...", {
  943. beforeExpr
  944. }),
  945. backQuote: createToken("`", {
  946. startsExpr
  947. }),
  948. dollarBraceL: createToken("${", {
  949. beforeExpr,
  950. startsExpr
  951. }),
  952. templateTail: createToken("...`", {
  953. startsExpr
  954. }),
  955. templateNonTail: createToken("...${", {
  956. beforeExpr,
  957. startsExpr
  958. }),
  959. at: createToken("@"),
  960. hash: createToken("#", {
  961. startsExpr
  962. }),
  963. interpreterDirective: createToken("#!..."),
  964. eq: createToken("=", {
  965. beforeExpr,
  966. isAssign
  967. }),
  968. assign: createToken("_=", {
  969. beforeExpr,
  970. isAssign
  971. }),
  972. slashAssign: createToken("_=", {
  973. beforeExpr,
  974. isAssign
  975. }),
  976. xorAssign: createToken("_=", {
  977. beforeExpr,
  978. isAssign
  979. }),
  980. moduloAssign: createToken("_=", {
  981. beforeExpr,
  982. isAssign
  983. }),
  984. incDec: createToken("++/--", {
  985. prefix,
  986. postfix,
  987. startsExpr
  988. }),
  989. bang: createToken("!", {
  990. beforeExpr,
  991. prefix,
  992. startsExpr
  993. }),
  994. tilde: createToken("~", {
  995. beforeExpr,
  996. prefix,
  997. startsExpr
  998. }),
  999. pipeline: createBinop("|>", 0),
  1000. nullishCoalescing: createBinop("??", 1),
  1001. logicalOR: createBinop("||", 1),
  1002. logicalAND: createBinop("&&", 2),
  1003. bitwiseOR: createBinop("|", 3),
  1004. bitwiseXOR: createBinop("^", 4),
  1005. bitwiseAND: createBinop("&", 5),
  1006. equality: createBinop("==/!=/===/!==", 6),
  1007. lt: createBinop("</>/<=/>=", 7),
  1008. gt: createBinop("</>/<=/>=", 7),
  1009. relational: createBinop("</>/<=/>=", 7),
  1010. bitShift: createBinop("<</>>/>>>", 8),
  1011. plusMin: createToken("+/-", {
  1012. beforeExpr,
  1013. binop: 9,
  1014. prefix,
  1015. startsExpr
  1016. }),
  1017. modulo: createToken("%", {
  1018. binop: 10,
  1019. startsExpr
  1020. }),
  1021. star: createToken("*", {
  1022. binop: 10
  1023. }),
  1024. slash: createBinop("/", 10),
  1025. exponent: createToken("**", {
  1026. beforeExpr,
  1027. binop: 11,
  1028. rightAssociative: true
  1029. }),
  1030. _in: createKeyword("in", {
  1031. beforeExpr,
  1032. binop: 7
  1033. }),
  1034. _instanceof: createKeyword("instanceof", {
  1035. beforeExpr,
  1036. binop: 7
  1037. }),
  1038. _break: createKeyword("break"),
  1039. _case: createKeyword("case", {
  1040. beforeExpr
  1041. }),
  1042. _catch: createKeyword("catch"),
  1043. _continue: createKeyword("continue"),
  1044. _debugger: createKeyword("debugger"),
  1045. _default: createKeyword("default", {
  1046. beforeExpr
  1047. }),
  1048. _else: createKeyword("else", {
  1049. beforeExpr
  1050. }),
  1051. _finally: createKeyword("finally"),
  1052. _function: createKeyword("function", {
  1053. startsExpr
  1054. }),
  1055. _if: createKeyword("if"),
  1056. _return: createKeyword("return", {
  1057. beforeExpr
  1058. }),
  1059. _switch: createKeyword("switch"),
  1060. _throw: createKeyword("throw", {
  1061. beforeExpr,
  1062. prefix,
  1063. startsExpr
  1064. }),
  1065. _try: createKeyword("try"),
  1066. _var: createKeyword("var"),
  1067. _const: createKeyword("const"),
  1068. _with: createKeyword("with"),
  1069. _new: createKeyword("new", {
  1070. beforeExpr,
  1071. startsExpr
  1072. }),
  1073. _this: createKeyword("this", {
  1074. startsExpr
  1075. }),
  1076. _super: createKeyword("super", {
  1077. startsExpr
  1078. }),
  1079. _class: createKeyword("class", {
  1080. startsExpr
  1081. }),
  1082. _extends: createKeyword("extends", {
  1083. beforeExpr
  1084. }),
  1085. _export: createKeyword("export"),
  1086. _import: createKeyword("import", {
  1087. startsExpr
  1088. }),
  1089. _null: createKeyword("null", {
  1090. startsExpr
  1091. }),
  1092. _true: createKeyword("true", {
  1093. startsExpr
  1094. }),
  1095. _false: createKeyword("false", {
  1096. startsExpr
  1097. }),
  1098. _typeof: createKeyword("typeof", {
  1099. beforeExpr,
  1100. prefix,
  1101. startsExpr
  1102. }),
  1103. _void: createKeyword("void", {
  1104. beforeExpr,
  1105. prefix,
  1106. startsExpr
  1107. }),
  1108. _delete: createKeyword("delete", {
  1109. beforeExpr,
  1110. prefix,
  1111. startsExpr
  1112. }),
  1113. _do: createKeyword("do", {
  1114. isLoop,
  1115. beforeExpr
  1116. }),
  1117. _for: createKeyword("for", {
  1118. isLoop
  1119. }),
  1120. _while: createKeyword("while", {
  1121. isLoop
  1122. }),
  1123. _as: createKeywordLike("as", {
  1124. startsExpr
  1125. }),
  1126. _assert: createKeywordLike("assert", {
  1127. startsExpr
  1128. }),
  1129. _async: createKeywordLike("async", {
  1130. startsExpr
  1131. }),
  1132. _await: createKeywordLike("await", {
  1133. startsExpr
  1134. }),
  1135. _from: createKeywordLike("from", {
  1136. startsExpr
  1137. }),
  1138. _get: createKeywordLike("get", {
  1139. startsExpr
  1140. }),
  1141. _let: createKeywordLike("let", {
  1142. startsExpr
  1143. }),
  1144. _meta: createKeywordLike("meta", {
  1145. startsExpr
  1146. }),
  1147. _of: createKeywordLike("of", {
  1148. startsExpr
  1149. }),
  1150. _sent: createKeywordLike("sent", {
  1151. startsExpr
  1152. }),
  1153. _set: createKeywordLike("set", {
  1154. startsExpr
  1155. }),
  1156. _static: createKeywordLike("static", {
  1157. startsExpr
  1158. }),
  1159. _yield: createKeywordLike("yield", {
  1160. startsExpr
  1161. }),
  1162. _asserts: createKeywordLike("asserts", {
  1163. startsExpr
  1164. }),
  1165. _checks: createKeywordLike("checks", {
  1166. startsExpr
  1167. }),
  1168. _exports: createKeywordLike("exports", {
  1169. startsExpr
  1170. }),
  1171. _global: createKeywordLike("global", {
  1172. startsExpr
  1173. }),
  1174. _implements: createKeywordLike("implements", {
  1175. startsExpr
  1176. }),
  1177. _intrinsic: createKeywordLike("intrinsic", {
  1178. startsExpr
  1179. }),
  1180. _infer: createKeywordLike("infer", {
  1181. startsExpr
  1182. }),
  1183. _is: createKeywordLike("is", {
  1184. startsExpr
  1185. }),
  1186. _mixins: createKeywordLike("mixins", {
  1187. startsExpr
  1188. }),
  1189. _proto: createKeywordLike("proto", {
  1190. startsExpr
  1191. }),
  1192. _require: createKeywordLike("require", {
  1193. startsExpr
  1194. }),
  1195. _keyof: createKeywordLike("keyof", {
  1196. startsExpr
  1197. }),
  1198. _readonly: createKeywordLike("readonly", {
  1199. startsExpr
  1200. }),
  1201. _unique: createKeywordLike("unique", {
  1202. startsExpr
  1203. }),
  1204. _abstract: createKeywordLike("abstract", {
  1205. startsExpr
  1206. }),
  1207. _declare: createKeywordLike("declare", {
  1208. startsExpr
  1209. }),
  1210. _enum: createKeywordLike("enum", {
  1211. startsExpr
  1212. }),
  1213. _module: createKeywordLike("module", {
  1214. startsExpr
  1215. }),
  1216. _namespace: createKeywordLike("namespace", {
  1217. startsExpr
  1218. }),
  1219. _interface: createKeywordLike("interface", {
  1220. startsExpr
  1221. }),
  1222. _type: createKeywordLike("type", {
  1223. startsExpr
  1224. }),
  1225. _opaque: createKeywordLike("opaque", {
  1226. startsExpr
  1227. }),
  1228. name: createToken("name", {
  1229. startsExpr
  1230. }),
  1231. string: createToken("string", {
  1232. startsExpr
  1233. }),
  1234. num: createToken("num", {
  1235. startsExpr
  1236. }),
  1237. bigint: createToken("bigint", {
  1238. startsExpr
  1239. }),
  1240. decimal: createToken("decimal", {
  1241. startsExpr
  1242. }),
  1243. regexp: createToken("regexp", {
  1244. startsExpr
  1245. }),
  1246. privateName: createToken("#name", {
  1247. startsExpr
  1248. }),
  1249. eof: createToken("eof"),
  1250. jsxName: createToken("jsxName"),
  1251. jsxText: createToken("jsxText", {
  1252. beforeExpr: true
  1253. }),
  1254. jsxTagStart: createToken("jsxTagStart", {
  1255. startsExpr: true
  1256. }),
  1257. jsxTagEnd: createToken("jsxTagEnd"),
  1258. placeholder: createToken("%%", {
  1259. startsExpr: true
  1260. })
  1261. };
  1262. function tokenIsIdentifier(token) {
  1263. return token >= 89 && token <= 124;
  1264. }
  1265. function tokenKeywordOrIdentifierIsKeyword(token) {
  1266. return token <= 88;
  1267. }
  1268. function tokenIsKeywordOrIdentifier(token) {
  1269. return token >= 54 && token <= 124;
  1270. }
  1271. function tokenIsLiteralPropertyName(token) {
  1272. return token >= 54 && token <= 128;
  1273. }
  1274. function tokenComesBeforeExpression(token) {
  1275. return tokenBeforeExprs[token];
  1276. }
  1277. function tokenCanStartExpression(token) {
  1278. return tokenStartsExprs[token];
  1279. }
  1280. function tokenIsAssignment(token) {
  1281. return token >= 29 && token <= 33;
  1282. }
  1283. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1284. return token >= 121 && token <= 123;
  1285. }
  1286. function tokenIsLoop(token) {
  1287. return token >= 86 && token <= 88;
  1288. }
  1289. function tokenIsKeyword(token) {
  1290. return token >= 54 && token <= 88;
  1291. }
  1292. function tokenIsOperator(token) {
  1293. return token >= 37 && token <= 55;
  1294. }
  1295. function tokenIsPostfix(token) {
  1296. return token === 34;
  1297. }
  1298. function tokenIsPrefix(token) {
  1299. return tokenPrefixes[token];
  1300. }
  1301. function tokenIsTSTypeOperator(token) {
  1302. return token >= 113 && token <= 115;
  1303. }
  1304. function tokenIsTSDeclarationStart(token) {
  1305. return token >= 116 && token <= 122;
  1306. }
  1307. function tokenLabelName(token) {
  1308. return tokenLabels[token];
  1309. }
  1310. function tokenOperatorPrecedence(token) {
  1311. return tokenBinops[token];
  1312. }
  1313. function tokenIsRightAssociative(token) {
  1314. return token === 53;
  1315. }
  1316. function tokenIsTemplate(token) {
  1317. return token >= 24 && token <= 25;
  1318. }
  1319. function getExportedToken(token) {
  1320. return tokenTypes[token];
  1321. }
  1322. function isTokenType(obj) {
  1323. return typeof obj === "number";
  1324. }
  1325. {
  1326. tokenTypes[8].updateContext = context => {
  1327. context.pop();
  1328. };
  1329. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1330. context.push(types.brace);
  1331. };
  1332. tokenTypes[22].updateContext = context => {
  1333. if (context[context.length - 1] === types.template) {
  1334. context.pop();
  1335. } else {
  1336. context.push(types.template);
  1337. }
  1338. };
  1339. tokenTypes[134].updateContext = context => {
  1340. context.push(types.j_expr, types.j_oTag);
  1341. };
  1342. }
  1343. 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";
  1344. 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";
  1345. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1346. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1347. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1348. 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];
  1349. 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];
  1350. function isInAstralSet(code, set) {
  1351. let pos = 0x10000;
  1352. for (let i = 0, length = set.length; i < length; i += 2) {
  1353. pos += set[i];
  1354. if (pos > code) return false;
  1355. pos += set[i + 1];
  1356. if (pos >= code) return true;
  1357. }
  1358. return false;
  1359. }
  1360. function isIdentifierStart(code) {
  1361. if (code < 65) return code === 36;
  1362. if (code <= 90) return true;
  1363. if (code < 97) return code === 95;
  1364. if (code <= 122) return true;
  1365. if (code <= 0xffff) {
  1366. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1367. }
  1368. return isInAstralSet(code, astralIdentifierStartCodes);
  1369. }
  1370. function isIdentifierChar(code) {
  1371. if (code < 48) return code === 36;
  1372. if (code < 58) return true;
  1373. if (code < 65) return false;
  1374. if (code <= 90) return true;
  1375. if (code < 97) return code === 95;
  1376. if (code <= 122) return true;
  1377. if (code <= 0xffff) {
  1378. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1379. }
  1380. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1381. }
  1382. const reservedWords = {
  1383. 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"],
  1384. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1385. strictBind: ["eval", "arguments"]
  1386. };
  1387. const keywords = new Set(reservedWords.keyword);
  1388. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1389. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1390. function isReservedWord(word, inModule) {
  1391. return inModule && word === "await" || word === "enum";
  1392. }
  1393. function isStrictReservedWord(word, inModule) {
  1394. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1395. }
  1396. function isStrictBindOnlyReservedWord(word) {
  1397. return reservedWordsStrictBindSet.has(word);
  1398. }
  1399. function isStrictBindReservedWord(word, inModule) {
  1400. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1401. }
  1402. function isKeyword(word) {
  1403. return keywords.has(word);
  1404. }
  1405. function isIteratorStart(current, next) {
  1406. return current === 64 && next === 64;
  1407. }
  1408. 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"]);
  1409. function canBeReservedWord(word) {
  1410. return reservedWordLikeSet.has(word);
  1411. }
  1412. const SCOPE_OTHER = 0b000000000,
  1413. SCOPE_PROGRAM = 0b000000001,
  1414. SCOPE_FUNCTION = 0b000000010,
  1415. SCOPE_ARROW = 0b000000100,
  1416. SCOPE_SIMPLE_CATCH = 0b000001000,
  1417. SCOPE_SUPER = 0b000010000,
  1418. SCOPE_DIRECT_SUPER = 0b000100000,
  1419. SCOPE_CLASS = 0b001000000,
  1420. SCOPE_STATIC_BLOCK = 0b010000000,
  1421. SCOPE_TS_MODULE = 0b100000000,
  1422. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
  1423. const BIND_KIND_VALUE = 0b000000000001,
  1424. BIND_KIND_TYPE = 0b000000000010,
  1425. BIND_SCOPE_VAR = 0b000000000100,
  1426. BIND_SCOPE_LEXICAL = 0b000000001000,
  1427. BIND_SCOPE_FUNCTION = 0b000000010000,
  1428. BIND_FLAGS_NONE = 0b000001000000,
  1429. BIND_FLAGS_CLASS = 0b000010000000,
  1430. BIND_FLAGS_TS_ENUM = 0b000100000000,
  1431. BIND_FLAGS_TS_CONST_ENUM = 0b001000000000,
  1432. BIND_FLAGS_TS_EXPORT_ONLY = 0b010000000000,
  1433. BIND_FLAGS_FLOW_DECLARE_FN = 0b100000000000;
  1434. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  1435. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1436. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1437. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1438. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1439. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1440. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  1441. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1442. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1443. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1444. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1445. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1446. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1447. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1448. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1449. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1450. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1451. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1452. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1453. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1454. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1455. CLASS_ELEMENT_OTHER = 0;
  1456. class Scope {
  1457. constructor(flags) {
  1458. this.var = new Set();
  1459. this.lexical = new Set();
  1460. this.functions = new Set();
  1461. this.flags = flags;
  1462. }
  1463. }
  1464. class ScopeHandler {
  1465. constructor(raise, inModule) {
  1466. this.scopeStack = [];
  1467. this.undefinedExports = new Map();
  1468. this.undefinedPrivateNames = new Map();
  1469. this.raise = raise;
  1470. this.inModule = inModule;
  1471. }
  1472. get inFunction() {
  1473. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  1474. }
  1475. get allowSuper() {
  1476. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  1477. }
  1478. get allowDirectSuper() {
  1479. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  1480. }
  1481. get inClass() {
  1482. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  1483. }
  1484. get inClassAndNotInNonArrowFunction() {
  1485. const flags = this.currentThisScopeFlags();
  1486. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  1487. }
  1488. get inStaticBlock() {
  1489. for (let i = this.scopeStack.length - 1;; i--) {
  1490. const {
  1491. flags
  1492. } = this.scopeStack[i];
  1493. if (flags & SCOPE_STATIC_BLOCK) {
  1494. return true;
  1495. }
  1496. if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
  1497. return false;
  1498. }
  1499. }
  1500. }
  1501. get inNonArrowFunction() {
  1502. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  1503. }
  1504. get treatFunctionsAsVar() {
  1505. return this.treatFunctionsAsVarInScope(this.currentScope());
  1506. }
  1507. createScope(flags) {
  1508. return new Scope(flags);
  1509. }
  1510. enter(flags) {
  1511. this.scopeStack.push(this.createScope(flags));
  1512. }
  1513. exit() {
  1514. this.scopeStack.pop();
  1515. }
  1516. treatFunctionsAsVarInScope(scope) {
  1517. return !!(scope.flags & SCOPE_FUNCTION || !this.inModule && scope.flags & SCOPE_PROGRAM);
  1518. }
  1519. declareName(name, bindingType, pos) {
  1520. let scope = this.currentScope();
  1521. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1522. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1523. if (bindingType & BIND_SCOPE_FUNCTION) {
  1524. scope.functions.add(name);
  1525. } else {
  1526. scope.lexical.add(name);
  1527. }
  1528. if (bindingType & BIND_SCOPE_LEXICAL) {
  1529. this.maybeExportDefined(scope, name);
  1530. }
  1531. } else if (bindingType & BIND_SCOPE_VAR) {
  1532. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1533. scope = this.scopeStack[i];
  1534. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1535. scope.var.add(name);
  1536. this.maybeExportDefined(scope, name);
  1537. if (scope.flags & SCOPE_VAR) break;
  1538. }
  1539. }
  1540. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1541. this.undefinedExports.delete(name);
  1542. }
  1543. }
  1544. maybeExportDefined(scope, name) {
  1545. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1546. this.undefinedExports.delete(name);
  1547. }
  1548. }
  1549. checkRedeclarationInScope(scope, name, bindingType, pos) {
  1550. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1551. this.raise(pos, ErrorMessages.VarRedeclaration, name);
  1552. }
  1553. }
  1554. isRedeclaredInScope(scope, name, bindingType) {
  1555. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1556. if (bindingType & BIND_SCOPE_LEXICAL) {
  1557. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  1558. }
  1559. if (bindingType & BIND_SCOPE_FUNCTION) {
  1560. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  1561. }
  1562. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  1563. }
  1564. checkLocalExport(id) {
  1565. const {
  1566. name
  1567. } = id;
  1568. const topLevelScope = this.scopeStack[0];
  1569. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  1570. this.undefinedExports.set(name, id.start);
  1571. }
  1572. }
  1573. currentScope() {
  1574. return this.scopeStack[this.scopeStack.length - 1];
  1575. }
  1576. currentVarScopeFlags() {
  1577. for (let i = this.scopeStack.length - 1;; i--) {
  1578. const {
  1579. flags
  1580. } = this.scopeStack[i];
  1581. if (flags & SCOPE_VAR) {
  1582. return flags;
  1583. }
  1584. }
  1585. }
  1586. currentThisScopeFlags() {
  1587. for (let i = this.scopeStack.length - 1;; i--) {
  1588. const {
  1589. flags
  1590. } = this.scopeStack[i];
  1591. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  1592. return flags;
  1593. }
  1594. }
  1595. }
  1596. }
  1597. class FlowScope extends Scope {
  1598. constructor(...args) {
  1599. super(...args);
  1600. this.declareFunctions = new Set();
  1601. }
  1602. }
  1603. class FlowScopeHandler extends ScopeHandler {
  1604. createScope(flags) {
  1605. return new FlowScope(flags);
  1606. }
  1607. declareName(name, bindingType, pos) {
  1608. const scope = this.currentScope();
  1609. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1610. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1611. this.maybeExportDefined(scope, name);
  1612. scope.declareFunctions.add(name);
  1613. return;
  1614. }
  1615. super.declareName(...arguments);
  1616. }
  1617. isRedeclaredInScope(scope, name, bindingType) {
  1618. if (super.isRedeclaredInScope(...arguments)) return true;
  1619. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1620. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  1621. }
  1622. return false;
  1623. }
  1624. checkLocalExport(id) {
  1625. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1626. super.checkLocalExport(id);
  1627. }
  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.topicContext = {
  1648. maxNumOfResolvableTopics: 0,
  1649. maxTopicIndex: null
  1650. };
  1651. this.soloAwait = false;
  1652. this.inFSharpPipelineDirectBody = false;
  1653. this.labels = [];
  1654. this.decoratorStack = [[]];
  1655. this.comments = [];
  1656. this.commentStack = [];
  1657. this.pos = 0;
  1658. this.type = 131;
  1659. this.value = null;
  1660. this.start = 0;
  1661. this.end = 0;
  1662. this.lastTokEndLoc = null;
  1663. this.lastTokStartLoc = null;
  1664. this.lastTokStart = 0;
  1665. this.lastTokEnd = 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);
  1682. }
  1683. curPosition() {
  1684. return new Position(this.curLine, this.pos - this.lineStart);
  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 VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100]);
  1704. const forbiddenNumericSeparatorSiblings = {
  1705. decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
  1706. hex: [46, 88, 95, 120]
  1707. };
  1708. const allowedNumericSeparatorSiblings = {};
  1709. allowedNumericSeparatorSiblings.bin = [48, 49];
  1710. allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
  1711. allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
  1712. allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
  1713. class Token {
  1714. constructor(state) {
  1715. this.type = state.type;
  1716. this.value = state.value;
  1717. this.start = state.start;
  1718. this.end = state.end;
  1719. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  1720. }
  1721. }
  1722. class Tokenizer extends ParserError {
  1723. constructor(options, input) {
  1724. super();
  1725. this.isLookahead = void 0;
  1726. this.tokens = [];
  1727. this.state = new State();
  1728. this.state.init(options);
  1729. this.input = input;
  1730. this.length = input.length;
  1731. this.isLookahead = false;
  1732. }
  1733. pushToken(token) {
  1734. this.tokens.length = this.state.tokensLength;
  1735. this.tokens.push(token);
  1736. ++this.state.tokensLength;
  1737. }
  1738. next() {
  1739. this.checkKeywordEscapes();
  1740. if (this.options.tokens) {
  1741. this.pushToken(new Token(this.state));
  1742. }
  1743. this.state.lastTokEnd = this.state.end;
  1744. this.state.lastTokStart = this.state.start;
  1745. this.state.lastTokEndLoc = this.state.endLoc;
  1746. this.state.lastTokStartLoc = this.state.startLoc;
  1747. this.nextToken();
  1748. }
  1749. eat(type) {
  1750. if (this.match(type)) {
  1751. this.next();
  1752. return true;
  1753. } else {
  1754. return false;
  1755. }
  1756. }
  1757. match(type) {
  1758. return this.state.type === type;
  1759. }
  1760. createLookaheadState(state) {
  1761. return {
  1762. pos: state.pos,
  1763. value: null,
  1764. type: state.type,
  1765. start: state.start,
  1766. end: state.end,
  1767. lastTokEnd: state.end,
  1768. context: [this.curContext()],
  1769. inType: state.inType
  1770. };
  1771. }
  1772. lookahead() {
  1773. const old = this.state;
  1774. this.state = this.createLookaheadState(old);
  1775. this.isLookahead = true;
  1776. this.nextToken();
  1777. this.isLookahead = false;
  1778. const curr = this.state;
  1779. this.state = old;
  1780. return curr;
  1781. }
  1782. nextTokenStart() {
  1783. return this.nextTokenStartSince(this.state.pos);
  1784. }
  1785. nextTokenStartSince(pos) {
  1786. skipWhiteSpace.lastIndex = pos;
  1787. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  1788. }
  1789. lookaheadCharCode() {
  1790. return this.input.charCodeAt(this.nextTokenStart());
  1791. }
  1792. codePointAtPos(pos) {
  1793. let cp = this.input.charCodeAt(pos);
  1794. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  1795. const trail = this.input.charCodeAt(pos);
  1796. if ((trail & 0xfc00) === 0xdc00) {
  1797. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  1798. }
  1799. }
  1800. return cp;
  1801. }
  1802. setStrict(strict) {
  1803. this.state.strict = strict;
  1804. if (strict) {
  1805. this.state.strictErrors.forEach((message, pos) => this.raise(pos, message));
  1806. this.state.strictErrors.clear();
  1807. }
  1808. }
  1809. curContext() {
  1810. return this.state.context[this.state.context.length - 1];
  1811. }
  1812. nextToken() {
  1813. this.skipSpace();
  1814. this.state.start = this.state.pos;
  1815. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  1816. if (this.state.pos >= this.length) {
  1817. this.finishToken(131);
  1818. return;
  1819. }
  1820. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  1821. }
  1822. skipBlockComment() {
  1823. let startLoc;
  1824. if (!this.isLookahead) startLoc = this.state.curPosition();
  1825. const start = this.state.pos;
  1826. const end = this.input.indexOf("*/", start + 2);
  1827. if (end === -1) throw this.raise(start, ErrorMessages.UnterminatedComment);
  1828. this.state.pos = end + 2;
  1829. lineBreakG.lastIndex = start + 2;
  1830. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  1831. ++this.state.curLine;
  1832. this.state.lineStart = lineBreakG.lastIndex;
  1833. }
  1834. if (this.isLookahead) return;
  1835. const comment = {
  1836. type: "CommentBlock",
  1837. value: this.input.slice(start + 2, end),
  1838. start,
  1839. end: end + 2,
  1840. loc: new SourceLocation(startLoc, this.state.curPosition())
  1841. };
  1842. if (this.options.tokens) this.pushToken(comment);
  1843. return comment;
  1844. }
  1845. skipLineComment(startSkip) {
  1846. const start = this.state.pos;
  1847. let startLoc;
  1848. if (!this.isLookahead) startLoc = this.state.curPosition();
  1849. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  1850. if (this.state.pos < this.length) {
  1851. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  1852. ch = this.input.charCodeAt(this.state.pos);
  1853. }
  1854. }
  1855. if (this.isLookahead) return;
  1856. const end = this.state.pos;
  1857. const value = this.input.slice(start + startSkip, end);
  1858. const comment = {
  1859. type: "CommentLine",
  1860. value,
  1861. start,
  1862. end,
  1863. loc: new SourceLocation(startLoc, this.state.curPosition())
  1864. };
  1865. if (this.options.tokens) this.pushToken(comment);
  1866. return comment;
  1867. }
  1868. skipSpace() {
  1869. const spaceStart = this.state.pos;
  1870. const comments = [];
  1871. loop: while (this.state.pos < this.length) {
  1872. const ch = this.input.charCodeAt(this.state.pos);
  1873. switch (ch) {
  1874. case 32:
  1875. case 160:
  1876. case 9:
  1877. ++this.state.pos;
  1878. break;
  1879. case 13:
  1880. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  1881. ++this.state.pos;
  1882. }
  1883. case 10:
  1884. case 8232:
  1885. case 8233:
  1886. ++this.state.pos;
  1887. ++this.state.curLine;
  1888. this.state.lineStart = this.state.pos;
  1889. break;
  1890. case 47:
  1891. switch (this.input.charCodeAt(this.state.pos + 1)) {
  1892. case 42:
  1893. {
  1894. const comment = this.skipBlockComment();
  1895. if (comment !== undefined) {
  1896. this.addComment(comment);
  1897. if (this.options.attachComment) comments.push(comment);
  1898. }
  1899. break;
  1900. }
  1901. case 47:
  1902. {
  1903. const comment = this.skipLineComment(2);
  1904. if (comment !== undefined) {
  1905. this.addComment(comment);
  1906. if (this.options.attachComment) comments.push(comment);
  1907. }
  1908. break;
  1909. }
  1910. default:
  1911. break loop;
  1912. }
  1913. break;
  1914. default:
  1915. if (isWhitespace(ch)) {
  1916. ++this.state.pos;
  1917. } else if (ch === 45 && !this.inModule) {
  1918. const pos = this.state.pos;
  1919. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  1920. const comment = this.skipLineComment(3);
  1921. if (comment !== undefined) {
  1922. this.addComment(comment);
  1923. if (this.options.attachComment) comments.push(comment);
  1924. }
  1925. } else {
  1926. break loop;
  1927. }
  1928. } else if (ch === 60 && !this.inModule) {
  1929. const pos = this.state.pos;
  1930. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  1931. const comment = this.skipLineComment(4);
  1932. if (comment !== undefined) {
  1933. this.addComment(comment);
  1934. if (this.options.attachComment) comments.push(comment);
  1935. }
  1936. } else {
  1937. break loop;
  1938. }
  1939. } else {
  1940. break loop;
  1941. }
  1942. }
  1943. }
  1944. if (comments.length > 0) {
  1945. const end = this.state.pos;
  1946. const CommentWhitespace = {
  1947. start: spaceStart,
  1948. end,
  1949. comments,
  1950. leadingNode: null,
  1951. trailingNode: null,
  1952. containingNode: null
  1953. };
  1954. this.state.commentStack.push(CommentWhitespace);
  1955. }
  1956. }
  1957. finishToken(type, val) {
  1958. this.state.end = this.state.pos;
  1959. const prevType = this.state.type;
  1960. this.state.type = type;
  1961. this.state.value = val;
  1962. if (!this.isLookahead) {
  1963. this.state.endLoc = this.state.curPosition();
  1964. this.updateContext(prevType);
  1965. }
  1966. }
  1967. replaceToken(type) {
  1968. this.state.type = type;
  1969. this.updateContext();
  1970. }
  1971. readToken_numberSign() {
  1972. if (this.state.pos === 0 && this.readToken_interpreter()) {
  1973. return;
  1974. }
  1975. const nextPos = this.state.pos + 1;
  1976. const next = this.codePointAtPos(nextPos);
  1977. if (next >= 48 && next <= 57) {
  1978. throw this.raise(this.state.pos, ErrorMessages.UnexpectedDigitAfterHash);
  1979. }
  1980. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  1981. this.expectPlugin("recordAndTuple");
  1982. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
  1983. throw this.raise(this.state.pos, next === 123 ? ErrorMessages.RecordExpressionHashIncorrectStartSyntaxType : ErrorMessages.TupleExpressionHashIncorrectStartSyntaxType);
  1984. }
  1985. this.state.pos += 2;
  1986. if (next === 123) {
  1987. this.finishToken(7);
  1988. } else {
  1989. this.finishToken(1);
  1990. }
  1991. } else if (isIdentifierStart(next)) {
  1992. ++this.state.pos;
  1993. this.finishToken(130, this.readWord1(next));
  1994. } else if (next === 92) {
  1995. ++this.state.pos;
  1996. this.finishToken(130, this.readWord1());
  1997. } else {
  1998. this.finishOp(27, 1);
  1999. }
  2000. }
  2001. readToken_dot() {
  2002. const next = this.input.charCodeAt(this.state.pos + 1);
  2003. if (next >= 48 && next <= 57) {
  2004. this.readNumber(true);
  2005. return;
  2006. }
  2007. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  2008. this.state.pos += 3;
  2009. this.finishToken(21);
  2010. } else {
  2011. ++this.state.pos;
  2012. this.finishToken(16);
  2013. }
  2014. }
  2015. readToken_slash() {
  2016. const next = this.input.charCodeAt(this.state.pos + 1);
  2017. if (next === 61) {
  2018. this.finishOp(31, 2);
  2019. } else {
  2020. this.finishOp(52, 1);
  2021. }
  2022. }
  2023. readToken_interpreter() {
  2024. if (this.state.pos !== 0 || this.length < 2) return false;
  2025. let ch = this.input.charCodeAt(this.state.pos + 1);
  2026. if (ch !== 33) return false;
  2027. const start = this.state.pos;
  2028. this.state.pos += 1;
  2029. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2030. ch = this.input.charCodeAt(this.state.pos);
  2031. }
  2032. const value = this.input.slice(start + 2, this.state.pos);
  2033. this.finishToken(28, value);
  2034. return true;
  2035. }
  2036. readToken_mult_modulo(code) {
  2037. let type = code === 42 ? 51 : 50;
  2038. let width = 1;
  2039. let next = this.input.charCodeAt(this.state.pos + 1);
  2040. if (code === 42 && next === 42) {
  2041. width++;
  2042. next = this.input.charCodeAt(this.state.pos + 2);
  2043. type = 53;
  2044. }
  2045. if (next === 61 && !this.state.inType) {
  2046. width++;
  2047. type = code === 37 ? 33 : 30;
  2048. }
  2049. this.finishOp(type, width);
  2050. }
  2051. readToken_pipe_amp(code) {
  2052. const next = this.input.charCodeAt(this.state.pos + 1);
  2053. if (next === code) {
  2054. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  2055. this.finishOp(30, 3);
  2056. } else {
  2057. this.finishOp(code === 124 ? 39 : 40, 2);
  2058. }
  2059. return;
  2060. }
  2061. if (code === 124) {
  2062. if (next === 62) {
  2063. this.finishOp(37, 2);
  2064. return;
  2065. }
  2066. if (this.hasPlugin("recordAndTuple") && next === 125) {
  2067. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2068. throw this.raise(this.state.pos, ErrorMessages.RecordExpressionBarIncorrectEndSyntaxType);
  2069. }
  2070. this.state.pos += 2;
  2071. this.finishToken(9);
  2072. return;
  2073. }
  2074. if (this.hasPlugin("recordAndTuple") && next === 93) {
  2075. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2076. throw this.raise(this.state.pos, ErrorMessages.TupleExpressionBarIncorrectEndSyntaxType);
  2077. }
  2078. this.state.pos += 2;
  2079. this.finishToken(4);
  2080. return;
  2081. }
  2082. }
  2083. if (next === 61) {
  2084. this.finishOp(30, 2);
  2085. return;
  2086. }
  2087. this.finishOp(code === 124 ? 41 : 43, 1);
  2088. }
  2089. readToken_caret() {
  2090. const next = this.input.charCodeAt(this.state.pos + 1);
  2091. if (next === 61 && !this.state.inType) {
  2092. this.finishOp(32, 2);
  2093. } else {
  2094. this.finishOp(42, 1);
  2095. }
  2096. }
  2097. readToken_plus_min(code) {
  2098. const next = this.input.charCodeAt(this.state.pos + 1);
  2099. if (next === code) {
  2100. this.finishOp(34, 2);
  2101. return;
  2102. }
  2103. if (next === 61) {
  2104. this.finishOp(30, 2);
  2105. } else {
  2106. this.finishOp(49, 1);
  2107. }
  2108. }
  2109. readToken_lt() {
  2110. const {
  2111. pos
  2112. } = this.state;
  2113. const next = this.input.charCodeAt(pos + 1);
  2114. if (next === 60) {
  2115. if (this.input.charCodeAt(pos + 2) === 61) {
  2116. this.finishOp(30, 3);
  2117. return;
  2118. }
  2119. this.finishOp(48, 2);
  2120. return;
  2121. }
  2122. if (next === 61) {
  2123. this.finishOp(47, 2);
  2124. return;
  2125. }
  2126. this.finishOp(45, 1);
  2127. }
  2128. readToken_gt() {
  2129. const {
  2130. pos
  2131. } = this.state;
  2132. const next = this.input.charCodeAt(pos + 1);
  2133. if (next === 62) {
  2134. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  2135. if (this.input.charCodeAt(pos + size) === 61) {
  2136. this.finishOp(30, size + 1);
  2137. return;
  2138. }
  2139. this.finishOp(48, size);
  2140. return;
  2141. }
  2142. if (next === 61) {
  2143. this.finishOp(47, 2);
  2144. return;
  2145. }
  2146. this.finishOp(46, 1);
  2147. }
  2148. readToken_eq_excl(code) {
  2149. const next = this.input.charCodeAt(this.state.pos + 1);
  2150. if (next === 61) {
  2151. this.finishOp(44, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  2152. return;
  2153. }
  2154. if (code === 61 && next === 62) {
  2155. this.state.pos += 2;
  2156. this.finishToken(19);
  2157. return;
  2158. }
  2159. this.finishOp(code === 61 ? 29 : 35, 1);
  2160. }
  2161. readToken_question() {
  2162. const next = this.input.charCodeAt(this.state.pos + 1);
  2163. const next2 = this.input.charCodeAt(this.state.pos + 2);
  2164. if (next === 63) {
  2165. if (next2 === 61) {
  2166. this.finishOp(30, 3);
  2167. } else {
  2168. this.finishOp(38, 2);
  2169. }
  2170. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  2171. this.state.pos += 2;
  2172. this.finishToken(18);
  2173. } else {
  2174. ++this.state.pos;
  2175. this.finishToken(17);
  2176. }
  2177. }
  2178. getTokenFromCode(code) {
  2179. switch (code) {
  2180. case 46:
  2181. this.readToken_dot();
  2182. return;
  2183. case 40:
  2184. ++this.state.pos;
  2185. this.finishToken(10);
  2186. return;
  2187. case 41:
  2188. ++this.state.pos;
  2189. this.finishToken(11);
  2190. return;
  2191. case 59:
  2192. ++this.state.pos;
  2193. this.finishToken(13);
  2194. return;
  2195. case 44:
  2196. ++this.state.pos;
  2197. this.finishToken(12);
  2198. return;
  2199. case 91:
  2200. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2201. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2202. throw this.raise(this.state.pos, ErrorMessages.TupleExpressionBarIncorrectStartSyntaxType);
  2203. }
  2204. this.state.pos += 2;
  2205. this.finishToken(2);
  2206. } else {
  2207. ++this.state.pos;
  2208. this.finishToken(0);
  2209. }
  2210. return;
  2211. case 93:
  2212. ++this.state.pos;
  2213. this.finishToken(3);
  2214. return;
  2215. case 123:
  2216. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2217. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2218. throw this.raise(this.state.pos, ErrorMessages.RecordExpressionBarIncorrectStartSyntaxType);
  2219. }
  2220. this.state.pos += 2;
  2221. this.finishToken(6);
  2222. } else {
  2223. ++this.state.pos;
  2224. this.finishToken(5);
  2225. }
  2226. return;
  2227. case 125:
  2228. ++this.state.pos;
  2229. this.finishToken(8);
  2230. return;
  2231. case 58:
  2232. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  2233. this.finishOp(15, 2);
  2234. } else {
  2235. ++this.state.pos;
  2236. this.finishToken(14);
  2237. }
  2238. return;
  2239. case 63:
  2240. this.readToken_question();
  2241. return;
  2242. case 96:
  2243. this.readTemplateToken();
  2244. return;
  2245. case 48:
  2246. {
  2247. const next = this.input.charCodeAt(this.state.pos + 1);
  2248. if (next === 120 || next === 88) {
  2249. this.readRadixNumber(16);
  2250. return;
  2251. }
  2252. if (next === 111 || next === 79) {
  2253. this.readRadixNumber(8);
  2254. return;
  2255. }
  2256. if (next === 98 || next === 66) {
  2257. this.readRadixNumber(2);
  2258. return;
  2259. }
  2260. }
  2261. case 49:
  2262. case 50:
  2263. case 51:
  2264. case 52:
  2265. case 53:
  2266. case 54:
  2267. case 55:
  2268. case 56:
  2269. case 57:
  2270. this.readNumber(false);
  2271. return;
  2272. case 34:
  2273. case 39:
  2274. this.readString(code);
  2275. return;
  2276. case 47:
  2277. this.readToken_slash();
  2278. return;
  2279. case 37:
  2280. case 42:
  2281. this.readToken_mult_modulo(code);
  2282. return;
  2283. case 124:
  2284. case 38:
  2285. this.readToken_pipe_amp(code);
  2286. return;
  2287. case 94:
  2288. this.readToken_caret();
  2289. return;
  2290. case 43:
  2291. case 45:
  2292. this.readToken_plus_min(code);
  2293. return;
  2294. case 60:
  2295. this.readToken_lt();
  2296. return;
  2297. case 62:
  2298. this.readToken_gt();
  2299. return;
  2300. case 61:
  2301. case 33:
  2302. this.readToken_eq_excl(code);
  2303. return;
  2304. case 126:
  2305. this.finishOp(36, 1);
  2306. return;
  2307. case 64:
  2308. ++this.state.pos;
  2309. this.finishToken(26);
  2310. return;
  2311. case 35:
  2312. this.readToken_numberSign();
  2313. return;
  2314. case 92:
  2315. this.readWord();
  2316. return;
  2317. default:
  2318. if (isIdentifierStart(code)) {
  2319. this.readWord(code);
  2320. return;
  2321. }
  2322. }
  2323. throw this.raise(this.state.pos, ErrorMessages.InvalidOrUnexpectedToken, String.fromCodePoint(code));
  2324. }
  2325. finishOp(type, size) {
  2326. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2327. this.state.pos += size;
  2328. this.finishToken(type, str);
  2329. }
  2330. readRegexp() {
  2331. const start = this.state.start + 1;
  2332. let escaped, inClass;
  2333. let {
  2334. pos
  2335. } = this.state;
  2336. for (;; ++pos) {
  2337. if (pos >= this.length) {
  2338. throw this.raise(start, ErrorMessages.UnterminatedRegExp);
  2339. }
  2340. const ch = this.input.charCodeAt(pos);
  2341. if (isNewLine(ch)) {
  2342. throw this.raise(start, ErrorMessages.UnterminatedRegExp);
  2343. }
  2344. if (escaped) {
  2345. escaped = false;
  2346. } else {
  2347. if (ch === 91) {
  2348. inClass = true;
  2349. } else if (ch === 93 && inClass) {
  2350. inClass = false;
  2351. } else if (ch === 47 && !inClass) {
  2352. break;
  2353. }
  2354. escaped = ch === 92;
  2355. }
  2356. }
  2357. const content = this.input.slice(start, pos);
  2358. ++pos;
  2359. let mods = "";
  2360. while (pos < this.length) {
  2361. const cp = this.codePointAtPos(pos);
  2362. const char = String.fromCharCode(cp);
  2363. if (VALID_REGEX_FLAGS.has(cp)) {
  2364. if (mods.includes(char)) {
  2365. this.raise(pos + 1, ErrorMessages.DuplicateRegExpFlags);
  2366. }
  2367. } else if (isIdentifierChar(cp) || cp === 92) {
  2368. this.raise(pos + 1, ErrorMessages.MalformedRegExpFlags);
  2369. } else {
  2370. break;
  2371. }
  2372. ++pos;
  2373. mods += char;
  2374. }
  2375. this.state.pos = pos;
  2376. this.finishToken(129, {
  2377. pattern: content,
  2378. flags: mods
  2379. });
  2380. }
  2381. readInt(radix, len, forceLen, allowNumSeparator = true) {
  2382. const start = this.state.pos;
  2383. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  2384. const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
  2385. let invalid = false;
  2386. let total = 0;
  2387. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  2388. const code = this.input.charCodeAt(this.state.pos);
  2389. let val;
  2390. if (code === 95) {
  2391. const prev = this.input.charCodeAt(this.state.pos - 1);
  2392. const next = this.input.charCodeAt(this.state.pos + 1);
  2393. if (allowedSiblings.indexOf(next) === -1) {
  2394. this.raise(this.state.pos, ErrorMessages.UnexpectedNumericSeparator);
  2395. } else if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
  2396. this.raise(this.state.pos, ErrorMessages.UnexpectedNumericSeparator);
  2397. }
  2398. if (!allowNumSeparator) {
  2399. this.raise(this.state.pos, ErrorMessages.NumericSeparatorInEscapeSequence);
  2400. }
  2401. ++this.state.pos;
  2402. continue;
  2403. }
  2404. if (code >= 97) {
  2405. val = code - 97 + 10;
  2406. } else if (code >= 65) {
  2407. val = code - 65 + 10;
  2408. } else if (_isDigit(code)) {
  2409. val = code - 48;
  2410. } else {
  2411. val = Infinity;
  2412. }
  2413. if (val >= radix) {
  2414. if (this.options.errorRecovery && val <= 9) {
  2415. val = 0;
  2416. this.raise(this.state.start + i + 2, ErrorMessages.InvalidDigit, radix);
  2417. } else if (forceLen) {
  2418. val = 0;
  2419. invalid = true;
  2420. } else {
  2421. break;
  2422. }
  2423. }
  2424. ++this.state.pos;
  2425. total = total * radix + val;
  2426. }
  2427. if (this.state.pos === start || len != null && this.state.pos - start !== len || invalid) {
  2428. return null;
  2429. }
  2430. return total;
  2431. }
  2432. readRadixNumber(radix) {
  2433. const start = this.state.pos;
  2434. let isBigInt = false;
  2435. this.state.pos += 2;
  2436. const val = this.readInt(radix);
  2437. if (val == null) {
  2438. this.raise(this.state.start + 2, ErrorMessages.InvalidDigit, radix);
  2439. }
  2440. const next = this.input.charCodeAt(this.state.pos);
  2441. if (next === 110) {
  2442. ++this.state.pos;
  2443. isBigInt = true;
  2444. } else if (next === 109) {
  2445. throw this.raise(start, ErrorMessages.InvalidDecimal);
  2446. }
  2447. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  2448. throw this.raise(this.state.pos, ErrorMessages.NumberIdentifier);
  2449. }
  2450. if (isBigInt) {
  2451. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  2452. this.finishToken(127, str);
  2453. return;
  2454. }
  2455. this.finishToken(126, val);
  2456. }
  2457. readNumber(startsWithDot) {
  2458. const start = this.state.pos;
  2459. let isFloat = false;
  2460. let isBigInt = false;
  2461. let isDecimal = false;
  2462. let hasExponent = false;
  2463. let isOctal = false;
  2464. if (!startsWithDot && this.readInt(10) === null) {
  2465. this.raise(start, ErrorMessages.InvalidNumber);
  2466. }
  2467. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  2468. if (hasLeadingZero) {
  2469. const integer = this.input.slice(start, this.state.pos);
  2470. this.recordStrictModeErrors(start, ErrorMessages.StrictOctalLiteral);
  2471. if (!this.state.strict) {
  2472. const underscorePos = integer.indexOf("_");
  2473. if (underscorePos > 0) {
  2474. this.raise(underscorePos + start, ErrorMessages.ZeroDigitNumericSeparator);
  2475. }
  2476. }
  2477. isOctal = hasLeadingZero && !/[89]/.test(integer);
  2478. }
  2479. let next = this.input.charCodeAt(this.state.pos);
  2480. if (next === 46 && !isOctal) {
  2481. ++this.state.pos;
  2482. this.readInt(10);
  2483. isFloat = true;
  2484. next = this.input.charCodeAt(this.state.pos);
  2485. }
  2486. if ((next === 69 || next === 101) && !isOctal) {
  2487. next = this.input.charCodeAt(++this.state.pos);
  2488. if (next === 43 || next === 45) {
  2489. ++this.state.pos;
  2490. }
  2491. if (this.readInt(10) === null) {
  2492. this.raise(start, ErrorMessages.InvalidOrMissingExponent);
  2493. }
  2494. isFloat = true;
  2495. hasExponent = true;
  2496. next = this.input.charCodeAt(this.state.pos);
  2497. }
  2498. if (next === 110) {
  2499. if (isFloat || hasLeadingZero) {
  2500. this.raise(start, ErrorMessages.InvalidBigIntLiteral);
  2501. }
  2502. ++this.state.pos;
  2503. isBigInt = true;
  2504. }
  2505. if (next === 109) {
  2506. this.expectPlugin("decimal", this.state.pos);
  2507. if (hasExponent || hasLeadingZero) {
  2508. this.raise(start, ErrorMessages.InvalidDecimal);
  2509. }
  2510. ++this.state.pos;
  2511. isDecimal = true;
  2512. }
  2513. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  2514. throw this.raise(this.state.pos, ErrorMessages.NumberIdentifier);
  2515. }
  2516. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  2517. if (isBigInt) {
  2518. this.finishToken(127, str);
  2519. return;
  2520. }
  2521. if (isDecimal) {
  2522. this.finishToken(128, str);
  2523. return;
  2524. }
  2525. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  2526. this.finishToken(126, val);
  2527. }
  2528. readCodePoint(throwOnInvalid) {
  2529. const ch = this.input.charCodeAt(this.state.pos);
  2530. let code;
  2531. if (ch === 123) {
  2532. const codePos = ++this.state.pos;
  2533. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, true, throwOnInvalid);
  2534. ++this.state.pos;
  2535. if (code !== null && code > 0x10ffff) {
  2536. if (throwOnInvalid) {
  2537. this.raise(codePos, ErrorMessages.InvalidCodePoint);
  2538. } else {
  2539. return null;
  2540. }
  2541. }
  2542. } else {
  2543. code = this.readHexChar(4, false, throwOnInvalid);
  2544. }
  2545. return code;
  2546. }
  2547. readString(quote) {
  2548. let out = "",
  2549. chunkStart = ++this.state.pos;
  2550. for (;;) {
  2551. if (this.state.pos >= this.length) {
  2552. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  2553. }
  2554. const ch = this.input.charCodeAt(this.state.pos);
  2555. if (ch === quote) break;
  2556. if (ch === 92) {
  2557. out += this.input.slice(chunkStart, this.state.pos);
  2558. out += this.readEscapedChar(false);
  2559. chunkStart = this.state.pos;
  2560. } else if (ch === 8232 || ch === 8233) {
  2561. ++this.state.pos;
  2562. ++this.state.curLine;
  2563. this.state.lineStart = this.state.pos;
  2564. } else if (isNewLine(ch)) {
  2565. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  2566. } else {
  2567. ++this.state.pos;
  2568. }
  2569. }
  2570. out += this.input.slice(chunkStart, this.state.pos++);
  2571. this.finishToken(125, out);
  2572. }
  2573. readTemplateContinuation() {
  2574. if (!this.match(8)) {
  2575. this.unexpected(this.state.start, 8);
  2576. }
  2577. this.state.pos--;
  2578. this.readTemplateToken();
  2579. }
  2580. readTemplateToken() {
  2581. let out = "",
  2582. chunkStart = this.state.pos,
  2583. containsInvalid = false;
  2584. ++this.state.pos;
  2585. for (;;) {
  2586. if (this.state.pos >= this.length) {
  2587. throw this.raise(this.state.start + 1, ErrorMessages.UnterminatedTemplate);
  2588. }
  2589. const ch = this.input.charCodeAt(this.state.pos);
  2590. if (ch === 96) {
  2591. ++this.state.pos;
  2592. out += this.input.slice(chunkStart, this.state.pos);
  2593. this.finishToken(24, containsInvalid ? null : out);
  2594. return;
  2595. }
  2596. if (ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  2597. this.state.pos += 2;
  2598. out += this.input.slice(chunkStart, this.state.pos);
  2599. this.finishToken(25, containsInvalid ? null : out);
  2600. return;
  2601. }
  2602. if (ch === 92) {
  2603. out += this.input.slice(chunkStart, this.state.pos);
  2604. const escaped = this.readEscapedChar(true);
  2605. if (escaped === null) {
  2606. containsInvalid = true;
  2607. } else {
  2608. out += escaped;
  2609. }
  2610. chunkStart = this.state.pos;
  2611. } else if (isNewLine(ch)) {
  2612. out += this.input.slice(chunkStart, this.state.pos);
  2613. ++this.state.pos;
  2614. switch (ch) {
  2615. case 13:
  2616. if (this.input.charCodeAt(this.state.pos) === 10) {
  2617. ++this.state.pos;
  2618. }
  2619. case 10:
  2620. out += "\n";
  2621. break;
  2622. default:
  2623. out += String.fromCharCode(ch);
  2624. break;
  2625. }
  2626. ++this.state.curLine;
  2627. this.state.lineStart = this.state.pos;
  2628. chunkStart = this.state.pos;
  2629. } else {
  2630. ++this.state.pos;
  2631. }
  2632. }
  2633. }
  2634. recordStrictModeErrors(pos, message) {
  2635. if (this.state.strict && !this.state.strictErrors.has(pos)) {
  2636. this.raise(pos, message);
  2637. } else {
  2638. this.state.strictErrors.set(pos, message);
  2639. }
  2640. }
  2641. readEscapedChar(inTemplate) {
  2642. const throwOnInvalid = !inTemplate;
  2643. const ch = this.input.charCodeAt(++this.state.pos);
  2644. ++this.state.pos;
  2645. switch (ch) {
  2646. case 110:
  2647. return "\n";
  2648. case 114:
  2649. return "\r";
  2650. case 120:
  2651. {
  2652. const code = this.readHexChar(2, false, throwOnInvalid);
  2653. return code === null ? null : String.fromCharCode(code);
  2654. }
  2655. case 117:
  2656. {
  2657. const code = this.readCodePoint(throwOnInvalid);
  2658. return code === null ? null : String.fromCodePoint(code);
  2659. }
  2660. case 116:
  2661. return "\t";
  2662. case 98:
  2663. return "\b";
  2664. case 118:
  2665. return "\u000b";
  2666. case 102:
  2667. return "\f";
  2668. case 13:
  2669. if (this.input.charCodeAt(this.state.pos) === 10) {
  2670. ++this.state.pos;
  2671. }
  2672. case 10:
  2673. this.state.lineStart = this.state.pos;
  2674. ++this.state.curLine;
  2675. case 8232:
  2676. case 8233:
  2677. return "";
  2678. case 56:
  2679. case 57:
  2680. if (inTemplate) {
  2681. return null;
  2682. } else {
  2683. this.recordStrictModeErrors(this.state.pos - 1, ErrorMessages.StrictNumericEscape);
  2684. }
  2685. default:
  2686. if (ch >= 48 && ch <= 55) {
  2687. const codePos = this.state.pos - 1;
  2688. const match = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/);
  2689. let octalStr = match[0];
  2690. let octal = parseInt(octalStr, 8);
  2691. if (octal > 255) {
  2692. octalStr = octalStr.slice(0, -1);
  2693. octal = parseInt(octalStr, 8);
  2694. }
  2695. this.state.pos += octalStr.length - 1;
  2696. const next = this.input.charCodeAt(this.state.pos);
  2697. if (octalStr !== "0" || next === 56 || next === 57) {
  2698. if (inTemplate) {
  2699. return null;
  2700. } else {
  2701. this.recordStrictModeErrors(codePos, ErrorMessages.StrictNumericEscape);
  2702. }
  2703. }
  2704. return String.fromCharCode(octal);
  2705. }
  2706. return String.fromCharCode(ch);
  2707. }
  2708. }
  2709. readHexChar(len, forceLen, throwOnInvalid) {
  2710. const codePos = this.state.pos;
  2711. const n = this.readInt(16, len, forceLen, false);
  2712. if (n === null) {
  2713. if (throwOnInvalid) {
  2714. this.raise(codePos, ErrorMessages.InvalidEscapeSequence);
  2715. } else {
  2716. this.state.pos = codePos - 1;
  2717. }
  2718. }
  2719. return n;
  2720. }
  2721. readWord1(firstCode) {
  2722. this.state.containsEsc = false;
  2723. let word = "";
  2724. const start = this.state.pos;
  2725. let chunkStart = this.state.pos;
  2726. if (firstCode !== undefined) {
  2727. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  2728. }
  2729. while (this.state.pos < this.length) {
  2730. const ch = this.codePointAtPos(this.state.pos);
  2731. if (isIdentifierChar(ch)) {
  2732. this.state.pos += ch <= 0xffff ? 1 : 2;
  2733. } else if (ch === 92) {
  2734. this.state.containsEsc = true;
  2735. word += this.input.slice(chunkStart, this.state.pos);
  2736. const escStart = this.state.pos;
  2737. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  2738. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  2739. this.raise(this.state.pos, ErrorMessages.MissingUnicodeEscape);
  2740. chunkStart = this.state.pos - 1;
  2741. continue;
  2742. }
  2743. ++this.state.pos;
  2744. const esc = this.readCodePoint(true);
  2745. if (esc !== null) {
  2746. if (!identifierCheck(esc)) {
  2747. this.raise(escStart, ErrorMessages.EscapedCharNotAnIdentifier);
  2748. }
  2749. word += String.fromCodePoint(esc);
  2750. }
  2751. chunkStart = this.state.pos;
  2752. } else {
  2753. break;
  2754. }
  2755. }
  2756. return word + this.input.slice(chunkStart, this.state.pos);
  2757. }
  2758. readWord(firstCode) {
  2759. const word = this.readWord1(firstCode);
  2760. const type = keywords$1.get(word);
  2761. if (type !== undefined) {
  2762. this.finishToken(type, tokenLabelName(type));
  2763. } else {
  2764. this.finishToken(124, word);
  2765. }
  2766. }
  2767. checkKeywordEscapes() {
  2768. const {
  2769. type
  2770. } = this.state;
  2771. if (tokenIsKeyword(type) && this.state.containsEsc) {
  2772. this.raise(this.state.start, ErrorMessages.InvalidEscapedReservedWord, tokenLabelName(type));
  2773. }
  2774. }
  2775. updateContext(prevType) {}
  2776. }
  2777. class ClassScope {
  2778. constructor() {
  2779. this.privateNames = new Set();
  2780. this.loneAccessors = new Map();
  2781. this.undefinedPrivateNames = new Map();
  2782. }
  2783. }
  2784. class ClassScopeHandler {
  2785. constructor(raise) {
  2786. this.stack = [];
  2787. this.undefinedPrivateNames = new Map();
  2788. this.raise = raise;
  2789. }
  2790. current() {
  2791. return this.stack[this.stack.length - 1];
  2792. }
  2793. enter() {
  2794. this.stack.push(new ClassScope());
  2795. }
  2796. exit() {
  2797. const oldClassScope = this.stack.pop();
  2798. const current = this.current();
  2799. for (const [name, pos] of Array.from(oldClassScope.undefinedPrivateNames)) {
  2800. if (current) {
  2801. if (!current.undefinedPrivateNames.has(name)) {
  2802. current.undefinedPrivateNames.set(name, pos);
  2803. }
  2804. } else {
  2805. this.raise(pos, ErrorMessages.InvalidPrivateFieldResolution, name);
  2806. }
  2807. }
  2808. }
  2809. declarePrivateName(name, elementType, pos) {
  2810. const {
  2811. privateNames,
  2812. loneAccessors,
  2813. undefinedPrivateNames
  2814. } = this.current();
  2815. let redefined = privateNames.has(name);
  2816. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  2817. const accessor = redefined && loneAccessors.get(name);
  2818. if (accessor) {
  2819. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  2820. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  2821. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  2822. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  2823. redefined = oldKind === newKind || oldStatic !== newStatic;
  2824. if (!redefined) loneAccessors.delete(name);
  2825. } else if (!redefined) {
  2826. loneAccessors.set(name, elementType);
  2827. }
  2828. }
  2829. if (redefined) {
  2830. this.raise(pos, ErrorMessages.PrivateNameRedeclaration, name);
  2831. }
  2832. privateNames.add(name);
  2833. undefinedPrivateNames.delete(name);
  2834. }
  2835. usePrivateName(name, pos) {
  2836. let classScope;
  2837. for (classScope of this.stack) {
  2838. if (classScope.privateNames.has(name)) return;
  2839. }
  2840. if (classScope) {
  2841. classScope.undefinedPrivateNames.set(name, pos);
  2842. } else {
  2843. this.raise(pos, ErrorMessages.InvalidPrivateFieldResolution, name);
  2844. }
  2845. }
  2846. }
  2847. const kExpression = 0,
  2848. kMaybeArrowParameterDeclaration = 1,
  2849. kMaybeAsyncArrowParameterDeclaration = 2,
  2850. kParameterDeclaration = 3;
  2851. class ExpressionScope {
  2852. constructor(type = kExpression) {
  2853. this.type = void 0;
  2854. this.type = type;
  2855. }
  2856. canBeArrowParameterDeclaration() {
  2857. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  2858. }
  2859. isCertainlyParameterDeclaration() {
  2860. return this.type === kParameterDeclaration;
  2861. }
  2862. }
  2863. class ArrowHeadParsingScope extends ExpressionScope {
  2864. constructor(type) {
  2865. super(type);
  2866. this.errors = new Map();
  2867. }
  2868. recordDeclarationError(pos, template) {
  2869. this.errors.set(pos, template);
  2870. }
  2871. clearDeclarationError(pos) {
  2872. this.errors.delete(pos);
  2873. }
  2874. iterateErrors(iterator) {
  2875. this.errors.forEach(iterator);
  2876. }
  2877. }
  2878. class ExpressionScopeHandler {
  2879. constructor(raise) {
  2880. this.stack = [new ExpressionScope()];
  2881. this.raise = raise;
  2882. }
  2883. enter(scope) {
  2884. this.stack.push(scope);
  2885. }
  2886. exit() {
  2887. this.stack.pop();
  2888. }
  2889. recordParameterInitializerError(pos, template) {
  2890. const {
  2891. stack
  2892. } = this;
  2893. let i = stack.length - 1;
  2894. let scope = stack[i];
  2895. while (!scope.isCertainlyParameterDeclaration()) {
  2896. if (scope.canBeArrowParameterDeclaration()) {
  2897. scope.recordDeclarationError(pos, template);
  2898. } else {
  2899. return;
  2900. }
  2901. scope = stack[--i];
  2902. }
  2903. this.raise(pos, template);
  2904. }
  2905. recordParenthesizedIdentifierError(pos, template) {
  2906. const {
  2907. stack
  2908. } = this;
  2909. const scope = stack[stack.length - 1];
  2910. if (scope.isCertainlyParameterDeclaration()) {
  2911. this.raise(pos, template);
  2912. } else if (scope.canBeArrowParameterDeclaration()) {
  2913. scope.recordDeclarationError(pos, template);
  2914. } else {
  2915. return;
  2916. }
  2917. }
  2918. recordAsyncArrowParametersError(pos, template) {
  2919. const {
  2920. stack
  2921. } = this;
  2922. let i = stack.length - 1;
  2923. let scope = stack[i];
  2924. while (scope.canBeArrowParameterDeclaration()) {
  2925. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  2926. scope.recordDeclarationError(pos, template);
  2927. }
  2928. scope = stack[--i];
  2929. }
  2930. }
  2931. validateAsPattern() {
  2932. const {
  2933. stack
  2934. } = this;
  2935. const currentScope = stack[stack.length - 1];
  2936. if (!currentScope.canBeArrowParameterDeclaration()) return;
  2937. currentScope.iterateErrors((template, pos) => {
  2938. this.raise(pos, template);
  2939. let i = stack.length - 2;
  2940. let scope = stack[i];
  2941. while (scope.canBeArrowParameterDeclaration()) {
  2942. scope.clearDeclarationError(pos);
  2943. scope = stack[--i];
  2944. }
  2945. });
  2946. }
  2947. }
  2948. function newParameterDeclarationScope() {
  2949. return new ExpressionScope(kParameterDeclaration);
  2950. }
  2951. function newArrowHeadScope() {
  2952. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  2953. }
  2954. function newAsyncArrowScope() {
  2955. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  2956. }
  2957. function newExpressionScope() {
  2958. return new ExpressionScope();
  2959. }
  2960. const PARAM = 0b0000,
  2961. PARAM_YIELD = 0b0001,
  2962. PARAM_AWAIT = 0b0010,
  2963. PARAM_RETURN = 0b0100,
  2964. PARAM_IN = 0b1000;
  2965. class ProductionParameterHandler {
  2966. constructor() {
  2967. this.stacks = [];
  2968. }
  2969. enter(flags) {
  2970. this.stacks.push(flags);
  2971. }
  2972. exit() {
  2973. this.stacks.pop();
  2974. }
  2975. currentFlags() {
  2976. return this.stacks[this.stacks.length - 1];
  2977. }
  2978. get hasAwait() {
  2979. return (this.currentFlags() & PARAM_AWAIT) > 0;
  2980. }
  2981. get hasYield() {
  2982. return (this.currentFlags() & PARAM_YIELD) > 0;
  2983. }
  2984. get hasReturn() {
  2985. return (this.currentFlags() & PARAM_RETURN) > 0;
  2986. }
  2987. get hasIn() {
  2988. return (this.currentFlags() & PARAM_IN) > 0;
  2989. }
  2990. }
  2991. function functionFlags(isAsync, isGenerator) {
  2992. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  2993. }
  2994. class UtilParser extends Tokenizer {
  2995. addExtra(node, key, val) {
  2996. if (!node) return;
  2997. const extra = node.extra = node.extra || {};
  2998. extra[key] = val;
  2999. }
  3000. isContextual(token) {
  3001. return this.state.type === token && !this.state.containsEsc;
  3002. }
  3003. isUnparsedContextual(nameStart, name) {
  3004. const nameEnd = nameStart + name.length;
  3005. if (this.input.slice(nameStart, nameEnd) === name) {
  3006. const nextCh = this.input.charCodeAt(nameEnd);
  3007. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  3008. }
  3009. return false;
  3010. }
  3011. isLookaheadContextual(name) {
  3012. const next = this.nextTokenStart();
  3013. return this.isUnparsedContextual(next, name);
  3014. }
  3015. eatContextual(token) {
  3016. if (this.isContextual(token)) {
  3017. this.next();
  3018. return true;
  3019. }
  3020. return false;
  3021. }
  3022. expectContextual(token, template) {
  3023. if (!this.eatContextual(token)) this.unexpected(null, template);
  3024. }
  3025. canInsertSemicolon() {
  3026. return this.match(131) || this.match(8) || this.hasPrecedingLineBreak();
  3027. }
  3028. hasPrecedingLineBreak() {
  3029. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  3030. }
  3031. hasFollowingLineBreak() {
  3032. skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
  3033. return skipWhiteSpaceToLineBreak.test(this.input);
  3034. }
  3035. isLineTerminator() {
  3036. return this.eat(13) || this.canInsertSemicolon();
  3037. }
  3038. semicolon(allowAsi = true) {
  3039. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  3040. this.raise(this.state.lastTokEnd, ErrorMessages.MissingSemicolon);
  3041. }
  3042. expect(type, pos) {
  3043. this.eat(type) || this.unexpected(pos, type);
  3044. }
  3045. assertNoSpace(message = "Unexpected space.") {
  3046. if (this.state.start > this.state.lastTokEnd) {
  3047. this.raise(this.state.lastTokEnd, {
  3048. code: ErrorCodes.SyntaxError,
  3049. reasonCode: "UnexpectedSpace",
  3050. template: message
  3051. });
  3052. }
  3053. }
  3054. unexpected(pos, messageOrType = {
  3055. code: ErrorCodes.SyntaxError,
  3056. reasonCode: "UnexpectedToken",
  3057. template: "Unexpected token"
  3058. }) {
  3059. if (isTokenType(messageOrType)) {
  3060. messageOrType = {
  3061. code: ErrorCodes.SyntaxError,
  3062. reasonCode: "UnexpectedToken",
  3063. template: `Unexpected token, expected "${tokenLabelName(messageOrType)}"`
  3064. };
  3065. }
  3066. throw this.raise(pos != null ? pos : this.state.start, messageOrType);
  3067. }
  3068. getPluginNamesFromConfigs(pluginConfigs) {
  3069. return pluginConfigs.map(c => {
  3070. if (typeof c === "string") {
  3071. return c;
  3072. } else {
  3073. return c[0];
  3074. }
  3075. });
  3076. }
  3077. expectPlugin(pluginConfig, pos) {
  3078. if (!this.hasPlugin(pluginConfig)) {
  3079. throw this.raiseWithData(pos != null ? pos : this.state.start, {
  3080. missingPlugin: this.getPluginNamesFromConfigs([pluginConfig])
  3081. }, `This experimental syntax requires enabling the parser plugin: ${JSON.stringify(pluginConfig)}.`);
  3082. }
  3083. return true;
  3084. }
  3085. expectOnePlugin(pluginConfigs, pos) {
  3086. if (!pluginConfigs.some(c => this.hasPlugin(c))) {
  3087. throw this.raiseWithData(pos != null ? pos : this.state.start, {
  3088. missingPlugin: this.getPluginNamesFromConfigs(pluginConfigs)
  3089. }, `This experimental syntax requires enabling one of the following parser plugin(s): ${pluginConfigs.map(c => JSON.stringify(c)).join(", ")}.`);
  3090. }
  3091. }
  3092. tryParse(fn, oldState = this.state.clone()) {
  3093. const abortSignal = {
  3094. node: null
  3095. };
  3096. try {
  3097. const node = fn((node = null) => {
  3098. abortSignal.node = node;
  3099. throw abortSignal;
  3100. });
  3101. if (this.state.errors.length > oldState.errors.length) {
  3102. const failState = this.state;
  3103. this.state = oldState;
  3104. this.state.tokensLength = failState.tokensLength;
  3105. return {
  3106. node,
  3107. error: failState.errors[oldState.errors.length],
  3108. thrown: false,
  3109. aborted: false,
  3110. failState
  3111. };
  3112. }
  3113. return {
  3114. node,
  3115. error: null,
  3116. thrown: false,
  3117. aborted: false,
  3118. failState: null
  3119. };
  3120. } catch (error) {
  3121. const failState = this.state;
  3122. this.state = oldState;
  3123. if (error instanceof SyntaxError) {
  3124. return {
  3125. node: null,
  3126. error,
  3127. thrown: true,
  3128. aborted: false,
  3129. failState
  3130. };
  3131. }
  3132. if (error === abortSignal) {
  3133. return {
  3134. node: abortSignal.node,
  3135. error: null,
  3136. thrown: false,
  3137. aborted: true,
  3138. failState
  3139. };
  3140. }
  3141. throw error;
  3142. }
  3143. }
  3144. checkExpressionErrors(refExpressionErrors, andThrow) {
  3145. if (!refExpressionErrors) return false;
  3146. const {
  3147. shorthandAssign,
  3148. doubleProto,
  3149. optionalParameters
  3150. } = refExpressionErrors;
  3151. const hasErrors = shorthandAssign + doubleProto + optionalParameters > -3;
  3152. if (!andThrow) {
  3153. return hasErrors;
  3154. } else if (hasErrors) {
  3155. if (shorthandAssign >= 0) {
  3156. this.raise(shorthandAssign, ErrorMessages.InvalidCoverInitializedName);
  3157. }
  3158. if (doubleProto >= 0) {
  3159. this.raise(doubleProto, ErrorMessages.DuplicateProto);
  3160. }
  3161. if (optionalParameters >= 0) {
  3162. this.unexpected(optionalParameters);
  3163. }
  3164. }
  3165. }
  3166. isLiteralPropertyName() {
  3167. return tokenIsLiteralPropertyName(this.state.type);
  3168. }
  3169. isPrivateName(node) {
  3170. return node.type === "PrivateName";
  3171. }
  3172. getPrivateNameSV(node) {
  3173. return node.id.name;
  3174. }
  3175. hasPropertyAsPrivateName(node) {
  3176. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  3177. }
  3178. isOptionalChain(node) {
  3179. return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
  3180. }
  3181. isObjectProperty(node) {
  3182. return node.type === "ObjectProperty";
  3183. }
  3184. isObjectMethod(node) {
  3185. return node.type === "ObjectMethod";
  3186. }
  3187. initializeScopes(inModule = this.options.sourceType === "module") {
  3188. const oldLabels = this.state.labels;
  3189. this.state.labels = [];
  3190. const oldExportedIdentifiers = this.exportedIdentifiers;
  3191. this.exportedIdentifiers = new Set();
  3192. const oldInModule = this.inModule;
  3193. this.inModule = inModule;
  3194. const oldScope = this.scope;
  3195. const ScopeHandler = this.getScopeHandler();
  3196. this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
  3197. const oldProdParam = this.prodParam;
  3198. this.prodParam = new ProductionParameterHandler();
  3199. const oldClassScope = this.classScope;
  3200. this.classScope = new ClassScopeHandler(this.raise.bind(this));
  3201. const oldExpressionScope = this.expressionScope;
  3202. this.expressionScope = new ExpressionScopeHandler(this.raise.bind(this));
  3203. return () => {
  3204. this.state.labels = oldLabels;
  3205. this.exportedIdentifiers = oldExportedIdentifiers;
  3206. this.inModule = oldInModule;
  3207. this.scope = oldScope;
  3208. this.prodParam = oldProdParam;
  3209. this.classScope = oldClassScope;
  3210. this.expressionScope = oldExpressionScope;
  3211. };
  3212. }
  3213. enterInitialScopes() {
  3214. let paramFlags = PARAM;
  3215. if (this.inModule) {
  3216. paramFlags |= PARAM_AWAIT;
  3217. }
  3218. this.scope.enter(SCOPE_PROGRAM);
  3219. this.prodParam.enter(paramFlags);
  3220. }
  3221. }
  3222. class ExpressionErrors {
  3223. constructor() {
  3224. this.shorthandAssign = -1;
  3225. this.doubleProto = -1;
  3226. this.optionalParameters = -1;
  3227. }
  3228. }
  3229. class Node {
  3230. constructor(parser, pos, loc) {
  3231. this.type = "";
  3232. this.start = pos;
  3233. this.end = 0;
  3234. this.loc = new SourceLocation(loc);
  3235. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  3236. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  3237. }
  3238. }
  3239. const NodePrototype = Node.prototype;
  3240. {
  3241. NodePrototype.__clone = function () {
  3242. const newNode = new Node();
  3243. const keys = Object.keys(this);
  3244. for (let i = 0, length = keys.length; i < length; i++) {
  3245. const key = keys[i];
  3246. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  3247. newNode[key] = this[key];
  3248. }
  3249. }
  3250. return newNode;
  3251. };
  3252. }
  3253. function clonePlaceholder(node) {
  3254. return cloneIdentifier(node);
  3255. }
  3256. function cloneIdentifier(node) {
  3257. const {
  3258. type,
  3259. start,
  3260. end,
  3261. loc,
  3262. range,
  3263. extra,
  3264. name
  3265. } = node;
  3266. const cloned = Object.create(NodePrototype);
  3267. cloned.type = type;
  3268. cloned.start = start;
  3269. cloned.end = end;
  3270. cloned.loc = loc;
  3271. cloned.range = range;
  3272. cloned.extra = extra;
  3273. cloned.name = name;
  3274. if (type === "Placeholder") {
  3275. cloned.expectedNode = node.expectedNode;
  3276. }
  3277. return cloned;
  3278. }
  3279. function cloneStringLiteral(node) {
  3280. const {
  3281. type,
  3282. start,
  3283. end,
  3284. loc,
  3285. range,
  3286. extra
  3287. } = node;
  3288. if (type === "Placeholder") {
  3289. return clonePlaceholder(node);
  3290. }
  3291. const cloned = Object.create(NodePrototype);
  3292. cloned.type = type;
  3293. cloned.start = start;
  3294. cloned.end = end;
  3295. cloned.loc = loc;
  3296. cloned.range = range;
  3297. if (node.raw !== undefined) {
  3298. cloned.raw = node.raw;
  3299. } else {
  3300. cloned.extra = extra;
  3301. }
  3302. cloned.value = node.value;
  3303. return cloned;
  3304. }
  3305. class NodeUtils extends UtilParser {
  3306. startNode() {
  3307. return new Node(this, this.state.start, this.state.startLoc);
  3308. }
  3309. startNodeAt(pos, loc) {
  3310. return new Node(this, pos, loc);
  3311. }
  3312. startNodeAtNode(type) {
  3313. return this.startNodeAt(type.start, type.loc.start);
  3314. }
  3315. finishNode(node, type) {
  3316. return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc);
  3317. }
  3318. finishNodeAt(node, type, pos, loc) {
  3319. node.type = type;
  3320. node.end = pos;
  3321. node.loc.end = loc;
  3322. if (this.options.ranges) node.range[1] = pos;
  3323. if (this.options.attachComment) this.processComment(node);
  3324. return node;
  3325. }
  3326. resetStartLocation(node, start, startLoc) {
  3327. node.start = start;
  3328. node.loc.start = startLoc;
  3329. if (this.options.ranges) node.range[0] = start;
  3330. }
  3331. resetEndLocation(node, end = this.state.lastTokEnd, endLoc = this.state.lastTokEndLoc) {
  3332. node.end = end;
  3333. node.loc.end = endLoc;
  3334. if (this.options.ranges) node.range[1] = end;
  3335. }
  3336. resetStartLocationFromNode(node, locationNode) {
  3337. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  3338. }
  3339. }
  3340. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3341. const FlowErrors = makeErrorTemplates({
  3342. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  3343. 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.",
  3344. AssignReservedType: "Cannot overwrite reserved type %0.",
  3345. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  3346. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  3347. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  3348. EnumBooleanMemberNotInitialized: "Boolean enum members need to be initialized. Use either `%0 = true,` or `%0 = false,` in enum `%1`.",
  3349. EnumDuplicateMemberName: "Enum member names need to be unique, but the name `%0` has already been used before in enum `%1`.",
  3350. EnumInconsistentMemberValues: "Enum `%0` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.",
  3351. EnumInvalidExplicitType: "Enum type `%1` is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  3352. EnumInvalidExplicitTypeUnknownSupplied: "Supplied enum type is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  3353. EnumInvalidMemberInitializerPrimaryType: "Enum `%0` has type `%2`, so the initializer of `%1` needs to be a %2 literal.",
  3354. EnumInvalidMemberInitializerSymbolType: "Symbol enum members cannot be initialized. Use `%1,` in enum `%0`.",
  3355. EnumInvalidMemberInitializerUnknownType: "The enum member initializer for `%1` needs to be a literal (either a boolean, number, or string) in enum `%0`.",
  3356. EnumInvalidMemberName: "Enum member names cannot start with lowercase 'a' through 'z'. Instead of using `%0`, consider using `%1`, in enum `%2`.",
  3357. EnumNumberMemberNotInitialized: "Number enum members need to be initialized, e.g. `%1 = 1` in enum `%0`.",
  3358. EnumStringMemberInconsistentlyInitailized: "String enum members need to consistently either all use initializers, or use no initializers, in enum `%0`.",
  3359. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  3360. 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.",
  3361. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  3362. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  3363. InexactVariance: "Explicit inexact syntax cannot have variance.",
  3364. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  3365. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  3366. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  3367. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  3368. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  3369. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  3370. SpreadVariance: "Spread properties cannot have variance.",
  3371. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  3372. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  3373. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  3374. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  3375. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  3376. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  3377. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  3378. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  3379. UnexpectedReservedType: "Unexpected reserved type %0.",
  3380. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  3381. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  3382. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  3383. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  3384. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  3385. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  3386. UnsupportedDeclareExportKind: "`declare export %0` is not supported. Use `%1` instead.",
  3387. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  3388. UnterminatedFlowComment: "Unterminated flow-comment."
  3389. }, ErrorCodes.SyntaxError, "flow");
  3390. function isEsModuleType(bodyElement) {
  3391. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3392. }
  3393. function hasTypeImportKind(node) {
  3394. return node.importKind === "type" || node.importKind === "typeof";
  3395. }
  3396. function isMaybeDefaultImport(type) {
  3397. return tokenIsKeywordOrIdentifier(type) && type !== 93;
  3398. }
  3399. const exportSuggestions = {
  3400. const: "declare export var",
  3401. let: "declare export var",
  3402. type: "export type",
  3403. interface: "export interface"
  3404. };
  3405. function partition(list, test) {
  3406. const list1 = [];
  3407. const list2 = [];
  3408. for (let i = 0; i < list.length; i++) {
  3409. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  3410. }
  3411. return [list1, list2];
  3412. }
  3413. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  3414. var flow = (superClass => class extends superClass {
  3415. constructor(...args) {
  3416. super(...args);
  3417. this.flowPragma = undefined;
  3418. }
  3419. getScopeHandler() {
  3420. return FlowScopeHandler;
  3421. }
  3422. shouldParseTypes() {
  3423. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  3424. }
  3425. shouldParseEnums() {
  3426. return !!this.getPluginOption("flow", "enums");
  3427. }
  3428. finishToken(type, val) {
  3429. if (type !== 125 && type !== 13 && type !== 28) {
  3430. if (this.flowPragma === undefined) {
  3431. this.flowPragma = null;
  3432. }
  3433. }
  3434. return super.finishToken(type, val);
  3435. }
  3436. addComment(comment) {
  3437. if (this.flowPragma === undefined) {
  3438. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  3439. if (!matches) ; else if (matches[1] === "flow") {
  3440. this.flowPragma = "flow";
  3441. } else if (matches[1] === "noflow") {
  3442. this.flowPragma = "noflow";
  3443. } else {
  3444. throw new Error("Unexpected flow pragma");
  3445. }
  3446. }
  3447. return super.addComment(comment);
  3448. }
  3449. flowParseTypeInitialiser(tok) {
  3450. const oldInType = this.state.inType;
  3451. this.state.inType = true;
  3452. this.expect(tok || 14);
  3453. const type = this.flowParseType();
  3454. this.state.inType = oldInType;
  3455. return type;
  3456. }
  3457. flowParsePredicate() {
  3458. const node = this.startNode();
  3459. const moduloPos = this.state.start;
  3460. this.next();
  3461. this.expectContextual(103);
  3462. if (this.state.lastTokStart > moduloPos + 1) {
  3463. this.raise(moduloPos, FlowErrors.UnexpectedSpaceBetweenModuloChecks);
  3464. }
  3465. if (this.eat(10)) {
  3466. node.value = this.parseExpression();
  3467. this.expect(11);
  3468. return this.finishNode(node, "DeclaredPredicate");
  3469. } else {
  3470. return this.finishNode(node, "InferredPredicate");
  3471. }
  3472. }
  3473. flowParseTypeAndPredicateInitialiser() {
  3474. const oldInType = this.state.inType;
  3475. this.state.inType = true;
  3476. this.expect(14);
  3477. let type = null;
  3478. let predicate = null;
  3479. if (this.match(50)) {
  3480. this.state.inType = oldInType;
  3481. predicate = this.flowParsePredicate();
  3482. } else {
  3483. type = this.flowParseType();
  3484. this.state.inType = oldInType;
  3485. if (this.match(50)) {
  3486. predicate = this.flowParsePredicate();
  3487. }
  3488. }
  3489. return [type, predicate];
  3490. }
  3491. flowParseDeclareClass(node) {
  3492. this.next();
  3493. this.flowParseInterfaceish(node, true);
  3494. return this.finishNode(node, "DeclareClass");
  3495. }
  3496. flowParseDeclareFunction(node) {
  3497. this.next();
  3498. const id = node.id = this.parseIdentifier();
  3499. const typeNode = this.startNode();
  3500. const typeContainer = this.startNode();
  3501. if (this.match(45)) {
  3502. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  3503. } else {
  3504. typeNode.typeParameters = null;
  3505. }
  3506. this.expect(10);
  3507. const tmp = this.flowParseFunctionTypeParams();
  3508. typeNode.params = tmp.params;
  3509. typeNode.rest = tmp.rest;
  3510. typeNode.this = tmp._this;
  3511. this.expect(11);
  3512. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  3513. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  3514. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  3515. this.resetEndLocation(id);
  3516. this.semicolon();
  3517. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.start);
  3518. return this.finishNode(node, "DeclareFunction");
  3519. }
  3520. flowParseDeclare(node, insideModule) {
  3521. if (this.match(76)) {
  3522. return this.flowParseDeclareClass(node);
  3523. } else if (this.match(64)) {
  3524. return this.flowParseDeclareFunction(node);
  3525. } else if (this.match(70)) {
  3526. return this.flowParseDeclareVariable(node);
  3527. } else if (this.eatContextual(119)) {
  3528. if (this.match(16)) {
  3529. return this.flowParseDeclareModuleExports(node);
  3530. } else {
  3531. if (insideModule) {
  3532. this.raise(this.state.lastTokStart, FlowErrors.NestedDeclareModule);
  3533. }
  3534. return this.flowParseDeclareModule(node);
  3535. }
  3536. } else if (this.isContextual(122)) {
  3537. return this.flowParseDeclareTypeAlias(node);
  3538. } else if (this.isContextual(123)) {
  3539. return this.flowParseDeclareOpaqueType(node);
  3540. } else if (this.isContextual(121)) {
  3541. return this.flowParseDeclareInterface(node);
  3542. } else if (this.match(78)) {
  3543. return this.flowParseDeclareExportDeclaration(node, insideModule);
  3544. } else {
  3545. throw this.unexpected();
  3546. }
  3547. }
  3548. flowParseDeclareVariable(node) {
  3549. this.next();
  3550. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  3551. this.scope.declareName(node.id.name, BIND_VAR, node.id.start);
  3552. this.semicolon();
  3553. return this.finishNode(node, "DeclareVariable");
  3554. }
  3555. flowParseDeclareModule(node) {
  3556. this.scope.enter(SCOPE_OTHER);
  3557. if (this.match(125)) {
  3558. node.id = this.parseExprAtom();
  3559. } else {
  3560. node.id = this.parseIdentifier();
  3561. }
  3562. const bodyNode = node.body = this.startNode();
  3563. const body = bodyNode.body = [];
  3564. this.expect(5);
  3565. while (!this.match(8)) {
  3566. let bodyNode = this.startNode();
  3567. if (this.match(79)) {
  3568. this.next();
  3569. if (!this.isContextual(122) && !this.match(83)) {
  3570. this.raise(this.state.lastTokStart, FlowErrors.InvalidNonTypeImportInDeclareModule);
  3571. }
  3572. this.parseImport(bodyNode);
  3573. } else {
  3574. this.expectContextual(117, FlowErrors.UnsupportedStatementInDeclareModule);
  3575. bodyNode = this.flowParseDeclare(bodyNode, true);
  3576. }
  3577. body.push(bodyNode);
  3578. }
  3579. this.scope.exit();
  3580. this.expect(8);
  3581. this.finishNode(bodyNode, "BlockStatement");
  3582. let kind = null;
  3583. let hasModuleExport = false;
  3584. body.forEach(bodyElement => {
  3585. if (isEsModuleType(bodyElement)) {
  3586. if (kind === "CommonJS") {
  3587. this.raise(bodyElement.start, FlowErrors.AmbiguousDeclareModuleKind);
  3588. }
  3589. kind = "ES";
  3590. } else if (bodyElement.type === "DeclareModuleExports") {
  3591. if (hasModuleExport) {
  3592. this.raise(bodyElement.start, FlowErrors.DuplicateDeclareModuleExports);
  3593. }
  3594. if (kind === "ES") {
  3595. this.raise(bodyElement.start, FlowErrors.AmbiguousDeclareModuleKind);
  3596. }
  3597. kind = "CommonJS";
  3598. hasModuleExport = true;
  3599. }
  3600. });
  3601. node.kind = kind || "CommonJS";
  3602. return this.finishNode(node, "DeclareModule");
  3603. }
  3604. flowParseDeclareExportDeclaration(node, insideModule) {
  3605. this.expect(78);
  3606. if (this.eat(61)) {
  3607. if (this.match(64) || this.match(76)) {
  3608. node.declaration = this.flowParseDeclare(this.startNode());
  3609. } else {
  3610. node.declaration = this.flowParseType();
  3611. this.semicolon();
  3612. }
  3613. node.default = true;
  3614. return this.finishNode(node, "DeclareExportDeclaration");
  3615. } else {
  3616. if (this.match(71) || this.isLet() || (this.isContextual(122) || this.isContextual(121)) && !insideModule) {
  3617. const label = this.state.value;
  3618. const suggestion = exportSuggestions[label];
  3619. throw this.raise(this.state.start, FlowErrors.UnsupportedDeclareExportKind, label, suggestion);
  3620. }
  3621. if (this.match(70) || this.match(64) || this.match(76) || this.isContextual(123)) {
  3622. node.declaration = this.flowParseDeclare(this.startNode());
  3623. node.default = false;
  3624. return this.finishNode(node, "DeclareExportDeclaration");
  3625. } else if (this.match(51) || this.match(5) || this.isContextual(121) || this.isContextual(122) || this.isContextual(123)) {
  3626. node = this.parseExport(node);
  3627. if (node.type === "ExportNamedDeclaration") {
  3628. node.type = "ExportDeclaration";
  3629. node.default = false;
  3630. delete node.exportKind;
  3631. }
  3632. node.type = "Declare" + node.type;
  3633. return node;
  3634. }
  3635. }
  3636. throw this.unexpected();
  3637. }
  3638. flowParseDeclareModuleExports(node) {
  3639. this.next();
  3640. this.expectContextual(104);
  3641. node.typeAnnotation = this.flowParseTypeAnnotation();
  3642. this.semicolon();
  3643. return this.finishNode(node, "DeclareModuleExports");
  3644. }
  3645. flowParseDeclareTypeAlias(node) {
  3646. this.next();
  3647. this.flowParseTypeAlias(node);
  3648. node.type = "DeclareTypeAlias";
  3649. return node;
  3650. }
  3651. flowParseDeclareOpaqueType(node) {
  3652. this.next();
  3653. this.flowParseOpaqueType(node, true);
  3654. node.type = "DeclareOpaqueType";
  3655. return node;
  3656. }
  3657. flowParseDeclareInterface(node) {
  3658. this.next();
  3659. this.flowParseInterfaceish(node);
  3660. return this.finishNode(node, "DeclareInterface");
  3661. }
  3662. flowParseInterfaceish(node, isClass = false) {
  3663. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  3664. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.start);
  3665. if (this.match(45)) {
  3666. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3667. } else {
  3668. node.typeParameters = null;
  3669. }
  3670. node.extends = [];
  3671. node.implements = [];
  3672. node.mixins = [];
  3673. if (this.eat(77)) {
  3674. do {
  3675. node.extends.push(this.flowParseInterfaceExtends());
  3676. } while (!isClass && this.eat(12));
  3677. }
  3678. if (this.isContextual(110)) {
  3679. this.next();
  3680. do {
  3681. node.mixins.push(this.flowParseInterfaceExtends());
  3682. } while (this.eat(12));
  3683. }
  3684. if (this.isContextual(106)) {
  3685. this.next();
  3686. do {
  3687. node.implements.push(this.flowParseInterfaceExtends());
  3688. } while (this.eat(12));
  3689. }
  3690. node.body = this.flowParseObjectType({
  3691. allowStatic: isClass,
  3692. allowExact: false,
  3693. allowSpread: false,
  3694. allowProto: isClass,
  3695. allowInexact: false
  3696. });
  3697. }
  3698. flowParseInterfaceExtends() {
  3699. const node = this.startNode();
  3700. node.id = this.flowParseQualifiedTypeIdentifier();
  3701. if (this.match(45)) {
  3702. node.typeParameters = this.flowParseTypeParameterInstantiation();
  3703. } else {
  3704. node.typeParameters = null;
  3705. }
  3706. return this.finishNode(node, "InterfaceExtends");
  3707. }
  3708. flowParseInterface(node) {
  3709. this.flowParseInterfaceish(node);
  3710. return this.finishNode(node, "InterfaceDeclaration");
  3711. }
  3712. checkNotUnderscore(word) {
  3713. if (word === "_") {
  3714. this.raise(this.state.start, FlowErrors.UnexpectedReservedUnderscore);
  3715. }
  3716. }
  3717. checkReservedType(word, startLoc, declaration) {
  3718. if (!reservedTypes.has(word)) return;
  3719. this.raise(startLoc, declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, word);
  3720. }
  3721. flowParseRestrictedIdentifier(liberal, declaration) {
  3722. this.checkReservedType(this.state.value, this.state.start, declaration);
  3723. return this.parseIdentifier(liberal);
  3724. }
  3725. flowParseTypeAlias(node) {
  3726. node.id = this.flowParseRestrictedIdentifier(false, true);
  3727. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  3728. if (this.match(45)) {
  3729. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3730. } else {
  3731. node.typeParameters = null;
  3732. }
  3733. node.right = this.flowParseTypeInitialiser(29);
  3734. this.semicolon();
  3735. return this.finishNode(node, "TypeAlias");
  3736. }
  3737. flowParseOpaqueType(node, declare) {
  3738. this.expectContextual(122);
  3739. node.id = this.flowParseRestrictedIdentifier(true, true);
  3740. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  3741. if (this.match(45)) {
  3742. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3743. } else {
  3744. node.typeParameters = null;
  3745. }
  3746. node.supertype = null;
  3747. if (this.match(14)) {
  3748. node.supertype = this.flowParseTypeInitialiser(14);
  3749. }
  3750. node.impltype = null;
  3751. if (!declare) {
  3752. node.impltype = this.flowParseTypeInitialiser(29);
  3753. }
  3754. this.semicolon();
  3755. return this.finishNode(node, "OpaqueType");
  3756. }
  3757. flowParseTypeParameter(requireDefault = false) {
  3758. const nodeStart = this.state.start;
  3759. const node = this.startNode();
  3760. const variance = this.flowParseVariance();
  3761. const ident = this.flowParseTypeAnnotatableIdentifier();
  3762. node.name = ident.name;
  3763. node.variance = variance;
  3764. node.bound = ident.typeAnnotation;
  3765. if (this.match(29)) {
  3766. this.eat(29);
  3767. node.default = this.flowParseType();
  3768. } else {
  3769. if (requireDefault) {
  3770. this.raise(nodeStart, FlowErrors.MissingTypeParamDefault);
  3771. }
  3772. }
  3773. return this.finishNode(node, "TypeParameter");
  3774. }
  3775. flowParseTypeParameterDeclaration() {
  3776. const oldInType = this.state.inType;
  3777. const node = this.startNode();
  3778. node.params = [];
  3779. this.state.inType = true;
  3780. if (this.match(45) || this.match(134)) {
  3781. this.next();
  3782. } else {
  3783. this.unexpected();
  3784. }
  3785. let defaultRequired = false;
  3786. do {
  3787. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  3788. node.params.push(typeParameter);
  3789. if (typeParameter.default) {
  3790. defaultRequired = true;
  3791. }
  3792. if (!this.match(46)) {
  3793. this.expect(12);
  3794. }
  3795. } while (!this.match(46));
  3796. this.expect(46);
  3797. this.state.inType = oldInType;
  3798. return this.finishNode(node, "TypeParameterDeclaration");
  3799. }
  3800. flowParseTypeParameterInstantiation() {
  3801. const node = this.startNode();
  3802. const oldInType = this.state.inType;
  3803. node.params = [];
  3804. this.state.inType = true;
  3805. this.expect(45);
  3806. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3807. this.state.noAnonFunctionType = false;
  3808. while (!this.match(46)) {
  3809. node.params.push(this.flowParseType());
  3810. if (!this.match(46)) {
  3811. this.expect(12);
  3812. }
  3813. }
  3814. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3815. this.expect(46);
  3816. this.state.inType = oldInType;
  3817. return this.finishNode(node, "TypeParameterInstantiation");
  3818. }
  3819. flowParseTypeParameterInstantiationCallOrNew() {
  3820. const node = this.startNode();
  3821. const oldInType = this.state.inType;
  3822. node.params = [];
  3823. this.state.inType = true;
  3824. this.expect(45);
  3825. while (!this.match(46)) {
  3826. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  3827. if (!this.match(46)) {
  3828. this.expect(12);
  3829. }
  3830. }
  3831. this.expect(46);
  3832. this.state.inType = oldInType;
  3833. return this.finishNode(node, "TypeParameterInstantiation");
  3834. }
  3835. flowParseInterfaceType() {
  3836. const node = this.startNode();
  3837. this.expectContextual(121);
  3838. node.extends = [];
  3839. if (this.eat(77)) {
  3840. do {
  3841. node.extends.push(this.flowParseInterfaceExtends());
  3842. } while (this.eat(12));
  3843. }
  3844. node.body = this.flowParseObjectType({
  3845. allowStatic: false,
  3846. allowExact: false,
  3847. allowSpread: false,
  3848. allowProto: false,
  3849. allowInexact: false
  3850. });
  3851. return this.finishNode(node, "InterfaceTypeAnnotation");
  3852. }
  3853. flowParseObjectPropertyKey() {
  3854. return this.match(126) || this.match(125) ? this.parseExprAtom() : this.parseIdentifier(true);
  3855. }
  3856. flowParseObjectTypeIndexer(node, isStatic, variance) {
  3857. node.static = isStatic;
  3858. if (this.lookahead().type === 14) {
  3859. node.id = this.flowParseObjectPropertyKey();
  3860. node.key = this.flowParseTypeInitialiser();
  3861. } else {
  3862. node.id = null;
  3863. node.key = this.flowParseType();
  3864. }
  3865. this.expect(3);
  3866. node.value = this.flowParseTypeInitialiser();
  3867. node.variance = variance;
  3868. return this.finishNode(node, "ObjectTypeIndexer");
  3869. }
  3870. flowParseObjectTypeInternalSlot(node, isStatic) {
  3871. node.static = isStatic;
  3872. node.id = this.flowParseObjectPropertyKey();
  3873. this.expect(3);
  3874. this.expect(3);
  3875. if (this.match(45) || this.match(10)) {
  3876. node.method = true;
  3877. node.optional = false;
  3878. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  3879. } else {
  3880. node.method = false;
  3881. if (this.eat(17)) {
  3882. node.optional = true;
  3883. }
  3884. node.value = this.flowParseTypeInitialiser();
  3885. }
  3886. return this.finishNode(node, "ObjectTypeInternalSlot");
  3887. }
  3888. flowParseObjectTypeMethodish(node) {
  3889. node.params = [];
  3890. node.rest = null;
  3891. node.typeParameters = null;
  3892. node.this = null;
  3893. if (this.match(45)) {
  3894. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3895. }
  3896. this.expect(10);
  3897. if (this.match(74)) {
  3898. node.this = this.flowParseFunctionTypeParam(true);
  3899. node.this.name = null;
  3900. if (!this.match(11)) {
  3901. this.expect(12);
  3902. }
  3903. }
  3904. while (!this.match(11) && !this.match(21)) {
  3905. node.params.push(this.flowParseFunctionTypeParam(false));
  3906. if (!this.match(11)) {
  3907. this.expect(12);
  3908. }
  3909. }
  3910. if (this.eat(21)) {
  3911. node.rest = this.flowParseFunctionTypeParam(false);
  3912. }
  3913. this.expect(11);
  3914. node.returnType = this.flowParseTypeInitialiser();
  3915. return this.finishNode(node, "FunctionTypeAnnotation");
  3916. }
  3917. flowParseObjectTypeCallProperty(node, isStatic) {
  3918. const valueNode = this.startNode();
  3919. node.static = isStatic;
  3920. node.value = this.flowParseObjectTypeMethodish(valueNode);
  3921. return this.finishNode(node, "ObjectTypeCallProperty");
  3922. }
  3923. flowParseObjectType({
  3924. allowStatic,
  3925. allowExact,
  3926. allowSpread,
  3927. allowProto,
  3928. allowInexact
  3929. }) {
  3930. const oldInType = this.state.inType;
  3931. this.state.inType = true;
  3932. const nodeStart = this.startNode();
  3933. nodeStart.callProperties = [];
  3934. nodeStart.properties = [];
  3935. nodeStart.indexers = [];
  3936. nodeStart.internalSlots = [];
  3937. let endDelim;
  3938. let exact;
  3939. let inexact = false;
  3940. if (allowExact && this.match(6)) {
  3941. this.expect(6);
  3942. endDelim = 9;
  3943. exact = true;
  3944. } else {
  3945. this.expect(5);
  3946. endDelim = 8;
  3947. exact = false;
  3948. }
  3949. nodeStart.exact = exact;
  3950. while (!this.match(endDelim)) {
  3951. let isStatic = false;
  3952. let protoStart = null;
  3953. let inexactStart = null;
  3954. const node = this.startNode();
  3955. if (allowProto && this.isContextual(111)) {
  3956. const lookahead = this.lookahead();
  3957. if (lookahead.type !== 14 && lookahead.type !== 17) {
  3958. this.next();
  3959. protoStart = this.state.start;
  3960. allowStatic = false;
  3961. }
  3962. }
  3963. if (allowStatic && this.isContextual(100)) {
  3964. const lookahead = this.lookahead();
  3965. if (lookahead.type !== 14 && lookahead.type !== 17) {
  3966. this.next();
  3967. isStatic = true;
  3968. }
  3969. }
  3970. const variance = this.flowParseVariance();
  3971. if (this.eat(0)) {
  3972. if (protoStart != null) {
  3973. this.unexpected(protoStart);
  3974. }
  3975. if (this.eat(0)) {
  3976. if (variance) {
  3977. this.unexpected(variance.start);
  3978. }
  3979. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  3980. } else {
  3981. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  3982. }
  3983. } else if (this.match(10) || this.match(45)) {
  3984. if (protoStart != null) {
  3985. this.unexpected(protoStart);
  3986. }
  3987. if (variance) {
  3988. this.unexpected(variance.start);
  3989. }
  3990. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  3991. } else {
  3992. let kind = "init";
  3993. if (this.isContextual(94) || this.isContextual(99)) {
  3994. const lookahead = this.lookahead();
  3995. if (tokenIsLiteralPropertyName(lookahead.type)) {
  3996. kind = this.state.value;
  3997. this.next();
  3998. }
  3999. }
  4000. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  4001. if (propOrInexact === null) {
  4002. inexact = true;
  4003. inexactStart = this.state.lastTokStart;
  4004. } else {
  4005. nodeStart.properties.push(propOrInexact);
  4006. }
  4007. }
  4008. this.flowObjectTypeSemicolon();
  4009. if (inexactStart && !this.match(8) && !this.match(9)) {
  4010. this.raise(inexactStart, FlowErrors.UnexpectedExplicitInexactInObject);
  4011. }
  4012. }
  4013. this.expect(endDelim);
  4014. if (allowSpread) {
  4015. nodeStart.inexact = inexact;
  4016. }
  4017. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  4018. this.state.inType = oldInType;
  4019. return out;
  4020. }
  4021. flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact) {
  4022. if (this.eat(21)) {
  4023. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  4024. if (isInexactToken) {
  4025. if (!allowSpread) {
  4026. this.raise(this.state.lastTokStart, FlowErrors.InexactInsideNonObject);
  4027. } else if (!allowInexact) {
  4028. this.raise(this.state.lastTokStart, FlowErrors.InexactInsideExact);
  4029. }
  4030. if (variance) {
  4031. this.raise(variance.start, FlowErrors.InexactVariance);
  4032. }
  4033. return null;
  4034. }
  4035. if (!allowSpread) {
  4036. this.raise(this.state.lastTokStart, FlowErrors.UnexpectedSpreadType);
  4037. }
  4038. if (protoStart != null) {
  4039. this.unexpected(protoStart);
  4040. }
  4041. if (variance) {
  4042. this.raise(variance.start, FlowErrors.SpreadVariance);
  4043. }
  4044. node.argument = this.flowParseType();
  4045. return this.finishNode(node, "ObjectTypeSpreadProperty");
  4046. } else {
  4047. node.key = this.flowParseObjectPropertyKey();
  4048. node.static = isStatic;
  4049. node.proto = protoStart != null;
  4050. node.kind = kind;
  4051. let optional = false;
  4052. if (this.match(45) || this.match(10)) {
  4053. node.method = true;
  4054. if (protoStart != null) {
  4055. this.unexpected(protoStart);
  4056. }
  4057. if (variance) {
  4058. this.unexpected(variance.start);
  4059. }
  4060. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  4061. if (kind === "get" || kind === "set") {
  4062. this.flowCheckGetterSetterParams(node);
  4063. }
  4064. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  4065. this.raise(node.value.this.start, FlowErrors.ThisParamBannedInConstructor);
  4066. }
  4067. } else {
  4068. if (kind !== "init") this.unexpected();
  4069. node.method = false;
  4070. if (this.eat(17)) {
  4071. optional = true;
  4072. }
  4073. node.value = this.flowParseTypeInitialiser();
  4074. node.variance = variance;
  4075. }
  4076. node.optional = optional;
  4077. return this.finishNode(node, "ObjectTypeProperty");
  4078. }
  4079. }
  4080. flowCheckGetterSetterParams(property) {
  4081. const paramCount = property.kind === "get" ? 0 : 1;
  4082. const start = property.start;
  4083. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  4084. if (property.value.this) {
  4085. this.raise(property.value.this.start, property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam);
  4086. }
  4087. if (length !== paramCount) {
  4088. if (property.kind === "get") {
  4089. this.raise(start, ErrorMessages.BadGetterArity);
  4090. } else {
  4091. this.raise(start, ErrorMessages.BadSetterArity);
  4092. }
  4093. }
  4094. if (property.kind === "set" && property.value.rest) {
  4095. this.raise(start, ErrorMessages.BadSetterRestParameter);
  4096. }
  4097. }
  4098. flowObjectTypeSemicolon() {
  4099. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  4100. this.unexpected();
  4101. }
  4102. }
  4103. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  4104. startPos = startPos || this.state.start;
  4105. startLoc = startLoc || this.state.startLoc;
  4106. let node = id || this.flowParseRestrictedIdentifier(true);
  4107. while (this.eat(16)) {
  4108. const node2 = this.startNodeAt(startPos, startLoc);
  4109. node2.qualification = node;
  4110. node2.id = this.flowParseRestrictedIdentifier(true);
  4111. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  4112. }
  4113. return node;
  4114. }
  4115. flowParseGenericType(startPos, startLoc, id) {
  4116. const node = this.startNodeAt(startPos, startLoc);
  4117. node.typeParameters = null;
  4118. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  4119. if (this.match(45)) {
  4120. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4121. }
  4122. return this.finishNode(node, "GenericTypeAnnotation");
  4123. }
  4124. flowParseTypeofType() {
  4125. const node = this.startNode();
  4126. this.expect(83);
  4127. node.argument = this.flowParsePrimaryType();
  4128. return this.finishNode(node, "TypeofTypeAnnotation");
  4129. }
  4130. flowParseTupleType() {
  4131. const node = this.startNode();
  4132. node.types = [];
  4133. this.expect(0);
  4134. while (this.state.pos < this.length && !this.match(3)) {
  4135. node.types.push(this.flowParseType());
  4136. if (this.match(3)) break;
  4137. this.expect(12);
  4138. }
  4139. this.expect(3);
  4140. return this.finishNode(node, "TupleTypeAnnotation");
  4141. }
  4142. flowParseFunctionTypeParam(first) {
  4143. let name = null;
  4144. let optional = false;
  4145. let typeAnnotation = null;
  4146. const node = this.startNode();
  4147. const lh = this.lookahead();
  4148. const isThis = this.state.type === 74;
  4149. if (lh.type === 14 || lh.type === 17) {
  4150. if (isThis && !first) {
  4151. this.raise(node.start, FlowErrors.ThisParamMustBeFirst);
  4152. }
  4153. name = this.parseIdentifier(isThis);
  4154. if (this.eat(17)) {
  4155. optional = true;
  4156. if (isThis) {
  4157. this.raise(node.start, FlowErrors.ThisParamMayNotBeOptional);
  4158. }
  4159. }
  4160. typeAnnotation = this.flowParseTypeInitialiser();
  4161. } else {
  4162. typeAnnotation = this.flowParseType();
  4163. }
  4164. node.name = name;
  4165. node.optional = optional;
  4166. node.typeAnnotation = typeAnnotation;
  4167. return this.finishNode(node, "FunctionTypeParam");
  4168. }
  4169. reinterpretTypeAsFunctionTypeParam(type) {
  4170. const node = this.startNodeAt(type.start, type.loc.start);
  4171. node.name = null;
  4172. node.optional = false;
  4173. node.typeAnnotation = type;
  4174. return this.finishNode(node, "FunctionTypeParam");
  4175. }
  4176. flowParseFunctionTypeParams(params = []) {
  4177. let rest = null;
  4178. let _this = null;
  4179. if (this.match(74)) {
  4180. _this = this.flowParseFunctionTypeParam(true);
  4181. _this.name = null;
  4182. if (!this.match(11)) {
  4183. this.expect(12);
  4184. }
  4185. }
  4186. while (!this.match(11) && !this.match(21)) {
  4187. params.push(this.flowParseFunctionTypeParam(false));
  4188. if (!this.match(11)) {
  4189. this.expect(12);
  4190. }
  4191. }
  4192. if (this.eat(21)) {
  4193. rest = this.flowParseFunctionTypeParam(false);
  4194. }
  4195. return {
  4196. params,
  4197. rest,
  4198. _this
  4199. };
  4200. }
  4201. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  4202. switch (id.name) {
  4203. case "any":
  4204. return this.finishNode(node, "AnyTypeAnnotation");
  4205. case "bool":
  4206. case "boolean":
  4207. return this.finishNode(node, "BooleanTypeAnnotation");
  4208. case "mixed":
  4209. return this.finishNode(node, "MixedTypeAnnotation");
  4210. case "empty":
  4211. return this.finishNode(node, "EmptyTypeAnnotation");
  4212. case "number":
  4213. return this.finishNode(node, "NumberTypeAnnotation");
  4214. case "string":
  4215. return this.finishNode(node, "StringTypeAnnotation");
  4216. case "symbol":
  4217. return this.finishNode(node, "SymbolTypeAnnotation");
  4218. default:
  4219. this.checkNotUnderscore(id.name);
  4220. return this.flowParseGenericType(startPos, startLoc, id);
  4221. }
  4222. }
  4223. flowParsePrimaryType() {
  4224. const startPos = this.state.start;
  4225. const startLoc = this.state.startLoc;
  4226. const node = this.startNode();
  4227. let tmp;
  4228. let type;
  4229. let isGroupedType = false;
  4230. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4231. switch (this.state.type) {
  4232. case 5:
  4233. return this.flowParseObjectType({
  4234. allowStatic: false,
  4235. allowExact: false,
  4236. allowSpread: true,
  4237. allowProto: false,
  4238. allowInexact: true
  4239. });
  4240. case 6:
  4241. return this.flowParseObjectType({
  4242. allowStatic: false,
  4243. allowExact: true,
  4244. allowSpread: true,
  4245. allowProto: false,
  4246. allowInexact: false
  4247. });
  4248. case 0:
  4249. this.state.noAnonFunctionType = false;
  4250. type = this.flowParseTupleType();
  4251. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4252. return type;
  4253. case 45:
  4254. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4255. this.expect(10);
  4256. tmp = this.flowParseFunctionTypeParams();
  4257. node.params = tmp.params;
  4258. node.rest = tmp.rest;
  4259. node.this = tmp._this;
  4260. this.expect(11);
  4261. this.expect(19);
  4262. node.returnType = this.flowParseType();
  4263. return this.finishNode(node, "FunctionTypeAnnotation");
  4264. case 10:
  4265. this.next();
  4266. if (!this.match(11) && !this.match(21)) {
  4267. if (tokenIsIdentifier(this.state.type) || this.match(74)) {
  4268. const token = this.lookahead().type;
  4269. isGroupedType = token !== 17 && token !== 14;
  4270. } else {
  4271. isGroupedType = true;
  4272. }
  4273. }
  4274. if (isGroupedType) {
  4275. this.state.noAnonFunctionType = false;
  4276. type = this.flowParseType();
  4277. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4278. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  4279. this.expect(11);
  4280. return type;
  4281. } else {
  4282. this.eat(12);
  4283. }
  4284. }
  4285. if (type) {
  4286. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  4287. } else {
  4288. tmp = this.flowParseFunctionTypeParams();
  4289. }
  4290. node.params = tmp.params;
  4291. node.rest = tmp.rest;
  4292. node.this = tmp._this;
  4293. this.expect(11);
  4294. this.expect(19);
  4295. node.returnType = this.flowParseType();
  4296. node.typeParameters = null;
  4297. return this.finishNode(node, "FunctionTypeAnnotation");
  4298. case 125:
  4299. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  4300. case 81:
  4301. case 82:
  4302. node.value = this.match(81);
  4303. this.next();
  4304. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  4305. case 49:
  4306. if (this.state.value === "-") {
  4307. this.next();
  4308. if (this.match(126)) {
  4309. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  4310. }
  4311. if (this.match(127)) {
  4312. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  4313. }
  4314. throw this.raise(this.state.start, FlowErrors.UnexpectedSubtractionOperand);
  4315. }
  4316. throw this.unexpected();
  4317. case 126:
  4318. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  4319. case 127:
  4320. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  4321. case 84:
  4322. this.next();
  4323. return this.finishNode(node, "VoidTypeAnnotation");
  4324. case 80:
  4325. this.next();
  4326. return this.finishNode(node, "NullLiteralTypeAnnotation");
  4327. case 74:
  4328. this.next();
  4329. return this.finishNode(node, "ThisTypeAnnotation");
  4330. case 51:
  4331. this.next();
  4332. return this.finishNode(node, "ExistsTypeAnnotation");
  4333. case 83:
  4334. return this.flowParseTypeofType();
  4335. default:
  4336. if (tokenIsKeyword(this.state.type)) {
  4337. const label = tokenLabelName(this.state.type);
  4338. this.next();
  4339. return super.createIdentifier(node, label);
  4340. } else if (tokenIsIdentifier(this.state.type)) {
  4341. if (this.isContextual(121)) {
  4342. return this.flowParseInterfaceType();
  4343. }
  4344. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  4345. }
  4346. }
  4347. throw this.unexpected();
  4348. }
  4349. flowParsePostfixType() {
  4350. const startPos = this.state.start;
  4351. const startLoc = this.state.startLoc;
  4352. let type = this.flowParsePrimaryType();
  4353. let seenOptionalIndexedAccess = false;
  4354. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  4355. const node = this.startNodeAt(startPos, startLoc);
  4356. const optional = this.eat(18);
  4357. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  4358. this.expect(0);
  4359. if (!optional && this.match(3)) {
  4360. node.elementType = type;
  4361. this.next();
  4362. type = this.finishNode(node, "ArrayTypeAnnotation");
  4363. } else {
  4364. node.objectType = type;
  4365. node.indexType = this.flowParseType();
  4366. this.expect(3);
  4367. if (seenOptionalIndexedAccess) {
  4368. node.optional = optional;
  4369. type = this.finishNode(node, "OptionalIndexedAccessType");
  4370. } else {
  4371. type = this.finishNode(node, "IndexedAccessType");
  4372. }
  4373. }
  4374. }
  4375. return type;
  4376. }
  4377. flowParsePrefixType() {
  4378. const node = this.startNode();
  4379. if (this.eat(17)) {
  4380. node.typeAnnotation = this.flowParsePrefixType();
  4381. return this.finishNode(node, "NullableTypeAnnotation");
  4382. } else {
  4383. return this.flowParsePostfixType();
  4384. }
  4385. }
  4386. flowParseAnonFunctionWithoutParens() {
  4387. const param = this.flowParsePrefixType();
  4388. if (!this.state.noAnonFunctionType && this.eat(19)) {
  4389. const node = this.startNodeAt(param.start, param.loc.start);
  4390. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  4391. node.rest = null;
  4392. node.this = null;
  4393. node.returnType = this.flowParseType();
  4394. node.typeParameters = null;
  4395. return this.finishNode(node, "FunctionTypeAnnotation");
  4396. }
  4397. return param;
  4398. }
  4399. flowParseIntersectionType() {
  4400. const node = this.startNode();
  4401. this.eat(43);
  4402. const type = this.flowParseAnonFunctionWithoutParens();
  4403. node.types = [type];
  4404. while (this.eat(43)) {
  4405. node.types.push(this.flowParseAnonFunctionWithoutParens());
  4406. }
  4407. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  4408. }
  4409. flowParseUnionType() {
  4410. const node = this.startNode();
  4411. this.eat(41);
  4412. const type = this.flowParseIntersectionType();
  4413. node.types = [type];
  4414. while (this.eat(41)) {
  4415. node.types.push(this.flowParseIntersectionType());
  4416. }
  4417. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  4418. }
  4419. flowParseType() {
  4420. const oldInType = this.state.inType;
  4421. this.state.inType = true;
  4422. const type = this.flowParseUnionType();
  4423. this.state.inType = oldInType;
  4424. return type;
  4425. }
  4426. flowParseTypeOrImplicitInstantiation() {
  4427. if (this.state.type === 124 && this.state.value === "_") {
  4428. const startPos = this.state.start;
  4429. const startLoc = this.state.startLoc;
  4430. const node = this.parseIdentifier();
  4431. return this.flowParseGenericType(startPos, startLoc, node);
  4432. } else {
  4433. return this.flowParseType();
  4434. }
  4435. }
  4436. flowParseTypeAnnotation() {
  4437. const node = this.startNode();
  4438. node.typeAnnotation = this.flowParseTypeInitialiser();
  4439. return this.finishNode(node, "TypeAnnotation");
  4440. }
  4441. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  4442. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  4443. if (this.match(14)) {
  4444. ident.typeAnnotation = this.flowParseTypeAnnotation();
  4445. this.resetEndLocation(ident);
  4446. }
  4447. return ident;
  4448. }
  4449. typeCastToParameter(node) {
  4450. node.expression.typeAnnotation = node.typeAnnotation;
  4451. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  4452. return node.expression;
  4453. }
  4454. flowParseVariance() {
  4455. let variance = null;
  4456. if (this.match(49)) {
  4457. variance = this.startNode();
  4458. if (this.state.value === "+") {
  4459. variance.kind = "plus";
  4460. } else {
  4461. variance.kind = "minus";
  4462. }
  4463. this.next();
  4464. this.finishNode(variance, "Variance");
  4465. }
  4466. return variance;
  4467. }
  4468. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  4469. if (allowExpressionBody) {
  4470. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  4471. }
  4472. return super.parseFunctionBody(node, false, isMethod);
  4473. }
  4474. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  4475. if (this.match(14)) {
  4476. const typeNode = this.startNode();
  4477. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4478. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  4479. }
  4480. super.parseFunctionBodyAndFinish(node, type, isMethod);
  4481. }
  4482. parseStatement(context, topLevel) {
  4483. if (this.state.strict && this.isContextual(121)) {
  4484. const lookahead = this.lookahead();
  4485. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  4486. const node = this.startNode();
  4487. this.next();
  4488. return this.flowParseInterface(node);
  4489. }
  4490. } else if (this.shouldParseEnums() && this.isContextual(118)) {
  4491. const node = this.startNode();
  4492. this.next();
  4493. return this.flowParseEnumDeclaration(node);
  4494. }
  4495. const stmt = super.parseStatement(context, topLevel);
  4496. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  4497. this.flowPragma = null;
  4498. }
  4499. return stmt;
  4500. }
  4501. parseExpressionStatement(node, expr) {
  4502. if (expr.type === "Identifier") {
  4503. if (expr.name === "declare") {
  4504. if (this.match(76) || tokenIsIdentifier(this.state.type) || this.match(64) || this.match(70) || this.match(78)) {
  4505. return this.flowParseDeclare(node);
  4506. }
  4507. } else if (tokenIsIdentifier(this.state.type)) {
  4508. if (expr.name === "interface") {
  4509. return this.flowParseInterface(node);
  4510. } else if (expr.name === "type") {
  4511. return this.flowParseTypeAlias(node);
  4512. } else if (expr.name === "opaque") {
  4513. return this.flowParseOpaqueType(node, false);
  4514. }
  4515. }
  4516. }
  4517. return super.parseExpressionStatement(node, expr);
  4518. }
  4519. shouldParseExportDeclaration() {
  4520. const {
  4521. type
  4522. } = this.state;
  4523. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 118) {
  4524. return !this.state.containsEsc;
  4525. }
  4526. return super.shouldParseExportDeclaration();
  4527. }
  4528. isExportDefaultSpecifier() {
  4529. const {
  4530. type
  4531. } = this.state;
  4532. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 118) {
  4533. return this.state.containsEsc;
  4534. }
  4535. return super.isExportDefaultSpecifier();
  4536. }
  4537. parseExportDefaultExpression() {
  4538. if (this.shouldParseEnums() && this.isContextual(118)) {
  4539. const node = this.startNode();
  4540. this.next();
  4541. return this.flowParseEnumDeclaration(node);
  4542. }
  4543. return super.parseExportDefaultExpression();
  4544. }
  4545. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  4546. if (!this.match(17)) return expr;
  4547. if (this.state.maybeInArrowParameters) {
  4548. const nextCh = this.lookaheadCharCode();
  4549. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  4550. this.setOptionalParametersError(refExpressionErrors);
  4551. return expr;
  4552. }
  4553. }
  4554. this.expect(17);
  4555. const state = this.state.clone();
  4556. const originalNoArrowAt = this.state.noArrowAt;
  4557. const node = this.startNodeAt(startPos, startLoc);
  4558. let {
  4559. consequent,
  4560. failed
  4561. } = this.tryParseConditionalConsequent();
  4562. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  4563. if (failed || invalid.length > 0) {
  4564. const noArrowAt = [...originalNoArrowAt];
  4565. if (invalid.length > 0) {
  4566. this.state = state;
  4567. this.state.noArrowAt = noArrowAt;
  4568. for (let i = 0; i < invalid.length; i++) {
  4569. noArrowAt.push(invalid[i].start);
  4570. }
  4571. ({
  4572. consequent,
  4573. failed
  4574. } = this.tryParseConditionalConsequent());
  4575. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  4576. }
  4577. if (failed && valid.length > 1) {
  4578. this.raise(state.start, FlowErrors.AmbiguousConditionalArrow);
  4579. }
  4580. if (failed && valid.length === 1) {
  4581. this.state = state;
  4582. noArrowAt.push(valid[0].start);
  4583. this.state.noArrowAt = noArrowAt;
  4584. ({
  4585. consequent,
  4586. failed
  4587. } = this.tryParseConditionalConsequent());
  4588. }
  4589. }
  4590. this.getArrowLikeExpressions(consequent, true);
  4591. this.state.noArrowAt = originalNoArrowAt;
  4592. this.expect(14);
  4593. node.test = expr;
  4594. node.consequent = consequent;
  4595. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  4596. return this.finishNode(node, "ConditionalExpression");
  4597. }
  4598. tryParseConditionalConsequent() {
  4599. this.state.noArrowParamsConversionAt.push(this.state.start);
  4600. const consequent = this.parseMaybeAssignAllowIn();
  4601. const failed = !this.match(14);
  4602. this.state.noArrowParamsConversionAt.pop();
  4603. return {
  4604. consequent,
  4605. failed
  4606. };
  4607. }
  4608. getArrowLikeExpressions(node, disallowInvalid) {
  4609. const stack = [node];
  4610. const arrows = [];
  4611. while (stack.length !== 0) {
  4612. const node = stack.pop();
  4613. if (node.type === "ArrowFunctionExpression") {
  4614. if (node.typeParameters || !node.returnType) {
  4615. this.finishArrowValidation(node);
  4616. } else {
  4617. arrows.push(node);
  4618. }
  4619. stack.push(node.body);
  4620. } else if (node.type === "ConditionalExpression") {
  4621. stack.push(node.consequent);
  4622. stack.push(node.alternate);
  4623. }
  4624. }
  4625. if (disallowInvalid) {
  4626. arrows.forEach(node => this.finishArrowValidation(node));
  4627. return [arrows, []];
  4628. }
  4629. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  4630. }
  4631. finishArrowValidation(node) {
  4632. var _node$extra;
  4633. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingComma, false);
  4634. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  4635. super.checkParams(node, false, true);
  4636. this.scope.exit();
  4637. }
  4638. forwardNoArrowParamsConversionAt(node, parse) {
  4639. let result;
  4640. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  4641. this.state.noArrowParamsConversionAt.push(this.state.start);
  4642. result = parse();
  4643. this.state.noArrowParamsConversionAt.pop();
  4644. } else {
  4645. result = parse();
  4646. }
  4647. return result;
  4648. }
  4649. parseParenItem(node, startPos, startLoc) {
  4650. node = super.parseParenItem(node, startPos, startLoc);
  4651. if (this.eat(17)) {
  4652. node.optional = true;
  4653. this.resetEndLocation(node);
  4654. }
  4655. if (this.match(14)) {
  4656. const typeCastNode = this.startNodeAt(startPos, startLoc);
  4657. typeCastNode.expression = node;
  4658. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  4659. return this.finishNode(typeCastNode, "TypeCastExpression");
  4660. }
  4661. return node;
  4662. }
  4663. assertModuleNodeAllowed(node) {
  4664. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  4665. return;
  4666. }
  4667. super.assertModuleNodeAllowed(node);
  4668. }
  4669. parseExport(node) {
  4670. const decl = super.parseExport(node);
  4671. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  4672. decl.exportKind = decl.exportKind || "value";
  4673. }
  4674. return decl;
  4675. }
  4676. parseExportDeclaration(node) {
  4677. if (this.isContextual(122)) {
  4678. node.exportKind = "type";
  4679. const declarationNode = this.startNode();
  4680. this.next();
  4681. if (this.match(5)) {
  4682. node.specifiers = this.parseExportSpecifiers(true);
  4683. this.parseExportFrom(node);
  4684. return null;
  4685. } else {
  4686. return this.flowParseTypeAlias(declarationNode);
  4687. }
  4688. } else if (this.isContextual(123)) {
  4689. node.exportKind = "type";
  4690. const declarationNode = this.startNode();
  4691. this.next();
  4692. return this.flowParseOpaqueType(declarationNode, false);
  4693. } else if (this.isContextual(121)) {
  4694. node.exportKind = "type";
  4695. const declarationNode = this.startNode();
  4696. this.next();
  4697. return this.flowParseInterface(declarationNode);
  4698. } else if (this.shouldParseEnums() && this.isContextual(118)) {
  4699. node.exportKind = "value";
  4700. const declarationNode = this.startNode();
  4701. this.next();
  4702. return this.flowParseEnumDeclaration(declarationNode);
  4703. } else {
  4704. return super.parseExportDeclaration(node);
  4705. }
  4706. }
  4707. eatExportStar(node) {
  4708. if (super.eatExportStar(...arguments)) return true;
  4709. if (this.isContextual(122) && this.lookahead().type === 51) {
  4710. node.exportKind = "type";
  4711. this.next();
  4712. this.next();
  4713. return true;
  4714. }
  4715. return false;
  4716. }
  4717. maybeParseExportNamespaceSpecifier(node) {
  4718. const pos = this.state.start;
  4719. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  4720. if (hasNamespace && node.exportKind === "type") {
  4721. this.unexpected(pos);
  4722. }
  4723. return hasNamespace;
  4724. }
  4725. parseClassId(node, isStatement, optionalId) {
  4726. super.parseClassId(node, isStatement, optionalId);
  4727. if (this.match(45)) {
  4728. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4729. }
  4730. }
  4731. parseClassMember(classBody, member, state) {
  4732. const pos = this.state.start;
  4733. if (this.isContextual(117)) {
  4734. if (this.parseClassMemberFromModifier(classBody, member)) {
  4735. return;
  4736. }
  4737. member.declare = true;
  4738. }
  4739. super.parseClassMember(classBody, member, state);
  4740. if (member.declare) {
  4741. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  4742. this.raise(pos, FlowErrors.DeclareClassElement);
  4743. } else if (member.value) {
  4744. this.raise(member.value.start, FlowErrors.DeclareClassFieldInitializer);
  4745. }
  4746. }
  4747. }
  4748. isIterator(word) {
  4749. return word === "iterator" || word === "asyncIterator";
  4750. }
  4751. readIterator() {
  4752. const word = super.readWord1();
  4753. const fullWord = "@@" + word;
  4754. if (!this.isIterator(word) || !this.state.inType) {
  4755. this.raise(this.state.pos, ErrorMessages.InvalidIdentifier, fullWord);
  4756. }
  4757. this.finishToken(124, fullWord);
  4758. }
  4759. getTokenFromCode(code) {
  4760. const next = this.input.charCodeAt(this.state.pos + 1);
  4761. if (code === 123 && next === 124) {
  4762. return this.finishOp(6, 2);
  4763. } else if (this.state.inType && (code === 62 || code === 60)) {
  4764. return this.finishOp(code === 62 ? 46 : 45, 1);
  4765. } else if (this.state.inType && code === 63) {
  4766. if (next === 46) {
  4767. return this.finishOp(18, 2);
  4768. }
  4769. return this.finishOp(17, 1);
  4770. } else if (isIteratorStart(code, next)) {
  4771. this.state.pos += 2;
  4772. return this.readIterator();
  4773. } else {
  4774. return super.getTokenFromCode(code);
  4775. }
  4776. }
  4777. isAssignable(node, isBinding) {
  4778. if (node.type === "TypeCastExpression") {
  4779. return this.isAssignable(node.expression, isBinding);
  4780. } else {
  4781. return super.isAssignable(node, isBinding);
  4782. }
  4783. }
  4784. toAssignable(node, isLHS = false) {
  4785. if (node.type === "TypeCastExpression") {
  4786. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  4787. } else {
  4788. return super.toAssignable(node, isLHS);
  4789. }
  4790. }
  4791. toAssignableList(exprList, trailingCommaPos, isLHS) {
  4792. for (let i = 0; i < exprList.length; i++) {
  4793. const expr = exprList[i];
  4794. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  4795. exprList[i] = this.typeCastToParameter(expr);
  4796. }
  4797. }
  4798. return super.toAssignableList(exprList, trailingCommaPos, isLHS);
  4799. }
  4800. toReferencedList(exprList, isParenthesizedExpr) {
  4801. for (let i = 0; i < exprList.length; i++) {
  4802. var _expr$extra;
  4803. const expr = exprList[i];
  4804. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  4805. this.raise(expr.typeAnnotation.start, FlowErrors.TypeCastInPattern);
  4806. }
  4807. }
  4808. return exprList;
  4809. }
  4810. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  4811. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  4812. if (canBePattern && !this.state.maybeInArrowParameters) {
  4813. this.toReferencedList(node.elements);
  4814. }
  4815. return node;
  4816. }
  4817. checkLVal(expr, ...args) {
  4818. if (expr.type !== "TypeCastExpression") {
  4819. return super.checkLVal(expr, ...args);
  4820. }
  4821. }
  4822. parseClassProperty(node) {
  4823. if (this.match(14)) {
  4824. node.typeAnnotation = this.flowParseTypeAnnotation();
  4825. }
  4826. return super.parseClassProperty(node);
  4827. }
  4828. parseClassPrivateProperty(node) {
  4829. if (this.match(14)) {
  4830. node.typeAnnotation = this.flowParseTypeAnnotation();
  4831. }
  4832. return super.parseClassPrivateProperty(node);
  4833. }
  4834. isClassMethod() {
  4835. return this.match(45) || super.isClassMethod();
  4836. }
  4837. isClassProperty() {
  4838. return this.match(14) || super.isClassProperty();
  4839. }
  4840. isNonstaticConstructor(method) {
  4841. return !this.match(14) && super.isNonstaticConstructor(method);
  4842. }
  4843. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  4844. if (method.variance) {
  4845. this.unexpected(method.variance.start);
  4846. }
  4847. delete method.variance;
  4848. if (this.match(45)) {
  4849. method.typeParameters = this.flowParseTypeParameterDeclaration();
  4850. }
  4851. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  4852. if (method.params && isConstructor) {
  4853. const params = method.params;
  4854. if (params.length > 0 && this.isThisParam(params[0])) {
  4855. this.raise(method.start, FlowErrors.ThisParamBannedInConstructor);
  4856. }
  4857. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  4858. const params = method.value.params;
  4859. if (params.length > 0 && this.isThisParam(params[0])) {
  4860. this.raise(method.start, FlowErrors.ThisParamBannedInConstructor);
  4861. }
  4862. }
  4863. }
  4864. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  4865. if (method.variance) {
  4866. this.unexpected(method.variance.start);
  4867. }
  4868. delete method.variance;
  4869. if (this.match(45)) {
  4870. method.typeParameters = this.flowParseTypeParameterDeclaration();
  4871. }
  4872. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  4873. }
  4874. parseClassSuper(node) {
  4875. super.parseClassSuper(node);
  4876. if (node.superClass && this.match(45)) {
  4877. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  4878. }
  4879. if (this.isContextual(106)) {
  4880. this.next();
  4881. const implemented = node.implements = [];
  4882. do {
  4883. const node = this.startNode();
  4884. node.id = this.flowParseRestrictedIdentifier(true);
  4885. if (this.match(45)) {
  4886. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4887. } else {
  4888. node.typeParameters = null;
  4889. }
  4890. implemented.push(this.finishNode(node, "ClassImplements"));
  4891. } while (this.eat(12));
  4892. }
  4893. }
  4894. checkGetterSetterParams(method) {
  4895. super.checkGetterSetterParams(method);
  4896. const params = this.getObjectOrClassMethodParams(method);
  4897. if (params.length > 0) {
  4898. const param = params[0];
  4899. if (this.isThisParam(param) && method.kind === "get") {
  4900. this.raise(param.start, FlowErrors.GetterMayNotHaveThisParam);
  4901. } else if (this.isThisParam(param)) {
  4902. this.raise(param.start, FlowErrors.SetterMayNotHaveThisParam);
  4903. }
  4904. }
  4905. }
  4906. parsePropertyNamePrefixOperator(node) {
  4907. node.variance = this.flowParseVariance();
  4908. }
  4909. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  4910. if (prop.variance) {
  4911. this.unexpected(prop.variance.start);
  4912. }
  4913. delete prop.variance;
  4914. let typeParameters;
  4915. if (this.match(45) && !isAccessor) {
  4916. typeParameters = this.flowParseTypeParameterDeclaration();
  4917. if (!this.match(10)) this.unexpected();
  4918. }
  4919. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  4920. if (typeParameters) {
  4921. (prop.value || prop).typeParameters = typeParameters;
  4922. }
  4923. }
  4924. parseAssignableListItemTypes(param) {
  4925. if (this.eat(17)) {
  4926. if (param.type !== "Identifier") {
  4927. this.raise(param.start, FlowErrors.PatternIsOptional);
  4928. }
  4929. if (this.isThisParam(param)) {
  4930. this.raise(param.start, FlowErrors.ThisParamMayNotBeOptional);
  4931. }
  4932. param.optional = true;
  4933. }
  4934. if (this.match(14)) {
  4935. param.typeAnnotation = this.flowParseTypeAnnotation();
  4936. } else if (this.isThisParam(param)) {
  4937. this.raise(param.start, FlowErrors.ThisParamAnnotationRequired);
  4938. }
  4939. if (this.match(29) && this.isThisParam(param)) {
  4940. this.raise(param.start, FlowErrors.ThisParamNoDefault);
  4941. }
  4942. this.resetEndLocation(param);
  4943. return param;
  4944. }
  4945. parseMaybeDefault(startPos, startLoc, left) {
  4946. const node = super.parseMaybeDefault(startPos, startLoc, left);
  4947. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  4948. this.raise(node.typeAnnotation.start, FlowErrors.TypeBeforeInitializer);
  4949. }
  4950. return node;
  4951. }
  4952. shouldParseDefaultImport(node) {
  4953. if (!hasTypeImportKind(node)) {
  4954. return super.shouldParseDefaultImport(node);
  4955. }
  4956. return isMaybeDefaultImport(this.state.type);
  4957. }
  4958. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  4959. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  4960. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  4961. node.specifiers.push(this.finishNode(specifier, type));
  4962. }
  4963. maybeParseDefaultImportSpecifier(node) {
  4964. node.importKind = "value";
  4965. let kind = null;
  4966. if (this.match(83)) {
  4967. kind = "typeof";
  4968. } else if (this.isContextual(122)) {
  4969. kind = "type";
  4970. }
  4971. if (kind) {
  4972. const lh = this.lookahead();
  4973. const {
  4974. type
  4975. } = lh;
  4976. if (kind === "type" && type === 51) {
  4977. this.unexpected(lh.start);
  4978. }
  4979. if (isMaybeDefaultImport(type) || type === 5 || type === 51) {
  4980. this.next();
  4981. node.importKind = kind;
  4982. }
  4983. }
  4984. return super.maybeParseDefaultImportSpecifier(node);
  4985. }
  4986. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  4987. const firstIdent = specifier.imported;
  4988. let specifierTypeKind = null;
  4989. if (firstIdent.type === "Identifier") {
  4990. if (firstIdent.name === "type") {
  4991. specifierTypeKind = "type";
  4992. } else if (firstIdent.name === "typeof") {
  4993. specifierTypeKind = "typeof";
  4994. }
  4995. }
  4996. let isBinding = false;
  4997. if (this.isContextual(89) && !this.isLookaheadContextual("as")) {
  4998. const as_ident = this.parseIdentifier(true);
  4999. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5000. specifier.imported = as_ident;
  5001. specifier.importKind = specifierTypeKind;
  5002. specifier.local = cloneIdentifier(as_ident);
  5003. } else {
  5004. specifier.imported = firstIdent;
  5005. specifier.importKind = null;
  5006. specifier.local = this.parseIdentifier();
  5007. }
  5008. } else {
  5009. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5010. specifier.imported = this.parseIdentifier(true);
  5011. specifier.importKind = specifierTypeKind;
  5012. } else {
  5013. if (importedIsString) {
  5014. throw this.raise(specifier.start, ErrorMessages.ImportBindingIsString, firstIdent.value);
  5015. }
  5016. specifier.imported = firstIdent;
  5017. specifier.importKind = null;
  5018. }
  5019. if (this.eatContextual(89)) {
  5020. specifier.local = this.parseIdentifier();
  5021. } else {
  5022. isBinding = true;
  5023. specifier.local = cloneIdentifier(specifier.imported);
  5024. }
  5025. }
  5026. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5027. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5028. this.raise(specifier.start, FlowErrors.ImportTypeShorthandOnlyInPureImport);
  5029. }
  5030. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5031. this.checkReservedType(specifier.local.name, specifier.local.start, true);
  5032. }
  5033. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5034. this.checkReservedWord(specifier.local.name, specifier.start, true, true);
  5035. }
  5036. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  5037. return this.finishNode(specifier, "ImportSpecifier");
  5038. }
  5039. parseBindingAtom() {
  5040. switch (this.state.type) {
  5041. case 74:
  5042. return this.parseIdentifier(true);
  5043. default:
  5044. return super.parseBindingAtom();
  5045. }
  5046. }
  5047. parseFunctionParams(node, allowModifiers) {
  5048. const kind = node.kind;
  5049. if (kind !== "get" && kind !== "set" && this.match(45)) {
  5050. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5051. }
  5052. super.parseFunctionParams(node, allowModifiers);
  5053. }
  5054. parseVarId(decl, kind) {
  5055. super.parseVarId(decl, kind);
  5056. if (this.match(14)) {
  5057. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5058. this.resetEndLocation(decl.id);
  5059. }
  5060. }
  5061. parseAsyncArrowFromCallExpression(node, call) {
  5062. if (this.match(14)) {
  5063. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5064. this.state.noAnonFunctionType = true;
  5065. node.returnType = this.flowParseTypeAnnotation();
  5066. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5067. }
  5068. return super.parseAsyncArrowFromCallExpression(node, call);
  5069. }
  5070. shouldParseAsyncArrow() {
  5071. return this.match(14) || super.shouldParseAsyncArrow();
  5072. }
  5073. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5074. var _jsx;
  5075. let state = null;
  5076. let jsx;
  5077. if (this.hasPlugin("jsx") && (this.match(134) || this.match(45))) {
  5078. state = this.state.clone();
  5079. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5080. if (!jsx.error) return jsx.node;
  5081. const {
  5082. context
  5083. } = this.state;
  5084. const currentContext = context[context.length - 1];
  5085. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5086. context.pop();
  5087. }
  5088. }
  5089. if ((_jsx = jsx) != null && _jsx.error || this.match(45)) {
  5090. var _jsx2, _jsx3;
  5091. state = state || this.state.clone();
  5092. let typeParameters;
  5093. const arrow = this.tryParse(abort => {
  5094. var _arrowExpression$extr;
  5095. typeParameters = this.flowParseTypeParameterDeclaration();
  5096. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5097. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5098. this.resetStartLocationFromNode(result, typeParameters);
  5099. return result;
  5100. });
  5101. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5102. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5103. if (expr.type !== "ArrowFunctionExpression") abort();
  5104. expr.typeParameters = typeParameters;
  5105. this.resetStartLocationFromNode(expr, typeParameters);
  5106. return arrowExpression;
  5107. }, state);
  5108. let arrowExpression = null;
  5109. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5110. if (!arrow.error && !arrow.aborted) {
  5111. if (arrow.node.async) {
  5112. this.raise(typeParameters.start, FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction);
  5113. }
  5114. return arrow.node;
  5115. }
  5116. arrowExpression = arrow.node;
  5117. }
  5118. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5119. this.state = jsx.failState;
  5120. return jsx.node;
  5121. }
  5122. if (arrowExpression) {
  5123. this.state = arrow.failState;
  5124. return arrowExpression;
  5125. }
  5126. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5127. if (arrow.thrown) throw arrow.error;
  5128. throw this.raise(typeParameters.start, FlowErrors.UnexpectedTokenAfterTypeParameter);
  5129. }
  5130. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5131. }
  5132. parseArrow(node) {
  5133. if (this.match(14)) {
  5134. const result = this.tryParse(() => {
  5135. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5136. this.state.noAnonFunctionType = true;
  5137. const typeNode = this.startNode();
  5138. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5139. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5140. if (this.canInsertSemicolon()) this.unexpected();
  5141. if (!this.match(19)) this.unexpected();
  5142. return typeNode;
  5143. });
  5144. if (result.thrown) return null;
  5145. if (result.error) this.state = result.failState;
  5146. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5147. }
  5148. return super.parseArrow(node);
  5149. }
  5150. shouldParseArrow(params) {
  5151. return this.match(14) || super.shouldParseArrow(params);
  5152. }
  5153. setArrowFunctionParameters(node, params) {
  5154. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5155. node.params = params;
  5156. } else {
  5157. super.setArrowFunctionParameters(node, params);
  5158. }
  5159. }
  5160. checkParams(node, allowDuplicates, isArrowFunction) {
  5161. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5162. return;
  5163. }
  5164. for (let i = 0; i < node.params.length; i++) {
  5165. if (this.isThisParam(node.params[i]) && i > 0) {
  5166. this.raise(node.params[i].start, FlowErrors.ThisParamMustBeFirst);
  5167. }
  5168. }
  5169. return super.checkParams(...arguments);
  5170. }
  5171. parseParenAndDistinguishExpression(canBeArrow) {
  5172. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  5173. }
  5174. parseSubscripts(base, startPos, startLoc, noCalls) {
  5175. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  5176. this.next();
  5177. const node = this.startNodeAt(startPos, startLoc);
  5178. node.callee = base;
  5179. node.arguments = this.parseCallExpressionArguments(11, false);
  5180. base = this.finishNode(node, "CallExpression");
  5181. } else if (base.type === "Identifier" && base.name === "async" && this.match(45)) {
  5182. const state = this.state.clone();
  5183. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  5184. if (!arrow.error && !arrow.aborted) return arrow.node;
  5185. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  5186. if (result.node && !result.error) return result.node;
  5187. if (arrow.node) {
  5188. this.state = arrow.failState;
  5189. return arrow.node;
  5190. }
  5191. if (result.node) {
  5192. this.state = result.failState;
  5193. return result.node;
  5194. }
  5195. throw arrow.error || result.error;
  5196. }
  5197. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  5198. }
  5199. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  5200. if (this.match(18) && this.isLookaheadToken_lt()) {
  5201. subscriptState.optionalChainMember = true;
  5202. if (noCalls) {
  5203. subscriptState.stop = true;
  5204. return base;
  5205. }
  5206. this.next();
  5207. const node = this.startNodeAt(startPos, startLoc);
  5208. node.callee = base;
  5209. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5210. this.expect(10);
  5211. node.arguments = this.parseCallExpressionArguments(11, false);
  5212. node.optional = true;
  5213. return this.finishCallExpression(node, true);
  5214. } else if (!noCalls && this.shouldParseTypes() && this.match(45)) {
  5215. const node = this.startNodeAt(startPos, startLoc);
  5216. node.callee = base;
  5217. const result = this.tryParse(() => {
  5218. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5219. this.expect(10);
  5220. node.arguments = this.parseCallExpressionArguments(11, false);
  5221. if (subscriptState.optionalChainMember) node.optional = false;
  5222. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5223. });
  5224. if (result.node) {
  5225. if (result.error) this.state = result.failState;
  5226. return result.node;
  5227. }
  5228. }
  5229. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  5230. }
  5231. parseNewArguments(node) {
  5232. let targs = null;
  5233. if (this.shouldParseTypes() && this.match(45)) {
  5234. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5235. }
  5236. node.typeArguments = targs;
  5237. super.parseNewArguments(node);
  5238. }
  5239. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  5240. const node = this.startNodeAt(startPos, startLoc);
  5241. this.parseFunctionParams(node);
  5242. if (!this.parseArrow(node)) return;
  5243. return this.parseArrowExpression(node, undefined, true);
  5244. }
  5245. readToken_mult_modulo(code) {
  5246. const next = this.input.charCodeAt(this.state.pos + 1);
  5247. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5248. this.state.hasFlowComment = false;
  5249. this.state.pos += 2;
  5250. this.nextToken();
  5251. return;
  5252. }
  5253. super.readToken_mult_modulo(code);
  5254. }
  5255. readToken_pipe_amp(code) {
  5256. const next = this.input.charCodeAt(this.state.pos + 1);
  5257. if (code === 124 && next === 125) {
  5258. this.finishOp(9, 2);
  5259. return;
  5260. }
  5261. super.readToken_pipe_amp(code);
  5262. }
  5263. parseTopLevel(file, program) {
  5264. const fileNode = super.parseTopLevel(file, program);
  5265. if (this.state.hasFlowComment) {
  5266. this.raise(this.state.pos, FlowErrors.UnterminatedFlowComment);
  5267. }
  5268. return fileNode;
  5269. }
  5270. skipBlockComment() {
  5271. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5272. if (this.state.hasFlowComment) {
  5273. this.unexpected(null, FlowErrors.NestedFlowComment);
  5274. }
  5275. this.hasFlowCommentCompletion();
  5276. this.state.pos += this.skipFlowComment();
  5277. this.state.hasFlowComment = true;
  5278. return;
  5279. }
  5280. if (this.state.hasFlowComment) {
  5281. const end = this.input.indexOf("*-/", this.state.pos += 2);
  5282. if (end === -1) {
  5283. throw this.raise(this.state.pos - 2, ErrorMessages.UnterminatedComment);
  5284. }
  5285. this.state.pos = end + 3;
  5286. return;
  5287. }
  5288. return super.skipBlockComment();
  5289. }
  5290. skipFlowComment() {
  5291. const {
  5292. pos
  5293. } = this.state;
  5294. let shiftToFirstNonWhiteSpace = 2;
  5295. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5296. shiftToFirstNonWhiteSpace++;
  5297. }
  5298. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5299. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5300. if (ch2 === 58 && ch3 === 58) {
  5301. return shiftToFirstNonWhiteSpace + 2;
  5302. }
  5303. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5304. return shiftToFirstNonWhiteSpace + 12;
  5305. }
  5306. if (ch2 === 58 && ch3 !== 58) {
  5307. return shiftToFirstNonWhiteSpace;
  5308. }
  5309. return false;
  5310. }
  5311. hasFlowCommentCompletion() {
  5312. const end = this.input.indexOf("*/", this.state.pos);
  5313. if (end === -1) {
  5314. throw this.raise(this.state.pos, ErrorMessages.UnterminatedComment);
  5315. }
  5316. }
  5317. flowEnumErrorBooleanMemberNotInitialized(pos, {
  5318. enumName,
  5319. memberName
  5320. }) {
  5321. this.raise(pos, FlowErrors.EnumBooleanMemberNotInitialized, memberName, enumName);
  5322. }
  5323. flowEnumErrorInvalidMemberName(pos, {
  5324. enumName,
  5325. memberName
  5326. }) {
  5327. const suggestion = memberName[0].toUpperCase() + memberName.slice(1);
  5328. this.raise(pos, FlowErrors.EnumInvalidMemberName, memberName, suggestion, enumName);
  5329. }
  5330. flowEnumErrorDuplicateMemberName(pos, {
  5331. enumName,
  5332. memberName
  5333. }) {
  5334. this.raise(pos, FlowErrors.EnumDuplicateMemberName, memberName, enumName);
  5335. }
  5336. flowEnumErrorInconsistentMemberValues(pos, {
  5337. enumName
  5338. }) {
  5339. this.raise(pos, FlowErrors.EnumInconsistentMemberValues, enumName);
  5340. }
  5341. flowEnumErrorInvalidExplicitType(pos, {
  5342. enumName,
  5343. suppliedType
  5344. }) {
  5345. return this.raise(pos, suppliedType === null ? FlowErrors.EnumInvalidExplicitTypeUnknownSupplied : FlowErrors.EnumInvalidExplicitType, enumName, suppliedType);
  5346. }
  5347. flowEnumErrorInvalidMemberInitializer(pos, {
  5348. enumName,
  5349. explicitType,
  5350. memberName
  5351. }) {
  5352. let message = null;
  5353. switch (explicitType) {
  5354. case "boolean":
  5355. case "number":
  5356. case "string":
  5357. message = FlowErrors.EnumInvalidMemberInitializerPrimaryType;
  5358. break;
  5359. case "symbol":
  5360. message = FlowErrors.EnumInvalidMemberInitializerSymbolType;
  5361. break;
  5362. default:
  5363. message = FlowErrors.EnumInvalidMemberInitializerUnknownType;
  5364. }
  5365. return this.raise(pos, message, enumName, memberName, explicitType);
  5366. }
  5367. flowEnumErrorNumberMemberNotInitialized(pos, {
  5368. enumName,
  5369. memberName
  5370. }) {
  5371. this.raise(pos, FlowErrors.EnumNumberMemberNotInitialized, enumName, memberName);
  5372. }
  5373. flowEnumErrorStringMemberInconsistentlyInitailized(pos, {
  5374. enumName
  5375. }) {
  5376. this.raise(pos, FlowErrors.EnumStringMemberInconsistentlyInitailized, enumName);
  5377. }
  5378. flowEnumMemberInit() {
  5379. const startPos = this.state.start;
  5380. const endOfInit = () => this.match(12) || this.match(8);
  5381. switch (this.state.type) {
  5382. case 126:
  5383. {
  5384. const literal = this.parseNumericLiteral(this.state.value);
  5385. if (endOfInit()) {
  5386. return {
  5387. type: "number",
  5388. pos: literal.start,
  5389. value: literal
  5390. };
  5391. }
  5392. return {
  5393. type: "invalid",
  5394. pos: startPos
  5395. };
  5396. }
  5397. case 125:
  5398. {
  5399. const literal = this.parseStringLiteral(this.state.value);
  5400. if (endOfInit()) {
  5401. return {
  5402. type: "string",
  5403. pos: literal.start,
  5404. value: literal
  5405. };
  5406. }
  5407. return {
  5408. type: "invalid",
  5409. pos: startPos
  5410. };
  5411. }
  5412. case 81:
  5413. case 82:
  5414. {
  5415. const literal = this.parseBooleanLiteral(this.match(81));
  5416. if (endOfInit()) {
  5417. return {
  5418. type: "boolean",
  5419. pos: literal.start,
  5420. value: literal
  5421. };
  5422. }
  5423. return {
  5424. type: "invalid",
  5425. pos: startPos
  5426. };
  5427. }
  5428. default:
  5429. return {
  5430. type: "invalid",
  5431. pos: startPos
  5432. };
  5433. }
  5434. }
  5435. flowEnumMemberRaw() {
  5436. const pos = this.state.start;
  5437. const id = this.parseIdentifier(true);
  5438. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  5439. type: "none",
  5440. pos
  5441. };
  5442. return {
  5443. id,
  5444. init
  5445. };
  5446. }
  5447. flowEnumCheckExplicitTypeMismatch(pos, context, expectedType) {
  5448. const {
  5449. explicitType
  5450. } = context;
  5451. if (explicitType === null) {
  5452. return;
  5453. }
  5454. if (explicitType !== expectedType) {
  5455. this.flowEnumErrorInvalidMemberInitializer(pos, context);
  5456. }
  5457. }
  5458. flowEnumMembers({
  5459. enumName,
  5460. explicitType
  5461. }) {
  5462. const seenNames = new Set();
  5463. const members = {
  5464. booleanMembers: [],
  5465. numberMembers: [],
  5466. stringMembers: [],
  5467. defaultedMembers: []
  5468. };
  5469. let hasUnknownMembers = false;
  5470. while (!this.match(8)) {
  5471. if (this.eat(21)) {
  5472. hasUnknownMembers = true;
  5473. break;
  5474. }
  5475. const memberNode = this.startNode();
  5476. const {
  5477. id,
  5478. init
  5479. } = this.flowEnumMemberRaw();
  5480. const memberName = id.name;
  5481. if (memberName === "") {
  5482. continue;
  5483. }
  5484. if (/^[a-z]/.test(memberName)) {
  5485. this.flowEnumErrorInvalidMemberName(id.start, {
  5486. enumName,
  5487. memberName
  5488. });
  5489. }
  5490. if (seenNames.has(memberName)) {
  5491. this.flowEnumErrorDuplicateMemberName(id.start, {
  5492. enumName,
  5493. memberName
  5494. });
  5495. }
  5496. seenNames.add(memberName);
  5497. const context = {
  5498. enumName,
  5499. explicitType,
  5500. memberName
  5501. };
  5502. memberNode.id = id;
  5503. switch (init.type) {
  5504. case "boolean":
  5505. {
  5506. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "boolean");
  5507. memberNode.init = init.value;
  5508. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  5509. break;
  5510. }
  5511. case "number":
  5512. {
  5513. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "number");
  5514. memberNode.init = init.value;
  5515. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  5516. break;
  5517. }
  5518. case "string":
  5519. {
  5520. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "string");
  5521. memberNode.init = init.value;
  5522. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  5523. break;
  5524. }
  5525. case "invalid":
  5526. {
  5527. throw this.flowEnumErrorInvalidMemberInitializer(init.pos, context);
  5528. }
  5529. case "none":
  5530. {
  5531. switch (explicitType) {
  5532. case "boolean":
  5533. this.flowEnumErrorBooleanMemberNotInitialized(init.pos, context);
  5534. break;
  5535. case "number":
  5536. this.flowEnumErrorNumberMemberNotInitialized(init.pos, context);
  5537. break;
  5538. default:
  5539. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  5540. }
  5541. }
  5542. }
  5543. if (!this.match(8)) {
  5544. this.expect(12);
  5545. }
  5546. }
  5547. return {
  5548. members,
  5549. hasUnknownMembers
  5550. };
  5551. }
  5552. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  5553. enumName
  5554. }) {
  5555. if (initializedMembers.length === 0) {
  5556. return defaultedMembers;
  5557. } else if (defaultedMembers.length === 0) {
  5558. return initializedMembers;
  5559. } else if (defaultedMembers.length > initializedMembers.length) {
  5560. for (const member of initializedMembers) {
  5561. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  5562. enumName
  5563. });
  5564. }
  5565. return defaultedMembers;
  5566. } else {
  5567. for (const member of defaultedMembers) {
  5568. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  5569. enumName
  5570. });
  5571. }
  5572. return initializedMembers;
  5573. }
  5574. }
  5575. flowEnumParseExplicitType({
  5576. enumName
  5577. }) {
  5578. if (this.eatContextual(97)) {
  5579. if (!tokenIsIdentifier(this.state.type)) {
  5580. throw this.flowEnumErrorInvalidExplicitType(this.state.start, {
  5581. enumName,
  5582. suppliedType: null
  5583. });
  5584. }
  5585. const {
  5586. value
  5587. } = this.state;
  5588. this.next();
  5589. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  5590. this.flowEnumErrorInvalidExplicitType(this.state.start, {
  5591. enumName,
  5592. suppliedType: value
  5593. });
  5594. }
  5595. return value;
  5596. }
  5597. return null;
  5598. }
  5599. flowEnumBody(node, {
  5600. enumName,
  5601. nameLoc
  5602. }) {
  5603. const explicitType = this.flowEnumParseExplicitType({
  5604. enumName
  5605. });
  5606. this.expect(5);
  5607. const {
  5608. members,
  5609. hasUnknownMembers
  5610. } = this.flowEnumMembers({
  5611. enumName,
  5612. explicitType
  5613. });
  5614. node.hasUnknownMembers = hasUnknownMembers;
  5615. switch (explicitType) {
  5616. case "boolean":
  5617. node.explicitType = true;
  5618. node.members = members.booleanMembers;
  5619. this.expect(8);
  5620. return this.finishNode(node, "EnumBooleanBody");
  5621. case "number":
  5622. node.explicitType = true;
  5623. node.members = members.numberMembers;
  5624. this.expect(8);
  5625. return this.finishNode(node, "EnumNumberBody");
  5626. case "string":
  5627. node.explicitType = true;
  5628. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  5629. enumName
  5630. });
  5631. this.expect(8);
  5632. return this.finishNode(node, "EnumStringBody");
  5633. case "symbol":
  5634. node.members = members.defaultedMembers;
  5635. this.expect(8);
  5636. return this.finishNode(node, "EnumSymbolBody");
  5637. default:
  5638. {
  5639. const empty = () => {
  5640. node.members = [];
  5641. this.expect(8);
  5642. return this.finishNode(node, "EnumStringBody");
  5643. };
  5644. node.explicitType = false;
  5645. const boolsLen = members.booleanMembers.length;
  5646. const numsLen = members.numberMembers.length;
  5647. const strsLen = members.stringMembers.length;
  5648. const defaultedLen = members.defaultedMembers.length;
  5649. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  5650. return empty();
  5651. } else if (!boolsLen && !numsLen) {
  5652. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  5653. enumName
  5654. });
  5655. this.expect(8);
  5656. return this.finishNode(node, "EnumStringBody");
  5657. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  5658. for (const member of members.defaultedMembers) {
  5659. this.flowEnumErrorBooleanMemberNotInitialized(member.start, {
  5660. enumName,
  5661. memberName: member.id.name
  5662. });
  5663. }
  5664. node.members = members.booleanMembers;
  5665. this.expect(8);
  5666. return this.finishNode(node, "EnumBooleanBody");
  5667. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  5668. for (const member of members.defaultedMembers) {
  5669. this.flowEnumErrorNumberMemberNotInitialized(member.start, {
  5670. enumName,
  5671. memberName: member.id.name
  5672. });
  5673. }
  5674. node.members = members.numberMembers;
  5675. this.expect(8);
  5676. return this.finishNode(node, "EnumNumberBody");
  5677. } else {
  5678. this.flowEnumErrorInconsistentMemberValues(nameLoc, {
  5679. enumName
  5680. });
  5681. return empty();
  5682. }
  5683. }
  5684. }
  5685. }
  5686. flowParseEnumDeclaration(node) {
  5687. const id = this.parseIdentifier();
  5688. node.id = id;
  5689. node.body = this.flowEnumBody(this.startNode(), {
  5690. enumName: id.name,
  5691. nameLoc: id.start
  5692. });
  5693. return this.finishNode(node, "EnumDeclaration");
  5694. }
  5695. isLookaheadToken_lt() {
  5696. const next = this.nextTokenStart();
  5697. if (this.input.charCodeAt(next) === 60) {
  5698. const afterNext = this.input.charCodeAt(next + 1);
  5699. return afterNext !== 60 && afterNext !== 61;
  5700. }
  5701. return false;
  5702. }
  5703. maybeUnwrapTypeCastExpression(node) {
  5704. return node.type === "TypeCastExpression" ? node.expression : node;
  5705. }
  5706. });
  5707. const entities = {
  5708. quot: "\u0022",
  5709. amp: "&",
  5710. apos: "\u0027",
  5711. lt: "<",
  5712. gt: ">",
  5713. nbsp: "\u00A0",
  5714. iexcl: "\u00A1",
  5715. cent: "\u00A2",
  5716. pound: "\u00A3",
  5717. curren: "\u00A4",
  5718. yen: "\u00A5",
  5719. brvbar: "\u00A6",
  5720. sect: "\u00A7",
  5721. uml: "\u00A8",
  5722. copy: "\u00A9",
  5723. ordf: "\u00AA",
  5724. laquo: "\u00AB",
  5725. not: "\u00AC",
  5726. shy: "\u00AD",
  5727. reg: "\u00AE",
  5728. macr: "\u00AF",
  5729. deg: "\u00B0",
  5730. plusmn: "\u00B1",
  5731. sup2: "\u00B2",
  5732. sup3: "\u00B3",
  5733. acute: "\u00B4",
  5734. micro: "\u00B5",
  5735. para: "\u00B6",
  5736. middot: "\u00B7",
  5737. cedil: "\u00B8",
  5738. sup1: "\u00B9",
  5739. ordm: "\u00BA",
  5740. raquo: "\u00BB",
  5741. frac14: "\u00BC",
  5742. frac12: "\u00BD",
  5743. frac34: "\u00BE",
  5744. iquest: "\u00BF",
  5745. Agrave: "\u00C0",
  5746. Aacute: "\u00C1",
  5747. Acirc: "\u00C2",
  5748. Atilde: "\u00C3",
  5749. Auml: "\u00C4",
  5750. Aring: "\u00C5",
  5751. AElig: "\u00C6",
  5752. Ccedil: "\u00C7",
  5753. Egrave: "\u00C8",
  5754. Eacute: "\u00C9",
  5755. Ecirc: "\u00CA",
  5756. Euml: "\u00CB",
  5757. Igrave: "\u00CC",
  5758. Iacute: "\u00CD",
  5759. Icirc: "\u00CE",
  5760. Iuml: "\u00CF",
  5761. ETH: "\u00D0",
  5762. Ntilde: "\u00D1",
  5763. Ograve: "\u00D2",
  5764. Oacute: "\u00D3",
  5765. Ocirc: "\u00D4",
  5766. Otilde: "\u00D5",
  5767. Ouml: "\u00D6",
  5768. times: "\u00D7",
  5769. Oslash: "\u00D8",
  5770. Ugrave: "\u00D9",
  5771. Uacute: "\u00DA",
  5772. Ucirc: "\u00DB",
  5773. Uuml: "\u00DC",
  5774. Yacute: "\u00DD",
  5775. THORN: "\u00DE",
  5776. szlig: "\u00DF",
  5777. agrave: "\u00E0",
  5778. aacute: "\u00E1",
  5779. acirc: "\u00E2",
  5780. atilde: "\u00E3",
  5781. auml: "\u00E4",
  5782. aring: "\u00E5",
  5783. aelig: "\u00E6",
  5784. ccedil: "\u00E7",
  5785. egrave: "\u00E8",
  5786. eacute: "\u00E9",
  5787. ecirc: "\u00EA",
  5788. euml: "\u00EB",
  5789. igrave: "\u00EC",
  5790. iacute: "\u00ED",
  5791. icirc: "\u00EE",
  5792. iuml: "\u00EF",
  5793. eth: "\u00F0",
  5794. ntilde: "\u00F1",
  5795. ograve: "\u00F2",
  5796. oacute: "\u00F3",
  5797. ocirc: "\u00F4",
  5798. otilde: "\u00F5",
  5799. ouml: "\u00F6",
  5800. divide: "\u00F7",
  5801. oslash: "\u00F8",
  5802. ugrave: "\u00F9",
  5803. uacute: "\u00FA",
  5804. ucirc: "\u00FB",
  5805. uuml: "\u00FC",
  5806. yacute: "\u00FD",
  5807. thorn: "\u00FE",
  5808. yuml: "\u00FF",
  5809. OElig: "\u0152",
  5810. oelig: "\u0153",
  5811. Scaron: "\u0160",
  5812. scaron: "\u0161",
  5813. Yuml: "\u0178",
  5814. fnof: "\u0192",
  5815. circ: "\u02C6",
  5816. tilde: "\u02DC",
  5817. Alpha: "\u0391",
  5818. Beta: "\u0392",
  5819. Gamma: "\u0393",
  5820. Delta: "\u0394",
  5821. Epsilon: "\u0395",
  5822. Zeta: "\u0396",
  5823. Eta: "\u0397",
  5824. Theta: "\u0398",
  5825. Iota: "\u0399",
  5826. Kappa: "\u039A",
  5827. Lambda: "\u039B",
  5828. Mu: "\u039C",
  5829. Nu: "\u039D",
  5830. Xi: "\u039E",
  5831. Omicron: "\u039F",
  5832. Pi: "\u03A0",
  5833. Rho: "\u03A1",
  5834. Sigma: "\u03A3",
  5835. Tau: "\u03A4",
  5836. Upsilon: "\u03A5",
  5837. Phi: "\u03A6",
  5838. Chi: "\u03A7",
  5839. Psi: "\u03A8",
  5840. Omega: "\u03A9",
  5841. alpha: "\u03B1",
  5842. beta: "\u03B2",
  5843. gamma: "\u03B3",
  5844. delta: "\u03B4",
  5845. epsilon: "\u03B5",
  5846. zeta: "\u03B6",
  5847. eta: "\u03B7",
  5848. theta: "\u03B8",
  5849. iota: "\u03B9",
  5850. kappa: "\u03BA",
  5851. lambda: "\u03BB",
  5852. mu: "\u03BC",
  5853. nu: "\u03BD",
  5854. xi: "\u03BE",
  5855. omicron: "\u03BF",
  5856. pi: "\u03C0",
  5857. rho: "\u03C1",
  5858. sigmaf: "\u03C2",
  5859. sigma: "\u03C3",
  5860. tau: "\u03C4",
  5861. upsilon: "\u03C5",
  5862. phi: "\u03C6",
  5863. chi: "\u03C7",
  5864. psi: "\u03C8",
  5865. omega: "\u03C9",
  5866. thetasym: "\u03D1",
  5867. upsih: "\u03D2",
  5868. piv: "\u03D6",
  5869. ensp: "\u2002",
  5870. emsp: "\u2003",
  5871. thinsp: "\u2009",
  5872. zwnj: "\u200C",
  5873. zwj: "\u200D",
  5874. lrm: "\u200E",
  5875. rlm: "\u200F",
  5876. ndash: "\u2013",
  5877. mdash: "\u2014",
  5878. lsquo: "\u2018",
  5879. rsquo: "\u2019",
  5880. sbquo: "\u201A",
  5881. ldquo: "\u201C",
  5882. rdquo: "\u201D",
  5883. bdquo: "\u201E",
  5884. dagger: "\u2020",
  5885. Dagger: "\u2021",
  5886. bull: "\u2022",
  5887. hellip: "\u2026",
  5888. permil: "\u2030",
  5889. prime: "\u2032",
  5890. Prime: "\u2033",
  5891. lsaquo: "\u2039",
  5892. rsaquo: "\u203A",
  5893. oline: "\u203E",
  5894. frasl: "\u2044",
  5895. euro: "\u20AC",
  5896. image: "\u2111",
  5897. weierp: "\u2118",
  5898. real: "\u211C",
  5899. trade: "\u2122",
  5900. alefsym: "\u2135",
  5901. larr: "\u2190",
  5902. uarr: "\u2191",
  5903. rarr: "\u2192",
  5904. darr: "\u2193",
  5905. harr: "\u2194",
  5906. crarr: "\u21B5",
  5907. lArr: "\u21D0",
  5908. uArr: "\u21D1",
  5909. rArr: "\u21D2",
  5910. dArr: "\u21D3",
  5911. hArr: "\u21D4",
  5912. forall: "\u2200",
  5913. part: "\u2202",
  5914. exist: "\u2203",
  5915. empty: "\u2205",
  5916. nabla: "\u2207",
  5917. isin: "\u2208",
  5918. notin: "\u2209",
  5919. ni: "\u220B",
  5920. prod: "\u220F",
  5921. sum: "\u2211",
  5922. minus: "\u2212",
  5923. lowast: "\u2217",
  5924. radic: "\u221A",
  5925. prop: "\u221D",
  5926. infin: "\u221E",
  5927. ang: "\u2220",
  5928. and: "\u2227",
  5929. or: "\u2228",
  5930. cap: "\u2229",
  5931. cup: "\u222A",
  5932. int: "\u222B",
  5933. there4: "\u2234",
  5934. sim: "\u223C",
  5935. cong: "\u2245",
  5936. asymp: "\u2248",
  5937. ne: "\u2260",
  5938. equiv: "\u2261",
  5939. le: "\u2264",
  5940. ge: "\u2265",
  5941. sub: "\u2282",
  5942. sup: "\u2283",
  5943. nsub: "\u2284",
  5944. sube: "\u2286",
  5945. supe: "\u2287",
  5946. oplus: "\u2295",
  5947. otimes: "\u2297",
  5948. perp: "\u22A5",
  5949. sdot: "\u22C5",
  5950. lceil: "\u2308",
  5951. rceil: "\u2309",
  5952. lfloor: "\u230A",
  5953. rfloor: "\u230B",
  5954. lang: "\u2329",
  5955. rang: "\u232A",
  5956. loz: "\u25CA",
  5957. spades: "\u2660",
  5958. clubs: "\u2663",
  5959. hearts: "\u2665",
  5960. diams: "\u2666"
  5961. };
  5962. const HEX_NUMBER = /^[\da-fA-F]+$/;
  5963. const DECIMAL_NUMBER = /^\d+$/;
  5964. const JsxErrors = makeErrorTemplates({
  5965. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  5966. MissingClosingTagElement: "Expected corresponding JSX closing tag for <%0>.",
  5967. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  5968. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  5969. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  5970. UnterminatedJsxContent: "Unterminated JSX contents.",
  5971. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  5972. }, ErrorCodes.SyntaxError, "jsx");
  5973. function isFragment(object) {
  5974. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  5975. }
  5976. function getQualifiedJSXName(object) {
  5977. if (object.type === "JSXIdentifier") {
  5978. return object.name;
  5979. }
  5980. if (object.type === "JSXNamespacedName") {
  5981. return object.namespace.name + ":" + object.name.name;
  5982. }
  5983. if (object.type === "JSXMemberExpression") {
  5984. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  5985. }
  5986. throw new Error("Node had unexpected type: " + object.type);
  5987. }
  5988. var jsx = (superClass => class extends superClass {
  5989. jsxReadToken() {
  5990. let out = "";
  5991. let chunkStart = this.state.pos;
  5992. for (;;) {
  5993. if (this.state.pos >= this.length) {
  5994. throw this.raise(this.state.start, JsxErrors.UnterminatedJsxContent);
  5995. }
  5996. const ch = this.input.charCodeAt(this.state.pos);
  5997. switch (ch) {
  5998. case 60:
  5999. case 123:
  6000. if (this.state.pos === this.state.start) {
  6001. if (ch === 60 && this.state.canStartJSXElement) {
  6002. ++this.state.pos;
  6003. return this.finishToken(134);
  6004. }
  6005. return super.getTokenFromCode(ch);
  6006. }
  6007. out += this.input.slice(chunkStart, this.state.pos);
  6008. return this.finishToken(133, out);
  6009. case 38:
  6010. out += this.input.slice(chunkStart, this.state.pos);
  6011. out += this.jsxReadEntity();
  6012. chunkStart = this.state.pos;
  6013. break;
  6014. case 62:
  6015. case 125:
  6016. default:
  6017. if (isNewLine(ch)) {
  6018. out += this.input.slice(chunkStart, this.state.pos);
  6019. out += this.jsxReadNewLine(true);
  6020. chunkStart = this.state.pos;
  6021. } else {
  6022. ++this.state.pos;
  6023. }
  6024. }
  6025. }
  6026. }
  6027. jsxReadNewLine(normalizeCRLF) {
  6028. const ch = this.input.charCodeAt(this.state.pos);
  6029. let out;
  6030. ++this.state.pos;
  6031. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6032. ++this.state.pos;
  6033. out = normalizeCRLF ? "\n" : "\r\n";
  6034. } else {
  6035. out = String.fromCharCode(ch);
  6036. }
  6037. ++this.state.curLine;
  6038. this.state.lineStart = this.state.pos;
  6039. return out;
  6040. }
  6041. jsxReadString(quote) {
  6042. let out = "";
  6043. let chunkStart = ++this.state.pos;
  6044. for (;;) {
  6045. if (this.state.pos >= this.length) {
  6046. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  6047. }
  6048. const ch = this.input.charCodeAt(this.state.pos);
  6049. if (ch === quote) break;
  6050. if (ch === 38) {
  6051. out += this.input.slice(chunkStart, this.state.pos);
  6052. out += this.jsxReadEntity();
  6053. chunkStart = this.state.pos;
  6054. } else if (isNewLine(ch)) {
  6055. out += this.input.slice(chunkStart, this.state.pos);
  6056. out += this.jsxReadNewLine(false);
  6057. chunkStart = this.state.pos;
  6058. } else {
  6059. ++this.state.pos;
  6060. }
  6061. }
  6062. out += this.input.slice(chunkStart, this.state.pos++);
  6063. return this.finishToken(125, out);
  6064. }
  6065. jsxReadEntity() {
  6066. let str = "";
  6067. let count = 0;
  6068. let entity;
  6069. let ch = this.input[this.state.pos];
  6070. const startPos = ++this.state.pos;
  6071. while (this.state.pos < this.length && count++ < 10) {
  6072. ch = this.input[this.state.pos++];
  6073. if (ch === ";") {
  6074. if (str[0] === "#") {
  6075. if (str[1] === "x") {
  6076. str = str.substr(2);
  6077. if (HEX_NUMBER.test(str)) {
  6078. entity = String.fromCodePoint(parseInt(str, 16));
  6079. }
  6080. } else {
  6081. str = str.substr(1);
  6082. if (DECIMAL_NUMBER.test(str)) {
  6083. entity = String.fromCodePoint(parseInt(str, 10));
  6084. }
  6085. }
  6086. } else {
  6087. entity = entities[str];
  6088. }
  6089. break;
  6090. }
  6091. str += ch;
  6092. }
  6093. if (!entity) {
  6094. this.state.pos = startPos;
  6095. return "&";
  6096. }
  6097. return entity;
  6098. }
  6099. jsxReadWord() {
  6100. let ch;
  6101. const start = this.state.pos;
  6102. do {
  6103. ch = this.input.charCodeAt(++this.state.pos);
  6104. } while (isIdentifierChar(ch) || ch === 45);
  6105. return this.finishToken(132, this.input.slice(start, this.state.pos));
  6106. }
  6107. jsxParseIdentifier() {
  6108. const node = this.startNode();
  6109. if (this.match(132)) {
  6110. node.name = this.state.value;
  6111. } else if (tokenIsKeyword(this.state.type)) {
  6112. node.name = tokenLabelName(this.state.type);
  6113. } else {
  6114. this.unexpected();
  6115. }
  6116. this.next();
  6117. return this.finishNode(node, "JSXIdentifier");
  6118. }
  6119. jsxParseNamespacedName() {
  6120. const startPos = this.state.start;
  6121. const startLoc = this.state.startLoc;
  6122. const name = this.jsxParseIdentifier();
  6123. if (!this.eat(14)) return name;
  6124. const node = this.startNodeAt(startPos, startLoc);
  6125. node.namespace = name;
  6126. node.name = this.jsxParseIdentifier();
  6127. return this.finishNode(node, "JSXNamespacedName");
  6128. }
  6129. jsxParseElementName() {
  6130. const startPos = this.state.start;
  6131. const startLoc = this.state.startLoc;
  6132. let node = this.jsxParseNamespacedName();
  6133. if (node.type === "JSXNamespacedName") {
  6134. return node;
  6135. }
  6136. while (this.eat(16)) {
  6137. const newNode = this.startNodeAt(startPos, startLoc);
  6138. newNode.object = node;
  6139. newNode.property = this.jsxParseIdentifier();
  6140. node = this.finishNode(newNode, "JSXMemberExpression");
  6141. }
  6142. return node;
  6143. }
  6144. jsxParseAttributeValue() {
  6145. let node;
  6146. switch (this.state.type) {
  6147. case 5:
  6148. node = this.startNode();
  6149. this.setContext(types.brace);
  6150. this.next();
  6151. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6152. if (node.expression.type === "JSXEmptyExpression") {
  6153. this.raise(node.start, JsxErrors.AttributeIsEmpty);
  6154. }
  6155. return node;
  6156. case 134:
  6157. case 125:
  6158. return this.parseExprAtom();
  6159. default:
  6160. throw this.raise(this.state.start, JsxErrors.UnsupportedJsxValue);
  6161. }
  6162. }
  6163. jsxParseEmptyExpression() {
  6164. const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
  6165. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.start, this.state.startLoc);
  6166. }
  6167. jsxParseSpreadChild(node) {
  6168. this.next();
  6169. node.expression = this.parseExpression();
  6170. this.setContext(types.j_oTag);
  6171. this.expect(8);
  6172. return this.finishNode(node, "JSXSpreadChild");
  6173. }
  6174. jsxParseExpressionContainer(node, previousContext) {
  6175. if (this.match(8)) {
  6176. node.expression = this.jsxParseEmptyExpression();
  6177. } else {
  6178. const expression = this.parseExpression();
  6179. node.expression = expression;
  6180. }
  6181. this.setContext(previousContext);
  6182. this.expect(8);
  6183. return this.finishNode(node, "JSXExpressionContainer");
  6184. }
  6185. jsxParseAttribute() {
  6186. const node = this.startNode();
  6187. if (this.match(5)) {
  6188. this.setContext(types.brace);
  6189. this.next();
  6190. this.expect(21);
  6191. node.argument = this.parseMaybeAssignAllowIn();
  6192. this.setContext(types.j_oTag);
  6193. this.expect(8);
  6194. return this.finishNode(node, "JSXSpreadAttribute");
  6195. }
  6196. node.name = this.jsxParseNamespacedName();
  6197. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6198. return this.finishNode(node, "JSXAttribute");
  6199. }
  6200. jsxParseOpeningElementAt(startPos, startLoc) {
  6201. const node = this.startNodeAt(startPos, startLoc);
  6202. if (this.match(135)) {
  6203. this.expect(135);
  6204. return this.finishNode(node, "JSXOpeningFragment");
  6205. }
  6206. node.name = this.jsxParseElementName();
  6207. return this.jsxParseOpeningElementAfterName(node);
  6208. }
  6209. jsxParseOpeningElementAfterName(node) {
  6210. const attributes = [];
  6211. while (!this.match(52) && !this.match(135)) {
  6212. attributes.push(this.jsxParseAttribute());
  6213. }
  6214. node.attributes = attributes;
  6215. node.selfClosing = this.eat(52);
  6216. this.expect(135);
  6217. return this.finishNode(node, "JSXOpeningElement");
  6218. }
  6219. jsxParseClosingElementAt(startPos, startLoc) {
  6220. const node = this.startNodeAt(startPos, startLoc);
  6221. if (this.match(135)) {
  6222. this.expect(135);
  6223. return this.finishNode(node, "JSXClosingFragment");
  6224. }
  6225. node.name = this.jsxParseElementName();
  6226. this.expect(135);
  6227. return this.finishNode(node, "JSXClosingElement");
  6228. }
  6229. jsxParseElementAt(startPos, startLoc) {
  6230. const node = this.startNodeAt(startPos, startLoc);
  6231. const children = [];
  6232. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  6233. let closingElement = null;
  6234. if (!openingElement.selfClosing) {
  6235. contents: for (;;) {
  6236. switch (this.state.type) {
  6237. case 134:
  6238. startPos = this.state.start;
  6239. startLoc = this.state.startLoc;
  6240. this.next();
  6241. if (this.eat(52)) {
  6242. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  6243. break contents;
  6244. }
  6245. children.push(this.jsxParseElementAt(startPos, startLoc));
  6246. break;
  6247. case 133:
  6248. children.push(this.parseExprAtom());
  6249. break;
  6250. case 5:
  6251. {
  6252. const node = this.startNode();
  6253. this.setContext(types.brace);
  6254. this.next();
  6255. if (this.match(21)) {
  6256. children.push(this.jsxParseSpreadChild(node));
  6257. } else {
  6258. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6259. }
  6260. break;
  6261. }
  6262. default:
  6263. throw this.unexpected();
  6264. }
  6265. }
  6266. if (isFragment(openingElement) && !isFragment(closingElement)) {
  6267. this.raise(closingElement.start, JsxErrors.MissingClosingTagFragment);
  6268. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6269. this.raise(closingElement.start, JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name));
  6270. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6271. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6272. this.raise(closingElement.start, JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name));
  6273. }
  6274. }
  6275. }
  6276. if (isFragment(openingElement)) {
  6277. node.openingFragment = openingElement;
  6278. node.closingFragment = closingElement;
  6279. } else {
  6280. node.openingElement = openingElement;
  6281. node.closingElement = closingElement;
  6282. }
  6283. node.children = children;
  6284. if (this.match(45)) {
  6285. throw this.raise(this.state.start, JsxErrors.UnwrappedAdjacentJSXElements);
  6286. }
  6287. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6288. }
  6289. jsxParseElement() {
  6290. const startPos = this.state.start;
  6291. const startLoc = this.state.startLoc;
  6292. this.next();
  6293. return this.jsxParseElementAt(startPos, startLoc);
  6294. }
  6295. setContext(newContext) {
  6296. const {
  6297. context
  6298. } = this.state;
  6299. context[context.length - 1] = newContext;
  6300. }
  6301. parseExprAtom(refExpressionErrors) {
  6302. if (this.match(133)) {
  6303. return this.parseLiteral(this.state.value, "JSXText");
  6304. } else if (this.match(134)) {
  6305. return this.jsxParseElement();
  6306. } else if (this.match(45) && this.input.charCodeAt(this.state.pos) !== 33) {
  6307. this.replaceToken(134);
  6308. return this.jsxParseElement();
  6309. } else {
  6310. return super.parseExprAtom(refExpressionErrors);
  6311. }
  6312. }
  6313. skipSpace() {
  6314. const curContext = this.curContext();
  6315. if (!curContext.preserveSpace) super.skipSpace();
  6316. }
  6317. getTokenFromCode(code) {
  6318. const context = this.curContext();
  6319. if (context === types.j_expr) {
  6320. return this.jsxReadToken();
  6321. }
  6322. if (context === types.j_oTag || context === types.j_cTag) {
  6323. if (isIdentifierStart(code)) {
  6324. return this.jsxReadWord();
  6325. }
  6326. if (code === 62) {
  6327. ++this.state.pos;
  6328. return this.finishToken(135);
  6329. }
  6330. if ((code === 34 || code === 39) && context === types.j_oTag) {
  6331. return this.jsxReadString(code);
  6332. }
  6333. }
  6334. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  6335. ++this.state.pos;
  6336. return this.finishToken(134);
  6337. }
  6338. return super.getTokenFromCode(code);
  6339. }
  6340. updateContext(prevType) {
  6341. const {
  6342. context,
  6343. type
  6344. } = this.state;
  6345. if (type === 52 && prevType === 134) {
  6346. context.splice(-2, 2, types.j_cTag);
  6347. this.state.canStartJSXElement = false;
  6348. } else if (type === 134) {
  6349. context.push(types.j_oTag);
  6350. } else if (type === 135) {
  6351. const out = context[context.length - 1];
  6352. if (out === types.j_oTag && prevType === 52 || out === types.j_cTag) {
  6353. context.pop();
  6354. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  6355. } else {
  6356. this.setContext(types.j_expr);
  6357. this.state.canStartJSXElement = true;
  6358. }
  6359. } else {
  6360. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  6361. }
  6362. }
  6363. });
  6364. class TypeScriptScope extends Scope {
  6365. constructor(...args) {
  6366. super(...args);
  6367. this.types = new Set();
  6368. this.enums = new Set();
  6369. this.constEnums = new Set();
  6370. this.classes = new Set();
  6371. this.exportOnlyBindings = new Set();
  6372. }
  6373. }
  6374. class TypeScriptScopeHandler extends ScopeHandler {
  6375. createScope(flags) {
  6376. return new TypeScriptScope(flags);
  6377. }
  6378. declareName(name, bindingType, pos) {
  6379. const scope = this.currentScope();
  6380. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  6381. this.maybeExportDefined(scope, name);
  6382. scope.exportOnlyBindings.add(name);
  6383. return;
  6384. }
  6385. super.declareName(...arguments);
  6386. if (bindingType & BIND_KIND_TYPE) {
  6387. if (!(bindingType & BIND_KIND_VALUE)) {
  6388. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  6389. this.maybeExportDefined(scope, name);
  6390. }
  6391. scope.types.add(name);
  6392. }
  6393. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  6394. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  6395. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  6396. }
  6397. isRedeclaredInScope(scope, name, bindingType) {
  6398. if (scope.enums.has(name)) {
  6399. if (bindingType & BIND_FLAGS_TS_ENUM) {
  6400. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  6401. const wasConst = scope.constEnums.has(name);
  6402. return isConst !== wasConst;
  6403. }
  6404. return true;
  6405. }
  6406. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  6407. if (scope.lexical.has(name)) {
  6408. return !!(bindingType & BIND_KIND_VALUE);
  6409. } else {
  6410. return false;
  6411. }
  6412. }
  6413. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  6414. return true;
  6415. }
  6416. return super.isRedeclaredInScope(...arguments);
  6417. }
  6418. checkLocalExport(id) {
  6419. const topLevelScope = this.scopeStack[0];
  6420. const {
  6421. name
  6422. } = id;
  6423. if (!topLevelScope.types.has(name) && !topLevelScope.exportOnlyBindings.has(name)) {
  6424. super.checkLocalExport(id);
  6425. }
  6426. }
  6427. }
  6428. function nonNull(x) {
  6429. if (x == null) {
  6430. throw new Error(`Unexpected ${x} value.`);
  6431. }
  6432. return x;
  6433. }
  6434. function assert(x) {
  6435. if (!x) {
  6436. throw new Error("Assert fail");
  6437. }
  6438. }
  6439. const TSErrors = makeErrorTemplates({
  6440. AbstractMethodHasImplementation: "Method '%0' cannot have an implementation because it is marked abstract.",
  6441. AbstractPropertyHasInitializer: "Property '%0' cannot have an initializer because it is marked abstract.",
  6442. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  6443. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  6444. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  6445. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  6446. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  6447. DeclareAccessor: "'declare' is not allowed in %0ters.",
  6448. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  6449. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  6450. DuplicateAccessibilityModifier: "Accessibility modifier already seen.",
  6451. DuplicateModifier: "Duplicate modifier: '%0'.",
  6452. EmptyHeritageClauseType: "'%0' list cannot be empty.",
  6453. EmptyTypeArguments: "Type argument list cannot be empty.",
  6454. EmptyTypeParameters: "Type parameter list cannot be empty.",
  6455. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  6456. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  6457. IncompatibleModifiers: "'%0' modifier cannot be used with '%1' modifier.",
  6458. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  6459. IndexSignatureHasAccessibility: "Index signatures cannot have an accessibility modifier ('%0').",
  6460. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  6461. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  6462. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  6463. InvalidModifierOnTypeMember: "'%0' modifier cannot appear on a type member.",
  6464. InvalidModifiersOrder: "'%0' modifier must precede '%1' modifier.",
  6465. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  6466. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  6467. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  6468. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  6469. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  6470. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  6471. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  6472. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  6473. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  6474. PrivateElementHasAccessibility: "Private elements cannot have an accessibility modifier ('%0').",
  6475. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  6476. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  6477. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  6478. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  6479. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  6480. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  6481. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  6482. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  6483. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  6484. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  6485. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  6486. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  6487. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  6488. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  6489. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  6490. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  6491. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  6492. UnsupportedSignatureParameterKind: "Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got %0."
  6493. }, ErrorCodes.SyntaxError, "typescript");
  6494. function keywordTypeFromName(value) {
  6495. switch (value) {
  6496. case "any":
  6497. return "TSAnyKeyword";
  6498. case "boolean":
  6499. return "TSBooleanKeyword";
  6500. case "bigint":
  6501. return "TSBigIntKeyword";
  6502. case "never":
  6503. return "TSNeverKeyword";
  6504. case "number":
  6505. return "TSNumberKeyword";
  6506. case "object":
  6507. return "TSObjectKeyword";
  6508. case "string":
  6509. return "TSStringKeyword";
  6510. case "symbol":
  6511. return "TSSymbolKeyword";
  6512. case "undefined":
  6513. return "TSUndefinedKeyword";
  6514. case "unknown":
  6515. return "TSUnknownKeyword";
  6516. default:
  6517. return undefined;
  6518. }
  6519. }
  6520. function tsIsAccessModifier(modifier) {
  6521. return modifier === "private" || modifier === "public" || modifier === "protected";
  6522. }
  6523. var typescript = (superClass => class extends superClass {
  6524. getScopeHandler() {
  6525. return TypeScriptScopeHandler;
  6526. }
  6527. tsIsIdentifier() {
  6528. return tokenIsIdentifier(this.state.type);
  6529. }
  6530. tsTokenCanFollowModifier() {
  6531. return (this.match(0) || this.match(5) || this.match(51) || this.match(21) || this.match(130) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  6532. }
  6533. tsNextTokenCanFollowModifier() {
  6534. this.next();
  6535. return this.tsTokenCanFollowModifier();
  6536. }
  6537. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  6538. if (!tokenIsIdentifier(this.state.type)) {
  6539. return undefined;
  6540. }
  6541. const modifier = this.state.value;
  6542. if (allowedModifiers.indexOf(modifier) !== -1) {
  6543. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  6544. return undefined;
  6545. }
  6546. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  6547. return modifier;
  6548. }
  6549. }
  6550. return undefined;
  6551. }
  6552. tsParseModifiers(modified, allowedModifiers, disallowedModifiers, errorTemplate, stopOnStartOfClassStaticBlock) {
  6553. const enforceOrder = (pos, modifier, before, after) => {
  6554. if (modifier === before && modified[after]) {
  6555. this.raise(pos, TSErrors.InvalidModifiersOrder, before, after);
  6556. }
  6557. };
  6558. const incompatible = (pos, modifier, mod1, mod2) => {
  6559. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  6560. this.raise(pos, TSErrors.IncompatibleModifiers, mod1, mod2);
  6561. }
  6562. };
  6563. for (;;) {
  6564. const startPos = this.state.start;
  6565. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  6566. if (!modifier) break;
  6567. if (tsIsAccessModifier(modifier)) {
  6568. if (modified.accessibility) {
  6569. this.raise(startPos, TSErrors.DuplicateAccessibilityModifier);
  6570. } else {
  6571. enforceOrder(startPos, modifier, modifier, "override");
  6572. enforceOrder(startPos, modifier, modifier, "static");
  6573. enforceOrder(startPos, modifier, modifier, "readonly");
  6574. modified.accessibility = modifier;
  6575. }
  6576. } else {
  6577. if (Object.hasOwnProperty.call(modified, modifier)) {
  6578. this.raise(startPos, TSErrors.DuplicateModifier, modifier);
  6579. } else {
  6580. enforceOrder(startPos, modifier, "static", "readonly");
  6581. enforceOrder(startPos, modifier, "static", "override");
  6582. enforceOrder(startPos, modifier, "override", "readonly");
  6583. enforceOrder(startPos, modifier, "abstract", "override");
  6584. incompatible(startPos, modifier, "declare", "override");
  6585. incompatible(startPos, modifier, "static", "abstract");
  6586. }
  6587. modified[modifier] = true;
  6588. }
  6589. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  6590. this.raise(startPos, errorTemplate, modifier);
  6591. }
  6592. }
  6593. }
  6594. tsIsListTerminator(kind) {
  6595. switch (kind) {
  6596. case "EnumMembers":
  6597. case "TypeMembers":
  6598. return this.match(8);
  6599. case "HeritageClauseElement":
  6600. return this.match(5);
  6601. case "TupleElementTypes":
  6602. return this.match(3);
  6603. case "TypeParametersOrArguments":
  6604. return this.match(46);
  6605. }
  6606. throw new Error("Unreachable");
  6607. }
  6608. tsParseList(kind, parseElement) {
  6609. const result = [];
  6610. while (!this.tsIsListTerminator(kind)) {
  6611. result.push(parseElement());
  6612. }
  6613. return result;
  6614. }
  6615. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  6616. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  6617. }
  6618. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  6619. const result = [];
  6620. let trailingCommaPos = -1;
  6621. for (;;) {
  6622. if (this.tsIsListTerminator(kind)) {
  6623. break;
  6624. }
  6625. trailingCommaPos = -1;
  6626. const element = parseElement();
  6627. if (element == null) {
  6628. return undefined;
  6629. }
  6630. result.push(element);
  6631. if (this.eat(12)) {
  6632. trailingCommaPos = this.state.lastTokStart;
  6633. continue;
  6634. }
  6635. if (this.tsIsListTerminator(kind)) {
  6636. break;
  6637. }
  6638. if (expectSuccess) {
  6639. this.expect(12);
  6640. }
  6641. return undefined;
  6642. }
  6643. if (refTrailingCommaPos) {
  6644. refTrailingCommaPos.value = trailingCommaPos;
  6645. }
  6646. return result;
  6647. }
  6648. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  6649. if (!skipFirstToken) {
  6650. if (bracket) {
  6651. this.expect(0);
  6652. } else {
  6653. this.expect(45);
  6654. }
  6655. }
  6656. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  6657. if (bracket) {
  6658. this.expect(3);
  6659. } else {
  6660. this.expect(46);
  6661. }
  6662. return result;
  6663. }
  6664. tsParseImportType() {
  6665. const node = this.startNode();
  6666. this.expect(79);
  6667. this.expect(10);
  6668. if (!this.match(125)) {
  6669. this.raise(this.state.start, TSErrors.UnsupportedImportTypeArgument);
  6670. }
  6671. node.argument = this.parseExprAtom();
  6672. this.expect(11);
  6673. if (this.eat(16)) {
  6674. node.qualifier = this.tsParseEntityName(true);
  6675. }
  6676. if (this.match(45)) {
  6677. node.typeParameters = this.tsParseTypeArguments();
  6678. }
  6679. return this.finishNode(node, "TSImportType");
  6680. }
  6681. tsParseEntityName(allowReservedWords) {
  6682. let entity = this.parseIdentifier();
  6683. while (this.eat(16)) {
  6684. const node = this.startNodeAtNode(entity);
  6685. node.left = entity;
  6686. node.right = this.parseIdentifier(allowReservedWords);
  6687. entity = this.finishNode(node, "TSQualifiedName");
  6688. }
  6689. return entity;
  6690. }
  6691. tsParseTypeReference() {
  6692. const node = this.startNode();
  6693. node.typeName = this.tsParseEntityName(false);
  6694. if (!this.hasPrecedingLineBreak() && this.match(45)) {
  6695. node.typeParameters = this.tsParseTypeArguments();
  6696. }
  6697. return this.finishNode(node, "TSTypeReference");
  6698. }
  6699. tsParseThisTypePredicate(lhs) {
  6700. this.next();
  6701. const node = this.startNodeAtNode(lhs);
  6702. node.parameterName = lhs;
  6703. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  6704. node.asserts = false;
  6705. return this.finishNode(node, "TSTypePredicate");
  6706. }
  6707. tsParseThisTypeNode() {
  6708. const node = this.startNode();
  6709. this.next();
  6710. return this.finishNode(node, "TSThisType");
  6711. }
  6712. tsParseTypeQuery() {
  6713. const node = this.startNode();
  6714. this.expect(83);
  6715. if (this.match(79)) {
  6716. node.exprName = this.tsParseImportType();
  6717. } else {
  6718. node.exprName = this.tsParseEntityName(true);
  6719. }
  6720. return this.finishNode(node, "TSTypeQuery");
  6721. }
  6722. tsParseTypeParameter() {
  6723. const node = this.startNode();
  6724. node.name = this.tsParseTypeParameterName();
  6725. node.constraint = this.tsEatThenParseType(77);
  6726. node.default = this.tsEatThenParseType(29);
  6727. return this.finishNode(node, "TSTypeParameter");
  6728. }
  6729. tsTryParseTypeParameters() {
  6730. if (this.match(45)) {
  6731. return this.tsParseTypeParameters();
  6732. }
  6733. }
  6734. tsParseTypeParameters() {
  6735. const node = this.startNode();
  6736. if (this.match(45) || this.match(134)) {
  6737. this.next();
  6738. } else {
  6739. this.unexpected();
  6740. }
  6741. const refTrailingCommaPos = {
  6742. value: -1
  6743. };
  6744. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true, refTrailingCommaPos);
  6745. if (node.params.length === 0) {
  6746. this.raise(node.start, TSErrors.EmptyTypeParameters);
  6747. }
  6748. if (refTrailingCommaPos.value !== -1) {
  6749. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  6750. }
  6751. return this.finishNode(node, "TSTypeParameterDeclaration");
  6752. }
  6753. tsTryNextParseConstantContext() {
  6754. if (this.lookahead().type === 71) {
  6755. this.next();
  6756. return this.tsParseTypeReference();
  6757. }
  6758. return null;
  6759. }
  6760. tsFillSignature(returnToken, signature) {
  6761. const returnTokenRequired = returnToken === 19;
  6762. signature.typeParameters = this.tsTryParseTypeParameters();
  6763. this.expect(10);
  6764. signature.parameters = this.tsParseBindingListForSignature();
  6765. if (returnTokenRequired) {
  6766. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  6767. } else if (this.match(returnToken)) {
  6768. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  6769. }
  6770. }
  6771. tsParseBindingListForSignature() {
  6772. return this.parseBindingList(11, 41).map(pattern => {
  6773. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  6774. this.raise(pattern.start, TSErrors.UnsupportedSignatureParameterKind, pattern.type);
  6775. }
  6776. return pattern;
  6777. });
  6778. }
  6779. tsParseTypeMemberSemicolon() {
  6780. if (!this.eat(12) && !this.isLineTerminator()) {
  6781. this.expect(13);
  6782. }
  6783. }
  6784. tsParseSignatureMember(kind, node) {
  6785. this.tsFillSignature(14, node);
  6786. this.tsParseTypeMemberSemicolon();
  6787. return this.finishNode(node, kind);
  6788. }
  6789. tsIsUnambiguouslyIndexSignature() {
  6790. this.next();
  6791. if (tokenIsIdentifier(this.state.type)) {
  6792. this.next();
  6793. return this.match(14);
  6794. }
  6795. return false;
  6796. }
  6797. tsTryParseIndexSignature(node) {
  6798. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  6799. return undefined;
  6800. }
  6801. this.expect(0);
  6802. const id = this.parseIdentifier();
  6803. id.typeAnnotation = this.tsParseTypeAnnotation();
  6804. this.resetEndLocation(id);
  6805. this.expect(3);
  6806. node.parameters = [id];
  6807. const type = this.tsTryParseTypeAnnotation();
  6808. if (type) node.typeAnnotation = type;
  6809. this.tsParseTypeMemberSemicolon();
  6810. return this.finishNode(node, "TSIndexSignature");
  6811. }
  6812. tsParsePropertyOrMethodSignature(node, readonly) {
  6813. if (this.eat(17)) node.optional = true;
  6814. const nodeAny = node;
  6815. if (this.match(10) || this.match(45)) {
  6816. if (readonly) {
  6817. this.raise(node.start, TSErrors.ReadonlyForMethodSignature);
  6818. }
  6819. const method = nodeAny;
  6820. if (method.kind && this.match(45)) {
  6821. this.raise(this.state.pos, TSErrors.AccesorCannotHaveTypeParameters);
  6822. }
  6823. this.tsFillSignature(14, method);
  6824. this.tsParseTypeMemberSemicolon();
  6825. if (method.kind === "get") {
  6826. if (method.parameters.length > 0) {
  6827. this.raise(this.state.pos, ErrorMessages.BadGetterArity);
  6828. if (this.isThisParam(method.parameters[0])) {
  6829. this.raise(this.state.pos, TSErrors.AccesorCannotDeclareThisParameter);
  6830. }
  6831. }
  6832. } else if (method.kind === "set") {
  6833. if (method.parameters.length !== 1) {
  6834. this.raise(this.state.pos, ErrorMessages.BadSetterArity);
  6835. } else {
  6836. const firstParameter = method.parameters[0];
  6837. if (this.isThisParam(firstParameter)) {
  6838. this.raise(this.state.pos, TSErrors.AccesorCannotDeclareThisParameter);
  6839. }
  6840. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  6841. this.raise(this.state.pos, TSErrors.SetAccesorCannotHaveOptionalParameter);
  6842. }
  6843. if (firstParameter.type === "RestElement") {
  6844. this.raise(this.state.pos, TSErrors.SetAccesorCannotHaveRestParameter);
  6845. }
  6846. }
  6847. if (method.typeAnnotation) {
  6848. this.raise(method.typeAnnotation.start, TSErrors.SetAccesorCannotHaveReturnType);
  6849. }
  6850. } else {
  6851. method.kind = "method";
  6852. }
  6853. return this.finishNode(method, "TSMethodSignature");
  6854. } else {
  6855. const property = nodeAny;
  6856. if (readonly) property.readonly = true;
  6857. const type = this.tsTryParseTypeAnnotation();
  6858. if (type) property.typeAnnotation = type;
  6859. this.tsParseTypeMemberSemicolon();
  6860. return this.finishNode(property, "TSPropertySignature");
  6861. }
  6862. }
  6863. tsParseTypeMember() {
  6864. const node = this.startNode();
  6865. if (this.match(10) || this.match(45)) {
  6866. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  6867. }
  6868. if (this.match(73)) {
  6869. const id = this.startNode();
  6870. this.next();
  6871. if (this.match(10) || this.match(45)) {
  6872. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  6873. } else {
  6874. node.key = this.createIdentifier(id, "new");
  6875. return this.tsParsePropertyOrMethodSignature(node, false);
  6876. }
  6877. }
  6878. this.tsParseModifiers(node, ["readonly"], ["declare", "abstract", "private", "protected", "public", "static", "override"], TSErrors.InvalidModifierOnTypeMember);
  6879. const idx = this.tsTryParseIndexSignature(node);
  6880. if (idx) {
  6881. return idx;
  6882. }
  6883. this.parsePropertyName(node);
  6884. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  6885. node.kind = node.key.name;
  6886. this.parsePropertyName(node);
  6887. }
  6888. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  6889. }
  6890. tsParseTypeLiteral() {
  6891. const node = this.startNode();
  6892. node.members = this.tsParseObjectTypeMembers();
  6893. return this.finishNode(node, "TSTypeLiteral");
  6894. }
  6895. tsParseObjectTypeMembers() {
  6896. this.expect(5);
  6897. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  6898. this.expect(8);
  6899. return members;
  6900. }
  6901. tsIsStartOfMappedType() {
  6902. this.next();
  6903. if (this.eat(49)) {
  6904. return this.isContextual(114);
  6905. }
  6906. if (this.isContextual(114)) {
  6907. this.next();
  6908. }
  6909. if (!this.match(0)) {
  6910. return false;
  6911. }
  6912. this.next();
  6913. if (!this.tsIsIdentifier()) {
  6914. return false;
  6915. }
  6916. this.next();
  6917. return this.match(54);
  6918. }
  6919. tsParseMappedTypeParameter() {
  6920. const node = this.startNode();
  6921. node.name = this.tsParseTypeParameterName();
  6922. node.constraint = this.tsExpectThenParseType(54);
  6923. return this.finishNode(node, "TSTypeParameter");
  6924. }
  6925. tsParseMappedType() {
  6926. const node = this.startNode();
  6927. this.expect(5);
  6928. if (this.match(49)) {
  6929. node.readonly = this.state.value;
  6930. this.next();
  6931. this.expectContextual(114);
  6932. } else if (this.eatContextual(114)) {
  6933. node.readonly = true;
  6934. }
  6935. this.expect(0);
  6936. node.typeParameter = this.tsParseMappedTypeParameter();
  6937. node.nameType = this.eatContextual(89) ? this.tsParseType() : null;
  6938. this.expect(3);
  6939. if (this.match(49)) {
  6940. node.optional = this.state.value;
  6941. this.next();
  6942. this.expect(17);
  6943. } else if (this.eat(17)) {
  6944. node.optional = true;
  6945. }
  6946. node.typeAnnotation = this.tsTryParseType();
  6947. this.semicolon();
  6948. this.expect(8);
  6949. return this.finishNode(node, "TSMappedType");
  6950. }
  6951. tsParseTupleType() {
  6952. const node = this.startNode();
  6953. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  6954. let seenOptionalElement = false;
  6955. let labeledElements = null;
  6956. node.elementTypes.forEach(elementNode => {
  6957. var _labeledElements;
  6958. let {
  6959. type
  6960. } = elementNode;
  6961. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  6962. this.raise(elementNode.start, TSErrors.OptionalTypeBeforeRequired);
  6963. }
  6964. seenOptionalElement = seenOptionalElement || type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType";
  6965. if (type === "TSRestType") {
  6966. elementNode = elementNode.typeAnnotation;
  6967. type = elementNode.type;
  6968. }
  6969. const isLabeled = type === "TSNamedTupleMember";
  6970. labeledElements = (_labeledElements = labeledElements) != null ? _labeledElements : isLabeled;
  6971. if (labeledElements !== isLabeled) {
  6972. this.raise(elementNode.start, TSErrors.MixedLabeledAndUnlabeledElements);
  6973. }
  6974. });
  6975. return this.finishNode(node, "TSTupleType");
  6976. }
  6977. tsParseTupleElementType() {
  6978. const {
  6979. start: startPos,
  6980. startLoc
  6981. } = this.state;
  6982. const rest = this.eat(21);
  6983. let type = this.tsParseType();
  6984. const optional = this.eat(17);
  6985. const labeled = this.eat(14);
  6986. if (labeled) {
  6987. const labeledNode = this.startNodeAtNode(type);
  6988. labeledNode.optional = optional;
  6989. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  6990. labeledNode.label = type.typeName;
  6991. } else {
  6992. this.raise(type.start, TSErrors.InvalidTupleMemberLabel);
  6993. labeledNode.label = type;
  6994. }
  6995. labeledNode.elementType = this.tsParseType();
  6996. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  6997. } else if (optional) {
  6998. const optionalTypeNode = this.startNodeAtNode(type);
  6999. optionalTypeNode.typeAnnotation = type;
  7000. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  7001. }
  7002. if (rest) {
  7003. const restNode = this.startNodeAt(startPos, startLoc);
  7004. restNode.typeAnnotation = type;
  7005. type = this.finishNode(restNode, "TSRestType");
  7006. }
  7007. return type;
  7008. }
  7009. tsParseParenthesizedType() {
  7010. const node = this.startNode();
  7011. this.expect(10);
  7012. node.typeAnnotation = this.tsParseType();
  7013. this.expect(11);
  7014. return this.finishNode(node, "TSParenthesizedType");
  7015. }
  7016. tsParseFunctionOrConstructorType(type, abstract) {
  7017. const node = this.startNode();
  7018. if (type === "TSConstructorType") {
  7019. node.abstract = !!abstract;
  7020. if (abstract) this.next();
  7021. this.next();
  7022. }
  7023. this.tsFillSignature(19, node);
  7024. return this.finishNode(node, type);
  7025. }
  7026. tsParseLiteralTypeNode() {
  7027. const node = this.startNode();
  7028. node.literal = (() => {
  7029. switch (this.state.type) {
  7030. case 126:
  7031. case 127:
  7032. case 125:
  7033. case 81:
  7034. case 82:
  7035. return this.parseExprAtom();
  7036. default:
  7037. throw this.unexpected();
  7038. }
  7039. })();
  7040. return this.finishNode(node, "TSLiteralType");
  7041. }
  7042. tsParseTemplateLiteralType() {
  7043. const node = this.startNode();
  7044. node.literal = this.parseTemplate(false);
  7045. return this.finishNode(node, "TSLiteralType");
  7046. }
  7047. parseTemplateSubstitution() {
  7048. if (this.state.inType) return this.tsParseType();
  7049. return super.parseTemplateSubstitution();
  7050. }
  7051. tsParseThisTypeOrThisTypePredicate() {
  7052. const thisKeyword = this.tsParseThisTypeNode();
  7053. if (this.isContextual(109) && !this.hasPrecedingLineBreak()) {
  7054. return this.tsParseThisTypePredicate(thisKeyword);
  7055. } else {
  7056. return thisKeyword;
  7057. }
  7058. }
  7059. tsParseNonArrayType() {
  7060. switch (this.state.type) {
  7061. case 125:
  7062. case 126:
  7063. case 127:
  7064. case 81:
  7065. case 82:
  7066. return this.tsParseLiteralTypeNode();
  7067. case 49:
  7068. if (this.state.value === "-") {
  7069. const node = this.startNode();
  7070. const nextToken = this.lookahead();
  7071. if (nextToken.type !== 126 && nextToken.type !== 127) {
  7072. throw this.unexpected();
  7073. }
  7074. node.literal = this.parseMaybeUnary();
  7075. return this.finishNode(node, "TSLiteralType");
  7076. }
  7077. break;
  7078. case 74:
  7079. return this.tsParseThisTypeOrThisTypePredicate();
  7080. case 83:
  7081. return this.tsParseTypeQuery();
  7082. case 79:
  7083. return this.tsParseImportType();
  7084. case 5:
  7085. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  7086. case 0:
  7087. return this.tsParseTupleType();
  7088. case 10:
  7089. return this.tsParseParenthesizedType();
  7090. case 25:
  7091. case 24:
  7092. return this.tsParseTemplateLiteralType();
  7093. default:
  7094. {
  7095. const {
  7096. type
  7097. } = this.state;
  7098. if (tokenIsIdentifier(type) || type === 84 || type === 80) {
  7099. const nodeType = type === 84 ? "TSVoidKeyword" : type === 80 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  7100. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  7101. const node = this.startNode();
  7102. this.next();
  7103. return this.finishNode(node, nodeType);
  7104. }
  7105. return this.tsParseTypeReference();
  7106. }
  7107. }
  7108. }
  7109. throw this.unexpected();
  7110. }
  7111. tsParseArrayTypeOrHigher() {
  7112. let type = this.tsParseNonArrayType();
  7113. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  7114. if (this.match(3)) {
  7115. const node = this.startNodeAtNode(type);
  7116. node.elementType = type;
  7117. this.expect(3);
  7118. type = this.finishNode(node, "TSArrayType");
  7119. } else {
  7120. const node = this.startNodeAtNode(type);
  7121. node.objectType = type;
  7122. node.indexType = this.tsParseType();
  7123. this.expect(3);
  7124. type = this.finishNode(node, "TSIndexedAccessType");
  7125. }
  7126. }
  7127. return type;
  7128. }
  7129. tsParseTypeOperator() {
  7130. const node = this.startNode();
  7131. const operator = this.state.value;
  7132. this.next();
  7133. node.operator = operator;
  7134. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  7135. if (operator === "readonly") {
  7136. this.tsCheckTypeAnnotationForReadOnly(node);
  7137. }
  7138. return this.finishNode(node, "TSTypeOperator");
  7139. }
  7140. tsCheckTypeAnnotationForReadOnly(node) {
  7141. switch (node.typeAnnotation.type) {
  7142. case "TSTupleType":
  7143. case "TSArrayType":
  7144. return;
  7145. default:
  7146. this.raise(node.start, TSErrors.UnexpectedReadonly);
  7147. }
  7148. }
  7149. tsParseInferType() {
  7150. const node = this.startNode();
  7151. this.expectContextual(108);
  7152. const typeParameter = this.startNode();
  7153. typeParameter.name = this.tsParseTypeParameterName();
  7154. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  7155. return this.finishNode(node, "TSInferType");
  7156. }
  7157. tsParseTypeOperatorOrHigher() {
  7158. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  7159. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(108) ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  7160. }
  7161. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  7162. const node = this.startNode();
  7163. const hasLeadingOperator = this.eat(operator);
  7164. const types = [];
  7165. do {
  7166. types.push(parseConstituentType());
  7167. } while (this.eat(operator));
  7168. if (types.length === 1 && !hasLeadingOperator) {
  7169. return types[0];
  7170. }
  7171. node.types = types;
  7172. return this.finishNode(node, kind);
  7173. }
  7174. tsParseIntersectionTypeOrHigher() {
  7175. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 43);
  7176. }
  7177. tsParseUnionTypeOrHigher() {
  7178. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 41);
  7179. }
  7180. tsIsStartOfFunctionType() {
  7181. if (this.match(45)) {
  7182. return true;
  7183. }
  7184. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  7185. }
  7186. tsSkipParameterStart() {
  7187. if (tokenIsIdentifier(this.state.type) || this.match(74)) {
  7188. this.next();
  7189. return true;
  7190. }
  7191. if (this.match(5)) {
  7192. let braceStackCounter = 1;
  7193. this.next();
  7194. while (braceStackCounter > 0) {
  7195. if (this.match(5)) {
  7196. ++braceStackCounter;
  7197. } else if (this.match(8)) {
  7198. --braceStackCounter;
  7199. }
  7200. this.next();
  7201. }
  7202. return true;
  7203. }
  7204. if (this.match(0)) {
  7205. let braceStackCounter = 1;
  7206. this.next();
  7207. while (braceStackCounter > 0) {
  7208. if (this.match(0)) {
  7209. ++braceStackCounter;
  7210. } else if (this.match(3)) {
  7211. --braceStackCounter;
  7212. }
  7213. this.next();
  7214. }
  7215. return true;
  7216. }
  7217. return false;
  7218. }
  7219. tsIsUnambiguouslyStartOfFunctionType() {
  7220. this.next();
  7221. if (this.match(11) || this.match(21)) {
  7222. return true;
  7223. }
  7224. if (this.tsSkipParameterStart()) {
  7225. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  7226. return true;
  7227. }
  7228. if (this.match(11)) {
  7229. this.next();
  7230. if (this.match(19)) {
  7231. return true;
  7232. }
  7233. }
  7234. }
  7235. return false;
  7236. }
  7237. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  7238. return this.tsInType(() => {
  7239. const t = this.startNode();
  7240. this.expect(returnToken);
  7241. const node = this.startNode();
  7242. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  7243. if (asserts && this.match(74)) {
  7244. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  7245. if (thisTypePredicate.type === "TSThisType") {
  7246. node.parameterName = thisTypePredicate;
  7247. node.asserts = true;
  7248. node.typeAnnotation = null;
  7249. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  7250. } else {
  7251. this.resetStartLocationFromNode(thisTypePredicate, node);
  7252. thisTypePredicate.asserts = true;
  7253. }
  7254. t.typeAnnotation = thisTypePredicate;
  7255. return this.finishNode(t, "TSTypeAnnotation");
  7256. }
  7257. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  7258. if (!typePredicateVariable) {
  7259. if (!asserts) {
  7260. return this.tsParseTypeAnnotation(false, t);
  7261. }
  7262. node.parameterName = this.parseIdentifier();
  7263. node.asserts = asserts;
  7264. node.typeAnnotation = null;
  7265. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  7266. return this.finishNode(t, "TSTypeAnnotation");
  7267. }
  7268. const type = this.tsParseTypeAnnotation(false);
  7269. node.parameterName = typePredicateVariable;
  7270. node.typeAnnotation = type;
  7271. node.asserts = asserts;
  7272. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  7273. return this.finishNode(t, "TSTypeAnnotation");
  7274. });
  7275. }
  7276. tsTryParseTypeOrTypePredicateAnnotation() {
  7277. return this.match(14) ? this.tsParseTypeOrTypePredicateAnnotation(14) : undefined;
  7278. }
  7279. tsTryParseTypeAnnotation() {
  7280. return this.match(14) ? this.tsParseTypeAnnotation() : undefined;
  7281. }
  7282. tsTryParseType() {
  7283. return this.tsEatThenParseType(14);
  7284. }
  7285. tsParseTypePredicatePrefix() {
  7286. const id = this.parseIdentifier();
  7287. if (this.isContextual(109) && !this.hasPrecedingLineBreak()) {
  7288. this.next();
  7289. return id;
  7290. }
  7291. }
  7292. tsParseTypePredicateAsserts() {
  7293. if (this.state.type !== 102) {
  7294. return false;
  7295. }
  7296. const containsEsc = this.state.containsEsc;
  7297. this.next();
  7298. if (!tokenIsIdentifier(this.state.type) && !this.match(74)) {
  7299. return false;
  7300. }
  7301. if (containsEsc) {
  7302. this.raise(this.state.lastTokStart, ErrorMessages.InvalidEscapedReservedWord, "asserts");
  7303. }
  7304. return true;
  7305. }
  7306. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  7307. this.tsInType(() => {
  7308. if (eatColon) this.expect(14);
  7309. t.typeAnnotation = this.tsParseType();
  7310. });
  7311. return this.finishNode(t, "TSTypeAnnotation");
  7312. }
  7313. tsParseType() {
  7314. assert(this.state.inType);
  7315. const type = this.tsParseNonConditionalType();
  7316. if (this.hasPrecedingLineBreak() || !this.eat(77)) {
  7317. return type;
  7318. }
  7319. const node = this.startNodeAtNode(type);
  7320. node.checkType = type;
  7321. node.extendsType = this.tsParseNonConditionalType();
  7322. this.expect(17);
  7323. node.trueType = this.tsParseType();
  7324. this.expect(14);
  7325. node.falseType = this.tsParseType();
  7326. return this.finishNode(node, "TSConditionalType");
  7327. }
  7328. isAbstractConstructorSignature() {
  7329. return this.isContextual(116) && this.lookahead().type === 73;
  7330. }
  7331. tsParseNonConditionalType() {
  7332. if (this.tsIsStartOfFunctionType()) {
  7333. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  7334. }
  7335. if (this.match(73)) {
  7336. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  7337. } else if (this.isAbstractConstructorSignature()) {
  7338. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  7339. }
  7340. return this.tsParseUnionTypeOrHigher();
  7341. }
  7342. tsParseTypeAssertion() {
  7343. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  7344. this.raise(this.state.start, TSErrors.ReservedTypeAssertion);
  7345. }
  7346. const node = this.startNode();
  7347. const _const = this.tsTryNextParseConstantContext();
  7348. node.typeAnnotation = _const || this.tsNextThenParseType();
  7349. this.expect(46);
  7350. node.expression = this.parseMaybeUnary();
  7351. return this.finishNode(node, "TSTypeAssertion");
  7352. }
  7353. tsParseHeritageClause(descriptor) {
  7354. const originalStart = this.state.start;
  7355. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  7356. if (!delimitedList.length) {
  7357. this.raise(originalStart, TSErrors.EmptyHeritageClauseType, descriptor);
  7358. }
  7359. return delimitedList;
  7360. }
  7361. tsParseExpressionWithTypeArguments() {
  7362. const node = this.startNode();
  7363. node.expression = this.tsParseEntityName(false);
  7364. if (this.match(45)) {
  7365. node.typeParameters = this.tsParseTypeArguments();
  7366. }
  7367. return this.finishNode(node, "TSExpressionWithTypeArguments");
  7368. }
  7369. tsParseInterfaceDeclaration(node) {
  7370. if (tokenIsIdentifier(this.state.type)) {
  7371. node.id = this.parseIdentifier();
  7372. this.checkLVal(node.id, "typescript interface declaration", BIND_TS_INTERFACE);
  7373. } else {
  7374. node.id = null;
  7375. this.raise(this.state.start, TSErrors.MissingInterfaceName);
  7376. }
  7377. node.typeParameters = this.tsTryParseTypeParameters();
  7378. if (this.eat(77)) {
  7379. node.extends = this.tsParseHeritageClause("extends");
  7380. }
  7381. const body = this.startNode();
  7382. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  7383. node.body = this.finishNode(body, "TSInterfaceBody");
  7384. return this.finishNode(node, "TSInterfaceDeclaration");
  7385. }
  7386. tsParseTypeAliasDeclaration(node) {
  7387. node.id = this.parseIdentifier();
  7388. this.checkLVal(node.id, "typescript type alias", BIND_TS_TYPE);
  7389. node.typeParameters = this.tsTryParseTypeParameters();
  7390. node.typeAnnotation = this.tsInType(() => {
  7391. this.expect(29);
  7392. if (this.isContextual(107) && this.lookahead().type !== 16) {
  7393. const node = this.startNode();
  7394. this.next();
  7395. return this.finishNode(node, "TSIntrinsicKeyword");
  7396. }
  7397. return this.tsParseType();
  7398. });
  7399. this.semicolon();
  7400. return this.finishNode(node, "TSTypeAliasDeclaration");
  7401. }
  7402. tsInNoContext(cb) {
  7403. const oldContext = this.state.context;
  7404. this.state.context = [oldContext[0]];
  7405. try {
  7406. return cb();
  7407. } finally {
  7408. this.state.context = oldContext;
  7409. }
  7410. }
  7411. tsInType(cb) {
  7412. const oldInType = this.state.inType;
  7413. this.state.inType = true;
  7414. try {
  7415. return cb();
  7416. } finally {
  7417. this.state.inType = oldInType;
  7418. }
  7419. }
  7420. tsEatThenParseType(token) {
  7421. return !this.match(token) ? undefined : this.tsNextThenParseType();
  7422. }
  7423. tsExpectThenParseType(token) {
  7424. return this.tsDoThenParseType(() => this.expect(token));
  7425. }
  7426. tsNextThenParseType() {
  7427. return this.tsDoThenParseType(() => this.next());
  7428. }
  7429. tsDoThenParseType(cb) {
  7430. return this.tsInType(() => {
  7431. cb();
  7432. return this.tsParseType();
  7433. });
  7434. }
  7435. tsParseEnumMember() {
  7436. const node = this.startNode();
  7437. node.id = this.match(125) ? this.parseExprAtom() : this.parseIdentifier(true);
  7438. if (this.eat(29)) {
  7439. node.initializer = this.parseMaybeAssignAllowIn();
  7440. }
  7441. return this.finishNode(node, "TSEnumMember");
  7442. }
  7443. tsParseEnumDeclaration(node, isConst) {
  7444. if (isConst) node.const = true;
  7445. node.id = this.parseIdentifier();
  7446. this.checkLVal(node.id, "typescript enum declaration", isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  7447. this.expect(5);
  7448. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  7449. this.expect(8);
  7450. return this.finishNode(node, "TSEnumDeclaration");
  7451. }
  7452. tsParseModuleBlock() {
  7453. const node = this.startNode();
  7454. this.scope.enter(SCOPE_OTHER);
  7455. this.expect(5);
  7456. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  7457. this.scope.exit();
  7458. return this.finishNode(node, "TSModuleBlock");
  7459. }
  7460. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  7461. node.id = this.parseIdentifier();
  7462. if (!nested) {
  7463. this.checkLVal(node.id, "module or namespace declaration", BIND_TS_NAMESPACE);
  7464. }
  7465. if (this.eat(16)) {
  7466. const inner = this.startNode();
  7467. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  7468. node.body = inner;
  7469. } else {
  7470. this.scope.enter(SCOPE_TS_MODULE);
  7471. this.prodParam.enter(PARAM);
  7472. node.body = this.tsParseModuleBlock();
  7473. this.prodParam.exit();
  7474. this.scope.exit();
  7475. }
  7476. return this.finishNode(node, "TSModuleDeclaration");
  7477. }
  7478. tsParseAmbientExternalModuleDeclaration(node) {
  7479. if (this.isContextual(105)) {
  7480. node.global = true;
  7481. node.id = this.parseIdentifier();
  7482. } else if (this.match(125)) {
  7483. node.id = this.parseExprAtom();
  7484. } else {
  7485. this.unexpected();
  7486. }
  7487. if (this.match(5)) {
  7488. this.scope.enter(SCOPE_TS_MODULE);
  7489. this.prodParam.enter(PARAM);
  7490. node.body = this.tsParseModuleBlock();
  7491. this.prodParam.exit();
  7492. this.scope.exit();
  7493. } else {
  7494. this.semicolon();
  7495. }
  7496. return this.finishNode(node, "TSModuleDeclaration");
  7497. }
  7498. tsParseImportEqualsDeclaration(node, isExport) {
  7499. node.isExport = isExport || false;
  7500. node.id = this.parseIdentifier();
  7501. this.checkLVal(node.id, "import equals declaration", BIND_LEXICAL);
  7502. this.expect(29);
  7503. const moduleReference = this.tsParseModuleReference();
  7504. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  7505. this.raise(moduleReference.start, TSErrors.ImportAliasHasImportType);
  7506. }
  7507. node.moduleReference = moduleReference;
  7508. this.semicolon();
  7509. return this.finishNode(node, "TSImportEqualsDeclaration");
  7510. }
  7511. tsIsExternalModuleReference() {
  7512. return this.isContextual(112) && this.lookaheadCharCode() === 40;
  7513. }
  7514. tsParseModuleReference() {
  7515. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  7516. }
  7517. tsParseExternalModuleReference() {
  7518. const node = this.startNode();
  7519. this.expectContextual(112);
  7520. this.expect(10);
  7521. if (!this.match(125)) {
  7522. throw this.unexpected();
  7523. }
  7524. node.expression = this.parseExprAtom();
  7525. this.expect(11);
  7526. return this.finishNode(node, "TSExternalModuleReference");
  7527. }
  7528. tsLookAhead(f) {
  7529. const state = this.state.clone();
  7530. const res = f();
  7531. this.state = state;
  7532. return res;
  7533. }
  7534. tsTryParseAndCatch(f) {
  7535. const result = this.tryParse(abort => f() || abort());
  7536. if (result.aborted || !result.node) return undefined;
  7537. if (result.error) this.state = result.failState;
  7538. return result.node;
  7539. }
  7540. tsTryParse(f) {
  7541. const state = this.state.clone();
  7542. const result = f();
  7543. if (result !== undefined && result !== false) {
  7544. return result;
  7545. } else {
  7546. this.state = state;
  7547. return undefined;
  7548. }
  7549. }
  7550. tsTryParseDeclare(nany) {
  7551. if (this.isLineTerminator()) {
  7552. return;
  7553. }
  7554. let starttype = this.state.type;
  7555. let kind;
  7556. if (this.isContextual(95)) {
  7557. starttype = 70;
  7558. kind = "let";
  7559. }
  7560. return this.tsInAmbientContext(() => {
  7561. switch (starttype) {
  7562. case 64:
  7563. nany.declare = true;
  7564. return this.parseFunctionStatement(nany, false, true);
  7565. case 76:
  7566. nany.declare = true;
  7567. return this.parseClass(nany, true, false);
  7568. case 71:
  7569. if (this.match(71) && this.isLookaheadContextual("enum")) {
  7570. this.expect(71);
  7571. this.expectContextual(118);
  7572. return this.tsParseEnumDeclaration(nany, true);
  7573. }
  7574. case 70:
  7575. kind = kind || this.state.value;
  7576. return this.parseVarStatement(nany, kind);
  7577. case 105:
  7578. return this.tsParseAmbientExternalModuleDeclaration(nany);
  7579. default:
  7580. {
  7581. if (tokenIsIdentifier(starttype)) {
  7582. return this.tsParseDeclaration(nany, this.state.value, true);
  7583. }
  7584. }
  7585. }
  7586. });
  7587. }
  7588. tsTryParseExportDeclaration() {
  7589. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  7590. }
  7591. tsParseExpressionStatement(node, expr) {
  7592. switch (expr.name) {
  7593. case "declare":
  7594. {
  7595. const declaration = this.tsTryParseDeclare(node);
  7596. if (declaration) {
  7597. declaration.declare = true;
  7598. return declaration;
  7599. }
  7600. break;
  7601. }
  7602. case "global":
  7603. if (this.match(5)) {
  7604. this.scope.enter(SCOPE_TS_MODULE);
  7605. this.prodParam.enter(PARAM);
  7606. const mod = node;
  7607. mod.global = true;
  7608. mod.id = expr;
  7609. mod.body = this.tsParseModuleBlock();
  7610. this.scope.exit();
  7611. this.prodParam.exit();
  7612. return this.finishNode(mod, "TSModuleDeclaration");
  7613. }
  7614. break;
  7615. default:
  7616. return this.tsParseDeclaration(node, expr.name, false);
  7617. }
  7618. }
  7619. tsParseDeclaration(node, value, next) {
  7620. switch (value) {
  7621. case "abstract":
  7622. if (this.tsCheckLineTerminator(next) && (this.match(76) || tokenIsIdentifier(this.state.type))) {
  7623. return this.tsParseAbstractDeclaration(node);
  7624. }
  7625. break;
  7626. case "enum":
  7627. if (next || tokenIsIdentifier(this.state.type)) {
  7628. if (next) this.next();
  7629. return this.tsParseEnumDeclaration(node, false);
  7630. }
  7631. break;
  7632. case "interface":
  7633. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  7634. return this.tsParseInterfaceDeclaration(node);
  7635. }
  7636. break;
  7637. case "module":
  7638. if (this.tsCheckLineTerminator(next)) {
  7639. if (this.match(125)) {
  7640. return this.tsParseAmbientExternalModuleDeclaration(node);
  7641. } else if (tokenIsIdentifier(this.state.type)) {
  7642. return this.tsParseModuleOrNamespaceDeclaration(node);
  7643. }
  7644. }
  7645. break;
  7646. case "namespace":
  7647. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  7648. return this.tsParseModuleOrNamespaceDeclaration(node);
  7649. }
  7650. break;
  7651. case "type":
  7652. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  7653. return this.tsParseTypeAliasDeclaration(node);
  7654. }
  7655. break;
  7656. }
  7657. }
  7658. tsCheckLineTerminator(next) {
  7659. if (next) {
  7660. if (this.hasFollowingLineBreak()) return false;
  7661. this.next();
  7662. return true;
  7663. }
  7664. return !this.isLineTerminator();
  7665. }
  7666. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  7667. if (!this.match(45)) {
  7668. return undefined;
  7669. }
  7670. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  7671. this.state.maybeInArrowParameters = true;
  7672. const res = this.tsTryParseAndCatch(() => {
  7673. const node = this.startNodeAt(startPos, startLoc);
  7674. node.typeParameters = this.tsParseTypeParameters();
  7675. super.parseFunctionParams(node);
  7676. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  7677. this.expect(19);
  7678. return node;
  7679. });
  7680. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  7681. if (!res) {
  7682. return undefined;
  7683. }
  7684. return this.parseArrowExpression(res, null, true);
  7685. }
  7686. tsParseTypeArguments() {
  7687. const node = this.startNode();
  7688. node.params = this.tsInType(() => this.tsInNoContext(() => {
  7689. this.expect(45);
  7690. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  7691. }));
  7692. if (node.params.length === 0) {
  7693. this.raise(node.start, TSErrors.EmptyTypeArguments);
  7694. }
  7695. this.expect(46);
  7696. return this.finishNode(node, "TSTypeParameterInstantiation");
  7697. }
  7698. tsIsDeclarationStart() {
  7699. return tokenIsTSDeclarationStart(this.state.type);
  7700. }
  7701. isExportDefaultSpecifier() {
  7702. if (this.tsIsDeclarationStart()) return false;
  7703. return super.isExportDefaultSpecifier();
  7704. }
  7705. parseAssignableListItem(allowModifiers, decorators) {
  7706. const startPos = this.state.start;
  7707. const startLoc = this.state.startLoc;
  7708. let accessibility;
  7709. let readonly = false;
  7710. let override = false;
  7711. if (allowModifiers !== undefined) {
  7712. const modified = {};
  7713. this.tsParseModifiers(modified, ["public", "private", "protected", "override", "readonly"]);
  7714. accessibility = modified.accessibility;
  7715. override = modified.override;
  7716. readonly = modified.readonly;
  7717. if (allowModifiers === false && (accessibility || readonly || override)) {
  7718. this.raise(startPos, TSErrors.UnexpectedParameterModifier);
  7719. }
  7720. }
  7721. const left = this.parseMaybeDefault();
  7722. this.parseAssignableListItemTypes(left);
  7723. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  7724. if (accessibility || readonly || override) {
  7725. const pp = this.startNodeAt(startPos, startLoc);
  7726. if (decorators.length) {
  7727. pp.decorators = decorators;
  7728. }
  7729. if (accessibility) pp.accessibility = accessibility;
  7730. if (readonly) pp.readonly = readonly;
  7731. if (override) pp.override = override;
  7732. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  7733. this.raise(pp.start, TSErrors.UnsupportedParameterPropertyKind);
  7734. }
  7735. pp.parameter = elt;
  7736. return this.finishNode(pp, "TSParameterProperty");
  7737. }
  7738. if (decorators.length) {
  7739. left.decorators = decorators;
  7740. }
  7741. return elt;
  7742. }
  7743. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  7744. if (this.match(14)) {
  7745. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  7746. }
  7747. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  7748. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  7749. this.finishNode(node, bodilessType);
  7750. return;
  7751. }
  7752. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  7753. this.raise(node.start, TSErrors.DeclareFunctionHasImplementation);
  7754. if (node.declare) {
  7755. super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  7756. return;
  7757. }
  7758. }
  7759. super.parseFunctionBodyAndFinish(node, type, isMethod);
  7760. }
  7761. registerFunctionStatementId(node) {
  7762. if (!node.body && node.id) {
  7763. this.checkLVal(node.id, "function name", BIND_TS_AMBIENT);
  7764. } else {
  7765. super.registerFunctionStatementId(...arguments);
  7766. }
  7767. }
  7768. tsCheckForInvalidTypeCasts(items) {
  7769. items.forEach(node => {
  7770. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  7771. this.raise(node.typeAnnotation.start, TSErrors.UnexpectedTypeAnnotation);
  7772. }
  7773. });
  7774. }
  7775. toReferencedList(exprList, isInParens) {
  7776. this.tsCheckForInvalidTypeCasts(exprList);
  7777. return exprList;
  7778. }
  7779. parseArrayLike(...args) {
  7780. const node = super.parseArrayLike(...args);
  7781. if (node.type === "ArrayExpression") {
  7782. this.tsCheckForInvalidTypeCasts(node.elements);
  7783. }
  7784. return node;
  7785. }
  7786. parseSubscript(base, startPos, startLoc, noCalls, state) {
  7787. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  7788. this.state.canStartJSXElement = false;
  7789. this.next();
  7790. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  7791. nonNullExpression.expression = base;
  7792. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  7793. }
  7794. let isOptionalCall = false;
  7795. if (this.match(18) && this.lookaheadCharCode() === 60) {
  7796. if (noCalls) {
  7797. state.stop = true;
  7798. return base;
  7799. }
  7800. state.optionalChainMember = isOptionalCall = true;
  7801. this.next();
  7802. }
  7803. if (this.match(45)) {
  7804. let missingParenErrorPos;
  7805. const result = this.tsTryParseAndCatch(() => {
  7806. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  7807. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  7808. if (asyncArrowFn) {
  7809. return asyncArrowFn;
  7810. }
  7811. }
  7812. const node = this.startNodeAt(startPos, startLoc);
  7813. node.callee = base;
  7814. const typeArguments = this.tsParseTypeArguments();
  7815. if (typeArguments) {
  7816. if (isOptionalCall && !this.match(10)) {
  7817. missingParenErrorPos = this.state.pos;
  7818. this.unexpected();
  7819. }
  7820. if (!noCalls && this.eat(10)) {
  7821. node.arguments = this.parseCallExpressionArguments(11, false);
  7822. this.tsCheckForInvalidTypeCasts(node.arguments);
  7823. node.typeParameters = typeArguments;
  7824. if (state.optionalChainMember) {
  7825. node.optional = isOptionalCall;
  7826. }
  7827. return this.finishCallExpression(node, state.optionalChainMember);
  7828. } else if (tokenIsTemplate(this.state.type)) {
  7829. const result = this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  7830. result.typeParameters = typeArguments;
  7831. return result;
  7832. }
  7833. }
  7834. this.unexpected();
  7835. });
  7836. if (missingParenErrorPos) {
  7837. this.unexpected(missingParenErrorPos, 10);
  7838. }
  7839. if (result) return result;
  7840. }
  7841. return super.parseSubscript(base, startPos, startLoc, noCalls, state);
  7842. }
  7843. parseNewArguments(node) {
  7844. if (this.match(45)) {
  7845. const typeParameters = this.tsTryParseAndCatch(() => {
  7846. const args = this.tsParseTypeArguments();
  7847. if (!this.match(10)) this.unexpected();
  7848. return args;
  7849. });
  7850. if (typeParameters) {
  7851. node.typeParameters = typeParameters;
  7852. }
  7853. }
  7854. super.parseNewArguments(node);
  7855. }
  7856. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  7857. if (tokenOperatorPrecedence(54) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual(89)) {
  7858. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  7859. node.expression = left;
  7860. const _const = this.tsTryNextParseConstantContext();
  7861. if (_const) {
  7862. node.typeAnnotation = _const;
  7863. } else {
  7864. node.typeAnnotation = this.tsNextThenParseType();
  7865. }
  7866. this.finishNode(node, "TSAsExpression");
  7867. this.reScan_lt_gt();
  7868. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  7869. }
  7870. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec);
  7871. }
  7872. checkReservedWord(word, startLoc, checkKeywords, isBinding) {}
  7873. checkDuplicateExports() {}
  7874. parseImport(node) {
  7875. node.importKind = "value";
  7876. if (tokenIsIdentifier(this.state.type) || this.match(51) || this.match(5)) {
  7877. let ahead = this.lookahead();
  7878. if (this.isContextual(122) && ahead.type !== 12 && ahead.type !== 93 && ahead.type !== 29) {
  7879. node.importKind = "type";
  7880. this.next();
  7881. ahead = this.lookahead();
  7882. }
  7883. if (tokenIsIdentifier(this.state.type) && ahead.type === 29) {
  7884. return this.tsParseImportEqualsDeclaration(node);
  7885. }
  7886. }
  7887. const importNode = super.parseImport(node);
  7888. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  7889. this.raise(importNode.start, TSErrors.TypeImportCannotSpecifyDefaultAndNamed);
  7890. }
  7891. return importNode;
  7892. }
  7893. parseExport(node) {
  7894. if (this.match(79)) {
  7895. this.next();
  7896. if (this.isContextual(122) && this.lookaheadCharCode() !== 61) {
  7897. node.importKind = "type";
  7898. this.next();
  7899. } else {
  7900. node.importKind = "value";
  7901. }
  7902. return this.tsParseImportEqualsDeclaration(node, true);
  7903. } else if (this.eat(29)) {
  7904. const assign = node;
  7905. assign.expression = this.parseExpression();
  7906. this.semicolon();
  7907. return this.finishNode(assign, "TSExportAssignment");
  7908. } else if (this.eatContextual(89)) {
  7909. const decl = node;
  7910. this.expectContextual(120);
  7911. decl.id = this.parseIdentifier();
  7912. this.semicolon();
  7913. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  7914. } else {
  7915. if (this.isContextual(122) && this.lookahead().type === 5) {
  7916. this.next();
  7917. node.exportKind = "type";
  7918. } else {
  7919. node.exportKind = "value";
  7920. }
  7921. return super.parseExport(node);
  7922. }
  7923. }
  7924. isAbstractClass() {
  7925. return this.isContextual(116) && this.lookahead().type === 76;
  7926. }
  7927. parseExportDefaultExpression() {
  7928. if (this.isAbstractClass()) {
  7929. const cls = this.startNode();
  7930. this.next();
  7931. cls.abstract = true;
  7932. this.parseClass(cls, true, true);
  7933. return cls;
  7934. }
  7935. if (this.match(121)) {
  7936. const interfaceNode = this.startNode();
  7937. this.next();
  7938. const result = this.tsParseInterfaceDeclaration(interfaceNode);
  7939. if (result) return result;
  7940. }
  7941. return super.parseExportDefaultExpression();
  7942. }
  7943. parseStatementContent(context, topLevel) {
  7944. if (this.state.type === 71) {
  7945. const ahead = this.lookahead();
  7946. if (ahead.type === 118) {
  7947. const node = this.startNode();
  7948. this.next();
  7949. this.expectContextual(118);
  7950. return this.tsParseEnumDeclaration(node, true);
  7951. }
  7952. }
  7953. return super.parseStatementContent(context, topLevel);
  7954. }
  7955. parseAccessModifier() {
  7956. return this.tsParseModifier(["public", "protected", "private"]);
  7957. }
  7958. tsHasSomeModifiers(member, modifiers) {
  7959. return modifiers.some(modifier => {
  7960. if (tsIsAccessModifier(modifier)) {
  7961. return member.accessibility === modifier;
  7962. }
  7963. return !!member[modifier];
  7964. });
  7965. }
  7966. tsIsStartOfStaticBlocks() {
  7967. return this.isContextual(100) && this.lookaheadCharCode() === 123;
  7968. }
  7969. parseClassMember(classBody, member, state) {
  7970. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  7971. this.tsParseModifiers(member, modifiers, undefined, undefined, true);
  7972. const callParseClassMemberWithIsStatic = () => {
  7973. if (this.tsIsStartOfStaticBlocks()) {
  7974. this.next();
  7975. this.next();
  7976. if (this.tsHasSomeModifiers(member, modifiers)) {
  7977. this.raise(this.state.pos, TSErrors.StaticBlockCannotHaveModifier);
  7978. }
  7979. this.parseClassStaticBlock(classBody, member);
  7980. } else {
  7981. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  7982. }
  7983. };
  7984. if (member.declare) {
  7985. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  7986. } else {
  7987. callParseClassMemberWithIsStatic();
  7988. }
  7989. }
  7990. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  7991. const idx = this.tsTryParseIndexSignature(member);
  7992. if (idx) {
  7993. classBody.body.push(idx);
  7994. if (member.abstract) {
  7995. this.raise(member.start, TSErrors.IndexSignatureHasAbstract);
  7996. }
  7997. if (member.accessibility) {
  7998. this.raise(member.start, TSErrors.IndexSignatureHasAccessibility, member.accessibility);
  7999. }
  8000. if (member.declare) {
  8001. this.raise(member.start, TSErrors.IndexSignatureHasDeclare);
  8002. }
  8003. if (member.override) {
  8004. this.raise(member.start, TSErrors.IndexSignatureHasOverride);
  8005. }
  8006. return;
  8007. }
  8008. if (!this.state.inAbstractClass && member.abstract) {
  8009. this.raise(member.start, TSErrors.NonAbstractClassHasAbstractMethod);
  8010. }
  8011. if (member.override) {
  8012. if (!state.hadSuperClass) {
  8013. this.raise(member.start, TSErrors.OverrideNotInSubClass);
  8014. }
  8015. }
  8016. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  8017. }
  8018. parsePostMemberNameModifiers(methodOrProp) {
  8019. const optional = this.eat(17);
  8020. if (optional) methodOrProp.optional = true;
  8021. if (methodOrProp.readonly && this.match(10)) {
  8022. this.raise(methodOrProp.start, TSErrors.ClassMethodHasReadonly);
  8023. }
  8024. if (methodOrProp.declare && this.match(10)) {
  8025. this.raise(methodOrProp.start, TSErrors.ClassMethodHasDeclare);
  8026. }
  8027. }
  8028. parseExpressionStatement(node, expr) {
  8029. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  8030. return decl || super.parseExpressionStatement(node, expr);
  8031. }
  8032. shouldParseExportDeclaration() {
  8033. if (this.tsIsDeclarationStart()) return true;
  8034. return super.shouldParseExportDeclaration();
  8035. }
  8036. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  8037. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  8038. return super.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  8039. }
  8040. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  8041. if (!result.node) {
  8042. if (result.error) {
  8043. super.setOptionalParametersError(refExpressionErrors, result.error);
  8044. }
  8045. return expr;
  8046. }
  8047. if (result.error) this.state = result.failState;
  8048. return result.node;
  8049. }
  8050. parseParenItem(node, startPos, startLoc) {
  8051. node = super.parseParenItem(node, startPos, startLoc);
  8052. if (this.eat(17)) {
  8053. node.optional = true;
  8054. this.resetEndLocation(node);
  8055. }
  8056. if (this.match(14)) {
  8057. const typeCastNode = this.startNodeAt(startPos, startLoc);
  8058. typeCastNode.expression = node;
  8059. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  8060. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  8061. }
  8062. return node;
  8063. }
  8064. parseExportDeclaration(node) {
  8065. const startPos = this.state.start;
  8066. const startLoc = this.state.startLoc;
  8067. const isDeclare = this.eatContextual(117);
  8068. if (isDeclare && (this.isContextual(117) || !this.shouldParseExportDeclaration())) {
  8069. throw this.raise(this.state.start, TSErrors.ExpectedAmbientAfterExportDeclare);
  8070. }
  8071. let declaration;
  8072. if (tokenIsIdentifier(this.state.type)) {
  8073. declaration = this.tsTryParseExportDeclaration();
  8074. }
  8075. if (!declaration) {
  8076. declaration = super.parseExportDeclaration(node);
  8077. }
  8078. if (declaration && (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare)) {
  8079. node.exportKind = "type";
  8080. }
  8081. if (declaration && isDeclare) {
  8082. this.resetStartLocation(declaration, startPos, startLoc);
  8083. declaration.declare = true;
  8084. }
  8085. return declaration;
  8086. }
  8087. parseClassId(node, isStatement, optionalId) {
  8088. if ((!isStatement || optionalId) && this.isContextual(106)) {
  8089. return;
  8090. }
  8091. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  8092. const typeParameters = this.tsTryParseTypeParameters();
  8093. if (typeParameters) node.typeParameters = typeParameters;
  8094. }
  8095. parseClassPropertyAnnotation(node) {
  8096. if (!node.optional && this.eat(35)) {
  8097. node.definite = true;
  8098. }
  8099. const type = this.tsTryParseTypeAnnotation();
  8100. if (type) node.typeAnnotation = type;
  8101. }
  8102. parseClassProperty(node) {
  8103. this.parseClassPropertyAnnotation(node);
  8104. if (this.state.isAmbientContext && this.match(29)) {
  8105. this.raise(this.state.start, TSErrors.DeclareClassFieldHasInitializer);
  8106. }
  8107. if (node.abstract && this.match(29)) {
  8108. const {
  8109. key
  8110. } = node;
  8111. this.raise(this.state.start, TSErrors.AbstractPropertyHasInitializer, key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`);
  8112. }
  8113. return super.parseClassProperty(node);
  8114. }
  8115. parseClassPrivateProperty(node) {
  8116. if (node.abstract) {
  8117. this.raise(node.start, TSErrors.PrivateElementHasAbstract);
  8118. }
  8119. if (node.accessibility) {
  8120. this.raise(node.start, TSErrors.PrivateElementHasAccessibility, node.accessibility);
  8121. }
  8122. this.parseClassPropertyAnnotation(node);
  8123. return super.parseClassPrivateProperty(node);
  8124. }
  8125. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  8126. const typeParameters = this.tsTryParseTypeParameters();
  8127. if (typeParameters && isConstructor) {
  8128. this.raise(typeParameters.start, TSErrors.ConstructorHasTypeParameters);
  8129. }
  8130. if (method.declare && (method.kind === "get" || method.kind === "set")) {
  8131. this.raise(method.start, TSErrors.DeclareAccessor, method.kind);
  8132. }
  8133. if (typeParameters) method.typeParameters = typeParameters;
  8134. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  8135. }
  8136. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  8137. const typeParameters = this.tsTryParseTypeParameters();
  8138. if (typeParameters) method.typeParameters = typeParameters;
  8139. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  8140. }
  8141. declareClassPrivateMethodInScope(node, kind) {
  8142. if (node.type === "TSDeclareMethod") return;
  8143. if (node.type === "MethodDefinition" && !node.value.body) return;
  8144. super.declareClassPrivateMethodInScope(node, kind);
  8145. }
  8146. parseClassSuper(node) {
  8147. super.parseClassSuper(node);
  8148. if (node.superClass && this.match(45)) {
  8149. node.superTypeParameters = this.tsParseTypeArguments();
  8150. }
  8151. if (this.eatContextual(106)) {
  8152. node.implements = this.tsParseHeritageClause("implements");
  8153. }
  8154. }
  8155. parseObjPropValue(prop, ...args) {
  8156. const typeParameters = this.tsTryParseTypeParameters();
  8157. if (typeParameters) prop.typeParameters = typeParameters;
  8158. super.parseObjPropValue(prop, ...args);
  8159. }
  8160. parseFunctionParams(node, allowModifiers) {
  8161. const typeParameters = this.tsTryParseTypeParameters();
  8162. if (typeParameters) node.typeParameters = typeParameters;
  8163. super.parseFunctionParams(node, allowModifiers);
  8164. }
  8165. parseVarId(decl, kind) {
  8166. super.parseVarId(decl, kind);
  8167. if (decl.id.type === "Identifier" && this.eat(35)) {
  8168. decl.definite = true;
  8169. }
  8170. const type = this.tsTryParseTypeAnnotation();
  8171. if (type) {
  8172. decl.id.typeAnnotation = type;
  8173. this.resetEndLocation(decl.id);
  8174. }
  8175. }
  8176. parseAsyncArrowFromCallExpression(node, call) {
  8177. if (this.match(14)) {
  8178. node.returnType = this.tsParseTypeAnnotation();
  8179. }
  8180. return super.parseAsyncArrowFromCallExpression(node, call);
  8181. }
  8182. parseMaybeAssign(...args) {
  8183. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  8184. let state;
  8185. let jsx;
  8186. let typeCast;
  8187. if (this.hasPlugin("jsx") && (this.match(134) || this.match(45))) {
  8188. state = this.state.clone();
  8189. jsx = this.tryParse(() => super.parseMaybeAssign(...args), state);
  8190. if (!jsx.error) return jsx.node;
  8191. const {
  8192. context
  8193. } = this.state;
  8194. const currentContext = context[context.length - 1];
  8195. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  8196. context.pop();
  8197. }
  8198. }
  8199. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(45)) {
  8200. return super.parseMaybeAssign(...args);
  8201. }
  8202. let typeParameters;
  8203. state = state || this.state.clone();
  8204. const arrow = this.tryParse(abort => {
  8205. var _expr$extra, _typeParameters;
  8206. typeParameters = this.tsParseTypeParameters();
  8207. const expr = super.parseMaybeAssign(...args);
  8208. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  8209. abort();
  8210. }
  8211. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  8212. this.resetStartLocationFromNode(expr, typeParameters);
  8213. }
  8214. expr.typeParameters = typeParameters;
  8215. return expr;
  8216. }, state);
  8217. if (!arrow.error && !arrow.aborted) {
  8218. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  8219. return arrow.node;
  8220. }
  8221. if (!jsx) {
  8222. assert(!this.hasPlugin("jsx"));
  8223. typeCast = this.tryParse(() => super.parseMaybeAssign(...args), state);
  8224. if (!typeCast.error) return typeCast.node;
  8225. }
  8226. if ((_jsx2 = jsx) != null && _jsx2.node) {
  8227. this.state = jsx.failState;
  8228. return jsx.node;
  8229. }
  8230. if (arrow.node) {
  8231. this.state = arrow.failState;
  8232. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  8233. return arrow.node;
  8234. }
  8235. if ((_typeCast = typeCast) != null && _typeCast.node) {
  8236. this.state = typeCast.failState;
  8237. return typeCast.node;
  8238. }
  8239. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  8240. if (arrow.thrown) throw arrow.error;
  8241. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  8242. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  8243. }
  8244. reportReservedArrowTypeParam(node) {
  8245. var _node$extra;
  8246. if (node.params.length === 1 && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8247. this.raise(node.start, TSErrors.ReservedArrowTypeParam);
  8248. }
  8249. }
  8250. parseMaybeUnary(refExpressionErrors) {
  8251. if (!this.hasPlugin("jsx") && this.match(45)) {
  8252. return this.tsParseTypeAssertion();
  8253. } else {
  8254. return super.parseMaybeUnary(refExpressionErrors);
  8255. }
  8256. }
  8257. parseArrow(node) {
  8258. if (this.match(14)) {
  8259. const result = this.tryParse(abort => {
  8260. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  8261. if (this.canInsertSemicolon() || !this.match(19)) abort();
  8262. return returnType;
  8263. });
  8264. if (result.aborted) return;
  8265. if (!result.thrown) {
  8266. if (result.error) this.state = result.failState;
  8267. node.returnType = result.node;
  8268. }
  8269. }
  8270. return super.parseArrow(node);
  8271. }
  8272. parseAssignableListItemTypes(param) {
  8273. if (this.eat(17)) {
  8274. if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
  8275. this.raise(param.start, TSErrors.PatternIsOptional);
  8276. }
  8277. param.optional = true;
  8278. }
  8279. const type = this.tsTryParseTypeAnnotation();
  8280. if (type) param.typeAnnotation = type;
  8281. this.resetEndLocation(param);
  8282. return param;
  8283. }
  8284. isAssignable(node, isBinding) {
  8285. switch (node.type) {
  8286. case "TSTypeCastExpression":
  8287. return this.isAssignable(node.expression, isBinding);
  8288. case "TSParameterProperty":
  8289. return true;
  8290. default:
  8291. return super.isAssignable(node, isBinding);
  8292. }
  8293. }
  8294. toAssignable(node, isLHS = false) {
  8295. switch (node.type) {
  8296. case "TSTypeCastExpression":
  8297. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  8298. case "TSParameterProperty":
  8299. return super.toAssignable(node, isLHS);
  8300. case "ParenthesizedExpression":
  8301. return this.toAssignableParenthesizedExpression(node, isLHS);
  8302. case "TSAsExpression":
  8303. case "TSNonNullExpression":
  8304. case "TSTypeAssertion":
  8305. node.expression = this.toAssignable(node.expression, isLHS);
  8306. return node;
  8307. default:
  8308. return super.toAssignable(node, isLHS);
  8309. }
  8310. }
  8311. toAssignableParenthesizedExpression(node, isLHS) {
  8312. switch (node.expression.type) {
  8313. case "TSAsExpression":
  8314. case "TSNonNullExpression":
  8315. case "TSTypeAssertion":
  8316. case "ParenthesizedExpression":
  8317. node.expression = this.toAssignable(node.expression, isLHS);
  8318. return node;
  8319. default:
  8320. return super.toAssignable(node, isLHS);
  8321. }
  8322. }
  8323. checkLVal(expr, contextDescription, ...args) {
  8324. var _expr$extra2;
  8325. switch (expr.type) {
  8326. case "TSTypeCastExpression":
  8327. return;
  8328. case "TSParameterProperty":
  8329. this.checkLVal(expr.parameter, "parameter property", ...args);
  8330. return;
  8331. case "TSAsExpression":
  8332. case "TSTypeAssertion":
  8333. if (!args[0] && contextDescription !== "parenthesized expression" && !((_expr$extra2 = expr.extra) != null && _expr$extra2.parenthesized)) {
  8334. this.raise(expr.start, ErrorMessages.InvalidLhs, contextDescription);
  8335. break;
  8336. }
  8337. this.checkLVal(expr.expression, "parenthesized expression", ...args);
  8338. return;
  8339. case "TSNonNullExpression":
  8340. this.checkLVal(expr.expression, contextDescription, ...args);
  8341. return;
  8342. default:
  8343. super.checkLVal(expr, contextDescription, ...args);
  8344. return;
  8345. }
  8346. }
  8347. parseBindingAtom() {
  8348. switch (this.state.type) {
  8349. case 74:
  8350. return this.parseIdentifier(true);
  8351. default:
  8352. return super.parseBindingAtom();
  8353. }
  8354. }
  8355. parseMaybeDecoratorArguments(expr) {
  8356. if (this.match(45)) {
  8357. const typeArguments = this.tsParseTypeArguments();
  8358. if (this.match(10)) {
  8359. const call = super.parseMaybeDecoratorArguments(expr);
  8360. call.typeParameters = typeArguments;
  8361. return call;
  8362. }
  8363. this.unexpected(this.state.start, 10);
  8364. }
  8365. return super.parseMaybeDecoratorArguments(expr);
  8366. }
  8367. checkCommaAfterRest(close) {
  8368. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  8369. this.next();
  8370. } else {
  8371. super.checkCommaAfterRest(close);
  8372. }
  8373. }
  8374. isClassMethod() {
  8375. return this.match(45) || super.isClassMethod();
  8376. }
  8377. isClassProperty() {
  8378. return this.match(35) || this.match(14) || super.isClassProperty();
  8379. }
  8380. parseMaybeDefault(...args) {
  8381. const node = super.parseMaybeDefault(...args);
  8382. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  8383. this.raise(node.typeAnnotation.start, TSErrors.TypeAnnotationAfterAssign);
  8384. }
  8385. return node;
  8386. }
  8387. getTokenFromCode(code) {
  8388. if (this.state.inType) {
  8389. if (code === 62) {
  8390. return this.finishOp(46, 1);
  8391. }
  8392. if (code === 60) {
  8393. return this.finishOp(45, 1);
  8394. }
  8395. }
  8396. return super.getTokenFromCode(code);
  8397. }
  8398. reScan_lt_gt() {
  8399. const {
  8400. type
  8401. } = this.state;
  8402. if (type === 45) {
  8403. this.state.pos -= 1;
  8404. this.readToken_lt();
  8405. } else if (type === 46) {
  8406. this.state.pos -= 1;
  8407. this.readToken_gt();
  8408. }
  8409. }
  8410. toAssignableList(exprList) {
  8411. for (let i = 0; i < exprList.length; i++) {
  8412. const expr = exprList[i];
  8413. if (!expr) continue;
  8414. switch (expr.type) {
  8415. case "TSTypeCastExpression":
  8416. exprList[i] = this.typeCastToParameter(expr);
  8417. break;
  8418. case "TSAsExpression":
  8419. case "TSTypeAssertion":
  8420. if (!this.state.maybeInArrowParameters) {
  8421. exprList[i] = this.typeCastToParameter(expr);
  8422. } else {
  8423. this.raise(expr.start, TSErrors.UnexpectedTypeCastInParameter);
  8424. }
  8425. break;
  8426. }
  8427. }
  8428. return super.toAssignableList(...arguments);
  8429. }
  8430. typeCastToParameter(node) {
  8431. node.expression.typeAnnotation = node.typeAnnotation;
  8432. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  8433. return node.expression;
  8434. }
  8435. shouldParseArrow(params) {
  8436. if (this.match(14)) {
  8437. return params.every(expr => this.isAssignable(expr, true));
  8438. }
  8439. return super.shouldParseArrow(params);
  8440. }
  8441. shouldParseAsyncArrow() {
  8442. return this.match(14) || super.shouldParseAsyncArrow();
  8443. }
  8444. canHaveLeadingDecorator() {
  8445. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  8446. }
  8447. jsxParseOpeningElementAfterName(node) {
  8448. if (this.match(45)) {
  8449. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArguments());
  8450. if (typeArguments) node.typeParameters = typeArguments;
  8451. }
  8452. return super.jsxParseOpeningElementAfterName(node);
  8453. }
  8454. getGetterSetterExpectedParamCount(method) {
  8455. const baseCount = super.getGetterSetterExpectedParamCount(method);
  8456. const params = this.getObjectOrClassMethodParams(method);
  8457. const firstParam = params[0];
  8458. const hasContextParam = firstParam && this.isThisParam(firstParam);
  8459. return hasContextParam ? baseCount + 1 : baseCount;
  8460. }
  8461. parseCatchClauseParam() {
  8462. const param = super.parseCatchClauseParam();
  8463. const type = this.tsTryParseTypeAnnotation();
  8464. if (type) {
  8465. param.typeAnnotation = type;
  8466. this.resetEndLocation(param);
  8467. }
  8468. return param;
  8469. }
  8470. tsInAmbientContext(cb) {
  8471. const oldIsAmbientContext = this.state.isAmbientContext;
  8472. this.state.isAmbientContext = true;
  8473. try {
  8474. return cb();
  8475. } finally {
  8476. this.state.isAmbientContext = oldIsAmbientContext;
  8477. }
  8478. }
  8479. parseClass(node, ...args) {
  8480. const oldInAbstractClass = this.state.inAbstractClass;
  8481. this.state.inAbstractClass = !!node.abstract;
  8482. try {
  8483. return super.parseClass(node, ...args);
  8484. } finally {
  8485. this.state.inAbstractClass = oldInAbstractClass;
  8486. }
  8487. }
  8488. tsParseAbstractDeclaration(node) {
  8489. if (this.match(76)) {
  8490. node.abstract = true;
  8491. return this.parseClass(node, true, false);
  8492. } else if (this.isContextual(121)) {
  8493. if (!this.hasFollowingLineBreak()) {
  8494. node.abstract = true;
  8495. this.raise(node.start, TSErrors.NonClassMethodPropertyHasAbstractModifer);
  8496. this.next();
  8497. return this.tsParseInterfaceDeclaration(node);
  8498. }
  8499. } else {
  8500. this.unexpected(null, 76);
  8501. }
  8502. }
  8503. parseMethod(...args) {
  8504. const method = super.parseMethod(...args);
  8505. if (method.abstract) {
  8506. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  8507. if (hasBody) {
  8508. const {
  8509. key
  8510. } = method;
  8511. this.raise(method.start, TSErrors.AbstractMethodHasImplementation, key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`);
  8512. }
  8513. }
  8514. return method;
  8515. }
  8516. tsParseTypeParameterName() {
  8517. const typeName = this.parseIdentifier();
  8518. return typeName.name;
  8519. }
  8520. shouldParseAsAmbientContext() {
  8521. return !!this.getPluginOption("typescript", "dts");
  8522. }
  8523. parse() {
  8524. if (this.shouldParseAsAmbientContext()) {
  8525. this.state.isAmbientContext = true;
  8526. }
  8527. return super.parse();
  8528. }
  8529. getExpression() {
  8530. if (this.shouldParseAsAmbientContext()) {
  8531. this.state.isAmbientContext = true;
  8532. }
  8533. return super.getExpression();
  8534. }
  8535. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  8536. if (!isString && isMaybeTypeOnly) {
  8537. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  8538. return this.finishNode(node, "ExportSpecifier");
  8539. }
  8540. node.exportKind = "value";
  8541. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  8542. }
  8543. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  8544. if (!importedIsString && isMaybeTypeOnly) {
  8545. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  8546. return this.finishNode(specifier, "ImportSpecifier");
  8547. }
  8548. specifier.importKind = "value";
  8549. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly);
  8550. }
  8551. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  8552. const leftOfAsKey = isImport ? "imported" : "local";
  8553. const rightOfAsKey = isImport ? "local" : "exported";
  8554. let leftOfAs = node[leftOfAsKey];
  8555. let rightOfAs;
  8556. let hasTypeSpecifier = false;
  8557. let canParseAsKeyword = true;
  8558. const pos = leftOfAs.start;
  8559. if (this.isContextual(89)) {
  8560. const firstAs = this.parseIdentifier();
  8561. if (this.isContextual(89)) {
  8562. const secondAs = this.parseIdentifier();
  8563. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  8564. hasTypeSpecifier = true;
  8565. leftOfAs = firstAs;
  8566. rightOfAs = this.parseIdentifier();
  8567. canParseAsKeyword = false;
  8568. } else {
  8569. rightOfAs = secondAs;
  8570. canParseAsKeyword = false;
  8571. }
  8572. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  8573. canParseAsKeyword = false;
  8574. rightOfAs = this.parseIdentifier();
  8575. } else {
  8576. hasTypeSpecifier = true;
  8577. leftOfAs = firstAs;
  8578. }
  8579. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  8580. hasTypeSpecifier = true;
  8581. leftOfAs = this.parseIdentifier();
  8582. }
  8583. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  8584. this.raise(pos, isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports);
  8585. }
  8586. node[leftOfAsKey] = leftOfAs;
  8587. node[rightOfAsKey] = rightOfAs;
  8588. const kindKey = isImport ? "importKind" : "exportKind";
  8589. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  8590. if (canParseAsKeyword && this.eatContextual(89)) {
  8591. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  8592. }
  8593. if (!node[rightOfAsKey]) {
  8594. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  8595. }
  8596. if (isImport) {
  8597. this.checkLVal(node[rightOfAsKey], "import specifier", BIND_LEXICAL);
  8598. }
  8599. }
  8600. });
  8601. const PlaceHolderErrors = makeErrorTemplates({
  8602. ClassNameIsRequired: "A class name is required."
  8603. }, ErrorCodes.SyntaxError);
  8604. var placeholders = (superClass => class extends superClass {
  8605. parsePlaceholder(expectedNode) {
  8606. if (this.match(136)) {
  8607. const node = this.startNode();
  8608. this.next();
  8609. this.assertNoSpace("Unexpected space in placeholder.");
  8610. node.name = super.parseIdentifier(true);
  8611. this.assertNoSpace("Unexpected space in placeholder.");
  8612. this.expect(136);
  8613. return this.finishPlaceholder(node, expectedNode);
  8614. }
  8615. }
  8616. finishPlaceholder(node, expectedNode) {
  8617. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  8618. node.expectedNode = expectedNode;
  8619. return isFinished ? node : this.finishNode(node, "Placeholder");
  8620. }
  8621. getTokenFromCode(code) {
  8622. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  8623. return this.finishOp(136, 2);
  8624. }
  8625. return super.getTokenFromCode(...arguments);
  8626. }
  8627. parseExprAtom() {
  8628. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  8629. }
  8630. parseIdentifier() {
  8631. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  8632. }
  8633. checkReservedWord(word) {
  8634. if (word !== undefined) super.checkReservedWord(...arguments);
  8635. }
  8636. parseBindingAtom() {
  8637. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  8638. }
  8639. checkLVal(expr) {
  8640. if (expr.type !== "Placeholder") super.checkLVal(...arguments);
  8641. }
  8642. toAssignable(node) {
  8643. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  8644. node.expectedNode = "Pattern";
  8645. return node;
  8646. }
  8647. return super.toAssignable(...arguments);
  8648. }
  8649. isLet(context) {
  8650. if (super.isLet(context)) {
  8651. return true;
  8652. }
  8653. if (!this.isContextual(95)) {
  8654. return false;
  8655. }
  8656. if (context) return false;
  8657. const nextToken = this.lookahead();
  8658. if (nextToken.type === 136) {
  8659. return true;
  8660. }
  8661. return false;
  8662. }
  8663. verifyBreakContinue(node) {
  8664. if (node.label && node.label.type === "Placeholder") return;
  8665. super.verifyBreakContinue(...arguments);
  8666. }
  8667. parseExpressionStatement(node, expr) {
  8668. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  8669. return super.parseExpressionStatement(...arguments);
  8670. }
  8671. if (this.match(14)) {
  8672. const stmt = node;
  8673. stmt.label = this.finishPlaceholder(expr, "Identifier");
  8674. this.next();
  8675. stmt.body = this.parseStatement("label");
  8676. return this.finishNode(stmt, "LabeledStatement");
  8677. }
  8678. this.semicolon();
  8679. node.name = expr.name;
  8680. return this.finishPlaceholder(node, "Statement");
  8681. }
  8682. parseBlock() {
  8683. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  8684. }
  8685. parseFunctionId() {
  8686. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  8687. }
  8688. parseClass(node, isStatement, optionalId) {
  8689. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  8690. this.next();
  8691. this.takeDecorators(node);
  8692. const oldStrict = this.state.strict;
  8693. const placeholder = this.parsePlaceholder("Identifier");
  8694. if (placeholder) {
  8695. if (this.match(77) || this.match(136) || this.match(5)) {
  8696. node.id = placeholder;
  8697. } else if (optionalId || !isStatement) {
  8698. node.id = null;
  8699. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  8700. return this.finishNode(node, type);
  8701. } else {
  8702. this.unexpected(null, PlaceHolderErrors.ClassNameIsRequired);
  8703. }
  8704. } else {
  8705. this.parseClassId(node, isStatement, optionalId);
  8706. }
  8707. this.parseClassSuper(node);
  8708. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass, oldStrict);
  8709. return this.finishNode(node, type);
  8710. }
  8711. parseExport(node) {
  8712. const placeholder = this.parsePlaceholder("Identifier");
  8713. if (!placeholder) return super.parseExport(...arguments);
  8714. if (!this.isContextual(93) && !this.match(12)) {
  8715. node.specifiers = [];
  8716. node.source = null;
  8717. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  8718. return this.finishNode(node, "ExportNamedDeclaration");
  8719. }
  8720. this.expectPlugin("exportDefaultFrom");
  8721. const specifier = this.startNode();
  8722. specifier.exported = placeholder;
  8723. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  8724. return super.parseExport(node);
  8725. }
  8726. isExportDefaultSpecifier() {
  8727. if (this.match(61)) {
  8728. const next = this.nextTokenStart();
  8729. if (this.isUnparsedContextual(next, "from")) {
  8730. if (this.input.startsWith(tokenLabelName(136), this.nextTokenStartSince(next + 4))) {
  8731. return true;
  8732. }
  8733. }
  8734. }
  8735. return super.isExportDefaultSpecifier();
  8736. }
  8737. maybeParseExportDefaultSpecifier(node) {
  8738. if (node.specifiers && node.specifiers.length > 0) {
  8739. return true;
  8740. }
  8741. return super.maybeParseExportDefaultSpecifier(...arguments);
  8742. }
  8743. checkExport(node) {
  8744. const {
  8745. specifiers
  8746. } = node;
  8747. if (specifiers != null && specifiers.length) {
  8748. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  8749. }
  8750. super.checkExport(node);
  8751. node.specifiers = specifiers;
  8752. }
  8753. parseImport(node) {
  8754. const placeholder = this.parsePlaceholder("Identifier");
  8755. if (!placeholder) return super.parseImport(...arguments);
  8756. node.specifiers = [];
  8757. if (!this.isContextual(93) && !this.match(12)) {
  8758. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  8759. this.semicolon();
  8760. return this.finishNode(node, "ImportDeclaration");
  8761. }
  8762. const specifier = this.startNodeAtNode(placeholder);
  8763. specifier.local = placeholder;
  8764. this.finishNode(specifier, "ImportDefaultSpecifier");
  8765. node.specifiers.push(specifier);
  8766. if (this.eat(12)) {
  8767. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  8768. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  8769. }
  8770. this.expectContextual(93);
  8771. node.source = this.parseImportSource();
  8772. this.semicolon();
  8773. return this.finishNode(node, "ImportDeclaration");
  8774. }
  8775. parseImportSource() {
  8776. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  8777. }
  8778. });
  8779. var v8intrinsic = (superClass => class extends superClass {
  8780. parseV8Intrinsic() {
  8781. if (this.match(50)) {
  8782. const v8IntrinsicStart = this.state.start;
  8783. const node = this.startNode();
  8784. this.next();
  8785. if (tokenIsIdentifier(this.state.type)) {
  8786. const name = this.parseIdentifierName(this.state.start);
  8787. const identifier = this.createIdentifier(node, name);
  8788. identifier.type = "V8IntrinsicIdentifier";
  8789. if (this.match(10)) {
  8790. return identifier;
  8791. }
  8792. }
  8793. this.unexpected(v8IntrinsicStart);
  8794. }
  8795. }
  8796. parseExprAtom() {
  8797. return this.parseV8Intrinsic() || super.parseExprAtom(...arguments);
  8798. }
  8799. });
  8800. function hasPlugin(plugins, expectedConfig) {
  8801. const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
  8802. const expectedKeys = Object.keys(expectedOptions);
  8803. const expectedOptionsIsEmpty = expectedKeys.length === 0;
  8804. return plugins.some(p => {
  8805. if (typeof p === "string") {
  8806. return expectedOptionsIsEmpty && p === expectedName;
  8807. } else {
  8808. const [pluginName, pluginOptions] = p;
  8809. if (pluginName !== expectedName) {
  8810. return false;
  8811. }
  8812. for (const key of expectedKeys) {
  8813. if (pluginOptions[key] !== expectedOptions[key]) {
  8814. return false;
  8815. }
  8816. }
  8817. return true;
  8818. }
  8819. });
  8820. }
  8821. function getPluginOption(plugins, name, option) {
  8822. const plugin = plugins.find(plugin => {
  8823. if (Array.isArray(plugin)) {
  8824. return plugin[0] === name;
  8825. } else {
  8826. return plugin === name;
  8827. }
  8828. });
  8829. if (plugin && Array.isArray(plugin)) {
  8830. return plugin[1][option];
  8831. }
  8832. return null;
  8833. }
  8834. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  8835. const TOPIC_TOKENS = ["^", "%", "#"];
  8836. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  8837. function validatePlugins(plugins) {
  8838. if (hasPlugin(plugins, "decorators")) {
  8839. if (hasPlugin(plugins, "decorators-legacy")) {
  8840. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  8841. }
  8842. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  8843. if (decoratorsBeforeExport == null) {
  8844. 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'.");
  8845. } else if (typeof decoratorsBeforeExport !== "boolean") {
  8846. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  8847. }
  8848. }
  8849. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  8850. throw new Error("Cannot combine flow and typescript plugins.");
  8851. }
  8852. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  8853. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  8854. }
  8855. if (hasPlugin(plugins, "pipelineOperator")) {
  8856. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  8857. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  8858. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  8859. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  8860. }
  8861. const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
  8862. syntaxType: "hash"
  8863. }]);
  8864. if (proposal === "hack") {
  8865. if (hasPlugin(plugins, "placeholders")) {
  8866. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  8867. }
  8868. if (hasPlugin(plugins, "v8intrinsic")) {
  8869. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  8870. }
  8871. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  8872. if (!TOPIC_TOKENS.includes(topicToken)) {
  8873. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  8874. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  8875. }
  8876. if (topicToken === "#" && tupleSyntaxIsHash) {
  8877. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  8878. }
  8879. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  8880. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  8881. }
  8882. }
  8883. if (hasPlugin(plugins, "moduleAttributes")) {
  8884. {
  8885. if (hasPlugin(plugins, "importAssertions")) {
  8886. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  8887. }
  8888. const moduleAttributesVerionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  8889. if (moduleAttributesVerionPluginOption !== "may-2020") {
  8890. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  8891. }
  8892. }
  8893. }
  8894. if (hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  8895. throw new Error("'recordAndTuple' requires 'syntaxType' option whose value should be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  8896. }
  8897. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  8898. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  8899. error.missingPlugins = "doExpressions";
  8900. throw error;
  8901. }
  8902. }
  8903. const mixinPlugins = {
  8904. estree,
  8905. jsx,
  8906. flow,
  8907. typescript,
  8908. v8intrinsic,
  8909. placeholders
  8910. };
  8911. const mixinPluginNames = Object.keys(mixinPlugins);
  8912. const defaultOptions = {
  8913. sourceType: "script",
  8914. sourceFilename: undefined,
  8915. startColumn: 0,
  8916. startLine: 1,
  8917. allowAwaitOutsideFunction: false,
  8918. allowReturnOutsideFunction: false,
  8919. allowImportExportEverywhere: false,
  8920. allowSuperOutsideMethod: false,
  8921. allowUndeclaredExports: false,
  8922. plugins: [],
  8923. strictMode: null,
  8924. ranges: false,
  8925. tokens: false,
  8926. createParenthesizedExpressions: false,
  8927. errorRecovery: false,
  8928. attachComment: true
  8929. };
  8930. function getOptions(opts) {
  8931. const options = {};
  8932. for (const key of Object.keys(defaultOptions)) {
  8933. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  8934. }
  8935. return options;
  8936. }
  8937. const unwrapParenthesizedExpression = node => {
  8938. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  8939. };
  8940. class LValParser extends NodeUtils {
  8941. toAssignable(node, isLHS = false) {
  8942. var _node$extra, _node$extra3;
  8943. let parenthesized = undefined;
  8944. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  8945. parenthesized = unwrapParenthesizedExpression(node);
  8946. if (isLHS) {
  8947. if (parenthesized.type === "Identifier") {
  8948. this.expressionScope.recordParenthesizedIdentifierError(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8949. } else if (parenthesized.type !== "MemberExpression") {
  8950. this.raise(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8951. }
  8952. } else {
  8953. this.raise(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8954. }
  8955. }
  8956. switch (node.type) {
  8957. case "Identifier":
  8958. case "ObjectPattern":
  8959. case "ArrayPattern":
  8960. case "AssignmentPattern":
  8961. case "RestElement":
  8962. break;
  8963. case "ObjectExpression":
  8964. node.type = "ObjectPattern";
  8965. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  8966. var _node$extra2;
  8967. const prop = node.properties[i];
  8968. const isLast = i === last;
  8969. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  8970. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingComma) {
  8971. this.raiseRestNotLast(node.extra.trailingComma);
  8972. }
  8973. }
  8974. break;
  8975. case "ObjectProperty":
  8976. this.toAssignable(node.value, isLHS);
  8977. break;
  8978. case "SpreadElement":
  8979. {
  8980. this.checkToRestConversion(node);
  8981. node.type = "RestElement";
  8982. const arg = node.argument;
  8983. this.toAssignable(arg, isLHS);
  8984. break;
  8985. }
  8986. case "ArrayExpression":
  8987. node.type = "ArrayPattern";
  8988. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingComma, isLHS);
  8989. break;
  8990. case "AssignmentExpression":
  8991. if (node.operator !== "=") {
  8992. this.raise(node.left.end, ErrorMessages.MissingEqInAssignment);
  8993. }
  8994. node.type = "AssignmentPattern";
  8995. delete node.operator;
  8996. this.toAssignable(node.left, isLHS);
  8997. break;
  8998. case "ParenthesizedExpression":
  8999. this.toAssignable(parenthesized, isLHS);
  9000. break;
  9001. }
  9002. return node;
  9003. }
  9004. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  9005. if (prop.type === "ObjectMethod") {
  9006. const error = prop.kind === "get" || prop.kind === "set" ? ErrorMessages.PatternHasAccessor : ErrorMessages.PatternHasMethod;
  9007. this.raise(prop.key.start, error);
  9008. } else if (prop.type === "SpreadElement" && !isLast) {
  9009. this.raiseRestNotLast(prop.start);
  9010. } else {
  9011. this.toAssignable(prop, isLHS);
  9012. }
  9013. }
  9014. toAssignableList(exprList, trailingCommaPos, isLHS) {
  9015. let end = exprList.length;
  9016. if (end) {
  9017. const last = exprList[end - 1];
  9018. if ((last == null ? void 0 : last.type) === "RestElement") {
  9019. --end;
  9020. } else if ((last == null ? void 0 : last.type) === "SpreadElement") {
  9021. last.type = "RestElement";
  9022. let arg = last.argument;
  9023. this.toAssignable(arg, isLHS);
  9024. arg = unwrapParenthesizedExpression(arg);
  9025. if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
  9026. this.unexpected(arg.start);
  9027. }
  9028. if (trailingCommaPos) {
  9029. this.raiseTrailingCommaAfterRest(trailingCommaPos);
  9030. }
  9031. --end;
  9032. }
  9033. }
  9034. for (let i = 0; i < end; i++) {
  9035. const elt = exprList[i];
  9036. if (elt) {
  9037. this.toAssignable(elt, isLHS);
  9038. if (elt.type === "RestElement") {
  9039. this.raiseRestNotLast(elt.start);
  9040. }
  9041. }
  9042. }
  9043. return exprList;
  9044. }
  9045. isAssignable(node, isBinding) {
  9046. switch (node.type) {
  9047. case "Identifier":
  9048. case "ObjectPattern":
  9049. case "ArrayPattern":
  9050. case "AssignmentPattern":
  9051. case "RestElement":
  9052. return true;
  9053. case "ObjectExpression":
  9054. {
  9055. const last = node.properties.length - 1;
  9056. return node.properties.every((prop, i) => {
  9057. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  9058. });
  9059. }
  9060. case "ObjectProperty":
  9061. return this.isAssignable(node.value);
  9062. case "SpreadElement":
  9063. return this.isAssignable(node.argument);
  9064. case "ArrayExpression":
  9065. return node.elements.every(element => element === null || this.isAssignable(element));
  9066. case "AssignmentExpression":
  9067. return node.operator === "=";
  9068. case "ParenthesizedExpression":
  9069. return this.isAssignable(node.expression);
  9070. case "MemberExpression":
  9071. case "OptionalMemberExpression":
  9072. return !isBinding;
  9073. default:
  9074. return false;
  9075. }
  9076. }
  9077. toReferencedList(exprList, isParenthesizedExpr) {
  9078. return exprList;
  9079. }
  9080. toReferencedListDeep(exprList, isParenthesizedExpr) {
  9081. this.toReferencedList(exprList, isParenthesizedExpr);
  9082. for (const expr of exprList) {
  9083. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  9084. this.toReferencedListDeep(expr.elements);
  9085. }
  9086. }
  9087. }
  9088. parseSpread(refExpressionErrors, refNeedsArrowPos) {
  9089. const node = this.startNode();
  9090. this.next();
  9091. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined, refNeedsArrowPos);
  9092. return this.finishNode(node, "SpreadElement");
  9093. }
  9094. parseRestBinding() {
  9095. const node = this.startNode();
  9096. this.next();
  9097. node.argument = this.parseBindingAtom();
  9098. return this.finishNode(node, "RestElement");
  9099. }
  9100. parseBindingAtom() {
  9101. switch (this.state.type) {
  9102. case 0:
  9103. {
  9104. const node = this.startNode();
  9105. this.next();
  9106. node.elements = this.parseBindingList(3, 93, true);
  9107. return this.finishNode(node, "ArrayPattern");
  9108. }
  9109. case 5:
  9110. return this.parseObjectLike(8, true);
  9111. }
  9112. return this.parseIdentifier();
  9113. }
  9114. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  9115. const elts = [];
  9116. let first = true;
  9117. while (!this.eat(close)) {
  9118. if (first) {
  9119. first = false;
  9120. } else {
  9121. this.expect(12);
  9122. }
  9123. if (allowEmpty && this.match(12)) {
  9124. elts.push(null);
  9125. } else if (this.eat(close)) {
  9126. break;
  9127. } else if (this.match(21)) {
  9128. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  9129. this.checkCommaAfterRest(closeCharCode);
  9130. this.expect(close);
  9131. break;
  9132. } else {
  9133. const decorators = [];
  9134. if (this.match(26) && this.hasPlugin("decorators")) {
  9135. this.raise(this.state.start, ErrorMessages.UnsupportedParameterDecorator);
  9136. }
  9137. while (this.match(26)) {
  9138. decorators.push(this.parseDecorator());
  9139. }
  9140. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  9141. }
  9142. }
  9143. return elts;
  9144. }
  9145. parseBindingRestProperty(prop) {
  9146. this.next();
  9147. prop.argument = this.parseIdentifier();
  9148. this.checkCommaAfterRest(125);
  9149. return this.finishNode(prop, "RestElement");
  9150. }
  9151. parseBindingProperty() {
  9152. const prop = this.startNode();
  9153. const {
  9154. type,
  9155. start: startPos,
  9156. startLoc
  9157. } = this.state;
  9158. if (type === 21) {
  9159. return this.parseBindingRestProperty(prop);
  9160. } else {
  9161. this.parsePropertyName(prop);
  9162. }
  9163. prop.method = false;
  9164. this.parseObjPropValue(prop, startPos, startLoc, false, false, true, false);
  9165. return prop;
  9166. }
  9167. parseAssignableListItem(allowModifiers, decorators) {
  9168. const left = this.parseMaybeDefault();
  9169. this.parseAssignableListItemTypes(left);
  9170. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  9171. if (decorators.length) {
  9172. left.decorators = decorators;
  9173. }
  9174. return elt;
  9175. }
  9176. parseAssignableListItemTypes(param) {
  9177. return param;
  9178. }
  9179. parseMaybeDefault(startPos, startLoc, left) {
  9180. var _startLoc, _startPos, _left;
  9181. startLoc = (_startLoc = startLoc) != null ? _startLoc : this.state.startLoc;
  9182. startPos = (_startPos = startPos) != null ? _startPos : this.state.start;
  9183. left = (_left = left) != null ? _left : this.parseBindingAtom();
  9184. if (!this.eat(29)) return left;
  9185. const node = this.startNodeAt(startPos, startLoc);
  9186. node.left = left;
  9187. node.right = this.parseMaybeAssignAllowIn();
  9188. return this.finishNode(node, "AssignmentPattern");
  9189. }
  9190. checkLVal(expr, contextDescription, bindingType = BIND_NONE, checkClashes, disallowLetBinding, strictModeChanged = false) {
  9191. switch (expr.type) {
  9192. case "Identifier":
  9193. {
  9194. const {
  9195. name
  9196. } = expr;
  9197. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(name, this.inModule) : isStrictBindOnlyReservedWord(name))) {
  9198. this.raise(expr.start, bindingType === BIND_NONE ? ErrorMessages.StrictEvalArguments : ErrorMessages.StrictEvalArgumentsBinding, name);
  9199. }
  9200. if (checkClashes) {
  9201. if (checkClashes.has(name)) {
  9202. this.raise(expr.start, ErrorMessages.ParamDupe);
  9203. } else {
  9204. checkClashes.add(name);
  9205. }
  9206. }
  9207. if (disallowLetBinding && name === "let") {
  9208. this.raise(expr.start, ErrorMessages.LetInLexicalBinding);
  9209. }
  9210. if (!(bindingType & BIND_NONE)) {
  9211. this.scope.declareName(name, bindingType, expr.start);
  9212. }
  9213. break;
  9214. }
  9215. case "MemberExpression":
  9216. if (bindingType !== BIND_NONE) {
  9217. this.raise(expr.start, ErrorMessages.InvalidPropertyBindingPattern);
  9218. }
  9219. break;
  9220. case "ObjectPattern":
  9221. for (let prop of expr.properties) {
  9222. if (this.isObjectProperty(prop)) prop = prop.value;else if (this.isObjectMethod(prop)) continue;
  9223. this.checkLVal(prop, "object destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  9224. }
  9225. break;
  9226. case "ArrayPattern":
  9227. for (const elem of expr.elements) {
  9228. if (elem) {
  9229. this.checkLVal(elem, "array destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  9230. }
  9231. }
  9232. break;
  9233. case "AssignmentPattern":
  9234. this.checkLVal(expr.left, "assignment pattern", bindingType, checkClashes);
  9235. break;
  9236. case "RestElement":
  9237. this.checkLVal(expr.argument, "rest element", bindingType, checkClashes);
  9238. break;
  9239. case "ParenthesizedExpression":
  9240. this.checkLVal(expr.expression, "parenthesized expression", bindingType, checkClashes);
  9241. break;
  9242. default:
  9243. {
  9244. this.raise(expr.start, bindingType === BIND_NONE ? ErrorMessages.InvalidLhs : ErrorMessages.InvalidLhsBinding, contextDescription);
  9245. }
  9246. }
  9247. }
  9248. checkToRestConversion(node) {
  9249. if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
  9250. this.raise(node.argument.start, ErrorMessages.InvalidRestAssignmentPattern);
  9251. }
  9252. }
  9253. checkCommaAfterRest(close) {
  9254. if (this.match(12)) {
  9255. if (this.lookaheadCharCode() === close) {
  9256. this.raiseTrailingCommaAfterRest(this.state.start);
  9257. } else {
  9258. this.raiseRestNotLast(this.state.start);
  9259. }
  9260. }
  9261. }
  9262. raiseRestNotLast(pos) {
  9263. throw this.raise(pos, ErrorMessages.ElementAfterRest);
  9264. }
  9265. raiseTrailingCommaAfterRest(pos) {
  9266. this.raise(pos, ErrorMessages.RestTrailingComma);
  9267. }
  9268. }
  9269. const invalidHackPipeBodies = new Map([["ArrowFunctionExpression", "arrow function"], ["AssignmentExpression", "assignment"], ["ConditionalExpression", "conditional"], ["YieldExpression", "yield"]]);
  9270. class ExpressionParser extends LValParser {
  9271. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  9272. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  9273. return;
  9274. }
  9275. const key = prop.key;
  9276. const name = key.type === "Identifier" ? key.name : key.value;
  9277. if (name === "__proto__") {
  9278. if (isRecord) {
  9279. this.raise(key.start, ErrorMessages.RecordNoProto);
  9280. return;
  9281. }
  9282. if (protoRef.used) {
  9283. if (refExpressionErrors) {
  9284. if (refExpressionErrors.doubleProto === -1) {
  9285. refExpressionErrors.doubleProto = key.start;
  9286. }
  9287. } else {
  9288. this.raise(key.start, ErrorMessages.DuplicateProto);
  9289. }
  9290. }
  9291. protoRef.used = true;
  9292. }
  9293. }
  9294. shouldExitDescending(expr, potentialArrowAt) {
  9295. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  9296. }
  9297. getExpression() {
  9298. this.enterInitialScopes();
  9299. this.nextToken();
  9300. const expr = this.parseExpression();
  9301. if (!this.match(131)) {
  9302. this.unexpected();
  9303. }
  9304. this.finalizeRemainingComments();
  9305. expr.comments = this.state.comments;
  9306. expr.errors = this.state.errors;
  9307. if (this.options.tokens) {
  9308. expr.tokens = this.tokens;
  9309. }
  9310. return expr;
  9311. }
  9312. parseExpression(disallowIn, refExpressionErrors) {
  9313. if (disallowIn) {
  9314. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  9315. }
  9316. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  9317. }
  9318. parseExpressionBase(refExpressionErrors) {
  9319. const startPos = this.state.start;
  9320. const startLoc = this.state.startLoc;
  9321. const expr = this.parseMaybeAssign(refExpressionErrors);
  9322. if (this.match(12)) {
  9323. const node = this.startNodeAt(startPos, startLoc);
  9324. node.expressions = [expr];
  9325. while (this.eat(12)) {
  9326. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  9327. }
  9328. this.toReferencedList(node.expressions);
  9329. return this.finishNode(node, "SequenceExpression");
  9330. }
  9331. return expr;
  9332. }
  9333. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  9334. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  9335. }
  9336. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  9337. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  9338. }
  9339. setOptionalParametersError(refExpressionErrors, resultError) {
  9340. var _resultError$pos;
  9341. refExpressionErrors.optionalParameters = (_resultError$pos = resultError == null ? void 0 : resultError.pos) != null ? _resultError$pos : this.state.start;
  9342. }
  9343. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9344. const startPos = this.state.start;
  9345. const startLoc = this.state.startLoc;
  9346. if (this.isContextual(101)) {
  9347. if (this.prodParam.hasYield) {
  9348. let left = this.parseYield();
  9349. if (afterLeftParse) {
  9350. left = afterLeftParse.call(this, left, startPos, startLoc);
  9351. }
  9352. return left;
  9353. }
  9354. }
  9355. let ownExpressionErrors;
  9356. if (refExpressionErrors) {
  9357. ownExpressionErrors = false;
  9358. } else {
  9359. refExpressionErrors = new ExpressionErrors();
  9360. ownExpressionErrors = true;
  9361. }
  9362. const {
  9363. type
  9364. } = this.state;
  9365. if (type === 10 || tokenIsIdentifier(type)) {
  9366. this.state.potentialArrowAt = this.state.start;
  9367. }
  9368. let left = this.parseMaybeConditional(refExpressionErrors);
  9369. if (afterLeftParse) {
  9370. left = afterLeftParse.call(this, left, startPos, startLoc);
  9371. }
  9372. if (tokenIsAssignment(this.state.type)) {
  9373. const node = this.startNodeAt(startPos, startLoc);
  9374. const operator = this.state.value;
  9375. node.operator = operator;
  9376. if (this.match(29)) {
  9377. node.left = this.toAssignable(left, true);
  9378. if (refExpressionErrors.doubleProto >= startPos) {
  9379. refExpressionErrors.doubleProto = -1;
  9380. }
  9381. if (refExpressionErrors.shorthandAssign >= startPos) {
  9382. refExpressionErrors.shorthandAssign = -1;
  9383. }
  9384. } else {
  9385. node.left = left;
  9386. }
  9387. this.checkLVal(left, "assignment expression");
  9388. this.next();
  9389. node.right = this.parseMaybeAssign();
  9390. return this.finishNode(node, "AssignmentExpression");
  9391. } else if (ownExpressionErrors) {
  9392. this.checkExpressionErrors(refExpressionErrors, true);
  9393. }
  9394. return left;
  9395. }
  9396. parseMaybeConditional(refExpressionErrors) {
  9397. const startPos = this.state.start;
  9398. const startLoc = this.state.startLoc;
  9399. const potentialArrowAt = this.state.potentialArrowAt;
  9400. const expr = this.parseExprOps(refExpressionErrors);
  9401. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  9402. return expr;
  9403. }
  9404. return this.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  9405. }
  9406. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  9407. if (this.eat(17)) {
  9408. const node = this.startNodeAt(startPos, startLoc);
  9409. node.test = expr;
  9410. node.consequent = this.parseMaybeAssignAllowIn();
  9411. this.expect(14);
  9412. node.alternate = this.parseMaybeAssign();
  9413. return this.finishNode(node, "ConditionalExpression");
  9414. }
  9415. return expr;
  9416. }
  9417. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  9418. return this.match(130) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  9419. }
  9420. parseExprOps(refExpressionErrors) {
  9421. const startPos = this.state.start;
  9422. const startLoc = this.state.startLoc;
  9423. const potentialArrowAt = this.state.potentialArrowAt;
  9424. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  9425. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  9426. return expr;
  9427. }
  9428. return this.parseExprOp(expr, startPos, startLoc, -1);
  9429. }
  9430. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  9431. if (this.isPrivateName(left)) {
  9432. const value = this.getPrivateNameSV(left);
  9433. const {
  9434. start
  9435. } = left;
  9436. if (minPrec >= tokenOperatorPrecedence(54) || !this.prodParam.hasIn || !this.match(54)) {
  9437. this.raise(start, ErrorMessages.PrivateInExpectedIn, value);
  9438. }
  9439. this.classScope.usePrivateName(value, start);
  9440. }
  9441. const op = this.state.type;
  9442. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(54))) {
  9443. let prec = tokenOperatorPrecedence(op);
  9444. if (prec > minPrec) {
  9445. if (op === 37) {
  9446. this.expectPlugin("pipelineOperator");
  9447. if (this.state.inFSharpPipelineDirectBody) {
  9448. return left;
  9449. }
  9450. this.checkPipelineAtInfixOperator(left, leftStartPos);
  9451. }
  9452. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  9453. node.left = left;
  9454. node.operator = this.state.value;
  9455. const logical = op === 39 || op === 40;
  9456. const coalesce = op === 38;
  9457. if (coalesce) {
  9458. prec = tokenOperatorPrecedence(40);
  9459. }
  9460. this.next();
  9461. if (op === 37 && this.hasPlugin(["pipelineOperator", {
  9462. proposal: "minimal"
  9463. }])) {
  9464. if (this.state.type === 92 && this.prodParam.hasAwait) {
  9465. throw this.raise(this.state.start, ErrorMessages.UnexpectedAwaitAfterPipelineBody);
  9466. }
  9467. }
  9468. node.right = this.parseExprOpRightExpr(op, prec);
  9469. this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  9470. const nextOp = this.state.type;
  9471. if (coalesce && (nextOp === 39 || nextOp === 40) || logical && nextOp === 38) {
  9472. throw this.raise(this.state.start, ErrorMessages.MixingCoalesceWithLogical);
  9473. }
  9474. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  9475. }
  9476. }
  9477. return left;
  9478. }
  9479. parseExprOpRightExpr(op, prec) {
  9480. const startPos = this.state.start;
  9481. const startLoc = this.state.startLoc;
  9482. switch (op) {
  9483. case 37:
  9484. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  9485. case "hack":
  9486. return this.withTopicBindingContext(() => {
  9487. return this.parseHackPipeBody();
  9488. });
  9489. case "smart":
  9490. return this.withTopicBindingContext(() => {
  9491. if (this.prodParam.hasYield && this.isContextual(101)) {
  9492. throw this.raise(this.state.start, ErrorMessages.PipeBodyIsTighter, this.state.value);
  9493. }
  9494. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startPos, startLoc);
  9495. });
  9496. case "fsharp":
  9497. return this.withSoloAwaitPermittingContext(() => {
  9498. return this.parseFSharpPipelineBody(prec);
  9499. });
  9500. }
  9501. default:
  9502. return this.parseExprOpBaseRightExpr(op, prec);
  9503. }
  9504. }
  9505. parseExprOpBaseRightExpr(op, prec) {
  9506. const startPos = this.state.start;
  9507. const startLoc = this.state.startLoc;
  9508. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  9509. }
  9510. parseHackPipeBody() {
  9511. var _body$extra;
  9512. const {
  9513. start
  9514. } = this.state;
  9515. const body = this.parseMaybeAssign();
  9516. if (invalidHackPipeBodies.has(body.type) && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  9517. this.raise(start, ErrorMessages.PipeUnparenthesizedBody, invalidHackPipeBodies.get(body.type));
  9518. }
  9519. if (!this.topicReferenceWasUsedInCurrentContext()) {
  9520. this.raise(start, ErrorMessages.PipeTopicUnused);
  9521. }
  9522. return body;
  9523. }
  9524. checkExponentialAfterUnary(node) {
  9525. if (this.match(53)) {
  9526. this.raise(node.argument.start, ErrorMessages.UnexpectedTokenUnaryExponentiation);
  9527. }
  9528. }
  9529. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9530. const startPos = this.state.start;
  9531. const startLoc = this.state.startLoc;
  9532. const isAwait = this.isContextual(92);
  9533. if (isAwait && this.isAwaitAllowed()) {
  9534. this.next();
  9535. const expr = this.parseAwait(startPos, startLoc);
  9536. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  9537. return expr;
  9538. }
  9539. const update = this.match(34);
  9540. const node = this.startNode();
  9541. if (tokenIsPrefix(this.state.type)) {
  9542. node.operator = this.state.value;
  9543. node.prefix = true;
  9544. if (this.match(68)) {
  9545. this.expectPlugin("throwExpressions");
  9546. }
  9547. const isDelete = this.match(85);
  9548. this.next();
  9549. node.argument = this.parseMaybeUnary(null, true);
  9550. this.checkExpressionErrors(refExpressionErrors, true);
  9551. if (this.state.strict && isDelete) {
  9552. const arg = node.argument;
  9553. if (arg.type === "Identifier") {
  9554. this.raise(node.start, ErrorMessages.StrictDelete);
  9555. } else if (this.hasPropertyAsPrivateName(arg)) {
  9556. this.raise(node.start, ErrorMessages.DeletePrivateField);
  9557. }
  9558. }
  9559. if (!update) {
  9560. if (!sawUnary) this.checkExponentialAfterUnary(node);
  9561. return this.finishNode(node, "UnaryExpression");
  9562. }
  9563. }
  9564. const expr = this.parseUpdate(node, update, refExpressionErrors);
  9565. if (isAwait) {
  9566. const {
  9567. type
  9568. } = this.state;
  9569. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(50);
  9570. if (startsExpr && !this.isAmbiguousAwait()) {
  9571. this.raiseOverwrite(startPos, ErrorMessages.AwaitNotInAsyncContext);
  9572. return this.parseAwait(startPos, startLoc);
  9573. }
  9574. }
  9575. return expr;
  9576. }
  9577. parseUpdate(node, update, refExpressionErrors) {
  9578. if (update) {
  9579. this.checkLVal(node.argument, "prefix operation");
  9580. return this.finishNode(node, "UpdateExpression");
  9581. }
  9582. const startPos = this.state.start;
  9583. const startLoc = this.state.startLoc;
  9584. let expr = this.parseExprSubscripts(refExpressionErrors);
  9585. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  9586. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  9587. const node = this.startNodeAt(startPos, startLoc);
  9588. node.operator = this.state.value;
  9589. node.prefix = false;
  9590. node.argument = expr;
  9591. this.checkLVal(expr, "postfix operation");
  9592. this.next();
  9593. expr = this.finishNode(node, "UpdateExpression");
  9594. }
  9595. return expr;
  9596. }
  9597. parseExprSubscripts(refExpressionErrors) {
  9598. const startPos = this.state.start;
  9599. const startLoc = this.state.startLoc;
  9600. const potentialArrowAt = this.state.potentialArrowAt;
  9601. const expr = this.parseExprAtom(refExpressionErrors);
  9602. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  9603. return expr;
  9604. }
  9605. return this.parseSubscripts(expr, startPos, startLoc);
  9606. }
  9607. parseSubscripts(base, startPos, startLoc, noCalls) {
  9608. const state = {
  9609. optionalChainMember: false,
  9610. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  9611. stop: false
  9612. };
  9613. do {
  9614. base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
  9615. state.maybeAsyncArrow = false;
  9616. } while (!state.stop);
  9617. return base;
  9618. }
  9619. parseSubscript(base, startPos, startLoc, noCalls, state) {
  9620. const {
  9621. type
  9622. } = this.state;
  9623. if (!noCalls && type === 15) {
  9624. return this.parseBind(base, startPos, startLoc, noCalls, state);
  9625. } else if (tokenIsTemplate(type)) {
  9626. return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  9627. }
  9628. let optional = false;
  9629. if (type === 18) {
  9630. if (noCalls && this.lookaheadCharCode() === 40) {
  9631. state.stop = true;
  9632. return base;
  9633. }
  9634. state.optionalChainMember = optional = true;
  9635. this.next();
  9636. }
  9637. if (!noCalls && this.match(10)) {
  9638. return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
  9639. } else {
  9640. const computed = this.eat(0);
  9641. if (computed || optional || this.eat(16)) {
  9642. return this.parseMember(base, startPos, startLoc, state, computed, optional);
  9643. } else {
  9644. state.stop = true;
  9645. return base;
  9646. }
  9647. }
  9648. }
  9649. parseMember(base, startPos, startLoc, state, computed, optional) {
  9650. const node = this.startNodeAt(startPos, startLoc);
  9651. node.object = base;
  9652. node.computed = computed;
  9653. if (computed) {
  9654. node.property = this.parseExpression();
  9655. this.expect(3);
  9656. } else if (this.match(130)) {
  9657. if (base.type === "Super") {
  9658. this.raise(startPos, ErrorMessages.SuperPrivateField);
  9659. }
  9660. this.classScope.usePrivateName(this.state.value, this.state.start);
  9661. node.property = this.parsePrivateName();
  9662. } else {
  9663. node.property = this.parseIdentifier(true);
  9664. }
  9665. if (state.optionalChainMember) {
  9666. node.optional = optional;
  9667. return this.finishNode(node, "OptionalMemberExpression");
  9668. } else {
  9669. return this.finishNode(node, "MemberExpression");
  9670. }
  9671. }
  9672. parseBind(base, startPos, startLoc, noCalls, state) {
  9673. const node = this.startNodeAt(startPos, startLoc);
  9674. node.object = base;
  9675. this.next();
  9676. node.callee = this.parseNoCallExpr();
  9677. state.stop = true;
  9678. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  9679. }
  9680. parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
  9681. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9682. let refExpressionErrors = null;
  9683. this.state.maybeInArrowParameters = true;
  9684. this.next();
  9685. let node = this.startNodeAt(startPos, startLoc);
  9686. node.callee = base;
  9687. if (state.maybeAsyncArrow) {
  9688. this.expressionScope.enter(newAsyncArrowScope());
  9689. refExpressionErrors = new ExpressionErrors();
  9690. }
  9691. if (state.optionalChainMember) {
  9692. node.optional = optional;
  9693. }
  9694. if (optional) {
  9695. node.arguments = this.parseCallExpressionArguments(11);
  9696. } else {
  9697. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  9698. }
  9699. this.finishCallExpression(node, state.optionalChainMember);
  9700. if (state.maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  9701. state.stop = true;
  9702. this.expressionScope.validateAsPattern();
  9703. this.expressionScope.exit();
  9704. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  9705. } else {
  9706. if (state.maybeAsyncArrow) {
  9707. this.checkExpressionErrors(refExpressionErrors, true);
  9708. this.expressionScope.exit();
  9709. }
  9710. this.toReferencedArguments(node);
  9711. }
  9712. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9713. return node;
  9714. }
  9715. toReferencedArguments(node, isParenthesizedExpr) {
  9716. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  9717. }
  9718. parseTaggedTemplateExpression(base, startPos, startLoc, state) {
  9719. const node = this.startNodeAt(startPos, startLoc);
  9720. node.tag = base;
  9721. node.quasi = this.parseTemplate(true);
  9722. if (state.optionalChainMember) {
  9723. this.raise(startPos, ErrorMessages.OptionalChainingNoTemplate);
  9724. }
  9725. return this.finishNode(node, "TaggedTemplateExpression");
  9726. }
  9727. atPossibleAsyncArrow(base) {
  9728. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEnd === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  9729. }
  9730. finishCallExpression(node, optional) {
  9731. if (node.callee.type === "Import") {
  9732. if (node.arguments.length === 2) {
  9733. {
  9734. if (!this.hasPlugin("moduleAttributes")) {
  9735. this.expectPlugin("importAssertions");
  9736. }
  9737. }
  9738. }
  9739. if (node.arguments.length === 0 || node.arguments.length > 2) {
  9740. this.raise(node.start, ErrorMessages.ImportCallArity, this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? "one or two arguments" : "one argument");
  9741. } else {
  9742. for (const arg of node.arguments) {
  9743. if (arg.type === "SpreadElement") {
  9744. this.raise(arg.start, ErrorMessages.ImportCallSpreadArgument);
  9745. }
  9746. }
  9747. }
  9748. }
  9749. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  9750. }
  9751. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  9752. const elts = [];
  9753. let first = true;
  9754. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9755. this.state.inFSharpPipelineDirectBody = false;
  9756. while (!this.eat(close)) {
  9757. if (first) {
  9758. first = false;
  9759. } else {
  9760. this.expect(12);
  9761. if (this.match(close)) {
  9762. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  9763. this.raise(this.state.lastTokStart, ErrorMessages.ImportCallArgumentTrailingComma);
  9764. }
  9765. if (nodeForExtra) {
  9766. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  9767. }
  9768. this.next();
  9769. break;
  9770. }
  9771. }
  9772. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  9773. }
  9774. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9775. return elts;
  9776. }
  9777. shouldParseAsyncArrow() {
  9778. return this.match(19) && !this.canInsertSemicolon();
  9779. }
  9780. parseAsyncArrowFromCallExpression(node, call) {
  9781. var _call$extra;
  9782. this.resetPreviousNodeTrailingComments(call);
  9783. this.expect(19);
  9784. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingComma);
  9785. if (call.innerComments) {
  9786. setInnerComments(node, call.innerComments);
  9787. }
  9788. if (call.callee.trailingComments) {
  9789. setInnerComments(node, call.callee.trailingComments);
  9790. }
  9791. return node;
  9792. }
  9793. parseNoCallExpr() {
  9794. const startPos = this.state.start;
  9795. const startLoc = this.state.startLoc;
  9796. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  9797. }
  9798. parseExprAtom(refExpressionErrors) {
  9799. let node;
  9800. const {
  9801. type
  9802. } = this.state;
  9803. switch (type) {
  9804. case 75:
  9805. return this.parseSuper();
  9806. case 79:
  9807. node = this.startNode();
  9808. this.next();
  9809. if (this.match(16)) {
  9810. return this.parseImportMetaProperty(node);
  9811. }
  9812. if (!this.match(10)) {
  9813. this.raise(this.state.lastTokStart, ErrorMessages.UnsupportedImport);
  9814. }
  9815. return this.finishNode(node, "Import");
  9816. case 74:
  9817. node = this.startNode();
  9818. this.next();
  9819. return this.finishNode(node, "ThisExpression");
  9820. case 86:
  9821. {
  9822. return this.parseDo(this.startNode(), false);
  9823. }
  9824. case 52:
  9825. case 31:
  9826. {
  9827. this.readRegexp();
  9828. return this.parseRegExpLiteral(this.state.value);
  9829. }
  9830. case 126:
  9831. return this.parseNumericLiteral(this.state.value);
  9832. case 127:
  9833. return this.parseBigIntLiteral(this.state.value);
  9834. case 128:
  9835. return this.parseDecimalLiteral(this.state.value);
  9836. case 125:
  9837. return this.parseStringLiteral(this.state.value);
  9838. case 80:
  9839. return this.parseNullLiteral();
  9840. case 81:
  9841. return this.parseBooleanLiteral(true);
  9842. case 82:
  9843. return this.parseBooleanLiteral(false);
  9844. case 10:
  9845. {
  9846. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  9847. return this.parseParenAndDistinguishExpression(canBeArrow);
  9848. }
  9849. case 2:
  9850. case 1:
  9851. {
  9852. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  9853. }
  9854. case 0:
  9855. {
  9856. return this.parseArrayLike(3, true, false, refExpressionErrors);
  9857. }
  9858. case 6:
  9859. case 7:
  9860. {
  9861. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  9862. }
  9863. case 5:
  9864. {
  9865. return this.parseObjectLike(8, false, false, refExpressionErrors);
  9866. }
  9867. case 64:
  9868. return this.parseFunctionOrFunctionSent();
  9869. case 26:
  9870. this.parseDecorators();
  9871. case 76:
  9872. node = this.startNode();
  9873. this.takeDecorators(node);
  9874. return this.parseClass(node, false);
  9875. case 73:
  9876. return this.parseNewOrNewTarget();
  9877. case 25:
  9878. case 24:
  9879. return this.parseTemplate(false);
  9880. case 15:
  9881. {
  9882. node = this.startNode();
  9883. this.next();
  9884. node.object = null;
  9885. const callee = node.callee = this.parseNoCallExpr();
  9886. if (callee.type === "MemberExpression") {
  9887. return this.finishNode(node, "BindExpression");
  9888. } else {
  9889. throw this.raise(callee.start, ErrorMessages.UnsupportedBind);
  9890. }
  9891. }
  9892. case 130:
  9893. {
  9894. this.raise(this.state.start, ErrorMessages.PrivateInExpectedIn, this.state.value);
  9895. return this.parsePrivateName();
  9896. }
  9897. case 33:
  9898. {
  9899. return this.parseTopicReferenceThenEqualsSign(50, "%");
  9900. }
  9901. case 32:
  9902. {
  9903. return this.parseTopicReferenceThenEqualsSign(42, "^");
  9904. }
  9905. case 42:
  9906. case 50:
  9907. case 27:
  9908. {
  9909. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  9910. if (pipeProposal) {
  9911. return this.parseTopicReference(pipeProposal);
  9912. } else {
  9913. throw this.unexpected();
  9914. }
  9915. }
  9916. case 45:
  9917. {
  9918. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  9919. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  9920. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  9921. break;
  9922. } else {
  9923. throw this.unexpected();
  9924. }
  9925. }
  9926. default:
  9927. if (tokenIsIdentifier(type)) {
  9928. if (this.isContextual(119) && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  9929. return this.parseModuleExpression();
  9930. }
  9931. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  9932. const containsEsc = this.state.containsEsc;
  9933. const id = this.parseIdentifier();
  9934. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  9935. const {
  9936. type
  9937. } = this.state;
  9938. if (type === 64) {
  9939. this.resetPreviousNodeTrailingComments(id);
  9940. this.next();
  9941. return this.parseFunction(this.startNodeAtNode(id), undefined, true);
  9942. } else if (tokenIsIdentifier(type)) {
  9943. if (this.lookaheadCharCode() === 61) {
  9944. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  9945. } else {
  9946. return id;
  9947. }
  9948. } else if (type === 86) {
  9949. this.resetPreviousNodeTrailingComments(id);
  9950. return this.parseDo(this.startNodeAtNode(id), true);
  9951. }
  9952. }
  9953. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  9954. this.next();
  9955. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  9956. }
  9957. return id;
  9958. } else {
  9959. throw this.unexpected();
  9960. }
  9961. }
  9962. }
  9963. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  9964. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  9965. if (pipeProposal) {
  9966. this.state.type = topicTokenType;
  9967. this.state.value = topicTokenValue;
  9968. this.state.pos--;
  9969. this.state.end--;
  9970. this.state.endLoc.column--;
  9971. return this.parseTopicReference(pipeProposal);
  9972. } else {
  9973. throw this.unexpected();
  9974. }
  9975. }
  9976. parseTopicReference(pipeProposal) {
  9977. const node = this.startNode();
  9978. const start = this.state.start;
  9979. const tokenType = this.state.type;
  9980. this.next();
  9981. return this.finishTopicReference(node, start, pipeProposal, tokenType);
  9982. }
  9983. finishTopicReference(node, start, pipeProposal, tokenType) {
  9984. if (this.testTopicReferenceConfiguration(pipeProposal, start, tokenType)) {
  9985. let nodeType;
  9986. if (pipeProposal === "smart") {
  9987. nodeType = "PipelinePrimaryTopicReference";
  9988. } else {
  9989. nodeType = "TopicReference";
  9990. }
  9991. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  9992. if (pipeProposal === "smart") {
  9993. this.raise(start, ErrorMessages.PrimaryTopicNotAllowed);
  9994. } else {
  9995. this.raise(start, ErrorMessages.PipeTopicUnbound);
  9996. }
  9997. }
  9998. this.registerTopicReference();
  9999. return this.finishNode(node, nodeType);
  10000. } else {
  10001. throw this.raise(start, ErrorMessages.PipeTopicUnconfiguredToken, tokenLabelName(tokenType));
  10002. }
  10003. }
  10004. testTopicReferenceConfiguration(pipeProposal, start, tokenType) {
  10005. switch (pipeProposal) {
  10006. case "hack":
  10007. {
  10008. return this.hasPlugin(["pipelineOperator", {
  10009. topicToken: tokenLabelName(tokenType)
  10010. }]);
  10011. }
  10012. case "smart":
  10013. return tokenType === 27;
  10014. default:
  10015. throw this.raise(start, ErrorMessages.PipeTopicRequiresHackPipes);
  10016. }
  10017. }
  10018. parseAsyncArrowUnaryFunction(node) {
  10019. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  10020. const params = [this.parseIdentifier()];
  10021. this.prodParam.exit();
  10022. if (this.hasPrecedingLineBreak()) {
  10023. this.raise(this.state.pos, ErrorMessages.LineTerminatorBeforeArrow);
  10024. }
  10025. this.expect(19);
  10026. this.parseArrowExpression(node, params, true);
  10027. return node;
  10028. }
  10029. parseDo(node, isAsync) {
  10030. this.expectPlugin("doExpressions");
  10031. if (isAsync) {
  10032. this.expectPlugin("asyncDoExpressions");
  10033. }
  10034. node.async = isAsync;
  10035. this.next();
  10036. const oldLabels = this.state.labels;
  10037. this.state.labels = [];
  10038. if (isAsync) {
  10039. this.prodParam.enter(PARAM_AWAIT);
  10040. node.body = this.parseBlock();
  10041. this.prodParam.exit();
  10042. } else {
  10043. node.body = this.parseBlock();
  10044. }
  10045. this.state.labels = oldLabels;
  10046. return this.finishNode(node, "DoExpression");
  10047. }
  10048. parseSuper() {
  10049. const node = this.startNode();
  10050. this.next();
  10051. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  10052. this.raise(node.start, ErrorMessages.SuperNotAllowed);
  10053. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  10054. this.raise(node.start, ErrorMessages.UnexpectedSuper);
  10055. }
  10056. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  10057. this.raise(node.start, ErrorMessages.UnsupportedSuper);
  10058. }
  10059. return this.finishNode(node, "Super");
  10060. }
  10061. parsePrivateName() {
  10062. const node = this.startNode();
  10063. const id = this.startNodeAt(this.state.start + 1, new Position(this.state.curLine, this.state.start + 1 - this.state.lineStart));
  10064. const name = this.state.value;
  10065. this.next();
  10066. node.id = this.createIdentifier(id, name);
  10067. return this.finishNode(node, "PrivateName");
  10068. }
  10069. parseFunctionOrFunctionSent() {
  10070. const node = this.startNode();
  10071. this.next();
  10072. if (this.prodParam.hasYield && this.match(16)) {
  10073. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  10074. this.next();
  10075. if (this.match(98)) {
  10076. this.expectPlugin("functionSent");
  10077. } else if (!this.hasPlugin("functionSent")) {
  10078. this.unexpected();
  10079. }
  10080. return this.parseMetaProperty(node, meta, "sent");
  10081. }
  10082. return this.parseFunction(node);
  10083. }
  10084. parseMetaProperty(node, meta, propertyName) {
  10085. node.meta = meta;
  10086. const containsEsc = this.state.containsEsc;
  10087. node.property = this.parseIdentifier(true);
  10088. if (node.property.name !== propertyName || containsEsc) {
  10089. this.raise(node.property.start, ErrorMessages.UnsupportedMetaProperty, meta.name, propertyName);
  10090. }
  10091. return this.finishNode(node, "MetaProperty");
  10092. }
  10093. parseImportMetaProperty(node) {
  10094. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  10095. this.next();
  10096. if (this.isContextual(96)) {
  10097. if (!this.inModule) {
  10098. this.raise(id.start, SourceTypeModuleErrorMessages.ImportMetaOutsideModule);
  10099. }
  10100. this.sawUnambiguousESM = true;
  10101. }
  10102. return this.parseMetaProperty(node, id, "meta");
  10103. }
  10104. parseLiteralAtNode(value, type, node) {
  10105. this.addExtra(node, "rawValue", value);
  10106. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  10107. node.value = value;
  10108. this.next();
  10109. return this.finishNode(node, type);
  10110. }
  10111. parseLiteral(value, type) {
  10112. const node = this.startNode();
  10113. return this.parseLiteralAtNode(value, type, node);
  10114. }
  10115. parseStringLiteral(value) {
  10116. return this.parseLiteral(value, "StringLiteral");
  10117. }
  10118. parseNumericLiteral(value) {
  10119. return this.parseLiteral(value, "NumericLiteral");
  10120. }
  10121. parseBigIntLiteral(value) {
  10122. return this.parseLiteral(value, "BigIntLiteral");
  10123. }
  10124. parseDecimalLiteral(value) {
  10125. return this.parseLiteral(value, "DecimalLiteral");
  10126. }
  10127. parseRegExpLiteral(value) {
  10128. const node = this.parseLiteral(value.value, "RegExpLiteral");
  10129. node.pattern = value.pattern;
  10130. node.flags = value.flags;
  10131. return node;
  10132. }
  10133. parseBooleanLiteral(value) {
  10134. const node = this.startNode();
  10135. node.value = value;
  10136. this.next();
  10137. return this.finishNode(node, "BooleanLiteral");
  10138. }
  10139. parseNullLiteral() {
  10140. const node = this.startNode();
  10141. this.next();
  10142. return this.finishNode(node, "NullLiteral");
  10143. }
  10144. parseParenAndDistinguishExpression(canBeArrow) {
  10145. const startPos = this.state.start;
  10146. const startLoc = this.state.startLoc;
  10147. let val;
  10148. this.next();
  10149. this.expressionScope.enter(newArrowHeadScope());
  10150. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10151. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10152. this.state.maybeInArrowParameters = true;
  10153. this.state.inFSharpPipelineDirectBody = false;
  10154. const innerStartPos = this.state.start;
  10155. const innerStartLoc = this.state.startLoc;
  10156. const exprList = [];
  10157. const refExpressionErrors = new ExpressionErrors();
  10158. let first = true;
  10159. let spreadStart;
  10160. let optionalCommaStart;
  10161. while (!this.match(11)) {
  10162. if (first) {
  10163. first = false;
  10164. } else {
  10165. this.expect(12, refExpressionErrors.optionalParameters === -1 ? null : refExpressionErrors.optionalParameters);
  10166. if (this.match(11)) {
  10167. optionalCommaStart = this.state.start;
  10168. break;
  10169. }
  10170. }
  10171. if (this.match(21)) {
  10172. const spreadNodeStartPos = this.state.start;
  10173. const spreadNodeStartLoc = this.state.startLoc;
  10174. spreadStart = this.state.start;
  10175. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  10176. this.checkCommaAfterRest(41);
  10177. break;
  10178. } else {
  10179. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  10180. }
  10181. }
  10182. const innerEndPos = this.state.lastTokEnd;
  10183. const innerEndLoc = this.state.lastTokEndLoc;
  10184. this.expect(11);
  10185. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10186. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10187. let arrowNode = this.startNodeAt(startPos, startLoc);
  10188. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  10189. this.expressionScope.validateAsPattern();
  10190. this.expressionScope.exit();
  10191. this.parseArrowExpression(arrowNode, exprList, false);
  10192. return arrowNode;
  10193. }
  10194. this.expressionScope.exit();
  10195. if (!exprList.length) {
  10196. this.unexpected(this.state.lastTokStart);
  10197. }
  10198. if (optionalCommaStart) this.unexpected(optionalCommaStart);
  10199. if (spreadStart) this.unexpected(spreadStart);
  10200. this.checkExpressionErrors(refExpressionErrors, true);
  10201. this.toReferencedListDeep(exprList, true);
  10202. if (exprList.length > 1) {
  10203. val = this.startNodeAt(innerStartPos, innerStartLoc);
  10204. val.expressions = exprList;
  10205. this.finishNode(val, "SequenceExpression");
  10206. this.resetEndLocation(val, innerEndPos, innerEndLoc);
  10207. } else {
  10208. val = exprList[0];
  10209. }
  10210. if (!this.options.createParenthesizedExpressions) {
  10211. this.addExtra(val, "parenthesized", true);
  10212. this.addExtra(val, "parenStart", startPos);
  10213. this.takeSurroundingComments(val, startPos, this.state.lastTokEnd);
  10214. return val;
  10215. }
  10216. const parenExpression = this.startNodeAt(startPos, startLoc);
  10217. parenExpression.expression = val;
  10218. this.finishNode(parenExpression, "ParenthesizedExpression");
  10219. return parenExpression;
  10220. }
  10221. shouldParseArrow(params) {
  10222. return !this.canInsertSemicolon();
  10223. }
  10224. parseArrow(node) {
  10225. if (this.eat(19)) {
  10226. return node;
  10227. }
  10228. }
  10229. parseParenItem(node, startPos, startLoc) {
  10230. return node;
  10231. }
  10232. parseNewOrNewTarget() {
  10233. const node = this.startNode();
  10234. this.next();
  10235. if (this.match(16)) {
  10236. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  10237. this.next();
  10238. const metaProp = this.parseMetaProperty(node, meta, "target");
  10239. if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
  10240. this.raise(metaProp.start, ErrorMessages.UnexpectedNewTarget);
  10241. }
  10242. return metaProp;
  10243. }
  10244. return this.parseNew(node);
  10245. }
  10246. parseNew(node) {
  10247. node.callee = this.parseNoCallExpr();
  10248. if (node.callee.type === "Import") {
  10249. this.raise(node.callee.start, ErrorMessages.ImportCallNotNewExpression);
  10250. } else if (this.isOptionalChain(node.callee)) {
  10251. this.raise(this.state.lastTokEnd, ErrorMessages.OptionalChainingNoNew);
  10252. } else if (this.eat(18)) {
  10253. this.raise(this.state.start, ErrorMessages.OptionalChainingNoNew);
  10254. }
  10255. this.parseNewArguments(node);
  10256. return this.finishNode(node, "NewExpression");
  10257. }
  10258. parseNewArguments(node) {
  10259. if (this.eat(10)) {
  10260. const args = this.parseExprList(11);
  10261. this.toReferencedList(args);
  10262. node.arguments = args;
  10263. } else {
  10264. node.arguments = [];
  10265. }
  10266. }
  10267. parseTemplateElement(isTagged) {
  10268. const {
  10269. start,
  10270. end,
  10271. value
  10272. } = this.state;
  10273. const elemStart = start + 1;
  10274. const elem = this.startNodeAt(elemStart, createPositionWithColumnOffset(this.state.startLoc, 1));
  10275. if (value === null) {
  10276. if (!isTagged) {
  10277. this.raise(start + 2, ErrorMessages.InvalidEscapeSequenceTemplate);
  10278. }
  10279. }
  10280. const isTail = this.match(24);
  10281. const endOffset = isTail ? -1 : -2;
  10282. const elemEnd = end + endOffset;
  10283. elem.value = {
  10284. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  10285. cooked: value === null ? null : value.slice(1, endOffset)
  10286. };
  10287. elem.tail = isTail;
  10288. this.next();
  10289. this.finishNode(elem, "TemplateElement");
  10290. this.resetEndLocation(elem, elemEnd, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  10291. return elem;
  10292. }
  10293. parseTemplate(isTagged) {
  10294. const node = this.startNode();
  10295. node.expressions = [];
  10296. let curElt = this.parseTemplateElement(isTagged);
  10297. node.quasis = [curElt];
  10298. while (!curElt.tail) {
  10299. node.expressions.push(this.parseTemplateSubstitution());
  10300. this.readTemplateContinuation();
  10301. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  10302. }
  10303. return this.finishNode(node, "TemplateLiteral");
  10304. }
  10305. parseTemplateSubstitution() {
  10306. return this.parseExpression();
  10307. }
  10308. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  10309. if (isRecord) {
  10310. this.expectPlugin("recordAndTuple");
  10311. }
  10312. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10313. this.state.inFSharpPipelineDirectBody = false;
  10314. const propHash = Object.create(null);
  10315. let first = true;
  10316. const node = this.startNode();
  10317. node.properties = [];
  10318. this.next();
  10319. while (!this.match(close)) {
  10320. if (first) {
  10321. first = false;
  10322. } else {
  10323. this.expect(12);
  10324. if (this.match(close)) {
  10325. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  10326. break;
  10327. }
  10328. }
  10329. let prop;
  10330. if (isPattern) {
  10331. prop = this.parseBindingProperty();
  10332. } else {
  10333. prop = this.parsePropertyDefinition(refExpressionErrors);
  10334. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  10335. }
  10336. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  10337. this.raise(prop.start, ErrorMessages.InvalidRecordProperty);
  10338. }
  10339. if (prop.shorthand) {
  10340. this.addExtra(prop, "shorthand", true);
  10341. }
  10342. node.properties.push(prop);
  10343. }
  10344. this.next();
  10345. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10346. let type = "ObjectExpression";
  10347. if (isPattern) {
  10348. type = "ObjectPattern";
  10349. } else if (isRecord) {
  10350. type = "RecordExpression";
  10351. }
  10352. return this.finishNode(node, type);
  10353. }
  10354. maybeAsyncOrAccessorProp(prop) {
  10355. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(51));
  10356. }
  10357. parsePropertyDefinition(refExpressionErrors) {
  10358. let decorators = [];
  10359. if (this.match(26)) {
  10360. if (this.hasPlugin("decorators")) {
  10361. this.raise(this.state.start, ErrorMessages.UnsupportedPropertyDecorator);
  10362. }
  10363. while (this.match(26)) {
  10364. decorators.push(this.parseDecorator());
  10365. }
  10366. }
  10367. const prop = this.startNode();
  10368. let isAsync = false;
  10369. let isAccessor = false;
  10370. let startPos;
  10371. let startLoc;
  10372. if (this.match(21)) {
  10373. if (decorators.length) this.unexpected();
  10374. return this.parseSpread();
  10375. }
  10376. if (decorators.length) {
  10377. prop.decorators = decorators;
  10378. decorators = [];
  10379. }
  10380. prop.method = false;
  10381. if (refExpressionErrors) {
  10382. startPos = this.state.start;
  10383. startLoc = this.state.startLoc;
  10384. }
  10385. let isGenerator = this.eat(51);
  10386. this.parsePropertyNamePrefixOperator(prop);
  10387. const containsEsc = this.state.containsEsc;
  10388. const key = this.parsePropertyName(prop);
  10389. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  10390. const keyName = key.name;
  10391. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  10392. isAsync = true;
  10393. this.resetPreviousNodeTrailingComments(key);
  10394. isGenerator = this.eat(51);
  10395. this.parsePropertyName(prop);
  10396. }
  10397. if (keyName === "get" || keyName === "set") {
  10398. isAccessor = true;
  10399. this.resetPreviousNodeTrailingComments(key);
  10400. prop.kind = keyName;
  10401. if (this.match(51)) {
  10402. isGenerator = true;
  10403. this.raise(this.state.pos, ErrorMessages.AccessorIsGenerator, keyName);
  10404. this.next();
  10405. }
  10406. this.parsePropertyName(prop);
  10407. }
  10408. }
  10409. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  10410. return prop;
  10411. }
  10412. getGetterSetterExpectedParamCount(method) {
  10413. return method.kind === "get" ? 0 : 1;
  10414. }
  10415. getObjectOrClassMethodParams(method) {
  10416. return method.params;
  10417. }
  10418. checkGetterSetterParams(method) {
  10419. var _params;
  10420. const paramCount = this.getGetterSetterExpectedParamCount(method);
  10421. const params = this.getObjectOrClassMethodParams(method);
  10422. const start = method.start;
  10423. if (params.length !== paramCount) {
  10424. if (method.kind === "get") {
  10425. this.raise(start, ErrorMessages.BadGetterArity);
  10426. } else {
  10427. this.raise(start, ErrorMessages.BadSetterArity);
  10428. }
  10429. }
  10430. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  10431. this.raise(start, ErrorMessages.BadSetterRestParameter);
  10432. }
  10433. }
  10434. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  10435. if (isAccessor) {
  10436. this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  10437. this.checkGetterSetterParams(prop);
  10438. return prop;
  10439. }
  10440. if (isAsync || isGenerator || this.match(10)) {
  10441. if (isPattern) this.unexpected();
  10442. prop.kind = "method";
  10443. prop.method = true;
  10444. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  10445. }
  10446. }
  10447. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  10448. prop.shorthand = false;
  10449. if (this.eat(14)) {
  10450. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  10451. return this.finishNode(prop, "ObjectProperty");
  10452. }
  10453. if (!prop.computed && prop.key.type === "Identifier") {
  10454. this.checkReservedWord(prop.key.name, prop.key.start, true, false);
  10455. if (isPattern) {
  10456. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  10457. } else if (this.match(29)) {
  10458. const shorthandAssign = this.state.start;
  10459. if (refExpressionErrors != null) {
  10460. if (refExpressionErrors.shorthandAssign === -1) {
  10461. refExpressionErrors.shorthandAssign = shorthandAssign;
  10462. }
  10463. } else {
  10464. this.raise(shorthandAssign, ErrorMessages.InvalidCoverInitializedName);
  10465. }
  10466. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  10467. } else {
  10468. prop.value = cloneIdentifier(prop.key);
  10469. }
  10470. prop.shorthand = true;
  10471. return this.finishNode(prop, "ObjectProperty");
  10472. }
  10473. }
  10474. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  10475. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  10476. if (!node) this.unexpected();
  10477. return node;
  10478. }
  10479. parsePropertyName(prop) {
  10480. if (this.eat(0)) {
  10481. prop.computed = true;
  10482. prop.key = this.parseMaybeAssignAllowIn();
  10483. this.expect(3);
  10484. } else {
  10485. const {
  10486. type,
  10487. value
  10488. } = this.state;
  10489. let key;
  10490. if (tokenIsKeywordOrIdentifier(type)) {
  10491. key = this.parseIdentifier(true);
  10492. } else {
  10493. switch (type) {
  10494. case 126:
  10495. key = this.parseNumericLiteral(value);
  10496. break;
  10497. case 125:
  10498. key = this.parseStringLiteral(value);
  10499. break;
  10500. case 127:
  10501. key = this.parseBigIntLiteral(value);
  10502. break;
  10503. case 128:
  10504. key = this.parseDecimalLiteral(value);
  10505. break;
  10506. case 130:
  10507. {
  10508. const privateKeyPos = this.state.start + 1;
  10509. this.raise(privateKeyPos, ErrorMessages.UnexpectedPrivateField);
  10510. key = this.parsePrivateName();
  10511. break;
  10512. }
  10513. default:
  10514. throw this.unexpected();
  10515. }
  10516. }
  10517. prop.key = key;
  10518. if (type !== 130) {
  10519. prop.computed = false;
  10520. }
  10521. }
  10522. return prop.key;
  10523. }
  10524. initFunction(node, isAsync) {
  10525. node.id = null;
  10526. node.generator = false;
  10527. node.async = !!isAsync;
  10528. }
  10529. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  10530. this.initFunction(node, isAsync);
  10531. node.generator = !!isGenerator;
  10532. const allowModifiers = isConstructor;
  10533. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  10534. this.prodParam.enter(functionFlags(isAsync, node.generator));
  10535. this.parseFunctionParams(node, allowModifiers);
  10536. this.parseFunctionBodyAndFinish(node, type, true);
  10537. this.prodParam.exit();
  10538. this.scope.exit();
  10539. return node;
  10540. }
  10541. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  10542. if (isTuple) {
  10543. this.expectPlugin("recordAndTuple");
  10544. }
  10545. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10546. this.state.inFSharpPipelineDirectBody = false;
  10547. const node = this.startNode();
  10548. this.next();
  10549. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  10550. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10551. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  10552. }
  10553. parseArrowExpression(node, params, isAsync, trailingCommaPos) {
  10554. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  10555. let flags = functionFlags(isAsync, false);
  10556. if (!this.match(0) && this.prodParam.hasIn) {
  10557. flags |= PARAM_IN;
  10558. }
  10559. this.prodParam.enter(flags);
  10560. this.initFunction(node, isAsync);
  10561. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10562. if (params) {
  10563. this.state.maybeInArrowParameters = true;
  10564. this.setArrowFunctionParameters(node, params, trailingCommaPos);
  10565. }
  10566. this.state.maybeInArrowParameters = false;
  10567. this.parseFunctionBody(node, true);
  10568. this.prodParam.exit();
  10569. this.scope.exit();
  10570. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10571. return this.finishNode(node, "ArrowFunctionExpression");
  10572. }
  10573. setArrowFunctionParameters(node, params, trailingCommaPos) {
  10574. node.params = this.toAssignableList(params, trailingCommaPos, false);
  10575. }
  10576. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  10577. this.parseFunctionBody(node, false, isMethod);
  10578. this.finishNode(node, type);
  10579. }
  10580. parseFunctionBody(node, allowExpression, isMethod = false) {
  10581. const isExpression = allowExpression && !this.match(5);
  10582. this.expressionScope.enter(newExpressionScope());
  10583. if (isExpression) {
  10584. node.body = this.parseMaybeAssign();
  10585. this.checkParams(node, false, allowExpression, false);
  10586. } else {
  10587. const oldStrict = this.state.strict;
  10588. const oldLabels = this.state.labels;
  10589. this.state.labels = [];
  10590. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  10591. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  10592. const nonSimple = !this.isSimpleParamList(node.params);
  10593. if (hasStrictModeDirective && nonSimple) {
  10594. const errorPos = (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.end : node.start;
  10595. this.raise(errorPos, ErrorMessages.IllegalLanguageModeDirective);
  10596. }
  10597. const strictModeChanged = !oldStrict && this.state.strict;
  10598. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  10599. if (this.state.strict && node.id) {
  10600. this.checkLVal(node.id, "function name", BIND_OUTSIDE, undefined, undefined, strictModeChanged);
  10601. }
  10602. });
  10603. this.prodParam.exit();
  10604. this.state.labels = oldLabels;
  10605. }
  10606. this.expressionScope.exit();
  10607. }
  10608. isSimpleParamList(params) {
  10609. for (let i = 0, len = params.length; i < len; i++) {
  10610. if (params[i].type !== "Identifier") return false;
  10611. }
  10612. return true;
  10613. }
  10614. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  10615. const checkClashes = new Set();
  10616. for (const param of node.params) {
  10617. this.checkLVal(param, "function parameter list", BIND_VAR, allowDuplicates ? null : checkClashes, undefined, strictModeChanged);
  10618. }
  10619. }
  10620. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  10621. const elts = [];
  10622. let first = true;
  10623. while (!this.eat(close)) {
  10624. if (first) {
  10625. first = false;
  10626. } else {
  10627. this.expect(12);
  10628. if (this.match(close)) {
  10629. if (nodeForExtra) {
  10630. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  10631. }
  10632. this.next();
  10633. break;
  10634. }
  10635. }
  10636. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  10637. }
  10638. return elts;
  10639. }
  10640. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  10641. let elt;
  10642. if (this.match(12)) {
  10643. if (!allowEmpty) {
  10644. this.raise(this.state.pos, ErrorMessages.UnexpectedToken, ",");
  10645. }
  10646. elt = null;
  10647. } else if (this.match(21)) {
  10648. const spreadNodeStartPos = this.state.start;
  10649. const spreadNodeStartLoc = this.state.startLoc;
  10650. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartPos, spreadNodeStartLoc);
  10651. } else if (this.match(17)) {
  10652. this.expectPlugin("partialApplication");
  10653. if (!allowPlaceholder) {
  10654. this.raise(this.state.start, ErrorMessages.UnexpectedArgumentPlaceholder);
  10655. }
  10656. const node = this.startNode();
  10657. this.next();
  10658. elt = this.finishNode(node, "ArgumentPlaceholder");
  10659. } else {
  10660. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  10661. }
  10662. return elt;
  10663. }
  10664. parseIdentifier(liberal) {
  10665. const node = this.startNode();
  10666. const name = this.parseIdentifierName(node.start, liberal);
  10667. return this.createIdentifier(node, name);
  10668. }
  10669. createIdentifier(node, name) {
  10670. node.name = name;
  10671. node.loc.identifierName = name;
  10672. return this.finishNode(node, "Identifier");
  10673. }
  10674. parseIdentifierName(pos, liberal) {
  10675. let name;
  10676. const {
  10677. start,
  10678. type
  10679. } = this.state;
  10680. if (tokenIsKeywordOrIdentifier(type)) {
  10681. name = this.state.value;
  10682. } else {
  10683. throw this.unexpected();
  10684. }
  10685. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  10686. if (liberal) {
  10687. if (tokenIsKeyword) {
  10688. this.replaceToken(124);
  10689. }
  10690. } else {
  10691. this.checkReservedWord(name, start, tokenIsKeyword, false);
  10692. }
  10693. this.next();
  10694. return name;
  10695. }
  10696. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  10697. if (word.length > 10) {
  10698. return;
  10699. }
  10700. if (!canBeReservedWord(word)) {
  10701. return;
  10702. }
  10703. if (word === "yield") {
  10704. if (this.prodParam.hasYield) {
  10705. this.raise(startLoc, ErrorMessages.YieldBindingIdentifier);
  10706. return;
  10707. }
  10708. } else if (word === "await") {
  10709. if (this.prodParam.hasAwait) {
  10710. this.raise(startLoc, ErrorMessages.AwaitBindingIdentifier);
  10711. return;
  10712. } else if (this.scope.inStaticBlock) {
  10713. this.raise(startLoc, ErrorMessages.AwaitBindingIdentifierInStaticBlock);
  10714. return;
  10715. } else {
  10716. this.expressionScope.recordAsyncArrowParametersError(startLoc, ErrorMessages.AwaitBindingIdentifier);
  10717. }
  10718. } else if (word === "arguments") {
  10719. if (this.scope.inClassAndNotInNonArrowFunction) {
  10720. this.raise(startLoc, ErrorMessages.ArgumentsInClass);
  10721. return;
  10722. }
  10723. }
  10724. if (checkKeywords && isKeyword(word)) {
  10725. this.raise(startLoc, ErrorMessages.UnexpectedKeyword, word);
  10726. return;
  10727. }
  10728. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  10729. if (reservedTest(word, this.inModule)) {
  10730. this.raise(startLoc, ErrorMessages.UnexpectedReservedWord, word);
  10731. }
  10732. }
  10733. isAwaitAllowed() {
  10734. if (this.prodParam.hasAwait) return true;
  10735. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  10736. return true;
  10737. }
  10738. return false;
  10739. }
  10740. parseAwait(startPos, startLoc) {
  10741. const node = this.startNodeAt(startPos, startLoc);
  10742. this.expressionScope.recordParameterInitializerError(node.start, ErrorMessages.AwaitExpressionFormalParameter);
  10743. if (this.eat(51)) {
  10744. this.raise(node.start, ErrorMessages.ObsoleteAwaitStar);
  10745. }
  10746. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  10747. if (this.isAmbiguousAwait()) {
  10748. this.ambiguousScriptDifferentAst = true;
  10749. } else {
  10750. this.sawUnambiguousESM = true;
  10751. }
  10752. }
  10753. if (!this.state.soloAwait) {
  10754. node.argument = this.parseMaybeUnary(null, true);
  10755. }
  10756. return this.finishNode(node, "AwaitExpression");
  10757. }
  10758. isAmbiguousAwait() {
  10759. if (this.hasPrecedingLineBreak()) return true;
  10760. const {
  10761. type
  10762. } = this.state;
  10763. return type === 49 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 129 || type === 52 || this.hasPlugin("v8intrinsic") && type === 50;
  10764. }
  10765. parseYield() {
  10766. const node = this.startNode();
  10767. this.expressionScope.recordParameterInitializerError(node.start, ErrorMessages.YieldInParameter);
  10768. this.next();
  10769. let delegating = false;
  10770. let argument = null;
  10771. if (!this.hasPrecedingLineBreak()) {
  10772. delegating = this.eat(51);
  10773. switch (this.state.type) {
  10774. case 13:
  10775. case 131:
  10776. case 8:
  10777. case 11:
  10778. case 3:
  10779. case 9:
  10780. case 14:
  10781. case 12:
  10782. if (!delegating) break;
  10783. default:
  10784. argument = this.parseMaybeAssign();
  10785. }
  10786. }
  10787. node.delegate = delegating;
  10788. node.argument = argument;
  10789. return this.finishNode(node, "YieldExpression");
  10790. }
  10791. checkPipelineAtInfixOperator(left, leftStartPos) {
  10792. if (this.hasPlugin(["pipelineOperator", {
  10793. proposal: "smart"
  10794. }])) {
  10795. if (left.type === "SequenceExpression") {
  10796. this.raise(leftStartPos, ErrorMessages.PipelineHeadSequenceExpression);
  10797. }
  10798. }
  10799. }
  10800. checkHackPipeBodyEarlyErrors(startPos) {
  10801. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10802. this.raise(startPos, ErrorMessages.PipeTopicUnused);
  10803. }
  10804. }
  10805. parseSmartPipelineBodyInStyle(childExpr, startPos, startLoc) {
  10806. const bodyNode = this.startNodeAt(startPos, startLoc);
  10807. if (this.isSimpleReference(childExpr)) {
  10808. bodyNode.callee = childExpr;
  10809. return this.finishNode(bodyNode, "PipelineBareFunction");
  10810. } else {
  10811. this.checkSmartPipeTopicBodyEarlyErrors(startPos);
  10812. bodyNode.expression = childExpr;
  10813. return this.finishNode(bodyNode, "PipelineTopicExpression");
  10814. }
  10815. }
  10816. isSimpleReference(expression) {
  10817. switch (expression.type) {
  10818. case "MemberExpression":
  10819. return !expression.computed && this.isSimpleReference(expression.object);
  10820. case "Identifier":
  10821. return true;
  10822. default:
  10823. return false;
  10824. }
  10825. }
  10826. checkSmartPipeTopicBodyEarlyErrors(startPos) {
  10827. if (this.match(19)) {
  10828. throw this.raise(this.state.start, ErrorMessages.PipelineBodyNoArrow);
  10829. } else if (!this.topicReferenceWasUsedInCurrentContext()) {
  10830. this.raise(startPos, ErrorMessages.PipelineTopicUnused);
  10831. }
  10832. }
  10833. withTopicBindingContext(callback) {
  10834. const outerContextTopicState = this.state.topicContext;
  10835. this.state.topicContext = {
  10836. maxNumOfResolvableTopics: 1,
  10837. maxTopicIndex: null
  10838. };
  10839. try {
  10840. return callback();
  10841. } finally {
  10842. this.state.topicContext = outerContextTopicState;
  10843. }
  10844. }
  10845. withSmartMixTopicForbiddingContext(callback) {
  10846. if (this.hasPlugin(["pipelineOperator", {
  10847. proposal: "smart"
  10848. }])) {
  10849. const outerContextTopicState = this.state.topicContext;
  10850. this.state.topicContext = {
  10851. maxNumOfResolvableTopics: 0,
  10852. maxTopicIndex: null
  10853. };
  10854. try {
  10855. return callback();
  10856. } finally {
  10857. this.state.topicContext = outerContextTopicState;
  10858. }
  10859. } else {
  10860. return callback();
  10861. }
  10862. }
  10863. withSoloAwaitPermittingContext(callback) {
  10864. const outerContextSoloAwaitState = this.state.soloAwait;
  10865. this.state.soloAwait = true;
  10866. try {
  10867. return callback();
  10868. } finally {
  10869. this.state.soloAwait = outerContextSoloAwaitState;
  10870. }
  10871. }
  10872. allowInAnd(callback) {
  10873. const flags = this.prodParam.currentFlags();
  10874. const prodParamToSet = PARAM_IN & ~flags;
  10875. if (prodParamToSet) {
  10876. this.prodParam.enter(flags | PARAM_IN);
  10877. try {
  10878. return callback();
  10879. } finally {
  10880. this.prodParam.exit();
  10881. }
  10882. }
  10883. return callback();
  10884. }
  10885. disallowInAnd(callback) {
  10886. const flags = this.prodParam.currentFlags();
  10887. const prodParamToClear = PARAM_IN & flags;
  10888. if (prodParamToClear) {
  10889. this.prodParam.enter(flags & ~PARAM_IN);
  10890. try {
  10891. return callback();
  10892. } finally {
  10893. this.prodParam.exit();
  10894. }
  10895. }
  10896. return callback();
  10897. }
  10898. registerTopicReference() {
  10899. this.state.topicContext.maxTopicIndex = 0;
  10900. }
  10901. topicReferenceIsAllowedInCurrentContext() {
  10902. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  10903. }
  10904. topicReferenceWasUsedInCurrentContext() {
  10905. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  10906. }
  10907. parseFSharpPipelineBody(prec) {
  10908. const startPos = this.state.start;
  10909. const startLoc = this.state.startLoc;
  10910. this.state.potentialArrowAt = this.state.start;
  10911. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10912. this.state.inFSharpPipelineDirectBody = true;
  10913. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, prec);
  10914. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10915. return ret;
  10916. }
  10917. parseModuleExpression() {
  10918. this.expectPlugin("moduleBlocks");
  10919. const node = this.startNode();
  10920. this.next();
  10921. this.eat(5);
  10922. const revertScopes = this.initializeScopes(true);
  10923. this.enterInitialScopes();
  10924. const program = this.startNode();
  10925. try {
  10926. node.body = this.parseProgram(program, 8, "module");
  10927. } finally {
  10928. revertScopes();
  10929. }
  10930. this.eat(8);
  10931. return this.finishNode(node, "ModuleExpression");
  10932. }
  10933. parsePropertyNamePrefixOperator(prop) {}
  10934. }
  10935. const loopLabel = {
  10936. kind: "loop"
  10937. },
  10938. switchLabel = {
  10939. kind: "switch"
  10940. };
  10941. const FUNC_NO_FLAGS = 0b000,
  10942. FUNC_STATEMENT = 0b001,
  10943. FUNC_HANGING_STATEMENT = 0b010,
  10944. FUNC_NULLABLE_ID = 0b100;
  10945. const loneSurrogate = /[\uD800-\uDFFF]/u;
  10946. const keywordRelationalOperator = /in(?:stanceof)?/y;
  10947. function babel7CompatTokens(tokens, input) {
  10948. for (let i = 0; i < tokens.length; i++) {
  10949. const token = tokens[i];
  10950. const {
  10951. type
  10952. } = token;
  10953. if (typeof type === "number") {
  10954. {
  10955. if (type === 130) {
  10956. const {
  10957. loc,
  10958. start,
  10959. value,
  10960. end
  10961. } = token;
  10962. const hashEndPos = start + 1;
  10963. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  10964. tokens.splice(i, 1, new Token({
  10965. type: getExportedToken(27),
  10966. value: "#",
  10967. start: start,
  10968. end: hashEndPos,
  10969. startLoc: loc.start,
  10970. endLoc: hashEndLoc
  10971. }), new Token({
  10972. type: getExportedToken(124),
  10973. value: value,
  10974. start: hashEndPos,
  10975. end: end,
  10976. startLoc: hashEndLoc,
  10977. endLoc: loc.end
  10978. }));
  10979. i++;
  10980. continue;
  10981. }
  10982. if (tokenIsTemplate(type)) {
  10983. const {
  10984. loc,
  10985. start,
  10986. value,
  10987. end
  10988. } = token;
  10989. const backquoteEnd = start + 1;
  10990. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  10991. let startToken;
  10992. if (input.charCodeAt(start) === 96) {
  10993. startToken = new Token({
  10994. type: getExportedToken(22),
  10995. value: "`",
  10996. start: start,
  10997. end: backquoteEnd,
  10998. startLoc: loc.start,
  10999. endLoc: backquoteEndLoc
  11000. });
  11001. } else {
  11002. startToken = new Token({
  11003. type: getExportedToken(8),
  11004. value: "}",
  11005. start: start,
  11006. end: backquoteEnd,
  11007. startLoc: loc.start,
  11008. endLoc: backquoteEndLoc
  11009. });
  11010. }
  11011. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  11012. if (type === 24) {
  11013. templateElementEnd = end - 1;
  11014. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  11015. templateValue = value === null ? null : value.slice(1, -1);
  11016. endToken = new Token({
  11017. type: getExportedToken(22),
  11018. value: "`",
  11019. start: templateElementEnd,
  11020. end: end,
  11021. startLoc: templateElementEndLoc,
  11022. endLoc: loc.end
  11023. });
  11024. } else {
  11025. templateElementEnd = end - 2;
  11026. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  11027. templateValue = value === null ? null : value.slice(1, -2);
  11028. endToken = new Token({
  11029. type: getExportedToken(23),
  11030. value: "${",
  11031. start: templateElementEnd,
  11032. end: end,
  11033. startLoc: templateElementEndLoc,
  11034. endLoc: loc.end
  11035. });
  11036. }
  11037. tokens.splice(i, 1, startToken, new Token({
  11038. type: getExportedToken(20),
  11039. value: templateValue,
  11040. start: backquoteEnd,
  11041. end: templateElementEnd,
  11042. startLoc: backquoteEndLoc,
  11043. endLoc: templateElementEndLoc
  11044. }), endToken);
  11045. i += 2;
  11046. continue;
  11047. }
  11048. }
  11049. token.type = getExportedToken(type);
  11050. }
  11051. }
  11052. return tokens;
  11053. }
  11054. class StatementParser extends ExpressionParser {
  11055. parseTopLevel(file, program) {
  11056. file.program = this.parseProgram(program);
  11057. file.comments = this.state.comments;
  11058. if (this.options.tokens) {
  11059. file.tokens = babel7CompatTokens(this.tokens, this.input);
  11060. }
  11061. return this.finishNode(file, "File");
  11062. }
  11063. parseProgram(program, end = 131, sourceType = this.options.sourceType) {
  11064. program.sourceType = sourceType;
  11065. program.interpreter = this.parseInterpreterDirective();
  11066. this.parseBlockBody(program, true, true, end);
  11067. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  11068. for (const [name] of Array.from(this.scope.undefinedExports)) {
  11069. const pos = this.scope.undefinedExports.get(name);
  11070. this.raise(pos, ErrorMessages.ModuleExportUndefined, name);
  11071. }
  11072. }
  11073. return this.finishNode(program, "Program");
  11074. }
  11075. stmtToDirective(stmt) {
  11076. const directive = stmt;
  11077. directive.type = "Directive";
  11078. directive.value = directive.expression;
  11079. delete directive.expression;
  11080. const directiveLiteral = directive.value;
  11081. const expressionValue = directiveLiteral.value;
  11082. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  11083. const val = directiveLiteral.value = raw.slice(1, -1);
  11084. this.addExtra(directiveLiteral, "raw", raw);
  11085. this.addExtra(directiveLiteral, "rawValue", val);
  11086. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  11087. directiveLiteral.type = "DirectiveLiteral";
  11088. return directive;
  11089. }
  11090. parseInterpreterDirective() {
  11091. if (!this.match(28)) {
  11092. return null;
  11093. }
  11094. const node = this.startNode();
  11095. node.value = this.state.value;
  11096. this.next();
  11097. return this.finishNode(node, "InterpreterDirective");
  11098. }
  11099. isLet(context) {
  11100. if (!this.isContextual(95)) {
  11101. return false;
  11102. }
  11103. return this.isLetKeyword(context);
  11104. }
  11105. isLetKeyword(context) {
  11106. const next = this.nextTokenStart();
  11107. const nextCh = this.codePointAtPos(next);
  11108. if (nextCh === 92 || nextCh === 91) {
  11109. return true;
  11110. }
  11111. if (context) return false;
  11112. if (nextCh === 123) return true;
  11113. if (isIdentifierStart(nextCh)) {
  11114. keywordRelationalOperator.lastIndex = next;
  11115. if (keywordRelationalOperator.test(this.input)) {
  11116. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  11117. if (!isIdentifierChar(endCh) && endCh !== 92) {
  11118. return false;
  11119. }
  11120. }
  11121. return true;
  11122. }
  11123. return false;
  11124. }
  11125. parseStatement(context, topLevel) {
  11126. if (this.match(26)) {
  11127. this.parseDecorators(true);
  11128. }
  11129. return this.parseStatementContent(context, topLevel);
  11130. }
  11131. parseStatementContent(context, topLevel) {
  11132. let starttype = this.state.type;
  11133. const node = this.startNode();
  11134. let kind;
  11135. if (this.isLet(context)) {
  11136. starttype = 70;
  11137. kind = "let";
  11138. }
  11139. switch (starttype) {
  11140. case 56:
  11141. return this.parseBreakContinueStatement(node, true);
  11142. case 59:
  11143. return this.parseBreakContinueStatement(node, false);
  11144. case 60:
  11145. return this.parseDebuggerStatement(node);
  11146. case 86:
  11147. return this.parseDoStatement(node);
  11148. case 87:
  11149. return this.parseForStatement(node);
  11150. case 64:
  11151. if (this.lookaheadCharCode() === 46) break;
  11152. if (context) {
  11153. if (this.state.strict) {
  11154. this.raise(this.state.start, ErrorMessages.StrictFunction);
  11155. } else if (context !== "if" && context !== "label") {
  11156. this.raise(this.state.start, ErrorMessages.SloppyFunction);
  11157. }
  11158. }
  11159. return this.parseFunctionStatement(node, false, !context);
  11160. case 76:
  11161. if (context) this.unexpected();
  11162. return this.parseClass(node, true);
  11163. case 65:
  11164. return this.parseIfStatement(node);
  11165. case 66:
  11166. return this.parseReturnStatement(node);
  11167. case 67:
  11168. return this.parseSwitchStatement(node);
  11169. case 68:
  11170. return this.parseThrowStatement(node);
  11171. case 69:
  11172. return this.parseTryStatement(node);
  11173. case 71:
  11174. case 70:
  11175. kind = kind || this.state.value;
  11176. if (context && kind !== "var") {
  11177. this.raise(this.state.start, ErrorMessages.UnexpectedLexicalDeclaration);
  11178. }
  11179. return this.parseVarStatement(node, kind);
  11180. case 88:
  11181. return this.parseWhileStatement(node);
  11182. case 72:
  11183. return this.parseWithStatement(node);
  11184. case 5:
  11185. return this.parseBlock();
  11186. case 13:
  11187. return this.parseEmptyStatement(node);
  11188. case 79:
  11189. {
  11190. const nextTokenCharCode = this.lookaheadCharCode();
  11191. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  11192. break;
  11193. }
  11194. }
  11195. case 78:
  11196. {
  11197. if (!this.options.allowImportExportEverywhere && !topLevel) {
  11198. this.raise(this.state.start, ErrorMessages.UnexpectedImportExport);
  11199. }
  11200. this.next();
  11201. let result;
  11202. if (starttype === 79) {
  11203. result = this.parseImport(node);
  11204. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  11205. this.sawUnambiguousESM = true;
  11206. }
  11207. } else {
  11208. result = this.parseExport(node);
  11209. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  11210. this.sawUnambiguousESM = true;
  11211. }
  11212. }
  11213. this.assertModuleNodeAllowed(node);
  11214. return result;
  11215. }
  11216. default:
  11217. {
  11218. if (this.isAsyncFunction()) {
  11219. if (context) {
  11220. this.raise(this.state.start, ErrorMessages.AsyncFunctionInSingleStatementContext);
  11221. }
  11222. this.next();
  11223. return this.parseFunctionStatement(node, true, !context);
  11224. }
  11225. }
  11226. }
  11227. const maybeName = this.state.value;
  11228. const expr = this.parseExpression();
  11229. if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
  11230. return this.parseLabeledStatement(node, maybeName, expr, context);
  11231. } else {
  11232. return this.parseExpressionStatement(node, expr);
  11233. }
  11234. }
  11235. assertModuleNodeAllowed(node) {
  11236. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  11237. this.raise(node.start, SourceTypeModuleErrorMessages.ImportOutsideModule);
  11238. }
  11239. }
  11240. takeDecorators(node) {
  11241. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  11242. if (decorators.length) {
  11243. node.decorators = decorators;
  11244. this.resetStartLocationFromNode(node, decorators[0]);
  11245. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  11246. }
  11247. }
  11248. canHaveLeadingDecorator() {
  11249. return this.match(76);
  11250. }
  11251. parseDecorators(allowExport) {
  11252. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  11253. while (this.match(26)) {
  11254. const decorator = this.parseDecorator();
  11255. currentContextDecorators.push(decorator);
  11256. }
  11257. if (this.match(78)) {
  11258. if (!allowExport) {
  11259. this.unexpected();
  11260. }
  11261. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  11262. this.raise(this.state.start, ErrorMessages.DecoratorExportClass);
  11263. }
  11264. } else if (!this.canHaveLeadingDecorator()) {
  11265. throw this.raise(this.state.start, ErrorMessages.UnexpectedLeadingDecorator);
  11266. }
  11267. }
  11268. parseDecorator() {
  11269. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  11270. const node = this.startNode();
  11271. this.next();
  11272. if (this.hasPlugin("decorators")) {
  11273. this.state.decoratorStack.push([]);
  11274. const startPos = this.state.start;
  11275. const startLoc = this.state.startLoc;
  11276. let expr;
  11277. if (this.eat(10)) {
  11278. expr = this.parseExpression();
  11279. this.expect(11);
  11280. } else {
  11281. expr = this.parseIdentifier(false);
  11282. while (this.eat(16)) {
  11283. const node = this.startNodeAt(startPos, startLoc);
  11284. node.object = expr;
  11285. node.property = this.parseIdentifier(true);
  11286. node.computed = false;
  11287. expr = this.finishNode(node, "MemberExpression");
  11288. }
  11289. }
  11290. node.expression = this.parseMaybeDecoratorArguments(expr);
  11291. this.state.decoratorStack.pop();
  11292. } else {
  11293. node.expression = this.parseExprSubscripts();
  11294. }
  11295. return this.finishNode(node, "Decorator");
  11296. }
  11297. parseMaybeDecoratorArguments(expr) {
  11298. if (this.eat(10)) {
  11299. const node = this.startNodeAtNode(expr);
  11300. node.callee = expr;
  11301. node.arguments = this.parseCallExpressionArguments(11, false);
  11302. this.toReferencedList(node.arguments);
  11303. return this.finishNode(node, "CallExpression");
  11304. }
  11305. return expr;
  11306. }
  11307. parseBreakContinueStatement(node, isBreak) {
  11308. this.next();
  11309. if (this.isLineTerminator()) {
  11310. node.label = null;
  11311. } else {
  11312. node.label = this.parseIdentifier();
  11313. this.semicolon();
  11314. }
  11315. this.verifyBreakContinue(node, isBreak);
  11316. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  11317. }
  11318. verifyBreakContinue(node, isBreak) {
  11319. let i;
  11320. for (i = 0; i < this.state.labels.length; ++i) {
  11321. const lab = this.state.labels[i];
  11322. if (node.label == null || lab.name === node.label.name) {
  11323. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  11324. if (node.label && isBreak) break;
  11325. }
  11326. }
  11327. if (i === this.state.labels.length) {
  11328. this.raise(node.start, ErrorMessages.IllegalBreakContinue, isBreak ? "break" : "continue");
  11329. }
  11330. }
  11331. parseDebuggerStatement(node) {
  11332. this.next();
  11333. this.semicolon();
  11334. return this.finishNode(node, "DebuggerStatement");
  11335. }
  11336. parseHeaderExpression() {
  11337. this.expect(10);
  11338. const val = this.parseExpression();
  11339. this.expect(11);
  11340. return val;
  11341. }
  11342. parseDoStatement(node) {
  11343. this.next();
  11344. this.state.labels.push(loopLabel);
  11345. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("do"));
  11346. this.state.labels.pop();
  11347. this.expect(88);
  11348. node.test = this.parseHeaderExpression();
  11349. this.eat(13);
  11350. return this.finishNode(node, "DoWhileStatement");
  11351. }
  11352. parseForStatement(node) {
  11353. this.next();
  11354. this.state.labels.push(loopLabel);
  11355. let awaitAt = -1;
  11356. if (this.isAwaitAllowed() && this.eatContextual(92)) {
  11357. awaitAt = this.state.lastTokStart;
  11358. }
  11359. this.scope.enter(SCOPE_OTHER);
  11360. this.expect(10);
  11361. if (this.match(13)) {
  11362. if (awaitAt > -1) {
  11363. this.unexpected(awaitAt);
  11364. }
  11365. return this.parseFor(node, null);
  11366. }
  11367. const startsWithLet = this.isContextual(95);
  11368. const isLet = startsWithLet && this.isLetKeyword();
  11369. if (this.match(70) || this.match(71) || isLet) {
  11370. const init = this.startNode();
  11371. const kind = isLet ? "let" : this.state.value;
  11372. this.next();
  11373. this.parseVar(init, true, kind);
  11374. this.finishNode(init, "VariableDeclaration");
  11375. if ((this.match(54) || this.isContextual(97)) && init.declarations.length === 1) {
  11376. return this.parseForIn(node, init, awaitAt);
  11377. }
  11378. if (awaitAt > -1) {
  11379. this.unexpected(awaitAt);
  11380. }
  11381. return this.parseFor(node, init);
  11382. }
  11383. const startsWithAsync = this.isContextual(91);
  11384. const refExpressionErrors = new ExpressionErrors();
  11385. const init = this.parseExpression(true, refExpressionErrors);
  11386. const isForOf = this.isContextual(97);
  11387. if (isForOf) {
  11388. if (startsWithLet) {
  11389. this.raise(init.start, ErrorMessages.ForOfLet);
  11390. } else if (awaitAt === -1 && startsWithAsync && init.type === "Identifier") {
  11391. this.raise(init.start, ErrorMessages.ForOfAsync);
  11392. }
  11393. }
  11394. if (isForOf || this.match(54)) {
  11395. this.toAssignable(init, true);
  11396. const description = isForOf ? "for-of statement" : "for-in statement";
  11397. this.checkLVal(init, description);
  11398. return this.parseForIn(node, init, awaitAt);
  11399. } else {
  11400. this.checkExpressionErrors(refExpressionErrors, true);
  11401. }
  11402. if (awaitAt > -1) {
  11403. this.unexpected(awaitAt);
  11404. }
  11405. return this.parseFor(node, init);
  11406. }
  11407. parseFunctionStatement(node, isAsync, declarationPosition) {
  11408. this.next();
  11409. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  11410. }
  11411. parseIfStatement(node) {
  11412. this.next();
  11413. node.test = this.parseHeaderExpression();
  11414. node.consequent = this.parseStatement("if");
  11415. node.alternate = this.eat(62) ? this.parseStatement("if") : null;
  11416. return this.finishNode(node, "IfStatement");
  11417. }
  11418. parseReturnStatement(node) {
  11419. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  11420. this.raise(this.state.start, ErrorMessages.IllegalReturn);
  11421. }
  11422. this.next();
  11423. if (this.isLineTerminator()) {
  11424. node.argument = null;
  11425. } else {
  11426. node.argument = this.parseExpression();
  11427. this.semicolon();
  11428. }
  11429. return this.finishNode(node, "ReturnStatement");
  11430. }
  11431. parseSwitchStatement(node) {
  11432. this.next();
  11433. node.discriminant = this.parseHeaderExpression();
  11434. const cases = node.cases = [];
  11435. this.expect(5);
  11436. this.state.labels.push(switchLabel);
  11437. this.scope.enter(SCOPE_OTHER);
  11438. let cur;
  11439. for (let sawDefault; !this.match(8);) {
  11440. if (this.match(57) || this.match(61)) {
  11441. const isCase = this.match(57);
  11442. if (cur) this.finishNode(cur, "SwitchCase");
  11443. cases.push(cur = this.startNode());
  11444. cur.consequent = [];
  11445. this.next();
  11446. if (isCase) {
  11447. cur.test = this.parseExpression();
  11448. } else {
  11449. if (sawDefault) {
  11450. this.raise(this.state.lastTokStart, ErrorMessages.MultipleDefaultsInSwitch);
  11451. }
  11452. sawDefault = true;
  11453. cur.test = null;
  11454. }
  11455. this.expect(14);
  11456. } else {
  11457. if (cur) {
  11458. cur.consequent.push(this.parseStatement(null));
  11459. } else {
  11460. this.unexpected();
  11461. }
  11462. }
  11463. }
  11464. this.scope.exit();
  11465. if (cur) this.finishNode(cur, "SwitchCase");
  11466. this.next();
  11467. this.state.labels.pop();
  11468. return this.finishNode(node, "SwitchStatement");
  11469. }
  11470. parseThrowStatement(node) {
  11471. this.next();
  11472. if (this.hasPrecedingLineBreak()) {
  11473. this.raise(this.state.lastTokEnd, ErrorMessages.NewlineAfterThrow);
  11474. }
  11475. node.argument = this.parseExpression();
  11476. this.semicolon();
  11477. return this.finishNode(node, "ThrowStatement");
  11478. }
  11479. parseCatchClauseParam() {
  11480. const param = this.parseBindingAtom();
  11481. const simple = param.type === "Identifier";
  11482. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  11483. this.checkLVal(param, "catch clause", BIND_LEXICAL);
  11484. return param;
  11485. }
  11486. parseTryStatement(node) {
  11487. this.next();
  11488. node.block = this.parseBlock();
  11489. node.handler = null;
  11490. if (this.match(58)) {
  11491. const clause = this.startNode();
  11492. this.next();
  11493. if (this.match(10)) {
  11494. this.expect(10);
  11495. clause.param = this.parseCatchClauseParam();
  11496. this.expect(11);
  11497. } else {
  11498. clause.param = null;
  11499. this.scope.enter(SCOPE_OTHER);
  11500. }
  11501. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  11502. this.scope.exit();
  11503. node.handler = this.finishNode(clause, "CatchClause");
  11504. }
  11505. node.finalizer = this.eat(63) ? this.parseBlock() : null;
  11506. if (!node.handler && !node.finalizer) {
  11507. this.raise(node.start, ErrorMessages.NoCatchOrFinally);
  11508. }
  11509. return this.finishNode(node, "TryStatement");
  11510. }
  11511. parseVarStatement(node, kind) {
  11512. this.next();
  11513. this.parseVar(node, false, kind);
  11514. this.semicolon();
  11515. return this.finishNode(node, "VariableDeclaration");
  11516. }
  11517. parseWhileStatement(node) {
  11518. this.next();
  11519. node.test = this.parseHeaderExpression();
  11520. this.state.labels.push(loopLabel);
  11521. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("while"));
  11522. this.state.labels.pop();
  11523. return this.finishNode(node, "WhileStatement");
  11524. }
  11525. parseWithStatement(node) {
  11526. if (this.state.strict) {
  11527. this.raise(this.state.start, ErrorMessages.StrictWith);
  11528. }
  11529. this.next();
  11530. node.object = this.parseHeaderExpression();
  11531. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("with"));
  11532. return this.finishNode(node, "WithStatement");
  11533. }
  11534. parseEmptyStatement(node) {
  11535. this.next();
  11536. return this.finishNode(node, "EmptyStatement");
  11537. }
  11538. parseLabeledStatement(node, maybeName, expr, context) {
  11539. for (const label of this.state.labels) {
  11540. if (label.name === maybeName) {
  11541. this.raise(expr.start, ErrorMessages.LabelRedeclaration, maybeName);
  11542. }
  11543. }
  11544. const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(67) ? "switch" : null;
  11545. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  11546. const label = this.state.labels[i];
  11547. if (label.statementStart === node.start) {
  11548. label.statementStart = this.state.start;
  11549. label.kind = kind;
  11550. } else {
  11551. break;
  11552. }
  11553. }
  11554. this.state.labels.push({
  11555. name: maybeName,
  11556. kind: kind,
  11557. statementStart: this.state.start
  11558. });
  11559. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  11560. this.state.labels.pop();
  11561. node.label = expr;
  11562. return this.finishNode(node, "LabeledStatement");
  11563. }
  11564. parseExpressionStatement(node, expr) {
  11565. node.expression = expr;
  11566. this.semicolon();
  11567. return this.finishNode(node, "ExpressionStatement");
  11568. }
  11569. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  11570. const node = this.startNode();
  11571. if (allowDirectives) {
  11572. this.state.strictErrors.clear();
  11573. }
  11574. this.expect(5);
  11575. if (createNewLexicalScope) {
  11576. this.scope.enter(SCOPE_OTHER);
  11577. }
  11578. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  11579. if (createNewLexicalScope) {
  11580. this.scope.exit();
  11581. }
  11582. return this.finishNode(node, "BlockStatement");
  11583. }
  11584. isValidDirective(stmt) {
  11585. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  11586. }
  11587. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  11588. const body = node.body = [];
  11589. const directives = node.directives = [];
  11590. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  11591. }
  11592. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  11593. const oldStrict = this.state.strict;
  11594. let hasStrictModeDirective = false;
  11595. let parsedNonDirective = false;
  11596. while (!this.match(end)) {
  11597. const stmt = this.parseStatement(null, topLevel);
  11598. if (directives && !parsedNonDirective) {
  11599. if (this.isValidDirective(stmt)) {
  11600. const directive = this.stmtToDirective(stmt);
  11601. directives.push(directive);
  11602. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  11603. hasStrictModeDirective = true;
  11604. this.setStrict(true);
  11605. }
  11606. continue;
  11607. }
  11608. parsedNonDirective = true;
  11609. this.state.strictErrors.clear();
  11610. }
  11611. body.push(stmt);
  11612. }
  11613. if (afterBlockParse) {
  11614. afterBlockParse.call(this, hasStrictModeDirective);
  11615. }
  11616. if (!oldStrict) {
  11617. this.setStrict(false);
  11618. }
  11619. this.next();
  11620. }
  11621. parseFor(node, init) {
  11622. node.init = init;
  11623. this.semicolon(false);
  11624. node.test = this.match(13) ? null : this.parseExpression();
  11625. this.semicolon(false);
  11626. node.update = this.match(11) ? null : this.parseExpression();
  11627. this.expect(11);
  11628. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  11629. this.scope.exit();
  11630. this.state.labels.pop();
  11631. return this.finishNode(node, "ForStatement");
  11632. }
  11633. parseForIn(node, init, awaitAt) {
  11634. const isForIn = this.match(54);
  11635. this.next();
  11636. if (isForIn) {
  11637. if (awaitAt > -1) this.unexpected(awaitAt);
  11638. } else {
  11639. node.await = awaitAt > -1;
  11640. }
  11641. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  11642. this.raise(init.start, ErrorMessages.ForInOfLoopInitializer, isForIn ? "for-in" : "for-of");
  11643. } else if (init.type === "AssignmentPattern") {
  11644. this.raise(init.start, ErrorMessages.InvalidLhs, "for-loop");
  11645. }
  11646. node.left = init;
  11647. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  11648. this.expect(11);
  11649. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  11650. this.scope.exit();
  11651. this.state.labels.pop();
  11652. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  11653. }
  11654. parseVar(node, isFor, kind) {
  11655. const declarations = node.declarations = [];
  11656. const isTypescript = this.hasPlugin("typescript");
  11657. node.kind = kind;
  11658. for (;;) {
  11659. const decl = this.startNode();
  11660. this.parseVarId(decl, kind);
  11661. if (this.eat(29)) {
  11662. decl.init = isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  11663. } else {
  11664. if (kind === "const" && !(this.match(54) || this.isContextual(97))) {
  11665. if (!isTypescript) {
  11666. this.raise(this.state.lastTokEnd, ErrorMessages.DeclarationMissingInitializer, "Const declarations");
  11667. }
  11668. } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(54) || this.isContextual(97)))) {
  11669. this.raise(this.state.lastTokEnd, ErrorMessages.DeclarationMissingInitializer, "Complex binding patterns");
  11670. }
  11671. decl.init = null;
  11672. }
  11673. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  11674. if (!this.eat(12)) break;
  11675. }
  11676. return node;
  11677. }
  11678. parseVarId(decl, kind) {
  11679. decl.id = this.parseBindingAtom();
  11680. this.checkLVal(decl.id, "variable declaration", kind === "var" ? BIND_VAR : BIND_LEXICAL, undefined, kind !== "var");
  11681. }
  11682. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  11683. const isStatement = statement & FUNC_STATEMENT;
  11684. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  11685. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  11686. this.initFunction(node, isAsync);
  11687. if (this.match(51) && isHangingStatement) {
  11688. this.raise(this.state.start, ErrorMessages.GeneratorInSingleStatementContext);
  11689. }
  11690. node.generator = this.eat(51);
  11691. if (isStatement) {
  11692. node.id = this.parseFunctionId(requireId);
  11693. }
  11694. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11695. this.state.maybeInArrowParameters = false;
  11696. this.scope.enter(SCOPE_FUNCTION);
  11697. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11698. if (!isStatement) {
  11699. node.id = this.parseFunctionId();
  11700. }
  11701. this.parseFunctionParams(node, false);
  11702. this.withSmartMixTopicForbiddingContext(() => {
  11703. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  11704. });
  11705. this.prodParam.exit();
  11706. this.scope.exit();
  11707. if (isStatement && !isHangingStatement) {
  11708. this.registerFunctionStatementId(node);
  11709. }
  11710. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11711. return node;
  11712. }
  11713. parseFunctionId(requireId) {
  11714. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  11715. }
  11716. parseFunctionParams(node, allowModifiers) {
  11717. this.expect(10);
  11718. this.expressionScope.enter(newParameterDeclarationScope());
  11719. node.params = this.parseBindingList(11, 41, false, allowModifiers);
  11720. this.expressionScope.exit();
  11721. }
  11722. registerFunctionStatementId(node) {
  11723. if (!node.id) return;
  11724. this.scope.declareName(node.id.name, this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, node.id.start);
  11725. }
  11726. parseClass(node, isStatement, optionalId) {
  11727. this.next();
  11728. this.takeDecorators(node);
  11729. const oldStrict = this.state.strict;
  11730. this.state.strict = true;
  11731. this.parseClassId(node, isStatement, optionalId);
  11732. this.parseClassSuper(node);
  11733. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  11734. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  11735. }
  11736. isClassProperty() {
  11737. return this.match(29) || this.match(13) || this.match(8);
  11738. }
  11739. isClassMethod() {
  11740. return this.match(10);
  11741. }
  11742. isNonstaticConstructor(method) {
  11743. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  11744. }
  11745. parseClassBody(hadSuperClass, oldStrict) {
  11746. this.classScope.enter();
  11747. const state = {
  11748. hadConstructor: false,
  11749. hadSuperClass
  11750. };
  11751. let decorators = [];
  11752. const classBody = this.startNode();
  11753. classBody.body = [];
  11754. this.expect(5);
  11755. this.withSmartMixTopicForbiddingContext(() => {
  11756. while (!this.match(8)) {
  11757. if (this.eat(13)) {
  11758. if (decorators.length > 0) {
  11759. throw this.raise(this.state.lastTokEnd, ErrorMessages.DecoratorSemicolon);
  11760. }
  11761. continue;
  11762. }
  11763. if (this.match(26)) {
  11764. decorators.push(this.parseDecorator());
  11765. continue;
  11766. }
  11767. const member = this.startNode();
  11768. if (decorators.length) {
  11769. member.decorators = decorators;
  11770. this.resetStartLocationFromNode(member, decorators[0]);
  11771. decorators = [];
  11772. }
  11773. this.parseClassMember(classBody, member, state);
  11774. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  11775. this.raise(member.start, ErrorMessages.DecoratorConstructor);
  11776. }
  11777. }
  11778. });
  11779. this.state.strict = oldStrict;
  11780. this.next();
  11781. if (decorators.length) {
  11782. throw this.raise(this.state.start, ErrorMessages.TrailingDecorator);
  11783. }
  11784. this.classScope.exit();
  11785. return this.finishNode(classBody, "ClassBody");
  11786. }
  11787. parseClassMemberFromModifier(classBody, member) {
  11788. const key = this.parseIdentifier(true);
  11789. if (this.isClassMethod()) {
  11790. const method = member;
  11791. method.kind = "method";
  11792. method.computed = false;
  11793. method.key = key;
  11794. method.static = false;
  11795. this.pushClassMethod(classBody, method, false, false, false, false);
  11796. return true;
  11797. } else if (this.isClassProperty()) {
  11798. const prop = member;
  11799. prop.computed = false;
  11800. prop.key = key;
  11801. prop.static = false;
  11802. classBody.body.push(this.parseClassProperty(prop));
  11803. return true;
  11804. }
  11805. this.resetPreviousNodeTrailingComments(key);
  11806. return false;
  11807. }
  11808. parseClassMember(classBody, member, state) {
  11809. const isStatic = this.isContextual(100);
  11810. if (isStatic) {
  11811. if (this.parseClassMemberFromModifier(classBody, member)) {
  11812. return;
  11813. }
  11814. if (this.eat(5)) {
  11815. this.parseClassStaticBlock(classBody, member);
  11816. return;
  11817. }
  11818. }
  11819. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  11820. }
  11821. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  11822. const publicMethod = member;
  11823. const privateMethod = member;
  11824. const publicProp = member;
  11825. const privateProp = member;
  11826. const method = publicMethod;
  11827. const publicMember = publicMethod;
  11828. member.static = isStatic;
  11829. this.parsePropertyNamePrefixOperator(member);
  11830. if (this.eat(51)) {
  11831. method.kind = "method";
  11832. const isPrivateName = this.match(130);
  11833. this.parseClassElementName(method);
  11834. if (isPrivateName) {
  11835. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  11836. return;
  11837. }
  11838. if (this.isNonstaticConstructor(publicMethod)) {
  11839. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsGenerator);
  11840. }
  11841. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  11842. return;
  11843. }
  11844. const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
  11845. const isPrivate = this.match(130);
  11846. const key = this.parseClassElementName(member);
  11847. const maybeQuestionTokenStart = this.state.start;
  11848. this.parsePostMemberNameModifiers(publicMember);
  11849. if (this.isClassMethod()) {
  11850. method.kind = "method";
  11851. if (isPrivate) {
  11852. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  11853. return;
  11854. }
  11855. const isConstructor = this.isNonstaticConstructor(publicMethod);
  11856. let allowsDirectSuper = false;
  11857. if (isConstructor) {
  11858. publicMethod.kind = "constructor";
  11859. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  11860. this.raise(key.start, ErrorMessages.DuplicateConstructor);
  11861. }
  11862. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  11863. this.raise(key.start, ErrorMessages.OverrideOnConstructor);
  11864. }
  11865. state.hadConstructor = true;
  11866. allowsDirectSuper = state.hadSuperClass;
  11867. }
  11868. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  11869. } else if (this.isClassProperty()) {
  11870. if (isPrivate) {
  11871. this.pushClassPrivateProperty(classBody, privateProp);
  11872. } else {
  11873. this.pushClassProperty(classBody, publicProp);
  11874. }
  11875. } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
  11876. this.resetPreviousNodeTrailingComments(key);
  11877. const isGenerator = this.eat(51);
  11878. if (publicMember.optional) {
  11879. this.unexpected(maybeQuestionTokenStart);
  11880. }
  11881. method.kind = "method";
  11882. const isPrivate = this.match(130);
  11883. this.parseClassElementName(method);
  11884. this.parsePostMemberNameModifiers(publicMember);
  11885. if (isPrivate) {
  11886. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  11887. } else {
  11888. if (this.isNonstaticConstructor(publicMethod)) {
  11889. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsAsync);
  11890. }
  11891. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  11892. }
  11893. } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(51) && this.isLineTerminator())) {
  11894. this.resetPreviousNodeTrailingComments(key);
  11895. method.kind = key.name;
  11896. const isPrivate = this.match(130);
  11897. this.parseClassElementName(publicMethod);
  11898. if (isPrivate) {
  11899. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  11900. } else {
  11901. if (this.isNonstaticConstructor(publicMethod)) {
  11902. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsAccessor);
  11903. }
  11904. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  11905. }
  11906. this.checkGetterSetterParams(publicMethod);
  11907. } else if (this.isLineTerminator()) {
  11908. if (isPrivate) {
  11909. this.pushClassPrivateProperty(classBody, privateProp);
  11910. } else {
  11911. this.pushClassProperty(classBody, publicProp);
  11912. }
  11913. } else {
  11914. this.unexpected();
  11915. }
  11916. }
  11917. parseClassElementName(member) {
  11918. const {
  11919. type,
  11920. value,
  11921. start
  11922. } = this.state;
  11923. if ((type === 124 || type === 125) && member.static && value === "prototype") {
  11924. this.raise(start, ErrorMessages.StaticPrototype);
  11925. }
  11926. if (type === 130) {
  11927. if (value === "constructor") {
  11928. this.raise(start, ErrorMessages.ConstructorClassPrivateField);
  11929. }
  11930. const key = this.parsePrivateName();
  11931. member.key = key;
  11932. return key;
  11933. }
  11934. return this.parsePropertyName(member);
  11935. }
  11936. parseClassStaticBlock(classBody, member) {
  11937. var _member$decorators;
  11938. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  11939. const oldLabels = this.state.labels;
  11940. this.state.labels = [];
  11941. this.prodParam.enter(PARAM);
  11942. const body = member.body = [];
  11943. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  11944. this.prodParam.exit();
  11945. this.scope.exit();
  11946. this.state.labels = oldLabels;
  11947. classBody.body.push(this.finishNode(member, "StaticBlock"));
  11948. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  11949. this.raise(member.start, ErrorMessages.DecoratorStaticBlock);
  11950. }
  11951. }
  11952. pushClassProperty(classBody, prop) {
  11953. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  11954. this.raise(prop.key.start, ErrorMessages.ConstructorClassField);
  11955. }
  11956. classBody.body.push(this.parseClassProperty(prop));
  11957. }
  11958. pushClassPrivateProperty(classBody, prop) {
  11959. const node = this.parseClassPrivateProperty(prop);
  11960. classBody.body.push(node);
  11961. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.start);
  11962. }
  11963. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  11964. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  11965. }
  11966. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  11967. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  11968. classBody.body.push(node);
  11969. 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;
  11970. this.declareClassPrivateMethodInScope(node, kind);
  11971. }
  11972. declareClassPrivateMethodInScope(node, kind) {
  11973. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.start);
  11974. }
  11975. parsePostMemberNameModifiers(methodOrProp) {}
  11976. parseClassPrivateProperty(node) {
  11977. this.parseInitializer(node);
  11978. this.semicolon();
  11979. return this.finishNode(node, "ClassPrivateProperty");
  11980. }
  11981. parseClassProperty(node) {
  11982. this.parseInitializer(node);
  11983. this.semicolon();
  11984. return this.finishNode(node, "ClassProperty");
  11985. }
  11986. parseInitializer(node) {
  11987. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  11988. this.expressionScope.enter(newExpressionScope());
  11989. this.prodParam.enter(PARAM);
  11990. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  11991. this.expressionScope.exit();
  11992. this.prodParam.exit();
  11993. this.scope.exit();
  11994. }
  11995. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  11996. if (tokenIsIdentifier(this.state.type)) {
  11997. node.id = this.parseIdentifier();
  11998. if (isStatement) {
  11999. this.checkLVal(node.id, "class name", bindingType);
  12000. }
  12001. } else {
  12002. if (optionalId || !isStatement) {
  12003. node.id = null;
  12004. } else {
  12005. this.unexpected(null, ErrorMessages.MissingClassName);
  12006. }
  12007. }
  12008. }
  12009. parseClassSuper(node) {
  12010. node.superClass = this.eat(77) ? this.parseExprSubscripts() : null;
  12011. }
  12012. parseExport(node) {
  12013. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  12014. const parseAfterDefault = !hasDefault || this.eat(12);
  12015. const hasStar = parseAfterDefault && this.eatExportStar(node);
  12016. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  12017. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  12018. const isFromRequired = hasDefault || hasStar;
  12019. if (hasStar && !hasNamespace) {
  12020. if (hasDefault) this.unexpected();
  12021. this.parseExportFrom(node, true);
  12022. return this.finishNode(node, "ExportAllDeclaration");
  12023. }
  12024. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  12025. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  12026. throw this.unexpected(null, 5);
  12027. }
  12028. let hasDeclaration;
  12029. if (isFromRequired || hasSpecifiers) {
  12030. hasDeclaration = false;
  12031. this.parseExportFrom(node, isFromRequired);
  12032. } else {
  12033. hasDeclaration = this.maybeParseExportDeclaration(node);
  12034. }
  12035. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  12036. this.checkExport(node, true, false, !!node.source);
  12037. return this.finishNode(node, "ExportNamedDeclaration");
  12038. }
  12039. if (this.eat(61)) {
  12040. node.declaration = this.parseExportDefaultExpression();
  12041. this.checkExport(node, true, true);
  12042. return this.finishNode(node, "ExportDefaultDeclaration");
  12043. }
  12044. throw this.unexpected(null, 5);
  12045. }
  12046. eatExportStar(node) {
  12047. return this.eat(51);
  12048. }
  12049. maybeParseExportDefaultSpecifier(node) {
  12050. if (this.isExportDefaultSpecifier()) {
  12051. this.expectPlugin("exportDefaultFrom");
  12052. const specifier = this.startNode();
  12053. specifier.exported = this.parseIdentifier(true);
  12054. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  12055. return true;
  12056. }
  12057. return false;
  12058. }
  12059. maybeParseExportNamespaceSpecifier(node) {
  12060. if (this.isContextual(89)) {
  12061. if (!node.specifiers) node.specifiers = [];
  12062. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  12063. this.next();
  12064. specifier.exported = this.parseModuleExportName();
  12065. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  12066. return true;
  12067. }
  12068. return false;
  12069. }
  12070. maybeParseExportNamedSpecifiers(node) {
  12071. if (this.match(5)) {
  12072. if (!node.specifiers) node.specifiers = [];
  12073. const isTypeExport = node.exportKind === "type";
  12074. node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  12075. node.source = null;
  12076. node.declaration = null;
  12077. if (this.hasPlugin("importAssertions")) {
  12078. node.assertions = [];
  12079. }
  12080. return true;
  12081. }
  12082. return false;
  12083. }
  12084. maybeParseExportDeclaration(node) {
  12085. if (this.shouldParseExportDeclaration()) {
  12086. node.specifiers = [];
  12087. node.source = null;
  12088. if (this.hasPlugin("importAssertions")) {
  12089. node.assertions = [];
  12090. }
  12091. node.declaration = this.parseExportDeclaration(node);
  12092. return true;
  12093. }
  12094. return false;
  12095. }
  12096. isAsyncFunction() {
  12097. if (!this.isContextual(91)) return false;
  12098. const next = this.nextTokenStart();
  12099. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  12100. }
  12101. parseExportDefaultExpression() {
  12102. const expr = this.startNode();
  12103. const isAsync = this.isAsyncFunction();
  12104. if (this.match(64) || isAsync) {
  12105. this.next();
  12106. if (isAsync) {
  12107. this.next();
  12108. }
  12109. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  12110. } else if (this.match(76)) {
  12111. return this.parseClass(expr, true, true);
  12112. } else if (this.match(26)) {
  12113. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  12114. this.raise(this.state.start, ErrorMessages.DecoratorBeforeExport);
  12115. }
  12116. this.parseDecorators(false);
  12117. return this.parseClass(expr, true, true);
  12118. } else if (this.match(71) || this.match(70) || this.isLet()) {
  12119. throw this.raise(this.state.start, ErrorMessages.UnsupportedDefaultExport);
  12120. } else {
  12121. const res = this.parseMaybeAssignAllowIn();
  12122. this.semicolon();
  12123. return res;
  12124. }
  12125. }
  12126. parseExportDeclaration(node) {
  12127. return this.parseStatement(null);
  12128. }
  12129. isExportDefaultSpecifier() {
  12130. const {
  12131. type
  12132. } = this.state;
  12133. if (tokenIsIdentifier(type)) {
  12134. if (type === 91 && !this.state.containsEsc || type === 95) {
  12135. return false;
  12136. }
  12137. if ((type === 122 || type === 121) && !this.state.containsEsc) {
  12138. const {
  12139. type: nextType
  12140. } = this.lookahead();
  12141. if (tokenIsIdentifier(nextType) && nextType !== 93 || nextType === 5) {
  12142. this.expectOnePlugin(["flow", "typescript"]);
  12143. return false;
  12144. }
  12145. }
  12146. } else if (!this.match(61)) {
  12147. return false;
  12148. }
  12149. const next = this.nextTokenStart();
  12150. const hasFrom = this.isUnparsedContextual(next, "from");
  12151. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  12152. return true;
  12153. }
  12154. if (this.match(61) && hasFrom) {
  12155. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  12156. return nextAfterFrom === 34 || nextAfterFrom === 39;
  12157. }
  12158. return false;
  12159. }
  12160. parseExportFrom(node, expect) {
  12161. if (this.eatContextual(93)) {
  12162. node.source = this.parseImportSource();
  12163. this.checkExport(node);
  12164. const assertions = this.maybeParseImportAssertions();
  12165. if (assertions) {
  12166. node.assertions = assertions;
  12167. }
  12168. } else if (expect) {
  12169. this.unexpected();
  12170. }
  12171. this.semicolon();
  12172. }
  12173. shouldParseExportDeclaration() {
  12174. const {
  12175. type
  12176. } = this.state;
  12177. if (type === 26) {
  12178. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12179. if (this.hasPlugin("decorators")) {
  12180. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  12181. this.unexpected(this.state.start, ErrorMessages.DecoratorBeforeExport);
  12182. } else {
  12183. return true;
  12184. }
  12185. }
  12186. }
  12187. return type === 70 || type === 71 || type === 64 || type === 76 || this.isLet() || this.isAsyncFunction();
  12188. }
  12189. checkExport(node, checkNames, isDefault, isFrom) {
  12190. if (checkNames) {
  12191. if (isDefault) {
  12192. this.checkDuplicateExports(node, "default");
  12193. if (this.hasPlugin("exportDefaultFrom")) {
  12194. var _declaration$extra;
  12195. const declaration = node.declaration;
  12196. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  12197. this.raise(declaration.start, ErrorMessages.ExportDefaultFromAsIdentifier);
  12198. }
  12199. }
  12200. } else if (node.specifiers && node.specifiers.length) {
  12201. for (const specifier of node.specifiers) {
  12202. const {
  12203. exported
  12204. } = specifier;
  12205. const exportedName = exported.type === "Identifier" ? exported.name : exported.value;
  12206. this.checkDuplicateExports(specifier, exportedName);
  12207. if (!isFrom && specifier.local) {
  12208. const {
  12209. local
  12210. } = specifier;
  12211. if (local.type !== "Identifier") {
  12212. this.raise(specifier.start, ErrorMessages.ExportBindingIsString, local.value, exportedName);
  12213. } else {
  12214. this.checkReservedWord(local.name, local.start, true, false);
  12215. this.scope.checkLocalExport(local);
  12216. }
  12217. }
  12218. }
  12219. } else if (node.declaration) {
  12220. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  12221. const id = node.declaration.id;
  12222. if (!id) throw new Error("Assertion failure");
  12223. this.checkDuplicateExports(node, id.name);
  12224. } else if (node.declaration.type === "VariableDeclaration") {
  12225. for (const declaration of node.declaration.declarations) {
  12226. this.checkDeclaration(declaration.id);
  12227. }
  12228. }
  12229. }
  12230. }
  12231. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  12232. if (currentContextDecorators.length) {
  12233. throw this.raise(node.start, ErrorMessages.UnsupportedDecoratorExport);
  12234. }
  12235. }
  12236. checkDeclaration(node) {
  12237. if (node.type === "Identifier") {
  12238. this.checkDuplicateExports(node, node.name);
  12239. } else if (node.type === "ObjectPattern") {
  12240. for (const prop of node.properties) {
  12241. this.checkDeclaration(prop);
  12242. }
  12243. } else if (node.type === "ArrayPattern") {
  12244. for (const elem of node.elements) {
  12245. if (elem) {
  12246. this.checkDeclaration(elem);
  12247. }
  12248. }
  12249. } else if (node.type === "ObjectProperty") {
  12250. this.checkDeclaration(node.value);
  12251. } else if (node.type === "RestElement") {
  12252. this.checkDeclaration(node.argument);
  12253. } else if (node.type === "AssignmentPattern") {
  12254. this.checkDeclaration(node.left);
  12255. }
  12256. }
  12257. checkDuplicateExports(node, name) {
  12258. if (this.exportedIdentifiers.has(name)) {
  12259. this.raise(node.start, name === "default" ? ErrorMessages.DuplicateDefaultExport : ErrorMessages.DuplicateExport, name);
  12260. }
  12261. this.exportedIdentifiers.add(name);
  12262. }
  12263. parseExportSpecifiers(isInTypeExport) {
  12264. const nodes = [];
  12265. let first = true;
  12266. this.expect(5);
  12267. while (!this.eat(8)) {
  12268. if (first) {
  12269. first = false;
  12270. } else {
  12271. this.expect(12);
  12272. if (this.eat(8)) break;
  12273. }
  12274. const isMaybeTypeOnly = this.isContextual(122);
  12275. const isString = this.match(125);
  12276. const node = this.startNode();
  12277. node.local = this.parseModuleExportName();
  12278. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  12279. }
  12280. return nodes;
  12281. }
  12282. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  12283. if (this.eatContextual(89)) {
  12284. node.exported = this.parseModuleExportName();
  12285. } else if (isString) {
  12286. node.exported = cloneStringLiteral(node.local);
  12287. } else if (!node.exported) {
  12288. node.exported = cloneIdentifier(node.local);
  12289. }
  12290. return this.finishNode(node, "ExportSpecifier");
  12291. }
  12292. parseModuleExportName() {
  12293. if (this.match(125)) {
  12294. const result = this.parseStringLiteral(this.state.value);
  12295. const surrogate = result.value.match(loneSurrogate);
  12296. if (surrogate) {
  12297. this.raise(result.start, ErrorMessages.ModuleExportNameHasLoneSurrogate, surrogate[0].charCodeAt(0).toString(16));
  12298. }
  12299. return result;
  12300. }
  12301. return this.parseIdentifier(true);
  12302. }
  12303. parseImport(node) {
  12304. node.specifiers = [];
  12305. if (!this.match(125)) {
  12306. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  12307. const parseNext = !hasDefault || this.eat(12);
  12308. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  12309. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  12310. this.expectContextual(93);
  12311. }
  12312. node.source = this.parseImportSource();
  12313. const assertions = this.maybeParseImportAssertions();
  12314. if (assertions) {
  12315. node.assertions = assertions;
  12316. } else {
  12317. const attributes = this.maybeParseModuleAttributes();
  12318. if (attributes) {
  12319. node.attributes = attributes;
  12320. }
  12321. }
  12322. this.semicolon();
  12323. return this.finishNode(node, "ImportDeclaration");
  12324. }
  12325. parseImportSource() {
  12326. if (!this.match(125)) this.unexpected();
  12327. return this.parseExprAtom();
  12328. }
  12329. shouldParseDefaultImport(node) {
  12330. return tokenIsIdentifier(this.state.type);
  12331. }
  12332. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  12333. specifier.local = this.parseIdentifier();
  12334. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  12335. node.specifiers.push(this.finishNode(specifier, type));
  12336. }
  12337. parseAssertEntries() {
  12338. const attrs = [];
  12339. const attrNames = new Set();
  12340. do {
  12341. if (this.match(8)) {
  12342. break;
  12343. }
  12344. const node = this.startNode();
  12345. const keyName = this.state.value;
  12346. if (attrNames.has(keyName)) {
  12347. this.raise(this.state.start, ErrorMessages.ModuleAttributesWithDuplicateKeys, keyName);
  12348. }
  12349. attrNames.add(keyName);
  12350. if (this.match(125)) {
  12351. node.key = this.parseStringLiteral(keyName);
  12352. } else {
  12353. node.key = this.parseIdentifier(true);
  12354. }
  12355. this.expect(14);
  12356. if (!this.match(125)) {
  12357. throw this.unexpected(this.state.start, ErrorMessages.ModuleAttributeInvalidValue);
  12358. }
  12359. node.value = this.parseStringLiteral(this.state.value);
  12360. this.finishNode(node, "ImportAttribute");
  12361. attrs.push(node);
  12362. } while (this.eat(12));
  12363. return attrs;
  12364. }
  12365. maybeParseModuleAttributes() {
  12366. if (this.match(72) && !this.hasPrecedingLineBreak()) {
  12367. this.expectPlugin("moduleAttributes");
  12368. this.next();
  12369. } else {
  12370. if (this.hasPlugin("moduleAttributes")) return [];
  12371. return null;
  12372. }
  12373. const attrs = [];
  12374. const attributes = new Set();
  12375. do {
  12376. const node = this.startNode();
  12377. node.key = this.parseIdentifier(true);
  12378. if (node.key.name !== "type") {
  12379. this.raise(node.key.start, ErrorMessages.ModuleAttributeDifferentFromType, node.key.name);
  12380. }
  12381. if (attributes.has(node.key.name)) {
  12382. this.raise(node.key.start, ErrorMessages.ModuleAttributesWithDuplicateKeys, node.key.name);
  12383. }
  12384. attributes.add(node.key.name);
  12385. this.expect(14);
  12386. if (!this.match(125)) {
  12387. throw this.unexpected(this.state.start, ErrorMessages.ModuleAttributeInvalidValue);
  12388. }
  12389. node.value = this.parseStringLiteral(this.state.value);
  12390. this.finishNode(node, "ImportAttribute");
  12391. attrs.push(node);
  12392. } while (this.eat(12));
  12393. return attrs;
  12394. }
  12395. maybeParseImportAssertions() {
  12396. if (this.isContextual(90) && !this.hasPrecedingLineBreak()) {
  12397. this.expectPlugin("importAssertions");
  12398. this.next();
  12399. } else {
  12400. if (this.hasPlugin("importAssertions")) return [];
  12401. return null;
  12402. }
  12403. this.eat(5);
  12404. const attrs = this.parseAssertEntries();
  12405. this.eat(8);
  12406. return attrs;
  12407. }
  12408. maybeParseDefaultImportSpecifier(node) {
  12409. if (this.shouldParseDefaultImport(node)) {
  12410. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier", "default import specifier");
  12411. return true;
  12412. }
  12413. return false;
  12414. }
  12415. maybeParseStarImportSpecifier(node) {
  12416. if (this.match(51)) {
  12417. const specifier = this.startNode();
  12418. this.next();
  12419. this.expectContextual(89);
  12420. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier", "import namespace specifier");
  12421. return true;
  12422. }
  12423. return false;
  12424. }
  12425. parseNamedImportSpecifiers(node) {
  12426. let first = true;
  12427. this.expect(5);
  12428. while (!this.eat(8)) {
  12429. if (first) {
  12430. first = false;
  12431. } else {
  12432. if (this.eat(14)) {
  12433. throw this.raise(this.state.start, ErrorMessages.DestructureNamedImport);
  12434. }
  12435. this.expect(12);
  12436. if (this.eat(8)) break;
  12437. }
  12438. const specifier = this.startNode();
  12439. const importedIsString = this.match(125);
  12440. const isMaybeTypeOnly = this.isContextual(122);
  12441. specifier.imported = this.parseModuleExportName();
  12442. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly);
  12443. node.specifiers.push(importSpecifier);
  12444. }
  12445. }
  12446. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  12447. if (this.eatContextual(89)) {
  12448. specifier.local = this.parseIdentifier();
  12449. } else {
  12450. const {
  12451. imported
  12452. } = specifier;
  12453. if (importedIsString) {
  12454. throw this.raise(specifier.start, ErrorMessages.ImportBindingIsString, imported.value);
  12455. }
  12456. this.checkReservedWord(imported.name, specifier.start, true, true);
  12457. if (!specifier.local) {
  12458. specifier.local = cloneIdentifier(imported);
  12459. }
  12460. }
  12461. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  12462. return this.finishNode(specifier, "ImportSpecifier");
  12463. }
  12464. isThisParam(param) {
  12465. return param.type === "Identifier" && param.name === "this";
  12466. }
  12467. }
  12468. class Parser extends StatementParser {
  12469. constructor(options, input) {
  12470. options = getOptions(options);
  12471. super(options, input);
  12472. this.options = options;
  12473. this.initializeScopes();
  12474. this.plugins = pluginsMap(this.options.plugins);
  12475. this.filename = options.sourceFilename;
  12476. }
  12477. getScopeHandler() {
  12478. return ScopeHandler;
  12479. }
  12480. parse() {
  12481. this.enterInitialScopes();
  12482. const file = this.startNode();
  12483. const program = this.startNode();
  12484. this.nextToken();
  12485. file.errors = null;
  12486. this.parseTopLevel(file, program);
  12487. file.errors = this.state.errors;
  12488. return file;
  12489. }
  12490. }
  12491. function pluginsMap(plugins) {
  12492. const pluginMap = new Map();
  12493. for (const plugin of plugins) {
  12494. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  12495. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  12496. }
  12497. return pluginMap;
  12498. }
  12499. function parse(input, options) {
  12500. var _options;
  12501. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  12502. options = Object.assign({}, options);
  12503. try {
  12504. options.sourceType = "module";
  12505. const parser = getParser(options, input);
  12506. const ast = parser.parse();
  12507. if (parser.sawUnambiguousESM) {
  12508. return ast;
  12509. }
  12510. if (parser.ambiguousScriptDifferentAst) {
  12511. try {
  12512. options.sourceType = "script";
  12513. return getParser(options, input).parse();
  12514. } catch (_unused) {}
  12515. } else {
  12516. ast.program.sourceType = "script";
  12517. }
  12518. return ast;
  12519. } catch (moduleError) {
  12520. try {
  12521. options.sourceType = "script";
  12522. return getParser(options, input).parse();
  12523. } catch (_unused2) {}
  12524. throw moduleError;
  12525. }
  12526. } else {
  12527. return getParser(options, input).parse();
  12528. }
  12529. }
  12530. function parseExpression(input, options) {
  12531. const parser = getParser(options, input);
  12532. if (parser.options.strictMode) {
  12533. parser.state.strict = true;
  12534. }
  12535. return parser.getExpression();
  12536. }
  12537. function generateExportedTokenTypes(internalTokenTypes) {
  12538. const tokenTypes = {};
  12539. for (const typeName of Object.keys(internalTokenTypes)) {
  12540. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  12541. }
  12542. return tokenTypes;
  12543. }
  12544. const tokTypes = generateExportedTokenTypes(tt);
  12545. function getParser(options, input) {
  12546. let cls = Parser;
  12547. if (options != null && options.plugins) {
  12548. validatePlugins(options.plugins);
  12549. cls = getParserClass(options.plugins);
  12550. }
  12551. return new cls(options, input);
  12552. }
  12553. const parserClassCache = {};
  12554. function getParserClass(pluginsFromOptions) {
  12555. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  12556. const key = pluginList.join("/");
  12557. let cls = parserClassCache[key];
  12558. if (!cls) {
  12559. cls = Parser;
  12560. for (const plugin of pluginList) {
  12561. cls = mixinPlugins[plugin](cls);
  12562. }
  12563. parserClassCache[key] = cls;
  12564. }
  12565. return cls;
  12566. }
  12567. exports.parse = parse;
  12568. exports.parseExpression = parseExpression;
  12569. exports.tokTypes = tokTypes;
  12570. //# sourceMappingURL=index.js.map