index.js 329 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. const beforeExpr = true;
  4. const startsExpr = true;
  5. const isLoop = true;
  6. const isAssign = true;
  7. const prefix = true;
  8. const postfix = true;
  9. class TokenType {
  10. constructor(label, conf = {}) {
  11. this.label = label;
  12. this.keyword = conf.keyword;
  13. this.beforeExpr = !!conf.beforeExpr;
  14. this.startsExpr = !!conf.startsExpr;
  15. this.rightAssociative = !!conf.rightAssociative;
  16. this.isLoop = !!conf.isLoop;
  17. this.isAssign = !!conf.isAssign;
  18. this.prefix = !!conf.prefix;
  19. this.postfix = !!conf.postfix;
  20. this.binop = conf.binop != null ? conf.binop : null;
  21. this.updateContext = null;
  22. }
  23. }
  24. const keywords = new Map();
  25. function createKeyword(name, options = {}) {
  26. options.keyword = name;
  27. const token = new TokenType(name, options);
  28. keywords.set(name, token);
  29. return token;
  30. }
  31. function createBinop(name, binop) {
  32. return new TokenType(name, {
  33. beforeExpr,
  34. binop
  35. });
  36. }
  37. const types = {
  38. num: new TokenType("num", {
  39. startsExpr
  40. }),
  41. bigint: new TokenType("bigint", {
  42. startsExpr
  43. }),
  44. regexp: new TokenType("regexp", {
  45. startsExpr
  46. }),
  47. string: new TokenType("string", {
  48. startsExpr
  49. }),
  50. name: new TokenType("name", {
  51. startsExpr
  52. }),
  53. eof: new TokenType("eof"),
  54. bracketL: new TokenType("[", {
  55. beforeExpr,
  56. startsExpr
  57. }),
  58. bracketR: new TokenType("]"),
  59. braceL: new TokenType("{", {
  60. beforeExpr,
  61. startsExpr
  62. }),
  63. braceBarL: new TokenType("{|", {
  64. beforeExpr,
  65. startsExpr
  66. }),
  67. braceR: new TokenType("}"),
  68. braceBarR: new TokenType("|}"),
  69. parenL: new TokenType("(", {
  70. beforeExpr,
  71. startsExpr
  72. }),
  73. parenR: new TokenType(")"),
  74. comma: new TokenType(",", {
  75. beforeExpr
  76. }),
  77. semi: new TokenType(";", {
  78. beforeExpr
  79. }),
  80. colon: new TokenType(":", {
  81. beforeExpr
  82. }),
  83. doubleColon: new TokenType("::", {
  84. beforeExpr
  85. }),
  86. dot: new TokenType("."),
  87. question: new TokenType("?", {
  88. beforeExpr
  89. }),
  90. questionDot: new TokenType("?."),
  91. arrow: new TokenType("=>", {
  92. beforeExpr
  93. }),
  94. template: new TokenType("template"),
  95. ellipsis: new TokenType("...", {
  96. beforeExpr
  97. }),
  98. backQuote: new TokenType("`", {
  99. startsExpr
  100. }),
  101. dollarBraceL: new TokenType("${", {
  102. beforeExpr,
  103. startsExpr
  104. }),
  105. at: new TokenType("@"),
  106. hash: new TokenType("#", {
  107. startsExpr
  108. }),
  109. interpreterDirective: new TokenType("#!..."),
  110. eq: new TokenType("=", {
  111. beforeExpr,
  112. isAssign
  113. }),
  114. assign: new TokenType("_=", {
  115. beforeExpr,
  116. isAssign
  117. }),
  118. incDec: new TokenType("++/--", {
  119. prefix,
  120. postfix,
  121. startsExpr
  122. }),
  123. bang: new TokenType("!", {
  124. beforeExpr,
  125. prefix,
  126. startsExpr
  127. }),
  128. tilde: new TokenType("~", {
  129. beforeExpr,
  130. prefix,
  131. startsExpr
  132. }),
  133. pipeline: createBinop("|>", 0),
  134. nullishCoalescing: createBinop("??", 1),
  135. logicalOR: createBinop("||", 1),
  136. logicalAND: createBinop("&&", 2),
  137. bitwiseOR: createBinop("|", 3),
  138. bitwiseXOR: createBinop("^", 4),
  139. bitwiseAND: createBinop("&", 5),
  140. equality: createBinop("==/!=/===/!==", 6),
  141. relational: createBinop("</>/<=/>=", 7),
  142. bitShift: createBinop("<</>>/>>>", 8),
  143. plusMin: new TokenType("+/-", {
  144. beforeExpr,
  145. binop: 9,
  146. prefix,
  147. startsExpr
  148. }),
  149. modulo: createBinop("%", 10),
  150. star: createBinop("*", 10),
  151. slash: createBinop("/", 10),
  152. exponent: new TokenType("**", {
  153. beforeExpr,
  154. binop: 11,
  155. rightAssociative: true
  156. }),
  157. _break: createKeyword("break"),
  158. _case: createKeyword("case", {
  159. beforeExpr
  160. }),
  161. _catch: createKeyword("catch"),
  162. _continue: createKeyword("continue"),
  163. _debugger: createKeyword("debugger"),
  164. _default: createKeyword("default", {
  165. beforeExpr
  166. }),
  167. _do: createKeyword("do", {
  168. isLoop,
  169. beforeExpr
  170. }),
  171. _else: createKeyword("else", {
  172. beforeExpr
  173. }),
  174. _finally: createKeyword("finally"),
  175. _for: createKeyword("for", {
  176. isLoop
  177. }),
  178. _function: createKeyword("function", {
  179. startsExpr
  180. }),
  181. _if: createKeyword("if"),
  182. _return: createKeyword("return", {
  183. beforeExpr
  184. }),
  185. _switch: createKeyword("switch"),
  186. _throw: createKeyword("throw", {
  187. beforeExpr,
  188. prefix,
  189. startsExpr
  190. }),
  191. _try: createKeyword("try"),
  192. _var: createKeyword("var"),
  193. _const: createKeyword("const"),
  194. _while: createKeyword("while", {
  195. isLoop
  196. }),
  197. _with: createKeyword("with"),
  198. _new: createKeyword("new", {
  199. beforeExpr,
  200. startsExpr
  201. }),
  202. _this: createKeyword("this", {
  203. startsExpr
  204. }),
  205. _super: createKeyword("super", {
  206. startsExpr
  207. }),
  208. _class: createKeyword("class", {
  209. startsExpr
  210. }),
  211. _extends: createKeyword("extends", {
  212. beforeExpr
  213. }),
  214. _export: createKeyword("export"),
  215. _import: createKeyword("import", {
  216. startsExpr
  217. }),
  218. _null: createKeyword("null", {
  219. startsExpr
  220. }),
  221. _true: createKeyword("true", {
  222. startsExpr
  223. }),
  224. _false: createKeyword("false", {
  225. startsExpr
  226. }),
  227. _in: createKeyword("in", {
  228. beforeExpr,
  229. binop: 7
  230. }),
  231. _instanceof: createKeyword("instanceof", {
  232. beforeExpr,
  233. binop: 7
  234. }),
  235. _typeof: createKeyword("typeof", {
  236. beforeExpr,
  237. prefix,
  238. startsExpr
  239. }),
  240. _void: createKeyword("void", {
  241. beforeExpr,
  242. prefix,
  243. startsExpr
  244. }),
  245. _delete: createKeyword("delete", {
  246. beforeExpr,
  247. prefix,
  248. startsExpr
  249. })
  250. };
  251. const SCOPE_OTHER = 0b000000000,
  252. SCOPE_PROGRAM = 0b000000001,
  253. SCOPE_FUNCTION = 0b000000010,
  254. SCOPE_ASYNC = 0b000000100,
  255. SCOPE_GENERATOR = 0b000001000,
  256. SCOPE_ARROW = 0b000010000,
  257. SCOPE_SIMPLE_CATCH = 0b000100000,
  258. SCOPE_SUPER = 0b001000000,
  259. SCOPE_DIRECT_SUPER = 0b010000000,
  260. SCOPE_CLASS = 0b100000000,
  261. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION;
  262. function functionFlags(isAsync, isGenerator) {
  263. return SCOPE_FUNCTION | (isAsync ? SCOPE_ASYNC : 0) | (isGenerator ? SCOPE_GENERATOR : 0);
  264. }
  265. const BIND_KIND_VALUE = 0b00000000001,
  266. BIND_KIND_TYPE = 0b00000000010,
  267. BIND_SCOPE_VAR = 0b00000000100,
  268. BIND_SCOPE_LEXICAL = 0b00000001000,
  269. BIND_SCOPE_FUNCTION = 0b00000010000,
  270. BIND_FLAGS_NONE = 0b00001000000,
  271. BIND_FLAGS_CLASS = 0b00010000000,
  272. BIND_FLAGS_TS_ENUM = 0b00100000000,
  273. BIND_FLAGS_TS_CONST_ENUM = 0b01000000000,
  274. BIND_FLAGS_TS_EXPORT_ONLY = 0b10000000000;
  275. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  276. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  277. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  278. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  279. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  280. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  281. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  282. BIND_TS_FN_TYPE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  283. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  284. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  285. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  286. BIND_TS_NAMESPACE = BIND_TS_FN_TYPE;
  287. function isSimpleProperty(node) {
  288. return node != null && node.type === "Property" && node.kind === "init" && node.method === false;
  289. }
  290. var estree = (superClass => class extends superClass {
  291. estreeParseRegExpLiteral({
  292. pattern,
  293. flags
  294. }) {
  295. let regex = null;
  296. try {
  297. regex = new RegExp(pattern, flags);
  298. } catch (e) {}
  299. const node = this.estreeParseLiteral(regex);
  300. node.regex = {
  301. pattern,
  302. flags
  303. };
  304. return node;
  305. }
  306. estreeParseLiteral(value) {
  307. return this.parseLiteral(value, "Literal");
  308. }
  309. directiveToStmt(directive) {
  310. const directiveLiteral = directive.value;
  311. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  312. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  313. expression.value = directiveLiteral.value;
  314. expression.raw = directiveLiteral.extra.raw;
  315. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
  316. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  317. return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
  318. }
  319. initFunction(node, isAsync) {
  320. super.initFunction(node, isAsync);
  321. node.expression = false;
  322. }
  323. checkDeclaration(node) {
  324. if (isSimpleProperty(node)) {
  325. this.checkDeclaration(node.value);
  326. } else {
  327. super.checkDeclaration(node);
  328. }
  329. }
  330. checkGetterSetterParams(method) {
  331. const prop = method;
  332. const paramCount = prop.kind === "get" ? 0 : 1;
  333. const start = prop.start;
  334. if (prop.value.params.length !== paramCount) {
  335. if (prop.kind === "get") {
  336. this.raise(start, "getter must not have any formal parameters");
  337. } else {
  338. this.raise(start, "setter must have exactly one formal parameter");
  339. }
  340. }
  341. if (prop.kind === "set" && prop.value.params[0].type === "RestElement") {
  342. this.raise(start, "setter function argument must not be a rest parameter");
  343. }
  344. }
  345. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  346. switch (expr.type) {
  347. case "ObjectPattern":
  348. expr.properties.forEach(prop => {
  349. this.checkLVal(prop.type === "Property" ? prop.value : prop, bindingType, checkClashes, "object destructuring pattern");
  350. });
  351. break;
  352. default:
  353. super.checkLVal(expr, bindingType, checkClashes, contextDescription);
  354. }
  355. }
  356. checkPropClash(prop, propHash) {
  357. if (prop.type === "SpreadElement" || prop.computed || prop.method || prop.shorthand) {
  358. return;
  359. }
  360. const key = prop.key;
  361. const name = key.type === "Identifier" ? key.name : String(key.value);
  362. if (name === "__proto__" && prop.kind === "init") {
  363. if (propHash.proto) {
  364. this.raise(key.start, "Redefinition of __proto__ property");
  365. }
  366. propHash.proto = true;
  367. }
  368. }
  369. isStrictBody(node) {
  370. const isBlockStatement = node.body.type === "BlockStatement";
  371. if (isBlockStatement && node.body.body.length > 0) {
  372. for (let _i = 0, _node$body$body = node.body.body; _i < _node$body$body.length; _i++) {
  373. const directive = _node$body$body[_i];
  374. if (directive.type === "ExpressionStatement" && directive.expression.type === "Literal") {
  375. if (directive.expression.value === "use strict") return true;
  376. } else {
  377. break;
  378. }
  379. }
  380. }
  381. return false;
  382. }
  383. isValidDirective(stmt) {
  384. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && (!stmt.expression.extra || !stmt.expression.extra.parenthesized);
  385. }
  386. stmtToDirective(stmt) {
  387. const directive = super.stmtToDirective(stmt);
  388. const value = stmt.expression.value;
  389. directive.value.value = value;
  390. return directive;
  391. }
  392. parseBlockBody(node, allowDirectives, topLevel, end) {
  393. super.parseBlockBody(node, allowDirectives, topLevel, end);
  394. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  395. node.body = directiveStatements.concat(node.body);
  396. delete node.directives;
  397. }
  398. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  399. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  400. if (method.typeParameters) {
  401. method.value.typeParameters = method.typeParameters;
  402. delete method.typeParameters;
  403. }
  404. classBody.body.push(method);
  405. }
  406. parseExprAtom(refShorthandDefaultPos) {
  407. switch (this.state.type) {
  408. case types.regexp:
  409. return this.estreeParseRegExpLiteral(this.state.value);
  410. case types.num:
  411. case types.string:
  412. return this.estreeParseLiteral(this.state.value);
  413. case types._null:
  414. return this.estreeParseLiteral(null);
  415. case types._true:
  416. return this.estreeParseLiteral(true);
  417. case types._false:
  418. return this.estreeParseLiteral(false);
  419. default:
  420. return super.parseExprAtom(refShorthandDefaultPos);
  421. }
  422. }
  423. parseLiteral(value, type, startPos, startLoc) {
  424. const node = super.parseLiteral(value, type, startPos, startLoc);
  425. node.raw = node.extra.raw;
  426. delete node.extra;
  427. return node;
  428. }
  429. parseFunctionBody(node, allowExpression, isMethod = false) {
  430. super.parseFunctionBody(node, allowExpression, isMethod);
  431. node.expression = node.body.type !== "BlockStatement";
  432. }
  433. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  434. let funcNode = this.startNode();
  435. funcNode.kind = node.kind;
  436. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  437. funcNode.type = "FunctionExpression";
  438. delete funcNode.kind;
  439. node.value = funcNode;
  440. type = type === "ClassMethod" ? "MethodDefinition" : type;
  441. return this.finishNode(node, type);
  442. }
  443. parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) {
  444. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc);
  445. if (node) {
  446. node.type = "Property";
  447. if (node.kind === "method") node.kind = "init";
  448. node.shorthand = false;
  449. }
  450. return node;
  451. }
  452. parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos) {
  453. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos);
  454. if (node) {
  455. node.kind = "init";
  456. node.type = "Property";
  457. }
  458. return node;
  459. }
  460. toAssignable(node, isBinding, contextDescription) {
  461. if (isSimpleProperty(node)) {
  462. this.toAssignable(node.value, isBinding, contextDescription);
  463. return node;
  464. }
  465. return super.toAssignable(node, isBinding, contextDescription);
  466. }
  467. toAssignableObjectExpressionProp(prop, isBinding, isLast) {
  468. if (prop.kind === "get" || prop.kind === "set") {
  469. this.raise(prop.key.start, "Object pattern can't contain getter or setter");
  470. } else if (prop.method) {
  471. this.raise(prop.key.start, "Object pattern can't contain methods");
  472. } else {
  473. super.toAssignableObjectExpressionProp(prop, isBinding, isLast);
  474. }
  475. }
  476. });
  477. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  478. const lineBreakG = new RegExp(lineBreak.source, "g");
  479. function isNewLine(code) {
  480. switch (code) {
  481. case 10:
  482. case 13:
  483. case 8232:
  484. case 8233:
  485. return true;
  486. default:
  487. return false;
  488. }
  489. }
  490. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  491. function isWhitespace(code) {
  492. switch (code) {
  493. case 0x0009:
  494. case 0x000b:
  495. case 0x000c:
  496. case 32:
  497. case 160:
  498. case 5760:
  499. case 0x2000:
  500. case 0x2001:
  501. case 0x2002:
  502. case 0x2003:
  503. case 0x2004:
  504. case 0x2005:
  505. case 0x2006:
  506. case 0x2007:
  507. case 0x2008:
  508. case 0x2009:
  509. case 0x200a:
  510. case 0x202f:
  511. case 0x205f:
  512. case 0x3000:
  513. case 0xfeff:
  514. return true;
  515. default:
  516. return false;
  517. }
  518. }
  519. class TokContext {
  520. constructor(token, isExpr, preserveSpace, override) {
  521. this.token = token;
  522. this.isExpr = !!isExpr;
  523. this.preserveSpace = !!preserveSpace;
  524. this.override = override;
  525. }
  526. }
  527. const types$1 = {
  528. braceStatement: new TokContext("{", false),
  529. braceExpression: new TokContext("{", true),
  530. templateQuasi: new TokContext("${", false),
  531. parenStatement: new TokContext("(", false),
  532. parenExpression: new TokContext("(", true),
  533. template: new TokContext("`", true, true, p => p.readTmplToken()),
  534. functionExpression: new TokContext("function", true),
  535. functionStatement: new TokContext("function", false)
  536. };
  537. types.parenR.updateContext = types.braceR.updateContext = function () {
  538. if (this.state.context.length === 1) {
  539. this.state.exprAllowed = true;
  540. return;
  541. }
  542. let out = this.state.context.pop();
  543. if (out === types$1.braceStatement && this.curContext().token === "function") {
  544. out = this.state.context.pop();
  545. }
  546. this.state.exprAllowed = !out.isExpr;
  547. };
  548. types.name.updateContext = function (prevType) {
  549. let allowed = false;
  550. if (prevType !== types.dot) {
  551. if (this.state.value === "of" && !this.state.exprAllowed || this.state.value === "yield" && this.scope.inGenerator) {
  552. allowed = true;
  553. }
  554. }
  555. this.state.exprAllowed = allowed;
  556. if (this.state.isIterator) {
  557. this.state.isIterator = false;
  558. }
  559. };
  560. types.braceL.updateContext = function (prevType) {
  561. this.state.context.push(this.braceIsBlock(prevType) ? types$1.braceStatement : types$1.braceExpression);
  562. this.state.exprAllowed = true;
  563. };
  564. types.dollarBraceL.updateContext = function () {
  565. this.state.context.push(types$1.templateQuasi);
  566. this.state.exprAllowed = true;
  567. };
  568. types.parenL.updateContext = function (prevType) {
  569. const statementParens = prevType === types._if || prevType === types._for || prevType === types._with || prevType === types._while;
  570. this.state.context.push(statementParens ? types$1.parenStatement : types$1.parenExpression);
  571. this.state.exprAllowed = true;
  572. };
  573. types.incDec.updateContext = function () {};
  574. types._function.updateContext = types._class.updateContext = function (prevType) {
  575. if (prevType.beforeExpr && prevType !== types.semi && prevType !== types._else && !(prevType === types._return && lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start))) && !((prevType === types.colon || prevType === types.braceL) && this.curContext() === types$1.b_stat)) {
  576. this.state.context.push(types$1.functionExpression);
  577. } else {
  578. this.state.context.push(types$1.functionStatement);
  579. }
  580. this.state.exprAllowed = false;
  581. };
  582. types.backQuote.updateContext = function () {
  583. if (this.curContext() === types$1.template) {
  584. this.state.context.pop();
  585. } else {
  586. this.state.context.push(types$1.template);
  587. }
  588. this.state.exprAllowed = false;
  589. };
  590. const reservedWords = {
  591. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  592. strictBind: ["eval", "arguments"]
  593. };
  594. const reservedWordsStrictSet = new Set(reservedWords.strict);
  595. const reservedWordsStrictBindSet = new Set(reservedWords.strict.concat(reservedWords.strictBind));
  596. const isReservedWord = (word, inModule) => {
  597. return inModule && word === "await" || word === "enum";
  598. };
  599. function isStrictReservedWord(word, inModule) {
  600. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  601. }
  602. function isStrictBindReservedWord(word, inModule) {
  603. return isReservedWord(word, inModule) || reservedWordsStrictBindSet.has(word);
  604. }
  605. function isKeyword(word) {
  606. return keywords.has(word);
  607. }
  608. const keywordRelationalOperator = /^in(stanceof)?$/;
  609. let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u08a0-\u08b4\u08b6-\u08bd\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fef\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7bf\ua7c2-\ua7c6\ua7f7-\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-\uab67\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";
  610. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08d3-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1df9\u1dfb-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\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";
  611. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  612. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  613. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  614. const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 157, 310, 10, 21, 11, 7, 153, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 477, 28, 11, 0, 9, 21, 155, 22, 13, 52, 76, 44, 33, 24, 27, 35, 30, 0, 12, 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, 0, 33, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 230, 43, 117, 63, 32, 0, 161, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 35, 56, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 270, 921, 103, 110, 18, 195, 2749, 1070, 4050, 582, 8634, 568, 8, 30, 114, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 754, 9486, 286, 50, 2, 18, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 2357, 44, 11, 6, 17, 0, 370, 43, 1301, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42710, 42, 4148, 12, 221, 3, 5761, 15, 7472, 3104, 541];
  615. 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, 525, 10, 176, 2, 54, 14, 32, 9, 16, 3, 46, 10, 54, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 4, 9, 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, 232, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 19306, 9, 135, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 19723, 1, 5319, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 262, 6, 10, 9, 419, 13, 1495, 6, 110, 6, 6, 9, 792487, 239];
  616. function isInAstralSet(code, set) {
  617. let pos = 0x10000;
  618. for (let i = 0, length = set.length; i < length; i += 2) {
  619. pos += set[i];
  620. if (pos > code) return false;
  621. pos += set[i + 1];
  622. if (pos >= code) return true;
  623. }
  624. return false;
  625. }
  626. function isIdentifierStart(code) {
  627. if (code < 65) return code === 36;
  628. if (code <= 90) return true;
  629. if (code < 97) return code === 95;
  630. if (code <= 122) return true;
  631. if (code <= 0xffff) {
  632. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  633. }
  634. return isInAstralSet(code, astralIdentifierStartCodes);
  635. }
  636. function isIteratorStart(current, next) {
  637. return current === 64 && next === 64;
  638. }
  639. function isIdentifierChar(code) {
  640. if (code < 48) return code === 36;
  641. if (code < 58) return true;
  642. if (code < 65) return false;
  643. if (code <= 90) return true;
  644. if (code < 97) return code === 95;
  645. if (code <= 122) return true;
  646. if (code <= 0xffff) {
  647. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  648. }
  649. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  650. }
  651. const reservedTypes = ["any", "bool", "boolean", "empty", "false", "mixed", "null", "number", "static", "string", "true", "typeof", "void", "interface", "extends", "_"];
  652. function isEsModuleType(bodyElement) {
  653. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  654. }
  655. function hasTypeImportKind(node) {
  656. return node.importKind === "type" || node.importKind === "typeof";
  657. }
  658. function isMaybeDefaultImport(state) {
  659. return (state.type === types.name || !!state.type.keyword) && state.value !== "from";
  660. }
  661. const exportSuggestions = {
  662. const: "declare export var",
  663. let: "declare export var",
  664. type: "export type",
  665. interface: "export interface"
  666. };
  667. function partition(list, test) {
  668. const list1 = [];
  669. const list2 = [];
  670. for (let i = 0; i < list.length; i++) {
  671. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  672. }
  673. return [list1, list2];
  674. }
  675. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  676. var flow = (superClass => class extends superClass {
  677. constructor(options, input) {
  678. super(options, input);
  679. this.flowPragma = undefined;
  680. }
  681. shouldParseTypes() {
  682. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  683. }
  684. finishToken(type, val) {
  685. if (type !== types.string && type !== types.semi && type !== types.interpreterDirective) {
  686. if (this.flowPragma === undefined) {
  687. this.flowPragma = null;
  688. }
  689. }
  690. return super.finishToken(type, val);
  691. }
  692. addComment(comment) {
  693. if (this.flowPragma === undefined) {
  694. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  695. if (!matches) ; else if (matches[1] === "flow") {
  696. this.flowPragma = "flow";
  697. } else if (matches[1] === "noflow") {
  698. this.flowPragma = "noflow";
  699. } else {
  700. throw new Error("Unexpected flow pragma");
  701. }
  702. }
  703. return super.addComment(comment);
  704. }
  705. flowParseTypeInitialiser(tok) {
  706. const oldInType = this.state.inType;
  707. this.state.inType = true;
  708. this.expect(tok || types.colon);
  709. const type = this.flowParseType();
  710. this.state.inType = oldInType;
  711. return type;
  712. }
  713. flowParsePredicate() {
  714. const node = this.startNode();
  715. const moduloLoc = this.state.startLoc;
  716. const moduloPos = this.state.start;
  717. this.expect(types.modulo);
  718. const checksLoc = this.state.startLoc;
  719. this.expectContextual("checks");
  720. if (moduloLoc.line !== checksLoc.line || moduloLoc.column !== checksLoc.column - 1) {
  721. this.raise(moduloPos, "Spaces between ´%´ and ´checks´ are not allowed here.");
  722. }
  723. if (this.eat(types.parenL)) {
  724. node.value = this.parseExpression();
  725. this.expect(types.parenR);
  726. return this.finishNode(node, "DeclaredPredicate");
  727. } else {
  728. return this.finishNode(node, "InferredPredicate");
  729. }
  730. }
  731. flowParseTypeAndPredicateInitialiser() {
  732. const oldInType = this.state.inType;
  733. this.state.inType = true;
  734. this.expect(types.colon);
  735. let type = null;
  736. let predicate = null;
  737. if (this.match(types.modulo)) {
  738. this.state.inType = oldInType;
  739. predicate = this.flowParsePredicate();
  740. } else {
  741. type = this.flowParseType();
  742. this.state.inType = oldInType;
  743. if (this.match(types.modulo)) {
  744. predicate = this.flowParsePredicate();
  745. }
  746. }
  747. return [type, predicate];
  748. }
  749. flowParseDeclareClass(node) {
  750. this.next();
  751. this.flowParseInterfaceish(node, true);
  752. return this.finishNode(node, "DeclareClass");
  753. }
  754. flowParseDeclareFunction(node) {
  755. this.next();
  756. const id = node.id = this.parseIdentifier();
  757. const typeNode = this.startNode();
  758. const typeContainer = this.startNode();
  759. if (this.isRelational("<")) {
  760. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  761. } else {
  762. typeNode.typeParameters = null;
  763. }
  764. this.expect(types.parenL);
  765. const tmp = this.flowParseFunctionTypeParams();
  766. typeNode.params = tmp.params;
  767. typeNode.rest = tmp.rest;
  768. this.expect(types.parenR);
  769. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  770. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  771. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  772. this.resetEndLocation(id);
  773. this.semicolon();
  774. return this.finishNode(node, "DeclareFunction");
  775. }
  776. flowParseDeclare(node, insideModule) {
  777. if (this.match(types._class)) {
  778. return this.flowParseDeclareClass(node);
  779. } else if (this.match(types._function)) {
  780. return this.flowParseDeclareFunction(node);
  781. } else if (this.match(types._var)) {
  782. return this.flowParseDeclareVariable(node);
  783. } else if (this.eatContextual("module")) {
  784. if (this.match(types.dot)) {
  785. return this.flowParseDeclareModuleExports(node);
  786. } else {
  787. if (insideModule) {
  788. this.unexpected(this.state.lastTokStart, "`declare module` cannot be used inside another `declare module`");
  789. }
  790. return this.flowParseDeclareModule(node);
  791. }
  792. } else if (this.isContextual("type")) {
  793. return this.flowParseDeclareTypeAlias(node);
  794. } else if (this.isContextual("opaque")) {
  795. return this.flowParseDeclareOpaqueType(node);
  796. } else if (this.isContextual("interface")) {
  797. return this.flowParseDeclareInterface(node);
  798. } else if (this.match(types._export)) {
  799. return this.flowParseDeclareExportDeclaration(node, insideModule);
  800. } else {
  801. throw this.unexpected();
  802. }
  803. }
  804. flowParseDeclareVariable(node) {
  805. this.next();
  806. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  807. this.semicolon();
  808. return this.finishNode(node, "DeclareVariable");
  809. }
  810. flowParseDeclareModule(node) {
  811. this.scope.enter(SCOPE_OTHER);
  812. if (this.match(types.string)) {
  813. node.id = this.parseExprAtom();
  814. } else {
  815. node.id = this.parseIdentifier();
  816. }
  817. const bodyNode = node.body = this.startNode();
  818. const body = bodyNode.body = [];
  819. this.expect(types.braceL);
  820. while (!this.match(types.braceR)) {
  821. let bodyNode = this.startNode();
  822. if (this.match(types._import)) {
  823. this.next();
  824. if (!this.isContextual("type") && !this.match(types._typeof)) {
  825. this.unexpected(this.state.lastTokStart, "Imports within a `declare module` body must always be `import type` or `import typeof`");
  826. }
  827. this.parseImport(bodyNode);
  828. } else {
  829. this.expectContextual("declare", "Only declares and type imports are allowed inside declare module");
  830. bodyNode = this.flowParseDeclare(bodyNode, true);
  831. }
  832. body.push(bodyNode);
  833. }
  834. this.scope.exit();
  835. this.expect(types.braceR);
  836. this.finishNode(bodyNode, "BlockStatement");
  837. let kind = null;
  838. let hasModuleExport = false;
  839. const errorMessage = "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";
  840. body.forEach(bodyElement => {
  841. if (isEsModuleType(bodyElement)) {
  842. if (kind === "CommonJS") {
  843. this.unexpected(bodyElement.start, errorMessage);
  844. }
  845. kind = "ES";
  846. } else if (bodyElement.type === "DeclareModuleExports") {
  847. if (hasModuleExport) {
  848. this.unexpected(bodyElement.start, "Duplicate `declare module.exports` statement");
  849. }
  850. if (kind === "ES") this.unexpected(bodyElement.start, errorMessage);
  851. kind = "CommonJS";
  852. hasModuleExport = true;
  853. }
  854. });
  855. node.kind = kind || "CommonJS";
  856. return this.finishNode(node, "DeclareModule");
  857. }
  858. flowParseDeclareExportDeclaration(node, insideModule) {
  859. this.expect(types._export);
  860. if (this.eat(types._default)) {
  861. if (this.match(types._function) || this.match(types._class)) {
  862. node.declaration = this.flowParseDeclare(this.startNode());
  863. } else {
  864. node.declaration = this.flowParseType();
  865. this.semicolon();
  866. }
  867. node.default = true;
  868. return this.finishNode(node, "DeclareExportDeclaration");
  869. } else {
  870. if (this.match(types._const) || this.isLet() || (this.isContextual("type") || this.isContextual("interface")) && !insideModule) {
  871. const label = this.state.value;
  872. const suggestion = exportSuggestions[label];
  873. this.unexpected(this.state.start, `\`declare export ${label}\` is not supported. Use \`${suggestion}\` instead`);
  874. }
  875. if (this.match(types._var) || this.match(types._function) || this.match(types._class) || this.isContextual("opaque")) {
  876. node.declaration = this.flowParseDeclare(this.startNode());
  877. node.default = false;
  878. return this.finishNode(node, "DeclareExportDeclaration");
  879. } else if (this.match(types.star) || this.match(types.braceL) || this.isContextual("interface") || this.isContextual("type") || this.isContextual("opaque")) {
  880. node = this.parseExport(node);
  881. if (node.type === "ExportNamedDeclaration") {
  882. node.type = "ExportDeclaration";
  883. node.default = false;
  884. delete node.exportKind;
  885. }
  886. node.type = "Declare" + node.type;
  887. return node;
  888. }
  889. }
  890. throw this.unexpected();
  891. }
  892. flowParseDeclareModuleExports(node) {
  893. this.next();
  894. this.expectContextual("exports");
  895. node.typeAnnotation = this.flowParseTypeAnnotation();
  896. this.semicolon();
  897. return this.finishNode(node, "DeclareModuleExports");
  898. }
  899. flowParseDeclareTypeAlias(node) {
  900. this.next();
  901. this.flowParseTypeAlias(node);
  902. node.type = "DeclareTypeAlias";
  903. return node;
  904. }
  905. flowParseDeclareOpaqueType(node) {
  906. this.next();
  907. this.flowParseOpaqueType(node, true);
  908. node.type = "DeclareOpaqueType";
  909. return node;
  910. }
  911. flowParseDeclareInterface(node) {
  912. this.next();
  913. this.flowParseInterfaceish(node);
  914. return this.finishNode(node, "DeclareInterface");
  915. }
  916. flowParseInterfaceish(node, isClass = false) {
  917. node.id = this.flowParseRestrictedIdentifier(!isClass);
  918. if (this.isRelational("<")) {
  919. node.typeParameters = this.flowParseTypeParameterDeclaration();
  920. } else {
  921. node.typeParameters = null;
  922. }
  923. node.extends = [];
  924. node.implements = [];
  925. node.mixins = [];
  926. if (this.eat(types._extends)) {
  927. do {
  928. node.extends.push(this.flowParseInterfaceExtends());
  929. } while (!isClass && this.eat(types.comma));
  930. }
  931. if (this.isContextual("mixins")) {
  932. this.next();
  933. do {
  934. node.mixins.push(this.flowParseInterfaceExtends());
  935. } while (this.eat(types.comma));
  936. }
  937. if (this.isContextual("implements")) {
  938. this.next();
  939. do {
  940. node.implements.push(this.flowParseInterfaceExtends());
  941. } while (this.eat(types.comma));
  942. }
  943. node.body = this.flowParseObjectType({
  944. allowStatic: isClass,
  945. allowExact: false,
  946. allowSpread: false,
  947. allowProto: isClass,
  948. allowInexact: false
  949. });
  950. }
  951. flowParseInterfaceExtends() {
  952. const node = this.startNode();
  953. node.id = this.flowParseQualifiedTypeIdentifier();
  954. if (this.isRelational("<")) {
  955. node.typeParameters = this.flowParseTypeParameterInstantiation();
  956. } else {
  957. node.typeParameters = null;
  958. }
  959. return this.finishNode(node, "InterfaceExtends");
  960. }
  961. flowParseInterface(node) {
  962. this.flowParseInterfaceish(node);
  963. return this.finishNode(node, "InterfaceDeclaration");
  964. }
  965. checkNotUnderscore(word) {
  966. if (word === "_") {
  967. throw this.unexpected(null, "`_` is only allowed as a type argument to call or new");
  968. }
  969. }
  970. checkReservedType(word, startLoc) {
  971. if (reservedTypes.indexOf(word) > -1) {
  972. this.raise(startLoc, `Cannot overwrite reserved type ${word}`);
  973. }
  974. }
  975. flowParseRestrictedIdentifier(liberal) {
  976. this.checkReservedType(this.state.value, this.state.start);
  977. return this.parseIdentifier(liberal);
  978. }
  979. flowParseTypeAlias(node) {
  980. node.id = this.flowParseRestrictedIdentifier();
  981. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  982. if (this.isRelational("<")) {
  983. node.typeParameters = this.flowParseTypeParameterDeclaration();
  984. } else {
  985. node.typeParameters = null;
  986. }
  987. node.right = this.flowParseTypeInitialiser(types.eq);
  988. this.semicolon();
  989. return this.finishNode(node, "TypeAlias");
  990. }
  991. flowParseOpaqueType(node, declare) {
  992. this.expectContextual("type");
  993. node.id = this.flowParseRestrictedIdentifier(true);
  994. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  995. if (this.isRelational("<")) {
  996. node.typeParameters = this.flowParseTypeParameterDeclaration();
  997. } else {
  998. node.typeParameters = null;
  999. }
  1000. node.supertype = null;
  1001. if (this.match(types.colon)) {
  1002. node.supertype = this.flowParseTypeInitialiser(types.colon);
  1003. }
  1004. node.impltype = null;
  1005. if (!declare) {
  1006. node.impltype = this.flowParseTypeInitialiser(types.eq);
  1007. }
  1008. this.semicolon();
  1009. return this.finishNode(node, "OpaqueType");
  1010. }
  1011. flowParseTypeParameter(requireDefault = false) {
  1012. const nodeStart = this.state.start;
  1013. const node = this.startNode();
  1014. const variance = this.flowParseVariance();
  1015. const ident = this.flowParseTypeAnnotatableIdentifier();
  1016. node.name = ident.name;
  1017. node.variance = variance;
  1018. node.bound = ident.typeAnnotation;
  1019. if (this.match(types.eq)) {
  1020. this.eat(types.eq);
  1021. node.default = this.flowParseType();
  1022. } else {
  1023. if (requireDefault) {
  1024. this.unexpected(nodeStart, "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.");
  1025. }
  1026. }
  1027. return this.finishNode(node, "TypeParameter");
  1028. }
  1029. flowParseTypeParameterDeclaration() {
  1030. const oldInType = this.state.inType;
  1031. const node = this.startNode();
  1032. node.params = [];
  1033. this.state.inType = true;
  1034. if (this.isRelational("<") || this.match(types.jsxTagStart)) {
  1035. this.next();
  1036. } else {
  1037. this.unexpected();
  1038. }
  1039. let defaultRequired = false;
  1040. do {
  1041. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  1042. node.params.push(typeParameter);
  1043. if (typeParameter.default) {
  1044. defaultRequired = true;
  1045. }
  1046. if (!this.isRelational(">")) {
  1047. this.expect(types.comma);
  1048. }
  1049. } while (!this.isRelational(">"));
  1050. this.expectRelational(">");
  1051. this.state.inType = oldInType;
  1052. return this.finishNode(node, "TypeParameterDeclaration");
  1053. }
  1054. flowParseTypeParameterInstantiation() {
  1055. const node = this.startNode();
  1056. const oldInType = this.state.inType;
  1057. node.params = [];
  1058. this.state.inType = true;
  1059. this.expectRelational("<");
  1060. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1061. this.state.noAnonFunctionType = false;
  1062. while (!this.isRelational(">")) {
  1063. node.params.push(this.flowParseType());
  1064. if (!this.isRelational(">")) {
  1065. this.expect(types.comma);
  1066. }
  1067. }
  1068. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1069. this.expectRelational(">");
  1070. this.state.inType = oldInType;
  1071. return this.finishNode(node, "TypeParameterInstantiation");
  1072. }
  1073. flowParseTypeParameterInstantiationCallOrNew() {
  1074. const node = this.startNode();
  1075. const oldInType = this.state.inType;
  1076. node.params = [];
  1077. this.state.inType = true;
  1078. this.expectRelational("<");
  1079. while (!this.isRelational(">")) {
  1080. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  1081. if (!this.isRelational(">")) {
  1082. this.expect(types.comma);
  1083. }
  1084. }
  1085. this.expectRelational(">");
  1086. this.state.inType = oldInType;
  1087. return this.finishNode(node, "TypeParameterInstantiation");
  1088. }
  1089. flowParseInterfaceType() {
  1090. const node = this.startNode();
  1091. this.expectContextual("interface");
  1092. node.extends = [];
  1093. if (this.eat(types._extends)) {
  1094. do {
  1095. node.extends.push(this.flowParseInterfaceExtends());
  1096. } while (this.eat(types.comma));
  1097. }
  1098. node.body = this.flowParseObjectType({
  1099. allowStatic: false,
  1100. allowExact: false,
  1101. allowSpread: false,
  1102. allowProto: false,
  1103. allowInexact: false
  1104. });
  1105. return this.finishNode(node, "InterfaceTypeAnnotation");
  1106. }
  1107. flowParseObjectPropertyKey() {
  1108. return this.match(types.num) || this.match(types.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  1109. }
  1110. flowParseObjectTypeIndexer(node, isStatic, variance) {
  1111. node.static = isStatic;
  1112. if (this.lookahead().type === types.colon) {
  1113. node.id = this.flowParseObjectPropertyKey();
  1114. node.key = this.flowParseTypeInitialiser();
  1115. } else {
  1116. node.id = null;
  1117. node.key = this.flowParseType();
  1118. }
  1119. this.expect(types.bracketR);
  1120. node.value = this.flowParseTypeInitialiser();
  1121. node.variance = variance;
  1122. return this.finishNode(node, "ObjectTypeIndexer");
  1123. }
  1124. flowParseObjectTypeInternalSlot(node, isStatic) {
  1125. node.static = isStatic;
  1126. node.id = this.flowParseObjectPropertyKey();
  1127. this.expect(types.bracketR);
  1128. this.expect(types.bracketR);
  1129. if (this.isRelational("<") || this.match(types.parenL)) {
  1130. node.method = true;
  1131. node.optional = false;
  1132. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  1133. } else {
  1134. node.method = false;
  1135. if (this.eat(types.question)) {
  1136. node.optional = true;
  1137. }
  1138. node.value = this.flowParseTypeInitialiser();
  1139. }
  1140. return this.finishNode(node, "ObjectTypeInternalSlot");
  1141. }
  1142. flowParseObjectTypeMethodish(node) {
  1143. node.params = [];
  1144. node.rest = null;
  1145. node.typeParameters = null;
  1146. if (this.isRelational("<")) {
  1147. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1148. }
  1149. this.expect(types.parenL);
  1150. while (!this.match(types.parenR) && !this.match(types.ellipsis)) {
  1151. node.params.push(this.flowParseFunctionTypeParam());
  1152. if (!this.match(types.parenR)) {
  1153. this.expect(types.comma);
  1154. }
  1155. }
  1156. if (this.eat(types.ellipsis)) {
  1157. node.rest = this.flowParseFunctionTypeParam();
  1158. }
  1159. this.expect(types.parenR);
  1160. node.returnType = this.flowParseTypeInitialiser();
  1161. return this.finishNode(node, "FunctionTypeAnnotation");
  1162. }
  1163. flowParseObjectTypeCallProperty(node, isStatic) {
  1164. const valueNode = this.startNode();
  1165. node.static = isStatic;
  1166. node.value = this.flowParseObjectTypeMethodish(valueNode);
  1167. return this.finishNode(node, "ObjectTypeCallProperty");
  1168. }
  1169. flowParseObjectType({
  1170. allowStatic,
  1171. allowExact,
  1172. allowSpread,
  1173. allowProto,
  1174. allowInexact
  1175. }) {
  1176. const oldInType = this.state.inType;
  1177. this.state.inType = true;
  1178. const nodeStart = this.startNode();
  1179. nodeStart.callProperties = [];
  1180. nodeStart.properties = [];
  1181. nodeStart.indexers = [];
  1182. nodeStart.internalSlots = [];
  1183. let endDelim;
  1184. let exact;
  1185. let inexact = false;
  1186. if (allowExact && this.match(types.braceBarL)) {
  1187. this.expect(types.braceBarL);
  1188. endDelim = types.braceBarR;
  1189. exact = true;
  1190. } else {
  1191. this.expect(types.braceL);
  1192. endDelim = types.braceR;
  1193. exact = false;
  1194. }
  1195. nodeStart.exact = exact;
  1196. while (!this.match(endDelim)) {
  1197. let isStatic = false;
  1198. let protoStart = null;
  1199. const node = this.startNode();
  1200. if (allowProto && this.isContextual("proto")) {
  1201. const lookahead = this.lookahead();
  1202. if (lookahead.type !== types.colon && lookahead.type !== types.question) {
  1203. this.next();
  1204. protoStart = this.state.start;
  1205. allowStatic = false;
  1206. }
  1207. }
  1208. if (allowStatic && this.isContextual("static")) {
  1209. const lookahead = this.lookahead();
  1210. if (lookahead.type !== types.colon && lookahead.type !== types.question) {
  1211. this.next();
  1212. isStatic = true;
  1213. }
  1214. }
  1215. const variance = this.flowParseVariance();
  1216. if (this.eat(types.bracketL)) {
  1217. if (protoStart != null) {
  1218. this.unexpected(protoStart);
  1219. }
  1220. if (this.eat(types.bracketL)) {
  1221. if (variance) {
  1222. this.unexpected(variance.start);
  1223. }
  1224. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  1225. } else {
  1226. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  1227. }
  1228. } else if (this.match(types.parenL) || this.isRelational("<")) {
  1229. if (protoStart != null) {
  1230. this.unexpected(protoStart);
  1231. }
  1232. if (variance) {
  1233. this.unexpected(variance.start);
  1234. }
  1235. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  1236. } else {
  1237. let kind = "init";
  1238. if (this.isContextual("get") || this.isContextual("set")) {
  1239. const lookahead = this.lookahead();
  1240. if (lookahead.type === types.name || lookahead.type === types.string || lookahead.type === types.num) {
  1241. kind = this.state.value;
  1242. this.next();
  1243. }
  1244. }
  1245. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact);
  1246. if (propOrInexact === null) {
  1247. inexact = true;
  1248. } else {
  1249. nodeStart.properties.push(propOrInexact);
  1250. }
  1251. }
  1252. this.flowObjectTypeSemicolon();
  1253. }
  1254. this.expect(endDelim);
  1255. if (allowSpread) {
  1256. nodeStart.inexact = inexact;
  1257. }
  1258. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  1259. this.state.inType = oldInType;
  1260. return out;
  1261. }
  1262. flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact) {
  1263. if (this.match(types.ellipsis)) {
  1264. if (!allowSpread) {
  1265. this.unexpected(null, "Spread operator cannot appear in class or interface definitions");
  1266. }
  1267. if (protoStart != null) {
  1268. this.unexpected(protoStart);
  1269. }
  1270. if (variance) {
  1271. this.unexpected(variance.start, "Spread properties cannot have variance");
  1272. }
  1273. this.expect(types.ellipsis);
  1274. const isInexactToken = this.eat(types.comma) || this.eat(types.semi);
  1275. if (this.match(types.braceR)) {
  1276. if (allowInexact) return null;
  1277. this.unexpected(null, "Explicit inexact syntax is only allowed inside inexact objects");
  1278. }
  1279. if (this.match(types.braceBarR)) {
  1280. this.unexpected(null, "Explicit inexact syntax cannot appear inside an explicit exact object type");
  1281. }
  1282. if (isInexactToken) {
  1283. this.unexpected(null, "Explicit inexact syntax must appear at the end of an inexact object");
  1284. }
  1285. node.argument = this.flowParseType();
  1286. return this.finishNode(node, "ObjectTypeSpreadProperty");
  1287. } else {
  1288. node.key = this.flowParseObjectPropertyKey();
  1289. node.static = isStatic;
  1290. node.proto = protoStart != null;
  1291. node.kind = kind;
  1292. let optional = false;
  1293. if (this.isRelational("<") || this.match(types.parenL)) {
  1294. node.method = true;
  1295. if (protoStart != null) {
  1296. this.unexpected(protoStart);
  1297. }
  1298. if (variance) {
  1299. this.unexpected(variance.start);
  1300. }
  1301. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  1302. if (kind === "get" || kind === "set") {
  1303. this.flowCheckGetterSetterParams(node);
  1304. }
  1305. } else {
  1306. if (kind !== "init") this.unexpected();
  1307. node.method = false;
  1308. if (this.eat(types.question)) {
  1309. optional = true;
  1310. }
  1311. node.value = this.flowParseTypeInitialiser();
  1312. node.variance = variance;
  1313. }
  1314. node.optional = optional;
  1315. return this.finishNode(node, "ObjectTypeProperty");
  1316. }
  1317. }
  1318. flowCheckGetterSetterParams(property) {
  1319. const paramCount = property.kind === "get" ? 0 : 1;
  1320. const start = property.start;
  1321. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  1322. if (length !== paramCount) {
  1323. if (property.kind === "get") {
  1324. this.raise(start, "getter must not have any formal parameters");
  1325. } else {
  1326. this.raise(start, "setter must have exactly one formal parameter");
  1327. }
  1328. }
  1329. if (property.kind === "set" && property.value.rest) {
  1330. this.raise(start, "setter function argument must not be a rest parameter");
  1331. }
  1332. }
  1333. flowObjectTypeSemicolon() {
  1334. if (!this.eat(types.semi) && !this.eat(types.comma) && !this.match(types.braceR) && !this.match(types.braceBarR)) {
  1335. this.unexpected();
  1336. }
  1337. }
  1338. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  1339. startPos = startPos || this.state.start;
  1340. startLoc = startLoc || this.state.startLoc;
  1341. let node = id || this.parseIdentifier();
  1342. while (this.eat(types.dot)) {
  1343. const node2 = this.startNodeAt(startPos, startLoc);
  1344. node2.qualification = node;
  1345. node2.id = this.parseIdentifier();
  1346. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  1347. }
  1348. return node;
  1349. }
  1350. flowParseGenericType(startPos, startLoc, id) {
  1351. const node = this.startNodeAt(startPos, startLoc);
  1352. node.typeParameters = null;
  1353. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  1354. if (this.isRelational("<")) {
  1355. node.typeParameters = this.flowParseTypeParameterInstantiation();
  1356. }
  1357. return this.finishNode(node, "GenericTypeAnnotation");
  1358. }
  1359. flowParseTypeofType() {
  1360. const node = this.startNode();
  1361. this.expect(types._typeof);
  1362. node.argument = this.flowParsePrimaryType();
  1363. return this.finishNode(node, "TypeofTypeAnnotation");
  1364. }
  1365. flowParseTupleType() {
  1366. const node = this.startNode();
  1367. node.types = [];
  1368. this.expect(types.bracketL);
  1369. while (this.state.pos < this.length && !this.match(types.bracketR)) {
  1370. node.types.push(this.flowParseType());
  1371. if (this.match(types.bracketR)) break;
  1372. this.expect(types.comma);
  1373. }
  1374. this.expect(types.bracketR);
  1375. return this.finishNode(node, "TupleTypeAnnotation");
  1376. }
  1377. flowParseFunctionTypeParam() {
  1378. let name = null;
  1379. let optional = false;
  1380. let typeAnnotation = null;
  1381. const node = this.startNode();
  1382. const lh = this.lookahead();
  1383. if (lh.type === types.colon || lh.type === types.question) {
  1384. name = this.parseIdentifier();
  1385. if (this.eat(types.question)) {
  1386. optional = true;
  1387. }
  1388. typeAnnotation = this.flowParseTypeInitialiser();
  1389. } else {
  1390. typeAnnotation = this.flowParseType();
  1391. }
  1392. node.name = name;
  1393. node.optional = optional;
  1394. node.typeAnnotation = typeAnnotation;
  1395. return this.finishNode(node, "FunctionTypeParam");
  1396. }
  1397. reinterpretTypeAsFunctionTypeParam(type) {
  1398. const node = this.startNodeAt(type.start, type.loc.start);
  1399. node.name = null;
  1400. node.optional = false;
  1401. node.typeAnnotation = type;
  1402. return this.finishNode(node, "FunctionTypeParam");
  1403. }
  1404. flowParseFunctionTypeParams(params = []) {
  1405. let rest = null;
  1406. while (!this.match(types.parenR) && !this.match(types.ellipsis)) {
  1407. params.push(this.flowParseFunctionTypeParam());
  1408. if (!this.match(types.parenR)) {
  1409. this.expect(types.comma);
  1410. }
  1411. }
  1412. if (this.eat(types.ellipsis)) {
  1413. rest = this.flowParseFunctionTypeParam();
  1414. }
  1415. return {
  1416. params,
  1417. rest
  1418. };
  1419. }
  1420. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  1421. switch (id.name) {
  1422. case "any":
  1423. return this.finishNode(node, "AnyTypeAnnotation");
  1424. case "bool":
  1425. case "boolean":
  1426. return this.finishNode(node, "BooleanTypeAnnotation");
  1427. case "mixed":
  1428. return this.finishNode(node, "MixedTypeAnnotation");
  1429. case "empty":
  1430. return this.finishNode(node, "EmptyTypeAnnotation");
  1431. case "number":
  1432. return this.finishNode(node, "NumberTypeAnnotation");
  1433. case "string":
  1434. return this.finishNode(node, "StringTypeAnnotation");
  1435. default:
  1436. this.checkNotUnderscore(id.name);
  1437. return this.flowParseGenericType(startPos, startLoc, id);
  1438. }
  1439. }
  1440. flowParsePrimaryType() {
  1441. const startPos = this.state.start;
  1442. const startLoc = this.state.startLoc;
  1443. const node = this.startNode();
  1444. let tmp;
  1445. let type;
  1446. let isGroupedType = false;
  1447. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1448. switch (this.state.type) {
  1449. case types.name:
  1450. if (this.isContextual("interface")) {
  1451. return this.flowParseInterfaceType();
  1452. }
  1453. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  1454. case types.braceL:
  1455. return this.flowParseObjectType({
  1456. allowStatic: false,
  1457. allowExact: false,
  1458. allowSpread: true,
  1459. allowProto: false,
  1460. allowInexact: true
  1461. });
  1462. case types.braceBarL:
  1463. return this.flowParseObjectType({
  1464. allowStatic: false,
  1465. allowExact: true,
  1466. allowSpread: true,
  1467. allowProto: false,
  1468. allowInexact: false
  1469. });
  1470. case types.bracketL:
  1471. this.state.noAnonFunctionType = false;
  1472. type = this.flowParseTupleType();
  1473. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1474. return type;
  1475. case types.relational:
  1476. if (this.state.value === "<") {
  1477. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1478. this.expect(types.parenL);
  1479. tmp = this.flowParseFunctionTypeParams();
  1480. node.params = tmp.params;
  1481. node.rest = tmp.rest;
  1482. this.expect(types.parenR);
  1483. this.expect(types.arrow);
  1484. node.returnType = this.flowParseType();
  1485. return this.finishNode(node, "FunctionTypeAnnotation");
  1486. }
  1487. break;
  1488. case types.parenL:
  1489. this.next();
  1490. if (!this.match(types.parenR) && !this.match(types.ellipsis)) {
  1491. if (this.match(types.name)) {
  1492. const token = this.lookahead().type;
  1493. isGroupedType = token !== types.question && token !== types.colon;
  1494. } else {
  1495. isGroupedType = true;
  1496. }
  1497. }
  1498. if (isGroupedType) {
  1499. this.state.noAnonFunctionType = false;
  1500. type = this.flowParseType();
  1501. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1502. if (this.state.noAnonFunctionType || !(this.match(types.comma) || this.match(types.parenR) && this.lookahead().type === types.arrow)) {
  1503. this.expect(types.parenR);
  1504. return type;
  1505. } else {
  1506. this.eat(types.comma);
  1507. }
  1508. }
  1509. if (type) {
  1510. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  1511. } else {
  1512. tmp = this.flowParseFunctionTypeParams();
  1513. }
  1514. node.params = tmp.params;
  1515. node.rest = tmp.rest;
  1516. this.expect(types.parenR);
  1517. this.expect(types.arrow);
  1518. node.returnType = this.flowParseType();
  1519. node.typeParameters = null;
  1520. return this.finishNode(node, "FunctionTypeAnnotation");
  1521. case types.string:
  1522. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  1523. case types._true:
  1524. case types._false:
  1525. node.value = this.match(types._true);
  1526. this.next();
  1527. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  1528. case types.plusMin:
  1529. if (this.state.value === "-") {
  1530. this.next();
  1531. if (this.match(types.num)) {
  1532. return this.parseLiteral(-this.state.value, "NumberLiteralTypeAnnotation", node.start, node.loc.start);
  1533. }
  1534. if (this.match(types.bigint)) {
  1535. return this.parseLiteral(-this.state.value, "BigIntLiteralTypeAnnotation", node.start, node.loc.start);
  1536. }
  1537. this.unexpected(null, `Unexpected token, expected "number" or "bigint"`);
  1538. }
  1539. this.unexpected();
  1540. case types.num:
  1541. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  1542. case types.bigint:
  1543. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  1544. case types._void:
  1545. this.next();
  1546. return this.finishNode(node, "VoidTypeAnnotation");
  1547. case types._null:
  1548. this.next();
  1549. return this.finishNode(node, "NullLiteralTypeAnnotation");
  1550. case types._this:
  1551. this.next();
  1552. return this.finishNode(node, "ThisTypeAnnotation");
  1553. case types.star:
  1554. this.next();
  1555. return this.finishNode(node, "ExistsTypeAnnotation");
  1556. default:
  1557. if (this.state.type.keyword === "typeof") {
  1558. return this.flowParseTypeofType();
  1559. } else if (this.state.type.keyword) {
  1560. const label = this.state.type.label;
  1561. this.next();
  1562. return super.createIdentifier(node, label);
  1563. }
  1564. }
  1565. throw this.unexpected();
  1566. }
  1567. flowParsePostfixType() {
  1568. const startPos = this.state.start,
  1569. startLoc = this.state.startLoc;
  1570. let type = this.flowParsePrimaryType();
  1571. while (this.match(types.bracketL) && !this.canInsertSemicolon()) {
  1572. const node = this.startNodeAt(startPos, startLoc);
  1573. node.elementType = type;
  1574. this.expect(types.bracketL);
  1575. this.expect(types.bracketR);
  1576. type = this.finishNode(node, "ArrayTypeAnnotation");
  1577. }
  1578. return type;
  1579. }
  1580. flowParsePrefixType() {
  1581. const node = this.startNode();
  1582. if (this.eat(types.question)) {
  1583. node.typeAnnotation = this.flowParsePrefixType();
  1584. return this.finishNode(node, "NullableTypeAnnotation");
  1585. } else {
  1586. return this.flowParsePostfixType();
  1587. }
  1588. }
  1589. flowParseAnonFunctionWithoutParens() {
  1590. const param = this.flowParsePrefixType();
  1591. if (!this.state.noAnonFunctionType && this.eat(types.arrow)) {
  1592. const node = this.startNodeAt(param.start, param.loc.start);
  1593. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  1594. node.rest = null;
  1595. node.returnType = this.flowParseType();
  1596. node.typeParameters = null;
  1597. return this.finishNode(node, "FunctionTypeAnnotation");
  1598. }
  1599. return param;
  1600. }
  1601. flowParseIntersectionType() {
  1602. const node = this.startNode();
  1603. this.eat(types.bitwiseAND);
  1604. const type = this.flowParseAnonFunctionWithoutParens();
  1605. node.types = [type];
  1606. while (this.eat(types.bitwiseAND)) {
  1607. node.types.push(this.flowParseAnonFunctionWithoutParens());
  1608. }
  1609. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  1610. }
  1611. flowParseUnionType() {
  1612. const node = this.startNode();
  1613. this.eat(types.bitwiseOR);
  1614. const type = this.flowParseIntersectionType();
  1615. node.types = [type];
  1616. while (this.eat(types.bitwiseOR)) {
  1617. node.types.push(this.flowParseIntersectionType());
  1618. }
  1619. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  1620. }
  1621. flowParseType() {
  1622. const oldInType = this.state.inType;
  1623. this.state.inType = true;
  1624. const type = this.flowParseUnionType();
  1625. this.state.inType = oldInType;
  1626. this.state.exprAllowed = this.state.exprAllowed || this.state.noAnonFunctionType;
  1627. return type;
  1628. }
  1629. flowParseTypeOrImplicitInstantiation() {
  1630. if (this.state.type === types.name && this.state.value === "_") {
  1631. const startPos = this.state.start;
  1632. const startLoc = this.state.startLoc;
  1633. const node = this.parseIdentifier();
  1634. return this.flowParseGenericType(startPos, startLoc, node);
  1635. } else {
  1636. return this.flowParseType();
  1637. }
  1638. }
  1639. flowParseTypeAnnotation() {
  1640. const node = this.startNode();
  1641. node.typeAnnotation = this.flowParseTypeInitialiser();
  1642. return this.finishNode(node, "TypeAnnotation");
  1643. }
  1644. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  1645. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  1646. if (this.match(types.colon)) {
  1647. ident.typeAnnotation = this.flowParseTypeAnnotation();
  1648. this.resetEndLocation(ident);
  1649. }
  1650. return ident;
  1651. }
  1652. typeCastToParameter(node) {
  1653. node.expression.typeAnnotation = node.typeAnnotation;
  1654. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  1655. return node.expression;
  1656. }
  1657. flowParseVariance() {
  1658. let variance = null;
  1659. if (this.match(types.plusMin)) {
  1660. variance = this.startNode();
  1661. if (this.state.value === "+") {
  1662. variance.kind = "plus";
  1663. } else {
  1664. variance.kind = "minus";
  1665. }
  1666. this.next();
  1667. this.finishNode(variance, "Variance");
  1668. }
  1669. return variance;
  1670. }
  1671. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  1672. if (allowExpressionBody) {
  1673. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  1674. }
  1675. return super.parseFunctionBody(node, false, isMethod);
  1676. }
  1677. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  1678. if (this.match(types.colon)) {
  1679. const typeNode = this.startNode();
  1680. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  1681. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  1682. }
  1683. super.parseFunctionBodyAndFinish(node, type, isMethod);
  1684. }
  1685. parseStatement(context, topLevel) {
  1686. if (this.state.strict && this.match(types.name) && this.state.value === "interface") {
  1687. const node = this.startNode();
  1688. this.next();
  1689. return this.flowParseInterface(node);
  1690. } else {
  1691. const stmt = super.parseStatement(context, topLevel);
  1692. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  1693. this.flowPragma = null;
  1694. }
  1695. return stmt;
  1696. }
  1697. }
  1698. parseExpressionStatement(node, expr) {
  1699. if (expr.type === "Identifier") {
  1700. if (expr.name === "declare") {
  1701. if (this.match(types._class) || this.match(types.name) || this.match(types._function) || this.match(types._var) || this.match(types._export)) {
  1702. return this.flowParseDeclare(node);
  1703. }
  1704. } else if (this.match(types.name)) {
  1705. if (expr.name === "interface") {
  1706. return this.flowParseInterface(node);
  1707. } else if (expr.name === "type") {
  1708. return this.flowParseTypeAlias(node);
  1709. } else if (expr.name === "opaque") {
  1710. return this.flowParseOpaqueType(node, false);
  1711. }
  1712. }
  1713. }
  1714. return super.parseExpressionStatement(node, expr);
  1715. }
  1716. shouldParseExportDeclaration() {
  1717. return this.isContextual("type") || this.isContextual("interface") || this.isContextual("opaque") || super.shouldParseExportDeclaration();
  1718. }
  1719. isExportDefaultSpecifier() {
  1720. if (this.match(types.name) && (this.state.value === "type" || this.state.value === "interface" || this.state.value === "opaque")) {
  1721. return false;
  1722. }
  1723. return super.isExportDefaultSpecifier();
  1724. }
  1725. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  1726. if (!this.match(types.question)) return expr;
  1727. if (refNeedsArrowPos) {
  1728. const state = this.state.clone();
  1729. try {
  1730. return super.parseConditional(expr, noIn, startPos, startLoc);
  1731. } catch (err) {
  1732. if (err instanceof SyntaxError) {
  1733. this.state = state;
  1734. refNeedsArrowPos.start = err.pos || this.state.start;
  1735. return expr;
  1736. } else {
  1737. throw err;
  1738. }
  1739. }
  1740. }
  1741. this.expect(types.question);
  1742. const state = this.state.clone();
  1743. const originalNoArrowAt = this.state.noArrowAt;
  1744. const node = this.startNodeAt(startPos, startLoc);
  1745. let {
  1746. consequent,
  1747. failed
  1748. } = this.tryParseConditionalConsequent();
  1749. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  1750. if (failed || invalid.length > 0) {
  1751. const noArrowAt = [...originalNoArrowAt];
  1752. if (invalid.length > 0) {
  1753. this.state = state;
  1754. this.state.noArrowAt = noArrowAt;
  1755. for (let i = 0; i < invalid.length; i++) {
  1756. noArrowAt.push(invalid[i].start);
  1757. }
  1758. ({
  1759. consequent,
  1760. failed
  1761. } = this.tryParseConditionalConsequent());
  1762. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  1763. }
  1764. if (failed && valid.length > 1) {
  1765. this.raise(state.start, "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.");
  1766. }
  1767. if (failed && valid.length === 1) {
  1768. this.state = state;
  1769. this.state.noArrowAt = noArrowAt.concat(valid[0].start);
  1770. ({
  1771. consequent,
  1772. failed
  1773. } = this.tryParseConditionalConsequent());
  1774. }
  1775. this.getArrowLikeExpressions(consequent, true);
  1776. }
  1777. this.state.noArrowAt = originalNoArrowAt;
  1778. this.expect(types.colon);
  1779. node.test = expr;
  1780. node.consequent = consequent;
  1781. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(noIn, undefined, undefined, undefined));
  1782. return this.finishNode(node, "ConditionalExpression");
  1783. }
  1784. tryParseConditionalConsequent() {
  1785. this.state.noArrowParamsConversionAt.push(this.state.start);
  1786. const consequent = this.parseMaybeAssign();
  1787. const failed = !this.match(types.colon);
  1788. this.state.noArrowParamsConversionAt.pop();
  1789. return {
  1790. consequent,
  1791. failed
  1792. };
  1793. }
  1794. getArrowLikeExpressions(node, disallowInvalid) {
  1795. const stack = [node];
  1796. const arrows = [];
  1797. while (stack.length !== 0) {
  1798. const node = stack.pop();
  1799. if (node.type === "ArrowFunctionExpression") {
  1800. if (node.typeParameters || !node.returnType) {
  1801. this.toAssignableList(node.params, true, "arrow function parameters");
  1802. this.scope.enter(functionFlags(false, false) | SCOPE_ARROW);
  1803. super.checkParams(node, false, true);
  1804. this.scope.exit();
  1805. } else {
  1806. arrows.push(node);
  1807. }
  1808. stack.push(node.body);
  1809. } else if (node.type === "ConditionalExpression") {
  1810. stack.push(node.consequent);
  1811. stack.push(node.alternate);
  1812. }
  1813. }
  1814. if (disallowInvalid) {
  1815. for (let i = 0; i < arrows.length; i++) {
  1816. this.toAssignableList(node.params, true, "arrow function parameters");
  1817. }
  1818. return [arrows, []];
  1819. }
  1820. return partition(arrows, node => {
  1821. try {
  1822. this.toAssignableList(node.params, true, "arrow function parameters");
  1823. return true;
  1824. } catch (err) {
  1825. return false;
  1826. }
  1827. });
  1828. }
  1829. forwardNoArrowParamsConversionAt(node, parse) {
  1830. let result;
  1831. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  1832. this.state.noArrowParamsConversionAt.push(this.state.start);
  1833. result = parse();
  1834. this.state.noArrowParamsConversionAt.pop();
  1835. } else {
  1836. result = parse();
  1837. }
  1838. return result;
  1839. }
  1840. parseParenItem(node, startPos, startLoc) {
  1841. node = super.parseParenItem(node, startPos, startLoc);
  1842. if (this.eat(types.question)) {
  1843. node.optional = true;
  1844. this.resetEndLocation(node);
  1845. }
  1846. if (this.match(types.colon)) {
  1847. const typeCastNode = this.startNodeAt(startPos, startLoc);
  1848. typeCastNode.expression = node;
  1849. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  1850. return this.finishNode(typeCastNode, "TypeCastExpression");
  1851. }
  1852. return node;
  1853. }
  1854. assertModuleNodeAllowed(node) {
  1855. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  1856. return;
  1857. }
  1858. super.assertModuleNodeAllowed(node);
  1859. }
  1860. parseExport(node) {
  1861. const decl = super.parseExport(node);
  1862. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  1863. decl.exportKind = decl.exportKind || "value";
  1864. }
  1865. return decl;
  1866. }
  1867. parseExportDeclaration(node) {
  1868. if (this.isContextual("type")) {
  1869. node.exportKind = "type";
  1870. const declarationNode = this.startNode();
  1871. this.next();
  1872. if (this.match(types.braceL)) {
  1873. node.specifiers = this.parseExportSpecifiers();
  1874. this.parseExportFrom(node);
  1875. return null;
  1876. } else {
  1877. return this.flowParseTypeAlias(declarationNode);
  1878. }
  1879. } else if (this.isContextual("opaque")) {
  1880. node.exportKind = "type";
  1881. const declarationNode = this.startNode();
  1882. this.next();
  1883. return this.flowParseOpaqueType(declarationNode, false);
  1884. } else if (this.isContextual("interface")) {
  1885. node.exportKind = "type";
  1886. const declarationNode = this.startNode();
  1887. this.next();
  1888. return this.flowParseInterface(declarationNode);
  1889. } else {
  1890. return super.parseExportDeclaration(node);
  1891. }
  1892. }
  1893. eatExportStar(node) {
  1894. if (super.eatExportStar(...arguments)) return true;
  1895. if (this.isContextual("type") && this.lookahead().type === types.star) {
  1896. node.exportKind = "type";
  1897. this.next();
  1898. this.next();
  1899. return true;
  1900. }
  1901. return false;
  1902. }
  1903. maybeParseExportNamespaceSpecifier(node) {
  1904. const pos = this.state.start;
  1905. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  1906. if (hasNamespace && node.exportKind === "type") {
  1907. this.unexpected(pos);
  1908. }
  1909. return hasNamespace;
  1910. }
  1911. parseClassId(node, isStatement, optionalId) {
  1912. super.parseClassId(node, isStatement, optionalId);
  1913. if (this.isRelational("<")) {
  1914. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1915. }
  1916. }
  1917. getTokenFromCode(code) {
  1918. const next = this.input.charCodeAt(this.state.pos + 1);
  1919. if (code === 123 && next === 124) {
  1920. return this.finishOp(types.braceBarL, 2);
  1921. } else if (this.state.inType && (code === 62 || code === 60)) {
  1922. return this.finishOp(types.relational, 1);
  1923. } else if (isIteratorStart(code, next)) {
  1924. this.state.isIterator = true;
  1925. return super.readWord();
  1926. } else {
  1927. return super.getTokenFromCode(code);
  1928. }
  1929. }
  1930. toAssignable(node, isBinding, contextDescription) {
  1931. if (node.type === "TypeCastExpression") {
  1932. return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription);
  1933. } else {
  1934. return super.toAssignable(node, isBinding, contextDescription);
  1935. }
  1936. }
  1937. toAssignableList(exprList, isBinding, contextDescription) {
  1938. for (let i = 0; i < exprList.length; i++) {
  1939. const expr = exprList[i];
  1940. if (expr && expr.type === "TypeCastExpression") {
  1941. exprList[i] = this.typeCastToParameter(expr);
  1942. }
  1943. }
  1944. return super.toAssignableList(exprList, isBinding, contextDescription);
  1945. }
  1946. toReferencedList(exprList, isParenthesizedExpr) {
  1947. for (let i = 0; i < exprList.length; i++) {
  1948. const expr = exprList[i];
  1949. if (expr && expr.type === "TypeCastExpression" && (!expr.extra || !expr.extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  1950. this.raise(expr.typeAnnotation.start, "The type cast expression is expected to be wrapped with parenthesis");
  1951. }
  1952. }
  1953. return exprList;
  1954. }
  1955. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  1956. if (expr.type !== "TypeCastExpression") {
  1957. return super.checkLVal(expr, bindingType, checkClashes, contextDescription);
  1958. }
  1959. }
  1960. parseClassProperty(node) {
  1961. if (this.match(types.colon)) {
  1962. node.typeAnnotation = this.flowParseTypeAnnotation();
  1963. }
  1964. return super.parseClassProperty(node);
  1965. }
  1966. parseClassPrivateProperty(node) {
  1967. if (this.match(types.colon)) {
  1968. node.typeAnnotation = this.flowParseTypeAnnotation();
  1969. }
  1970. return super.parseClassPrivateProperty(node);
  1971. }
  1972. isClassMethod() {
  1973. return this.isRelational("<") || super.isClassMethod();
  1974. }
  1975. isClassProperty() {
  1976. return this.match(types.colon) || super.isClassProperty();
  1977. }
  1978. isNonstaticConstructor(method) {
  1979. return !this.match(types.colon) && super.isNonstaticConstructor(method);
  1980. }
  1981. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  1982. if (method.variance) {
  1983. this.unexpected(method.variance.start);
  1984. }
  1985. delete method.variance;
  1986. if (this.isRelational("<")) {
  1987. method.typeParameters = this.flowParseTypeParameterDeclaration();
  1988. }
  1989. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  1990. }
  1991. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  1992. if (method.variance) {
  1993. this.unexpected(method.variance.start);
  1994. }
  1995. delete method.variance;
  1996. if (this.isRelational("<")) {
  1997. method.typeParameters = this.flowParseTypeParameterDeclaration();
  1998. }
  1999. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  2000. }
  2001. parseClassSuper(node) {
  2002. super.parseClassSuper(node);
  2003. if (node.superClass && this.isRelational("<")) {
  2004. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  2005. }
  2006. if (this.isContextual("implements")) {
  2007. this.next();
  2008. const implemented = node.implements = [];
  2009. do {
  2010. const node = this.startNode();
  2011. node.id = this.flowParseRestrictedIdentifier(true);
  2012. if (this.isRelational("<")) {
  2013. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2014. } else {
  2015. node.typeParameters = null;
  2016. }
  2017. implemented.push(this.finishNode(node, "ClassImplements"));
  2018. } while (this.eat(types.comma));
  2019. }
  2020. }
  2021. parsePropertyName(node) {
  2022. const variance = this.flowParseVariance();
  2023. const key = super.parsePropertyName(node);
  2024. node.variance = variance;
  2025. return key;
  2026. }
  2027. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc) {
  2028. if (prop.variance) {
  2029. this.unexpected(prop.variance.start);
  2030. }
  2031. delete prop.variance;
  2032. let typeParameters;
  2033. if (this.isRelational("<")) {
  2034. typeParameters = this.flowParseTypeParameterDeclaration();
  2035. if (!this.match(types.parenL)) this.unexpected();
  2036. }
  2037. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc);
  2038. if (typeParameters) {
  2039. (prop.value || prop).typeParameters = typeParameters;
  2040. }
  2041. }
  2042. parseAssignableListItemTypes(param) {
  2043. if (this.eat(types.question)) {
  2044. if (param.type !== "Identifier") {
  2045. throw this.raise(param.start, "A binding pattern parameter cannot be optional in an implementation signature.");
  2046. }
  2047. param.optional = true;
  2048. }
  2049. if (this.match(types.colon)) {
  2050. param.typeAnnotation = this.flowParseTypeAnnotation();
  2051. }
  2052. this.resetEndLocation(param);
  2053. return param;
  2054. }
  2055. parseMaybeDefault(startPos, startLoc, left) {
  2056. const node = super.parseMaybeDefault(startPos, startLoc, left);
  2057. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  2058. this.raise(node.typeAnnotation.start, "Type annotations must come before default assignments, " + "e.g. instead of `age = 25: number` use `age: number = 25`");
  2059. }
  2060. return node;
  2061. }
  2062. shouldParseDefaultImport(node) {
  2063. if (!hasTypeImportKind(node)) {
  2064. return super.shouldParseDefaultImport(node);
  2065. }
  2066. return isMaybeDefaultImport(this.state);
  2067. }
  2068. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  2069. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true) : this.parseIdentifier();
  2070. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, contextDescription);
  2071. node.specifiers.push(this.finishNode(specifier, type));
  2072. }
  2073. maybeParseDefaultImportSpecifier(node) {
  2074. node.importKind = "value";
  2075. let kind = null;
  2076. if (this.match(types._typeof)) {
  2077. kind = "typeof";
  2078. } else if (this.isContextual("type")) {
  2079. kind = "type";
  2080. }
  2081. if (kind) {
  2082. const lh = this.lookahead();
  2083. if (kind === "type" && lh.type === types.star) {
  2084. this.unexpected(lh.start);
  2085. }
  2086. if (isMaybeDefaultImport(lh) || lh.type === types.braceL || lh.type === types.star) {
  2087. this.next();
  2088. node.importKind = kind;
  2089. }
  2090. }
  2091. return super.maybeParseDefaultImportSpecifier(node);
  2092. }
  2093. parseImportSpecifier(node) {
  2094. const specifier = this.startNode();
  2095. const firstIdentLoc = this.state.start;
  2096. const firstIdent = this.parseIdentifier(true);
  2097. let specifierTypeKind = null;
  2098. if (firstIdent.name === "type") {
  2099. specifierTypeKind = "type";
  2100. } else if (firstIdent.name === "typeof") {
  2101. specifierTypeKind = "typeof";
  2102. }
  2103. let isBinding = false;
  2104. if (this.isContextual("as") && !this.isLookaheadContextual("as")) {
  2105. const as_ident = this.parseIdentifier(true);
  2106. if (specifierTypeKind !== null && !this.match(types.name) && !this.state.type.keyword) {
  2107. specifier.imported = as_ident;
  2108. specifier.importKind = specifierTypeKind;
  2109. specifier.local = as_ident.__clone();
  2110. } else {
  2111. specifier.imported = firstIdent;
  2112. specifier.importKind = null;
  2113. specifier.local = this.parseIdentifier();
  2114. }
  2115. } else if (specifierTypeKind !== null && (this.match(types.name) || this.state.type.keyword)) {
  2116. specifier.imported = this.parseIdentifier(true);
  2117. specifier.importKind = specifierTypeKind;
  2118. if (this.eatContextual("as")) {
  2119. specifier.local = this.parseIdentifier();
  2120. } else {
  2121. isBinding = true;
  2122. specifier.local = specifier.imported.__clone();
  2123. }
  2124. } else {
  2125. isBinding = true;
  2126. specifier.imported = firstIdent;
  2127. specifier.importKind = null;
  2128. specifier.local = specifier.imported.__clone();
  2129. }
  2130. const nodeIsTypeImport = hasTypeImportKind(node);
  2131. const specifierIsTypeImport = hasTypeImportKind(specifier);
  2132. if (nodeIsTypeImport && specifierIsTypeImport) {
  2133. this.raise(firstIdentLoc, "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");
  2134. }
  2135. if (nodeIsTypeImport || specifierIsTypeImport) {
  2136. this.checkReservedType(specifier.local.name, specifier.local.start);
  2137. }
  2138. if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) {
  2139. this.checkReservedWord(specifier.local.name, specifier.start, true, true);
  2140. }
  2141. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, "import specifier");
  2142. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  2143. }
  2144. parseFunctionParams(node, allowModifiers) {
  2145. const kind = node.kind;
  2146. if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
  2147. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2148. }
  2149. super.parseFunctionParams(node, allowModifiers);
  2150. }
  2151. parseVarId(decl, kind) {
  2152. super.parseVarId(decl, kind);
  2153. if (this.match(types.colon)) {
  2154. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  2155. this.resetEndLocation(decl.id);
  2156. }
  2157. }
  2158. parseAsyncArrowFromCallExpression(node, call) {
  2159. if (this.match(types.colon)) {
  2160. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2161. this.state.noAnonFunctionType = true;
  2162. node.returnType = this.flowParseTypeAnnotation();
  2163. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2164. }
  2165. return super.parseAsyncArrowFromCallExpression(node, call);
  2166. }
  2167. shouldParseAsyncArrow() {
  2168. return this.match(types.colon) || super.shouldParseAsyncArrow();
  2169. }
  2170. parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) {
  2171. let jsxError = null;
  2172. if (this.hasPlugin("jsx") && (this.match(types.jsxTagStart) || this.isRelational("<"))) {
  2173. const state = this.state.clone();
  2174. try {
  2175. return super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos);
  2176. } catch (err) {
  2177. if (err instanceof SyntaxError) {
  2178. this.state = state;
  2179. const cLength = this.state.context.length;
  2180. if (this.state.context[cLength - 1] === types$1.j_oTag) {
  2181. this.state.context.length -= 2;
  2182. }
  2183. jsxError = err;
  2184. } else {
  2185. throw err;
  2186. }
  2187. }
  2188. }
  2189. if (jsxError != null || this.isRelational("<")) {
  2190. let arrowExpression;
  2191. let typeParameters;
  2192. try {
  2193. typeParameters = this.flowParseTypeParameterDeclaration();
  2194. arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos));
  2195. arrowExpression.typeParameters = typeParameters;
  2196. this.resetStartLocationFromNode(arrowExpression, typeParameters);
  2197. } catch (err) {
  2198. throw jsxError || err;
  2199. }
  2200. if (arrowExpression.type === "ArrowFunctionExpression") {
  2201. return arrowExpression;
  2202. } else if (jsxError != null) {
  2203. throw jsxError;
  2204. } else {
  2205. this.raise(typeParameters.start, "Expected an arrow function after this type parameter declaration");
  2206. }
  2207. }
  2208. return super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos);
  2209. }
  2210. parseArrow(node) {
  2211. if (this.match(types.colon)) {
  2212. const state = this.state.clone();
  2213. try {
  2214. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2215. this.state.noAnonFunctionType = true;
  2216. const typeNode = this.startNode();
  2217. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  2218. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2219. if (this.canInsertSemicolon()) this.unexpected();
  2220. if (!this.match(types.arrow)) this.unexpected();
  2221. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  2222. } catch (err) {
  2223. if (err instanceof SyntaxError) {
  2224. this.state = state;
  2225. } else {
  2226. throw err;
  2227. }
  2228. }
  2229. }
  2230. return super.parseArrow(node);
  2231. }
  2232. shouldParseArrow() {
  2233. return this.match(types.colon) || super.shouldParseArrow();
  2234. }
  2235. setArrowFunctionParameters(node, params) {
  2236. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  2237. node.params = params;
  2238. } else {
  2239. super.setArrowFunctionParameters(node, params);
  2240. }
  2241. }
  2242. checkParams(node, allowDuplicates, isArrowFunction) {
  2243. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  2244. return;
  2245. }
  2246. return super.checkParams(node, allowDuplicates, isArrowFunction);
  2247. }
  2248. parseParenAndDistinguishExpression(canBeArrow) {
  2249. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  2250. }
  2251. parseSubscripts(base, startPos, startLoc, noCalls) {
  2252. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  2253. this.next();
  2254. const node = this.startNodeAt(startPos, startLoc);
  2255. node.callee = base;
  2256. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  2257. base = this.finishNode(node, "CallExpression");
  2258. } else if (base.type === "Identifier" && base.name === "async" && this.isRelational("<")) {
  2259. const state = this.state.clone();
  2260. let error;
  2261. try {
  2262. const node = this.parseAsyncArrowWithTypeParameters(startPos, startLoc);
  2263. if (node) return node;
  2264. } catch (e) {
  2265. error = e;
  2266. }
  2267. this.state = state;
  2268. try {
  2269. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  2270. } catch (e) {
  2271. throw error || e;
  2272. }
  2273. }
  2274. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  2275. }
  2276. parseSubscript(base, startPos, startLoc, noCalls, subscriptState, maybeAsyncArrow) {
  2277. if (this.match(types.questionDot) && this.isLookaheadRelational("<")) {
  2278. this.expectPlugin("optionalChaining");
  2279. subscriptState.optionalChainMember = true;
  2280. if (noCalls) {
  2281. subscriptState.stop = true;
  2282. return base;
  2283. }
  2284. this.next();
  2285. const node = this.startNodeAt(startPos, startLoc);
  2286. node.callee = base;
  2287. node.typeArguments = this.flowParseTypeParameterInstantiation();
  2288. this.expect(types.parenL);
  2289. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  2290. node.optional = true;
  2291. return this.finishNode(node, "OptionalCallExpression");
  2292. } else if (!noCalls && this.shouldParseTypes() && this.isRelational("<")) {
  2293. const node = this.startNodeAt(startPos, startLoc);
  2294. node.callee = base;
  2295. const state = this.state.clone();
  2296. try {
  2297. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  2298. this.expect(types.parenL);
  2299. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  2300. if (subscriptState.optionalChainMember) {
  2301. node.optional = false;
  2302. return this.finishNode(node, "OptionalCallExpression");
  2303. }
  2304. return this.finishNode(node, "CallExpression");
  2305. } catch (e) {
  2306. if (e instanceof SyntaxError) {
  2307. this.state = state;
  2308. } else {
  2309. throw e;
  2310. }
  2311. }
  2312. }
  2313. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState, maybeAsyncArrow);
  2314. }
  2315. parseNewArguments(node) {
  2316. let targs = null;
  2317. if (this.shouldParseTypes() && this.isRelational("<")) {
  2318. const state = this.state.clone();
  2319. try {
  2320. targs = this.flowParseTypeParameterInstantiationCallOrNew();
  2321. } catch (e) {
  2322. if (e instanceof SyntaxError) {
  2323. this.state = state;
  2324. } else {
  2325. throw e;
  2326. }
  2327. }
  2328. }
  2329. node.typeArguments = targs;
  2330. super.parseNewArguments(node);
  2331. }
  2332. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  2333. const node = this.startNodeAt(startPos, startLoc);
  2334. this.parseFunctionParams(node);
  2335. if (!this.parseArrow(node)) return;
  2336. return this.parseArrowExpression(node, undefined, true);
  2337. }
  2338. readToken_mult_modulo(code) {
  2339. const next = this.input.charCodeAt(this.state.pos + 1);
  2340. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  2341. this.state.hasFlowComment = false;
  2342. this.state.pos += 2;
  2343. this.nextToken();
  2344. return;
  2345. }
  2346. super.readToken_mult_modulo(code);
  2347. }
  2348. readToken_pipe_amp(code) {
  2349. const next = this.input.charCodeAt(this.state.pos + 1);
  2350. if (code === 124 && next === 125) {
  2351. this.finishOp(types.braceBarR, 2);
  2352. return;
  2353. }
  2354. super.readToken_pipe_amp(code);
  2355. }
  2356. parseTopLevel(file, program) {
  2357. const fileNode = super.parseTopLevel(file, program);
  2358. if (this.state.hasFlowComment) {
  2359. this.unexpected(null, "Unterminated flow-comment");
  2360. }
  2361. return fileNode;
  2362. }
  2363. skipBlockComment() {
  2364. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  2365. if (this.state.hasFlowComment) {
  2366. this.unexpected(null, "Cannot have a flow comment inside another flow comment");
  2367. }
  2368. this.hasFlowCommentCompletion();
  2369. this.state.pos += this.skipFlowComment();
  2370. this.state.hasFlowComment = true;
  2371. return;
  2372. }
  2373. if (this.state.hasFlowComment) {
  2374. const end = this.input.indexOf("*-/", this.state.pos += 2);
  2375. if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment");
  2376. this.state.pos = end + 3;
  2377. return;
  2378. }
  2379. super.skipBlockComment();
  2380. }
  2381. skipFlowComment() {
  2382. const {
  2383. pos
  2384. } = this.state;
  2385. let shiftToFirstNonWhiteSpace = 2;
  2386. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  2387. shiftToFirstNonWhiteSpace++;
  2388. }
  2389. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  2390. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  2391. if (ch2 === 58 && ch3 === 58) {
  2392. return shiftToFirstNonWhiteSpace + 2;
  2393. }
  2394. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  2395. return shiftToFirstNonWhiteSpace + 12;
  2396. }
  2397. if (ch2 === 58 && ch3 !== 58) {
  2398. return shiftToFirstNonWhiteSpace;
  2399. }
  2400. return false;
  2401. }
  2402. hasFlowCommentCompletion() {
  2403. const end = this.input.indexOf("*/", this.state.pos);
  2404. if (end === -1) {
  2405. this.raise(this.state.pos, "Unterminated comment");
  2406. }
  2407. }
  2408. });
  2409. const entities = {
  2410. quot: "\u0022",
  2411. amp: "&",
  2412. apos: "\u0027",
  2413. lt: "<",
  2414. gt: ">",
  2415. nbsp: "\u00A0",
  2416. iexcl: "\u00A1",
  2417. cent: "\u00A2",
  2418. pound: "\u00A3",
  2419. curren: "\u00A4",
  2420. yen: "\u00A5",
  2421. brvbar: "\u00A6",
  2422. sect: "\u00A7",
  2423. uml: "\u00A8",
  2424. copy: "\u00A9",
  2425. ordf: "\u00AA",
  2426. laquo: "\u00AB",
  2427. not: "\u00AC",
  2428. shy: "\u00AD",
  2429. reg: "\u00AE",
  2430. macr: "\u00AF",
  2431. deg: "\u00B0",
  2432. plusmn: "\u00B1",
  2433. sup2: "\u00B2",
  2434. sup3: "\u00B3",
  2435. acute: "\u00B4",
  2436. micro: "\u00B5",
  2437. para: "\u00B6",
  2438. middot: "\u00B7",
  2439. cedil: "\u00B8",
  2440. sup1: "\u00B9",
  2441. ordm: "\u00BA",
  2442. raquo: "\u00BB",
  2443. frac14: "\u00BC",
  2444. frac12: "\u00BD",
  2445. frac34: "\u00BE",
  2446. iquest: "\u00BF",
  2447. Agrave: "\u00C0",
  2448. Aacute: "\u00C1",
  2449. Acirc: "\u00C2",
  2450. Atilde: "\u00C3",
  2451. Auml: "\u00C4",
  2452. Aring: "\u00C5",
  2453. AElig: "\u00C6",
  2454. Ccedil: "\u00C7",
  2455. Egrave: "\u00C8",
  2456. Eacute: "\u00C9",
  2457. Ecirc: "\u00CA",
  2458. Euml: "\u00CB",
  2459. Igrave: "\u00CC",
  2460. Iacute: "\u00CD",
  2461. Icirc: "\u00CE",
  2462. Iuml: "\u00CF",
  2463. ETH: "\u00D0",
  2464. Ntilde: "\u00D1",
  2465. Ograve: "\u00D2",
  2466. Oacute: "\u00D3",
  2467. Ocirc: "\u00D4",
  2468. Otilde: "\u00D5",
  2469. Ouml: "\u00D6",
  2470. times: "\u00D7",
  2471. Oslash: "\u00D8",
  2472. Ugrave: "\u00D9",
  2473. Uacute: "\u00DA",
  2474. Ucirc: "\u00DB",
  2475. Uuml: "\u00DC",
  2476. Yacute: "\u00DD",
  2477. THORN: "\u00DE",
  2478. szlig: "\u00DF",
  2479. agrave: "\u00E0",
  2480. aacute: "\u00E1",
  2481. acirc: "\u00E2",
  2482. atilde: "\u00E3",
  2483. auml: "\u00E4",
  2484. aring: "\u00E5",
  2485. aelig: "\u00E6",
  2486. ccedil: "\u00E7",
  2487. egrave: "\u00E8",
  2488. eacute: "\u00E9",
  2489. ecirc: "\u00EA",
  2490. euml: "\u00EB",
  2491. igrave: "\u00EC",
  2492. iacute: "\u00ED",
  2493. icirc: "\u00EE",
  2494. iuml: "\u00EF",
  2495. eth: "\u00F0",
  2496. ntilde: "\u00F1",
  2497. ograve: "\u00F2",
  2498. oacute: "\u00F3",
  2499. ocirc: "\u00F4",
  2500. otilde: "\u00F5",
  2501. ouml: "\u00F6",
  2502. divide: "\u00F7",
  2503. oslash: "\u00F8",
  2504. ugrave: "\u00F9",
  2505. uacute: "\u00FA",
  2506. ucirc: "\u00FB",
  2507. uuml: "\u00FC",
  2508. yacute: "\u00FD",
  2509. thorn: "\u00FE",
  2510. yuml: "\u00FF",
  2511. OElig: "\u0152",
  2512. oelig: "\u0153",
  2513. Scaron: "\u0160",
  2514. scaron: "\u0161",
  2515. Yuml: "\u0178",
  2516. fnof: "\u0192",
  2517. circ: "\u02C6",
  2518. tilde: "\u02DC",
  2519. Alpha: "\u0391",
  2520. Beta: "\u0392",
  2521. Gamma: "\u0393",
  2522. Delta: "\u0394",
  2523. Epsilon: "\u0395",
  2524. Zeta: "\u0396",
  2525. Eta: "\u0397",
  2526. Theta: "\u0398",
  2527. Iota: "\u0399",
  2528. Kappa: "\u039A",
  2529. Lambda: "\u039B",
  2530. Mu: "\u039C",
  2531. Nu: "\u039D",
  2532. Xi: "\u039E",
  2533. Omicron: "\u039F",
  2534. Pi: "\u03A0",
  2535. Rho: "\u03A1",
  2536. Sigma: "\u03A3",
  2537. Tau: "\u03A4",
  2538. Upsilon: "\u03A5",
  2539. Phi: "\u03A6",
  2540. Chi: "\u03A7",
  2541. Psi: "\u03A8",
  2542. Omega: "\u03A9",
  2543. alpha: "\u03B1",
  2544. beta: "\u03B2",
  2545. gamma: "\u03B3",
  2546. delta: "\u03B4",
  2547. epsilon: "\u03B5",
  2548. zeta: "\u03B6",
  2549. eta: "\u03B7",
  2550. theta: "\u03B8",
  2551. iota: "\u03B9",
  2552. kappa: "\u03BA",
  2553. lambda: "\u03BB",
  2554. mu: "\u03BC",
  2555. nu: "\u03BD",
  2556. xi: "\u03BE",
  2557. omicron: "\u03BF",
  2558. pi: "\u03C0",
  2559. rho: "\u03C1",
  2560. sigmaf: "\u03C2",
  2561. sigma: "\u03C3",
  2562. tau: "\u03C4",
  2563. upsilon: "\u03C5",
  2564. phi: "\u03C6",
  2565. chi: "\u03C7",
  2566. psi: "\u03C8",
  2567. omega: "\u03C9",
  2568. thetasym: "\u03D1",
  2569. upsih: "\u03D2",
  2570. piv: "\u03D6",
  2571. ensp: "\u2002",
  2572. emsp: "\u2003",
  2573. thinsp: "\u2009",
  2574. zwnj: "\u200C",
  2575. zwj: "\u200D",
  2576. lrm: "\u200E",
  2577. rlm: "\u200F",
  2578. ndash: "\u2013",
  2579. mdash: "\u2014",
  2580. lsquo: "\u2018",
  2581. rsquo: "\u2019",
  2582. sbquo: "\u201A",
  2583. ldquo: "\u201C",
  2584. rdquo: "\u201D",
  2585. bdquo: "\u201E",
  2586. dagger: "\u2020",
  2587. Dagger: "\u2021",
  2588. bull: "\u2022",
  2589. hellip: "\u2026",
  2590. permil: "\u2030",
  2591. prime: "\u2032",
  2592. Prime: "\u2033",
  2593. lsaquo: "\u2039",
  2594. rsaquo: "\u203A",
  2595. oline: "\u203E",
  2596. frasl: "\u2044",
  2597. euro: "\u20AC",
  2598. image: "\u2111",
  2599. weierp: "\u2118",
  2600. real: "\u211C",
  2601. trade: "\u2122",
  2602. alefsym: "\u2135",
  2603. larr: "\u2190",
  2604. uarr: "\u2191",
  2605. rarr: "\u2192",
  2606. darr: "\u2193",
  2607. harr: "\u2194",
  2608. crarr: "\u21B5",
  2609. lArr: "\u21D0",
  2610. uArr: "\u21D1",
  2611. rArr: "\u21D2",
  2612. dArr: "\u21D3",
  2613. hArr: "\u21D4",
  2614. forall: "\u2200",
  2615. part: "\u2202",
  2616. exist: "\u2203",
  2617. empty: "\u2205",
  2618. nabla: "\u2207",
  2619. isin: "\u2208",
  2620. notin: "\u2209",
  2621. ni: "\u220B",
  2622. prod: "\u220F",
  2623. sum: "\u2211",
  2624. minus: "\u2212",
  2625. lowast: "\u2217",
  2626. radic: "\u221A",
  2627. prop: "\u221D",
  2628. infin: "\u221E",
  2629. ang: "\u2220",
  2630. and: "\u2227",
  2631. or: "\u2228",
  2632. cap: "\u2229",
  2633. cup: "\u222A",
  2634. int: "\u222B",
  2635. there4: "\u2234",
  2636. sim: "\u223C",
  2637. cong: "\u2245",
  2638. asymp: "\u2248",
  2639. ne: "\u2260",
  2640. equiv: "\u2261",
  2641. le: "\u2264",
  2642. ge: "\u2265",
  2643. sub: "\u2282",
  2644. sup: "\u2283",
  2645. nsub: "\u2284",
  2646. sube: "\u2286",
  2647. supe: "\u2287",
  2648. oplus: "\u2295",
  2649. otimes: "\u2297",
  2650. perp: "\u22A5",
  2651. sdot: "\u22C5",
  2652. lceil: "\u2308",
  2653. rceil: "\u2309",
  2654. lfloor: "\u230A",
  2655. rfloor: "\u230B",
  2656. lang: "\u2329",
  2657. rang: "\u232A",
  2658. loz: "\u25CA",
  2659. spades: "\u2660",
  2660. clubs: "\u2663",
  2661. hearts: "\u2665",
  2662. diams: "\u2666"
  2663. };
  2664. const HEX_NUMBER = /^[\da-fA-F]+$/;
  2665. const DECIMAL_NUMBER = /^\d+$/;
  2666. types$1.j_oTag = new TokContext("<tag", false);
  2667. types$1.j_cTag = new TokContext("</tag", false);
  2668. types$1.j_expr = new TokContext("<tag>...</tag>", true, true);
  2669. types.jsxName = new TokenType("jsxName");
  2670. types.jsxText = new TokenType("jsxText", {
  2671. beforeExpr: true
  2672. });
  2673. types.jsxTagStart = new TokenType("jsxTagStart", {
  2674. startsExpr: true
  2675. });
  2676. types.jsxTagEnd = new TokenType("jsxTagEnd");
  2677. types.jsxTagStart.updateContext = function () {
  2678. this.state.context.push(types$1.j_expr);
  2679. this.state.context.push(types$1.j_oTag);
  2680. this.state.exprAllowed = false;
  2681. };
  2682. types.jsxTagEnd.updateContext = function (prevType) {
  2683. const out = this.state.context.pop();
  2684. if (out === types$1.j_oTag && prevType === types.slash || out === types$1.j_cTag) {
  2685. this.state.context.pop();
  2686. this.state.exprAllowed = this.curContext() === types$1.j_expr;
  2687. } else {
  2688. this.state.exprAllowed = true;
  2689. }
  2690. };
  2691. function isFragment(object) {
  2692. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  2693. }
  2694. function getQualifiedJSXName(object) {
  2695. if (object.type === "JSXIdentifier") {
  2696. return object.name;
  2697. }
  2698. if (object.type === "JSXNamespacedName") {
  2699. return object.namespace.name + ":" + object.name.name;
  2700. }
  2701. if (object.type === "JSXMemberExpression") {
  2702. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  2703. }
  2704. throw new Error("Node had unexpected type: " + object.type);
  2705. }
  2706. var jsx = (superClass => class extends superClass {
  2707. jsxReadToken() {
  2708. let out = "";
  2709. let chunkStart = this.state.pos;
  2710. for (;;) {
  2711. if (this.state.pos >= this.length) {
  2712. this.raise(this.state.start, "Unterminated JSX contents");
  2713. }
  2714. const ch = this.input.charCodeAt(this.state.pos);
  2715. switch (ch) {
  2716. case 60:
  2717. case 123:
  2718. if (this.state.pos === this.state.start) {
  2719. if (ch === 60 && this.state.exprAllowed) {
  2720. ++this.state.pos;
  2721. return this.finishToken(types.jsxTagStart);
  2722. }
  2723. return super.getTokenFromCode(ch);
  2724. }
  2725. out += this.input.slice(chunkStart, this.state.pos);
  2726. return this.finishToken(types.jsxText, out);
  2727. case 38:
  2728. out += this.input.slice(chunkStart, this.state.pos);
  2729. out += this.jsxReadEntity();
  2730. chunkStart = this.state.pos;
  2731. break;
  2732. default:
  2733. if (isNewLine(ch)) {
  2734. out += this.input.slice(chunkStart, this.state.pos);
  2735. out += this.jsxReadNewLine(true);
  2736. chunkStart = this.state.pos;
  2737. } else {
  2738. ++this.state.pos;
  2739. }
  2740. }
  2741. }
  2742. }
  2743. jsxReadNewLine(normalizeCRLF) {
  2744. const ch = this.input.charCodeAt(this.state.pos);
  2745. let out;
  2746. ++this.state.pos;
  2747. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  2748. ++this.state.pos;
  2749. out = normalizeCRLF ? "\n" : "\r\n";
  2750. } else {
  2751. out = String.fromCharCode(ch);
  2752. }
  2753. ++this.state.curLine;
  2754. this.state.lineStart = this.state.pos;
  2755. return out;
  2756. }
  2757. jsxReadString(quote) {
  2758. let out = "";
  2759. let chunkStart = ++this.state.pos;
  2760. for (;;) {
  2761. if (this.state.pos >= this.length) {
  2762. this.raise(this.state.start, "Unterminated string constant");
  2763. }
  2764. const ch = this.input.charCodeAt(this.state.pos);
  2765. if (ch === quote) break;
  2766. if (ch === 38) {
  2767. out += this.input.slice(chunkStart, this.state.pos);
  2768. out += this.jsxReadEntity();
  2769. chunkStart = this.state.pos;
  2770. } else if (isNewLine(ch)) {
  2771. out += this.input.slice(chunkStart, this.state.pos);
  2772. out += this.jsxReadNewLine(false);
  2773. chunkStart = this.state.pos;
  2774. } else {
  2775. ++this.state.pos;
  2776. }
  2777. }
  2778. out += this.input.slice(chunkStart, this.state.pos++);
  2779. return this.finishToken(types.string, out);
  2780. }
  2781. jsxReadEntity() {
  2782. let str = "";
  2783. let count = 0;
  2784. let entity;
  2785. let ch = this.input[this.state.pos];
  2786. const startPos = ++this.state.pos;
  2787. while (this.state.pos < this.length && count++ < 10) {
  2788. ch = this.input[this.state.pos++];
  2789. if (ch === ";") {
  2790. if (str[0] === "#") {
  2791. if (str[1] === "x") {
  2792. str = str.substr(2);
  2793. if (HEX_NUMBER.test(str)) {
  2794. entity = String.fromCodePoint(parseInt(str, 16));
  2795. }
  2796. } else {
  2797. str = str.substr(1);
  2798. if (DECIMAL_NUMBER.test(str)) {
  2799. entity = String.fromCodePoint(parseInt(str, 10));
  2800. }
  2801. }
  2802. } else {
  2803. entity = entities[str];
  2804. }
  2805. break;
  2806. }
  2807. str += ch;
  2808. }
  2809. if (!entity) {
  2810. this.state.pos = startPos;
  2811. return "&";
  2812. }
  2813. return entity;
  2814. }
  2815. jsxReadWord() {
  2816. let ch;
  2817. const start = this.state.pos;
  2818. do {
  2819. ch = this.input.charCodeAt(++this.state.pos);
  2820. } while (isIdentifierChar(ch) || ch === 45);
  2821. return this.finishToken(types.jsxName, this.input.slice(start, this.state.pos));
  2822. }
  2823. jsxParseIdentifier() {
  2824. const node = this.startNode();
  2825. if (this.match(types.jsxName)) {
  2826. node.name = this.state.value;
  2827. } else if (this.state.type.keyword) {
  2828. node.name = this.state.type.keyword;
  2829. } else {
  2830. this.unexpected();
  2831. }
  2832. this.next();
  2833. return this.finishNode(node, "JSXIdentifier");
  2834. }
  2835. jsxParseNamespacedName() {
  2836. const startPos = this.state.start;
  2837. const startLoc = this.state.startLoc;
  2838. const name = this.jsxParseIdentifier();
  2839. if (!this.eat(types.colon)) return name;
  2840. const node = this.startNodeAt(startPos, startLoc);
  2841. node.namespace = name;
  2842. node.name = this.jsxParseIdentifier();
  2843. return this.finishNode(node, "JSXNamespacedName");
  2844. }
  2845. jsxParseElementName() {
  2846. const startPos = this.state.start;
  2847. const startLoc = this.state.startLoc;
  2848. let node = this.jsxParseNamespacedName();
  2849. while (this.eat(types.dot)) {
  2850. const newNode = this.startNodeAt(startPos, startLoc);
  2851. newNode.object = node;
  2852. newNode.property = this.jsxParseIdentifier();
  2853. node = this.finishNode(newNode, "JSXMemberExpression");
  2854. }
  2855. return node;
  2856. }
  2857. jsxParseAttributeValue() {
  2858. let node;
  2859. switch (this.state.type) {
  2860. case types.braceL:
  2861. node = this.startNode();
  2862. this.next();
  2863. node = this.jsxParseExpressionContainer(node);
  2864. if (node.expression.type === "JSXEmptyExpression") {
  2865. throw this.raise(node.start, "JSX attributes must only be assigned a non-empty expression");
  2866. } else {
  2867. return node;
  2868. }
  2869. case types.jsxTagStart:
  2870. case types.string:
  2871. return this.parseExprAtom();
  2872. default:
  2873. throw this.raise(this.state.start, "JSX value should be either an expression or a quoted JSX text");
  2874. }
  2875. }
  2876. jsxParseEmptyExpression() {
  2877. const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
  2878. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.start, this.state.startLoc);
  2879. }
  2880. jsxParseSpreadChild(node) {
  2881. this.next();
  2882. node.expression = this.parseExpression();
  2883. this.expect(types.braceR);
  2884. return this.finishNode(node, "JSXSpreadChild");
  2885. }
  2886. jsxParseExpressionContainer(node) {
  2887. if (this.match(types.braceR)) {
  2888. node.expression = this.jsxParseEmptyExpression();
  2889. } else {
  2890. node.expression = this.parseExpression();
  2891. }
  2892. this.expect(types.braceR);
  2893. return this.finishNode(node, "JSXExpressionContainer");
  2894. }
  2895. jsxParseAttribute() {
  2896. const node = this.startNode();
  2897. if (this.eat(types.braceL)) {
  2898. this.expect(types.ellipsis);
  2899. node.argument = this.parseMaybeAssign();
  2900. this.expect(types.braceR);
  2901. return this.finishNode(node, "JSXSpreadAttribute");
  2902. }
  2903. node.name = this.jsxParseNamespacedName();
  2904. node.value = this.eat(types.eq) ? this.jsxParseAttributeValue() : null;
  2905. return this.finishNode(node, "JSXAttribute");
  2906. }
  2907. jsxParseOpeningElementAt(startPos, startLoc) {
  2908. const node = this.startNodeAt(startPos, startLoc);
  2909. if (this.match(types.jsxTagEnd)) {
  2910. this.expect(types.jsxTagEnd);
  2911. return this.finishNode(node, "JSXOpeningFragment");
  2912. }
  2913. node.name = this.jsxParseElementName();
  2914. return this.jsxParseOpeningElementAfterName(node);
  2915. }
  2916. jsxParseOpeningElementAfterName(node) {
  2917. const attributes = [];
  2918. while (!this.match(types.slash) && !this.match(types.jsxTagEnd)) {
  2919. attributes.push(this.jsxParseAttribute());
  2920. }
  2921. node.attributes = attributes;
  2922. node.selfClosing = this.eat(types.slash);
  2923. this.expect(types.jsxTagEnd);
  2924. return this.finishNode(node, "JSXOpeningElement");
  2925. }
  2926. jsxParseClosingElementAt(startPos, startLoc) {
  2927. const node = this.startNodeAt(startPos, startLoc);
  2928. if (this.match(types.jsxTagEnd)) {
  2929. this.expect(types.jsxTagEnd);
  2930. return this.finishNode(node, "JSXClosingFragment");
  2931. }
  2932. node.name = this.jsxParseElementName();
  2933. this.expect(types.jsxTagEnd);
  2934. return this.finishNode(node, "JSXClosingElement");
  2935. }
  2936. jsxParseElementAt(startPos, startLoc) {
  2937. const node = this.startNodeAt(startPos, startLoc);
  2938. const children = [];
  2939. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  2940. let closingElement = null;
  2941. if (!openingElement.selfClosing) {
  2942. contents: for (;;) {
  2943. switch (this.state.type) {
  2944. case types.jsxTagStart:
  2945. startPos = this.state.start;
  2946. startLoc = this.state.startLoc;
  2947. this.next();
  2948. if (this.eat(types.slash)) {
  2949. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  2950. break contents;
  2951. }
  2952. children.push(this.jsxParseElementAt(startPos, startLoc));
  2953. break;
  2954. case types.jsxText:
  2955. children.push(this.parseExprAtom());
  2956. break;
  2957. case types.braceL:
  2958. {
  2959. const node = this.startNode();
  2960. this.next();
  2961. if (this.match(types.ellipsis)) {
  2962. children.push(this.jsxParseSpreadChild(node));
  2963. } else {
  2964. children.push(this.jsxParseExpressionContainer(node));
  2965. }
  2966. break;
  2967. }
  2968. default:
  2969. throw this.unexpected();
  2970. }
  2971. }
  2972. if (isFragment(openingElement) && !isFragment(closingElement)) {
  2973. this.raise(closingElement.start, "Expected corresponding JSX closing tag for <>");
  2974. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  2975. this.raise(closingElement.start, "Expected corresponding JSX closing tag for <" + getQualifiedJSXName(openingElement.name) + ">");
  2976. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  2977. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  2978. this.raise(closingElement.start, "Expected corresponding JSX closing tag for <" + getQualifiedJSXName(openingElement.name) + ">");
  2979. }
  2980. }
  2981. }
  2982. if (isFragment(openingElement)) {
  2983. node.openingFragment = openingElement;
  2984. node.closingFragment = closingElement;
  2985. } else {
  2986. node.openingElement = openingElement;
  2987. node.closingElement = closingElement;
  2988. }
  2989. node.children = children;
  2990. if (this.match(types.relational) && this.state.value === "<") {
  2991. this.raise(this.state.start, "Adjacent JSX elements must be wrapped in an enclosing tag. " + "Did you want a JSX fragment <>...</>?");
  2992. }
  2993. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  2994. }
  2995. jsxParseElement() {
  2996. const startPos = this.state.start;
  2997. const startLoc = this.state.startLoc;
  2998. this.next();
  2999. return this.jsxParseElementAt(startPos, startLoc);
  3000. }
  3001. parseExprAtom(refShortHandDefaultPos) {
  3002. if (this.match(types.jsxText)) {
  3003. return this.parseLiteral(this.state.value, "JSXText");
  3004. } else if (this.match(types.jsxTagStart)) {
  3005. return this.jsxParseElement();
  3006. } else if (this.isRelational("<") && this.input.charCodeAt(this.state.pos) !== 33) {
  3007. this.finishToken(types.jsxTagStart);
  3008. return this.jsxParseElement();
  3009. } else {
  3010. return super.parseExprAtom(refShortHandDefaultPos);
  3011. }
  3012. }
  3013. getTokenFromCode(code) {
  3014. if (this.state.inPropertyName) return super.getTokenFromCode(code);
  3015. const context = this.curContext();
  3016. if (context === types$1.j_expr) {
  3017. return this.jsxReadToken();
  3018. }
  3019. if (context === types$1.j_oTag || context === types$1.j_cTag) {
  3020. if (isIdentifierStart(code)) {
  3021. return this.jsxReadWord();
  3022. }
  3023. if (code === 62) {
  3024. ++this.state.pos;
  3025. return this.finishToken(types.jsxTagEnd);
  3026. }
  3027. if ((code === 34 || code === 39) && context === types$1.j_oTag) {
  3028. return this.jsxReadString(code);
  3029. }
  3030. }
  3031. if (code === 60 && this.state.exprAllowed && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  3032. ++this.state.pos;
  3033. return this.finishToken(types.jsxTagStart);
  3034. }
  3035. return super.getTokenFromCode(code);
  3036. }
  3037. updateContext(prevType) {
  3038. if (this.match(types.braceL)) {
  3039. const curContext = this.curContext();
  3040. if (curContext === types$1.j_oTag) {
  3041. this.state.context.push(types$1.braceExpression);
  3042. } else if (curContext === types$1.j_expr) {
  3043. this.state.context.push(types$1.templateQuasi);
  3044. } else {
  3045. super.updateContext(prevType);
  3046. }
  3047. this.state.exprAllowed = true;
  3048. } else if (this.match(types.slash) && prevType === types.jsxTagStart) {
  3049. this.state.context.length -= 2;
  3050. this.state.context.push(types$1.j_cTag);
  3051. this.state.exprAllowed = false;
  3052. } else {
  3053. return super.updateContext(prevType);
  3054. }
  3055. }
  3056. });
  3057. class Scope {
  3058. constructor(flags) {
  3059. this.var = [];
  3060. this.lexical = [];
  3061. this.functions = [];
  3062. this.flags = flags;
  3063. }
  3064. }
  3065. class ScopeHandler {
  3066. constructor(raise, inModule) {
  3067. this.scopeStack = [];
  3068. this.undefinedExports = new Map();
  3069. this.raise = raise;
  3070. this.inModule = inModule;
  3071. }
  3072. get inFunction() {
  3073. return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0;
  3074. }
  3075. get inGenerator() {
  3076. return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0;
  3077. }
  3078. get inAsync() {
  3079. return (this.currentVarScope().flags & SCOPE_ASYNC) > 0;
  3080. }
  3081. get allowSuper() {
  3082. return (this.currentThisScope().flags & SCOPE_SUPER) > 0;
  3083. }
  3084. get allowDirectSuper() {
  3085. return (this.currentThisScope().flags & SCOPE_DIRECT_SUPER) > 0;
  3086. }
  3087. get inNonArrowFunction() {
  3088. return (this.currentThisScope().flags & SCOPE_FUNCTION) > 0;
  3089. }
  3090. get treatFunctionsAsVar() {
  3091. return this.treatFunctionsAsVarInScope(this.currentScope());
  3092. }
  3093. createScope(flags) {
  3094. return new Scope(flags);
  3095. }
  3096. enter(flags) {
  3097. this.scopeStack.push(this.createScope(flags));
  3098. }
  3099. exit() {
  3100. this.scopeStack.pop();
  3101. }
  3102. treatFunctionsAsVarInScope(scope) {
  3103. return !!(scope.flags & SCOPE_FUNCTION || !this.inModule && scope.flags & SCOPE_PROGRAM);
  3104. }
  3105. declareName(name, bindingType, pos) {
  3106. let scope = this.currentScope();
  3107. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  3108. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  3109. if (bindingType & BIND_SCOPE_FUNCTION) {
  3110. scope.functions.push(name);
  3111. } else {
  3112. scope.lexical.push(name);
  3113. }
  3114. if (bindingType & BIND_SCOPE_LEXICAL) {
  3115. this.maybeExportDefined(scope, name);
  3116. }
  3117. } else if (bindingType & BIND_SCOPE_VAR) {
  3118. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  3119. scope = this.scopeStack[i];
  3120. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  3121. scope.var.push(name);
  3122. this.maybeExportDefined(scope, name);
  3123. if (scope.flags & SCOPE_VAR) break;
  3124. }
  3125. }
  3126. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  3127. this.undefinedExports.delete(name);
  3128. }
  3129. }
  3130. maybeExportDefined(scope, name) {
  3131. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  3132. this.undefinedExports.delete(name);
  3133. }
  3134. }
  3135. checkRedeclarationInScope(scope, name, bindingType, pos) {
  3136. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  3137. this.raise(pos, `Identifier '${name}' has already been declared`);
  3138. }
  3139. }
  3140. isRedeclaredInScope(scope, name, bindingType) {
  3141. if (!(bindingType & BIND_KIND_VALUE)) return false;
  3142. if (bindingType & BIND_SCOPE_LEXICAL) {
  3143. return scope.lexical.indexOf(name) > -1 || scope.functions.indexOf(name) > -1 || scope.var.indexOf(name) > -1;
  3144. }
  3145. if (bindingType & BIND_SCOPE_FUNCTION) {
  3146. return scope.lexical.indexOf(name) > -1 || !this.treatFunctionsAsVarInScope(scope) && scope.var.indexOf(name) > -1;
  3147. }
  3148. return scope.lexical.indexOf(name) > -1 && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical[0] === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.indexOf(name) > -1;
  3149. }
  3150. checkLocalExport(id) {
  3151. if (this.scopeStack[0].lexical.indexOf(id.name) === -1 && this.scopeStack[0].var.indexOf(id.name) === -1 && this.scopeStack[0].functions.indexOf(id.name) === -1) {
  3152. this.undefinedExports.set(id.name, id.start);
  3153. }
  3154. }
  3155. currentScope() {
  3156. return this.scopeStack[this.scopeStack.length - 1];
  3157. }
  3158. currentVarScope() {
  3159. for (let i = this.scopeStack.length - 1;; i--) {
  3160. const scope = this.scopeStack[i];
  3161. if (scope.flags & SCOPE_VAR) {
  3162. return scope;
  3163. }
  3164. }
  3165. }
  3166. currentThisScope() {
  3167. for (let i = this.scopeStack.length - 1;; i--) {
  3168. const scope = this.scopeStack[i];
  3169. if ((scope.flags & SCOPE_VAR || scope.flags & SCOPE_CLASS) && !(scope.flags & SCOPE_ARROW)) {
  3170. return scope;
  3171. }
  3172. }
  3173. }
  3174. }
  3175. class TypeScriptScope extends Scope {
  3176. constructor(...args) {
  3177. super(...args);
  3178. this.types = [];
  3179. this.enums = [];
  3180. this.constEnums = [];
  3181. this.classes = [];
  3182. this.exportOnlyBindings = [];
  3183. }
  3184. }
  3185. class TypeScriptScopeHandler extends ScopeHandler {
  3186. createScope(flags) {
  3187. return new TypeScriptScope(flags);
  3188. }
  3189. declareName(name, bindingType, pos) {
  3190. const scope = this.currentScope();
  3191. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  3192. this.maybeExportDefined(scope, name);
  3193. scope.exportOnlyBindings.push(name);
  3194. return;
  3195. }
  3196. super.declareName(...arguments);
  3197. if (bindingType & BIND_KIND_TYPE) {
  3198. if (!(bindingType & BIND_KIND_VALUE)) {
  3199. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  3200. this.maybeExportDefined(scope, name);
  3201. }
  3202. scope.types.push(name);
  3203. }
  3204. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.push(name);
  3205. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.push(name);
  3206. if (bindingType & BIND_FLAGS_CLASS) scope.classes.push(name);
  3207. }
  3208. isRedeclaredInScope(scope, name, bindingType) {
  3209. if (scope.enums.indexOf(name) > -1) {
  3210. if (bindingType & BIND_FLAGS_TS_ENUM) {
  3211. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  3212. const wasConst = scope.constEnums.indexOf(name) > -1;
  3213. return isConst !== wasConst;
  3214. }
  3215. return true;
  3216. }
  3217. if (bindingType & BIND_FLAGS_CLASS && scope.classes.indexOf(name) > -1) {
  3218. if (scope.lexical.indexOf(name) > -1) {
  3219. return !!(bindingType & BIND_KIND_VALUE);
  3220. } else {
  3221. return false;
  3222. }
  3223. }
  3224. if (bindingType & BIND_KIND_TYPE && scope.types.indexOf(name) > -1) {
  3225. return true;
  3226. }
  3227. return super.isRedeclaredInScope(...arguments);
  3228. }
  3229. checkLocalExport(id) {
  3230. if (this.scopeStack[0].types.indexOf(id.name) === -1 && this.scopeStack[0].exportOnlyBindings.indexOf(id.name) === -1) {
  3231. super.checkLocalExport(id);
  3232. }
  3233. }
  3234. }
  3235. function nonNull(x) {
  3236. if (x == null) {
  3237. throw new Error(`Unexpected ${x} value.`);
  3238. }
  3239. return x;
  3240. }
  3241. function assert(x) {
  3242. if (!x) {
  3243. throw new Error("Assert fail");
  3244. }
  3245. }
  3246. function keywordTypeFromName(value) {
  3247. switch (value) {
  3248. case "any":
  3249. return "TSAnyKeyword";
  3250. case "boolean":
  3251. return "TSBooleanKeyword";
  3252. case "bigint":
  3253. return "TSBigIntKeyword";
  3254. case "never":
  3255. return "TSNeverKeyword";
  3256. case "number":
  3257. return "TSNumberKeyword";
  3258. case "object":
  3259. return "TSObjectKeyword";
  3260. case "string":
  3261. return "TSStringKeyword";
  3262. case "symbol":
  3263. return "TSSymbolKeyword";
  3264. case "undefined":
  3265. return "TSUndefinedKeyword";
  3266. case "unknown":
  3267. return "TSUnknownKeyword";
  3268. default:
  3269. return undefined;
  3270. }
  3271. }
  3272. var typescript = (superClass => class extends superClass {
  3273. getScopeHandler() {
  3274. return TypeScriptScopeHandler;
  3275. }
  3276. tsIsIdentifier() {
  3277. return this.match(types.name);
  3278. }
  3279. tsNextTokenCanFollowModifier() {
  3280. this.next();
  3281. return !this.hasPrecedingLineBreak() && !this.match(types.parenL) && !this.match(types.parenR) && !this.match(types.colon) && !this.match(types.eq) && !this.match(types.question) && !this.match(types.bang);
  3282. }
  3283. tsParseModifier(allowedModifiers) {
  3284. if (!this.match(types.name)) {
  3285. return undefined;
  3286. }
  3287. const modifier = this.state.value;
  3288. if (allowedModifiers.indexOf(modifier) !== -1 && this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  3289. return modifier;
  3290. }
  3291. return undefined;
  3292. }
  3293. tsIsListTerminator(kind) {
  3294. switch (kind) {
  3295. case "EnumMembers":
  3296. case "TypeMembers":
  3297. return this.match(types.braceR);
  3298. case "HeritageClauseElement":
  3299. return this.match(types.braceL);
  3300. case "TupleElementTypes":
  3301. return this.match(types.bracketR);
  3302. case "TypeParametersOrArguments":
  3303. return this.isRelational(">");
  3304. }
  3305. throw new Error("Unreachable");
  3306. }
  3307. tsParseList(kind, parseElement) {
  3308. const result = [];
  3309. while (!this.tsIsListTerminator(kind)) {
  3310. result.push(parseElement());
  3311. }
  3312. return result;
  3313. }
  3314. tsParseDelimitedList(kind, parseElement) {
  3315. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true));
  3316. }
  3317. tsParseDelimitedListWorker(kind, parseElement, expectSuccess) {
  3318. const result = [];
  3319. while (true) {
  3320. if (this.tsIsListTerminator(kind)) {
  3321. break;
  3322. }
  3323. const element = parseElement();
  3324. if (element == null) {
  3325. return undefined;
  3326. }
  3327. result.push(element);
  3328. if (this.eat(types.comma)) {
  3329. continue;
  3330. }
  3331. if (this.tsIsListTerminator(kind)) {
  3332. break;
  3333. }
  3334. if (expectSuccess) {
  3335. this.expect(types.comma);
  3336. }
  3337. return undefined;
  3338. }
  3339. return result;
  3340. }
  3341. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken) {
  3342. if (!skipFirstToken) {
  3343. if (bracket) {
  3344. this.expect(types.bracketL);
  3345. } else {
  3346. this.expectRelational("<");
  3347. }
  3348. }
  3349. const result = this.tsParseDelimitedList(kind, parseElement);
  3350. if (bracket) {
  3351. this.expect(types.bracketR);
  3352. } else {
  3353. this.expectRelational(">");
  3354. }
  3355. return result;
  3356. }
  3357. tsParseImportType() {
  3358. const node = this.startNode();
  3359. this.expect(types._import);
  3360. this.expect(types.parenL);
  3361. if (!this.match(types.string)) {
  3362. throw this.unexpected(null, "Argument in a type import must be a string literal");
  3363. }
  3364. node.argument = this.parseExprAtom();
  3365. this.expect(types.parenR);
  3366. if (this.eat(types.dot)) {
  3367. node.qualifier = this.tsParseEntityName(true);
  3368. }
  3369. if (this.isRelational("<")) {
  3370. node.typeParameters = this.tsParseTypeArguments();
  3371. }
  3372. return this.finishNode(node, "TSImportType");
  3373. }
  3374. tsParseEntityName(allowReservedWords) {
  3375. let entity = this.parseIdentifier();
  3376. while (this.eat(types.dot)) {
  3377. const node = this.startNodeAtNode(entity);
  3378. node.left = entity;
  3379. node.right = this.parseIdentifier(allowReservedWords);
  3380. entity = this.finishNode(node, "TSQualifiedName");
  3381. }
  3382. return entity;
  3383. }
  3384. tsParseTypeReference() {
  3385. const node = this.startNode();
  3386. node.typeName = this.tsParseEntityName(false);
  3387. if (!this.hasPrecedingLineBreak() && this.isRelational("<")) {
  3388. node.typeParameters = this.tsParseTypeArguments();
  3389. }
  3390. return this.finishNode(node, "TSTypeReference");
  3391. }
  3392. tsParseThisTypePredicate(lhs) {
  3393. this.next();
  3394. const node = this.startNodeAtNode(lhs);
  3395. node.parameterName = lhs;
  3396. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  3397. return this.finishNode(node, "TSTypePredicate");
  3398. }
  3399. tsParseThisTypeNode() {
  3400. const node = this.startNode();
  3401. this.next();
  3402. return this.finishNode(node, "TSThisType");
  3403. }
  3404. tsParseTypeQuery() {
  3405. const node = this.startNode();
  3406. this.expect(types._typeof);
  3407. if (this.match(types._import)) {
  3408. node.exprName = this.tsParseImportType();
  3409. } else {
  3410. node.exprName = this.tsParseEntityName(true);
  3411. }
  3412. return this.finishNode(node, "TSTypeQuery");
  3413. }
  3414. tsParseTypeParameter() {
  3415. const node = this.startNode();
  3416. node.name = this.parseIdentifierName(node.start);
  3417. node.constraint = this.tsEatThenParseType(types._extends);
  3418. node.default = this.tsEatThenParseType(types.eq);
  3419. return this.finishNode(node, "TSTypeParameter");
  3420. }
  3421. tsTryParseTypeParameters() {
  3422. if (this.isRelational("<")) {
  3423. return this.tsParseTypeParameters();
  3424. }
  3425. }
  3426. tsParseTypeParameters() {
  3427. const node = this.startNode();
  3428. if (this.isRelational("<") || this.match(types.jsxTagStart)) {
  3429. this.next();
  3430. } else {
  3431. this.unexpected();
  3432. }
  3433. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true);
  3434. return this.finishNode(node, "TSTypeParameterDeclaration");
  3435. }
  3436. tsTryNextParseConstantContext() {
  3437. if (this.lookahead().type === types._const) {
  3438. this.next();
  3439. return this.tsParseTypeReference();
  3440. }
  3441. return null;
  3442. }
  3443. tsFillSignature(returnToken, signature) {
  3444. const returnTokenRequired = returnToken === types.arrow;
  3445. signature.typeParameters = this.tsTryParseTypeParameters();
  3446. this.expect(types.parenL);
  3447. signature.parameters = this.tsParseBindingListForSignature();
  3448. if (returnTokenRequired) {
  3449. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  3450. } else if (this.match(returnToken)) {
  3451. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  3452. }
  3453. }
  3454. tsParseBindingListForSignature() {
  3455. return this.parseBindingList(types.parenR).map(pattern => {
  3456. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  3457. throw this.unexpected(pattern.start, `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${pattern.type}`);
  3458. }
  3459. return pattern;
  3460. });
  3461. }
  3462. tsParseTypeMemberSemicolon() {
  3463. if (!this.eat(types.comma)) {
  3464. this.semicolon();
  3465. }
  3466. }
  3467. tsParseSignatureMember(kind, node) {
  3468. this.tsFillSignature(types.colon, node);
  3469. this.tsParseTypeMemberSemicolon();
  3470. return this.finishNode(node, kind);
  3471. }
  3472. tsIsUnambiguouslyIndexSignature() {
  3473. this.next();
  3474. return this.eat(types.name) && this.match(types.colon);
  3475. }
  3476. tsTryParseIndexSignature(node) {
  3477. if (!(this.match(types.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  3478. return undefined;
  3479. }
  3480. this.expect(types.bracketL);
  3481. const id = this.parseIdentifier();
  3482. id.typeAnnotation = this.tsParseTypeAnnotation();
  3483. this.resetEndLocation(id);
  3484. this.expect(types.bracketR);
  3485. node.parameters = [id];
  3486. const type = this.tsTryParseTypeAnnotation();
  3487. if (type) node.typeAnnotation = type;
  3488. this.tsParseTypeMemberSemicolon();
  3489. return this.finishNode(node, "TSIndexSignature");
  3490. }
  3491. tsParsePropertyOrMethodSignature(node, readonly) {
  3492. if (this.eat(types.question)) node.optional = true;
  3493. const nodeAny = node;
  3494. if (!readonly && (this.match(types.parenL) || this.isRelational("<"))) {
  3495. const method = nodeAny;
  3496. this.tsFillSignature(types.colon, method);
  3497. this.tsParseTypeMemberSemicolon();
  3498. return this.finishNode(method, "TSMethodSignature");
  3499. } else {
  3500. const property = nodeAny;
  3501. if (readonly) property.readonly = true;
  3502. const type = this.tsTryParseTypeAnnotation();
  3503. if (type) property.typeAnnotation = type;
  3504. this.tsParseTypeMemberSemicolon();
  3505. return this.finishNode(property, "TSPropertySignature");
  3506. }
  3507. }
  3508. tsParseTypeMember() {
  3509. const node = this.startNode();
  3510. if (this.match(types.parenL) || this.isRelational("<")) {
  3511. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  3512. }
  3513. if (this.match(types._new)) {
  3514. const id = this.startNode();
  3515. this.next();
  3516. if (this.match(types.parenL) || this.isRelational("<")) {
  3517. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  3518. } else {
  3519. node.key = this.createIdentifier(id, "new");
  3520. return this.tsParsePropertyOrMethodSignature(node, false);
  3521. }
  3522. }
  3523. const readonly = !!this.tsParseModifier(["readonly"]);
  3524. const idx = this.tsTryParseIndexSignature(node);
  3525. if (idx) {
  3526. if (readonly) node.readonly = true;
  3527. return idx;
  3528. }
  3529. this.parsePropertyName(node);
  3530. return this.tsParsePropertyOrMethodSignature(node, readonly);
  3531. }
  3532. tsParseTypeLiteral() {
  3533. const node = this.startNode();
  3534. node.members = this.tsParseObjectTypeMembers();
  3535. return this.finishNode(node, "TSTypeLiteral");
  3536. }
  3537. tsParseObjectTypeMembers() {
  3538. this.expect(types.braceL);
  3539. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  3540. this.expect(types.braceR);
  3541. return members;
  3542. }
  3543. tsIsStartOfMappedType() {
  3544. this.next();
  3545. if (this.eat(types.plusMin)) {
  3546. return this.isContextual("readonly");
  3547. }
  3548. if (this.isContextual("readonly")) {
  3549. this.next();
  3550. }
  3551. if (!this.match(types.bracketL)) {
  3552. return false;
  3553. }
  3554. this.next();
  3555. if (!this.tsIsIdentifier()) {
  3556. return false;
  3557. }
  3558. this.next();
  3559. return this.match(types._in);
  3560. }
  3561. tsParseMappedTypeParameter() {
  3562. const node = this.startNode();
  3563. node.name = this.parseIdentifierName(node.start);
  3564. node.constraint = this.tsExpectThenParseType(types._in);
  3565. return this.finishNode(node, "TSTypeParameter");
  3566. }
  3567. tsParseMappedType() {
  3568. const node = this.startNode();
  3569. this.expect(types.braceL);
  3570. if (this.match(types.plusMin)) {
  3571. node.readonly = this.state.value;
  3572. this.next();
  3573. this.expectContextual("readonly");
  3574. } else if (this.eatContextual("readonly")) {
  3575. node.readonly = true;
  3576. }
  3577. this.expect(types.bracketL);
  3578. node.typeParameter = this.tsParseMappedTypeParameter();
  3579. this.expect(types.bracketR);
  3580. if (this.match(types.plusMin)) {
  3581. node.optional = this.state.value;
  3582. this.next();
  3583. this.expect(types.question);
  3584. } else if (this.eat(types.question)) {
  3585. node.optional = true;
  3586. }
  3587. node.typeAnnotation = this.tsTryParseType();
  3588. this.semicolon();
  3589. this.expect(types.braceR);
  3590. return this.finishNode(node, "TSMappedType");
  3591. }
  3592. tsParseTupleType() {
  3593. const node = this.startNode();
  3594. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  3595. let seenOptionalElement = false;
  3596. node.elementTypes.forEach(elementNode => {
  3597. if (elementNode.type === "TSOptionalType") {
  3598. seenOptionalElement = true;
  3599. } else if (seenOptionalElement && elementNode.type !== "TSRestType") {
  3600. this.raise(elementNode.start, "A required element cannot follow an optional element.");
  3601. }
  3602. });
  3603. return this.finishNode(node, "TSTupleType");
  3604. }
  3605. tsParseTupleElementType() {
  3606. if (this.match(types.ellipsis)) {
  3607. const restNode = this.startNode();
  3608. this.next();
  3609. restNode.typeAnnotation = this.tsParseType();
  3610. this.checkCommaAfterRest();
  3611. return this.finishNode(restNode, "TSRestType");
  3612. }
  3613. const type = this.tsParseType();
  3614. if (this.eat(types.question)) {
  3615. const optionalTypeNode = this.startNodeAtNode(type);
  3616. optionalTypeNode.typeAnnotation = type;
  3617. return this.finishNode(optionalTypeNode, "TSOptionalType");
  3618. }
  3619. return type;
  3620. }
  3621. tsParseParenthesizedType() {
  3622. const node = this.startNode();
  3623. this.expect(types.parenL);
  3624. node.typeAnnotation = this.tsParseType();
  3625. this.expect(types.parenR);
  3626. return this.finishNode(node, "TSParenthesizedType");
  3627. }
  3628. tsParseFunctionOrConstructorType(type) {
  3629. const node = this.startNode();
  3630. if (type === "TSConstructorType") {
  3631. this.expect(types._new);
  3632. }
  3633. this.tsFillSignature(types.arrow, node);
  3634. return this.finishNode(node, type);
  3635. }
  3636. tsParseLiteralTypeNode() {
  3637. const node = this.startNode();
  3638. node.literal = (() => {
  3639. switch (this.state.type) {
  3640. case types.num:
  3641. case types.string:
  3642. case types._true:
  3643. case types._false:
  3644. return this.parseExprAtom();
  3645. default:
  3646. throw this.unexpected();
  3647. }
  3648. })();
  3649. return this.finishNode(node, "TSLiteralType");
  3650. }
  3651. tsParseTemplateLiteralType() {
  3652. const node = this.startNode();
  3653. const templateNode = this.parseTemplate(false);
  3654. if (templateNode.expressions.length > 0) {
  3655. throw this.raise(templateNode.expressions[0].start, "Template literal types cannot have any substitution");
  3656. }
  3657. node.literal = templateNode;
  3658. return this.finishNode(node, "TSLiteralType");
  3659. }
  3660. tsParseNonArrayType() {
  3661. switch (this.state.type) {
  3662. case types.name:
  3663. case types._void:
  3664. case types._null:
  3665. {
  3666. const type = this.match(types._void) ? "TSVoidKeyword" : this.match(types._null) ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  3667. if (type !== undefined && this.lookahead().type !== types.dot) {
  3668. const node = this.startNode();
  3669. this.next();
  3670. return this.finishNode(node, type);
  3671. }
  3672. return this.tsParseTypeReference();
  3673. }
  3674. case types.string:
  3675. case types.num:
  3676. case types._true:
  3677. case types._false:
  3678. return this.tsParseLiteralTypeNode();
  3679. case types.plusMin:
  3680. if (this.state.value === "-") {
  3681. const node = this.startNode();
  3682. if (this.lookahead().type !== types.num) {
  3683. throw this.unexpected();
  3684. }
  3685. node.literal = this.parseMaybeUnary();
  3686. return this.finishNode(node, "TSLiteralType");
  3687. }
  3688. break;
  3689. case types._this:
  3690. {
  3691. const thisKeyword = this.tsParseThisTypeNode();
  3692. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  3693. return this.tsParseThisTypePredicate(thisKeyword);
  3694. } else {
  3695. return thisKeyword;
  3696. }
  3697. }
  3698. case types._typeof:
  3699. return this.tsParseTypeQuery();
  3700. case types._import:
  3701. return this.tsParseImportType();
  3702. case types.braceL:
  3703. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  3704. case types.bracketL:
  3705. return this.tsParseTupleType();
  3706. case types.parenL:
  3707. return this.tsParseParenthesizedType();
  3708. case types.backQuote:
  3709. return this.tsParseTemplateLiteralType();
  3710. }
  3711. throw this.unexpected();
  3712. }
  3713. tsParseArrayTypeOrHigher() {
  3714. let type = this.tsParseNonArrayType();
  3715. while (!this.hasPrecedingLineBreak() && this.eat(types.bracketL)) {
  3716. if (this.match(types.bracketR)) {
  3717. const node = this.startNodeAtNode(type);
  3718. node.elementType = type;
  3719. this.expect(types.bracketR);
  3720. type = this.finishNode(node, "TSArrayType");
  3721. } else {
  3722. const node = this.startNodeAtNode(type);
  3723. node.objectType = type;
  3724. node.indexType = this.tsParseType();
  3725. this.expect(types.bracketR);
  3726. type = this.finishNode(node, "TSIndexedAccessType");
  3727. }
  3728. }
  3729. return type;
  3730. }
  3731. tsParseTypeOperator(operator) {
  3732. const node = this.startNode();
  3733. this.expectContextual(operator);
  3734. node.operator = operator;
  3735. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  3736. if (operator === "readonly") {
  3737. this.tsCheckTypeAnnotationForReadOnly(node);
  3738. }
  3739. return this.finishNode(node, "TSTypeOperator");
  3740. }
  3741. tsCheckTypeAnnotationForReadOnly(node) {
  3742. switch (node.typeAnnotation.type) {
  3743. case "TSTupleType":
  3744. case "TSArrayType":
  3745. return;
  3746. default:
  3747. this.raise(node.start, "'readonly' type modifier is only permitted on array and tuple literal types.");
  3748. }
  3749. }
  3750. tsParseInferType() {
  3751. const node = this.startNode();
  3752. this.expectContextual("infer");
  3753. const typeParameter = this.startNode();
  3754. typeParameter.name = this.parseIdentifierName(typeParameter.start);
  3755. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  3756. return this.finishNode(node, "TSInferType");
  3757. }
  3758. tsParseTypeOperatorOrHigher() {
  3759. const operator = ["keyof", "unique", "readonly"].find(kw => this.isContextual(kw));
  3760. return operator ? this.tsParseTypeOperator(operator) : this.isContextual("infer") ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  3761. }
  3762. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  3763. this.eat(operator);
  3764. let type = parseConstituentType();
  3765. if (this.match(operator)) {
  3766. const types = [type];
  3767. while (this.eat(operator)) {
  3768. types.push(parseConstituentType());
  3769. }
  3770. const node = this.startNodeAtNode(type);
  3771. node.types = types;
  3772. type = this.finishNode(node, kind);
  3773. }
  3774. return type;
  3775. }
  3776. tsParseIntersectionTypeOrHigher() {
  3777. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), types.bitwiseAND);
  3778. }
  3779. tsParseUnionTypeOrHigher() {
  3780. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), types.bitwiseOR);
  3781. }
  3782. tsIsStartOfFunctionType() {
  3783. if (this.isRelational("<")) {
  3784. return true;
  3785. }
  3786. return this.match(types.parenL) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  3787. }
  3788. tsSkipParameterStart() {
  3789. if (this.match(types.name) || this.match(types._this)) {
  3790. this.next();
  3791. return true;
  3792. }
  3793. if (this.match(types.braceL)) {
  3794. let braceStackCounter = 1;
  3795. this.next();
  3796. while (braceStackCounter > 0) {
  3797. if (this.match(types.braceL)) {
  3798. ++braceStackCounter;
  3799. } else if (this.match(types.braceR)) {
  3800. --braceStackCounter;
  3801. }
  3802. this.next();
  3803. }
  3804. return true;
  3805. }
  3806. if (this.match(types.bracketL)) {
  3807. let braceStackCounter = 1;
  3808. this.next();
  3809. while (braceStackCounter > 0) {
  3810. if (this.match(types.bracketL)) {
  3811. ++braceStackCounter;
  3812. } else if (this.match(types.bracketR)) {
  3813. --braceStackCounter;
  3814. }
  3815. this.next();
  3816. }
  3817. return true;
  3818. }
  3819. return false;
  3820. }
  3821. tsIsUnambiguouslyStartOfFunctionType() {
  3822. this.next();
  3823. if (this.match(types.parenR) || this.match(types.ellipsis)) {
  3824. return true;
  3825. }
  3826. if (this.tsSkipParameterStart()) {
  3827. if (this.match(types.colon) || this.match(types.comma) || this.match(types.question) || this.match(types.eq)) {
  3828. return true;
  3829. }
  3830. if (this.match(types.parenR)) {
  3831. this.next();
  3832. if (this.match(types.arrow)) {
  3833. return true;
  3834. }
  3835. }
  3836. }
  3837. return false;
  3838. }
  3839. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  3840. return this.tsInType(() => {
  3841. const t = this.startNode();
  3842. this.expect(returnToken);
  3843. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  3844. if (!typePredicateVariable) {
  3845. return this.tsParseTypeAnnotation(false, t);
  3846. }
  3847. const type = this.tsParseTypeAnnotation(false);
  3848. const node = this.startNodeAtNode(typePredicateVariable);
  3849. node.parameterName = typePredicateVariable;
  3850. node.typeAnnotation = type;
  3851. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  3852. return this.finishNode(t, "TSTypeAnnotation");
  3853. });
  3854. }
  3855. tsTryParseTypeOrTypePredicateAnnotation() {
  3856. return this.match(types.colon) ? this.tsParseTypeOrTypePredicateAnnotation(types.colon) : undefined;
  3857. }
  3858. tsTryParseTypeAnnotation() {
  3859. return this.match(types.colon) ? this.tsParseTypeAnnotation() : undefined;
  3860. }
  3861. tsTryParseType() {
  3862. return this.tsEatThenParseType(types.colon);
  3863. }
  3864. tsParseTypePredicatePrefix() {
  3865. const id = this.parseIdentifier();
  3866. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  3867. this.next();
  3868. return id;
  3869. }
  3870. }
  3871. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  3872. this.tsInType(() => {
  3873. if (eatColon) this.expect(types.colon);
  3874. t.typeAnnotation = this.tsParseType();
  3875. });
  3876. return this.finishNode(t, "TSTypeAnnotation");
  3877. }
  3878. tsParseType() {
  3879. assert(this.state.inType);
  3880. const type = this.tsParseNonConditionalType();
  3881. if (this.hasPrecedingLineBreak() || !this.eat(types._extends)) {
  3882. return type;
  3883. }
  3884. const node = this.startNodeAtNode(type);
  3885. node.checkType = type;
  3886. node.extendsType = this.tsParseNonConditionalType();
  3887. this.expect(types.question);
  3888. node.trueType = this.tsParseType();
  3889. this.expect(types.colon);
  3890. node.falseType = this.tsParseType();
  3891. return this.finishNode(node, "TSConditionalType");
  3892. }
  3893. tsParseNonConditionalType() {
  3894. if (this.tsIsStartOfFunctionType()) {
  3895. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  3896. }
  3897. if (this.match(types._new)) {
  3898. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  3899. }
  3900. return this.tsParseUnionTypeOrHigher();
  3901. }
  3902. tsParseTypeAssertion() {
  3903. const node = this.startNode();
  3904. const _const = this.tsTryNextParseConstantContext();
  3905. node.typeAnnotation = _const || this.tsNextThenParseType();
  3906. this.expectRelational(">");
  3907. node.expression = this.parseMaybeUnary();
  3908. return this.finishNode(node, "TSTypeAssertion");
  3909. }
  3910. tsParseHeritageClause(descriptor) {
  3911. const originalStart = this.state.start;
  3912. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  3913. if (!delimitedList.length) {
  3914. this.raise(originalStart, `'${descriptor}' list cannot be empty.`);
  3915. }
  3916. return delimitedList;
  3917. }
  3918. tsParseExpressionWithTypeArguments() {
  3919. const node = this.startNode();
  3920. node.expression = this.tsParseEntityName(false);
  3921. if (this.isRelational("<")) {
  3922. node.typeParameters = this.tsParseTypeArguments();
  3923. }
  3924. return this.finishNode(node, "TSExpressionWithTypeArguments");
  3925. }
  3926. tsParseInterfaceDeclaration(node) {
  3927. node.id = this.parseIdentifier();
  3928. this.checkLVal(node.id, BIND_TS_INTERFACE, undefined, "typescript interface declaration");
  3929. node.typeParameters = this.tsTryParseTypeParameters();
  3930. if (this.eat(types._extends)) {
  3931. node.extends = this.tsParseHeritageClause("extends");
  3932. }
  3933. const body = this.startNode();
  3934. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  3935. node.body = this.finishNode(body, "TSInterfaceBody");
  3936. return this.finishNode(node, "TSInterfaceDeclaration");
  3937. }
  3938. tsParseTypeAliasDeclaration(node) {
  3939. node.id = this.parseIdentifier();
  3940. this.checkLVal(node.id, BIND_TS_TYPE, undefined, "typescript type alias");
  3941. node.typeParameters = this.tsTryParseTypeParameters();
  3942. node.typeAnnotation = this.tsExpectThenParseType(types.eq);
  3943. this.semicolon();
  3944. return this.finishNode(node, "TSTypeAliasDeclaration");
  3945. }
  3946. tsInNoContext(cb) {
  3947. const oldContext = this.state.context;
  3948. this.state.context = [oldContext[0]];
  3949. try {
  3950. return cb();
  3951. } finally {
  3952. this.state.context = oldContext;
  3953. }
  3954. }
  3955. tsInType(cb) {
  3956. const oldInType = this.state.inType;
  3957. this.state.inType = true;
  3958. try {
  3959. return cb();
  3960. } finally {
  3961. this.state.inType = oldInType;
  3962. }
  3963. }
  3964. tsEatThenParseType(token) {
  3965. return !this.match(token) ? undefined : this.tsNextThenParseType();
  3966. }
  3967. tsExpectThenParseType(token) {
  3968. return this.tsDoThenParseType(() => this.expect(token));
  3969. }
  3970. tsNextThenParseType() {
  3971. return this.tsDoThenParseType(() => this.next());
  3972. }
  3973. tsDoThenParseType(cb) {
  3974. return this.tsInType(() => {
  3975. cb();
  3976. return this.tsParseType();
  3977. });
  3978. }
  3979. tsParseEnumMember() {
  3980. const node = this.startNode();
  3981. node.id = this.match(types.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  3982. if (this.eat(types.eq)) {
  3983. node.initializer = this.parseMaybeAssign();
  3984. }
  3985. return this.finishNode(node, "TSEnumMember");
  3986. }
  3987. tsParseEnumDeclaration(node, isConst) {
  3988. if (isConst) node.const = true;
  3989. node.id = this.parseIdentifier();
  3990. this.checkLVal(node.id, isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM, undefined, "typescript enum declaration");
  3991. this.expect(types.braceL);
  3992. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  3993. this.expect(types.braceR);
  3994. return this.finishNode(node, "TSEnumDeclaration");
  3995. }
  3996. tsParseModuleBlock() {
  3997. const node = this.startNode();
  3998. this.scope.enter(SCOPE_OTHER);
  3999. this.expect(types.braceL);
  4000. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, types.braceR);
  4001. this.scope.exit();
  4002. return this.finishNode(node, "TSModuleBlock");
  4003. }
  4004. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  4005. node.id = this.parseIdentifier();
  4006. if (!nested) {
  4007. this.checkLVal(node.id, BIND_TS_NAMESPACE, null, "module or namespace declaration");
  4008. }
  4009. if (this.eat(types.dot)) {
  4010. const inner = this.startNode();
  4011. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  4012. node.body = inner;
  4013. } else {
  4014. node.body = this.tsParseModuleBlock();
  4015. }
  4016. return this.finishNode(node, "TSModuleDeclaration");
  4017. }
  4018. tsParseAmbientExternalModuleDeclaration(node) {
  4019. if (this.isContextual("global")) {
  4020. node.global = true;
  4021. node.id = this.parseIdentifier();
  4022. } else if (this.match(types.string)) {
  4023. node.id = this.parseExprAtom();
  4024. } else {
  4025. this.unexpected();
  4026. }
  4027. if (this.match(types.braceL)) {
  4028. node.body = this.tsParseModuleBlock();
  4029. } else {
  4030. this.semicolon();
  4031. }
  4032. return this.finishNode(node, "TSModuleDeclaration");
  4033. }
  4034. tsParseImportEqualsDeclaration(node, isExport) {
  4035. node.isExport = isExport || false;
  4036. node.id = this.parseIdentifier();
  4037. this.expect(types.eq);
  4038. node.moduleReference = this.tsParseModuleReference();
  4039. this.semicolon();
  4040. return this.finishNode(node, "TSImportEqualsDeclaration");
  4041. }
  4042. tsIsExternalModuleReference() {
  4043. return this.isContextual("require") && this.lookahead().type === types.parenL;
  4044. }
  4045. tsParseModuleReference() {
  4046. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  4047. }
  4048. tsParseExternalModuleReference() {
  4049. const node = this.startNode();
  4050. this.expectContextual("require");
  4051. this.expect(types.parenL);
  4052. if (!this.match(types.string)) {
  4053. throw this.unexpected();
  4054. }
  4055. node.expression = this.parseExprAtom();
  4056. this.expect(types.parenR);
  4057. return this.finishNode(node, "TSExternalModuleReference");
  4058. }
  4059. tsLookAhead(f) {
  4060. const state = this.state.clone();
  4061. const res = f();
  4062. this.state = state;
  4063. return res;
  4064. }
  4065. tsTryParseAndCatch(f) {
  4066. const state = this.state.clone();
  4067. try {
  4068. return f();
  4069. } catch (e) {
  4070. if (e instanceof SyntaxError) {
  4071. this.state = state;
  4072. return undefined;
  4073. }
  4074. throw e;
  4075. }
  4076. }
  4077. tsTryParse(f) {
  4078. const state = this.state.clone();
  4079. const result = f();
  4080. if (result !== undefined && result !== false) {
  4081. return result;
  4082. } else {
  4083. this.state = state;
  4084. return undefined;
  4085. }
  4086. }
  4087. tsTryParseDeclare(nany) {
  4088. if (this.isLineTerminator()) {
  4089. return;
  4090. }
  4091. let starttype = this.state.type;
  4092. let kind;
  4093. if (this.isContextual("let")) {
  4094. starttype = types._var;
  4095. kind = "let";
  4096. }
  4097. switch (starttype) {
  4098. case types._function:
  4099. return this.parseFunctionStatement(nany, false, true);
  4100. case types._class:
  4101. return this.parseClass(nany, true, false);
  4102. case types._const:
  4103. if (this.match(types._const) && this.isLookaheadContextual("enum")) {
  4104. this.expect(types._const);
  4105. this.expectContextual("enum");
  4106. return this.tsParseEnumDeclaration(nany, true);
  4107. }
  4108. case types._var:
  4109. kind = kind || this.state.value;
  4110. return this.parseVarStatement(nany, kind);
  4111. case types.name:
  4112. {
  4113. const value = this.state.value;
  4114. if (value === "global") {
  4115. return this.tsParseAmbientExternalModuleDeclaration(nany);
  4116. } else {
  4117. return this.tsParseDeclaration(nany, value, true);
  4118. }
  4119. }
  4120. }
  4121. }
  4122. tsTryParseExportDeclaration() {
  4123. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  4124. }
  4125. tsParseExpressionStatement(node, expr) {
  4126. switch (expr.name) {
  4127. case "declare":
  4128. {
  4129. const declaration = this.tsTryParseDeclare(node);
  4130. if (declaration) {
  4131. declaration.declare = true;
  4132. return declaration;
  4133. }
  4134. break;
  4135. }
  4136. case "global":
  4137. if (this.match(types.braceL)) {
  4138. const mod = node;
  4139. mod.global = true;
  4140. mod.id = expr;
  4141. mod.body = this.tsParseModuleBlock();
  4142. return this.finishNode(mod, "TSModuleDeclaration");
  4143. }
  4144. break;
  4145. default:
  4146. return this.tsParseDeclaration(node, expr.name, false);
  4147. }
  4148. }
  4149. tsParseDeclaration(node, value, next) {
  4150. switch (value) {
  4151. case "abstract":
  4152. if (this.tsCheckLineTerminatorAndMatch(types._class, next)) {
  4153. const cls = node;
  4154. cls.abstract = true;
  4155. if (next) {
  4156. this.next();
  4157. if (!this.match(types._class)) {
  4158. this.unexpected(null, types._class);
  4159. }
  4160. }
  4161. return this.parseClass(cls, true, false);
  4162. }
  4163. break;
  4164. case "enum":
  4165. if (next || this.match(types.name)) {
  4166. if (next) this.next();
  4167. return this.tsParseEnumDeclaration(node, false);
  4168. }
  4169. break;
  4170. case "interface":
  4171. if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  4172. if (next) this.next();
  4173. return this.tsParseInterfaceDeclaration(node);
  4174. }
  4175. break;
  4176. case "module":
  4177. if (next) this.next();
  4178. if (this.match(types.string)) {
  4179. return this.tsParseAmbientExternalModuleDeclaration(node);
  4180. } else if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  4181. return this.tsParseModuleOrNamespaceDeclaration(node);
  4182. }
  4183. break;
  4184. case "namespace":
  4185. if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  4186. if (next) this.next();
  4187. return this.tsParseModuleOrNamespaceDeclaration(node);
  4188. }
  4189. break;
  4190. case "type":
  4191. if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  4192. if (next) this.next();
  4193. return this.tsParseTypeAliasDeclaration(node);
  4194. }
  4195. break;
  4196. }
  4197. }
  4198. tsCheckLineTerminatorAndMatch(tokenType, next) {
  4199. return (next || this.match(tokenType)) && !this.isLineTerminator();
  4200. }
  4201. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  4202. if (!this.isRelational("<")) {
  4203. return undefined;
  4204. }
  4205. const res = this.tsTryParseAndCatch(() => {
  4206. const node = this.startNodeAt(startPos, startLoc);
  4207. node.typeParameters = this.tsParseTypeParameters();
  4208. super.parseFunctionParams(node);
  4209. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  4210. this.expect(types.arrow);
  4211. return node;
  4212. });
  4213. if (!res) {
  4214. return undefined;
  4215. }
  4216. return this.parseArrowExpression(res, null, true);
  4217. }
  4218. tsParseTypeArguments() {
  4219. const node = this.startNode();
  4220. node.params = this.tsInType(() => this.tsInNoContext(() => {
  4221. this.expectRelational("<");
  4222. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  4223. }));
  4224. this.state.exprAllowed = false;
  4225. this.expectRelational(">");
  4226. return this.finishNode(node, "TSTypeParameterInstantiation");
  4227. }
  4228. tsIsDeclarationStart() {
  4229. if (this.match(types.name)) {
  4230. switch (this.state.value) {
  4231. case "abstract":
  4232. case "declare":
  4233. case "enum":
  4234. case "interface":
  4235. case "module":
  4236. case "namespace":
  4237. case "type":
  4238. return true;
  4239. }
  4240. }
  4241. return false;
  4242. }
  4243. isExportDefaultSpecifier() {
  4244. if (this.tsIsDeclarationStart()) return false;
  4245. return super.isExportDefaultSpecifier();
  4246. }
  4247. parseAssignableListItem(allowModifiers, decorators) {
  4248. const startPos = this.state.start;
  4249. const startLoc = this.state.startLoc;
  4250. let accessibility;
  4251. let readonly = false;
  4252. if (allowModifiers) {
  4253. accessibility = this.parseAccessModifier();
  4254. readonly = !!this.tsParseModifier(["readonly"]);
  4255. }
  4256. const left = this.parseMaybeDefault();
  4257. this.parseAssignableListItemTypes(left);
  4258. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  4259. if (accessibility || readonly) {
  4260. const pp = this.startNodeAt(startPos, startLoc);
  4261. if (decorators.length) {
  4262. pp.decorators = decorators;
  4263. }
  4264. if (accessibility) pp.accessibility = accessibility;
  4265. if (readonly) pp.readonly = readonly;
  4266. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  4267. throw this.raise(pp.start, "A parameter property may not be declared using a binding pattern.");
  4268. }
  4269. pp.parameter = elt;
  4270. return this.finishNode(pp, "TSParameterProperty");
  4271. }
  4272. if (decorators.length) {
  4273. left.decorators = decorators;
  4274. }
  4275. return elt;
  4276. }
  4277. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  4278. if (this.match(types.colon)) {
  4279. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(types.colon);
  4280. }
  4281. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" ? "TSDeclareMethod" : undefined;
  4282. if (bodilessType && !this.match(types.braceL) && this.isLineTerminator()) {
  4283. this.finishNode(node, bodilessType);
  4284. return;
  4285. }
  4286. super.parseFunctionBodyAndFinish(node, type, isMethod);
  4287. }
  4288. checkFunctionStatementId(node) {
  4289. if (!node.body && node.id) {
  4290. this.checkLVal(node.id, BIND_TS_FN_TYPE, null, "function name");
  4291. } else {
  4292. super.checkFunctionStatementId(...arguments);
  4293. }
  4294. }
  4295. parseSubscript(base, startPos, startLoc, noCalls, state, maybeAsyncArrow) {
  4296. if (!this.hasPrecedingLineBreak() && this.match(types.bang)) {
  4297. this.state.exprAllowed = false;
  4298. this.next();
  4299. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  4300. nonNullExpression.expression = base;
  4301. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  4302. }
  4303. if (this.isRelational("<")) {
  4304. const result = this.tsTryParseAndCatch(() => {
  4305. if (!noCalls && this.atPossibleAsync(base)) {
  4306. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  4307. if (asyncArrowFn) {
  4308. return asyncArrowFn;
  4309. }
  4310. }
  4311. const node = this.startNodeAt(startPos, startLoc);
  4312. node.callee = base;
  4313. const typeArguments = this.tsParseTypeArguments();
  4314. if (typeArguments) {
  4315. if (!noCalls && this.eat(types.parenL)) {
  4316. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  4317. node.typeParameters = typeArguments;
  4318. return this.finishCallExpression(node);
  4319. } else if (this.match(types.backQuote)) {
  4320. return this.parseTaggedTemplateExpression(startPos, startLoc, base, state, typeArguments);
  4321. }
  4322. }
  4323. this.unexpected();
  4324. });
  4325. if (result) return result;
  4326. }
  4327. return super.parseSubscript(base, startPos, startLoc, noCalls, state, maybeAsyncArrow);
  4328. }
  4329. parseNewArguments(node) {
  4330. if (this.isRelational("<")) {
  4331. const typeParameters = this.tsTryParseAndCatch(() => {
  4332. const args = this.tsParseTypeArguments();
  4333. if (!this.match(types.parenL)) this.unexpected();
  4334. return args;
  4335. });
  4336. if (typeParameters) {
  4337. node.typeParameters = typeParameters;
  4338. }
  4339. }
  4340. super.parseNewArguments(node);
  4341. }
  4342. parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn) {
  4343. if (nonNull(types._in.binop) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual("as")) {
  4344. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  4345. node.expression = left;
  4346. const _const = this.tsTryNextParseConstantContext();
  4347. if (_const) {
  4348. node.typeAnnotation = _const;
  4349. } else {
  4350. node.typeAnnotation = this.tsNextThenParseType();
  4351. }
  4352. this.finishNode(node, "TSAsExpression");
  4353. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn);
  4354. }
  4355. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn);
  4356. }
  4357. checkReservedWord(word, startLoc, checkKeywords, isBinding) {}
  4358. checkDuplicateExports() {}
  4359. parseImport(node) {
  4360. if (this.match(types.name) && this.lookahead().type === types.eq) {
  4361. return this.tsParseImportEqualsDeclaration(node);
  4362. }
  4363. return super.parseImport(node);
  4364. }
  4365. parseExport(node) {
  4366. if (this.match(types._import)) {
  4367. this.expect(types._import);
  4368. return this.tsParseImportEqualsDeclaration(node, true);
  4369. } else if (this.eat(types.eq)) {
  4370. const assign = node;
  4371. assign.expression = this.parseExpression();
  4372. this.semicolon();
  4373. return this.finishNode(assign, "TSExportAssignment");
  4374. } else if (this.eatContextual("as")) {
  4375. const decl = node;
  4376. this.expectContextual("namespace");
  4377. decl.id = this.parseIdentifier();
  4378. this.semicolon();
  4379. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  4380. } else {
  4381. return super.parseExport(node);
  4382. }
  4383. }
  4384. isAbstractClass() {
  4385. return this.isContextual("abstract") && this.lookahead().type === types._class;
  4386. }
  4387. parseExportDefaultExpression() {
  4388. if (this.isAbstractClass()) {
  4389. const cls = this.startNode();
  4390. this.next();
  4391. this.parseClass(cls, true, true);
  4392. cls.abstract = true;
  4393. return cls;
  4394. }
  4395. if (this.state.value === "interface") {
  4396. const result = this.tsParseDeclaration(this.startNode(), this.state.value, true);
  4397. if (result) return result;
  4398. }
  4399. return super.parseExportDefaultExpression();
  4400. }
  4401. parseStatementContent(context, topLevel) {
  4402. if (this.state.type === types._const) {
  4403. const ahead = this.lookahead();
  4404. if (ahead.type === types.name && ahead.value === "enum") {
  4405. const node = this.startNode();
  4406. this.expect(types._const);
  4407. this.expectContextual("enum");
  4408. return this.tsParseEnumDeclaration(node, true);
  4409. }
  4410. }
  4411. return super.parseStatementContent(context, topLevel);
  4412. }
  4413. parseAccessModifier() {
  4414. return this.tsParseModifier(["public", "protected", "private"]);
  4415. }
  4416. parseClassMember(classBody, member, state, constructorAllowsSuper) {
  4417. const accessibility = this.parseAccessModifier();
  4418. if (accessibility) member.accessibility = accessibility;
  4419. super.parseClassMember(classBody, member, state, constructorAllowsSuper);
  4420. }
  4421. parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper) {
  4422. const methodOrProp = member;
  4423. const prop = member;
  4424. const propOrIdx = member;
  4425. let abstract = false,
  4426. readonly = false;
  4427. const mod = this.tsParseModifier(["abstract", "readonly"]);
  4428. switch (mod) {
  4429. case "readonly":
  4430. readonly = true;
  4431. abstract = !!this.tsParseModifier(["abstract"]);
  4432. break;
  4433. case "abstract":
  4434. abstract = true;
  4435. readonly = !!this.tsParseModifier(["readonly"]);
  4436. break;
  4437. }
  4438. if (abstract) methodOrProp.abstract = true;
  4439. if (readonly) propOrIdx.readonly = true;
  4440. if (!abstract && !isStatic && !methodOrProp.accessibility) {
  4441. const idx = this.tsTryParseIndexSignature(member);
  4442. if (idx) {
  4443. classBody.body.push(idx);
  4444. return;
  4445. }
  4446. }
  4447. if (readonly) {
  4448. methodOrProp.static = isStatic;
  4449. this.parseClassPropertyName(prop);
  4450. this.parsePostMemberNameModifiers(methodOrProp);
  4451. this.pushClassProperty(classBody, prop);
  4452. return;
  4453. }
  4454. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper);
  4455. }
  4456. parsePostMemberNameModifiers(methodOrProp) {
  4457. const optional = this.eat(types.question);
  4458. if (optional) methodOrProp.optional = true;
  4459. }
  4460. parseExpressionStatement(node, expr) {
  4461. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  4462. return decl || super.parseExpressionStatement(node, expr);
  4463. }
  4464. shouldParseExportDeclaration() {
  4465. if (this.tsIsDeclarationStart()) return true;
  4466. return super.shouldParseExportDeclaration();
  4467. }
  4468. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  4469. if (!refNeedsArrowPos || !this.match(types.question)) {
  4470. return super.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos);
  4471. }
  4472. const state = this.state.clone();
  4473. try {
  4474. return super.parseConditional(expr, noIn, startPos, startLoc);
  4475. } catch (err) {
  4476. if (!(err instanceof SyntaxError)) {
  4477. throw err;
  4478. }
  4479. this.state = state;
  4480. refNeedsArrowPos.start = err.pos || this.state.start;
  4481. return expr;
  4482. }
  4483. }
  4484. parseParenItem(node, startPos, startLoc) {
  4485. node = super.parseParenItem(node, startPos, startLoc);
  4486. if (this.eat(types.question)) {
  4487. node.optional = true;
  4488. this.resetEndLocation(node);
  4489. }
  4490. if (this.match(types.colon)) {
  4491. const typeCastNode = this.startNodeAt(startPos, startLoc);
  4492. typeCastNode.expression = node;
  4493. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  4494. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  4495. }
  4496. return node;
  4497. }
  4498. parseExportDeclaration(node) {
  4499. const startPos = this.state.start;
  4500. const startLoc = this.state.startLoc;
  4501. const isDeclare = this.eatContextual("declare");
  4502. let declaration;
  4503. if (this.match(types.name)) {
  4504. declaration = this.tsTryParseExportDeclaration();
  4505. }
  4506. if (!declaration) {
  4507. declaration = super.parseExportDeclaration(node);
  4508. }
  4509. if (declaration && isDeclare) {
  4510. this.resetStartLocation(declaration, startPos, startLoc);
  4511. declaration.declare = true;
  4512. }
  4513. return declaration;
  4514. }
  4515. parseClassId(node, isStatement, optionalId) {
  4516. if ((!isStatement || optionalId) && this.isContextual("implements")) {
  4517. return;
  4518. }
  4519. super.parseClassId(...arguments);
  4520. const typeParameters = this.tsTryParseTypeParameters();
  4521. if (typeParameters) node.typeParameters = typeParameters;
  4522. }
  4523. parseClassProperty(node) {
  4524. if (!node.optional && this.eat(types.bang)) {
  4525. node.definite = true;
  4526. }
  4527. const type = this.tsTryParseTypeAnnotation();
  4528. if (type) node.typeAnnotation = type;
  4529. return super.parseClassProperty(node);
  4530. }
  4531. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  4532. const typeParameters = this.tsTryParseTypeParameters();
  4533. if (typeParameters) method.typeParameters = typeParameters;
  4534. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  4535. }
  4536. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  4537. const typeParameters = this.tsTryParseTypeParameters();
  4538. if (typeParameters) method.typeParameters = typeParameters;
  4539. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  4540. }
  4541. parseClassSuper(node) {
  4542. super.parseClassSuper(node);
  4543. if (node.superClass && this.isRelational("<")) {
  4544. node.superTypeParameters = this.tsParseTypeArguments();
  4545. }
  4546. if (this.eatContextual("implements")) {
  4547. node.implements = this.tsParseHeritageClause("implements");
  4548. }
  4549. }
  4550. parseObjPropValue(prop, ...args) {
  4551. const typeParameters = this.tsTryParseTypeParameters();
  4552. if (typeParameters) prop.typeParameters = typeParameters;
  4553. super.parseObjPropValue(prop, ...args);
  4554. }
  4555. parseFunctionParams(node, allowModifiers) {
  4556. const typeParameters = this.tsTryParseTypeParameters();
  4557. if (typeParameters) node.typeParameters = typeParameters;
  4558. super.parseFunctionParams(node, allowModifiers);
  4559. }
  4560. parseVarId(decl, kind) {
  4561. super.parseVarId(decl, kind);
  4562. if (decl.id.type === "Identifier" && this.eat(types.bang)) {
  4563. decl.definite = true;
  4564. }
  4565. const type = this.tsTryParseTypeAnnotation();
  4566. if (type) {
  4567. decl.id.typeAnnotation = type;
  4568. this.resetEndLocation(decl.id);
  4569. }
  4570. }
  4571. parseAsyncArrowFromCallExpression(node, call) {
  4572. if (this.match(types.colon)) {
  4573. node.returnType = this.tsParseTypeAnnotation();
  4574. }
  4575. return super.parseAsyncArrowFromCallExpression(node, call);
  4576. }
  4577. parseMaybeAssign(...args) {
  4578. let jsxError;
  4579. if (this.match(types.jsxTagStart)) {
  4580. const context = this.curContext();
  4581. assert(context === types$1.j_oTag);
  4582. assert(this.state.context[this.state.context.length - 2] === types$1.j_expr);
  4583. const state = this.state.clone();
  4584. try {
  4585. return super.parseMaybeAssign(...args);
  4586. } catch (err) {
  4587. if (!(err instanceof SyntaxError)) {
  4588. throw err;
  4589. }
  4590. this.state = state;
  4591. assert(this.curContext() === types$1.j_oTag);
  4592. this.state.context.pop();
  4593. assert(this.curContext() === types$1.j_expr);
  4594. this.state.context.pop();
  4595. jsxError = err;
  4596. }
  4597. }
  4598. if (jsxError === undefined && !this.isRelational("<")) {
  4599. return super.parseMaybeAssign(...args);
  4600. }
  4601. let arrowExpression;
  4602. let typeParameters;
  4603. const state = this.state.clone();
  4604. try {
  4605. typeParameters = this.tsParseTypeParameters();
  4606. arrowExpression = super.parseMaybeAssign(...args);
  4607. if (arrowExpression.type !== "ArrowFunctionExpression" || arrowExpression.extra && arrowExpression.extra.parenthesized) {
  4608. this.unexpected();
  4609. }
  4610. } catch (err) {
  4611. if (!(err instanceof SyntaxError)) {
  4612. throw err;
  4613. }
  4614. if (jsxError) {
  4615. throw jsxError;
  4616. }
  4617. assert(!this.hasPlugin("jsx"));
  4618. this.state = state;
  4619. return super.parseMaybeAssign(...args);
  4620. }
  4621. if (typeParameters && typeParameters.params.length !== 0) {
  4622. this.resetStartLocationFromNode(arrowExpression, typeParameters);
  4623. }
  4624. arrowExpression.typeParameters = typeParameters;
  4625. return arrowExpression;
  4626. }
  4627. parseMaybeUnary(refShorthandDefaultPos) {
  4628. if (!this.hasPlugin("jsx") && this.isRelational("<")) {
  4629. return this.tsParseTypeAssertion();
  4630. } else {
  4631. return super.parseMaybeUnary(refShorthandDefaultPos);
  4632. }
  4633. }
  4634. parseArrow(node) {
  4635. if (this.match(types.colon)) {
  4636. const state = this.state.clone();
  4637. try {
  4638. const returnType = this.tsParseTypeOrTypePredicateAnnotation(types.colon);
  4639. if (this.canInsertSemicolon() || !this.match(types.arrow)) {
  4640. this.state = state;
  4641. return undefined;
  4642. }
  4643. node.returnType = returnType;
  4644. } catch (err) {
  4645. if (err instanceof SyntaxError) {
  4646. this.state = state;
  4647. } else {
  4648. throw err;
  4649. }
  4650. }
  4651. }
  4652. return super.parseArrow(node);
  4653. }
  4654. parseAssignableListItemTypes(param) {
  4655. if (this.eat(types.question)) {
  4656. if (param.type !== "Identifier") {
  4657. throw this.raise(param.start, "A binding pattern parameter cannot be optional in an implementation signature.");
  4658. }
  4659. param.optional = true;
  4660. }
  4661. const type = this.tsTryParseTypeAnnotation();
  4662. if (type) param.typeAnnotation = type;
  4663. this.resetEndLocation(param);
  4664. return param;
  4665. }
  4666. toAssignable(node, isBinding, contextDescription) {
  4667. switch (node.type) {
  4668. case "TSTypeCastExpression":
  4669. return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription);
  4670. case "TSParameterProperty":
  4671. return super.toAssignable(node, isBinding, contextDescription);
  4672. case "TSAsExpression":
  4673. case "TSNonNullExpression":
  4674. case "TSTypeAssertion":
  4675. node.expression = this.toAssignable(node.expression, isBinding, contextDescription);
  4676. return node;
  4677. default:
  4678. return super.toAssignable(node, isBinding, contextDescription);
  4679. }
  4680. }
  4681. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  4682. switch (expr.type) {
  4683. case "TSTypeCastExpression":
  4684. return;
  4685. case "TSParameterProperty":
  4686. this.checkLVal(expr.parameter, bindingType, checkClashes, "parameter property");
  4687. return;
  4688. case "TSAsExpression":
  4689. case "TSNonNullExpression":
  4690. case "TSTypeAssertion":
  4691. this.checkLVal(expr.expression, bindingType, checkClashes, contextDescription);
  4692. return;
  4693. default:
  4694. super.checkLVal(expr, bindingType, checkClashes, contextDescription);
  4695. return;
  4696. }
  4697. }
  4698. parseBindingAtom() {
  4699. switch (this.state.type) {
  4700. case types._this:
  4701. return this.parseIdentifier(true);
  4702. default:
  4703. return super.parseBindingAtom();
  4704. }
  4705. }
  4706. parseMaybeDecoratorArguments(expr) {
  4707. if (this.isRelational("<")) {
  4708. const typeArguments = this.tsParseTypeArguments();
  4709. if (this.match(types.parenL)) {
  4710. const call = super.parseMaybeDecoratorArguments(expr);
  4711. call.typeParameters = typeArguments;
  4712. return call;
  4713. }
  4714. this.unexpected(this.state.start, types.parenL);
  4715. }
  4716. return super.parseMaybeDecoratorArguments(expr);
  4717. }
  4718. isClassMethod() {
  4719. return this.isRelational("<") || super.isClassMethod();
  4720. }
  4721. isClassProperty() {
  4722. return this.match(types.bang) || this.match(types.colon) || super.isClassProperty();
  4723. }
  4724. parseMaybeDefault(...args) {
  4725. const node = super.parseMaybeDefault(...args);
  4726. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  4727. this.raise(node.typeAnnotation.start, "Type annotations must come before default assignments, " + "e.g. instead of `age = 25: number` use `age: number = 25`");
  4728. }
  4729. return node;
  4730. }
  4731. getTokenFromCode(code) {
  4732. if (this.state.inType && (code === 62 || code === 60)) {
  4733. return this.finishOp(types.relational, 1);
  4734. } else {
  4735. return super.getTokenFromCode(code);
  4736. }
  4737. }
  4738. toAssignableList(exprList, isBinding, contextDescription) {
  4739. for (let i = 0; i < exprList.length; i++) {
  4740. const expr = exprList[i];
  4741. if (!expr) continue;
  4742. switch (expr.type) {
  4743. case "TSTypeCastExpression":
  4744. exprList[i] = this.typeCastToParameter(expr);
  4745. break;
  4746. case "TSAsExpression":
  4747. case "TSTypeAssertion":
  4748. this.raise(expr.start, "Unexpected type cast in parameter position.");
  4749. break;
  4750. }
  4751. }
  4752. return super.toAssignableList(exprList, isBinding, contextDescription);
  4753. }
  4754. typeCastToParameter(node) {
  4755. node.expression.typeAnnotation = node.typeAnnotation;
  4756. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  4757. return node.expression;
  4758. }
  4759. toReferencedList(exprList, isInParens) {
  4760. for (let i = 0; i < exprList.length; i++) {
  4761. const expr = exprList[i];
  4762. if (expr && expr._exprListItem && expr.type === "TsTypeCastExpression") {
  4763. this.raise(expr.start, "Did not expect a type annotation here.");
  4764. }
  4765. }
  4766. return exprList;
  4767. }
  4768. shouldParseArrow() {
  4769. return this.match(types.colon) || super.shouldParseArrow();
  4770. }
  4771. shouldParseAsyncArrow() {
  4772. return this.match(types.colon) || super.shouldParseAsyncArrow();
  4773. }
  4774. canHaveLeadingDecorator() {
  4775. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  4776. }
  4777. jsxParseOpeningElementAfterName(node) {
  4778. if (this.isRelational("<")) {
  4779. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArguments());
  4780. if (typeArguments) node.typeParameters = typeArguments;
  4781. }
  4782. return super.jsxParseOpeningElementAfterName(node);
  4783. }
  4784. getGetterSetterExpectedParamCount(method) {
  4785. const baseCount = super.getGetterSetterExpectedParamCount(method);
  4786. const firstParam = method.params[0];
  4787. const hasContextParam = firstParam && firstParam.type === "Identifier" && firstParam.name === "this";
  4788. return hasContextParam ? baseCount + 1 : baseCount;
  4789. }
  4790. });
  4791. types.placeholder = new TokenType("%%", {
  4792. startsExpr: true
  4793. });
  4794. var placeholders = (superClass => class extends superClass {
  4795. parsePlaceholder(expectedNode) {
  4796. if (this.match(types.placeholder)) {
  4797. const node = this.startNode();
  4798. this.next();
  4799. this.assertNoSpace("Unexpected space in placeholder.");
  4800. node.name = super.parseIdentifier(true);
  4801. this.assertNoSpace("Unexpected space in placeholder.");
  4802. this.expect(types.placeholder);
  4803. return this.finishPlaceholder(node, expectedNode);
  4804. }
  4805. }
  4806. finishPlaceholder(node, expectedNode) {
  4807. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  4808. node.expectedNode = expectedNode;
  4809. return isFinished ? node : this.finishNode(node, "Placeholder");
  4810. }
  4811. getTokenFromCode(code) {
  4812. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  4813. return this.finishOp(types.placeholder, 2);
  4814. }
  4815. return super.getTokenFromCode(...arguments);
  4816. }
  4817. parseExprAtom() {
  4818. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  4819. }
  4820. parseIdentifier() {
  4821. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  4822. }
  4823. checkReservedWord(word) {
  4824. if (word !== undefined) super.checkReservedWord(...arguments);
  4825. }
  4826. parseBindingAtom() {
  4827. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  4828. }
  4829. checkLVal(expr) {
  4830. if (expr.type !== "Placeholder") super.checkLVal(...arguments);
  4831. }
  4832. toAssignable(node) {
  4833. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  4834. node.expectedNode = "Pattern";
  4835. return node;
  4836. }
  4837. return super.toAssignable(...arguments);
  4838. }
  4839. verifyBreakContinue(node) {
  4840. if (node.label && node.label.type === "Placeholder") return;
  4841. super.verifyBreakContinue(...arguments);
  4842. }
  4843. parseExpressionStatement(node, expr) {
  4844. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  4845. return super.parseExpressionStatement(...arguments);
  4846. }
  4847. if (this.match(types.colon)) {
  4848. const stmt = node;
  4849. stmt.label = this.finishPlaceholder(expr, "Identifier");
  4850. this.next();
  4851. stmt.body = this.parseStatement("label");
  4852. return this.finishNode(stmt, "LabeledStatement");
  4853. }
  4854. this.semicolon();
  4855. node.name = expr.name;
  4856. return this.finishPlaceholder(node, "Statement");
  4857. }
  4858. parseBlock() {
  4859. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  4860. }
  4861. parseFunctionId() {
  4862. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  4863. }
  4864. parseClass(node, isStatement, optionalId) {
  4865. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  4866. this.next();
  4867. this.takeDecorators(node);
  4868. const placeholder = this.parsePlaceholder("Identifier");
  4869. if (placeholder) {
  4870. if (this.match(types._extends) || this.match(types.placeholder) || this.match(types.braceL)) {
  4871. node.id = placeholder;
  4872. } else if (optionalId || !isStatement) {
  4873. node.id = null;
  4874. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  4875. return this.finishNode(node, type);
  4876. } else {
  4877. this.unexpected(null, "A class name is required");
  4878. }
  4879. } else {
  4880. this.parseClassId(node, isStatement, optionalId);
  4881. }
  4882. this.parseClassSuper(node);
  4883. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass);
  4884. return this.finishNode(node, type);
  4885. }
  4886. parseExport(node) {
  4887. const placeholder = this.parsePlaceholder("Identifier");
  4888. if (!placeholder) return super.parseExport(...arguments);
  4889. if (!this.isContextual("from") && !this.match(types.comma)) {
  4890. node.specifiers = [];
  4891. node.source = null;
  4892. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  4893. return this.finishNode(node, "ExportNamedDeclaration");
  4894. }
  4895. this.expectPlugin("exportDefaultFrom");
  4896. const specifier = this.startNode();
  4897. specifier.exported = placeholder;
  4898. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  4899. return super.parseExport(node);
  4900. }
  4901. maybeParseExportDefaultSpecifier(node) {
  4902. if (node.specifiers && node.specifiers.length > 0) {
  4903. return true;
  4904. }
  4905. return super.maybeParseExportDefaultSpecifier(...arguments);
  4906. }
  4907. checkExport(node) {
  4908. const {
  4909. specifiers
  4910. } = node;
  4911. if (specifiers && specifiers.length) {
  4912. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  4913. }
  4914. super.checkExport(node);
  4915. node.specifiers = specifiers;
  4916. }
  4917. parseImport(node) {
  4918. const placeholder = this.parsePlaceholder("Identifier");
  4919. if (!placeholder) return super.parseImport(...arguments);
  4920. node.specifiers = [];
  4921. if (!this.isContextual("from") && !this.match(types.comma)) {
  4922. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  4923. this.semicolon();
  4924. return this.finishNode(node, "ImportDeclaration");
  4925. }
  4926. const specifier = this.startNodeAtNode(placeholder);
  4927. specifier.local = placeholder;
  4928. this.finishNode(specifier, "ImportDefaultSpecifier");
  4929. node.specifiers.push(specifier);
  4930. if (this.eat(types.comma)) {
  4931. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  4932. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  4933. }
  4934. this.expectContextual("from");
  4935. node.source = this.parseImportSource();
  4936. this.semicolon();
  4937. return this.finishNode(node, "ImportDeclaration");
  4938. }
  4939. parseImportSource() {
  4940. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  4941. }
  4942. });
  4943. function hasPlugin(plugins, name) {
  4944. return plugins.some(plugin => {
  4945. if (Array.isArray(plugin)) {
  4946. return plugin[0] === name;
  4947. } else {
  4948. return plugin === name;
  4949. }
  4950. });
  4951. }
  4952. function getPluginOption(plugins, name, option) {
  4953. const plugin = plugins.find(plugin => {
  4954. if (Array.isArray(plugin)) {
  4955. return plugin[0] === name;
  4956. } else {
  4957. return plugin === name;
  4958. }
  4959. });
  4960. if (plugin && Array.isArray(plugin)) {
  4961. return plugin[1][option];
  4962. }
  4963. return null;
  4964. }
  4965. const PIPELINE_PROPOSALS = ["minimal", "smart", "fsharp"];
  4966. function validatePlugins(plugins) {
  4967. if (hasPlugin(plugins, "decorators")) {
  4968. if (hasPlugin(plugins, "decorators-legacy")) {
  4969. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  4970. }
  4971. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  4972. if (decoratorsBeforeExport == null) {
  4973. 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'.");
  4974. } else if (typeof decoratorsBeforeExport !== "boolean") {
  4975. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  4976. }
  4977. }
  4978. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  4979. throw new Error("Cannot combine flow and typescript plugins.");
  4980. }
  4981. if (hasPlugin(plugins, "pipelineOperator") && !PIPELINE_PROPOSALS.includes(getPluginOption(plugins, "pipelineOperator", "proposal"))) {
  4982. throw new Error("'pipelineOperator' requires 'proposal' option whose value should be one of: " + PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "));
  4983. }
  4984. }
  4985. const mixinPlugins = {
  4986. estree,
  4987. jsx,
  4988. flow,
  4989. typescript,
  4990. placeholders
  4991. };
  4992. const mixinPluginNames = Object.keys(mixinPlugins);
  4993. const defaultOptions = {
  4994. sourceType: "script",
  4995. sourceFilename: undefined,
  4996. startLine: 1,
  4997. allowAwaitOutsideFunction: false,
  4998. allowReturnOutsideFunction: false,
  4999. allowImportExportEverywhere: false,
  5000. allowSuperOutsideMethod: false,
  5001. allowUndeclaredExports: false,
  5002. plugins: [],
  5003. strictMode: null,
  5004. ranges: false,
  5005. tokens: false,
  5006. createParenthesizedExpressions: false
  5007. };
  5008. function getOptions(opts) {
  5009. const options = {};
  5010. for (let _i = 0, _Object$keys = Object.keys(defaultOptions); _i < _Object$keys.length; _i++) {
  5011. const key = _Object$keys[_i];
  5012. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  5013. }
  5014. return options;
  5015. }
  5016. class Position {
  5017. constructor(line, col) {
  5018. this.line = line;
  5019. this.column = col;
  5020. }
  5021. }
  5022. class SourceLocation {
  5023. constructor(start, end) {
  5024. this.start = start;
  5025. this.end = end;
  5026. }
  5027. }
  5028. function getLineInfo(input, offset) {
  5029. let line = 1;
  5030. let lineStart = 0;
  5031. let match;
  5032. lineBreakG.lastIndex = 0;
  5033. while ((match = lineBreakG.exec(input)) && match.index < offset) {
  5034. line++;
  5035. lineStart = lineBreakG.lastIndex;
  5036. }
  5037. return new Position(line, offset - lineStart);
  5038. }
  5039. class BaseParser {
  5040. constructor() {
  5041. this.sawUnambiguousESM = false;
  5042. }
  5043. hasPlugin(name) {
  5044. return this.plugins.has(name);
  5045. }
  5046. getPluginOption(plugin, name) {
  5047. if (this.hasPlugin(plugin)) return this.plugins.get(plugin)[name];
  5048. }
  5049. }
  5050. function last(stack) {
  5051. return stack[stack.length - 1];
  5052. }
  5053. class CommentsParser extends BaseParser {
  5054. addComment(comment) {
  5055. if (this.filename) comment.loc.filename = this.filename;
  5056. this.state.trailingComments.push(comment);
  5057. this.state.leadingComments.push(comment);
  5058. }
  5059. processComment(node) {
  5060. if (node.type === "Program" && node.body.length > 0) return;
  5061. const stack = this.state.commentStack;
  5062. let firstChild, lastChild, trailingComments, i, j;
  5063. if (this.state.trailingComments.length > 0) {
  5064. if (this.state.trailingComments[0].start >= node.end) {
  5065. trailingComments = this.state.trailingComments;
  5066. this.state.trailingComments = [];
  5067. } else {
  5068. this.state.trailingComments.length = 0;
  5069. }
  5070. } else if (stack.length > 0) {
  5071. const lastInStack = last(stack);
  5072. if (lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
  5073. trailingComments = lastInStack.trailingComments;
  5074. delete lastInStack.trailingComments;
  5075. }
  5076. }
  5077. if (stack.length > 0 && last(stack).start >= node.start) {
  5078. firstChild = stack.pop();
  5079. }
  5080. while (stack.length > 0 && last(stack).start >= node.start) {
  5081. lastChild = stack.pop();
  5082. }
  5083. if (!lastChild && firstChild) lastChild = firstChild;
  5084. if (firstChild && this.state.leadingComments.length > 0) {
  5085. const lastComment = last(this.state.leadingComments);
  5086. if (firstChild.type === "ObjectProperty") {
  5087. if (lastComment.start >= node.start) {
  5088. if (this.state.commentPreviousNode) {
  5089. for (j = 0; j < this.state.leadingComments.length; j++) {
  5090. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  5091. this.state.leadingComments.splice(j, 1);
  5092. j--;
  5093. }
  5094. }
  5095. if (this.state.leadingComments.length > 0) {
  5096. firstChild.trailingComments = this.state.leadingComments;
  5097. this.state.leadingComments = [];
  5098. }
  5099. }
  5100. }
  5101. } else if (node.type === "CallExpression" && node.arguments && node.arguments.length) {
  5102. const lastArg = last(node.arguments);
  5103. if (lastArg && lastComment.start >= lastArg.start && lastComment.end <= node.end) {
  5104. if (this.state.commentPreviousNode) {
  5105. for (j = 0; j < this.state.leadingComments.length; j++) {
  5106. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  5107. this.state.leadingComments.splice(j, 1);
  5108. j--;
  5109. }
  5110. }
  5111. if (this.state.leadingComments.length > 0) {
  5112. lastArg.trailingComments = this.state.leadingComments;
  5113. this.state.leadingComments = [];
  5114. }
  5115. }
  5116. }
  5117. }
  5118. }
  5119. if (lastChild) {
  5120. if (lastChild.leadingComments) {
  5121. if (lastChild !== node && lastChild.leadingComments.length > 0 && last(lastChild.leadingComments).end <= node.start) {
  5122. node.leadingComments = lastChild.leadingComments;
  5123. delete lastChild.leadingComments;
  5124. } else {
  5125. for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
  5126. if (lastChild.leadingComments[i].end <= node.start) {
  5127. node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
  5128. break;
  5129. }
  5130. }
  5131. }
  5132. }
  5133. } else if (this.state.leadingComments.length > 0) {
  5134. if (last(this.state.leadingComments).end <= node.start) {
  5135. if (this.state.commentPreviousNode) {
  5136. for (j = 0; j < this.state.leadingComments.length; j++) {
  5137. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  5138. this.state.leadingComments.splice(j, 1);
  5139. j--;
  5140. }
  5141. }
  5142. }
  5143. if (this.state.leadingComments.length > 0) {
  5144. node.leadingComments = this.state.leadingComments;
  5145. this.state.leadingComments = [];
  5146. }
  5147. } else {
  5148. for (i = 0; i < this.state.leadingComments.length; i++) {
  5149. if (this.state.leadingComments[i].end > node.start) {
  5150. break;
  5151. }
  5152. }
  5153. const leadingComments = this.state.leadingComments.slice(0, i);
  5154. if (leadingComments.length) {
  5155. node.leadingComments = leadingComments;
  5156. }
  5157. trailingComments = this.state.leadingComments.slice(i);
  5158. if (trailingComments.length === 0) {
  5159. trailingComments = null;
  5160. }
  5161. }
  5162. }
  5163. this.state.commentPreviousNode = node;
  5164. if (trailingComments) {
  5165. if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
  5166. node.innerComments = trailingComments;
  5167. } else {
  5168. node.trailingComments = trailingComments;
  5169. }
  5170. }
  5171. stack.push(node);
  5172. }
  5173. }
  5174. class LocationParser extends CommentsParser {
  5175. getLocationForPosition(pos) {
  5176. let loc;
  5177. 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);
  5178. return loc;
  5179. }
  5180. raise(pos, message, {
  5181. missingPluginNames,
  5182. code
  5183. } = {}) {
  5184. const loc = this.getLocationForPosition(pos);
  5185. message += ` (${loc.line}:${loc.column})`;
  5186. const err = new SyntaxError(message);
  5187. err.pos = pos;
  5188. err.loc = loc;
  5189. if (missingPluginNames) {
  5190. err.missingPlugin = missingPluginNames;
  5191. }
  5192. if (code !== undefined) {
  5193. err.code = code;
  5194. }
  5195. throw err;
  5196. }
  5197. }
  5198. class State {
  5199. constructor() {
  5200. this.potentialArrowAt = -1;
  5201. this.noArrowAt = [];
  5202. this.noArrowParamsConversionAt = [];
  5203. this.commaAfterSpreadAt = -1;
  5204. this.inParameters = false;
  5205. this.maybeInArrowParameters = false;
  5206. this.inPipeline = false;
  5207. this.inType = false;
  5208. this.noAnonFunctionType = false;
  5209. this.inPropertyName = false;
  5210. this.inClassProperty = false;
  5211. this.hasFlowComment = false;
  5212. this.isIterator = false;
  5213. this.topicContext = {
  5214. maxNumOfResolvableTopics: 0,
  5215. maxTopicIndex: null
  5216. };
  5217. this.soloAwait = false;
  5218. this.inFSharpPipelineDirectBody = false;
  5219. this.classLevel = 0;
  5220. this.labels = [];
  5221. this.decoratorStack = [[]];
  5222. this.yieldPos = 0;
  5223. this.awaitPos = 0;
  5224. this.tokens = [];
  5225. this.comments = [];
  5226. this.trailingComments = [];
  5227. this.leadingComments = [];
  5228. this.commentStack = [];
  5229. this.commentPreviousNode = null;
  5230. this.pos = 0;
  5231. this.lineStart = 0;
  5232. this.type = types.eof;
  5233. this.value = null;
  5234. this.start = 0;
  5235. this.end = 0;
  5236. this.lastTokEndLoc = null;
  5237. this.lastTokStartLoc = null;
  5238. this.lastTokStart = 0;
  5239. this.lastTokEnd = 0;
  5240. this.context = [types$1.braceStatement];
  5241. this.exprAllowed = true;
  5242. this.containsEsc = false;
  5243. this.containsOctal = false;
  5244. this.octalPosition = null;
  5245. this.exportedIdentifiers = [];
  5246. this.invalidTemplateEscapePosition = null;
  5247. }
  5248. init(options) {
  5249. this.strict = options.strictMode === false ? false : options.sourceType === "module";
  5250. this.curLine = options.startLine;
  5251. this.startLoc = this.endLoc = this.curPosition();
  5252. }
  5253. curPosition() {
  5254. return new Position(this.curLine, this.pos - this.lineStart);
  5255. }
  5256. clone(skipArrays) {
  5257. const state = new State();
  5258. const keys = Object.keys(this);
  5259. for (let i = 0, length = keys.length; i < length; i++) {
  5260. const key = keys[i];
  5261. let val = this[key];
  5262. if (!skipArrays && Array.isArray(val)) {
  5263. val = val.slice();
  5264. }
  5265. state[key] = val;
  5266. }
  5267. return state;
  5268. }
  5269. }
  5270. var _isDigit = function isDigit(code) {
  5271. return code >= 48 && code <= 57;
  5272. };
  5273. const VALID_REGEX_FLAGS = new Set(["g", "m", "s", "i", "y", "u"]);
  5274. const forbiddenNumericSeparatorSiblings = {
  5275. decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
  5276. hex: [46, 88, 95, 120]
  5277. };
  5278. const allowedNumericSeparatorSiblings = {};
  5279. allowedNumericSeparatorSiblings.bin = [48, 49];
  5280. allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
  5281. allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
  5282. allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
  5283. class Token {
  5284. constructor(state) {
  5285. this.type = state.type;
  5286. this.value = state.value;
  5287. this.start = state.start;
  5288. this.end = state.end;
  5289. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  5290. }
  5291. }
  5292. class Tokenizer extends LocationParser {
  5293. constructor(options, input) {
  5294. super();
  5295. this.state = new State();
  5296. this.state.init(options);
  5297. this.input = input;
  5298. this.length = input.length;
  5299. this.isLookahead = false;
  5300. }
  5301. next() {
  5302. if (this.options.tokens && !this.isLookahead) {
  5303. this.state.tokens.push(new Token(this.state));
  5304. }
  5305. this.state.lastTokEnd = this.state.end;
  5306. this.state.lastTokStart = this.state.start;
  5307. this.state.lastTokEndLoc = this.state.endLoc;
  5308. this.state.lastTokStartLoc = this.state.startLoc;
  5309. this.nextToken();
  5310. }
  5311. eat(type) {
  5312. if (this.match(type)) {
  5313. this.next();
  5314. return true;
  5315. } else {
  5316. return false;
  5317. }
  5318. }
  5319. match(type) {
  5320. return this.state.type === type;
  5321. }
  5322. lookahead() {
  5323. const old = this.state;
  5324. this.state = old.clone(true);
  5325. this.isLookahead = true;
  5326. this.next();
  5327. this.isLookahead = false;
  5328. const curr = this.state;
  5329. this.state = old;
  5330. return curr;
  5331. }
  5332. setStrict(strict) {
  5333. this.state.strict = strict;
  5334. if (!this.match(types.num) && !this.match(types.string)) return;
  5335. this.state.pos = this.state.start;
  5336. while (this.state.pos < this.state.lineStart) {
  5337. this.state.lineStart = this.input.lastIndexOf("\n", this.state.lineStart - 2) + 1;
  5338. --this.state.curLine;
  5339. }
  5340. this.nextToken();
  5341. }
  5342. curContext() {
  5343. return this.state.context[this.state.context.length - 1];
  5344. }
  5345. nextToken() {
  5346. const curContext = this.curContext();
  5347. if (!curContext || !curContext.preserveSpace) this.skipSpace();
  5348. this.state.containsOctal = false;
  5349. this.state.octalPosition = null;
  5350. this.state.start = this.state.pos;
  5351. this.state.startLoc = this.state.curPosition();
  5352. if (this.state.pos >= this.length) {
  5353. this.finishToken(types.eof);
  5354. return;
  5355. }
  5356. if (curContext.override) {
  5357. curContext.override(this);
  5358. } else {
  5359. this.getTokenFromCode(this.input.codePointAt(this.state.pos));
  5360. }
  5361. }
  5362. pushComment(block, text, start, end, startLoc, endLoc) {
  5363. const comment = {
  5364. type: block ? "CommentBlock" : "CommentLine",
  5365. value: text,
  5366. start: start,
  5367. end: end,
  5368. loc: new SourceLocation(startLoc, endLoc)
  5369. };
  5370. if (this.options.tokens) this.state.tokens.push(comment);
  5371. this.state.comments.push(comment);
  5372. this.addComment(comment);
  5373. }
  5374. skipBlockComment() {
  5375. const startLoc = this.state.curPosition();
  5376. const start = this.state.pos;
  5377. const end = this.input.indexOf("*/", this.state.pos += 2);
  5378. if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment");
  5379. this.state.pos = end + 2;
  5380. lineBreakG.lastIndex = start;
  5381. let match;
  5382. while ((match = lineBreakG.exec(this.input)) && match.index < this.state.pos) {
  5383. ++this.state.curLine;
  5384. this.state.lineStart = match.index + match[0].length;
  5385. }
  5386. if (this.isLookahead) return;
  5387. this.pushComment(true, this.input.slice(start + 2, end), start, this.state.pos, startLoc, this.state.curPosition());
  5388. }
  5389. skipLineComment(startSkip) {
  5390. const start = this.state.pos;
  5391. const startLoc = this.state.curPosition();
  5392. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  5393. if (this.state.pos < this.length) {
  5394. while (ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233 && ++this.state.pos < this.length) {
  5395. ch = this.input.charCodeAt(this.state.pos);
  5396. }
  5397. }
  5398. if (this.isLookahead) return;
  5399. this.pushComment(false, this.input.slice(start + startSkip, this.state.pos), start, this.state.pos, startLoc, this.state.curPosition());
  5400. }
  5401. skipSpace() {
  5402. loop: while (this.state.pos < this.length) {
  5403. const ch = this.input.charCodeAt(this.state.pos);
  5404. switch (ch) {
  5405. case 32:
  5406. case 160:
  5407. case 9:
  5408. ++this.state.pos;
  5409. break;
  5410. case 13:
  5411. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  5412. ++this.state.pos;
  5413. }
  5414. case 10:
  5415. case 8232:
  5416. case 8233:
  5417. ++this.state.pos;
  5418. ++this.state.curLine;
  5419. this.state.lineStart = this.state.pos;
  5420. break;
  5421. case 47:
  5422. switch (this.input.charCodeAt(this.state.pos + 1)) {
  5423. case 42:
  5424. this.skipBlockComment();
  5425. break;
  5426. case 47:
  5427. this.skipLineComment(2);
  5428. break;
  5429. default:
  5430. break loop;
  5431. }
  5432. break;
  5433. default:
  5434. if (isWhitespace(ch)) {
  5435. ++this.state.pos;
  5436. } else {
  5437. break loop;
  5438. }
  5439. }
  5440. }
  5441. }
  5442. finishToken(type, val) {
  5443. this.state.end = this.state.pos;
  5444. this.state.endLoc = this.state.curPosition();
  5445. const prevType = this.state.type;
  5446. this.state.type = type;
  5447. this.state.value = val;
  5448. if (!this.isLookahead) this.updateContext(prevType);
  5449. }
  5450. readToken_numberSign() {
  5451. if (this.state.pos === 0 && this.readToken_interpreter()) {
  5452. return;
  5453. }
  5454. const nextPos = this.state.pos + 1;
  5455. const next = this.input.charCodeAt(nextPos);
  5456. if (next >= 48 && next <= 57) {
  5457. this.raise(this.state.pos, "Unexpected digit after hash token");
  5458. }
  5459. if ((this.hasPlugin("classPrivateProperties") || this.hasPlugin("classPrivateMethods")) && this.state.classLevel > 0) {
  5460. ++this.state.pos;
  5461. this.finishToken(types.hash);
  5462. return;
  5463. } else if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  5464. this.finishOp(types.hash, 1);
  5465. } else {
  5466. this.raise(this.state.pos, "Unexpected character '#'");
  5467. }
  5468. }
  5469. readToken_dot() {
  5470. const next = this.input.charCodeAt(this.state.pos + 1);
  5471. if (next >= 48 && next <= 57) {
  5472. this.readNumber(true);
  5473. return;
  5474. }
  5475. const next2 = this.input.charCodeAt(this.state.pos + 2);
  5476. if (next === 46 && next2 === 46) {
  5477. this.state.pos += 3;
  5478. this.finishToken(types.ellipsis);
  5479. } else {
  5480. ++this.state.pos;
  5481. this.finishToken(types.dot);
  5482. }
  5483. }
  5484. readToken_slash() {
  5485. if (this.state.exprAllowed && !this.state.inType) {
  5486. ++this.state.pos;
  5487. this.readRegexp();
  5488. return;
  5489. }
  5490. const next = this.input.charCodeAt(this.state.pos + 1);
  5491. if (next === 61) {
  5492. this.finishOp(types.assign, 2);
  5493. } else {
  5494. this.finishOp(types.slash, 1);
  5495. }
  5496. }
  5497. readToken_interpreter() {
  5498. if (this.state.pos !== 0 || this.length < 2) return false;
  5499. const start = this.state.pos;
  5500. this.state.pos += 1;
  5501. let ch = this.input.charCodeAt(this.state.pos);
  5502. if (ch !== 33) return false;
  5503. while (ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233 && ++this.state.pos < this.length) {
  5504. ch = this.input.charCodeAt(this.state.pos);
  5505. }
  5506. const value = this.input.slice(start + 2, this.state.pos);
  5507. this.finishToken(types.interpreterDirective, value);
  5508. return true;
  5509. }
  5510. readToken_mult_modulo(code) {
  5511. let type = code === 42 ? types.star : types.modulo;
  5512. let width = 1;
  5513. let next = this.input.charCodeAt(this.state.pos + 1);
  5514. const exprAllowed = this.state.exprAllowed;
  5515. if (code === 42 && next === 42) {
  5516. width++;
  5517. next = this.input.charCodeAt(this.state.pos + 2);
  5518. type = types.exponent;
  5519. }
  5520. if (next === 61 && !exprAllowed) {
  5521. width++;
  5522. type = types.assign;
  5523. }
  5524. this.finishOp(type, width);
  5525. }
  5526. readToken_pipe_amp(code) {
  5527. const next = this.input.charCodeAt(this.state.pos + 1);
  5528. if (next === code) {
  5529. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  5530. this.finishOp(types.assign, 3);
  5531. } else {
  5532. this.finishOp(code === 124 ? types.logicalOR : types.logicalAND, 2);
  5533. }
  5534. return;
  5535. }
  5536. if (code === 124) {
  5537. if (next === 62) {
  5538. this.finishOp(types.pipeline, 2);
  5539. return;
  5540. }
  5541. }
  5542. if (next === 61) {
  5543. this.finishOp(types.assign, 2);
  5544. return;
  5545. }
  5546. this.finishOp(code === 124 ? types.bitwiseOR : types.bitwiseAND, 1);
  5547. }
  5548. readToken_caret() {
  5549. const next = this.input.charCodeAt(this.state.pos + 1);
  5550. if (next === 61) {
  5551. this.finishOp(types.assign, 2);
  5552. } else {
  5553. this.finishOp(types.bitwiseXOR, 1);
  5554. }
  5555. }
  5556. readToken_plus_min(code) {
  5557. const next = this.input.charCodeAt(this.state.pos + 1);
  5558. if (next === code) {
  5559. if (next === 45 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 62 && (this.state.lastTokEnd === 0 || lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.pos)))) {
  5560. this.skipLineComment(3);
  5561. this.skipSpace();
  5562. this.nextToken();
  5563. return;
  5564. }
  5565. this.finishOp(types.incDec, 2);
  5566. return;
  5567. }
  5568. if (next === 61) {
  5569. this.finishOp(types.assign, 2);
  5570. } else {
  5571. this.finishOp(types.plusMin, 1);
  5572. }
  5573. }
  5574. readToken_lt_gt(code) {
  5575. const next = this.input.charCodeAt(this.state.pos + 1);
  5576. let size = 1;
  5577. if (next === code) {
  5578. size = code === 62 && this.input.charCodeAt(this.state.pos + 2) === 62 ? 3 : 2;
  5579. if (this.input.charCodeAt(this.state.pos + size) === 61) {
  5580. this.finishOp(types.assign, size + 1);
  5581. return;
  5582. }
  5583. this.finishOp(types.bitShift, size);
  5584. return;
  5585. }
  5586. if (next === 33 && code === 60 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 45 && this.input.charCodeAt(this.state.pos + 3) === 45) {
  5587. this.skipLineComment(4);
  5588. this.skipSpace();
  5589. this.nextToken();
  5590. return;
  5591. }
  5592. if (next === 61) {
  5593. size = 2;
  5594. }
  5595. this.finishOp(types.relational, size);
  5596. }
  5597. readToken_eq_excl(code) {
  5598. const next = this.input.charCodeAt(this.state.pos + 1);
  5599. if (next === 61) {
  5600. this.finishOp(types.equality, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  5601. return;
  5602. }
  5603. if (code === 61 && next === 62) {
  5604. this.state.pos += 2;
  5605. this.finishToken(types.arrow);
  5606. return;
  5607. }
  5608. this.finishOp(code === 61 ? types.eq : types.bang, 1);
  5609. }
  5610. readToken_question() {
  5611. const next = this.input.charCodeAt(this.state.pos + 1);
  5612. const next2 = this.input.charCodeAt(this.state.pos + 2);
  5613. if (next === 63 && !this.state.inType) {
  5614. if (next2 === 61) {
  5615. this.finishOp(types.assign, 3);
  5616. } else {
  5617. this.finishOp(types.nullishCoalescing, 2);
  5618. }
  5619. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  5620. this.state.pos += 2;
  5621. this.finishToken(types.questionDot);
  5622. } else {
  5623. ++this.state.pos;
  5624. this.finishToken(types.question);
  5625. }
  5626. }
  5627. getTokenFromCode(code) {
  5628. switch (code) {
  5629. case 46:
  5630. this.readToken_dot();
  5631. return;
  5632. case 40:
  5633. ++this.state.pos;
  5634. this.finishToken(types.parenL);
  5635. return;
  5636. case 41:
  5637. ++this.state.pos;
  5638. this.finishToken(types.parenR);
  5639. return;
  5640. case 59:
  5641. ++this.state.pos;
  5642. this.finishToken(types.semi);
  5643. return;
  5644. case 44:
  5645. ++this.state.pos;
  5646. this.finishToken(types.comma);
  5647. return;
  5648. case 91:
  5649. ++this.state.pos;
  5650. this.finishToken(types.bracketL);
  5651. return;
  5652. case 93:
  5653. ++this.state.pos;
  5654. this.finishToken(types.bracketR);
  5655. return;
  5656. case 123:
  5657. ++this.state.pos;
  5658. this.finishToken(types.braceL);
  5659. return;
  5660. case 125:
  5661. ++this.state.pos;
  5662. this.finishToken(types.braceR);
  5663. return;
  5664. case 58:
  5665. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  5666. this.finishOp(types.doubleColon, 2);
  5667. } else {
  5668. ++this.state.pos;
  5669. this.finishToken(types.colon);
  5670. }
  5671. return;
  5672. case 63:
  5673. this.readToken_question();
  5674. return;
  5675. case 96:
  5676. ++this.state.pos;
  5677. this.finishToken(types.backQuote);
  5678. return;
  5679. case 48:
  5680. {
  5681. const next = this.input.charCodeAt(this.state.pos + 1);
  5682. if (next === 120 || next === 88) {
  5683. this.readRadixNumber(16);
  5684. return;
  5685. }
  5686. if (next === 111 || next === 79) {
  5687. this.readRadixNumber(8);
  5688. return;
  5689. }
  5690. if (next === 98 || next === 66) {
  5691. this.readRadixNumber(2);
  5692. return;
  5693. }
  5694. }
  5695. case 49:
  5696. case 50:
  5697. case 51:
  5698. case 52:
  5699. case 53:
  5700. case 54:
  5701. case 55:
  5702. case 56:
  5703. case 57:
  5704. this.readNumber(false);
  5705. return;
  5706. case 34:
  5707. case 39:
  5708. this.readString(code);
  5709. return;
  5710. case 47:
  5711. this.readToken_slash();
  5712. return;
  5713. case 37:
  5714. case 42:
  5715. this.readToken_mult_modulo(code);
  5716. return;
  5717. case 124:
  5718. case 38:
  5719. this.readToken_pipe_amp(code);
  5720. return;
  5721. case 94:
  5722. this.readToken_caret();
  5723. return;
  5724. case 43:
  5725. case 45:
  5726. this.readToken_plus_min(code);
  5727. return;
  5728. case 60:
  5729. case 62:
  5730. this.readToken_lt_gt(code);
  5731. return;
  5732. case 61:
  5733. case 33:
  5734. this.readToken_eq_excl(code);
  5735. return;
  5736. case 126:
  5737. this.finishOp(types.tilde, 1);
  5738. return;
  5739. case 64:
  5740. ++this.state.pos;
  5741. this.finishToken(types.at);
  5742. return;
  5743. case 35:
  5744. this.readToken_numberSign();
  5745. return;
  5746. case 92:
  5747. this.readWord();
  5748. return;
  5749. default:
  5750. if (isIdentifierStart(code)) {
  5751. this.readWord();
  5752. return;
  5753. }
  5754. }
  5755. this.raise(this.state.pos, `Unexpected character '${String.fromCodePoint(code)}'`);
  5756. }
  5757. finishOp(type, size) {
  5758. const str = this.input.slice(this.state.pos, this.state.pos + size);
  5759. this.state.pos += size;
  5760. this.finishToken(type, str);
  5761. }
  5762. readRegexp() {
  5763. const start = this.state.pos;
  5764. let escaped, inClass;
  5765. for (;;) {
  5766. if (this.state.pos >= this.length) {
  5767. this.raise(start, "Unterminated regular expression");
  5768. }
  5769. const ch = this.input.charAt(this.state.pos);
  5770. if (lineBreak.test(ch)) {
  5771. this.raise(start, "Unterminated regular expression");
  5772. }
  5773. if (escaped) {
  5774. escaped = false;
  5775. } else {
  5776. if (ch === "[") {
  5777. inClass = true;
  5778. } else if (ch === "]" && inClass) {
  5779. inClass = false;
  5780. } else if (ch === "/" && !inClass) {
  5781. break;
  5782. }
  5783. escaped = ch === "\\";
  5784. }
  5785. ++this.state.pos;
  5786. }
  5787. const content = this.input.slice(start, this.state.pos);
  5788. ++this.state.pos;
  5789. let mods = "";
  5790. while (this.state.pos < this.length) {
  5791. const char = this.input[this.state.pos];
  5792. const charCode = this.input.codePointAt(this.state.pos);
  5793. if (VALID_REGEX_FLAGS.has(char)) {
  5794. if (mods.indexOf(char) > -1) {
  5795. this.raise(this.state.pos + 1, "Duplicate regular expression flag");
  5796. }
  5797. ++this.state.pos;
  5798. mods += char;
  5799. } else if (isIdentifierChar(charCode) || charCode === 92) {
  5800. this.raise(this.state.pos + 1, "Invalid regular expression flag");
  5801. } else {
  5802. break;
  5803. }
  5804. }
  5805. this.finishToken(types.regexp, {
  5806. pattern: content,
  5807. flags: mods
  5808. });
  5809. }
  5810. readInt(radix, len) {
  5811. const start = this.state.pos;
  5812. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  5813. const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
  5814. let total = 0;
  5815. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  5816. const code = this.input.charCodeAt(this.state.pos);
  5817. let val;
  5818. if (this.hasPlugin("numericSeparator")) {
  5819. const prev = this.input.charCodeAt(this.state.pos - 1);
  5820. const next = this.input.charCodeAt(this.state.pos + 1);
  5821. if (code === 95) {
  5822. if (allowedSiblings.indexOf(next) === -1) {
  5823. this.raise(this.state.pos, "Invalid or unexpected token");
  5824. }
  5825. if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
  5826. this.raise(this.state.pos, "Invalid or unexpected token");
  5827. }
  5828. ++this.state.pos;
  5829. continue;
  5830. }
  5831. }
  5832. if (code >= 97) {
  5833. val = code - 97 + 10;
  5834. } else if (code >= 65) {
  5835. val = code - 65 + 10;
  5836. } else if (_isDigit(code)) {
  5837. val = code - 48;
  5838. } else {
  5839. val = Infinity;
  5840. }
  5841. if (val >= radix) break;
  5842. ++this.state.pos;
  5843. total = total * radix + val;
  5844. }
  5845. if (this.state.pos === start || len != null && this.state.pos - start !== len) {
  5846. return null;
  5847. }
  5848. return total;
  5849. }
  5850. readRadixNumber(radix) {
  5851. const start = this.state.pos;
  5852. let isBigInt = false;
  5853. this.state.pos += 2;
  5854. const val = this.readInt(radix);
  5855. if (val == null) {
  5856. this.raise(this.state.start + 2, "Expected number in radix " + radix);
  5857. }
  5858. if (this.hasPlugin("bigInt")) {
  5859. if (this.input.charCodeAt(this.state.pos) === 110) {
  5860. ++this.state.pos;
  5861. isBigInt = true;
  5862. }
  5863. }
  5864. if (isIdentifierStart(this.input.codePointAt(this.state.pos))) {
  5865. this.raise(this.state.pos, "Identifier directly after number");
  5866. }
  5867. if (isBigInt) {
  5868. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  5869. this.finishToken(types.bigint, str);
  5870. return;
  5871. }
  5872. this.finishToken(types.num, val);
  5873. }
  5874. readNumber(startsWithDot) {
  5875. const start = this.state.pos;
  5876. let isFloat = false;
  5877. let isBigInt = false;
  5878. if (!startsWithDot && this.readInt(10) === null) {
  5879. this.raise(start, "Invalid number");
  5880. }
  5881. let octal = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  5882. if (octal) {
  5883. if (this.state.strict) {
  5884. this.raise(start, "Legacy octal literals are not allowed in strict mode");
  5885. }
  5886. if (/[89]/.test(this.input.slice(start, this.state.pos))) {
  5887. octal = false;
  5888. }
  5889. }
  5890. let next = this.input.charCodeAt(this.state.pos);
  5891. if (next === 46 && !octal) {
  5892. ++this.state.pos;
  5893. this.readInt(10);
  5894. isFloat = true;
  5895. next = this.input.charCodeAt(this.state.pos);
  5896. }
  5897. if ((next === 69 || next === 101) && !octal) {
  5898. next = this.input.charCodeAt(++this.state.pos);
  5899. if (next === 43 || next === 45) {
  5900. ++this.state.pos;
  5901. }
  5902. if (this.readInt(10) === null) this.raise(start, "Invalid number");
  5903. isFloat = true;
  5904. next = this.input.charCodeAt(this.state.pos);
  5905. }
  5906. if (this.hasPlugin("bigInt")) {
  5907. if (next === 110) {
  5908. if (isFloat || octal) this.raise(start, "Invalid BigIntLiteral");
  5909. ++this.state.pos;
  5910. isBigInt = true;
  5911. }
  5912. }
  5913. if (isIdentifierStart(this.input.codePointAt(this.state.pos))) {
  5914. this.raise(this.state.pos, "Identifier directly after number");
  5915. }
  5916. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  5917. if (isBigInt) {
  5918. this.finishToken(types.bigint, str);
  5919. return;
  5920. }
  5921. const val = octal ? parseInt(str, 8) : parseFloat(str);
  5922. this.finishToken(types.num, val);
  5923. }
  5924. readCodePoint(throwOnInvalid) {
  5925. const ch = this.input.charCodeAt(this.state.pos);
  5926. let code;
  5927. if (ch === 123) {
  5928. const codePos = ++this.state.pos;
  5929. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, throwOnInvalid);
  5930. ++this.state.pos;
  5931. if (code === null) {
  5932. --this.state.invalidTemplateEscapePosition;
  5933. } else if (code > 0x10ffff) {
  5934. if (throwOnInvalid) {
  5935. this.raise(codePos, "Code point out of bounds");
  5936. } else {
  5937. this.state.invalidTemplateEscapePosition = codePos - 2;
  5938. return null;
  5939. }
  5940. }
  5941. } else {
  5942. code = this.readHexChar(4, throwOnInvalid);
  5943. }
  5944. return code;
  5945. }
  5946. readString(quote) {
  5947. let out = "",
  5948. chunkStart = ++this.state.pos;
  5949. for (;;) {
  5950. if (this.state.pos >= this.length) {
  5951. this.raise(this.state.start, "Unterminated string constant");
  5952. }
  5953. const ch = this.input.charCodeAt(this.state.pos);
  5954. if (ch === quote) break;
  5955. if (ch === 92) {
  5956. out += this.input.slice(chunkStart, this.state.pos);
  5957. out += this.readEscapedChar(false);
  5958. chunkStart = this.state.pos;
  5959. } else if (ch === 8232 || ch === 8233) {
  5960. ++this.state.pos;
  5961. ++this.state.curLine;
  5962. } else if (isNewLine(ch)) {
  5963. this.raise(this.state.start, "Unterminated string constant");
  5964. } else {
  5965. ++this.state.pos;
  5966. }
  5967. }
  5968. out += this.input.slice(chunkStart, this.state.pos++);
  5969. this.finishToken(types.string, out);
  5970. }
  5971. readTmplToken() {
  5972. let out = "",
  5973. chunkStart = this.state.pos,
  5974. containsInvalid = false;
  5975. for (;;) {
  5976. if (this.state.pos >= this.length) {
  5977. this.raise(this.state.start, "Unterminated template");
  5978. }
  5979. const ch = this.input.charCodeAt(this.state.pos);
  5980. if (ch === 96 || ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  5981. if (this.state.pos === this.state.start && this.match(types.template)) {
  5982. if (ch === 36) {
  5983. this.state.pos += 2;
  5984. this.finishToken(types.dollarBraceL);
  5985. return;
  5986. } else {
  5987. ++this.state.pos;
  5988. this.finishToken(types.backQuote);
  5989. return;
  5990. }
  5991. }
  5992. out += this.input.slice(chunkStart, this.state.pos);
  5993. this.finishToken(types.template, containsInvalid ? null : out);
  5994. return;
  5995. }
  5996. if (ch === 92) {
  5997. out += this.input.slice(chunkStart, this.state.pos);
  5998. const escaped = this.readEscapedChar(true);
  5999. if (escaped === null) {
  6000. containsInvalid = true;
  6001. } else {
  6002. out += escaped;
  6003. }
  6004. chunkStart = this.state.pos;
  6005. } else if (isNewLine(ch)) {
  6006. out += this.input.slice(chunkStart, this.state.pos);
  6007. ++this.state.pos;
  6008. switch (ch) {
  6009. case 13:
  6010. if (this.input.charCodeAt(this.state.pos) === 10) {
  6011. ++this.state.pos;
  6012. }
  6013. case 10:
  6014. out += "\n";
  6015. break;
  6016. default:
  6017. out += String.fromCharCode(ch);
  6018. break;
  6019. }
  6020. ++this.state.curLine;
  6021. this.state.lineStart = this.state.pos;
  6022. chunkStart = this.state.pos;
  6023. } else {
  6024. ++this.state.pos;
  6025. }
  6026. }
  6027. }
  6028. readEscapedChar(inTemplate) {
  6029. const throwOnInvalid = !inTemplate;
  6030. const ch = this.input.charCodeAt(++this.state.pos);
  6031. ++this.state.pos;
  6032. switch (ch) {
  6033. case 110:
  6034. return "\n";
  6035. case 114:
  6036. return "\r";
  6037. case 120:
  6038. {
  6039. const code = this.readHexChar(2, throwOnInvalid);
  6040. return code === null ? null : String.fromCharCode(code);
  6041. }
  6042. case 117:
  6043. {
  6044. const code = this.readCodePoint(throwOnInvalid);
  6045. return code === null ? null : String.fromCodePoint(code);
  6046. }
  6047. case 116:
  6048. return "\t";
  6049. case 98:
  6050. return "\b";
  6051. case 118:
  6052. return "\u000b";
  6053. case 102:
  6054. return "\f";
  6055. case 13:
  6056. if (this.input.charCodeAt(this.state.pos) === 10) {
  6057. ++this.state.pos;
  6058. }
  6059. case 10:
  6060. this.state.lineStart = this.state.pos;
  6061. ++this.state.curLine;
  6062. case 8232:
  6063. case 8233:
  6064. return "";
  6065. default:
  6066. if (ch >= 48 && ch <= 55) {
  6067. const codePos = this.state.pos - 1;
  6068. let octalStr = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/)[0];
  6069. let octal = parseInt(octalStr, 8);
  6070. if (octal > 255) {
  6071. octalStr = octalStr.slice(0, -1);
  6072. octal = parseInt(octalStr, 8);
  6073. }
  6074. this.state.pos += octalStr.length - 1;
  6075. const next = this.input.charCodeAt(this.state.pos);
  6076. if (octalStr !== "0" || next === 56 || next === 57) {
  6077. if (inTemplate) {
  6078. this.state.invalidTemplateEscapePosition = codePos;
  6079. return null;
  6080. } else if (this.state.strict) {
  6081. this.raise(codePos, "Octal literal in strict mode");
  6082. } else if (!this.state.containsOctal) {
  6083. this.state.containsOctal = true;
  6084. this.state.octalPosition = codePos;
  6085. }
  6086. }
  6087. return String.fromCharCode(octal);
  6088. }
  6089. return String.fromCharCode(ch);
  6090. }
  6091. }
  6092. readHexChar(len, throwOnInvalid) {
  6093. const codePos = this.state.pos;
  6094. const n = this.readInt(16, len);
  6095. if (n === null) {
  6096. if (throwOnInvalid) {
  6097. this.raise(codePos, "Bad character escape sequence");
  6098. } else {
  6099. this.state.pos = codePos - 1;
  6100. this.state.invalidTemplateEscapePosition = codePos - 1;
  6101. }
  6102. }
  6103. return n;
  6104. }
  6105. readWord1() {
  6106. let word = "";
  6107. this.state.containsEsc = false;
  6108. const start = this.state.pos;
  6109. let chunkStart = this.state.pos;
  6110. while (this.state.pos < this.length) {
  6111. const ch = this.input.codePointAt(this.state.pos);
  6112. if (isIdentifierChar(ch)) {
  6113. this.state.pos += ch <= 0xffff ? 1 : 2;
  6114. } else if (this.state.isIterator && ch === 64) {
  6115. ++this.state.pos;
  6116. } else if (ch === 92) {
  6117. this.state.containsEsc = true;
  6118. word += this.input.slice(chunkStart, this.state.pos);
  6119. const escStart = this.state.pos;
  6120. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  6121. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  6122. this.raise(this.state.pos, "Expecting Unicode escape sequence \\uXXXX");
  6123. }
  6124. ++this.state.pos;
  6125. const esc = this.readCodePoint(true);
  6126. if (!identifierCheck(esc, true)) {
  6127. this.raise(escStart, "Invalid Unicode escape");
  6128. }
  6129. word += String.fromCodePoint(esc);
  6130. chunkStart = this.state.pos;
  6131. } else {
  6132. break;
  6133. }
  6134. }
  6135. return word + this.input.slice(chunkStart, this.state.pos);
  6136. }
  6137. isIterator(word) {
  6138. return word === "@@iterator" || word === "@@asyncIterator";
  6139. }
  6140. readWord() {
  6141. const word = this.readWord1();
  6142. const type = keywords.get(word) || types.name;
  6143. if (type.keyword && this.state.containsEsc) {
  6144. this.raise(this.state.pos, `Escape sequence in keyword ${word}`);
  6145. }
  6146. if (this.state.isIterator && (!this.isIterator(word) || !this.state.inType)) {
  6147. this.raise(this.state.pos, `Invalid identifier ${word}`);
  6148. }
  6149. this.finishToken(type, word);
  6150. }
  6151. braceIsBlock(prevType) {
  6152. const parent = this.curContext();
  6153. if (parent === types$1.functionExpression || parent === types$1.functionStatement) {
  6154. return true;
  6155. }
  6156. if (prevType === types.colon && (parent === types$1.braceStatement || parent === types$1.braceExpression)) {
  6157. return !parent.isExpr;
  6158. }
  6159. if (prevType === types._return || prevType === types.name && this.state.exprAllowed) {
  6160. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  6161. }
  6162. if (prevType === types._else || prevType === types.semi || prevType === types.eof || prevType === types.parenR || prevType === types.arrow) {
  6163. return true;
  6164. }
  6165. if (prevType === types.braceL) {
  6166. return parent === types$1.braceStatement;
  6167. }
  6168. if (prevType === types._var || prevType === types._const || prevType === types.name) {
  6169. return false;
  6170. }
  6171. if (prevType === types.relational) {
  6172. return true;
  6173. }
  6174. return !this.state.exprAllowed;
  6175. }
  6176. updateContext(prevType) {
  6177. const type = this.state.type;
  6178. let update;
  6179. if (type.keyword && (prevType === types.dot || prevType === types.questionDot)) {
  6180. this.state.exprAllowed = false;
  6181. } else if (update = type.updateContext) {
  6182. update.call(this, prevType);
  6183. } else {
  6184. this.state.exprAllowed = type.beforeExpr;
  6185. }
  6186. }
  6187. }
  6188. const literal = /^('|")((?:\\?.)*?)\1/;
  6189. class UtilParser extends Tokenizer {
  6190. addExtra(node, key, val) {
  6191. if (!node) return;
  6192. const extra = node.extra = node.extra || {};
  6193. extra[key] = val;
  6194. }
  6195. isRelational(op) {
  6196. return this.match(types.relational) && this.state.value === op;
  6197. }
  6198. isLookaheadRelational(op) {
  6199. const l = this.lookahead();
  6200. return l.type === types.relational && l.value === op;
  6201. }
  6202. expectRelational(op) {
  6203. if (this.isRelational(op)) {
  6204. this.next();
  6205. } else {
  6206. this.unexpected(null, types.relational);
  6207. }
  6208. }
  6209. eatRelational(op) {
  6210. if (this.isRelational(op)) {
  6211. this.next();
  6212. return true;
  6213. }
  6214. return false;
  6215. }
  6216. isContextual(name) {
  6217. return this.match(types.name) && this.state.value === name && !this.state.containsEsc;
  6218. }
  6219. isLookaheadContextual(name) {
  6220. const l = this.lookahead();
  6221. return l.type === types.name && l.value === name;
  6222. }
  6223. eatContextual(name) {
  6224. return this.isContextual(name) && this.eat(types.name);
  6225. }
  6226. expectContextual(name, message) {
  6227. if (!this.eatContextual(name)) this.unexpected(null, message);
  6228. }
  6229. canInsertSemicolon() {
  6230. return this.match(types.eof) || this.match(types.braceR) || this.hasPrecedingLineBreak();
  6231. }
  6232. hasPrecedingLineBreak() {
  6233. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  6234. }
  6235. isLineTerminator() {
  6236. return this.eat(types.semi) || this.canInsertSemicolon();
  6237. }
  6238. semicolon() {
  6239. if (!this.isLineTerminator()) this.unexpected(null, types.semi);
  6240. }
  6241. expect(type, pos) {
  6242. this.eat(type) || this.unexpected(pos, type);
  6243. }
  6244. assertNoSpace(message = "Unexpected space.") {
  6245. if (this.state.start > this.state.lastTokEnd) {
  6246. this.raise(this.state.lastTokEnd, message);
  6247. }
  6248. }
  6249. unexpected(pos, messageOrType = "Unexpected token") {
  6250. if (typeof messageOrType !== "string") {
  6251. messageOrType = `Unexpected token, expected "${messageOrType.label}"`;
  6252. }
  6253. throw this.raise(pos != null ? pos : this.state.start, messageOrType);
  6254. }
  6255. expectPlugin(name, pos) {
  6256. if (!this.hasPlugin(name)) {
  6257. throw this.raise(pos != null ? pos : this.state.start, `This experimental syntax requires enabling the parser plugin: '${name}'`, {
  6258. missingPluginNames: [name]
  6259. });
  6260. }
  6261. return true;
  6262. }
  6263. expectOnePlugin(names, pos) {
  6264. if (!names.some(n => this.hasPlugin(n))) {
  6265. throw this.raise(pos != null ? pos : this.state.start, `This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(", ")}'`, {
  6266. missingPluginNames: names
  6267. });
  6268. }
  6269. }
  6270. checkYieldAwaitInDefaultParams() {
  6271. if (this.state.yieldPos && (!this.state.awaitPos || this.state.yieldPos < this.state.awaitPos)) {
  6272. this.raise(this.state.yieldPos, "Yield cannot be used as name inside a generator function");
  6273. }
  6274. if (this.state.awaitPos) {
  6275. this.raise(this.state.awaitPos, "Await cannot be used as name inside an async function");
  6276. }
  6277. }
  6278. strictDirective(start) {
  6279. for (;;) {
  6280. skipWhiteSpace.lastIndex = start;
  6281. start += skipWhiteSpace.exec(this.input)[0].length;
  6282. const match = literal.exec(this.input.slice(start));
  6283. if (!match) break;
  6284. if (match[2] === "use strict") return true;
  6285. start += match[0].length;
  6286. skipWhiteSpace.lastIndex = start;
  6287. start += skipWhiteSpace.exec(this.input)[0].length;
  6288. if (this.input[start] === ";") {
  6289. start++;
  6290. }
  6291. }
  6292. return false;
  6293. }
  6294. }
  6295. class Node {
  6296. constructor(parser, pos, loc) {
  6297. this.type = "";
  6298. this.start = pos;
  6299. this.end = 0;
  6300. this.loc = new SourceLocation(loc);
  6301. if (parser && parser.options.ranges) this.range = [pos, 0];
  6302. if (parser && parser.filename) this.loc.filename = parser.filename;
  6303. }
  6304. __clone() {
  6305. const newNode = new Node();
  6306. const keys = Object.keys(this);
  6307. for (let i = 0, length = keys.length; i < length; i++) {
  6308. const key = keys[i];
  6309. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  6310. newNode[key] = this[key];
  6311. }
  6312. }
  6313. return newNode;
  6314. }
  6315. }
  6316. class NodeUtils extends UtilParser {
  6317. startNode() {
  6318. return new Node(this, this.state.start, this.state.startLoc);
  6319. }
  6320. startNodeAt(pos, loc) {
  6321. return new Node(this, pos, loc);
  6322. }
  6323. startNodeAtNode(type) {
  6324. return this.startNodeAt(type.start, type.loc.start);
  6325. }
  6326. finishNode(node, type) {
  6327. return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc);
  6328. }
  6329. finishNodeAt(node, type, pos, loc) {
  6330. node.type = type;
  6331. node.end = pos;
  6332. node.loc.end = loc;
  6333. if (this.options.ranges) node.range[1] = pos;
  6334. this.processComment(node);
  6335. return node;
  6336. }
  6337. resetStartLocation(node, start, startLoc) {
  6338. node.start = start;
  6339. node.loc.start = startLoc;
  6340. if (this.options.ranges) node.range[0] = start;
  6341. }
  6342. resetEndLocation(node, end = this.state.lastTokEnd, endLoc = this.state.lastTokEndLoc) {
  6343. node.end = end;
  6344. node.loc.end = endLoc;
  6345. if (this.options.ranges) node.range[1] = end;
  6346. }
  6347. resetStartLocationFromNode(node, locationNode) {
  6348. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  6349. }
  6350. }
  6351. class LValParser extends NodeUtils {
  6352. toAssignable(node, isBinding, contextDescription) {
  6353. if (node) {
  6354. switch (node.type) {
  6355. case "Identifier":
  6356. case "ObjectPattern":
  6357. case "ArrayPattern":
  6358. case "AssignmentPattern":
  6359. break;
  6360. case "ObjectExpression":
  6361. node.type = "ObjectPattern";
  6362. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  6363. const prop = node.properties[i];
  6364. const isLast = i === last;
  6365. this.toAssignableObjectExpressionProp(prop, isBinding, isLast);
  6366. }
  6367. break;
  6368. case "ObjectProperty":
  6369. this.toAssignable(node.value, isBinding, contextDescription);
  6370. break;
  6371. case "SpreadElement":
  6372. {
  6373. this.checkToRestConversion(node);
  6374. node.type = "RestElement";
  6375. const arg = node.argument;
  6376. this.toAssignable(arg, isBinding, contextDescription);
  6377. break;
  6378. }
  6379. case "ArrayExpression":
  6380. node.type = "ArrayPattern";
  6381. this.toAssignableList(node.elements, isBinding, contextDescription);
  6382. break;
  6383. case "AssignmentExpression":
  6384. if (node.operator === "=") {
  6385. node.type = "AssignmentPattern";
  6386. delete node.operator;
  6387. } else {
  6388. this.raise(node.left.end, "Only '=' operator can be used for specifying default value.");
  6389. }
  6390. break;
  6391. case "ParenthesizedExpression":
  6392. node.expression = this.toAssignable(node.expression, isBinding, contextDescription);
  6393. break;
  6394. case "MemberExpression":
  6395. if (!isBinding) break;
  6396. default:
  6397. {
  6398. const message = "Invalid left-hand side" + (contextDescription ? " in " + contextDescription : "expression");
  6399. this.raise(node.start, message);
  6400. }
  6401. }
  6402. }
  6403. return node;
  6404. }
  6405. toAssignableObjectExpressionProp(prop, isBinding, isLast) {
  6406. if (prop.type === "ObjectMethod") {
  6407. const error = prop.kind === "get" || prop.kind === "set" ? "Object pattern can't contain getter or setter" : "Object pattern can't contain methods";
  6408. this.raise(prop.key.start, error);
  6409. } else if (prop.type === "SpreadElement" && !isLast) {
  6410. this.raiseRestNotLast(prop.start);
  6411. } else {
  6412. this.toAssignable(prop, isBinding, "object destructuring pattern");
  6413. }
  6414. }
  6415. toAssignableList(exprList, isBinding, contextDescription) {
  6416. let end = exprList.length;
  6417. if (end) {
  6418. const last = exprList[end - 1];
  6419. if (last && last.type === "RestElement") {
  6420. --end;
  6421. } else if (last && last.type === "SpreadElement") {
  6422. last.type = "RestElement";
  6423. const arg = last.argument;
  6424. this.toAssignable(arg, isBinding, contextDescription);
  6425. if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
  6426. this.unexpected(arg.start);
  6427. }
  6428. --end;
  6429. }
  6430. }
  6431. for (let i = 0; i < end; i++) {
  6432. const elt = exprList[i];
  6433. if (elt) {
  6434. this.toAssignable(elt, isBinding, contextDescription);
  6435. if (elt.type === "RestElement") {
  6436. this.raiseRestNotLast(elt.start);
  6437. }
  6438. }
  6439. }
  6440. return exprList;
  6441. }
  6442. toReferencedList(exprList, isParenthesizedExpr) {
  6443. return exprList;
  6444. }
  6445. toReferencedListDeep(exprList, isParenthesizedExpr) {
  6446. this.toReferencedList(exprList, isParenthesizedExpr);
  6447. for (let _i = 0; _i < exprList.length; _i++) {
  6448. const expr = exprList[_i];
  6449. if (expr && expr.type === "ArrayExpression") {
  6450. this.toReferencedListDeep(expr.elements);
  6451. }
  6452. }
  6453. return exprList;
  6454. }
  6455. parseSpread(refShorthandDefaultPos, refNeedsArrowPos) {
  6456. const node = this.startNode();
  6457. this.next();
  6458. node.argument = this.parseMaybeAssign(false, refShorthandDefaultPos, undefined, refNeedsArrowPos);
  6459. if (this.state.commaAfterSpreadAt === -1 && this.match(types.comma)) {
  6460. this.state.commaAfterSpreadAt = this.state.start;
  6461. }
  6462. return this.finishNode(node, "SpreadElement");
  6463. }
  6464. parseRestBinding() {
  6465. const node = this.startNode();
  6466. this.next();
  6467. node.argument = this.parseBindingAtom();
  6468. return this.finishNode(node, "RestElement");
  6469. }
  6470. parseBindingAtom() {
  6471. switch (this.state.type) {
  6472. case types.bracketL:
  6473. {
  6474. const node = this.startNode();
  6475. this.next();
  6476. node.elements = this.parseBindingList(types.bracketR, true);
  6477. return this.finishNode(node, "ArrayPattern");
  6478. }
  6479. case types.braceL:
  6480. return this.parseObj(true);
  6481. }
  6482. return this.parseIdentifier();
  6483. }
  6484. parseBindingList(close, allowEmpty, allowModifiers) {
  6485. const elts = [];
  6486. let first = true;
  6487. while (!this.eat(close)) {
  6488. if (first) {
  6489. first = false;
  6490. } else {
  6491. this.expect(types.comma);
  6492. }
  6493. if (allowEmpty && this.match(types.comma)) {
  6494. elts.push(null);
  6495. } else if (this.eat(close)) {
  6496. break;
  6497. } else if (this.match(types.ellipsis)) {
  6498. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  6499. this.checkCommaAfterRest();
  6500. this.expect(close);
  6501. break;
  6502. } else {
  6503. const decorators = [];
  6504. if (this.match(types.at) && this.hasPlugin("decorators")) {
  6505. this.raise(this.state.start, "Stage 2 decorators cannot be used to decorate parameters");
  6506. }
  6507. while (this.match(types.at)) {
  6508. decorators.push(this.parseDecorator());
  6509. }
  6510. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  6511. }
  6512. }
  6513. return elts;
  6514. }
  6515. parseAssignableListItem(allowModifiers, decorators) {
  6516. const left = this.parseMaybeDefault();
  6517. this.parseAssignableListItemTypes(left);
  6518. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  6519. if (decorators.length) {
  6520. left.decorators = decorators;
  6521. }
  6522. return elt;
  6523. }
  6524. parseAssignableListItemTypes(param) {
  6525. return param;
  6526. }
  6527. parseMaybeDefault(startPos, startLoc, left) {
  6528. startLoc = startLoc || this.state.startLoc;
  6529. startPos = startPos || this.state.start;
  6530. left = left || this.parseBindingAtom();
  6531. if (!this.eat(types.eq)) return left;
  6532. const node = this.startNodeAt(startPos, startLoc);
  6533. node.left = left;
  6534. node.right = this.parseMaybeAssign();
  6535. return this.finishNode(node, "AssignmentPattern");
  6536. }
  6537. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  6538. switch (expr.type) {
  6539. case "Identifier":
  6540. if (this.state.strict && isStrictBindReservedWord(expr.name, this.inModule)) {
  6541. this.raise(expr.start, `${bindingType === BIND_NONE ? "Assigning to" : "Binding"} '${expr.name}' in strict mode`);
  6542. }
  6543. if (checkClashes) {
  6544. const key = `_${expr.name}`;
  6545. if (checkClashes[key]) {
  6546. this.raise(expr.start, "Argument name clash");
  6547. } else {
  6548. checkClashes[key] = true;
  6549. }
  6550. }
  6551. if (bindingType === BIND_LEXICAL && expr.name === "let") {
  6552. this.raise(expr.start, "'let' is not allowed to be used as a name in 'let' or 'const' declarations.");
  6553. }
  6554. if (!(bindingType & BIND_NONE)) {
  6555. this.scope.declareName(expr.name, bindingType, expr.start);
  6556. }
  6557. break;
  6558. case "MemberExpression":
  6559. if (bindingType !== BIND_NONE) {
  6560. this.raise(expr.start, "Binding member expression");
  6561. }
  6562. break;
  6563. case "ObjectPattern":
  6564. for (let _i2 = 0, _expr$properties = expr.properties; _i2 < _expr$properties.length; _i2++) {
  6565. let prop = _expr$properties[_i2];
  6566. if (prop.type === "ObjectProperty") prop = prop.value;
  6567. this.checkLVal(prop, bindingType, checkClashes, "object destructuring pattern");
  6568. }
  6569. break;
  6570. case "ArrayPattern":
  6571. for (let _i3 = 0, _expr$elements = expr.elements; _i3 < _expr$elements.length; _i3++) {
  6572. const elem = _expr$elements[_i3];
  6573. if (elem) {
  6574. this.checkLVal(elem, bindingType, checkClashes, "array destructuring pattern");
  6575. }
  6576. }
  6577. break;
  6578. case "AssignmentPattern":
  6579. this.checkLVal(expr.left, bindingType, checkClashes, "assignment pattern");
  6580. break;
  6581. case "RestElement":
  6582. this.checkLVal(expr.argument, bindingType, checkClashes, "rest element");
  6583. break;
  6584. case "ParenthesizedExpression":
  6585. this.checkLVal(expr.expression, bindingType, checkClashes, "parenthesized expression");
  6586. break;
  6587. default:
  6588. {
  6589. const message = (bindingType === BIND_NONE ? "Invalid" : "Binding invalid") + " left-hand side" + (contextDescription ? " in " + contextDescription : "expression");
  6590. this.raise(expr.start, message);
  6591. }
  6592. }
  6593. }
  6594. checkToRestConversion(node) {
  6595. if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
  6596. this.raise(node.argument.start, "Invalid rest operator's argument");
  6597. }
  6598. }
  6599. checkCommaAfterRest() {
  6600. if (this.match(types.comma)) {
  6601. this.raiseRestNotLast(this.state.start);
  6602. }
  6603. }
  6604. checkCommaAfterRestFromSpread() {
  6605. if (this.state.commaAfterSpreadAt > -1) {
  6606. this.raiseRestNotLast(this.state.commaAfterSpreadAt);
  6607. }
  6608. }
  6609. raiseRestNotLast(pos) {
  6610. this.raise(pos, `Rest element must be last element`);
  6611. }
  6612. }
  6613. const unwrapParenthesizedExpression = node => {
  6614. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  6615. };
  6616. class ExpressionParser extends LValParser {
  6617. checkPropClash(prop, propHash) {
  6618. if (prop.type === "SpreadElement" || prop.computed || prop.kind || prop.shorthand) {
  6619. return;
  6620. }
  6621. const key = prop.key;
  6622. const name = key.type === "Identifier" ? key.name : String(key.value);
  6623. if (name === "__proto__") {
  6624. if (propHash.proto) {
  6625. this.raise(key.start, "Redefinition of __proto__ property");
  6626. }
  6627. propHash.proto = true;
  6628. }
  6629. }
  6630. getExpression() {
  6631. this.scope.enter(SCOPE_PROGRAM);
  6632. this.nextToken();
  6633. const expr = this.parseExpression();
  6634. if (!this.match(types.eof)) {
  6635. this.unexpected();
  6636. }
  6637. expr.comments = this.state.comments;
  6638. return expr;
  6639. }
  6640. parseExpression(noIn, refShorthandDefaultPos) {
  6641. const startPos = this.state.start;
  6642. const startLoc = this.state.startLoc;
  6643. const expr = this.parseMaybeAssign(noIn, refShorthandDefaultPos);
  6644. if (this.match(types.comma)) {
  6645. const node = this.startNodeAt(startPos, startLoc);
  6646. node.expressions = [expr];
  6647. while (this.eat(types.comma)) {
  6648. node.expressions.push(this.parseMaybeAssign(noIn, refShorthandDefaultPos));
  6649. }
  6650. this.toReferencedList(node.expressions);
  6651. return this.finishNode(node, "SequenceExpression");
  6652. }
  6653. return expr;
  6654. }
  6655. parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) {
  6656. const startPos = this.state.start;
  6657. const startLoc = this.state.startLoc;
  6658. if (this.isContextual("yield")) {
  6659. if (this.scope.inGenerator) {
  6660. let left = this.parseYield(noIn);
  6661. if (afterLeftParse) {
  6662. left = afterLeftParse.call(this, left, startPos, startLoc);
  6663. }
  6664. return left;
  6665. } else {
  6666. this.state.exprAllowed = false;
  6667. }
  6668. }
  6669. const oldCommaAfterSpreadAt = this.state.commaAfterSpreadAt;
  6670. this.state.commaAfterSpreadAt = -1;
  6671. let failOnShorthandAssign;
  6672. if (refShorthandDefaultPos) {
  6673. failOnShorthandAssign = false;
  6674. } else {
  6675. refShorthandDefaultPos = {
  6676. start: 0
  6677. };
  6678. failOnShorthandAssign = true;
  6679. }
  6680. if (this.match(types.parenL) || this.match(types.name)) {
  6681. this.state.potentialArrowAt = this.state.start;
  6682. }
  6683. let left = this.parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos);
  6684. if (afterLeftParse) {
  6685. left = afterLeftParse.call(this, left, startPos, startLoc);
  6686. }
  6687. if (this.state.type.isAssign) {
  6688. const node = this.startNodeAt(startPos, startLoc);
  6689. const operator = this.state.value;
  6690. node.operator = operator;
  6691. if (operator === "??=") {
  6692. this.expectPlugin("nullishCoalescingOperator");
  6693. this.expectPlugin("logicalAssignment");
  6694. }
  6695. if (operator === "||=" || operator === "&&=") {
  6696. this.expectPlugin("logicalAssignment");
  6697. }
  6698. node.left = this.match(types.eq) ? this.toAssignable(left, undefined, "assignment expression") : left;
  6699. refShorthandDefaultPos.start = 0;
  6700. this.checkLVal(left, undefined, undefined, "assignment expression");
  6701. const maybePattern = unwrapParenthesizedExpression(left);
  6702. let patternErrorMsg;
  6703. if (maybePattern.type === "ObjectPattern") {
  6704. patternErrorMsg = "`({a}) = 0` use `({a} = 0)`";
  6705. } else if (maybePattern.type === "ArrayPattern") {
  6706. patternErrorMsg = "`([a]) = 0` use `([a] = 0)`";
  6707. }
  6708. if (patternErrorMsg && (left.extra && left.extra.parenthesized || left.type === "ParenthesizedExpression")) {
  6709. this.raise(maybePattern.start, `You're trying to assign to a parenthesized expression, eg. instead of ${patternErrorMsg}`);
  6710. }
  6711. if (patternErrorMsg) this.checkCommaAfterRestFromSpread();
  6712. this.state.commaAfterSpreadAt = oldCommaAfterSpreadAt;
  6713. this.next();
  6714. node.right = this.parseMaybeAssign(noIn);
  6715. return this.finishNode(node, "AssignmentExpression");
  6716. } else if (failOnShorthandAssign && refShorthandDefaultPos.start) {
  6717. this.unexpected(refShorthandDefaultPos.start);
  6718. }
  6719. this.state.commaAfterSpreadAt = oldCommaAfterSpreadAt;
  6720. return left;
  6721. }
  6722. parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos) {
  6723. const startPos = this.state.start;
  6724. const startLoc = this.state.startLoc;
  6725. const potentialArrowAt = this.state.potentialArrowAt;
  6726. const expr = this.parseExprOps(noIn, refShorthandDefaultPos);
  6727. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  6728. return expr;
  6729. }
  6730. if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
  6731. return this.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos);
  6732. }
  6733. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  6734. if (this.eat(types.question)) {
  6735. const node = this.startNodeAt(startPos, startLoc);
  6736. node.test = expr;
  6737. node.consequent = this.parseMaybeAssign();
  6738. this.expect(types.colon);
  6739. node.alternate = this.parseMaybeAssign(noIn);
  6740. return this.finishNode(node, "ConditionalExpression");
  6741. }
  6742. return expr;
  6743. }
  6744. parseExprOps(noIn, refShorthandDefaultPos) {
  6745. const startPos = this.state.start;
  6746. const startLoc = this.state.startLoc;
  6747. const potentialArrowAt = this.state.potentialArrowAt;
  6748. const expr = this.parseMaybeUnary(refShorthandDefaultPos);
  6749. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  6750. return expr;
  6751. }
  6752. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  6753. return expr;
  6754. }
  6755. return this.parseExprOp(expr, startPos, startLoc, -1, noIn);
  6756. }
  6757. parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn) {
  6758. const prec = this.state.type.binop;
  6759. if (prec != null && (!noIn || !this.match(types._in))) {
  6760. if (prec > minPrec) {
  6761. const operator = this.state.value;
  6762. if (operator === "|>" && this.state.inFSharpPipelineDirectBody) {
  6763. return left;
  6764. }
  6765. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  6766. node.left = left;
  6767. node.operator = operator;
  6768. if (operator === "**" && left.type === "UnaryExpression" && (this.options.createParenthesizedExpressions || !(left.extra && left.extra.parenthesized))) {
  6769. this.raise(left.argument.start, "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.");
  6770. }
  6771. const op = this.state.type;
  6772. if (op === types.pipeline) {
  6773. this.expectPlugin("pipelineOperator");
  6774. this.state.inPipeline = true;
  6775. this.checkPipelineAtInfixOperator(left, leftStartPos);
  6776. } else if (op === types.nullishCoalescing) {
  6777. this.expectPlugin("nullishCoalescingOperator");
  6778. }
  6779. this.next();
  6780. if (op === types.pipeline && this.getPluginOption("pipelineOperator", "proposal") === "minimal") {
  6781. if (this.match(types.name) && this.state.value === "await" && this.scope.inAsync) {
  6782. throw this.raise(this.state.start, `Unexpected "await" after pipeline body; await must have parentheses in minimal proposal`);
  6783. }
  6784. }
  6785. node.right = this.parseExprOpRightExpr(op, prec, noIn);
  6786. this.finishNode(node, op === types.logicalOR || op === types.logicalAND || op === types.nullishCoalescing ? "LogicalExpression" : "BinaryExpression");
  6787. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn);
  6788. }
  6789. }
  6790. return left;
  6791. }
  6792. parseExprOpRightExpr(op, prec, noIn) {
  6793. const startPos = this.state.start;
  6794. const startLoc = this.state.startLoc;
  6795. switch (op) {
  6796. case types.pipeline:
  6797. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  6798. case "smart":
  6799. return this.withTopicPermittingContext(() => {
  6800. return this.parseSmartPipelineBody(this.parseExprOpBaseRightExpr(op, prec, noIn), startPos, startLoc);
  6801. });
  6802. case "fsharp":
  6803. return this.withSoloAwaitPermittingContext(() => {
  6804. return this.parseFSharpPipelineBody(prec, noIn);
  6805. });
  6806. }
  6807. default:
  6808. return this.parseExprOpBaseRightExpr(op, prec, noIn);
  6809. }
  6810. }
  6811. parseExprOpBaseRightExpr(op, prec, noIn) {
  6812. const startPos = this.state.start;
  6813. const startLoc = this.state.startLoc;
  6814. return this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn);
  6815. }
  6816. parseMaybeUnary(refShorthandDefaultPos) {
  6817. if (this.isContextual("await") && (this.scope.inAsync || !this.scope.inFunction && this.options.allowAwaitOutsideFunction)) {
  6818. return this.parseAwait();
  6819. } else if (this.state.type.prefix) {
  6820. const node = this.startNode();
  6821. const update = this.match(types.incDec);
  6822. node.operator = this.state.value;
  6823. node.prefix = true;
  6824. if (node.operator === "throw") {
  6825. this.expectPlugin("throwExpressions");
  6826. }
  6827. this.next();
  6828. node.argument = this.parseMaybeUnary();
  6829. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  6830. this.unexpected(refShorthandDefaultPos.start);
  6831. }
  6832. if (update) {
  6833. this.checkLVal(node.argument, undefined, undefined, "prefix operation");
  6834. } else if (this.state.strict && node.operator === "delete") {
  6835. const arg = node.argument;
  6836. if (arg.type === "Identifier") {
  6837. this.raise(node.start, "Deleting local variable in strict mode");
  6838. } else if (arg.type === "MemberExpression" && arg.property.type === "PrivateName") {
  6839. this.raise(node.start, "Deleting a private field is not allowed");
  6840. }
  6841. }
  6842. return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
  6843. }
  6844. const startPos = this.state.start;
  6845. const startLoc = this.state.startLoc;
  6846. let expr = this.parseExprSubscripts(refShorthandDefaultPos);
  6847. if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
  6848. while (this.state.type.postfix && !this.canInsertSemicolon()) {
  6849. const node = this.startNodeAt(startPos, startLoc);
  6850. node.operator = this.state.value;
  6851. node.prefix = false;
  6852. node.argument = expr;
  6853. this.checkLVal(expr, undefined, undefined, "postfix operation");
  6854. this.next();
  6855. expr = this.finishNode(node, "UpdateExpression");
  6856. }
  6857. return expr;
  6858. }
  6859. parseExprSubscripts(refShorthandDefaultPos) {
  6860. const startPos = this.state.start;
  6861. const startLoc = this.state.startLoc;
  6862. const potentialArrowAt = this.state.potentialArrowAt;
  6863. const expr = this.parseExprAtom(refShorthandDefaultPos);
  6864. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  6865. return expr;
  6866. }
  6867. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  6868. return expr;
  6869. }
  6870. return this.parseSubscripts(expr, startPos, startLoc);
  6871. }
  6872. parseSubscripts(base, startPos, startLoc, noCalls) {
  6873. const maybeAsyncArrow = this.atPossibleAsync(base);
  6874. const state = {
  6875. optionalChainMember: false,
  6876. stop: false
  6877. };
  6878. do {
  6879. base = this.parseSubscript(base, startPos, startLoc, noCalls, state, maybeAsyncArrow);
  6880. } while (!state.stop);
  6881. return base;
  6882. }
  6883. parseSubscript(base, startPos, startLoc, noCalls, state, maybeAsyncArrow) {
  6884. if (!noCalls && this.eat(types.doubleColon)) {
  6885. const node = this.startNodeAt(startPos, startLoc);
  6886. node.object = base;
  6887. node.callee = this.parseNoCallExpr();
  6888. state.stop = true;
  6889. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  6890. } else if (this.match(types.questionDot)) {
  6891. this.expectPlugin("optionalChaining");
  6892. state.optionalChainMember = true;
  6893. if (noCalls && this.lookahead().type === types.parenL) {
  6894. state.stop = true;
  6895. return base;
  6896. }
  6897. this.next();
  6898. const node = this.startNodeAt(startPos, startLoc);
  6899. if (this.eat(types.bracketL)) {
  6900. node.object = base;
  6901. node.property = this.parseExpression();
  6902. node.computed = true;
  6903. node.optional = true;
  6904. this.expect(types.bracketR);
  6905. return this.finishNode(node, "OptionalMemberExpression");
  6906. } else if (this.eat(types.parenL)) {
  6907. node.callee = base;
  6908. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  6909. node.optional = true;
  6910. return this.finishNode(node, "OptionalCallExpression");
  6911. } else {
  6912. node.object = base;
  6913. node.property = this.parseIdentifier(true);
  6914. node.computed = false;
  6915. node.optional = true;
  6916. return this.finishNode(node, "OptionalMemberExpression");
  6917. }
  6918. } else if (this.eat(types.dot)) {
  6919. const node = this.startNodeAt(startPos, startLoc);
  6920. node.object = base;
  6921. node.property = this.parseMaybePrivateName();
  6922. node.computed = false;
  6923. if (state.optionalChainMember) {
  6924. node.optional = false;
  6925. return this.finishNode(node, "OptionalMemberExpression");
  6926. }
  6927. return this.finishNode(node, "MemberExpression");
  6928. } else if (this.eat(types.bracketL)) {
  6929. const node = this.startNodeAt(startPos, startLoc);
  6930. node.object = base;
  6931. node.property = this.parseExpression();
  6932. node.computed = true;
  6933. this.expect(types.bracketR);
  6934. if (state.optionalChainMember) {
  6935. node.optional = false;
  6936. return this.finishNode(node, "OptionalMemberExpression");
  6937. }
  6938. return this.finishNode(node, "MemberExpression");
  6939. } else if (!noCalls && this.match(types.parenL)) {
  6940. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  6941. const oldYieldPos = this.state.yieldPos;
  6942. const oldAwaitPos = this.state.awaitPos;
  6943. this.state.maybeInArrowParameters = true;
  6944. this.state.yieldPos = 0;
  6945. this.state.awaitPos = 0;
  6946. this.next();
  6947. let node = this.startNodeAt(startPos, startLoc);
  6948. node.callee = base;
  6949. const oldCommaAfterSpreadAt = this.state.commaAfterSpreadAt;
  6950. this.state.commaAfterSpreadAt = -1;
  6951. node.arguments = this.parseCallExpressionArguments(types.parenR, maybeAsyncArrow, base.type === "Import", base.type !== "Super");
  6952. if (!state.optionalChainMember) {
  6953. this.finishCallExpression(node);
  6954. } else {
  6955. this.finishOptionalCallExpression(node);
  6956. }
  6957. if (maybeAsyncArrow && this.shouldParseAsyncArrow()) {
  6958. state.stop = true;
  6959. this.checkCommaAfterRestFromSpread();
  6960. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  6961. this.checkYieldAwaitInDefaultParams();
  6962. this.state.yieldPos = oldYieldPos;
  6963. this.state.awaitPos = oldAwaitPos;
  6964. } else {
  6965. this.toReferencedListDeep(node.arguments);
  6966. this.state.yieldPos = oldYieldPos || this.state.yieldPos;
  6967. this.state.awaitPos = oldAwaitPos || this.state.awaitPos;
  6968. }
  6969. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  6970. this.state.commaAfterSpreadAt = oldCommaAfterSpreadAt;
  6971. return node;
  6972. } else if (this.match(types.backQuote)) {
  6973. return this.parseTaggedTemplateExpression(startPos, startLoc, base, state);
  6974. } else {
  6975. state.stop = true;
  6976. return base;
  6977. }
  6978. }
  6979. parseTaggedTemplateExpression(startPos, startLoc, base, state, typeArguments) {
  6980. const node = this.startNodeAt(startPos, startLoc);
  6981. node.tag = base;
  6982. node.quasi = this.parseTemplate(true);
  6983. if (typeArguments) node.typeParameters = typeArguments;
  6984. if (state.optionalChainMember) {
  6985. this.raise(startPos, "Tagged Template Literals are not allowed in optionalChain");
  6986. }
  6987. return this.finishNode(node, "TaggedTemplateExpression");
  6988. }
  6989. atPossibleAsync(base) {
  6990. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEnd === base.end && !this.canInsertSemicolon() && this.input.slice(base.start, base.end) === "async";
  6991. }
  6992. finishCallExpression(node) {
  6993. if (node.callee.type === "Import") {
  6994. if (node.arguments.length !== 1) {
  6995. this.raise(node.start, "import() requires exactly one argument");
  6996. }
  6997. const importArg = node.arguments[0];
  6998. if (importArg && importArg.type === "SpreadElement") {
  6999. this.raise(importArg.start, "... is not allowed in import()");
  7000. }
  7001. }
  7002. return this.finishNode(node, "CallExpression");
  7003. }
  7004. finishOptionalCallExpression(node) {
  7005. if (node.callee.type === "Import") {
  7006. if (node.arguments.length !== 1) {
  7007. this.raise(node.start, "import() requires exactly one argument");
  7008. }
  7009. const importArg = node.arguments[0];
  7010. if (importArg && importArg.type === "SpreadElement") {
  7011. this.raise(importArg.start, "... is not allowed in import()");
  7012. }
  7013. }
  7014. return this.finishNode(node, "OptionalCallExpression");
  7015. }
  7016. parseCallExpressionArguments(close, possibleAsyncArrow, dynamicImport, allowPlaceholder) {
  7017. const elts = [];
  7018. let innerParenStart;
  7019. let first = true;
  7020. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  7021. this.state.inFSharpPipelineDirectBody = false;
  7022. while (!this.eat(close)) {
  7023. if (first) {
  7024. first = false;
  7025. } else {
  7026. this.expect(types.comma);
  7027. if (this.eat(close)) {
  7028. if (dynamicImport) {
  7029. this.raise(this.state.lastTokStart, "Trailing comma is disallowed inside import(...) arguments");
  7030. }
  7031. break;
  7032. }
  7033. }
  7034. if (this.match(types.parenL) && !innerParenStart) {
  7035. innerParenStart = this.state.start;
  7036. }
  7037. elts.push(this.parseExprListItem(false, possibleAsyncArrow ? {
  7038. start: 0
  7039. } : undefined, possibleAsyncArrow ? {
  7040. start: 0
  7041. } : undefined, allowPlaceholder));
  7042. }
  7043. if (possibleAsyncArrow && innerParenStart && this.shouldParseAsyncArrow()) {
  7044. this.unexpected();
  7045. }
  7046. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  7047. return elts;
  7048. }
  7049. shouldParseAsyncArrow() {
  7050. return this.match(types.arrow) && !this.canInsertSemicolon();
  7051. }
  7052. parseAsyncArrowFromCallExpression(node, call) {
  7053. this.expect(types.arrow);
  7054. this.parseArrowExpression(node, call.arguments, true);
  7055. return node;
  7056. }
  7057. parseNoCallExpr() {
  7058. const startPos = this.state.start;
  7059. const startLoc = this.state.startLoc;
  7060. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  7061. }
  7062. parseExprAtom(refShorthandDefaultPos) {
  7063. if (this.state.type === types.slash) this.readRegexp();
  7064. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  7065. let node;
  7066. switch (this.state.type) {
  7067. case types._super:
  7068. if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  7069. this.raise(this.state.start, "super is only allowed in object methods and classes");
  7070. }
  7071. node = this.startNode();
  7072. this.next();
  7073. if (this.match(types.parenL) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  7074. this.raise(node.start, "super() is only valid inside a class constructor of a subclass. " + "Maybe a typo in the method name ('constructor') or not extending another class?");
  7075. }
  7076. if (!this.match(types.parenL) && !this.match(types.bracketL) && !this.match(types.dot)) {
  7077. this.unexpected();
  7078. }
  7079. return this.finishNode(node, "Super");
  7080. case types._import:
  7081. node = this.startNode();
  7082. this.next();
  7083. if (this.match(types.dot)) {
  7084. return this.parseImportMetaProperty(node);
  7085. }
  7086. this.expectPlugin("dynamicImport", node.start);
  7087. if (!this.match(types.parenL)) {
  7088. this.unexpected(null, types.parenL);
  7089. }
  7090. return this.finishNode(node, "Import");
  7091. case types._this:
  7092. node = this.startNode();
  7093. this.next();
  7094. return this.finishNode(node, "ThisExpression");
  7095. case types.name:
  7096. {
  7097. node = this.startNode();
  7098. const containsEsc = this.state.containsEsc;
  7099. const id = this.parseIdentifier();
  7100. if (!containsEsc && id.name === "async" && this.match(types._function) && !this.canInsertSemicolon()) {
  7101. this.next();
  7102. return this.parseFunction(node, undefined, true);
  7103. } else if (canBeArrow && !containsEsc && id.name === "async" && this.match(types.name) && !this.canInsertSemicolon()) {
  7104. const params = [this.parseIdentifier()];
  7105. this.expect(types.arrow);
  7106. this.parseArrowExpression(node, params, true);
  7107. return node;
  7108. }
  7109. if (canBeArrow && this.match(types.arrow) && !this.canInsertSemicolon()) {
  7110. this.next();
  7111. this.parseArrowExpression(node, [id], false);
  7112. return node;
  7113. }
  7114. return id;
  7115. }
  7116. case types._do:
  7117. {
  7118. this.expectPlugin("doExpressions");
  7119. const node = this.startNode();
  7120. this.next();
  7121. const oldLabels = this.state.labels;
  7122. this.state.labels = [];
  7123. node.body = this.parseBlock();
  7124. this.state.labels = oldLabels;
  7125. return this.finishNode(node, "DoExpression");
  7126. }
  7127. case types.regexp:
  7128. {
  7129. const value = this.state.value;
  7130. node = this.parseLiteral(value.value, "RegExpLiteral");
  7131. node.pattern = value.pattern;
  7132. node.flags = value.flags;
  7133. return node;
  7134. }
  7135. case types.num:
  7136. return this.parseLiteral(this.state.value, "NumericLiteral");
  7137. case types.bigint:
  7138. return this.parseLiteral(this.state.value, "BigIntLiteral");
  7139. case types.string:
  7140. return this.parseLiteral(this.state.value, "StringLiteral");
  7141. case types._null:
  7142. node = this.startNode();
  7143. this.next();
  7144. return this.finishNode(node, "NullLiteral");
  7145. case types._true:
  7146. case types._false:
  7147. return this.parseBooleanLiteral();
  7148. case types.parenL:
  7149. return this.parseParenAndDistinguishExpression(canBeArrow);
  7150. case types.bracketL:
  7151. {
  7152. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  7153. this.state.inFSharpPipelineDirectBody = false;
  7154. node = this.startNode();
  7155. this.next();
  7156. node.elements = this.parseExprList(types.bracketR, true, refShorthandDefaultPos);
  7157. if (!this.state.maybeInArrowParameters) {
  7158. this.toReferencedList(node.elements);
  7159. }
  7160. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  7161. return this.finishNode(node, "ArrayExpression");
  7162. }
  7163. case types.braceL:
  7164. {
  7165. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  7166. this.state.inFSharpPipelineDirectBody = false;
  7167. const ret = this.parseObj(false, refShorthandDefaultPos);
  7168. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  7169. return ret;
  7170. }
  7171. case types._function:
  7172. return this.parseFunctionExpression();
  7173. case types.at:
  7174. this.parseDecorators();
  7175. case types._class:
  7176. node = this.startNode();
  7177. this.takeDecorators(node);
  7178. return this.parseClass(node, false);
  7179. case types._new:
  7180. return this.parseNew();
  7181. case types.backQuote:
  7182. return this.parseTemplate(false);
  7183. case types.doubleColon:
  7184. {
  7185. node = this.startNode();
  7186. this.next();
  7187. node.object = null;
  7188. const callee = node.callee = this.parseNoCallExpr();
  7189. if (callee.type === "MemberExpression") {
  7190. return this.finishNode(node, "BindExpression");
  7191. } else {
  7192. throw this.raise(callee.start, "Binding should be performed on object property.");
  7193. }
  7194. }
  7195. case types.hash:
  7196. {
  7197. if (this.state.inPipeline) {
  7198. node = this.startNode();
  7199. if (this.getPluginOption("pipelineOperator", "proposal") !== "smart") {
  7200. this.raise(node.start, "Primary Topic Reference found but pipelineOperator not passed 'smart' for 'proposal' option.");
  7201. }
  7202. this.next();
  7203. if (this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) {
  7204. this.registerTopicReference();
  7205. return this.finishNode(node, "PipelinePrimaryTopicReference");
  7206. } else {
  7207. throw this.raise(node.start, `Topic reference was used in a lexical context without topic binding`);
  7208. }
  7209. }
  7210. }
  7211. default:
  7212. throw this.unexpected();
  7213. }
  7214. }
  7215. parseBooleanLiteral() {
  7216. const node = this.startNode();
  7217. node.value = this.match(types._true);
  7218. this.next();
  7219. return this.finishNode(node, "BooleanLiteral");
  7220. }
  7221. parseMaybePrivateName() {
  7222. const isPrivate = this.match(types.hash);
  7223. if (isPrivate) {
  7224. this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]);
  7225. const node = this.startNode();
  7226. this.next();
  7227. this.assertNoSpace("Unexpected space between # and identifier");
  7228. node.id = this.parseIdentifier(true);
  7229. return this.finishNode(node, "PrivateName");
  7230. } else {
  7231. return this.parseIdentifier(true);
  7232. }
  7233. }
  7234. parseFunctionExpression() {
  7235. const node = this.startNode();
  7236. let meta = this.startNode();
  7237. this.next();
  7238. meta = this.createIdentifier(meta, "function");
  7239. if (this.scope.inGenerator && this.eat(types.dot)) {
  7240. return this.parseMetaProperty(node, meta, "sent");
  7241. }
  7242. return this.parseFunction(node);
  7243. }
  7244. parseMetaProperty(node, meta, propertyName) {
  7245. node.meta = meta;
  7246. if (meta.name === "function" && propertyName === "sent") {
  7247. if (this.isContextual(propertyName)) {
  7248. this.expectPlugin("functionSent");
  7249. } else if (!this.hasPlugin("functionSent")) {
  7250. this.unexpected();
  7251. }
  7252. }
  7253. const containsEsc = this.state.containsEsc;
  7254. node.property = this.parseIdentifier(true);
  7255. if (node.property.name !== propertyName || containsEsc) {
  7256. this.raise(node.property.start, `The only valid meta property for ${meta.name} is ${meta.name}.${propertyName}`);
  7257. }
  7258. return this.finishNode(node, "MetaProperty");
  7259. }
  7260. parseImportMetaProperty(node) {
  7261. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  7262. this.expect(types.dot);
  7263. if (this.isContextual("meta")) {
  7264. this.expectPlugin("importMeta");
  7265. } else if (!this.hasPlugin("importMeta")) {
  7266. this.raise(id.start, `Dynamic imports require a parameter: import('a.js')`);
  7267. }
  7268. if (!this.inModule) {
  7269. this.raise(id.start, `import.meta may appear only with 'sourceType: "module"'`, {
  7270. code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  7271. });
  7272. }
  7273. this.sawUnambiguousESM = true;
  7274. return this.parseMetaProperty(node, id, "meta");
  7275. }
  7276. parseLiteral(value, type, startPos, startLoc) {
  7277. startPos = startPos || this.state.start;
  7278. startLoc = startLoc || this.state.startLoc;
  7279. const node = this.startNodeAt(startPos, startLoc);
  7280. this.addExtra(node, "rawValue", value);
  7281. this.addExtra(node, "raw", this.input.slice(startPos, this.state.end));
  7282. node.value = value;
  7283. this.next();
  7284. return this.finishNode(node, type);
  7285. }
  7286. parseParenAndDistinguishExpression(canBeArrow) {
  7287. const startPos = this.state.start;
  7288. const startLoc = this.state.startLoc;
  7289. let val;
  7290. this.expect(types.parenL);
  7291. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  7292. const oldYieldPos = this.state.yieldPos;
  7293. const oldAwaitPos = this.state.awaitPos;
  7294. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  7295. this.state.maybeInArrowParameters = true;
  7296. this.state.yieldPos = 0;
  7297. this.state.awaitPos = 0;
  7298. this.state.inFSharpPipelineDirectBody = false;
  7299. const innerStartPos = this.state.start;
  7300. const innerStartLoc = this.state.startLoc;
  7301. const exprList = [];
  7302. const refShorthandDefaultPos = {
  7303. start: 0
  7304. };
  7305. const refNeedsArrowPos = {
  7306. start: 0
  7307. };
  7308. let first = true;
  7309. let spreadStart;
  7310. let optionalCommaStart;
  7311. while (!this.match(types.parenR)) {
  7312. if (first) {
  7313. first = false;
  7314. } else {
  7315. this.expect(types.comma, refNeedsArrowPos.start || null);
  7316. if (this.match(types.parenR)) {
  7317. optionalCommaStart = this.state.start;
  7318. break;
  7319. }
  7320. }
  7321. if (this.match(types.ellipsis)) {
  7322. const spreadNodeStartPos = this.state.start;
  7323. const spreadNodeStartLoc = this.state.startLoc;
  7324. spreadStart = this.state.start;
  7325. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  7326. this.checkCommaAfterRest();
  7327. break;
  7328. } else {
  7329. exprList.push(this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos));
  7330. }
  7331. }
  7332. const innerEndPos = this.state.start;
  7333. const innerEndLoc = this.state.startLoc;
  7334. this.expect(types.parenR);
  7335. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  7336. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  7337. let arrowNode = this.startNodeAt(startPos, startLoc);
  7338. if (canBeArrow && this.shouldParseArrow() && (arrowNode = this.parseArrow(arrowNode))) {
  7339. this.checkYieldAwaitInDefaultParams();
  7340. this.state.yieldPos = oldYieldPos;
  7341. this.state.awaitPos = oldAwaitPos;
  7342. for (let _i = 0; _i < exprList.length; _i++) {
  7343. const param = exprList[_i];
  7344. if (param.extra && param.extra.parenthesized) {
  7345. this.unexpected(param.extra.parenStart);
  7346. }
  7347. }
  7348. this.parseArrowExpression(arrowNode, exprList, false);
  7349. return arrowNode;
  7350. }
  7351. this.state.yieldPos = oldYieldPos || this.state.yieldPos;
  7352. this.state.awaitPos = oldAwaitPos || this.state.awaitPos;
  7353. if (!exprList.length) {
  7354. this.unexpected(this.state.lastTokStart);
  7355. }
  7356. if (optionalCommaStart) this.unexpected(optionalCommaStart);
  7357. if (spreadStart) this.unexpected(spreadStart);
  7358. if (refShorthandDefaultPos.start) {
  7359. this.unexpected(refShorthandDefaultPos.start);
  7360. }
  7361. if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);
  7362. this.toReferencedListDeep(exprList, true);
  7363. if (exprList.length > 1) {
  7364. val = this.startNodeAt(innerStartPos, innerStartLoc);
  7365. val.expressions = exprList;
  7366. this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
  7367. } else {
  7368. val = exprList[0];
  7369. }
  7370. if (!this.options.createParenthesizedExpressions) {
  7371. this.addExtra(val, "parenthesized", true);
  7372. this.addExtra(val, "parenStart", startPos);
  7373. return val;
  7374. }
  7375. const parenExpression = this.startNodeAt(startPos, startLoc);
  7376. parenExpression.expression = val;
  7377. this.finishNode(parenExpression, "ParenthesizedExpression");
  7378. return parenExpression;
  7379. }
  7380. shouldParseArrow() {
  7381. return !this.canInsertSemicolon();
  7382. }
  7383. parseArrow(node) {
  7384. if (this.eat(types.arrow)) {
  7385. return node;
  7386. }
  7387. }
  7388. parseParenItem(node, startPos, startLoc) {
  7389. return node;
  7390. }
  7391. parseNew() {
  7392. const node = this.startNode();
  7393. const meta = this.parseIdentifier(true);
  7394. if (this.eat(types.dot)) {
  7395. const metaProp = this.parseMetaProperty(node, meta, "target");
  7396. if (!this.scope.inNonArrowFunction && !this.state.inClassProperty) {
  7397. let error = "new.target can only be used in functions";
  7398. if (this.hasPlugin("classProperties")) {
  7399. error += " or class properties";
  7400. }
  7401. this.raise(metaProp.start, error);
  7402. }
  7403. return metaProp;
  7404. }
  7405. node.callee = this.parseNoCallExpr();
  7406. if (node.callee.type === "Import") {
  7407. this.raise(node.callee.start, "Cannot use new with import(...)");
  7408. } else if (node.callee.type === "OptionalMemberExpression" || node.callee.type === "OptionalCallExpression") {
  7409. this.raise(this.state.lastTokEnd, "constructors in/after an Optional Chain are not allowed");
  7410. } else if (this.eat(types.questionDot)) {
  7411. this.raise(this.state.start, "constructors in/after an Optional Chain are not allowed");
  7412. }
  7413. this.parseNewArguments(node);
  7414. return this.finishNode(node, "NewExpression");
  7415. }
  7416. parseNewArguments(node) {
  7417. if (this.eat(types.parenL)) {
  7418. const args = this.parseExprList(types.parenR);
  7419. this.toReferencedList(args);
  7420. node.arguments = args;
  7421. } else {
  7422. node.arguments = [];
  7423. }
  7424. }
  7425. parseTemplateElement(isTagged) {
  7426. const elem = this.startNode();
  7427. if (this.state.value === null) {
  7428. if (!isTagged) {
  7429. this.raise(this.state.invalidTemplateEscapePosition || 0, "Invalid escape sequence in template");
  7430. } else {
  7431. this.state.invalidTemplateEscapePosition = null;
  7432. }
  7433. }
  7434. elem.value = {
  7435. raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
  7436. cooked: this.state.value
  7437. };
  7438. this.next();
  7439. elem.tail = this.match(types.backQuote);
  7440. return this.finishNode(elem, "TemplateElement");
  7441. }
  7442. parseTemplate(isTagged) {
  7443. const node = this.startNode();
  7444. this.next();
  7445. node.expressions = [];
  7446. let curElt = this.parseTemplateElement(isTagged);
  7447. node.quasis = [curElt];
  7448. while (!curElt.tail) {
  7449. this.expect(types.dollarBraceL);
  7450. node.expressions.push(this.parseExpression());
  7451. this.expect(types.braceR);
  7452. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  7453. }
  7454. this.next();
  7455. return this.finishNode(node, "TemplateLiteral");
  7456. }
  7457. parseObj(isPattern, refShorthandDefaultPos) {
  7458. const propHash = Object.create(null);
  7459. let first = true;
  7460. const node = this.startNode();
  7461. node.properties = [];
  7462. this.next();
  7463. while (!this.eat(types.braceR)) {
  7464. if (first) {
  7465. first = false;
  7466. } else {
  7467. this.expect(types.comma);
  7468. if (this.eat(types.braceR)) break;
  7469. }
  7470. const prop = this.parseObjectMember(isPattern, refShorthandDefaultPos);
  7471. if (!isPattern) this.checkPropClash(prop, propHash);
  7472. if (prop.shorthand) {
  7473. this.addExtra(prop, "shorthand", true);
  7474. }
  7475. node.properties.push(prop);
  7476. }
  7477. return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression");
  7478. }
  7479. isAsyncProp(prop) {
  7480. return !prop.computed && prop.key.type === "Identifier" && prop.key.name === "async" && (this.match(types.name) || this.match(types.num) || this.match(types.string) || this.match(types.bracketL) || this.state.type.keyword || this.match(types.star)) && !this.hasPrecedingLineBreak();
  7481. }
  7482. parseObjectMember(isPattern, refShorthandDefaultPos) {
  7483. let decorators = [];
  7484. if (this.match(types.at)) {
  7485. if (this.hasPlugin("decorators")) {
  7486. this.raise(this.state.start, "Stage 2 decorators disallow object literal property decorators");
  7487. } else {
  7488. while (this.match(types.at)) {
  7489. decorators.push(this.parseDecorator());
  7490. }
  7491. }
  7492. }
  7493. const prop = this.startNode();
  7494. let isGenerator = false;
  7495. let isAsync = false;
  7496. let startPos;
  7497. let startLoc;
  7498. if (this.match(types.ellipsis)) {
  7499. if (decorators.length) this.unexpected();
  7500. if (isPattern) {
  7501. this.next();
  7502. prop.argument = this.parseIdentifier();
  7503. this.checkCommaAfterRest();
  7504. return this.finishNode(prop, "RestElement");
  7505. }
  7506. return this.parseSpread();
  7507. }
  7508. if (decorators.length) {
  7509. prop.decorators = decorators;
  7510. decorators = [];
  7511. }
  7512. prop.method = false;
  7513. if (isPattern || refShorthandDefaultPos) {
  7514. startPos = this.state.start;
  7515. startLoc = this.state.startLoc;
  7516. }
  7517. if (!isPattern) {
  7518. isGenerator = this.eat(types.star);
  7519. }
  7520. const containsEsc = this.state.containsEsc;
  7521. this.parsePropertyName(prop);
  7522. if (!isPattern && !containsEsc && !isGenerator && this.isAsyncProp(prop)) {
  7523. isAsync = true;
  7524. isGenerator = this.eat(types.star);
  7525. this.parsePropertyName(prop);
  7526. } else {
  7527. isAsync = false;
  7528. }
  7529. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc);
  7530. return prop;
  7531. }
  7532. isGetterOrSetterMethod(prop, isPattern) {
  7533. return !isPattern && !prop.computed && prop.key.type === "Identifier" && (prop.key.name === "get" || prop.key.name === "set") && (this.match(types.string) || this.match(types.num) || this.match(types.bracketL) || this.match(types.name) || !!this.state.type.keyword);
  7534. }
  7535. getGetterSetterExpectedParamCount(method) {
  7536. return method.kind === "get" ? 0 : 1;
  7537. }
  7538. checkGetterSetterParams(method) {
  7539. const paramCount = this.getGetterSetterExpectedParamCount(method);
  7540. const start = method.start;
  7541. if (method.params.length !== paramCount) {
  7542. if (method.kind === "get") {
  7543. this.raise(start, "getter must not have any formal parameters");
  7544. } else {
  7545. this.raise(start, "setter must have exactly one formal parameter");
  7546. }
  7547. }
  7548. if (method.kind === "set" && method.params[method.params.length - 1].type === "RestElement") {
  7549. this.raise(start, "setter function argument must not be a rest parameter");
  7550. }
  7551. }
  7552. parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) {
  7553. if (isAsync || isGenerator || this.match(types.parenL)) {
  7554. if (isPattern) this.unexpected();
  7555. prop.kind = "method";
  7556. prop.method = true;
  7557. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  7558. }
  7559. if (!containsEsc && this.isGetterOrSetterMethod(prop, isPattern)) {
  7560. if (isGenerator || isAsync) this.unexpected();
  7561. prop.kind = prop.key.name;
  7562. this.parsePropertyName(prop);
  7563. this.parseMethod(prop, false, false, false, false, "ObjectMethod");
  7564. this.checkGetterSetterParams(prop);
  7565. return prop;
  7566. }
  7567. }
  7568. parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos) {
  7569. prop.shorthand = false;
  7570. if (this.eat(types.colon)) {
  7571. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssign(false, refShorthandDefaultPos);
  7572. return this.finishNode(prop, "ObjectProperty");
  7573. }
  7574. if (!prop.computed && prop.key.type === "Identifier") {
  7575. this.checkReservedWord(prop.key.name, prop.key.start, true, true);
  7576. if (isPattern) {
  7577. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  7578. } else if (this.match(types.eq) && refShorthandDefaultPos) {
  7579. if (!refShorthandDefaultPos.start) {
  7580. refShorthandDefaultPos.start = this.state.start;
  7581. }
  7582. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  7583. } else {
  7584. prop.value = prop.key.__clone();
  7585. }
  7586. prop.shorthand = true;
  7587. return this.finishNode(prop, "ObjectProperty");
  7588. }
  7589. }
  7590. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc) {
  7591. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos);
  7592. if (!node) this.unexpected();
  7593. return node;
  7594. }
  7595. parsePropertyName(prop) {
  7596. if (this.eat(types.bracketL)) {
  7597. prop.computed = true;
  7598. prop.key = this.parseMaybeAssign();
  7599. this.expect(types.bracketR);
  7600. } else {
  7601. const oldInPropertyName = this.state.inPropertyName;
  7602. this.state.inPropertyName = true;
  7603. prop.key = this.match(types.num) || this.match(types.string) ? this.parseExprAtom() : this.parseMaybePrivateName();
  7604. if (prop.key.type !== "PrivateName") {
  7605. prop.computed = false;
  7606. }
  7607. this.state.inPropertyName = oldInPropertyName;
  7608. }
  7609. return prop.key;
  7610. }
  7611. initFunction(node, isAsync) {
  7612. node.id = null;
  7613. node.generator = false;
  7614. node.async = !!isAsync;
  7615. }
  7616. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  7617. const oldYieldPos = this.state.yieldPos;
  7618. const oldAwaitPos = this.state.awaitPos;
  7619. this.state.yieldPos = 0;
  7620. this.state.awaitPos = 0;
  7621. this.initFunction(node, isAsync);
  7622. node.generator = !!isGenerator;
  7623. const allowModifiers = isConstructor;
  7624. this.scope.enter(functionFlags(isAsync, node.generator) | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  7625. this.parseFunctionParams(node, allowModifiers);
  7626. this.checkYieldAwaitInDefaultParams();
  7627. this.parseFunctionBodyAndFinish(node, type, true);
  7628. this.scope.exit();
  7629. this.state.yieldPos = oldYieldPos;
  7630. this.state.awaitPos = oldAwaitPos;
  7631. return node;
  7632. }
  7633. parseArrowExpression(node, params, isAsync) {
  7634. this.scope.enter(functionFlags(isAsync, false) | SCOPE_ARROW);
  7635. this.initFunction(node, isAsync);
  7636. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  7637. const oldYieldPos = this.state.yieldPos;
  7638. const oldAwaitPos = this.state.awaitPos;
  7639. this.state.maybeInArrowParameters = false;
  7640. this.state.yieldPos = 0;
  7641. this.state.awaitPos = 0;
  7642. if (params) this.setArrowFunctionParameters(node, params);
  7643. this.parseFunctionBody(node, true);
  7644. this.scope.exit();
  7645. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  7646. this.state.yieldPos = oldYieldPos;
  7647. this.state.awaitPos = oldAwaitPos;
  7648. return this.finishNode(node, "ArrowFunctionExpression");
  7649. }
  7650. setArrowFunctionParameters(node, params) {
  7651. node.params = this.toAssignableList(params, true, "arrow function parameters");
  7652. }
  7653. isStrictBody(node) {
  7654. const isBlockStatement = node.body.type === "BlockStatement";
  7655. if (isBlockStatement && node.body.directives.length) {
  7656. for (let _i2 = 0, _node$body$directives = node.body.directives; _i2 < _node$body$directives.length; _i2++) {
  7657. const directive = _node$body$directives[_i2];
  7658. if (directive.value.value === "use strict") {
  7659. return true;
  7660. }
  7661. }
  7662. }
  7663. return false;
  7664. }
  7665. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  7666. this.parseFunctionBody(node, false, isMethod);
  7667. this.finishNode(node, type);
  7668. }
  7669. parseFunctionBody(node, allowExpression, isMethod = false) {
  7670. const isExpression = allowExpression && !this.match(types.braceL);
  7671. const oldStrict = this.state.strict;
  7672. let useStrict = false;
  7673. const oldInParameters = this.state.inParameters;
  7674. this.state.inParameters = false;
  7675. if (isExpression) {
  7676. node.body = this.parseMaybeAssign();
  7677. this.checkParams(node, false, allowExpression);
  7678. } else {
  7679. const nonSimple = !this.isSimpleParamList(node.params);
  7680. if (!oldStrict || nonSimple) {
  7681. useStrict = this.strictDirective(this.state.end);
  7682. if (useStrict && nonSimple) {
  7683. const errorPos = (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.end : node.start;
  7684. this.raise(errorPos, "Illegal 'use strict' directive in function with non-simple parameter list");
  7685. }
  7686. }
  7687. const oldLabels = this.state.labels;
  7688. this.state.labels = [];
  7689. if (useStrict) this.state.strict = true;
  7690. this.checkParams(node, !oldStrict && !useStrict && !allowExpression && !isMethod && !nonSimple, allowExpression);
  7691. node.body = this.parseBlock(true, false);
  7692. this.state.labels = oldLabels;
  7693. }
  7694. this.state.inParameters = oldInParameters;
  7695. if (this.state.strict && node.id) {
  7696. this.checkLVal(node.id, BIND_OUTSIDE, undefined, "function name");
  7697. }
  7698. this.state.strict = oldStrict;
  7699. }
  7700. isSimpleParamList(params) {
  7701. for (let i = 0, len = params.length; i < len; i++) {
  7702. if (params[i].type !== "Identifier") return false;
  7703. }
  7704. return true;
  7705. }
  7706. checkParams(node, allowDuplicates, isArrowFunction) {
  7707. const nameHash = Object.create(null);
  7708. for (let i = 0; i < node.params.length; i++) {
  7709. this.checkLVal(node.params[i], BIND_VAR, allowDuplicates ? null : nameHash, "function paramter list");
  7710. }
  7711. }
  7712. parseExprList(close, allowEmpty, refShorthandDefaultPos) {
  7713. const elts = [];
  7714. let first = true;
  7715. while (!this.eat(close)) {
  7716. if (first) {
  7717. first = false;
  7718. } else {
  7719. this.expect(types.comma);
  7720. if (this.eat(close)) break;
  7721. }
  7722. elts.push(this.parseExprListItem(allowEmpty, refShorthandDefaultPos));
  7723. }
  7724. return elts;
  7725. }
  7726. parseExprListItem(allowEmpty, refShorthandDefaultPos, refNeedsArrowPos, allowPlaceholder) {
  7727. let elt;
  7728. if (allowEmpty && this.match(types.comma)) {
  7729. elt = null;
  7730. } else if (this.match(types.ellipsis)) {
  7731. const spreadNodeStartPos = this.state.start;
  7732. const spreadNodeStartLoc = this.state.startLoc;
  7733. elt = this.parseParenItem(this.parseSpread(refShorthandDefaultPos, refNeedsArrowPos), spreadNodeStartPos, spreadNodeStartLoc);
  7734. } else if (this.match(types.question)) {
  7735. this.expectPlugin("partialApplication");
  7736. if (!allowPlaceholder) {
  7737. this.raise(this.state.start, "Unexpected argument placeholder");
  7738. }
  7739. const node = this.startNode();
  7740. this.next();
  7741. elt = this.finishNode(node, "ArgumentPlaceholder");
  7742. } else {
  7743. elt = this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos);
  7744. }
  7745. return elt;
  7746. }
  7747. parseIdentifier(liberal) {
  7748. const node = this.startNode();
  7749. const name = this.parseIdentifierName(node.start, liberal);
  7750. return this.createIdentifier(node, name);
  7751. }
  7752. createIdentifier(node, name) {
  7753. node.name = name;
  7754. node.loc.identifierName = name;
  7755. return this.finishNode(node, "Identifier");
  7756. }
  7757. parseIdentifierName(pos, liberal) {
  7758. let name;
  7759. if (this.match(types.name)) {
  7760. name = this.state.value;
  7761. } else if (this.state.type.keyword) {
  7762. name = this.state.type.keyword;
  7763. if ((name === "class" || name === "function") && (this.state.lastTokEnd !== this.state.lastTokStart + 1 || this.input.charCodeAt(this.state.lastTokStart) !== 46)) {
  7764. this.state.context.pop();
  7765. }
  7766. } else {
  7767. throw this.unexpected();
  7768. }
  7769. if (!liberal) {
  7770. this.checkReservedWord(name, this.state.start, !!this.state.type.keyword, false);
  7771. }
  7772. this.next();
  7773. return name;
  7774. }
  7775. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  7776. if (this.scope.inGenerator && word === "yield") {
  7777. this.raise(startLoc, "Can not use 'yield' as identifier inside a generator");
  7778. }
  7779. if (this.scope.inAsync && word === "await") {
  7780. this.raise(startLoc, "Can not use 'await' as identifier inside an async function");
  7781. }
  7782. if (this.state.inClassProperty && word === "arguments") {
  7783. this.raise(startLoc, "'arguments' is not allowed in class field initializer");
  7784. }
  7785. if (checkKeywords && isKeyword(word)) {
  7786. this.raise(startLoc, `Unexpected keyword '${word}'`);
  7787. }
  7788. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  7789. if (reservedTest(word, this.inModule)) {
  7790. if (!this.scope.inAsync && word === "await") {
  7791. this.raise(startLoc, "Can not use keyword 'await' outside an async function");
  7792. }
  7793. this.raise(startLoc, `Unexpected reserved word '${word}'`);
  7794. }
  7795. }
  7796. parseAwait() {
  7797. if (!this.state.awaitPos) {
  7798. this.state.awaitPos = this.state.start;
  7799. }
  7800. const node = this.startNode();
  7801. this.next();
  7802. if (this.state.inParameters) {
  7803. this.raise(node.start, "await is not allowed in async function parameters");
  7804. }
  7805. if (this.match(types.star)) {
  7806. this.raise(node.start, "await* has been removed from the async functions proposal. Use Promise.all() instead.");
  7807. }
  7808. if (!this.state.soloAwait) {
  7809. node.argument = this.parseMaybeUnary();
  7810. }
  7811. return this.finishNode(node, "AwaitExpression");
  7812. }
  7813. parseYield(noIn) {
  7814. if (!this.state.yieldPos) {
  7815. this.state.yieldPos = this.state.start;
  7816. }
  7817. const node = this.startNode();
  7818. if (this.state.inParameters) {
  7819. this.raise(node.start, "yield is not allowed in generator parameters");
  7820. }
  7821. this.next();
  7822. if (this.match(types.semi) || !this.match(types.star) && !this.state.type.startsExpr || this.canInsertSemicolon()) {
  7823. node.delegate = false;
  7824. node.argument = null;
  7825. } else {
  7826. node.delegate = this.eat(types.star);
  7827. node.argument = this.parseMaybeAssign(noIn);
  7828. }
  7829. return this.finishNode(node, "YieldExpression");
  7830. }
  7831. checkPipelineAtInfixOperator(left, leftStartPos) {
  7832. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  7833. if (left.type === "SequenceExpression") {
  7834. throw this.raise(leftStartPos, `Pipeline head should not be a comma-separated sequence expression`);
  7835. }
  7836. }
  7837. }
  7838. parseSmartPipelineBody(childExpression, startPos, startLoc) {
  7839. const pipelineStyle = this.checkSmartPipelineBodyStyle(childExpression);
  7840. this.checkSmartPipelineBodyEarlyErrors(childExpression, pipelineStyle, startPos);
  7841. return this.parseSmartPipelineBodyInStyle(childExpression, pipelineStyle, startPos, startLoc);
  7842. }
  7843. checkSmartPipelineBodyEarlyErrors(childExpression, pipelineStyle, startPos) {
  7844. if (this.match(types.arrow)) {
  7845. throw this.raise(this.state.start, `Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized`);
  7846. } else if (pipelineStyle === "PipelineTopicExpression" && childExpression.type === "SequenceExpression") {
  7847. throw this.raise(startPos, `Pipeline body may not be a comma-separated sequence expression`);
  7848. }
  7849. }
  7850. parseSmartPipelineBodyInStyle(childExpression, pipelineStyle, startPos, startLoc) {
  7851. const bodyNode = this.startNodeAt(startPos, startLoc);
  7852. switch (pipelineStyle) {
  7853. case "PipelineBareFunction":
  7854. bodyNode.callee = childExpression;
  7855. break;
  7856. case "PipelineBareConstructor":
  7857. bodyNode.callee = childExpression.callee;
  7858. break;
  7859. case "PipelineBareAwaitedFunction":
  7860. bodyNode.callee = childExpression.argument;
  7861. break;
  7862. case "PipelineTopicExpression":
  7863. if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
  7864. throw this.raise(startPos, `Pipeline is in topic style but does not use topic reference`);
  7865. }
  7866. bodyNode.expression = childExpression;
  7867. break;
  7868. default:
  7869. throw this.raise(startPos, `Unknown pipeline style ${pipelineStyle}`);
  7870. }
  7871. return this.finishNode(bodyNode, pipelineStyle);
  7872. }
  7873. checkSmartPipelineBodyStyle(expression) {
  7874. switch (expression.type) {
  7875. default:
  7876. return this.isSimpleReference(expression) ? "PipelineBareFunction" : "PipelineTopicExpression";
  7877. }
  7878. }
  7879. isSimpleReference(expression) {
  7880. switch (expression.type) {
  7881. case "MemberExpression":
  7882. return !expression.computed && this.isSimpleReference(expression.object);
  7883. case "Identifier":
  7884. return true;
  7885. default:
  7886. return false;
  7887. }
  7888. }
  7889. withTopicPermittingContext(callback) {
  7890. const outerContextTopicState = this.state.topicContext;
  7891. this.state.topicContext = {
  7892. maxNumOfResolvableTopics: 1,
  7893. maxTopicIndex: null
  7894. };
  7895. try {
  7896. return callback();
  7897. } finally {
  7898. this.state.topicContext = outerContextTopicState;
  7899. }
  7900. }
  7901. withTopicForbiddingContext(callback) {
  7902. const outerContextTopicState = this.state.topicContext;
  7903. this.state.topicContext = {
  7904. maxNumOfResolvableTopics: 0,
  7905. maxTopicIndex: null
  7906. };
  7907. try {
  7908. return callback();
  7909. } finally {
  7910. this.state.topicContext = outerContextTopicState;
  7911. }
  7912. }
  7913. withSoloAwaitPermittingContext(callback) {
  7914. const outerContextSoloAwaitState = this.state.soloAwait;
  7915. this.state.soloAwait = true;
  7916. try {
  7917. return callback();
  7918. } finally {
  7919. this.state.soloAwait = outerContextSoloAwaitState;
  7920. }
  7921. }
  7922. registerTopicReference() {
  7923. this.state.topicContext.maxTopicIndex = 0;
  7924. }
  7925. primaryTopicReferenceIsAllowedInCurrentTopicContext() {
  7926. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  7927. }
  7928. topicReferenceWasUsedInCurrentTopicContext() {
  7929. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  7930. }
  7931. parseFSharpPipelineBody(prec, noIn) {
  7932. const startPos = this.state.start;
  7933. const startLoc = this.state.startLoc;
  7934. this.state.potentialArrowAt = this.state.start;
  7935. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  7936. this.state.inFSharpPipelineDirectBody = true;
  7937. const ret = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, prec, noIn);
  7938. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  7939. return ret;
  7940. }
  7941. }
  7942. const loopLabel = {
  7943. kind: "loop"
  7944. },
  7945. switchLabel = {
  7946. kind: "switch"
  7947. };
  7948. const FUNC_NO_FLAGS = 0b000,
  7949. FUNC_STATEMENT = 0b001,
  7950. FUNC_HANGING_STATEMENT = 0b010,
  7951. FUNC_NULLABLE_ID = 0b100;
  7952. class StatementParser extends ExpressionParser {
  7953. parseTopLevel(file, program) {
  7954. program.sourceType = this.options.sourceType;
  7955. program.interpreter = this.parseInterpreterDirective();
  7956. this.parseBlockBody(program, true, true, types.eof);
  7957. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  7958. for (let _i = 0, _Array$from = Array.from(this.scope.undefinedExports); _i < _Array$from.length; _i++) {
  7959. const [name] = _Array$from[_i];
  7960. const pos = this.scope.undefinedExports.get(name);
  7961. this.raise(pos, `Export '${name}' is not defined`);
  7962. }
  7963. }
  7964. file.program = this.finishNode(program, "Program");
  7965. file.comments = this.state.comments;
  7966. if (this.options.tokens) file.tokens = this.state.tokens;
  7967. return this.finishNode(file, "File");
  7968. }
  7969. stmtToDirective(stmt) {
  7970. const expr = stmt.expression;
  7971. const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start);
  7972. const directive = this.startNodeAt(stmt.start, stmt.loc.start);
  7973. const raw = this.input.slice(expr.start, expr.end);
  7974. const val = directiveLiteral.value = raw.slice(1, -1);
  7975. this.addExtra(directiveLiteral, "raw", raw);
  7976. this.addExtra(directiveLiteral, "rawValue", val);
  7977. directive.value = this.finishNodeAt(directiveLiteral, "DirectiveLiteral", expr.end, expr.loc.end);
  7978. return this.finishNodeAt(directive, "Directive", stmt.end, stmt.loc.end);
  7979. }
  7980. parseInterpreterDirective() {
  7981. if (!this.match(types.interpreterDirective)) {
  7982. return null;
  7983. }
  7984. const node = this.startNode();
  7985. node.value = this.state.value;
  7986. this.next();
  7987. return this.finishNode(node, "InterpreterDirective");
  7988. }
  7989. isLet(context) {
  7990. if (!this.isContextual("let")) {
  7991. return false;
  7992. }
  7993. skipWhiteSpace.lastIndex = this.state.pos;
  7994. const skip = skipWhiteSpace.exec(this.input);
  7995. const next = this.state.pos + skip[0].length;
  7996. const nextCh = this.input.charCodeAt(next);
  7997. if (nextCh === 91) return true;
  7998. if (context) return false;
  7999. if (nextCh === 123) return true;
  8000. if (isIdentifierStart(nextCh)) {
  8001. let pos = next + 1;
  8002. while (isIdentifierChar(this.input.charCodeAt(pos))) {
  8003. ++pos;
  8004. }
  8005. const ident = this.input.slice(next, pos);
  8006. if (!keywordRelationalOperator.test(ident)) return true;
  8007. }
  8008. return false;
  8009. }
  8010. parseStatement(context, topLevel) {
  8011. if (this.match(types.at)) {
  8012. this.parseDecorators(true);
  8013. }
  8014. return this.parseStatementContent(context, topLevel);
  8015. }
  8016. parseStatementContent(context, topLevel) {
  8017. let starttype = this.state.type;
  8018. const node = this.startNode();
  8019. let kind;
  8020. if (this.isLet(context)) {
  8021. starttype = types._var;
  8022. kind = "let";
  8023. }
  8024. switch (starttype) {
  8025. case types._break:
  8026. case types._continue:
  8027. return this.parseBreakContinueStatement(node, starttype.keyword);
  8028. case types._debugger:
  8029. return this.parseDebuggerStatement(node);
  8030. case types._do:
  8031. return this.parseDoStatement(node);
  8032. case types._for:
  8033. return this.parseForStatement(node);
  8034. case types._function:
  8035. if (this.lookahead().type === types.dot) break;
  8036. if (context) {
  8037. if (this.state.strict) {
  8038. this.raise(this.state.start, "In strict mode code, functions can only be declared at top level or inside a block");
  8039. } else if (context !== "if" && context !== "label") {
  8040. this.raise(this.state.start, "In non-strict mode code, functions can only be declared at top level, " + "inside a block, or as the body of an if statement");
  8041. }
  8042. }
  8043. return this.parseFunctionStatement(node, false, !context);
  8044. case types._class:
  8045. if (context) this.unexpected();
  8046. return this.parseClass(node, true);
  8047. case types._if:
  8048. return this.parseIfStatement(node);
  8049. case types._return:
  8050. return this.parseReturnStatement(node);
  8051. case types._switch:
  8052. return this.parseSwitchStatement(node);
  8053. case types._throw:
  8054. return this.parseThrowStatement(node);
  8055. case types._try:
  8056. return this.parseTryStatement(node);
  8057. case types._const:
  8058. case types._var:
  8059. kind = kind || this.state.value;
  8060. if (context && kind !== "var") {
  8061. this.unexpected(this.state.start, "Lexical declaration cannot appear in a single-statement context");
  8062. }
  8063. return this.parseVarStatement(node, kind);
  8064. case types._while:
  8065. return this.parseWhileStatement(node);
  8066. case types._with:
  8067. return this.parseWithStatement(node);
  8068. case types.braceL:
  8069. return this.parseBlock();
  8070. case types.semi:
  8071. return this.parseEmptyStatement(node);
  8072. case types._export:
  8073. case types._import:
  8074. {
  8075. const nextToken = this.lookahead();
  8076. if (nextToken.type === types.parenL || nextToken.type === types.dot) {
  8077. break;
  8078. }
  8079. if (!this.options.allowImportExportEverywhere && !topLevel) {
  8080. this.raise(this.state.start, "'import' and 'export' may only appear at the top level");
  8081. }
  8082. this.next();
  8083. let result;
  8084. if (starttype === types._import) {
  8085. result = this.parseImport(node);
  8086. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  8087. this.sawUnambiguousESM = true;
  8088. }
  8089. } else {
  8090. result = this.parseExport(node);
  8091. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  8092. this.sawUnambiguousESM = true;
  8093. }
  8094. }
  8095. this.assertModuleNodeAllowed(node);
  8096. return result;
  8097. }
  8098. default:
  8099. {
  8100. if (this.isAsyncFunction()) {
  8101. if (context) {
  8102. this.unexpected(null, "Async functions can only be declared at the top level or inside a block");
  8103. }
  8104. this.next();
  8105. return this.parseFunctionStatement(node, true, !context);
  8106. }
  8107. }
  8108. }
  8109. const maybeName = this.state.value;
  8110. const expr = this.parseExpression();
  8111. if (starttype === types.name && expr.type === "Identifier" && this.eat(types.colon)) {
  8112. return this.parseLabeledStatement(node, maybeName, expr, context);
  8113. } else {
  8114. return this.parseExpressionStatement(node, expr);
  8115. }
  8116. }
  8117. assertModuleNodeAllowed(node) {
  8118. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  8119. this.raise(node.start, `'import' and 'export' may appear only with 'sourceType: "module"'`, {
  8120. code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  8121. });
  8122. }
  8123. }
  8124. takeDecorators(node) {
  8125. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  8126. if (decorators.length) {
  8127. node.decorators = decorators;
  8128. this.resetStartLocationFromNode(node, decorators[0]);
  8129. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  8130. }
  8131. }
  8132. canHaveLeadingDecorator() {
  8133. return this.match(types._class);
  8134. }
  8135. parseDecorators(allowExport) {
  8136. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  8137. while (this.match(types.at)) {
  8138. const decorator = this.parseDecorator();
  8139. currentContextDecorators.push(decorator);
  8140. }
  8141. if (this.match(types._export)) {
  8142. if (!allowExport) {
  8143. this.unexpected();
  8144. }
  8145. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  8146. this.raise(this.state.start, "Using the export keyword between a decorator and a class is not allowed. " + "Please use `export @dec class` instead.");
  8147. }
  8148. } else if (!this.canHaveLeadingDecorator()) {
  8149. this.raise(this.state.start, "Leading decorators must be attached to a class declaration");
  8150. }
  8151. }
  8152. parseDecorator() {
  8153. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  8154. const node = this.startNode();
  8155. this.next();
  8156. if (this.hasPlugin("decorators")) {
  8157. this.state.decoratorStack.push([]);
  8158. const startPos = this.state.start;
  8159. const startLoc = this.state.startLoc;
  8160. let expr;
  8161. if (this.eat(types.parenL)) {
  8162. expr = this.parseExpression();
  8163. this.expect(types.parenR);
  8164. } else {
  8165. expr = this.parseIdentifier(false);
  8166. while (this.eat(types.dot)) {
  8167. const node = this.startNodeAt(startPos, startLoc);
  8168. node.object = expr;
  8169. node.property = this.parseIdentifier(true);
  8170. node.computed = false;
  8171. expr = this.finishNode(node, "MemberExpression");
  8172. }
  8173. }
  8174. node.expression = this.parseMaybeDecoratorArguments(expr);
  8175. this.state.decoratorStack.pop();
  8176. } else {
  8177. node.expression = this.parseExprSubscripts();
  8178. }
  8179. return this.finishNode(node, "Decorator");
  8180. }
  8181. parseMaybeDecoratorArguments(expr) {
  8182. if (this.eat(types.parenL)) {
  8183. const node = this.startNodeAtNode(expr);
  8184. node.callee = expr;
  8185. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  8186. this.toReferencedList(node.arguments);
  8187. return this.finishNode(node, "CallExpression");
  8188. }
  8189. return expr;
  8190. }
  8191. parseBreakContinueStatement(node, keyword) {
  8192. const isBreak = keyword === "break";
  8193. this.next();
  8194. if (this.isLineTerminator()) {
  8195. node.label = null;
  8196. } else {
  8197. node.label = this.parseIdentifier();
  8198. this.semicolon();
  8199. }
  8200. this.verifyBreakContinue(node, keyword);
  8201. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  8202. }
  8203. verifyBreakContinue(node, keyword) {
  8204. const isBreak = keyword === "break";
  8205. let i;
  8206. for (i = 0; i < this.state.labels.length; ++i) {
  8207. const lab = this.state.labels[i];
  8208. if (node.label == null || lab.name === node.label.name) {
  8209. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  8210. if (node.label && isBreak) break;
  8211. }
  8212. }
  8213. if (i === this.state.labels.length) {
  8214. this.raise(node.start, "Unsyntactic " + keyword);
  8215. }
  8216. }
  8217. parseDebuggerStatement(node) {
  8218. this.next();
  8219. this.semicolon();
  8220. return this.finishNode(node, "DebuggerStatement");
  8221. }
  8222. parseHeaderExpression() {
  8223. this.expect(types.parenL);
  8224. const val = this.parseExpression();
  8225. this.expect(types.parenR);
  8226. return val;
  8227. }
  8228. parseDoStatement(node) {
  8229. this.next();
  8230. this.state.labels.push(loopLabel);
  8231. node.body = this.withTopicForbiddingContext(() => this.parseStatement("do"));
  8232. this.state.labels.pop();
  8233. this.expect(types._while);
  8234. node.test = this.parseHeaderExpression();
  8235. this.eat(types.semi);
  8236. return this.finishNode(node, "DoWhileStatement");
  8237. }
  8238. parseForStatement(node) {
  8239. this.next();
  8240. this.state.labels.push(loopLabel);
  8241. let awaitAt = -1;
  8242. if ((this.scope.inAsync || !this.scope.inFunction && this.options.allowAwaitOutsideFunction) && this.eatContextual("await")) {
  8243. awaitAt = this.state.lastTokStart;
  8244. }
  8245. this.scope.enter(SCOPE_OTHER);
  8246. this.expect(types.parenL);
  8247. if (this.match(types.semi)) {
  8248. if (awaitAt > -1) {
  8249. this.unexpected(awaitAt);
  8250. }
  8251. return this.parseFor(node, null);
  8252. }
  8253. const isLet = this.isLet();
  8254. if (this.match(types._var) || this.match(types._const) || isLet) {
  8255. const init = this.startNode();
  8256. const kind = isLet ? "let" : this.state.value;
  8257. this.next();
  8258. this.parseVar(init, true, kind);
  8259. this.finishNode(init, "VariableDeclaration");
  8260. if ((this.match(types._in) || this.isContextual("of")) && init.declarations.length === 1) {
  8261. return this.parseForIn(node, init, awaitAt);
  8262. }
  8263. if (awaitAt > -1) {
  8264. this.unexpected(awaitAt);
  8265. }
  8266. return this.parseFor(node, init);
  8267. }
  8268. const refShorthandDefaultPos = {
  8269. start: 0
  8270. };
  8271. const init = this.parseExpression(true, refShorthandDefaultPos);
  8272. if (this.match(types._in) || this.isContextual("of")) {
  8273. const description = this.isContextual("of") ? "for-of statement" : "for-in statement";
  8274. this.toAssignable(init, undefined, description);
  8275. this.checkLVal(init, undefined, undefined, description);
  8276. return this.parseForIn(node, init, awaitAt);
  8277. } else if (refShorthandDefaultPos.start) {
  8278. this.unexpected(refShorthandDefaultPos.start);
  8279. }
  8280. if (awaitAt > -1) {
  8281. this.unexpected(awaitAt);
  8282. }
  8283. return this.parseFor(node, init);
  8284. }
  8285. parseFunctionStatement(node, isAsync, declarationPosition) {
  8286. this.next();
  8287. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  8288. }
  8289. parseIfStatement(node) {
  8290. this.next();
  8291. node.test = this.parseHeaderExpression();
  8292. node.consequent = this.parseStatement("if");
  8293. node.alternate = this.eat(types._else) ? this.parseStatement("if") : null;
  8294. return this.finishNode(node, "IfStatement");
  8295. }
  8296. parseReturnStatement(node) {
  8297. if (!this.scope.inFunction && !this.options.allowReturnOutsideFunction) {
  8298. this.raise(this.state.start, "'return' outside of function");
  8299. }
  8300. this.next();
  8301. if (this.isLineTerminator()) {
  8302. node.argument = null;
  8303. } else {
  8304. node.argument = this.parseExpression();
  8305. this.semicolon();
  8306. }
  8307. return this.finishNode(node, "ReturnStatement");
  8308. }
  8309. parseSwitchStatement(node) {
  8310. this.next();
  8311. node.discriminant = this.parseHeaderExpression();
  8312. const cases = node.cases = [];
  8313. this.expect(types.braceL);
  8314. this.state.labels.push(switchLabel);
  8315. this.scope.enter(SCOPE_OTHER);
  8316. let cur;
  8317. for (let sawDefault; !this.match(types.braceR);) {
  8318. if (this.match(types._case) || this.match(types._default)) {
  8319. const isCase = this.match(types._case);
  8320. if (cur) this.finishNode(cur, "SwitchCase");
  8321. cases.push(cur = this.startNode());
  8322. cur.consequent = [];
  8323. this.next();
  8324. if (isCase) {
  8325. cur.test = this.parseExpression();
  8326. } else {
  8327. if (sawDefault) {
  8328. this.raise(this.state.lastTokStart, "Multiple default clauses");
  8329. }
  8330. sawDefault = true;
  8331. cur.test = null;
  8332. }
  8333. this.expect(types.colon);
  8334. } else {
  8335. if (cur) {
  8336. cur.consequent.push(this.parseStatement(null));
  8337. } else {
  8338. this.unexpected();
  8339. }
  8340. }
  8341. }
  8342. this.scope.exit();
  8343. if (cur) this.finishNode(cur, "SwitchCase");
  8344. this.next();
  8345. this.state.labels.pop();
  8346. return this.finishNode(node, "SwitchStatement");
  8347. }
  8348. parseThrowStatement(node) {
  8349. this.next();
  8350. if (lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start))) {
  8351. this.raise(this.state.lastTokEnd, "Illegal newline after throw");
  8352. }
  8353. node.argument = this.parseExpression();
  8354. this.semicolon();
  8355. return this.finishNode(node, "ThrowStatement");
  8356. }
  8357. parseTryStatement(node) {
  8358. this.next();
  8359. node.block = this.parseBlock();
  8360. node.handler = null;
  8361. if (this.match(types._catch)) {
  8362. const clause = this.startNode();
  8363. this.next();
  8364. if (this.match(types.parenL)) {
  8365. this.expect(types.parenL);
  8366. clause.param = this.parseBindingAtom();
  8367. const simple = clause.param.type === "Identifier";
  8368. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  8369. this.checkLVal(clause.param, BIND_LEXICAL, null, "catch clause");
  8370. this.expect(types.parenR);
  8371. } else {
  8372. clause.param = null;
  8373. this.scope.enter(SCOPE_OTHER);
  8374. }
  8375. clause.body = this.withTopicForbiddingContext(() => this.parseBlock(false, false));
  8376. this.scope.exit();
  8377. node.handler = this.finishNode(clause, "CatchClause");
  8378. }
  8379. node.finalizer = this.eat(types._finally) ? this.parseBlock() : null;
  8380. if (!node.handler && !node.finalizer) {
  8381. this.raise(node.start, "Missing catch or finally clause");
  8382. }
  8383. return this.finishNode(node, "TryStatement");
  8384. }
  8385. parseVarStatement(node, kind) {
  8386. this.next();
  8387. this.parseVar(node, false, kind);
  8388. this.semicolon();
  8389. return this.finishNode(node, "VariableDeclaration");
  8390. }
  8391. parseWhileStatement(node) {
  8392. this.next();
  8393. node.test = this.parseHeaderExpression();
  8394. this.state.labels.push(loopLabel);
  8395. node.body = this.withTopicForbiddingContext(() => this.parseStatement("while"));
  8396. this.state.labels.pop();
  8397. return this.finishNode(node, "WhileStatement");
  8398. }
  8399. parseWithStatement(node) {
  8400. if (this.state.strict) {
  8401. this.raise(this.state.start, "'with' in strict mode");
  8402. }
  8403. this.next();
  8404. node.object = this.parseHeaderExpression();
  8405. node.body = this.withTopicForbiddingContext(() => this.parseStatement("with"));
  8406. return this.finishNode(node, "WithStatement");
  8407. }
  8408. parseEmptyStatement(node) {
  8409. this.next();
  8410. return this.finishNode(node, "EmptyStatement");
  8411. }
  8412. parseLabeledStatement(node, maybeName, expr, context) {
  8413. for (let _i2 = 0, _this$state$labels = this.state.labels; _i2 < _this$state$labels.length; _i2++) {
  8414. const label = _this$state$labels[_i2];
  8415. if (label.name === maybeName) {
  8416. this.raise(expr.start, `Label '${maybeName}' is already declared`);
  8417. }
  8418. }
  8419. const kind = this.state.type.isLoop ? "loop" : this.match(types._switch) ? "switch" : null;
  8420. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  8421. const label = this.state.labels[i];
  8422. if (label.statementStart === node.start) {
  8423. label.statementStart = this.state.start;
  8424. label.kind = kind;
  8425. } else {
  8426. break;
  8427. }
  8428. }
  8429. this.state.labels.push({
  8430. name: maybeName,
  8431. kind: kind,
  8432. statementStart: this.state.start
  8433. });
  8434. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  8435. this.state.labels.pop();
  8436. node.label = expr;
  8437. return this.finishNode(node, "LabeledStatement");
  8438. }
  8439. parseExpressionStatement(node, expr) {
  8440. node.expression = expr;
  8441. this.semicolon();
  8442. return this.finishNode(node, "ExpressionStatement");
  8443. }
  8444. parseBlock(allowDirectives = false, createNewLexicalScope = true) {
  8445. const node = this.startNode();
  8446. this.expect(types.braceL);
  8447. if (createNewLexicalScope) {
  8448. this.scope.enter(SCOPE_OTHER);
  8449. }
  8450. this.parseBlockBody(node, allowDirectives, false, types.braceR);
  8451. if (createNewLexicalScope) {
  8452. this.scope.exit();
  8453. }
  8454. return this.finishNode(node, "BlockStatement");
  8455. }
  8456. isValidDirective(stmt) {
  8457. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  8458. }
  8459. parseBlockBody(node, allowDirectives, topLevel, end) {
  8460. const body = node.body = [];
  8461. const directives = node.directives = [];
  8462. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end);
  8463. }
  8464. parseBlockOrModuleBlockBody(body, directives, topLevel, end) {
  8465. let parsedNonDirective = false;
  8466. let oldStrict;
  8467. let octalPosition;
  8468. while (!this.eat(end)) {
  8469. if (!parsedNonDirective && this.state.containsOctal && !octalPosition) {
  8470. octalPosition = this.state.octalPosition;
  8471. }
  8472. const stmt = this.parseStatement(null, topLevel);
  8473. if (directives && !parsedNonDirective && this.isValidDirective(stmt)) {
  8474. const directive = this.stmtToDirective(stmt);
  8475. directives.push(directive);
  8476. if (oldStrict === undefined && directive.value.value === "use strict") {
  8477. oldStrict = this.state.strict;
  8478. this.setStrict(true);
  8479. if (octalPosition) {
  8480. this.raise(octalPosition, "Octal literal in strict mode");
  8481. }
  8482. }
  8483. continue;
  8484. }
  8485. parsedNonDirective = true;
  8486. body.push(stmt);
  8487. }
  8488. if (oldStrict === false) {
  8489. this.setStrict(false);
  8490. }
  8491. }
  8492. parseFor(node, init) {
  8493. node.init = init;
  8494. this.expect(types.semi);
  8495. node.test = this.match(types.semi) ? null : this.parseExpression();
  8496. this.expect(types.semi);
  8497. node.update = this.match(types.parenR) ? null : this.parseExpression();
  8498. this.expect(types.parenR);
  8499. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  8500. this.scope.exit();
  8501. this.state.labels.pop();
  8502. return this.finishNode(node, "ForStatement");
  8503. }
  8504. parseForIn(node, init, awaitAt) {
  8505. const isForIn = this.match(types._in);
  8506. this.next();
  8507. if (isForIn) {
  8508. if (awaitAt > -1) this.unexpected(awaitAt);
  8509. } else {
  8510. node.await = awaitAt > -1;
  8511. }
  8512. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  8513. this.raise(init.start, `${isForIn ? "for-in" : "for-of"} loop variable declaration may not have an initializer`);
  8514. } else if (init.type === "AssignmentPattern") {
  8515. this.raise(init.start, "Invalid left-hand side in for-loop");
  8516. }
  8517. node.left = init;
  8518. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssign();
  8519. this.expect(types.parenR);
  8520. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  8521. this.scope.exit();
  8522. this.state.labels.pop();
  8523. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  8524. }
  8525. parseVar(node, isFor, kind) {
  8526. const declarations = node.declarations = [];
  8527. const isTypescript = this.hasPlugin("typescript");
  8528. node.kind = kind;
  8529. for (;;) {
  8530. const decl = this.startNode();
  8531. this.parseVarId(decl, kind);
  8532. if (this.eat(types.eq)) {
  8533. decl.init = this.parseMaybeAssign(isFor);
  8534. } else {
  8535. if (kind === "const" && !(this.match(types._in) || this.isContextual("of"))) {
  8536. if (!isTypescript) {
  8537. this.unexpected();
  8538. }
  8539. } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(types._in) || this.isContextual("of")))) {
  8540. this.raise(this.state.lastTokEnd, "Complex binding patterns require an initialization value");
  8541. }
  8542. decl.init = null;
  8543. }
  8544. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  8545. if (!this.eat(types.comma)) break;
  8546. }
  8547. return node;
  8548. }
  8549. parseVarId(decl, kind) {
  8550. decl.id = this.parseBindingAtom();
  8551. this.checkLVal(decl.id, kind === "var" ? BIND_VAR : BIND_LEXICAL, undefined, "variable declaration");
  8552. }
  8553. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  8554. const isStatement = statement & FUNC_STATEMENT;
  8555. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  8556. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  8557. this.initFunction(node, isAsync);
  8558. if (this.match(types.star) && isHangingStatement) {
  8559. this.unexpected(this.state.start, "Generators can only be declared at the top level or inside a block");
  8560. }
  8561. node.generator = this.eat(types.star);
  8562. if (isStatement) {
  8563. node.id = this.parseFunctionId(requireId);
  8564. }
  8565. const oldInClassProperty = this.state.inClassProperty;
  8566. const oldYieldPos = this.state.yieldPos;
  8567. const oldAwaitPos = this.state.awaitPos;
  8568. this.state.inClassProperty = false;
  8569. this.state.yieldPos = 0;
  8570. this.state.awaitPos = 0;
  8571. this.scope.enter(functionFlags(node.async, node.generator));
  8572. if (!isStatement) {
  8573. node.id = this.parseFunctionId();
  8574. }
  8575. this.parseFunctionParams(node);
  8576. this.withTopicForbiddingContext(() => {
  8577. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  8578. });
  8579. this.scope.exit();
  8580. if (isStatement && !isHangingStatement) {
  8581. this.checkFunctionStatementId(node);
  8582. }
  8583. this.state.inClassProperty = oldInClassProperty;
  8584. this.state.yieldPos = oldYieldPos;
  8585. this.state.awaitPos = oldAwaitPos;
  8586. return node;
  8587. }
  8588. parseFunctionId(requireId) {
  8589. return requireId || this.match(types.name) ? this.parseIdentifier() : null;
  8590. }
  8591. parseFunctionParams(node, allowModifiers) {
  8592. const oldInParameters = this.state.inParameters;
  8593. this.state.inParameters = true;
  8594. this.expect(types.parenL);
  8595. node.params = this.parseBindingList(types.parenR, false, allowModifiers);
  8596. this.state.inParameters = oldInParameters;
  8597. this.checkYieldAwaitInDefaultParams();
  8598. }
  8599. checkFunctionStatementId(node) {
  8600. if (!node.id) return;
  8601. this.checkLVal(node.id, this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, null, "function name");
  8602. }
  8603. parseClass(node, isStatement, optionalId) {
  8604. this.next();
  8605. this.takeDecorators(node);
  8606. const oldStrict = this.state.strict;
  8607. this.state.strict = true;
  8608. this.parseClassId(node, isStatement, optionalId);
  8609. this.parseClassSuper(node);
  8610. node.body = this.parseClassBody(!!node.superClass);
  8611. this.state.strict = oldStrict;
  8612. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  8613. }
  8614. isClassProperty() {
  8615. return this.match(types.eq) || this.match(types.semi) || this.match(types.braceR);
  8616. }
  8617. isClassMethod() {
  8618. return this.match(types.parenL);
  8619. }
  8620. isNonstaticConstructor(method) {
  8621. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  8622. }
  8623. parseClassBody(constructorAllowsSuper) {
  8624. this.state.classLevel++;
  8625. const state = {
  8626. hadConstructor: false
  8627. };
  8628. let decorators = [];
  8629. const classBody = this.startNode();
  8630. classBody.body = [];
  8631. this.expect(types.braceL);
  8632. this.withTopicForbiddingContext(() => {
  8633. while (!this.eat(types.braceR)) {
  8634. if (this.eat(types.semi)) {
  8635. if (decorators.length > 0) {
  8636. this.raise(this.state.lastTokEnd, "Decorators must not be followed by a semicolon");
  8637. }
  8638. continue;
  8639. }
  8640. if (this.match(types.at)) {
  8641. decorators.push(this.parseDecorator());
  8642. continue;
  8643. }
  8644. const member = this.startNode();
  8645. if (decorators.length) {
  8646. member.decorators = decorators;
  8647. this.resetStartLocationFromNode(member, decorators[0]);
  8648. decorators = [];
  8649. }
  8650. this.parseClassMember(classBody, member, state, constructorAllowsSuper);
  8651. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  8652. this.raise(member.start, "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?");
  8653. }
  8654. }
  8655. });
  8656. if (decorators.length) {
  8657. this.raise(this.state.start, "You have trailing decorators with no method");
  8658. }
  8659. this.state.classLevel--;
  8660. return this.finishNode(classBody, "ClassBody");
  8661. }
  8662. parseClassMember(classBody, member, state, constructorAllowsSuper) {
  8663. let isStatic = false;
  8664. const containsEsc = this.state.containsEsc;
  8665. if (this.match(types.name) && this.state.value === "static") {
  8666. const key = this.parseIdentifier(true);
  8667. if (this.isClassMethod()) {
  8668. const method = member;
  8669. method.kind = "method";
  8670. method.computed = false;
  8671. method.key = key;
  8672. method.static = false;
  8673. this.pushClassMethod(classBody, method, false, false, false, false);
  8674. return;
  8675. } else if (this.isClassProperty()) {
  8676. const prop = member;
  8677. prop.computed = false;
  8678. prop.key = key;
  8679. prop.static = false;
  8680. classBody.body.push(this.parseClassProperty(prop));
  8681. return;
  8682. } else if (containsEsc) {
  8683. throw this.unexpected();
  8684. }
  8685. isStatic = true;
  8686. }
  8687. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper);
  8688. }
  8689. parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper) {
  8690. const publicMethod = member;
  8691. const privateMethod = member;
  8692. const publicProp = member;
  8693. const privateProp = member;
  8694. const method = publicMethod;
  8695. const publicMember = publicMethod;
  8696. member.static = isStatic;
  8697. if (this.eat(types.star)) {
  8698. method.kind = "method";
  8699. this.parseClassPropertyName(method);
  8700. if (method.key.type === "PrivateName") {
  8701. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  8702. return;
  8703. }
  8704. if (this.isNonstaticConstructor(publicMethod)) {
  8705. this.raise(publicMethod.key.start, "Constructor can't be a generator");
  8706. }
  8707. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  8708. return;
  8709. }
  8710. const containsEsc = this.state.containsEsc;
  8711. const key = this.parseClassPropertyName(member);
  8712. const isPrivate = key.type === "PrivateName";
  8713. const isSimple = key.type === "Identifier";
  8714. this.parsePostMemberNameModifiers(publicMember);
  8715. if (this.isClassMethod()) {
  8716. method.kind = "method";
  8717. if (isPrivate) {
  8718. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  8719. return;
  8720. }
  8721. const isConstructor = this.isNonstaticConstructor(publicMethod);
  8722. let allowsDirectSuper = false;
  8723. if (isConstructor) {
  8724. publicMethod.kind = "constructor";
  8725. if (publicMethod.decorators) {
  8726. this.raise(publicMethod.start, "You can't attach decorators to a class constructor");
  8727. }
  8728. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  8729. this.raise(key.start, "Duplicate constructor in the same class");
  8730. }
  8731. state.hadConstructor = true;
  8732. allowsDirectSuper = constructorAllowsSuper;
  8733. }
  8734. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  8735. } else if (this.isClassProperty()) {
  8736. if (isPrivate) {
  8737. this.pushClassPrivateProperty(classBody, privateProp);
  8738. } else {
  8739. this.pushClassProperty(classBody, publicProp);
  8740. }
  8741. } else if (isSimple && key.name === "async" && !containsEsc && !this.isLineTerminator()) {
  8742. const isGenerator = this.eat(types.star);
  8743. method.kind = "method";
  8744. this.parseClassPropertyName(method);
  8745. if (method.key.type === "PrivateName") {
  8746. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  8747. } else {
  8748. if (this.isNonstaticConstructor(publicMethod)) {
  8749. this.raise(publicMethod.key.start, "Constructor can't be an async function");
  8750. }
  8751. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  8752. }
  8753. } else if (isSimple && (key.name === "get" || key.name === "set") && !containsEsc && !(this.match(types.star) && this.isLineTerminator())) {
  8754. method.kind = key.name;
  8755. this.parseClassPropertyName(publicMethod);
  8756. if (method.key.type === "PrivateName") {
  8757. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  8758. } else {
  8759. if (this.isNonstaticConstructor(publicMethod)) {
  8760. this.raise(publicMethod.key.start, "Constructor can't have get/set modifier");
  8761. }
  8762. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  8763. }
  8764. this.checkGetterSetterParams(publicMethod);
  8765. } else if (this.isLineTerminator()) {
  8766. if (isPrivate) {
  8767. this.pushClassPrivateProperty(classBody, privateProp);
  8768. } else {
  8769. this.pushClassProperty(classBody, publicProp);
  8770. }
  8771. } else {
  8772. this.unexpected();
  8773. }
  8774. }
  8775. parseClassPropertyName(member) {
  8776. const key = this.parsePropertyName(member);
  8777. if (!member.computed && member.static && (key.name === "prototype" || key.value === "prototype")) {
  8778. this.raise(key.start, "Classes may not have static property named prototype");
  8779. }
  8780. if (key.type === "PrivateName" && key.id.name === "constructor") {
  8781. this.raise(key.start, "Classes may not have a private field named '#constructor'");
  8782. }
  8783. return key;
  8784. }
  8785. pushClassProperty(classBody, prop) {
  8786. if (this.isNonstaticConstructor(prop)) {
  8787. this.raise(prop.key.start, "Classes may not have a non-static field named 'constructor'");
  8788. }
  8789. classBody.body.push(this.parseClassProperty(prop));
  8790. }
  8791. pushClassPrivateProperty(classBody, prop) {
  8792. this.expectPlugin("classPrivateProperties", prop.key.start);
  8793. classBody.body.push(this.parseClassPrivateProperty(prop));
  8794. }
  8795. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  8796. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  8797. }
  8798. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  8799. this.expectPlugin("classPrivateMethods", method.key.start);
  8800. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true));
  8801. }
  8802. parsePostMemberNameModifiers(methodOrProp) {}
  8803. parseAccessModifier() {
  8804. return undefined;
  8805. }
  8806. parseClassPrivateProperty(node) {
  8807. this.state.inClassProperty = true;
  8808. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  8809. node.value = this.eat(types.eq) ? this.parseMaybeAssign() : null;
  8810. this.semicolon();
  8811. this.state.inClassProperty = false;
  8812. this.scope.exit();
  8813. return this.finishNode(node, "ClassPrivateProperty");
  8814. }
  8815. parseClassProperty(node) {
  8816. if (!node.typeAnnotation) {
  8817. this.expectPlugin("classProperties");
  8818. }
  8819. this.state.inClassProperty = true;
  8820. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  8821. if (this.match(types.eq)) {
  8822. this.expectPlugin("classProperties");
  8823. this.next();
  8824. node.value = this.parseMaybeAssign();
  8825. } else {
  8826. node.value = null;
  8827. }
  8828. this.semicolon();
  8829. this.state.inClassProperty = false;
  8830. this.scope.exit();
  8831. return this.finishNode(node, "ClassProperty");
  8832. }
  8833. parseClassId(node, isStatement, optionalId) {
  8834. if (this.match(types.name)) {
  8835. node.id = this.parseIdentifier();
  8836. if (isStatement) {
  8837. this.checkLVal(node.id, BIND_CLASS, undefined, "class name");
  8838. }
  8839. } else {
  8840. if (optionalId || !isStatement) {
  8841. node.id = null;
  8842. } else {
  8843. this.unexpected(null, "A class name is required");
  8844. }
  8845. }
  8846. }
  8847. parseClassSuper(node) {
  8848. node.superClass = this.eat(types._extends) ? this.parseExprSubscripts() : null;
  8849. }
  8850. parseExport(node) {
  8851. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  8852. const parseAfterDefault = !hasDefault || this.eat(types.comma);
  8853. const hasStar = parseAfterDefault && this.eatExportStar(node);
  8854. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  8855. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(types.comma));
  8856. const isFromRequired = hasDefault || hasStar;
  8857. if (hasStar && !hasNamespace) {
  8858. if (hasDefault) this.unexpected();
  8859. this.parseExportFrom(node, true);
  8860. return this.finishNode(node, "ExportAllDeclaration");
  8861. }
  8862. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  8863. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  8864. throw this.unexpected(null, types.braceL);
  8865. }
  8866. let hasDeclaration;
  8867. if (isFromRequired || hasSpecifiers) {
  8868. hasDeclaration = false;
  8869. this.parseExportFrom(node, isFromRequired);
  8870. } else {
  8871. hasDeclaration = this.maybeParseExportDeclaration(node);
  8872. }
  8873. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  8874. this.checkExport(node, true, false, !!node.source);
  8875. return this.finishNode(node, "ExportNamedDeclaration");
  8876. }
  8877. if (this.eat(types._default)) {
  8878. node.declaration = this.parseExportDefaultExpression();
  8879. this.checkExport(node, true, true);
  8880. return this.finishNode(node, "ExportDefaultDeclaration");
  8881. }
  8882. throw this.unexpected(null, types.braceL);
  8883. }
  8884. eatExportStar(node) {
  8885. return this.eat(types.star);
  8886. }
  8887. maybeParseExportDefaultSpecifier(node) {
  8888. if (this.isExportDefaultSpecifier()) {
  8889. this.expectPlugin("exportDefaultFrom");
  8890. const specifier = this.startNode();
  8891. specifier.exported = this.parseIdentifier(true);
  8892. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  8893. return true;
  8894. }
  8895. return false;
  8896. }
  8897. maybeParseExportNamespaceSpecifier(node) {
  8898. if (this.isContextual("as")) {
  8899. if (!node.specifiers) node.specifiers = [];
  8900. this.expectPlugin("exportNamespaceFrom");
  8901. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  8902. this.next();
  8903. specifier.exported = this.parseIdentifier(true);
  8904. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  8905. return true;
  8906. }
  8907. return false;
  8908. }
  8909. maybeParseExportNamedSpecifiers(node) {
  8910. if (this.match(types.braceL)) {
  8911. if (!node.specifiers) node.specifiers = [];
  8912. node.specifiers.push(...this.parseExportSpecifiers());
  8913. node.source = null;
  8914. node.declaration = null;
  8915. return true;
  8916. }
  8917. return false;
  8918. }
  8919. maybeParseExportDeclaration(node) {
  8920. if (this.shouldParseExportDeclaration()) {
  8921. if (this.isContextual("async")) {
  8922. const next = this.lookahead();
  8923. if (next.type !== types._function) {
  8924. this.unexpected(next.start, `Unexpected token, expected "function"`);
  8925. }
  8926. }
  8927. node.specifiers = [];
  8928. node.source = null;
  8929. node.declaration = this.parseExportDeclaration(node);
  8930. return true;
  8931. }
  8932. return false;
  8933. }
  8934. isAsyncFunction() {
  8935. if (!this.isContextual("async")) return false;
  8936. const {
  8937. pos
  8938. } = this.state;
  8939. skipWhiteSpace.lastIndex = pos;
  8940. const skip = skipWhiteSpace.exec(this.input);
  8941. if (!skip || !skip.length) return false;
  8942. const next = pos + skip[0].length;
  8943. return !lineBreak.test(this.input.slice(pos, next)) && this.input.slice(next, next + 8) === "function" && (next + 8 === this.length || !isIdentifierChar(this.input.charCodeAt(next + 8)));
  8944. }
  8945. parseExportDefaultExpression() {
  8946. const expr = this.startNode();
  8947. const isAsync = this.isAsyncFunction();
  8948. if (this.match(types._function) || isAsync) {
  8949. this.next();
  8950. if (isAsync) {
  8951. this.next();
  8952. }
  8953. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  8954. } else if (this.match(types._class)) {
  8955. return this.parseClass(expr, true, true);
  8956. } else if (this.match(types.at)) {
  8957. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  8958. this.unexpected(this.state.start, "Decorators must be placed *before* the 'export' keyword." + " You can set the 'decoratorsBeforeExport' option to false to use" + " the 'export @decorator class {}' syntax");
  8959. }
  8960. this.parseDecorators(false);
  8961. return this.parseClass(expr, true, true);
  8962. } else if (this.match(types._const) || this.match(types._var) || this.isLet()) {
  8963. return this.raise(this.state.start, "Only expressions, functions or classes are allowed as the `default` export.");
  8964. } else {
  8965. const res = this.parseMaybeAssign();
  8966. this.semicolon();
  8967. return res;
  8968. }
  8969. }
  8970. parseExportDeclaration(node) {
  8971. return this.parseStatement(null);
  8972. }
  8973. isExportDefaultSpecifier() {
  8974. if (this.match(types.name)) {
  8975. return this.state.value !== "async" && this.state.value !== "let";
  8976. }
  8977. if (!this.match(types._default)) {
  8978. return false;
  8979. }
  8980. const lookahead = this.lookahead();
  8981. return lookahead.type === types.comma || lookahead.type === types.name && lookahead.value === "from";
  8982. }
  8983. parseExportFrom(node, expect) {
  8984. if (this.eatContextual("from")) {
  8985. node.source = this.parseImportSource();
  8986. this.checkExport(node);
  8987. } else {
  8988. if (expect) {
  8989. this.unexpected();
  8990. } else {
  8991. node.source = null;
  8992. }
  8993. }
  8994. this.semicolon();
  8995. }
  8996. shouldParseExportDeclaration() {
  8997. if (this.match(types.at)) {
  8998. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  8999. if (this.hasPlugin("decorators")) {
  9000. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  9001. this.unexpected(this.state.start, "Decorators must be placed *before* the 'export' keyword." + " You can set the 'decoratorsBeforeExport' option to false to use" + " the 'export @decorator class {}' syntax");
  9002. } else {
  9003. return true;
  9004. }
  9005. }
  9006. }
  9007. return this.state.type.keyword === "var" || this.state.type.keyword === "const" || this.state.type.keyword === "function" || this.state.type.keyword === "class" || this.isLet() || this.isAsyncFunction();
  9008. }
  9009. checkExport(node, checkNames, isDefault, isFrom) {
  9010. if (checkNames) {
  9011. if (isDefault) {
  9012. this.checkDuplicateExports(node, "default");
  9013. } else if (node.specifiers && node.specifiers.length) {
  9014. for (let _i3 = 0, _node$specifiers = node.specifiers; _i3 < _node$specifiers.length; _i3++) {
  9015. const specifier = _node$specifiers[_i3];
  9016. this.checkDuplicateExports(specifier, specifier.exported.name);
  9017. if (!isFrom && specifier.local) {
  9018. this.checkReservedWord(specifier.local.name, specifier.local.start, true, false);
  9019. this.scope.checkLocalExport(specifier.local);
  9020. }
  9021. }
  9022. } else if (node.declaration) {
  9023. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  9024. const id = node.declaration.id;
  9025. if (!id) throw new Error("Assertion failure");
  9026. this.checkDuplicateExports(node, id.name);
  9027. } else if (node.declaration.type === "VariableDeclaration") {
  9028. for (let _i4 = 0, _node$declaration$dec = node.declaration.declarations; _i4 < _node$declaration$dec.length; _i4++) {
  9029. const declaration = _node$declaration$dec[_i4];
  9030. this.checkDeclaration(declaration.id);
  9031. }
  9032. }
  9033. }
  9034. }
  9035. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  9036. if (currentContextDecorators.length) {
  9037. const isClass = node.declaration && (node.declaration.type === "ClassDeclaration" || node.declaration.type === "ClassExpression");
  9038. if (!node.declaration || !isClass) {
  9039. throw this.raise(node.start, "You can only use decorators on an export when exporting a class");
  9040. }
  9041. this.takeDecorators(node.declaration);
  9042. }
  9043. }
  9044. checkDeclaration(node) {
  9045. if (node.type === "Identifier") {
  9046. this.checkDuplicateExports(node, node.name);
  9047. } else if (node.type === "ObjectPattern") {
  9048. for (let _i5 = 0, _node$properties = node.properties; _i5 < _node$properties.length; _i5++) {
  9049. const prop = _node$properties[_i5];
  9050. this.checkDeclaration(prop);
  9051. }
  9052. } else if (node.type === "ArrayPattern") {
  9053. for (let _i6 = 0, _node$elements = node.elements; _i6 < _node$elements.length; _i6++) {
  9054. const elem = _node$elements[_i6];
  9055. if (elem) {
  9056. this.checkDeclaration(elem);
  9057. }
  9058. }
  9059. } else if (node.type === "ObjectProperty") {
  9060. this.checkDeclaration(node.value);
  9061. } else if (node.type === "RestElement") {
  9062. this.checkDeclaration(node.argument);
  9063. } else if (node.type === "AssignmentPattern") {
  9064. this.checkDeclaration(node.left);
  9065. }
  9066. }
  9067. checkDuplicateExports(node, name) {
  9068. if (this.state.exportedIdentifiers.indexOf(name) > -1) {
  9069. throw this.raise(node.start, name === "default" ? "Only one default export allowed per module." : `\`${name}\` has already been exported. Exported identifiers must be unique.`);
  9070. }
  9071. this.state.exportedIdentifiers.push(name);
  9072. }
  9073. parseExportSpecifiers() {
  9074. const nodes = [];
  9075. let first = true;
  9076. this.expect(types.braceL);
  9077. while (!this.eat(types.braceR)) {
  9078. if (first) {
  9079. first = false;
  9080. } else {
  9081. this.expect(types.comma);
  9082. if (this.eat(types.braceR)) break;
  9083. }
  9084. const node = this.startNode();
  9085. node.local = this.parseIdentifier(true);
  9086. node.exported = this.eatContextual("as") ? this.parseIdentifier(true) : node.local.__clone();
  9087. nodes.push(this.finishNode(node, "ExportSpecifier"));
  9088. }
  9089. return nodes;
  9090. }
  9091. parseImport(node) {
  9092. node.specifiers = [];
  9093. if (!this.match(types.string)) {
  9094. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  9095. const parseNext = !hasDefault || this.eat(types.comma);
  9096. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  9097. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  9098. this.expectContextual("from");
  9099. }
  9100. node.source = this.parseImportSource();
  9101. this.semicolon();
  9102. return this.finishNode(node, "ImportDeclaration");
  9103. }
  9104. parseImportSource() {
  9105. if (!this.match(types.string)) this.unexpected();
  9106. return this.parseExprAtom();
  9107. }
  9108. shouldParseDefaultImport(node) {
  9109. return this.match(types.name);
  9110. }
  9111. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  9112. specifier.local = this.parseIdentifier();
  9113. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, contextDescription);
  9114. node.specifiers.push(this.finishNode(specifier, type));
  9115. }
  9116. maybeParseDefaultImportSpecifier(node) {
  9117. if (this.shouldParseDefaultImport(node)) {
  9118. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier", "default import specifier");
  9119. return true;
  9120. }
  9121. return false;
  9122. }
  9123. maybeParseStarImportSpecifier(node) {
  9124. if (this.match(types.star)) {
  9125. const specifier = this.startNode();
  9126. this.next();
  9127. this.expectContextual("as");
  9128. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier", "import namespace specifier");
  9129. return true;
  9130. }
  9131. return false;
  9132. }
  9133. parseNamedImportSpecifiers(node) {
  9134. let first = true;
  9135. this.expect(types.braceL);
  9136. while (!this.eat(types.braceR)) {
  9137. if (first) {
  9138. first = false;
  9139. } else {
  9140. if (this.eat(types.colon)) {
  9141. this.unexpected(null, "ES2015 named imports do not destructure. " + "Use another statement for destructuring after the import.");
  9142. }
  9143. this.expect(types.comma);
  9144. if (this.eat(types.braceR)) break;
  9145. }
  9146. this.parseImportSpecifier(node);
  9147. }
  9148. }
  9149. parseImportSpecifier(node) {
  9150. const specifier = this.startNode();
  9151. specifier.imported = this.parseIdentifier(true);
  9152. if (this.eatContextual("as")) {
  9153. specifier.local = this.parseIdentifier();
  9154. } else {
  9155. this.checkReservedWord(specifier.imported.name, specifier.start, true, true);
  9156. specifier.local = specifier.imported.__clone();
  9157. }
  9158. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, "import specifier");
  9159. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  9160. }
  9161. }
  9162. class Parser extends StatementParser {
  9163. constructor(options, input) {
  9164. options = getOptions(options);
  9165. super(options, input);
  9166. const ScopeHandler = this.getScopeHandler();
  9167. this.options = options;
  9168. this.inModule = this.options.sourceType === "module";
  9169. this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
  9170. this.plugins = pluginsMap(this.options.plugins);
  9171. this.filename = options.sourceFilename;
  9172. }
  9173. getScopeHandler() {
  9174. return ScopeHandler;
  9175. }
  9176. parse() {
  9177. this.scope.enter(SCOPE_PROGRAM);
  9178. const file = this.startNode();
  9179. const program = this.startNode();
  9180. this.nextToken();
  9181. return this.parseTopLevel(file, program);
  9182. }
  9183. }
  9184. function pluginsMap(plugins) {
  9185. const pluginMap = new Map();
  9186. for (let _i = 0; _i < plugins.length; _i++) {
  9187. const plugin = plugins[_i];
  9188. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  9189. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  9190. }
  9191. return pluginMap;
  9192. }
  9193. function parse(input, options) {
  9194. if (options && options.sourceType === "unambiguous") {
  9195. options = Object.assign({}, options);
  9196. try {
  9197. options.sourceType = "module";
  9198. const parser = getParser(options, input);
  9199. const ast = parser.parse();
  9200. if (!parser.sawUnambiguousESM) ast.program.sourceType = "script";
  9201. return ast;
  9202. } catch (moduleError) {
  9203. try {
  9204. options.sourceType = "script";
  9205. return getParser(options, input).parse();
  9206. } catch (scriptError) {}
  9207. throw moduleError;
  9208. }
  9209. } else {
  9210. return getParser(options, input).parse();
  9211. }
  9212. }
  9213. function parseExpression(input, options) {
  9214. const parser = getParser(options, input);
  9215. if (parser.options.strictMode) {
  9216. parser.state.strict = true;
  9217. }
  9218. return parser.getExpression();
  9219. }
  9220. function getParser(options, input) {
  9221. let cls = Parser;
  9222. if (options && options.plugins) {
  9223. validatePlugins(options.plugins);
  9224. cls = getParserClass(options.plugins);
  9225. }
  9226. return new cls(options, input);
  9227. }
  9228. const parserClassCache = {};
  9229. function getParserClass(pluginsFromOptions) {
  9230. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  9231. const key = pluginList.join("/");
  9232. let cls = parserClassCache[key];
  9233. if (!cls) {
  9234. cls = Parser;
  9235. for (let _i = 0; _i < pluginList.length; _i++) {
  9236. const plugin = pluginList[_i];
  9237. cls = mixinPlugins[plugin](cls);
  9238. }
  9239. parserClassCache[key] = cls;
  9240. }
  9241. return cls;
  9242. }
  9243. exports.parse = parse;
  9244. exports.parseExpression = parseExpression;
  9245. exports.tokTypes = types;