UI for Zipcoin Blue

rollup.browser.js 313KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627
  1. /*
  2. Rollup.js v0.50.0
  3. Sat Sep 16 2017 09:48:09 GMT-0400 (EDT) - commit b949eb08169115ff66648838cbc4833379bf9440
  4. https://github.com/rollup/rollup
  5. Released under the MIT License.
  6. */
  7. (function (global, factory) {
  8. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  9. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  10. (factory((global.rollup = {})));
  11. }(this, (function (exports) { 'use strict';
  12. const DEBUG = false;
  13. const map = new Map;
  14. let timeStartHelper;
  15. let timeEndHelper;
  16. if ( typeof process === 'undefined' || typeof process.hrtime === 'undefined' ) {
  17. timeStartHelper = function timeStartHelper () {
  18. return window.performance.now();
  19. };
  20. timeEndHelper = function timeEndHelper ( previous ) {
  21. return window.performance.now() - previous;
  22. };
  23. } else {
  24. timeStartHelper = function timeStartHelper () {
  25. return process.hrtime();
  26. };
  27. timeEndHelper = function timeEndHelper ( previous ) {
  28. const hrtime = process.hrtime( previous );
  29. return hrtime[0] * 1e3 + Math.floor( hrtime[1] / 1e6 );
  30. };
  31. }
  32. function timeStart ( label ) {
  33. if ( !map.has( label ) ) {
  34. map.set( label, {
  35. time: 0
  36. });
  37. }
  38. map.get( label ).start = timeStartHelper();
  39. }
  40. function timeEnd ( label ) {
  41. if ( map.has( label ) ) {
  42. const item = map.get( label );
  43. item.time += timeEndHelper( item.start );
  44. }
  45. }
  46. function flushTime ( log ) {
  47. if ( log === void 0 ) log = defaultLog;
  48. for ( const item of map.entries() ) {
  49. log( item[0], item[1].time );
  50. }
  51. map.clear();
  52. }
  53. function defaultLog ( label, time ) {
  54. if ( DEBUG ) {
  55. /* eslint-disable no-console */
  56. console.info( '%dms: %s', time, label );
  57. /* eslint-enable no-console */
  58. }
  59. }
  60. const absolutePath = /^(?:\/|(?:[A-Za-z]:)?[\\|/])/;
  61. const relativePath = /^\.?\.\//;
  62. function isAbsolute ( path ) {
  63. return absolutePath.test( path );
  64. }
  65. function isRelative ( path ) {
  66. return relativePath.test( path );
  67. }
  68. function normalize ( path ) {
  69. return path.replace( /\\/g, '/' );
  70. }
  71. function basename ( path ) {
  72. return path.split( /(\/|\\)/ ).pop();
  73. }
  74. function dirname ( path ) {
  75. const match = /(\/|\\)[^/\\]*$/.exec( path );
  76. if ( !match ) { return '.'; }
  77. const dir = path.slice( 0, -match[0].length );
  78. // If `dir` is the empty string, we're at root.
  79. return dir ? dir : '/';
  80. }
  81. function extname ( path ) {
  82. const match = /\.[^.]+$/.exec( basename( path ) );
  83. if ( !match ) { return ''; }
  84. return match[0];
  85. }
  86. function relative ( from, to ) {
  87. const fromParts = from.split( /[/\\]/ ).filter( Boolean );
  88. const toParts = to.split( /[/\\]/ ).filter( Boolean );
  89. while ( fromParts[0] && toParts[0] && fromParts[0] === toParts[0] ) {
  90. fromParts.shift();
  91. toParts.shift();
  92. }
  93. while ( toParts[0] === '.' || toParts[0] === '..' ) {
  94. const toPart = toParts.shift();
  95. if ( toPart === '..' ) {
  96. fromParts.pop();
  97. }
  98. }
  99. while ( fromParts.pop() ) {
  100. toParts.unshift( '..' );
  101. }
  102. return toParts.join( '/' );
  103. }
  104. function resolve () {
  105. var paths = [], len = arguments.length;
  106. while ( len-- ) paths[ len ] = arguments[ len ];
  107. let resolvedParts = paths.shift().split( /[/\\]/ );
  108. paths.forEach( path => {
  109. if ( isAbsolute( path ) ) {
  110. resolvedParts = path.split( /[/\\]/ );
  111. } else {
  112. const parts = path.split( /[/\\]/ );
  113. while ( parts[0] === '.' || parts[0] === '..' ) {
  114. const part = parts.shift();
  115. if ( part === '..' ) {
  116. resolvedParts.pop();
  117. }
  118. }
  119. resolvedParts.push.apply( resolvedParts, parts );
  120. }
  121. });
  122. return resolvedParts.join( '/' ); // TODO windows...
  123. }
  124. const nope = method => `Cannot use fs.${method} inside browser`;
  125. const lstatSync = nope( 'lstatSync' );
  126. const readdirSync = nope( 'readdirSync' );
  127. const readFileSync = nope( 'readFileSync' );
  128. const realpathSync = nope( 'realpathSync' );
  129. const writeFile = nope( 'writeFile' );
  130. var keys = Object.keys;
  131. function blank () {
  132. return Object.create( null );
  133. }
  134. function forOwn ( object, func ) {
  135. Object.keys( object ).forEach( key => func( object[ key ], key ) );
  136. }
  137. function assign ( target ) {
  138. var sources = [], len = arguments.length - 1;
  139. while ( len-- > 0 ) sources[ len ] = arguments[ len + 1 ];
  140. sources.forEach( source => {
  141. for ( const key in source ) {
  142. if ( source.hasOwnProperty( key ) ) { target[ key ] = source[ key ]; }
  143. }
  144. });
  145. return target;
  146. }
  147. function mapSequence ( array, fn ) {
  148. const results = [];
  149. let promise = Promise.resolve();
  150. function next ( member, i ) {
  151. return Promise.resolve( fn( member ) ).then( value => results[i] = value );
  152. }
  153. for ( let i = 0; i < array.length; i += 1 ) {
  154. promise = promise.then( () => next( array[i], i ) );
  155. }
  156. return promise.then( () => results );
  157. }
  158. function validateKeys ( actualKeys, allowedKeys ) {
  159. let i = actualKeys.length;
  160. while ( i-- ) {
  161. const key = actualKeys[i];
  162. if ( allowedKeys.indexOf( key ) === -1 ) {
  163. return new Error(
  164. `Unexpected key '${ key }' found, expected one of: ${ allowedKeys.join( ', ' ) }`
  165. );
  166. }
  167. }
  168. }
  169. function error ( props ) {
  170. // use the same constructor as props (if it's an error object)
  171. // so that err.name is preserved etc
  172. // (Object.keys below does not update these values because they
  173. // are properties on the prototype chain)
  174. // basically if props is a SyntaxError it will not be overriden as a generic Error
  175. const constructor = (props instanceof Error) ? props.constructor : Error;
  176. const err = new constructor( props.message );
  177. Object.keys( props ).forEach( key => {
  178. err[ key ] = props[ key ];
  179. });
  180. throw err;
  181. }
  182. // this looks ridiculous, but it prevents sourcemap tooling from mistaking
  183. // this for an actual sourceMappingURL
  184. let SOURCEMAPPING_URL = 'sourceMa';
  185. SOURCEMAPPING_URL += 'ppingURL';
  186. const SOURCEMAPPING_URL_RE = new RegExp( `^#\\s+${SOURCEMAPPING_URL}=.+\\n?` );
  187. var charToInteger = {};
  188. var integerToChar = {};
  189. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split( '' ).forEach( function ( char, i ) {
  190. charToInteger[ char ] = i;
  191. integerToChar[ i ] = char;
  192. });
  193. function decode$1 ( string ) {
  194. var result = [],
  195. len = string.length,
  196. i,
  197. hasContinuationBit,
  198. shift = 0,
  199. value = 0,
  200. integer,
  201. shouldNegate;
  202. for ( i = 0; i < len; i += 1 ) {
  203. integer = charToInteger[ string[i] ];
  204. if ( integer === undefined ) {
  205. throw new Error( 'Invalid character (' + string[i] + ')' );
  206. }
  207. hasContinuationBit = integer & 32;
  208. integer &= 31;
  209. value += integer << shift;
  210. if ( hasContinuationBit ) {
  211. shift += 5;
  212. } else {
  213. shouldNegate = value & 1;
  214. value >>= 1;
  215. result.push( shouldNegate ? -value : value );
  216. // reset
  217. value = shift = 0;
  218. }
  219. }
  220. return result;
  221. }
  222. function encode$1 ( value ) {
  223. var result, i;
  224. if ( typeof value === 'number' ) {
  225. result = encodeInteger( value );
  226. } else {
  227. result = '';
  228. for ( i = 0; i < value.length; i += 1 ) {
  229. result += encodeInteger( value[i] );
  230. }
  231. }
  232. return result;
  233. }
  234. function encodeInteger ( num ) {
  235. var result = '', clamped;
  236. if ( num < 0 ) {
  237. num = ( -num << 1 ) | 1;
  238. } else {
  239. num <<= 1;
  240. }
  241. do {
  242. clamped = num & 31;
  243. num >>= 5;
  244. if ( num > 0 ) {
  245. clamped |= 32;
  246. }
  247. result += integerToChar[ clamped ];
  248. } while ( num > 0 );
  249. return result;
  250. }
  251. function decodeSegments ( encodedSegments ) {
  252. var i = encodedSegments.length;
  253. var segments = new Array( i );
  254. while ( i-- ) { segments[i] = decode$1( encodedSegments[i] ); }
  255. return segments;
  256. }
  257. function decode$$1 ( mappings ) {
  258. var sourceFileIndex = 0; // second field
  259. var sourceCodeLine = 0; // third field
  260. var sourceCodeColumn = 0; // fourth field
  261. var nameIndex = 0; // fifth field
  262. var lines = mappings.split( ';' );
  263. var numLines = lines.length;
  264. var decoded = new Array( numLines );
  265. var i;
  266. var j;
  267. var line;
  268. var generatedCodeColumn;
  269. var decodedLine;
  270. var segments;
  271. var segment;
  272. var result;
  273. for ( i = 0; i < numLines; i += 1 ) {
  274. line = lines[i];
  275. generatedCodeColumn = 0; // first field - reset each time
  276. decodedLine = [];
  277. segments = decodeSegments( line.split( ',' ) );
  278. for ( j = 0; j < segments.length; j += 1 ) {
  279. segment = segments[j];
  280. if ( !segment.length ) {
  281. break;
  282. }
  283. generatedCodeColumn += segment[0];
  284. result = [ generatedCodeColumn ];
  285. decodedLine.push( result );
  286. if ( segment.length === 1 ) {
  287. // only one field!
  288. continue;
  289. }
  290. sourceFileIndex += segment[1];
  291. sourceCodeLine += segment[2];
  292. sourceCodeColumn += segment[3];
  293. result.push( sourceFileIndex, sourceCodeLine, sourceCodeColumn );
  294. if ( segment.length === 5 ) {
  295. nameIndex += segment[4];
  296. result.push( nameIndex );
  297. }
  298. }
  299. decoded[i] = decodedLine;
  300. }
  301. return decoded;
  302. }
  303. function encode$$1 ( decoded ) {
  304. var offsets = {
  305. generatedCodeColumn: 0,
  306. sourceFileIndex: 0, // second field
  307. sourceCodeLine: 0, // third field
  308. sourceCodeColumn: 0, // fourth field
  309. nameIndex: 0 // fifth field
  310. };
  311. return decoded.map( function (line) {
  312. offsets.generatedCodeColumn = 0; // first field - reset each time
  313. return line.map( encodeSegment ).join( ',' );
  314. }).join( ';' );
  315. function encodeSegment ( segment ) {
  316. if ( !segment.length ) {
  317. return segment;
  318. }
  319. var result = new Array( segment.length );
  320. result[0] = segment[0] - offsets.generatedCodeColumn;
  321. offsets.generatedCodeColumn = segment[0];
  322. if ( segment.length === 1 ) {
  323. // only one field!
  324. return encode$1( result );
  325. }
  326. result[1] = segment[1] - offsets.sourceFileIndex;
  327. result[2] = segment[2] - offsets.sourceCodeLine;
  328. result[3] = segment[3] - offsets.sourceCodeColumn;
  329. offsets.sourceFileIndex = segment[1];
  330. offsets.sourceCodeLine = segment[2];
  331. offsets.sourceCodeColumn = segment[3];
  332. if ( segment.length === 5 ) {
  333. result[4] = segment[4] - offsets.nameIndex;
  334. offsets.nameIndex = segment[4];
  335. }
  336. return encode$1( result );
  337. }
  338. }
  339. var charToInteger$1 = {};
  340. var integerToChar$1 = {};
  341. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split( '' ).forEach( function ( char, i ) {
  342. charToInteger$1[ char ] = i;
  343. integerToChar$1[ i ] = char;
  344. });
  345. function encode ( value ) {
  346. var result;
  347. if ( typeof value === 'number' ) {
  348. result = encodeInteger$1( value );
  349. } else {
  350. result = '';
  351. for ( var i = 0; i < value.length; i += 1 ) {
  352. result += encodeInteger$1( value[i] );
  353. }
  354. }
  355. return result;
  356. }
  357. function encodeInteger$1 ( num ) {
  358. var result = '';
  359. if ( num < 0 ) {
  360. num = ( -num << 1 ) | 1;
  361. } else {
  362. num <<= 1;
  363. }
  364. do {
  365. var clamped = num & 31;
  366. num >>= 5;
  367. if ( num > 0 ) {
  368. clamped |= 32;
  369. }
  370. result += integerToChar$1[ clamped ];
  371. } while ( num > 0 );
  372. return result;
  373. }
  374. function Chunk ( start, end, content ) {
  375. this.start = start;
  376. this.end = end;
  377. this.original = content;
  378. this.intro = '';
  379. this.outro = '';
  380. this.content = content;
  381. this.storeName = false;
  382. this.edited = false;
  383. // we make these non-enumerable, for sanity while debugging
  384. Object.defineProperties( this, {
  385. previous: { writable: true, value: null },
  386. next: { writable: true, value: null }
  387. });
  388. }
  389. Chunk.prototype = {
  390. appendLeft: function appendLeft ( content ) {
  391. this.outro += content;
  392. },
  393. appendRight: function appendRight ( content ) {
  394. this.intro = this.intro + content;
  395. },
  396. clone: function clone () {
  397. var chunk = new Chunk( this.start, this.end, this.original );
  398. chunk.intro = this.intro;
  399. chunk.outro = this.outro;
  400. chunk.content = this.content;
  401. chunk.storeName = this.storeName;
  402. chunk.edited = this.edited;
  403. return chunk;
  404. },
  405. contains: function contains ( index ) {
  406. return this.start < index && index < this.end;
  407. },
  408. eachNext: function eachNext ( fn ) {
  409. var chunk = this;
  410. while ( chunk ) {
  411. fn( chunk );
  412. chunk = chunk.next;
  413. }
  414. },
  415. eachPrevious: function eachPrevious ( fn ) {
  416. var chunk = this;
  417. while ( chunk ) {
  418. fn( chunk );
  419. chunk = chunk.previous;
  420. }
  421. },
  422. edit: function edit ( content, storeName, contentOnly ) {
  423. this.content = content;
  424. if ( !contentOnly ) {
  425. this.intro = '';
  426. this.outro = '';
  427. }
  428. this.storeName = storeName;
  429. this.edited = true;
  430. return this;
  431. },
  432. prependLeft: function prependLeft ( content ) {
  433. this.outro = content + this.outro;
  434. },
  435. prependRight: function prependRight ( content ) {
  436. this.intro = content + this.intro;
  437. },
  438. split: function split ( index ) {
  439. var sliceIndex = index - this.start;
  440. var originalBefore = this.original.slice( 0, sliceIndex );
  441. var originalAfter = this.original.slice( sliceIndex );
  442. this.original = originalBefore;
  443. var newChunk = new Chunk( index, this.end, originalAfter );
  444. newChunk.outro = this.outro;
  445. this.outro = '';
  446. this.end = index;
  447. if ( this.edited ) {
  448. // TODO is this block necessary?...
  449. newChunk.edit( '', false );
  450. this.content = '';
  451. } else {
  452. this.content = originalBefore;
  453. }
  454. newChunk.next = this.next;
  455. if ( newChunk.next ) { newChunk.next.previous = newChunk; }
  456. newChunk.previous = this;
  457. this.next = newChunk;
  458. return newChunk;
  459. },
  460. toString: function toString () {
  461. return this.intro + this.content + this.outro;
  462. },
  463. trimEnd: function trimEnd ( rx ) {
  464. this.outro = this.outro.replace( rx, '' );
  465. if ( this.outro.length ) { return true; }
  466. var trimmed = this.content.replace( rx, '' );
  467. if ( trimmed.length ) {
  468. if ( trimmed !== this.content ) {
  469. this.split( this.start + trimmed.length ).edit( '', false );
  470. }
  471. return true;
  472. } else {
  473. this.edit( '', false );
  474. this.intro = this.intro.replace( rx, '' );
  475. if ( this.intro.length ) { return true; }
  476. }
  477. },
  478. trimStart: function trimStart ( rx ) {
  479. this.intro = this.intro.replace( rx, '' );
  480. if ( this.intro.length ) { return true; }
  481. var trimmed = this.content.replace( rx, '' );
  482. if ( trimmed.length ) {
  483. if ( trimmed !== this.content ) {
  484. this.split( this.end - trimmed.length );
  485. this.edit( '', false );
  486. }
  487. return true;
  488. } else {
  489. this.edit( '', false );
  490. this.outro = this.outro.replace( rx, '' );
  491. if ( this.outro.length ) { return true; }
  492. }
  493. }
  494. };
  495. var _btoa;
  496. if ( typeof window !== 'undefined' && typeof window.btoa === 'function' ) {
  497. _btoa = window.btoa;
  498. } else if ( typeof Buffer === 'function' ) {
  499. _btoa = function (str) { return new Buffer( str ).toString( 'base64' ); };
  500. } else {
  501. _btoa = function () {
  502. throw new Error( 'Unsupported environment: `window.btoa` or `Buffer` should be supported.' );
  503. };
  504. }
  505. var btoa = _btoa;
  506. function SourceMap ( properties ) {
  507. this.version = 3;
  508. this.file = properties.file;
  509. this.sources = properties.sources;
  510. this.sourcesContent = properties.sourcesContent;
  511. this.names = properties.names;
  512. this.mappings = properties.mappings;
  513. }
  514. SourceMap.prototype = {
  515. toString: function toString () {
  516. return JSON.stringify( this );
  517. },
  518. toUrl: function toUrl () {
  519. return 'data:application/json;charset=utf-8;base64,' + btoa( this.toString() );
  520. }
  521. };
  522. function guessIndent ( code ) {
  523. var lines = code.split( '\n' );
  524. var tabbed = lines.filter( function (line) { return /^\t+/.test( line ); } );
  525. var spaced = lines.filter( function (line) { return /^ {2,}/.test( line ); } );
  526. if ( tabbed.length === 0 && spaced.length === 0 ) {
  527. return null;
  528. }
  529. // More lines tabbed than spaced? Assume tabs, and
  530. // default to tabs in the case of a tie (or nothing
  531. // to go on)
  532. if ( tabbed.length >= spaced.length ) {
  533. return '\t';
  534. }
  535. // Otherwise, we need to guess the multiple
  536. var min = spaced.reduce( function ( previous, current ) {
  537. var numSpaces = /^ +/.exec( current )[0].length;
  538. return Math.min( numSpaces, previous );
  539. }, Infinity );
  540. return new Array( min + 1 ).join( ' ' );
  541. }
  542. function getRelativePath ( from, to ) {
  543. var fromParts = from.split( /[\/\\]/ );
  544. var toParts = to.split( /[\/\\]/ );
  545. fromParts.pop(); // get dirname
  546. while ( fromParts[0] === toParts[0] ) {
  547. fromParts.shift();
  548. toParts.shift();
  549. }
  550. if ( fromParts.length ) {
  551. var i = fromParts.length;
  552. while ( i-- ) { fromParts[i] = '..'; }
  553. }
  554. return fromParts.concat( toParts ).join( '/' );
  555. }
  556. var toString$1 = Object.prototype.toString;
  557. function isObject ( thing ) {
  558. return toString$1.call( thing ) === '[object Object]';
  559. }
  560. function getLocator ( source ) {
  561. var originalLines = source.split( '\n' );
  562. var start = 0;
  563. var lineRanges = originalLines.map( function ( line, i ) {
  564. var end = start + line.length + 1;
  565. var range = { start: start, end: end, line: i };
  566. start = end;
  567. return range;
  568. });
  569. var i = 0;
  570. function rangeContains ( range, index ) {
  571. return range.start <= index && index < range.end;
  572. }
  573. function getLocation ( range, index ) {
  574. return { line: range.line, column: index - range.start };
  575. }
  576. return function locate ( index ) {
  577. var range = lineRanges[i];
  578. var d = index >= range.end ? 1 : -1;
  579. while ( range ) {
  580. if ( rangeContains( range, index ) ) { return getLocation( range, index ); }
  581. i += d;
  582. range = lineRanges[i];
  583. }
  584. };
  585. }
  586. function Mappings ( hires ) {
  587. var this$1 = this;
  588. var offsets = {
  589. generatedCodeColumn: 0,
  590. sourceIndex: 0,
  591. sourceCodeLine: 0,
  592. sourceCodeColumn: 0,
  593. sourceCodeName: 0
  594. };
  595. var generatedCodeLine = 0;
  596. var generatedCodeColumn = 0;
  597. this.raw = [];
  598. var rawSegments = this.raw[ generatedCodeLine ] = [];
  599. var pending = null;
  600. this.addEdit = function ( sourceIndex, content, original, loc, nameIndex ) {
  601. if ( content.length ) {
  602. rawSegments.push([
  603. generatedCodeColumn,
  604. sourceIndex,
  605. loc.line,
  606. loc.column,
  607. nameIndex ]);
  608. } else if ( pending ) {
  609. rawSegments.push( pending );
  610. }
  611. this$1.advance( content );
  612. pending = null;
  613. };
  614. this.addUneditedChunk = function ( sourceIndex, chunk, original, loc, sourcemapLocations ) {
  615. var originalCharIndex = chunk.start;
  616. var first = true;
  617. while ( originalCharIndex < chunk.end ) {
  618. if ( hires || first || sourcemapLocations[ originalCharIndex ] ) {
  619. rawSegments.push([
  620. generatedCodeColumn,
  621. sourceIndex,
  622. loc.line,
  623. loc.column,
  624. -1
  625. ]);
  626. }
  627. if ( original[ originalCharIndex ] === '\n' ) {
  628. loc.line += 1;
  629. loc.column = 0;
  630. generatedCodeLine += 1;
  631. this$1.raw[ generatedCodeLine ] = rawSegments = [];
  632. generatedCodeColumn = 0;
  633. } else {
  634. loc.column += 1;
  635. generatedCodeColumn += 1;
  636. }
  637. originalCharIndex += 1;
  638. first = false;
  639. }
  640. pending = [
  641. generatedCodeColumn,
  642. sourceIndex,
  643. loc.line,
  644. loc.column,
  645. -1 ];
  646. };
  647. this.advance = function (str) {
  648. if ( !str ) { return; }
  649. var lines = str.split( '\n' );
  650. var lastLine = lines.pop();
  651. if ( lines.length ) {
  652. generatedCodeLine += lines.length;
  653. this$1.raw[ generatedCodeLine ] = rawSegments = [];
  654. generatedCodeColumn = lastLine.length;
  655. } else {
  656. generatedCodeColumn += lastLine.length;
  657. }
  658. };
  659. this.encode = function () {
  660. return this$1.raw.map( function (segments) {
  661. var generatedCodeColumn = 0;
  662. return segments.map( function (segment) {
  663. var arr = [
  664. segment[0] - generatedCodeColumn,
  665. segment[1] - offsets.sourceIndex,
  666. segment[2] - offsets.sourceCodeLine,
  667. segment[3] - offsets.sourceCodeColumn
  668. ];
  669. generatedCodeColumn = segment[0];
  670. offsets.sourceIndex = segment[1];
  671. offsets.sourceCodeLine = segment[2];
  672. offsets.sourceCodeColumn = segment[3];
  673. if ( ~segment[4] ) {
  674. arr.push( segment[4] - offsets.sourceCodeName );
  675. offsets.sourceCodeName = segment[4];
  676. }
  677. return encode( arr );
  678. }).join( ',' );
  679. }).join( ';' );
  680. };
  681. }
  682. var Stats = function Stats () {
  683. Object.defineProperties( this, {
  684. startTimes: { value: {} }
  685. });
  686. };
  687. Stats.prototype.time = function time ( label ) {
  688. this.startTimes[ label ] = process.hrtime();
  689. };
  690. Stats.prototype.timeEnd = function timeEnd ( label ) {
  691. var elapsed = process.hrtime( this.startTimes[ label ] );
  692. if ( !this[ label ] ) { this[ label ] = 0; }
  693. this[ label ] += elapsed[0] * 1e3 + elapsed[1] * 1e-6;
  694. };
  695. var warned = {
  696. insertLeft: false,
  697. insertRight: false,
  698. storeName: false
  699. };
  700. function MagicString$1 ( string, options ) {
  701. if ( options === void 0 ) options = {};
  702. var chunk = new Chunk( 0, string.length, string );
  703. Object.defineProperties( this, {
  704. original: { writable: true, value: string },
  705. outro: { writable: true, value: '' },
  706. intro: { writable: true, value: '' },
  707. firstChunk: { writable: true, value: chunk },
  708. lastChunk: { writable: true, value: chunk },
  709. lastSearchedChunk: { writable: true, value: chunk },
  710. byStart: { writable: true, value: {} },
  711. byEnd: { writable: true, value: {} },
  712. filename: { writable: true, value: options.filename },
  713. indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
  714. sourcemapLocations: { writable: true, value: {} },
  715. storedNames: { writable: true, value: {} },
  716. indentStr: { writable: true, value: guessIndent( string ) }
  717. });
  718. this.byStart[ 0 ] = chunk;
  719. this.byEnd[ string.length ] = chunk;
  720. }
  721. MagicString$1.prototype = {
  722. addSourcemapLocation: function addSourcemapLocation ( char ) {
  723. this.sourcemapLocations[ char ] = true;
  724. },
  725. append: function append ( content ) {
  726. if ( typeof content !== 'string' ) { throw new TypeError( 'outro content must be a string' ); }
  727. this.outro += content;
  728. return this;
  729. },
  730. appendLeft: function appendLeft ( index, content ) {
  731. if ( typeof content !== 'string' ) { throw new TypeError( 'inserted content must be a string' ); }
  732. this._split( index );
  733. var chunk = this.byEnd[ index ];
  734. if ( chunk ) {
  735. chunk.appendLeft( content );
  736. } else {
  737. this.intro += content;
  738. }
  739. return this;
  740. },
  741. appendRight: function appendRight ( index, content ) {
  742. if ( typeof content !== 'string' ) { throw new TypeError( 'inserted content must be a string' ); }
  743. this._split( index );
  744. var chunk = this.byStart[ index ];
  745. if ( chunk ) {
  746. chunk.appendRight( content );
  747. } else {
  748. this.outro += content;
  749. }
  750. return this;
  751. },
  752. clone: function clone () {
  753. var cloned = new MagicString$1( this.original, { filename: this.filename });
  754. var originalChunk = this.firstChunk;
  755. var clonedChunk = cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone();
  756. while ( originalChunk ) {
  757. cloned.byStart[ clonedChunk.start ] = clonedChunk;
  758. cloned.byEnd[ clonedChunk.end ] = clonedChunk;
  759. var nextOriginalChunk = originalChunk.next;
  760. var nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
  761. if ( nextClonedChunk ) {
  762. clonedChunk.next = nextClonedChunk;
  763. nextClonedChunk.previous = clonedChunk;
  764. clonedChunk = nextClonedChunk;
  765. }
  766. originalChunk = nextOriginalChunk;
  767. }
  768. cloned.lastChunk = clonedChunk;
  769. if ( this.indentExclusionRanges ) {
  770. cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
  771. }
  772. Object.keys( this.sourcemapLocations ).forEach( function (loc) {
  773. cloned.sourcemapLocations[ loc ] = true;
  774. });
  775. return cloned;
  776. },
  777. generateMap: function generateMap ( options ) {
  778. var this$1 = this;
  779. options = options || {};
  780. var sourceIndex = 0;
  781. var names = Object.keys( this.storedNames );
  782. var mappings = new Mappings( options.hires );
  783. var locate = getLocator( this.original );
  784. if ( this.intro ) {
  785. mappings.advance( this.intro );
  786. }
  787. this.firstChunk.eachNext( function (chunk) {
  788. var loc = locate( chunk.start );
  789. if ( chunk.intro.length ) { mappings.advance( chunk.intro ); }
  790. if ( chunk.edited ) {
  791. mappings.addEdit( sourceIndex, chunk.content, chunk.original, loc, chunk.storeName ? names.indexOf( chunk.original ) : -1 );
  792. } else {
  793. mappings.addUneditedChunk( sourceIndex, chunk, this$1.original, loc, this$1.sourcemapLocations );
  794. }
  795. if ( chunk.outro.length ) { mappings.advance( chunk.outro ); }
  796. });
  797. var map = new SourceMap({
  798. file: ( options.file ? options.file.split( /[\/\\]/ ).pop() : null ),
  799. sources: [ options.source ? getRelativePath( options.file || '', options.source ) : null ],
  800. sourcesContent: options.includeContent ? [ this.original ] : [ null ],
  801. names: names,
  802. mappings: mappings.encode()
  803. });
  804. return map;
  805. },
  806. getIndentString: function getIndentString () {
  807. return this.indentStr === null ? '\t' : this.indentStr;
  808. },
  809. indent: function indent ( indentStr, options ) {
  810. var this$1 = this;
  811. var pattern = /^[^\r\n]/gm;
  812. if ( isObject( indentStr ) ) {
  813. options = indentStr;
  814. indentStr = undefined;
  815. }
  816. indentStr = indentStr !== undefined ? indentStr : ( this.indentStr || '\t' );
  817. if ( indentStr === '' ) { return this; } // noop
  818. options = options || {};
  819. // Process exclusion ranges
  820. var isExcluded = {};
  821. if ( options.exclude ) {
  822. var exclusions = typeof options.exclude[0] === 'number' ? [ options.exclude ] : options.exclude;
  823. exclusions.forEach( function (exclusion) {
  824. for ( var i = exclusion[0]; i < exclusion[1]; i += 1 ) {
  825. isExcluded[i] = true;
  826. }
  827. });
  828. }
  829. var shouldIndentNextCharacter = options.indentStart !== false;
  830. var replacer = function (match) {
  831. if ( shouldIndentNextCharacter ) { return ("" + indentStr + match); }
  832. shouldIndentNextCharacter = true;
  833. return match;
  834. };
  835. this.intro = this.intro.replace( pattern, replacer );
  836. var charIndex = 0;
  837. var chunk = this.firstChunk;
  838. while ( chunk ) {
  839. var end = chunk.end;
  840. if ( chunk.edited ) {
  841. if ( !isExcluded[ charIndex ] ) {
  842. chunk.content = chunk.content.replace( pattern, replacer );
  843. if ( chunk.content.length ) {
  844. shouldIndentNextCharacter = chunk.content[ chunk.content.length - 1 ] === '\n';
  845. }
  846. }
  847. } else {
  848. charIndex = chunk.start;
  849. while ( charIndex < end ) {
  850. if ( !isExcluded[ charIndex ] ) {
  851. var char = this$1.original[ charIndex ];
  852. if ( char === '\n' ) {
  853. shouldIndentNextCharacter = true;
  854. } else if ( char !== '\r' && shouldIndentNextCharacter ) {
  855. shouldIndentNextCharacter = false;
  856. if ( charIndex === chunk.start ) {
  857. chunk.prependRight( indentStr );
  858. } else {
  859. this$1._splitChunk( chunk, charIndex );
  860. chunk = chunk.next;
  861. chunk.prependRight( indentStr );
  862. }
  863. }
  864. }
  865. charIndex += 1;
  866. }
  867. }
  868. charIndex = chunk.end;
  869. chunk = chunk.next;
  870. }
  871. this.outro = this.outro.replace( pattern, replacer );
  872. return this;
  873. },
  874. insert: function insert () {
  875. throw new Error( 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)' );
  876. },
  877. insertLeft: function insertLeft ( index, content ) {
  878. if ( !warned.insertLeft ) {
  879. console.warn( 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead' ); // eslint-disable-line no-console
  880. warned.insertLeft = true;
  881. }
  882. return this.appendLeft( index, content );
  883. },
  884. insertRight: function insertRight ( index, content ) {
  885. if ( !warned.insertRight ) {
  886. console.warn( 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead' ); // eslint-disable-line no-console
  887. warned.insertRight = true;
  888. }
  889. return this.prependRight( index, content );
  890. },
  891. move: function move ( start, end, index ) {
  892. if ( index >= start && index <= end ) { throw new Error( 'Cannot move a selection inside itself' ); }
  893. this._split( start );
  894. this._split( end );
  895. this._split( index );
  896. var first = this.byStart[ start ];
  897. var last = this.byEnd[ end ];
  898. var oldLeft = first.previous;
  899. var oldRight = last.next;
  900. var newRight = this.byStart[ index ];
  901. if ( !newRight && last === this.lastChunk ) { return this; }
  902. var newLeft = newRight ? newRight.previous : this.lastChunk;
  903. if ( oldLeft ) { oldLeft.next = oldRight; }
  904. if ( oldRight ) { oldRight.previous = oldLeft; }
  905. if ( newLeft ) { newLeft.next = first; }
  906. if ( newRight ) { newRight.previous = last; }
  907. if ( !first.previous ) { this.firstChunk = last.next; }
  908. if ( !last.next ) {
  909. this.lastChunk = first.previous;
  910. this.lastChunk.next = null;
  911. }
  912. first.previous = newLeft;
  913. last.next = newRight || null;
  914. if ( !newLeft ) { this.firstChunk = first; }
  915. if ( !newRight ) { this.lastChunk = last; }
  916. return this;
  917. },
  918. overwrite: function overwrite ( start, end, content, options ) {
  919. var this$1 = this;
  920. if ( typeof content !== 'string' ) { throw new TypeError( 'replacement content must be a string' ); }
  921. while ( start < 0 ) { start += this$1.original.length; }
  922. while ( end < 0 ) { end += this$1.original.length; }
  923. if ( end > this.original.length ) { throw new Error( 'end is out of bounds' ); }
  924. if ( start === end ) { throw new Error( 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead' ); }
  925. this._split( start );
  926. this._split( end );
  927. if ( options === true ) {
  928. if ( !warned.storeName ) {
  929. console.warn( 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string' ); // eslint-disable-line no-console
  930. warned.storeName = true;
  931. }
  932. options = { storeName: true };
  933. }
  934. var storeName = options !== undefined ? options.storeName : false;
  935. var contentOnly = options !== undefined ? options.contentOnly : false;
  936. if ( storeName ) {
  937. var original = this.original.slice( start, end );
  938. this.storedNames[ original ] = true;
  939. }
  940. var first = this.byStart[ start ];
  941. var last = this.byEnd[ end ];
  942. if ( first ) {
  943. if ( end > first.end && first.next !== this.byStart[ first.end ] ) {
  944. throw new Error( 'Cannot overwrite across a split point' );
  945. }
  946. first.edit( content, storeName, contentOnly );
  947. if ( first !== last ) {
  948. var chunk = first.next;
  949. while ( chunk !== last ) {
  950. chunk.edit( '', false );
  951. chunk = chunk.next;
  952. }
  953. chunk.edit( '', false );
  954. }
  955. }
  956. else {
  957. // must be inserting at the end
  958. var newChunk = new Chunk( start, end, '' ).edit( content, storeName );
  959. // TODO last chunk in the array may not be the last chunk, if it's moved...
  960. last.next = newChunk;
  961. newChunk.previous = last;
  962. }
  963. return this;
  964. },
  965. prepend: function prepend ( content ) {
  966. if ( typeof content !== 'string' ) { throw new TypeError( 'outro content must be a string' ); }
  967. this.intro = content + this.intro;
  968. return this;
  969. },
  970. prependLeft: function prependLeft ( index, content ) {
  971. if ( typeof content !== 'string' ) { throw new TypeError( 'inserted content must be a string' ); }
  972. this._split( index );
  973. var chunk = this.byEnd[ index ];
  974. if ( chunk ) {
  975. chunk.prependLeft( content );
  976. } else {
  977. this.intro = content + this.intro;
  978. }
  979. return this;
  980. },
  981. prependRight: function prependRight ( index, content ) {
  982. if ( typeof content !== 'string' ) { throw new TypeError( 'inserted content must be a string' ); }
  983. this._split( index );
  984. var chunk = this.byStart[ index ];
  985. if ( chunk ) {
  986. chunk.prependRight( content );
  987. } else {
  988. this.outro = content + this.outro;
  989. }
  990. return this;
  991. },
  992. remove: function remove ( start, end ) {
  993. var this$1 = this;
  994. while ( start < 0 ) { start += this$1.original.length; }
  995. while ( end < 0 ) { end += this$1.original.length; }
  996. if ( start === end ) { return this; }
  997. if ( start < 0 || end > this.original.length ) { throw new Error( 'Character is out of bounds' ); }
  998. if ( start > end ) { throw new Error( 'end must be greater than start' ); }
  999. this._split( start );
  1000. this._split( end );
  1001. var chunk = this.byStart[ start ];
  1002. while ( chunk ) {
  1003. chunk.intro = '';
  1004. chunk.outro = '';
  1005. chunk.edit( '' );
  1006. chunk = end > chunk.end ? this$1.byStart[ chunk.end ] : null;
  1007. }
  1008. return this;
  1009. },
  1010. slice: function slice ( start, end ) {
  1011. var this$1 = this;
  1012. if ( start === void 0 ) start = 0;
  1013. if ( end === void 0 ) end = this.original.length;
  1014. while ( start < 0 ) { start += this$1.original.length; }
  1015. while ( end < 0 ) { end += this$1.original.length; }
  1016. var result = '';
  1017. // find start chunk
  1018. var chunk = this.firstChunk;
  1019. while ( chunk && ( chunk.start > start || chunk.end <= start ) ) {
  1020. // found end chunk before start
  1021. if ( chunk.start < end && chunk.end >= end ) {
  1022. return result;
  1023. }
  1024. chunk = chunk.next;
  1025. }
  1026. if ( chunk && chunk.edited && chunk.start !== start ) { throw new Error(("Cannot use replaced character " + start + " as slice start anchor.")); }
  1027. var startChunk = chunk;
  1028. while ( chunk ) {
  1029. if ( chunk.intro && ( startChunk !== chunk || chunk.start === start ) ) {
  1030. result += chunk.intro;
  1031. }
  1032. var containsEnd = chunk.start < end && chunk.end >= end;
  1033. if ( containsEnd && chunk.edited && chunk.end !== end ) { throw new Error(("Cannot use replaced character " + end + " as slice end anchor.")); }
  1034. var sliceStart = startChunk === chunk ? start - chunk.start : 0;
  1035. var sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
  1036. result += chunk.content.slice( sliceStart, sliceEnd );
  1037. if ( chunk.outro && ( !containsEnd || chunk.end === end ) ) {
  1038. result += chunk.outro;
  1039. }
  1040. if ( containsEnd ) {
  1041. break;
  1042. }
  1043. chunk = chunk.next;
  1044. }
  1045. return result;
  1046. },
  1047. // TODO deprecate this? not really very useful
  1048. snip: function snip ( start, end ) {
  1049. var clone = this.clone();
  1050. clone.remove( 0, start );
  1051. clone.remove( end, clone.original.length );
  1052. return clone;
  1053. },
  1054. _split: function _split ( index ) {
  1055. var this$1 = this;
  1056. if ( this.byStart[ index ] || this.byEnd[ index ] ) { return; }
  1057. var chunk = this.lastSearchedChunk;
  1058. var searchForward = index > chunk.end;
  1059. while ( true ) {
  1060. if ( chunk.contains( index ) ) { return this$1._splitChunk( chunk, index ); }
  1061. chunk = searchForward ?
  1062. this$1.byStart[ chunk.end ] :
  1063. this$1.byEnd[ chunk.start ];
  1064. }
  1065. },
  1066. _splitChunk: function _splitChunk ( chunk, index ) {
  1067. if ( chunk.edited && chunk.content.length ) { // zero-length edited chunks are a special case (overlapping replacements)
  1068. var loc = getLocator( this.original )( index );
  1069. throw new Error( ("Cannot split a chunk that has already been edited (" + (loc.line) + ":" + (loc.column) + " – \"" + (chunk.original) + "\")") );
  1070. }
  1071. var newChunk = chunk.split( index );
  1072. this.byEnd[ index ] = chunk;
  1073. this.byStart[ index ] = newChunk;
  1074. this.byEnd[ newChunk.end ] = newChunk;
  1075. if ( chunk === this.lastChunk ) { this.lastChunk = newChunk; }
  1076. this.lastSearchedChunk = chunk;
  1077. return true;
  1078. },
  1079. toString: function toString () {
  1080. var str = this.intro;
  1081. var chunk = this.firstChunk;
  1082. while ( chunk ) {
  1083. str += chunk.toString();
  1084. chunk = chunk.next;
  1085. }
  1086. return str + this.outro;
  1087. },
  1088. trimLines: function trimLines () {
  1089. return this.trim('[\\r\\n]');
  1090. },
  1091. trim: function trim ( charType ) {
  1092. return this.trimStart( charType ).trimEnd( charType );
  1093. },
  1094. trimEnd: function trimEnd ( charType ) {
  1095. var this$1 = this;
  1096. var rx = new RegExp( ( charType || '\\s' ) + '+$' );
  1097. this.outro = this.outro.replace( rx, '' );
  1098. if ( this.outro.length ) { return this; }
  1099. var chunk = this.lastChunk;
  1100. do {
  1101. var end = chunk.end;
  1102. var aborted = chunk.trimEnd( rx );
  1103. // if chunk was trimmed, we have a new lastChunk
  1104. if ( chunk.end !== end ) {
  1105. if ( this$1.lastChunk === chunk ) {
  1106. this$1.lastChunk = chunk.next;
  1107. }
  1108. this$1.byEnd[ chunk.end ] = chunk;
  1109. this$1.byStart[ chunk.next.start ] = chunk.next;
  1110. this$1.byEnd[ chunk.next.end ] = chunk.next;
  1111. }
  1112. if ( aborted ) { return this$1; }
  1113. chunk = chunk.previous;
  1114. } while ( chunk );
  1115. return this;
  1116. },
  1117. trimStart: function trimStart ( charType ) {
  1118. var this$1 = this;
  1119. var rx = new RegExp( '^' + ( charType || '\\s' ) + '+' );
  1120. this.intro = this.intro.replace( rx, '' );
  1121. if ( this.intro.length ) { return this; }
  1122. var chunk = this.firstChunk;
  1123. do {
  1124. var end = chunk.end;
  1125. var aborted = chunk.trimStart( rx );
  1126. if ( chunk.end !== end ) {
  1127. // special case...
  1128. if ( chunk === this$1.lastChunk ) { this$1.lastChunk = chunk.next; }
  1129. this$1.byEnd[ chunk.end ] = chunk;
  1130. this$1.byStart[ chunk.next.start ] = chunk.next;
  1131. this$1.byEnd[ chunk.next.end ] = chunk.next;
  1132. }
  1133. if ( aborted ) { return this$1; }
  1134. chunk = chunk.next;
  1135. } while ( chunk );
  1136. return this;
  1137. }
  1138. };
  1139. var hasOwnProp = Object.prototype.hasOwnProperty;
  1140. function Bundle$1 ( options ) {
  1141. if ( options === void 0 ) options = {};
  1142. this.intro = options.intro || '';
  1143. this.separator = options.separator !== undefined ? options.separator : '\n';
  1144. this.sources = [];
  1145. this.uniqueSources = [];
  1146. this.uniqueSourceIndexByFilename = {};
  1147. }
  1148. Bundle$1.prototype = {
  1149. addSource: function addSource ( source ) {
  1150. if ( source instanceof MagicString$1 ) {
  1151. return this.addSource({
  1152. content: source,
  1153. filename: source.filename,
  1154. separator: this.separator
  1155. });
  1156. }
  1157. if ( !isObject( source ) || !source.content ) {
  1158. throw new Error( 'bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`' );
  1159. }
  1160. [ 'filename', 'indentExclusionRanges', 'separator' ].forEach( function (option) {
  1161. if ( !hasOwnProp.call( source, option ) ) { source[ option ] = source.content[ option ]; }
  1162. });
  1163. if ( source.separator === undefined ) { // TODO there's a bunch of this sort of thing, needs cleaning up
  1164. source.separator = this.separator;
  1165. }
  1166. if ( source.filename ) {
  1167. if ( !hasOwnProp.call( this.uniqueSourceIndexByFilename, source.filename ) ) {
  1168. this.uniqueSourceIndexByFilename[ source.filename ] = this.uniqueSources.length;
  1169. this.uniqueSources.push({ filename: source.filename, content: source.content.original });
  1170. } else {
  1171. var uniqueSource = this.uniqueSources[ this.uniqueSourceIndexByFilename[ source.filename ] ];
  1172. if ( source.content.original !== uniqueSource.content ) {
  1173. throw new Error( ("Illegal source: same filename (" + (source.filename) + "), different contents") );
  1174. }
  1175. }
  1176. }
  1177. this.sources.push( source );
  1178. return this;
  1179. },
  1180. append: function append ( str, options ) {
  1181. this.addSource({
  1182. content: new MagicString$1( str ),
  1183. separator: ( options && options.separator ) || ''
  1184. });
  1185. return this;
  1186. },
  1187. clone: function clone () {
  1188. var bundle = new Bundle$1({
  1189. intro: this.intro,
  1190. separator: this.separator
  1191. });
  1192. this.sources.forEach( function (source) {
  1193. bundle.addSource({
  1194. filename: source.filename,
  1195. content: source.content.clone(),
  1196. separator: source.separator
  1197. });
  1198. });
  1199. return bundle;
  1200. },
  1201. generateMap: function generateMap ( options ) {
  1202. var this$1 = this;
  1203. if ( options === void 0 ) options = {};
  1204. var names = [];
  1205. this.sources.forEach( function (source) {
  1206. Object.keys( source.content.storedNames ).forEach( function (name) {
  1207. if ( !~names.indexOf( name ) ) { names.push( name ); }
  1208. });
  1209. });
  1210. var mappings = new Mappings( options.hires );
  1211. if ( this.intro ) {
  1212. mappings.advance( this.intro );
  1213. }
  1214. this.sources.forEach( function ( source, i ) {
  1215. if ( i > 0 ) {
  1216. mappings.advance( this$1.separator );
  1217. }
  1218. var sourceIndex = source.filename ? this$1.uniqueSourceIndexByFilename[ source.filename ] : -1;
  1219. var magicString = source.content;
  1220. var locate = getLocator( magicString.original );
  1221. if ( magicString.intro ) {
  1222. mappings.advance( magicString.intro );
  1223. }
  1224. magicString.firstChunk.eachNext( function (chunk) {
  1225. var loc = locate( chunk.start );
  1226. if ( chunk.intro.length ) { mappings.advance( chunk.intro ); }
  1227. if ( source.filename ) {
  1228. if ( chunk.edited ) {
  1229. mappings.addEdit( sourceIndex, chunk.content, chunk.original, loc, chunk.storeName ? names.indexOf( chunk.original ) : -1 );
  1230. } else {
  1231. mappings.addUneditedChunk( sourceIndex, chunk, magicString.original, loc, magicString.sourcemapLocations );
  1232. }
  1233. }
  1234. else {
  1235. mappings.advance( chunk.content );
  1236. }
  1237. if ( chunk.outro.length ) { mappings.advance( chunk.outro ); }
  1238. });
  1239. if ( magicString.outro ) {
  1240. mappings.advance( magicString.outro );
  1241. }
  1242. });
  1243. return new SourceMap({
  1244. file: ( options.file ? options.file.split( /[\/\\]/ ).pop() : null ),
  1245. sources: this.uniqueSources.map( function (source) {
  1246. return options.file ? getRelativePath( options.file, source.filename ) : source.filename;
  1247. }),
  1248. sourcesContent: this.uniqueSources.map( function (source) {
  1249. return options.includeContent ? source.content : null;
  1250. }),
  1251. names: names,
  1252. mappings: mappings.encode()
  1253. });
  1254. },
  1255. getIndentString: function getIndentString () {
  1256. var indentStringCounts = {};
  1257. this.sources.forEach( function (source) {
  1258. var indentStr = source.content.indentStr;
  1259. if ( indentStr === null ) { return; }
  1260. if ( !indentStringCounts[ indentStr ] ) { indentStringCounts[ indentStr ] = 0; }
  1261. indentStringCounts[ indentStr ] += 1;
  1262. });
  1263. return ( Object.keys( indentStringCounts ).sort( function ( a, b ) {
  1264. return indentStringCounts[a] - indentStringCounts[b];
  1265. })[0] ) || '\t';
  1266. },
  1267. indent: function indent ( indentStr ) {
  1268. var this$1 = this;
  1269. if ( !arguments.length ) {
  1270. indentStr = this.getIndentString();
  1271. }
  1272. if ( indentStr === '' ) { return this; } // noop
  1273. var trailingNewline = !this.intro || this.intro.slice( -1 ) === '\n';
  1274. this.sources.forEach( function ( source, i ) {
  1275. var separator = source.separator !== undefined ? source.separator : this$1.separator;
  1276. var indentStart = trailingNewline || ( i > 0 && /\r?\n$/.test( separator ) );
  1277. source.content.indent( indentStr, {
  1278. exclude: source.indentExclusionRanges,
  1279. indentStart: indentStart//: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator )
  1280. });
  1281. // TODO this is a very slow way to determine this
  1282. trailingNewline = source.content.toString().slice( 0, -1 ) === '\n';
  1283. });
  1284. if ( this.intro ) {
  1285. this.intro = indentStr + this.intro.replace( /^[^\n]/gm, function ( match, index ) {
  1286. return index > 0 ? indentStr + match : match;
  1287. });
  1288. }
  1289. return this;
  1290. },
  1291. prepend: function prepend ( str ) {
  1292. this.intro = str + this.intro;
  1293. return this;
  1294. },
  1295. toString: function toString () {
  1296. var this$1 = this;
  1297. var body = this.sources.map( function ( source, i ) {
  1298. var separator = source.separator !== undefined ? source.separator : this$1.separator;
  1299. var str = ( i > 0 ? separator : '' ) + source.content.toString();
  1300. return str;
  1301. }).join( '' );
  1302. return this.intro + body;
  1303. },
  1304. trimLines: function trimLines () {
  1305. return this.trim('[\\r\\n]');
  1306. },
  1307. trim: function trim ( charType ) {
  1308. return this.trimStart( charType ).trimEnd( charType );
  1309. },
  1310. trimStart: function trimStart ( charType ) {
  1311. var this$1 = this;
  1312. var rx = new RegExp( '^' + ( charType || '\\s' ) + '+' );
  1313. this.intro = this.intro.replace( rx, '' );
  1314. if ( !this.intro ) {
  1315. var source;
  1316. var i = 0;
  1317. do {
  1318. source = this$1.sources[i];
  1319. if ( !source ) {
  1320. break;
  1321. }
  1322. source.content.trimStart( charType );
  1323. i += 1;
  1324. } while ( source.content.toString() === '' ); // TODO faster way to determine non-empty source?
  1325. }
  1326. return this;
  1327. },
  1328. trimEnd: function trimEnd ( charType ) {
  1329. var this$1 = this;
  1330. var rx = new RegExp( ( charType || '\\s' ) + '+$' );
  1331. var source;
  1332. var i = this.sources.length - 1;
  1333. do {
  1334. source = this$1.sources[i];
  1335. if ( !source ) {
  1336. this$1.intro = this$1.intro.replace( rx, '' );
  1337. break;
  1338. }
  1339. source.content.trimEnd( charType );
  1340. i -= 1;
  1341. } while ( source.content.toString() === '' ); // TODO faster way to determine non-empty source?
  1342. return this;
  1343. }
  1344. };
  1345. // Return the first non-falsy result from an array of
  1346. // maybe-sync, maybe-promise-returning functions
  1347. function first ( candidates ) {
  1348. return function () {
  1349. var args = [], len = arguments.length;
  1350. while ( len-- ) args[ len ] = arguments[ len ];
  1351. return candidates.reduce( ( promise, candidate ) => {
  1352. return promise.then( result => result != null ?
  1353. result :
  1354. Promise.resolve( candidate.apply( void 0, args ) ) );
  1355. }, Promise.resolve() );
  1356. };
  1357. }
  1358. function find ( array, fn ) {
  1359. for ( let i = 0; i < array.length; i += 1 ) {
  1360. if ( fn( array[i], i ) ) { return array[i]; }
  1361. }
  1362. return null;
  1363. }
  1364. // Reserved word lists for various dialects of the language
  1365. var reservedWords = {
  1366. 3: "abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile",
  1367. 5: "class enum extends super const export import",
  1368. 6: "enum",
  1369. strict: "implements interface let package private protected public static yield",
  1370. strictBind: "eval arguments"
  1371. };
  1372. // And the keywords
  1373. var ecma5AndLessKeywords = "break case catch continue debugger default do else finally for function if return switch throw try var while with null true false instanceof typeof void delete new in this";
  1374. var keywords = {
  1375. 5: ecma5AndLessKeywords,
  1376. 6: ecma5AndLessKeywords + " const class extends export import super"
  1377. };
  1378. // ## Character categories
  1379. // Big ugly regular expressions that match characters in the
  1380. // whitespace, identifier, and identifier-start categories. These
  1381. // are only applied when a character is found to actually have a
  1382. // code point above 128.
  1383. // Generated by `bin/generate-identifier-regex.js`.
  1384. var 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\u0561-\u0587\u05d0-\u05ea\u05f0-\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\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\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\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\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-\u1877\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\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\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-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fd5\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7ae\ua7b0-\ua7b7\ua7f7-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\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-\uab65\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";
  1385. var 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\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08d4-\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\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\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-\u0c03\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\u0d01-\u0d03\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-\u0eb9\u0ebb\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\u1cf2-\u1cf4\u1cf8\u1cf9\u1dc0-\u1df5\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\ua900-\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";
  1386. var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1387. var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1388. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1389. // These are a run-length and offset encoded representation of the
  1390. // >0xffff code points that are a valid part of identifiers. The
  1391. // offset starts at 0x10000, and each pair of numbers represents an
  1392. // offset to the next range, and then a size of the range. They were
  1393. // generated by bin/generate-identifier-regex.js
  1394. // eslint-disable-next-line comma-spacing
  1395. var astralIdentifierStartCodes = [0,11,2,25,2,18,2,1,2,14,3,13,35,122,70,52,268,28,4,48,48,31,17,26,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,26,45,28,4,28,36,7,2,27,28,53,11,21,11,18,14,17,111,72,56,50,14,50,785,52,76,44,33,24,27,35,42,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,54,47,21,1,2,0,185,46,42,3,37,47,21,0,60,42,86,25,391,63,32,0,449,56,264,8,2,36,18,0,50,29,881,921,103,110,18,195,2749,1070,4050,582,8634,568,8,30,114,29,19,47,17,3,32,20,6,18,881,68,12,0,67,12,65,0,32,6124,20,754,9486,1,3071,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,4149,196,60,67,1213,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,10591,541];
  1396. // eslint-disable-next-line comma-spacing
  1397. var astralIdentifierCodes = [509,0,227,0,150,4,294,9,1368,2,2,1,6,3,41,2,5,0,166,1,1306,2,54,14,32,9,16,3,46,10,54,9,7,2,37,13,2,9,52,0,13,2,49,13,10,2,4,9,83,11,7,0,161,11,6,9,7,3,57,0,2,6,3,1,3,2,10,0,11,1,3,6,4,4,193,17,10,9,87,19,13,9,214,6,3,8,28,1,83,16,16,9,82,12,9,9,84,14,5,9,423,9,838,7,2,7,17,9,57,21,2,13,19882,9,135,4,60,6,26,9,1016,45,17,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,2214,6,110,6,6,9,792487,239];
  1398. // This has a complexity linear to the value of the code. The
  1399. // assumption is that looking up astral identifier characters is
  1400. // rare.
  1401. function isInAstralSet(code, set) {
  1402. var pos = 0x10000;
  1403. for (var i = 0; i < set.length; i += 2) {
  1404. pos += set[i];
  1405. if (pos > code) { return false }
  1406. pos += set[i + 1];
  1407. if (pos >= code) { return true }
  1408. }
  1409. }
  1410. // Test whether a given character code starts an identifier.
  1411. function isIdentifierStart(code, astral) {
  1412. if (code < 65) { return code === 36 }
  1413. if (code < 91) { return true }
  1414. if (code < 97) { return code === 95 }
  1415. if (code < 123) { return true }
  1416. if (code <= 0xffff) { return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code)) }
  1417. if (astral === false) { return false }
  1418. return isInAstralSet(code, astralIdentifierStartCodes)
  1419. }
  1420. // Test whether a given character is part of an identifier.
  1421. function isIdentifierChar(code, astral) {
  1422. if (code < 48) { return code === 36 }
  1423. if (code < 58) { return true }
  1424. if (code < 65) { return false }
  1425. if (code < 91) { return true }
  1426. if (code < 97) { return code === 95 }
  1427. if (code < 123) { return true }
  1428. if (code <= 0xffff) { return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code)) }
  1429. if (astral === false) { return false }
  1430. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes)
  1431. }
  1432. // ## Token types
  1433. // The assignment of fine-grained, information-carrying type objects
  1434. // allows the tokenizer to store the information it has about a
  1435. // token in a way that is very cheap for the parser to look up.
  1436. // All token type variables start with an underscore, to make them
  1437. // easy to recognize.
  1438. // The `beforeExpr` property is used to disambiguate between regular
  1439. // expressions and divisions. It is set on all token types that can
  1440. // be followed by an expression (thus, a slash after them would be a
  1441. // regular expression).
  1442. //
  1443. // The `startsExpr` property is used to check if the token ends a
  1444. // `yield` expression. It is set on all token types that either can
  1445. // directly start an expression (like a quotation mark) or can
  1446. // continue an expression (like the body of a string).
  1447. //
  1448. // `isLoop` marks a keyword as starting a loop, which is important
  1449. // to know when parsing a label, in order to allow or disallow
  1450. // continue jumps to that label.
  1451. var TokenType = function TokenType(label, conf) {
  1452. if ( conf === void 0 ) { conf = {}; }
  1453. this.label = label;
  1454. this.keyword = conf.keyword;
  1455. this.beforeExpr = !!conf.beforeExpr;
  1456. this.startsExpr = !!conf.startsExpr;
  1457. this.isLoop = !!conf.isLoop;
  1458. this.isAssign = !!conf.isAssign;
  1459. this.prefix = !!conf.prefix;
  1460. this.postfix = !!conf.postfix;
  1461. this.binop = conf.binop || null;
  1462. this.updateContext = null;
  1463. };
  1464. function binop(name, prec) {
  1465. return new TokenType(name, {beforeExpr: true, binop: prec})
  1466. }
  1467. var beforeExpr = {beforeExpr: true};
  1468. var startsExpr = {startsExpr: true};
  1469. // Map keyword names to token types.
  1470. var keywords$1 = {};
  1471. // Succinct definitions of keyword token types
  1472. function kw(name, options) {
  1473. if ( options === void 0 ) { options = {}; }
  1474. options.keyword = name;
  1475. return keywords$1[name] = new TokenType(name, options)
  1476. }
  1477. var types = {
  1478. num: new TokenType("num", startsExpr),
  1479. regexp: new TokenType("regexp", startsExpr),
  1480. string: new TokenType("string", startsExpr),
  1481. name: new TokenType("name", startsExpr),
  1482. eof: new TokenType("eof"),
  1483. // Punctuation token types.
  1484. bracketL: new TokenType("[", {beforeExpr: true, startsExpr: true}),
  1485. bracketR: new TokenType("]"),
  1486. braceL: new TokenType("{", {beforeExpr: true, startsExpr: true}),
  1487. braceR: new TokenType("}"),
  1488. parenL: new TokenType("(", {beforeExpr: true, startsExpr: true}),
  1489. parenR: new TokenType(")"),
  1490. comma: new TokenType(",", beforeExpr),
  1491. semi: new TokenType(";", beforeExpr),
  1492. colon: new TokenType(":", beforeExpr),
  1493. dot: new TokenType("."),
  1494. question: new TokenType("?", beforeExpr),
  1495. arrow: new TokenType("=>", beforeExpr),
  1496. template: new TokenType("template"),
  1497. invalidTemplate: new TokenType("invalidTemplate"),
  1498. ellipsis: new TokenType("...", beforeExpr),
  1499. backQuote: new TokenType("`", startsExpr),
  1500. dollarBraceL: new TokenType("${", {beforeExpr: true, startsExpr: true}),
  1501. // Operators. These carry several kinds of properties to help the
  1502. // parser use them properly (the presence of these properties is
  1503. // what categorizes them as operators).
  1504. //
  1505. // `binop`, when present, specifies that this operator is a binary
  1506. // operator, and will refer to its precedence.
  1507. //
  1508. // `prefix` and `postfix` mark the operator as a prefix or postfix
  1509. // unary operator.
  1510. //
  1511. // `isAssign` marks all of `=`, `+=`, `-=` etcetera, which act as
  1512. // binary operators with a very low precedence, that should result
  1513. // in AssignmentExpression nodes.
  1514. eq: new TokenType("=", {beforeExpr: true, isAssign: true}),
  1515. assign: new TokenType("_=", {beforeExpr: true, isAssign: true}),
  1516. incDec: new TokenType("++/--", {prefix: true, postfix: true, startsExpr: true}),
  1517. prefix: new TokenType("!/~", {beforeExpr: true, prefix: true, startsExpr: true}),
  1518. logicalOR: binop("||", 1),
  1519. logicalAND: binop("&&", 2),
  1520. bitwiseOR: binop("|", 3),
  1521. bitwiseXOR: binop("^", 4),
  1522. bitwiseAND: binop("&", 5),
  1523. equality: binop("==/!=/===/!==", 6),
  1524. relational: binop("</>/<=/>=", 7),
  1525. bitShift: binop("<</>>/>>>", 8),
  1526. plusMin: new TokenType("+/-", {beforeExpr: true, binop: 9, prefix: true, startsExpr: true}),
  1527. modulo: binop("%", 10),
  1528. star: binop("*", 10),
  1529. slash: binop("/", 10),
  1530. starstar: new TokenType("**", {beforeExpr: true}),
  1531. // Keyword token types.
  1532. _break: kw("break"),
  1533. _case: kw("case", beforeExpr),
  1534. _catch: kw("catch"),
  1535. _continue: kw("continue"),
  1536. _debugger: kw("debugger"),
  1537. _default: kw("default", beforeExpr),
  1538. _do: kw("do", {isLoop: true, beforeExpr: true}),
  1539. _else: kw("else", beforeExpr),
  1540. _finally: kw("finally"),
  1541. _for: kw("for", {isLoop: true}),
  1542. _function: kw("function", startsExpr),
  1543. _if: kw("if"),
  1544. _return: kw("return", beforeExpr),
  1545. _switch: kw("switch"),
  1546. _throw: kw("throw", beforeExpr),
  1547. _try: kw("try"),
  1548. _var: kw("var"),
  1549. _const: kw("const"),
  1550. _while: kw("while", {isLoop: true}),
  1551. _with: kw("with"),
  1552. _new: kw("new", {beforeExpr: true, startsExpr: true}),
  1553. _this: kw("this", startsExpr),
  1554. _super: kw("super", startsExpr),
  1555. _class: kw("class", startsExpr),
  1556. _extends: kw("extends", beforeExpr),
  1557. _export: kw("export"),
  1558. _import: kw("import"),
  1559. _null: kw("null", startsExpr),
  1560. _true: kw("true", startsExpr),
  1561. _false: kw("false", startsExpr),
  1562. _in: kw("in", {beforeExpr: true, binop: 7}),
  1563. _instanceof: kw("instanceof", {beforeExpr: true, binop: 7}),
  1564. _typeof: kw("typeof", {beforeExpr: true, prefix: true, startsExpr: true}),
  1565. _void: kw("void", {beforeExpr: true, prefix: true, startsExpr: true}),
  1566. _delete: kw("delete", {beforeExpr: true, prefix: true, startsExpr: true})
  1567. };
  1568. // Matches a whole line break (where CRLF is considered a single
  1569. // line break). Used to count lines.
  1570. var lineBreak = /\r\n?|\n|\u2028|\u2029/;
  1571. var lineBreakG = new RegExp(lineBreak.source, "g");
  1572. function isNewLine(code) {
  1573. return code === 10 || code === 13 || code === 0x2028 || code === 0x2029
  1574. }
  1575. var nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/;
  1576. var skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  1577. var ref = Object.prototype;
  1578. var hasOwnProperty = ref.hasOwnProperty;
  1579. var toString = ref.toString;
  1580. // Checks if an object has a property.
  1581. function has(obj, propName) {
  1582. return hasOwnProperty.call(obj, propName)
  1583. }
  1584. var isArray = Array.isArray || (function (obj) { return (
  1585. toString.call(obj) === "[object Array]"
  1586. ); });
  1587. // These are used when `options.locations` is on, for the
  1588. // `startLoc` and `endLoc` properties.
  1589. var Position = function Position(line, col) {
  1590. this.line = line;
  1591. this.column = col;
  1592. };
  1593. Position.prototype.offset = function offset (n) {
  1594. return new Position(this.line, this.column + n)
  1595. };
  1596. var SourceLocation = function SourceLocation(p, start, end) {
  1597. this.start = start;
  1598. this.end = end;
  1599. if (p.sourceFile !== null) { this.source = p.sourceFile; }
  1600. };
  1601. // The `getLineInfo` function is mostly useful when the
  1602. // `locations` option is off (for performance reasons) and you
  1603. // want to find the line/column position for a given character
  1604. // offset. `input` should be the code string that the offset refers
  1605. // into.
  1606. function getLineInfo(input, offset) {
  1607. for (var line = 1, cur = 0;;) {
  1608. lineBreakG.lastIndex = cur;
  1609. var match = lineBreakG.exec(input);
  1610. if (match && match.index < offset) {
  1611. ++line;
  1612. cur = match.index + match[0].length;
  1613. } else {
  1614. return new Position(line, offset - cur)
  1615. }
  1616. }
  1617. }
  1618. // A second optional argument can be given to further configure
  1619. // the parser process. These options are recognized:
  1620. var defaultOptions = {
  1621. // `ecmaVersion` indicates the ECMAScript version to parse. Must
  1622. // be either 3, 5, 6 (2015), 7 (2016), or 8 (2017). This influences support
  1623. // for strict mode, the set of reserved words, and support for
  1624. // new syntax features. The default is 7.
  1625. ecmaVersion: 7,
  1626. // `sourceType` indicates the mode the code should be parsed in.
  1627. // Can be either `"script"` or `"module"`. This influences global
  1628. // strict mode and parsing of `import` and `export` declarations.
  1629. sourceType: "script",
  1630. // `onInsertedSemicolon` can be a callback that will be called
  1631. // when a semicolon is automatically inserted. It will be passed
  1632. // th position of the comma as an offset, and if `locations` is
  1633. // enabled, it is given the location as a `{line, column}` object
  1634. // as second argument.
  1635. onInsertedSemicolon: null,
  1636. // `onTrailingComma` is similar to `onInsertedSemicolon`, but for
  1637. // trailing commas.
  1638. onTrailingComma: null,
  1639. // By default, reserved words are only enforced if ecmaVersion >= 5.
  1640. // Set `allowReserved` to a boolean value to explicitly turn this on
  1641. // an off. When this option has the value "never", reserved words
  1642. // and keywords can also not be used as property names.
  1643. allowReserved: null,
  1644. // When enabled, a return at the top level is not considered an
  1645. // error.
  1646. allowReturnOutsideFunction: false,
  1647. // When enabled, import/export statements are not constrained to
  1648. // appearing at the top of the program.
  1649. allowImportExportEverywhere: false,
  1650. // When enabled, hashbang directive in the beginning of file
  1651. // is allowed and treated as a line comment.
  1652. allowHashBang: false,
  1653. // When `locations` is on, `loc` properties holding objects with
  1654. // `start` and `end` properties in `{line, column}` form (with
  1655. // line being 1-based and column 0-based) will be attached to the
  1656. // nodes.
  1657. locations: false,
  1658. // A function can be passed as `onToken` option, which will
  1659. // cause Acorn to call that function with object in the same
  1660. // format as tokens returned from `tokenizer().getToken()`. Note
  1661. // that you are not allowed to call the parser from the
  1662. // callback—that will corrupt its internal state.
  1663. onToken: null,
  1664. // A function can be passed as `onComment` option, which will
  1665. // cause Acorn to call that function with `(block, text, start,
  1666. // end)` parameters whenever a comment is skipped. `block` is a
  1667. // boolean indicating whether this is a block (`/* */`) comment,
  1668. // `text` is the content of the comment, and `start` and `end` are
  1669. // character offsets that denote the start and end of the comment.
  1670. // When the `locations` option is on, two more parameters are
  1671. // passed, the full `{line, column}` locations of the start and
  1672. // end of the comments. Note that you are not allowed to call the
  1673. // parser from the callback—that will corrupt its internal state.
  1674. onComment: null,
  1675. // Nodes have their start and end characters offsets recorded in
  1676. // `start` and `end` properties (directly on the node, rather than
  1677. // the `loc` object, which holds line/column data. To also add a
  1678. // [semi-standardized][range] `range` property holding a `[start,
  1679. // end]` array with the same numbers, set the `ranges` option to
  1680. // `true`.
  1681. //
  1682. // [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
  1683. ranges: false,
  1684. // It is possible to parse multiple files into a single AST by
  1685. // passing the tree produced by parsing the first file as
  1686. // `program` option in subsequent parses. This will add the
  1687. // toplevel forms of the parsed file to the `Program` (top) node
  1688. // of an existing parse tree.
  1689. program: null,
  1690. // When `locations` is on, you can pass this to record the source
  1691. // file in every node's `loc` object.
  1692. sourceFile: null,
  1693. // This value, if given, is stored in every node, whether
  1694. // `locations` is on or off.
  1695. directSourceFile: null,
  1696. // When enabled, parenthesized expressions are represented by
  1697. // (non-standard) ParenthesizedExpression nodes
  1698. preserveParens: false,
  1699. plugins: {}
  1700. };
  1701. // Interpret and default an options object
  1702. function getOptions(opts) {
  1703. var options = {};
  1704. for (var opt in defaultOptions)
  1705. { options[opt] = opts && has(opts, opt) ? opts[opt] : defaultOptions[opt]; }
  1706. if (options.ecmaVersion >= 2015)
  1707. { options.ecmaVersion -= 2009; }
  1708. if (options.allowReserved == null)
  1709. { options.allowReserved = options.ecmaVersion < 5; }
  1710. if (isArray(options.onToken)) {
  1711. var tokens = options.onToken;
  1712. options.onToken = function (token) { return tokens.push(token); };
  1713. }
  1714. if (isArray(options.onComment))
  1715. { options.onComment = pushComment(options, options.onComment); }
  1716. return options
  1717. }
  1718. function pushComment(options, array) {
  1719. return function(block, text, start, end, startLoc, endLoc) {
  1720. var comment = {
  1721. type: block ? "Block" : "Line",
  1722. value: text,
  1723. start: start,
  1724. end: end
  1725. };
  1726. if (options.locations)
  1727. { comment.loc = new SourceLocation(this, startLoc, endLoc); }
  1728. if (options.ranges)
  1729. { comment.range = [start, end]; }
  1730. array.push(comment);
  1731. }
  1732. }
  1733. // Registered plugins
  1734. var plugins = {};
  1735. function keywordRegexp(words) {
  1736. return new RegExp("^(?:" + words.replace(/ /g, "|") + ")$")
  1737. }
  1738. var Parser = function Parser(options, input, startPos) {
  1739. this.options = options = getOptions(options);
  1740. this.sourceFile = options.sourceFile;
  1741. this.keywords = keywordRegexp(keywords[options.ecmaVersion >= 6 ? 6 : 5]);
  1742. var reserved = "";
  1743. if (!options.allowReserved) {
  1744. for (var v = options.ecmaVersion;; v--)
  1745. { if (reserved = reservedWords[v]) { break } }
  1746. if (options.sourceType == "module") { reserved += " await"; }
  1747. }
  1748. this.reservedWords = keywordRegexp(reserved);
  1749. var reservedStrict = (reserved ? reserved + " " : "") + reservedWords.strict;
  1750. this.reservedWordsStrict = keywordRegexp(reservedStrict);
  1751. this.reservedWordsStrictBind = keywordRegexp(reservedStrict + " " + reservedWords.strictBind);
  1752. this.input = String(input);
  1753. // Used to signal to callers of `readWord1` whether the word
  1754. // contained any escape sequences. This is needed because words with
  1755. // escape sequences must not be interpreted as keywords.
  1756. this.containsEsc = false;
  1757. // Load plugins
  1758. this.loadPlugins(options.plugins);
  1759. // Set up token state
  1760. // The current position of the tokenizer in the input.
  1761. if (startPos) {
  1762. this.pos = startPos;
  1763. this.lineStart = this.input.lastIndexOf("\n", startPos - 1) + 1;
  1764. this.curLine = this.input.slice(0, this.lineStart).split(lineBreak).length;
  1765. } else {
  1766. this.pos = this.lineStart = 0;
  1767. this.curLine = 1;
  1768. }
  1769. // Properties of the current token:
  1770. // Its type
  1771. this.type = types.eof;
  1772. // For tokens that include more information than their type, the value
  1773. this.value = null;
  1774. // Its start and end offset
  1775. this.start = this.end = this.pos;
  1776. // And, if locations are used, the {line, column} object
  1777. // corresponding to those offsets
  1778. this.startLoc = this.endLoc = this.curPosition();
  1779. // Position information for the previous token
  1780. this.lastTokEndLoc = this.lastTokStartLoc = null;
  1781. this.lastTokStart = this.lastTokEnd = this.pos;
  1782. // The context stack is used to superficially track syntactic
  1783. // context to predict whether a regular expression is allowed in a
  1784. // given position.
  1785. this.context = this.initialContext();
  1786. this.exprAllowed = true;
  1787. // Figure out if it's a module code.
  1788. this.inModule = options.sourceType === "module";
  1789. this.strict = this.inModule || this.strictDirective(this.pos);
  1790. // Used to signify the start of a potential arrow function
  1791. this.potentialArrowAt = -1;
  1792. // Flags to track whether we are in a function, a generator, an async function.
  1793. this.inFunction = this.inGenerator = this.inAsync = false;
  1794. // Positions to delayed-check that yield/await does not exist in default parameters.
  1795. this.yieldPos = this.awaitPos = 0;
  1796. // Labels in scope.
  1797. this.labels = [];
  1798. // If enabled, skip leading hashbang line.
  1799. if (this.pos === 0 && options.allowHashBang && this.input.slice(0, 2) === "#!")
  1800. { this.skipLineComment(2); }
  1801. // Scope tracking for duplicate variable names (see scope.js)
  1802. this.scopeStack = [];
  1803. this.enterFunctionScope();
  1804. };
  1805. // DEPRECATED Kept for backwards compatibility until 3.0 in case a plugin uses them
  1806. Parser.prototype.isKeyword = function isKeyword (word) { return this.keywords.test(word) };
  1807. Parser.prototype.isReservedWord = function isReservedWord (word) { return this.reservedWords.test(word) };
  1808. Parser.prototype.extend = function extend (name, f) {
  1809. this[name] = f(this[name]);
  1810. };
  1811. Parser.prototype.loadPlugins = function loadPlugins (pluginConfigs) {
  1812. var this$1 = this;
  1813. for (var name in pluginConfigs) {
  1814. var plugin = plugins[name];
  1815. if (!plugin) { throw new Error("Plugin '" + name + "' not found") }
  1816. plugin(this$1, pluginConfigs[name]);
  1817. }
  1818. };
  1819. Parser.prototype.parse = function parse () {
  1820. var node = this.options.program || this.startNode();
  1821. this.nextToken();
  1822. return this.parseTopLevel(node)
  1823. };
  1824. var pp = Parser.prototype;
  1825. // ## Parser utilities
  1826. var literal = /^(?:'((?:\\.|[^'])*?)'|"((?:\\.|[^"])*?)"|;)/;
  1827. pp.strictDirective = function(start) {
  1828. var this$1 = this;
  1829. for (;;) {
  1830. skipWhiteSpace.lastIndex = start;
  1831. start += skipWhiteSpace.exec(this$1.input)[0].length;
  1832. var match = literal.exec(this$1.input.slice(start));
  1833. if (!match) { return false }
  1834. if ((match[1] || match[2]) == "use strict") { return true }
  1835. start += match[0].length;
  1836. }
  1837. };
  1838. // Predicate that tests whether the next token is of the given
  1839. // type, and if yes, consumes it as a side effect.
  1840. pp.eat = function(type) {
  1841. if (this.type === type) {
  1842. this.next();
  1843. return true
  1844. } else {
  1845. return false
  1846. }
  1847. };
  1848. // Tests whether parsed token is a contextual keyword.
  1849. pp.isContextual = function(name) {
  1850. return this.type === types.name && this.value === name
  1851. };
  1852. // Consumes contextual keyword if possible.
  1853. pp.eatContextual = function(name) {
  1854. return this.value === name && this.eat(types.name)
  1855. };
  1856. // Asserts that following token is given contextual keyword.
  1857. pp.expectContextual = function(name) {
  1858. if (!this.eatContextual(name)) { this.unexpected(); }
  1859. };
  1860. // Test whether a semicolon can be inserted at the current position.
  1861. pp.canInsertSemicolon = function() {
  1862. return this.type === types.eof ||
  1863. this.type === types.braceR ||
  1864. lineBreak.test(this.input.slice(this.lastTokEnd, this.start))
  1865. };
  1866. pp.insertSemicolon = function() {
  1867. if (this.canInsertSemicolon()) {
  1868. if (this.options.onInsertedSemicolon)
  1869. { this.options.onInsertedSemicolon(this.lastTokEnd, this.lastTokEndLoc); }
  1870. return true
  1871. }
  1872. };
  1873. // Consume a semicolon, or, failing that, see if we are allowed to
  1874. // pretend that there is a semicolon at this position.
  1875. pp.semicolon = function() {
  1876. if (!this.eat(types.semi) && !this.insertSemicolon()) { this.unexpected(); }
  1877. };
  1878. pp.afterTrailingComma = function(tokType, notNext) {
  1879. if (this.type == tokType) {
  1880. if (this.options.onTrailingComma)
  1881. { this.options.onTrailingComma(this.lastTokStart, this.lastTokStartLoc); }
  1882. if (!notNext)
  1883. { this.next(); }
  1884. return true
  1885. }
  1886. };
  1887. // Expect a token of a given type. If found, consume it, otherwise,
  1888. // raise an unexpected token error.
  1889. pp.expect = function(type) {
  1890. this.eat(type) || this.unexpected();
  1891. };
  1892. // Raise an unexpected token error.
  1893. pp.unexpected = function(pos) {
  1894. this.raise(pos != null ? pos : this.start, "Unexpected token");
  1895. };
  1896. function DestructuringErrors() {
  1897. this.shorthandAssign =
  1898. this.trailingComma =
  1899. this.parenthesizedAssign =
  1900. this.parenthesizedBind =
  1901. -1;
  1902. }
  1903. pp.checkPatternErrors = function(refDestructuringErrors, isAssign) {
  1904. if (!refDestructuringErrors) { return }
  1905. if (refDestructuringErrors.trailingComma > -1)
  1906. { this.raiseRecoverable(refDestructuringErrors.trailingComma, "Comma is not permitted after the rest element"); }
  1907. var parens = isAssign ? refDestructuringErrors.parenthesizedAssign : refDestructuringErrors.parenthesizedBind;
  1908. if (parens > -1) { this.raiseRecoverable(parens, "Parenthesized pattern"); }
  1909. };
  1910. pp.checkExpressionErrors = function(refDestructuringErrors, andThrow) {
  1911. var pos = refDestructuringErrors ? refDestructuringErrors.shorthandAssign : -1;
  1912. if (!andThrow) { return pos >= 0 }
  1913. if (pos > -1) { this.raise(pos, "Shorthand property assignments are valid only in destructuring patterns"); }
  1914. };
  1915. pp.checkYieldAwaitInDefaultParams = function() {
  1916. if (this.yieldPos && (!this.awaitPos || this.yieldPos < this.awaitPos))
  1917. { this.raise(this.yieldPos, "Yield expression cannot be a default value"); }
  1918. if (this.awaitPos)
  1919. { this.raise(this.awaitPos, "Await expression cannot be a default value"); }
  1920. };
  1921. pp.isSimpleAssignTarget = function(expr) {
  1922. if (expr.type === "ParenthesizedExpression")
  1923. { return this.isSimpleAssignTarget(expr.expression) }
  1924. return expr.type === "Identifier" || expr.type === "MemberExpression"
  1925. };
  1926. var pp$1 = Parser.prototype;
  1927. // ### Statement parsing
  1928. // Parse a program. Initializes the parser, reads any number of
  1929. // statements, and wraps them in a Program node. Optionally takes a
  1930. // `program` argument. If present, the statements will be appended
  1931. // to its body instead of creating a new node.
  1932. pp$1.parseTopLevel = function(node) {
  1933. var this$1 = this;
  1934. var exports = {};
  1935. if (!node.body) { node.body = []; }
  1936. while (this.type !== types.eof) {
  1937. var stmt = this$1.parseStatement(true, true, exports);
  1938. node.body.push(stmt);
  1939. }
  1940. this.next();
  1941. if (this.options.ecmaVersion >= 6) {
  1942. node.sourceType = this.options.sourceType;
  1943. }
  1944. return this.finishNode(node, "Program")
  1945. };
  1946. var loopLabel = {kind: "loop"};
  1947. var switchLabel = {kind: "switch"};
  1948. pp$1.isLet = function() {
  1949. if (this.type !== types.name || this.options.ecmaVersion < 6 || this.value != "let") { return false }
  1950. skipWhiteSpace.lastIndex = this.pos;
  1951. var skip = skipWhiteSpace.exec(this.input);
  1952. var next = this.pos + skip[0].length, nextCh = this.input.charCodeAt(next);
  1953. if (nextCh === 91 || nextCh == 123) { return true } // '{' and '['
  1954. if (isIdentifierStart(nextCh, true)) {
  1955. var pos = next + 1;
  1956. while (isIdentifierChar(this.input.charCodeAt(pos), true)) { ++pos; }
  1957. var ident = this.input.slice(next, pos);
  1958. if (!this.isKeyword(ident)) { return true }
  1959. }
  1960. return false
  1961. };
  1962. // check 'async [no LineTerminator here] function'
  1963. // - 'async /*foo*/ function' is OK.
  1964. // - 'async /*\n*/ function' is invalid.
  1965. pp$1.isAsyncFunction = function() {
  1966. if (this.type !== types.name || this.options.ecmaVersion < 8 || this.value != "async")
  1967. { return false }
  1968. skipWhiteSpace.lastIndex = this.pos;
  1969. var skip = skipWhiteSpace.exec(this.input);
  1970. var next = this.pos + skip[0].length;
  1971. return !lineBreak.test(this.input.slice(this.pos, next)) &&
  1972. this.input.slice(next, next + 8) === "function" &&
  1973. (next + 8 == this.input.length || !isIdentifierChar(this.input.charAt(next + 8)))
  1974. };
  1975. // Parse a single statement.
  1976. //
  1977. // If expecting a statement and finding a slash operator, parse a
  1978. // regular expression literal. This is to handle cases like
  1979. // `if (foo) /blah/.exec(foo)`, where looking at the previous token
  1980. // does not help.
  1981. pp$1.parseStatement = function(declaration, topLevel, exports) {
  1982. var starttype = this.type, node = this.startNode(), kind;
  1983. if (this.isLet()) {
  1984. starttype = types._var;
  1985. kind = "let";
  1986. }
  1987. // Most types of statements are recognized by the keyword they
  1988. // start with. Many are trivial to parse, some require a bit of
  1989. // complexity.
  1990. switch (starttype) {
  1991. case types._break: case types._continue: return this.parseBreakContinueStatement(node, starttype.keyword)
  1992. case types._debugger: return this.parseDebuggerStatement(node)
  1993. case types._do: return this.parseDoStatement(node)
  1994. case types._for: return this.parseForStatement(node)
  1995. case types._function:
  1996. if (!declaration && this.options.ecmaVersion >= 6) { this.unexpected(); }
  1997. return this.parseFunctionStatement(node, false)
  1998. case types._class:
  1999. if (!declaration) { this.unexpected(); }
  2000. return this.parseClass(node, true)
  2001. case types._if: return this.parseIfStatement(node)
  2002. case types._return: return this.parseReturnStatement(node)
  2003. case types._switch: return this.parseSwitchStatement(node)
  2004. case types._throw: return this.parseThrowStatement(node)
  2005. case types._try: return this.parseTryStatement(node)
  2006. case types._const: case types._var:
  2007. kind = kind || this.value;
  2008. if (!declaration && kind != "var") { this.unexpected(); }
  2009. return this.parseVarStatement(node, kind)
  2010. case types._while: return this.parseWhileStatement(node)
  2011. case types._with: return this.parseWithStatement(node)
  2012. case types.braceL: return this.parseBlock()
  2013. case types.semi: return this.parseEmptyStatement(node)
  2014. case types._export:
  2015. case types._import:
  2016. if (!this.options.allowImportExportEverywhere) {
  2017. if (!topLevel)
  2018. { this.raise(this.start, "'import' and 'export' may only appear at the top level"); }
  2019. if (!this.inModule)
  2020. { this.raise(this.start, "'import' and 'export' may appear only with 'sourceType: module'"); }
  2021. }
  2022. return starttype === types._import ? this.parseImport(node) : this.parseExport(node, exports)
  2023. // If the statement does not start with a statement keyword or a
  2024. // brace, it's an ExpressionStatement or LabeledStatement. We
  2025. // simply start parsing an expression, and afterwards, if the
  2026. // next token is a colon and the expression was a simple
  2027. // Identifier node, we switch to interpreting it as a label.
  2028. default:
  2029. if (this.isAsyncFunction() && declaration) {
  2030. this.next();
  2031. return this.parseFunctionStatement(node, true)
  2032. }
  2033. var maybeName = this.value, expr = this.parseExpression();
  2034. if (starttype === types.name && expr.type === "Identifier" && this.eat(types.colon))
  2035. { return this.parseLabeledStatement(node, maybeName, expr) }
  2036. else { return this.parseExpressionStatement(node, expr) }
  2037. }
  2038. };
  2039. pp$1.parseBreakContinueStatement = function(node, keyword) {
  2040. var this$1 = this;
  2041. var isBreak = keyword == "break";
  2042. this.next();
  2043. if (this.eat(types.semi) || this.insertSemicolon()) { node.label = null; }
  2044. else if (this.type !== types.name) { this.unexpected(); }
  2045. else {
  2046. node.label = this.parseIdent();
  2047. this.semicolon();
  2048. }
  2049. // Verify that there is an actual destination to break or
  2050. // continue to.
  2051. var i = 0;
  2052. for (; i < this.labels.length; ++i) {
  2053. var lab = this$1.labels[i];
  2054. if (node.label == null || lab.name === node.label.name) {
  2055. if (lab.kind != null && (isBreak || lab.kind === "loop")) { break }
  2056. if (node.label && isBreak) { break }
  2057. }
  2058. }
  2059. if (i === this.labels.length) { this.raise(node.start, "Unsyntactic " + keyword); }
  2060. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement")
  2061. };
  2062. pp$1.parseDebuggerStatement = function(node) {
  2063. this.next();
  2064. this.semicolon();
  2065. return this.finishNode(node, "DebuggerStatement")
  2066. };
  2067. pp$1.parseDoStatement = function(node) {
  2068. this.next();
  2069. this.labels.push(loopLabel);
  2070. node.body = this.parseStatement(false);
  2071. this.labels.pop();
  2072. this.expect(types._while);
  2073. node.test = this.parseParenExpression();
  2074. if (this.options.ecmaVersion >= 6)
  2075. { this.eat(types.semi); }
  2076. else
  2077. { this.semicolon(); }
  2078. return this.finishNode(node, "DoWhileStatement")
  2079. };
  2080. // Disambiguating between a `for` and a `for`/`in` or `for`/`of`
  2081. // loop is non-trivial. Basically, we have to parse the init `var`
  2082. // statement or expression, disallowing the `in` operator (see
  2083. // the second parameter to `parseExpression`), and then check
  2084. // whether the next token is `in` or `of`. When there is no init
  2085. // part (semicolon immediately after the opening parenthesis), it
  2086. // is a regular `for` loop.
  2087. pp$1.parseForStatement = function(node) {
  2088. this.next();
  2089. this.labels.push(loopLabel);
  2090. this.enterLexicalScope();
  2091. this.expect(types.parenL);
  2092. if (this.type === types.semi) { return this.parseFor(node, null) }
  2093. var isLet = this.isLet();
  2094. if (this.type === types._var || this.type === types._const || isLet) {
  2095. var init$1 = this.startNode(), kind = isLet ? "let" : this.value;
  2096. this.next();
  2097. this.parseVar(init$1, true, kind);
  2098. this.finishNode(init$1, "VariableDeclaration");
  2099. if ((this.type === types._in || (this.options.ecmaVersion >= 6 && this.isContextual("of"))) && init$1.declarations.length === 1 &&
  2100. !(kind !== "var" && init$1.declarations[0].init))
  2101. { return this.parseForIn(node, init$1) }
  2102. return this.parseFor(node, init$1)
  2103. }
  2104. var refDestructuringErrors = new DestructuringErrors;
  2105. var init = this.parseExpression(true, refDestructuringErrors);
  2106. if (this.type === types._in || (this.options.ecmaVersion >= 6 && this.isContextual("of"))) {
  2107. this.toAssignable(init);
  2108. this.checkLVal(init);
  2109. this.checkPatternErrors(refDestructuringErrors, true);
  2110. return this.parseForIn(node, init)
  2111. } else {
  2112. this.checkExpressionErrors(refDestructuringErrors, true);
  2113. }
  2114. return this.parseFor(node, init)
  2115. };
  2116. pp$1.parseFunctionStatement = function(node, isAsync) {
  2117. this.next();
  2118. return this.parseFunction(node, true, false, isAsync)
  2119. };
  2120. pp$1.isFunction = function() {
  2121. return this.type === types._function || this.isAsyncFunction()
  2122. };
  2123. pp$1.parseIfStatement = function(node) {
  2124. this.next();
  2125. node.test = this.parseParenExpression();
  2126. // allow function declarations in branches, but only in non-strict mode
  2127. node.consequent = this.parseStatement(!this.strict && this.isFunction());
  2128. node.alternate = this.eat(types._else) ? this.parseStatement(!this.strict && this.isFunction()) : null;
  2129. return this.finishNode(node, "IfStatement")
  2130. };
  2131. pp$1.parseReturnStatement = function(node) {
  2132. if (!this.inFunction && !this.options.allowReturnOutsideFunction)
  2133. { this.raise(this.start, "'return' outside of function"); }
  2134. this.next();
  2135. // In `return` (and `break`/`continue`), the keywords with
  2136. // optional arguments, we eagerly look for a semicolon or the
  2137. // possibility to insert one.
  2138. if (this.eat(types.semi) || this.insertSemicolon()) { node.argument = null; }
  2139. else { node.argument = this.parseExpression(); this.semicolon(); }
  2140. return this.finishNode(node, "ReturnStatement")
  2141. };
  2142. pp$1.parseSwitchStatement = function(node) {
  2143. var this$1 = this;
  2144. this.next();
  2145. node.discriminant = this.parseParenExpression();
  2146. node.cases = [];
  2147. this.expect(types.braceL);
  2148. this.labels.push(switchLabel);
  2149. this.enterLexicalScope();
  2150. // Statements under must be grouped (by label) in SwitchCase
  2151. // nodes. `cur` is used to keep the node that we are currently
  2152. // adding statements to.
  2153. var cur;
  2154. for (var sawDefault = false; this.type != types.braceR;) {
  2155. if (this$1.type === types._case || this$1.type === types._default) {
  2156. var isCase = this$1.type === types._case;
  2157. if (cur) { this$1.finishNode(cur, "SwitchCase"); }
  2158. node.cases.push(cur = this$1.startNode());
  2159. cur.consequent = [];
  2160. this$1.next();
  2161. if (isCase) {
  2162. cur.test = this$1.parseExpression();
  2163. } else {
  2164. if (sawDefault) { this$1.raiseRecoverable(this$1.lastTokStart, "Multiple default clauses"); }
  2165. sawDefault = true;
  2166. cur.test = null;
  2167. }
  2168. this$1.expect(types.colon);
  2169. } else {
  2170. if (!cur) { this$1.unexpected(); }
  2171. cur.consequent.push(this$1.parseStatement(true));
  2172. }
  2173. }
  2174. this.exitLexicalScope();
  2175. if (cur) { this.finishNode(cur, "SwitchCase"); }
  2176. this.next(); // Closing brace
  2177. this.labels.pop();
  2178. return this.finishNode(node, "SwitchStatement")
  2179. };
  2180. pp$1.parseThrowStatement = function(node) {
  2181. this.next();
  2182. if (lineBreak.test(this.input.slice(this.lastTokEnd, this.start)))
  2183. { this.raise(this.lastTokEnd, "Illegal newline after throw"); }
  2184. node.argument = this.parseExpression();
  2185. this.semicolon();
  2186. return this.finishNode(node, "ThrowStatement")
  2187. };
  2188. // Reused empty array added for node fields that are always empty.
  2189. var empty = [];
  2190. pp$1.parseTryStatement = function(node) {
  2191. this.next();
  2192. node.block = this.parseBlock();
  2193. node.handler = null;
  2194. if (this.type === types._catch) {
  2195. var clause = this.startNode();
  2196. this.next();
  2197. this.expect(types.parenL);
  2198. clause.param = this.parseBindingAtom();
  2199. this.enterLexicalScope();
  2200. this.checkLVal(clause.param, "let");
  2201. this.expect(types.parenR);
  2202. clause.body = this.parseBlock(false);
  2203. this.exitLexicalScope();
  2204. node.handler = this.finishNode(clause, "CatchClause");
  2205. }
  2206. node.finalizer = this.eat(types._finally) ? this.parseBlock() : null;
  2207. if (!node.handler && !node.finalizer)
  2208. { this.raise(node.start, "Missing catch or finally clause"); }
  2209. return this.finishNode(node, "TryStatement")
  2210. };
  2211. pp$1.parseVarStatement = function(node, kind) {
  2212. this.next();
  2213. this.parseVar(node, false, kind);
  2214. this.semicolon();
  2215. return this.finishNode(node, "VariableDeclaration")
  2216. };
  2217. pp$1.parseWhileStatement = function(node) {
  2218. this.next();
  2219. node.test = this.parseParenExpression();
  2220. this.labels.push(loopLabel);
  2221. node.body = this.parseStatement(false);
  2222. this.labels.pop();
  2223. return this.finishNode(node, "WhileStatement")
  2224. };
  2225. pp$1.parseWithStatement = function(node) {
  2226. if (this.strict) { this.raise(this.start, "'with' in strict mode"); }
  2227. this.next();
  2228. node.object = this.parseParenExpression();
  2229. node.body = this.parseStatement(false);
  2230. return this.finishNode(node, "WithStatement")
  2231. };
  2232. pp$1.parseEmptyStatement = function(node) {
  2233. this.next();
  2234. return this.finishNode(node, "EmptyStatement")
  2235. };
  2236. pp$1.parseLabeledStatement = function(node, maybeName, expr) {
  2237. var this$1 = this;
  2238. for (var i$1 = 0, list = this$1.labels; i$1 < list.length; i$1 += 1)
  2239. {
  2240. var label = list[i$1];
  2241. if (label.name === maybeName)
  2242. { this$1.raise(expr.start, "Label '" + maybeName + "' is already declared");
  2243. } }
  2244. var kind = this.type.isLoop ? "loop" : this.type === types._switch ? "switch" : null;
  2245. for (var i = this.labels.length - 1; i >= 0; i--) {
  2246. var label$1 = this$1.labels[i];
  2247. if (label$1.statementStart == node.start) {
  2248. label$1.statementStart = this$1.start;
  2249. label$1.kind = kind;
  2250. } else { break }
  2251. }
  2252. this.labels.push({name: maybeName, kind: kind, statementStart: this.start});
  2253. node.body = this.parseStatement(true);
  2254. if (node.body.type == "ClassDeclaration" ||
  2255. node.body.type == "VariableDeclaration" && node.body.kind != "var" ||
  2256. node.body.type == "FunctionDeclaration" && (this.strict || node.body.generator))
  2257. { this.raiseRecoverable(node.body.start, "Invalid labeled declaration"); }
  2258. this.labels.pop();
  2259. node.label = expr;
  2260. return this.finishNode(node, "LabeledStatement")
  2261. };
  2262. pp$1.parseExpressionStatement = function(node, expr) {
  2263. node.expression = expr;
  2264. this.semicolon();
  2265. return this.finishNode(node, "ExpressionStatement")
  2266. };
  2267. // Parse a semicolon-enclosed block of statements, handling `"use
  2268. // strict"` declarations when `allowStrict` is true (used for
  2269. // function bodies).
  2270. pp$1.parseBlock = function(createNewLexicalScope) {
  2271. var this$1 = this;
  2272. if ( createNewLexicalScope === void 0 ) { createNewLexicalScope = true; }
  2273. var node = this.startNode();
  2274. node.body = [];
  2275. this.expect(types.braceL);
  2276. if (createNewLexicalScope) {
  2277. this.enterLexicalScope();
  2278. }
  2279. while (!this.eat(types.braceR)) {
  2280. var stmt = this$1.parseStatement(true);
  2281. node.body.push(stmt);
  2282. }
  2283. if (createNewLexicalScope) {
  2284. this.exitLexicalScope();
  2285. }
  2286. return this.finishNode(node, "BlockStatement")
  2287. };
  2288. // Parse a regular `for` loop. The disambiguation code in
  2289. // `parseStatement` will already have parsed the init statement or
  2290. // expression.
  2291. pp$1.parseFor = function(node, init) {
  2292. node.init = init;
  2293. this.expect(types.semi);
  2294. node.test = this.type === types.semi ? null : this.parseExpression();
  2295. this.expect(types.semi);
  2296. node.update = this.type === types.parenR ? null : this.parseExpression();
  2297. this.expect(types.parenR);
  2298. this.exitLexicalScope();
  2299. node.body = this.parseStatement(false);
  2300. this.labels.pop();
  2301. return this.finishNode(node, "ForStatement")
  2302. };
  2303. // Parse a `for`/`in` and `for`/`of` loop, which are almost
  2304. // same from parser's perspective.
  2305. pp$1.parseForIn = function(node, init) {
  2306. var type = this.type === types._in ? "ForInStatement" : "ForOfStatement";
  2307. this.next();
  2308. node.left = init;
  2309. node.right = this.parseExpression();
  2310. this.expect(types.parenR);
  2311. this.exitLexicalScope();
  2312. node.body = this.parseStatement(false);
  2313. this.labels.pop();
  2314. return this.finishNode(node, type)
  2315. };
  2316. // Parse a list of variable declarations.
  2317. pp$1.parseVar = function(node, isFor, kind) {
  2318. var this$1 = this;
  2319. node.declarations = [];
  2320. node.kind = kind;
  2321. for (;;) {
  2322. var decl = this$1.startNode();
  2323. this$1.parseVarId(decl, kind);
  2324. if (this$1.eat(types.eq)) {
  2325. decl.init = this$1.parseMaybeAssign(isFor);
  2326. } else if (kind === "const" && !(this$1.type === types._in || (this$1.options.ecmaVersion >= 6 && this$1.isContextual("of")))) {
  2327. this$1.unexpected();
  2328. } else if (decl.id.type != "Identifier" && !(isFor && (this$1.type === types._in || this$1.isContextual("of")))) {
  2329. this$1.raise(this$1.lastTokEnd, "Complex binding patterns require an initialization value");
  2330. } else {
  2331. decl.init = null;
  2332. }
  2333. node.declarations.push(this$1.finishNode(decl, "VariableDeclarator"));
  2334. if (!this$1.eat(types.comma)) { break }
  2335. }
  2336. return node
  2337. };
  2338. pp$1.parseVarId = function(decl, kind) {
  2339. decl.id = this.parseBindingAtom(kind);
  2340. this.checkLVal(decl.id, kind, false);
  2341. };
  2342. // Parse a function declaration or literal (depending on the
  2343. // `isStatement` parameter).
  2344. pp$1.parseFunction = function(node, isStatement, allowExpressionBody, isAsync) {
  2345. this.initFunction(node);
  2346. if (this.options.ecmaVersion >= 6 && !isAsync)
  2347. { node.generator = this.eat(types.star); }
  2348. if (this.options.ecmaVersion >= 8)
  2349. { node.async = !!isAsync; }
  2350. if (isStatement) {
  2351. node.id = isStatement === "nullableID" && this.type != types.name ? null : this.parseIdent();
  2352. if (node.id) {
  2353. this.checkLVal(node.id, "var");
  2354. }
  2355. }
  2356. var oldInGen = this.inGenerator, oldInAsync = this.inAsync,
  2357. oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldInFunc = this.inFunction;
  2358. this.inGenerator = node.generator;
  2359. this.inAsync = node.async;
  2360. this.yieldPos = 0;
  2361. this.awaitPos = 0;
  2362. this.inFunction = true;
  2363. this.enterFunctionScope();
  2364. if (!isStatement)
  2365. { node.id = this.type == types.name ? this.parseIdent() : null; }
  2366. this.parseFunctionParams(node);
  2367. this.parseFunctionBody(node, allowExpressionBody);
  2368. this.inGenerator = oldInGen;
  2369. this.inAsync = oldInAsync;
  2370. this.yieldPos = oldYieldPos;
  2371. this.awaitPos = oldAwaitPos;
  2372. this.inFunction = oldInFunc;
  2373. return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression")
  2374. };
  2375. pp$1.parseFunctionParams = function(node) {
  2376. this.expect(types.parenL);
  2377. node.params = this.parseBindingList(types.parenR, false, this.options.ecmaVersion >= 8);
  2378. this.checkYieldAwaitInDefaultParams();
  2379. };
  2380. // Parse a class declaration or literal (depending on the
  2381. // `isStatement` parameter).
  2382. pp$1.parseClass = function(node, isStatement) {
  2383. var this$1 = this;
  2384. this.next();
  2385. this.parseClassId(node, isStatement);
  2386. this.parseClassSuper(node);
  2387. var classBody = this.startNode();
  2388. var hadConstructor = false;
  2389. classBody.body = [];
  2390. this.expect(types.braceL);
  2391. while (!this.eat(types.braceR)) {
  2392. if (this$1.eat(types.semi)) { continue }
  2393. var method = this$1.startNode();
  2394. var isGenerator = this$1.eat(types.star);
  2395. var isAsync = false;
  2396. var isMaybeStatic = this$1.type === types.name && this$1.value === "static";
  2397. this$1.parsePropertyName(method);
  2398. method.static = isMaybeStatic && this$1.type !== types.parenL;
  2399. if (method.static) {
  2400. if (isGenerator) { this$1.unexpected(); }
  2401. isGenerator = this$1.eat(types.star);
  2402. this$1.parsePropertyName(method);
  2403. }
  2404. if (this$1.options.ecmaVersion >= 8 && !isGenerator && !method.computed &&
  2405. method.key.type === "Identifier" && method.key.name === "async" && this$1.type !== types.parenL &&
  2406. !this$1.canInsertSemicolon()) {
  2407. isAsync = true;
  2408. this$1.parsePropertyName(method);
  2409. }
  2410. method.kind = "method";
  2411. var isGetSet = false;
  2412. if (!method.computed) {
  2413. var key = method.key;
  2414. if (!isGenerator && !isAsync && key.type === "Identifier" && this$1.type !== types.parenL && (key.name === "get" || key.name === "set")) {
  2415. isGetSet = true;
  2416. method.kind = key.name;
  2417. key = this$1.parsePropertyName(method);
  2418. }
  2419. if (!method.static && (key.type === "Identifier" && key.name === "constructor" ||
  2420. key.type === "Literal" && key.value === "constructor")) {
  2421. if (hadConstructor) { this$1.raise(key.start, "Duplicate constructor in the same class"); }
  2422. if (isGetSet) { this$1.raise(key.start, "Constructor can't have get/set modifier"); }
  2423. if (isGenerator) { this$1.raise(key.start, "Constructor can't be a generator"); }
  2424. if (isAsync) { this$1.raise(key.start, "Constructor can't be an async method"); }
  2425. method.kind = "constructor";
  2426. hadConstructor = true;
  2427. }
  2428. }
  2429. this$1.parseClassMethod(classBody, method, isGenerator, isAsync);
  2430. if (isGetSet) {
  2431. var paramCount = method.kind === "get" ? 0 : 1;
  2432. if (method.value.params.length !== paramCount) {
  2433. var start = method.value.start;
  2434. if (method.kind === "get")
  2435. { this$1.raiseRecoverable(start, "getter should have no params"); }
  2436. else
  2437. { this$1.raiseRecoverable(start, "setter should have exactly one param"); }
  2438. } else {
  2439. if (method.kind === "set" && method.value.params[0].type === "RestElement")
  2440. { this$1.raiseRecoverable(method.value.params[0].start, "Setter cannot use rest params"); }
  2441. }
  2442. }
  2443. }
  2444. node.body = this.finishNode(classBody, "ClassBody");
  2445. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression")
  2446. };
  2447. pp$1.parseClassMethod = function(classBody, method, isGenerator, isAsync) {
  2448. method.value = this.parseMethod(isGenerator, isAsync);
  2449. classBody.body.push(this.finishNode(method, "MethodDefinition"));
  2450. };
  2451. pp$1.parseClassId = function(node, isStatement) {
  2452. node.id = this.type === types.name ? this.parseIdent() : isStatement === true ? this.unexpected() : null;
  2453. };
  2454. pp$1.parseClassSuper = function(node) {
  2455. node.superClass = this.eat(types._extends) ? this.parseExprSubscripts() : null;
  2456. };
  2457. // Parses module export declaration.
  2458. pp$1.parseExport = function(node, exports) {
  2459. var this$1 = this;
  2460. this.next();
  2461. // export * from '...'
  2462. if (this.eat(types.star)) {
  2463. this.expectContextual("from");
  2464. node.source = this.type === types.string ? this.parseExprAtom() : this.unexpected();
  2465. this.semicolon();
  2466. return this.finishNode(node, "ExportAllDeclaration")
  2467. }
  2468. if (this.eat(types._default)) { // export default ...
  2469. this.checkExport(exports, "default", this.lastTokStart);
  2470. var isAsync;
  2471. if (this.type === types._function || (isAsync = this.isAsyncFunction())) {
  2472. var fNode = this.startNode();
  2473. this.next();
  2474. if (isAsync) { this.next(); }
  2475. node.declaration = this.parseFunction(fNode, "nullableID", false, isAsync);
  2476. } else if (this.type === types._class) {
  2477. var cNode = this.startNode();
  2478. node.declaration = this.parseClass(cNode, "nullableID");
  2479. } else {
  2480. node.declaration = this.parseMaybeAssign();
  2481. this.semicolon();
  2482. }
  2483. return this.finishNode(node, "ExportDefaultDeclaration")
  2484. }
  2485. // export var|const|let|function|class ...
  2486. if (this.shouldParseExportStatement()) {
  2487. node.declaration = this.parseStatement(true);
  2488. if (node.declaration.type === "VariableDeclaration")
  2489. { this.checkVariableExport(exports, node.declaration.declarations); }
  2490. else
  2491. { this.checkExport(exports, node.declaration.id.name, node.declaration.id.start); }
  2492. node.specifiers = [];
  2493. node.source = null;
  2494. } else { // export { x, y as z } [from '...']
  2495. node.declaration = null;
  2496. node.specifiers = this.parseExportSpecifiers(exports);
  2497. if (this.eatContextual("from")) {
  2498. node.source = this.type === types.string ? this.parseExprAtom() : this.unexpected();
  2499. } else {
  2500. // check for keywords used as local names
  2501. for (var i = 0, list = node.specifiers; i < list.length; i += 1) {
  2502. var spec = list[i];
  2503. this$1.checkUnreserved(spec.local);
  2504. }
  2505. node.source = null;
  2506. }
  2507. this.semicolon();
  2508. }
  2509. return this.finishNode(node, "ExportNamedDeclaration")
  2510. };
  2511. pp$1.checkExport = function(exports, name, pos) {
  2512. if (!exports) { return }
  2513. if (has(exports, name))
  2514. { this.raiseRecoverable(pos, "Duplicate export '" + name + "'"); }
  2515. exports[name] = true;
  2516. };
  2517. pp$1.checkPatternExport = function(exports, pat) {
  2518. var this$1 = this;
  2519. var type = pat.type;
  2520. if (type == "Identifier")
  2521. { this.checkExport(exports, pat.name, pat.start); }
  2522. else if (type == "ObjectPattern")
  2523. { for (var i = 0, list = pat.properties; i < list.length; i += 1)
  2524. {
  2525. var prop = list[i];
  2526. this$1.checkPatternExport(exports, prop.value);
  2527. } }
  2528. else if (type == "ArrayPattern")
  2529. { for (var i$1 = 0, list$1 = pat.elements; i$1 < list$1.length; i$1 += 1) {
  2530. var elt = list$1[i$1];
  2531. if (elt) { this$1.checkPatternExport(exports, elt); }
  2532. } }
  2533. else if (type == "AssignmentPattern")
  2534. { this.checkPatternExport(exports, pat.left); }
  2535. else if (type == "ParenthesizedExpression")
  2536. { this.checkPatternExport(exports, pat.expression); }
  2537. };
  2538. pp$1.checkVariableExport = function(exports, decls) {
  2539. var this$1 = this;
  2540. if (!exports) { return }
  2541. for (var i = 0, list = decls; i < list.length; i += 1)
  2542. {
  2543. var decl = list[i];
  2544. this$1.checkPatternExport(exports, decl.id);
  2545. }
  2546. };
  2547. pp$1.shouldParseExportStatement = function() {
  2548. return this.type.keyword === "var" ||
  2549. this.type.keyword === "const" ||
  2550. this.type.keyword === "class" ||
  2551. this.type.keyword === "function" ||
  2552. this.isLet() ||
  2553. this.isAsyncFunction()
  2554. };
  2555. // Parses a comma-separated list of module exports.
  2556. pp$1.parseExportSpecifiers = function(exports) {
  2557. var this$1 = this;
  2558. var nodes = [], first = true;
  2559. // export { x, y as z } [from '...']
  2560. this.expect(types.braceL);
  2561. while (!this.eat(types.braceR)) {
  2562. if (!first) {
  2563. this$1.expect(types.comma);
  2564. if (this$1.afterTrailingComma(types.braceR)) { break }
  2565. } else { first = false; }
  2566. var node = this$1.startNode();
  2567. node.local = this$1.parseIdent(true);
  2568. node.exported = this$1.eatContextual("as") ? this$1.parseIdent(true) : node.local;
  2569. this$1.checkExport(exports, node.exported.name, node.exported.start);
  2570. nodes.push(this$1.finishNode(node, "ExportSpecifier"));
  2571. }
  2572. return nodes
  2573. };
  2574. // Parses import declaration.
  2575. pp$1.parseImport = function(node) {
  2576. this.next();
  2577. // import '...'
  2578. if (this.type === types.string) {
  2579. node.specifiers = empty;
  2580. node.source = this.parseExprAtom();
  2581. } else {
  2582. node.specifiers = this.parseImportSpecifiers();
  2583. this.expectContextual("from");
  2584. node.source = this.type === types.string ? this.parseExprAtom() : this.unexpected();
  2585. }
  2586. this.semicolon();
  2587. return this.finishNode(node, "ImportDeclaration")
  2588. };
  2589. // Parses a comma-separated list of module imports.
  2590. pp$1.parseImportSpecifiers = function() {
  2591. var this$1 = this;
  2592. var nodes = [], first = true;
  2593. if (this.type === types.name) {
  2594. // import defaultObj, { x, y as z } from '...'
  2595. var node = this.startNode();
  2596. node.local = this.parseIdent();
  2597. this.checkLVal(node.local, "let");
  2598. nodes.push(this.finishNode(node, "ImportDefaultSpecifier"));
  2599. if (!this.eat(types.comma)) { return nodes }
  2600. }
  2601. if (this.type === types.star) {
  2602. var node$1 = this.startNode();
  2603. this.next();
  2604. this.expectContextual("as");
  2605. node$1.local = this.parseIdent();
  2606. this.checkLVal(node$1.local, "let");
  2607. nodes.push(this.finishNode(node$1, "ImportNamespaceSpecifier"));
  2608. return nodes
  2609. }
  2610. this.expect(types.braceL);
  2611. while (!this.eat(types.braceR)) {
  2612. if (!first) {
  2613. this$1.expect(types.comma);
  2614. if (this$1.afterTrailingComma(types.braceR)) { break }
  2615. } else { first = false; }
  2616. var node$2 = this$1.startNode();
  2617. node$2.imported = this$1.parseIdent(true);
  2618. if (this$1.eatContextual("as")) {
  2619. node$2.local = this$1.parseIdent();
  2620. } else {
  2621. this$1.checkUnreserved(node$2.imported);
  2622. node$2.local = node$2.imported;
  2623. }
  2624. this$1.checkLVal(node$2.local, "let");
  2625. nodes.push(this$1.finishNode(node$2, "ImportSpecifier"));
  2626. }
  2627. return nodes
  2628. };
  2629. var pp$2 = Parser.prototype;
  2630. // Convert existing expression atom to assignable pattern
  2631. // if possible.
  2632. pp$2.toAssignable = function(node, isBinding) {
  2633. var this$1 = this;
  2634. if (this.options.ecmaVersion >= 6 && node) {
  2635. switch (node.type) {
  2636. case "Identifier":
  2637. if (this.inAsync && node.name === "await")
  2638. { this.raise(node.start, "Can not use 'await' as identifier inside an async function"); }
  2639. break
  2640. case "ObjectPattern":
  2641. case "ArrayPattern":
  2642. break
  2643. case "ObjectExpression":
  2644. node.type = "ObjectPattern";
  2645. for (var i = 0, list = node.properties; i < list.length; i += 1) {
  2646. var prop = list[i];
  2647. if (prop.kind !== "init") { this$1.raise(prop.key.start, "Object pattern can't contain getter or setter"); }
  2648. this$1.toAssignable(prop.value, isBinding);
  2649. }
  2650. break
  2651. case "ArrayExpression":
  2652. node.type = "ArrayPattern";
  2653. this.toAssignableList(node.elements, isBinding);
  2654. break
  2655. case "AssignmentExpression":
  2656. if (node.operator === "=") {
  2657. node.type = "AssignmentPattern";
  2658. delete node.operator;
  2659. this.toAssignable(node.left, isBinding);
  2660. // falls through to AssignmentPattern
  2661. } else {
  2662. this.raise(node.left.end, "Only '=' operator can be used for specifying default value.");
  2663. break
  2664. }
  2665. case "AssignmentPattern":
  2666. break
  2667. case "ParenthesizedExpression":
  2668. this.toAssignable(node.expression, isBinding);
  2669. break
  2670. case "MemberExpression":
  2671. if (!isBinding) { break }
  2672. default:
  2673. this.raise(node.start, "Assigning to rvalue");
  2674. }
  2675. }
  2676. return node
  2677. };
  2678. // Convert list of expression atoms to binding list.
  2679. pp$2.toAssignableList = function(exprList, isBinding) {
  2680. var this$1 = this;
  2681. var end = exprList.length;
  2682. if (end) {
  2683. var last = exprList[end - 1];
  2684. if (last && last.type == "RestElement") {
  2685. --end;
  2686. } else if (last && last.type == "SpreadElement") {
  2687. last.type = "RestElement";
  2688. var arg = last.argument;
  2689. this.toAssignable(arg, isBinding);
  2690. --end;
  2691. }
  2692. if (this.options.ecmaVersion === 6 && isBinding && last && last.type === "RestElement" && last.argument.type !== "Identifier")
  2693. { this.unexpected(last.argument.start); }
  2694. }
  2695. for (var i = 0; i < end; i++) {
  2696. var elt = exprList[i];
  2697. if (elt) { this$1.toAssignable(elt, isBinding); }
  2698. }
  2699. return exprList
  2700. };
  2701. // Parses spread element.
  2702. pp$2.parseSpread = function(refDestructuringErrors) {
  2703. var node = this.startNode();
  2704. this.next();
  2705. node.argument = this.parseMaybeAssign(false, refDestructuringErrors);
  2706. return this.finishNode(node, "SpreadElement")
  2707. };
  2708. pp$2.parseRestBinding = function() {
  2709. var node = this.startNode();
  2710. this.next();
  2711. // RestElement inside of a function parameter must be an identifier
  2712. if (this.options.ecmaVersion === 6 && this.type !== types.name)
  2713. { this.unexpected(); }
  2714. node.argument = this.parseBindingAtom();
  2715. return this.finishNode(node, "RestElement")
  2716. };
  2717. // Parses lvalue (assignable) atom.
  2718. pp$2.parseBindingAtom = function() {
  2719. if (this.options.ecmaVersion < 6) { return this.parseIdent() }
  2720. switch (this.type) {
  2721. case types.name:
  2722. return this.parseIdent()
  2723. case types.bracketL:
  2724. var node = this.startNode();
  2725. this.next();
  2726. node.elements = this.parseBindingList(types.bracketR, true, true);
  2727. return this.finishNode(node, "ArrayPattern")
  2728. case types.braceL:
  2729. return this.parseObj(true)
  2730. default:
  2731. this.unexpected();
  2732. }
  2733. };
  2734. pp$2.parseBindingList = function(close, allowEmpty, allowTrailingComma) {
  2735. var this$1 = this;
  2736. var elts = [], first = true;
  2737. while (!this.eat(close)) {
  2738. if (first) { first = false; }
  2739. else { this$1.expect(types.comma); }
  2740. if (allowEmpty && this$1.type === types.comma) {
  2741. elts.push(null);
  2742. } else if (allowTrailingComma && this$1.afterTrailingComma(close)) {
  2743. break
  2744. } else if (this$1.type === types.ellipsis) {
  2745. var rest = this$1.parseRestBinding();
  2746. this$1.parseBindingListItem(rest);
  2747. elts.push(rest);
  2748. if (this$1.type === types.comma) { this$1.raise(this$1.start, "Comma is not permitted after the rest element"); }
  2749. this$1.expect(close);
  2750. break
  2751. } else {
  2752. var elem = this$1.parseMaybeDefault(this$1.start, this$1.startLoc);
  2753. this$1.parseBindingListItem(elem);
  2754. elts.push(elem);
  2755. }
  2756. }
  2757. return elts
  2758. };
  2759. pp$2.parseBindingListItem = function(param) {
  2760. return param
  2761. };
  2762. // Parses assignment pattern around given atom if possible.
  2763. pp$2.parseMaybeDefault = function(startPos, startLoc, left) {
  2764. left = left || this.parseBindingAtom();
  2765. if (this.options.ecmaVersion < 6 || !this.eat(types.eq)) { return left }
  2766. var node = this.startNodeAt(startPos, startLoc);
  2767. node.left = left;
  2768. node.right = this.parseMaybeAssign();
  2769. return this.finishNode(node, "AssignmentPattern")
  2770. };
  2771. // Verify that a node is an lval — something that can be assigned
  2772. // to.
  2773. // bindingType can be either:
  2774. // 'var' indicating that the lval creates a 'var' binding
  2775. // 'let' indicating that the lval creates a lexical ('let' or 'const') binding
  2776. // 'none' indicating that the binding should be checked for illegal identifiers, but not for duplicate references
  2777. pp$2.checkLVal = function(expr, bindingType, checkClashes) {
  2778. var this$1 = this;
  2779. switch (expr.type) {
  2780. case "Identifier":
  2781. if (this.strict && this.reservedWordsStrictBind.test(expr.name))
  2782. { this.raiseRecoverable(expr.start, (bindingType ? "Binding " : "Assigning to ") + expr.name + " in strict mode"); }
  2783. if (checkClashes) {
  2784. if (has(checkClashes, expr.name))
  2785. { this.raiseRecoverable(expr.start, "Argument name clash"); }
  2786. checkClashes[expr.name] = true;
  2787. }
  2788. if (bindingType && bindingType !== "none") {
  2789. if (
  2790. bindingType === "var" && !this.canDeclareVarName(expr.name) ||
  2791. bindingType !== "var" && !this.canDeclareLexicalName(expr.name)
  2792. ) {
  2793. this.raiseRecoverable(expr.start, ("Identifier '" + (expr.name) + "' has already been declared"));
  2794. }
  2795. if (bindingType === "var") {
  2796. this.declareVarName(expr.name);
  2797. } else {
  2798. this.declareLexicalName(expr.name);
  2799. }
  2800. }
  2801. break
  2802. case "MemberExpression":
  2803. if (bindingType) { this.raiseRecoverable(expr.start, (bindingType ? "Binding" : "Assigning to") + " member expression"); }
  2804. break
  2805. case "ObjectPattern":
  2806. for (var i = 0, list = expr.properties; i < list.length; i += 1)
  2807. {
  2808. var prop = list[i];
  2809. this$1.checkLVal(prop.value, bindingType, checkClashes);
  2810. }
  2811. break
  2812. case "ArrayPattern":
  2813. for (var i$1 = 0, list$1 = expr.elements; i$1 < list$1.length; i$1 += 1) {
  2814. var elem = list$1[i$1];
  2815. if (elem) { this$1.checkLVal(elem, bindingType, checkClashes); }
  2816. }
  2817. break
  2818. case "AssignmentPattern":
  2819. this.checkLVal(expr.left, bindingType, checkClashes);
  2820. break
  2821. case "RestElement":
  2822. this.checkLVal(expr.argument, bindingType, checkClashes);
  2823. break
  2824. case "ParenthesizedExpression":
  2825. this.checkLVal(expr.expression, bindingType, checkClashes);
  2826. break
  2827. default:
  2828. this.raise(expr.start, (bindingType ? "Binding" : "Assigning to") + " rvalue");
  2829. }
  2830. };
  2831. // A recursive descent parser operates by defining functions for all
  2832. // syntactic elements, and recursively calling those, each function
  2833. // advancing the input stream and returning an AST node. Precedence
  2834. // of constructs (for example, the fact that `!x[1]` means `!(x[1])`
  2835. // instead of `(!x)[1]` is handled by the fact that the parser
  2836. // function that parses unary prefix operators is called first, and
  2837. // in turn calls the function that parses `[]` subscripts — that
  2838. // way, it'll receive the node for `x[1]` already parsed, and wraps
  2839. // *that* in the unary operator node.
  2840. //
  2841. // Acorn uses an [operator precedence parser][opp] to handle binary
  2842. // operator precedence, because it is much more compact than using
  2843. // the technique outlined above, which uses different, nesting
  2844. // functions to specify precedence, for all of the ten binary
  2845. // precedence levels that JavaScript defines.
  2846. //
  2847. // [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
  2848. var pp$3 = Parser.prototype;
  2849. // Check if property name clashes with already added.
  2850. // Object/class getters and setters are not allowed to clash —
  2851. // either with each other or with an init property — and in
  2852. // strict mode, init properties are also not allowed to be repeated.
  2853. pp$3.checkPropClash = function(prop, propHash) {
  2854. if (this.options.ecmaVersion >= 6 && (prop.computed || prop.method || prop.shorthand))
  2855. { return }
  2856. var key = prop.key;
  2857. var name;
  2858. switch (key.type) {
  2859. case "Identifier": name = key.name; break
  2860. case "Literal": name = String(key.value); break
  2861. default: return
  2862. }
  2863. var kind = prop.kind;
  2864. if (this.options.ecmaVersion >= 6) {
  2865. if (name === "__proto__" && kind === "init") {
  2866. if (propHash.proto) { this.raiseRecoverable(key.start, "Redefinition of __proto__ property"); }
  2867. propHash.proto = true;
  2868. }
  2869. return
  2870. }
  2871. name = "$" + name;
  2872. var other = propHash[name];
  2873. if (other) {
  2874. var redefinition;
  2875. if (kind === "init") {
  2876. redefinition = this.strict && other.init || other.get || other.set;
  2877. } else {
  2878. redefinition = other.init || other[kind];
  2879. }
  2880. if (redefinition)
  2881. { this.raiseRecoverable(key.start, "Redefinition of property"); }
  2882. } else {
  2883. other = propHash[name] = {
  2884. init: false,
  2885. get: false,
  2886. set: false
  2887. };
  2888. }
  2889. other[kind] = true;
  2890. };
  2891. // ### Expression parsing
  2892. // These nest, from the most general expression type at the top to
  2893. // 'atomic', nondivisible expression types at the bottom. Most of
  2894. // the functions will simply let the function(s) below them parse,
  2895. // and, *if* the syntactic construct they handle is present, wrap
  2896. // the AST node that the inner parser gave them in another node.
  2897. // Parse a full expression. The optional arguments are used to
  2898. // forbid the `in` operator (in for loops initalization expressions)
  2899. // and provide reference for storing '=' operator inside shorthand
  2900. // property assignment in contexts where both object expression
  2901. // and object pattern might appear (so it's possible to raise
  2902. // delayed syntax error at correct position).
  2903. pp$3.parseExpression = function(noIn, refDestructuringErrors) {
  2904. var this$1 = this;
  2905. var startPos = this.start, startLoc = this.startLoc;
  2906. var expr = this.parseMaybeAssign(noIn, refDestructuringErrors);
  2907. if (this.type === types.comma) {
  2908. var node = this.startNodeAt(startPos, startLoc);
  2909. node.expressions = [expr];
  2910. while (this.eat(types.comma)) { node.expressions.push(this$1.parseMaybeAssign(noIn, refDestructuringErrors)); }
  2911. return this.finishNode(node, "SequenceExpression")
  2912. }
  2913. return expr
  2914. };
  2915. // Parse an assignment expression. This includes applications of
  2916. // operators like `+=`.
  2917. pp$3.parseMaybeAssign = function(noIn, refDestructuringErrors, afterLeftParse) {
  2918. if (this.inGenerator && this.isContextual("yield")) { return this.parseYield() }
  2919. var ownDestructuringErrors = false, oldParenAssign = -1, oldTrailingComma = -1;
  2920. if (refDestructuringErrors) {
  2921. oldParenAssign = refDestructuringErrors.parenthesizedAssign;
  2922. oldTrailingComma = refDestructuringErrors.trailingComma;
  2923. refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = -1;
  2924. } else {
  2925. refDestructuringErrors = new DestructuringErrors;
  2926. ownDestructuringErrors = true;
  2927. }
  2928. var startPos = this.start, startLoc = this.startLoc;
  2929. if (this.type == types.parenL || this.type == types.name)
  2930. { this.potentialArrowAt = this.start; }
  2931. var left = this.parseMaybeConditional(noIn, refDestructuringErrors);
  2932. if (afterLeftParse) { left = afterLeftParse.call(this, left, startPos, startLoc); }
  2933. if (this.type.isAssign) {
  2934. this.checkPatternErrors(refDestructuringErrors, true);
  2935. if (!ownDestructuringErrors) { DestructuringErrors.call(refDestructuringErrors); }
  2936. var node = this.startNodeAt(startPos, startLoc);
  2937. node.operator = this.value;
  2938. node.left = this.type === types.eq ? this.toAssignable(left) : left;
  2939. refDestructuringErrors.shorthandAssign = -1; // reset because shorthand default was used correctly
  2940. this.checkLVal(left);
  2941. this.next();
  2942. node.right = this.parseMaybeAssign(noIn);
  2943. return this.finishNode(node, "AssignmentExpression")
  2944. } else {
  2945. if (ownDestructuringErrors) { this.checkExpressionErrors(refDestructuringErrors, true); }
  2946. }
  2947. if (oldParenAssign > -1) { refDestructuringErrors.parenthesizedAssign = oldParenAssign; }
  2948. if (oldTrailingComma > -1) { refDestructuringErrors.trailingComma = oldTrailingComma; }
  2949. return left
  2950. };
  2951. // Parse a ternary conditional (`?:`) operator.
  2952. pp$3.parseMaybeConditional = function(noIn, refDestructuringErrors) {
  2953. var startPos = this.start, startLoc = this.startLoc;
  2954. var expr = this.parseExprOps(noIn, refDestructuringErrors);
  2955. if (this.checkExpressionErrors(refDestructuringErrors)) { return expr }
  2956. if (this.eat(types.question)) {
  2957. var node = this.startNodeAt(startPos, startLoc);
  2958. node.test = expr;
  2959. node.consequent = this.parseMaybeAssign();
  2960. this.expect(types.colon);
  2961. node.alternate = this.parseMaybeAssign(noIn);
  2962. return this.finishNode(node, "ConditionalExpression")
  2963. }
  2964. return expr
  2965. };
  2966. // Start the precedence parser.
  2967. pp$3.parseExprOps = function(noIn, refDestructuringErrors) {
  2968. var startPos = this.start, startLoc = this.startLoc;
  2969. var expr = this.parseMaybeUnary(refDestructuringErrors, false);
  2970. if (this.checkExpressionErrors(refDestructuringErrors)) { return expr }
  2971. return expr.start == startPos && expr.type === "ArrowFunctionExpression" ? expr : this.parseExprOp(expr, startPos, startLoc, -1, noIn)
  2972. };
  2973. // Parse binary operators with the operator precedence parsing
  2974. // algorithm. `left` is the left-hand side of the operator.
  2975. // `minPrec` provides context that allows the function to stop and
  2976. // defer further parser to one of its callers when it encounters an
  2977. // operator that has a lower precedence than the set it is parsing.
  2978. pp$3.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, noIn) {
  2979. var prec = this.type.binop;
  2980. if (prec != null && (!noIn || this.type !== types._in)) {
  2981. if (prec > minPrec) {
  2982. var logical = this.type === types.logicalOR || this.type === types.logicalAND;
  2983. var op = this.value;
  2984. this.next();
  2985. var startPos = this.start, startLoc = this.startLoc;
  2986. var right = this.parseExprOp(this.parseMaybeUnary(null, false), startPos, startLoc, prec, noIn);
  2987. var node = this.buildBinary(leftStartPos, leftStartLoc, left, right, op, logical);
  2988. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn)
  2989. }
  2990. }
  2991. return left
  2992. };
  2993. pp$3.buildBinary = function(startPos, startLoc, left, right, op, logical) {
  2994. var node = this.startNodeAt(startPos, startLoc);
  2995. node.left = left;
  2996. node.operator = op;
  2997. node.right = right;
  2998. return this.finishNode(node, logical ? "LogicalExpression" : "BinaryExpression")
  2999. };
  3000. // Parse unary operators, both prefix and postfix.
  3001. pp$3.parseMaybeUnary = function(refDestructuringErrors, sawUnary) {
  3002. var this$1 = this;
  3003. var startPos = this.start, startLoc = this.startLoc, expr;
  3004. if (this.inAsync && this.isContextual("await")) {
  3005. expr = this.parseAwait(refDestructuringErrors);
  3006. sawUnary = true;
  3007. } else if (this.type.prefix) {
  3008. var node = this.startNode(), update = this.type === types.incDec;
  3009. node.operator = this.value;
  3010. node.prefix = true;
  3011. this.next();
  3012. node.argument = this.parseMaybeUnary(null, true);
  3013. this.checkExpressionErrors(refDestructuringErrors, true);
  3014. if (update) { this.checkLVal(node.argument); }
  3015. else if (this.strict && node.operator === "delete" &&
  3016. node.argument.type === "Identifier")
  3017. { this.raiseRecoverable(node.start, "Deleting local variable in strict mode"); }
  3018. else { sawUnary = true; }
  3019. expr = this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
  3020. } else {
  3021. expr = this.parseExprSubscripts(refDestructuringErrors);
  3022. if (this.checkExpressionErrors(refDestructuringErrors)) { return expr }
  3023. while (this.type.postfix && !this.canInsertSemicolon()) {
  3024. var node$1 = this$1.startNodeAt(startPos, startLoc);
  3025. node$1.operator = this$1.value;
  3026. node$1.prefix = false;
  3027. node$1.argument = expr;
  3028. this$1.checkLVal(expr);
  3029. this$1.next();
  3030. expr = this$1.finishNode(node$1, "UpdateExpression");
  3031. }
  3032. }
  3033. if (!sawUnary && this.eat(types.starstar))
  3034. { return this.buildBinary(startPos, startLoc, expr, this.parseMaybeUnary(null, false), "**", false) }
  3035. else
  3036. { return expr }
  3037. };
  3038. // Parse call, dot, and `[]`-subscript expressions.
  3039. pp$3.parseExprSubscripts = function(refDestructuringErrors) {
  3040. var startPos = this.start, startLoc = this.startLoc;
  3041. var expr = this.parseExprAtom(refDestructuringErrors);
  3042. var skipArrowSubscripts = expr.type === "ArrowFunctionExpression" && this.input.slice(this.lastTokStart, this.lastTokEnd) !== ")";
  3043. if (this.checkExpressionErrors(refDestructuringErrors) || skipArrowSubscripts) { return expr }
  3044. var result = this.parseSubscripts(expr, startPos, startLoc);
  3045. if (refDestructuringErrors && result.type === "MemberExpression") {
  3046. if (refDestructuringErrors.parenthesizedAssign >= result.start) { refDestructuringErrors.parenthesizedAssign = -1; }
  3047. if (refDestructuringErrors.parenthesizedBind >= result.start) { refDestructuringErrors.parenthesizedBind = -1; }
  3048. }
  3049. return result
  3050. };
  3051. pp$3.parseSubscripts = function(base, startPos, startLoc, noCalls) {
  3052. var this$1 = this;
  3053. var maybeAsyncArrow = this.options.ecmaVersion >= 8 && base.type === "Identifier" && base.name === "async" &&
  3054. this.lastTokEnd == base.end && !this.canInsertSemicolon();
  3055. for (var computed = (void 0);;) {
  3056. if ((computed = this$1.eat(types.bracketL)) || this$1.eat(types.dot)) {
  3057. var node = this$1.startNodeAt(startPos, startLoc);
  3058. node.object = base;
  3059. node.property = computed ? this$1.parseExpression() : this$1.parseIdent(true);
  3060. node.computed = !!computed;
  3061. if (computed) { this$1.expect(types.bracketR); }
  3062. base = this$1.finishNode(node, "MemberExpression");
  3063. } else if (!noCalls && this$1.eat(types.parenL)) {
  3064. var refDestructuringErrors = new DestructuringErrors, oldYieldPos = this$1.yieldPos, oldAwaitPos = this$1.awaitPos;
  3065. this$1.yieldPos = 0;
  3066. this$1.awaitPos = 0;
  3067. var exprList = this$1.parseExprList(types.parenR, this$1.options.ecmaVersion >= 8, false, refDestructuringErrors);
  3068. if (maybeAsyncArrow && !this$1.canInsertSemicolon() && this$1.eat(types.arrow)) {
  3069. this$1.checkPatternErrors(refDestructuringErrors, false);
  3070. this$1.checkYieldAwaitInDefaultParams();
  3071. this$1.yieldPos = oldYieldPos;
  3072. this$1.awaitPos = oldAwaitPos;
  3073. return this$1.parseArrowExpression(this$1.startNodeAt(startPos, startLoc), exprList, true)
  3074. }
  3075. this$1.checkExpressionErrors(refDestructuringErrors, true);
  3076. this$1.yieldPos = oldYieldPos || this$1.yieldPos;
  3077. this$1.awaitPos = oldAwaitPos || this$1.awaitPos;
  3078. var node$1 = this$1.startNodeAt(startPos, startLoc);
  3079. node$1.callee = base;
  3080. node$1.arguments = exprList;
  3081. base = this$1.finishNode(node$1, "CallExpression");
  3082. } else if (this$1.type === types.backQuote) {
  3083. var node$2 = this$1.startNodeAt(startPos, startLoc);
  3084. node$2.tag = base;
  3085. node$2.quasi = this$1.parseTemplate({isTagged: true});
  3086. base = this$1.finishNode(node$2, "TaggedTemplateExpression");
  3087. } else {
  3088. return base
  3089. }
  3090. }
  3091. };
  3092. // Parse an atomic expression — either a single token that is an
  3093. // expression, an expression started by a keyword like `function` or
  3094. // `new`, or an expression wrapped in punctuation like `()`, `[]`,
  3095. // or `{}`.
  3096. pp$3.parseExprAtom = function(refDestructuringErrors) {
  3097. var node, canBeArrow = this.potentialArrowAt == this.start;
  3098. switch (this.type) {
  3099. case types._super:
  3100. if (!this.inFunction)
  3101. { this.raise(this.start, "'super' outside of function or class"); }
  3102. case types._this:
  3103. var type = this.type === types._this ? "ThisExpression" : "Super";
  3104. node = this.startNode();
  3105. this.next();
  3106. return this.finishNode(node, type)
  3107. case types.name:
  3108. var startPos = this.start, startLoc = this.startLoc;
  3109. var id = this.parseIdent(this.type !== types.name);
  3110. if (this.options.ecmaVersion >= 8 && id.name === "async" && !this.canInsertSemicolon() && this.eat(types._function))
  3111. { return this.parseFunction(this.startNodeAt(startPos, startLoc), false, false, true) }
  3112. if (canBeArrow && !this.canInsertSemicolon()) {
  3113. if (this.eat(types.arrow))
  3114. { return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), [id], false) }
  3115. if (this.options.ecmaVersion >= 8 && id.name === "async" && this.type === types.name) {
  3116. id = this.parseIdent();
  3117. if (this.canInsertSemicolon() || !this.eat(types.arrow))
  3118. { this.unexpected(); }
  3119. return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), [id], true)
  3120. }
  3121. }
  3122. return id
  3123. case types.regexp:
  3124. var value = this.value;
  3125. node = this.parseLiteral(value.value);
  3126. node.regex = {pattern: value.pattern, flags: value.flags};
  3127. return node
  3128. case types.num: case types.string:
  3129. return this.parseLiteral(this.value)
  3130. case types._null: case types._true: case types._false:
  3131. node = this.startNode();
  3132. node.value = this.type === types._null ? null : this.type === types._true;
  3133. node.raw = this.type.keyword;
  3134. this.next();
  3135. return this.finishNode(node, "Literal")
  3136. case types.parenL:
  3137. var start = this.start, expr = this.parseParenAndDistinguishExpression(canBeArrow);
  3138. if (refDestructuringErrors) {
  3139. if (refDestructuringErrors.parenthesizedAssign < 0 && !this.isSimpleAssignTarget(expr))
  3140. { refDestructuringErrors.parenthesizedAssign = start; }
  3141. if (refDestructuringErrors.parenthesizedBind < 0)
  3142. { refDestructuringErrors.parenthesizedBind = start; }
  3143. }
  3144. return expr
  3145. case types.bracketL:
  3146. node = this.startNode();
  3147. this.next();
  3148. node.elements = this.parseExprList(types.bracketR, true, true, refDestructuringErrors);
  3149. return this.finishNode(node, "ArrayExpression")
  3150. case types.braceL:
  3151. return this.parseObj(false, refDestructuringErrors)
  3152. case types._function:
  3153. node = this.startNode();
  3154. this.next();
  3155. return this.parseFunction(node, false)
  3156. case types._class:
  3157. return this.parseClass(this.startNode(), false)
  3158. case types._new:
  3159. return this.parseNew()
  3160. case types.backQuote:
  3161. return this.parseTemplate()
  3162. default:
  3163. this.unexpected();
  3164. }
  3165. };
  3166. pp$3.parseLiteral = function(value) {
  3167. var node = this.startNode();
  3168. node.value = value;
  3169. node.raw = this.input.slice(this.start, this.end);
  3170. this.next();
  3171. return this.finishNode(node, "Literal")
  3172. };
  3173. pp$3.parseParenExpression = function() {
  3174. this.expect(types.parenL);
  3175. var val = this.parseExpression();
  3176. this.expect(types.parenR);
  3177. return val
  3178. };
  3179. pp$3.parseParenAndDistinguishExpression = function(canBeArrow) {
  3180. var this$1 = this;
  3181. var startPos = this.start, startLoc = this.startLoc, val, allowTrailingComma = this.options.ecmaVersion >= 8;
  3182. if (this.options.ecmaVersion >= 6) {
  3183. this.next();
  3184. var innerStartPos = this.start, innerStartLoc = this.startLoc;
  3185. var exprList = [], first = true, lastIsComma = false;
  3186. var refDestructuringErrors = new DestructuringErrors, oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, spreadStart, innerParenStart;
  3187. this.yieldPos = 0;
  3188. this.awaitPos = 0;
  3189. while (this.type !== types.parenR) {
  3190. first ? first = false : this$1.expect(types.comma);
  3191. if (allowTrailingComma && this$1.afterTrailingComma(types.parenR, true)) {
  3192. lastIsComma = true;
  3193. break
  3194. } else if (this$1.type === types.ellipsis) {
  3195. spreadStart = this$1.start;
  3196. exprList.push(this$1.parseParenItem(this$1.parseRestBinding()));
  3197. if (this$1.type === types.comma) { this$1.raise(this$1.start, "Comma is not permitted after the rest element"); }
  3198. break
  3199. } else {
  3200. if (this$1.type === types.parenL && !innerParenStart) {
  3201. innerParenStart = this$1.start;
  3202. }
  3203. exprList.push(this$1.parseMaybeAssign(false, refDestructuringErrors, this$1.parseParenItem));
  3204. }
  3205. }
  3206. var innerEndPos = this.start, innerEndLoc = this.startLoc;
  3207. this.expect(types.parenR);
  3208. if (canBeArrow && !this.canInsertSemicolon() && this.eat(types.arrow)) {
  3209. this.checkPatternErrors(refDestructuringErrors, false);
  3210. this.checkYieldAwaitInDefaultParams();
  3211. if (innerParenStart) { this.unexpected(innerParenStart); }
  3212. this.yieldPos = oldYieldPos;
  3213. this.awaitPos = oldAwaitPos;
  3214. return this.parseParenArrowList(startPos, startLoc, exprList)
  3215. }
  3216. if (!exprList.length || lastIsComma) { this.unexpected(this.lastTokStart); }
  3217. if (spreadStart) { this.unexpected(spreadStart); }
  3218. this.checkExpressionErrors(refDestructuringErrors, true);
  3219. this.yieldPos = oldYieldPos || this.yieldPos;
  3220. this.awaitPos = oldAwaitPos || this.awaitPos;
  3221. if (exprList.length > 1) {
  3222. val = this.startNodeAt(innerStartPos, innerStartLoc);
  3223. val.expressions = exprList;
  3224. this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
  3225. } else {
  3226. val = exprList[0];
  3227. }
  3228. } else {
  3229. val = this.parseParenExpression();
  3230. }
  3231. if (this.options.preserveParens) {
  3232. var par = this.startNodeAt(startPos, startLoc);
  3233. par.expression = val;
  3234. return this.finishNode(par, "ParenthesizedExpression")
  3235. } else {
  3236. return val
  3237. }
  3238. };
  3239. pp$3.parseParenItem = function(item) {
  3240. return item
  3241. };
  3242. pp$3.parseParenArrowList = function(startPos, startLoc, exprList) {
  3243. return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), exprList)
  3244. };
  3245. // New's precedence is slightly tricky. It must allow its argument to
  3246. // be a `[]` or dot subscript expression, but not a call — at least,
  3247. // not without wrapping it in parentheses. Thus, it uses the noCalls
  3248. // argument to parseSubscripts to prevent it from consuming the
  3249. // argument list.
  3250. var empty$1 = [];
  3251. pp$3.parseNew = function() {
  3252. var node = this.startNode();
  3253. var meta = this.parseIdent(true);
  3254. if (this.options.ecmaVersion >= 6 && this.eat(types.dot)) {
  3255. node.meta = meta;
  3256. node.property = this.parseIdent(true);
  3257. if (node.property.name !== "target")
  3258. { this.raiseRecoverable(node.property.start, "The only valid meta property for new is new.target"); }
  3259. if (!this.inFunction)
  3260. { this.raiseRecoverable(node.start, "new.target can only be used in functions"); }
  3261. return this.finishNode(node, "MetaProperty")
  3262. }
  3263. var startPos = this.start, startLoc = this.startLoc;
  3264. node.callee = this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  3265. if (this.eat(types.parenL)) { node.arguments = this.parseExprList(types.parenR, this.options.ecmaVersion >= 8, false); }
  3266. else { node.arguments = empty$1; }
  3267. return this.finishNode(node, "NewExpression")
  3268. };
  3269. // Parse template expression.
  3270. pp$3.parseTemplateElement = function(ref) {
  3271. var isTagged = ref.isTagged;
  3272. var elem = this.startNode();
  3273. if (this.type === types.invalidTemplate) {
  3274. if (!isTagged) {
  3275. this.raiseRecoverable(this.start, "Bad escape sequence in untagged template literal");
  3276. }
  3277. elem.value = {
  3278. raw: this.value,
  3279. cooked: null
  3280. };
  3281. } else {
  3282. elem.value = {
  3283. raw: this.input.slice(this.start, this.end).replace(/\r\n?/g, "\n"),
  3284. cooked: this.value
  3285. };
  3286. }
  3287. this.next();
  3288. elem.tail = this.type === types.backQuote;
  3289. return this.finishNode(elem, "TemplateElement")
  3290. };
  3291. pp$3.parseTemplate = function(ref) {
  3292. var this$1 = this;
  3293. if ( ref === void 0 ) { ref = {}; }
  3294. var isTagged = ref.isTagged; if ( isTagged === void 0 ) { isTagged = false; }
  3295. var node = this.startNode();
  3296. this.next();
  3297. node.expressions = [];
  3298. var curElt = this.parseTemplateElement({isTagged: isTagged});
  3299. node.quasis = [curElt];
  3300. while (!curElt.tail) {
  3301. this$1.expect(types.dollarBraceL);
  3302. node.expressions.push(this$1.parseExpression());
  3303. this$1.expect(types.braceR);
  3304. node.quasis.push(curElt = this$1.parseTemplateElement({isTagged: isTagged}));
  3305. }
  3306. this.next();
  3307. return this.finishNode(node, "TemplateLiteral")
  3308. };
  3309. // Parse an object literal or binding pattern.
  3310. pp$3.isAsyncProp = function(prop) {
  3311. return !prop.computed && prop.key.type === "Identifier" && prop.key.name === "async" &&
  3312. (this.type === types.name || this.type === types.num || this.type === types.string || this.type === types.bracketL || this.type.keyword) &&
  3313. !lineBreak.test(this.input.slice(this.lastTokEnd, this.start))
  3314. };
  3315. pp$3.parseObj = function(isPattern, refDestructuringErrors) {
  3316. var this$1 = this;
  3317. var node = this.startNode(), first = true, propHash = {};
  3318. node.properties = [];
  3319. this.next();
  3320. while (!this.eat(types.braceR)) {
  3321. if (!first) {
  3322. this$1.expect(types.comma);
  3323. if (this$1.afterTrailingComma(types.braceR)) { break }
  3324. } else { first = false; }
  3325. var prop = this$1.startNode(), isGenerator = (void 0), isAsync = (void 0), startPos = (void 0), startLoc = (void 0);
  3326. if (this$1.options.ecmaVersion >= 6) {
  3327. prop.method = false;
  3328. prop.shorthand = false;
  3329. if (isPattern || refDestructuringErrors) {
  3330. startPos = this$1.start;
  3331. startLoc = this$1.startLoc;
  3332. }
  3333. if (!isPattern)
  3334. { isGenerator = this$1.eat(types.star); }
  3335. }
  3336. this$1.parsePropertyName(prop);
  3337. if (!isPattern && this$1.options.ecmaVersion >= 8 && !isGenerator && this$1.isAsyncProp(prop)) {
  3338. isAsync = true;
  3339. this$1.parsePropertyName(prop, refDestructuringErrors);
  3340. } else {
  3341. isAsync = false;
  3342. }
  3343. this$1.parsePropertyValue(prop, isPattern, isGenerator, isAsync, startPos, startLoc, refDestructuringErrors);
  3344. this$1.checkPropClash(prop, propHash);
  3345. node.properties.push(this$1.finishNode(prop, "Property"));
  3346. }
  3347. return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression")
  3348. };
  3349. pp$3.parsePropertyValue = function(prop, isPattern, isGenerator, isAsync, startPos, startLoc, refDestructuringErrors) {
  3350. if ((isGenerator || isAsync) && this.type === types.colon)
  3351. { this.unexpected(); }
  3352. if (this.eat(types.colon)) {
  3353. prop.value = isPattern ? this.parseMaybeDefault(this.start, this.startLoc) : this.parseMaybeAssign(false, refDestructuringErrors);
  3354. prop.kind = "init";
  3355. } else if (this.options.ecmaVersion >= 6 && this.type === types.parenL) {
  3356. if (isPattern) { this.unexpected(); }
  3357. prop.kind = "init";
  3358. prop.method = true;
  3359. prop.value = this.parseMethod(isGenerator, isAsync);
  3360. } else if (this.options.ecmaVersion >= 5 && !prop.computed && prop.key.type === "Identifier" &&
  3361. (prop.key.name === "get" || prop.key.name === "set") &&
  3362. (this.type != types.comma && this.type != types.braceR)) {
  3363. if (isGenerator || isAsync || isPattern) { this.unexpected(); }
  3364. prop.kind = prop.key.name;
  3365. this.parsePropertyName(prop);
  3366. prop.value = this.parseMethod(false);
  3367. var paramCount = prop.kind === "get" ? 0 : 1;
  3368. if (prop.value.params.length !== paramCount) {
  3369. var start = prop.value.start;
  3370. if (prop.kind === "get")
  3371. { this.raiseRecoverable(start, "getter should have no params"); }
  3372. else
  3373. { this.raiseRecoverable(start, "setter should have exactly one param"); }
  3374. } else {
  3375. if (prop.kind === "set" && prop.value.params[0].type === "RestElement")
  3376. { this.raiseRecoverable(prop.value.params[0].start, "Setter cannot use rest params"); }
  3377. }
  3378. } else if (this.options.ecmaVersion >= 6 && !prop.computed && prop.key.type === "Identifier") {
  3379. this.checkUnreserved(prop.key);
  3380. prop.kind = "init";
  3381. if (isPattern) {
  3382. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key);
  3383. } else if (this.type === types.eq && refDestructuringErrors) {
  3384. if (refDestructuringErrors.shorthandAssign < 0)
  3385. { refDestructuringErrors.shorthandAssign = this.start; }
  3386. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key);
  3387. } else {
  3388. prop.value = prop.key;
  3389. }
  3390. prop.shorthand = true;
  3391. } else { this.unexpected(); }
  3392. };
  3393. pp$3.parsePropertyName = function(prop) {
  3394. if (this.options.ecmaVersion >= 6) {
  3395. if (this.eat(types.bracketL)) {
  3396. prop.computed = true;
  3397. prop.key = this.parseMaybeAssign();
  3398. this.expect(types.bracketR);
  3399. return prop.key
  3400. } else {
  3401. prop.computed = false;
  3402. }
  3403. }
  3404. return prop.key = this.type === types.num || this.type === types.string ? this.parseExprAtom() : this.parseIdent(true)
  3405. };
  3406. // Initialize empty function node.
  3407. pp$3.initFunction = function(node) {
  3408. node.id = null;
  3409. if (this.options.ecmaVersion >= 6) {
  3410. node.generator = false;
  3411. node.expression = false;
  3412. }
  3413. if (this.options.ecmaVersion >= 8)
  3414. { node.async = false; }
  3415. };
  3416. // Parse object or class method.
  3417. pp$3.parseMethod = function(isGenerator, isAsync) {
  3418. var node = this.startNode(), oldInGen = this.inGenerator, oldInAsync = this.inAsync,
  3419. oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldInFunc = this.inFunction;
  3420. this.initFunction(node);
  3421. if (this.options.ecmaVersion >= 6)
  3422. { node.generator = isGenerator; }
  3423. if (this.options.ecmaVersion >= 8)
  3424. { node.async = !!isAsync; }
  3425. this.inGenerator = node.generator;
  3426. this.inAsync = node.async;
  3427. this.yieldPos = 0;
  3428. this.awaitPos = 0;
  3429. this.inFunction = true;
  3430. this.enterFunctionScope();
  3431. this.expect(types.parenL);
  3432. node.params = this.parseBindingList(types.parenR, false, this.options.ecmaVersion >= 8);
  3433. this.checkYieldAwaitInDefaultParams();
  3434. this.parseFunctionBody(node, false);
  3435. this.inGenerator = oldInGen;
  3436. this.inAsync = oldInAsync;
  3437. this.yieldPos = oldYieldPos;
  3438. this.awaitPos = oldAwaitPos;
  3439. this.inFunction = oldInFunc;
  3440. return this.finishNode(node, "FunctionExpression")
  3441. };
  3442. // Parse arrow function expression with given parameters.
  3443. pp$3.parseArrowExpression = function(node, params, isAsync) {
  3444. var oldInGen = this.inGenerator, oldInAsync = this.inAsync,
  3445. oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldInFunc = this.inFunction;
  3446. this.enterFunctionScope();
  3447. this.initFunction(node);
  3448. if (this.options.ecmaVersion >= 8)
  3449. { node.async = !!isAsync; }
  3450. this.inGenerator = false;
  3451. this.inAsync = node.async;
  3452. this.yieldPos = 0;
  3453. this.awaitPos = 0;
  3454. this.inFunction = true;
  3455. node.params = this.toAssignableList(params, true);
  3456. this.parseFunctionBody(node, true);
  3457. this.inGenerator = oldInGen;
  3458. this.inAsync = oldInAsync;
  3459. this.yieldPos = oldYieldPos;
  3460. this.awaitPos = oldAwaitPos;
  3461. this.inFunction = oldInFunc;
  3462. return this.finishNode(node, "ArrowFunctionExpression")
  3463. };
  3464. // Parse function body and check parameters.
  3465. pp$3.parseFunctionBody = function(node, isArrowFunction) {
  3466. var isExpression = isArrowFunction && this.type !== types.braceL;
  3467. var oldStrict = this.strict, useStrict = false;
  3468. if (isExpression) {
  3469. node.body = this.parseMaybeAssign();
  3470. node.expression = true;
  3471. this.checkParams(node, false);
  3472. } else {
  3473. var nonSimple = this.options.ecmaVersion >= 7 && !this.isSimpleParamList(node.params);
  3474. if (!oldStrict || nonSimple) {
  3475. useStrict = this.strictDirective(this.end);
  3476. // If this is a strict mode function, verify that argument names
  3477. // are not repeated, and it does not try to bind the words `eval`
  3478. // or `arguments`.
  3479. if (useStrict && nonSimple)
  3480. { this.raiseRecoverable(node.start, "Illegal 'use strict' directive in function with non-simple parameter list"); }
  3481. }
  3482. // Start a new scope with regard to labels and the `inFunction`
  3483. // flag (restore them to their old value afterwards).
  3484. var oldLabels = this.labels;
  3485. this.labels = [];
  3486. if (useStrict) { this.strict = true; }
  3487. // Add the params to varDeclaredNames to ensure that an error is thrown
  3488. // if a let/const declaration in the function clashes with one of the params.
  3489. this.checkParams(node, !oldStrict && !useStrict && !isArrowFunction && this.isSimpleParamList(node.params));
  3490. node.body = this.parseBlock(false);
  3491. node.expression = false;
  3492. this.labels = oldLabels;
  3493. }
  3494. this.exitFunctionScope();
  3495. if (this.strict && node.id) {
  3496. // Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval'
  3497. this.checkLVal(node.id, "none");
  3498. }
  3499. this.strict = oldStrict;
  3500. };
  3501. pp$3.isSimpleParamList = function(params) {
  3502. for (var i = 0, list = params; i < list.length; i += 1)
  3503. {
  3504. var param = list[i];
  3505. if (param.type !== "Identifier") { return false
  3506. } }
  3507. return true
  3508. };
  3509. // Checks function params for various disallowed patterns such as using "eval"
  3510. // or "arguments" and duplicate parameters.
  3511. pp$3.checkParams = function(node, allowDuplicates) {
  3512. var this$1 = this;
  3513. var nameHash = {};
  3514. for (var i = 0, list = node.params; i < list.length; i += 1)
  3515. {
  3516. var param = list[i];
  3517. this$1.checkLVal(param, "var", allowDuplicates ? null : nameHash);
  3518. }
  3519. };
  3520. // Parses a comma-separated list of expressions, and returns them as
  3521. // an array. `close` is the token type that ends the list, and
  3522. // `allowEmpty` can be turned on to allow subsequent commas with
  3523. // nothing in between them to be parsed as `null` (which is needed
  3524. // for array literals).
  3525. pp$3.parseExprList = function(close, allowTrailingComma, allowEmpty, refDestructuringErrors) {
  3526. var this$1 = this;
  3527. var elts = [], first = true;
  3528. while (!this.eat(close)) {
  3529. if (!first) {
  3530. this$1.expect(types.comma);
  3531. if (allowTrailingComma && this$1.afterTrailingComma(close)) { break }
  3532. } else { first = false; }
  3533. var elt = (void 0);
  3534. if (allowEmpty && this$1.type === types.comma)
  3535. { elt = null; }
  3536. else if (this$1.type === types.ellipsis) {
  3537. elt = this$1.parseSpread(refDestructuringErrors);
  3538. if (refDestructuringErrors && this$1.type === types.comma && refDestructuringErrors.trailingComma < 0)
  3539. { refDestructuringErrors.trailingComma = this$1.start; }
  3540. } else {
  3541. elt = this$1.parseMaybeAssign(false, refDestructuringErrors);
  3542. }
  3543. elts.push(elt);
  3544. }
  3545. return elts
  3546. };
  3547. // Parse the next token as an identifier. If `liberal` is true (used
  3548. // when parsing properties), it will also convert keywords into
  3549. // identifiers.
  3550. pp$3.checkUnreserved = function(ref) {
  3551. var start = ref.start;
  3552. var end = ref.end;
  3553. var name = ref.name;
  3554. if (this.inGenerator && name === "yield")
  3555. { this.raiseRecoverable(start, "Can not use 'yield' as identifier inside a generator"); }
  3556. if (this.inAsync && name === "await")
  3557. { this.raiseRecoverable(start, "Can not use 'await' as identifier inside an async function"); }
  3558. if (this.isKeyword(name))
  3559. { this.raise(start, ("Unexpected keyword '" + name + "'")); }
  3560. if (this.options.ecmaVersion < 6 &&
  3561. this.input.slice(start, end).indexOf("\\") != -1) { return }
  3562. var re = this.strict ? this.reservedWordsStrict : this.reservedWords;
  3563. if (re.test(name))
  3564. { this.raiseRecoverable(start, ("The keyword '" + name + "' is reserved")); }
  3565. };
  3566. pp$3.parseIdent = function(liberal, isBinding) {
  3567. var node = this.startNode();
  3568. if (liberal && this.options.allowReserved == "never") { liberal = false; }
  3569. if (this.type === types.name) {
  3570. node.name = this.value;
  3571. } else if (this.type.keyword) {
  3572. node.name = this.type.keyword;
  3573. } else {
  3574. this.unexpected();
  3575. }
  3576. this.next();
  3577. this.finishNode(node, "Identifier");
  3578. if (!liberal) { this.checkUnreserved(node); }
  3579. return node
  3580. };
  3581. // Parses yield expression inside generator.
  3582. pp$3.parseYield = function() {
  3583. if (!this.yieldPos) { this.yieldPos = this.start; }
  3584. var node = this.startNode();
  3585. this.next();
  3586. if (this.type == types.semi || this.canInsertSemicolon() || (this.type != types.star && !this.type.startsExpr)) {
  3587. node.delegate = false;
  3588. node.argument = null;
  3589. } else {
  3590. node.delegate = this.eat(types.star);
  3591. node.argument = this.parseMaybeAssign();
  3592. }
  3593. return this.finishNode(node, "YieldExpression")
  3594. };
  3595. pp$3.parseAwait = function() {
  3596. if (!this.awaitPos) { this.awaitPos = this.start; }
  3597. var node = this.startNode();
  3598. this.next();
  3599. node.argument = this.parseMaybeUnary(null, true);
  3600. return this.finishNode(node, "AwaitExpression")
  3601. };
  3602. var pp$4 = Parser.prototype;
  3603. // This function is used to raise exceptions on parse errors. It
  3604. // takes an offset integer (into the current `input`) to indicate
  3605. // the location of the error, attaches the position to the end
  3606. // of the error message, and then raises a `SyntaxError` with that
  3607. // message.
  3608. pp$4.raise = function(pos, message) {
  3609. var loc = getLineInfo(this.input, pos);
  3610. message += " (" + loc.line + ":" + loc.column + ")";
  3611. var err = new SyntaxError(message);
  3612. err.pos = pos; err.loc = loc; err.raisedAt = this.pos;
  3613. throw err
  3614. };
  3615. pp$4.raiseRecoverable = pp$4.raise;
  3616. pp$4.curPosition = function() {
  3617. if (this.options.locations) {
  3618. return new Position(this.curLine, this.pos - this.lineStart)
  3619. }
  3620. };
  3621. var pp$5 = Parser.prototype;
  3622. // Object.assign polyfill
  3623. var assign$1 = Object.assign || function(target) {
  3624. var sources = [], len = arguments.length - 1;
  3625. while ( len-- > 0 ) { sources[ len ] = arguments[ len + 1 ]; }
  3626. for (var i = 0, list = sources; i < list.length; i += 1) {
  3627. var source = list[i];
  3628. for (var key in source) {
  3629. if (has(source, key)) {
  3630. target[key] = source[key];
  3631. }
  3632. }
  3633. }
  3634. return target
  3635. };
  3636. // The functions in this module keep track of declared variables in the current scope in order to detect duplicate variable names.
  3637. pp$5.enterFunctionScope = function() {
  3638. // var: a hash of var-declared names in the current lexical scope
  3639. // lexical: a hash of lexically-declared names in the current lexical scope
  3640. // childVar: a hash of var-declared names in all child lexical scopes of the current lexical scope (within the current function scope)
  3641. // parentLexical: a hash of lexically-declared names in all parent lexical scopes of the current lexical scope (within the current function scope)
  3642. this.scopeStack.push({var: {}, lexical: {}, childVar: {}, parentLexical: {}});
  3643. };
  3644. pp$5.exitFunctionScope = function() {
  3645. this.scopeStack.pop();
  3646. };
  3647. pp$5.enterLexicalScope = function() {
  3648. var parentScope = this.scopeStack[this.scopeStack.length - 1];
  3649. var childScope = {var: {}, lexical: {}, childVar: {}, parentLexical: {}};
  3650. this.scopeStack.push(childScope);
  3651. assign$1(childScope.parentLexical, parentScope.lexical, parentScope.parentLexical);
  3652. };
  3653. pp$5.exitLexicalScope = function() {
  3654. var childScope = this.scopeStack.pop();
  3655. var parentScope = this.scopeStack[this.scopeStack.length - 1];
  3656. assign$1(parentScope.childVar, childScope.var, childScope.childVar);
  3657. };
  3658. /**
  3659. * A name can be declared with `var` if there are no variables with the same name declared with `let`/`const`
  3660. * in the current lexical scope or any of the parent lexical scopes in this function.
  3661. */
  3662. pp$5.canDeclareVarName = function(name) {
  3663. var currentScope = this.scopeStack[this.scopeStack.length - 1];
  3664. return !has(currentScope.lexical, name) && !has(currentScope.parentLexical, name)
  3665. };
  3666. /**
  3667. * A name can be declared with `let`/`const` if there are no variables with the same name declared with `let`/`const`
  3668. * in the current scope, and there are no variables with the same name declared with `var` in the current scope or in
  3669. * any child lexical scopes in this function.
  3670. */
  3671. pp$5.canDeclareLexicalName = function(name) {
  3672. var currentScope = this.scopeStack[this.scopeStack.length - 1];
  3673. return !has(currentScope.lexical, name) && !has(currentScope.var, name) && !has(currentScope.childVar, name)
  3674. };
  3675. pp$5.declareVarName = function(name) {
  3676. this.scopeStack[this.scopeStack.length - 1].var[name] = true;
  3677. };
  3678. pp$5.declareLexicalName = function(name) {
  3679. this.scopeStack[this.scopeStack.length - 1].lexical[name] = true;
  3680. };
  3681. var Node = function Node(parser, pos, loc) {
  3682. this.type = "";
  3683. this.start = pos;
  3684. this.end = 0;
  3685. if (parser.options.locations)
  3686. { this.loc = new SourceLocation(parser, loc); }
  3687. if (parser.options.directSourceFile)
  3688. { this.sourceFile = parser.options.directSourceFile; }
  3689. if (parser.options.ranges)
  3690. { this.range = [pos, 0]; }
  3691. };
  3692. // Start an AST node, attaching a start offset.
  3693. var pp$6 = Parser.prototype;
  3694. pp$6.startNode = function() {
  3695. return new Node(this, this.start, this.startLoc)
  3696. };
  3697. pp$6.startNodeAt = function(pos, loc) {
  3698. return new Node(this, pos, loc)
  3699. };
  3700. // Finish an AST node, adding `type` and `end` properties.
  3701. function finishNodeAt(node, type, pos, loc) {
  3702. node.type = type;
  3703. node.end = pos;
  3704. if (this.options.locations)
  3705. { node.loc.end = loc; }
  3706. if (this.options.ranges)
  3707. { node.range[1] = pos; }
  3708. return node
  3709. }
  3710. pp$6.finishNode = function(node, type) {
  3711. return finishNodeAt.call(this, node, type, this.lastTokEnd, this.lastTokEndLoc)
  3712. };
  3713. // Finish node at given position
  3714. pp$6.finishNodeAt = function(node, type, pos, loc) {
  3715. return finishNodeAt.call(this, node, type, pos, loc)
  3716. };
  3717. // The algorithm used to determine whether a regexp can appear at a
  3718. // given point in the program is loosely based on sweet.js' approach.
  3719. // See https://github.com/mozilla/sweet.js/wiki/design
  3720. var TokContext = function TokContext(token, isExpr, preserveSpace, override, generator) {
  3721. this.token = token;
  3722. this.isExpr = !!isExpr;
  3723. this.preserveSpace = !!preserveSpace;
  3724. this.override = override;
  3725. this.generator = !!generator;
  3726. };
  3727. var types$1 = {
  3728. b_stat: new TokContext("{", false),
  3729. b_expr: new TokContext("{", true),
  3730. b_tmpl: new TokContext("${", false),
  3731. p_stat: new TokContext("(", false),
  3732. p_expr: new TokContext("(", true),
  3733. q_tmpl: new TokContext("`", true, true, function (p) { return p.tryReadTemplateToken(); }),
  3734. f_stat: new TokContext("function", false),
  3735. f_expr: new TokContext("function", true),
  3736. f_expr_gen: new TokContext("function", true, false, null, true),
  3737. f_gen: new TokContext("function", false, false, null, true)
  3738. };
  3739. var pp$7 = Parser.prototype;
  3740. pp$7.initialContext = function() {
  3741. return [types$1.b_stat]
  3742. };
  3743. pp$7.braceIsBlock = function(prevType) {
  3744. var parent = this.curContext();
  3745. if (parent === types$1.f_expr || parent === types$1.f_stat)
  3746. { return true }
  3747. if (prevType === types.colon && (parent === types$1.b_stat || parent === types$1.b_expr))
  3748. { return !parent.isExpr }
  3749. // The check for `tt.name && exprAllowed` detects whether we are
  3750. // after a `yield` or `of` construct. See the `updateContext` for
  3751. // `tt.name`.
  3752. if (prevType === types._return || prevType == types.name && this.exprAllowed)
  3753. { return lineBreak.test(this.input.slice(this.lastTokEnd, this.start)) }
  3754. if (prevType === types._else || prevType === types.semi || prevType === types.eof || prevType === types.parenR || prevType == types.arrow)
  3755. { return true }
  3756. if (prevType == types.braceL)
  3757. { return parent === types$1.b_stat }
  3758. if (prevType == types._var || prevType == types.name)
  3759. { return false }
  3760. return !this.exprAllowed
  3761. };
  3762. pp$7.inGeneratorContext = function() {
  3763. var this$1 = this;
  3764. for (var i = this.context.length - 1; i >= 1; i--) {
  3765. var context = this$1.context[i];
  3766. if (context.token === "function")
  3767. { return context.generator }
  3768. }
  3769. return false
  3770. };
  3771. pp$7.updateContext = function(prevType) {
  3772. var update, type = this.type;
  3773. if (type.keyword && prevType == types.dot)
  3774. { this.exprAllowed = false; }
  3775. else if (update = type.updateContext)
  3776. { update.call(this, prevType); }
  3777. else
  3778. { this.exprAllowed = type.beforeExpr; }
  3779. };
  3780. // Token-specific context update code
  3781. types.parenR.updateContext = types.braceR.updateContext = function() {
  3782. if (this.context.length == 1) {
  3783. this.exprAllowed = true;
  3784. return
  3785. }
  3786. var out = this.context.pop();
  3787. if (out === types$1.b_stat && this.curContext().token === "function") {
  3788. out = this.context.pop();
  3789. }
  3790. this.exprAllowed = !out.isExpr;
  3791. };
  3792. types.braceL.updateContext = function(prevType) {
  3793. this.context.push(this.braceIsBlock(prevType) ? types$1.b_stat : types$1.b_expr);
  3794. this.exprAllowed = true;
  3795. };
  3796. types.dollarBraceL.updateContext = function() {
  3797. this.context.push(types$1.b_tmpl);
  3798. this.exprAllowed = true;
  3799. };
  3800. types.parenL.updateContext = function(prevType) {
  3801. var statementParens = prevType === types._if || prevType === types._for || prevType === types._with || prevType === types._while;
  3802. this.context.push(statementParens ? types$1.p_stat : types$1.p_expr);
  3803. this.exprAllowed = true;
  3804. };
  3805. types.incDec.updateContext = function() {
  3806. // tokExprAllowed stays unchanged
  3807. };
  3808. types._function.updateContext = types._class.updateContext = function(prevType) {
  3809. if (prevType.beforeExpr && prevType !== types.semi && prevType !== types._else &&
  3810. !((prevType === types.colon || prevType === types.braceL) && this.curContext() === types$1.b_stat))
  3811. { this.context.push(types$1.f_expr); }
  3812. else
  3813. { this.context.push(types$1.f_stat); }
  3814. this.exprAllowed = false;
  3815. };
  3816. types.backQuote.updateContext = function() {
  3817. if (this.curContext() === types$1.q_tmpl)
  3818. { this.context.pop(); }
  3819. else
  3820. { this.context.push(types$1.q_tmpl); }
  3821. this.exprAllowed = false;
  3822. };
  3823. types.star.updateContext = function(prevType) {
  3824. if (prevType == types._function) {
  3825. var index = this.context.length - 1;
  3826. if (this.context[index] === types$1.f_expr)
  3827. { this.context[index] = types$1.f_expr_gen; }
  3828. else
  3829. { this.context[index] = types$1.f_gen; }
  3830. }
  3831. this.exprAllowed = true;
  3832. };
  3833. types.name.updateContext = function(prevType) {
  3834. var allowed = false;
  3835. if (this.options.ecmaVersion >= 6) {
  3836. if (this.value == "of" && !this.exprAllowed ||
  3837. this.value == "yield" && this.inGeneratorContext())
  3838. { allowed = true; }
  3839. }
  3840. this.exprAllowed = allowed;
  3841. };
  3842. // Object type used to represent tokens. Note that normally, tokens
  3843. // simply exist as properties on the parser object. This is only
  3844. // used for the onToken callback and the external tokenizer.
  3845. var Token = function Token(p) {
  3846. this.type = p.type;
  3847. this.value = p.value;
  3848. this.start = p.start;
  3849. this.end = p.end;
  3850. if (p.options.locations)
  3851. { this.loc = new SourceLocation(p, p.startLoc, p.endLoc); }
  3852. if (p.options.ranges)
  3853. { this.range = [p.start, p.end]; }
  3854. };
  3855. // ## Tokenizer
  3856. var pp$8 = Parser.prototype;
  3857. // Are we running under Rhino?
  3858. var isRhino = typeof Packages == "object" && Object.prototype.toString.call(Packages) == "[object JavaPackage]";
  3859. // Move to the next token
  3860. pp$8.next = function() {
  3861. if (this.options.onToken)
  3862. { this.options.onToken(new Token(this)); }
  3863. this.lastTokEnd = this.end;
  3864. this.lastTokStart = this.start;
  3865. this.lastTokEndLoc = this.endLoc;
  3866. this.lastTokStartLoc = this.startLoc;
  3867. this.nextToken();
  3868. };
  3869. pp$8.getToken = function() {
  3870. this.next();
  3871. return new Token(this)
  3872. };
  3873. // If we're in an ES6 environment, make parsers iterable
  3874. if (typeof Symbol !== "undefined")
  3875. { pp$8[Symbol.iterator] = function() {
  3876. var this$1 = this;
  3877. return {
  3878. next: function () {
  3879. var token = this$1.getToken();
  3880. return {
  3881. done: token.type === types.eof,
  3882. value: token
  3883. }
  3884. }
  3885. }
  3886. }; }
  3887. // Toggle strict mode. Re-reads the next number or string to please
  3888. // pedantic tests (`"use strict"; 010;` should fail).
  3889. pp$8.curContext = function() {
  3890. return this.context[this.context.length - 1]
  3891. };
  3892. // Read a single token, updating the parser object's token-related
  3893. // properties.
  3894. pp$8.nextToken = function() {
  3895. var curContext = this.curContext();
  3896. if (!curContext || !curContext.preserveSpace) { this.skipSpace(); }
  3897. this.start = this.pos;
  3898. if (this.options.locations) { this.startLoc = this.curPosition(); }
  3899. if (this.pos >= this.input.length) { return this.finishToken(types.eof) }
  3900. if (curContext.override) { return curContext.override(this) }
  3901. else { this.readToken(this.fullCharCodeAtPos()); }
  3902. };
  3903. pp$8.readToken = function(code) {
  3904. // Identifier or keyword. '\uXXXX' sequences are allowed in
  3905. // identifiers, so '\' also dispatches to that.
  3906. if (isIdentifierStart(code, this.options.ecmaVersion >= 6) || code === 92 /* '\' */)
  3907. { return this.readWord() }
  3908. return this.getTokenFromCode(code)
  3909. };
  3910. pp$8.fullCharCodeAtPos = function() {
  3911. var code = this.input.charCodeAt(this.pos);
  3912. if (code <= 0xd7ff || code >= 0xe000) { return code }
  3913. var next = this.input.charCodeAt(this.pos + 1);
  3914. return (code << 10) + next - 0x35fdc00
  3915. };
  3916. pp$8.skipBlockComment = function() {
  3917. var this$1 = this;
  3918. var startLoc = this.options.onComment && this.curPosition();
  3919. var start = this.pos, end = this.input.indexOf("*/", this.pos += 2);
  3920. if (end === -1) { this.raise(this.pos - 2, "Unterminated comment"); }
  3921. this.pos = end + 2;
  3922. if (this.options.locations) {
  3923. lineBreakG.lastIndex = start;
  3924. var match;
  3925. while ((match = lineBreakG.exec(this.input)) && match.index < this.pos) {
  3926. ++this$1.curLine;
  3927. this$1.lineStart = match.index + match[0].length;
  3928. }
  3929. }
  3930. if (this.options.onComment)
  3931. { this.options.onComment(true, this.input.slice(start + 2, end), start, this.pos,
  3932. startLoc, this.curPosition()); }
  3933. };
  3934. pp$8.skipLineComment = function(startSkip) {
  3935. var this$1 = this;
  3936. var start = this.pos;
  3937. var startLoc = this.options.onComment && this.curPosition();
  3938. var ch = this.input.charCodeAt(this.pos += startSkip);
  3939. while (this.pos < this.input.length && !isNewLine(ch)) {
  3940. ch = this$1.input.charCodeAt(++this$1.pos);
  3941. }
  3942. if (this.options.onComment)
  3943. { this.options.onComment(false, this.input.slice(start + startSkip, this.pos), start, this.pos,
  3944. startLoc, this.curPosition()); }
  3945. };
  3946. // Called at the start of the parse and after every token. Skips
  3947. // whitespace and comments, and.
  3948. pp$8.skipSpace = function() {
  3949. var this$1 = this;
  3950. loop: while (this.pos < this.input.length) {
  3951. var ch = this$1.input.charCodeAt(this$1.pos);
  3952. switch (ch) {
  3953. case 32: case 160: // ' '
  3954. ++this$1.pos;
  3955. break
  3956. case 13:
  3957. if (this$1.input.charCodeAt(this$1.pos + 1) === 10) {
  3958. ++this$1.pos;
  3959. }
  3960. case 10: case 8232: case 8233:
  3961. ++this$1.pos;
  3962. if (this$1.options.locations) {
  3963. ++this$1.curLine;
  3964. this$1.lineStart = this$1.pos;
  3965. }
  3966. break
  3967. case 47: // '/'
  3968. switch (this$1.input.charCodeAt(this$1.pos + 1)) {
  3969. case 42: // '*'
  3970. this$1.skipBlockComment();
  3971. break
  3972. case 47:
  3973. this$1.skipLineComment(2);
  3974. break
  3975. default:
  3976. break loop
  3977. }
  3978. break
  3979. default:
  3980. if (ch > 8 && ch < 14 || ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch))) {
  3981. ++this$1.pos;
  3982. } else {
  3983. break loop
  3984. }
  3985. }
  3986. }
  3987. };
  3988. // Called at the end of every token. Sets `end`, `val`, and
  3989. // maintains `context` and `exprAllowed`, and skips the space after
  3990. // the token, so that the next one's `start` will point at the
  3991. // right position.
  3992. pp$8.finishToken = function(type, val) {
  3993. this.end = this.pos;
  3994. if (this.options.locations) { this.endLoc = this.curPosition(); }
  3995. var prevType = this.type;
  3996. this.type = type;
  3997. this.value = val;
  3998. this.updateContext(prevType);
  3999. };
  4000. // ### Token reading
  4001. // This is the function that is called to fetch the next token. It
  4002. // is somewhat obscure, because it works in character codes rather
  4003. // than characters, and because operator parsing has been inlined
  4004. // into it.
  4005. //
  4006. // All in the name of speed.
  4007. //
  4008. pp$8.readToken_dot = function() {
  4009. var next = this.input.charCodeAt(this.pos + 1);
  4010. if (next >= 48 && next <= 57) { return this.readNumber(true) }
  4011. var next2 = this.input.charCodeAt(this.pos + 2);
  4012. if (this.options.ecmaVersion >= 6 && next === 46 && next2 === 46) { // 46 = dot '.'
  4013. this.pos += 3;
  4014. return this.finishToken(types.ellipsis)
  4015. } else {
  4016. ++this.pos;
  4017. return this.finishToken(types.dot)
  4018. }
  4019. };
  4020. pp$8.readToken_slash = function() { // '/'
  4021. var next = this.input.charCodeAt(this.pos + 1);
  4022. if (this.exprAllowed) { ++this.pos; return this.readRegexp() }
  4023. if (next === 61) { return this.finishOp(types.assign, 2) }
  4024. return this.finishOp(types.slash, 1)
  4025. };
  4026. pp$8.readToken_mult_modulo_exp = function(code) { // '%*'
  4027. var next = this.input.charCodeAt(this.pos + 1);
  4028. var size = 1;
  4029. var tokentype = code === 42 ? types.star : types.modulo;
  4030. // exponentiation operator ** and **=
  4031. if (this.options.ecmaVersion >= 7 && next === 42) {
  4032. ++size;
  4033. tokentype = types.starstar;
  4034. next = this.input.charCodeAt(this.pos + 2);
  4035. }
  4036. if (next === 61) { return this.finishOp(types.assign, size + 1) }
  4037. return this.finishOp(tokentype, size)
  4038. };
  4039. pp$8.readToken_pipe_amp = function(code) { // '|&'
  4040. var next = this.input.charCodeAt(this.pos + 1);
  4041. if (next === code) { return this.finishOp(code === 124 ? types.logicalOR : types.logicalAND, 2) }
  4042. if (next === 61) { return this.finishOp(types.assign, 2) }
  4043. return this.finishOp(code === 124 ? types.bitwiseOR : types.bitwiseAND, 1)
  4044. };
  4045. pp$8.readToken_caret = function() { // '^'
  4046. var next = this.input.charCodeAt(this.pos + 1);
  4047. if (next === 61) { return this.finishOp(types.assign, 2) }
  4048. return this.finishOp(types.bitwiseXOR, 1)
  4049. };
  4050. pp$8.readToken_plus_min = function(code) { // '+-'
  4051. var next = this.input.charCodeAt(this.pos + 1);
  4052. if (next === code) {
  4053. if (next == 45 && !this.inModule && this.input.charCodeAt(this.pos + 2) == 62 &&
  4054. (this.lastTokEnd === 0 || lineBreak.test(this.input.slice(this.lastTokEnd, this.pos)))) {
  4055. // A `-->` line comment
  4056. this.skipLineComment(3);
  4057. this.skipSpace();
  4058. return this.nextToken()
  4059. }
  4060. return this.finishOp(types.incDec, 2)
  4061. }
  4062. if (next === 61) { return this.finishOp(types.assign, 2) }
  4063. return this.finishOp(types.plusMin, 1)
  4064. };
  4065. pp$8.readToken_lt_gt = function(code) { // '<>'
  4066. var next = this.input.charCodeAt(this.pos + 1);
  4067. var size = 1;
  4068. if (next === code) {
  4069. size = code === 62 && this.input.charCodeAt(this.pos + 2) === 62 ? 3 : 2;
  4070. if (this.input.charCodeAt(this.pos + size) === 61) { return this.finishOp(types.assign, size + 1) }
  4071. return this.finishOp(types.bitShift, size)
  4072. }
  4073. if (next == 33 && code == 60 && !this.inModule && this.input.charCodeAt(this.pos + 2) == 45 &&
  4074. this.input.charCodeAt(this.pos + 3) == 45) {
  4075. // `<!--`, an XML-style comment that should be interpreted as a line comment
  4076. this.skipLineComment(4);
  4077. this.skipSpace();
  4078. return this.nextToken()
  4079. }
  4080. if (next === 61) { size = 2; }
  4081. return this.finishOp(types.relational, size)
  4082. };
  4083. pp$8.readToken_eq_excl = function(code) { // '=!'
  4084. var next = this.input.charCodeAt(this.pos + 1);
  4085. if (next === 61) { return this.finishOp(types.equality, this.input.charCodeAt(this.pos + 2) === 61 ? 3 : 2) }
  4086. if (code === 61 && next === 62 && this.options.ecmaVersion >= 6) { // '=>'
  4087. this.pos += 2;
  4088. return this.finishToken(types.arrow)
  4089. }
  4090. return this.finishOp(code === 61 ? types.eq : types.prefix, 1)
  4091. };
  4092. pp$8.getTokenFromCode = function(code) {
  4093. switch (code) {
  4094. // The interpretation of a dot depends on whether it is followed
  4095. // by a digit or another two dots.
  4096. case 46: // '.'
  4097. return this.readToken_dot()
  4098. // Punctuation tokens.
  4099. case 40: ++this.pos; return this.finishToken(types.parenL)
  4100. case 41: ++this.pos; return this.finishToken(types.parenR)
  4101. case 59: ++this.pos; return this.finishToken(types.semi)
  4102. case 44: ++this.pos; return this.finishToken(types.comma)
  4103. case 91: ++this.pos; return this.finishToken(types.bracketL)
  4104. case 93: ++this.pos; return this.finishToken(types.bracketR)
  4105. case 123: ++this.pos; return this.finishToken(types.braceL)
  4106. case 125: ++this.pos; return this.finishToken(types.braceR)
  4107. case 58: ++this.pos; return this.finishToken(types.colon)
  4108. case 63: ++this.pos; return this.finishToken(types.question)
  4109. case 96: // '`'
  4110. if (this.options.ecmaVersion < 6) { break }
  4111. ++this.pos;
  4112. return this.finishToken(types.backQuote)
  4113. case 48: // '0'
  4114. var next = this.input.charCodeAt(this.pos + 1);
  4115. if (next === 120 || next === 88) { return this.readRadixNumber(16) } // '0x', '0X' - hex number
  4116. if (this.options.ecmaVersion >= 6) {
  4117. if (next === 111 || next === 79) { return this.readRadixNumber(8) } // '0o', '0O' - octal number
  4118. if (next === 98 || next === 66) { return this.readRadixNumber(2) } // '0b', '0B' - binary number
  4119. }
  4120. // Anything else beginning with a digit is an integer, octal
  4121. // number, or float.
  4122. case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: // 1-9
  4123. return this.readNumber(false)
  4124. // Quotes produce strings.
  4125. case 34: case 39: // '"', "'"
  4126. return this.readString(code)
  4127. // Operators are parsed inline in tiny state machines. '=' (61) is
  4128. // often referred to. `finishOp` simply skips the amount of
  4129. // characters it is given as second argument, and returns a token
  4130. // of the type given by its first argument.
  4131. case 47: // '/'
  4132. return this.readToken_slash()
  4133. case 37: case 42: // '%*'
  4134. return this.readToken_mult_modulo_exp(code)
  4135. case 124: case 38: // '|&'
  4136. return this.readToken_pipe_amp(code)
  4137. case 94: // '^'
  4138. return this.readToken_caret()
  4139. case 43: case 45: // '+-'
  4140. return this.readToken_plus_min(code)
  4141. case 60: case 62: // '<>'
  4142. return this.readToken_lt_gt(code)
  4143. case 61: case 33: // '=!'
  4144. return this.readToken_eq_excl(code)
  4145. case 126: // '~'
  4146. return this.finishOp(types.prefix, 1)
  4147. }
  4148. this.raise(this.pos, "Unexpected character '" + codePointToString(code) + "'");
  4149. };
  4150. pp$8.finishOp = function(type, size) {
  4151. var str = this.input.slice(this.pos, this.pos + size);
  4152. this.pos += size;
  4153. return this.finishToken(type, str)
  4154. };
  4155. // Parse a regular expression. Some context-awareness is necessary,
  4156. // since a '/' inside a '[]' set does not end the expression.
  4157. function tryCreateRegexp(src, flags, throwErrorAt, parser) {
  4158. try {
  4159. return new RegExp(src, flags)
  4160. } catch (e) {
  4161. if (throwErrorAt !== undefined) {
  4162. if (e instanceof SyntaxError) { parser.raise(throwErrorAt, "Error parsing regular expression: " + e.message); }
  4163. throw e
  4164. }
  4165. }
  4166. }
  4167. var regexpUnicodeSupport = !!tryCreateRegexp("\uffff", "u");
  4168. pp$8.readRegexp = function() {
  4169. var this$1 = this;
  4170. var escaped, inClass, start = this.pos;
  4171. for (;;) {
  4172. if (this$1.pos >= this$1.input.length) { this$1.raise(start, "Unterminated regular expression"); }
  4173. var ch = this$1.input.charAt(this$1.pos);
  4174. if (lineBreak.test(ch)) { this$1.raise(start, "Unterminated regular expression"); }
  4175. if (!escaped) {
  4176. if (ch === "[") { inClass = true; }
  4177. else if (ch === "]" && inClass) { inClass = false; }
  4178. else if (ch === "/" && !inClass) { break }
  4179. escaped = ch === "\\";
  4180. } else { escaped = false; }
  4181. ++this$1.pos;
  4182. }
  4183. var content = this.input.slice(start, this.pos);
  4184. ++this.pos;
  4185. // Need to use `readWord1` because '\uXXXX' sequences are allowed
  4186. // here (don't ask).
  4187. var mods = this.readWord1();
  4188. var tmp = content, tmpFlags = "";
  4189. if (mods) {
  4190. var validFlags = /^[gim]*$/;
  4191. if (this.options.ecmaVersion >= 6) { validFlags = /^[gimuy]*$/; }
  4192. if (!validFlags.test(mods)) { this.raise(start, "Invalid regular expression flag"); }
  4193. if (mods.indexOf("u") >= 0) {
  4194. if (regexpUnicodeSupport) {
  4195. tmpFlags = "u";
  4196. } else {
  4197. // Replace each astral symbol and every Unicode escape sequence that
  4198. // possibly represents an astral symbol or a paired surrogate with a
  4199. // single ASCII symbol to avoid throwing on regular expressions that
  4200. // are only valid in combination with the `/u` flag.
  4201. // Note: replacing with the ASCII symbol `x` might cause false
  4202. // negatives in unlikely scenarios. For example, `[\u{61}-b]` is a
  4203. // perfectly valid pattern that is equivalent to `[a-b]`, but it would
  4204. // be replaced by `[x-b]` which throws an error.
  4205. tmp = tmp.replace(/\\u\{([0-9a-fA-F]+)\}/g, function (_match, code, offset) {
  4206. code = Number("0x" + code);
  4207. if (code > 0x10FFFF) { this$1.raise(start + offset + 3, "Code point out of bounds"); }
  4208. return "x"
  4209. });
  4210. tmp = tmp.replace(/\\u([a-fA-F0-9]{4})|[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "x");
  4211. tmpFlags = tmpFlags.replace("u", "");
  4212. }
  4213. }
  4214. }
  4215. // Detect invalid regular expressions.
  4216. var value = null;
  4217. // Rhino's regular expression parser is flaky and throws uncatchable exceptions,
  4218. // so don't do detection if we are running under Rhino
  4219. if (!isRhino) {
  4220. tryCreateRegexp(tmp, tmpFlags, start, this);
  4221. // Get a regular expression object for this pattern-flag pair, or `null` in
  4222. // case the current environment doesn't support the flags it uses.
  4223. value = tryCreateRegexp(content, mods);
  4224. }
  4225. return this.finishToken(types.regexp, {pattern: content, flags: mods, value: value})
  4226. };
  4227. // Read an integer in the given radix. Return null if zero digits
  4228. // were read, the integer value otherwise. When `len` is given, this
  4229. // will return `null` unless the integer has exactly `len` digits.
  4230. pp$8.readInt = function(radix, len) {
  4231. var this$1 = this;
  4232. var start = this.pos, total = 0;
  4233. for (var i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  4234. var code = this$1.input.charCodeAt(this$1.pos), val = (void 0);
  4235. if (code >= 97) { val = code - 97 + 10; } // a
  4236. else if (code >= 65) { val = code - 65 + 10; } // A
  4237. else if (code >= 48 && code <= 57) { val = code - 48; } // 0-9
  4238. else { val = Infinity; }
  4239. if (val >= radix) { break }
  4240. ++this$1.pos;
  4241. total = total * radix + val;
  4242. }
  4243. if (this.pos === start || len != null && this.pos - start !== len) { return null }
  4244. return total
  4245. };
  4246. pp$8.readRadixNumber = function(radix) {
  4247. this.pos += 2; // 0x
  4248. var val = this.readInt(radix);
  4249. if (val == null) { this.raise(this.start + 2, "Expected number in radix " + radix); }
  4250. if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number"); }
  4251. return this.finishToken(types.num, val)
  4252. };
  4253. // Read an integer, octal integer, or floating-point number.
  4254. pp$8.readNumber = function(startsWithDot) {
  4255. var start = this.pos, isFloat = false, octal = this.input.charCodeAt(this.pos) === 48;
  4256. if (!startsWithDot && this.readInt(10) === null) { this.raise(start, "Invalid number"); }
  4257. if (octal && this.pos == start + 1) { octal = false; }
  4258. var next = this.input.charCodeAt(this.pos);
  4259. if (next === 46 && !octal) { // '.'
  4260. ++this.pos;
  4261. this.readInt(10);
  4262. isFloat = true;
  4263. next = this.input.charCodeAt(this.pos);
  4264. }
  4265. if ((next === 69 || next === 101) && !octal) { // 'eE'
  4266. next = this.input.charCodeAt(++this.pos);
  4267. if (next === 43 || next === 45) { ++this.pos; } // '+-'
  4268. if (this.readInt(10) === null) { this.raise(start, "Invalid number"); }
  4269. isFloat = true;
  4270. }
  4271. if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number"); }
  4272. var str = this.input.slice(start, this.pos), val;
  4273. if (isFloat) { val = parseFloat(str); }
  4274. else if (!octal || str.length === 1) { val = parseInt(str, 10); }
  4275. else if (this.strict) { this.raise(start, "Invalid number"); }
  4276. else if (/[89]/.test(str)) { val = parseInt(str, 10); }
  4277. else { val = parseInt(str, 8); }
  4278. return this.finishToken(types.num, val)
  4279. };
  4280. // Read a string value, interpreting backslash-escapes.
  4281. pp$8.readCodePoint = function() {
  4282. var ch = this.input.charCodeAt(this.pos), code;
  4283. if (ch === 123) { // '{'
  4284. if (this.options.ecmaVersion < 6) { this.unexpected(); }
  4285. var codePos = ++this.pos;
  4286. code = this.readHexChar(this.input.indexOf("}", this.pos) - this.pos);
  4287. ++this.pos;
  4288. if (code > 0x10FFFF) { this.invalidStringToken(codePos, "Code point out of bounds"); }
  4289. } else {
  4290. code = this.readHexChar(4);
  4291. }
  4292. return code
  4293. };
  4294. function codePointToString(code) {
  4295. // UTF-16 Decoding
  4296. if (code <= 0xFFFF) { return String.fromCharCode(code) }
  4297. code -= 0x10000;
  4298. return String.fromCharCode((code >> 10) + 0xD800, (code & 1023) + 0xDC00)
  4299. }
  4300. pp$8.readString = function(quote) {
  4301. var this$1 = this;
  4302. var out = "", chunkStart = ++this.pos;
  4303. for (;;) {
  4304. if (this$1.pos >= this$1.input.length) { this$1.raise(this$1.start, "Unterminated string constant"); }
  4305. var ch = this$1.input.charCodeAt(this$1.pos);
  4306. if (ch === quote) { break }
  4307. if (ch === 92) { // '\'
  4308. out += this$1.input.slice(chunkStart, this$1.pos);
  4309. out += this$1.readEscapedChar(false);
  4310. chunkStart = this$1.pos;
  4311. } else {
  4312. if (isNewLine(ch)) { this$1.raise(this$1.start, "Unterminated string constant"); }
  4313. ++this$1.pos;
  4314. }
  4315. }
  4316. out += this.input.slice(chunkStart, this.pos++);
  4317. return this.finishToken(types.string, out)
  4318. };
  4319. // Reads template string tokens.
  4320. var INVALID_TEMPLATE_ESCAPE_ERROR = {};
  4321. pp$8.tryReadTemplateToken = function() {
  4322. this.inTemplateElement = true;
  4323. try {
  4324. this.readTmplToken();
  4325. } catch (err) {
  4326. if (err === INVALID_TEMPLATE_ESCAPE_ERROR) {
  4327. this.readInvalidTemplateToken();
  4328. } else {
  4329. throw err
  4330. }
  4331. }
  4332. this.inTemplateElement = false;
  4333. };
  4334. pp$8.invalidStringToken = function(position, message) {
  4335. if (this.inTemplateElement && this.options.ecmaVersion >= 9) {
  4336. throw INVALID_TEMPLATE_ESCAPE_ERROR
  4337. } else {
  4338. this.raise(position, message);
  4339. }
  4340. };
  4341. pp$8.readTmplToken = function() {
  4342. var this$1 = this;
  4343. var out = "", chunkStart = this.pos;
  4344. for (;;) {
  4345. if (this$1.pos >= this$1.input.length) { this$1.raise(this$1.start, "Unterminated template"); }
  4346. var ch = this$1.input.charCodeAt(this$1.pos);
  4347. if (ch === 96 || ch === 36 && this$1.input.charCodeAt(this$1.pos + 1) === 123) { // '`', '${'
  4348. if (this$1.pos === this$1.start && (this$1.type === types.template || this$1.type === types.invalidTemplate)) {
  4349. if (ch === 36) {
  4350. this$1.pos += 2;
  4351. return this$1.finishToken(types.dollarBraceL)
  4352. } else {
  4353. ++this$1.pos;
  4354. return this$1.finishToken(types.backQuote)
  4355. }
  4356. }
  4357. out += this$1.input.slice(chunkStart, this$1.pos);
  4358. return this$1.finishToken(types.template, out)
  4359. }
  4360. if (ch === 92) { // '\'
  4361. out += this$1.input.slice(chunkStart, this$1.pos);
  4362. out += this$1.readEscapedChar(true);
  4363. chunkStart = this$1.pos;
  4364. } else if (isNewLine(ch)) {
  4365. out += this$1.input.slice(chunkStart, this$1.pos);
  4366. ++this$1.pos;
  4367. switch (ch) {
  4368. case 13:
  4369. if (this$1.input.charCodeAt(this$1.pos) === 10) { ++this$1.pos; }
  4370. case 10:
  4371. out += "\n";
  4372. break
  4373. default:
  4374. out += String.fromCharCode(ch);
  4375. break
  4376. }
  4377. if (this$1.options.locations) {
  4378. ++this$1.curLine;
  4379. this$1.lineStart = this$1.pos;
  4380. }
  4381. chunkStart = this$1.pos;
  4382. } else {
  4383. ++this$1.pos;
  4384. }
  4385. }
  4386. };
  4387. // Reads a template token to search for the end, without validating any escape sequences
  4388. pp$8.readInvalidTemplateToken = function() {
  4389. var this$1 = this;
  4390. for (; this.pos < this.input.length; this.pos++) {
  4391. switch (this$1.input[this$1.pos]) {
  4392. case "\\":
  4393. ++this$1.pos;
  4394. break
  4395. case "$":
  4396. if (this$1.input[this$1.pos + 1] !== "{") {
  4397. break
  4398. }
  4399. // falls through
  4400. case "`":
  4401. return this$1.finishToken(types.invalidTemplate, this$1.input.slice(this$1.start, this$1.pos))
  4402. // no default
  4403. }
  4404. }
  4405. this.raise(this.start, "Unterminated template");
  4406. };
  4407. // Used to read escaped characters
  4408. pp$8.readEscapedChar = function(inTemplate) {
  4409. var ch = this.input.charCodeAt(++this.pos);
  4410. ++this.pos;
  4411. switch (ch) {
  4412. case 110: return "\n" // 'n' -> '\n'
  4413. case 114: return "\r" // 'r' -> '\r'
  4414. case 120: return String.fromCharCode(this.readHexChar(2)) // 'x'
  4415. case 117: return codePointToString(this.readCodePoint()) // 'u'
  4416. case 116: return "\t" // 't' -> '\t'
  4417. case 98: return "\b" // 'b' -> '\b'
  4418. case 118: return "\u000b" // 'v' -> '\u000b'
  4419. case 102: return "\f" // 'f' -> '\f'
  4420. case 13: if (this.input.charCodeAt(this.pos) === 10) { ++this.pos; } // '\r\n'
  4421. case 10: // ' \n'
  4422. if (this.options.locations) { this.lineStart = this.pos; ++this.curLine; }
  4423. return ""
  4424. default:
  4425. if (ch >= 48 && ch <= 55) {
  4426. var octalStr = this.input.substr(this.pos - 1, 3).match(/^[0-7]+/)[0];
  4427. var octal = parseInt(octalStr, 8);
  4428. if (octal > 255) {
  4429. octalStr = octalStr.slice(0, -1);
  4430. octal = parseInt(octalStr, 8);
  4431. }
  4432. if (octalStr !== "0" && (this.strict || inTemplate)) {
  4433. this.invalidStringToken(this.pos - 2, "Octal literal in strict mode");
  4434. }
  4435. this.pos += octalStr.length - 1;
  4436. return String.fromCharCode(octal)
  4437. }
  4438. return String.fromCharCode(ch)
  4439. }
  4440. };
  4441. // Used to read character escape sequences ('\x', '\u', '\U').
  4442. pp$8.readHexChar = function(len) {
  4443. var codePos = this.pos;
  4444. var n = this.readInt(16, len);
  4445. if (n === null) { this.invalidStringToken(codePos, "Bad character escape sequence"); }
  4446. return n
  4447. };
  4448. // Read an identifier, and return it as a string. Sets `this.containsEsc`
  4449. // to whether the word contained a '\u' escape.
  4450. //
  4451. // Incrementally adds only escaped chars, adding other chunks as-is
  4452. // as a micro-optimization.
  4453. pp$8.readWord1 = function() {
  4454. var this$1 = this;
  4455. this.containsEsc = false;
  4456. var word = "", first = true, chunkStart = this.pos;
  4457. var astral = this.options.ecmaVersion >= 6;
  4458. while (this.pos < this.input.length) {
  4459. var ch = this$1.fullCharCodeAtPos();
  4460. if (isIdentifierChar(ch, astral)) {
  4461. this$1.pos += ch <= 0xffff ? 1 : 2;
  4462. } else if (ch === 92) { // "\"
  4463. this$1.containsEsc = true;
  4464. word += this$1.input.slice(chunkStart, this$1.pos);
  4465. var escStart = this$1.pos;
  4466. if (this$1.input.charCodeAt(++this$1.pos) != 117) // "u"
  4467. { this$1.invalidStringToken(this$1.pos, "Expecting Unicode escape sequence \\uXXXX"); }
  4468. ++this$1.pos;
  4469. var esc = this$1.readCodePoint();
  4470. if (!(first ? isIdentifierStart : isIdentifierChar)(esc, astral))
  4471. { this$1.invalidStringToken(escStart, "Invalid Unicode escape"); }
  4472. word += codePointToString(esc);
  4473. chunkStart = this$1.pos;
  4474. } else {
  4475. break
  4476. }
  4477. first = false;
  4478. }
  4479. return word + this.input.slice(chunkStart, this.pos)
  4480. };
  4481. // Read an identifier or keyword token. Will check for reserved
  4482. // words when necessary.
  4483. pp$8.readWord = function() {
  4484. var word = this.readWord1();
  4485. var type = types.name;
  4486. if (this.keywords.test(word)) {
  4487. if (this.containsEsc) { this.raiseRecoverable(this.start, "Escape sequence in keyword " + word); }
  4488. type = keywords$1[word];
  4489. }
  4490. return this.finishToken(type, word)
  4491. };
  4492. // The main exported interface (under `self.acorn` when in the
  4493. // browser) is a `parse` function that takes a code string and
  4494. // returns an abstract syntax tree as specified by [Mozilla parser
  4495. // API][api].
  4496. //
  4497. // [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
  4498. function parse(input, options) {
  4499. return new Parser(options, input).parse()
  4500. }
  4501. function getLocator$1(source, options) {
  4502. if (options === void 0) { options = {}; }
  4503. var offsetLine = options.offsetLine || 0;
  4504. var offsetColumn = options.offsetColumn || 0;
  4505. var originalLines = source.split('\n');
  4506. var start = 0;
  4507. var lineRanges = originalLines.map(function (line, i) {
  4508. var end = start + line.length + 1;
  4509. var range = { start: start, end: end, line: i };
  4510. start = end;
  4511. return range;
  4512. });
  4513. var i = 0;
  4514. function rangeContains(range, index) {
  4515. return range.start <= index && index < range.end;
  4516. }
  4517. function getLocation(range, index) {
  4518. return { line: offsetLine + range.line, column: offsetColumn + index - range.start, character: index };
  4519. }
  4520. function locate(search, startIndex) {
  4521. if (typeof search === 'string') {
  4522. search = source.indexOf(search, startIndex || 0);
  4523. }
  4524. var range = lineRanges[i];
  4525. var d = search >= range.end ? 1 : -1;
  4526. while (range) {
  4527. if (rangeContains(range, search))
  4528. return getLocation(range, search);
  4529. i += d;
  4530. range = lineRanges[i];
  4531. }
  4532. }
  4533. return locate;
  4534. }
  4535. function locate(source, search, options) {
  4536. if (typeof options === 'number') {
  4537. throw new Error('locate takes a { startIndex, offsetLine, offsetColumn } object as the third argument');
  4538. }
  4539. return getLocator$1(source, options)(search, options && options.startIndex);
  4540. }
  4541. const reservedWords$1 = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public'.split( ' ' );
  4542. const builtins = 'Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl'.split( ' ' );
  4543. const blacklisted = blank();
  4544. reservedWords$1.concat( builtins ).forEach( word => blacklisted[ word ] = true );
  4545. const illegalCharacters = /[^$_a-zA-Z0-9]/g;
  4546. const startsWithDigit = str => /\d/.test( str[0] );
  4547. function isLegal ( str ) {
  4548. if ( startsWithDigit(str) || blacklisted[ str ] ) {
  4549. return false;
  4550. }
  4551. if ( illegalCharacters.test(str) ) {
  4552. return false;
  4553. }
  4554. return true;
  4555. }
  4556. function makeLegal ( str ) {
  4557. str = str
  4558. .replace( /-(\w)/g, ( _, letter ) => letter.toUpperCase() )
  4559. .replace( illegalCharacters, '_' );
  4560. if ( startsWithDigit(str) || blacklisted[ str ] ) { str = `_${str}`; }
  4561. return str;
  4562. }
  4563. function spaces ( i ) {
  4564. let result = '';
  4565. while ( i-- ) { result += ' '; }
  4566. return result;
  4567. }
  4568. function tabsToSpaces ( str ) {
  4569. return str.replace( /^\t+/, match => match.split( '\t' ).join( ' ' ) );
  4570. }
  4571. function getCodeFrame ( source, line, column ) {
  4572. let lines = source.split( '\n' );
  4573. const frameStart = Math.max( 0, line - 3 );
  4574. let frameEnd = Math.min( line + 2, lines.length );
  4575. lines = lines.slice( frameStart, frameEnd );
  4576. while ( !/\S/.test( lines[ lines.length - 1 ] ) ) {
  4577. lines.pop();
  4578. frameEnd -= 1;
  4579. }
  4580. const digits = String( frameEnd ).length;
  4581. return lines
  4582. .map( ( str, i ) => {
  4583. const isErrorLine = frameStart + i + 1 === line;
  4584. let lineNum = String( i + frameStart + 1 );
  4585. while ( lineNum.length < digits ) { lineNum = ` ${lineNum}`; }
  4586. if ( isErrorLine ) {
  4587. const indicator = spaces( digits + 2 + tabsToSpaces( str.slice( 0, column ) ).length ) + '^';
  4588. return `${lineNum}: ${tabsToSpaces( str )}\n${indicator}`;
  4589. }
  4590. return `${lineNum}: ${tabsToSpaces( str )}`;
  4591. })
  4592. .join( '\n' );
  4593. }
  4594. function relativeId ( id ) {
  4595. if ( typeof process === 'undefined' || !isAbsolute( id ) ) { return id; }
  4596. return relative( process.cwd(), id );
  4597. }
  4598. class Variable {
  4599. constructor ( name ) {
  4600. this.name = name;
  4601. }
  4602. addCall () {}
  4603. getName () {
  4604. return this.name;
  4605. }
  4606. hasEffectsWhenCalled () {
  4607. return true;
  4608. }
  4609. hasEffectsWhenMutated () {
  4610. return true;
  4611. }
  4612. includeVariable () {
  4613. if ( this.included ) {
  4614. return false;
  4615. }
  4616. this.included = true;
  4617. return true;
  4618. }
  4619. }
  4620. class NamespaceVariable extends Variable {
  4621. constructor ( module ) {
  4622. super( module.basename() );
  4623. this.isNamespace = true;
  4624. this.module = module;
  4625. this.needsNamespaceBlock = false;
  4626. this.originals = blank();
  4627. module.getExports().concat( module.getReexports() ).forEach( name => {
  4628. this.originals[ name ] = module.traceExport( name );
  4629. } );
  4630. }
  4631. addReference ( node ) {
  4632. this.name = node.name;
  4633. }
  4634. assignExpression () {}
  4635. includeVariable () {
  4636. const hasBeenIncluded = super.includeVariable();
  4637. if ( hasBeenIncluded ) {
  4638. this.needsNamespaceBlock = true;
  4639. forOwn( this.originals, original => original.includeVariable() );
  4640. }
  4641. return hasBeenIncluded;
  4642. }
  4643. renderBlock ( es, legacy, indentString ) {
  4644. const members = keys( this.originals ).map( name => {
  4645. const original = this.originals[ name ];
  4646. if ( original.isReassigned && !legacy ) {
  4647. return `${indentString}get ${name} () { return ${original.getName( es )}; }`;
  4648. }
  4649. if ( legacy && ~reservedWords$1.indexOf( name ) ) { name = `'${name}'`; }
  4650. return `${indentString}${name}: ${original.getName( es )}`;
  4651. } );
  4652. const callee = legacy ? `(Object.freeze || Object)` : `Object.freeze`;
  4653. return `${this.module.bundle.varOrConst} ${this.getName( es )} = ${callee}({\n${members.join( ',\n' )}\n});\n\n`;
  4654. }
  4655. }
  4656. function extractNames ( param ) {
  4657. const names = [];
  4658. extractors[ param.type ]( names, param );
  4659. return names;
  4660. }
  4661. const extractors = {
  4662. Identifier ( names, param ) {
  4663. names.push( param.name );
  4664. },
  4665. ObjectPattern ( names, param ) {
  4666. param.properties.forEach( prop => {
  4667. extractors[ prop.value.type ]( names, prop.value );
  4668. });
  4669. },
  4670. ArrayPattern ( names, param ) {
  4671. param.elements.forEach( element => {
  4672. if ( element ) { extractors[ element.type ]( names, element ); }
  4673. });
  4674. },
  4675. RestElement ( names, param ) {
  4676. extractors[ param.argument.type ]( names, param.argument );
  4677. },
  4678. AssignmentPattern ( names, param ) {
  4679. extractors[ param.left.type ]( names, param.left );
  4680. }
  4681. };
  4682. const UNKNOWN_VALUE = { toString: () => '[[UNKNOWN]]' };
  4683. const UNKNOWN_ASSIGNMENT = {
  4684. type: 'UNKNOWN',
  4685. bindCall: () => {},
  4686. hasEffectsWhenCalled: () => true,
  4687. hasEffectsWhenMutated: () => true,
  4688. };
  4689. const UNDEFINED_ASSIGNMENT = {
  4690. type: 'UNDEFINED',
  4691. bindCall: () => {},
  4692. hasEffectsWhenCalled: () => true,
  4693. hasEffectsWhenMutated: () => true,
  4694. };
  4695. const UNKNOWN_OBJECT_LITERAL = {
  4696. type: 'UNKNOWN_OBJECT_LITERAL',
  4697. bindCall: () => {},
  4698. hasEffectsWhenCalled: () => true,
  4699. hasEffectsWhenMutated: () => false,
  4700. };
  4701. const OPTION_IGNORE_BREAK_STATEMENTS = 'IGNORE_BREAK_STATEMENTS';
  4702. const OPTION_IGNORE_RETURN_AWAIT_YIELD = 'IGNORE_RETURN_AWAIT_YIELD';
  4703. const OPTION_IGNORE_SAFE_THIS_MUTATIONS = 'IGNORE_SAFE_THIS_MUTATIONS';
  4704. const OPTION_CALLED_NODES = 'CALLED_NODES';
  4705. const OPTION_MUTATED_NODES = 'MUTATED_NODES';
  4706. const IGNORED_LABELS = 'IGNORED_LABELS';
  4707. /** Wrapper to ensure immutability */
  4708. class ExecutionPathOptions {
  4709. /**
  4710. * @returns {ExecutionPathOptions}
  4711. */
  4712. static create () {
  4713. return new this( {} );
  4714. }
  4715. constructor ( optionValues ) {
  4716. this._optionValues = optionValues;
  4717. }
  4718. /**
  4719. * Returns a new ExecutionPathOptions instance with the given option set to a new value.
  4720. * Does not mutate the current instance. Also works in sub-classes.
  4721. * @param {string} option - The name of an option
  4722. * @param {*} value - The new value of the option
  4723. * @returns {ExecutionPathOptions} A new options instance
  4724. */
  4725. set ( option, value ) {
  4726. return new this.constructor( Object.assign( {}, this._optionValues, { [option]: value } ) );
  4727. }
  4728. /**
  4729. * @param {string} option - The name of an option
  4730. * @returns {*} Its value
  4731. */
  4732. get ( option ) {
  4733. return this._optionValues[ option ];
  4734. }
  4735. /**
  4736. * @return {boolean}
  4737. */
  4738. ignoreBreakStatements () {
  4739. return this.get( OPTION_IGNORE_BREAK_STATEMENTS );
  4740. }
  4741. /**
  4742. * @param {boolean} [value=true]
  4743. * @return {ExecutionPathOptions}
  4744. */
  4745. setIgnoreBreakStatements ( value ) {
  4746. if ( value === void 0 ) value = true;
  4747. return this.set( OPTION_IGNORE_BREAK_STATEMENTS, value );
  4748. }
  4749. /**
  4750. * @param {string} labelName
  4751. * @return {boolean}
  4752. */
  4753. ignoreLabel ( labelName ) {
  4754. const ignoredLabels = this.get( IGNORED_LABELS );
  4755. return ignoredLabels && ignoredLabels.has( labelName );
  4756. }
  4757. /**
  4758. * @param {string} labelName
  4759. * @return {ExecutionPathOptions}
  4760. */
  4761. setIgnoreLabel ( labelName ) {
  4762. return this.set( IGNORED_LABELS, new Set( this.get( IGNORED_LABELS ) ).add( labelName ) );
  4763. }
  4764. /**
  4765. * @return {ExecutionPathOptions}
  4766. */
  4767. setIgnoreNoLabels () {
  4768. return this.set( IGNORED_LABELS, null );
  4769. }
  4770. /**
  4771. * @return {boolean}
  4772. */
  4773. ignoreReturnAwaitYield () {
  4774. return this.get( OPTION_IGNORE_RETURN_AWAIT_YIELD );
  4775. }
  4776. /**
  4777. * @param {boolean} [value=true]
  4778. * @return {ExecutionPathOptions}
  4779. */
  4780. setIgnoreReturnAwaitYield ( value ) {
  4781. if ( value === void 0 ) value = true;
  4782. return this.set( OPTION_IGNORE_RETURN_AWAIT_YIELD, value );
  4783. }
  4784. /**
  4785. * @return {boolean}
  4786. */
  4787. ignoreSafeThisMutations () {
  4788. return this.get( OPTION_IGNORE_SAFE_THIS_MUTATIONS );
  4789. }
  4790. /**
  4791. * @param {boolean} [value=true]
  4792. * @return {ExecutionPathOptions}
  4793. */
  4794. setIgnoreSafeThisMutations ( value ) {
  4795. if ( value === void 0 ) value = true;
  4796. return this.set( OPTION_IGNORE_SAFE_THIS_MUTATIONS, value );
  4797. }
  4798. /**
  4799. * @param {Node} node
  4800. * @return {ExecutionPathOptions}
  4801. */
  4802. addMutatedNode ( node ) {
  4803. return this.set( OPTION_MUTATED_NODES, new Set( this.get( OPTION_MUTATED_NODES ) ).add( node ) );
  4804. }
  4805. /**
  4806. * @param {Node} node
  4807. * @return {boolean}
  4808. */
  4809. hasNodeBeenMutated ( node ) {
  4810. const mutatedNodes = this.get( OPTION_MUTATED_NODES );
  4811. return mutatedNodes && mutatedNodes.has( node );
  4812. }
  4813. /**
  4814. * @param {Node} node
  4815. * @return {ExecutionPathOptions}
  4816. */
  4817. addCalledNode ( node ) {
  4818. return this.set( OPTION_CALLED_NODES, new Set( this.get( OPTION_CALLED_NODES ) ).add( node ) );
  4819. }
  4820. /**
  4821. * @param {Node} node
  4822. * @return {boolean}
  4823. */
  4824. hasNodeBeenCalled ( node ) {
  4825. const calledNodes = this.get( OPTION_CALLED_NODES );
  4826. return calledNodes && calledNodes.has( node );
  4827. }
  4828. /**
  4829. * @param {Node} calledNode
  4830. * @return {ExecutionPathOptions}
  4831. */
  4832. getHasEffectsWhenCalledOptions ( calledNode ) {
  4833. return this
  4834. .addCalledNode( calledNode )
  4835. .setIgnoreReturnAwaitYield()
  4836. .setIgnoreBreakStatements( false )
  4837. .setIgnoreNoLabels();
  4838. }
  4839. }
  4840. class Node$1 {
  4841. /**
  4842. * Called once all nodes have been initialised and the scopes have been populated.
  4843. * Use this to bind assignments and calls to variables.
  4844. */
  4845. bind () {
  4846. this.eachChild( child => child.bind() );
  4847. }
  4848. /**
  4849. * Bind an expression as an assignment to a node.
  4850. * The default noop implementation is ok as long as hasEffectsWhenAssigned
  4851. * always returns true for this node. Otherwise it should be overridden.
  4852. * @param {Node} expression
  4853. */
  4854. bindAssignment () {}
  4855. /**
  4856. * Binds ways a node is called to a node. Current options are:
  4857. * - withNew: boolean - Did this call use the "new" operator
  4858. * The default noop implementation is ok as long as hasEffectsWhenCalled
  4859. * always returns true for this node. Otherwise it should be overridden.
  4860. * @param callOptions
  4861. */
  4862. bindCall () {}
  4863. eachChild ( callback ) {
  4864. this.keys.forEach( key => {
  4865. const value = this[ key ];
  4866. if ( !value ) { return; }
  4867. if ( Array.isArray( value ) ) {
  4868. value.forEach( child => child && callback( child ) );
  4869. } else {
  4870. callback( value );
  4871. }
  4872. } );
  4873. }
  4874. getValue () {
  4875. return UNKNOWN_VALUE;
  4876. }
  4877. /**
  4878. * Determine if this Node would have an effect on the bundle.
  4879. * This is usually true for already included nodes. Exceptions are e.g. break statements
  4880. * which only have an effect if their surrounding loop or switch statement is included.
  4881. * The options pass on information like this about the current execution path.
  4882. * @param {ExecutionPathOptions} options
  4883. * @return {boolean}
  4884. */
  4885. hasEffects ( options ) {
  4886. return this.included || this.someChild( child => child.hasEffects( options ) );
  4887. }
  4888. /**
  4889. * Special make-shift logic to treat cases where apparently side-effect free statements
  4890. * are executed for side-effects. The most important case are getters with side-effects.
  4891. * Once we can reliably handle this case in member expressions, this function should
  4892. * probably be removed again.
  4893. * @param {ExecutionPathOptions} options
  4894. * @return {boolean}
  4895. */
  4896. hasEffectsAsExpressionStatement () {
  4897. return true;
  4898. }
  4899. /**
  4900. * @param {ExecutionPathOptions} options
  4901. * @return {boolean}
  4902. */
  4903. hasEffectsWhenAssigned () {
  4904. return true;
  4905. }
  4906. /**
  4907. * @param {ExecutionPathOptions} options
  4908. * @return {boolean}
  4909. */
  4910. hasEffectsWhenCalled () {
  4911. return true;
  4912. }
  4913. /**
  4914. * @param {ExecutionPathOptions} options
  4915. * @return {boolean}
  4916. */
  4917. hasEffectsWhenMutated () {
  4918. return true;
  4919. }
  4920. /**
  4921. * Includes the node in the bundle. Children are usually included if they are
  4922. * necessary for this node (e.g. a function body) or if they have effects.
  4923. * Necessary variables should be included as well. Should return true if any
  4924. * nodes or variables have been added that were missing before.
  4925. * @return {boolean}
  4926. */
  4927. includeInBundle () {
  4928. if ( this.isFullyIncluded() ) { return false; }
  4929. let addedNewNodes = false;
  4930. this.eachChild( childNode => {
  4931. if ( childNode.includeInBundle() ) {
  4932. addedNewNodes = true;
  4933. }
  4934. } );
  4935. if ( this.included && !addedNewNodes ) {
  4936. return false;
  4937. }
  4938. this.included = true;
  4939. return true;
  4940. }
  4941. /**
  4942. * Alternative version of includeInBundle to override the default behaviour of
  4943. * declarations to only include nodes for declarators that have an effect. Necessary
  4944. * for for-loops that do not use a declared loop variable.
  4945. * @return {boolean}
  4946. */
  4947. includeWithAllDeclarations () {
  4948. return this.includeInBundle();
  4949. }
  4950. /**
  4951. * Assign a scope to this node and make sure all children have the right scopes.
  4952. * Perform any additional initialisation that does not depend on the scope being
  4953. * populated with variables.
  4954. * Usually one should not override this function but override initialiseScope,
  4955. * initialiseNode and/or initialiseChildren instead. BlockScopes have a special
  4956. * alternative initialisation initialiseAndReplaceScope.
  4957. * @param {Scope} parentScope
  4958. */
  4959. initialise ( parentScope ) {
  4960. this.initialiseScope( parentScope );
  4961. this.initialiseNode( parentScope );
  4962. this.initialiseChildren( parentScope );
  4963. }
  4964. /**
  4965. * Override to change how and with what scopes children are initialised
  4966. * @param {Scope} parentScope
  4967. */
  4968. initialiseChildren () {
  4969. this.eachChild( child => child.initialise( this.scope ) );
  4970. }
  4971. /**
  4972. * Override to perform special initialisation steps after the scope is initialised
  4973. * @param {Scope} parentScope
  4974. */
  4975. initialiseNode () {}
  4976. /**
  4977. * Override if this scope should receive a different scope than the parent scope.
  4978. * @param {Scope} parentScope
  4979. */
  4980. initialiseScope ( parentScope ) {
  4981. this.scope = parentScope;
  4982. }
  4983. insertSemicolon ( code ) {
  4984. if ( code.original[ this.end - 1 ] !== ';' ) {
  4985. code.appendLeft( this.end, ';' );
  4986. }
  4987. }
  4988. /**
  4989. * Shortcut to skip checking this node for effects when all children have already
  4990. * been included.
  4991. * @param {Scope} parentScope
  4992. */
  4993. isFullyIncluded () {
  4994. if ( this._fullyIncluded ) {
  4995. return true;
  4996. }
  4997. this._fullyIncluded = this.included && !this.someChild( child => !child.isFullyIncluded() );
  4998. }
  4999. locate () {
  5000. // useful for debugging
  5001. const location = locate( this.module.code, this.start, { offsetLine: 1 } );
  5002. location.file = this.module.id;
  5003. location.toString = () => JSON.stringify( location );
  5004. return location;
  5005. }
  5006. render ( code, es ) {
  5007. this.eachChild( child => child.render( code, es ) );
  5008. }
  5009. /**
  5010. * Start a new execution path to determine if this node has an effect on the bundle and
  5011. * should therefore be included. Unless they are fully included, included nodes should
  5012. * always be included again in subsequent visits as the inclusion of additional variables
  5013. * may require the inclusion of more child nodes in e.g. block statements.
  5014. * @return {boolean}
  5015. */
  5016. shouldBeIncluded () {
  5017. return this.hasEffects( ExecutionPathOptions.create() );
  5018. }
  5019. someChild ( callback ) {
  5020. return this.keys.some( key => {
  5021. const value = this[ key ];
  5022. if ( !value ) { return false; }
  5023. if ( Array.isArray( value ) ) {
  5024. return value.some( child => child && callback( child ) );
  5025. }
  5026. return callback( value );
  5027. } );
  5028. }
  5029. toString () {
  5030. return this.module.code.slice( this.start, this.end );
  5031. }
  5032. }
  5033. class ArrayPattern extends Node$1 {
  5034. bindAssignment () {
  5035. this.eachChild( child => child.bindAssignment( UNKNOWN_ASSIGNMENT ) );
  5036. }
  5037. hasEffectsWhenAssigned ( options ) {
  5038. return this.someChild( child => child.hasEffectsWhenAssigned( options ) );
  5039. }
  5040. initialiseAndDeclare ( parentScope, kind ) {
  5041. this.initialiseScope( parentScope );
  5042. this.eachChild( child => child.initialiseAndDeclare( parentScope, kind, UNKNOWN_ASSIGNMENT ) );
  5043. }
  5044. }
  5045. class LocalVariable extends Variable {
  5046. constructor ( name, declarator, init ) {
  5047. super( name );
  5048. this.isReassigned = false;
  5049. this.exportName = null;
  5050. this.declarations = new Set( declarator ? [ declarator ] : null );
  5051. this.assignedExpressions = new Set( init ? [ init ] : null );
  5052. this.calls = new Set();
  5053. }
  5054. addDeclaration ( identifier ) {
  5055. this.declarations.add( identifier );
  5056. }
  5057. addCall ( callOptions ) {
  5058. // To prevent infinite loops
  5059. if ( this.calls.has( callOptions ) ) { return; }
  5060. this.calls.add( callOptions );
  5061. Array.from( this.assignedExpressions ).forEach( expression => expression.bindCall( callOptions ) );
  5062. }
  5063. addReference () {}
  5064. assignExpression ( expression ) {
  5065. this.assignedExpressions.add( expression );
  5066. this.isReassigned = true;
  5067. Array.from( this.calls ).forEach( callOptions => expression.bindCall( callOptions ) );
  5068. }
  5069. getName ( es ) {
  5070. if ( es ) { return this.name; }
  5071. if ( !this.isReassigned || !this.exportName ) { return this.name; }
  5072. return `exports.${this.exportName}`;
  5073. }
  5074. hasEffectsWhenCalled ( options ) {
  5075. return Array.from( this.assignedExpressions ).some( node =>
  5076. !options.hasNodeBeenCalled( node )
  5077. && node.hasEffectsWhenCalled( options.getHasEffectsWhenCalledOptions( node ) )
  5078. );
  5079. }
  5080. hasEffectsWhenMutated ( options ) {
  5081. return this.included
  5082. || Array.from( this.assignedExpressions ).some( node =>
  5083. !options.hasNodeBeenMutated( node ) &&
  5084. node.hasEffectsWhenMutated( options.addMutatedNode( node ) )
  5085. );
  5086. }
  5087. includeVariable () {
  5088. const hasBeenIncluded = super.includeVariable();
  5089. if ( hasBeenIncluded ) {
  5090. this.declarations.forEach( identifier => identifier.includeInBundle() );
  5091. }
  5092. return hasBeenIncluded;
  5093. }
  5094. toString () {
  5095. return this.name;
  5096. }
  5097. }
  5098. class ParameterVariable extends LocalVariable {
  5099. constructor ( name, declarator ) {
  5100. super( name, declarator, UNKNOWN_ASSIGNMENT );
  5101. }
  5102. getName () {
  5103. return this.name;
  5104. }
  5105. }
  5106. class Scope {
  5107. constructor ( options ) {
  5108. if ( options === void 0 ) options = {};
  5109. this.parent = options.parent;
  5110. this.isModuleScope = !!options.isModuleScope;
  5111. this.children = [];
  5112. if ( this.parent ) { this.parent.children.push( this ); }
  5113. this.variables = blank();
  5114. }
  5115. addDeclaration ( identifier, isHoisted, init ) {
  5116. const name = identifier.name;
  5117. if ( this.variables[ name ] ) {
  5118. const variable = this.variables[ name ];
  5119. variable.addDeclaration( identifier );
  5120. init && variable.assignExpression( init );
  5121. } else {
  5122. this.variables[ name ] = new LocalVariable( identifier.name, identifier, init );
  5123. }
  5124. }
  5125. addParameterDeclaration ( identifier ) {
  5126. const name = identifier.name;
  5127. this.variables[ name ] = new ParameterVariable( name, identifier );
  5128. }
  5129. contains ( name ) {
  5130. return !!this.variables[ name ] ||
  5131. ( this.parent ? this.parent.contains( name ) : false );
  5132. }
  5133. deshadow ( names ) {
  5134. keys( this.variables ).forEach( key => {
  5135. const declaration = this.variables[ key ];
  5136. // we can disregard exports.foo etc
  5137. if ( declaration.exportName && declaration.isReassigned ) { return; }
  5138. const name = declaration.getName( true );
  5139. let deshadowed = name;
  5140. let i = 1;
  5141. while ( names.has( deshadowed ) ) {
  5142. deshadowed = `${name}$$${i++}`;
  5143. }
  5144. declaration.name = deshadowed;
  5145. } );
  5146. this.children.forEach( scope => scope.deshadow( names ) );
  5147. }
  5148. findLexicalBoundary () {
  5149. return this.parent.findLexicalBoundary();
  5150. }
  5151. findVariable ( name ) {
  5152. return this.variables[ name ] ||
  5153. ( this.parent && this.parent.findVariable( name ) );
  5154. }
  5155. }
  5156. class ArrowFunctionExpression extends Node$1 {
  5157. // Should receive an implementation once we start tracking parameter values
  5158. bindCall () {}
  5159. hasEffects () {
  5160. return this.included;
  5161. }
  5162. hasEffectsWhenCalled ( options ) {
  5163. return this.params.some( param => param.hasEffects( options ) )
  5164. || this.body.hasEffects( options );
  5165. }
  5166. hasEffectsWhenMutated () {
  5167. return this.included;
  5168. }
  5169. initialiseChildren () {
  5170. this.params.forEach( param => param.initialiseAndDeclare( this.scope, 'parameter' ) );
  5171. if ( this.body.initialiseAndReplaceScope ) {
  5172. this.body.initialiseAndReplaceScope( new Scope( { parent: this.scope } ) );
  5173. } else {
  5174. this.body.initialise( this.scope );
  5175. }
  5176. }
  5177. initialiseScope ( parentScope ) {
  5178. this.scope = new Scope( { parent: parentScope } );
  5179. }
  5180. }
  5181. // TODO tidy this up a bit (e.g. they can both use node.module.imports)
  5182. function disallowIllegalReassignment ( scope, node ) {
  5183. if ( node.type === 'MemberExpression' && node.object.type === 'Identifier' ) {
  5184. const variable = scope.findVariable( node.object.name );
  5185. if ( variable.isNamespace ) {
  5186. node.module.error({
  5187. code: 'ILLEGAL_NAMESPACE_REASSIGNMENT',
  5188. message: `Illegal reassignment to import '${node.object.name}'`
  5189. }, node.start );
  5190. }
  5191. }
  5192. else if ( node.type === 'Identifier' ) {
  5193. if ( node.module.imports[ node.name ] && !scope.contains( node.name ) ) {
  5194. node.module.error({
  5195. code: 'ILLEGAL_REASSIGNMENT',
  5196. message: `Illegal reassignment to import '${node.name}'`
  5197. }, node.start );
  5198. }
  5199. }
  5200. }
  5201. class AssignmentExpression extends Node$1 {
  5202. bind () {
  5203. super.bind();
  5204. disallowIllegalReassignment( this.scope, this.left );
  5205. this.left.bindAssignment( this.right );
  5206. }
  5207. hasEffects ( options ) {
  5208. return super.hasEffects( options ) || this.left.hasEffectsWhenAssigned( options );
  5209. }
  5210. hasEffectsAsExpressionStatement ( options ) {
  5211. return this.hasEffects( options );
  5212. }
  5213. }
  5214. class AssignmentPattern extends Node$1 {
  5215. bind () {
  5216. super.bind();
  5217. this.left.bindAssignment( this.right );
  5218. }
  5219. bindAssignment ( expression ) {
  5220. this.left.bindAssignment( expression );
  5221. }
  5222. hasEffectsWhenAssigned ( options ) {
  5223. return this.left.hasEffectsWhenAssigned( options );
  5224. }
  5225. initialiseAndDeclare ( parentScope, kind, init ) {
  5226. this.initialiseScope( parentScope );
  5227. this.right.initialise( parentScope );
  5228. this.left.initialiseAndDeclare( parentScope, kind, init );
  5229. }
  5230. }
  5231. class AwaitExpression extends Node$1 {
  5232. hasEffects ( options ) {
  5233. return super.hasEffects( options )
  5234. || !options.ignoreReturnAwaitYield();
  5235. }
  5236. hasEffectsAsExpressionStatement ( options ) {
  5237. return this.hasEffects( options );
  5238. }
  5239. }
  5240. const operators = {
  5241. '==': ( left, right ) => left == right,
  5242. '!=': ( left, right ) => left != right,
  5243. '===': ( left, right ) => left === right,
  5244. '!==': ( left, right ) => left !== right,
  5245. '<': ( left, right ) => left < right,
  5246. '<=': ( left, right ) => left <= right,
  5247. '>': ( left, right ) => left > right,
  5248. '>=': ( left, right ) => left >= right,
  5249. '<<': ( left, right ) => left << right,
  5250. '>>': ( left, right ) => left >> right,
  5251. '>>>': ( left, right ) => left >>> right,
  5252. '+': ( left, right ) => left + right,
  5253. '-': ( left, right ) => left - right,
  5254. '*': ( left, right ) => left * right,
  5255. '/': ( left, right ) => left / right,
  5256. '%': ( left, right ) => left % right,
  5257. '|': ( left, right ) => left | right,
  5258. '^': ( left, right ) => left ^ right,
  5259. '&': ( left, right ) => left & right,
  5260. '**': ( left, right ) => Math.pow( left, right ),
  5261. in: ( left, right ) => left in right,
  5262. instanceof: ( left, right ) => left instanceof right
  5263. };
  5264. class BinaryExpression extends Node$1 {
  5265. getValue () {
  5266. const leftValue = this.left.getValue();
  5267. if ( leftValue === UNKNOWN_VALUE ) { return UNKNOWN_VALUE; }
  5268. const rightValue = this.right.getValue();
  5269. if ( rightValue === UNKNOWN_VALUE ) { return UNKNOWN_VALUE; }
  5270. if ( !operators[ this.operator ] ) { return UNKNOWN_VALUE; }
  5271. return operators[ this.operator ]( leftValue, rightValue );
  5272. }
  5273. }
  5274. class Statement extends Node$1 {
  5275. render ( code, es ) {
  5276. if ( !this.module.bundle.treeshake || this.included ) {
  5277. super.render( code, es );
  5278. } else {
  5279. code.remove( this.leadingCommentStart || this.start, this.next || this.end );
  5280. }
  5281. }
  5282. }
  5283. class BlockScope extends Scope {
  5284. addDeclaration ( identifier, isHoisted, init ) {
  5285. if ( isHoisted ) {
  5286. this.parent.addDeclaration( identifier, isHoisted, init );
  5287. } else {
  5288. super.addDeclaration( identifier, false, init );
  5289. }
  5290. }
  5291. }
  5292. class BlockStatement extends Statement {
  5293. bind () {
  5294. this.body.forEach( node => node.bind() );
  5295. }
  5296. hasEffects ( options ) {
  5297. // Empty block statements do not have effects even though they may be included as e.g. function body
  5298. return this.body.some( child => child.hasEffects( options ) );
  5299. }
  5300. includeInBundle () {
  5301. if ( this.isFullyIncluded() ) { return false; }
  5302. let addedNewNodes = false;
  5303. this.body.forEach( node => {
  5304. if ( node.shouldBeIncluded() ) {
  5305. if ( node.includeInBundle() ) {
  5306. addedNewNodes = true;
  5307. }
  5308. }
  5309. } );
  5310. if ( !this.included || addedNewNodes ) {
  5311. this.included = true;
  5312. return true;
  5313. }
  5314. return false;
  5315. }
  5316. initialiseAndReplaceScope ( scope ) {
  5317. this.scope = scope;
  5318. this.initialiseNode();
  5319. this.initialiseChildren( scope );
  5320. }
  5321. initialiseChildren () {
  5322. let lastNode;
  5323. for ( const node of this.body ) {
  5324. node.initialise( this.scope );
  5325. if ( lastNode ) { lastNode.next = node.start; }
  5326. lastNode = node;
  5327. }
  5328. }
  5329. initialiseScope ( parentScope ) {
  5330. this.scope = new BlockScope( { parent: parentScope } );
  5331. }
  5332. render ( code, es ) {
  5333. if ( this.body.length ) {
  5334. for ( const node of this.body ) {
  5335. node.render( code, es );
  5336. }
  5337. } else {
  5338. Statement.prototype.render.call( this, code, es );
  5339. }
  5340. }
  5341. }
  5342. class BreakStatement extends Node$1 {
  5343. hasEffects ( options ) {
  5344. return super.hasEffects( options )
  5345. || !options.ignoreBreakStatements()
  5346. || (this.label && !options.ignoreLabel( this.label.name ));
  5347. }
  5348. }
  5349. class CallExpression extends Node$1 {
  5350. bind () {
  5351. if ( this.callee.type === 'Identifier' ) {
  5352. const variable = this.scope.findVariable( this.callee.name );
  5353. if ( variable.isNamespace ) {
  5354. this.module.error( {
  5355. code: 'CANNOT_CALL_NAMESPACE',
  5356. message: `Cannot call a namespace ('${this.callee.name}')`
  5357. }, this.start );
  5358. }
  5359. if ( this.callee.name === 'eval' && variable.isGlobal ) {
  5360. this.module.warn( {
  5361. code: 'EVAL',
  5362. message: `Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification`,
  5363. url: 'https://github.com/rollup/rollup/wiki/Troubleshooting#avoiding-eval'
  5364. }, this.start );
  5365. }
  5366. }
  5367. super.bind();
  5368. this.callee.bindCall( { withNew: false } );
  5369. }
  5370. hasEffects ( options ) {
  5371. return this.included
  5372. || this.arguments.some( child => child.hasEffects( options ) )
  5373. || this.callee.hasEffectsWhenCalled( options.getHasEffectsWhenCalledOptions( this.callee ) );
  5374. }
  5375. hasEffectsAsExpressionStatement ( options ) {
  5376. return this.hasEffects( options );
  5377. }
  5378. }
  5379. class CatchClause extends Node$1 {
  5380. initialiseChildren () {
  5381. this.param && this.param.initialiseAndDeclare( this.scope, 'parameter' );
  5382. this.body.initialiseAndReplaceScope( this.scope );
  5383. }
  5384. initialiseScope ( parentScope ) {
  5385. this.scope = new BlockScope( { parent: parentScope } );
  5386. }
  5387. }
  5388. class ClassBody extends Node$1 {
  5389. bindCall ( callOptions ) {
  5390. if ( this.classConstructor ) {
  5391. this.classConstructor.bindCall( callOptions );
  5392. }
  5393. }
  5394. hasEffectsWhenCalled ( options ) {
  5395. if ( this.classConstructor ) {
  5396. return this.classConstructor.hasEffectsWhenCalled( options );
  5397. }
  5398. return false;
  5399. }
  5400. initialiseNode () {
  5401. this.classConstructor = this.body.find( method => method.kind === 'constructor' );
  5402. }
  5403. }
  5404. class ClassNode extends Node$1 {
  5405. bindCall ( callOptions ) {
  5406. if ( this.superClass ) {
  5407. this.superClass.bindCall( callOptions );
  5408. }
  5409. this.body.bindCall( callOptions );
  5410. }
  5411. hasEffectsAsExpressionStatement ( options ) {
  5412. return this.hasEffects( options );
  5413. }
  5414. hasEffectsWhenCalled ( options ) {
  5415. return this.body.hasEffectsWhenCalled( options )
  5416. || ( this.superClass && this.superClass.hasEffectsWhenCalled( options ) );
  5417. }
  5418. initialiseChildren () {
  5419. if ( this.superClass ) {
  5420. this.superClass.initialise( this.scope );
  5421. }
  5422. this.body.initialise( this.scope );
  5423. }
  5424. initialiseScope ( parentScope ) {
  5425. this.scope = new Scope( { parent: parentScope } );
  5426. }
  5427. }
  5428. class ClassDeclaration extends ClassNode {
  5429. initialiseChildren ( parentScope ) {
  5430. // Class declarations are like let declarations: Not hoisted, can be reassigned, cannot be redeclared
  5431. this.id && this.id.initialiseAndDeclare( parentScope, 'class', this );
  5432. super.initialiseChildren( parentScope );
  5433. }
  5434. render ( code, es ) {
  5435. if ( !this.module.bundle.treeshake || this.included ) {
  5436. super.render( code, es );
  5437. } else {
  5438. code.remove( this.leadingCommentStart || this.start, this.next || this.end );
  5439. }
  5440. }
  5441. }
  5442. class ClassExpression extends ClassNode {
  5443. initialiseChildren ( parentScope ) {
  5444. this.id && this.id.initialiseAndDeclare( this.scope, 'class', this );
  5445. super.initialiseChildren( parentScope );
  5446. }
  5447. }
  5448. class ConditionalExpression extends Node$1 {
  5449. initialiseChildren ( parentScope ) {
  5450. if ( this.module.bundle.treeshake ) {
  5451. this.testValue = this.test.getValue();
  5452. if ( this.testValue === UNKNOWN_VALUE ) {
  5453. super.initialiseChildren( parentScope );
  5454. } else if ( this.testValue ) {
  5455. this.consequent.initialise( this.scope );
  5456. this.alternate = null;
  5457. } else if ( this.alternate ) {
  5458. this.alternate.initialise( this.scope );
  5459. this.consequent = null;
  5460. }
  5461. } else {
  5462. super.initialiseChildren( parentScope );
  5463. }
  5464. }
  5465. getValue () {
  5466. const testValue = this.test.getValue();
  5467. if ( testValue === UNKNOWN_VALUE ) { return UNKNOWN_VALUE; }
  5468. return testValue ? this.consequent.getValue() : this.alternate.getValue();
  5469. }
  5470. render ( code, es ) {
  5471. if ( !this.module.bundle.treeshake ) {
  5472. super.render( code, es );
  5473. }
  5474. else {
  5475. if ( this.testValue === UNKNOWN_VALUE ) {
  5476. super.render( code, es );
  5477. }
  5478. else if ( this.testValue ) {
  5479. code.remove( this.start, this.consequent.start );
  5480. code.remove( this.consequent.end, this.end );
  5481. if ( this.consequent.type === 'SequenceExpression' ) {
  5482. code.prependRight( this.consequent.start, '(' );
  5483. code.appendLeft( this.consequent.end, ')' );
  5484. }
  5485. this.consequent.render( code, es );
  5486. } else {
  5487. code.remove( this.start, this.alternate.start );
  5488. code.remove( this.alternate.end, this.end );
  5489. if ( this.alternate.type === 'SequenceExpression' ) {
  5490. code.prependRight( this.alternate.start, '(' );
  5491. code.appendLeft( this.alternate.end, ')' );
  5492. }
  5493. this.alternate.render( code, es );
  5494. }
  5495. }
  5496. }
  5497. }
  5498. class DoWhileStatement extends Statement {
  5499. hasEffects ( options ) {
  5500. return (
  5501. this.included
  5502. || this.test.hasEffects( options )
  5503. || this.body.hasEffects( options.setIgnoreBreakStatements() )
  5504. );
  5505. }
  5506. }
  5507. class EmptyStatement extends Statement {
  5508. render ( code ) {
  5509. if ( this.parent.type === 'BlockStatement' || this.parent.type === 'Program' ) {
  5510. code.remove( this.start, this.end );
  5511. }
  5512. }
  5513. }
  5514. class ExportAllDeclaration extends Node$1 {
  5515. initialiseNode () {
  5516. this.isExportDeclaration = true;
  5517. }
  5518. render ( code ) {
  5519. code.remove( this.leadingCommentStart || this.start, this.next || this.end );
  5520. }
  5521. }
  5522. const functionOrClassDeclaration = /^(?:Function|Class)Declaration/;
  5523. class ExportDefaultDeclaration extends Node$1 {
  5524. addCall ( options ) {
  5525. this.declaration.bindCall( options );
  5526. }
  5527. addReference ( reference ) {
  5528. this.name = reference.name;
  5529. if ( this.original ) { this.original.addReference( reference ); }
  5530. }
  5531. bind () {
  5532. const name = ( this.declaration.id && this.declaration.id.name ) || this.declaration.name;
  5533. if ( name ) { this.original = this.scope.findVariable( name ); }
  5534. this.declaration.bind();
  5535. }
  5536. getName ( es ) {
  5537. if ( this.original && !this.original.isReassigned ) {
  5538. return this.original.getName( es );
  5539. }
  5540. return this.name;
  5541. }
  5542. hasEffectsWhenCalled ( options ) {
  5543. return this.declaration.hasEffectsWhenCalled( options );
  5544. }
  5545. includeVariable () {
  5546. if ( this.included ) {
  5547. return false;
  5548. }
  5549. this.included = true;
  5550. this.declaration.includeInBundle();
  5551. return true;
  5552. }
  5553. includeInBundle () {
  5554. if ( this.declaration.shouldBeIncluded() ) {
  5555. return this.declaration.includeInBundle();
  5556. }
  5557. return false;
  5558. }
  5559. initialiseNode () {
  5560. this.isExportDeclaration = true;
  5561. this.isDefault = true;
  5562. this.name = ( this.declaration.id && this.declaration.id.name ) || this.declaration.name || this.module.basename();
  5563. this.scope.variables.default = this;
  5564. }
  5565. // TODO this is total chaos, tidy it up
  5566. render ( code, es ) {
  5567. const treeshake = this.module.bundle.treeshake;
  5568. const name = this.getName( es );
  5569. // paren workaround: find first non-whitespace character position after `export default`
  5570. let declaration_start;
  5571. if ( this.declaration ) {
  5572. const statementStr = code.original.slice( this.start, this.end );
  5573. declaration_start = this.start + statementStr.match( /^\s*export\s+default\s*/ )[ 0 ].length;
  5574. }
  5575. if ( this.included || this.declaration.included ) {
  5576. if ( this.included ) {
  5577. if ( functionOrClassDeclaration.test( this.declaration.type ) ) {
  5578. if ( this.declaration.id ) {
  5579. code.remove( this.start, declaration_start );
  5580. } else {
  5581. code.overwrite( this.start, declaration_start, `var ${this.name} = ` );
  5582. if ( code.original[ this.end - 1 ] !== ';' ) { code.appendLeft( this.end, ';' ); }
  5583. }
  5584. }
  5585. else {
  5586. if ( this.original && this.original.getName( es ) === name ) {
  5587. // prevent `var foo = foo`
  5588. code.remove( this.leadingCommentStart || this.start, this.next || this.end );
  5589. return; // don't render children. TODO this seems like a bit of a hack
  5590. } else {
  5591. code.overwrite( this.start, declaration_start, `${this.module.bundle.varOrConst} ${name} = ` );
  5592. }
  5593. this.insertSemicolon( code );
  5594. }
  5595. } else {
  5596. // remove `var foo` from `var foo = bar()`, if `foo` is unused
  5597. code.remove( this.start, declaration_start );
  5598. }
  5599. super.render( code, es );
  5600. } else {
  5601. if ( treeshake ) {
  5602. if ( functionOrClassDeclaration.test( this.declaration.type ) ) {
  5603. code.remove( this.leadingCommentStart || this.start, this.next || this.end );
  5604. } else {
  5605. const hasEffects = this.declaration.hasEffects( ExecutionPathOptions.create() );
  5606. code.remove( this.start, hasEffects ? declaration_start : this.next || this.end );
  5607. }
  5608. } else if ( name === this.declaration.name ) {
  5609. code.remove( this.start, this.next || this.end );
  5610. } else {
  5611. code.overwrite( this.start, declaration_start, `${this.module.bundle.varOrConst} ${name} = ` );
  5612. }
  5613. // code.remove( this.start, this.next || this.end );
  5614. }
  5615. }
  5616. }
  5617. class ExportNamedDeclaration extends Node$1 {
  5618. bind () {
  5619. // Do not bind specifiers
  5620. if ( this.declaration ) { this.declaration.bind(); }
  5621. }
  5622. hasEffects ( options ) {
  5623. return this.included || (this.declaration && this.declaration.hasEffects( options ));
  5624. }
  5625. initialiseNode () {
  5626. this.isExportDeclaration = true;
  5627. }
  5628. render ( code, es ) {
  5629. if ( this.declaration ) {
  5630. code.remove( this.start, this.declaration.start );
  5631. this.declaration.render( code, es );
  5632. } else {
  5633. const start = this.leadingCommentStart || this.start;
  5634. const end = this.next || this.end;
  5635. if ( this.defaultExport ) {
  5636. const name = this.defaultExport.getName( es );
  5637. const originalName = this.defaultExport.original.getName( es );
  5638. if ( name !== originalName ) {
  5639. code.overwrite( start, end, `var ${name} = ${originalName};` );
  5640. return;
  5641. }
  5642. }
  5643. code.remove( start, end );
  5644. }
  5645. }
  5646. }
  5647. class ExpressionStatement extends Statement {
  5648. hasEffects ( options ) {
  5649. return super.hasEffects( options ) || this.expression.hasEffectsAsExpressionStatement(options);
  5650. }
  5651. render ( code, es ) {
  5652. super.render( code, es );
  5653. if ( this.included ) { this.insertSemicolon( code ); }
  5654. }
  5655. }
  5656. class ForStatement extends Statement {
  5657. hasEffects ( options ) {
  5658. return (
  5659. this.included
  5660. || this.init && this.init.hasEffects( options )
  5661. || this.test && this.test.hasEffects( options )
  5662. || this.update && this.update.hasEffects( options )
  5663. || this.body.hasEffects( options.setIgnoreBreakStatements() )
  5664. );
  5665. }
  5666. initialiseChildren () {
  5667. if ( this.init ) { this.init.initialise( this.scope ); }
  5668. if ( this.test ) { this.test.initialise( this.scope ); }
  5669. if ( this.update ) { this.update.initialise( this.scope ); }
  5670. if ( this.body.type === 'BlockStatement' ) {
  5671. this.body.initialiseScope( this.scope );
  5672. this.body.initialiseChildren();
  5673. } else {
  5674. this.body.initialise( this.scope );
  5675. }
  5676. }
  5677. initialiseScope ( parentScope ) {
  5678. this.scope = new BlockScope( { parent: parentScope } );
  5679. }
  5680. }
  5681. class ForInStatement extends Statement {
  5682. hasEffects ( options ) {
  5683. return (
  5684. this.included
  5685. || this.left && (this.left.hasEffects( options ) || this.left.hasEffectsWhenAssigned( options ))
  5686. || this.right && this.right.hasEffects( options )
  5687. || this.body.hasEffects( options.setIgnoreBreakStatements() )
  5688. );
  5689. }
  5690. initialiseChildren () {
  5691. this.left.initialise( this.scope );
  5692. this.right.initialise( this.scope.parent );
  5693. this.body.initialiseAndReplaceScope ?
  5694. this.body.initialiseAndReplaceScope( this.scope ) :
  5695. this.body.initialise( this.scope );
  5696. }
  5697. includeInBundle () {
  5698. let addedNewNodes = super.includeInBundle();
  5699. if ( this.left.includeWithAllDeclarations() ) {
  5700. addedNewNodes = true;
  5701. }
  5702. return addedNewNodes;
  5703. }
  5704. initialiseScope ( parentScope ) {
  5705. this.scope = new BlockScope( { parent: parentScope } );
  5706. }
  5707. }
  5708. class ForOfStatement extends Statement {
  5709. bind () {
  5710. super.bind();
  5711. this.left.bindAssignment( UNKNOWN_ASSIGNMENT );
  5712. }
  5713. hasEffects ( options ) {
  5714. return (
  5715. this.included
  5716. || this.left && (this.left.hasEffects( options ) || this.left.hasEffectsWhenAssigned( options ))
  5717. || this.right && this.right.hasEffects( options )
  5718. || this.body.hasEffects( options.setIgnoreBreakStatements() )
  5719. );
  5720. }
  5721. includeInBundle () {
  5722. let addedNewNodes = super.includeInBundle();
  5723. if ( this.left.includeWithAllDeclarations() ) {
  5724. addedNewNodes = true;
  5725. }
  5726. return addedNewNodes;
  5727. }
  5728. initialiseChildren () {
  5729. this.left.initialise( this.scope );
  5730. this.right.initialise( this.scope.parent );
  5731. this.body.initialiseAndReplaceScope ?
  5732. this.body.initialiseAndReplaceScope( this.scope ) :
  5733. this.body.initialise( this.scope );
  5734. }
  5735. initialiseScope ( parentScope ) {
  5736. this.scope = new BlockScope( { parent: parentScope } );
  5737. }
  5738. }
  5739. class FunctionScope extends Scope {
  5740. constructor ( options ) {
  5741. if ( options === void 0 ) options = {};
  5742. super( options );
  5743. this.variables.arguments = new ParameterVariable( 'arguments' );
  5744. this.variables.this = new LocalVariable( 'this', null, null );
  5745. }
  5746. findLexicalBoundary () {
  5747. return this;
  5748. }
  5749. }
  5750. class FunctionNode extends Node$1 {
  5751. bindCall ( ref ) {
  5752. var withNew = ref.withNew;
  5753. const thisVariable = this.scope.findVariable( 'this' );
  5754. if ( withNew ) {
  5755. thisVariable.assignExpression( UNKNOWN_OBJECT_LITERAL );
  5756. } else {
  5757. thisVariable.assignExpression( UNKNOWN_ASSIGNMENT );
  5758. }
  5759. }
  5760. hasEffects ( options ) {
  5761. return this.included || (this.id && this.id.hasEffects( options ));
  5762. }
  5763. hasEffectsAsExpressionStatement ( options ) {
  5764. return this.hasEffects( options );
  5765. }
  5766. hasEffectsWhenCalled ( options ) {
  5767. const innerOptions = options.setIgnoreSafeThisMutations();
  5768. return this.params.some( param => param.hasEffects( innerOptions ) )
  5769. || this.body.hasEffects( innerOptions );
  5770. }
  5771. hasEffectsWhenMutated () {
  5772. return this.included;
  5773. }
  5774. initialiseScope ( parentScope ) {
  5775. this.scope = new FunctionScope( { parent: parentScope } );
  5776. }
  5777. }
  5778. class FunctionDeclaration extends FunctionNode {
  5779. initialiseChildren ( parentScope ) {
  5780. this.id && this.id.initialiseAndDeclare( parentScope, 'function', this );
  5781. this.params.forEach( param => param.initialiseAndDeclare( this.scope, 'parameter' ) );
  5782. this.body.initialiseAndReplaceScope( new Scope( { parent: this.scope } ) );
  5783. }
  5784. render ( code, es ) {
  5785. if ( !this.module.bundle.treeshake || this.included ) {
  5786. super.render( code, es );
  5787. } else {
  5788. code.remove( this.leadingCommentStart || this.start, this.next || this.end );
  5789. }
  5790. }
  5791. }
  5792. class FunctionExpression extends FunctionNode {
  5793. initialiseChildren () {
  5794. this.id && this.id.initialiseAndDeclare( this.scope, 'function', this );
  5795. this.params.forEach( param => param.initialiseAndDeclare( this.scope, 'parameter' ) );
  5796. this.body.initialiseAndReplaceScope( new Scope( { parent: this.scope } ) );
  5797. }
  5798. }
  5799. function isReference (node, parent) {
  5800. if (node.type === 'MemberExpression') {
  5801. return !node.computed && isReference(node.object, node);
  5802. }
  5803. if (node.type === 'Identifier') {
  5804. // the only time we could have an identifier node without a parent is
  5805. // if it's the entire body of a function without a block statement –
  5806. // i.e. an arrow function expression like `a => a`
  5807. if (!parent) return true;
  5808. // TODO is this right?
  5809. if (parent.type === 'MemberExpression' || parent.type === 'MethodDefinition') {
  5810. return parent.computed || node === parent.object;
  5811. }
  5812. // disregard the `bar` in `{ bar: foo }`, but keep it in `{ [bar]: foo }`
  5813. if (parent.type === 'Property') return parent.computed || node === parent.value;
  5814. // disregard the `bar` in `class Foo { bar () {...} }`
  5815. if (parent.type === 'MethodDefinition') return false;
  5816. // disregard the `bar` in `export { foo as bar }`
  5817. if (parent.type === 'ExportSpecifier' && node !== parent.local) return false;
  5818. return true;
  5819. }
  5820. return false;
  5821. }
  5822. function isAssignmentPatternLhs ( node, parent ) {
  5823. // special case: `({ foo = 42 }) => {...}`
  5824. // `foo` actually has two different parents, the Property of the
  5825. // ObjectPattern, and the AssignmentPattern. In one case it's a
  5826. // reference, in one case it's not, because it's shorthand for
  5827. // `({ foo: foo = 42 }) => {...}`. But unlike a regular shorthand
  5828. // property, the `foo` node appears at different levels of the tree
  5829. return (
  5830. parent.type === 'Property' &&
  5831. parent.shorthand &&
  5832. parent.value.type === 'AssignmentPattern' &&
  5833. parent.value.left === node
  5834. );
  5835. }
  5836. class Identifier extends Node$1 {
  5837. bind () {
  5838. if ( isReference( this, this.parent ) || isAssignmentPatternLhs( this, this.parent ) ) {
  5839. this.variable = this.scope.findVariable( this.name );
  5840. this.variable.addReference( this );
  5841. }
  5842. }
  5843. bindAssignment ( expression ) {
  5844. if ( this.variable ) {
  5845. this.variable.assignExpression( expression );
  5846. }
  5847. }
  5848. bindCall ( callOptions ) {
  5849. if ( this.variable ) {
  5850. this.variable.addCall( callOptions );
  5851. }
  5852. }
  5853. hasEffectsAsExpressionStatement ( options ) {
  5854. return this.hasEffects( options ) || this.variable.isGlobal;
  5855. }
  5856. hasEffectsWhenAssigned () {
  5857. return this.variable && this.variable.included;
  5858. }
  5859. hasEffectsWhenCalled ( options ) {
  5860. if ( !this.variable ) {
  5861. return true;
  5862. }
  5863. return this.variable.hasEffectsWhenCalled( options );
  5864. }
  5865. hasEffectsWhenMutated ( options ) {
  5866. return this.variable && this.variable.hasEffectsWhenMutated( options );
  5867. }
  5868. includeInBundle () {
  5869. if ( this.included ) { return false; }
  5870. this.included = true;
  5871. this.variable && this.variable.includeVariable();
  5872. return true;
  5873. }
  5874. initialiseAndDeclare ( parentScope, kind, init ) {
  5875. this.initialiseScope( parentScope );
  5876. switch ( kind ) {
  5877. case 'var':
  5878. case 'function':
  5879. this.scope.addDeclaration( this, true, init );
  5880. break;
  5881. case 'let':
  5882. case 'const':
  5883. case 'class':
  5884. this.scope.addDeclaration( this, false, init );
  5885. break;
  5886. case 'parameter':
  5887. this.scope.addParameterDeclaration( this );
  5888. break;
  5889. default:
  5890. throw new Error( 'Unexpected identifier kind', kind );
  5891. }
  5892. }
  5893. render ( code, es ) {
  5894. if ( this.variable ) {
  5895. const name = this.variable.getName( es );
  5896. if ( name !== this.name ) {
  5897. code.overwrite( this.start, this.end, name, { storeName: true, contentOnly: false } );
  5898. // special case
  5899. if ( this.parent.type === 'Property' && this.parent.shorthand ) {
  5900. code.appendLeft( this.start, `${this.name}: ` );
  5901. }
  5902. }
  5903. }
  5904. }
  5905. }
  5906. // Statement types which may contain if-statements as direct children.
  5907. const statementsWithIfStatements = new Set( [
  5908. 'DoWhileStatement',
  5909. 'ForInStatement',
  5910. 'ForOfStatement',
  5911. 'ForStatement',
  5912. 'IfStatement',
  5913. 'WhileStatement'
  5914. ] );
  5915. function getHoistedVars ( node, scope ) {
  5916. const hoistedVars = [];
  5917. function visit ( node ) {
  5918. if ( node.type === 'VariableDeclaration' && node.kind === 'var' ) {
  5919. node.declarations.forEach( declarator => {
  5920. declarator.init = null;
  5921. declarator.initialise( scope );
  5922. extractNames( declarator.id ).forEach( name => {
  5923. if ( hoistedVars.indexOf( name ) < 0 ) { hoistedVars.push( name ); }
  5924. } );
  5925. } );
  5926. }
  5927. else if ( !/Function/.test( node.type ) ) {
  5928. node.eachChild( visit );
  5929. }
  5930. }
  5931. visit( node );
  5932. return hoistedVars;
  5933. }
  5934. class IfStatement extends Statement {
  5935. initialiseChildren ( parentScope ) {
  5936. super.initialiseChildren( parentScope );
  5937. if ( this.module.bundle.treeshake ) {
  5938. this.testValue = this.test.getValue();
  5939. if ( this.testValue === UNKNOWN_VALUE ) {
  5940. return;
  5941. }
  5942. if ( this.testValue ) {
  5943. if ( this.alternate ) {
  5944. this.hoistedVars = getHoistedVars( this.alternate, this.scope );
  5945. this.alternate = null;
  5946. }
  5947. } else {
  5948. this.hoistedVars = getHoistedVars( this.consequent, this.scope );
  5949. this.consequent = null;
  5950. }
  5951. }
  5952. }
  5953. render ( code, es ) {
  5954. if ( this.module.bundle.treeshake ) {
  5955. if ( this.testValue === UNKNOWN_VALUE ) {
  5956. super.render( code, es );
  5957. }
  5958. else {
  5959. code.overwrite( this.test.start, this.test.end, JSON.stringify( this.testValue ) );
  5960. // TODO if no block-scoped declarations, remove enclosing
  5961. // curlies and dedent block (if there is a block)
  5962. if ( this.hoistedVars ) {
  5963. const names = this.hoistedVars
  5964. .map( name => {
  5965. const variable = this.scope.findVariable( name );
  5966. return variable.included ? variable.getName() : null;
  5967. } )
  5968. .filter( Boolean );
  5969. if ( names.length > 0 ) {
  5970. code.appendLeft( this.start, `var ${names.join( ', ' )};\n\n` );
  5971. }
  5972. }
  5973. if ( this.testValue ) {
  5974. code.remove( this.start, this.consequent.start );
  5975. code.remove( this.consequent.end, this.end );
  5976. this.consequent.render( code, es );
  5977. }
  5978. else {
  5979. code.remove( this.start, this.alternate ? this.alternate.start : this.next || this.end );
  5980. if ( this.alternate ) {
  5981. this.alternate.render( code, es );
  5982. }
  5983. else if ( statementsWithIfStatements.has( this.parent.type ) ) {
  5984. code.prependRight( this.start, '{}' );
  5985. }
  5986. }
  5987. }
  5988. }
  5989. else {
  5990. super.render( code, es );
  5991. }
  5992. }
  5993. }
  5994. class ImportDeclaration extends Node$1 {
  5995. bind () {
  5996. // noop
  5997. // TODO do the inter-module binding setup here?
  5998. }
  5999. initialiseNode () {
  6000. this.isImportDeclaration = true;
  6001. }
  6002. render ( code ) {
  6003. code.remove( this.start, this.next || this.end );
  6004. }
  6005. }
  6006. class LabeledStatement extends Statement {
  6007. hasEffects ( options ) {
  6008. return this.body.hasEffects(
  6009. options
  6010. .setIgnoreLabel( this.label.name )
  6011. .setIgnoreBreakStatements()
  6012. );
  6013. }
  6014. }
  6015. class Literal extends Node$1 {
  6016. getValue () {
  6017. return this.value;
  6018. }
  6019. hasEffectsWhenMutated () {
  6020. return false;
  6021. }
  6022. render ( code ) {
  6023. if ( typeof this.value === 'string' ) {
  6024. code.indentExclusionRanges.push( [ this.start + 1, this.end - 1 ] );
  6025. }
  6026. }
  6027. }
  6028. const operators$1 = {
  6029. '&&': ( left, right ) => left && right,
  6030. '||': ( left, right ) => left || right
  6031. };
  6032. class LogicalExpression extends Node$1 {
  6033. getValue () {
  6034. const leftValue = this.left.getValue();
  6035. if ( leftValue === UNKNOWN_VALUE ) { return UNKNOWN_VALUE; }
  6036. const rightValue = this.right.getValue();
  6037. if ( rightValue === UNKNOWN_VALUE ) { return UNKNOWN_VALUE; }
  6038. return operators$1[ this.operator ]( leftValue, rightValue );
  6039. }
  6040. hasEffectsWhenMutated ( options ) {
  6041. const leftValue = this.left.getValue();
  6042. if ( leftValue === UNKNOWN_VALUE ) {
  6043. return this.left.hasEffectsWhenMutated( options ) || this.right.hasEffectsWhenMutated( options );
  6044. }
  6045. if ((leftValue && this.operator === '||') || (!leftValue && this.operator === '&&')) {
  6046. return this.left.hasEffectsWhenMutated( options );
  6047. }
  6048. return this.right.hasEffectsWhenMutated( options );
  6049. }
  6050. }
  6051. function flatten ( node ) {
  6052. const parts = [];
  6053. while ( node.type === 'MemberExpression' ) {
  6054. if ( node.computed ) { return null; }
  6055. parts.unshift( node.property.name );
  6056. node = node.object;
  6057. }
  6058. if ( node.type !== 'Identifier' ) { return null; }
  6059. const name = node.name;
  6060. parts.unshift( name );
  6061. return { name, keypath: parts.join( '.' ) };
  6062. }
  6063. const pureFunctions = {};
  6064. const arrayTypes = 'Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array'.split( ' ' );
  6065. const simdTypes = 'Int8x16 Int16x8 Int32x4 Float32x4 Float64x2'.split( ' ' );
  6066. const simdMethods = 'abs add and bool check div equal extractLane fromFloat32x4 fromFloat32x4Bits fromFloat64x2 fromFloat64x2Bits fromInt16x8Bits fromInt32x4 fromInt32x4Bits fromInt8x16Bits greaterThan greaterThanOrEqual lessThan lessThanOrEqual load max maxNum min minNum mul neg not notEqual or reciprocalApproximation reciprocalSqrtApproximation replaceLane select selectBits shiftLeftByScalar shiftRightArithmeticByScalar shiftRightLogicalByScalar shuffle splat sqrt store sub swizzle xor'.split( ' ' );
  6067. const allSimdMethods = [];
  6068. simdTypes.forEach( t => {
  6069. simdMethods.forEach( m => {
  6070. allSimdMethods.push( `SIMD.${t}.${m}` );
  6071. });
  6072. });
  6073. [
  6074. 'Array.isArray',
  6075. 'Error', 'EvalError', 'InternalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError',
  6076. 'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'decodeURI', 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', 'escape', 'unescape',
  6077. 'Object', 'Object.create', 'Object.getNotifier', 'Object.getOwn', 'Object.getOwnPropertyDescriptor', 'Object.getOwnPropertyNames', 'Object.getOwnPropertySymbols', 'Object.getPrototypeOf', 'Object.is', 'Object.isExtensible', 'Object.isFrozen', 'Object.isSealed', 'Object.keys',
  6078. 'Function', 'Boolean',
  6079. 'Number', 'Number.isFinite', 'Number.isInteger', 'Number.isNaN', 'Number.isSafeInteger', 'Number.parseFloat', 'Number.parseInt',
  6080. 'Symbol', 'Symbol.for', 'Symbol.keyFor',
  6081. 'Math.abs', 'Math.acos', 'Math.acosh', 'Math.asin', 'Math.asinh', 'Math.atan', 'Math.atan2', 'Math.atanh', 'Math.cbrt', 'Math.ceil', 'Math.clz32', 'Math.cos', 'Math.cosh', 'Math.exp', 'Math.expm1', 'Math.floor', 'Math.fround', 'Math.hypot', 'Math.imul', 'Math.log', 'Math.log10', 'Math.log1p', 'Math.log2', 'Math.max', 'Math.min', 'Math.pow', 'Math.random', 'Math.round', 'Math.sign', 'Math.sin', 'Math.sinh', 'Math.sqrt', 'Math.tan', 'Math.tanh', 'Math.trunc',
  6082. 'Date', 'Date.UTC', 'Date.now', 'Date.parse',
  6083. 'String', 'String.fromCharCode', 'String.fromCodePoint', 'String.raw',
  6084. 'RegExp',
  6085. 'Map', 'Set', 'WeakMap', 'WeakSet',
  6086. 'ArrayBuffer', 'ArrayBuffer.isView',
  6087. 'DataView',
  6088. 'JSON.parse', 'JSON.stringify',
  6089. 'Promise.all', 'Promise.race', 'Promise.resolve',
  6090. 'Intl.Collator', 'Intl.Collator.supportedLocalesOf', 'Intl.DateTimeFormat', 'Intl.DateTimeFormat.supportedLocalesOf', 'Intl.NumberFormat', 'Intl.NumberFormat.supportedLocalesOf'
  6091. // TODO properties of e.g. window...
  6092. ].concat(
  6093. arrayTypes,
  6094. arrayTypes.map( t => `${t}.from` ),
  6095. arrayTypes.map( t => `${t}.of` ),
  6096. simdTypes.map( t => `SIMD.${t}` ),
  6097. allSimdMethods
  6098. ).forEach( name => pureFunctions[ name ] = true );
  6099. const validProp = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/;
  6100. class Keypath {
  6101. constructor ( node ) {
  6102. this.parts = [];
  6103. while ( node.type === 'MemberExpression' ) {
  6104. const prop = node.property;
  6105. if ( node.computed ) {
  6106. if ( prop.type !== 'Literal' || typeof prop.value !== 'string' || !validProp.test( prop.value ) ) {
  6107. this.computed = true;
  6108. return;
  6109. }
  6110. }
  6111. this.parts.unshift( prop );
  6112. node = node.object;
  6113. }
  6114. this.root = node;
  6115. }
  6116. }
  6117. class MemberExpression extends Node$1 {
  6118. bind () {
  6119. // if this resolves to a namespaced declaration, prepare
  6120. // to replace it
  6121. // TODO this code is a bit inefficient
  6122. const keypath = new Keypath( this );
  6123. if ( !keypath.computed && keypath.root.type === 'Identifier' ) {
  6124. let variable = this.scope.findVariable( keypath.root.name );
  6125. while ( variable.isNamespace && keypath.parts.length ) {
  6126. const exporterId = variable.module.id;
  6127. const part = keypath.parts[ 0 ];
  6128. variable = variable.module.traceExport( part.name || part.value );
  6129. if ( !variable ) {
  6130. this.module.warn( {
  6131. code: 'MISSING_EXPORT',
  6132. missing: part.name || part.value,
  6133. importer: relativeId( this.module.id ),
  6134. exporter: relativeId( exporterId ),
  6135. message: `'${part.name || part.value}' is not exported by '${relativeId( exporterId )}'`,
  6136. url: `https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module`
  6137. }, part.start );
  6138. this.replacement = 'undefined';
  6139. return;
  6140. }
  6141. keypath.parts.shift();
  6142. }
  6143. if ( keypath.parts.length ) {
  6144. super.bind();
  6145. return; // not a namespaced declaration
  6146. }
  6147. this.variable = variable;
  6148. if ( variable.isExternal ) {
  6149. variable.module.suggestName( keypath.root.name );
  6150. }
  6151. }
  6152. else {
  6153. super.bind();
  6154. }
  6155. }
  6156. bindCall ( callOptions ) {
  6157. if ( this.variable ) {
  6158. this.variable.addCall( callOptions );
  6159. }
  6160. }
  6161. hasEffectsWhenAssigned ( options ) {
  6162. return this.object.hasEffectsWhenMutated( options );
  6163. }
  6164. includeInBundle () {
  6165. let addedNewNodes = super.includeInBundle();
  6166. if ( this.variable && !this.variable.included ) {
  6167. this.variable.includeVariable();
  6168. addedNewNodes = true;
  6169. }
  6170. return addedNewNodes;
  6171. }
  6172. hasEffectsWhenCalled ( options ) {
  6173. if ( this.variable ) {
  6174. return this.variable.hasEffectsWhenCalled( options );
  6175. }
  6176. if ( !isReference( this ) ) {
  6177. return true;
  6178. }
  6179. const flattenedNode = flatten( this );
  6180. return !(this.scope.findVariable( flattenedNode.name ).isGlobal && pureFunctions[ flattenedNode.keypath ]);
  6181. }
  6182. render ( code, es ) {
  6183. if ( this.variable ) {
  6184. const name = this.variable.getName( es );
  6185. if ( name !== this.name ) { code.overwrite( this.start, this.end, name, { storeName: true, contentOnly: false } ); }
  6186. }
  6187. else if ( this.replacement ) {
  6188. code.overwrite( this.start, this.end, this.replacement, { storeName: true, contentOnly: false } );
  6189. }
  6190. super.render( code, es );
  6191. }
  6192. }
  6193. class MethodDefinition extends Node$1 {
  6194. bindCall ( callOptions ) {
  6195. this.value.bindCall( callOptions );
  6196. }
  6197. hasEffects ( options ) {
  6198. return this.key.hasEffects( options );
  6199. }
  6200. hasEffectsWhenCalled ( options ) {
  6201. return this.value.hasEffectsWhenCalled( options );
  6202. }
  6203. }
  6204. class NewExpression extends Node$1 {
  6205. bind () {
  6206. super.bind();
  6207. this.callee.bindCall( { withNew: true } );
  6208. }
  6209. hasEffects ( options ) {
  6210. return this.included
  6211. || this.arguments.some( child => child.hasEffects( options ) )
  6212. || this.callee.hasEffectsWhenCalled( options.getHasEffectsWhenCalledOptions( this.callee ) );
  6213. }
  6214. }
  6215. class ObjectExpression extends Node$1 {
  6216. hasEffectsWhenMutated () {
  6217. return false;
  6218. }
  6219. }
  6220. class ObjectPattern extends Node$1 {
  6221. bindAssignment ( expression ) {
  6222. this.properties.forEach( child => child.bindAssignment( expression ) );
  6223. }
  6224. hasEffectsWhenAssigned ( options ) {
  6225. return this.someChild( child => child.hasEffectsWhenAssigned( options ) );
  6226. }
  6227. initialiseAndDeclare ( parentScope, kind, init ) {
  6228. this.initialiseScope( parentScope );
  6229. this.properties.forEach( child => child.initialiseAndDeclare( parentScope, kind, init ) );
  6230. }
  6231. }
  6232. class Property extends Node$1 {
  6233. bindAssignment () {
  6234. this.value.bindAssignment( UNKNOWN_ASSIGNMENT );
  6235. }
  6236. hasEffectsWhenAssigned ( options ) {
  6237. return this.value.hasEffectsWhenAssigned( options );
  6238. }
  6239. initialiseAndDeclare ( parentScope, kind, init ) {
  6240. this.initialiseScope( parentScope );
  6241. this.key.initialise( parentScope );
  6242. this.value.initialiseAndDeclare( parentScope, kind, init && UNKNOWN_ASSIGNMENT );
  6243. }
  6244. render ( code, es ) {
  6245. if ( !this.shorthand ) {
  6246. this.key.render( code, es );
  6247. }
  6248. this.value.render( code, es );
  6249. }
  6250. }
  6251. class RestElement extends Node$1 {
  6252. bindAssignment () {
  6253. this.argument.bindAssignment( UNKNOWN_ASSIGNMENT );
  6254. }
  6255. hasEffectsWhenAssigned ( options ) {
  6256. return this.argument.hasEffectsWhenAssigned( options );
  6257. }
  6258. initialiseAndDeclare ( parentScope, kind ) {
  6259. this.initialiseScope( parentScope );
  6260. this.argument.initialiseAndDeclare( parentScope, kind, UNKNOWN_ASSIGNMENT );
  6261. }
  6262. }
  6263. class ReturnStatement extends Statement {
  6264. hasEffects ( options ) {
  6265. return super.hasEffects( options )
  6266. || !options.ignoreReturnAwaitYield();
  6267. }
  6268. }
  6269. class SwitchCase extends Node$1 {
  6270. includeInBundle () {
  6271. if ( this.isFullyIncluded() ) { return false; }
  6272. let addedNewNodes = false;
  6273. if (this.test && this.test.includeInBundle()) {
  6274. addedNewNodes = true;
  6275. }
  6276. this.consequent.forEach( node => {
  6277. if ( node.shouldBeIncluded() ) {
  6278. if ( node.includeInBundle() ) {
  6279. addedNewNodes = true;
  6280. }
  6281. }
  6282. } );
  6283. if ( !this.included || addedNewNodes ) {
  6284. this.included = true;
  6285. return true;
  6286. }
  6287. return false;
  6288. }
  6289. }
  6290. class SwitchStatement extends Statement {
  6291. hasEffects ( options ) {
  6292. return super.hasEffects( options.setIgnoreBreakStatements() );
  6293. }
  6294. initialiseScope ( parentScope ) {
  6295. this.scope = new BlockScope( { parent: parentScope } );
  6296. }
  6297. }
  6298. class TaggedTemplateExpression extends Node$1 {
  6299. bind () {
  6300. if ( this.tag.type === 'Identifier' ) {
  6301. const variable = this.scope.findVariable( this.tag.name );
  6302. if ( variable.isNamespace ) {
  6303. this.module.error( {
  6304. code: 'CANNOT_CALL_NAMESPACE',
  6305. message: `Cannot call a namespace ('${this.tag.name}')`
  6306. }, this.start );
  6307. }
  6308. if ( this.tag.name === 'eval' && variable.isGlobal ) {
  6309. this.module.warn( {
  6310. code: 'EVAL',
  6311. message: `Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification`,
  6312. url: 'https://github.com/rollup/rollup/wiki/Troubleshooting#avoiding-eval'
  6313. }, this.start );
  6314. }
  6315. }
  6316. super.bind();
  6317. this.tag.bindCall( { withNew: false } );
  6318. }
  6319. hasEffects ( options ) {
  6320. return super.hasEffects( options )
  6321. || this.tag.hasEffectsWhenCalled( options.getHasEffectsWhenCalledOptions( this.tag ) );
  6322. }
  6323. }
  6324. class TemplateElement extends Node$1 {
  6325. hasEffects() {
  6326. return false;
  6327. }
  6328. }
  6329. class TemplateLiteral extends Node$1 {
  6330. render ( code, es ) {
  6331. code.indentExclusionRanges.push( [ this.start, this.end ] );
  6332. super.render( code, es );
  6333. }
  6334. }
  6335. class ThisExpression extends Node$1 {
  6336. initialiseNode () {
  6337. const lexicalBoundary = this.scope.findLexicalBoundary();
  6338. if ( lexicalBoundary.isModuleScope ) {
  6339. this.alias = this.module.context;
  6340. if ( this.alias === 'undefined' ) {
  6341. this.module.warn( {
  6342. code: 'THIS_IS_UNDEFINED',
  6343. message: `The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten`,
  6344. url: `https://github.com/rollup/rollup/wiki/Troubleshooting#this-is-undefined`
  6345. }, this.start );
  6346. }
  6347. }
  6348. }
  6349. bind () {
  6350. this.variable = this.scope.findVariable( 'this' );
  6351. }
  6352. hasEffectsWhenMutated ( options ) {
  6353. return !options.ignoreSafeThisMutations() || this.variable.hasEffectsWhenMutated( options );
  6354. }
  6355. render ( code ) {
  6356. if ( this.alias ) {
  6357. code.overwrite( this.start, this.end, this.alias, { storeName: true, contentOnly: false } );
  6358. }
  6359. }
  6360. }
  6361. class ThrowStatement extends Node$1 {
  6362. hasEffects () {
  6363. return true;
  6364. }
  6365. }
  6366. const operators$2 = {
  6367. '-': value => -value,
  6368. '+': value => +value,
  6369. '!': value => !value,
  6370. '~': value => ~value,
  6371. typeof: value => typeof value,
  6372. void: () => undefined,
  6373. delete: () => UNKNOWN_VALUE
  6374. };
  6375. class UnaryExpression extends Node$1 {
  6376. bind () {
  6377. if ( this.value === UNKNOWN_VALUE ) { super.bind(); }
  6378. }
  6379. getValue () {
  6380. const argumentValue = this.argument.getValue();
  6381. if ( argumentValue === UNKNOWN_VALUE ) { return UNKNOWN_VALUE; }
  6382. return operators$2[ this.operator ]( argumentValue );
  6383. }
  6384. hasEffects ( options ) {
  6385. return this.included
  6386. || this.argument.hasEffects( options )
  6387. || (this.operator === 'delete' && (
  6388. this.argument.type !== 'MemberExpression'
  6389. || this.argument.object.hasEffectsWhenMutated( options )
  6390. ));
  6391. }
  6392. hasEffectsAsExpressionStatement ( options ) {
  6393. return this.hasEffects( options );
  6394. }
  6395. initialiseNode () {
  6396. this.value = this.getValue();
  6397. }
  6398. }
  6399. class UpdateExpression extends Node$1 {
  6400. bind () {
  6401. disallowIllegalReassignment( this.scope, this.argument );
  6402. if ( this.argument.type === 'Identifier' ) {
  6403. const variable = this.scope.findVariable( this.argument.name );
  6404. variable.isReassigned = true;
  6405. }
  6406. super.bind();
  6407. }
  6408. hasEffects ( options ) {
  6409. return this.included || this.argument.hasEffectsWhenAssigned( options );
  6410. }
  6411. hasEffectsAsExpressionStatement ( options ) {
  6412. return this.hasEffects( options );
  6413. }
  6414. }
  6415. class VariableDeclarator extends Node$1 {
  6416. bindAssignment ( expression ) {
  6417. this.id.bindAssignment( expression );
  6418. }
  6419. initialiseDeclarator ( parentScope, kind ) {
  6420. this.initialiseScope( parentScope );
  6421. this.init && this.init.initialise( this.scope );
  6422. this.id.initialiseAndDeclare( this.scope, kind, this.init );
  6423. }
  6424. // TODO Deleting this does not break any tests. Find meaningful test or delete.
  6425. render ( code, es ) {
  6426. extractNames( this.id ).forEach( name => {
  6427. const variable = this.scope.findVariable( name );
  6428. if ( !es && variable.exportName && variable.isReassigned ) {
  6429. if ( this.init ) {
  6430. code.overwrite( this.start, this.id.end, variable.getName( es ) );
  6431. } else if ( this.module.bundle.treeshake ) {
  6432. code.remove( this.start, this.end );
  6433. }
  6434. }
  6435. } );
  6436. super.render( code, es );
  6437. }
  6438. }
  6439. function getSeparator ( code, start ) {
  6440. let c = start;
  6441. while ( c > 0 && code[ c - 1 ] !== '\n' ) {
  6442. c -= 1;
  6443. if ( code[ c ] === ';' || code[ c ] === '{' ) { return '; '; }
  6444. }
  6445. const lineStart = code.slice( c, start ).match( /^\s*/ )[ 0 ];
  6446. return `;\n${lineStart}`;
  6447. }
  6448. const forStatement = /^For(?:Of|In)?Statement/;
  6449. class VariableDeclaration extends Node$1 {
  6450. bindAssignment () {
  6451. this.eachChild( child => child.bindAssignment( UNKNOWN_ASSIGNMENT ) );
  6452. }
  6453. hasEffectsWhenAssigned () {
  6454. return false;
  6455. }
  6456. includeWithAllDeclarations () {
  6457. if ( this.isFullyIncluded() ) { return false; }
  6458. let addedNewNodes = false;
  6459. this.declarations.forEach( declarator => {
  6460. if ( declarator.includeInBundle() ) {
  6461. addedNewNodes = true;
  6462. }
  6463. } );
  6464. if ( !this.included || addedNewNodes ) {
  6465. this.included = true;
  6466. return true;
  6467. }
  6468. return false;
  6469. }
  6470. includeInBundle () {
  6471. if ( this.isFullyIncluded() ) { return false; }
  6472. let addedNewNodes = false;
  6473. this.declarations.forEach( declarator => {
  6474. if ( declarator.shouldBeIncluded() ) {
  6475. if ( declarator.includeInBundle() ) {
  6476. addedNewNodes = true;
  6477. }
  6478. }
  6479. } );
  6480. if ( !this.included || addedNewNodes ) {
  6481. this.included = true;
  6482. return true;
  6483. }
  6484. return false;
  6485. }
  6486. initialiseChildren () {
  6487. this.declarations.forEach( child => child.initialiseDeclarator( this.scope, this.kind ) );
  6488. }
  6489. render ( code, es ) {
  6490. const treeshake = this.module.bundle.treeshake;
  6491. let shouldSeparate = false;
  6492. let separator;
  6493. if ( this.scope.isModuleScope && !forStatement.test( this.parent.type ) ) {
  6494. shouldSeparate = true;
  6495. separator = getSeparator( this.module.code, this.start );
  6496. }
  6497. let c = this.start;
  6498. let empty = true;
  6499. for ( let i = 0; i < this.declarations.length; i += 1 ) {
  6500. const declarator = this.declarations[ i ];
  6501. const prefix = empty ? '' : separator; // TODO indentation
  6502. if ( declarator.id.type === 'Identifier' ) {
  6503. const variable = this.scope.findVariable( declarator.id.name );
  6504. const isExportedAndReassigned = !es && variable.exportName && variable.isReassigned;
  6505. if ( isExportedAndReassigned ) {
  6506. if ( declarator.init ) {
  6507. if ( shouldSeparate ) { code.overwrite( c, declarator.start, prefix ); }
  6508. c = declarator.end;
  6509. empty = false;
  6510. }
  6511. } else if ( !treeshake || variable.included ) {
  6512. if ( shouldSeparate ) { code.overwrite( c, declarator.start, `${prefix}${this.kind} ` ); } // TODO indentation
  6513. c = declarator.end;
  6514. empty = false;
  6515. }
  6516. } else {
  6517. const exportAssignments = [];
  6518. let isIncluded = false;
  6519. extractNames( declarator.id ).forEach( name => {
  6520. const variable = this.scope.findVariable( name );
  6521. const isExportedAndReassigned = !es && variable.exportName && variable.isReassigned;
  6522. if ( isExportedAndReassigned ) {
  6523. // code.overwrite( c, declarator.start, prefix );
  6524. // c = declarator.end;
  6525. // empty = false;
  6526. exportAssignments.push( 'TODO' );
  6527. } else if ( declarator.included ) {
  6528. isIncluded = true;
  6529. }
  6530. } );
  6531. if ( !treeshake || isIncluded ) {
  6532. if ( shouldSeparate ) { code.overwrite( c, declarator.start, `${prefix}${this.kind} ` ); } // TODO indentation
  6533. c = declarator.end;
  6534. empty = false;
  6535. }
  6536. if ( exportAssignments.length ) {
  6537. throw new Error( 'TODO' );
  6538. }
  6539. }
  6540. declarator.render( code, es );
  6541. }
  6542. if ( treeshake && empty ) {
  6543. code.remove( this.leadingCommentStart || this.start, this.next || this.end );
  6544. } else {
  6545. // always include a semi-colon (https://github.com/rollup/rollup/pull/1013),
  6546. // unless it's a var declaration in a loop head
  6547. const needsSemicolon = !forStatement.test( this.parent.type ) || this === this.parent.body;
  6548. if ( this.end > c ) {
  6549. code.overwrite( c, this.end, needsSemicolon ? ';' : '' );
  6550. } else if ( needsSemicolon ) {
  6551. this.insertSemicolon( code );
  6552. }
  6553. }
  6554. }
  6555. }
  6556. class WhileStatement extends Statement {
  6557. hasEffects ( options ) {
  6558. return (
  6559. this.included
  6560. || this.test.hasEffects( options )
  6561. || this.body.hasEffects( options.setIgnoreBreakStatements() )
  6562. );
  6563. }
  6564. }
  6565. class YieldExpression extends Node$1 {
  6566. hasEffects ( options ) {
  6567. return super.hasEffects( options )
  6568. || !options.ignoreReturnAwaitYield();
  6569. }
  6570. hasEffectsAsExpressionStatement ( options ) {
  6571. return this.hasEffects( options );
  6572. }
  6573. }
  6574. var nodes = {
  6575. ArrayExpression: Node$1,
  6576. ArrayPattern,
  6577. ArrowFunctionExpression,
  6578. AssignmentExpression,
  6579. AssignmentPattern,
  6580. AwaitExpression,
  6581. BinaryExpression,
  6582. BlockStatement,
  6583. BreakStatement,
  6584. CallExpression,
  6585. CatchClause,
  6586. ClassBody,
  6587. ClassDeclaration,
  6588. ClassExpression,
  6589. ConditionalExpression,
  6590. DoWhileStatement,
  6591. EmptyStatement,
  6592. ExportAllDeclaration,
  6593. ExportDefaultDeclaration,
  6594. ExportNamedDeclaration,
  6595. ExpressionStatement,
  6596. ForStatement,
  6597. ForInStatement,
  6598. ForOfStatement,
  6599. FunctionDeclaration,
  6600. FunctionExpression,
  6601. Identifier,
  6602. IfStatement,
  6603. ImportDeclaration,
  6604. LabeledStatement,
  6605. Literal,
  6606. LogicalExpression,
  6607. MemberExpression,
  6608. MethodDefinition,
  6609. NewExpression,
  6610. ObjectExpression,
  6611. ObjectPattern,
  6612. Property,
  6613. RestElement,
  6614. ReturnStatement,
  6615. SwitchCase,
  6616. SwitchStatement,
  6617. TaggedTemplateExpression,
  6618. TemplateElement,
  6619. TemplateLiteral,
  6620. ThisExpression,
  6621. ThrowStatement,
  6622. TryStatement: Statement,
  6623. UnaryExpression,
  6624. UpdateExpression,
  6625. VariableDeclarator,
  6626. VariableDeclaration,
  6627. WhileStatement,
  6628. YieldExpression
  6629. };
  6630. class UnknownNode extends Node$1 {
  6631. hasEffects () {
  6632. return true;
  6633. }
  6634. }
  6635. var keys$1 = {
  6636. Program: [ 'body' ],
  6637. Literal: []
  6638. };
  6639. const newline = /\n/;
  6640. function enhance ( ast, module, comments ) {
  6641. enhanceNode( ast, module, module, module.magicString );
  6642. let comment = comments.shift();
  6643. for ( const node of ast.body ) {
  6644. if ( comment && ( comment.start < node.start ) ) {
  6645. node.leadingCommentStart = comment.start;
  6646. }
  6647. while ( comment && comment.end < node.end ) { comment = comments.shift(); }
  6648. // if the next comment is on the same line as the end of the node,
  6649. // treat is as a trailing comment
  6650. if ( comment && !newline.test( module.code.slice( node.end, comment.start ) ) ) {
  6651. node.trailingCommentEnd = comment.end; // TODO is node.trailingCommentEnd used anywhere?
  6652. comment = comments.shift();
  6653. }
  6654. node.initialise( module.scope );
  6655. }
  6656. }
  6657. function enhanceNode ( raw, parent, module, code ) {
  6658. if ( !raw ) { return; }
  6659. if ( 'length' in raw ) {
  6660. for ( let i = 0; i < raw.length; i += 1 ) {
  6661. enhanceNode( raw[i], parent, module, code );
  6662. }
  6663. return;
  6664. }
  6665. // with e.g. shorthand properties, key and value are
  6666. // the same node. We don't want to enhance an object twice
  6667. if ( raw.__enhanced ) { return; }
  6668. raw.__enhanced = true;
  6669. if ( !keys$1[ raw.type ] ) {
  6670. keys$1[ raw.type ] = Object.keys( raw ).filter( key => typeof raw[ key ] === 'object' );
  6671. }
  6672. raw.parent = parent;
  6673. raw.module = module;
  6674. raw.keys = keys$1[ raw.type ];
  6675. code.addSourcemapLocation( raw.start );
  6676. code.addSourcemapLocation( raw.end );
  6677. for ( const key of keys$1[ raw.type ] ) {
  6678. enhanceNode( raw[ key ], raw, module, code );
  6679. }
  6680. const type = nodes[ raw.type ] || UnknownNode;
  6681. raw.__proto__ = type.prototype;
  6682. }
  6683. function clone ( node ) {
  6684. if ( !node ) { return node; }
  6685. if ( typeof node !== 'object' ) { return node; }
  6686. if ( Array.isArray( node ) ) {
  6687. const cloned = new Array( node.length );
  6688. for ( let i = 0; i < node.length; i += 1 ) { cloned[i] = clone( node[i] ); }
  6689. return cloned;
  6690. }
  6691. const cloned = {};
  6692. for ( const key in node ) {
  6693. cloned[ key ] = clone( node[ key ] );
  6694. }
  6695. return cloned;
  6696. }
  6697. class ModuleScope extends Scope {
  6698. constructor ( module ) {
  6699. super( {
  6700. isModuleScope: true,
  6701. parent: module.bundle.scope
  6702. } );
  6703. this.module = module;
  6704. this.variables.this = new LocalVariable( 'this', null, UNDEFINED_ASSIGNMENT );
  6705. }
  6706. deshadow ( names ) {
  6707. names = new Set( names );
  6708. forOwn( this.module.imports, specifier => {
  6709. if ( specifier.module.isExternal ) { return; }
  6710. const addDeclaration = declaration => {
  6711. if ( declaration.isNamespace && !declaration.isExternal ) {
  6712. declaration.module.getExports().forEach( name => {
  6713. addDeclaration( declaration.module.traceExport( name ) );
  6714. } );
  6715. }
  6716. names.add( declaration.name );
  6717. };
  6718. specifier.module.getExports().forEach( name => {
  6719. addDeclaration( specifier.module.traceExport( name ) );
  6720. } );
  6721. if ( specifier.name !== '*' ) {
  6722. const declaration = specifier.module.traceExport( specifier.name );
  6723. if ( !declaration ) {
  6724. this.module.warn( {
  6725. code: 'NON_EXISTENT_EXPORT',
  6726. name: specifier.name,
  6727. source: specifier.module.id,
  6728. message: `Non-existent export '${specifier.name}' is imported from ${relativeId( specifier.module.id )}`
  6729. }, specifier.specifier.start );
  6730. return;
  6731. }
  6732. const name = declaration.getName( true );
  6733. if ( name !== specifier.name ) {
  6734. names.add( declaration.getName( true ) );
  6735. }
  6736. if ( specifier.name !== 'default' && specifier.specifier.imported.name !== specifier.specifier.local.name ) {
  6737. names.add( specifier.specifier.imported.name );
  6738. }
  6739. }
  6740. } );
  6741. super.deshadow( names );
  6742. }
  6743. findLexicalBoundary () {
  6744. return this;
  6745. }
  6746. findVariable ( name ) {
  6747. if ( this.variables[ name ] ) {
  6748. return this.variables[ name ];
  6749. }
  6750. return this.module.trace( name ) || this.parent.findVariable( name );
  6751. }
  6752. }
  6753. function tryParse ( module, acornOptions ) {
  6754. try {
  6755. return parse( module.code, assign( {
  6756. ecmaVersion: 8,
  6757. sourceType: 'module',
  6758. onComment: ( block, text, start, end ) => module.comments.push( { block, text, start, end } ),
  6759. preserveParens: false
  6760. }, acornOptions ) );
  6761. } catch ( err ) {
  6762. module.error( {
  6763. code: 'PARSE_ERROR',
  6764. message: err.message.replace( / \(\d+:\d+\)$/, '' )
  6765. }, err.pos );
  6766. }
  6767. }
  6768. function includeFully ( node ) {
  6769. node.includeInBundle();
  6770. node.eachChild( includeFully );
  6771. }
  6772. class Module {
  6773. constructor ( ref ) {
  6774. var id = ref.id;
  6775. var code = ref.code;
  6776. var originalCode = ref.originalCode;
  6777. var originalSourcemap = ref.originalSourcemap;
  6778. var ast = ref.ast;
  6779. var sourcemapChain = ref.sourcemapChain;
  6780. var resolvedIds = ref.resolvedIds;
  6781. var resolvedExternalIds = ref.resolvedExternalIds;
  6782. var bundle = ref.bundle;
  6783. this.code = code;
  6784. this.id = id;
  6785. this.bundle = bundle;
  6786. this.originalCode = originalCode;
  6787. this.originalSourcemap = originalSourcemap;
  6788. this.sourcemapChain = sourcemapChain;
  6789. this.comments = [];
  6790. timeStart( 'ast' );
  6791. if ( ast ) {
  6792. // prevent mutating the provided AST, as it may be reused on
  6793. // subsequent incremental rebuilds
  6794. this.ast = clone( ast );
  6795. this.astClone = ast;
  6796. } else {
  6797. this.ast = tryParse( this, bundle.acornOptions ); // TODO what happens to comments if AST is provided?
  6798. this.astClone = clone( this.ast );
  6799. }
  6800. timeEnd( 'ast' );
  6801. this.excludeFromSourcemap = /\0/.test( id );
  6802. this.context = bundle.getModuleContext( id );
  6803. // all dependencies
  6804. this.sources = [];
  6805. this.dependencies = [];
  6806. this.resolvedIds = resolvedIds || blank();
  6807. this.resolvedExternalIds = resolvedExternalIds || blank();
  6808. // imports and exports, indexed by local name
  6809. this.imports = blank();
  6810. this.exports = blank();
  6811. this.exportsAll = blank();
  6812. this.reexports = blank();
  6813. this.exportAllSources = [];
  6814. this.exportAllModules = null;
  6815. // By default, `id` is the filename. Custom resolvers and loaders
  6816. // can change that, but it makes sense to use it for the source filename
  6817. this.magicString = new MagicString$1( code, {
  6818. filename: this.excludeFromSourcemap ? null : id, // don't include plugin helpers in sourcemap
  6819. indentExclusionRanges: []
  6820. } );
  6821. // remove existing sourceMappingURL comments
  6822. this.comments = this.comments.filter( comment => {
  6823. //only one line comment can contain source maps
  6824. const isSourceMapComment = !comment.block && SOURCEMAPPING_URL_RE.test( comment.text );
  6825. if ( isSourceMapComment ) {
  6826. this.magicString.remove( comment.start, comment.end );
  6827. }
  6828. return !isSourceMapComment;
  6829. } );
  6830. this.declarations = blank();
  6831. this.type = 'Module'; // TODO only necessary so that Scope knows this should be treated as a function scope... messy
  6832. this.scope = new ModuleScope( this );
  6833. timeStart( 'analyse' );
  6834. this.analyse();
  6835. timeEnd( 'analyse' );
  6836. this.strongDependencies = [];
  6837. }
  6838. addExport ( node ) {
  6839. const source = node.source && node.source.value;
  6840. // export { name } from './other.js'
  6841. if ( source ) {
  6842. if ( !~this.sources.indexOf( source ) ) { this.sources.push( source ); }
  6843. if ( node.type === 'ExportAllDeclaration' ) {
  6844. // Store `export * from '...'` statements in an array of delegates.
  6845. // When an unknown import is encountered, we see if one of them can satisfy it.
  6846. this.exportAllSources.push( source );
  6847. }
  6848. else {
  6849. node.specifiers.forEach( specifier => {
  6850. const name = specifier.exported.name;
  6851. if ( this.exports[ name ] || this.reexports[ name ] ) {
  6852. this.error( {
  6853. code: 'DUPLICATE_EXPORT',
  6854. message: `A module cannot have multiple exports with the same name ('${name}')`
  6855. }, specifier.start );
  6856. }
  6857. this.reexports[ name ] = {
  6858. start: specifier.start,
  6859. source,
  6860. localName: specifier.local.name,
  6861. module: null // filled in later
  6862. };
  6863. } );
  6864. }
  6865. }
  6866. // export default function foo () {}
  6867. // export default foo;
  6868. // export default 42;
  6869. else if ( node.type === 'ExportDefaultDeclaration' ) {
  6870. const identifier = ( node.declaration.id && node.declaration.id.name ) || node.declaration.name;
  6871. if ( this.exports.default ) {
  6872. this.error( {
  6873. code: 'DUPLICATE_EXPORT',
  6874. message: `A module can only have one default export`
  6875. }, node.start );
  6876. }
  6877. this.exports.default = {
  6878. localName: 'default',
  6879. identifier
  6880. };
  6881. }
  6882. // export var { foo, bar } = ...
  6883. // export var foo = 42;
  6884. // export var a = 1, b = 2, c = 3;
  6885. // export function foo () {}
  6886. else if ( node.declaration ) {
  6887. const declaration = node.declaration;
  6888. if ( declaration.type === 'VariableDeclaration' ) {
  6889. declaration.declarations.forEach( decl => {
  6890. extractNames( decl.id ).forEach( localName => {
  6891. this.exports[ localName ] = { localName };
  6892. } );
  6893. } );
  6894. } else {
  6895. // export function foo () {}
  6896. const localName = declaration.id.name;
  6897. this.exports[ localName ] = { localName };
  6898. }
  6899. }
  6900. // export { foo, bar, baz }
  6901. else {
  6902. node.specifiers.forEach( specifier => {
  6903. const localName = specifier.local.name;
  6904. const exportedName = specifier.exported.name;
  6905. if ( this.exports[ exportedName ] || this.reexports[ exportedName ] ) {
  6906. this.error( {
  6907. code: 'DUPLICATE_EXPORT',
  6908. message: `A module cannot have multiple exports with the same name ('${exportedName}')`
  6909. }, specifier.start );
  6910. }
  6911. this.exports[ exportedName ] = { localName };
  6912. } );
  6913. }
  6914. }
  6915. addImport ( node ) {
  6916. const source = node.source.value;
  6917. if ( !~this.sources.indexOf( source ) ) { this.sources.push( source ); }
  6918. node.specifiers.forEach( specifier => {
  6919. const localName = specifier.local.name;
  6920. if ( this.imports[ localName ] ) {
  6921. this.error( {
  6922. code: 'DUPLICATE_IMPORT',
  6923. message: `Duplicated import '${localName}'`
  6924. }, specifier.start );
  6925. }
  6926. const isDefault = specifier.type === 'ImportDefaultSpecifier';
  6927. const isNamespace = specifier.type === 'ImportNamespaceSpecifier';
  6928. const name = isDefault ? 'default' : isNamespace ? '*' : specifier.imported.name;
  6929. this.imports[ localName ] = { source, specifier, name, module: null };
  6930. } );
  6931. }
  6932. analyse () {
  6933. enhance( this.ast, this, this.comments );
  6934. // discover this module's imports and exports
  6935. let lastNode;
  6936. for ( const node of this.ast.body ) {
  6937. if ( node.isImportDeclaration ) {
  6938. this.addImport( node );
  6939. } else if ( node.isExportDeclaration ) {
  6940. this.addExport( node );
  6941. }
  6942. if ( lastNode ) { lastNode.next = node.leadingCommentStart || node.start; }
  6943. lastNode = node;
  6944. }
  6945. }
  6946. basename () {
  6947. const base = basename( this.id );
  6948. const ext = extname( this.id );
  6949. return makeLegal( ext ? base.slice( 0, -ext.length ) : base );
  6950. }
  6951. bindImportSpecifiers () {
  6952. [ this.imports, this.reexports ].forEach( specifiers => {
  6953. keys( specifiers ).forEach( name => {
  6954. const specifier = specifiers[ name ];
  6955. const id = this.resolvedIds[ specifier.source ] || this.resolvedExternalIds[ specifier.source ];
  6956. specifier.module = this.bundle.moduleById.get( id );
  6957. } );
  6958. } );
  6959. this.exportAllModules = this.exportAllSources.map( source => {
  6960. const id = this.resolvedIds[ source ] || this.resolvedExternalIds[ source ];
  6961. return this.bundle.moduleById.get( id );
  6962. } );
  6963. this.sources.forEach( source => {
  6964. const id = this.resolvedIds[ source ];
  6965. if ( id ) {
  6966. const module = this.bundle.moduleById.get( id );
  6967. this.dependencies.push( module );
  6968. }
  6969. } );
  6970. }
  6971. bindReferences () {
  6972. for ( const node of this.ast.body ) {
  6973. node.bind();
  6974. }
  6975. // if ( this.declarations.default ) {
  6976. // if ( this.exports.default.identifier ) {
  6977. // const declaration = this.trace( this.exports.default.identifier );
  6978. // if ( declaration ) this.declarations.default.bind( declaration );
  6979. // }
  6980. // }
  6981. }
  6982. error ( props, pos ) {
  6983. if ( pos !== undefined ) {
  6984. props.pos = pos;
  6985. var ref = locate( this.code, pos, { offsetLine: 1 } );
  6986. var line = ref.line;
  6987. var column = ref.column; // TODO trace sourcemaps
  6988. props.loc = { file: this.id, line, column };
  6989. props.frame = getCodeFrame( this.code, line, column );
  6990. }
  6991. error( props );
  6992. }
  6993. getExports () {
  6994. return keys( this.exports );
  6995. }
  6996. getReexports () {
  6997. const reexports = blank();
  6998. keys( this.reexports ).forEach( name => {
  6999. reexports[ name ] = true;
  7000. } );
  7001. this.exportAllModules.forEach( module => {
  7002. if ( module.isExternal ) {
  7003. reexports[ `*${module.id}` ] = true;
  7004. return;
  7005. }
  7006. module.getExports().concat( module.getReexports() ).forEach( name => {
  7007. if ( name !== 'default' ) { reexports[ name ] = true; }
  7008. } );
  7009. } );
  7010. return keys( reexports );
  7011. }
  7012. includeAllInBundle () {
  7013. this.ast.body.forEach( includeFully );
  7014. }
  7015. includeInBundle () {
  7016. let addedNewNodes = false;
  7017. this.ast.body.forEach( node => {
  7018. if ( node.shouldBeIncluded() ) {
  7019. if ( node.includeInBundle() ) {
  7020. addedNewNodes = true;
  7021. }
  7022. }
  7023. } );
  7024. return addedNewNodes;
  7025. }
  7026. namespace () {
  7027. if ( !this.declarations[ '*' ] ) {
  7028. this.declarations[ '*' ] = new NamespaceVariable( this );
  7029. }
  7030. return this.declarations[ '*' ];
  7031. }
  7032. render ( es, legacy ) {
  7033. const magicString = this.magicString.clone();
  7034. for ( const node of this.ast.body ) {
  7035. node.render( magicString, es );
  7036. }
  7037. if ( this.namespace().needsNamespaceBlock ) {
  7038. magicString.append( '\n\n' + this.namespace().renderBlock( es, legacy, '\t' ) ); // TODO use correct indentation
  7039. }
  7040. return magicString.trim();
  7041. }
  7042. toJSON () {
  7043. return {
  7044. id: this.id,
  7045. dependencies: this.dependencies.map( module => module.id ),
  7046. code: this.code,
  7047. originalCode: this.originalCode,
  7048. originalSourcemap: this.originalSourcemap,
  7049. ast: this.astClone,
  7050. sourcemapChain: this.sourcemapChain,
  7051. resolvedIds: this.resolvedIds,
  7052. resolvedExternalIds: this.resolvedExternalIds
  7053. };
  7054. }
  7055. trace ( name ) {
  7056. // TODO this is slightly circular
  7057. if ( name in this.scope.variables ) {
  7058. return this.scope.variables[ name ];
  7059. }
  7060. if ( name in this.imports ) {
  7061. const importDeclaration = this.imports[ name ];
  7062. const otherModule = importDeclaration.module;
  7063. if ( importDeclaration.name === '*' && !otherModule.isExternal ) {
  7064. return otherModule.namespace();
  7065. }
  7066. const declaration = otherModule.traceExport( importDeclaration.name );
  7067. if ( !declaration ) {
  7068. this.error( {
  7069. code: 'MISSING_EXPORT',
  7070. message: `'${importDeclaration.name}' is not exported by ${relativeId( otherModule.id )}`,
  7071. url: `https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module`
  7072. }, importDeclaration.specifier.start );
  7073. }
  7074. return declaration;
  7075. }
  7076. return null;
  7077. }
  7078. traceExport ( name ) {
  7079. // export * from 'external'
  7080. if ( name[ 0 ] === '*' ) {
  7081. const module = this.bundle.moduleById.get( name.slice( 1 ) );
  7082. return module.traceExport( '*' );
  7083. }
  7084. // export { foo } from './other.js'
  7085. const reexportDeclaration = this.reexports[ name ];
  7086. if ( reexportDeclaration ) {
  7087. const declaration = reexportDeclaration.module.traceExport( reexportDeclaration.localName );
  7088. if ( !declaration ) {
  7089. this.error( {
  7090. code: 'MISSING_EXPORT',
  7091. message: `'${reexportDeclaration.localName}' is not exported by ${relativeId( reexportDeclaration.module.id )}`,
  7092. url: `https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module`
  7093. }, reexportDeclaration.start );
  7094. }
  7095. return declaration;
  7096. }
  7097. const exportDeclaration = this.exports[ name ];
  7098. if ( exportDeclaration ) {
  7099. const name = exportDeclaration.localName;
  7100. const declaration = this.trace( name );
  7101. return declaration || this.bundle.scope.findVariable( name );
  7102. }
  7103. if ( name === 'default' ) { return; }
  7104. for ( let i = 0; i < this.exportAllModules.length; i += 1 ) {
  7105. const module = this.exportAllModules[ i ];
  7106. const declaration = module.traceExport( name );
  7107. if ( declaration ) { return declaration; }
  7108. }
  7109. }
  7110. warn ( warning, pos ) {
  7111. if ( pos !== undefined ) {
  7112. warning.pos = pos;
  7113. var ref = locate( this.code, pos, { offsetLine: 1 } );
  7114. var line = ref.line;
  7115. var column = ref.column; // TODO trace sourcemaps
  7116. warning.loc = { file: this.id, line, column };
  7117. warning.frame = getCodeFrame( this.code, line, column );
  7118. }
  7119. warning.id = this.id;
  7120. this.bundle.warn( warning );
  7121. }
  7122. }
  7123. class ExternalVariable extends Variable {
  7124. constructor ( module, name ) {
  7125. super( name );
  7126. this.module = module;
  7127. this.safeName = null;
  7128. this.isExternal = true;
  7129. this.isNamespace = name === '*';
  7130. }
  7131. addReference ( reference ) {
  7132. if ( this.name === 'default' || this.name === '*' ) {
  7133. this.module.suggestName( reference.name );
  7134. }
  7135. }
  7136. getName ( es ) {
  7137. if ( this.name === '*' ) {
  7138. return this.module.name;
  7139. }
  7140. if ( this.name === 'default' ) {
  7141. return this.module.exportsNamespace || ( !es && this.module.exportsNames ) ?
  7142. `${this.module.name}__default` :
  7143. this.module.name;
  7144. }
  7145. return es ? this.safeName : `${this.module.name}.${this.name}`;
  7146. }
  7147. includeDeclaration () {
  7148. if ( this.included ) {
  7149. return false;
  7150. }
  7151. this.included = true;
  7152. this.module.used = true;
  7153. return true;
  7154. }
  7155. setSafeName ( name ) {
  7156. this.safeName = name;
  7157. }
  7158. }
  7159. class ExternalModule {
  7160. constructor ( id ) {
  7161. this.id = id;
  7162. const parts = id.split( /[\\/]/ );
  7163. this.name = makeLegal( parts.pop() );
  7164. this.nameSuggestions = blank();
  7165. this.mostCommonSuggestion = 0;
  7166. this.isExternal = true;
  7167. this.used = false;
  7168. this.declarations = blank();
  7169. this.exportsNames = false;
  7170. }
  7171. suggestName ( name ) {
  7172. if ( !this.nameSuggestions[ name ] ) { this.nameSuggestions[ name ] = 0; }
  7173. this.nameSuggestions[ name ] += 1;
  7174. if ( this.nameSuggestions[ name ] > this.mostCommonSuggestion ) {
  7175. this.mostCommonSuggestion = this.nameSuggestions[ name ];
  7176. this.name = name;
  7177. }
  7178. }
  7179. traceExport ( name ) {
  7180. if ( name !== 'default' && name !== '*' ) { this.exportsNames = true; }
  7181. if ( name === '*' ) { this.exportsNamespace = true; }
  7182. return this.declarations[ name ]
  7183. || (this.declarations[ name ] = new ExternalVariable( this, name ));
  7184. }
  7185. }
  7186. function getInteropBlock ( bundle, options ) {
  7187. return bundle.externalModules
  7188. .map( module => {
  7189. if ( !module.declarations.default || options.interop === false ) { return null; }
  7190. if ( module.exportsNamespace ) {
  7191. return `${bundle.varOrConst} ${module.name}__default = ${module.name}['default'];`;
  7192. }
  7193. if ( module.exportsNames ) {
  7194. return `${bundle.varOrConst} ${module.name}__default = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};`;
  7195. }
  7196. return `${module.name} = ${module.name} && ${module.name}.hasOwnProperty('default') ? ${module.name}['default'] : ${module.name};`;
  7197. })
  7198. .filter( Boolean )
  7199. .join( '\n' );
  7200. }
  7201. function getExportBlock ( bundle, exportMode, mechanism ) {
  7202. if ( mechanism === void 0 ) mechanism = 'return';
  7203. const entryModule = bundle.entryModule;
  7204. if ( exportMode === 'default' ) {
  7205. return `${mechanism} ${entryModule.traceExport( 'default' ).getName( false )};`;
  7206. }
  7207. const exports = entryModule.getExports().concat( entryModule.getReexports() )
  7208. .map( name => {
  7209. if ( name[0] === '*' ) {
  7210. // export all from external
  7211. const id = name.slice( 1 );
  7212. const module = bundle.moduleById.get( id );
  7213. return `Object.keys(${module.name}).forEach(function (key) { exports[key] = ${module.name}[key]; });`;
  7214. }
  7215. const prop = name === 'default' ? `['default']` : `.${name}`;
  7216. const declaration = entryModule.traceExport( name );
  7217. const lhs = `exports${prop}`;
  7218. const rhs = declaration ?
  7219. declaration.getName( false ) :
  7220. name; // exporting a global
  7221. // prevent `exports.count = exports.count`
  7222. if ( lhs === rhs ) { return null; }
  7223. return `${lhs} = ${rhs};`;
  7224. });
  7225. return exports
  7226. .filter( Boolean )
  7227. .join( '\n' );
  7228. }
  7229. var esModuleExport = `Object.defineProperty(exports, '__esModule', { value: true });`;
  7230. const builtins$1 = {
  7231. process: true,
  7232. events: true,
  7233. stream: true,
  7234. util: true,
  7235. path: true,
  7236. buffer: true,
  7237. querystring: true,
  7238. url: true,
  7239. string_decoder: true,
  7240. punycode: true,
  7241. http: true,
  7242. https: true,
  7243. os: true,
  7244. assert: true,
  7245. constants: true,
  7246. timers: true,
  7247. console: true,
  7248. vm: true,
  7249. zlib: true,
  7250. tty: true,
  7251. domain: true
  7252. };
  7253. // Creating a browser bundle that depends on Node.js built-in modules ('util'). You might need to include https://www.npmjs.com/package/rollup-plugin-node-builtins
  7254. function warnOnBuiltins ( bundle ) {
  7255. const externalBuiltins = bundle.externalModules
  7256. .filter( mod => mod.id in builtins$1 )
  7257. .map( mod => mod.id );
  7258. if ( !externalBuiltins.length ) { return; }
  7259. const detail = externalBuiltins.length === 1 ?
  7260. `module ('${externalBuiltins[0]}')` :
  7261. `modules (${externalBuiltins.slice( 0, -1 ).map( name => `'${name}'` ).join( ', ' )} and '${externalBuiltins.slice( -1 )}')`;
  7262. bundle.warn({
  7263. code: 'MISSING_NODE_BUILTINS',
  7264. modules: externalBuiltins,
  7265. message: `Creating a browser bundle that depends on Node.js built-in ${detail}. You might need to include https://www.npmjs.com/package/rollup-plugin-node-builtins`
  7266. });
  7267. }
  7268. function amd ( bundle, magicString, ref, options ) {
  7269. var exportMode = ref.exportMode;
  7270. var getPath = ref.getPath;
  7271. var indentString = ref.indentString;
  7272. var intro = ref.intro;
  7273. var outro = ref.outro;
  7274. warnOnBuiltins( bundle );
  7275. const deps = bundle.externalModules.map( m => `'${getPath(m.id)}'` );
  7276. const args = bundle.externalModules.map( m => m.name );
  7277. if ( exportMode === 'named' ) {
  7278. args.unshift( `exports` );
  7279. deps.unshift( `'exports'` );
  7280. }
  7281. const amdOptions = options.amd || {};
  7282. const params =
  7283. ( amdOptions.id ? `'${amdOptions.id}', ` : `` ) +
  7284. ( deps.length ? `[${deps.join( ', ' )}], ` : `` );
  7285. const useStrict = options.strict !== false ? ` 'use strict';` : ``;
  7286. const define = amdOptions.define || 'define';
  7287. const wrapperStart = `${define}(${params}function (${args.join( ', ' )}) {${useStrict}\n\n`;
  7288. // var foo__default = 'default' in foo ? foo['default'] : foo;
  7289. const interopBlock = getInteropBlock( bundle, options );
  7290. if ( interopBlock ) { magicString.prepend( interopBlock + '\n\n' ); }
  7291. if ( intro ) { magicString.prepend( intro ); }
  7292. const exportBlock = getExportBlock( bundle, exportMode );
  7293. if ( exportBlock ) { magicString.append( '\n\n' + exportBlock ); }
  7294. if ( exportMode === 'named' && options.legacy !== true ) { magicString.append( `\n\n${esModuleExport}` ); }
  7295. if ( outro ) { magicString.append( outro ); }
  7296. return magicString
  7297. .indent( indentString )
  7298. .append( '\n\n});' )
  7299. .prepend( wrapperStart );
  7300. }
  7301. function cjs ( bundle, magicString, ref, options ) {
  7302. var exportMode = ref.exportMode;
  7303. var getPath = ref.getPath;
  7304. var intro = ref.intro;
  7305. var outro = ref.outro;
  7306. intro = ( options.strict === false ? intro : `'use strict';\n\n${intro}` ) +
  7307. ( exportMode === 'named' && options.legacy !== true ? `${esModuleExport}\n\n` : '' );
  7308. let needsInterop = false;
  7309. const varOrConst = bundle.varOrConst;
  7310. const interop = options.interop !== false;
  7311. // TODO handle empty imports, once they're supported
  7312. const importBlock = bundle.externalModules
  7313. .map( module => {
  7314. if ( interop && module.declarations.default ) {
  7315. if ( module.exportsNamespace ) {
  7316. return `${varOrConst} ${module.name} = require('${getPath(module.id)}');` +
  7317. `\n${varOrConst} ${module.name}__default = ${module.name}['default'];`;
  7318. }
  7319. needsInterop = true;
  7320. if ( module.exportsNames ) {
  7321. return `${varOrConst} ${module.name} = require('${getPath(module.id)}');` +
  7322. `\n${varOrConst} ${module.name}__default = _interopDefault(${module.name});`;
  7323. }
  7324. return `${varOrConst} ${module.name} = _interopDefault(require('${getPath(module.id)}'));`;
  7325. } else {
  7326. const includedDeclarations = Object.keys( module.declarations )
  7327. .filter( name => module.declarations[ name ].included );
  7328. const needsVar = includedDeclarations.length || module.reexported;
  7329. return needsVar ?
  7330. `${varOrConst} ${module.name} = require('${getPath(module.id)}');` :
  7331. `require('${getPath(module.id)}');`;
  7332. }
  7333. })
  7334. .join( '\n' );
  7335. if ( needsInterop ) {
  7336. intro += `function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }\n\n`;
  7337. }
  7338. if ( importBlock ) {
  7339. intro += importBlock + '\n\n';
  7340. }
  7341. magicString.prepend( intro );
  7342. const exportBlock = getExportBlock( bundle, exportMode, 'module.exports =' );
  7343. if ( exportBlock ) { magicString.append( '\n\n' + exportBlock ); }
  7344. if ( outro ) { magicString.append( outro ); }
  7345. return magicString;
  7346. }
  7347. function notDefault ( name ) {
  7348. return name !== 'default';
  7349. }
  7350. function es ( bundle, magicString, ref ) {
  7351. var getPath = ref.getPath;
  7352. var intro = ref.intro;
  7353. var outro = ref.outro;
  7354. const importBlock = bundle.externalModules
  7355. .map( module => {
  7356. const specifiers = [];
  7357. const specifiersList = [specifiers];
  7358. const importedNames = keys( module.declarations )
  7359. .filter( name => name !== '*' && name !== 'default' )
  7360. .filter( name => module.declarations[ name ].included )
  7361. .map( name => {
  7362. if ( name[0] === '*' ) {
  7363. return `* as ${module.name}`;
  7364. }
  7365. const declaration = module.declarations[ name ];
  7366. if ( declaration.name === declaration.safeName ) { return declaration.name; }
  7367. return `${declaration.name} as ${declaration.safeName}`;
  7368. })
  7369. .filter( Boolean );
  7370. if ( module.declarations.default ) {
  7371. if ( module.exportsNamespace ) {
  7372. specifiersList.push([ `${module.name}__default` ]);
  7373. } else {
  7374. specifiers.push( module.name );
  7375. }
  7376. }
  7377. const namespaceSpecifier = module.declarations['*'] && module.declarations['*'].included ? `* as ${module.name}` : null; // TODO prevent unnecessary namespace import, e.g form/external-imports
  7378. const namedSpecifier = importedNames.length ? `{ ${importedNames.sort().join( ', ' )} }` : null;
  7379. if ( namespaceSpecifier && namedSpecifier ) {
  7380. // Namespace and named specifiers cannot be combined.
  7381. specifiersList.push( [namespaceSpecifier] );
  7382. specifiers.push( namedSpecifier );
  7383. } else if ( namedSpecifier ) {
  7384. specifiers.push( namedSpecifier );
  7385. } else if ( namespaceSpecifier ) {
  7386. specifiers.push( namespaceSpecifier );
  7387. }
  7388. return specifiersList
  7389. .map( specifiers => {
  7390. if ( specifiers.length ) {
  7391. return `import ${specifiers.join( ', ' )} from '${getPath(module.id)}';`;
  7392. }
  7393. return module.reexported ?
  7394. null :
  7395. `import '${getPath(module.id)}';`;
  7396. })
  7397. .filter( Boolean )
  7398. .join( '\n' );
  7399. })
  7400. .join( '\n' );
  7401. if ( importBlock ) { intro += importBlock + '\n\n'; }
  7402. if ( intro ) { magicString.prepend( intro ); }
  7403. const module = bundle.entryModule;
  7404. const exportInternalSpecifiers = [];
  7405. const exportExternalSpecifiers = new Map();
  7406. const exportAllDeclarations = [];
  7407. module.getExports()
  7408. .filter( notDefault )
  7409. .forEach( name => {
  7410. const declaration = module.traceExport( name );
  7411. const rendered = declaration.getName( true );
  7412. exportInternalSpecifiers.push( rendered === name ? name : `${rendered} as ${name}` );
  7413. });
  7414. module.getReexports()
  7415. .filter( notDefault )
  7416. .forEach( name => {
  7417. const declaration = module.traceExport( name );
  7418. if ( declaration.isExternal ) {
  7419. if ( name[0] === '*' ) {
  7420. // export * from 'external'
  7421. exportAllDeclarations.push( `export * from '${name.slice( 1 )}';` );
  7422. } else {
  7423. if ( !exportExternalSpecifiers.has( declaration.module.id ) ) { exportExternalSpecifiers.set( declaration.module.id, [] ); }
  7424. exportExternalSpecifiers.get( declaration.module.id ).push( name );
  7425. }
  7426. return;
  7427. }
  7428. const rendered = declaration.getName( true );
  7429. exportInternalSpecifiers.push( rendered === name ? name : `${rendered} as ${name}` );
  7430. });
  7431. const exportBlock = [];
  7432. if ( exportInternalSpecifiers.length ) { exportBlock.push( `export { ${exportInternalSpecifiers.join(', ')} };` ); }
  7433. if ( module.exports.default || module.reexports.default ) { exportBlock.push( `export default ${module.traceExport( 'default' ).getName( true )};` ); }
  7434. if ( exportAllDeclarations.length ) { exportBlock.push( exportAllDeclarations.join( '\n' ) ); }
  7435. if ( exportExternalSpecifiers.size ) {
  7436. exportExternalSpecifiers.forEach( ( specifiers, id ) => {
  7437. exportBlock.push( `export { ${specifiers.join( ', ' )} } from '${id}';` );
  7438. });
  7439. }
  7440. if ( exportBlock.length ) { magicString.append( '\n\n' + exportBlock.join( '\n' ).trim() ); }
  7441. if ( outro ) { magicString.append( outro ); }
  7442. return magicString.trim();
  7443. }
  7444. function getGlobalNameMaker ( globals, bundle, fallback ) {
  7445. if ( fallback === void 0 ) fallback = null;
  7446. const fn = typeof globals === 'function' ? globals : id => globals[ id ];
  7447. return function ( module ) {
  7448. const name = fn( module.id );
  7449. if ( name ) { return name; }
  7450. if ( Object.keys( module.declarations ).length > 0 ) {
  7451. bundle.warn({
  7452. code: 'MISSING_GLOBAL_NAME',
  7453. source: module.id,
  7454. guess: module.name,
  7455. message: `No name was provided for external module '${module.id}' in options.globals – guessing '${module.name}'`
  7456. });
  7457. return module.name;
  7458. }
  7459. return fallback;
  7460. };
  7461. }
  7462. // Generate strings which dereference dotted properties, but use array notation `['prop-deref']`
  7463. // if the property name isn't trivial
  7464. const shouldUseDot = /^[a-zA-Z$_][a-zA-Z0-9$_]*$/;
  7465. function property ( prop ) {
  7466. return shouldUseDot.test( prop ) ? `.${prop}` : `['${prop}']`;
  7467. }
  7468. function keypath ( keypath ) {
  7469. return keypath.split( '.' ).map( property ).join( '' );
  7470. }
  7471. function trimEmptyImports ( modules ) {
  7472. let i = modules.length;
  7473. while ( i-- ) {
  7474. const module = modules[i];
  7475. if ( Object.keys( module.declarations ).length > 0 ) {
  7476. return modules.slice( 0, i + 1 );
  7477. }
  7478. }
  7479. return [];
  7480. }
  7481. function setupNamespace ( keypath$$1 ) {
  7482. const parts = keypath$$1.split( '.' );
  7483. parts.pop();
  7484. let acc = 'this';
  7485. return parts
  7486. .map( part => ( acc += property( part ), `${acc} = ${acc} || {};` ) )
  7487. .join( '\n' ) + '\n';
  7488. }
  7489. const thisProp = name => `this${keypath( name )}`;
  7490. function iife ( bundle, magicString, ref, options ) {
  7491. var exportMode = ref.exportMode;
  7492. var indentString = ref.indentString;
  7493. var intro = ref.intro;
  7494. var outro = ref.outro;
  7495. const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle, 'null' );
  7496. var extend = options.extend;
  7497. var name = options.name;
  7498. const isNamespaced = name && name.indexOf( '.' ) !== -1;
  7499. const possibleVariableAssignment = !extend && !isNamespaced;
  7500. if ( name && possibleVariableAssignment && !isLegal(name) ) {
  7501. error({
  7502. code: 'ILLEGAL_IDENTIFIER_AS_NAME',
  7503. message: `Given name (${name}) is not legal JS identifier. If you need this you can try --extend option`
  7504. });
  7505. }
  7506. warnOnBuiltins( bundle );
  7507. const external = trimEmptyImports( bundle.externalModules );
  7508. const dependencies = external.map( globalNameMaker );
  7509. const args = external.map( m => m.name );
  7510. if ( exportMode !== 'none' && !name ) {
  7511. error({
  7512. code: 'INVALID_OPTION',
  7513. message: `You must supply options.name for IIFE bundles`
  7514. });
  7515. }
  7516. if ( extend ) {
  7517. dependencies.unshift( `(${thisProp(name)} = ${thisProp(name)} || {})` );
  7518. args.unshift( 'exports' );
  7519. } else if ( exportMode === 'named' ) {
  7520. dependencies.unshift( '{}' );
  7521. args.unshift( 'exports' );
  7522. }
  7523. const useStrict = options.strict !== false ? `${indentString}'use strict';\n\n` : ``;
  7524. let wrapperIntro = `(function (${args}) {\n${useStrict}`;
  7525. if ( exportMode !== 'none' && !extend) {
  7526. wrapperIntro = ( isNamespaced ? thisProp(name) : `${bundle.varOrConst} ${name}` ) + ` = ${wrapperIntro}`;
  7527. }
  7528. if ( isNamespaced ) {
  7529. wrapperIntro = setupNamespace( name ) + wrapperIntro;
  7530. }
  7531. let wrapperOutro = `\n\n}(${dependencies}));`;
  7532. if (!extend && exportMode === 'named') {
  7533. wrapperOutro = `\n\n${indentString}return exports;${wrapperOutro}`;
  7534. }
  7535. // var foo__default = 'default' in foo ? foo['default'] : foo;
  7536. const interopBlock = getInteropBlock( bundle, options );
  7537. if ( interopBlock ) { magicString.prepend( interopBlock + '\n\n' ); }
  7538. if ( intro ) { magicString.prepend( intro ); }
  7539. const exportBlock = getExportBlock( bundle, exportMode );
  7540. if ( exportBlock ) { magicString.append( '\n\n' + exportBlock ); }
  7541. if ( outro ) { magicString.append( outro ); }
  7542. return magicString
  7543. .indent( indentString )
  7544. .prepend( wrapperIntro )
  7545. .append( wrapperOutro );
  7546. }
  7547. function globalProp ( name ) {
  7548. if ( !name ) { return 'null'; }
  7549. return `global${ keypath( name ) }`;
  7550. }
  7551. function setupNamespace$1 ( name ) {
  7552. const parts = name.split( '.' );
  7553. const last = property( parts.pop() );
  7554. let acc = 'global';
  7555. return parts
  7556. .map( part => ( acc += property( part ), `${acc} = ${acc} || {}` ) )
  7557. .concat( `${acc}${last}` )
  7558. .join( ', ' );
  7559. }
  7560. function safeAccess ( name ) {
  7561. const parts = name.split( '.' );
  7562. let acc = 'global';
  7563. return parts
  7564. .map( part => ( acc += property( part ), acc ) )
  7565. .join( ` && ` );
  7566. }
  7567. const wrapperOutro = '\n\n})));';
  7568. function umd ( bundle, magicString, ref, options ) {
  7569. var exportMode = ref.exportMode;
  7570. var getPath = ref.getPath;
  7571. var indentString = ref.indentString;
  7572. var intro = ref.intro;
  7573. var outro = ref.outro;
  7574. if ( exportMode !== 'none' && !options.name ) {
  7575. error({
  7576. code: 'INVALID_OPTION',
  7577. message: 'You must supply options.name for UMD bundles'
  7578. });
  7579. }
  7580. warnOnBuiltins( bundle );
  7581. const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle );
  7582. const amdDeps = bundle.externalModules.map( m => `'${getPath(m.id)}'` );
  7583. const cjsDeps = bundle.externalModules.map( m => `require('${getPath(m.id)}')` );
  7584. const trimmed = trimEmptyImports( bundle.externalModules );
  7585. const globalDeps = trimmed.map( module => globalProp( globalNameMaker( module ) ) );
  7586. const args = trimmed.map( m => m.name );
  7587. if ( exportMode === 'named' ) {
  7588. amdDeps.unshift( `'exports'` );
  7589. cjsDeps.unshift( `exports` );
  7590. globalDeps.unshift( `(${setupNamespace$1(options.name)} = ${options.extend ? `${globalProp(options.name)} || ` : '' }{})` );
  7591. args.unshift( 'exports' );
  7592. }
  7593. const amdOptions = options.amd || {};
  7594. const amdParams =
  7595. ( amdOptions.id ? `'${amdOptions.id}', ` : `` ) +
  7596. ( amdDeps.length ? `[${amdDeps.join( ', ' )}], ` : `` );
  7597. const define = amdOptions.define || 'define';
  7598. const cjsExport = exportMode === 'default' ? `module.exports = ` : ``;
  7599. const defaultExport = exportMode === 'default' ? `${setupNamespace$1(options.name)} = ` : '';
  7600. const useStrict = options.strict !== false ? ` 'use strict';` : ``;
  7601. let globalExport;
  7602. if (options.noConflict === true) {
  7603. let factory;
  7604. if ( exportMode === 'default' ) {
  7605. factory = `var exports = factory(${globalDeps});`;
  7606. } else if ( exportMode === 'named' ) {
  7607. const module = globalDeps.shift();
  7608. factory = `var exports = ${module};
  7609. factory(${['exports'].concat(globalDeps)});`;
  7610. }
  7611. globalExport = `(function() {
  7612. var current = ${safeAccess(options.name)};
  7613. ${factory}
  7614. ${globalProp(options.name)} = exports;
  7615. exports.noConflict = function() { ${globalProp(options.name)} = current; return exports; };
  7616. })()`;
  7617. } else {
  7618. globalExport = `(${defaultExport}factory(${globalDeps}))`;
  7619. }
  7620. const wrapperIntro =
  7621. `(function (global, factory) {
  7622. typeof exports === 'object' && typeof module !== 'undefined' ? ${cjsExport}factory(${cjsDeps.join( ', ' )}) :
  7623. typeof ${define} === 'function' && ${define}.amd ? ${define}(${amdParams}factory) :
  7624. ${globalExport};
  7625. }(this, (function (${args}) {${useStrict}
  7626. `.replace( /^\t\t/gm, '' ).replace( /^\t/gm, indentString || '\t' );
  7627. // var foo__default = 'default' in foo ? foo['default'] : foo;
  7628. const interopBlock = getInteropBlock( bundle, options );
  7629. if ( interopBlock ) { magicString.prepend( interopBlock + '\n\n' ); }
  7630. if ( intro ) { magicString.prepend( intro ); }
  7631. const exportBlock = getExportBlock( bundle, exportMode );
  7632. if ( exportBlock ) { magicString.append( '\n\n' + exportBlock ); }
  7633. if ( exportMode === 'named' && options.legacy !== true ) { magicString.append( `\n\n${esModuleExport}` ); }
  7634. if ( outro ) { magicString.append( outro ); }
  7635. return magicString
  7636. .trim()
  7637. .indent( indentString )
  7638. .append( wrapperOutro )
  7639. .prepend( wrapperIntro );
  7640. }
  7641. var finalisers = { amd, cjs, es, iife, umd };
  7642. function ensureArray ( thing ) {
  7643. if ( Array.isArray( thing ) ) { return thing; }
  7644. if ( thing == undefined ) { return []; }
  7645. return [ thing ];
  7646. }
  7647. function load ( id ) {
  7648. return readFileSync( id, 'utf-8' );
  7649. }
  7650. function findFile ( file ) {
  7651. try {
  7652. const stats = lstatSync( file );
  7653. if ( stats.isSymbolicLink() ) { return findFile( realpathSync( file ) ); }
  7654. if ( stats.isFile() ) {
  7655. // check case
  7656. const name = basename( file );
  7657. const files = readdirSync( dirname( file ) );
  7658. if ( ~files.indexOf( name ) ) { return file; }
  7659. }
  7660. } catch ( err ) {
  7661. // suppress
  7662. }
  7663. }
  7664. function addJsExtensionIfNecessary ( file ) {
  7665. return findFile( file ) || findFile( file + '.js' );
  7666. }
  7667. function resolveId ( importee, importer ) {
  7668. if ( typeof process === 'undefined' ) {
  7669. error({
  7670. code: 'MISSING_PROCESS',
  7671. message: `It looks like you're using Rollup in a non-Node.js environment. This means you must supply a plugin with custom resolveId and load functions`,
  7672. url: 'https://github.com/rollup/rollup/wiki/Plugins'
  7673. });
  7674. }
  7675. // external modules (non-entry modules that start with neither '.' or '/')
  7676. // are skipped at this stage.
  7677. if ( importer !== undefined && !isAbsolute( importee ) && importee[0] !== '.' ) { return null; }
  7678. // `resolve` processes paths from right to left, prepending them until an
  7679. // absolute path is created. Absolute importees therefore shortcircuit the
  7680. // resolve call and require no special handing on our part.
  7681. // See https://nodejs.org/api/path.html#path_path_resolve_paths
  7682. return addJsExtensionIfNecessary(
  7683. resolve( importer ? dirname( importer ) : resolve(), importee ) );
  7684. }
  7685. function makeOnwarn () {
  7686. const warned = blank();
  7687. return warning => {
  7688. const str = warning.toString();
  7689. if ( str in warned ) { return; }
  7690. console.error( str ); //eslint-disable-line no-console
  7691. warned[ str ] = true;
  7692. };
  7693. }
  7694. function badExports ( option, keys$$1 ) {
  7695. error({
  7696. code: 'INVALID_EXPORT_OPTION',
  7697. message: `'${option}' was specified for options.exports, but entry module has following exports: ${keys$$1.join(', ')}`
  7698. });
  7699. }
  7700. function getExportMode ( bundle, ref ) {
  7701. var exportMode = ref.exports;
  7702. var name = ref.name;
  7703. var format = ref.format;
  7704. const exportKeys = keys( bundle.entryModule.exports )
  7705. .concat( keys( bundle.entryModule.reexports ) )
  7706. .concat( bundle.entryModule.exportAllSources ); // not keys, but makes our job easier this way
  7707. if ( exportMode === 'default' ) {
  7708. if ( exportKeys.length !== 1 || exportKeys[0] !== 'default' ) {
  7709. badExports( 'default', exportKeys );
  7710. }
  7711. } else if ( exportMode === 'none' && exportKeys.length ) {
  7712. badExports( 'none', exportKeys );
  7713. }
  7714. if ( !exportMode || exportMode === 'auto' ) {
  7715. if ( exportKeys.length === 0 ) {
  7716. exportMode = 'none';
  7717. } else if ( exportKeys.length === 1 && exportKeys[0] === 'default' ) {
  7718. exportMode = 'default';
  7719. } else {
  7720. if ( bundle.entryModule.exports.default && format !== 'es') {
  7721. bundle.warn({
  7722. code: 'MIXED_EXPORTS',
  7723. message: `Using named and default exports together. Consumers of your bundle will have to use ${name || 'bundle'}['default'] to access the default export, which may not be what you want. Use \`exports: 'named'\` to disable this warning`,
  7724. url: `https://github.com/rollup/rollup/wiki/JavaScript-API#exports`
  7725. });
  7726. }
  7727. exportMode = 'named';
  7728. }
  7729. }
  7730. if ( !/(?:default|named|none)/.test( exportMode ) ) {
  7731. error({
  7732. code: 'INVALID_EXPORT_OPTION',
  7733. message: `options.exports must be 'default', 'named', 'none', 'auto', or left unspecified (defaults to 'auto')`
  7734. });
  7735. }
  7736. return exportMode;
  7737. }
  7738. function getIndentString ( magicString, options ) {
  7739. if ( options.indent === true ) {
  7740. return magicString.getIndentString();
  7741. }
  7742. return options.indent || '';
  7743. }
  7744. function transform ( bundle, source, id, plugins ) {
  7745. const sourcemapChain = [];
  7746. const originalSourcemap = typeof source.map === 'string' ? JSON.parse( source.map ) : source.map;
  7747. if ( originalSourcemap && typeof originalSourcemap.mappings === 'string' ) {
  7748. originalSourcemap.mappings = decode$$1( originalSourcemap.mappings );
  7749. }
  7750. const originalCode = source.code;
  7751. let ast = source.ast;
  7752. let promise = Promise.resolve( source.code );
  7753. plugins.forEach( plugin => {
  7754. if ( !plugin.transform ) { return; }
  7755. promise = promise.then( previous => {
  7756. function augment ( object, pos, code ) {
  7757. if ( typeof object === 'string' ) {
  7758. object = { message: object };
  7759. }
  7760. if ( object.code ) { object.pluginCode = object.code; }
  7761. object.code = code;
  7762. if ( pos !== undefined ) {
  7763. if ( pos.line !== undefined && pos.column !== undefined ) {
  7764. var line = pos.line;
  7765. var column = pos.column;
  7766. object.loc = { file: id, line, column };
  7767. object.frame = getCodeFrame( previous, line, column );
  7768. }
  7769. else {
  7770. object.pos = pos;
  7771. var ref = locate( previous, pos, { offsetLine: 1 });
  7772. var line = ref.line;
  7773. var column = ref.column;
  7774. object.loc = { file: id, line, column };
  7775. object.frame = getCodeFrame( previous, line, column );
  7776. }
  7777. }
  7778. object.plugin = plugin.name;
  7779. object.id = id;
  7780. return object;
  7781. }
  7782. let throwing;
  7783. const context = {
  7784. warn: ( warning, pos ) => {
  7785. warning = augment( warning, pos, 'PLUGIN_WARNING' );
  7786. bundle.warn( warning );
  7787. },
  7788. error ( err, pos ) {
  7789. err = augment( err, pos, 'PLUGIN_ERROR' );
  7790. throwing = true;
  7791. error( err );
  7792. }
  7793. };
  7794. let transformed;
  7795. try {
  7796. transformed = plugin.transform.call( context, previous, id );
  7797. } catch ( err ) {
  7798. if ( !throwing ) { context.error( err ); }
  7799. error( err );
  7800. }
  7801. return Promise.resolve( transformed )
  7802. .then( result => {
  7803. if ( result == null ) { return previous; }
  7804. if ( typeof result === 'string' ) {
  7805. result = {
  7806. code: result,
  7807. ast: null,
  7808. map: null
  7809. };
  7810. }
  7811. // `result.map` can only be a string if `result` isn't
  7812. else if ( typeof result.map === 'string' ) {
  7813. result.map = JSON.parse( result.map );
  7814. }
  7815. if ( result.map && typeof result.map.mappings === 'string' ) {
  7816. result.map.mappings = decode$$1( result.map.mappings );
  7817. }
  7818. sourcemapChain.push( result.map || { missing: true, plugin: plugin.name }); // lil' bit hacky but it works
  7819. ast = result.ast;
  7820. return result.code;
  7821. })
  7822. .catch( err => {
  7823. err = augment( err, undefined, 'PLUGIN_ERROR' );
  7824. error( err );
  7825. });
  7826. });
  7827. });
  7828. return promise.then( code => ({ code, originalCode, originalSourcemap, ast, sourcemapChain }) );
  7829. }
  7830. function transformBundle ( code, plugins, sourcemapChain, options ) {
  7831. return plugins.reduce( ( promise, plugin ) => {
  7832. if ( !plugin.transformBundle ) { return promise; }
  7833. return promise.then( code => {
  7834. return Promise.resolve().then( () => {
  7835. return plugin.transformBundle( code, { format : options.format } );
  7836. }).then( result => {
  7837. if ( result == null ) { return code; }
  7838. if ( typeof result === 'string' ) {
  7839. result = {
  7840. code: result,
  7841. map: null
  7842. };
  7843. }
  7844. const map = typeof result.map === 'string' ? JSON.parse( result.map ) : result.map;
  7845. if ( map && typeof map.mappings === 'string' ) {
  7846. map.mappings = decode$$1( map.mappings );
  7847. }
  7848. sourcemapChain.push( map );
  7849. return result.code;
  7850. }).catch( err => {
  7851. error({
  7852. code: 'BAD_BUNDLE_TRANSFORMER',
  7853. message: `Error transforming bundle${plugin.name ? ` with '${plugin.name}' plugin` : ''}: ${err.message}`,
  7854. plugin: plugin.name
  7855. });
  7856. });
  7857. });
  7858. }, Promise.resolve( code ) );
  7859. }
  7860. class Source {
  7861. constructor ( filename, content ) {
  7862. this.isOriginal = true;
  7863. this.filename = filename;
  7864. this.content = content;
  7865. }
  7866. traceSegment ( line, column, name ) {
  7867. return { line, column, name, source: this };
  7868. }
  7869. }
  7870. class Link {
  7871. constructor ( map, sources ) {
  7872. this.sources = sources;
  7873. this.names = map.names;
  7874. this.mappings = map.mappings;
  7875. }
  7876. traceMappings () {
  7877. const sources = [];
  7878. const sourcesContent = [];
  7879. const names = [];
  7880. const mappings = this.mappings.map( line => {
  7881. const tracedLine = [];
  7882. line.forEach( segment => {
  7883. const source = this.sources[ segment[1] ];
  7884. if ( !source ) { return; }
  7885. const traced = source.traceSegment( segment[2], segment[3], this.names[ segment[4] ] );
  7886. if ( traced ) {
  7887. let sourceIndex = null;
  7888. let nameIndex = null;
  7889. segment = [
  7890. segment[0],
  7891. null,
  7892. traced.line,
  7893. traced.column
  7894. ];
  7895. // newer sources are more likely to be used, so search backwards.
  7896. sourceIndex = sources.lastIndexOf( traced.source.filename );
  7897. if ( sourceIndex === -1 ) {
  7898. sourceIndex = sources.length;
  7899. sources.push( traced.source.filename );
  7900. sourcesContent[ sourceIndex ] = traced.source.content;
  7901. } else if ( sourcesContent[ sourceIndex ] == null ) {
  7902. sourcesContent[ sourceIndex ] = traced.source.content;
  7903. } else if ( traced.source.content != null && sourcesContent[ sourceIndex ] !== traced.source.content ) {
  7904. error({
  7905. message: `Multiple conflicting contents for sourcemap source ${source.filename}`
  7906. });
  7907. }
  7908. segment[1] = sourceIndex;
  7909. if ( traced.name ) {
  7910. nameIndex = names.indexOf( traced.name );
  7911. if ( nameIndex === -1 ) {
  7912. nameIndex = names.length;
  7913. names.push( traced.name );
  7914. }
  7915. segment[4] = nameIndex;
  7916. }
  7917. tracedLine.push( segment );
  7918. }
  7919. });
  7920. return tracedLine;
  7921. });
  7922. return { sources, sourcesContent, names, mappings };
  7923. }
  7924. traceSegment ( line, column, name ) {
  7925. const segments = this.mappings[ line ];
  7926. if ( !segments ) { return null; }
  7927. for ( let i = 0; i < segments.length; i += 1 ) {
  7928. const segment = segments[i];
  7929. if ( segment[0] > column ) { return null; }
  7930. if ( segment[0] === column ) {
  7931. const source = this.sources[ segment[1] ];
  7932. if ( !source ) { return null; }
  7933. return source.traceSegment( segment[2], segment[3], this.names[ segment[4] ] || name );
  7934. }
  7935. }
  7936. return null;
  7937. }
  7938. }
  7939. function collapseSourcemaps ( bundle, file, map, modules, bundleSourcemapChain ) {
  7940. const moduleSources = modules.filter( module => !module.excludeFromSourcemap ).map( module => {
  7941. let sourcemapChain = module.sourcemapChain;
  7942. let source;
  7943. if ( module.originalSourcemap == null ) {
  7944. source = new Source( module.id, module.originalCode );
  7945. } else {
  7946. const sources = module.originalSourcemap.sources;
  7947. const sourcesContent = module.originalSourcemap.sourcesContent || [];
  7948. if ( sources == null || ( sources.length <= 1 && sources[0] == null ) ) {
  7949. source = new Source( module.id, sourcesContent[0] );
  7950. sourcemapChain = [ module.originalSourcemap ].concat( sourcemapChain );
  7951. } else {
  7952. // TODO indiscriminately treating IDs and sources as normal paths is probably bad.
  7953. const directory = dirname( module.id ) || '.';
  7954. const sourceRoot = module.originalSourcemap.sourceRoot || '.';
  7955. const baseSources = sources.map( (source, i) => {
  7956. return new Source( resolve( directory, sourceRoot, source ), sourcesContent[i] );
  7957. });
  7958. source = new Link( module.originalSourcemap, baseSources );
  7959. }
  7960. }
  7961. sourcemapChain.forEach( map => {
  7962. if ( map.missing ) {
  7963. bundle.warn({
  7964. code: 'SOURCEMAP_BROKEN',
  7965. plugin: map.plugin,
  7966. message: `Sourcemap is likely to be incorrect: a plugin${map.plugin ? ` ('${map.plugin}')` : ``} was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help`,
  7967. url: `https://github.com/rollup/rollup/wiki/Troubleshooting#sourcemap-is-likely-to-be-incorrect`
  7968. });
  7969. map = {
  7970. names: [],
  7971. mappings: ''
  7972. };
  7973. }
  7974. source = new Link( map, [ source ]);
  7975. });
  7976. return source;
  7977. });
  7978. let source = new Link( map, moduleSources );
  7979. bundleSourcemapChain.forEach( map => {
  7980. source = new Link( map, [ source ] );
  7981. });
  7982. var ref = source.traceMappings();
  7983. var sources = ref.sources;
  7984. var sourcesContent = ref.sourcesContent;
  7985. var names = ref.names;
  7986. var mappings = ref.mappings;
  7987. if ( file ) {
  7988. const directory = dirname( file );
  7989. sources = sources.map( source => relative( directory, source ) );
  7990. map.file = basename( file );
  7991. }
  7992. // we re-use the `map` object because it has convenient toString/toURL methods
  7993. map.sources = sources;
  7994. map.sourcesContent = sourcesContent;
  7995. map.names = names;
  7996. map.mappings = encode$$1( mappings );
  7997. return map;
  7998. }
  7999. function callIfFunction ( thing ) {
  8000. return typeof thing === 'function' ? thing() : thing;
  8001. }
  8002. class GlobalVariable extends Variable {
  8003. constructor ( name ) {
  8004. super( name );
  8005. this.isExternal = true;
  8006. this.isGlobal = true;
  8007. this.isReassigned = false;
  8008. this.included = true;
  8009. }
  8010. addReference ( reference ) {
  8011. if ( reference.isReassignment ) { this.isReassigned = true; }
  8012. }
  8013. assignExpression () {}
  8014. hasEffectsWhenCalled () {
  8015. return !pureFunctions[ this.name ];
  8016. }
  8017. }
  8018. class BundleScope extends Scope {
  8019. findVariable ( name ) {
  8020. if ( !this.variables[ name ] ) {
  8021. this.variables[ name ] = new GlobalVariable( name );
  8022. }
  8023. return this.variables[ name ];
  8024. }
  8025. }
  8026. class Bundle$$1 {
  8027. constructor ( options ) {
  8028. this.cachedModules = new Map();
  8029. if ( options.cache ) {
  8030. options.cache.modules.forEach( module => {
  8031. this.cachedModules.set( module.id, module );
  8032. } );
  8033. }
  8034. this.plugins = ensureArray( options.plugins );
  8035. options = this.plugins.reduce( ( acc, plugin ) => {
  8036. if ( plugin.options ) { return plugin.options( acc ) || acc; }
  8037. return acc;
  8038. }, options );
  8039. if ( !options.input ) {
  8040. throw new Error( 'You must supply options.input to rollup' );
  8041. }
  8042. this.entry = options.input;
  8043. this.entryId = null;
  8044. this.entryModule = null;
  8045. this.treeshake = options.treeshake !== false;
  8046. if ( options.pureExternalModules === true ) {
  8047. this.isPureExternalModule = () => true;
  8048. } else if ( typeof options.pureExternalModules === 'function' ) {
  8049. this.isPureExternalModule = options.pureExternalModules;
  8050. } else if ( Array.isArray( options.pureExternalModules ) ) {
  8051. const pureExternalModules = new Set( options.pureExternalModules );
  8052. this.isPureExternalModule = id => pureExternalModules.has( id );
  8053. } else {
  8054. this.isPureExternalModule = () => false;
  8055. }
  8056. this.resolveId = first(
  8057. [ id => this.isExternal( id ) ? false : null ]
  8058. .concat( this.plugins.map( plugin => plugin.resolveId ).filter( Boolean ) )
  8059. .concat( resolveId )
  8060. );
  8061. const loaders = this.plugins
  8062. .map( plugin => plugin.load )
  8063. .filter( Boolean );
  8064. this.hasLoaders = loaders.length !== 0;
  8065. this.load = first( loaders.concat( load ) );
  8066. this.scope = new BundleScope();
  8067. // TODO strictly speaking, this only applies with non-ES6, non-default-only bundles
  8068. [ 'module', 'exports', '_interopDefault' ].forEach( name => {
  8069. this.scope.findVariable( name ); // creates global variable as side-effect
  8070. } );
  8071. this.moduleById = new Map();
  8072. this.modules = [];
  8073. this.externalModules = [];
  8074. this.context = String( options.context );
  8075. const optionsModuleContext = options.moduleContext;
  8076. if ( typeof optionsModuleContext === 'function' ) {
  8077. this.getModuleContext = id => optionsModuleContext( id ) || this.context;
  8078. } else if ( typeof optionsModuleContext === 'object' ) {
  8079. const moduleContext = new Map();
  8080. Object.keys( optionsModuleContext ).forEach( key => {
  8081. moduleContext.set( resolve( key ), optionsModuleContext[ key ] );
  8082. } );
  8083. this.getModuleContext = id => moduleContext.get( id ) || this.context;
  8084. } else {
  8085. this.getModuleContext = () => this.context;
  8086. }
  8087. if ( typeof options.external === 'function' ) {
  8088. this.isExternal = options.external;
  8089. } else {
  8090. const ids = ensureArray( options.external );
  8091. this.isExternal = id => ids.indexOf( id ) !== -1;
  8092. }
  8093. this.onwarn = options.onwarn || makeOnwarn();
  8094. this.varOrConst = options.preferConst ? 'const' : 'var';
  8095. this.legacy = options.legacy;
  8096. this.acornOptions = options.acorn || {};
  8097. }
  8098. build () {
  8099. // Phase 1 – discovery. We load the entry module and find which
  8100. // modules it imports, and import those, until we have all
  8101. // of the entry module's dependencies
  8102. return this.resolveId( this.entry, undefined )
  8103. .then( id => {
  8104. if ( id === false ) {
  8105. error( {
  8106. code: 'UNRESOLVED_ENTRY',
  8107. message: `Entry module cannot be external`
  8108. } );
  8109. }
  8110. if ( id == null ) {
  8111. error( {
  8112. code: 'UNRESOLVED_ENTRY',
  8113. message: `Could not resolve entry (${this.entry})`
  8114. } );
  8115. }
  8116. this.entryId = id;
  8117. return this.fetchModule( id, undefined );
  8118. } )
  8119. .then( entryModule => {
  8120. this.entryModule = entryModule;
  8121. // Phase 2 – binding. We link references to their variables
  8122. // to generate a complete picture of the bundle
  8123. timeStart( 'phase 2' );
  8124. this.modules.forEach( module => module.bindImportSpecifiers() );
  8125. this.modules.forEach( module => module.bindReferences() );
  8126. timeEnd( 'phase 2' );
  8127. // Phase 3 – marking. We include all statements that should be included
  8128. timeStart( 'phase 3' );
  8129. // mark all export statements
  8130. entryModule.getExports().forEach( name => {
  8131. const variable = entryModule.traceExport( name );
  8132. variable.exportName = name;
  8133. variable.includeVariable();
  8134. if ( variable.isNamespace ) {
  8135. variable.needsNamespaceBlock = true;
  8136. }
  8137. } );
  8138. entryModule.getReexports().forEach( name => {
  8139. const variable = entryModule.traceExport( name );
  8140. if ( variable.isExternal ) {
  8141. variable.reexported = variable.module.reexported = true;
  8142. } else {
  8143. variable.exportName = name;
  8144. variable.includeVariable();
  8145. }
  8146. } );
  8147. // mark statements that should appear in the bundle
  8148. if ( this.treeshake ) {
  8149. let addedNewNodes;
  8150. do {
  8151. addedNewNodes = false;
  8152. this.modules.forEach( module => {
  8153. if ( module.includeInBundle() ) {
  8154. addedNewNodes = true;
  8155. }
  8156. } );
  8157. } while ( addedNewNodes );
  8158. } else {
  8159. // Necessary to properly replace namespace imports
  8160. this.modules.forEach( module => module.includeAllInBundle() );
  8161. }
  8162. timeEnd( 'phase 3' );
  8163. // Phase 4 – final preparation. We order the modules with an
  8164. // enhanced topological sort that accounts for cycles, then
  8165. // ensure that names are deconflicted throughout the bundle
  8166. timeStart( 'phase 4' );
  8167. // while we're here, check for unused external imports
  8168. this.externalModules.forEach( module => {
  8169. const unused = Object.keys( module.declarations )
  8170. .filter( name => name !== '*' )
  8171. .filter( name => !module.declarations[ name ].included && !module.declarations[ name ].reexported );
  8172. if ( unused.length === 0 ) { return; }
  8173. const names = unused.length === 1 ?
  8174. `'${unused[ 0 ]}' is` :
  8175. `${unused.slice( 0, -1 ).map( name => `'${name}'` ).join( ', ' )} and '${unused.slice( -1 )}' are`;
  8176. this.warn( {
  8177. code: 'UNUSED_EXTERNAL_IMPORT',
  8178. source: module.id,
  8179. names: unused,
  8180. message: `${names} imported from external module '${module.id}' but never used`
  8181. } );
  8182. } );
  8183. // prune unused external imports
  8184. this.externalModules = this.externalModules.filter( module => {
  8185. return module.used || !this.isPureExternalModule( module.id );
  8186. } );
  8187. this.orderedModules = this.sort();
  8188. this.deconflict();
  8189. timeEnd( 'phase 4' );
  8190. } );
  8191. }
  8192. deconflict () {
  8193. const used = blank();
  8194. // ensure no conflicts with globals
  8195. keys( this.scope.variables ).forEach( name => used[ name ] = 1 );
  8196. function getSafeName ( name ) {
  8197. while ( used[ name ] ) {
  8198. name += `$${used[ name ]++}`;
  8199. }
  8200. used[ name ] = 1;
  8201. return name;
  8202. }
  8203. const toDeshadow = new Set();
  8204. this.externalModules.forEach( module => {
  8205. const safeName = getSafeName( module.name );
  8206. toDeshadow.add( safeName );
  8207. module.name = safeName;
  8208. // ensure we don't shadow named external imports, if
  8209. // we're creating an ES6 bundle
  8210. forOwn( module.declarations, ( declaration, name ) => {
  8211. const safeName = getSafeName( name );
  8212. toDeshadow.add( safeName );
  8213. declaration.setSafeName( safeName );
  8214. } );
  8215. } );
  8216. this.modules.forEach( module => {
  8217. forOwn( module.scope.variables, variable => {
  8218. if ( variable.isDefault && variable.declaration.id ) {
  8219. return;
  8220. }
  8221. variable.name = getSafeName( variable.name );
  8222. } );
  8223. // deconflict reified namespaces
  8224. const namespace = module.namespace();
  8225. if ( namespace.needsNamespaceBlock ) {
  8226. namespace.name = getSafeName( namespace.name );
  8227. }
  8228. } );
  8229. this.scope.deshadow( toDeshadow );
  8230. }
  8231. fetchModule ( id, importer ) {
  8232. // short-circuit cycles
  8233. if ( this.moduleById.has( id ) ) { return null; }
  8234. this.moduleById.set( id, null );
  8235. return this.load( id )
  8236. .catch( err => {
  8237. let msg = `Could not load ${id}`;
  8238. if ( importer ) { msg += ` (imported by ${importer})`; }
  8239. msg += `: ${err.message}`;
  8240. throw new Error( msg );
  8241. } )
  8242. .then( source => {
  8243. if ( typeof source === 'string' ) { return source; }
  8244. if ( source && typeof source === 'object' && source.code ) { return source; }
  8245. // TODO report which plugin failed
  8246. error( {
  8247. code: 'BAD_LOADER',
  8248. message: `Error loading ${relativeId( id )}: plugin load hook should return a string, a { code, map } object, or nothing/null`
  8249. } );
  8250. } )
  8251. .then( source => {
  8252. if ( typeof source === 'string' ) {
  8253. source = {
  8254. code: source,
  8255. ast: null
  8256. };
  8257. }
  8258. if ( this.cachedModules.has( id ) && this.cachedModules.get( id ).originalCode === source.code ) {
  8259. return this.cachedModules.get( id );
  8260. }
  8261. return transform( this, source, id, this.plugins );
  8262. } )
  8263. .then( source => {
  8264. var code = source.code;
  8265. var originalCode = source.originalCode;
  8266. var originalSourcemap = source.originalSourcemap;
  8267. var ast = source.ast;
  8268. var sourcemapChain = source.sourcemapChain;
  8269. var resolvedIds = source.resolvedIds;
  8270. const module = new Module( {
  8271. id,
  8272. code,
  8273. originalCode,
  8274. originalSourcemap,
  8275. ast,
  8276. sourcemapChain,
  8277. resolvedIds,
  8278. bundle: this
  8279. } );
  8280. this.modules.push( module );
  8281. this.moduleById.set( id, module );
  8282. return this.fetchAllDependencies( module ).then( () => {
  8283. keys( module.exports ).forEach( name => {
  8284. if ( name !== 'default' ) {
  8285. module.exportsAll[ name ] = module.id;
  8286. }
  8287. } );
  8288. module.exportAllSources.forEach( source => {
  8289. const id = module.resolvedIds[ source ] || module.resolvedExternalIds[ source ];
  8290. const exportAllModule = this.moduleById.get( id );
  8291. if ( exportAllModule.isExternal ) { return; }
  8292. keys( exportAllModule.exportsAll ).forEach( name => {
  8293. if ( name in module.exportsAll ) {
  8294. this.warn( {
  8295. code: 'NAMESPACE_CONFLICT',
  8296. reexporter: module.id,
  8297. name,
  8298. sources: [ module.exportsAll[ name ], exportAllModule.exportsAll[ name ] ],
  8299. message: `Conflicting namespaces: ${relativeId( module.id )} re-exports '${name}' from both ${relativeId(
  8300. module.exportsAll[ name ] )} and ${relativeId( exportAllModule.exportsAll[ name ] )} (will be ignored)`
  8301. } );
  8302. } else {
  8303. module.exportsAll[ name ] = exportAllModule.exportsAll[ name ];
  8304. }
  8305. } );
  8306. } );
  8307. return module;
  8308. } );
  8309. } );
  8310. }
  8311. fetchAllDependencies ( module ) {
  8312. return mapSequence( module.sources, source => {
  8313. const resolvedId = module.resolvedIds[ source ];
  8314. return ( resolvedId ? Promise.resolve( resolvedId ) : this.resolveId( source, module.id ) )
  8315. .then( resolvedId => {
  8316. const externalId = resolvedId || (isRelative( source ) ? resolve( module.id, '..', source ) : source);
  8317. let isExternal = this.isExternal( externalId );
  8318. if ( !resolvedId && !isExternal ) {
  8319. if ( isRelative( source ) ) {
  8320. error( {
  8321. code: 'UNRESOLVED_IMPORT',
  8322. message: `Could not resolve '${source}' from ${relativeId( module.id )}`
  8323. } );
  8324. }
  8325. this.warn( {
  8326. code: 'UNRESOLVED_IMPORT',
  8327. source,
  8328. importer: relativeId( module.id ),
  8329. message: `'${source}' is imported by ${relativeId(
  8330. module.id )}, but could not be resolved – treating it as an external dependency`,
  8331. url: 'https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency'
  8332. } );
  8333. isExternal = true;
  8334. }
  8335. if ( isExternal ) {
  8336. module.resolvedExternalIds[ source ] = externalId;
  8337. if ( !this.moduleById.has( externalId ) ) {
  8338. const module = new ExternalModule( externalId );
  8339. this.externalModules.push( module );
  8340. this.moduleById.set( externalId, module );
  8341. }
  8342. const externalModule = this.moduleById.get( externalId );
  8343. // add external declarations so we can detect which are never used
  8344. Object.keys( module.imports ).forEach( name => {
  8345. const importDeclaration = module.imports[ name ];
  8346. if ( importDeclaration.source !== source ) { return; }
  8347. externalModule.traceExport( importDeclaration.name );
  8348. } );
  8349. } else {
  8350. if ( resolvedId === module.id ) {
  8351. // need to find the actual import declaration, so we can provide
  8352. // a useful error message. Bit hoop-jumpy but what can you do
  8353. const declaration = module.ast.body.find( node => {
  8354. return ( node.isImportDeclaration || node.isExportDeclaration ) && node.source.value === source;
  8355. } );
  8356. const declarationType = /Export/.test( declaration.type ) ? 'export' : 'import';
  8357. module.error( {
  8358. code: 'CANNOT_IMPORT_SELF',
  8359. message: `A module cannot ${declarationType} itself`
  8360. }, declaration.start );
  8361. }
  8362. module.resolvedIds[ source ] = resolvedId;
  8363. return this.fetchModule( resolvedId, module.id );
  8364. }
  8365. } );
  8366. } );
  8367. }
  8368. getPathRelativeToEntryDirname ( resolvedId ) {
  8369. if ( isRelative( resolvedId ) || isAbsolute( resolvedId ) ) {
  8370. const entryDirname = dirname( this.entryId );
  8371. const relativeToEntry = normalize( relative( entryDirname, resolvedId ) );
  8372. return isRelative( relativeToEntry ) ? relativeToEntry : `./${relativeToEntry}`;
  8373. }
  8374. return resolvedId;
  8375. }
  8376. render ( options ) {
  8377. if ( options === void 0 ) options = {};
  8378. return Promise.resolve().then( () => {
  8379. // Determine export mode - 'default', 'named', 'none'
  8380. const exportMode = getExportMode( this, options );
  8381. let magicString = new Bundle$1( { separator: '\n\n' } );
  8382. const usedModules = [];
  8383. timeStart( 'render modules' );
  8384. this.orderedModules.forEach( module => {
  8385. const source = module.render( options.format === 'es', this.legacy );
  8386. if ( source.toString().length ) {
  8387. magicString.addSource( source );
  8388. usedModules.push( module );
  8389. }
  8390. } );
  8391. if ( !magicString.toString().trim() && this.entryModule.getExports().length === 0 && this.entryModule.getReexports().length === 0 ) {
  8392. this.warn( {
  8393. code: 'EMPTY_BUNDLE',
  8394. message: 'Generated an empty bundle'
  8395. } );
  8396. }
  8397. timeEnd( 'render modules' );
  8398. let intro = [ options.intro ]
  8399. .concat(
  8400. this.plugins.map( plugin => plugin.intro && plugin.intro() )
  8401. )
  8402. .filter( Boolean )
  8403. .join( '\n\n' );
  8404. if ( intro ) { intro += '\n\n'; }
  8405. let outro = [ options.outro ]
  8406. .concat(
  8407. this.plugins.map( plugin => plugin.outro && plugin.outro() )
  8408. )
  8409. .filter( Boolean )
  8410. .join( '\n\n' );
  8411. if ( outro ) { outro = `\n\n${outro}`; }
  8412. const indentString = getIndentString( magicString, options );
  8413. const finalise = finalisers[ options.format ];
  8414. if ( !finalise ) {
  8415. error( {
  8416. code: 'INVALID_OPTION',
  8417. message: `Invalid format: ${options.format} - valid options are ${keys( finalisers ).join( ', ' )}`
  8418. } );
  8419. }
  8420. timeStart( 'render format' );
  8421. const optionsPaths = options.paths;
  8422. const getPath = (
  8423. typeof optionsPaths === 'function' ?
  8424. ( id => optionsPaths( id ) || this.getPathRelativeToEntryDirname( id ) ) :
  8425. optionsPaths ?
  8426. ( id => optionsPaths.hasOwnProperty( id ) ? optionsPaths[ id ] : this.getPathRelativeToEntryDirname( id ) ) :
  8427. id => this.getPathRelativeToEntryDirname( id )
  8428. );
  8429. magicString = finalise( this, magicString.trim(), { exportMode, getPath, indentString, intro, outro }, options );
  8430. timeEnd( 'render format' );
  8431. const banner = [ options.banner ]
  8432. .concat( this.plugins.map( plugin => plugin.banner ) )
  8433. .map( callIfFunction )
  8434. .filter( Boolean )
  8435. .join( '\n' );
  8436. const footer = [ options.footer ]
  8437. .concat( this.plugins.map( plugin => plugin.footer ) )
  8438. .map( callIfFunction )
  8439. .filter( Boolean )
  8440. .join( '\n' );
  8441. if ( banner ) { magicString.prepend( banner + '\n' ); }
  8442. if ( footer ) { magicString.append( '\n' + footer ); }
  8443. const prevCode = magicString.toString();
  8444. let map = null;
  8445. const bundleSourcemapChain = [];
  8446. return transformBundle( prevCode, this.plugins, bundleSourcemapChain, options ).then( code => {
  8447. if ( options.sourcemap ) {
  8448. timeStart( 'sourcemap' );
  8449. let file = options.sourcemapFile || options.file;
  8450. if ( file ) { file = resolve( typeof process !== 'undefined' ? process.cwd() : '', file ); }
  8451. if ( this.hasLoaders || find( this.plugins, plugin => plugin.transform || plugin.transformBundle ) ) {
  8452. map = magicString.generateMap( {} );
  8453. if ( typeof map.mappings === 'string' ) {
  8454. map.mappings = decode$$1( map.mappings );
  8455. }
  8456. map = collapseSourcemaps( this, file, map, usedModules, bundleSourcemapChain );
  8457. } else {
  8458. map = magicString.generateMap( { file, includeContent: true } );
  8459. }
  8460. map.sources = map.sources.map( normalize );
  8461. timeEnd( 'sourcemap' );
  8462. }
  8463. if ( code[ code.length - 1 ] !== '\n' ) { code += '\n'; }
  8464. return { code, map };
  8465. } );
  8466. } );
  8467. }
  8468. sort () {
  8469. let hasCycles;
  8470. const seen = {};
  8471. const ordered = [];
  8472. const stronglyDependsOn = blank();
  8473. const dependsOn = blank();
  8474. this.modules.forEach( module => {
  8475. stronglyDependsOn[ module.id ] = blank();
  8476. dependsOn[ module.id ] = blank();
  8477. } );
  8478. this.modules.forEach( module => {
  8479. function processStrongDependency ( dependency ) {
  8480. if ( dependency === module || stronglyDependsOn[ module.id ][ dependency.id ] ) { return; }
  8481. stronglyDependsOn[ module.id ][ dependency.id ] = true;
  8482. dependency.strongDependencies.forEach( processStrongDependency );
  8483. }
  8484. function processDependency ( dependency ) {
  8485. if ( dependency === module || dependsOn[ module.id ][ dependency.id ] ) { return; }
  8486. dependsOn[ module.id ][ dependency.id ] = true;
  8487. dependency.dependencies.forEach( processDependency );
  8488. }
  8489. module.strongDependencies.forEach( processStrongDependency );
  8490. module.dependencies.forEach( processDependency );
  8491. } );
  8492. const visit = module => {
  8493. if ( seen[ module.id ] ) {
  8494. hasCycles = true;
  8495. return;
  8496. }
  8497. seen[ module.id ] = true;
  8498. module.dependencies.forEach( visit );
  8499. ordered.push( module );
  8500. };
  8501. visit( this.entryModule );
  8502. if ( hasCycles ) {
  8503. ordered.forEach( ( a, i ) => {
  8504. for ( i += 1; i < ordered.length; i += 1 ) {
  8505. const b = ordered[ i ];
  8506. // TODO reinstate this! it no longer works
  8507. if ( stronglyDependsOn[ a.id ][ b.id ] ) {
  8508. // somewhere, there is a module that imports b before a. Because
  8509. // b imports a, a is placed before b. We need to find the module
  8510. // in question, so we can provide a useful error message
  8511. let parent = '[[unknown]]';
  8512. const visited = {};
  8513. const findParent = module => {
  8514. if ( dependsOn[ module.id ][ a.id ] && dependsOn[ module.id ][ b.id ] ) {
  8515. parent = module.id;
  8516. return true;
  8517. }
  8518. visited[ module.id ] = true;
  8519. for ( let i = 0; i < module.dependencies.length; i += 1 ) {
  8520. const dependency = module.dependencies[ i ];
  8521. if ( !visited[ dependency.id ] && findParent( dependency ) ) { return true; }
  8522. }
  8523. };
  8524. findParent( this.entryModule );
  8525. this.onwarn(
  8526. `Module ${a.id} may be unable to evaluate without ${b.id}, but is included first due to a cyclical dependency. Consider swapping the import statements in ${parent} to ensure correct ordering`
  8527. );
  8528. }
  8529. }
  8530. } );
  8531. }
  8532. return ordered;
  8533. }
  8534. warn ( warning ) {
  8535. warning.toString = () => {
  8536. let str = '';
  8537. if ( warning.plugin ) { str += `(${warning.plugin} plugin) `; }
  8538. if ( warning.loc ) { str += `${relativeId( warning.loc.file )} (${warning.loc.line}:${warning.loc.column}) `; }
  8539. str += warning.message;
  8540. return str;
  8541. };
  8542. this.onwarn( warning );
  8543. }
  8544. }
  8545. const ALLOWED_KEYS = [
  8546. 'acorn',
  8547. 'amd',
  8548. 'banner',
  8549. 'cache',
  8550. 'context',
  8551. 'entry',
  8552. 'exports',
  8553. 'extend',
  8554. 'external',
  8555. 'file',
  8556. 'footer',
  8557. 'format',
  8558. 'globals',
  8559. 'indent',
  8560. 'input',
  8561. 'interop',
  8562. 'intro',
  8563. 'legacy',
  8564. 'moduleContext',
  8565. 'name',
  8566. 'noConflict',
  8567. 'onwarn',
  8568. 'output',
  8569. 'outro',
  8570. 'paths',
  8571. 'plugins',
  8572. 'preferConst',
  8573. 'pureExternalModules',
  8574. 'sourcemap',
  8575. 'sourcemapFile',
  8576. 'strict',
  8577. 'targets',
  8578. 'treeshake',
  8579. 'watch'
  8580. ];
  8581. function checkAmd ( options ) {
  8582. if ( options.moduleId ) {
  8583. if ( options.amd ) { throw new Error( 'Cannot have both options.amd and options.moduleId' ); }
  8584. options.amd = { id: options.moduleId };
  8585. delete options.moduleId;
  8586. const message = `options.moduleId is deprecated in favour of options.amd = { id: moduleId }`;
  8587. if ( options.onwarn ) {
  8588. options.onwarn({ message });
  8589. } else {
  8590. console.warn( message ); // eslint-disable-line no-console
  8591. }
  8592. }
  8593. }
  8594. function checkInputOptions ( options, warn ) {
  8595. if ( options.transform || options.load || options.resolveId || options.resolveExternal ) {
  8596. throw new Error( 'The `transform`, `load`, `resolveId` and `resolveExternal` options are deprecated in favour of a unified plugin API. See https://github.com/rollup/rollup/wiki/Plugins for details' );
  8597. }
  8598. if ( options.entry && !options.input ) {
  8599. options.input = options.entry;
  8600. warn({
  8601. message: `options.entry is deprecated, use options.input`
  8602. });
  8603. }
  8604. const err = validateKeys( keys(options), ALLOWED_KEYS );
  8605. if ( err ) { throw err; }
  8606. }
  8607. const deprecated = {
  8608. dest: 'file',
  8609. moduleName: 'name',
  8610. sourceMap: 'sourcemap',
  8611. sourceMapFile: 'sourcemapFile',
  8612. useStrict: 'strict'
  8613. };
  8614. function checkOutputOptions ( options, warn ) {
  8615. if ( options.format === 'es6' ) {
  8616. error({
  8617. message: 'The `es6` output format is deprecated – use `es` instead',
  8618. url: `https://github.com/rollup/rollup/wiki/JavaScript-API#format`
  8619. });
  8620. }
  8621. if ( !options.format ) {
  8622. error({
  8623. message: `You must specify options.format, which can be one of 'amd', 'cjs', 'es', 'iife' or 'umd'`,
  8624. url: `https://github.com/rollup/rollup/wiki/JavaScript-API#format`
  8625. });
  8626. }
  8627. if ( options.moduleId ) {
  8628. if ( options.amd ) { throw new Error( 'Cannot have both options.amd and options.moduleId' ); }
  8629. options.amd = { id: options.moduleId };
  8630. delete options.moduleId;
  8631. warn({
  8632. message: `options.moduleId is deprecated in favour of options.amd = { id: moduleId }`
  8633. });
  8634. }
  8635. const deprecations = [];
  8636. Object.keys( deprecated ).forEach( old => {
  8637. if ( old in options ) {
  8638. deprecations.push({ old, new: deprecated[ old ] });
  8639. options[ deprecated[ old ] ] = options[ old ];
  8640. delete options[ old ];
  8641. }
  8642. });
  8643. if ( deprecations.length ) {
  8644. const message = `The following options have been renamed — please update your config: ${deprecations.map(option => `${option.old} -> ${option.new}`).join(', ')}`;
  8645. warn({
  8646. code: 'DEPRECATED_OPTIONS',
  8647. message,
  8648. deprecations
  8649. });
  8650. }
  8651. }
  8652. const throwAsyncGenerateError = {
  8653. get () {
  8654. throw new Error( `bundle.generate(...) now returns a Promise instead of a { code, map } object` );
  8655. }
  8656. };
  8657. function rollup ( options ) {
  8658. try {
  8659. if ( !options ) {
  8660. throw new Error( 'You must supply an options object to rollup' );
  8661. }
  8662. const warn = options.onwarn || (warning => console.warn( warning.message )); // eslint-disable-line no-console
  8663. checkInputOptions( options, warn );
  8664. const bundle = new Bundle$$1( options );
  8665. timeStart( '--BUILD--' );
  8666. return bundle.build().then( () => {
  8667. timeEnd( '--BUILD--' );
  8668. function generate ( options ) {
  8669. if ( !options ) {
  8670. throw new Error( 'You must supply an options object' );
  8671. }
  8672. checkOutputOptions( options, warn );
  8673. checkAmd( options );
  8674. timeStart( '--GENERATE--' );
  8675. const promise = Promise.resolve()
  8676. .then( () => bundle.render( options ) )
  8677. .then( rendered => {
  8678. timeEnd( '--GENERATE--' );
  8679. bundle.plugins.forEach( plugin => {
  8680. if ( plugin.ongenerate ) {
  8681. plugin.ongenerate( assign({
  8682. bundle: result
  8683. }, options ), rendered);
  8684. }
  8685. });
  8686. flushTime();
  8687. return rendered;
  8688. });
  8689. Object.defineProperty( promise, 'code', throwAsyncGenerateError );
  8690. Object.defineProperty( promise, 'map', throwAsyncGenerateError );
  8691. return promise;
  8692. }
  8693. const result = {
  8694. imports: bundle.externalModules.map( module => module.id ),
  8695. exports: keys( bundle.entryModule.exports ),
  8696. modules: bundle.orderedModules.map( module => module.toJSON() ),
  8697. generate,
  8698. write: options => {
  8699. if ( !options || (!options.file && !options.dest) ) {
  8700. error({
  8701. code: 'MISSING_OPTION',
  8702. message: 'You must specify options.file'
  8703. });
  8704. }
  8705. return generate( options ).then( result => {
  8706. const file = options.file;
  8707. var code = result.code;
  8708. var map = result.map;
  8709. const promises = [];
  8710. if ( options.sourcemap ) {
  8711. let url;
  8712. if ( options.sourcemap === 'inline' ) {
  8713. url = map.toUrl();
  8714. } else {
  8715. url = `${basename( file )}.map`;
  8716. promises.push( writeFile( file + '.map', map.toString() ) );
  8717. }
  8718. code += `//# ${SOURCEMAPPING_URL}=${url}\n`;
  8719. }
  8720. promises.push( writeFile( file, code ) );
  8721. return Promise.all( promises ).then( () => {
  8722. return mapSequence( bundle.plugins.filter( plugin => plugin.onwrite ), plugin => {
  8723. return Promise.resolve( plugin.onwrite( assign({
  8724. bundle: result
  8725. }, options ), result));
  8726. });
  8727. });
  8728. });
  8729. }
  8730. };
  8731. return result;
  8732. });
  8733. } catch ( err ) {
  8734. return Promise.reject( err );
  8735. }
  8736. }
  8737. var version$1 = "0.50.0";
  8738. exports.rollup = rollup;
  8739. exports.VERSION = version$1;
  8740. Object.defineProperty(exports, '__esModule', { value: true });
  8741. })));
  8742. //# sourceMappingURL=rollup.browser.js.map