compress.js 595 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. "use strict";
  34. function Compressor(options, false_by_default) {
  35. if (!(this instanceof Compressor))
  36. return new Compressor(options, false_by_default);
  37. TreeTransformer.call(this, this.before, this.after);
  38. this.options = defaults(options, {
  39. annotations : !false_by_default,
  40. arguments : !false_by_default,
  41. arrows : !false_by_default,
  42. assignments : !false_by_default,
  43. awaits : !false_by_default,
  44. booleans : !false_by_default,
  45. collapse_vars : !false_by_default,
  46. comparisons : !false_by_default,
  47. conditionals : !false_by_default,
  48. dead_code : !false_by_default,
  49. default_values : !false_by_default,
  50. directives : !false_by_default,
  51. drop_console : false,
  52. drop_debugger : !false_by_default,
  53. evaluate : !false_by_default,
  54. expression : false,
  55. functions : !false_by_default,
  56. global_defs : false,
  57. hoist_exports : !false_by_default,
  58. hoist_funs : false,
  59. hoist_props : !false_by_default,
  60. hoist_vars : false,
  61. ie : false,
  62. if_return : !false_by_default,
  63. imports : !false_by_default,
  64. inline : !false_by_default,
  65. join_vars : !false_by_default,
  66. keep_fargs : false_by_default,
  67. keep_fnames : false,
  68. keep_infinity : false,
  69. loops : !false_by_default,
  70. merge_vars : !false_by_default,
  71. negate_iife : !false_by_default,
  72. objects : !false_by_default,
  73. optional_chains : !false_by_default,
  74. passes : 1,
  75. properties : !false_by_default,
  76. pure_funcs : null,
  77. pure_getters : !false_by_default && "strict",
  78. reduce_funcs : !false_by_default,
  79. reduce_vars : !false_by_default,
  80. rests : !false_by_default,
  81. sequences : !false_by_default,
  82. side_effects : !false_by_default,
  83. spreads : !false_by_default,
  84. strings : !false_by_default,
  85. switches : !false_by_default,
  86. templates : !false_by_default,
  87. top_retain : null,
  88. toplevel : !!(options && options["top_retain"]),
  89. typeofs : !false_by_default,
  90. unsafe : false,
  91. unsafe_comps : false,
  92. unsafe_Function : false,
  93. unsafe_math : false,
  94. unsafe_proto : false,
  95. unsafe_regexp : false,
  96. unsafe_undefined: false,
  97. unused : !false_by_default,
  98. varify : !false_by_default,
  99. webkit : false,
  100. yields : !false_by_default,
  101. }, true);
  102. var evaluate = this.options["evaluate"];
  103. this.eval_threshold = /eager/.test(evaluate) ? 1 / 0 : +evaluate;
  104. var global_defs = this.options["global_defs"];
  105. if (typeof global_defs == "object") for (var key in global_defs) {
  106. if (/^@/.test(key) && HOP(global_defs, key)) {
  107. global_defs[key.slice(1)] = parse(global_defs[key], { expression: true });
  108. }
  109. }
  110. if (this.options["inline"] === true) this.options["inline"] = 4;
  111. this.drop_fargs = this.options["keep_fargs"] ? return_false : function(lambda, parent) {
  112. if (lambda.length_read) return false;
  113. var name = lambda.name;
  114. if (!name) return parent && parent.TYPE == "Call" && parent.expression === lambda;
  115. if (name.fixed_value() !== lambda) return false;
  116. var def = name.definition();
  117. if (def.direct_access) return false;
  118. var escaped = def.escaped;
  119. return escaped && escaped.depth != 1;
  120. };
  121. var pure_funcs = this.options["pure_funcs"];
  122. if (typeof pure_funcs == "function") {
  123. this.pure_funcs = pure_funcs;
  124. } else if (typeof pure_funcs == "string") {
  125. this.pure_funcs = function(node) {
  126. var expr;
  127. if (node instanceof AST_Call) {
  128. expr = node.expression;
  129. } else if (node instanceof AST_Template) {
  130. expr = node.tag;
  131. }
  132. return !(expr && pure_funcs === expr.print_to_string());
  133. };
  134. } else if (Array.isArray(pure_funcs)) {
  135. this.pure_funcs = function(node) {
  136. var expr;
  137. if (node instanceof AST_Call) {
  138. expr = node.expression;
  139. } else if (node instanceof AST_Template) {
  140. expr = node.tag;
  141. }
  142. return !(expr && member(expr.print_to_string(), pure_funcs));
  143. };
  144. } else {
  145. this.pure_funcs = return_true;
  146. }
  147. var sequences = this.options["sequences"];
  148. this.sequences_limit = sequences == 1 ? 800 : sequences | 0;
  149. var top_retain = this.options["top_retain"];
  150. if (top_retain instanceof RegExp) {
  151. this.top_retain = function(def) {
  152. return top_retain.test(def.name);
  153. };
  154. } else if (typeof top_retain == "function") {
  155. this.top_retain = top_retain;
  156. } else if (top_retain) {
  157. if (typeof top_retain == "string") {
  158. top_retain = top_retain.split(/,/);
  159. }
  160. this.top_retain = function(def) {
  161. return member(def.name, top_retain);
  162. };
  163. }
  164. var toplevel = this.options["toplevel"];
  165. this.toplevel = typeof toplevel == "string" ? {
  166. funcs: /funcs/.test(toplevel),
  167. vars: /vars/.test(toplevel)
  168. } : {
  169. funcs: toplevel,
  170. vars: toplevel
  171. };
  172. }
  173. Compressor.prototype = new TreeTransformer(function(node, descend, in_list) {
  174. if (node._squeezed) return node;
  175. var is_scope = node instanceof AST_Scope;
  176. if (is_scope) {
  177. node.hoist_properties(this);
  178. node.hoist_declarations(this);
  179. node.process_returns(this);
  180. }
  181. // Before https://github.com/mishoo/UglifyJS/pull/1602 AST_Node.optimize()
  182. // would call AST_Node.transform() if a different instance of AST_Node is
  183. // produced after OPT().
  184. // This corrupts TreeWalker.stack, which cause AST look-ups to malfunction.
  185. // Migrate and defer all children's AST_Node.transform() to below, which
  186. // will now happen after this parent AST_Node has been properly substituted
  187. // thus gives a consistent AST snapshot.
  188. descend(node, this);
  189. // Existing code relies on how AST_Node.optimize() worked, and omitting the
  190. // following replacement call would result in degraded efficiency of both
  191. // output and performance.
  192. descend(node, this);
  193. var opt = node.optimize(this);
  194. if (is_scope && opt === node && !this.has_directive("use asm") && !opt.pinned()) {
  195. opt.drop_unused(this);
  196. if (opt.merge_variables(this)) opt.drop_unused(this);
  197. descend(opt, this);
  198. }
  199. if (opt === node) opt._squeezed = true;
  200. return opt;
  201. });
  202. Compressor.prototype.option = function(key) {
  203. return this.options[key];
  204. };
  205. Compressor.prototype.exposed = function(def) {
  206. if (def.exported) return true;
  207. if (def.undeclared) return true;
  208. if (!(def.global || def.scope.resolve() instanceof AST_Toplevel)) return false;
  209. var toplevel = this.toplevel;
  210. return !all(def.orig, function(sym) {
  211. return toplevel[sym instanceof AST_SymbolDefun ? "funcs" : "vars"];
  212. });
  213. };
  214. Compressor.prototype.compress = function(node) {
  215. node = node.resolve_defines(this);
  216. node.hoist_exports(this);
  217. if (this.option("expression")) node.process_expression(true);
  218. var merge_vars = this.options.merge_vars;
  219. var passes = +this.options.passes || 1;
  220. var min_count = 1 / 0;
  221. var stopping = false;
  222. var mangle = { ie: this.option("ie") };
  223. for (var pass = 0; pass < passes; pass++) {
  224. node.figure_out_scope(mangle);
  225. if (pass > 0 || this.option("reduce_vars"))
  226. node.reset_opt_flags(this);
  227. this.options.merge_vars = merge_vars && (stopping || pass == passes - 1);
  228. node = node.transform(this);
  229. if (passes > 1) {
  230. var count = 0;
  231. node.walk(new TreeWalker(function() {
  232. count++;
  233. }));
  234. AST_Node.info("pass {pass}: last_count: {min_count}, count: {count}", {
  235. pass: pass,
  236. min_count: min_count,
  237. count: count,
  238. });
  239. if (count < min_count) {
  240. min_count = count;
  241. stopping = false;
  242. } else if (stopping) {
  243. break;
  244. } else {
  245. stopping = true;
  246. }
  247. }
  248. }
  249. if (this.option("expression")) node.process_expression(false);
  250. return node;
  251. };
  252. (function(OPT) {
  253. OPT(AST_Node, function(self, compressor) {
  254. return self;
  255. });
  256. AST_Node.DEFMETHOD("equivalent_to", function(node) {
  257. return this.TYPE == node.TYPE && this.print_to_string() == node.print_to_string();
  258. });
  259. AST_Toplevel.DEFMETHOD("hoist_exports", function(compressor) {
  260. if (!compressor.option("hoist_exports")) return;
  261. var body = this.body, props = [];
  262. for (var i = 0; i < body.length; i++) {
  263. var stat = body[i];
  264. if (stat instanceof AST_ExportDeclaration) {
  265. body[i] = stat = stat.body;
  266. if (stat instanceof AST_Definitions) {
  267. stat.definitions.forEach(function(defn) {
  268. defn.name.match_symbol(export_symbol, true);
  269. });
  270. } else {
  271. export_symbol(stat.name);
  272. }
  273. } else if (stat instanceof AST_ExportReferences) {
  274. body.splice(i--, 1);
  275. [].push.apply(props, stat.properties);
  276. }
  277. }
  278. if (props.length) body.push(make_node(AST_ExportReferences, this, { properties: props }));
  279. function export_symbol(sym) {
  280. if (!(sym instanceof AST_SymbolDeclaration)) return;
  281. var node = make_node(AST_SymbolExport, sym, sym);
  282. node.alias = node.name;
  283. props.push(node);
  284. }
  285. });
  286. AST_Scope.DEFMETHOD("process_expression", function(insert, transform) {
  287. var self = this;
  288. var tt = new TreeTransformer(function(node) {
  289. if (insert) {
  290. if (node instanceof AST_Directive) node = make_node(AST_SimpleStatement, node, {
  291. body: make_node(AST_String, node, node),
  292. });
  293. if (node instanceof AST_SimpleStatement) {
  294. return transform ? transform(node) : make_node(AST_Return, node, { value: node.body });
  295. }
  296. } else if (node instanceof AST_Return) {
  297. if (transform) return transform(node);
  298. var value = node.value;
  299. if (value instanceof AST_String) return make_node(AST_Directive, node, value);
  300. return make_node(AST_SimpleStatement, node, {
  301. body: value || make_node(AST_UnaryPrefix, node, {
  302. operator: "void",
  303. expression: make_node(AST_Number, node, { value: 0 }),
  304. }),
  305. });
  306. }
  307. if (node instanceof AST_Block) {
  308. if (node instanceof AST_Lambda) {
  309. if (node !== self) return node;
  310. } else if (insert === "awaits" && node instanceof AST_Try) {
  311. if (node.bfinally) return node;
  312. }
  313. for (var index = node.body.length; --index >= 0;) {
  314. var stat = node.body[index];
  315. if (!is_declaration(stat, true)) {
  316. node.body[index] = stat.transform(tt);
  317. break;
  318. }
  319. }
  320. } else if (node instanceof AST_If) {
  321. node.body = node.body.transform(tt);
  322. if (node.alternative) node.alternative = node.alternative.transform(tt);
  323. } else if (node instanceof AST_With) {
  324. node.body = node.body.transform(tt);
  325. }
  326. return node;
  327. });
  328. self.transform(tt);
  329. });
  330. function read_property(obj, node) {
  331. var key = node.get_property();
  332. if (key instanceof AST_Node) return;
  333. var value;
  334. if (obj instanceof AST_Array) {
  335. var elements = obj.elements;
  336. if (key == "length") return make_node_from_constant(elements.length, obj);
  337. if (typeof key == "number" && key in elements) value = elements[key];
  338. } else if (obj instanceof AST_Lambda) {
  339. if (key == "length") {
  340. obj.length_read = true;
  341. return make_node_from_constant(obj.argnames.length, obj);
  342. }
  343. } else if (obj instanceof AST_Object) {
  344. key = "" + key;
  345. var props = obj.properties;
  346. for (var i = props.length; --i >= 0;) {
  347. var prop = props[i];
  348. if (!can_hoist_property(prop)) return;
  349. if (!value && props[i].key === key) value = props[i].value;
  350. }
  351. }
  352. return value instanceof AST_SymbolRef && value.fixed_value() || value;
  353. }
  354. function is_read_only_fn(value, name) {
  355. if (value instanceof AST_Boolean) return native_fns.Boolean[name];
  356. if (value instanceof AST_Number) return native_fns.Number[name];
  357. if (value instanceof AST_String) return native_fns.String[name];
  358. if (name == "valueOf") return false;
  359. if (value instanceof AST_Array) return native_fns.Array[name];
  360. if (value instanceof AST_Lambda) return native_fns.Function[name];
  361. if (value instanceof AST_Object) return native_fns.Object[name];
  362. if (value instanceof AST_RegExp) return native_fns.RegExp[name] && !value.value.global;
  363. }
  364. function is_modified(compressor, tw, node, value, level, immutable, recursive) {
  365. var parent = tw.parent(level);
  366. if (compressor.option("unsafe") && parent instanceof AST_Dot && is_read_only_fn(value, parent.property)) {
  367. return;
  368. }
  369. var lhs = is_lhs(node, parent);
  370. if (lhs) return lhs;
  371. if (level == 0 && value && value.is_constant()) return;
  372. if (parent instanceof AST_Array) return is_modified(compressor, tw, parent, parent, level + 1);
  373. if (parent instanceof AST_Assign) switch (parent.operator) {
  374. case "=":
  375. return is_modified(compressor, tw, parent, value, level + 1, immutable, recursive);
  376. case "&&=":
  377. case "||=":
  378. case "??=":
  379. return is_modified(compressor, tw, parent, parent, level + 1);
  380. default:
  381. return;
  382. }
  383. if (parent instanceof AST_Binary) {
  384. if (!lazy_op[parent.operator]) return;
  385. return is_modified(compressor, tw, parent, parent, level + 1);
  386. }
  387. if (parent instanceof AST_Call) {
  388. return !immutable
  389. && parent.expression === node
  390. && !parent.is_expr_pure(compressor)
  391. && (!(value instanceof AST_LambdaExpression) || !(parent instanceof AST_New) && value.contains_this());
  392. }
  393. if (parent instanceof AST_Conditional) {
  394. if (parent.condition === node) return;
  395. return is_modified(compressor, tw, parent, parent, level + 1);
  396. }
  397. if (parent instanceof AST_ForEnumeration) return parent.init === node;
  398. if (parent instanceof AST_ObjectKeyVal) {
  399. if (parent.value !== node) return;
  400. var obj = tw.parent(level + 1);
  401. return is_modified(compressor, tw, obj, obj, level + 2);
  402. }
  403. if (parent instanceof AST_PropAccess) {
  404. if (parent.expression !== node) return;
  405. var prop = read_property(value, parent);
  406. return (!immutable || recursive) && is_modified(compressor, tw, parent, prop, level + 1);
  407. }
  408. if (parent instanceof AST_Sequence) {
  409. if (parent.tail_node() !== node) return;
  410. return is_modified(compressor, tw, parent, value, level + 1, immutable, recursive);
  411. }
  412. }
  413. function is_lambda(node) {
  414. return node instanceof AST_Class || node instanceof AST_Lambda;
  415. }
  416. function safe_for_extends(node) {
  417. return node instanceof AST_Class || node instanceof AST_Defun || node instanceof AST_Function;
  418. }
  419. function is_arguments(def) {
  420. return def.name == "arguments" && def.scope.uses_arguments;
  421. }
  422. function cross_scope(def, sym) {
  423. do {
  424. if (def === sym) return false;
  425. if (sym instanceof AST_Scope) return true;
  426. } while (sym = sym.parent_scope);
  427. }
  428. function can_drop_symbol(ref, compressor, keep_lambda) {
  429. var def = ref.redef || ref.definition();
  430. if (ref.in_arg && is_funarg(def)) return false;
  431. return all(def.orig, function(sym) {
  432. if (sym instanceof AST_SymbolConst || sym instanceof AST_SymbolLet) {
  433. if (sym instanceof AST_SymbolImport) return true;
  434. return compressor && can_varify(compressor, sym);
  435. }
  436. return !(keep_lambda && sym instanceof AST_SymbolLambda);
  437. });
  438. }
  439. function has_escaped(d, scope, node, parent) {
  440. if (parent instanceof AST_Assign) return parent.operator == "=" && parent.right === node;
  441. if (parent instanceof AST_Call) return parent.expression !== node || parent instanceof AST_New;
  442. if (parent instanceof AST_Exit) return parent.value === node && scope.resolve() !== d.scope.resolve();
  443. if (parent instanceof AST_VarDef) return parent.value === node;
  444. }
  445. var RE_POSITIVE_INTEGER = /^(0|[1-9][0-9]*)$/;
  446. (function(def) {
  447. def(AST_Node, noop);
  448. function reset_def(tw, compressor, def) {
  449. def.assignments = 0;
  450. def.bool_return = 0;
  451. def.drop_return = 0;
  452. def.cross_loop = false;
  453. def.direct_access = false;
  454. def.escaped = [];
  455. def.fixed = !def.const_redefs
  456. && !def.scope.pinned()
  457. && !compressor.exposed(def)
  458. && !(def.init instanceof AST_LambdaExpression && def.init !== def.scope)
  459. && def.init;
  460. def.reassigned = 0;
  461. def.recursive_refs = 0;
  462. def.references = [];
  463. def.single_use = undefined;
  464. }
  465. function reset_block_variables(tw, compressor, scope) {
  466. scope.variables.each(function(def) {
  467. reset_def(tw, compressor, def);
  468. });
  469. }
  470. function reset_variables(tw, compressor, scope) {
  471. scope.fn_defs = [];
  472. scope.variables.each(function(def) {
  473. reset_def(tw, compressor, def);
  474. var init = def.init;
  475. if (init instanceof AST_LambdaDefinition) {
  476. scope.fn_defs.push(init);
  477. init.safe_ids = null;
  478. }
  479. if (def.fixed === null) {
  480. def.safe_ids = tw.safe_ids;
  481. mark(tw, def);
  482. } else if (def.fixed) {
  483. tw.loop_ids[def.id] = tw.in_loop;
  484. mark(tw, def);
  485. }
  486. });
  487. scope.may_call_this = function() {
  488. scope.may_call_this = scope.contains_this() ? return_true : return_false;
  489. };
  490. if (scope.uses_arguments) scope.each_argname(function(node) {
  491. node.definition().last_ref = false;
  492. });
  493. if (compressor.option("ie")) scope.variables.each(function(def) {
  494. var d = def.orig[0].definition();
  495. if (d !== def) d.fixed = false;
  496. });
  497. }
  498. function safe_to_visit(tw, fn) {
  499. var marker = fn.safe_ids;
  500. return marker === undefined || marker === tw.safe_ids;
  501. }
  502. function walk_fn_def(tw, fn) {
  503. var was_scanning = tw.fn_scanning;
  504. tw.fn_scanning = fn;
  505. fn.walk(tw);
  506. tw.fn_scanning = was_scanning;
  507. }
  508. function revisit_fn_def(tw, fn) {
  509. fn.enclosed.forEach(function(d) {
  510. if (fn.variables.get(d.name) === d) return;
  511. if (safe_to_read(tw, d)) return;
  512. d.single_use = false;
  513. var fixed = d.fixed;
  514. if (typeof fixed == "function") fixed = fixed();
  515. if (fixed instanceof AST_Lambda && fixed.safe_ids !== undefined) return;
  516. d.fixed = false;
  517. });
  518. }
  519. function mark_fn_def(tw, def, fn) {
  520. var marker = fn.safe_ids;
  521. if (marker === undefined) return;
  522. if (marker === false) return;
  523. if (fn.parent_scope.resolve().may_call_this === return_true) {
  524. if (member(fn, tw.fn_visited)) revisit_fn_def(tw, fn);
  525. } else if (marker) {
  526. var visited = member(fn, tw.fn_visited);
  527. if (marker === tw.safe_ids) {
  528. if (!visited) walk_fn_def(tw, fn);
  529. } else if (visited) {
  530. revisit_fn_def(tw, fn);
  531. } else {
  532. fn.safe_ids = false;
  533. }
  534. } else if (tw.fn_scanning && tw.fn_scanning !== def.scope.resolve()) {
  535. fn.safe_ids = false;
  536. } else {
  537. fn.safe_ids = tw.safe_ids;
  538. walk_fn_def(tw, fn);
  539. }
  540. }
  541. function pop_scope(tw, scope) {
  542. var fn_defs = scope.fn_defs;
  543. var tangled = scope.may_call_this === return_true ? fn_defs : fn_defs.filter(function(fn) {
  544. if (fn.safe_ids === false) return true;
  545. fn.safe_ids = tw.safe_ids;
  546. walk_fn_def(tw, fn);
  547. return false;
  548. });
  549. pop(tw);
  550. tangled.forEach(function(fn) {
  551. fn.safe_ids = tw.safe_ids;
  552. walk_fn_def(tw, fn);
  553. });
  554. fn_defs.forEach(function(fn) {
  555. fn.safe_ids = undefined;
  556. });
  557. scope.fn_defs = undefined;
  558. scope.may_call_this = undefined;
  559. }
  560. function push(tw) {
  561. tw.safe_ids = Object.create(tw.safe_ids);
  562. }
  563. function pop(tw) {
  564. tw.safe_ids = Object.getPrototypeOf(tw.safe_ids);
  565. }
  566. function mark(tw, def) {
  567. tw.safe_ids[def.id] = {};
  568. }
  569. function push_ref(def, ref) {
  570. def.references.push(ref);
  571. if (def.last_ref !== false) def.last_ref = ref;
  572. }
  573. function safe_to_read(tw, def) {
  574. if (def.single_use == "m") return false;
  575. var safe = tw.safe_ids[def.id];
  576. if (safe) {
  577. var in_order = HOP(tw.safe_ids, def.id);
  578. if (!in_order) safe.read = safe.read && safe.read !== tw.safe_ids ? true : tw.safe_ids;
  579. if (def.fixed == null) {
  580. if (is_arguments(def)) return false;
  581. if (def.global && def.name == "arguments") return false;
  582. tw.loop_ids[def.id] = null;
  583. def.fixed = make_node(AST_Undefined, def.orig[0]);
  584. if (in_order) def.safe_ids = undefined;
  585. return true;
  586. }
  587. return !safe.assign || safe.assign === tw.safe_ids;
  588. }
  589. return def.fixed instanceof AST_LambdaDefinition;
  590. }
  591. function safe_to_assign(tw, def, declare) {
  592. if (!declare) {
  593. if (is_funarg(def) && def.scope.uses_arguments && !tw.has_directive("use strict")) return false;
  594. if (!all(def.orig, function(sym) {
  595. return !(sym instanceof AST_SymbolConst);
  596. })) return false;
  597. }
  598. if (def.fixed === undefined) return declare || all(def.orig, function(sym) {
  599. return !(sym instanceof AST_SymbolLet);
  600. });
  601. if (def.fixed === false || def.fixed === 0) return false;
  602. var safe = tw.safe_ids[def.id];
  603. if (def.safe_ids) {
  604. def.safe_ids[def.id] = false;
  605. def.safe_ids = undefined;
  606. return def.fixed === null || HOP(tw.safe_ids, def.id) && !safe.read;
  607. }
  608. if (!HOP(tw.safe_ids, def.id)) {
  609. if (!safe) return false;
  610. if (safe.read) {
  611. var scope = tw.find_parent(AST_BlockScope);
  612. if (scope instanceof AST_Class) return false;
  613. if (def.scope.resolve() !== scope.resolve()) return false;
  614. }
  615. safe.assign = safe.assign && safe.assign !== tw.safe_ids ? true : tw.safe_ids;
  616. }
  617. if (def.fixed != null && safe.read) {
  618. if (safe.read !== tw.safe_ids) return false;
  619. if (tw.loop_ids[def.id] !== tw.in_loop) return false;
  620. }
  621. return safe_to_read(tw, def) && all(def.orig, function(sym) {
  622. return !(sym instanceof AST_SymbolLambda);
  623. });
  624. }
  625. function make_ref(ref, fixed) {
  626. var node = make_node(AST_SymbolRef, ref, ref);
  627. node.fixed = fixed || make_node(AST_Undefined, ref);
  628. return node;
  629. }
  630. function replace_ref(ref, fixed) {
  631. return function() {
  632. var node = make_ref(ref, fixed);
  633. var def = ref.definition();
  634. def.references.push(node);
  635. def.replaced++;
  636. return node;
  637. };
  638. }
  639. function ref_once(compressor, def) {
  640. return compressor.option("unused")
  641. && !def.scope.pinned()
  642. && def.single_use !== false
  643. && def.references.length - def.recursive_refs == 1
  644. && !(is_funarg(def) && def.scope.uses_arguments);
  645. }
  646. function is_immutable(value) {
  647. if (!value) return false;
  648. if (value instanceof AST_Assign) {
  649. var op = value.operator;
  650. return op == "=" ? is_immutable(value.right) : !lazy_op[op.slice(0, -1)];
  651. }
  652. if (value instanceof AST_Sequence) return is_immutable(value.tail_node());
  653. return value.is_constant() || is_lambda(value) || value instanceof AST_ObjectIdentity;
  654. }
  655. function value_in_use(node, parent) {
  656. if (parent instanceof AST_Array) return true;
  657. if (parent instanceof AST_Binary) return lazy_op[parent.operator];
  658. if (parent instanceof AST_Conditional) return parent.condition !== node;
  659. if (parent instanceof AST_Sequence) return parent.tail_node() === node;
  660. if (parent instanceof AST_Spread) return true;
  661. }
  662. function mark_escaped(tw, d, scope, node, value, level, depth) {
  663. var parent = tw.parent(level);
  664. if (value && value.is_constant()) return;
  665. if (has_escaped(d, scope, node, parent)) {
  666. d.escaped.push(parent);
  667. if (depth > 1 && !(value && value.is_constant_expression(scope))) depth = 1;
  668. if (!d.escaped.depth || d.escaped.depth > depth) d.escaped.depth = depth;
  669. if (d.scope.resolve() !== scope.resolve()) d.escaped.cross_scope = true;
  670. if (d.fixed) d.fixed.escaped = d.escaped;
  671. return;
  672. } else if (value_in_use(node, parent)) {
  673. mark_escaped(tw, d, scope, parent, parent, level + 1, depth);
  674. } else if (parent instanceof AST_ObjectKeyVal && parent.value === node) {
  675. var obj = tw.parent(level + 1);
  676. mark_escaped(tw, d, scope, obj, obj, level + 2, depth);
  677. } else if (parent instanceof AST_PropAccess && parent.expression === node) {
  678. value = read_property(value, parent);
  679. mark_escaped(tw, d, scope, parent, value, level + 1, depth + 1);
  680. if (value) return;
  681. }
  682. if (level > 0) return;
  683. if (parent instanceof AST_Call && parent.expression === node) return;
  684. if (parent instanceof AST_Sequence && parent.tail_node() !== node) return;
  685. if (parent instanceof AST_SimpleStatement) return;
  686. if (parent instanceof AST_Unary && !unary_side_effects[parent.operator]) return;
  687. d.direct_access = true;
  688. if (d.fixed) d.fixed.direct_access = true;
  689. }
  690. function mark_assignment_to_arguments(node) {
  691. if (!(node instanceof AST_Sub)) return;
  692. var expr = node.expression;
  693. if (!(expr instanceof AST_SymbolRef)) return;
  694. var def = expr.definition();
  695. if (!is_arguments(def)) return;
  696. var key = node.property;
  697. if (key.is_constant()) key = key.value;
  698. if (!(key instanceof AST_Node) && !RE_POSITIVE_INTEGER.test(key)) return;
  699. def.reassigned++;
  700. (key instanceof AST_Node ? def.scope.argnames : [ def.scope.argnames[key] ]).forEach(function(argname) {
  701. if (argname instanceof AST_SymbolFunarg) argname.definition().fixed = false;
  702. });
  703. }
  704. function make_fixed(save, fn) {
  705. var prev_save, prev_value;
  706. return function() {
  707. var current = save();
  708. if (prev_save !== current) {
  709. prev_save = current;
  710. prev_value = fn(current);
  711. }
  712. return prev_value;
  713. };
  714. }
  715. function make_fixed_default(compressor, node, save) {
  716. var prev_save, prev_seq;
  717. return function() {
  718. var current = save();
  719. var ev;
  720. if (!is_undefined(current, compressor) && (ev = fuzzy_eval(compressor, current, true)) !== undefined) {
  721. return ev instanceof AST_Node ? node : current;
  722. }
  723. if (prev_save !== current) {
  724. prev_save = current;
  725. prev_seq = make_sequence(node, [ current, node.value ]);
  726. }
  727. return prev_seq;
  728. };
  729. }
  730. function scan_declaration(tw, compressor, lhs, fixed, visit) {
  731. var scanner = new TreeWalker(function(node) {
  732. if (node instanceof AST_DefaultValue) {
  733. reset_flags(node);
  734. push(tw);
  735. node.value.walk(tw);
  736. pop(tw);
  737. var save = fixed;
  738. if (save) fixed = make_fixed_default(compressor, node, save);
  739. node.name.walk(scanner);
  740. fixed = save;
  741. return true;
  742. }
  743. if (node instanceof AST_DestructuredArray) {
  744. reset_flags(node);
  745. var save = fixed;
  746. node.elements.forEach(function(node, index) {
  747. if (node instanceof AST_Hole) return reset_flags(node);
  748. if (save) fixed = make_fixed(save, function(value) {
  749. return make_node(AST_Sub, node, {
  750. expression: value,
  751. property: make_node(AST_Number, node, { value: index }),
  752. });
  753. });
  754. node.walk(scanner);
  755. });
  756. if (node.rest) {
  757. var fixed_node;
  758. if (save) fixed = compressor.option("rests") && make_fixed(save, function(value) {
  759. if (!(value instanceof AST_Array)) return node;
  760. for (var i = 0, len = node.elements.length; i < len; i++) {
  761. if (value.elements[i] instanceof AST_Spread) return node;
  762. }
  763. if (!fixed_node) fixed_node = make_node(AST_Array, node);
  764. fixed_node.elements = value.elements.slice(len);
  765. return fixed_node;
  766. });
  767. node.rest.walk(scanner);
  768. }
  769. fixed = save;
  770. return true;
  771. }
  772. if (node instanceof AST_DestructuredObject) {
  773. reset_flags(node);
  774. var save = fixed;
  775. node.properties.forEach(function(node) {
  776. reset_flags(node);
  777. if (node.key instanceof AST_Node) {
  778. push(tw);
  779. node.key.walk(tw);
  780. pop(tw);
  781. }
  782. if (save) fixed = make_fixed(save, function(value) {
  783. var key = node.key;
  784. var type = AST_Sub;
  785. if (typeof key == "string") {
  786. if (is_identifier_string(key)) {
  787. type = AST_Dot;
  788. } else {
  789. key = make_node_from_constant(key, node);
  790. }
  791. }
  792. return make_node(type, node, {
  793. expression: value,
  794. property: key,
  795. });
  796. });
  797. node.value.walk(scanner);
  798. });
  799. if (node.rest) {
  800. fixed = false;
  801. node.rest.walk(scanner);
  802. }
  803. fixed = save;
  804. return true;
  805. }
  806. visit(node, fixed, function() {
  807. var save_len = tw.stack.length;
  808. for (var i = 0, len = scanner.stack.length - 1; i < len; i++) {
  809. tw.stack.push(scanner.stack[i]);
  810. }
  811. node.walk(tw);
  812. tw.stack.length = save_len;
  813. });
  814. return true;
  815. });
  816. lhs.walk(scanner);
  817. }
  818. function reduce_iife(tw, descend, compressor) {
  819. var fn = this;
  820. fn.inlined = false;
  821. var iife = tw.parent();
  822. var hit = is_async(fn) || is_generator(fn);
  823. var aborts = false;
  824. fn.walk(new TreeWalker(function(node) {
  825. if (hit) return aborts = true;
  826. if (node instanceof AST_Return) return hit = true;
  827. if (node instanceof AST_Scope && node !== fn) return true;
  828. }));
  829. if (aborts) push(tw);
  830. reset_variables(tw, compressor, fn);
  831. // Virtually turn IIFE parameters into variable definitions:
  832. // (function(a,b) {...})(c,d) ---> (function() {var a=c,b=d; ...})()
  833. // So existing transformation rules can work on them.
  834. var safe = !fn.uses_arguments || tw.has_directive("use strict");
  835. fn.argnames.forEach(function(argname, i) {
  836. var value = iife.args[i];
  837. scan_declaration(tw, compressor, argname, function() {
  838. var j = fn.argnames.indexOf(argname);
  839. var arg = j < 0 ? value : iife.args[j];
  840. if (arg instanceof AST_Sequence && arg.expressions.length < 2) arg = arg.expressions[0];
  841. return arg || make_node(AST_Undefined, iife);
  842. }, visit);
  843. });
  844. var rest = fn.rest, fixed_node;
  845. if (rest) scan_declaration(tw, compressor, rest, compressor.option("rests") && function() {
  846. if (fn.rest !== rest) return rest;
  847. if (!fixed_node) fixed_node = make_node(AST_Array, fn);
  848. fixed_node.elements = iife.args.slice(fn.argnames.length);
  849. return fixed_node;
  850. }, visit);
  851. walk_lambda(fn, tw);
  852. var safe_ids = tw.safe_ids;
  853. pop_scope(tw, fn);
  854. if (!aborts) tw.safe_ids = safe_ids;
  855. return true;
  856. function visit(node, fixed) {
  857. var d = node.definition();
  858. if (fixed && safe && d.fixed === undefined) {
  859. mark(tw, d);
  860. tw.loop_ids[d.id] = tw.in_loop;
  861. d.fixed = fixed;
  862. d.fixed.assigns = [ node ];
  863. } else {
  864. d.fixed = false;
  865. }
  866. }
  867. }
  868. def(AST_Assign, function(tw, descend, compressor) {
  869. var node = this;
  870. var left = node.left;
  871. var right = node.right;
  872. var ld = left instanceof AST_SymbolRef && left.definition();
  873. var scan = ld || left instanceof AST_Destructured;
  874. switch (node.operator) {
  875. case "=":
  876. if (left.equivalent_to(right) && !left.has_side_effects(compressor)) {
  877. right.walk(tw);
  878. walk_prop(left);
  879. node.redundant = true;
  880. return true;
  881. }
  882. if (ld && right instanceof AST_LambdaExpression) {
  883. walk_assign();
  884. right.parent_scope.resolve().fn_defs.push(right);
  885. right.safe_ids = null;
  886. if (!ld.fixed || !node.write_only) mark_fn_def(tw, ld, right);
  887. return true;
  888. }
  889. if (scan) {
  890. right.walk(tw);
  891. walk_assign();
  892. return true;
  893. }
  894. mark_assignment_to_arguments(left);
  895. return;
  896. case "&&=":
  897. case "||=":
  898. case "??=":
  899. var lazy = true;
  900. default:
  901. if (!scan) {
  902. mark_assignment_to_arguments(left);
  903. return walk_lazy();
  904. }
  905. ld.assignments++;
  906. var fixed = ld.fixed;
  907. if (is_modified(compressor, tw, node, node, 0)) {
  908. ld.fixed = false;
  909. return walk_lazy();
  910. }
  911. var safe = safe_to_read(tw, ld);
  912. if (lazy) push(tw);
  913. right.walk(tw);
  914. if (lazy) pop(tw);
  915. if (safe && !left.in_arg && safe_to_assign(tw, ld)) {
  916. push_ref(ld, left);
  917. mark(tw, ld);
  918. if (ld.single_use) ld.single_use = false;
  919. left.fixed = ld.fixed = function() {
  920. return make_node(AST_Binary, node, {
  921. operator: node.operator.slice(0, -1),
  922. left: make_ref(left, fixed),
  923. right: node.right,
  924. });
  925. };
  926. left.fixed.assigns = !fixed || !fixed.assigns ? [ ld.orig[0] ] : fixed.assigns.slice();
  927. left.fixed.assigns.push(node);
  928. left.fixed.to_binary = replace_ref(left, fixed);
  929. } else {
  930. left.walk(tw);
  931. ld.fixed = false;
  932. }
  933. return true;
  934. }
  935. function walk_prop(lhs) {
  936. if (lhs instanceof AST_Dot) {
  937. walk_prop(lhs.expression);
  938. } else if (lhs instanceof AST_Sub) {
  939. walk_prop(lhs.expression);
  940. lhs.property.walk(tw);
  941. } else if (lhs instanceof AST_SymbolRef) {
  942. var d = lhs.definition();
  943. push_ref(d, lhs);
  944. if (d.fixed) {
  945. lhs.fixed = d.fixed;
  946. if (lhs.fixed.assigns) {
  947. lhs.fixed.assigns.push(node);
  948. } else {
  949. lhs.fixed.assigns = [ node ];
  950. }
  951. }
  952. } else {
  953. lhs.walk(tw);
  954. }
  955. }
  956. function walk_assign() {
  957. var recursive = ld && recursive_ref(tw, ld);
  958. var modified = is_modified(compressor, tw, node, right, 0, is_immutable(right), recursive);
  959. scan_declaration(tw, compressor, left, function() {
  960. return node.right;
  961. }, function(sym, fixed, walk) {
  962. if (!(sym instanceof AST_SymbolRef)) {
  963. mark_assignment_to_arguments(sym);
  964. walk();
  965. return;
  966. }
  967. var d = sym.definition();
  968. d.assignments++;
  969. if (!fixed || sym.in_arg || !safe_to_assign(tw, d)) {
  970. walk();
  971. d.fixed = false;
  972. } else if (modified) {
  973. walk();
  974. d.fixed = 0;
  975. } else {
  976. push_ref(d, sym);
  977. mark(tw, d);
  978. if (left instanceof AST_Destructured
  979. || d.orig.length == 1 && d.orig[0] instanceof AST_SymbolDefun) {
  980. d.single_use = false;
  981. }
  982. tw.loop_ids[d.id] = tw.in_loop;
  983. sym.fixed = d.fixed = fixed;
  984. sym.fixed.assigns = [ node ];
  985. mark_escaped(tw, d, sym.scope, node, right, 0, 1);
  986. }
  987. });
  988. }
  989. function walk_lazy() {
  990. if (!lazy) return;
  991. left.walk(tw);
  992. push(tw);
  993. right.walk(tw);
  994. pop(tw);
  995. return true;
  996. }
  997. });
  998. def(AST_Binary, function(tw) {
  999. if (!lazy_op[this.operator]) return;
  1000. this.left.walk(tw);
  1001. push(tw);
  1002. this.right.walk(tw);
  1003. pop(tw);
  1004. return true;
  1005. });
  1006. def(AST_BlockScope, function(tw, descend, compressor) {
  1007. reset_block_variables(tw, compressor, this);
  1008. });
  1009. def(AST_Call, function(tw, descend) {
  1010. var node = this;
  1011. var exp = node.expression;
  1012. if (exp instanceof AST_LambdaExpression) {
  1013. var iife = is_iife_single(node);
  1014. node.args.forEach(function(arg) {
  1015. arg.walk(tw);
  1016. if (arg instanceof AST_Spread) iife = false;
  1017. });
  1018. if (iife) exp.reduce_vars = reduce_iife;
  1019. exp.walk(tw);
  1020. if (iife) delete exp.reduce_vars;
  1021. return true;
  1022. }
  1023. if (node.TYPE == "Call") switch (tw.in_boolean_context()) {
  1024. case "d":
  1025. var drop = true;
  1026. case true:
  1027. mark_refs(exp, drop);
  1028. }
  1029. exp.walk(tw);
  1030. var optional = node.optional;
  1031. if (optional) push(tw);
  1032. node.args.forEach(function(arg) {
  1033. arg.walk(tw);
  1034. });
  1035. if (optional) pop(tw);
  1036. var fixed = exp instanceof AST_SymbolRef && exp.fixed_value();
  1037. if (fixed instanceof AST_Lambda) {
  1038. mark_fn_def(tw, exp.definition(), fixed);
  1039. } else {
  1040. tw.find_parent(AST_Scope).may_call_this();
  1041. }
  1042. return true;
  1043. function mark_refs(node, drop) {
  1044. if (node instanceof AST_Assign) {
  1045. if (node.operator != "=") return;
  1046. mark_refs(node.left, drop);
  1047. mark_refs(node.right, drop);
  1048. } else if (node instanceof AST_Binary) {
  1049. if (!lazy_op[node.operator]) return;
  1050. mark_refs(node.left, drop);
  1051. mark_refs(node.right, drop);
  1052. } else if (node instanceof AST_Conditional) {
  1053. mark_refs(node.consequent, drop);
  1054. mark_refs(node.alternative, drop);
  1055. } else if (node instanceof AST_SymbolRef) {
  1056. var def = node.definition();
  1057. def.bool_return++;
  1058. if (drop) def.drop_return++;
  1059. }
  1060. }
  1061. });
  1062. def(AST_Class, function(tw, descend, compressor) {
  1063. var node = this;
  1064. reset_block_variables(tw, compressor, node);
  1065. if (node.extends) node.extends.walk(tw);
  1066. var props = node.properties.filter(function(prop) {
  1067. reset_flags(prop);
  1068. if (prop.key instanceof AST_Node) prop.key.walk(tw);
  1069. return prop.value;
  1070. });
  1071. if (node.name) {
  1072. var d = node.name.definition();
  1073. var parent = tw.parent();
  1074. if (parent instanceof AST_ExportDeclaration || parent instanceof AST_ExportDefault) d.single_use = false;
  1075. if (safe_to_assign(tw, d, true)) {
  1076. mark(tw, d);
  1077. tw.loop_ids[d.id] = tw.in_loop;
  1078. d.fixed = function() {
  1079. return node;
  1080. };
  1081. d.fixed.assigns = [ node ];
  1082. if (!is_safe_lexical(d)) d.single_use = false;
  1083. } else {
  1084. d.fixed = false;
  1085. }
  1086. }
  1087. props.forEach(function(prop) {
  1088. if (!prop.static || prop instanceof AST_ClassField && prop.value.contains_this()) {
  1089. push(tw);
  1090. prop.value.walk(tw);
  1091. pop(tw);
  1092. } else {
  1093. prop.value.walk(tw);
  1094. }
  1095. });
  1096. return true;
  1097. });
  1098. def(AST_Conditional, function(tw) {
  1099. this.condition.walk(tw);
  1100. push(tw);
  1101. this.consequent.walk(tw);
  1102. pop(tw);
  1103. push(tw);
  1104. this.alternative.walk(tw);
  1105. pop(tw);
  1106. return true;
  1107. });
  1108. def(AST_DefaultValue, function(tw) {
  1109. this.name.walk(tw);
  1110. push(tw);
  1111. this.value.walk(tw);
  1112. pop(tw);
  1113. return true;
  1114. });
  1115. def(AST_Do, function(tw) {
  1116. var save_loop = tw.in_loop;
  1117. tw.in_loop = this;
  1118. push(tw);
  1119. this.body.walk(tw);
  1120. if (has_loop_control(this, tw.parent())) {
  1121. pop(tw);
  1122. push(tw);
  1123. }
  1124. this.condition.walk(tw);
  1125. pop(tw);
  1126. tw.in_loop = save_loop;
  1127. return true;
  1128. });
  1129. def(AST_For, function(tw, descend, compressor) {
  1130. var node = this;
  1131. reset_block_variables(tw, compressor, node);
  1132. if (node.init) node.init.walk(tw);
  1133. var save_loop = tw.in_loop;
  1134. tw.in_loop = node;
  1135. push(tw);
  1136. if (node.condition) node.condition.walk(tw);
  1137. node.body.walk(tw);
  1138. if (node.step) {
  1139. if (has_loop_control(node, tw.parent())) {
  1140. pop(tw);
  1141. push(tw);
  1142. }
  1143. node.step.walk(tw);
  1144. }
  1145. pop(tw);
  1146. tw.in_loop = save_loop;
  1147. return true;
  1148. });
  1149. def(AST_ForEnumeration, function(tw, descend, compressor) {
  1150. var node = this;
  1151. reset_block_variables(tw, compressor, node);
  1152. node.object.walk(tw);
  1153. var save_loop = tw.in_loop;
  1154. tw.in_loop = node;
  1155. push(tw);
  1156. var init = node.init;
  1157. if (init instanceof AST_Definitions) {
  1158. init.definitions[0].name.mark_symbol(function(node) {
  1159. if (node instanceof AST_SymbolDeclaration) {
  1160. var def = node.definition();
  1161. def.assignments++;
  1162. def.fixed = false;
  1163. }
  1164. }, tw);
  1165. } else if (init instanceof AST_Destructured || init instanceof AST_SymbolRef) {
  1166. init.mark_symbol(function(node) {
  1167. if (node instanceof AST_SymbolRef) {
  1168. var def = node.definition();
  1169. push_ref(def, node);
  1170. def.assignments++;
  1171. if (!node.is_immutable()) def.fixed = false;
  1172. }
  1173. }, tw);
  1174. } else {
  1175. init.walk(tw);
  1176. }
  1177. node.body.walk(tw);
  1178. pop(tw);
  1179. tw.in_loop = save_loop;
  1180. return true;
  1181. });
  1182. def(AST_If, function(tw) {
  1183. this.condition.walk(tw);
  1184. push(tw);
  1185. this.body.walk(tw);
  1186. pop(tw);
  1187. if (this.alternative) {
  1188. push(tw);
  1189. this.alternative.walk(tw);
  1190. pop(tw);
  1191. }
  1192. return true;
  1193. });
  1194. def(AST_LabeledStatement, function(tw) {
  1195. push(tw);
  1196. this.body.walk(tw);
  1197. pop(tw);
  1198. return true;
  1199. });
  1200. def(AST_Lambda, function(tw, descend, compressor) {
  1201. var fn = this;
  1202. if (!safe_to_visit(tw, fn)) return true;
  1203. if (!push_uniq(tw.fn_visited, fn)) return true;
  1204. fn.inlined = false;
  1205. push(tw);
  1206. reset_variables(tw, compressor, fn);
  1207. descend();
  1208. pop_scope(tw, fn);
  1209. if (fn.name) mark_escaped(tw, fn.name.definition(), fn, fn.name, fn, 0, 1);
  1210. return true;
  1211. });
  1212. def(AST_LambdaDefinition, function(tw, descend, compressor) {
  1213. var fn = this;
  1214. var def = fn.name.definition();
  1215. var parent = tw.parent();
  1216. if (parent instanceof AST_ExportDeclaration || parent instanceof AST_ExportDefault) def.single_use = false;
  1217. if (!safe_to_visit(tw, fn)) return true;
  1218. if (!push_uniq(tw.fn_visited, fn)) return true;
  1219. fn.inlined = false;
  1220. push(tw);
  1221. reset_variables(tw, compressor, fn);
  1222. descend();
  1223. pop_scope(tw, fn);
  1224. return true;
  1225. });
  1226. def(AST_Sub, function(tw) {
  1227. if (!this.optional) return;
  1228. this.expression.walk(tw);
  1229. push(tw);
  1230. this.property.walk(tw);
  1231. pop(tw);
  1232. return true;
  1233. });
  1234. def(AST_Switch, function(tw, descend, compressor) {
  1235. var node = this;
  1236. reset_block_variables(tw, compressor, node);
  1237. node.expression.walk(tw);
  1238. var first = true;
  1239. node.body.forEach(function(branch) {
  1240. if (branch instanceof AST_Default) return;
  1241. branch.expression.walk(tw);
  1242. if (first) {
  1243. first = false;
  1244. push(tw);
  1245. }
  1246. })
  1247. if (!first) pop(tw);
  1248. walk_body(node, tw);
  1249. return true;
  1250. });
  1251. def(AST_SwitchBranch, function(tw) {
  1252. push(tw);
  1253. walk_body(this, tw);
  1254. pop(tw);
  1255. return true;
  1256. });
  1257. def(AST_SymbolCatch, function() {
  1258. this.definition().fixed = false;
  1259. });
  1260. def(AST_SymbolImport, function() {
  1261. this.definition().fixed = false;
  1262. });
  1263. def(AST_SymbolRef, function(tw, descend, compressor) {
  1264. var d = this.definition();
  1265. push_ref(d, this);
  1266. if (d.references.length == 1 && !d.fixed && d.orig[0] instanceof AST_SymbolDefun) {
  1267. tw.loop_ids[d.id] = tw.in_loop;
  1268. }
  1269. var recursive = recursive_ref(tw, d);
  1270. if (recursive) recursive.enclosed.forEach(function(def) {
  1271. if (d === def) return;
  1272. if (def.scope.resolve() === recursive) return;
  1273. var assigns = def.fixed && def.fixed.assigns;
  1274. if (!assigns) return;
  1275. if (assigns[assigns.length - 1] instanceof AST_VarDef) return;
  1276. var safe = tw.safe_ids[def.id];
  1277. if (!safe) return;
  1278. safe.assign = true;
  1279. });
  1280. if (d.fixed === false || d.fixed === 0) {
  1281. var redef = d.redefined();
  1282. if (redef && cross_scope(d.scope, this.scope)) redef.single_use = false;
  1283. } else if (d.fixed === undefined || !safe_to_read(tw, d)) {
  1284. d.fixed = false;
  1285. } else if (d.fixed) {
  1286. if (this.in_arg && d.orig[0] instanceof AST_SymbolLambda) this.fixed = d.scope;
  1287. var value = this.fixed_value();
  1288. if (recursive) {
  1289. d.recursive_refs++;
  1290. } else if (value && ref_once(compressor, d)) {
  1291. d.in_loop = tw.loop_ids[d.id] !== tw.in_loop;
  1292. d.single_use = is_lambda(value)
  1293. && !value.pinned()
  1294. && (!d.in_loop || tw.parent() instanceof AST_Call)
  1295. || !d.in_loop
  1296. && d.scope === this.scope.resolve()
  1297. && value.is_constant_expression();
  1298. } else {
  1299. d.single_use = false;
  1300. }
  1301. if (is_modified(compressor, tw, this, value, 0, is_immutable(value), recursive)) {
  1302. if (d.single_use) {
  1303. d.single_use = "m";
  1304. } else {
  1305. d.fixed = 0;
  1306. }
  1307. }
  1308. if (d.fixed && tw.loop_ids[d.id] !== tw.in_loop) d.cross_loop = true;
  1309. mark_escaped(tw, d, this.scope, this, value, 0, 1);
  1310. }
  1311. if (!this.fixed) this.fixed = d.fixed;
  1312. var parent;
  1313. if (value instanceof AST_Lambda
  1314. && !((parent = tw.parent()) instanceof AST_Call && parent.expression === this)) {
  1315. mark_fn_def(tw, d, value);
  1316. }
  1317. });
  1318. def(AST_Template, function(tw, descend) {
  1319. var node = this;
  1320. var tag = node.tag;
  1321. if (!tag) return;
  1322. if (tag instanceof AST_LambdaExpression) {
  1323. node.expressions.forEach(function(exp) {
  1324. exp.walk(tw);
  1325. });
  1326. tag.walk(tw);
  1327. return true;
  1328. }
  1329. tag.walk(tw);
  1330. node.expressions.forEach(function(exp) {
  1331. exp.walk(tw);
  1332. });
  1333. var fixed = tag instanceof AST_SymbolRef && tag.fixed_value();
  1334. if (fixed instanceof AST_Lambda) {
  1335. mark_fn_def(tw, tag.definition(), fixed);
  1336. } else {
  1337. tw.find_parent(AST_Scope).may_call_this();
  1338. }
  1339. return true;
  1340. });
  1341. def(AST_Toplevel, function(tw, descend, compressor) {
  1342. var node = this;
  1343. node.globals.each(function(def) {
  1344. reset_def(tw, compressor, def);
  1345. });
  1346. push(tw);
  1347. reset_variables(tw, compressor, node);
  1348. descend();
  1349. pop_scope(tw, node);
  1350. return true;
  1351. });
  1352. def(AST_Try, function(tw, descend, compressor) {
  1353. var node = this;
  1354. reset_block_variables(tw, compressor, node);
  1355. push(tw);
  1356. walk_body(node, tw);
  1357. pop(tw);
  1358. if (node.bcatch) {
  1359. push(tw);
  1360. node.bcatch.walk(tw);
  1361. pop(tw);
  1362. }
  1363. if (node.bfinally) node.bfinally.walk(tw);
  1364. return true;
  1365. });
  1366. def(AST_Unary, function(tw, descend) {
  1367. var node = this;
  1368. if (!UNARY_POSTFIX[node.operator]) return;
  1369. var exp = node.expression;
  1370. if (!(exp instanceof AST_SymbolRef)) {
  1371. mark_assignment_to_arguments(exp);
  1372. return;
  1373. }
  1374. var d = exp.definition();
  1375. d.assignments++;
  1376. var fixed = d.fixed;
  1377. if (safe_to_read(tw, d) && !exp.in_arg && safe_to_assign(tw, d)) {
  1378. push_ref(d, exp);
  1379. mark(tw, d);
  1380. if (d.single_use) d.single_use = false;
  1381. d.fixed = function() {
  1382. return make_node(AST_Binary, node, {
  1383. operator: node.operator.slice(0, -1),
  1384. left: make_node(AST_UnaryPrefix, node, {
  1385. operator: "+",
  1386. expression: make_ref(exp, fixed),
  1387. }),
  1388. right: make_node(AST_Number, node, { value: 1 }),
  1389. });
  1390. };
  1391. d.fixed.assigns = fixed && fixed.assigns ? fixed.assigns.slice() : [];
  1392. d.fixed.assigns.push(node);
  1393. if (node instanceof AST_UnaryPrefix) {
  1394. exp.fixed = d.fixed;
  1395. } else {
  1396. exp.fixed = function() {
  1397. return make_node(AST_UnaryPrefix, node, {
  1398. operator: "+",
  1399. expression: make_ref(exp, fixed),
  1400. });
  1401. };
  1402. exp.fixed.assigns = fixed && fixed.assigns;
  1403. exp.fixed.to_prefix = replace_ref(exp, d.fixed);
  1404. }
  1405. } else {
  1406. exp.walk(tw);
  1407. d.fixed = false;
  1408. }
  1409. return true;
  1410. });
  1411. def(AST_VarDef, function(tw, descend, compressor) {
  1412. var node = this;
  1413. var value = node.value;
  1414. if (value instanceof AST_LambdaExpression && node.name instanceof AST_SymbolDeclaration) {
  1415. walk_defn();
  1416. value.parent_scope.resolve().fn_defs.push(value);
  1417. value.safe_ids = null;
  1418. var ld = node.name.definition();
  1419. if (!ld.fixed) mark_fn_def(tw, ld, value);
  1420. } else if (value) {
  1421. value.walk(tw);
  1422. walk_defn();
  1423. } else if (tw.parent() instanceof AST_Let) {
  1424. walk_defn();
  1425. }
  1426. return true;
  1427. function walk_defn() {
  1428. scan_declaration(tw, compressor, node.name, function() {
  1429. return node.value || make_node(AST_Undefined, node);
  1430. }, function(name, fixed) {
  1431. var d = name.definition();
  1432. if (fixed && safe_to_assign(tw, d, true)) {
  1433. mark(tw, d);
  1434. tw.loop_ids[d.id] = tw.in_loop;
  1435. d.fixed = fixed;
  1436. d.fixed.assigns = [ node ];
  1437. if (name instanceof AST_SymbolConst && d.redefined()
  1438. || !(can_drop_symbol(name) || is_safe_lexical(d))) {
  1439. d.single_use = false;
  1440. }
  1441. } else {
  1442. d.fixed = false;
  1443. }
  1444. });
  1445. }
  1446. });
  1447. def(AST_While, function(tw, descend) {
  1448. var save_loop = tw.in_loop;
  1449. tw.in_loop = this;
  1450. push(tw);
  1451. descend();
  1452. pop(tw);
  1453. tw.in_loop = save_loop;
  1454. return true;
  1455. });
  1456. })(function(node, func) {
  1457. node.DEFMETHOD("reduce_vars", func);
  1458. });
  1459. function reset_flags(node) {
  1460. node._squeezed = false;
  1461. node._optimized = false;
  1462. if (node instanceof AST_BlockScope) node._var_names = undefined;
  1463. if (node instanceof AST_SymbolRef) node.fixed = undefined;
  1464. }
  1465. AST_Toplevel.DEFMETHOD("reset_opt_flags", function(compressor) {
  1466. var tw = new TreeWalker(compressor.option("reduce_vars") ? function(node, descend) {
  1467. reset_flags(node);
  1468. return node.reduce_vars(tw, descend, compressor);
  1469. } : reset_flags);
  1470. // Flow control for visiting lambda definitions
  1471. tw.fn_scanning = null;
  1472. tw.fn_visited = [];
  1473. // Record the loop body in which `AST_SymbolDeclaration` is first encountered
  1474. tw.in_loop = null;
  1475. tw.loop_ids = Object.create(null);
  1476. // Stack of look-up tables to keep track of whether a `SymbolDef` has been
  1477. // properly assigned before use:
  1478. // - `push()` & `pop()` when visiting conditional branches
  1479. // - backup & restore via `save_ids` when visiting out-of-order sections
  1480. tw.safe_ids = Object.create(null);
  1481. this.walk(tw);
  1482. });
  1483. AST_Symbol.DEFMETHOD("fixed_value", function() {
  1484. var fixed = this.definition().fixed;
  1485. if (fixed) {
  1486. if (this.fixed) fixed = this.fixed;
  1487. return (fixed instanceof AST_Node ? fixed : fixed()).tail_node();
  1488. }
  1489. fixed = fixed === 0 && this.fixed;
  1490. if (!fixed) return fixed;
  1491. var value = (fixed instanceof AST_Node ? fixed : fixed()).tail_node();
  1492. return value.is_constant() && value;
  1493. });
  1494. AST_SymbolRef.DEFMETHOD("is_immutable", function() {
  1495. var def = this.redef || this.definition();
  1496. return (this.in_arg || def.orig.length == 1) && def.orig[0] instanceof AST_SymbolLambda;
  1497. });
  1498. AST_Node.DEFMETHOD("convert_symbol", noop);
  1499. function convert_destructured(type, process) {
  1500. return this.transform(new TreeTransformer(function(node, descend) {
  1501. if (node instanceof AST_DefaultValue) {
  1502. node = node.clone();
  1503. node.name = node.name.transform(this);
  1504. return node;
  1505. }
  1506. if (node instanceof AST_Destructured) {
  1507. node = node.clone();
  1508. descend(node, this);
  1509. return node;
  1510. }
  1511. if (node instanceof AST_DestructuredKeyVal) {
  1512. node = node.clone();
  1513. node.value = node.value.transform(this);
  1514. return node;
  1515. }
  1516. return node.convert_symbol(type, process);
  1517. }));
  1518. }
  1519. AST_DefaultValue.DEFMETHOD("convert_symbol", convert_destructured);
  1520. AST_Destructured.DEFMETHOD("convert_symbol", convert_destructured);
  1521. function convert_symbol(type, process) {
  1522. var node = make_node(type, this, this);
  1523. process(node, this);
  1524. return node;
  1525. }
  1526. AST_SymbolDeclaration.DEFMETHOD("convert_symbol", convert_symbol);
  1527. AST_SymbolRef.DEFMETHOD("convert_symbol", convert_symbol);
  1528. function process_to_assign(ref) {
  1529. var def = ref.definition();
  1530. def.assignments++;
  1531. def.references.push(ref);
  1532. }
  1533. function mark_destructured(process, tw) {
  1534. var marker = new TreeWalker(function(node) {
  1535. if (node instanceof AST_DefaultValue) {
  1536. node.value.walk(tw);
  1537. node.name.walk(marker);
  1538. return true;
  1539. }
  1540. if (node instanceof AST_DestructuredKeyVal) {
  1541. if (node.key instanceof AST_Node) node.key.walk(tw);
  1542. node.value.walk(marker);
  1543. return true;
  1544. }
  1545. return process(node);
  1546. });
  1547. this.walk(marker);
  1548. }
  1549. AST_DefaultValue.DEFMETHOD("mark_symbol", mark_destructured);
  1550. AST_Destructured.DEFMETHOD("mark_symbol", mark_destructured);
  1551. function mark_symbol(process) {
  1552. return process(this);
  1553. }
  1554. AST_SymbolDeclaration.DEFMETHOD("mark_symbol", mark_symbol);
  1555. AST_SymbolRef.DEFMETHOD("mark_symbol", mark_symbol);
  1556. AST_Node.DEFMETHOD("match_symbol", function(predicate) {
  1557. return predicate(this);
  1558. });
  1559. function match_destructured(predicate, ignore_side_effects) {
  1560. var found = false;
  1561. var tw = new TreeWalker(function(node) {
  1562. if (found) return true;
  1563. if (node instanceof AST_DefaultValue) {
  1564. if (!ignore_side_effects) return found = true;
  1565. node.name.walk(tw);
  1566. return true;
  1567. }
  1568. if (node instanceof AST_DestructuredKeyVal) {
  1569. if (!ignore_side_effects && node.key instanceof AST_Node) return found = true;
  1570. node.value.walk(tw);
  1571. return true;
  1572. }
  1573. if (predicate(node)) return found = true;
  1574. });
  1575. this.walk(tw);
  1576. return found;
  1577. }
  1578. AST_DefaultValue.DEFMETHOD("match_symbol", match_destructured);
  1579. AST_Destructured.DEFMETHOD("match_symbol", match_destructured);
  1580. function in_async_generator(scope) {
  1581. return scope instanceof AST_AsyncGeneratorDefun || scope instanceof AST_AsyncGeneratorFunction;
  1582. }
  1583. function find_scope(compressor) {
  1584. var level = 0, node;
  1585. while (node = compressor.parent(level++)) {
  1586. if (node.variables) return node;
  1587. }
  1588. }
  1589. var identifier_atom = makePredicate("Infinity NaN undefined");
  1590. function is_lhs_read_only(lhs, compressor) {
  1591. if (lhs instanceof AST_ObjectIdentity) return true;
  1592. if (lhs instanceof AST_PropAccess) {
  1593. if (lhs.property === "__proto__") return true;
  1594. lhs = lhs.expression;
  1595. if (lhs instanceof AST_SymbolRef) {
  1596. if (lhs.is_immutable()) return false;
  1597. lhs = lhs.fixed_value();
  1598. }
  1599. if (!lhs) return true;
  1600. if (lhs.tail_node().is_constant()) return true;
  1601. return is_lhs_read_only(lhs, compressor);
  1602. }
  1603. if (lhs instanceof AST_SymbolRef) {
  1604. if (lhs.is_immutable()) return true;
  1605. var def = lhs.definition();
  1606. return compressor.exposed(def) && identifier_atom[def.name];
  1607. }
  1608. return false;
  1609. }
  1610. function make_node(ctor, orig, props) {
  1611. if (!props) props = {};
  1612. if (orig) {
  1613. if (!props.start) props.start = orig.start;
  1614. if (!props.end) props.end = orig.end;
  1615. }
  1616. return new ctor(props);
  1617. }
  1618. function make_sequence(orig, expressions) {
  1619. if (expressions.length == 1) return expressions[0];
  1620. return make_node(AST_Sequence, orig, {
  1621. expressions: expressions.reduce(merge_sequence, [])
  1622. });
  1623. }
  1624. function make_node_from_constant(val, orig) {
  1625. switch (typeof val) {
  1626. case "string":
  1627. return make_node(AST_String, orig, { value: val });
  1628. case "number":
  1629. if (isNaN(val)) return make_node(AST_NaN, orig);
  1630. if (isFinite(val)) {
  1631. return 1 / val < 0 ? make_node(AST_UnaryPrefix, orig, {
  1632. operator: "-",
  1633. expression: make_node(AST_Number, orig, { value: -val })
  1634. }) : make_node(AST_Number, orig, { value: val });
  1635. }
  1636. return val < 0 ? make_node(AST_UnaryPrefix, orig, {
  1637. operator: "-",
  1638. expression: make_node(AST_Infinity, orig),
  1639. }) : make_node(AST_Infinity, orig);
  1640. case "boolean":
  1641. return make_node(val ? AST_True : AST_False, orig);
  1642. case "undefined":
  1643. return make_node(AST_Undefined, orig);
  1644. default:
  1645. if (val === null) {
  1646. return make_node(AST_Null, orig);
  1647. }
  1648. if (val instanceof RegExp) {
  1649. return make_node(AST_RegExp, orig, { value: val });
  1650. }
  1651. throw new Error(string_template("Can't handle constant of type: {type}", {
  1652. type: typeof val
  1653. }));
  1654. }
  1655. }
  1656. function needs_unbinding(compressor, val) {
  1657. return val instanceof AST_PropAccess
  1658. || is_undeclared_ref(val) && val.name == "eval";
  1659. }
  1660. // we shouldn't compress (1,func)(something) to
  1661. // func(something) because that changes the meaning of
  1662. // the func (becomes lexical instead of global).
  1663. function maintain_this_binding(compressor, parent, orig, val) {
  1664. var wrap = false;
  1665. if (parent.TYPE == "Call") {
  1666. wrap = parent.expression === orig && needs_unbinding(compressor, val);
  1667. } else if (parent instanceof AST_Template) {
  1668. wrap = parent.tag === orig && needs_unbinding(compressor, val);
  1669. } else if (parent instanceof AST_UnaryPrefix) {
  1670. wrap = parent.operator == "delete"
  1671. || parent.operator == "typeof" && is_undeclared_ref(val);
  1672. }
  1673. return wrap ? make_sequence(orig, [ make_node(AST_Number, orig, { value: 0 }), val ]) : val;
  1674. }
  1675. function merge_sequence(array, node) {
  1676. if (node instanceof AST_Sequence) {
  1677. [].push.apply(array, node.expressions);
  1678. } else {
  1679. array.push(node);
  1680. }
  1681. return array;
  1682. }
  1683. function is_lexical_definition(stat) {
  1684. return stat instanceof AST_Const || stat instanceof AST_DefClass || stat instanceof AST_Let;
  1685. }
  1686. function safe_to_trim(stat) {
  1687. if (stat instanceof AST_LambdaDefinition) {
  1688. var def = stat.name.definition();
  1689. var scope = stat.name.scope;
  1690. return def.scope === scope || all(def.references, function(ref) {
  1691. var s = ref.scope;
  1692. do {
  1693. if (s === scope) return true;
  1694. } while (s = s.parent_scope);
  1695. });
  1696. }
  1697. return !is_lexical_definition(stat);
  1698. }
  1699. function as_statement_array(thing) {
  1700. if (thing === null) return [];
  1701. if (thing instanceof AST_BlockStatement) return all(thing.body, safe_to_trim) ? thing.body : [ thing ];
  1702. if (thing instanceof AST_EmptyStatement) return [];
  1703. if (is_statement(thing)) return [ thing ];
  1704. throw new Error("Can't convert thing to statement array");
  1705. }
  1706. function is_empty(thing) {
  1707. if (thing === null) return true;
  1708. if (thing instanceof AST_EmptyStatement) return true;
  1709. if (thing instanceof AST_BlockStatement) return thing.body.length == 0;
  1710. return false;
  1711. }
  1712. function has_declarations_only(block) {
  1713. return all(block.body, function(stat) {
  1714. return is_empty(stat)
  1715. || stat instanceof AST_Defun
  1716. || stat instanceof AST_Var && declarations_only(stat);
  1717. });
  1718. }
  1719. function loop_body(x) {
  1720. if (x instanceof AST_IterationStatement) {
  1721. return x.body instanceof AST_BlockStatement ? x.body : x;
  1722. }
  1723. return x;
  1724. }
  1725. function is_iife_call(node) {
  1726. if (node.TYPE != "Call") return false;
  1727. do {
  1728. node = node.expression;
  1729. } while (node instanceof AST_PropAccess);
  1730. return node instanceof AST_LambdaExpression ? !is_arrow(node) : is_iife_call(node);
  1731. }
  1732. function is_iife_single(call) {
  1733. var exp = call.expression;
  1734. if (exp.name) return false;
  1735. if (!(call instanceof AST_New)) return true;
  1736. var found = false;
  1737. exp.walk(new TreeWalker(function(node) {
  1738. if (found) return true;
  1739. if (node instanceof AST_NewTarget) return found = true;
  1740. if (node instanceof AST_Scope && node !== exp) return true;
  1741. }));
  1742. return !found;
  1743. }
  1744. function is_undeclared_ref(node) {
  1745. return node instanceof AST_SymbolRef && node.definition().undeclared;
  1746. }
  1747. var global_names = makePredicate("Array Boolean clearInterval clearTimeout console Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error escape eval EvalError Function isFinite isNaN JSON Map Math Number parseFloat parseInt RangeError ReferenceError RegExp Object Set setInterval setTimeout String SyntaxError TypeError unescape URIError WeakMap WeakSet");
  1748. AST_SymbolRef.DEFMETHOD("is_declared", function(compressor) {
  1749. return this.defined
  1750. || !this.definition().undeclared
  1751. || compressor.option("unsafe") && global_names[this.name];
  1752. });
  1753. function declarations_only(node) {
  1754. return all(node.definitions, function(var_def) {
  1755. return !var_def.value;
  1756. });
  1757. }
  1758. function is_declaration(stat, lexical) {
  1759. if (stat instanceof AST_DefClass) return lexical && !stat.extends && all(stat.properties, function(prop) {
  1760. if (prop.key instanceof AST_Node) return false;
  1761. if (prop instanceof AST_ClassField && prop.static && prop.value) return false;
  1762. return true;
  1763. });
  1764. if (stat instanceof AST_Definitions) return (lexical || stat instanceof AST_Var) && declarations_only(stat);
  1765. if (stat instanceof AST_ExportDeclaration) return is_declaration(stat.body, lexical);
  1766. if (stat instanceof AST_ExportDefault) return is_declaration(stat.body, lexical);
  1767. return stat instanceof AST_LambdaDefinition;
  1768. }
  1769. function is_last_statement(body, stat) {
  1770. var index = body.lastIndexOf(stat);
  1771. if (index < 0) return false;
  1772. while (++index < body.length) {
  1773. if (!is_declaration(body[index], true)) return false;
  1774. }
  1775. return true;
  1776. }
  1777. // Certain combination of unused name + side effect leads to invalid AST:
  1778. // https://github.com/mishoo/UglifyJS/issues/44
  1779. // https://github.com/mishoo/UglifyJS/issues/1838
  1780. // https://github.com/mishoo/UglifyJS/issues/3371
  1781. // We fix it at this stage by moving the `var` outside the `for`.
  1782. function patch_for_init(node, in_list) {
  1783. var block;
  1784. if (node.init instanceof AST_BlockStatement) {
  1785. block = node.init;
  1786. node.init = block.body.pop();
  1787. block.body.push(node);
  1788. }
  1789. if (node.init instanceof AST_Defun) {
  1790. if (!block) block = make_node(AST_BlockStatement, node, { body: [ node ] });
  1791. block.body.splice(-1, 0, node.init);
  1792. node.init = null;
  1793. } else if (node.init instanceof AST_SimpleStatement) {
  1794. node.init = node.init.body;
  1795. } else if (is_empty(node.init)) {
  1796. node.init = null;
  1797. }
  1798. if (!block) return;
  1799. return in_list ? List.splice(block.body) : block;
  1800. }
  1801. function tighten_body(statements, compressor) {
  1802. var in_lambda = last_of(compressor, function(node) {
  1803. return node instanceof AST_Lambda;
  1804. });
  1805. var block_scope, in_loop, in_try, scope;
  1806. find_loop_scope_try();
  1807. var changed, last_changed, max_iter = 10;
  1808. do {
  1809. last_changed = changed;
  1810. changed = 0;
  1811. if (eliminate_spurious_blocks(statements)) changed = 1;
  1812. if (!changed && last_changed == 1) break;
  1813. if (compressor.option("dead_code")) {
  1814. if (eliminate_dead_code(statements, compressor)) changed = 2;
  1815. if (!changed && last_changed == 2) break;
  1816. }
  1817. if (compressor.option("if_return")) {
  1818. if (handle_if_return(statements, compressor)) changed = 3;
  1819. if (!changed && last_changed == 3) break;
  1820. }
  1821. if (compressor.option("awaits") && compressor.option("side_effects")) {
  1822. if (trim_awaits(statements, compressor)) changed = 4;
  1823. if (!changed && last_changed == 4) break;
  1824. }
  1825. if (compressor.option("inline") >= 4) {
  1826. if (inline_iife(statements, compressor)) changed = 5;
  1827. if (!changed && last_changed == 5) break;
  1828. }
  1829. if (compressor.sequences_limit > 0) {
  1830. if (sequencesize(statements, compressor)) changed = 6;
  1831. if (!changed && last_changed == 6) break;
  1832. if (sequencesize_2(statements, compressor)) changed = 7;
  1833. if (!changed && last_changed == 7) break;
  1834. }
  1835. if (compressor.option("join_vars")) {
  1836. if (join_consecutive_vars(statements)) changed = 8;
  1837. if (!changed && last_changed == 8) break;
  1838. }
  1839. if (compressor.option("collapse_vars")) {
  1840. if (collapse(statements, compressor)) changed = 9;
  1841. }
  1842. } while (changed && max_iter-- > 0);
  1843. return statements;
  1844. function last_of(compressor, predicate) {
  1845. var block = compressor.self(), stat, level = 0;
  1846. do {
  1847. do {
  1848. if (predicate(block)) return true;
  1849. block = compressor.parent(level++);
  1850. } while (block instanceof AST_If && (stat = block));
  1851. } while ((block instanceof AST_BlockStatement || block instanceof AST_Scope)
  1852. && is_last_statement(block.body, stat));
  1853. }
  1854. function find_loop_scope_try() {
  1855. var node = compressor.self(), level = 0;
  1856. do {
  1857. if (!block_scope && node.variables) block_scope = node;
  1858. if (node instanceof AST_Catch) {
  1859. if (compressor.parent(level).bfinally) {
  1860. if (!in_try) in_try = {};
  1861. in_try.bfinally = true;
  1862. }
  1863. level++;
  1864. } else if (node instanceof AST_Finally) {
  1865. level++;
  1866. } else if (node instanceof AST_IterationStatement) {
  1867. in_loop = true;
  1868. } else if (node instanceof AST_Scope) {
  1869. scope = node;
  1870. break;
  1871. } else if (node instanceof AST_Try) {
  1872. if (!in_try) in_try = {};
  1873. if (node.bcatch) in_try.bcatch = true;
  1874. if (node.bfinally) in_try.bfinally = true;
  1875. }
  1876. } while (node = compressor.parent(level++));
  1877. }
  1878. // Search from right to left for assignment-like expressions:
  1879. // - `var a = x;`
  1880. // - `a = x;`
  1881. // - `++a`
  1882. // For each candidate, scan from left to right for first usage, then try
  1883. // to fold assignment into the site for compression.
  1884. // Will not attempt to collapse assignments into or past code blocks
  1885. // which are not sequentially executed, e.g. loops and conditionals.
  1886. function collapse(statements, compressor) {
  1887. if (scope.pinned()) return;
  1888. var args;
  1889. var assignments = new Dictionary();
  1890. var candidates = [];
  1891. var changed = false;
  1892. var declare_only = new Dictionary();
  1893. var force_single;
  1894. var stat_index = statements.length;
  1895. var scanner = new TreeTransformer(function(node, descend) {
  1896. if (abort) return node;
  1897. // Skip nodes before `candidate` as quickly as possible
  1898. if (!hit) {
  1899. if (node !== hit_stack[hit_index]) return node;
  1900. hit_index++;
  1901. if (hit_index < hit_stack.length) return handle_custom_scan_order(node, scanner);
  1902. hit = true;
  1903. stop_after = (value_def ? find_stop_value : find_stop)(node, 0);
  1904. if (stop_after === node) abort = true;
  1905. return node;
  1906. }
  1907. var parent = scanner.parent();
  1908. // Stop only if candidate is found within conditional branches
  1909. if (!stop_if_hit && in_conditional(node, parent)) {
  1910. stop_if_hit = parent;
  1911. }
  1912. // Cascade compound assignments
  1913. if (compound && scan_lhs && can_replace && !stop_if_hit
  1914. && node instanceof AST_Assign && node.operator != "=" && node.left.equivalent_to(lhs)) {
  1915. replaced++;
  1916. changed = true;
  1917. AST_Node.info("Cascading {node} [{file}:{line},{col}]", {
  1918. node: node,
  1919. file: node.start.file,
  1920. line: node.start.line,
  1921. col: node.start.col,
  1922. });
  1923. can_replace = false;
  1924. lvalues = get_lvalues(lhs);
  1925. node.right.transform(scanner);
  1926. clear_write_only(candidate);
  1927. var folded;
  1928. if (abort) {
  1929. folded = candidate;
  1930. } else {
  1931. abort = true;
  1932. folded = make_node(AST_Binary, candidate, {
  1933. operator: compound,
  1934. left: lhs.fixed && lhs.definition().fixed ? lhs.fixed.to_binary() : lhs,
  1935. right: rvalue,
  1936. });
  1937. }
  1938. return make_node(AST_Assign, node, {
  1939. operator: "=",
  1940. left: node.left,
  1941. right: make_node(AST_Binary, node, {
  1942. operator: node.operator.slice(0, -1),
  1943. left: folded,
  1944. right: node.right,
  1945. }),
  1946. });
  1947. }
  1948. // Stop immediately if these node types are encountered
  1949. if (should_stop(node, parent)) {
  1950. abort = true;
  1951. return node;
  1952. }
  1953. // Skip transient nodes caused by single-use variable replacement
  1954. if (node.single_use && parent instanceof AST_VarDef && parent.value === node) return node;
  1955. // Replace variable with assignment when found
  1956. var hit_rhs;
  1957. if (!(node instanceof AST_SymbolDeclaration)
  1958. && (scan_lhs && lhs.equivalent_to(node)
  1959. || scan_rhs && (hit_rhs = scan_rhs(node, this)))) {
  1960. if (!can_replace || stop_if_hit && (hit_rhs || !lhs_local || !replace_all)) {
  1961. if (!hit_rhs && !value_def) abort = true;
  1962. return node;
  1963. }
  1964. if (is_lhs(node, parent)) {
  1965. if (value_def && !hit_rhs) assign_used = true;
  1966. return node;
  1967. }
  1968. if (!hit_rhs && verify_ref && node.fixed !== lhs.fixed) {
  1969. abort = true;
  1970. return node;
  1971. }
  1972. if (value_def) {
  1973. if (stop_if_hit && assign_pos == 0) assign_pos = remaining - replaced;
  1974. if (!hit_rhs) replaced++;
  1975. return node;
  1976. }
  1977. replaced++;
  1978. changed = abort = true;
  1979. AST_Node.info("Collapsing {node} [{file}:{line},{col}]", {
  1980. node: node,
  1981. file: node.start.file,
  1982. line: node.start.line,
  1983. col: node.start.col,
  1984. });
  1985. if (candidate.TYPE == "Binary") {
  1986. update_symbols(candidate, node);
  1987. return make_node(AST_Assign, candidate, {
  1988. operator: "=",
  1989. left: candidate.right.left,
  1990. right: candidate.operator == "&&" ? make_node(AST_Conditional, candidate, {
  1991. condition: candidate.left,
  1992. consequent: candidate.right.right,
  1993. alternative: node,
  1994. }) : make_node(AST_Conditional, candidate, {
  1995. condition: candidate.left,
  1996. consequent: node,
  1997. alternative: candidate.right.right,
  1998. }),
  1999. });
  2000. }
  2001. if (candidate instanceof AST_UnaryPostfix) return make_node(AST_UnaryPrefix, candidate, {
  2002. operator: candidate.operator,
  2003. expression: lhs.fixed && lhs.definition().fixed ? lhs.fixed.to_prefix() : lhs,
  2004. });
  2005. if (candidate instanceof AST_UnaryPrefix) {
  2006. clear_write_only(candidate);
  2007. return candidate;
  2008. }
  2009. update_symbols(rvalue, node);
  2010. if (candidate instanceof AST_VarDef) {
  2011. var def = candidate.name.definition();
  2012. if (def.references.length - def.replaced == 1 && !compressor.exposed(def)) {
  2013. def.replaced++;
  2014. return maintain_this_binding(compressor, parent, node, rvalue);
  2015. }
  2016. return make_node(AST_Assign, candidate, {
  2017. operator: "=",
  2018. left: node,
  2019. right: rvalue,
  2020. });
  2021. }
  2022. clear_write_only(rvalue);
  2023. var assign = candidate.clone();
  2024. assign.right = rvalue;
  2025. return assign;
  2026. }
  2027. // Stop signals related to AST_SymbolRef
  2028. if (should_stop_ref(node, parent)) {
  2029. abort = true;
  2030. return node;
  2031. }
  2032. // These node types have child nodes that execute sequentially,
  2033. // but are otherwise not safe to scan into or beyond them.
  2034. if (is_last_node(node, parent) || may_throw(node)) {
  2035. stop_after = node;
  2036. if (node instanceof AST_Scope) abort = true;
  2037. }
  2038. // Scan but don't replace inside getter/setter
  2039. if (node instanceof AST_Accessor) {
  2040. var replace = can_replace;
  2041. can_replace = false;
  2042. descend(node, scanner);
  2043. can_replace = replace;
  2044. return signal_abort(node);
  2045. }
  2046. // Scan but don't replace inside destructuring expression
  2047. if (node instanceof AST_Destructured) {
  2048. var replace = can_replace;
  2049. can_replace = false;
  2050. descend(node, scanner);
  2051. can_replace = replace;
  2052. return signal_abort(node);
  2053. }
  2054. // Scan but don't replace inside default value
  2055. if (node instanceof AST_DefaultValue) {
  2056. node.name = node.name.transform(scanner);
  2057. var replace = can_replace;
  2058. can_replace = false;
  2059. node.value = node.value.transform(scanner);
  2060. can_replace = replace;
  2061. return signal_abort(node);
  2062. }
  2063. // Scan but don't replace inside block scope with colliding variable
  2064. if (node instanceof AST_BlockScope
  2065. && !(node instanceof AST_Scope)
  2066. && !(node.variables && node.variables.all(function(def) {
  2067. return !enclosed.has(def.name) && !lvalues.has(def.name);
  2068. }))) {
  2069. var replace = can_replace;
  2070. can_replace = false;
  2071. if (!handle_custom_scan_order(node, scanner)) descend(node, scanner);
  2072. can_replace = replace;
  2073. return signal_abort(node);
  2074. }
  2075. return handle_custom_scan_order(node, scanner);
  2076. }, signal_abort);
  2077. var multi_replacer = new TreeTransformer(function(node) {
  2078. if (abort) return node;
  2079. // Skip nodes before `candidate` as quickly as possible
  2080. if (!hit) {
  2081. if (node !== hit_stack[hit_index]) return node;
  2082. hit_index++;
  2083. switch (hit_stack.length - hit_index) {
  2084. case 0:
  2085. hit = true;
  2086. if (assign_used) return node;
  2087. if (node !== candidate) return node;
  2088. if (node instanceof AST_VarDef) return node;
  2089. def.replaced++;
  2090. var parent = multi_replacer.parent();
  2091. if (parent instanceof AST_Sequence && parent.tail_node() !== node) {
  2092. value_def.replaced++;
  2093. if (rvalue === rhs_value) return List.skip;
  2094. return make_sequence(rhs_value, rhs_value.expressions.slice(0, -1));
  2095. }
  2096. return rvalue;
  2097. case 1:
  2098. if (!assign_used && node.body === candidate) {
  2099. hit = true;
  2100. def.replaced++;
  2101. value_def.replaced++;
  2102. return null;
  2103. }
  2104. default:
  2105. return handle_custom_scan_order(node, multi_replacer);
  2106. }
  2107. }
  2108. // Replace variable when found
  2109. if (node instanceof AST_SymbolRef && node.definition() === def) {
  2110. if (is_lhs(node, multi_replacer.parent())) return node;
  2111. if (!--replaced) abort = true;
  2112. AST_Node.info("Replacing {node} [{file}:{line},{col}]", {
  2113. node: node,
  2114. file: node.start.file,
  2115. line: node.start.line,
  2116. col: node.start.col,
  2117. });
  2118. var ref = rvalue.clone();
  2119. ref.scope = node.scope;
  2120. ref.reference();
  2121. if (replaced == assign_pos) {
  2122. abort = true;
  2123. return make_node(AST_Assign, candidate, {
  2124. operator: "=",
  2125. left: node,
  2126. right: ref,
  2127. });
  2128. }
  2129. def.replaced++;
  2130. return ref;
  2131. }
  2132. // Skip (non-executed) functions and (leading) default case in switch statements
  2133. if (node instanceof AST_Default || node instanceof AST_Scope) return node;
  2134. }, function(node) {
  2135. return patch_sequence(node, multi_replacer);
  2136. });
  2137. while (--stat_index >= 0) {
  2138. // Treat parameters as collapsible in IIFE, i.e.
  2139. // function(a, b){ ... }(x());
  2140. // would be translated into equivalent assignments:
  2141. // var a = x(), b = undefined;
  2142. if (stat_index == 0 && compressor.option("unused")) extract_args();
  2143. // Find collapsible assignments
  2144. var hit_stack = [];
  2145. extract_candidates(statements[stat_index]);
  2146. while (candidates.length > 0) {
  2147. hit_stack = candidates.pop();
  2148. var hit_index = 0;
  2149. var candidate = hit_stack[hit_stack.length - 1];
  2150. var assign_pos = -1;
  2151. var assign_used = false;
  2152. var verify_ref = false;
  2153. var remaining;
  2154. var value_def = null;
  2155. var stop_after = null;
  2156. var stop_if_hit = null;
  2157. var lhs = get_lhs(candidate);
  2158. var side_effects = lhs && lhs.has_side_effects(compressor);
  2159. var scan_lhs = lhs && (!side_effects || lhs instanceof AST_SymbolRef)
  2160. && !is_lhs_read_only(lhs, compressor);
  2161. var scan_rhs = foldable(candidate);
  2162. if (!scan_lhs && !scan_rhs) continue;
  2163. var compound = candidate instanceof AST_Assign && candidate.operator.slice(0, -1);
  2164. var funarg = candidate.name instanceof AST_SymbolFunarg;
  2165. var may_throw = return_false;
  2166. if (candidate.may_throw(compressor)) {
  2167. if (funarg && is_async(scope)) continue;
  2168. may_throw = in_try ? function(node) {
  2169. return node.has_side_effects(compressor);
  2170. } : side_effects_external;
  2171. }
  2172. var read_toplevel = false;
  2173. var modify_toplevel = false;
  2174. // Locate symbols which may execute code outside of scanning range
  2175. var enclosed = new Dictionary();
  2176. var well_defined = true;
  2177. var lvalues = get_lvalues(candidate);
  2178. var lhs_local = is_lhs_local(lhs);
  2179. var rhs_value = get_rvalue(candidate);
  2180. var rvalue = rhs_value;
  2181. if (!side_effects) {
  2182. if (!compound && rvalue instanceof AST_Sequence) rvalue = rvalue.tail_node();
  2183. side_effects = value_has_side_effects();
  2184. }
  2185. var check_destructured = in_try || !lhs_local ? function(node) {
  2186. return node instanceof AST_Destructured;
  2187. } : return_false;
  2188. var replace_all = replace_all_symbols(candidate);
  2189. var hit = funarg;
  2190. var abort = false;
  2191. var replaced = 0;
  2192. var can_replace = !args || !hit;
  2193. if (!can_replace) {
  2194. for (var j = candidate.arg_index + 1; !abort && j < args.length; j++) {
  2195. if (args[j]) args[j].transform(scanner);
  2196. }
  2197. can_replace = true;
  2198. }
  2199. for (var i = stat_index; !abort && i < statements.length; i++) {
  2200. statements[i].transform(scanner);
  2201. }
  2202. if (value_def) {
  2203. if (!replaced || remaining > replaced + assign_used) {
  2204. candidates.push(hit_stack);
  2205. force_single = true;
  2206. continue;
  2207. }
  2208. if (replaced == assign_pos) assign_used = true;
  2209. var def = lhs.definition();
  2210. abort = false;
  2211. hit_index = 0;
  2212. hit = funarg;
  2213. for (var i = stat_index; !abort && i < statements.length; i++) {
  2214. if (!statements[i].transform(multi_replacer)) statements.splice(i--, 1);
  2215. }
  2216. replaced = candidate instanceof AST_VarDef
  2217. && candidate === hit_stack[hit_stack.length - 1]
  2218. && def.references.length == def.replaced
  2219. && !compressor.exposed(def);
  2220. value_def.last_ref = false;
  2221. value_def.single_use = false;
  2222. changed = true;
  2223. }
  2224. if (replaced) remove_candidate(candidate);
  2225. }
  2226. }
  2227. return changed;
  2228. function signal_abort(node) {
  2229. if (abort) return node;
  2230. if (stop_after === node) abort = true;
  2231. if (stop_if_hit === node) stop_if_hit = null;
  2232. return node;
  2233. }
  2234. function handle_custom_scan_order(node, tt) {
  2235. if (!(node instanceof AST_BlockScope)) {
  2236. if (!(node instanceof AST_ClassProperty && !node.static)) return;
  2237. // Skip non-static class property values
  2238. if (node.key instanceof AST_Node) node.key = node.key.transform(tt);
  2239. return node;
  2240. }
  2241. // Skip (non-executed) functions
  2242. if (node instanceof AST_Scope) return node;
  2243. // Scan object only in a for-in/of statement
  2244. if (node instanceof AST_ForEnumeration) {
  2245. node.object = node.object.transform(tt);
  2246. abort = true;
  2247. return node;
  2248. }
  2249. // Scan first case expression only in a switch statement
  2250. if (node instanceof AST_Switch) {
  2251. node.expression = node.expression.transform(tt);
  2252. for (var i = 0; !abort && i < node.body.length; i++) {
  2253. var branch = node.body[i];
  2254. if (branch instanceof AST_Case) {
  2255. if (!hit) {
  2256. if (branch !== hit_stack[hit_index]) continue;
  2257. hit_index++;
  2258. }
  2259. branch.expression = branch.expression.transform(tt);
  2260. if (!replace_all) break;
  2261. scan_rhs = false;
  2262. }
  2263. }
  2264. abort = true;
  2265. return node;
  2266. }
  2267. }
  2268. function is_direct_assignment(node, parent) {
  2269. if (parent instanceof AST_Assign) return parent.operator == "=" && parent.left === node;
  2270. if (parent instanceof AST_DefaultValue) return parent.name === node;
  2271. if (parent instanceof AST_DestructuredArray) return true;
  2272. if (parent instanceof AST_DestructuredKeyVal) return parent.value === node;
  2273. }
  2274. function should_stop(node, parent) {
  2275. if (node === rvalue) return true;
  2276. if (parent instanceof AST_For) {
  2277. if (node !== parent.init) return true;
  2278. }
  2279. if (node instanceof AST_Assign) {
  2280. return node.operator != "=" && lhs.equivalent_to(node.left);
  2281. }
  2282. if (node instanceof AST_Call) {
  2283. if (!(lhs instanceof AST_PropAccess)) return false;
  2284. if (!lhs.equivalent_to(node.expression)) return false;
  2285. return !(rvalue instanceof AST_LambdaExpression && !rvalue.contains_this());
  2286. }
  2287. if (node instanceof AST_Class) return !compressor.has_directive("use strict");
  2288. if (node instanceof AST_Debugger) return true;
  2289. if (node instanceof AST_Defun) return funarg && lhs.name === node.name.name;
  2290. if (node instanceof AST_DestructuredKeyVal) return node.key instanceof AST_Node;
  2291. if (node instanceof AST_DWLoop) return true;
  2292. if (node instanceof AST_LoopControl) return true;
  2293. if (node instanceof AST_Try) return true;
  2294. if (node instanceof AST_With) return true;
  2295. return false;
  2296. }
  2297. function should_stop_ref(node, parent) {
  2298. if (!(node instanceof AST_SymbolRef)) return false;
  2299. if (node.is_declared(compressor)) {
  2300. if (node.fixed_value()) return false;
  2301. if (can_drop_symbol(node)) {
  2302. return !(parent instanceof AST_PropAccess && parent.expression === node)
  2303. && is_arguments(node.definition());
  2304. }
  2305. } else if (is_direct_assignment(node, parent)) {
  2306. return false;
  2307. }
  2308. if (!replace_all) return true;
  2309. scan_rhs = false;
  2310. return false;
  2311. }
  2312. function in_conditional(node, parent) {
  2313. if (parent instanceof AST_Assign) return parent.left !== node && lazy_op[parent.operator.slice(0, -1)];
  2314. if (parent instanceof AST_Binary) return parent.left !== node && lazy_op[parent.operator];
  2315. if (parent instanceof AST_Call) return parent.optional && parent.expression !== node;
  2316. if (parent instanceof AST_Case) return parent.expression !== node;
  2317. if (parent instanceof AST_Conditional) return parent.condition !== node;
  2318. if (parent instanceof AST_If) return parent.condition !== node;
  2319. if (parent instanceof AST_Sub) return parent.optional && parent.expression !== node;
  2320. }
  2321. function is_last_node(node, parent) {
  2322. if (node instanceof AST_Await) return true;
  2323. if (node.TYPE == "Binary") return node.operator == "in" && !is_object(node.right);
  2324. if (node instanceof AST_Call) {
  2325. var def, fn = node.expression;
  2326. if (fn instanceof AST_SymbolRef) {
  2327. def = fn.definition();
  2328. fn = fn.fixed_value();
  2329. }
  2330. if (!(fn instanceof AST_Lambda)) return !node.is_expr_pure(compressor);
  2331. if (def && recursive_ref(compressor, def, fn)) return true;
  2332. if (fn.collapse_scanning) return false;
  2333. fn.collapse_scanning = true;
  2334. var replace = can_replace;
  2335. can_replace = false;
  2336. var after = stop_after;
  2337. var if_hit = stop_if_hit;
  2338. if (!all(fn.argnames, function(argname) {
  2339. if (argname instanceof AST_DefaultValue) {
  2340. argname.value.transform(scanner);
  2341. if (abort) return false;
  2342. argname = argname.name;
  2343. }
  2344. return !(argname instanceof AST_Destructured);
  2345. })) {
  2346. abort = true;
  2347. } else if (is_arrow(fn) && fn.value) {
  2348. fn.value.transform(scanner);
  2349. } else for (var i = 0; !abort && i < fn.body.length; i++) {
  2350. var stat = fn.body[i];
  2351. if (stat instanceof AST_Return) {
  2352. if (stat.value) stat.value.transform(scanner);
  2353. break;
  2354. }
  2355. stat.transform(scanner);
  2356. }
  2357. stop_if_hit = if_hit;
  2358. stop_after = after;
  2359. can_replace = replace;
  2360. fn.collapse_scanning = false;
  2361. if (!abort) return false;
  2362. abort = false;
  2363. return true;
  2364. }
  2365. if (node instanceof AST_Exit) {
  2366. if (in_try) {
  2367. if (in_try.bfinally) return true;
  2368. if (in_try.bcatch && node instanceof AST_Throw) return true;
  2369. }
  2370. return side_effects || lhs instanceof AST_PropAccess || may_modify(lhs);
  2371. }
  2372. if (node instanceof AST_Function) {
  2373. return compressor.option("ie") && node.name && lvalues.has(node.name.name);
  2374. }
  2375. if (node instanceof AST_ObjectIdentity) return symbol_in_lvalues(node, parent);
  2376. if (node instanceof AST_PropAccess) {
  2377. if (side_effects) return true;
  2378. var exp = node.expression;
  2379. if (exp instanceof AST_SymbolRef && is_arguments(exp.definition())) return true;
  2380. if (compressor.option("unsafe")) {
  2381. if (is_undeclared_ref(exp) && global_names[exp.name]) return false;
  2382. if (is_static_fn(exp)) return false;
  2383. }
  2384. if (!well_defined) return true;
  2385. if (value_def) return false;
  2386. if (!in_try && lhs_local) return false;
  2387. if (node.optional) return false;
  2388. return exp.may_throw_on_access(compressor);
  2389. }
  2390. if (node instanceof AST_Spread) return true;
  2391. if (node instanceof AST_SymbolRef) {
  2392. if (symbol_in_lvalues(node, parent)) return !is_direct_assignment(node, parent);
  2393. if (side_effects && may_modify(node)) return true;
  2394. var def = node.definition();
  2395. return (in_try || def.scope.resolve() !== scope) && !can_drop_symbol(node);
  2396. }
  2397. if (node instanceof AST_Template) return !node.is_expr_pure(compressor);
  2398. if (node instanceof AST_VarDef) {
  2399. if (check_destructured(node.name)) return true;
  2400. return (node.value || parent instanceof AST_Let) && node.name.match_symbol(function(node) {
  2401. return node instanceof AST_SymbolDeclaration
  2402. && (lvalues.has(node.name) || side_effects && may_modify(node));
  2403. }, true);
  2404. }
  2405. if (node instanceof AST_Yield) return true;
  2406. var sym = is_lhs(node.left, node);
  2407. if (!sym) return false;
  2408. if (sym instanceof AST_PropAccess) return true;
  2409. if (check_destructured(sym)) return true;
  2410. return sym.match_symbol(function(node) {
  2411. return node instanceof AST_SymbolRef
  2412. && (lvalues.has(node.name) || read_toplevel && compressor.exposed(node.definition()));
  2413. }, true);
  2414. }
  2415. function may_throw_destructured(node, value) {
  2416. if (!value) return !(node instanceof AST_Symbol);
  2417. if (node instanceof AST_DefaultValue) {
  2418. return value.has_side_effects(compressor)
  2419. || node.value.has_side_effects(compressor)
  2420. || may_throw_destructured(node.name, is_undefined(value) && node.value);
  2421. }
  2422. if (node instanceof AST_Destructured) {
  2423. if (node.rest && may_throw_destructured(node.rest)) return true;
  2424. if (node instanceof AST_DestructuredArray) {
  2425. if (!(value instanceof AST_Array || value.is_string(compressor))) return true;
  2426. return !all(node.elements, function(element) {
  2427. return !may_throw_destructured(element);
  2428. });
  2429. }
  2430. if (node instanceof AST_DestructuredObject) {
  2431. if (!value.is_defined(compressor)) return true;
  2432. return !all(node.properties, function(prop) {
  2433. if (prop instanceof AST_Node && prop.has_side_effects(compressor)) return false;
  2434. return !may_throw_destructured(prop.value);
  2435. });
  2436. }
  2437. }
  2438. }
  2439. function extract_args() {
  2440. var iife, fn = compressor.self();
  2441. if (fn instanceof AST_LambdaExpression
  2442. && !is_generator(fn)
  2443. && !fn.uses_arguments
  2444. && !fn.pinned()
  2445. && (iife = compressor.parent()) instanceof AST_Call
  2446. && iife.expression === fn
  2447. && is_iife_single(iife)
  2448. && all(iife.args, function(arg) {
  2449. return !(arg instanceof AST_Spread);
  2450. })) {
  2451. var fn_strict = compressor.has_directive("use strict");
  2452. if (fn_strict && !member(fn_strict, fn.body)) fn_strict = false;
  2453. var has_await = is_async(fn) ? function(node) {
  2454. return node instanceof AST_Symbol && node.name == "await";
  2455. } : function(node) {
  2456. return node instanceof AST_Await && !tw.find_parent(AST_Scope);
  2457. };
  2458. var arg_scope = null;
  2459. var tw = new TreeWalker(function(node, descend) {
  2460. if (!arg) return true;
  2461. if (has_await(node) || node instanceof AST_Yield) {
  2462. arg = null;
  2463. return true;
  2464. }
  2465. if (node instanceof AST_ObjectIdentity && (fn_strict || !arg_scope)) {
  2466. arg = null;
  2467. return true;
  2468. }
  2469. if (node instanceof AST_SymbolRef && fn.variables.has(node.name)) {
  2470. var s = node.definition().scope;
  2471. if (s !== scope) while (s = s.parent_scope) {
  2472. if (s === scope) return true;
  2473. }
  2474. arg = null;
  2475. }
  2476. if (node instanceof AST_Scope && !is_arrow(node)) {
  2477. var save_scope = arg_scope;
  2478. arg_scope = node;
  2479. descend();
  2480. arg_scope = save_scope;
  2481. return true;
  2482. }
  2483. });
  2484. args = iife.args.slice();
  2485. var len = args.length;
  2486. var names = new Dictionary();
  2487. for (var i = fn.argnames.length; --i >= 0;) {
  2488. var sym = fn.argnames[i];
  2489. var arg = args[i];
  2490. var value;
  2491. if (sym instanceof AST_DefaultValue) {
  2492. value = sym.value;
  2493. sym = sym.name;
  2494. args[len + i] = value;
  2495. }
  2496. if (sym instanceof AST_Destructured) {
  2497. if (!may_throw_destructured(sym, arg)) continue;
  2498. candidates.length = 0;
  2499. break;
  2500. }
  2501. if (names.has(sym.name)) continue;
  2502. names.set(sym.name, true);
  2503. if (value) arg = is_undefined(arg) ? value : null;
  2504. if (!arg && !value) {
  2505. arg = make_node(AST_Undefined, sym).transform(compressor);
  2506. } else if (arg instanceof AST_Lambda && arg.pinned()) {
  2507. arg = null;
  2508. } else if (arg) {
  2509. arg.walk(tw);
  2510. }
  2511. if (!arg) continue;
  2512. var candidate = make_node(AST_VarDef, sym, {
  2513. name: sym,
  2514. value: arg,
  2515. });
  2516. candidate.name_index = i;
  2517. candidate.arg_index = value ? len + i : i;
  2518. candidates.unshift([ candidate ]);
  2519. }
  2520. }
  2521. }
  2522. function extract_candidates(expr, unused) {
  2523. hit_stack.push(expr);
  2524. if (expr instanceof AST_Array) {
  2525. expr.elements.forEach(function(node) {
  2526. extract_candidates(node, unused);
  2527. });
  2528. } else if (expr instanceof AST_Assign) {
  2529. var lhs = expr.left;
  2530. if (!(lhs instanceof AST_Destructured)) candidates.push(hit_stack.slice());
  2531. extract_candidates(lhs);
  2532. extract_candidates(expr.right);
  2533. if (lhs instanceof AST_SymbolRef && expr.operator == "=") {
  2534. assignments.set(lhs.name, (assignments.get(lhs.name) || 0) + 1);
  2535. }
  2536. } else if (expr instanceof AST_Await) {
  2537. extract_candidates(expr.expression, unused);
  2538. } else if (expr instanceof AST_Binary) {
  2539. var lazy = lazy_op[expr.operator];
  2540. if (unused
  2541. && lazy
  2542. && expr.operator != "??"
  2543. && expr.right instanceof AST_Assign
  2544. && expr.right.operator == "="
  2545. && !(expr.right.left instanceof AST_Destructured)) {
  2546. candidates.push(hit_stack.slice());
  2547. }
  2548. extract_candidates(expr.left, !lazy && unused);
  2549. extract_candidates(expr.right, unused);
  2550. } else if (expr instanceof AST_Call) {
  2551. extract_candidates(expr.expression);
  2552. expr.args.forEach(extract_candidates);
  2553. } else if (expr instanceof AST_Case) {
  2554. extract_candidates(expr.expression);
  2555. } else if (expr instanceof AST_Conditional) {
  2556. extract_candidates(expr.condition);
  2557. extract_candidates(expr.consequent, unused);
  2558. extract_candidates(expr.alternative, unused);
  2559. } else if (expr instanceof AST_Definitions) {
  2560. expr.definitions.forEach(extract_candidates);
  2561. } else if (expr instanceof AST_Dot) {
  2562. extract_candidates(expr.expression);
  2563. } else if (expr instanceof AST_DWLoop) {
  2564. extract_candidates(expr.condition);
  2565. if (!(expr.body instanceof AST_Block)) {
  2566. extract_candidates(expr.body);
  2567. }
  2568. } else if (expr instanceof AST_Exit) {
  2569. if (expr.value) extract_candidates(expr.value);
  2570. } else if (expr instanceof AST_For) {
  2571. if (expr.init) extract_candidates(expr.init, true);
  2572. if (expr.condition) extract_candidates(expr.condition);
  2573. if (expr.step) extract_candidates(expr.step, true);
  2574. if (!(expr.body instanceof AST_Block)) {
  2575. extract_candidates(expr.body);
  2576. }
  2577. } else if (expr instanceof AST_ForEnumeration) {
  2578. extract_candidates(expr.object);
  2579. if (!(expr.body instanceof AST_Block)) {
  2580. extract_candidates(expr.body);
  2581. }
  2582. } else if (expr instanceof AST_If) {
  2583. extract_candidates(expr.condition);
  2584. if (!(expr.body instanceof AST_Block)) {
  2585. extract_candidates(expr.body);
  2586. }
  2587. if (expr.alternative && !(expr.alternative instanceof AST_Block)) {
  2588. extract_candidates(expr.alternative);
  2589. }
  2590. } else if (expr instanceof AST_Object) {
  2591. expr.properties.forEach(function(prop) {
  2592. hit_stack.push(prop);
  2593. if (prop.key instanceof AST_Node) extract_candidates(prop.key);
  2594. if (prop instanceof AST_ObjectKeyVal) extract_candidates(prop.value, unused);
  2595. hit_stack.pop();
  2596. });
  2597. } else if (expr instanceof AST_Sequence) {
  2598. var end = expr.expressions.length - (unused ? 0 : 1);
  2599. expr.expressions.forEach(function(node, index) {
  2600. extract_candidates(node, index < end);
  2601. });
  2602. } else if (expr instanceof AST_SimpleStatement) {
  2603. extract_candidates(expr.body, true);
  2604. } else if (expr instanceof AST_Spread) {
  2605. extract_candidates(expr.expression);
  2606. } else if (expr instanceof AST_Sub) {
  2607. extract_candidates(expr.expression);
  2608. extract_candidates(expr.property);
  2609. } else if (expr instanceof AST_Switch) {
  2610. extract_candidates(expr.expression);
  2611. expr.body.forEach(extract_candidates);
  2612. } else if (expr instanceof AST_Unary) {
  2613. if (UNARY_POSTFIX[expr.operator]) {
  2614. candidates.push(hit_stack.slice());
  2615. } else {
  2616. extract_candidates(expr.expression);
  2617. }
  2618. } else if (expr instanceof AST_VarDef) {
  2619. if (expr.name instanceof AST_SymbolVar) {
  2620. if (expr.value) {
  2621. var def = expr.name.definition();
  2622. if (def.references.length > def.replaced) {
  2623. candidates.push(hit_stack.slice());
  2624. }
  2625. } else {
  2626. declare_only.set(expr.name.name, (declare_only.get(expr.name.name) || 0) + 1);
  2627. }
  2628. }
  2629. if (expr.value) extract_candidates(expr.value);
  2630. } else if (expr instanceof AST_Yield) {
  2631. if (expr.expression) extract_candidates(expr.expression);
  2632. }
  2633. hit_stack.pop();
  2634. }
  2635. function find_stop(node, level) {
  2636. var parent = scanner.parent(level);
  2637. if (parent instanceof AST_Array) return node;
  2638. if (parent instanceof AST_Assign) return node;
  2639. if (parent instanceof AST_Await) return node;
  2640. if (parent instanceof AST_Binary) return node;
  2641. if (parent instanceof AST_Call) return node;
  2642. if (parent instanceof AST_Case) return node;
  2643. if (parent instanceof AST_Conditional) return node;
  2644. if (parent instanceof AST_Definitions) return find_stop_unused(parent, level + 1);
  2645. if (parent instanceof AST_Exit) return node;
  2646. if (parent instanceof AST_If) return node;
  2647. if (parent instanceof AST_IterationStatement) return node;
  2648. if (parent instanceof AST_ObjectProperty) return node;
  2649. if (parent instanceof AST_PropAccess) return node;
  2650. if (parent instanceof AST_Sequence) {
  2651. return (parent.tail_node() === node ? find_stop : find_stop_unused)(parent, level + 1);
  2652. }
  2653. if (parent instanceof AST_SimpleStatement) return find_stop_unused(parent, level + 1);
  2654. if (parent instanceof AST_Spread) return node;
  2655. if (parent instanceof AST_Switch) return node;
  2656. if (parent instanceof AST_Unary) return node;
  2657. if (parent instanceof AST_VarDef) return node;
  2658. if (parent instanceof AST_Yield) return node;
  2659. return null;
  2660. }
  2661. function find_stop_logical(parent, op, level) {
  2662. var node;
  2663. do {
  2664. node = parent;
  2665. parent = scanner.parent(++level);
  2666. } while (parent instanceof AST_Assign && parent.operator.slice(0, -1) == op
  2667. || parent instanceof AST_Binary && parent.operator == op);
  2668. return node;
  2669. }
  2670. function find_stop_expr(expr, cont, node, parent, level) {
  2671. var replace = can_replace;
  2672. can_replace = false;
  2673. var after = stop_after;
  2674. var if_hit = stop_if_hit;
  2675. var stack = scanner.stack;
  2676. scanner.stack = [ parent ];
  2677. expr.transform(scanner);
  2678. scanner.stack = stack;
  2679. stop_if_hit = if_hit;
  2680. stop_after = after;
  2681. can_replace = replace;
  2682. if (abort) {
  2683. abort = false;
  2684. return node;
  2685. }
  2686. return cont(parent, level + 1);
  2687. }
  2688. function find_stop_value(node, level) {
  2689. var parent = scanner.parent(level);
  2690. if (parent instanceof AST_Array) return find_stop_value(parent, level + 1);
  2691. if (parent instanceof AST_Assign) {
  2692. if (may_throw(parent)) return node;
  2693. if (parent.left.match_symbol(function(ref) {
  2694. return ref instanceof AST_SymbolRef && (lhs.name == ref.name || value_def.name == ref.name);
  2695. })) return node;
  2696. var op;
  2697. if (parent.left === node || !lazy_op[op = parent.operator.slice(0, -1)]) {
  2698. return find_stop_value(parent, level + 1);
  2699. }
  2700. return find_stop_logical(parent, op, level);
  2701. }
  2702. if (parent instanceof AST_Binary) {
  2703. var op;
  2704. if (parent.left === node || !lazy_op[op = parent.operator]) {
  2705. return find_stop_value(parent, level + 1);
  2706. }
  2707. return find_stop_logical(parent, op, level);
  2708. }
  2709. if (parent instanceof AST_Call) return parent;
  2710. if (parent instanceof AST_Case) {
  2711. if (parent.expression !== node) return node;
  2712. return find_stop_value(parent, level + 1);
  2713. }
  2714. if (parent instanceof AST_Conditional) {
  2715. if (parent.condition !== node) return node;
  2716. return find_stop_value(parent, level + 1);
  2717. }
  2718. if (parent instanceof AST_Definitions) return find_stop_unused(parent, level + 1);
  2719. if (parent instanceof AST_Do) return node;
  2720. if (parent instanceof AST_Exit) return find_stop_unused(parent, level + 1);
  2721. if (parent instanceof AST_For) {
  2722. if (parent.init !== node && parent.condition !== node) return node;
  2723. return find_stop_value(parent, level + 1);
  2724. }
  2725. if (parent instanceof AST_ForEnumeration) {
  2726. if (parent.init !== node) return node;
  2727. return find_stop_value(parent, level + 1);
  2728. }
  2729. if (parent instanceof AST_If) {
  2730. if (parent.condition !== node) return node;
  2731. return find_stop_value(parent, level + 1);
  2732. }
  2733. if (parent instanceof AST_ObjectProperty) {
  2734. var obj = scanner.parent(level + 1);
  2735. return all(obj.properties, function(prop) {
  2736. return prop instanceof AST_ObjectKeyVal;
  2737. }) ? find_stop_value(obj, level + 2) : obj;
  2738. }
  2739. if (parent instanceof AST_PropAccess) {
  2740. var exp = parent.expression;
  2741. return exp === node ? find_stop_value(parent, level + 1) : node;
  2742. }
  2743. if (parent instanceof AST_Sequence) {
  2744. return (parent.tail_node() === node ? find_stop_value : find_stop_unused)(parent, level + 1);
  2745. }
  2746. if (parent instanceof AST_SimpleStatement) return find_stop_unused(parent, level + 1);
  2747. if (parent instanceof AST_Spread) return find_stop_value(parent, level + 1);
  2748. if (parent instanceof AST_Switch) {
  2749. if (parent.expression !== node) return node;
  2750. return find_stop_value(parent, level + 1);
  2751. }
  2752. if (parent instanceof AST_Unary) {
  2753. if (parent.operator == "delete") return node;
  2754. return find_stop_value(parent, level + 1);
  2755. }
  2756. if (parent instanceof AST_VarDef) return parent.name.match_symbol(function(sym) {
  2757. return sym instanceof AST_SymbolDeclaration && (lhs.name == sym.name || value_def.name == sym.name);
  2758. }) ? node : find_stop_value(parent, level + 1);
  2759. if (parent instanceof AST_While) {
  2760. if (parent.condition !== node) return node;
  2761. return find_stop_value(parent, level + 1);
  2762. }
  2763. if (parent instanceof AST_Yield) return find_stop_value(parent, level + 1);
  2764. return null;
  2765. }
  2766. function find_stop_unused(node, level) {
  2767. var parent = scanner.parent(level);
  2768. if (is_last_node(node, parent)) return node;
  2769. if (in_conditional(node, parent)) return node;
  2770. if (parent instanceof AST_Array) return find_stop_unused(parent, level + 1);
  2771. if (parent instanceof AST_Assign) return check_assignment(parent.left);
  2772. if (parent instanceof AST_Await) return node;
  2773. if (parent instanceof AST_Binary) return find_stop_unused(parent, level + 1);
  2774. if (parent instanceof AST_Call) return find_stop_unused(parent, level + 1);
  2775. if (parent instanceof AST_Case) return find_stop_unused(parent, level + 1);
  2776. if (parent instanceof AST_Conditional) return find_stop_unused(parent, level + 1);
  2777. if (parent instanceof AST_Definitions) return find_stop_unused(parent, level + 1);
  2778. if (parent instanceof AST_Exit) return find_stop_unused(parent, level + 1);
  2779. if (parent instanceof AST_If) return find_stop_unused(parent, level + 1);
  2780. if (parent instanceof AST_IterationStatement) return node;
  2781. if (parent instanceof AST_ObjectProperty) {
  2782. var obj = scanner.parent(level + 1);
  2783. return all(obj.properties, function(prop) {
  2784. return prop instanceof AST_ObjectKeyVal;
  2785. }) ? find_stop_unused(obj, level + 2) : obj;
  2786. }
  2787. if (parent instanceof AST_PropAccess) {
  2788. var exp = parent.expression;
  2789. if (exp === node) return find_stop_unused(parent, level + 1);
  2790. return find_stop_expr(exp, find_stop_unused, node, parent, level);
  2791. }
  2792. if (parent instanceof AST_Sequence) return find_stop_unused(parent, level + 1);
  2793. if (parent instanceof AST_SimpleStatement) return find_stop_unused(parent, level + 1);
  2794. if (parent instanceof AST_Spread) return node;
  2795. if (parent instanceof AST_Switch) return find_stop_unused(parent, level + 1);
  2796. if (parent instanceof AST_Unary) return find_stop_unused(parent, level + 1);
  2797. if (parent instanceof AST_VarDef) return check_assignment(parent.name);
  2798. if (parent instanceof AST_Yield) return node;
  2799. return null;
  2800. function check_assignment(lhs) {
  2801. if (may_throw(parent)) return node;
  2802. if (lhs !== node && lhs instanceof AST_Destructured) {
  2803. return find_stop_expr(lhs, find_stop_unused, node, parent, level);
  2804. }
  2805. return find_stop_unused(parent, level + 1);
  2806. }
  2807. }
  2808. function mangleable_var(rhs) {
  2809. if (force_single) {
  2810. force_single = false;
  2811. return;
  2812. }
  2813. if (remaining < 1) return;
  2814. rhs = rhs.tail_node();
  2815. var value = rhs instanceof AST_Assign && rhs.operator == "=" ? rhs.left : rhs;
  2816. if (!(value instanceof AST_SymbolRef)) return;
  2817. var def = value.definition();
  2818. if (def.undeclared) return;
  2819. if (is_arguments(def)) return;
  2820. if (value !== rhs) {
  2821. if (is_lhs_read_only(value, compressor)) return;
  2822. var referenced = def.references.length - def.replaced;
  2823. if (referenced < 2) return;
  2824. var expr = candidate.clone();
  2825. expr[expr instanceof AST_Assign ? "right" : "value"] = value;
  2826. if (candidate.name_index >= 0) {
  2827. expr.name_index = candidate.name_index;
  2828. expr.arg_index = candidate.arg_index;
  2829. }
  2830. candidate = expr;
  2831. }
  2832. return value_def = def;
  2833. }
  2834. function remaining_refs(def) {
  2835. return def.references.length - def.replaced - (assignments.get(def.name) || 0);
  2836. }
  2837. function get_lhs(expr) {
  2838. if (expr instanceof AST_Assign) {
  2839. var lhs = expr.left;
  2840. if (!(lhs instanceof AST_SymbolRef)) return lhs;
  2841. var def = lhs.definition();
  2842. if (scope.uses_arguments && is_funarg(def)) return lhs;
  2843. if (compressor.exposed(def)) return lhs;
  2844. remaining = remaining_refs(def);
  2845. if (def.fixed && lhs.fixed) {
  2846. var matches = def.references.filter(function(ref) {
  2847. return ref.fixed === lhs.fixed;
  2848. }).length - 1;
  2849. if (matches < remaining) {
  2850. remaining = matches;
  2851. assign_pos = 0;
  2852. verify_ref = true;
  2853. }
  2854. }
  2855. if (expr.operator == "=") mangleable_var(expr.right);
  2856. return lhs;
  2857. }
  2858. if (expr instanceof AST_Binary) return expr.right.left;
  2859. if (expr instanceof AST_Unary) return expr.expression;
  2860. if (expr instanceof AST_VarDef) {
  2861. var lhs = expr.name;
  2862. var def = lhs.definition();
  2863. if (def.const_redefs) return;
  2864. if (!member(lhs, def.orig)) return;
  2865. if (scope.uses_arguments && is_funarg(def)) return;
  2866. var declared = def.orig.length - def.eliminated - (declare_only.get(def.name) || 0);
  2867. remaining = remaining_refs(def);
  2868. if (def.fixed) remaining = Math.min(remaining, def.references.filter(function(ref) {
  2869. if (!ref.fixed) return true;
  2870. if (!ref.fixed.assigns) return true;
  2871. var assign = ref.fixed.assigns[0];
  2872. return assign === lhs || get_rvalue(assign) === expr.value;
  2873. }).length);
  2874. if (declared > 1 && !(lhs instanceof AST_SymbolFunarg)) {
  2875. mangleable_var(expr.value);
  2876. return make_node(AST_SymbolRef, lhs, lhs);
  2877. }
  2878. if (mangleable_var(expr.value) || remaining == 1 && !compressor.exposed(def)) {
  2879. return make_node(AST_SymbolRef, lhs, lhs);
  2880. }
  2881. return;
  2882. }
  2883. }
  2884. function get_rvalue(expr) {
  2885. if (expr instanceof AST_Assign) return expr.right;
  2886. if (expr instanceof AST_Binary) {
  2887. var node = expr.clone();
  2888. node.right = expr.right.right;
  2889. return node;
  2890. }
  2891. if (expr instanceof AST_VarDef) return expr.value;
  2892. }
  2893. function invariant(expr) {
  2894. if (expr instanceof AST_Array) return false;
  2895. if (expr instanceof AST_Binary && lazy_op[expr.operator]) {
  2896. return invariant(expr.left) && invariant(expr.right);
  2897. }
  2898. if (expr instanceof AST_Call) return false;
  2899. if (expr instanceof AST_Conditional) {
  2900. return invariant(expr.consequent) && invariant(expr.alternative);
  2901. }
  2902. if (expr instanceof AST_Object) return false;
  2903. return !expr.has_side_effects(compressor);
  2904. }
  2905. function foldable(expr) {
  2906. if (expr instanceof AST_Assign && expr.right.single_use) return;
  2907. var lhs_ids = Object.create(null);
  2908. var marker = new TreeWalker(function(node) {
  2909. if (node instanceof AST_SymbolRef) lhs_ids[node.definition().id] = true;
  2910. });
  2911. while (expr instanceof AST_Assign && expr.operator == "=") {
  2912. expr.left.walk(marker);
  2913. expr = expr.right;
  2914. }
  2915. if (expr instanceof AST_ObjectIdentity) return rhs_exact_match;
  2916. if (expr instanceof AST_SymbolRef) {
  2917. var value = expr.evaluate(compressor);
  2918. if (value === expr) return rhs_exact_match;
  2919. return rhs_fuzzy_match(value, rhs_exact_match);
  2920. }
  2921. if (expr.is_truthy()) return rhs_fuzzy_match(true, return_false);
  2922. if (expr.is_constant()) {
  2923. var ev = expr.evaluate(compressor);
  2924. if (!(ev instanceof AST_Node)) return rhs_fuzzy_match(ev, rhs_exact_match);
  2925. }
  2926. if (!(lhs instanceof AST_SymbolRef)) return false;
  2927. if (!invariant(expr)) return false;
  2928. var circular;
  2929. expr.walk(new TreeWalker(function(node) {
  2930. if (circular) return true;
  2931. if (node instanceof AST_SymbolRef && lhs_ids[node.definition().id]) circular = true;
  2932. }));
  2933. return !circular && rhs_exact_match;
  2934. function rhs_exact_match(node) {
  2935. return expr.equivalent_to(node);
  2936. }
  2937. }
  2938. function rhs_fuzzy_match(value, fallback) {
  2939. return function(node, tw) {
  2940. if (tw.in_boolean_context()) {
  2941. if (value && node.is_truthy() && !node.has_side_effects(compressor)) {
  2942. return true;
  2943. }
  2944. if (node.is_constant()) {
  2945. var ev = node.evaluate(compressor);
  2946. if (!(ev instanceof AST_Node)) return !ev == !value;
  2947. }
  2948. }
  2949. return fallback(node);
  2950. };
  2951. }
  2952. function clear_write_only(assign) {
  2953. while (assign.write_only) {
  2954. assign.write_only = false;
  2955. if (!(assign instanceof AST_Assign)) break;
  2956. assign = assign.right;
  2957. }
  2958. }
  2959. function update_symbols(value, node) {
  2960. var scope = node.scope || find_scope(scanner) || block_scope;
  2961. value.walk(new TreeWalker(function(node) {
  2962. if (node instanceof AST_BlockScope) return true;
  2963. if (node instanceof AST_Symbol) node.scope = scope;
  2964. }));
  2965. }
  2966. function may_be_global(node) {
  2967. if (node instanceof AST_SymbolRef) {
  2968. node = node.fixed_value();
  2969. if (!node) return true;
  2970. }
  2971. if (node instanceof AST_Assign) return node.operator == "=" && may_be_global(node.right);
  2972. return node instanceof AST_PropAccess || node instanceof AST_ObjectIdentity;
  2973. }
  2974. function get_lvalues(expr) {
  2975. var lvalues = new Dictionary();
  2976. if (expr instanceof AST_VarDef) {
  2977. if (!expr.name.definition().fixed) well_defined = false;
  2978. lvalues.add(expr.name.name, lhs);
  2979. }
  2980. var find_arguments = scope.uses_arguments && !compressor.has_directive("use strict");
  2981. var scan_toplevel = scope instanceof AST_Toplevel;
  2982. var tw = new TreeWalker(function(node) {
  2983. var value;
  2984. if (node instanceof AST_SymbolRef) {
  2985. value = node.fixed_value();
  2986. if (!value) {
  2987. value = node;
  2988. var def = node.definition();
  2989. var escaped = node.fixed && node.fixed.escaped || def.escaped;
  2990. if (!def.undeclared
  2991. && (def.assignments || !escaped || escaped.cross_scope)
  2992. && (has_escaped(def, node.scope, node, tw.parent()) || !same_scope(def))) {
  2993. well_defined = false;
  2994. }
  2995. }
  2996. } else if (node instanceof AST_ObjectIdentity) {
  2997. value = node;
  2998. }
  2999. if (value) {
  3000. lvalues.add(node.name, is_modified(compressor, tw, node, value, 0));
  3001. } else if (node instanceof AST_Lambda) {
  3002. for (var level = 0, parent, child = node; parent = tw.parent(level++); child = parent) {
  3003. if (parent instanceof AST_Assign) {
  3004. if (parent.left === child) break;
  3005. if (parent.operator == "=") continue;
  3006. if (lazy_op[parent.operator.slice(0, -1)]) continue;
  3007. break;
  3008. }
  3009. if (parent instanceof AST_Binary) {
  3010. if (lazy_op[parent.operator]) continue;
  3011. break;
  3012. }
  3013. if (parent instanceof AST_Call) return;
  3014. if (parent instanceof AST_Scope) return;
  3015. if (parent instanceof AST_Sequence) {
  3016. if (parent.tail_node() === child) continue;
  3017. break;
  3018. }
  3019. if (parent instanceof AST_Template) {
  3020. if (parent.tag) return;
  3021. break;
  3022. }
  3023. }
  3024. node.enclosed.forEach(function(def) {
  3025. if (def.scope !== node) enclosed.set(def.name, true);
  3026. });
  3027. return true;
  3028. } else if (find_arguments && node instanceof AST_Sub) {
  3029. scope.each_argname(function(argname) {
  3030. if (!compressor.option("reduce_vars") || argname.definition().assignments) {
  3031. if (!argname.definition().fixed) well_defined = false;
  3032. lvalues.add(argname.name, true);
  3033. }
  3034. });
  3035. find_arguments = false;
  3036. }
  3037. if (!scan_toplevel) return;
  3038. if (node.TYPE == "Call") {
  3039. if (modify_toplevel) return;
  3040. var exp = node.expression;
  3041. if (exp instanceof AST_PropAccess) return;
  3042. if (exp instanceof AST_LambdaExpression && !exp.contains_this()) return;
  3043. modify_toplevel = true;
  3044. } else if (node instanceof AST_PropAccess && may_be_global(node.expression)) {
  3045. if (node === lhs && !(expr instanceof AST_Unary)) {
  3046. modify_toplevel = true;
  3047. } else {
  3048. read_toplevel = true;
  3049. }
  3050. }
  3051. });
  3052. expr.walk(tw);
  3053. return lvalues;
  3054. }
  3055. function remove_candidate(expr) {
  3056. var value = rvalue === rhs_value ? null : make_sequence(rhs_value, rhs_value.expressions.slice(0, -1));
  3057. var index = expr.name_index;
  3058. if (index >= 0) {
  3059. var args, argname = scope.argnames[index];
  3060. if (argname instanceof AST_DefaultValue) {
  3061. scope.argnames[index] = argname = argname.clone();
  3062. argname.value = value || make_node(AST_Number, argname, { value: 0 });
  3063. } else if ((args = compressor.parent().args)[index]) {
  3064. scope.argnames[index] = argname.clone();
  3065. args[index] = value || make_node(AST_Number, args[index], { value: 0 });
  3066. }
  3067. return;
  3068. }
  3069. var end = hit_stack.length - 1;
  3070. var last = hit_stack[end];
  3071. if (last instanceof AST_VarDef || hit_stack[end - 1].body === last) end--;
  3072. var tt = new TreeTransformer(function(node, descend, in_list) {
  3073. if (hit) return node;
  3074. if (node !== hit_stack[hit_index]) return node;
  3075. hit_index++;
  3076. if (hit_index <= end) return handle_custom_scan_order(node, tt);
  3077. hit = true;
  3078. if (node instanceof AST_Definitions) {
  3079. declare_only.set(last.name.name, (declare_only.get(last.name.name) || 0) + 1);
  3080. if (value_def) value_def.replaced++;
  3081. var defns = node.definitions;
  3082. var index = defns.indexOf(last);
  3083. var defn = last.clone();
  3084. defn.value = null;
  3085. if (!value) {
  3086. node.definitions[index] = defn;
  3087. return node;
  3088. }
  3089. var body = [ make_node(AST_SimpleStatement, value, { body: value }) ];
  3090. if (index > 0) {
  3091. var head = node.clone();
  3092. head.definitions = defns.slice(0, index);
  3093. body.unshift(head);
  3094. node = node.clone();
  3095. node.definitions = defns.slice(index);
  3096. }
  3097. body.push(node);
  3098. node.definitions[0] = defn;
  3099. return in_list ? List.splice(body) : make_node(AST_BlockStatement, node, { body: body });
  3100. }
  3101. if (!value) return in_list ? List.skip : null;
  3102. return is_statement(node) ? make_node(AST_SimpleStatement, value, { body: value }) : value;
  3103. }, function(node, in_list) {
  3104. if (node instanceof AST_For) return patch_for_init(node, in_list);
  3105. return patch_sequence(node, tt);
  3106. });
  3107. abort = false;
  3108. hit = false;
  3109. hit_index = 0;
  3110. if (!(statements[stat_index] = statements[stat_index].transform(tt))) statements.splice(stat_index, 1);
  3111. }
  3112. function patch_sequence(node, tt) {
  3113. if (node instanceof AST_Sequence) switch (node.expressions.length) {
  3114. case 0: return null;
  3115. case 1: return maintain_this_binding(compressor, tt.parent(), node, node.expressions[0]);
  3116. }
  3117. }
  3118. function is_lhs_local(lhs) {
  3119. var sym = root_expr(lhs);
  3120. if (!(sym instanceof AST_SymbolRef)) return false;
  3121. if (sym.definition().scope.resolve() !== scope) return false;
  3122. if (!in_loop) return true;
  3123. if (compound) return false;
  3124. if (candidate instanceof AST_Unary) return false;
  3125. var lvalue = lvalues.get(sym.name);
  3126. return !lvalue || lvalue[0] === lhs;
  3127. }
  3128. function value_has_side_effects() {
  3129. if (candidate instanceof AST_Unary) return false;
  3130. return rvalue.has_side_effects(compressor);
  3131. }
  3132. function replace_all_symbols(expr) {
  3133. if (expr instanceof AST_Unary) return false;
  3134. if (side_effects) return false;
  3135. if (value_def) return true;
  3136. if (!(lhs instanceof AST_SymbolRef)) return false;
  3137. var referenced;
  3138. if (expr instanceof AST_VarDef) {
  3139. referenced = 1;
  3140. } else if (expr.operator == "=") {
  3141. referenced = 2;
  3142. } else {
  3143. return false;
  3144. }
  3145. var def = lhs.definition();
  3146. if (def.references.length - def.replaced == referenced) return true;
  3147. if (!def.fixed) return false;
  3148. if (!lhs.fixed) return false;
  3149. if (def.references.filter(function(ref) {
  3150. return ref.fixed === lhs.fixed;
  3151. }).length != referenced) return false;
  3152. verify_ref = true;
  3153. return true;
  3154. }
  3155. function symbol_in_lvalues(sym, parent) {
  3156. var lvalue = lvalues.get(sym.name);
  3157. if (!lvalue || all(lvalue, function(lhs) {
  3158. return !lhs;
  3159. })) return;
  3160. if (lvalue[0] !== lhs) return true;
  3161. scan_rhs = false;
  3162. }
  3163. function may_modify(sym) {
  3164. var def = sym.definition();
  3165. if (def.orig.length == 1 && def.orig[0] instanceof AST_SymbolDefun) return false;
  3166. if (def.scope.resolve() !== scope) return true;
  3167. if (modify_toplevel && compressor.exposed(def)) return true;
  3168. return !all(def.references, function(ref) {
  3169. return ref.scope.resolve(true) === scope;
  3170. });
  3171. }
  3172. function side_effects_external(node, lhs) {
  3173. if (node instanceof AST_Assign) return side_effects_external(node.left, true);
  3174. if (node instanceof AST_Unary) return side_effects_external(node.expression, true);
  3175. if (node instanceof AST_VarDef) return node.value && side_effects_external(node.value);
  3176. if (lhs) {
  3177. if (node instanceof AST_Dot) return side_effects_external(node.expression, true);
  3178. if (node instanceof AST_Sub) return side_effects_external(node.expression, true);
  3179. if (node instanceof AST_SymbolRef) return node.definition().scope.resolve() !== scope;
  3180. }
  3181. return false;
  3182. }
  3183. }
  3184. function eliminate_spurious_blocks(statements) {
  3185. var changed = false, seen_dirs = [];
  3186. for (var i = 0; i < statements.length;) {
  3187. var stat = statements[i];
  3188. if (stat instanceof AST_BlockStatement) {
  3189. if (all(stat.body, safe_to_trim)) {
  3190. changed = true;
  3191. eliminate_spurious_blocks(stat.body);
  3192. [].splice.apply(statements, [i, 1].concat(stat.body));
  3193. i += stat.body.length;
  3194. continue;
  3195. }
  3196. }
  3197. if (stat instanceof AST_Directive) {
  3198. if (member(stat.value, seen_dirs)) {
  3199. changed = true;
  3200. statements.splice(i, 1);
  3201. continue;
  3202. }
  3203. seen_dirs.push(stat.value);
  3204. }
  3205. if (stat instanceof AST_EmptyStatement) {
  3206. changed = true;
  3207. statements.splice(i, 1);
  3208. continue;
  3209. }
  3210. i++;
  3211. }
  3212. return changed;
  3213. }
  3214. function handle_if_return(statements, compressor) {
  3215. var changed = false;
  3216. var parent = compressor.parent();
  3217. var self = compressor.self();
  3218. var in_iife = in_lambda && parent && parent.TYPE == "Call" && parent.expression === self;
  3219. var chain_if_returns = in_lambda && compressor.option("conditionals") && compressor.option("sequences");
  3220. var multiple_if_returns = has_multiple_if_returns(statements);
  3221. for (var i = statements.length; --i >= 0;) {
  3222. var stat = statements[i];
  3223. var j = next_index(i);
  3224. var next = statements[j];
  3225. if (in_lambda && !next && stat instanceof AST_Return) {
  3226. if (!stat.value) {
  3227. changed = true;
  3228. statements.splice(i, 1);
  3229. continue;
  3230. }
  3231. var tail = stat.value.tail_node();
  3232. if (tail instanceof AST_UnaryPrefix && tail.operator == "void") {
  3233. changed = true;
  3234. var body;
  3235. if (tail === stat.value) {
  3236. body = tail.expression;
  3237. } else {
  3238. body = stat.value.clone();
  3239. body.expressions[body.length - 1] = tail.expression;
  3240. }
  3241. statements[i] = make_node(AST_SimpleStatement, stat, { body: body });
  3242. continue;
  3243. }
  3244. }
  3245. if (stat instanceof AST_If) {
  3246. var ab = aborts(stat.body);
  3247. if (can_merge_flow(ab)) {
  3248. if (ab.label) remove(ab.label.thedef.references, ab);
  3249. changed = true;
  3250. stat = stat.clone();
  3251. stat.body = make_node(AST_BlockStatement, stat, {
  3252. body: as_statement_array_with_return(stat.body, ab),
  3253. });
  3254. stat.alternative = make_node(AST_BlockStatement, stat, {
  3255. body: as_statement_array(stat.alternative).concat(extract_functions()),
  3256. });
  3257. statements[i] = stat;
  3258. statements[i] = stat.transform(compressor);
  3259. continue;
  3260. }
  3261. if (ab && !stat.alternative && next instanceof AST_Jump) {
  3262. var cond = stat.condition;
  3263. cond = best_of_expression(cond, cond.negate(compressor), stat.body instanceof AST_BlockStatement);
  3264. if (cond !== stat.condition) {
  3265. changed = true;
  3266. stat = stat.clone();
  3267. stat.condition = cond;
  3268. statements[j] = stat.body;
  3269. stat.body = next;
  3270. statements[i] = stat;
  3271. statements[i] = stat.transform(compressor);
  3272. continue;
  3273. }
  3274. }
  3275. var alt = aborts(stat.alternative);
  3276. if (can_merge_flow(alt)) {
  3277. if (alt.label) remove(alt.label.thedef.references, alt);
  3278. changed = true;
  3279. stat = stat.clone();
  3280. stat.body = make_node(AST_BlockStatement, stat.body, {
  3281. body: as_statement_array(stat.body).concat(extract_functions()),
  3282. });
  3283. stat.alternative = make_node(AST_BlockStatement, stat.alternative, {
  3284. body: as_statement_array_with_return(stat.alternative, alt),
  3285. });
  3286. statements[i] = stat;
  3287. statements[i] = stat.transform(compressor);
  3288. continue;
  3289. }
  3290. if (compressor.option("typeofs")) {
  3291. if (ab && !alt) {
  3292. var stats = make_node(AST_BlockStatement, self, { body: statements.slice(i + 1) });
  3293. mark_locally_defined(stat.condition, null, stats);
  3294. }
  3295. if (!ab && alt) {
  3296. var stats = make_node(AST_BlockStatement, self, { body: statements.slice(i + 1) });
  3297. mark_locally_defined(stat.condition, stats);
  3298. }
  3299. }
  3300. }
  3301. if (stat instanceof AST_If && stat.body instanceof AST_Return) {
  3302. var value = stat.body.value;
  3303. var in_bool = stat.body.in_bool || next instanceof AST_Return && next.in_bool;
  3304. //---
  3305. // if (foo()) return x; return y; ---> return foo() ? x : y;
  3306. if (!stat.alternative && next instanceof AST_Return) {
  3307. changed = true;
  3308. stat = stat.clone();
  3309. stat.alternative = next;
  3310. statements.splice(i, 1, stat.transform(compressor));
  3311. statements.splice(j, 1);
  3312. continue;
  3313. }
  3314. //---
  3315. // if (foo()) return x; [ return ; ] ---> return foo() ? x : undefined;
  3316. if (!stat.alternative && !next && in_lambda && (in_bool || value && multiple_if_returns)) {
  3317. changed = true;
  3318. stat = stat.clone();
  3319. stat.alternative = make_node(AST_Return, stat, { value: null });
  3320. statements.splice(i, 1, stat.transform(compressor));
  3321. continue;
  3322. }
  3323. //---
  3324. // if (a) return b; if (c) return d; e; ---> return a ? b : c ? d : void e;
  3325. //
  3326. // if sequences is not enabled, this can lead to an endless loop (issue #866).
  3327. // however, with sequences on this helps producing slightly better output for
  3328. // the example code.
  3329. var prev, prev_stat;
  3330. if (chain_if_returns && !stat.alternative
  3331. && (!(prev_stat = statements[prev = prev_index(i)]) && in_iife
  3332. || prev_stat instanceof AST_If && prev_stat.body instanceof AST_Return)
  3333. && next_index(j) == statements.length && next instanceof AST_SimpleStatement) {
  3334. changed = true;
  3335. var exprs = [];
  3336. var args = prev_stat ? trim_defns(prev, exprs) : [ i, 1 ];
  3337. stat = stat.clone();
  3338. exprs.push(stat.condition);
  3339. stat.condition = make_sequence(stat, exprs);
  3340. stat.alternative = make_node(AST_BlockStatement, next, {
  3341. body: [
  3342. next,
  3343. make_node(AST_Return, next, { value: null }),
  3344. ],
  3345. });
  3346. args.push(stat.transform(compressor));
  3347. statements.splice(j, 1);
  3348. [].splice.apply(statements, args);
  3349. i = prev + 1;
  3350. continue;
  3351. }
  3352. }
  3353. }
  3354. return changed;
  3355. function has_multiple_if_returns(statements) {
  3356. var n = 0;
  3357. for (var i = statements.length; --i >= 0;) {
  3358. var stat = statements[i];
  3359. if (stat instanceof AST_If && stat.body instanceof AST_Return) {
  3360. if (++n > 1) return true;
  3361. }
  3362. }
  3363. return false;
  3364. }
  3365. function match_target(target) {
  3366. return last_of(compressor, function(node) {
  3367. return node === target;
  3368. });
  3369. }
  3370. function can_drop_abort(ab) {
  3371. if (ab instanceof AST_Return) return in_lambda && is_undefined(ab.value);
  3372. if (!(ab instanceof AST_LoopControl)) return false;
  3373. var lct = compressor.loopcontrol_target(ab);
  3374. if (ab instanceof AST_Continue) return match_target(loop_body(lct));
  3375. if (lct instanceof AST_IterationStatement) return false;
  3376. return match_target(lct);
  3377. }
  3378. function can_merge_flow(ab) {
  3379. if (!can_drop_abort(ab)) return false;
  3380. for (var j = statements.length; --j > i;) {
  3381. var stat = statements[j];
  3382. if (stat instanceof AST_DefClass) {
  3383. if (stat.name.definition().preinit) return false;
  3384. } else if (stat instanceof AST_Const || stat instanceof AST_Let) {
  3385. if (!all(stat.definitions, function(defn) {
  3386. return !defn.name.match_symbol(function(node) {
  3387. return node instanceof AST_SymbolDeclaration && node.definition().preinit;
  3388. });
  3389. })) return false;
  3390. }
  3391. }
  3392. return true;
  3393. }
  3394. function extract_functions() {
  3395. var defuns = [];
  3396. var lexical = false;
  3397. var tail = statements.splice(i + 1).filter(function(stat) {
  3398. if (stat instanceof AST_LambdaDefinition) {
  3399. defuns.push(stat);
  3400. return false;
  3401. }
  3402. if (is_lexical_definition(stat)) lexical = true;
  3403. return true;
  3404. });
  3405. [].push.apply(lexical ? tail : statements, defuns);
  3406. return tail;
  3407. }
  3408. function as_statement_array_with_return(node, ab) {
  3409. var body = as_statement_array(node);
  3410. var block = body, last;
  3411. while ((last = block[block.length - 1]) !== ab) {
  3412. block = last.body;
  3413. }
  3414. block.pop();
  3415. if (ab.value) block.push(make_node(AST_SimpleStatement, ab.value, { body: ab.value }));
  3416. return body;
  3417. }
  3418. function next_index(i) {
  3419. for (var j = i + 1; j < statements.length; j++) {
  3420. if (!is_declaration(statements[j])) break;
  3421. }
  3422. return j;
  3423. }
  3424. function prev_index(i) {
  3425. for (var j = i; --j >= 0;) {
  3426. if (!(statements[j] instanceof AST_Var)) break;
  3427. }
  3428. return j;
  3429. }
  3430. function trim_defns(j, exprs) {
  3431. var args = [ j + 1, i - j ];
  3432. var var_defs = [];
  3433. while (++j < i) {
  3434. var stat = statements[j];
  3435. stat.remove_initializers(compressor, var_defs);
  3436. stat.definitions.forEach(function(var_def) {
  3437. if (!var_def.value) return;
  3438. exprs.push(make_node(AST_Assign, var_def, {
  3439. operator: "=",
  3440. left: var_def.name.convert_symbol(AST_SymbolRef, process_to_assign),
  3441. right: var_def.value,
  3442. }));
  3443. });
  3444. }
  3445. if (var_defs.length > 0) args.push(make_node(AST_Var, stat, { definitions: var_defs }));
  3446. return args;
  3447. }
  3448. }
  3449. function eliminate_dead_code(statements, compressor) {
  3450. var has_quit;
  3451. var self = compressor.self();
  3452. for (var i = 0, n = 0, len = statements.length; i < len; i++) {
  3453. var stat = statements[i];
  3454. if (stat instanceof AST_LoopControl) {
  3455. var lct = compressor.loopcontrol_target(stat);
  3456. if (loop_body(lct) !== self
  3457. || stat instanceof AST_Break && lct instanceof AST_IterationStatement) {
  3458. statements[n++] = stat;
  3459. } else if (stat.label) {
  3460. remove(stat.label.thedef.references, stat);
  3461. }
  3462. } else {
  3463. statements[n++] = stat;
  3464. }
  3465. if (aborts(stat)) {
  3466. has_quit = statements.slice(i + 1);
  3467. break;
  3468. }
  3469. }
  3470. statements.length = n;
  3471. if (has_quit) has_quit.forEach(function(stat) {
  3472. extract_declarations_from_unreachable_code(compressor, stat, statements);
  3473. });
  3474. return statements.length != len;
  3475. }
  3476. function trim_awaits(statements, compressor) {
  3477. if (!in_lambda || in_try && in_try.bfinally) return;
  3478. var changed = false;
  3479. for (var index = statements.length; --index >= 0;) {
  3480. var stat = statements[index];
  3481. if (!(stat instanceof AST_SimpleStatement)) break;
  3482. var node = stat.body;
  3483. if (!(node instanceof AST_Await)) break;
  3484. var exp = node.expression;
  3485. if (!is_primitive(compressor, exp)) break;
  3486. changed = true;
  3487. exp = exp.drop_side_effect_free(compressor, true);
  3488. if (exp) {
  3489. stat.body = exp;
  3490. break;
  3491. }
  3492. }
  3493. statements.length = index + 1;
  3494. return changed;
  3495. }
  3496. function inline_iife(statements, compressor) {
  3497. var changed = false;
  3498. var index = statements.length - 1;
  3499. if (in_lambda && index >= 0) {
  3500. var inlined = statements[index].try_inline(compressor, block_scope);
  3501. if (inlined) {
  3502. statements[index--] = inlined;
  3503. changed = true;
  3504. }
  3505. }
  3506. var loop = in_loop && in_try && in_try.bfinally ? "try" : in_loop;
  3507. for (; index >= 0; index--) {
  3508. var inlined = statements[index].try_inline(compressor, block_scope, true, loop);
  3509. if (!inlined) continue;
  3510. statements[index] = inlined;
  3511. changed = true;
  3512. }
  3513. return changed;
  3514. }
  3515. function sequencesize(statements, compressor) {
  3516. if (statements.length < 2) return;
  3517. var seq = [], n = 0;
  3518. function push_seq() {
  3519. if (!seq.length) return;
  3520. var body = make_sequence(seq[0], seq);
  3521. statements[n++] = make_node(AST_SimpleStatement, body, { body: body });
  3522. seq = [];
  3523. }
  3524. for (var i = 0, len = statements.length; i < len; i++) {
  3525. var stat = statements[i];
  3526. if (stat instanceof AST_SimpleStatement) {
  3527. if (seq.length >= compressor.sequences_limit) push_seq();
  3528. merge_sequence(seq, stat.body);
  3529. } else if (is_declaration(stat)) {
  3530. statements[n++] = stat;
  3531. } else {
  3532. push_seq();
  3533. statements[n++] = stat;
  3534. }
  3535. }
  3536. push_seq();
  3537. statements.length = n;
  3538. return n != len;
  3539. }
  3540. function to_simple_statement(block, decls) {
  3541. if (!(block instanceof AST_BlockStatement)) return block;
  3542. var stat = null;
  3543. for (var i = 0; i < block.body.length; i++) {
  3544. var line = block.body[i];
  3545. if (line instanceof AST_Var && declarations_only(line)) {
  3546. decls.push(line);
  3547. } else if (stat || is_lexical_definition(line)) {
  3548. return false;
  3549. } else {
  3550. stat = line;
  3551. }
  3552. }
  3553. return stat;
  3554. }
  3555. function sequencesize_2(statements, compressor) {
  3556. var changed = false, n = 0, prev;
  3557. for (var i = 0; i < statements.length; i++) {
  3558. var stat = statements[i];
  3559. if (prev) {
  3560. if (stat instanceof AST_Exit) {
  3561. if (stat.value || !in_async_generator(scope)) {
  3562. stat.value = cons_seq(stat.value || make_node(AST_Undefined, stat)).optimize(compressor);
  3563. }
  3564. } else if (stat instanceof AST_For) {
  3565. if (!(stat.init instanceof AST_Definitions)) {
  3566. var abort = false;
  3567. prev.body.walk(new TreeWalker(function(node) {
  3568. if (abort || node instanceof AST_Scope) return true;
  3569. if (node instanceof AST_Binary && node.operator == "in") {
  3570. abort = true;
  3571. return true;
  3572. }
  3573. }));
  3574. if (!abort) {
  3575. if (stat.init) stat.init = cons_seq(stat.init);
  3576. else {
  3577. stat.init = prev.body;
  3578. n--;
  3579. changed = true;
  3580. }
  3581. }
  3582. }
  3583. } else if (stat instanceof AST_ForIn) {
  3584. if (!is_lexical_definition(stat.init)) stat.object = cons_seq(stat.object);
  3585. } else if (stat instanceof AST_If) {
  3586. stat.condition = cons_seq(stat.condition);
  3587. } else if (stat instanceof AST_Switch) {
  3588. stat.expression = cons_seq(stat.expression);
  3589. } else if (stat instanceof AST_With) {
  3590. stat.expression = cons_seq(stat.expression);
  3591. }
  3592. }
  3593. if (compressor.option("conditionals") && stat instanceof AST_If) {
  3594. var decls = [];
  3595. var body = to_simple_statement(stat.body, decls);
  3596. var alt = to_simple_statement(stat.alternative, decls);
  3597. if (body !== false && alt !== false && decls.length > 0) {
  3598. var len = decls.length;
  3599. decls.push(make_node(AST_If, stat, {
  3600. condition: stat.condition,
  3601. body: body || make_node(AST_EmptyStatement, stat.body),
  3602. alternative: alt
  3603. }));
  3604. decls.unshift(n, 1);
  3605. [].splice.apply(statements, decls);
  3606. i += len;
  3607. n += len + 1;
  3608. prev = null;
  3609. changed = true;
  3610. continue;
  3611. }
  3612. }
  3613. statements[n++] = stat;
  3614. prev = stat instanceof AST_SimpleStatement ? stat : null;
  3615. }
  3616. statements.length = n;
  3617. return changed;
  3618. function cons_seq(right) {
  3619. n--;
  3620. changed = true;
  3621. var left = prev.body;
  3622. return make_sequence(left, [ left, right ]);
  3623. }
  3624. }
  3625. function extract_exprs(body) {
  3626. if (body instanceof AST_Assign) return [ body ];
  3627. if (body instanceof AST_Sequence) return body.expressions.slice();
  3628. }
  3629. function join_assigns(defn, body, keep) {
  3630. var exprs = extract_exprs(body);
  3631. if (!exprs) return;
  3632. keep = keep || 0;
  3633. var trimmed = false;
  3634. for (var i = exprs.length - keep; --i >= 0;) {
  3635. var expr = exprs[i];
  3636. if (!can_trim(expr)) continue;
  3637. var tail;
  3638. if (expr.left instanceof AST_SymbolRef) {
  3639. tail = exprs.slice(i + 1);
  3640. } else if (expr.left instanceof AST_PropAccess && can_trim(expr.left.expression)) {
  3641. tail = exprs.slice(i + 1);
  3642. var flattened = expr.clone();
  3643. expr = expr.left.expression;
  3644. flattened.left = flattened.left.clone();
  3645. flattened.left.expression = expr.left.clone();
  3646. tail.unshift(flattened);
  3647. } else {
  3648. continue;
  3649. }
  3650. if (tail.length == 0) continue;
  3651. if (!trim_assigns(expr.left, expr.right, tail)) continue;
  3652. trimmed = true;
  3653. exprs = exprs.slice(0, i).concat(expr, tail);
  3654. }
  3655. if (defn instanceof AST_Definitions) {
  3656. for (var i = defn.definitions.length; --i >= 0;) {
  3657. var def = defn.definitions[i];
  3658. if (!def.value) continue;
  3659. if (trim_assigns(def.name, def.value, exprs)) trimmed = true;
  3660. if (merge_conditional_assignments(def, exprs, keep)) trimmed = true;
  3661. break;
  3662. }
  3663. if (defn instanceof AST_Var && join_var_assign(defn.definitions, exprs, keep)) trimmed = true;
  3664. }
  3665. return trimmed && exprs;
  3666. function can_trim(node) {
  3667. return node instanceof AST_Assign && node.operator == "=";
  3668. }
  3669. }
  3670. function merge_assigns(prev, defn) {
  3671. if (!(prev instanceof AST_SimpleStatement)) return;
  3672. if (declarations_only(defn)) return;
  3673. var exprs = extract_exprs(prev.body);
  3674. if (!exprs) return;
  3675. var definitions = [];
  3676. if (!join_var_assign(definitions, exprs.reverse(), 0)) return;
  3677. defn.definitions = definitions.reverse().concat(defn.definitions);
  3678. return exprs.reverse();
  3679. }
  3680. function merge_conditional_assignments(var_def, exprs, keep) {
  3681. if (!compressor.option("conditionals")) return;
  3682. if (var_def.name instanceof AST_Destructured) return;
  3683. var trimmed = false;
  3684. var def = var_def.name.definition();
  3685. while (exprs.length > keep) {
  3686. var cond = to_conditional_assignment(compressor, def, var_def.value, exprs[0]);
  3687. if (!cond) break;
  3688. var_def.value = cond;
  3689. exprs.shift();
  3690. trimmed = true;
  3691. }
  3692. return trimmed;
  3693. }
  3694. function join_var_assign(definitions, exprs, keep) {
  3695. var trimmed = false;
  3696. while (exprs.length > keep) {
  3697. var expr = exprs[0];
  3698. if (!(expr instanceof AST_Assign)) break;
  3699. if (expr.operator != "=") break;
  3700. var lhs = expr.left;
  3701. if (!(lhs instanceof AST_SymbolRef)) break;
  3702. if (is_undeclared_ref(lhs)) break;
  3703. if (lhs.scope.resolve() !== scope) break;
  3704. var def = lhs.definition();
  3705. if (def.scope !== scope) break;
  3706. if (def.orig.length > def.eliminated + 1) break;
  3707. if (def.orig[0].TYPE != "SymbolVar") break;
  3708. var name = make_node(AST_SymbolVar, lhs, lhs);
  3709. definitions.push(make_node(AST_VarDef, expr, {
  3710. name: name,
  3711. value: expr.right
  3712. }));
  3713. def.orig.push(name);
  3714. def.replaced++;
  3715. exprs.shift();
  3716. trimmed = true;
  3717. }
  3718. return trimmed;
  3719. }
  3720. function trim_assigns(name, value, exprs) {
  3721. var names = new Dictionary();
  3722. names.set(name.name, true);
  3723. while (value instanceof AST_Assign && value.operator == "=") {
  3724. if (value.left instanceof AST_SymbolRef) names.set(value.left.name, true);
  3725. value = value.right;
  3726. }
  3727. if (!(value instanceof AST_Object)) return;
  3728. var trimmed = false;
  3729. do {
  3730. if (!try_join(exprs[0])) break;
  3731. exprs.shift();
  3732. trimmed = true;
  3733. } while (exprs.length);
  3734. return trimmed;
  3735. function try_join(node) {
  3736. if (!(node instanceof AST_Assign)) return;
  3737. if (node.operator != "=") return;
  3738. if (!(node.left instanceof AST_PropAccess)) return;
  3739. var sym = node.left.expression;
  3740. if (!(sym instanceof AST_SymbolRef)) return;
  3741. if (!names.has(sym.name)) return;
  3742. if (!node.right.is_constant_expression(scope)) return;
  3743. var prop = node.left.property;
  3744. if (prop instanceof AST_Node) {
  3745. if (try_join(prop)) prop = node.left.property = prop.right.clone();
  3746. prop = prop.evaluate(compressor);
  3747. }
  3748. if (prop instanceof AST_Node) return;
  3749. prop = "" + prop;
  3750. var diff = prop == "__proto__" || compressor.has_directive("use strict") ? function(node) {
  3751. var key = node.key;
  3752. return typeof key == "string" && key != prop && key != "__proto__";
  3753. } : function(node) {
  3754. var key = node.key;
  3755. if (node instanceof AST_ObjectGetter || node instanceof AST_ObjectSetter) {
  3756. return typeof key == "string" && key != prop;
  3757. }
  3758. return key !== "__proto__";
  3759. };
  3760. if (!all(value.properties, diff)) return;
  3761. value.properties.push(make_node(AST_ObjectKeyVal, node, {
  3762. key: prop,
  3763. value: node.right,
  3764. }));
  3765. return true;
  3766. }
  3767. }
  3768. function join_consecutive_vars(statements) {
  3769. var changed = false, defs;
  3770. for (var i = 0, j = -1; i < statements.length; i++) {
  3771. var stat = statements[i];
  3772. var prev = statements[j];
  3773. if (stat instanceof AST_Definitions) {
  3774. if (prev && prev.TYPE == stat.TYPE) {
  3775. prev.definitions = prev.definitions.concat(stat.definitions);
  3776. changed = true;
  3777. } else if (defs && defs.TYPE == stat.TYPE && declarations_only(stat)) {
  3778. defs.definitions = defs.definitions.concat(stat.definitions);
  3779. changed = true;
  3780. } else if (stat instanceof AST_Var) {
  3781. var exprs = merge_assigns(prev, stat);
  3782. if (exprs) {
  3783. if (exprs.length) {
  3784. prev.body = make_sequence(prev, exprs);
  3785. j++;
  3786. }
  3787. changed = true;
  3788. } else {
  3789. j++;
  3790. }
  3791. statements[j] = defs = stat;
  3792. } else {
  3793. statements[++j] = stat;
  3794. }
  3795. continue;
  3796. } else if (stat instanceof AST_Exit) {
  3797. stat.value = join_assigns_expr(stat.value);
  3798. } else if (stat instanceof AST_For) {
  3799. var exprs = join_assigns(prev, stat.init);
  3800. if (exprs) {
  3801. changed = true;
  3802. stat.init = exprs.length ? make_sequence(stat.init, exprs) : null;
  3803. } else if (prev instanceof AST_Var && (!stat.init || stat.init.TYPE == prev.TYPE)) {
  3804. if (stat.init) {
  3805. prev.definitions = prev.definitions.concat(stat.init.definitions);
  3806. }
  3807. stat = stat.clone();
  3808. defs = stat.init = prev;
  3809. statements[j] = merge_defns(stat);
  3810. changed = true;
  3811. continue;
  3812. } else if (defs && stat.init && defs.TYPE == stat.init.TYPE && declarations_only(stat.init)) {
  3813. defs.definitions = defs.definitions.concat(stat.init.definitions);
  3814. stat.init = null;
  3815. changed = true;
  3816. } else if (stat.init instanceof AST_Var) {
  3817. defs = stat.init;
  3818. exprs = merge_assigns(prev, stat.init);
  3819. if (exprs) {
  3820. changed = true;
  3821. if (exprs.length == 0) {
  3822. statements[j] = merge_defns(stat);
  3823. continue;
  3824. }
  3825. prev.body = make_sequence(prev, exprs);
  3826. }
  3827. }
  3828. } else if (stat instanceof AST_ForEnumeration) {
  3829. if (defs && defs.TYPE == stat.init.TYPE) {
  3830. var defns = defs.definitions.slice();
  3831. stat.init = stat.init.definitions[0].name.convert_symbol(AST_SymbolRef, function(ref, name) {
  3832. defns.push(make_node(AST_VarDef, name, {
  3833. name: name,
  3834. value: null,
  3835. }));
  3836. name.definition().references.push(ref);
  3837. });
  3838. defs.definitions = defns;
  3839. changed = true;
  3840. }
  3841. stat.object = join_assigns_expr(stat.object);
  3842. } else if (stat instanceof AST_If) {
  3843. stat.condition = join_assigns_expr(stat.condition);
  3844. } else if (stat instanceof AST_SimpleStatement) {
  3845. var exprs = join_assigns(prev, stat.body), next;
  3846. if (exprs) {
  3847. changed = true;
  3848. if (!exprs.length) continue;
  3849. stat.body = make_sequence(stat.body, exprs);
  3850. } else if (prev instanceof AST_Definitions
  3851. && (next = statements[i + 1])
  3852. && prev.TYPE == next.TYPE
  3853. && (next = next.definitions[0]).value) {
  3854. changed = true;
  3855. next.value = make_sequence(stat, [ stat.body, next.value ]);
  3856. continue;
  3857. }
  3858. } else if (stat instanceof AST_Switch) {
  3859. stat.expression = join_assigns_expr(stat.expression);
  3860. } else if (stat instanceof AST_With) {
  3861. stat.expression = join_assigns_expr(stat.expression);
  3862. }
  3863. statements[++j] = defs ? merge_defns(stat) : stat;
  3864. }
  3865. statements.length = j + 1;
  3866. return changed;
  3867. function join_assigns_expr(value) {
  3868. var exprs = join_assigns(prev, value, 1);
  3869. if (!exprs) return value;
  3870. changed = true;
  3871. var tail = value.tail_node();
  3872. if (exprs[exprs.length - 1] !== tail) exprs.push(tail.left);
  3873. return make_sequence(value, exprs);
  3874. }
  3875. function merge_defns(stat) {
  3876. return stat.transform(new TreeTransformer(function(node, descend, in_list) {
  3877. if (node instanceof AST_Definitions) {
  3878. if (defs === node) return node;
  3879. if (defs.TYPE != node.TYPE) return node;
  3880. var parent = this.parent();
  3881. if (parent instanceof AST_ForEnumeration && parent.init === node) return node;
  3882. if (!declarations_only(node)) return node;
  3883. defs.definitions = defs.definitions.concat(node.definitions);
  3884. changed = true;
  3885. if (parent instanceof AST_For && parent.init === node) return null;
  3886. return in_list ? List.skip : make_node(AST_EmptyStatement, node);
  3887. }
  3888. if (node instanceof AST_ExportDeclaration) return node;
  3889. if (node instanceof AST_Scope) return node;
  3890. if (!is_statement(node)) return node;
  3891. }));
  3892. }
  3893. }
  3894. }
  3895. function extract_declarations_from_unreachable_code(compressor, stat, target) {
  3896. var block;
  3897. var dropped = false;
  3898. stat.walk(new TreeWalker(function(node, descend) {
  3899. if (node instanceof AST_DefClass) {
  3900. node.extends = null;
  3901. node.properties = [];
  3902. push(node);
  3903. return true;
  3904. }
  3905. if (node instanceof AST_Definitions) {
  3906. var defns = [];
  3907. if (node.remove_initializers(compressor, defns)) {
  3908. AST_Node.warn("Dropping initialization in unreachable code [{file}:{line},{col}]", node.start);
  3909. }
  3910. if (defns.length > 0) {
  3911. node.definitions = defns;
  3912. push(node);
  3913. }
  3914. return true;
  3915. }
  3916. if (node instanceof AST_LambdaDefinition) {
  3917. push(node);
  3918. return true;
  3919. }
  3920. if (node instanceof AST_Scope) return true;
  3921. if (node instanceof AST_BlockScope) {
  3922. var save = block;
  3923. block = [];
  3924. descend();
  3925. if (block.required) {
  3926. target.push(make_node(AST_BlockStatement, stat, { body: block }));
  3927. } else if (block.length) {
  3928. [].push.apply(target, block);
  3929. }
  3930. block = save;
  3931. return true;
  3932. }
  3933. if (!(node instanceof AST_LoopControl)) dropped = true;
  3934. }));
  3935. if (dropped) AST_Node.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start);
  3936. function push(node) {
  3937. if (block) {
  3938. block.push(node);
  3939. if (!safe_to_trim(node)) block.required = true;
  3940. } else {
  3941. target.push(node);
  3942. }
  3943. }
  3944. }
  3945. function is_undefined(node, compressor) {
  3946. return node == null
  3947. || node.is_undefined
  3948. || node instanceof AST_Undefined
  3949. || node instanceof AST_UnaryPrefix
  3950. && node.operator == "void"
  3951. && !(compressor && node.expression.has_side_effects(compressor));
  3952. }
  3953. // is_truthy()
  3954. // return true if `!!node === true`
  3955. (function(def) {
  3956. def(AST_Node, return_false);
  3957. def(AST_Array, return_true);
  3958. def(AST_Assign, function() {
  3959. return this.operator == "=" && this.right.is_truthy();
  3960. });
  3961. def(AST_Lambda, return_true);
  3962. def(AST_Object, return_true);
  3963. def(AST_RegExp, return_true);
  3964. def(AST_Sequence, function() {
  3965. return this.tail_node().is_truthy();
  3966. });
  3967. def(AST_SymbolRef, function() {
  3968. var fixed = this.fixed_value();
  3969. if (!fixed) return false;
  3970. this.is_truthy = return_false;
  3971. var result = fixed.is_truthy();
  3972. delete this.is_truthy;
  3973. return result;
  3974. });
  3975. })(function(node, func) {
  3976. node.DEFMETHOD("is_truthy", func);
  3977. });
  3978. // is_negative_zero()
  3979. // return true if the node may represent -0
  3980. (function(def) {
  3981. def(AST_Node, return_true);
  3982. def(AST_Array, return_false);
  3983. function binary(op, left, right) {
  3984. switch (op) {
  3985. case "-":
  3986. return left.is_negative_zero()
  3987. && (!(right instanceof AST_Constant) || right.value == 0);
  3988. case "&&":
  3989. case "||":
  3990. return left.is_negative_zero() || right.is_negative_zero();
  3991. case "*":
  3992. case "/":
  3993. case "%":
  3994. case "**":
  3995. return true;
  3996. default:
  3997. return false;
  3998. }
  3999. }
  4000. def(AST_Assign, function() {
  4001. var op = this.operator;
  4002. if (op == "=") return this.right.is_negative_zero();
  4003. return binary(op.slice(0, -1), this.left, this.right);
  4004. });
  4005. def(AST_Binary, function() {
  4006. return binary(this.operator, this.left, this.right);
  4007. });
  4008. def(AST_Constant, function() {
  4009. return this.value == 0 && 1 / this.value < 0;
  4010. });
  4011. def(AST_Lambda, return_false);
  4012. def(AST_Object, return_false);
  4013. def(AST_RegExp, return_false);
  4014. def(AST_Sequence, function() {
  4015. return this.tail_node().is_negative_zero();
  4016. });
  4017. def(AST_SymbolRef, function() {
  4018. var fixed = this.fixed_value();
  4019. if (!fixed) return true;
  4020. this.is_negative_zero = return_true;
  4021. var result = fixed.is_negative_zero();
  4022. delete this.is_negative_zero;
  4023. return result;
  4024. });
  4025. def(AST_UnaryPrefix, function() {
  4026. return this.operator == "+" && this.expression.is_negative_zero()
  4027. || this.operator == "-";
  4028. });
  4029. })(function(node, func) {
  4030. node.DEFMETHOD("is_negative_zero", func);
  4031. });
  4032. // may_throw_on_access()
  4033. // returns true if this node may be null, undefined or contain `AST_Accessor`
  4034. (function(def) {
  4035. AST_Node.DEFMETHOD("may_throw_on_access", function(compressor, force) {
  4036. return !compressor.option("pure_getters") || this._dot_throw(compressor, force);
  4037. });
  4038. function is_strict(compressor, force) {
  4039. return force || /strict/.test(compressor.option("pure_getters"));
  4040. }
  4041. def(AST_Node, is_strict);
  4042. def(AST_Array, return_false);
  4043. def(AST_Assign, function(compressor) {
  4044. var op = this.operator;
  4045. var sym = this.left;
  4046. var rhs = this.right;
  4047. if (op != "=") {
  4048. return lazy_op[op.slice(0, -1)] && (sym._dot_throw(compressor) || rhs._dot_throw(compressor));
  4049. }
  4050. if (!rhs._dot_throw(compressor)) return false;
  4051. if (!(sym instanceof AST_SymbolRef)) return true;
  4052. if (rhs instanceof AST_Binary && rhs.operator == "||" && sym.name == rhs.left.name) {
  4053. return rhs.right._dot_throw(compressor);
  4054. }
  4055. return true;
  4056. });
  4057. def(AST_Binary, function(compressor) {
  4058. return lazy_op[this.operator] && (this.left._dot_throw(compressor) || this.right._dot_throw(compressor));
  4059. });
  4060. def(AST_Class, return_false);
  4061. def(AST_Conditional, function(compressor) {
  4062. return this.consequent._dot_throw(compressor) || this.alternative._dot_throw(compressor);
  4063. });
  4064. def(AST_Constant, return_false);
  4065. def(AST_Dot, function(compressor, force) {
  4066. if (!is_strict(compressor, force)) return false;
  4067. var exp = this.expression;
  4068. if (exp instanceof AST_SymbolRef) exp = exp.fixed_value();
  4069. return !(this.property == "prototype" && is_lambda(exp));
  4070. });
  4071. def(AST_Lambda, return_false);
  4072. def(AST_Null, return_true);
  4073. def(AST_Object, function(compressor, force) {
  4074. return is_strict(compressor, force) && !all(this.properties, function(prop) {
  4075. if (!(prop instanceof AST_ObjectKeyVal)) return false;
  4076. return !(prop.key === "__proto__" && prop.value._dot_throw(compressor, force));
  4077. });
  4078. });
  4079. def(AST_ObjectIdentity, function(compressor, force) {
  4080. return is_strict(compressor, force) && !this.scope.resolve().new;
  4081. });
  4082. def(AST_Sequence, function(compressor) {
  4083. return this.tail_node()._dot_throw(compressor);
  4084. });
  4085. def(AST_SymbolRef, function(compressor, force) {
  4086. if (this.is_undefined) return true;
  4087. if (!is_strict(compressor, force)) return false;
  4088. if (is_undeclared_ref(this) && this.is_declared(compressor)) return false;
  4089. if (this.is_immutable()) return false;
  4090. var def = this.definition();
  4091. if (is_arguments(def) && !def.scope.rest && all(def.scope.argnames, function(argname) {
  4092. return argname instanceof AST_SymbolFunarg;
  4093. })) return def.scope.uses_arguments > 2;
  4094. var fixed = this.fixed_value();
  4095. if (!fixed) return true;
  4096. this._dot_throw = return_true;
  4097. if (fixed._dot_throw(compressor)) {
  4098. delete this._dot_throw;
  4099. return true;
  4100. }
  4101. this._dot_throw = return_false;
  4102. return false;
  4103. });
  4104. def(AST_UnaryPrefix, function() {
  4105. return this.operator == "void";
  4106. });
  4107. def(AST_UnaryPostfix, return_false);
  4108. def(AST_Undefined, return_true);
  4109. })(function(node, func) {
  4110. node.DEFMETHOD("_dot_throw", func);
  4111. });
  4112. (function(def) {
  4113. def(AST_Node, return_false);
  4114. def(AST_Array, return_true);
  4115. function is_binary_defined(compressor, op, node) {
  4116. switch (op) {
  4117. case "&&":
  4118. return node.left.is_defined(compressor) && node.right.is_defined(compressor);
  4119. case "||":
  4120. return node.left.is_truthy() || node.right.is_defined(compressor);
  4121. case "??":
  4122. return node.left.is_defined(compressor) || node.right.is_defined(compressor);
  4123. default:
  4124. return true;
  4125. }
  4126. }
  4127. def(AST_Assign, function(compressor) {
  4128. var op = this.operator;
  4129. if (op == "=") return this.right.is_defined(compressor);
  4130. return is_binary_defined(compressor, op.slice(0, -1), this);
  4131. });
  4132. def(AST_Binary, function(compressor) {
  4133. return is_binary_defined(compressor, this.operator, this);
  4134. });
  4135. def(AST_Conditional, function(compressor) {
  4136. return this.consequent.is_defined(compressor) && this.alternative.is_defined(compressor);
  4137. });
  4138. def(AST_Constant, return_true);
  4139. def(AST_Hole, return_false);
  4140. def(AST_Lambda, return_true);
  4141. def(AST_Object, return_true);
  4142. def(AST_Sequence, function(compressor) {
  4143. return this.tail_node().is_defined(compressor);
  4144. });
  4145. def(AST_SymbolRef, function(compressor) {
  4146. if (this.is_undefined) return false;
  4147. if (is_undeclared_ref(this) && this.is_declared(compressor)) return true;
  4148. if (this.is_immutable()) return true;
  4149. var fixed = this.fixed_value();
  4150. if (!fixed) return false;
  4151. this.is_defined = return_false;
  4152. var result = fixed.is_defined(compressor);
  4153. delete this.is_defined;
  4154. return result;
  4155. });
  4156. def(AST_UnaryPrefix, function() {
  4157. return this.operator != "void";
  4158. });
  4159. def(AST_UnaryPostfix, return_true);
  4160. def(AST_Undefined, return_false);
  4161. })(function(node, func) {
  4162. node.DEFMETHOD("is_defined", func);
  4163. });
  4164. /* -----[ boolean/negation helpers ]----- */
  4165. // methods to determine whether an expression has a boolean result type
  4166. (function(def) {
  4167. def(AST_Node, return_false);
  4168. def(AST_Assign, function(compressor) {
  4169. return this.operator == "=" && this.right.is_boolean(compressor);
  4170. });
  4171. var binary = makePredicate("in instanceof == != === !== < <= >= >");
  4172. def(AST_Binary, function(compressor) {
  4173. return binary[this.operator] || lazy_op[this.operator]
  4174. && this.left.is_boolean(compressor)
  4175. && this.right.is_boolean(compressor);
  4176. });
  4177. def(AST_Boolean, return_true);
  4178. var fn = makePredicate("every hasOwnProperty isPrototypeOf propertyIsEnumerable some");
  4179. def(AST_Call, function(compressor) {
  4180. if (!compressor.option("unsafe")) return false;
  4181. var exp = this.expression;
  4182. return exp instanceof AST_Dot && (fn[exp.property]
  4183. || exp.property == "test" && exp.expression instanceof AST_RegExp);
  4184. });
  4185. def(AST_Conditional, function(compressor) {
  4186. return this.consequent.is_boolean(compressor) && this.alternative.is_boolean(compressor);
  4187. });
  4188. def(AST_New, return_false);
  4189. def(AST_Sequence, function(compressor) {
  4190. return this.tail_node().is_boolean(compressor);
  4191. });
  4192. def(AST_SymbolRef, function(compressor) {
  4193. var fixed = this.fixed_value();
  4194. if (!fixed) return false;
  4195. this.is_boolean = return_false;
  4196. var result = fixed.is_boolean(compressor);
  4197. delete this.is_boolean;
  4198. return result;
  4199. });
  4200. var unary = makePredicate("! delete");
  4201. def(AST_UnaryPrefix, function() {
  4202. return unary[this.operator];
  4203. });
  4204. })(function(node, func) {
  4205. node.DEFMETHOD("is_boolean", func);
  4206. });
  4207. // methods to determine if an expression has a numeric result type
  4208. (function(def) {
  4209. def(AST_Node, return_false);
  4210. var binary = makePredicate("- * / % ** & | ^ << >> >>>");
  4211. def(AST_Assign, function(compressor) {
  4212. return binary[this.operator.slice(0, -1)]
  4213. || this.operator == "=" && this.right.is_number(compressor);
  4214. });
  4215. def(AST_Binary, function(compressor) {
  4216. if (binary[this.operator]) return true;
  4217. if (this.operator != "+") return false;
  4218. return (this.left.is_boolean(compressor) || this.left.is_number(compressor))
  4219. && (this.right.is_boolean(compressor) || this.right.is_number(compressor));
  4220. });
  4221. var fn = makePredicate([
  4222. "charCodeAt",
  4223. "getDate",
  4224. "getDay",
  4225. "getFullYear",
  4226. "getHours",
  4227. "getMilliseconds",
  4228. "getMinutes",
  4229. "getMonth",
  4230. "getSeconds",
  4231. "getTime",
  4232. "getTimezoneOffset",
  4233. "getUTCDate",
  4234. "getUTCDay",
  4235. "getUTCFullYear",
  4236. "getUTCHours",
  4237. "getUTCMilliseconds",
  4238. "getUTCMinutes",
  4239. "getUTCMonth",
  4240. "getUTCSeconds",
  4241. "getYear",
  4242. "indexOf",
  4243. "lastIndexOf",
  4244. "localeCompare",
  4245. "push",
  4246. "search",
  4247. "setDate",
  4248. "setFullYear",
  4249. "setHours",
  4250. "setMilliseconds",
  4251. "setMinutes",
  4252. "setMonth",
  4253. "setSeconds",
  4254. "setTime",
  4255. "setUTCDate",
  4256. "setUTCFullYear",
  4257. "setUTCHours",
  4258. "setUTCMilliseconds",
  4259. "setUTCMinutes",
  4260. "setUTCMonth",
  4261. "setUTCSeconds",
  4262. "setYear",
  4263. ]);
  4264. def(AST_Call, function(compressor) {
  4265. if (!compressor.option("unsafe")) return false;
  4266. var exp = this.expression;
  4267. return exp instanceof AST_Dot && (fn[exp.property]
  4268. || is_undeclared_ref(exp.expression) && exp.expression.name == "Math");
  4269. });
  4270. def(AST_Conditional, function(compressor) {
  4271. return this.consequent.is_number(compressor) && this.alternative.is_number(compressor);
  4272. });
  4273. def(AST_New, return_false);
  4274. def(AST_Number, return_true);
  4275. def(AST_Sequence, function(compressor) {
  4276. return this.tail_node().is_number(compressor);
  4277. });
  4278. def(AST_SymbolRef, function(compressor, keep_unary) {
  4279. var fixed = this.fixed_value();
  4280. if (!fixed) return false;
  4281. if (keep_unary
  4282. && fixed instanceof AST_UnaryPrefix
  4283. && fixed.operator == "+"
  4284. && fixed.expression.equivalent_to(this)) {
  4285. return false;
  4286. }
  4287. this.is_number = return_false;
  4288. var result = fixed.is_number(compressor);
  4289. delete this.is_number;
  4290. return result;
  4291. });
  4292. var unary = makePredicate("+ - ~ ++ --");
  4293. def(AST_Unary, function() {
  4294. return unary[this.operator];
  4295. });
  4296. })(function(node, func) {
  4297. node.DEFMETHOD("is_number", func);
  4298. });
  4299. // methods to determine if an expression has a string result type
  4300. (function(def) {
  4301. def(AST_Node, return_false);
  4302. def(AST_Assign, function(compressor) {
  4303. switch (this.operator) {
  4304. case "+=":
  4305. if (this.left.is_string(compressor)) return true;
  4306. case "=":
  4307. return this.right.is_string(compressor);
  4308. }
  4309. });
  4310. def(AST_Binary, function(compressor) {
  4311. return this.operator == "+" &&
  4312. (this.left.is_string(compressor) || this.right.is_string(compressor));
  4313. });
  4314. var fn = makePredicate([
  4315. "charAt",
  4316. "substr",
  4317. "substring",
  4318. "toExponential",
  4319. "toFixed",
  4320. "toLowerCase",
  4321. "toPrecision",
  4322. "toString",
  4323. "toUpperCase",
  4324. "trim",
  4325. ]);
  4326. def(AST_Call, function(compressor) {
  4327. if (!compressor.option("unsafe")) return false;
  4328. var exp = this.expression;
  4329. return exp instanceof AST_Dot && fn[exp.property];
  4330. });
  4331. def(AST_Conditional, function(compressor) {
  4332. return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
  4333. });
  4334. def(AST_Sequence, function(compressor) {
  4335. return this.tail_node().is_string(compressor);
  4336. });
  4337. def(AST_String, return_true);
  4338. def(AST_SymbolRef, function(compressor) {
  4339. var fixed = this.fixed_value();
  4340. if (!fixed) return false;
  4341. this.is_string = return_false;
  4342. var result = fixed.is_string(compressor);
  4343. delete this.is_string;
  4344. return result;
  4345. });
  4346. def(AST_Template, function(compressor) {
  4347. return !this.tag || is_raw_tag(compressor, this.tag);
  4348. });
  4349. def(AST_UnaryPrefix, function() {
  4350. return this.operator == "typeof";
  4351. });
  4352. })(function(node, func) {
  4353. node.DEFMETHOD("is_string", func);
  4354. });
  4355. var lazy_op = makePredicate("&& || ??");
  4356. (function(def) {
  4357. function to_node(value, orig) {
  4358. if (value instanceof AST_Node) return value.clone(true);
  4359. if (Array.isArray(value)) return make_node(AST_Array, orig, {
  4360. elements: value.map(function(value) {
  4361. return to_node(value, orig);
  4362. })
  4363. });
  4364. if (value && typeof value == "object") {
  4365. var props = [];
  4366. for (var key in value) if (HOP(value, key)) {
  4367. props.push(make_node(AST_ObjectKeyVal, orig, {
  4368. key: key,
  4369. value: to_node(value[key], orig)
  4370. }));
  4371. }
  4372. return make_node(AST_Object, orig, {
  4373. properties: props
  4374. });
  4375. }
  4376. return make_node_from_constant(value, orig);
  4377. }
  4378. function warn(node) {
  4379. AST_Node.warn("global_defs {node} redefined [{file}:{line},{col}]", {
  4380. node: node,
  4381. file: node.start.file,
  4382. line: node.start.line,
  4383. col: node.start.col,
  4384. });
  4385. }
  4386. AST_Toplevel.DEFMETHOD("resolve_defines", function(compressor) {
  4387. if (!compressor.option("global_defs")) return this;
  4388. this.figure_out_scope({ ie: compressor.option("ie") });
  4389. return this.transform(new TreeTransformer(function(node) {
  4390. var def = node._find_defs(compressor, "");
  4391. if (!def) return;
  4392. var level = 0, child = node, parent;
  4393. while (parent = this.parent(level++)) {
  4394. if (!(parent instanceof AST_PropAccess)) break;
  4395. if (parent.expression !== child) break;
  4396. child = parent;
  4397. }
  4398. if (is_lhs(child, parent)) {
  4399. warn(node);
  4400. return;
  4401. }
  4402. return def;
  4403. }));
  4404. });
  4405. def(AST_Node, noop);
  4406. def(AST_Dot, function(compressor, suffix) {
  4407. return this.expression._find_defs(compressor, "." + this.property + suffix);
  4408. });
  4409. def(AST_SymbolDeclaration, function(compressor) {
  4410. if (!this.definition().global) return;
  4411. if (HOP(compressor.option("global_defs"), this.name)) warn(this);
  4412. });
  4413. def(AST_SymbolRef, function(compressor, suffix) {
  4414. if (!this.definition().global) return;
  4415. var defines = compressor.option("global_defs");
  4416. var name = this.name + suffix;
  4417. if (HOP(defines, name)) return to_node(defines[name], this);
  4418. });
  4419. })(function(node, func) {
  4420. node.DEFMETHOD("_find_defs", func);
  4421. });
  4422. function best_of_expression(ast1, ast2, threshold) {
  4423. var delta = ast2.print_to_string().length - ast1.print_to_string().length;
  4424. return delta < (threshold || 0) ? ast2 : ast1;
  4425. }
  4426. function best_of_statement(ast1, ast2, threshold) {
  4427. return best_of_expression(make_node(AST_SimpleStatement, ast1, {
  4428. body: ast1
  4429. }), make_node(AST_SimpleStatement, ast2, {
  4430. body: ast2
  4431. }), threshold).body;
  4432. }
  4433. function best_of(compressor, ast1, ast2, threshold) {
  4434. return (first_in_statement(compressor) ? best_of_statement : best_of_expression)(ast1, ast2, threshold);
  4435. }
  4436. function convert_to_predicate(obj) {
  4437. var map = Object.create(null);
  4438. Object.keys(obj).forEach(function(key) {
  4439. map[key] = makePredicate(obj[key]);
  4440. });
  4441. return map;
  4442. }
  4443. function skip_directives(body) {
  4444. for (var i = 0; i < body.length; i++) {
  4445. var stat = body[i];
  4446. if (!(stat instanceof AST_Directive)) return stat;
  4447. }
  4448. }
  4449. function arrow_first_statement() {
  4450. if (this.value) return make_node(AST_Return, this.value, {
  4451. value: this.value
  4452. });
  4453. return skip_directives(this.body);
  4454. }
  4455. AST_Arrow.DEFMETHOD("first_statement", arrow_first_statement);
  4456. AST_AsyncArrow.DEFMETHOD("first_statement", arrow_first_statement);
  4457. AST_Lambda.DEFMETHOD("first_statement", function() {
  4458. return skip_directives(this.body);
  4459. });
  4460. AST_Lambda.DEFMETHOD("length", function() {
  4461. var argnames = this.argnames;
  4462. for (var i = 0; i < argnames.length; i++) {
  4463. if (argnames[i] instanceof AST_DefaultValue) break;
  4464. }
  4465. return i;
  4466. });
  4467. function try_evaluate(compressor, node) {
  4468. var ev = node.evaluate(compressor);
  4469. if (ev === node) return node;
  4470. ev = make_node_from_constant(ev, node).optimize(compressor);
  4471. return best_of(compressor, node, ev, compressor.eval_threshold);
  4472. }
  4473. var object_fns = [
  4474. "constructor",
  4475. "toString",
  4476. "valueOf",
  4477. ];
  4478. var native_fns = convert_to_predicate({
  4479. Array: [
  4480. "indexOf",
  4481. "join",
  4482. "lastIndexOf",
  4483. "slice",
  4484. ].concat(object_fns),
  4485. Boolean: object_fns,
  4486. Function: object_fns,
  4487. Number: [
  4488. "toExponential",
  4489. "toFixed",
  4490. "toPrecision",
  4491. ].concat(object_fns),
  4492. Object: object_fns,
  4493. RegExp: [
  4494. "exec",
  4495. "test",
  4496. ].concat(object_fns),
  4497. String: [
  4498. "charAt",
  4499. "charCodeAt",
  4500. "concat",
  4501. "indexOf",
  4502. "italics",
  4503. "lastIndexOf",
  4504. "match",
  4505. "replace",
  4506. "search",
  4507. "slice",
  4508. "split",
  4509. "substr",
  4510. "substring",
  4511. "toLowerCase",
  4512. "toUpperCase",
  4513. "trim",
  4514. ].concat(object_fns),
  4515. });
  4516. var static_fns = convert_to_predicate({
  4517. Array: [
  4518. "isArray",
  4519. ],
  4520. Math: [
  4521. "abs",
  4522. "acos",
  4523. "asin",
  4524. "atan",
  4525. "ceil",
  4526. "cos",
  4527. "exp",
  4528. "floor",
  4529. "log",
  4530. "round",
  4531. "sin",
  4532. "sqrt",
  4533. "tan",
  4534. "atan2",
  4535. "pow",
  4536. "max",
  4537. "min",
  4538. ],
  4539. Number: [
  4540. "isFinite",
  4541. "isNaN",
  4542. ],
  4543. Object: [
  4544. "create",
  4545. "getOwnPropertyDescriptor",
  4546. "getOwnPropertyNames",
  4547. "getPrototypeOf",
  4548. "isExtensible",
  4549. "isFrozen",
  4550. "isSealed",
  4551. "keys",
  4552. ],
  4553. String: [
  4554. "fromCharCode",
  4555. "raw",
  4556. ],
  4557. });
  4558. function is_static_fn(node) {
  4559. if (!(node instanceof AST_Dot)) return false;
  4560. var expr = node.expression;
  4561. if (!is_undeclared_ref(expr)) return false;
  4562. var static_fn = static_fns[expr.name];
  4563. return static_fn && (static_fn[node.property] || expr.name == "Math" && node.property == "random");
  4564. }
  4565. // Accomodate when compress option evaluate=false
  4566. // as well as the common constant expressions !0 and -1
  4567. (function(def) {
  4568. def(AST_Node, return_false);
  4569. def(AST_Constant, return_true);
  4570. def(AST_RegExp, return_false);
  4571. var unaryPrefix = makePredicate("! ~ - + void");
  4572. def(AST_UnaryPrefix, function() {
  4573. return unaryPrefix[this.operator] && this.expression instanceof AST_Constant;
  4574. });
  4575. })(function(node, func) {
  4576. node.DEFMETHOD("is_constant", func);
  4577. });
  4578. // methods to evaluate a constant expression
  4579. (function(def) {
  4580. // If the node has been successfully reduced to a constant,
  4581. // then its value is returned; otherwise the element itself
  4582. // is returned.
  4583. //
  4584. // They can be distinguished as constant value is never a
  4585. // descendant of AST_Node.
  4586. //
  4587. // When `ignore_side_effects` is `true`, inspect the constant value
  4588. // produced without worrying about any side effects caused by said
  4589. // expression.
  4590. AST_Node.DEFMETHOD("evaluate", function(compressor, ignore_side_effects) {
  4591. if (!compressor.option("evaluate")) return this;
  4592. var cached = [];
  4593. var val = this._eval(compressor, ignore_side_effects, cached, 1);
  4594. cached.forEach(function(node) {
  4595. delete node._eval;
  4596. });
  4597. if (ignore_side_effects) return val;
  4598. if (!val || val instanceof RegExp) return val;
  4599. if (typeof val == "function" || typeof val == "object") return this;
  4600. return val;
  4601. });
  4602. var scan_modified = new TreeWalker(function(node) {
  4603. if (node instanceof AST_Assign) modified(node.left);
  4604. if (node instanceof AST_ForEnumeration) modified(node.init);
  4605. if (node instanceof AST_Unary && UNARY_POSTFIX[node.operator]) modified(node.expression);
  4606. });
  4607. function modified(node) {
  4608. if (node instanceof AST_DestructuredArray) {
  4609. node.elements.forEach(modified);
  4610. } else if (node instanceof AST_DestructuredObject) {
  4611. node.properties.forEach(function(prop) {
  4612. modified(prop.value);
  4613. });
  4614. } else if (node instanceof AST_PropAccess) {
  4615. modified(node.expression);
  4616. } else if (node instanceof AST_SymbolRef) {
  4617. node.definition().references.forEach(function(ref) {
  4618. delete ref._eval;
  4619. });
  4620. }
  4621. }
  4622. def(AST_Statement, function() {
  4623. throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
  4624. });
  4625. def(AST_Accessor, return_this);
  4626. def(AST_BigInt, return_this);
  4627. def(AST_Class, return_this);
  4628. def(AST_Node, return_this);
  4629. def(AST_Constant, function() {
  4630. return this.value;
  4631. });
  4632. def(AST_Assign, function(compressor, ignore_side_effects, cached, depth) {
  4633. var lhs = this.left;
  4634. if (!ignore_side_effects) {
  4635. if (!(lhs instanceof AST_SymbolRef)) return this;
  4636. if (!HOP(lhs, "_eval")) {
  4637. if (!lhs.fixed) return this;
  4638. var def = lhs.definition();
  4639. if (!def.fixed) return this;
  4640. if (def.undeclared) return this;
  4641. if (def.last_ref !== lhs) return this;
  4642. if (def.single_use == "m") return this;
  4643. }
  4644. }
  4645. var op = this.operator;
  4646. var node;
  4647. if (!HOP(lhs, "_eval") && lhs instanceof AST_SymbolRef && lhs.fixed && lhs.definition().fixed) {
  4648. node = lhs;
  4649. } else if (op == "=") {
  4650. node = this.right;
  4651. } else {
  4652. node = make_node(AST_Binary, this, {
  4653. operator: op.slice(0, -1),
  4654. left: lhs,
  4655. right: this.right,
  4656. });
  4657. }
  4658. lhs.walk(scan_modified);
  4659. var value = node._eval(compressor, ignore_side_effects, cached, depth);
  4660. if (typeof value == "object") return this;
  4661. modified(lhs);
  4662. return value;
  4663. });
  4664. def(AST_Sequence, function(compressor, ignore_side_effects, cached, depth) {
  4665. if (!ignore_side_effects) return this;
  4666. var exprs = this.expressions;
  4667. for (var i = 0, last = exprs.length - 1; i < last; i++) {
  4668. exprs[i].walk(scan_modified);
  4669. }
  4670. var tail = exprs[last];
  4671. var value = tail._eval(compressor, ignore_side_effects, cached, depth);
  4672. return value === tail ? this : value;
  4673. });
  4674. def(AST_Lambda, function(compressor) {
  4675. if (compressor.option("unsafe")) {
  4676. var fn = function() {};
  4677. fn.node = this;
  4678. fn.toString = function() {
  4679. return "function(){}";
  4680. };
  4681. return fn;
  4682. }
  4683. return this;
  4684. });
  4685. def(AST_Array, function(compressor, ignore_side_effects, cached, depth) {
  4686. if (compressor.option("unsafe")) {
  4687. var elements = [];
  4688. for (var i = 0; i < this.elements.length; i++) {
  4689. var element = this.elements[i];
  4690. if (element instanceof AST_Hole) return this;
  4691. var value = element._eval(compressor, ignore_side_effects, cached, depth);
  4692. if (element === value) return this;
  4693. elements.push(value);
  4694. }
  4695. return elements;
  4696. }
  4697. return this;
  4698. });
  4699. def(AST_Object, function(compressor, ignore_side_effects, cached, depth) {
  4700. if (compressor.option("unsafe")) {
  4701. var val = {};
  4702. for (var i = 0; i < this.properties.length; i++) {
  4703. var prop = this.properties[i];
  4704. if (!(prop instanceof AST_ObjectKeyVal)) return this;
  4705. var key = prop.key;
  4706. if (key instanceof AST_Node) {
  4707. key = key._eval(compressor, ignore_side_effects, cached, depth);
  4708. if (key === prop.key) return this;
  4709. }
  4710. switch (key) {
  4711. case "__proto__":
  4712. case "toString":
  4713. case "valueOf":
  4714. return this;
  4715. }
  4716. val[key] = prop.value._eval(compressor, ignore_side_effects, cached, depth);
  4717. if (val[key] === prop.value) return this;
  4718. }
  4719. return val;
  4720. }
  4721. return this;
  4722. });
  4723. var non_converting_unary = makePredicate("! typeof void");
  4724. def(AST_UnaryPrefix, function(compressor, ignore_side_effects, cached, depth) {
  4725. var e = this.expression;
  4726. var op = this.operator;
  4727. // Function would be evaluated to an array and so typeof would
  4728. // incorrectly return "object". Hence making is a special case.
  4729. if (compressor.option("typeofs")
  4730. && op == "typeof"
  4731. && (e instanceof AST_Lambda
  4732. || e instanceof AST_SymbolRef
  4733. && e.fixed_value() instanceof AST_Lambda)) {
  4734. return typeof function(){};
  4735. }
  4736. var def = e instanceof AST_SymbolRef && e.definition();
  4737. if (!non_converting_unary[op] && !(def && def.fixed)) depth++;
  4738. e.walk(scan_modified);
  4739. var v = e._eval(compressor, ignore_side_effects, cached, depth);
  4740. if (v === e) {
  4741. if (ignore_side_effects && op == "void") return;
  4742. return this;
  4743. }
  4744. switch (op) {
  4745. case "!": return !v;
  4746. case "typeof":
  4747. // typeof <RegExp> returns "object" or "function" on different platforms
  4748. // so cannot evaluate reliably
  4749. if (v instanceof RegExp) return this;
  4750. return typeof v;
  4751. case "void": return;
  4752. case "~": return ~v;
  4753. case "-": return -v;
  4754. case "+": return +v;
  4755. case "++":
  4756. case "--":
  4757. if (!def) return this;
  4758. if (!ignore_side_effects) {
  4759. if (def.undeclared) return this;
  4760. if (def.last_ref !== e) return this;
  4761. }
  4762. if (HOP(e, "_eval")) v = +(op[0] + 1) + +v;
  4763. modified(e);
  4764. return v;
  4765. }
  4766. return this;
  4767. });
  4768. def(AST_UnaryPostfix, function(compressor, ignore_side_effects, cached, depth) {
  4769. var e = this.expression;
  4770. if (!(e instanceof AST_SymbolRef)) {
  4771. if (!ignore_side_effects) return this;
  4772. } else if (!HOP(e, "_eval")) {
  4773. if (!e.fixed) return this;
  4774. if (!ignore_side_effects) {
  4775. var def = e.definition();
  4776. if (!def.fixed) return this;
  4777. if (def.undeclared) return this;
  4778. if (def.last_ref !== e) return this;
  4779. }
  4780. }
  4781. if (!(e instanceof AST_SymbolRef && e.definition().fixed)) depth++;
  4782. e.walk(scan_modified);
  4783. var v = e._eval(compressor, ignore_side_effects, cached, depth);
  4784. if (v === e) return this;
  4785. modified(e);
  4786. return +v;
  4787. });
  4788. var non_converting_binary = makePredicate("&& || === !==");
  4789. def(AST_Binary, function(compressor, ignore_side_effects, cached, depth) {
  4790. if (!non_converting_binary[this.operator]) depth++;
  4791. var left = this.left._eval(compressor, ignore_side_effects, cached, depth);
  4792. if (left === this.left) return this;
  4793. if (this.operator == (left ? "||" : "&&")) return left;
  4794. var rhs_ignore_side_effects = ignore_side_effects && !(left && typeof left == "object");
  4795. var right = this.right._eval(compressor, rhs_ignore_side_effects, cached, depth);
  4796. if (right === this.right) return this;
  4797. var result;
  4798. switch (this.operator) {
  4799. case "&&" : result = left && right; break;
  4800. case "||" : result = left || right; break;
  4801. case "??" :
  4802. result = left == null ? right : left;
  4803. break;
  4804. case "|" : result = left | right; break;
  4805. case "&" : result = left & right; break;
  4806. case "^" : result = left ^ right; break;
  4807. case "+" : result = left + right; break;
  4808. case "-" : result = left - right; break;
  4809. case "*" : result = left * right; break;
  4810. case "/" : result = left / right; break;
  4811. case "%" : result = left % right; break;
  4812. case "<<" : result = left << right; break;
  4813. case ">>" : result = left >> right; break;
  4814. case ">>>": result = left >>> right; break;
  4815. case "==" : result = left == right; break;
  4816. case "===": result = left === right; break;
  4817. case "!=" : result = left != right; break;
  4818. case "!==": result = left !== right; break;
  4819. case "<" : result = left < right; break;
  4820. case "<=" : result = left <= right; break;
  4821. case ">" : result = left > right; break;
  4822. case ">=" : result = left >= right; break;
  4823. case "**":
  4824. result = Math.pow(left, right);
  4825. break;
  4826. case "in":
  4827. if (right && typeof right == "object" && HOP(right, left)) {
  4828. result = true;
  4829. break;
  4830. }
  4831. default:
  4832. return this;
  4833. }
  4834. if (isNaN(result)) return compressor.find_parent(AST_With) ? this : result;
  4835. if (compressor.option("unsafe_math")
  4836. && !ignore_side_effects
  4837. && result
  4838. && typeof result == "number"
  4839. && (this.operator == "+" || this.operator == "-")) {
  4840. var digits = Math.max(0, decimals(left), decimals(right));
  4841. // 53-bit significand ---> 15.95 decimal places
  4842. if (digits < 16) return +result.toFixed(digits);
  4843. }
  4844. return result;
  4845. function decimals(operand) {
  4846. var match = /(\.[0-9]*)?(e[^e]+)?$/.exec(+operand);
  4847. return (match[1] || ".").length - 1 - (match[2] || "").slice(1);
  4848. }
  4849. });
  4850. def(AST_Conditional, function(compressor, ignore_side_effects, cached, depth) {
  4851. var condition = this.condition._eval(compressor, ignore_side_effects, cached, depth);
  4852. if (condition === this.condition) return this;
  4853. var node = condition ? this.consequent : this.alternative;
  4854. var value = node._eval(compressor, ignore_side_effects, cached, depth);
  4855. return value === node ? this : value;
  4856. });
  4857. function verify_escaped(ref, depth) {
  4858. var escaped = ref.definition().escaped;
  4859. switch (escaped.length) {
  4860. case 0:
  4861. return true;
  4862. case 1:
  4863. var found = false;
  4864. escaped[0].walk(new TreeWalker(function(node) {
  4865. if (found) return true;
  4866. if (node === ref) return found = true;
  4867. if (node instanceof AST_Scope) return true;
  4868. }));
  4869. return found;
  4870. default:
  4871. return depth <= escaped.depth;
  4872. }
  4873. }
  4874. def(AST_SymbolRef, function(compressor, ignore_side_effects, cached, depth) {
  4875. var fixed = this.fixed_value();
  4876. if (!fixed) return this;
  4877. var value;
  4878. if (HOP(fixed, "_eval")) {
  4879. value = fixed._eval();
  4880. } else {
  4881. this._eval = return_this;
  4882. value = fixed._eval(compressor, ignore_side_effects, cached, depth);
  4883. delete this._eval;
  4884. if (value === fixed) return this;
  4885. fixed._eval = function() {
  4886. return value;
  4887. };
  4888. cached.push(fixed);
  4889. }
  4890. return value && typeof value == "object" && !verify_escaped(this, depth) ? this : value;
  4891. });
  4892. var global_objs = {
  4893. Array: Array,
  4894. Math: Math,
  4895. Number: Number,
  4896. Object: Object,
  4897. String: String,
  4898. };
  4899. var static_values = convert_to_predicate({
  4900. Math: [
  4901. "E",
  4902. "LN10",
  4903. "LN2",
  4904. "LOG2E",
  4905. "LOG10E",
  4906. "PI",
  4907. "SQRT1_2",
  4908. "SQRT2",
  4909. ],
  4910. Number: [
  4911. "MAX_VALUE",
  4912. "MIN_VALUE",
  4913. "NaN",
  4914. "NEGATIVE_INFINITY",
  4915. "POSITIVE_INFINITY",
  4916. ],
  4917. });
  4918. var regexp_props = makePredicate("global ignoreCase multiline source");
  4919. def(AST_PropAccess, function(compressor, ignore_side_effects, cached, depth) {
  4920. if (compressor.option("unsafe")) {
  4921. var val;
  4922. var exp = this.expression;
  4923. if (!is_undeclared_ref(exp)) {
  4924. val = exp._eval(compressor, ignore_side_effects, cached, depth + 1);
  4925. if (val == null || val === exp) return this;
  4926. }
  4927. var key = this.property;
  4928. if (key instanceof AST_Node) {
  4929. key = key._eval(compressor, ignore_side_effects, cached, depth);
  4930. if (key === this.property) return this;
  4931. }
  4932. if (val === undefined) {
  4933. var static_value = static_values[exp.name];
  4934. if (!static_value || !static_value[key]) return this;
  4935. val = global_objs[exp.name];
  4936. } else if (val instanceof RegExp) {
  4937. if (!regexp_props[key]) return this;
  4938. } else if (typeof val == "object") {
  4939. if (!HOP(val, key)) return this;
  4940. } else if (typeof val == "function") switch (key) {
  4941. case "name":
  4942. return val.node.name ? val.node.name.name : "";
  4943. case "length":
  4944. return val.node.length();
  4945. default:
  4946. return this;
  4947. }
  4948. return val[key];
  4949. }
  4950. return this;
  4951. });
  4952. function eval_all(nodes, compressor, ignore_side_effects, cached, depth) {
  4953. var values = [];
  4954. for (var i = 0; i < nodes.length; i++) {
  4955. var node = nodes[i];
  4956. var value = node._eval(compressor, ignore_side_effects, cached, depth);
  4957. if (node === value) return;
  4958. values.push(value);
  4959. }
  4960. return values;
  4961. }
  4962. def(AST_Call, function(compressor, ignore_side_effects, cached, depth) {
  4963. var exp = this.expression;
  4964. var fn = exp instanceof AST_SymbolRef ? exp.fixed_value() : exp;
  4965. if (fn instanceof AST_Arrow || fn instanceof AST_Defun || fn instanceof AST_Function) {
  4966. if (fn.evaluating) return this;
  4967. if (fn.name && fn.name.definition().recursive_refs > 0) return this;
  4968. if (this.is_expr_pure(compressor)) return this;
  4969. var args = eval_all(this.args, compressor, ignore_side_effects, cached, depth);
  4970. if (!all(fn.argnames, function(sym, index) {
  4971. if (sym instanceof AST_DefaultValue) {
  4972. if (!args) return false;
  4973. if (args[index] === undefined) {
  4974. var value = sym.value._eval(compressor, ignore_side_effects, cached, depth);
  4975. if (value === sym.value) return false;
  4976. args[index] = value;
  4977. }
  4978. sym = sym.name;
  4979. }
  4980. return !(sym instanceof AST_Destructured);
  4981. })) return this;
  4982. if (fn.rest instanceof AST_Destructured) return this;
  4983. if (!args && !ignore_side_effects) return this;
  4984. var stat = fn.first_statement();
  4985. if (!(stat instanceof AST_Return)) {
  4986. if (ignore_side_effects) {
  4987. fn.walk(scan_modified);
  4988. var found = false;
  4989. fn.evaluating = true;
  4990. walk_body(fn, new TreeWalker(function(node) {
  4991. if (found) return true;
  4992. if (node instanceof AST_Return) {
  4993. if (node.value && node.value._eval(compressor, true, cached, depth) !== undefined) {
  4994. found = true;
  4995. }
  4996. return true;
  4997. }
  4998. if (node instanceof AST_Scope && node !== fn) return true;
  4999. }));
  5000. fn.evaluating = false;
  5001. if (!found) return;
  5002. }
  5003. return this;
  5004. }
  5005. var val = stat.value;
  5006. if (!val) return;
  5007. var cached_args = [];
  5008. if (!args || all(fn.argnames, function(sym, i) {
  5009. return assign(sym, args[i]);
  5010. }) && !(fn.rest && !assign(fn.rest, args.slice(fn.argnames.length))) || ignore_side_effects) {
  5011. if (ignore_side_effects) fn.argnames.forEach(function(sym) {
  5012. if (sym instanceof AST_DefaultValue) sym.value.walk(scan_modified);
  5013. });
  5014. fn.evaluating = true;
  5015. val = val._eval(compressor, ignore_side_effects, cached, depth);
  5016. fn.evaluating = false;
  5017. }
  5018. cached_args.forEach(function(node) {
  5019. delete node._eval;
  5020. });
  5021. return val === stat.value ? this : val;
  5022. } else if (compressor.option("unsafe") && exp instanceof AST_PropAccess) {
  5023. var key = exp.property;
  5024. if (key instanceof AST_Node) {
  5025. key = key._eval(compressor, ignore_side_effects, cached, depth);
  5026. if (key === exp.property) return this;
  5027. }
  5028. var val;
  5029. var e = exp.expression;
  5030. if (is_undeclared_ref(e)) {
  5031. var static_fn = static_fns[e.name];
  5032. if (!static_fn || !static_fn[key]) return this;
  5033. val = global_objs[e.name];
  5034. } else {
  5035. val = e._eval(compressor, ignore_side_effects, cached, depth + 1);
  5036. if (val == null || val === e) return this;
  5037. var native_fn = native_fns[val.constructor.name];
  5038. if (!native_fn || !native_fn[key]) return this;
  5039. if (val instanceof RegExp && val.global && !(e instanceof AST_RegExp)) return this;
  5040. }
  5041. var args = eval_all(this.args, compressor, ignore_side_effects, cached, depth);
  5042. if (!args) return this;
  5043. if (key == "replace" && typeof args[1] == "function") return this;
  5044. try {
  5045. return val[key].apply(val, args);
  5046. } catch (ex) {
  5047. AST_Node.warn("Error evaluating {code} [{file}:{line},{col}]", {
  5048. code: this,
  5049. file: this.start.file,
  5050. line: this.start.line,
  5051. col: this.start.col,
  5052. });
  5053. } finally {
  5054. if (val instanceof RegExp) val.lastIndex = 0;
  5055. }
  5056. }
  5057. return this;
  5058. function assign(sym, arg) {
  5059. if (sym instanceof AST_DefaultValue) sym = sym.name;
  5060. var def = sym.definition();
  5061. if (def.orig[def.orig.length - 1] !== sym) return false;
  5062. var value = arg;
  5063. def.references.forEach(function(node) {
  5064. node._eval = function() {
  5065. return value;
  5066. };
  5067. cached_args.push(node);
  5068. });
  5069. return true;
  5070. }
  5071. });
  5072. def(AST_New, return_this);
  5073. def(AST_Template, function(compressor, ignore_side_effects, cached, depth) {
  5074. if (!compressor.option("templates")) return this;
  5075. if (this.tag) {
  5076. if (!is_raw_tag(compressor, this.tag)) return this;
  5077. decode = function(str) {
  5078. return str;
  5079. };
  5080. }
  5081. var exprs = eval_all(this.expressions, compressor, ignore_side_effects, cached, depth);
  5082. if (!exprs) return this;
  5083. var malformed = false;
  5084. var ret = decode(this.strings[0]);
  5085. for (var i = 0; i < exprs.length; i++) {
  5086. ret += exprs[i] + decode(this.strings[i + 1]);
  5087. }
  5088. if (!malformed) return ret;
  5089. this._eval = return_this;
  5090. return this;
  5091. function decode(str) {
  5092. str = decode_template(str);
  5093. if (typeof str != "string") malformed = true;
  5094. return str;
  5095. }
  5096. });
  5097. })(function(node, func) {
  5098. node.DEFMETHOD("_eval", func);
  5099. });
  5100. // method to negate an expression
  5101. (function(def) {
  5102. function basic_negation(exp) {
  5103. return make_node(AST_UnaryPrefix, exp, {
  5104. operator: "!",
  5105. expression: exp
  5106. });
  5107. }
  5108. function best(orig, alt, first_in_statement) {
  5109. var negated = basic_negation(orig);
  5110. if (first_in_statement) {
  5111. var stat = make_node(AST_SimpleStatement, alt, {
  5112. body: alt
  5113. });
  5114. return best_of_expression(negated, stat) === stat ? alt : negated;
  5115. }
  5116. return best_of_expression(negated, alt);
  5117. }
  5118. def(AST_Node, function() {
  5119. return basic_negation(this);
  5120. });
  5121. def(AST_Statement, function() {
  5122. throw new Error("Cannot negate a statement");
  5123. });
  5124. def(AST_Binary, function(compressor, first_in_statement) {
  5125. var self = this.clone(), op = this.operator;
  5126. if (compressor.option("unsafe_comps")) {
  5127. switch (op) {
  5128. case "<=" : self.operator = ">" ; return self;
  5129. case "<" : self.operator = ">=" ; return self;
  5130. case ">=" : self.operator = "<" ; return self;
  5131. case ">" : self.operator = "<=" ; return self;
  5132. }
  5133. }
  5134. switch (op) {
  5135. case "==" : self.operator = "!="; return self;
  5136. case "!=" : self.operator = "=="; return self;
  5137. case "===": self.operator = "!=="; return self;
  5138. case "!==": self.operator = "==="; return self;
  5139. case "&&":
  5140. self.operator = "||";
  5141. self.left = self.left.negate(compressor, first_in_statement);
  5142. self.right = self.right.negate(compressor);
  5143. return best(this, self, first_in_statement);
  5144. case "||":
  5145. self.operator = "&&";
  5146. self.left = self.left.negate(compressor, first_in_statement);
  5147. self.right = self.right.negate(compressor);
  5148. return best(this, self, first_in_statement);
  5149. }
  5150. return basic_negation(this);
  5151. });
  5152. def(AST_ClassExpression, function() {
  5153. return basic_negation(this);
  5154. });
  5155. def(AST_Conditional, function(compressor, first_in_statement) {
  5156. var self = this.clone();
  5157. self.consequent = self.consequent.negate(compressor);
  5158. self.alternative = self.alternative.negate(compressor);
  5159. return best(this, self, first_in_statement);
  5160. });
  5161. def(AST_LambdaExpression, function() {
  5162. return basic_negation(this);
  5163. });
  5164. def(AST_Sequence, function(compressor) {
  5165. var expressions = this.expressions.slice();
  5166. expressions.push(expressions.pop().negate(compressor));
  5167. return make_sequence(this, expressions);
  5168. });
  5169. def(AST_UnaryPrefix, function() {
  5170. if (this.operator == "!")
  5171. return this.expression;
  5172. return basic_negation(this);
  5173. });
  5174. })(function(node, func) {
  5175. node.DEFMETHOD("negate", function(compressor, first_in_statement) {
  5176. return func.call(this, compressor, first_in_statement);
  5177. });
  5178. });
  5179. var global_pure_fns = makePredicate("Boolean decodeURI decodeURIComponent Date encodeURI encodeURIComponent Error escape EvalError isFinite isNaN Number Object parseFloat parseInt RangeError ReferenceError String SyntaxError TypeError unescape URIError");
  5180. var global_pure_constructors = makePredicate("Map Set WeakMap WeakSet");
  5181. AST_Call.DEFMETHOD("is_expr_pure", function(compressor) {
  5182. if (compressor.option("unsafe")) {
  5183. var expr = this.expression;
  5184. if (is_undeclared_ref(expr)) {
  5185. if (global_pure_fns[expr.name]) return true;
  5186. if (this instanceof AST_New && global_pure_constructors[expr.name]) return true;
  5187. }
  5188. if (is_static_fn(expr)) return true;
  5189. }
  5190. return compressor.option("annotations") && this.pure || !compressor.pure_funcs(this);
  5191. });
  5192. AST_Template.DEFMETHOD("is_expr_pure", function(compressor) {
  5193. var tag = this.tag;
  5194. if (!tag) return true;
  5195. if (compressor.option("unsafe")) {
  5196. if (is_undeclared_ref(tag) && global_pure_fns[tag.name]) return true;
  5197. if (tag instanceof AST_Dot && is_undeclared_ref(tag.expression)) {
  5198. var static_fn = static_fns[tag.expression.name];
  5199. return static_fn && (static_fn[tag.property]
  5200. || tag.expression.name == "Math" && tag.property == "random");
  5201. }
  5202. }
  5203. return !compressor.pure_funcs(this);
  5204. });
  5205. AST_Node.DEFMETHOD("is_call_pure", return_false);
  5206. AST_Call.DEFMETHOD("is_call_pure", function(compressor) {
  5207. if (!compressor.option("unsafe")) return false;
  5208. var dot = this.expression;
  5209. if (!(dot instanceof AST_Dot)) return false;
  5210. var exp = dot.expression;
  5211. var map;
  5212. var prop = dot.property;
  5213. if (exp instanceof AST_Array) {
  5214. map = native_fns.Array;
  5215. } else if (exp.is_boolean(compressor)) {
  5216. map = native_fns.Boolean;
  5217. } else if (exp.is_number(compressor)) {
  5218. map = native_fns.Number;
  5219. } else if (exp instanceof AST_RegExp) {
  5220. map = native_fns.RegExp;
  5221. } else if (exp.is_string(compressor)) {
  5222. map = native_fns.String;
  5223. if (prop == "replace") {
  5224. var arg = this.args[1];
  5225. if (arg && !arg.is_string(compressor)) return false;
  5226. }
  5227. } else if (!dot.may_throw_on_access(compressor)) {
  5228. map = native_fns.Object;
  5229. }
  5230. return map && map[prop];
  5231. });
  5232. // determine if object spread syntax may cause runtime exception
  5233. (function(def) {
  5234. def(AST_Node, return_false);
  5235. def(AST_Array, return_true);
  5236. def(AST_Assign, function() {
  5237. switch (this.operator) {
  5238. case "=":
  5239. return this.right.safe_to_spread();
  5240. case "&&=":
  5241. case "||=":
  5242. case "??=":
  5243. return this.left.safe_to_spread() && this.right.safe_to_spread();
  5244. }
  5245. return true;
  5246. });
  5247. def(AST_Binary, function() {
  5248. return !lazy_op[this.operator] || this.left.safe_to_spread() && this.right.safe_to_spread();
  5249. });
  5250. def(AST_Constant, return_true);
  5251. def(AST_Lambda, return_true);
  5252. def(AST_Object, function() {
  5253. return all(this.properties, function(prop) {
  5254. return !(prop instanceof AST_ObjectGetter || prop instanceof AST_Spread);
  5255. });
  5256. });
  5257. def(AST_Sequence, function() {
  5258. return this.tail_node().safe_to_spread();
  5259. });
  5260. def(AST_SymbolRef, function() {
  5261. var fixed = this.fixed_value();
  5262. return fixed && fixed.safe_to_spread();
  5263. });
  5264. def(AST_Unary, return_true);
  5265. })(function(node, func) {
  5266. node.DEFMETHOD("safe_to_spread", func);
  5267. });
  5268. // determine if expression has side effects
  5269. (function(def) {
  5270. function any(list, compressor, spread) {
  5271. return !all(list, spread ? function(node) {
  5272. return node instanceof AST_Spread ? !spread(node, compressor) : !node.has_side_effects(compressor);
  5273. } : function(node) {
  5274. return !node.has_side_effects(compressor);
  5275. });
  5276. }
  5277. function array_spread(node, compressor) {
  5278. var exp = node.expression;
  5279. return !exp.is_string(compressor) || exp.has_side_effects(compressor);
  5280. }
  5281. def(AST_Node, return_true);
  5282. def(AST_Array, function(compressor) {
  5283. return any(this.elements, compressor, array_spread);
  5284. });
  5285. def(AST_Assign, function(compressor) {
  5286. var lhs = this.left;
  5287. if (!(lhs instanceof AST_PropAccess)) return true;
  5288. var node = lhs.expression;
  5289. return !(node instanceof AST_ObjectIdentity)
  5290. || !node.scope.resolve().new
  5291. || lhs instanceof AST_Sub && lhs.property.has_side_effects(compressor)
  5292. || this.right.has_side_effects(compressor);
  5293. });
  5294. def(AST_Binary, function(compressor) {
  5295. return this.left.has_side_effects(compressor)
  5296. || this.right.has_side_effects(compressor)
  5297. || this.operator == "in" && !is_object(this.right);
  5298. });
  5299. def(AST_Block, function(compressor) {
  5300. return any(this.body, compressor);
  5301. });
  5302. def(AST_Call, function(compressor) {
  5303. if (!this.is_expr_pure(compressor)
  5304. && (!this.is_call_pure(compressor) || this.expression.has_side_effects(compressor))) {
  5305. return true;
  5306. }
  5307. return any(this.args, compressor, array_spread);
  5308. });
  5309. def(AST_Case, function(compressor) {
  5310. return this.expression.has_side_effects(compressor)
  5311. || any(this.body, compressor);
  5312. });
  5313. def(AST_Class, function(compressor) {
  5314. var base = this.extends;
  5315. if (base) {
  5316. if (base instanceof AST_SymbolRef) base = base.fixed_value();
  5317. if (!safe_for_extends(base)) return true;
  5318. }
  5319. return any(this.properties, compressor);
  5320. });
  5321. def(AST_ClassProperty, function(compressor) {
  5322. return this.key instanceof AST_Node && this.key.has_side_effects(compressor)
  5323. || this.static && this.value && this.value.has_side_effects(compressor);
  5324. });
  5325. def(AST_Conditional, function(compressor) {
  5326. return this.condition.has_side_effects(compressor)
  5327. || this.consequent.has_side_effects(compressor)
  5328. || this.alternative.has_side_effects(compressor);
  5329. });
  5330. def(AST_Constant, return_false);
  5331. def(AST_Definitions, function(compressor) {
  5332. return any(this.definitions, compressor);
  5333. });
  5334. def(AST_DestructuredArray, function(compressor) {
  5335. return any(this.elements, compressor);
  5336. });
  5337. def(AST_DestructuredKeyVal, function(compressor) {
  5338. return this.key instanceof AST_Node && this.key.has_side_effects(compressor)
  5339. || this.value.has_side_effects(compressor);
  5340. });
  5341. def(AST_DestructuredObject, function(compressor) {
  5342. return any(this.properties, compressor);
  5343. });
  5344. def(AST_Dot, function(compressor) {
  5345. return this.expression.may_throw_on_access(compressor)
  5346. || this.expression.has_side_effects(compressor);
  5347. });
  5348. def(AST_EmptyStatement, return_false);
  5349. def(AST_If, function(compressor) {
  5350. return this.condition.has_side_effects(compressor)
  5351. || this.body && this.body.has_side_effects(compressor)
  5352. || this.alternative && this.alternative.has_side_effects(compressor);
  5353. });
  5354. def(AST_LabeledStatement, function(compressor) {
  5355. return this.body.has_side_effects(compressor);
  5356. });
  5357. def(AST_Lambda, return_false);
  5358. def(AST_Object, function(compressor) {
  5359. return any(this.properties, compressor, function(node, compressor) {
  5360. var exp = node.expression;
  5361. return !exp.safe_to_spread() || exp.has_side_effects(compressor);
  5362. });
  5363. });
  5364. def(AST_ObjectIdentity, return_false);
  5365. def(AST_ObjectProperty, function(compressor) {
  5366. return this.key instanceof AST_Node && this.key.has_side_effects(compressor)
  5367. || this.value.has_side_effects(compressor);
  5368. });
  5369. def(AST_Sequence, function(compressor) {
  5370. return any(this.expressions, compressor);
  5371. });
  5372. def(AST_SimpleStatement, function(compressor) {
  5373. return this.body.has_side_effects(compressor);
  5374. });
  5375. def(AST_Sub, function(compressor) {
  5376. return this.expression.may_throw_on_access(compressor)
  5377. || this.expression.has_side_effects(compressor)
  5378. || this.property.has_side_effects(compressor);
  5379. });
  5380. def(AST_Switch, function(compressor) {
  5381. return this.expression.has_side_effects(compressor)
  5382. || any(this.body, compressor);
  5383. });
  5384. def(AST_SymbolDeclaration, return_false);
  5385. def(AST_SymbolRef, function(compressor) {
  5386. return !this.is_declared(compressor) || !can_drop_symbol(this, compressor);
  5387. });
  5388. def(AST_Template, function(compressor) {
  5389. return !this.is_expr_pure(compressor) || any(this.expressions, compressor);
  5390. });
  5391. def(AST_Try, function(compressor) {
  5392. return any(this.body, compressor)
  5393. || this.bcatch && this.bcatch.has_side_effects(compressor)
  5394. || this.bfinally && this.bfinally.has_side_effects(compressor);
  5395. });
  5396. def(AST_Unary, function(compressor) {
  5397. return unary_side_effects[this.operator]
  5398. || this.expression.has_side_effects(compressor);
  5399. });
  5400. def(AST_VarDef, function() {
  5401. return this.value;
  5402. });
  5403. })(function(node, func) {
  5404. node.DEFMETHOD("has_side_effects", func);
  5405. });
  5406. // determine if expression may throw
  5407. (function(def) {
  5408. def(AST_Node, return_true);
  5409. def(AST_Constant, return_false);
  5410. def(AST_EmptyStatement, return_false);
  5411. def(AST_Lambda, return_false);
  5412. def(AST_ObjectIdentity, return_false);
  5413. def(AST_SymbolDeclaration, return_false);
  5414. function any(list, compressor) {
  5415. for (var i = list.length; --i >= 0;)
  5416. if (list[i].may_throw(compressor))
  5417. return true;
  5418. return false;
  5419. }
  5420. function call_may_throw(exp, compressor) {
  5421. if (exp.may_throw(compressor)) return true;
  5422. if (exp instanceof AST_SymbolRef) exp = exp.fixed_value();
  5423. if (!(exp instanceof AST_Lambda)) return true;
  5424. if (any(exp.argnames, compressor)) return true;
  5425. if (any(exp.body, compressor)) return true;
  5426. return is_arrow(exp) && exp.value && exp.value.may_throw(compressor);
  5427. }
  5428. def(AST_Array, function(compressor) {
  5429. return any(this.elements, compressor);
  5430. });
  5431. def(AST_Assign, function(compressor) {
  5432. if (this.right.may_throw(compressor)) return true;
  5433. if (!compressor.has_directive("use strict")
  5434. && this.operator == "="
  5435. && this.left instanceof AST_SymbolRef) {
  5436. return false;
  5437. }
  5438. return this.left.may_throw(compressor);
  5439. });
  5440. def(AST_Await, function(compressor) {
  5441. return this.expression.may_throw(compressor);
  5442. });
  5443. def(AST_Binary, function(compressor) {
  5444. return this.left.may_throw(compressor)
  5445. || this.right.may_throw(compressor)
  5446. || this.operator == "in" && !is_object(this.right);
  5447. });
  5448. def(AST_Block, function(compressor) {
  5449. return any(this.body, compressor);
  5450. });
  5451. def(AST_Call, function(compressor) {
  5452. if (any(this.args, compressor)) return true;
  5453. if (this.is_expr_pure(compressor)) return false;
  5454. this.may_throw = return_true;
  5455. var ret = call_may_throw(this.expression, compressor);
  5456. delete this.may_throw;
  5457. return ret;
  5458. });
  5459. def(AST_Case, function(compressor) {
  5460. return this.expression.may_throw(compressor)
  5461. || any(this.body, compressor);
  5462. });
  5463. def(AST_Conditional, function(compressor) {
  5464. return this.condition.may_throw(compressor)
  5465. || this.consequent.may_throw(compressor)
  5466. || this.alternative.may_throw(compressor);
  5467. });
  5468. def(AST_DefaultValue, function(compressor) {
  5469. return this.name.may_throw(compressor)
  5470. || this.value && this.value.may_throw(compressor);
  5471. });
  5472. def(AST_Definitions, function(compressor) {
  5473. return any(this.definitions, compressor);
  5474. });
  5475. def(AST_Dot, function(compressor) {
  5476. return !this.optional && this.expression.may_throw_on_access(compressor)
  5477. || this.expression.may_throw(compressor);
  5478. });
  5479. def(AST_ForEnumeration, function(compressor) {
  5480. if (this.init.may_throw(compressor)) return true;
  5481. var obj = this.object;
  5482. if (obj.may_throw(compressor)) return true;
  5483. obj = obj.tail_node();
  5484. if (!(obj instanceof AST_Array || obj.is_string(compressor))) return true;
  5485. return this.body.may_throw(compressor);
  5486. });
  5487. def(AST_If, function(compressor) {
  5488. return this.condition.may_throw(compressor)
  5489. || this.body && this.body.may_throw(compressor)
  5490. || this.alternative && this.alternative.may_throw(compressor);
  5491. });
  5492. def(AST_LabeledStatement, function(compressor) {
  5493. return this.body.may_throw(compressor);
  5494. });
  5495. def(AST_Object, function(compressor) {
  5496. return any(this.properties, compressor);
  5497. });
  5498. def(AST_ObjectProperty, function(compressor) {
  5499. return this.value.may_throw(compressor)
  5500. || this.key instanceof AST_Node && this.key.may_throw(compressor);
  5501. });
  5502. def(AST_Return, function(compressor) {
  5503. return this.value && this.value.may_throw(compressor);
  5504. });
  5505. def(AST_Sequence, function(compressor) {
  5506. return any(this.expressions, compressor);
  5507. });
  5508. def(AST_SimpleStatement, function(compressor) {
  5509. return this.body.may_throw(compressor);
  5510. });
  5511. def(AST_Sub, function(compressor) {
  5512. return !this.optional && this.expression.may_throw_on_access(compressor)
  5513. || this.expression.may_throw(compressor)
  5514. || this.property.may_throw(compressor);
  5515. });
  5516. def(AST_Switch, function(compressor) {
  5517. return this.expression.may_throw(compressor)
  5518. || any(this.body, compressor);
  5519. });
  5520. def(AST_SymbolRef, function(compressor) {
  5521. return !this.is_declared(compressor) || !can_drop_symbol(this, compressor);
  5522. });
  5523. def(AST_Template, function(compressor) {
  5524. if (any(this.expressions, compressor)) return true;
  5525. if (this.is_expr_pure(compressor)) return false;
  5526. if (!this.tag) return false;
  5527. this.may_throw = return_true;
  5528. var ret = call_may_throw(this.tag, compressor);
  5529. delete this.may_throw;
  5530. return ret;
  5531. });
  5532. def(AST_Try, function(compressor) {
  5533. return (this.bcatch ? this.bcatch.may_throw(compressor) : any(this.body, compressor))
  5534. || this.bfinally && this.bfinally.may_throw(compressor);
  5535. });
  5536. def(AST_Unary, function(compressor) {
  5537. return this.expression.may_throw(compressor)
  5538. && !(this.operator == "typeof" && this.expression instanceof AST_SymbolRef);
  5539. });
  5540. def(AST_VarDef, function(compressor) {
  5541. return this.name.may_throw(compressor)
  5542. || this.value && this.value.may_throw(compressor);
  5543. });
  5544. })(function(node, func) {
  5545. node.DEFMETHOD("may_throw", func);
  5546. });
  5547. // determine if expression is constant
  5548. (function(def) {
  5549. function all_constant(list, scope) {
  5550. for (var i = list.length; --i >= 0;)
  5551. if (!list[i].is_constant_expression(scope))
  5552. return false;
  5553. return true;
  5554. }
  5555. def(AST_Node, return_false);
  5556. def(AST_Array, function(scope) {
  5557. return all_constant(this.elements, scope);
  5558. });
  5559. def(AST_Binary, function(scope) {
  5560. return this.left.is_constant_expression(scope)
  5561. && this.right.is_constant_expression(scope)
  5562. && (this.operator != "in" || is_object(this.right));
  5563. });
  5564. def(AST_Class, function(scope) {
  5565. var base = this.extends;
  5566. if (base && !safe_for_extends(base)) return false;
  5567. return all_constant(this.properties, scope);
  5568. });
  5569. def(AST_ClassProperty, function(scope) {
  5570. return typeof this.key == "string" && (!this.value || this.value.is_constant_expression(scope));
  5571. });
  5572. def(AST_Constant, return_true);
  5573. def(AST_Lambda, function(scope) {
  5574. var self = this;
  5575. var result = true;
  5576. var scopes = [];
  5577. self.walk(new TreeWalker(function(node, descend) {
  5578. if (!result) return true;
  5579. if (node instanceof AST_BlockScope) {
  5580. if (node === self) return;
  5581. scopes.push(node);
  5582. descend();
  5583. scopes.pop();
  5584. return true;
  5585. }
  5586. if (node instanceof AST_SymbolRef) {
  5587. if (self.inlined || node.redef) {
  5588. result = false;
  5589. return true;
  5590. }
  5591. if (self.variables.has(node.name)) return true;
  5592. var def = node.definition();
  5593. if (member(def.scope, scopes)) return true;
  5594. if (scope && !def.redefined()) {
  5595. var scope_def = scope.find_variable(node.name);
  5596. if (scope_def ? scope_def === def : def.undeclared) {
  5597. result = "f";
  5598. return true;
  5599. }
  5600. }
  5601. result = false;
  5602. return true;
  5603. }
  5604. if (node instanceof AST_ObjectIdentity) {
  5605. if (is_arrow(self) && all(scopes, function(s) {
  5606. return !(s instanceof AST_Scope) || is_arrow(s);
  5607. })) result = false;
  5608. return true;
  5609. }
  5610. }));
  5611. return result;
  5612. });
  5613. def(AST_Object, function(scope) {
  5614. return all_constant(this.properties, scope);
  5615. });
  5616. def(AST_ObjectProperty, function(scope) {
  5617. return typeof this.key == "string" && this.value.is_constant_expression(scope);
  5618. });
  5619. def(AST_Unary, function(scope) {
  5620. return this.expression.is_constant_expression(scope);
  5621. });
  5622. })(function(node, func) {
  5623. node.DEFMETHOD("is_constant_expression", func);
  5624. });
  5625. // tell me if a statement aborts
  5626. function aborts(thing) {
  5627. return thing && thing.aborts();
  5628. }
  5629. (function(def) {
  5630. def(AST_Statement, return_null);
  5631. def(AST_Jump, return_this);
  5632. function block_aborts() {
  5633. var n = this.body.length;
  5634. return n > 0 && aborts(this.body[n - 1]);
  5635. }
  5636. def(AST_BlockStatement, block_aborts);
  5637. def(AST_SwitchBranch, block_aborts);
  5638. def(AST_If, function() {
  5639. return this.alternative && aborts(this.body) && aborts(this.alternative) && this;
  5640. });
  5641. })(function(node, func) {
  5642. node.DEFMETHOD("aborts", func);
  5643. });
  5644. /* -----[ optimizers ]----- */
  5645. var directives = makePredicate(["use asm", "use strict"]);
  5646. OPT(AST_Directive, function(self, compressor) {
  5647. if (compressor.option("directives")
  5648. && (!directives[self.value] || compressor.has_directive(self.value) !== self)) {
  5649. return make_node(AST_EmptyStatement, self);
  5650. }
  5651. return self;
  5652. });
  5653. OPT(AST_Debugger, function(self, compressor) {
  5654. if (compressor.option("drop_debugger"))
  5655. return make_node(AST_EmptyStatement, self);
  5656. return self;
  5657. });
  5658. OPT(AST_LabeledStatement, function(self, compressor) {
  5659. if (compressor.option("dead_code")
  5660. && self.body instanceof AST_Break
  5661. && compressor.loopcontrol_target(self.body) === self.body) {
  5662. return make_node(AST_EmptyStatement, self);
  5663. }
  5664. return compressor.option("unused") && self.label.references.length == 0 ? self.body : self;
  5665. });
  5666. OPT(AST_LoopControl, function(self, compressor) {
  5667. if (!compressor.option("dead_code")) return self;
  5668. var label = self.label;
  5669. if (label) {
  5670. var lct = compressor.loopcontrol_target(self);
  5671. self.label = null;
  5672. if (compressor.loopcontrol_target(self) === lct) {
  5673. remove(label.thedef.references, self);
  5674. } else {
  5675. self.label = label;
  5676. }
  5677. }
  5678. return self;
  5679. });
  5680. OPT(AST_Block, function(self, compressor) {
  5681. self.body = tighten_body(self.body, compressor);
  5682. return self;
  5683. });
  5684. function trim_block(node, parent, in_list) {
  5685. switch (node.body.length) {
  5686. case 0:
  5687. return in_list ? List.skip : make_node(AST_EmptyStatement, node);
  5688. case 1:
  5689. var stat = node.body[0];
  5690. if (!safe_to_trim(stat)) return node;
  5691. if (parent instanceof AST_IterationStatement && stat instanceof AST_LambdaDefinition) return node;
  5692. return stat;
  5693. }
  5694. return node;
  5695. }
  5696. OPT(AST_BlockStatement, function(self, compressor) {
  5697. self.body = tighten_body(self.body, compressor);
  5698. return trim_block(self, compressor.parent());
  5699. });
  5700. function drop_rest_farg(fn, compressor) {
  5701. if (!compressor.option("rests")) return;
  5702. if (fn.uses_arguments) return;
  5703. if (!(fn.rest instanceof AST_DestructuredArray)) return;
  5704. if (!compressor.drop_fargs(fn, compressor.parent())) return;
  5705. fn.argnames = fn.argnames.concat(fn.rest.elements);
  5706. fn.rest = fn.rest.rest;
  5707. }
  5708. OPT(AST_Lambda, function(self, compressor) {
  5709. drop_rest_farg(self, compressor);
  5710. self.body = tighten_body(self.body, compressor);
  5711. return self;
  5712. });
  5713. function opt_arrow(self, compressor) {
  5714. if (!compressor.option("arrows")) return self;
  5715. drop_rest_farg(self, compressor);
  5716. var body = tighten_body(self.value ? [ self.first_statement() ] : self.body, compressor);
  5717. switch (body.length) {
  5718. case 1:
  5719. var stat = body[0];
  5720. if (stat instanceof AST_Return) {
  5721. self.body.length = 0;
  5722. self.value = stat.value;
  5723. break;
  5724. }
  5725. default:
  5726. self.body = body;
  5727. self.value = null;
  5728. break;
  5729. }
  5730. return self;
  5731. }
  5732. OPT(AST_Arrow, opt_arrow);
  5733. OPT(AST_AsyncArrow, opt_arrow);
  5734. OPT(AST_Function, function(self, compressor) {
  5735. drop_rest_farg(self, compressor);
  5736. self.body = tighten_body(self.body, compressor);
  5737. var parent = compressor.parent();
  5738. if (compressor.option("inline")) for (var i = 0; i < self.body.length; i++) {
  5739. var stat = self.body[i];
  5740. if (stat instanceof AST_Directive) continue;
  5741. if (stat instanceof AST_Return) {
  5742. if (i != self.body.length - 1) break;
  5743. var call = stat.value;
  5744. if (!call || call.TYPE != "Call") break;
  5745. if (call.is_expr_pure(compressor)) break;
  5746. var fn = call.expression;
  5747. if (fn instanceof AST_SymbolRef) {
  5748. if (self.name && self.name.definition() === fn.definition()) break;
  5749. fn = fn.fixed_value();
  5750. }
  5751. if (!(fn instanceof AST_Defun || fn instanceof AST_Function)) break;
  5752. if (fn.rest) break;
  5753. if (fn.uses_arguments) break;
  5754. if (fn === call.expression) {
  5755. if (fn.parent_scope !== self) break;
  5756. if (!all(fn.enclosed, function(def) {
  5757. return def.scope !== self;
  5758. })) break;
  5759. }
  5760. if (fn.name
  5761. && (parent instanceof AST_ClassMethod || parent instanceof AST_ObjectMethod)
  5762. && parent.value === compressor.self()) break;
  5763. if (fn.contains_this()) break;
  5764. var len = fn.argnames.length;
  5765. if (len > 0 && compressor.option("inline") < 2) break;
  5766. if (len > self.argnames.length) break;
  5767. if (!all(self.argnames, function(argname) {
  5768. return argname instanceof AST_SymbolFunarg;
  5769. })) break;
  5770. if (!all(call.args, function(arg) {
  5771. return !(arg instanceof AST_Spread);
  5772. })) break;
  5773. for (var j = 0; j < len; j++) {
  5774. var arg = call.args[j];
  5775. if (!(arg instanceof AST_SymbolRef)) break;
  5776. if (arg.definition() !== self.argnames[j].definition()) break;
  5777. }
  5778. if (j < len) break;
  5779. for (; j < call.args.length; j++) {
  5780. if (call.args[j].has_side_effects(compressor)) break;
  5781. }
  5782. if (j < call.args.length) break;
  5783. if (len < self.argnames.length && !compressor.drop_fargs(self, parent)) {
  5784. if (!compressor.drop_fargs(fn, call)) break;
  5785. do {
  5786. fn.argnames.push(fn.make_var(AST_SymbolFunarg, fn, "argument_" + len));
  5787. } while (++len < self.argnames.length);
  5788. }
  5789. return call.expression;
  5790. }
  5791. break;
  5792. }
  5793. return self;
  5794. });
  5795. var NO_MERGE = makePredicate("arguments await yield");
  5796. AST_Scope.DEFMETHOD("merge_variables", function(compressor) {
  5797. if (!compressor.option("merge_vars")) return;
  5798. var in_try, root, segment = {}, self = this;
  5799. var first = [], last = [], index = 0;
  5800. var declarations = new Dictionary();
  5801. var references = Object.create(null);
  5802. var prev = Object.create(null);
  5803. var tw = new TreeWalker(function(node, descend) {
  5804. if (node instanceof AST_Assign) {
  5805. var lhs = node.left;
  5806. var rhs = node.right;
  5807. if (lhs instanceof AST_Destructured) {
  5808. rhs.walk(tw);
  5809. var marker = new TreeWalker(function(node) {
  5810. if (node instanceof AST_Destructured) return;
  5811. if (node instanceof AST_DefaultValue) {
  5812. push();
  5813. node.value.walk(tw);
  5814. pop();
  5815. node.name.walk(marker);
  5816. } else if (node instanceof AST_DestructuredKeyVal) {
  5817. if (node.key instanceof AST_Node) {
  5818. push();
  5819. segment.block = node;
  5820. node.key.walk(tw);
  5821. node.value.walk(marker);
  5822. pop();
  5823. } else {
  5824. node.value.walk(marker);
  5825. }
  5826. } else if (node instanceof AST_SymbolRef) {
  5827. mark(node);
  5828. } else {
  5829. node.walk(tw);
  5830. }
  5831. return true;
  5832. });
  5833. lhs.walk(marker);
  5834. return true;
  5835. }
  5836. if (lazy_op[node.operator.slice(0, -1)]) {
  5837. lhs.walk(tw);
  5838. push();
  5839. rhs.walk(tw);
  5840. if (lhs instanceof AST_SymbolRef) mark(lhs);
  5841. pop();
  5842. return true;
  5843. }
  5844. if (lhs instanceof AST_SymbolRef) {
  5845. if (node.operator != "=") mark(lhs, true);
  5846. rhs.walk(tw);
  5847. mark(lhs);
  5848. return true;
  5849. }
  5850. return;
  5851. }
  5852. if (node instanceof AST_Binary) {
  5853. if (!lazy_op[node.operator]) return;
  5854. walk_cond(node);
  5855. return true;
  5856. }
  5857. if (node instanceof AST_Break) {
  5858. var target = tw.loopcontrol_target(node);
  5859. if (!(target instanceof AST_IterationStatement)) insert(target);
  5860. return true;
  5861. }
  5862. if (node instanceof AST_Call) {
  5863. var exp = node.expression;
  5864. var tail = exp.tail_node();
  5865. if (!(tail instanceof AST_LambdaExpression)) {
  5866. descend();
  5867. return mark_expression(exp);
  5868. }
  5869. if (exp !== tail) exp.expressions.slice(0, -1).forEach(function(node) {
  5870. node.walk(tw);
  5871. });
  5872. node.args.forEach(function(arg) {
  5873. arg.walk(tw);
  5874. });
  5875. tail.walk(tw);
  5876. return true;
  5877. }
  5878. if (node instanceof AST_Class) {
  5879. if (node.name) node.name.walk(tw);
  5880. if (node.extends) node.extends.walk(tw);
  5881. node.properties.filter(function(prop) {
  5882. if (prop.key instanceof AST_Node) prop.key.walk(tw);
  5883. return prop.value;
  5884. }).forEach(function(prop) {
  5885. if (prop.static) {
  5886. prop.value.walk(tw);
  5887. } else {
  5888. push(tw);
  5889. prop.value.walk(tw);
  5890. pop(tw);
  5891. }
  5892. });
  5893. return true;
  5894. }
  5895. if (node instanceof AST_Conditional) {
  5896. walk_cond(node.condition, node.consequent, node.alternative);
  5897. return true;
  5898. }
  5899. if (node instanceof AST_Continue) {
  5900. var target = tw.loopcontrol_target(node);
  5901. if (target instanceof AST_Do) insert(target);
  5902. return true;
  5903. }
  5904. if (node instanceof AST_Do) {
  5905. push();
  5906. segment.block = node;
  5907. segment.loop = true;
  5908. var save = segment;
  5909. node.body.walk(tw);
  5910. if (segment.inserted === node) segment = save;
  5911. node.condition.walk(tw);
  5912. pop();
  5913. return true;
  5914. }
  5915. if (node instanceof AST_For) {
  5916. if (node.init) node.init.walk(tw);
  5917. push();
  5918. segment.block = node;
  5919. segment.loop = true;
  5920. if (node.condition) node.condition.walk(tw);
  5921. node.body.walk(tw);
  5922. if (node.step) node.step.walk(tw);
  5923. pop();
  5924. return true;
  5925. }
  5926. if (node instanceof AST_ForEnumeration) {
  5927. node.object.walk(tw);
  5928. push();
  5929. segment.block = node;
  5930. segment.loop = true;
  5931. node.init.walk(tw);
  5932. node.body.walk(tw);
  5933. pop();
  5934. return true;
  5935. }
  5936. if (node instanceof AST_If) {
  5937. walk_cond(node.condition, node.body, node.alternative);
  5938. return true;
  5939. }
  5940. if (node instanceof AST_LabeledStatement) {
  5941. push();
  5942. segment.block = node;
  5943. var save = segment;
  5944. node.body.walk(tw);
  5945. if (segment.inserted === node) segment = save;
  5946. pop();
  5947. return true;
  5948. }
  5949. if (node instanceof AST_Scope) {
  5950. push();
  5951. segment.block = node;
  5952. if (node === self) root = segment;
  5953. if (node instanceof AST_Lambda) {
  5954. if (node.name) references[node.name.definition().id] = false;
  5955. var marker = node.uses_arguments && !tw.has_directive("use strict") ? function(node) {
  5956. if (node instanceof AST_SymbolFunarg) references[node.definition().id] = false;
  5957. } : function(node) {
  5958. if (node instanceof AST_SymbolFunarg) mark(node);
  5959. };
  5960. var scanner = new TreeWalker(function(ref) {
  5961. if (ref instanceof AST_SymbolDeclaration) references[ref.definition().id] = false;
  5962. if (!(ref instanceof AST_SymbolRef)) return;
  5963. var def = ref.definition();
  5964. var ldef = node.variables.get(ref.name);
  5965. if (ldef && (ldef === def
  5966. || def.undeclared
  5967. || node.parent_scope.find_variable(ref.name) === def)) {
  5968. references[def.id] = false;
  5969. references[ldef.id] = false;
  5970. } else {
  5971. var save = segment;
  5972. pop();
  5973. mark(ref, true);
  5974. segment = save;
  5975. }
  5976. return true;
  5977. });
  5978. node.argnames.forEach(function(argname) {
  5979. argname.mark_symbol(marker, scanner);
  5980. });
  5981. if (node.rest) node.rest.mark_symbol(marker, scanner);
  5982. }
  5983. walk_lambda(node, tw);
  5984. pop();
  5985. return true;
  5986. }
  5987. if (node instanceof AST_Sub) {
  5988. var exp = node.expression;
  5989. if (node.optional) {
  5990. exp.walk(tw);
  5991. push();
  5992. node.property.walk(tw);
  5993. pop();
  5994. } else {
  5995. descend();
  5996. }
  5997. return mark_expression(exp);
  5998. }
  5999. if (node instanceof AST_Switch) {
  6000. node.expression.walk(tw);
  6001. var save = segment;
  6002. node.body.forEach(function(branch) {
  6003. if (branch instanceof AST_Default) return;
  6004. branch.expression.walk(tw);
  6005. if (save === segment) push();
  6006. });
  6007. segment = save;
  6008. node.body.forEach(function(branch) {
  6009. push();
  6010. segment.block = node;
  6011. var save = segment;
  6012. walk_body(branch, tw);
  6013. if (segment.inserted === node) segment = save;
  6014. pop();
  6015. });
  6016. return true;
  6017. }
  6018. if (node instanceof AST_SymbolConst || node instanceof AST_SymbolLet) {
  6019. references[node.definition().id] = false;
  6020. return true;
  6021. }
  6022. if (node instanceof AST_SymbolRef) {
  6023. mark(node, true);
  6024. return true;
  6025. }
  6026. if (node instanceof AST_Try) {
  6027. var save_try = in_try;
  6028. in_try = node;
  6029. var save = segment;
  6030. walk_body(node, tw);
  6031. segment = save;
  6032. if (node.bcatch) {
  6033. if (node.bcatch.argname) node.bcatch.argname.mark_symbol(function(node) {
  6034. if (node instanceof AST_SymbolCatch) {
  6035. var def = node.definition();
  6036. references[def.id] = false;
  6037. if (def = def.redefined()) references[def.id] = false;
  6038. }
  6039. }, tw);
  6040. if (node.bfinally || (in_try = save_try)) {
  6041. walk_body(node.bcatch, tw);
  6042. } else {
  6043. push();
  6044. walk_body(node.bcatch, tw);
  6045. pop();
  6046. }
  6047. }
  6048. in_try = save_try;
  6049. segment = save;
  6050. if (node.bfinally) node.bfinally.walk(tw);
  6051. return true;
  6052. }
  6053. if (node instanceof AST_Unary) {
  6054. if (!UNARY_POSTFIX[node.operator]) return;
  6055. var sym = node.expression;
  6056. if (!(sym instanceof AST_SymbolRef)) return;
  6057. mark(sym, true);
  6058. return true;
  6059. }
  6060. if (node instanceof AST_VarDef) {
  6061. var assigned = node.value;
  6062. if (assigned) {
  6063. assigned.walk(tw);
  6064. } else {
  6065. assigned = segment.block instanceof AST_ForEnumeration && segment.block.init === tw.parent();
  6066. }
  6067. node.name.mark_symbol(assigned ? function(node) {
  6068. if (!(node instanceof AST_SymbolDeclaration)) return;
  6069. if (node instanceof AST_SymbolVar) {
  6070. mark(node);
  6071. } else {
  6072. references[node.definition().id] = false;
  6073. }
  6074. return true;
  6075. } : function(node) {
  6076. if (!(node instanceof AST_SymbolDeclaration)) return;
  6077. var id = node.definition().id;
  6078. if (!(node instanceof AST_SymbolVar)) {
  6079. references[id] = false;
  6080. } else if (!(id in references)) {
  6081. declarations.add(id, node);
  6082. } else if (references[id]) {
  6083. references[id].push(node);
  6084. }
  6085. return true;
  6086. }, tw);
  6087. return true;
  6088. }
  6089. if (node instanceof AST_While) {
  6090. push();
  6091. segment.block = node;
  6092. segment.loop = true;
  6093. descend();
  6094. pop();
  6095. return true;
  6096. }
  6097. function mark_expression(exp) {
  6098. if (compressor.option("ie")) {
  6099. var sym = root_expr(exp);
  6100. if (sym instanceof AST_SymbolRef) sym.walk(tw);
  6101. }
  6102. return true;
  6103. }
  6104. function walk_cond(condition, consequent, alternative) {
  6105. var save = segment;
  6106. var segments = [ save, save ];
  6107. if (condition instanceof AST_Binary) switch (condition.operator) {
  6108. case "&&":
  6109. segments[0] = walk_cond(condition.left, condition.right)[0];
  6110. break;
  6111. case "||":
  6112. case "??":
  6113. segments[1] = walk_cond(condition.left, null, condition.right)[1];
  6114. break;
  6115. default:
  6116. condition.walk(tw);
  6117. break;
  6118. } else if (condition instanceof AST_Conditional) {
  6119. walk_cond(condition.condition, condition.consequent, condition.alternative);
  6120. } else {
  6121. condition.walk(tw);
  6122. }
  6123. segment = segments[0];
  6124. if (consequent) {
  6125. push();
  6126. consequent.walk(tw);
  6127. }
  6128. segments[0] = segment;
  6129. segment = segments[1];
  6130. if (alternative) {
  6131. push();
  6132. alternative.walk(tw);
  6133. }
  6134. segments[1] = segment;
  6135. segment = save;
  6136. return segments;
  6137. }
  6138. });
  6139. tw.directives = Object.create(compressor.directives);
  6140. self.walk(tw);
  6141. var changed = false;
  6142. var merged = Object.create(null);
  6143. while (first.length && last.length) {
  6144. var tail = last.shift();
  6145. if (!tail) continue;
  6146. var def = tail.definition;
  6147. var tail_refs = references[def.id];
  6148. if (!tail_refs) continue;
  6149. tail_refs = { end: tail_refs.end };
  6150. while (def.id in merged) def = merged[def.id];
  6151. tail_refs.start = references[def.id].start;
  6152. var skipped = [];
  6153. do {
  6154. var head = first.shift();
  6155. if (tail.index > head.index) continue;
  6156. var id = head.definition.id;
  6157. if (!(id in prev)) continue;
  6158. var head_refs = references[id];
  6159. if (!head_refs) continue;
  6160. if (head_refs.start.block !== tail_refs.start.block
  6161. || !mergeable(head_refs, tail_refs)
  6162. || (head_refs.start.loop || !same_scope(def)) && !mergeable(tail_refs, head_refs)
  6163. || compressor.option("webkit") && is_funarg(def) !== is_funarg(head.definition)
  6164. || head.definition.const_redefs
  6165. || !all(head_refs, function(sym) {
  6166. return sym.scope.find_variable(def.name) === def;
  6167. })) {
  6168. skipped.push(head);
  6169. continue;
  6170. }
  6171. head_refs.forEach(function(sym) {
  6172. sym.thedef = def;
  6173. sym.name = def.name;
  6174. if (sym instanceof AST_SymbolRef) {
  6175. def.references.push(sym);
  6176. } else {
  6177. def.orig.push(sym);
  6178. }
  6179. });
  6180. if (!head.definition.fixed) def.fixed = false;
  6181. merged[id] = def;
  6182. changed = true;
  6183. break;
  6184. } while (first.length);
  6185. if (skipped.length) first = skipped.concat(first);
  6186. }
  6187. return changed;
  6188. function push() {
  6189. segment = Object.create(segment);
  6190. }
  6191. function pop() {
  6192. segment = Object.getPrototypeOf(segment);
  6193. }
  6194. function mark(sym, read) {
  6195. var def = sym.definition(), ldef, seg = segment;
  6196. if (in_try) {
  6197. push();
  6198. seg = segment;
  6199. pop();
  6200. }
  6201. if (def.id in references) {
  6202. var refs = references[def.id];
  6203. if (!refs) return;
  6204. if (refs.start.block !== seg.block) return references[def.id] = false;
  6205. refs.push(sym);
  6206. refs.end = seg;
  6207. if (def.id in prev) {
  6208. last[prev[def.id]] = null;
  6209. } else if (!read) {
  6210. return;
  6211. }
  6212. } else if ((ldef = self.variables.get(def.name)) !== def) {
  6213. if (ldef && root === seg) references[ldef.id] = false;
  6214. return references[def.id] = false;
  6215. } else if (compressor.exposed(def) || NO_MERGE[sym.name]) {
  6216. return references[def.id] = false;
  6217. } else {
  6218. var refs = declarations.get(def.id) || [];
  6219. refs.push(sym);
  6220. references[def.id] = refs;
  6221. if (!read) {
  6222. refs.start = seg;
  6223. return first.push({
  6224. index: index++,
  6225. definition: def,
  6226. });
  6227. }
  6228. if (seg.block !== self) return references[def.id] = false;
  6229. refs.start = root;
  6230. }
  6231. prev[def.id] = last.length;
  6232. last.push({
  6233. index: index++,
  6234. definition: def,
  6235. });
  6236. }
  6237. function insert(target) {
  6238. var stack = [];
  6239. while (true) {
  6240. if (HOP(segment, "block")) {
  6241. var block = segment.block;
  6242. if (block instanceof AST_LabeledStatement) block = block.body;
  6243. if (block === target) break;
  6244. }
  6245. stack.push(segment);
  6246. pop();
  6247. }
  6248. segment.inserted = segment.block;
  6249. push();
  6250. while (stack.length) {
  6251. var seg = stack.pop();
  6252. push();
  6253. if (HOP(seg, "block")) segment.block = seg.block;
  6254. if (HOP(seg, "loop")) segment.loop = seg.loop;
  6255. }
  6256. }
  6257. function must_visit(base, segment) {
  6258. return base === segment || base.isPrototypeOf(segment);
  6259. }
  6260. function mergeable(head, tail) {
  6261. return must_visit(head.start, head.end) || must_visit(head.start, tail.start);
  6262. }
  6263. });
  6264. function fill_holes(orig, elements) {
  6265. for (var i = elements.length; --i >= 0;) {
  6266. if (!elements[i]) elements[i] = make_node(AST_Hole, orig);
  6267. }
  6268. }
  6269. function to_class_expr(defcl, drop_name) {
  6270. var cl = make_node(AST_ClassExpression, defcl, defcl);
  6271. if (cl.name) cl.name = drop_name ? null : make_node(AST_SymbolClass, cl.name, cl.name);
  6272. return cl;
  6273. }
  6274. function to_func_expr(defun, drop_name) {
  6275. var ctor;
  6276. switch (defun.CTOR) {
  6277. case AST_AsyncDefun:
  6278. ctor = AST_AsyncFunction;
  6279. break;
  6280. case AST_AsyncGeneratorDefun:
  6281. ctor = AST_AsyncGeneratorFunction;
  6282. break;
  6283. case AST_Defun:
  6284. ctor = AST_Function;
  6285. break;
  6286. case AST_GeneratorDefun:
  6287. ctor = AST_GeneratorFunction;
  6288. break;
  6289. }
  6290. var fn = make_node(ctor, defun, defun);
  6291. fn.name = drop_name ? null : make_node(AST_SymbolLambda, defun.name, defun.name);
  6292. return fn;
  6293. }
  6294. AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
  6295. if (!compressor.option("unused")) return;
  6296. var self = this;
  6297. var drop_funcs = !(self instanceof AST_Toplevel) || compressor.toplevel.funcs;
  6298. var drop_vars = !(self instanceof AST_Toplevel) || compressor.toplevel.vars;
  6299. var assign_as_unused = /keep_assign/.test(compressor.option("unused")) ? return_false : function(node, props) {
  6300. var sym, nested = false;
  6301. if (node instanceof AST_Assign) {
  6302. if (node.write_only || node.operator == "=") sym = extract_reference(node.left, props);
  6303. } else if (node instanceof AST_Unary) {
  6304. if (node.write_only) sym = extract_reference(node.expression, props);
  6305. }
  6306. if (!(sym instanceof AST_SymbolRef)) return;
  6307. var def = sym.definition();
  6308. if (export_defaults[def.id]) return;
  6309. if (compressor.exposed(def)) return;
  6310. if (!can_drop_symbol(sym, compressor, nested)) return;
  6311. return sym;
  6312. function extract_reference(node, props) {
  6313. if (node instanceof AST_PropAccess) {
  6314. var expr = node.expression;
  6315. if (!expr.may_throw_on_access(compressor, true)) {
  6316. nested = true;
  6317. if (props && node instanceof AST_Sub) props.unshift(node.property);
  6318. return extract_reference(expr, props);
  6319. }
  6320. } else if (node instanceof AST_Assign && node.operator == "=") {
  6321. node.write_only = "p";
  6322. var ref = extract_reference(node.right);
  6323. if (!props) return ref;
  6324. props.assign = node;
  6325. return ref instanceof AST_SymbolRef ? ref : node.left;
  6326. }
  6327. return node;
  6328. }
  6329. };
  6330. var assign_in_use = Object.create(null);
  6331. var export_defaults = Object.create(null);
  6332. var find_variable = function(name) {
  6333. find_variable = compose(self, 0, noop);
  6334. return find_variable(name);
  6335. function compose(child, level, find) {
  6336. var parent = compressor.parent(level);
  6337. if (!parent) return find;
  6338. var in_arg = parent instanceof AST_Lambda && member(child, parent.argnames);
  6339. return compose(parent, level + 1, in_arg ? function(name) {
  6340. var def = find(name);
  6341. if (def) return def;
  6342. def = parent.variables.get(name);
  6343. if (def) {
  6344. var sym = def.orig[0];
  6345. if (sym instanceof AST_SymbolFunarg || sym instanceof AST_SymbolLambda) return def;
  6346. }
  6347. } : parent.variables ? function(name) {
  6348. return find(name) || parent.variables.get(name);
  6349. } : find);
  6350. }
  6351. };
  6352. var for_ins = Object.create(null);
  6353. var in_use = [];
  6354. var in_use_ids = Object.create(null); // avoid expensive linear scans of in_use
  6355. var value_read = Object.create(null);
  6356. var value_modified = Object.create(null);
  6357. var var_defs = Object.create(null);
  6358. if (self instanceof AST_Toplevel && compressor.top_retain) {
  6359. self.variables.each(function(def) {
  6360. if (compressor.top_retain(def) && !(def.id in in_use_ids)) {
  6361. in_use_ids[def.id] = true;
  6362. in_use.push(def);
  6363. }
  6364. });
  6365. }
  6366. var assignments = new Dictionary();
  6367. var initializations = new Dictionary();
  6368. // pass 1: find out which symbols are directly used in
  6369. // this scope (not in nested scopes).
  6370. var scope = this;
  6371. var tw = new TreeWalker(function(node, descend) {
  6372. if (node instanceof AST_Lambda && node.uses_arguments && !tw.has_directive("use strict")) {
  6373. node.each_argname(function(argname) {
  6374. var def = argname.definition();
  6375. if (!(def.id in in_use_ids)) {
  6376. in_use_ids[def.id] = true;
  6377. in_use.push(def);
  6378. }
  6379. });
  6380. }
  6381. if (node === self) return;
  6382. if (scope === self) {
  6383. if (node instanceof AST_DefClass) {
  6384. var def = node.name.definition();
  6385. var drop = drop_funcs && !def.exported;
  6386. if (!drop && !(def.id in in_use_ids)) {
  6387. in_use_ids[def.id] = true;
  6388. in_use.push(def);
  6389. }
  6390. if (node.extends) node.extends.walk(tw);
  6391. var used = tw.parent() instanceof AST_ExportDefault;
  6392. if (used) export_defaults[def.id] = true;
  6393. var values = [];
  6394. node.properties.forEach(function(prop) {
  6395. if (prop.key instanceof AST_Node) prop.key.walk(tw);
  6396. var value = prop.value;
  6397. if (!value) return;
  6398. if (prop instanceof AST_ClassField && prop.static) {
  6399. if (!used && value.contains_this()) used = true;
  6400. walk_class_prop(value);
  6401. } else {
  6402. values.push(value);
  6403. }
  6404. });
  6405. values.forEach(drop && used ? walk_class_prop : function(value) {
  6406. initializations.add(def.id, value);
  6407. });
  6408. return true;
  6409. }
  6410. if (node instanceof AST_LambdaDefinition) {
  6411. var def = node.name.definition();
  6412. if ((!drop_funcs || def.exported) && !(def.id in in_use_ids)) {
  6413. in_use_ids[def.id] = true;
  6414. in_use.push(def);
  6415. }
  6416. initializations.add(def.id, node);
  6417. if (tw.parent() instanceof AST_ExportDefault) {
  6418. export_defaults[def.id] = true;
  6419. } else {
  6420. return true;
  6421. }
  6422. }
  6423. if (node instanceof AST_Definitions) {
  6424. node.definitions.forEach(function(defn) {
  6425. var value = defn.value;
  6426. var side_effects = value
  6427. && (defn.name instanceof AST_Destructured || value.has_side_effects(compressor));
  6428. var shared = side_effects && value.tail_node().operator == "=";
  6429. defn.name.mark_symbol(function(name) {
  6430. if (!(name instanceof AST_SymbolDeclaration)) return;
  6431. var def = name.definition();
  6432. var_defs[def.id] = (var_defs[def.id] || 0) + 1;
  6433. if (node instanceof AST_Var && def.orig[0] instanceof AST_SymbolCatch) {
  6434. var redef = def.redefined();
  6435. if (redef) var_defs[redef.id] = (var_defs[redef.id] || 0) + 1;
  6436. }
  6437. if (!(def.id in in_use_ids) && (!drop_vars || def.exported
  6438. || (node instanceof AST_Const ? def.redefined() : def.const_redefs)
  6439. || !(node instanceof AST_Var || is_safe_lexical(def)))) {
  6440. in_use_ids[def.id] = true;
  6441. in_use.push(def);
  6442. }
  6443. if (value) {
  6444. if (!side_effects) {
  6445. initializations.add(def.id, value);
  6446. } else if (shared) {
  6447. verify_safe_usage(def, name, value_modified[def.id]);
  6448. }
  6449. assignments.add(def.id, defn);
  6450. }
  6451. return true;
  6452. }, tw);
  6453. if (side_effects) value.walk(tw);
  6454. });
  6455. return true;
  6456. }
  6457. if (node instanceof AST_SymbolFunarg) {
  6458. var def = node.definition();
  6459. var_defs[def.id] = (var_defs[def.id] || 0) + 1;
  6460. assignments.add(def.id, node);
  6461. return true;
  6462. }
  6463. if (node instanceof AST_SymbolImport) {
  6464. var def = node.definition();
  6465. if (!(def.id in in_use_ids) && (!drop_vars || !is_safe_lexical(def))) {
  6466. in_use_ids[def.id] = true;
  6467. in_use.push(def);
  6468. }
  6469. return true;
  6470. }
  6471. }
  6472. return scan_ref_scoped(node, descend, true);
  6473. function walk_class_prop(value) {
  6474. var save_scope = scope;
  6475. scope = node;
  6476. value.walk(tw);
  6477. scope = save_scope;
  6478. }
  6479. });
  6480. tw.directives = Object.create(compressor.directives);
  6481. self.walk(tw);
  6482. var drop_fn_name = compressor.option("keep_fnames") ? return_false : compressor.option("ie") ? function(def) {
  6483. return !compressor.exposed(def) && def.references.length == def.replaced;
  6484. } : function(def) {
  6485. if (!(def.id in in_use_ids)) return true;
  6486. if (def.orig.length - def.eliminated < 2) return false;
  6487. // function argument will always overshadow its name
  6488. if (def.orig[1] instanceof AST_SymbolFunarg) return true;
  6489. // retain if referenced within destructured object of argument
  6490. return all(def.references, function(ref) {
  6491. return !ref.in_arg;
  6492. });
  6493. };
  6494. if (compressor.option("ie")) initializations.each(function(init, id) {
  6495. if (id in in_use_ids) return;
  6496. init.forEach(function(init) {
  6497. init.walk(new TreeWalker(function(node) {
  6498. if (node instanceof AST_Function && node.name && !drop_fn_name(node.name.definition())) {
  6499. node.walk(tw);
  6500. return true;
  6501. }
  6502. if (node instanceof AST_Scope) return true;
  6503. }));
  6504. });
  6505. });
  6506. // pass 2: for every used symbol we need to walk its
  6507. // initialization code to figure out if it uses other
  6508. // symbols (that may not be in_use).
  6509. tw = new TreeWalker(scan_ref_scoped);
  6510. for (var i = 0; i < in_use.length; i++) {
  6511. var init = initializations.get(in_use[i].id);
  6512. if (init) init.forEach(function(init) {
  6513. init.walk(tw);
  6514. });
  6515. }
  6516. Object.keys(assign_in_use).forEach(function(id) {
  6517. var assigns = assign_in_use[id];
  6518. if (!assigns) {
  6519. delete assign_in_use[id];
  6520. return;
  6521. }
  6522. assigns = assigns.reduce(function(in_use, assigns) {
  6523. assigns.forEach(function(assign) {
  6524. push_uniq(in_use, assign);
  6525. });
  6526. return in_use;
  6527. }, []);
  6528. var in_use = (assignments.get(id) || []).filter(function(node) {
  6529. return find_if(node instanceof AST_Unary ? function(assign) {
  6530. return assign === node;
  6531. } : function(assign) {
  6532. if (assign === node) return true;
  6533. if (assign instanceof AST_Unary) return false;
  6534. return get_rvalue(assign) === get_rvalue(node);
  6535. }, assigns);
  6536. });
  6537. if (assigns.length == in_use.length) {
  6538. assign_in_use[id] = in_use;
  6539. } else {
  6540. delete assign_in_use[id];
  6541. }
  6542. });
  6543. // pass 3: we should drop declarations not in_use
  6544. var unused_fn_names = [];
  6545. var calls_to_drop_args = [];
  6546. var fns_with_marked_args = [];
  6547. var trimmer = new TreeTransformer(function(node) {
  6548. if (node instanceof AST_DefaultValue) return trim_default(trimmer, node);
  6549. if (node instanceof AST_Destructured && node.rest) node.rest = node.rest.transform(trimmer);
  6550. if (node instanceof AST_DestructuredArray) {
  6551. var trim = !node.rest;
  6552. for (var i = node.elements.length; --i >= 0;) {
  6553. var element = node.elements[i].transform(trimmer);
  6554. if (element) {
  6555. node.elements[i] = element;
  6556. trim = false;
  6557. } else if (trim) {
  6558. node.elements.pop();
  6559. } else {
  6560. node.elements[i] = make_node(AST_Hole, node.elements[i]);
  6561. }
  6562. }
  6563. return node;
  6564. }
  6565. if (node instanceof AST_DestructuredObject) {
  6566. var properties = [];
  6567. node.properties.forEach(function(prop) {
  6568. var retain = false;
  6569. if (prop.key instanceof AST_Node) {
  6570. prop.key = prop.key.transform(tt);
  6571. retain = prop.key.has_side_effects(compressor);
  6572. }
  6573. if ((retain || node.rest) && is_decl(prop.value)) {
  6574. prop.value = prop.value.transform(tt);
  6575. properties.push(prop);
  6576. } else {
  6577. var value = prop.value.transform(trimmer);
  6578. if (!value && node.rest) {
  6579. if (prop.value instanceof AST_DestructuredArray) {
  6580. value = make_node(AST_DestructuredArray, prop.value, { elements: [] });
  6581. } else {
  6582. value = make_node(AST_DestructuredObject, prop.value, { properties: [] });
  6583. }
  6584. }
  6585. if (value) {
  6586. prop.value = value;
  6587. properties.push(prop);
  6588. }
  6589. }
  6590. });
  6591. node.properties = properties;
  6592. return node;
  6593. }
  6594. if (node instanceof AST_SymbolDeclaration) return node.definition().id in in_use_ids ? node : null;
  6595. });
  6596. var tt = new TreeTransformer(function(node, descend, in_list) {
  6597. var parent = tt.parent();
  6598. if (drop_vars) {
  6599. var props = [], sym = assign_as_unused(node, props);
  6600. if (sym) {
  6601. var value;
  6602. if (can_drop_lhs(sym, node)) {
  6603. if (node instanceof AST_Assign) {
  6604. value = get_rhs(node);
  6605. if (node.write_only === true) value = value.drop_side_effect_free(compressor);
  6606. }
  6607. if (!value) value = make_node(AST_Number, node, { value: 0 });
  6608. }
  6609. if (value) {
  6610. if (props.assign) {
  6611. var assign = props.assign.drop_side_effect_free(compressor);
  6612. if (assign) {
  6613. assign.write_only = true;
  6614. props.unshift(assign);
  6615. }
  6616. }
  6617. if (!(parent instanceof AST_Sequence)
  6618. || parent.tail_node() === node
  6619. || value.has_side_effects(compressor)) {
  6620. props.push(value);
  6621. }
  6622. switch (props.length) {
  6623. case 0:
  6624. return List.skip;
  6625. case 1:
  6626. return maintain_this_binding(compressor, parent, node, props[0].transform(tt));
  6627. default:
  6628. return make_sequence(node, props.map(function(prop) {
  6629. return prop.transform(tt);
  6630. }));
  6631. }
  6632. }
  6633. } else if (node instanceof AST_UnaryPostfix
  6634. && node.expression instanceof AST_SymbolRef
  6635. && indexOf_assign(node.expression.definition(), node) < 0) {
  6636. return make_node(AST_UnaryPrefix, node, {
  6637. operator: "+",
  6638. expression: node.expression,
  6639. });
  6640. }
  6641. }
  6642. if (node instanceof AST_Call) {
  6643. calls_to_drop_args.push(node);
  6644. node.args = node.args.map(function(arg) {
  6645. return arg.transform(tt);
  6646. });
  6647. node.expression = node.expression.transform(tt);
  6648. return node;
  6649. }
  6650. if (scope !== self) return;
  6651. if (drop_funcs && node !== self && node instanceof AST_DefClass) {
  6652. var def = node.name.definition();
  6653. if (!(def.id in in_use_ids)) {
  6654. log(node.name, "Dropping unused class {name}");
  6655. def.eliminated++;
  6656. descend(node, tt);
  6657. var trimmed = to_class_expr(node, true);
  6658. if (parent instanceof AST_ExportDefault) return trimmed;
  6659. trimmed = trimmed.drop_side_effect_free(compressor, true);
  6660. if (trimmed) return make_node(AST_SimpleStatement, node, { body: trimmed });
  6661. return in_list ? List.skip : make_node(AST_EmptyStatement, node);
  6662. }
  6663. }
  6664. if (node instanceof AST_ClassExpression && node.name && drop_fn_name(node.name.definition())) {
  6665. unused_fn_names.push(node);
  6666. }
  6667. if (node instanceof AST_Lambda) {
  6668. if (drop_funcs && node !== self && node instanceof AST_LambdaDefinition) {
  6669. var def = node.name.definition();
  6670. if (!(def.id in in_use_ids)) {
  6671. log(node.name, "Dropping unused function {name}");
  6672. def.eliminated++;
  6673. if (parent instanceof AST_ExportDefault) {
  6674. descend_scope();
  6675. return to_func_expr(node, true);
  6676. }
  6677. return in_list ? List.skip : make_node(AST_EmptyStatement, node);
  6678. }
  6679. }
  6680. if (node instanceof AST_LambdaExpression && node.name && drop_fn_name(node.name.definition())) {
  6681. unused_fn_names.push(node);
  6682. }
  6683. if (!(node instanceof AST_Accessor)) {
  6684. var args, spread, trim = compressor.drop_fargs(node, parent);
  6685. if (trim && parent instanceof AST_Call && parent.expression === node) {
  6686. args = parent.args;
  6687. for (spread = 0; spread < args.length; spread++) {
  6688. if (args[spread] instanceof AST_Spread) break;
  6689. }
  6690. }
  6691. var argnames = node.argnames;
  6692. var rest = node.rest;
  6693. if (rest) {
  6694. if (!args || spread < argnames.length || rest instanceof AST_SymbolFunarg) {
  6695. rest = rest.transform(trimmer);
  6696. } else {
  6697. var trimmed = trim_destructured(rest, make_node(AST_Array, parent, {
  6698. elements: args.slice(argnames.length),
  6699. }), function(node) {
  6700. return node.definition().id in in_use_ids ? node : null;
  6701. }, !node.uses_arguments, rest);
  6702. rest = trimmed.name;
  6703. args.length = argnames.length;
  6704. if (trimmed.value.elements.length) [].push.apply(args, trimmed.value.elements);
  6705. }
  6706. if (rest instanceof AST_Destructured && !rest.rest
  6707. && (!node.uses_arguments || tt.has_directive("use strict"))) {
  6708. if (rest instanceof AST_DestructuredArray) {
  6709. if (rest.elements.length == 0) rest = null;
  6710. } else if (rest.properties.length == 0) {
  6711. rest = null;
  6712. }
  6713. }
  6714. node.rest = rest;
  6715. if (rest) trim = false;
  6716. }
  6717. var default_length = trim ? -1 : node.length();
  6718. for (var i = argnames.length; --i >= 0;) {
  6719. var sym = argnames[i];
  6720. if (sym instanceof AST_SymbolFunarg) {
  6721. var def = sym.definition();
  6722. if (def.id in in_use_ids) {
  6723. trim = false;
  6724. if (indexOf_assign(def, sym) < 0) sym.unused = null;
  6725. } else if (trim) {
  6726. log(sym, "Dropping unused function argument {name}");
  6727. argnames.pop();
  6728. } else {
  6729. sym.unused = true;
  6730. }
  6731. } else {
  6732. var funarg;
  6733. if (!args || spread < i) {
  6734. funarg = sym.transform(trimmer);
  6735. } else {
  6736. funarg = trim_destructured(sym, args[i], function(node) {
  6737. return node.definition().id in in_use_ids ? node : null;
  6738. }, !node.uses_arguments, sym).name;
  6739. }
  6740. if (funarg) {
  6741. trim = false;
  6742. } else if (trim) {
  6743. log_default(sym, "Dropping unused default argument {name}");
  6744. argnames.pop();
  6745. } else if (i > default_length) {
  6746. log_default(sym, "Dropping unused default argument assignment {name}");
  6747. if (sym.name instanceof AST_SymbolFunarg) sym.name.unused = true;
  6748. argnames[i] = sym.name;
  6749. } else {
  6750. log_default(sym, "Dropping unused default argument value {name}");
  6751. sym.value = make_node(AST_Number, sym, { value: 0 });
  6752. }
  6753. }
  6754. }
  6755. fns_with_marked_args.push(node);
  6756. }
  6757. }
  6758. if (node instanceof AST_Catch && node.argname instanceof AST_Destructured) {
  6759. node.argname.transform(trimmer);
  6760. }
  6761. if (node instanceof AST_Definitions && !(parent instanceof AST_ForEnumeration && parent.init === node)) {
  6762. // place uninitialized names at the start
  6763. var body = [], head = [], tail = [];
  6764. // for unused names whose initialization has
  6765. // side effects, we can cascade the init. code
  6766. // into the next one, or next statement.
  6767. var side_effects = [];
  6768. var duplicated = 0;
  6769. var is_var = node instanceof AST_Var;
  6770. node.definitions.forEach(function(def) {
  6771. if (def.value) def.value = def.value.transform(tt);
  6772. var value = def.value;
  6773. if (def.name instanceof AST_Destructured) {
  6774. var trimmed = trim_destructured(def.name, value, function(node) {
  6775. if (!drop_vars) return node;
  6776. if (node.definition().id in in_use_ids) return node;
  6777. if (is_catch(node)) return node;
  6778. if (is_var && !can_drop_symbol(node)) return node;
  6779. return null;
  6780. }, true);
  6781. if (trimmed.name) {
  6782. def = make_node(AST_VarDef, def, {
  6783. name: trimmed.name,
  6784. value: value = trimmed.value,
  6785. });
  6786. flush();
  6787. } else if (trimmed.value) {
  6788. side_effects.push(trimmed.value);
  6789. }
  6790. return;
  6791. }
  6792. var sym = def.name.definition();
  6793. var drop_sym = is_var ? can_drop_symbol(def.name) : is_safe_lexical(sym);
  6794. if (!drop_sym || !drop_vars || sym.id in in_use_ids) {
  6795. var index;
  6796. if (value && ((index = indexOf_assign(sym, def)) < 0 || self_assign(value.tail_node()))) {
  6797. def = def.clone();
  6798. value = value.drop_side_effect_free(compressor);
  6799. if (value) AST_Node.warn("Side effects in definition of variable {name} [{file}:{line},{col}]", template(def.name));
  6800. if (node instanceof AST_Const) {
  6801. def.value = value || make_node(AST_Number, def, { value: 0 });
  6802. } else {
  6803. def.value = null;
  6804. if (value) side_effects.push(value);
  6805. }
  6806. value = null;
  6807. if (index >= 0) assign_in_use[sym.id][index] = def;
  6808. }
  6809. var old_def, fn;
  6810. if (!value && !(node instanceof AST_Let)) {
  6811. if (parent instanceof AST_ExportDeclaration) {
  6812. flush();
  6813. } else if (drop_sym && var_defs[sym.id] > 1) {
  6814. AST_Node.info("Dropping declaration of variable {name} [{file}:{line},{col}]", template(def.name));
  6815. var_defs[sym.id]--;
  6816. sym.eliminated++;
  6817. } else {
  6818. head.push(def);
  6819. }
  6820. } else if (compressor.option("functions")
  6821. && !compressor.option("ie")
  6822. && drop_sym
  6823. && value
  6824. && var_defs[sym.id] == 1
  6825. && sym.assignments == 0
  6826. && (fn = value.tail_node()) instanceof AST_LambdaExpression
  6827. && !is_arguments(sym)
  6828. && !is_arrow(fn)
  6829. && assigned_once(fn, sym.references)
  6830. && can_declare_defun(fn)
  6831. && (old_def = rename_def(fn, def.name.name)) !== false) {
  6832. AST_Node.warn("Declaring {name} as function [{file}:{line},{col}]", template(def.name));
  6833. var ctor;
  6834. switch (fn.CTOR) {
  6835. case AST_AsyncFunction:
  6836. ctor = AST_AsyncDefun;
  6837. break;
  6838. case AST_AsyncGeneratorFunction:
  6839. ctor = AST_AsyncGeneratorDefun;
  6840. break;
  6841. case AST_Function:
  6842. ctor = AST_Defun;
  6843. break;
  6844. case AST_GeneratorFunction:
  6845. ctor = AST_GeneratorDefun;
  6846. break;
  6847. }
  6848. var defun = make_node(ctor, def, fn);
  6849. defun.name = make_node(AST_SymbolDefun, def.name, def.name);
  6850. var name_def = def.name.scope.resolve().def_function(defun.name);
  6851. if (old_def) old_def.forEach(function(node) {
  6852. node.name = name_def.name;
  6853. node.thedef = name_def;
  6854. node.reference();
  6855. });
  6856. body.push(defun);
  6857. if (value !== fn) [].push.apply(side_effects, value.expressions.slice(0, -1));
  6858. } else {
  6859. if (drop_sym
  6860. && var_defs[sym.id] > 1
  6861. && !(parent instanceof AST_ExportDeclaration)
  6862. && sym.orig.indexOf(def.name) > sym.eliminated) {
  6863. var_defs[sym.id]--;
  6864. duplicated++;
  6865. }
  6866. flush();
  6867. }
  6868. } else if (is_catch(def.name)) {
  6869. value = value && value.drop_side_effect_free(compressor);
  6870. if (value) side_effects.push(value);
  6871. if (var_defs[sym.id] > 1) {
  6872. AST_Node.warn("Dropping duplicated declaration of variable {name} [{file}:{line},{col}]", template(def.name));
  6873. var_defs[sym.id]--;
  6874. sym.eliminated++;
  6875. } else {
  6876. def.value = null;
  6877. head.push(def);
  6878. }
  6879. } else {
  6880. value = value && value.drop_side_effect_free(compressor);
  6881. if (value) {
  6882. AST_Node.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", template(def.name));
  6883. side_effects.push(value);
  6884. } else {
  6885. log(def.name, "Dropping unused variable {name}");
  6886. }
  6887. sym.eliminated++;
  6888. }
  6889. function self_assign(ref) {
  6890. return ref instanceof AST_SymbolRef && ref.definition() === sym;
  6891. }
  6892. function assigned_once(fn, refs) {
  6893. if (refs.length == 0) return fn === def.name.fixed_value();
  6894. return all(refs, function(ref) {
  6895. return fn === ref.fixed_value();
  6896. });
  6897. }
  6898. function can_declare_defun(fn) {
  6899. if (!is_var || compressor.has_directive("use strict") || !(fn instanceof AST_Function)) {
  6900. return parent instanceof AST_Scope;
  6901. }
  6902. return parent instanceof AST_Block
  6903. || parent instanceof AST_For && parent.init === node
  6904. || parent instanceof AST_If;
  6905. }
  6906. function rename_def(fn, name) {
  6907. if (!fn.name) return null;
  6908. var def = fn.name.definition();
  6909. if (def.orig.length > 1) return null;
  6910. if (def.assignments > 0) return false;
  6911. if (def.name == name) return def;
  6912. if (compressor.option("keep_fnames")) return false;
  6913. var forbidden;
  6914. switch (name) {
  6915. case "await":
  6916. forbidden = is_async;
  6917. break;
  6918. case "yield":
  6919. forbidden = is_generator;
  6920. break;
  6921. }
  6922. return all(def.references, function(ref) {
  6923. var scope = ref.scope;
  6924. if (scope.find_variable(name) !== sym) return false;
  6925. if (forbidden) do {
  6926. scope = scope.resolve();
  6927. if (forbidden(scope)) return false;
  6928. } while (scope !== fn && (scope = scope.parent_scope));
  6929. return true;
  6930. }) && def;
  6931. }
  6932. function is_catch(node) {
  6933. var sym = node.definition();
  6934. return sym.orig[0] instanceof AST_SymbolCatch && sym.scope.resolve() === node.scope.resolve();
  6935. }
  6936. function flush() {
  6937. if (side_effects.length > 0) {
  6938. if (tail.length == 0) {
  6939. body.push(make_node(AST_SimpleStatement, node, {
  6940. body: make_sequence(node, side_effects),
  6941. }));
  6942. } else if (value) {
  6943. side_effects.push(value);
  6944. def.value = make_sequence(value, side_effects);
  6945. } else {
  6946. def.value = make_node(AST_UnaryPrefix, def, {
  6947. operator: "void",
  6948. expression: make_sequence(def, side_effects),
  6949. });
  6950. }
  6951. side_effects = [];
  6952. }
  6953. tail.push(def);
  6954. }
  6955. });
  6956. switch (head.length) {
  6957. case 0:
  6958. if (tail.length == 0) break;
  6959. if (tail.length == duplicated) {
  6960. [].unshift.apply(side_effects, tail.map(function(def) {
  6961. AST_Node.info("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
  6962. var sym = def.name.definition();
  6963. var ref = make_node(AST_SymbolRef, def.name, def.name);
  6964. sym.references.push(ref);
  6965. var assign = make_node(AST_Assign, def, {
  6966. operator: "=",
  6967. left: ref,
  6968. right: def.value,
  6969. });
  6970. var index = indexOf_assign(sym, def);
  6971. if (index >= 0) assign_in_use[sym.id][index] = assign;
  6972. sym.eliminated++;
  6973. return assign;
  6974. }));
  6975. break;
  6976. }
  6977. case 1:
  6978. if (tail.length == 0) {
  6979. var id = head[0].name.definition().id;
  6980. if (id in for_ins) {
  6981. node.definitions = head;
  6982. for_ins[id].init = node;
  6983. break;
  6984. }
  6985. }
  6986. default:
  6987. var seq;
  6988. if (tail.length > 0 && (seq = tail[0].value) instanceof AST_Sequence) {
  6989. tail[0].value = seq.tail_node();
  6990. body.push(make_node(AST_SimpleStatement, node, {
  6991. body: make_sequence(seq, seq.expressions.slice(0, -1)),
  6992. }));
  6993. }
  6994. node.definitions = head.concat(tail);
  6995. body.push(node);
  6996. }
  6997. if (side_effects.length > 0) {
  6998. body.push(make_node(AST_SimpleStatement, node, { body: make_sequence(node, side_effects) }));
  6999. }
  7000. return insert_statements(body, node, in_list);
  7001. }
  7002. if (node instanceof AST_Assign) {
  7003. descend(node, tt);
  7004. if (!(node.left instanceof AST_Destructured)) return node;
  7005. var trimmed = trim_destructured(node.left, node.right, function(node) {
  7006. return node;
  7007. }, node.write_only === true);
  7008. if (trimmed.name) return make_node(AST_Assign, node, {
  7009. operator: node.operator,
  7010. left: trimmed.name,
  7011. right: trimmed.value,
  7012. });
  7013. if (trimmed.value) return trimmed.value;
  7014. if (parent instanceof AST_Sequence && parent.tail_node() !== node) return List.skip;
  7015. return make_node(AST_Number, node, { value: 0 });
  7016. }
  7017. if (node instanceof AST_LabeledStatement && node.body instanceof AST_For) {
  7018. // Certain combination of unused name + side effect leads to invalid AST:
  7019. // https://github.com/mishoo/UglifyJS/issues/1830
  7020. // We fix it at this stage by moving the label inwards, back to the `for`.
  7021. descend(node, tt);
  7022. if (node.body instanceof AST_BlockStatement) {
  7023. var block = node.body;
  7024. node.body = block.body.pop();
  7025. block.body.push(node);
  7026. return in_list ? List.splice(block.body) : block;
  7027. }
  7028. return node;
  7029. }
  7030. if (node instanceof AST_Scope) {
  7031. descend_scope();
  7032. return node;
  7033. }
  7034. if (node instanceof AST_SymbolImport) {
  7035. if (!compressor.option("imports") || node.definition().id in in_use_ids) return node;
  7036. return in_list ? List.skip : null;
  7037. }
  7038. function descend_scope() {
  7039. var save_scope = scope;
  7040. scope = node;
  7041. descend(node, tt);
  7042. scope = save_scope;
  7043. }
  7044. }, function(node, in_list) {
  7045. if (node instanceof AST_BlockStatement) return trim_block(node, tt.parent(), in_list);
  7046. if (node instanceof AST_For) return patch_for_init(node, in_list);
  7047. if (node instanceof AST_ForIn) {
  7048. if (!drop_vars || !compressor.option("loops")) return;
  7049. if (!is_empty(node.body)) return;
  7050. var sym = get_init_symbol(node);
  7051. if (!sym) return;
  7052. var def = sym.definition();
  7053. if (def.id in in_use_ids) return;
  7054. log(sym, "Dropping unused loop variable {name}");
  7055. if (for_ins[def.id] === node) delete for_ins[def.id];
  7056. var body = [];
  7057. var value = node.object.drop_side_effect_free(compressor);
  7058. if (value) {
  7059. AST_Node.warn("Side effects in object of for-in loop [{file}:{line},{col}]", value.start);
  7060. body.push(make_node(AST_SimpleStatement, node, {
  7061. body: value
  7062. }));
  7063. }
  7064. if (node.init instanceof AST_Definitions && def.orig[0] instanceof AST_SymbolCatch) {
  7065. body.push(node.init);
  7066. }
  7067. return insert_statements(body, node, in_list);
  7068. }
  7069. if (node instanceof AST_Import) {
  7070. if (node.properties && node.properties.length == 0) node.properties = null;
  7071. return node;
  7072. }
  7073. if (node instanceof AST_Sequence) {
  7074. if (node.expressions.length > 1) return;
  7075. return maintain_this_binding(compressor, tt.parent(), node, node.expressions[0]);
  7076. }
  7077. });
  7078. tt.push(compressor.parent());
  7079. self.transform(tt);
  7080. if (self instanceof AST_Lambda
  7081. && self.body.length == 1
  7082. && self.body[0] instanceof AST_Directive
  7083. && self.body[0].value == "use strict") {
  7084. self.body.length = 0;
  7085. }
  7086. unused_fn_names.forEach(function(fn) {
  7087. fn.name = null;
  7088. });
  7089. calls_to_drop_args.forEach(function(call) {
  7090. drop_unused_call_args(call, compressor, fns_with_marked_args);
  7091. });
  7092. function log(sym, text) {
  7093. AST_Node[sym.definition().references.length > 0 ? "info" : "warn"](text + " [{file}:{line},{col}]", template(sym));
  7094. }
  7095. function log_default(node, text) {
  7096. if (node.name instanceof AST_SymbolFunarg) {
  7097. log(node.name, text);
  7098. } else {
  7099. AST_Node.info(text + " [{file}:{line},{col}]", {
  7100. name: node,
  7101. file: node.start.file,
  7102. line: node.start.line,
  7103. col : node.start.col,
  7104. });
  7105. }
  7106. }
  7107. function template(sym) {
  7108. return {
  7109. name: sym.name,
  7110. file: sym.start.file,
  7111. line: sym.start.line,
  7112. col : sym.start.col,
  7113. };
  7114. }
  7115. function get_rvalue(expr) {
  7116. return expr[expr instanceof AST_Assign ? "right" : "value"];
  7117. }
  7118. function insert_statements(body, orig, in_list) {
  7119. switch (body.length) {
  7120. case 0:
  7121. return in_list ? List.skip : make_node(AST_EmptyStatement, orig);
  7122. case 1:
  7123. return body[0];
  7124. default:
  7125. return in_list ? List.splice(body) : make_node(AST_BlockStatement, orig, {
  7126. body: body
  7127. });
  7128. }
  7129. }
  7130. function track_assigns(def, node) {
  7131. if (def.scope.resolve() !== self) return false;
  7132. if (!def.fixed || !node.fixed) assign_in_use[def.id] = false;
  7133. return assign_in_use[def.id] !== false;
  7134. }
  7135. function add_assigns(def, node) {
  7136. if (!assign_in_use[def.id]) assign_in_use[def.id] = [];
  7137. if (node.fixed.assigns) push_uniq(assign_in_use[def.id], node.fixed.assigns);
  7138. }
  7139. function indexOf_assign(def, node) {
  7140. var nodes = assign_in_use[def.id];
  7141. return nodes && nodes.indexOf(node);
  7142. }
  7143. function verify_safe_usage(def, read, modified) {
  7144. if (def.id in in_use_ids) return;
  7145. if (read && modified) {
  7146. in_use_ids[def.id] = read;
  7147. in_use.push(def);
  7148. } else {
  7149. value_read[def.id] = read;
  7150. value_modified[def.id] = modified;
  7151. }
  7152. }
  7153. function can_drop_lhs(sym, node) {
  7154. var def = sym.definition();
  7155. var in_use = in_use_ids[def.id];
  7156. if (!in_use) return true;
  7157. if (node[node instanceof AST_Assign ? "left" : "expression"] !== sym) return false;
  7158. return in_use === sym && def.references.length - def.replaced == 1 || indexOf_assign(def, node) < 0;
  7159. }
  7160. function get_rhs(assign) {
  7161. var rhs = assign.right;
  7162. if (!assign.write_only) return rhs;
  7163. if (!(rhs instanceof AST_Binary && lazy_op[rhs.operator])) return rhs;
  7164. if (!(rhs.left instanceof AST_SymbolRef)) return rhs;
  7165. if (!(assign.left instanceof AST_SymbolRef)) return rhs;
  7166. var def = assign.left.definition();
  7167. if (rhs.left.definition() !== def) return rhs;
  7168. if (rhs.right.has_side_effects(compressor)) return rhs;
  7169. if (track_assigns(def, rhs.left)) add_assigns(def, rhs.left);
  7170. return rhs.right;
  7171. }
  7172. function get_init_symbol(for_in) {
  7173. var init = for_in.init;
  7174. if (init instanceof AST_Definitions) {
  7175. init = init.definitions[0].name;
  7176. return init instanceof AST_SymbolDeclaration && init;
  7177. }
  7178. while (init instanceof AST_PropAccess) init = init.expression.tail_node();
  7179. if (init instanceof AST_SymbolRef) return init;
  7180. }
  7181. function scan_ref_scoped(node, descend, init) {
  7182. if (node instanceof AST_Assign && node.left instanceof AST_SymbolRef) {
  7183. var def = node.left.definition();
  7184. if (def.scope.resolve() === self) assignments.add(def.id, node);
  7185. }
  7186. if (node instanceof AST_Unary && node.expression instanceof AST_SymbolRef) {
  7187. var def = node.expression.definition();
  7188. if (def.scope.resolve() === self) assignments.add(def.id, node);
  7189. }
  7190. var node_def, props = [], sym = assign_as_unused(node, props);
  7191. if (sym && ((node_def = sym.definition()).scope.resolve() === self
  7192. || self.variables.get(sym.name) === node_def)
  7193. && !(is_arguments(node_def) && !all(self.argnames, function(argname) {
  7194. return !argname.match_symbol(function(node) {
  7195. if (node instanceof AST_SymbolFunarg) {
  7196. var def = node.definition();
  7197. return def.references.length > def.replaced;
  7198. }
  7199. }, true);
  7200. }))) {
  7201. if (node.write_only === "p" && node.right.may_throw_on_access(compressor, true)) return;
  7202. var assign = props.assign;
  7203. if (assign) {
  7204. assign.write_only = true;
  7205. assign.walk(tw);
  7206. }
  7207. props.forEach(function(prop) {
  7208. prop.walk(tw);
  7209. });
  7210. if (node instanceof AST_Assign) {
  7211. var right = get_rhs(node), shared = false;
  7212. if (init && node.write_only === true && !right.has_side_effects(compressor)) {
  7213. initializations.add(node_def.id, right);
  7214. } else {
  7215. right.walk(tw);
  7216. shared = right.tail_node().operator == "=";
  7217. }
  7218. if (node.left === sym) {
  7219. if (!node.write_only || shared) {
  7220. verify_safe_usage(node_def, sym, value_modified[node_def.id]);
  7221. }
  7222. } else {
  7223. var fixed = sym.fixed_value();
  7224. if (!fixed || !fixed.is_constant()) {
  7225. verify_safe_usage(node_def, value_read[node_def.id], true);
  7226. }
  7227. }
  7228. }
  7229. if (track_assigns(node_def, sym) && is_lhs(sym, node) !== sym) add_assigns(node_def, sym);
  7230. return true;
  7231. }
  7232. if (node instanceof AST_ForIn) {
  7233. if (node.init instanceof AST_SymbolRef && scope === self) {
  7234. var id = node.init.definition().id;
  7235. if (!(id in for_ins)) for_ins[id] = node;
  7236. }
  7237. if (!drop_vars || !compressor.option("loops")) return;
  7238. if (!is_empty(node.body)) return;
  7239. if (node.init.has_side_effects(compressor)) return;
  7240. var sym = get_init_symbol(node);
  7241. if (!sym) return;
  7242. var def = sym.definition();
  7243. if (def.scope.resolve() !== self) {
  7244. var d = find_variable(sym.name);
  7245. if (d === def || d && d.redefined() === def) return;
  7246. }
  7247. node.object.walk(tw);
  7248. return true;
  7249. }
  7250. if (node instanceof AST_SymbolRef) {
  7251. node_def = node.definition();
  7252. if (!(node_def.id in in_use_ids)) {
  7253. in_use_ids[node_def.id] = true;
  7254. in_use.push(node_def);
  7255. }
  7256. if (cross_scope(node_def.scope, node.scope)) {
  7257. var redef = node_def.redefined();
  7258. if (redef && !(redef.id in in_use_ids)) {
  7259. in_use_ids[redef.id] = true;
  7260. in_use.push(redef);
  7261. }
  7262. }
  7263. if (track_assigns(node_def, node)) add_assigns(node_def, node);
  7264. return true;
  7265. }
  7266. if (node instanceof AST_Scope) {
  7267. var save_scope = scope;
  7268. scope = node;
  7269. descend();
  7270. scope = save_scope;
  7271. return true;
  7272. }
  7273. }
  7274. function is_decl(node) {
  7275. return (node instanceof AST_DefaultValue ? node.name : node) instanceof AST_SymbolDeclaration;
  7276. }
  7277. function trim_default(trimmer, node) {
  7278. node.value = node.value.transform(tt);
  7279. var name = node.name.transform(trimmer);
  7280. if (!name) {
  7281. if (node.name instanceof AST_Destructured) return null;
  7282. var value = node.value.drop_side_effect_free(compressor);
  7283. if (!value) return null;
  7284. log(node.name, "Side effects in default value of unused variable {name}");
  7285. node.name.unused = null;
  7286. node.value = value;
  7287. }
  7288. return node;
  7289. }
  7290. function trim_destructured(node, value, process, drop, root) {
  7291. var trimmer = new TreeTransformer(function(node) {
  7292. if (node instanceof AST_DefaultValue) {
  7293. if (!(compressor.option("default_values") && value && value.is_defined(compressor))) {
  7294. var save_drop = drop;
  7295. drop = false;
  7296. var trimmed = trim_default(trimmer, node);
  7297. drop = save_drop;
  7298. if (!trimmed && drop && value) value = value.drop_side_effect_free(compressor);
  7299. return trimmed;
  7300. } else if (node === root) {
  7301. root = node = node.name;
  7302. } else {
  7303. node = node.name;
  7304. }
  7305. }
  7306. if (node instanceof AST_DestructuredArray) {
  7307. var save_drop = drop;
  7308. var save_value = value;
  7309. if (value instanceof AST_SymbolRef) {
  7310. drop = false;
  7311. value = value.fixed_value();
  7312. }
  7313. var values = value instanceof AST_Array && value.elements;
  7314. var elements = [], newValues = drop && [], pos = 0;
  7315. node.elements.forEach(function(element, index) {
  7316. value = values && values[index];
  7317. if (value instanceof AST_Hole) {
  7318. value = null;
  7319. } else if (value instanceof AST_Spread) {
  7320. if (drop) {
  7321. newValues.length = pos;
  7322. fill_holes(save_value, newValues);
  7323. [].push.apply(newValues, values.slice(index));
  7324. save_value.elements = newValues;
  7325. }
  7326. value = values = false;
  7327. }
  7328. element = element.transform(trimmer);
  7329. if (element) elements[pos] = element;
  7330. if (drop && value) newValues[pos] = value;
  7331. if (element || value || !drop || !values) pos++;
  7332. });
  7333. value = values && make_node(AST_Array, save_value, {
  7334. elements: values.slice(node.elements.length),
  7335. });
  7336. if (node.rest) {
  7337. var was_drop = drop;
  7338. drop = false;
  7339. node.rest = node.rest.transform(compressor.option("rests") ? trimmer : tt);
  7340. drop = was_drop;
  7341. if (node.rest) elements.length = pos;
  7342. }
  7343. if (drop) {
  7344. if (value && !node.rest) value = value.drop_side_effect_free(compressor);
  7345. if (value instanceof AST_Array) {
  7346. value = value.elements;
  7347. } else if (value instanceof AST_Sequence) {
  7348. value = value.expressions;
  7349. } else if (value) {
  7350. value = [ value ];
  7351. }
  7352. if (value && value.length) {
  7353. newValues.length = pos;
  7354. [].push.apply(newValues, value);
  7355. }
  7356. }
  7357. value = save_value;
  7358. drop = save_drop;
  7359. if (values && newValues) {
  7360. fill_holes(value, newValues);
  7361. value.elements = newValues;
  7362. }
  7363. if (!node.rest && (value instanceof AST_Array
  7364. || value && value.is_string(compressor))) switch (elements.length) {
  7365. case 0:
  7366. if (node === root) break;
  7367. if (drop) value = value.drop_side_effect_free(compressor);
  7368. return null;
  7369. case 1:
  7370. if (!drop) break;
  7371. if (node === root) break;
  7372. var sym = elements[0];
  7373. if (sym.has_side_effects(compressor)) break;
  7374. if (value.has_side_effects(compressor) && sym.match_symbol(function(node) {
  7375. return node instanceof AST_PropAccess;
  7376. })) break;
  7377. value = make_node(AST_Sub, node, {
  7378. expression: value,
  7379. property: make_node(AST_Number, node, { value: 0 }),
  7380. });
  7381. return sym;
  7382. }
  7383. fill_holes(node, elements);
  7384. node.elements = elements;
  7385. return node;
  7386. }
  7387. if (node instanceof AST_DestructuredObject) {
  7388. var save_drop = drop;
  7389. var save_value = value;
  7390. if (value instanceof AST_SymbolRef) {
  7391. drop = false;
  7392. value = value.fixed_value();
  7393. }
  7394. var prop_keys, prop_map;
  7395. if (value instanceof AST_Object) {
  7396. prop_keys = [];
  7397. prop_map = new Dictionary();
  7398. value.properties.forEach(function(prop, index) {
  7399. if (prop instanceof AST_Spread) return prop_map = false;
  7400. var key = prop.key;
  7401. if (key instanceof AST_Node) key = key.evaluate(compressor, true);
  7402. if (key instanceof AST_Node) {
  7403. prop_map = false;
  7404. } else if (prop_map && !(prop instanceof AST_ObjectSetter)) {
  7405. prop_map.set(key, prop);
  7406. }
  7407. prop_keys[index] = key;
  7408. });
  7409. }
  7410. if (node.rest) {
  7411. value = false;
  7412. node.rest = node.rest.transform(compressor.option("rests") ? trimmer : tt);
  7413. }
  7414. var can_drop = new Dictionary();
  7415. var drop_keys = drop && new Dictionary();
  7416. var properties = [];
  7417. node.properties.map(function(prop) {
  7418. var key = prop.key;
  7419. if (key instanceof AST_Node) {
  7420. prop.key = key = key.transform(tt);
  7421. key = key.evaluate(compressor, true);
  7422. }
  7423. if (key instanceof AST_Node) {
  7424. drop_keys = false;
  7425. } else {
  7426. can_drop.set(key, !can_drop.has(key));
  7427. }
  7428. return key;
  7429. }).forEach(function(key, index) {
  7430. var prop = node.properties[index], trimmed;
  7431. if (key instanceof AST_Node) {
  7432. drop = false;
  7433. value = false;
  7434. trimmed = prop.value.transform(trimmer) || retain_lhs(prop.value);
  7435. } else {
  7436. drop = drop_keys && can_drop.get(key);
  7437. var mapped = prop_map && prop_map.get(key);
  7438. if (mapped) {
  7439. value = mapped.value;
  7440. if (value instanceof AST_Accessor) value = false;
  7441. } else {
  7442. value = false;
  7443. }
  7444. trimmed = prop.value.transform(trimmer);
  7445. if (!trimmed) {
  7446. if (node.rest || retain_key(prop)) trimmed = retain_lhs(prop.value);
  7447. if (drop_keys && !drop_keys.has(key)) {
  7448. if (mapped) {
  7449. drop_keys.set(key, mapped);
  7450. if (value === null) {
  7451. prop_map.set(key, retain_key(mapped) && make_node(AST_ObjectKeyVal, mapped, {
  7452. key: mapped.key,
  7453. value: make_node(AST_Number, mapped, { value: 0 }),
  7454. }));
  7455. }
  7456. } else {
  7457. drop_keys.set(key, true);
  7458. }
  7459. }
  7460. } else if (drop_keys) {
  7461. drop_keys.set(key, false);
  7462. }
  7463. if (value) mapped.value = value;
  7464. }
  7465. if (trimmed) {
  7466. prop.value = trimmed;
  7467. properties.push(prop);
  7468. }
  7469. });
  7470. value = save_value;
  7471. drop = save_drop;
  7472. if (drop_keys && prop_keys) value.properties = List(value.properties, function(prop, index) {
  7473. if (prop instanceof AST_Spread) return prop;
  7474. var key = prop_keys[index];
  7475. if (key instanceof AST_Node) return prop;
  7476. if (drop_keys.has(key)) {
  7477. var mapped = drop_keys.get(key);
  7478. if (!mapped) return prop;
  7479. if (mapped === prop) return prop_map.get(key) || List.skip;
  7480. } else if (node.rest) {
  7481. return prop;
  7482. }
  7483. var trimmed = prop.value.drop_side_effect_free(compressor);
  7484. if (trimmed) {
  7485. prop.value = trimmed;
  7486. return prop;
  7487. }
  7488. return retain_key(prop) ? make_node(AST_ObjectKeyVal, prop, {
  7489. key: prop.key,
  7490. value: make_node(AST_Number, prop, { value: 0 }),
  7491. }) : List.skip;
  7492. });
  7493. if (value && !node.rest) switch (properties.length) {
  7494. case 0:
  7495. if (node === root) break;
  7496. if (value.may_throw_on_access(compressor, true)) break;
  7497. if (drop) value = value.drop_side_effect_free(compressor);
  7498. return null;
  7499. case 1:
  7500. if (!drop) break;
  7501. if (node === root) break;
  7502. var prop = properties[0];
  7503. if (prop.key instanceof AST_Node) break;
  7504. if (prop.value.has_side_effects(compressor)) break;
  7505. if (value.has_side_effects(compressor) && prop.value.match_symbol(function(node) {
  7506. return node instanceof AST_PropAccess;
  7507. })) break;
  7508. value = make_node(AST_Sub, node, {
  7509. expression: value,
  7510. property: make_node_from_constant(prop.key, prop),
  7511. });
  7512. return prop.value;
  7513. }
  7514. node.properties = properties;
  7515. return node;
  7516. }
  7517. if (node instanceof AST_Hole) {
  7518. node = null;
  7519. } else {
  7520. node = process(node);
  7521. }
  7522. if (!node && drop && value) value = value.drop_side_effect_free(compressor);
  7523. return node;
  7524. });
  7525. return {
  7526. name: node.transform(trimmer),
  7527. value: value,
  7528. };
  7529. function retain_key(prop) {
  7530. return prop.key instanceof AST_Node && prop.key.has_side_effects(compressor);
  7531. }
  7532. function clear_write_only(node) {
  7533. if (node instanceof AST_Assign) {
  7534. node.write_only = false;
  7535. clear_write_only(node.right);
  7536. } else if (node instanceof AST_Binary) {
  7537. if (!lazy_op[node.operator]) return;
  7538. clear_write_only(node.left);
  7539. clear_write_only(node.right);
  7540. } else if (node instanceof AST_Conditional) {
  7541. clear_write_only(node.consequent);
  7542. clear_write_only(node.alternative);
  7543. } else if (node instanceof AST_Sequence) {
  7544. clear_write_only(node.tail_node());
  7545. } else if (node instanceof AST_Unary) {
  7546. node.write_only = false;
  7547. }
  7548. }
  7549. function retain_lhs(node) {
  7550. if (node instanceof AST_DefaultValue) return retain_lhs(node.name);
  7551. if (node instanceof AST_Destructured) {
  7552. if (value === null) {
  7553. value = make_node(AST_Number, node, { value: 0 });
  7554. } else if (value) {
  7555. if (value.may_throw_on_access(compressor, true)) {
  7556. value = make_node(AST_Array, node, {
  7557. elements: value instanceof AST_Sequence ? value.expressions : [ value ],
  7558. });
  7559. } else {
  7560. clear_write_only(value);
  7561. }
  7562. }
  7563. return make_node(AST_DestructuredObject, node, { properties: [] });
  7564. }
  7565. node.unused = null;
  7566. return node;
  7567. }
  7568. }
  7569. });
  7570. AST_Scope.DEFMETHOD("hoist_declarations", function(compressor) {
  7571. if (compressor.has_directive("use asm")) return;
  7572. var hoist_funs = compressor.option("hoist_funs");
  7573. var hoist_vars = compressor.option("hoist_vars");
  7574. var self = this;
  7575. if (hoist_vars) {
  7576. // let's count var_decl first, we seem to waste a lot of
  7577. // space if we hoist `var` when there's only one.
  7578. var var_decl = 0;
  7579. self.walk(new TreeWalker(function(node) {
  7580. if (var_decl > 1) return true;
  7581. if (node instanceof AST_ExportDeclaration) return true;
  7582. if (node instanceof AST_Scope && node !== self) return true;
  7583. if (node instanceof AST_Var) {
  7584. var_decl++;
  7585. return true;
  7586. }
  7587. }));
  7588. if (var_decl <= 1) hoist_vars = false;
  7589. }
  7590. if (!hoist_funs && !hoist_vars) return;
  7591. var consts = new Dictionary();
  7592. var dirs = [];
  7593. var hoisted = [];
  7594. var vars = new Dictionary();
  7595. var tt = new TreeTransformer(function(node, descend, in_list) {
  7596. if (node === self) return;
  7597. if (node instanceof AST_Directive) {
  7598. dirs.push(node);
  7599. return in_list ? List.skip : make_node(AST_EmptyStatement, node);
  7600. }
  7601. if (node instanceof AST_LambdaDefinition) {
  7602. if (!hoist_funs) return node;
  7603. var p = tt.parent();
  7604. if (p instanceof AST_ExportDeclaration) return node;
  7605. if (p instanceof AST_ExportDefault) return node;
  7606. if (p !== self && compressor.has_directive("use strict")) return node;
  7607. hoisted.push(node);
  7608. return in_list ? List.skip : make_node(AST_EmptyStatement, node);
  7609. }
  7610. if (node instanceof AST_Var) {
  7611. if (!hoist_vars) return node;
  7612. var p = tt.parent();
  7613. if (p instanceof AST_ExportDeclaration) return node;
  7614. if (!all(node.definitions, function(defn) {
  7615. var sym = defn.name;
  7616. return sym instanceof AST_SymbolVar
  7617. && !consts.has(sym.name)
  7618. && self.find_variable(sym.name) === sym.definition();
  7619. })) return node;
  7620. node.definitions.forEach(function(defn) {
  7621. vars.set(defn.name.name, defn);
  7622. });
  7623. var seq = node.to_assignments();
  7624. if (p instanceof AST_ForEnumeration && p.init === node) {
  7625. if (seq) return seq;
  7626. var sym = node.definitions[0].name;
  7627. return make_node(AST_SymbolRef, sym, sym);
  7628. }
  7629. if (p instanceof AST_For && p.init === node) return seq;
  7630. if (!seq) return in_list ? List.skip : make_node(AST_EmptyStatement, node);
  7631. return make_node(AST_SimpleStatement, node, { body: seq });
  7632. }
  7633. if (node instanceof AST_Scope) return node;
  7634. if (node instanceof AST_SymbolConst) {
  7635. consts.set(node.name, true);
  7636. return node;
  7637. }
  7638. });
  7639. self.transform(tt);
  7640. if (vars.size() > 0) {
  7641. // collect only vars which don't show up in self's arguments list
  7642. var defns = [];
  7643. if (self instanceof AST_Lambda) self.each_argname(function(argname) {
  7644. vars.del(argname.name);
  7645. });
  7646. vars.each(function(defn, name) {
  7647. defn = defn.clone();
  7648. defn.name = defn.name.clone();
  7649. defn.value = null;
  7650. defns.push(defn);
  7651. vars.set(name, defn);
  7652. defn.name.definition().orig.unshift(defn.name);
  7653. });
  7654. if (defns.length > 0) {
  7655. // try to merge in assignments
  7656. insert_vars(self.body);
  7657. hoisted.push(make_node(AST_Var, self, { definitions: defns }));
  7658. }
  7659. }
  7660. self.body = dirs.concat(hoisted, self.body);
  7661. function insert_vars(body) {
  7662. while (body.length) {
  7663. var stat = body[0];
  7664. if (stat instanceof AST_SimpleStatement) {
  7665. var expr = stat.body, sym, assign;
  7666. if (expr instanceof AST_Assign
  7667. && expr.operator == "="
  7668. && (sym = expr.left) instanceof AST_Symbol
  7669. && vars.has(sym.name)) {
  7670. var defn = vars.get(sym.name);
  7671. if (defn.value) break;
  7672. var value = expr.right;
  7673. if (value instanceof AST_Sequence) value = value.clone();
  7674. defn.value = value;
  7675. remove(defns, defn);
  7676. defns.push(defn);
  7677. body.shift();
  7678. continue;
  7679. }
  7680. if (expr instanceof AST_Sequence
  7681. && (assign = expr.expressions[0]) instanceof AST_Assign
  7682. && assign.operator == "="
  7683. && (sym = assign.left) instanceof AST_Symbol
  7684. && vars.has(sym.name)) {
  7685. var defn = vars.get(sym.name);
  7686. if (defn.value) break;
  7687. defn.value = assign.right;
  7688. remove(defns, defn);
  7689. defns.push(defn);
  7690. stat.body = make_sequence(expr, expr.expressions.slice(1));
  7691. continue;
  7692. }
  7693. }
  7694. if (stat instanceof AST_EmptyStatement) {
  7695. body.shift();
  7696. continue;
  7697. }
  7698. if (stat instanceof AST_BlockStatement && !insert_vars(stat.body)) {
  7699. body.shift();
  7700. continue;
  7701. }
  7702. break;
  7703. }
  7704. return body.length;
  7705. }
  7706. });
  7707. function scan_local_returns(fn, transform) {
  7708. fn.walk(new TreeWalker(function(node) {
  7709. if (node instanceof AST_Return) {
  7710. transform(node);
  7711. return true;
  7712. }
  7713. if (node instanceof AST_Scope && node !== fn) return true;
  7714. }));
  7715. }
  7716. function map_self_returns(fn) {
  7717. var map = Object.create(null);
  7718. scan_local_returns(fn, function(node) {
  7719. var value = node.value;
  7720. if (value) value = value.tail_node();
  7721. if (value instanceof AST_SymbolRef) {
  7722. var id = value.definition().id;
  7723. map[id] = (map[id] || 0) + 1;
  7724. }
  7725. });
  7726. return map;
  7727. }
  7728. function can_trim_returns(def, self_returns, compressor) {
  7729. if (compressor.exposed(def)) return false;
  7730. switch (def.references.length - def.replaced - (self_returns[def.id] || 0)) {
  7731. case def.drop_return:
  7732. return "d";
  7733. case def.bool_return:
  7734. return true;
  7735. }
  7736. }
  7737. function process_boolean_returns(fn, compressor) {
  7738. scan_local_returns(fn, function(node) {
  7739. node.in_bool = true;
  7740. var value = node.value;
  7741. if (value) {
  7742. var ev = fuzzy_eval(compressor, value);
  7743. if (!ev) {
  7744. value = value.drop_side_effect_free(compressor);
  7745. node.value = value ? make_sequence(node.value, [
  7746. value,
  7747. make_node(AST_Number, node.value, { value: 0 }),
  7748. ]) : null;
  7749. } else if (!(ev instanceof AST_Node)) {
  7750. value = value.drop_side_effect_free(compressor);
  7751. node.value = value ? make_sequence(node.value, [
  7752. value,
  7753. make_node(AST_Number, node.value, { value: 1 }),
  7754. ]) : make_node(AST_Number, node.value, { value: 1 });
  7755. }
  7756. }
  7757. });
  7758. }
  7759. AST_Scope.DEFMETHOD("process_returns", noop);
  7760. AST_Defun.DEFMETHOD("process_returns", function(compressor) {
  7761. if (!compressor.option("booleans")) return;
  7762. if (compressor.parent() instanceof AST_ExportDefault) return;
  7763. switch (can_trim_returns(this.name.definition(), map_self_returns(this), compressor)) {
  7764. case "d":
  7765. drop_returns(compressor, this, true);
  7766. break;
  7767. case true:
  7768. process_boolean_returns(this, compressor);
  7769. break;
  7770. }
  7771. });
  7772. AST_Function.DEFMETHOD("process_returns", function(compressor) {
  7773. if (!compressor.option("booleans")) return;
  7774. var drop = true;
  7775. var self_returns = map_self_returns(this);
  7776. if (this.name && !can_trim(this.name.definition())) return;
  7777. var parent = compressor.parent();
  7778. if (parent instanceof AST_Assign) {
  7779. if (parent.operator != "=") return;
  7780. var sym = parent.left;
  7781. if (!(sym instanceof AST_SymbolRef)) return;
  7782. if (!can_trim(sym.definition())) return;
  7783. } else if (parent instanceof AST_Call && parent.expression !== this) {
  7784. var exp = parent.expression;
  7785. if (exp instanceof AST_SymbolRef) exp = exp.fixed_value();
  7786. if (!(exp instanceof AST_Lambda)) return;
  7787. if (exp.uses_arguments || exp.pinned()) return;
  7788. var args = parent.args, sym;
  7789. for (var i = 0; i < args.length; i++) {
  7790. var arg = args[i];
  7791. if (arg === this) {
  7792. sym = exp.argnames[i];
  7793. if (!sym && exp.rest) return;
  7794. break;
  7795. }
  7796. if (arg instanceof AST_Spread) return;
  7797. }
  7798. if (sym instanceof AST_DefaultValue) sym = sym.name;
  7799. if (sym instanceof AST_SymbolFunarg && !can_trim(sym.definition())) return;
  7800. } else if (parent.TYPE == "Call") {
  7801. compressor.pop();
  7802. var in_bool = compressor.in_boolean_context();
  7803. compressor.push(this);
  7804. switch (in_bool) {
  7805. case true:
  7806. drop = false;
  7807. case "d":
  7808. break;
  7809. default:
  7810. return;
  7811. }
  7812. } else return;
  7813. if (drop) {
  7814. drop_returns(compressor, this, true);
  7815. } else {
  7816. process_boolean_returns(this, compressor);
  7817. }
  7818. function can_trim(def) {
  7819. switch (can_trim_returns(def, self_returns, compressor)) {
  7820. case true:
  7821. drop = false;
  7822. case "d":
  7823. return true;
  7824. }
  7825. }
  7826. });
  7827. AST_BlockScope.DEFMETHOD("var_names", function() {
  7828. var var_names = this._var_names;
  7829. if (!var_names) {
  7830. this._var_names = var_names = new Dictionary();
  7831. this.enclosed.forEach(function(def) {
  7832. var_names.set(def.name, true);
  7833. });
  7834. this.variables.each(function(def, name) {
  7835. var_names.set(name, true);
  7836. });
  7837. }
  7838. return var_names;
  7839. });
  7840. AST_Scope.DEFMETHOD("make_var", function(type, orig, prefix) {
  7841. var scopes = [ this ];
  7842. if (orig instanceof AST_SymbolDeclaration) orig.definition().references.forEach(function(ref) {
  7843. var s = ref.scope;
  7844. if (member(s, scopes)) return;
  7845. do {
  7846. push_uniq(scopes, s);
  7847. s = s.parent_scope;
  7848. } while (s && s !== this);
  7849. });
  7850. prefix = prefix.replace(/^[^a-z_$]|[^a-z0-9_$]/gi, "_");
  7851. var name = prefix;
  7852. for (var i = 0; !all(scopes, function(scope) {
  7853. return !scope.var_names().has(name);
  7854. }); i++) name = prefix + "$" + i;
  7855. var sym = make_node(type, orig, {
  7856. name: name,
  7857. scope: this,
  7858. });
  7859. var def = this.def_variable(sym);
  7860. scopes.forEach(function(scope) {
  7861. scope.enclosed.push(def);
  7862. scope.var_names().set(name, true);
  7863. });
  7864. return sym;
  7865. });
  7866. AST_Scope.DEFMETHOD("hoist_properties", function(compressor) {
  7867. if (!compressor.option("hoist_props") || compressor.has_directive("use asm")) return;
  7868. var self = this;
  7869. var top_retain = self instanceof AST_Toplevel && compressor.top_retain || return_false;
  7870. var defs_by_id = Object.create(null);
  7871. self.transform(new TreeTransformer(function(node, descend) {
  7872. if (node instanceof AST_Assign) {
  7873. if (node.operator != "=") return;
  7874. if (!node.write_only) return;
  7875. if (!can_hoist(node.left, node.right, 1)) return;
  7876. descend(node, this);
  7877. var defs = new Dictionary();
  7878. var assignments = [];
  7879. var decls = [];
  7880. node.right.properties.forEach(function(prop) {
  7881. var decl = make_sym(AST_SymbolVar, node.left, prop.key);
  7882. decls.push(make_node(AST_VarDef, node, {
  7883. name: decl,
  7884. value: null,
  7885. }));
  7886. var sym = make_node(AST_SymbolRef, node, {
  7887. name: decl.name,
  7888. scope: self,
  7889. thedef: decl.definition(),
  7890. });
  7891. sym.reference();
  7892. assignments.push(make_node(AST_Assign, node, {
  7893. operator: "=",
  7894. left: sym,
  7895. right: prop.value,
  7896. }));
  7897. });
  7898. defs.value = node.right;
  7899. defs_by_id[node.left.definition().id] = defs;
  7900. self.body.splice(self.body.indexOf(this.stack[1]) + 1, 0, make_node(AST_Var, node, {
  7901. definitions: decls,
  7902. }));
  7903. return make_sequence(node, assignments);
  7904. }
  7905. if (node instanceof AST_Scope) return node === self ? undefined : node;
  7906. if (node instanceof AST_VarDef) {
  7907. if (!can_hoist(node.name, node.value, 0)) return;
  7908. descend(node, this);
  7909. var defs = new Dictionary();
  7910. var var_defs = [];
  7911. var decl = node.clone();
  7912. decl.value = node.name instanceof AST_SymbolConst ? make_node(AST_Number, node, { value: 0 }) : null;
  7913. var_defs.push(decl);
  7914. node.value.properties.forEach(function(prop) {
  7915. var_defs.push(make_node(AST_VarDef, node, {
  7916. name: make_sym(node.name.CTOR, node.name, prop.key),
  7917. value: prop.value,
  7918. }));
  7919. });
  7920. defs.value = node.value;
  7921. defs_by_id[node.name.definition().id] = defs;
  7922. return List.splice(var_defs);
  7923. }
  7924. function make_sym(type, sym, key) {
  7925. var new_var = self.make_var(type, sym, sym.name + "_" + key);
  7926. defs.set(key, new_var.definition());
  7927. return new_var;
  7928. }
  7929. }));
  7930. self.transform(new TreeTransformer(function(node, descend) {
  7931. if (node instanceof AST_PropAccess) {
  7932. if (!(node.expression instanceof AST_SymbolRef)) return;
  7933. var defs = defs_by_id[node.expression.definition().id];
  7934. if (!defs) return;
  7935. if (node.expression.fixed_value() !== defs.value) return;
  7936. var def = defs.get(node.get_property());
  7937. var sym = make_node(AST_SymbolRef, node, {
  7938. name: def.name,
  7939. scope: node.expression.scope,
  7940. thedef: def,
  7941. });
  7942. sym.reference();
  7943. return sym;
  7944. }
  7945. if (node instanceof AST_SymbolRef) {
  7946. var defs = defs_by_id[node.definition().id];
  7947. if (!defs) return;
  7948. if (node.fixed_value() !== defs.value) return;
  7949. return make_node(AST_Object, node, { properties: [] });
  7950. }
  7951. }));
  7952. function can_hoist(sym, right, count) {
  7953. if (!(sym instanceof AST_Symbol)) return;
  7954. var def = sym.definition();
  7955. if (def.assignments != count) return;
  7956. if (def.references.length - def.replaced == count) return;
  7957. if (def.single_use) return;
  7958. if (top_retain(def)) return;
  7959. if (sym.fixed_value() !== right) return;
  7960. var fixed = sym.fixed || def.fixed;
  7961. if (fixed.direct_access) return;
  7962. if (fixed.escaped && fixed.escaped.depth == 1) return;
  7963. return right instanceof AST_Object
  7964. && right.properties.length > 0
  7965. && all(right.properties, can_hoist_property)
  7966. && can_drop_symbol(sym, compressor);
  7967. }
  7968. });
  7969. function fn_name_unused(fn, compressor) {
  7970. if (!fn.name || !compressor.option("ie")) return true;
  7971. var def = fn.name.definition();
  7972. if (compressor.exposed(def)) return false;
  7973. return all(def.references, function(sym) {
  7974. return !(sym instanceof AST_SymbolRef);
  7975. });
  7976. }
  7977. function drop_returns(compressor, exp, ignore_name) {
  7978. if (!(exp instanceof AST_Lambda)) return;
  7979. var arrow = is_arrow(exp);
  7980. var async = is_async(exp);
  7981. var changed = false;
  7982. var drop_body = false;
  7983. if (arrow && compressor.option("arrows")) {
  7984. if (!exp.value) {
  7985. drop_body = true;
  7986. } else if (!async || is_primitive(compressor, exp.value)) {
  7987. var dropped = exp.value.drop_side_effect_free(compressor);
  7988. if (dropped !== exp.value) {
  7989. changed = true;
  7990. exp.value = dropped;
  7991. }
  7992. }
  7993. } else if (!is_generator(exp)) {
  7994. if (!ignore_name && exp.name) {
  7995. var def = exp.name.definition();
  7996. drop_body = def.references.length == def.replaced;
  7997. } else {
  7998. drop_body = true;
  7999. }
  8000. }
  8001. if (drop_body) {
  8002. exp.process_expression(false, function(node) {
  8003. var value = node.value;
  8004. if (value) {
  8005. if (async && !is_primitive(compressor, value)) return node;
  8006. value = value.drop_side_effect_free(compressor, true);
  8007. }
  8008. changed = true;
  8009. if (!value) return make_node(AST_EmptyStatement, node);
  8010. return make_node(AST_SimpleStatement, node, { body: value });
  8011. });
  8012. scan_local_returns(exp, function(node) {
  8013. var value = node.value;
  8014. if (value) {
  8015. if (async && !is_primitive(compressor, value)) return;
  8016. var dropped = value.drop_side_effect_free(compressor);
  8017. if (dropped !== value) {
  8018. changed = true;
  8019. node.value = dropped;
  8020. }
  8021. }
  8022. });
  8023. }
  8024. if (async && compressor.option("awaits")) {
  8025. if (drop_body) exp.process_expression("awaits", function(node) {
  8026. var body = node.body;
  8027. if (body instanceof AST_Await) {
  8028. if (is_primitive(compressor, body.expression)) {
  8029. changed = true;
  8030. body = body.expression.drop_side_effect_free(compressor, true);
  8031. if (!body) return make_node(AST_EmptyStatement, node);
  8032. node.body = body;
  8033. }
  8034. } else if (body instanceof AST_Sequence) {
  8035. var exprs = body.expressions;
  8036. for (var i = exprs.length; --i >= 0;) {
  8037. var tail = exprs[i];
  8038. if (!(tail instanceof AST_Await)) break;
  8039. var value = tail.expression;
  8040. if (!is_primitive(compressor, value)) break;
  8041. changed = true;
  8042. if (exprs[i] = value.drop_side_effect_free(compressor)) break;
  8043. }
  8044. switch (i) {
  8045. case -1:
  8046. return make_node(AST_EmptyStatement, node);
  8047. case 0:
  8048. node.body = exprs[0];
  8049. break;
  8050. default:
  8051. exprs.length = i + 1;
  8052. break;
  8053. }
  8054. }
  8055. return node;
  8056. });
  8057. var abort = !drop_body && exp.name || arrow && exp.value && !is_primitive(compressor, exp.value);
  8058. var tw = new TreeWalker(function(node) {
  8059. if (abort) return true;
  8060. if (tw.parent() === exp && node.may_throw(compressor)) return abort = true;
  8061. if (node instanceof AST_Await) return abort = true;
  8062. if (node instanceof AST_ForAwaitOf) return abort = true;
  8063. if (node instanceof AST_Return) {
  8064. if (node.value && !is_primitive(compressor, node.value)) return abort = true;
  8065. return;
  8066. }
  8067. if (node instanceof AST_Scope && node !== exp) return true;
  8068. });
  8069. exp.walk(tw);
  8070. if (!abort) {
  8071. var ctor;
  8072. switch (exp.CTOR) {
  8073. case AST_AsyncArrow:
  8074. ctor = AST_Arrow;
  8075. break;
  8076. case AST_AsyncFunction:
  8077. ctor = AST_Function;
  8078. break;
  8079. case AST_AsyncGeneratorFunction:
  8080. ctor = AST_GeneratorFunction;
  8081. break;
  8082. }
  8083. return make_node(ctor, exp, exp);
  8084. }
  8085. }
  8086. return changed && exp.clone();
  8087. }
  8088. // drop_side_effect_free()
  8089. // remove side-effect-free parts which only affects return value
  8090. (function(def) {
  8091. // Drop side-effect-free elements from an array of expressions.
  8092. // Returns an array of expressions with side-effects or null
  8093. // if all elements were dropped. Note: original array may be
  8094. // returned if nothing changed.
  8095. function trim(nodes, compressor, first_in_statement, spread) {
  8096. var len = nodes.length;
  8097. var ret = [], changed = false;
  8098. for (var i = 0; i < len; i++) {
  8099. var node = nodes[i];
  8100. var trimmed;
  8101. if (spread && node instanceof AST_Spread) {
  8102. trimmed = spread(node, compressor, first_in_statement);
  8103. } else {
  8104. trimmed = node.drop_side_effect_free(compressor, first_in_statement);
  8105. }
  8106. if (trimmed !== node) changed = true;
  8107. if (trimmed) {
  8108. ret.push(trimmed);
  8109. first_in_statement = false;
  8110. }
  8111. }
  8112. return ret.length ? changed ? ret : nodes : null;
  8113. }
  8114. function array_spread(node, compressor, first_in_statement) {
  8115. var exp = node.expression;
  8116. if (!exp.is_string(compressor)) return node;
  8117. return exp.drop_side_effect_free(compressor, first_in_statement);
  8118. }
  8119. function convert_spread(node) {
  8120. return node instanceof AST_Spread ? make_node(AST_Array, node, { elements: [ node ] }) : node;
  8121. }
  8122. def(AST_Node, return_this);
  8123. def(AST_Accessor, return_null);
  8124. def(AST_Array, function(compressor, first_in_statement) {
  8125. var values = trim(this.elements, compressor, first_in_statement, array_spread);
  8126. if (!values) return null;
  8127. if (values === this.elements && all(values, function(node) {
  8128. return node instanceof AST_Spread;
  8129. })) return this;
  8130. return make_sequence(this, values.map(convert_spread));
  8131. });
  8132. def(AST_Assign, function(compressor) {
  8133. var left = this.left;
  8134. if (left instanceof AST_PropAccess) {
  8135. var expr = left.expression;
  8136. if (expr.may_throw_on_access(compressor, true)) return this;
  8137. if (compressor.has_directive("use strict") && expr.is_constant()) return this;
  8138. }
  8139. if (left.has_side_effects(compressor)) return this;
  8140. if (lazy_op[this.operator.slice(0, -1)]) return this;
  8141. this.write_only = true;
  8142. if (!root_expr(left).is_constant_expression(compressor.find_parent(AST_Scope))) return this;
  8143. return this.right.drop_side_effect_free(compressor);
  8144. });
  8145. def(AST_Await, function(compressor) {
  8146. if (!compressor.option("awaits")) return this;
  8147. var exp = this.expression;
  8148. if (!is_primitive(compressor, exp)) return this;
  8149. if (exp instanceof AST_UnaryPrefix && exp.operator == "!") exp = exp.expression;
  8150. var dropped = exp.drop_side_effect_free(compressor);
  8151. if (dropped === exp) return this;
  8152. if (!dropped) {
  8153. dropped = make_node(AST_Number, exp, { value: 0 });
  8154. } else if (!is_primitive(compressor, dropped)) {
  8155. dropped = dropped.negate(compressor);
  8156. }
  8157. var node = this.clone();
  8158. node.expression = dropped;
  8159. return node;
  8160. });
  8161. def(AST_Binary, function(compressor, first_in_statement) {
  8162. var left = this.left;
  8163. var right = this.right;
  8164. var op = this.operator;
  8165. if (op == "in" && !is_object(right)) {
  8166. var lhs = left.drop_side_effect_free(compressor, first_in_statement);
  8167. if (lhs === left) return this;
  8168. var node = this.clone();
  8169. node.left = lhs || make_node(AST_Number, left, { value: 0 });
  8170. return node;
  8171. }
  8172. var rhs = right.drop_side_effect_free(compressor, first_in_statement);
  8173. if (!rhs) return left.drop_side_effect_free(compressor, first_in_statement);
  8174. if (lazy_op[op] && rhs.has_side_effects(compressor)) {
  8175. var node = this;
  8176. if (rhs !== right) {
  8177. node = node.clone();
  8178. node.right = rhs.drop_side_effect_free(compressor);
  8179. }
  8180. if (op == "??") return node;
  8181. var negated = node.clone();
  8182. negated.operator = op == "&&" ? "||" : "&&";
  8183. negated.left = left.negate(compressor, first_in_statement);
  8184. if (negated.operator == negated.right.operator) swap_chain(negated);
  8185. var best = first_in_statement ? best_of_statement : best_of_expression;
  8186. return op == "&&" ? best(node, negated) : best(negated, node);
  8187. }
  8188. var lhs = left.drop_side_effect_free(compressor, first_in_statement);
  8189. if (!lhs) return rhs;
  8190. rhs = rhs.drop_side_effect_free(compressor);
  8191. if (!rhs) return lhs;
  8192. return make_sequence(this, [ lhs, rhs ]);
  8193. });
  8194. function assign_this_only(fn, compressor) {
  8195. fn.new = true;
  8196. var result = all(fn.body, function(stat) {
  8197. return !stat.has_side_effects(compressor);
  8198. }) && all(fn.argnames, function(argname) {
  8199. return !argname.match_symbol(return_false);
  8200. }) && !(fn.rest && fn.rest.match_symbol(return_false));
  8201. fn.new = false;
  8202. return result;
  8203. }
  8204. def(AST_Call, function(compressor, first_in_statement) {
  8205. var self = this;
  8206. if (self.is_expr_pure(compressor)) {
  8207. if (self.pure) AST_Node.warn("Dropping __PURE__ call [{file}:{line},{col}]", self.start);
  8208. var args = trim(self.args, compressor, first_in_statement, array_spread);
  8209. return args && make_sequence(self, args.map(convert_spread));
  8210. }
  8211. var exp = self.expression;
  8212. if (self.is_call_pure(compressor)) {
  8213. var exprs = self.args.slice();
  8214. exprs.unshift(exp.expression);
  8215. exprs = trim(exprs, compressor, first_in_statement, array_spread);
  8216. return exprs && make_sequence(self, exprs.map(convert_spread));
  8217. }
  8218. if (compressor.option("yields") && is_generator(exp)) {
  8219. var call = self.clone();
  8220. call.expression = make_node(AST_Function, exp, exp);
  8221. call.expression.body = [];
  8222. var opt = call.transform(compressor);
  8223. if (opt !== call) return opt.drop_side_effect_free(compressor, first_in_statement);
  8224. }
  8225. var dropped = drop_returns(compressor, exp);
  8226. if (dropped) {
  8227. // always shallow clone to ensure stripping of negated IIFEs
  8228. self = self.clone();
  8229. self.expression = dropped;
  8230. // avoid extraneous traversal
  8231. if (exp._squeezed) self.expression._squeezed = true;
  8232. }
  8233. if (self instanceof AST_New) {
  8234. var fn = exp;
  8235. if (fn instanceof AST_SymbolRef) fn = fn.fixed_value();
  8236. if (fn instanceof AST_Lambda) {
  8237. if (assign_this_only(fn, compressor)) {
  8238. var exprs = self.args.slice();
  8239. exprs.unshift(exp);
  8240. exprs = trim(exprs, compressor, first_in_statement, array_spread);
  8241. return exprs && make_sequence(self, exprs.map(convert_spread));
  8242. }
  8243. if (!fn.contains_this()) {
  8244. self = make_node(AST_Call, self, self);
  8245. self.expression = self.expression.clone();
  8246. self.args = self.args.slice();
  8247. }
  8248. }
  8249. }
  8250. self.call_only = true;
  8251. return self;
  8252. });
  8253. def(AST_ClassExpression, function(compressor, first_in_statement) {
  8254. var self = this;
  8255. var exprs = [], values = [];
  8256. var props = self.properties;
  8257. for (var i = 0; i < props.length; i++) {
  8258. var prop = props[i];
  8259. if (prop.key instanceof AST_Node) exprs.push(prop.key);
  8260. if (prop.static && prop.value
  8261. && prop instanceof AST_ClassField
  8262. && prop.value.has_side_effects(compressor)) {
  8263. if (prop.value.contains_this()) return self;
  8264. values.push(prop.value);
  8265. }
  8266. }
  8267. var base = self.extends;
  8268. if (base) {
  8269. if (base instanceof AST_SymbolRef) base = base.fixed_value();
  8270. base = !safe_for_extends(base);
  8271. if (!base) exprs.unshift(self.extends);
  8272. }
  8273. exprs = trim(exprs, compressor, first_in_statement);
  8274. if (exprs) first_in_statement = false;
  8275. values = trim(values, compressor, first_in_statement);
  8276. if (!exprs) {
  8277. if (!base && !values && !self.name) return null;
  8278. exprs = [];
  8279. }
  8280. if (base || self.name || !compressor.has_directive("use strict")) {
  8281. var node = to_class_expr(self);
  8282. if (!base) node.extends = null;
  8283. node.properties = [];
  8284. if (values) {
  8285. node.properties.push(make_node(AST_ClassField, self, {
  8286. static: true,
  8287. key: exprs.length ? make_sequence(self, exprs) : "c",
  8288. value: make_sequence(self, values),
  8289. }));
  8290. } else if (exprs.length) {
  8291. node.properties.push(make_node(AST_ClassMethod, self, {
  8292. key: make_sequence(self, exprs),
  8293. value: make_node(AST_Function, self, {
  8294. argnames: [],
  8295. body: [],
  8296. }).init_vars(node),
  8297. }));
  8298. }
  8299. return node;
  8300. }
  8301. if (values) exprs.push(make_node(AST_Call, self, {
  8302. expression: make_node(AST_Arrow, self, {
  8303. argnames: [],
  8304. body: [],
  8305. value: make_sequence(self, values),
  8306. }).init_vars(self.parent_scope),
  8307. args: [],
  8308. }));
  8309. return make_sequence(self, exprs);
  8310. });
  8311. def(AST_Conditional, function(compressor) {
  8312. var consequent = this.consequent.drop_side_effect_free(compressor);
  8313. var alternative = this.alternative.drop_side_effect_free(compressor);
  8314. if (consequent === this.consequent && alternative === this.alternative) return this;
  8315. var exprs;
  8316. if (compressor.option("ie")) {
  8317. exprs = [];
  8318. if (consequent instanceof AST_Function) {
  8319. exprs.push(consequent);
  8320. consequent = null;
  8321. }
  8322. if (alternative instanceof AST_Function) {
  8323. exprs.push(alternative);
  8324. alternative = null;
  8325. }
  8326. }
  8327. var node;
  8328. if (!consequent) {
  8329. node = alternative ? make_node(AST_Binary, this, {
  8330. operator: "||",
  8331. left: this.condition,
  8332. right: alternative,
  8333. }) : this.condition.drop_side_effect_free(compressor);
  8334. } else if (!alternative) {
  8335. node = make_node(AST_Binary, this, {
  8336. operator: "&&",
  8337. left: this.condition,
  8338. right: consequent,
  8339. });
  8340. } else {
  8341. node = this.clone();
  8342. node.consequent = consequent;
  8343. node.alternative = alternative;
  8344. }
  8345. if (!exprs) return node;
  8346. if (node) exprs.push(node);
  8347. return exprs.length == 0 ? null : make_sequence(this, exprs);
  8348. });
  8349. def(AST_Constant, return_null);
  8350. def(AST_Dot, function(compressor, first_in_statement) {
  8351. var expr = this.expression;
  8352. if (expr.may_throw_on_access(compressor)) return this;
  8353. return expr.drop_side_effect_free(compressor, first_in_statement);
  8354. });
  8355. def(AST_Function, function(compressor) {
  8356. return fn_name_unused(this, compressor) ? null : this;
  8357. });
  8358. def(AST_LambdaExpression, return_null);
  8359. def(AST_Object, function(compressor, first_in_statement) {
  8360. var exprs = [];
  8361. this.properties.forEach(function(prop) {
  8362. if (prop instanceof AST_Spread) {
  8363. exprs.push(prop);
  8364. } else {
  8365. if (prop.key instanceof AST_Node) exprs.push(prop.key);
  8366. exprs.push(prop.value);
  8367. }
  8368. });
  8369. var values = trim(exprs, compressor, first_in_statement, function(node, compressor, first_in_statement) {
  8370. var exp = node.expression;
  8371. return exp.safe_to_spread() ? exp.drop_side_effect_free(compressor, first_in_statement) : node;
  8372. });
  8373. if (!values) return null;
  8374. if (values === exprs && !all(values, function(node) {
  8375. return !(node instanceof AST_Spread);
  8376. })) return this;
  8377. return make_sequence(this, values.map(function(node) {
  8378. return node instanceof AST_Spread ? make_node(AST_Object, node, { properties: [ node ] }) : node;
  8379. }));
  8380. });
  8381. def(AST_ObjectIdentity, return_null);
  8382. def(AST_Sequence, function(compressor, first_in_statement) {
  8383. var expressions = trim(this.expressions, compressor, first_in_statement);
  8384. if (!expressions) return null;
  8385. var end = expressions.length - 1;
  8386. var last = expressions[end];
  8387. if (compressor.option("awaits") && end > 0 && last instanceof AST_Await && last.expression.is_constant()) {
  8388. expressions = expressions.slice(0, -1);
  8389. end--;
  8390. var expr = expressions[end];
  8391. last.expression = is_primitive(compressor, expr) ? expr : expr.negate(compressor);
  8392. expressions[end] = last;
  8393. }
  8394. var assign, cond, lhs;
  8395. if (compressor.option("conditionals")
  8396. && end > 0
  8397. && (assign = expressions[end - 1]) instanceof AST_Assign
  8398. && assign.operator == "="
  8399. && (lhs = assign.left) instanceof AST_SymbolRef
  8400. && (cond = to_conditional_assignment(compressor, lhs.definition(), assign.right, last))) {
  8401. assign = assign.clone();
  8402. assign.right = cond;
  8403. expressions = expressions.slice(0, -2);
  8404. expressions.push(assign.drop_side_effect_free(compressor, first_in_statement));
  8405. }
  8406. return expressions === this.expressions ? this : make_sequence(this, expressions);
  8407. });
  8408. def(AST_Sub, function(compressor, first_in_statement) {
  8409. var expr = this.expression;
  8410. if (expr.may_throw_on_access(compressor)) return this;
  8411. var prop = this.property;
  8412. expr = expr.drop_side_effect_free(compressor, first_in_statement);
  8413. if (!expr) return prop.drop_side_effect_free(compressor, first_in_statement);
  8414. prop = prop.drop_side_effect_free(compressor);
  8415. if (!prop) return expr;
  8416. return make_sequence(this, [ expr, prop ]);
  8417. });
  8418. def(AST_SymbolRef, function(compressor) {
  8419. return this.is_declared(compressor) && can_drop_symbol(this, compressor) ? null : this;
  8420. });
  8421. def(AST_Template, function(compressor, first_in_statement) {
  8422. var self = this;
  8423. if (self.is_expr_pure(compressor)) {
  8424. var expressions = self.expressions;
  8425. if (expressions.length == 0) return null;
  8426. return make_sequence(self, expressions).drop_side_effect_free(compressor, first_in_statement);
  8427. }
  8428. var tag = self.tag;
  8429. var dropped = drop_returns(compressor, tag);
  8430. if (dropped) {
  8431. // always shallow clone to signal internal changes
  8432. self = self.clone();
  8433. self.tag = dropped;
  8434. // avoid extraneous traversal
  8435. if (tag._squeezed) self.tag._squeezed = true;
  8436. }
  8437. return self;
  8438. });
  8439. def(AST_Unary, function(compressor, first_in_statement) {
  8440. var exp = this.expression;
  8441. if (unary_side_effects[this.operator]) {
  8442. this.write_only = !exp.has_side_effects(compressor);
  8443. return this;
  8444. }
  8445. if (this.operator == "typeof" && exp instanceof AST_SymbolRef && can_drop_symbol(exp, compressor)) {
  8446. return null;
  8447. }
  8448. var node = exp.drop_side_effect_free(compressor, first_in_statement);
  8449. if (first_in_statement && node && is_iife_call(node)) {
  8450. if (node === exp && this.operator == "!") return this;
  8451. return node.negate(compressor, first_in_statement);
  8452. }
  8453. return node;
  8454. });
  8455. })(function(node, func) {
  8456. node.DEFMETHOD("drop_side_effect_free", func);
  8457. });
  8458. OPT(AST_SimpleStatement, function(self, compressor) {
  8459. if (compressor.option("side_effects")) {
  8460. var body = self.body;
  8461. var node = body.drop_side_effect_free(compressor, true);
  8462. if (!node) {
  8463. AST_Node.warn("Dropping side-effect-free statement [{file}:{line},{col}]", self.start);
  8464. return make_node(AST_EmptyStatement, self);
  8465. }
  8466. if (node !== body) {
  8467. return make_node(AST_SimpleStatement, self, { body: node });
  8468. }
  8469. }
  8470. return self;
  8471. });
  8472. OPT(AST_While, function(self, compressor) {
  8473. return compressor.option("loops") ? make_node(AST_For, self, self).optimize(compressor) : self;
  8474. });
  8475. function has_loop_control(loop, parent, type) {
  8476. if (!type) type = AST_LoopControl;
  8477. var found = false;
  8478. var tw = new TreeWalker(function(node) {
  8479. if (found || node instanceof AST_Scope) return true;
  8480. if (node instanceof type && tw.loopcontrol_target(node) === loop) {
  8481. return found = true;
  8482. }
  8483. });
  8484. if (parent instanceof AST_LabeledStatement) tw.push(parent);
  8485. tw.push(loop);
  8486. loop.body.walk(tw);
  8487. return found;
  8488. }
  8489. OPT(AST_Do, function(self, compressor) {
  8490. if (!compressor.option("loops")) return self;
  8491. var cond = fuzzy_eval(compressor, self.condition);
  8492. if (!(cond instanceof AST_Node)) {
  8493. if (cond && !has_loop_control(self, compressor.parent(), AST_Continue)) return make_node(AST_For, self, {
  8494. body: make_node(AST_BlockStatement, self.body, {
  8495. body: [
  8496. self.body,
  8497. make_node(AST_SimpleStatement, self.condition, {
  8498. body: self.condition
  8499. }),
  8500. ]
  8501. })
  8502. }).optimize(compressor);
  8503. if (!has_loop_control(self, compressor.parent())) return make_node(AST_BlockStatement, self.body, {
  8504. body: [
  8505. self.body,
  8506. make_node(AST_SimpleStatement, self.condition, {
  8507. body: self.condition
  8508. }),
  8509. ]
  8510. }).optimize(compressor);
  8511. }
  8512. if (self.body instanceof AST_BlockStatement && !has_loop_control(self, compressor.parent(), AST_Continue)) {
  8513. var body = self.body.body;
  8514. for (var i = body.length; --i >= 0;) {
  8515. var stat = body[i];
  8516. if (stat instanceof AST_If
  8517. && !stat.alternative
  8518. && stat.body instanceof AST_Break
  8519. && compressor.loopcontrol_target(stat.body) === self) {
  8520. if (has_block_scope_refs(stat.condition)) break;
  8521. self.condition = make_node(AST_Binary, self, {
  8522. operator: "&&",
  8523. left: stat.condition.negate(compressor),
  8524. right: self.condition,
  8525. });
  8526. body.splice(i, 1);
  8527. } else if (stat instanceof AST_SimpleStatement) {
  8528. if (has_block_scope_refs(stat.body)) break;
  8529. self.condition = make_sequence(self, [
  8530. stat.body,
  8531. self.condition,
  8532. ]);
  8533. body.splice(i, 1);
  8534. } else if (!is_declaration(stat, true)) {
  8535. break;
  8536. }
  8537. }
  8538. self.body = trim_block(self.body, compressor.parent());
  8539. }
  8540. if (self.body instanceof AST_EmptyStatement) return make_node(AST_For, self, self).optimize(compressor);
  8541. if (self.body instanceof AST_SimpleStatement) return make_node(AST_For, self, {
  8542. condition: make_sequence(self.condition, [
  8543. self.body.body,
  8544. self.condition
  8545. ]),
  8546. body: make_node(AST_EmptyStatement, self)
  8547. }).optimize(compressor);
  8548. return self;
  8549. function has_block_scope_refs(node) {
  8550. var found = false;
  8551. node.walk(new TreeWalker(function(node) {
  8552. if (found) return true;
  8553. if (node instanceof AST_SymbolRef) {
  8554. if (!member(node.definition(), self.enclosed)) found = true;
  8555. return true;
  8556. }
  8557. }));
  8558. return found;
  8559. }
  8560. });
  8561. function if_break_in_loop(self, compressor) {
  8562. var first = first_statement(self.body);
  8563. if (compressor.option("dead_code")
  8564. && (first instanceof AST_Break
  8565. || first instanceof AST_Continue && external_target(first)
  8566. || first instanceof AST_Exit)) {
  8567. var body = [];
  8568. if (is_statement(self.init)) {
  8569. body.push(self.init);
  8570. } else if (self.init) {
  8571. body.push(make_node(AST_SimpleStatement, self.init, {
  8572. body: self.init
  8573. }));
  8574. }
  8575. var retain = external_target(first) || first instanceof AST_Exit;
  8576. if (self.condition && retain) {
  8577. body.push(make_node(AST_If, self, {
  8578. condition: self.condition,
  8579. body: first,
  8580. alternative: null
  8581. }));
  8582. } else if (self.condition) {
  8583. body.push(make_node(AST_SimpleStatement, self.condition, {
  8584. body: self.condition
  8585. }));
  8586. } else if (retain) {
  8587. body.push(first);
  8588. }
  8589. extract_declarations_from_unreachable_code(compressor, self.body, body);
  8590. return make_node(AST_BlockStatement, self, {
  8591. body: body
  8592. });
  8593. }
  8594. if (first instanceof AST_If) {
  8595. var ab = first_statement(first.body);
  8596. if (ab instanceof AST_Break && !external_target(ab)) {
  8597. if (self.condition) {
  8598. self.condition = make_node(AST_Binary, self.condition, {
  8599. left: self.condition,
  8600. operator: "&&",
  8601. right: first.condition.negate(compressor),
  8602. });
  8603. } else {
  8604. self.condition = first.condition.negate(compressor);
  8605. }
  8606. var body = as_statement_array(first.alternative);
  8607. extract_declarations_from_unreachable_code(compressor, first.body, body);
  8608. return drop_it(body);
  8609. }
  8610. ab = first_statement(first.alternative);
  8611. if (ab instanceof AST_Break && !external_target(ab)) {
  8612. if (self.condition) {
  8613. self.condition = make_node(AST_Binary, self.condition, {
  8614. left: self.condition,
  8615. operator: "&&",
  8616. right: first.condition,
  8617. });
  8618. } else {
  8619. self.condition = first.condition;
  8620. }
  8621. var body = as_statement_array(first.body);
  8622. extract_declarations_from_unreachable_code(compressor, first.alternative, body);
  8623. return drop_it(body);
  8624. }
  8625. }
  8626. return self;
  8627. function first_statement(body) {
  8628. return body instanceof AST_BlockStatement ? body.body[0] : body;
  8629. }
  8630. function external_target(node) {
  8631. return compressor.loopcontrol_target(node) !== compressor.self();
  8632. }
  8633. function drop_it(rest) {
  8634. if (self.body instanceof AST_BlockStatement) {
  8635. self.body = self.body.clone();
  8636. self.body.body = rest.concat(self.body.body.slice(1));
  8637. self.body = self.body.transform(compressor);
  8638. } else {
  8639. self.body = make_node(AST_BlockStatement, self.body, {
  8640. body: rest
  8641. }).transform(compressor);
  8642. }
  8643. return if_break_in_loop(self, compressor);
  8644. }
  8645. }
  8646. OPT(AST_For, function(self, compressor) {
  8647. if (!compressor.option("loops")) return self;
  8648. if (compressor.option("side_effects")) {
  8649. if (self.init) self.init = self.init.drop_side_effect_free(compressor);
  8650. if (self.step) self.step = self.step.drop_side_effect_free(compressor);
  8651. }
  8652. if (self.condition) {
  8653. var cond = fuzzy_eval(compressor, self.condition);
  8654. if (!cond) {
  8655. if (compressor.option("dead_code")) {
  8656. var body = [];
  8657. if (is_statement(self.init)) {
  8658. body.push(self.init);
  8659. } else if (self.init) {
  8660. body.push(make_node(AST_SimpleStatement, self.init, { body: self.init }));
  8661. }
  8662. body.push(make_node(AST_SimpleStatement, self.condition, { body: self.condition }));
  8663. extract_declarations_from_unreachable_code(compressor, self.body, body);
  8664. return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
  8665. }
  8666. } else if (!(cond instanceof AST_Node)) {
  8667. self.body = make_node(AST_BlockStatement, self.body, {
  8668. body: [
  8669. make_node(AST_SimpleStatement, self.condition, { body: self.condition }),
  8670. self.body,
  8671. ],
  8672. });
  8673. self.condition = null;
  8674. }
  8675. }
  8676. return if_break_in_loop(self, compressor);
  8677. });
  8678. OPT(AST_ForEnumeration, function(self, compressor) {
  8679. if (compressor.option("varify") && is_lexical_definition(self.init)) {
  8680. var name = self.init.definitions[0].name;
  8681. if ((name instanceof AST_Destructured || name instanceof AST_SymbolLet)
  8682. && !name.match_symbol(function(node) {
  8683. if (node instanceof AST_SymbolDeclaration) {
  8684. var def = node.definition();
  8685. return !same_scope(def) || may_overlap(compressor, def);
  8686. }
  8687. }, true)) {
  8688. self.init = to_var(self.init);
  8689. }
  8690. }
  8691. return self;
  8692. });
  8693. function mark_locally_defined(condition, consequent, alternative) {
  8694. if (condition instanceof AST_Sequence) condition = condition.tail_node();
  8695. if (!(condition instanceof AST_Binary)) return;
  8696. if (!(condition.left instanceof AST_String)) {
  8697. switch (condition.operator) {
  8698. case "&&":
  8699. mark_locally_defined(condition.left, consequent);
  8700. mark_locally_defined(condition.right, consequent);
  8701. break;
  8702. case "||":
  8703. mark_locally_defined(negate(condition.left), alternative);
  8704. mark_locally_defined(negate(condition.right), alternative);
  8705. break;
  8706. }
  8707. return;
  8708. }
  8709. if (!(condition.right instanceof AST_UnaryPrefix)) return;
  8710. if (condition.right.operator != "typeof") return;
  8711. var sym = condition.right.expression;
  8712. if (!is_undeclared_ref(sym)) return;
  8713. var body;
  8714. var undef = condition.left.value == "undefined";
  8715. switch (condition.operator) {
  8716. case "==":
  8717. body = undef ? alternative : consequent;
  8718. break;
  8719. case "!=":
  8720. body = undef ? consequent : alternative;
  8721. break;
  8722. default:
  8723. return;
  8724. }
  8725. if (!body) return;
  8726. var abort = false;
  8727. var def = sym.definition();
  8728. var fn;
  8729. var refs = [];
  8730. var scanned = [];
  8731. var tw = new TreeWalker(function(node, descend) {
  8732. if (abort) return true;
  8733. if (node instanceof AST_Assign) {
  8734. var ref = node.left;
  8735. if (!(ref instanceof AST_SymbolRef && ref.definition() === def)) return;
  8736. node.right.walk(tw);
  8737. switch (node.operator) {
  8738. case "=":
  8739. case "&&=":
  8740. abort = true;
  8741. }
  8742. return true;
  8743. }
  8744. if (node instanceof AST_Call) {
  8745. descend();
  8746. fn = node.expression.tail_node();
  8747. var save;
  8748. if (fn instanceof AST_SymbolRef) {
  8749. fn = fn.fixed_value();
  8750. save = refs.length;
  8751. }
  8752. if (!(fn instanceof AST_Lambda)) {
  8753. abort = true;
  8754. } else if (push_uniq(scanned, fn)) {
  8755. fn.walk(tw);
  8756. }
  8757. if (save >= 0) refs.length = save;
  8758. return true;
  8759. }
  8760. if (node instanceof AST_DWLoop) {
  8761. var save = refs.length;
  8762. descend();
  8763. if (abort) refs.length = save;
  8764. return true;
  8765. }
  8766. if (node instanceof AST_For) {
  8767. if (node.init) node.init.walk(tw);
  8768. var save = refs.length;
  8769. if (node.condition) node.condition.walk(tw);
  8770. node.body.walk(tw);
  8771. if (node.step) node.step.walk(tw);
  8772. if (abort) refs.length = save;
  8773. return true;
  8774. }
  8775. if (node instanceof AST_ForEnumeration) {
  8776. node.object.walk(tw);
  8777. var save = refs.length;
  8778. node.init.walk(tw);
  8779. node.body.walk(tw);
  8780. if (abort) refs.length = save;
  8781. return true;
  8782. }
  8783. if (node instanceof AST_Scope) {
  8784. if (node === fn) return;
  8785. return true;
  8786. }
  8787. if (node instanceof AST_SymbolRef) {
  8788. if (node.definition() === def) refs.push(node);
  8789. return true;
  8790. }
  8791. });
  8792. body.walk(tw);
  8793. refs.forEach(function(ref) {
  8794. ref.defined = true;
  8795. });
  8796. function negate(node) {
  8797. if (!(node instanceof AST_Binary)) return;
  8798. switch (node.operator) {
  8799. case "==":
  8800. node = node.clone();
  8801. node.operator = "!=";
  8802. return node;
  8803. case "!=":
  8804. node = node.clone();
  8805. node.operator = "==";
  8806. return node;
  8807. }
  8808. }
  8809. }
  8810. function fuzzy_eval(compressor, node, nullish) {
  8811. if (node.truthy) return true;
  8812. if (node.falsy && !nullish) return false;
  8813. if (node.is_truthy()) return true;
  8814. return node.evaluate(compressor, true);
  8815. }
  8816. function mark_duplicate_condition(compressor, node) {
  8817. var child;
  8818. var level = 0;
  8819. var negated = false;
  8820. var parent = compressor.self();
  8821. if (!is_statement(parent)) while (true) {
  8822. child = parent;
  8823. parent = compressor.parent(level++);
  8824. if (parent instanceof AST_Binary) {
  8825. var op = parent.operator;
  8826. if (!lazy_op[op]) return;
  8827. var left = parent.left;
  8828. if (left === child) continue;
  8829. if (match(left)) switch (op) {
  8830. case "&&":
  8831. node[negated ? "falsy" : "truthy"] = true;
  8832. break;
  8833. case "||":
  8834. case "??":
  8835. node[negated ? "truthy" : "falsy"] = true;
  8836. break;
  8837. }
  8838. } else if (parent instanceof AST_Conditional) {
  8839. var cond = parent.condition;
  8840. if (cond === child) continue;
  8841. if (match(cond)) switch (child) {
  8842. case parent.consequent:
  8843. node[negated ? "falsy" : "truthy"] = true;
  8844. break;
  8845. case parent.alternative:
  8846. node[negated ? "truthy" : "falsy"] = true;
  8847. break;
  8848. }
  8849. } else if (parent instanceof AST_Exit) {
  8850. break;
  8851. } else if (parent instanceof AST_If) {
  8852. break;
  8853. } else if (parent instanceof AST_Sequence) {
  8854. if (parent.expressions[0] === child) continue;
  8855. } else if (parent instanceof AST_SimpleStatement) {
  8856. break;
  8857. }
  8858. return;
  8859. }
  8860. while (true) {
  8861. child = parent;
  8862. parent = compressor.parent(level++);
  8863. if (parent instanceof AST_BlockStatement) {
  8864. if (parent.body[0] === child) continue;
  8865. } else if (parent instanceof AST_If) {
  8866. if (match(parent.condition)) switch (child) {
  8867. case parent.body:
  8868. node[negated ? "falsy" : "truthy"] = true;
  8869. break;
  8870. case parent.alternative:
  8871. node[negated ? "truthy" : "falsy"] = true;
  8872. break;
  8873. }
  8874. }
  8875. return;
  8876. }
  8877. function match(cond) {
  8878. if (node.equivalent_to(cond)) return true;
  8879. if (!(cond instanceof AST_UnaryPrefix)) return false;
  8880. if (cond.operator != "!") return false;
  8881. if (!node.equivalent_to(cond.expression)) return false;
  8882. negated = true;
  8883. return true;
  8884. }
  8885. }
  8886. OPT(AST_If, function(self, compressor) {
  8887. if (is_empty(self.alternative)) self.alternative = null;
  8888. if (!compressor.option("conditionals")) return self;
  8889. if (compressor.option("booleans") && !self.condition.has_side_effects(compressor)) {
  8890. mark_duplicate_condition(compressor, self.condition);
  8891. }
  8892. // if condition can be statically determined, warn and drop
  8893. // one of the blocks. note, statically determined implies
  8894. // “has no side effects”; also it doesn't work for cases like
  8895. // `x && true`, though it probably should.
  8896. if (compressor.option("dead_code")) {
  8897. var cond = fuzzy_eval(compressor, self.condition);
  8898. if (!cond) {
  8899. AST_Node.warn("Condition always false [{file}:{line},{col}]", self.condition.start);
  8900. var body = [
  8901. make_node(AST_SimpleStatement, self.condition, { body: self.condition }).transform(compressor),
  8902. ];
  8903. extract_declarations_from_unreachable_code(compressor, self.body, body);
  8904. if (self.alternative) body.push(self.alternative);
  8905. return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
  8906. } else if (!(cond instanceof AST_Node)) {
  8907. AST_Node.warn("Condition always true [{file}:{line},{col}]", self.condition.start);
  8908. var body = [
  8909. make_node(AST_SimpleStatement, self.condition, { body: self.condition }).transform(compressor),
  8910. self.body,
  8911. ];
  8912. if (self.alternative) extract_declarations_from_unreachable_code(compressor, self.alternative, body);
  8913. return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
  8914. }
  8915. }
  8916. var negated = self.condition.negate(compressor);
  8917. var self_condition_length = self.condition.print_to_string().length;
  8918. var negated_length = negated.print_to_string().length;
  8919. var negated_is_best = negated_length < self_condition_length;
  8920. if (self.alternative && negated_is_best) {
  8921. negated_is_best = false; // because we already do the switch here.
  8922. // no need to swap values of self_condition_length and negated_length
  8923. // here because they are only used in an equality comparison later on.
  8924. self.condition = negated;
  8925. var tmp = self.body;
  8926. self.body = self.alternative;
  8927. self.alternative = is_empty(tmp) ? null : tmp;
  8928. }
  8929. var body_defuns = [];
  8930. var body_var_defs = [];
  8931. var body_refs = [];
  8932. var body_exprs = sequencesize(self.body, body_defuns, body_var_defs, body_refs);
  8933. var alt_defuns = [];
  8934. var alt_var_defs = [];
  8935. var alt_refs = [];
  8936. var alt_exprs = sequencesize(self.alternative, alt_defuns, alt_var_defs, alt_refs);
  8937. if (body_exprs instanceof AST_BlockStatement || alt_exprs instanceof AST_BlockStatement) {
  8938. var body = [], var_defs = [];
  8939. if (body_exprs) {
  8940. [].push.apply(body, body_defuns);
  8941. [].push.apply(var_defs, body_var_defs);
  8942. if (body_exprs instanceof AST_BlockStatement) {
  8943. self.body = body_exprs;
  8944. } else if (body_exprs.length == 0) {
  8945. self.body = make_node(AST_EmptyStatement, self.body);
  8946. } else {
  8947. self.body = make_node(AST_SimpleStatement, self.body, {
  8948. body: make_sequence(self.body, body_exprs),
  8949. });
  8950. }
  8951. body_refs.forEach(process_to_assign);
  8952. }
  8953. if (alt_exprs) {
  8954. [].push.apply(body, alt_defuns);
  8955. [].push.apply(var_defs, alt_var_defs);
  8956. if (alt_exprs instanceof AST_BlockStatement) {
  8957. self.alternative = alt_exprs;
  8958. } else if (alt_exprs.length == 0) {
  8959. self.alternative = null;
  8960. } else {
  8961. self.alternative = make_node(AST_SimpleStatement, self.alternative, {
  8962. body: make_sequence(self.alternative, alt_exprs),
  8963. });
  8964. }
  8965. alt_refs.forEach(process_to_assign);
  8966. }
  8967. if (var_defs.length > 0) body.push(make_node(AST_Var, self, { definitions: var_defs }));
  8968. if (body.length > 0) {
  8969. body.push(self);
  8970. return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
  8971. }
  8972. } else if (body_exprs && alt_exprs) {
  8973. var body = body_defuns.concat(alt_defuns);
  8974. if (body_var_defs.length > 0 || alt_var_defs.length > 0) body.push(make_node(AST_Var, self, {
  8975. definitions: body_var_defs.concat(alt_var_defs),
  8976. }));
  8977. if (body_exprs.length == 0) {
  8978. body.push(make_node(AST_SimpleStatement, self.condition, {
  8979. body: alt_exprs.length > 0 ? make_node(AST_Binary, self, {
  8980. operator: "||",
  8981. left: self.condition,
  8982. right: make_sequence(self.alternative, alt_exprs),
  8983. }).transform(compressor) : self.condition.clone(),
  8984. }).optimize(compressor));
  8985. } else if (alt_exprs.length == 0) {
  8986. if (self_condition_length === negated_length && !negated_is_best
  8987. && self.condition instanceof AST_Binary && self.condition.operator == "||") {
  8988. // although the code length of self.condition and negated are the same,
  8989. // negated does not require additional surrounding parentheses.
  8990. // see https://github.com/mishoo/UglifyJS/issues/979
  8991. negated_is_best = true;
  8992. }
  8993. body.push(make_node(AST_SimpleStatement, self, {
  8994. body: make_node(AST_Binary, self, {
  8995. operator: negated_is_best ? "||" : "&&",
  8996. left: negated_is_best ? negated : self.condition,
  8997. right: make_sequence(self.body, body_exprs),
  8998. }).transform(compressor),
  8999. }).optimize(compressor));
  9000. } else {
  9001. body.push(make_node(AST_SimpleStatement, self, {
  9002. body: make_node(AST_Conditional, self, {
  9003. condition: self.condition,
  9004. consequent: make_sequence(self.body, body_exprs),
  9005. alternative: make_sequence(self.alternative, alt_exprs),
  9006. }),
  9007. }).optimize(compressor));
  9008. }
  9009. body_refs.forEach(process_to_assign);
  9010. alt_refs.forEach(process_to_assign);
  9011. return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
  9012. }
  9013. if (is_empty(self.body)) self = make_node(AST_If, self, {
  9014. condition: negated,
  9015. body: self.alternative,
  9016. alternative: null,
  9017. });
  9018. if (self.alternative instanceof AST_Exit && self.body.TYPE == self.alternative.TYPE) {
  9019. var exit = make_node(self.body.CTOR, self, {
  9020. value: make_node(AST_Conditional, self, {
  9021. condition: self.condition,
  9022. consequent: self.body.value || make_node(AST_Undefined, self.body).transform(compressor),
  9023. alternative: self.alternative.value
  9024. || make_node(AST_Undefined, self.alternative).transform(compressor),
  9025. }),
  9026. });
  9027. if (exit instanceof AST_Return) exit.in_bool = self.body.in_bool || self.alternative.in_bool;
  9028. return exit;
  9029. }
  9030. if (self.body instanceof AST_If && !self.body.alternative && !self.alternative) {
  9031. self = make_node(AST_If, self, {
  9032. condition: make_node(AST_Binary, self.condition, {
  9033. operator: "&&",
  9034. left: self.condition,
  9035. right: self.body.condition,
  9036. }),
  9037. body: self.body.body,
  9038. alternative: null,
  9039. });
  9040. }
  9041. if (aborts(self.body) && self.alternative) {
  9042. var alt = self.alternative;
  9043. self.alternative = null;
  9044. return make_node(AST_BlockStatement, self, { body: [ self, alt ] }).optimize(compressor);
  9045. }
  9046. if (aborts(self.alternative)) {
  9047. var body = self.body;
  9048. self.body = self.alternative;
  9049. self.condition = negated_is_best ? negated : self.condition.negate(compressor);
  9050. self.alternative = null;
  9051. return make_node(AST_BlockStatement, self, { body: [ self, body ] }).optimize(compressor);
  9052. }
  9053. if (compressor.option("typeofs")) mark_locally_defined(self.condition, self.body, self.alternative);
  9054. return self;
  9055. function sequencesize(stat, defuns, var_defs, refs) {
  9056. if (stat == null) return [];
  9057. if (stat instanceof AST_BlockStatement) {
  9058. var exprs = [];
  9059. for (var i = 0; i < stat.body.length; i++) {
  9060. var line = stat.body[i];
  9061. if (line instanceof AST_EmptyStatement) continue;
  9062. if (line instanceof AST_Exit) {
  9063. if (i == 0) return;
  9064. if (exprs.length > 0) {
  9065. line = line.clone();
  9066. exprs.push(line.value || make_node(AST_Undefined, line).transform(compressor));
  9067. line.value = make_sequence(stat, exprs);
  9068. }
  9069. var block = stat.clone();
  9070. block.body = block.body.slice(i + 1);
  9071. block.body.unshift(line);
  9072. return block;
  9073. }
  9074. if (line instanceof AST_LambdaDefinition) {
  9075. defuns.push(line);
  9076. } else if (line instanceof AST_SimpleStatement) {
  9077. if (!compressor.option("sequences") && exprs.length > 0) return;
  9078. exprs.push(line.body);
  9079. } else if (line instanceof AST_Var) {
  9080. if (!compressor.option("sequences") && exprs.length > 0) return;
  9081. line.remove_initializers(compressor, var_defs);
  9082. line.definitions.forEach(process_var_def);
  9083. } else {
  9084. return;
  9085. }
  9086. }
  9087. return exprs;
  9088. }
  9089. if (stat instanceof AST_LambdaDefinition) {
  9090. defuns.push(stat);
  9091. return [];
  9092. }
  9093. if (stat instanceof AST_EmptyStatement) return [];
  9094. if (stat instanceof AST_SimpleStatement) return [ stat.body ];
  9095. if (stat instanceof AST_Var) {
  9096. var exprs = [];
  9097. stat.remove_initializers(compressor, var_defs);
  9098. stat.definitions.forEach(process_var_def);
  9099. return exprs;
  9100. }
  9101. function process_var_def(var_def) {
  9102. if (!var_def.value) return;
  9103. exprs.push(make_node(AST_Assign, var_def, {
  9104. operator: "=",
  9105. left: var_def.name.convert_symbol(AST_SymbolRef, function(ref) {
  9106. refs.push(ref);
  9107. }),
  9108. right: var_def.value,
  9109. }));
  9110. }
  9111. }
  9112. });
  9113. OPT(AST_Switch, function(self, compressor) {
  9114. if (!compressor.option("switches")) return self;
  9115. if (!compressor.option("dead_code")) return self;
  9116. var body = [];
  9117. var branch;
  9118. var decl = [];
  9119. var default_branch;
  9120. var exact_match;
  9121. var side_effects = [];
  9122. for (var i = 0, len = self.body.length; i < len; i++) {
  9123. branch = self.body[i];
  9124. if (branch instanceof AST_Default) {
  9125. var prev = body[body.length - 1];
  9126. if (default_branch || is_break(branch.body[0], compressor) && (!prev || aborts(prev))) {
  9127. eliminate_branch(branch, prev);
  9128. continue;
  9129. } else {
  9130. default_branch = branch;
  9131. }
  9132. } else {
  9133. var exp = branch.expression;
  9134. var equals = make_node(AST_Binary, self, {
  9135. operator: "===",
  9136. left: self.expression,
  9137. right: exp,
  9138. }).evaluate(compressor, true);
  9139. if (!equals) {
  9140. if (exp.has_side_effects(compressor)) side_effects.push(exp);
  9141. eliminate_branch(branch, body[body.length - 1]);
  9142. continue;
  9143. }
  9144. if (!(equals instanceof AST_Node)) {
  9145. if (default_branch) {
  9146. var default_index = body.indexOf(default_branch);
  9147. body.splice(default_index, 1);
  9148. eliminate_branch(default_branch, body[default_index - 1]);
  9149. default_branch = null;
  9150. }
  9151. if (exp.has_side_effects(compressor)) {
  9152. exact_match = branch;
  9153. } else {
  9154. default_branch = branch = make_node(AST_Default, branch, branch);
  9155. }
  9156. while (++i < len) eliminate_branch(self.body[i], branch);
  9157. }
  9158. }
  9159. if (i + 1 >= len || aborts(branch)) {
  9160. var prev = body[body.length - 1];
  9161. var statements = branch.body;
  9162. if (aborts(prev)) switch (prev.body.length - statements.length) {
  9163. case 1:
  9164. var stat = prev.body[prev.body.length - 1];
  9165. if (!is_break(stat, compressor)) break;
  9166. statements = statements.concat(stat);
  9167. case 0:
  9168. var prev_block = make_node(AST_BlockStatement, prev, prev);
  9169. var next_block = make_node(AST_BlockStatement, branch, { body: statements });
  9170. if (prev_block.equivalent_to(next_block)) prev.body = [];
  9171. }
  9172. }
  9173. if (side_effects.length) {
  9174. if (branch instanceof AST_Default) {
  9175. body.push(make_node(AST_Case, self, { expression: make_sequence(self, side_effects), body: [] }));
  9176. } else {
  9177. side_effects.push(branch.expression);
  9178. branch.expression = make_sequence(self, side_effects);
  9179. }
  9180. side_effects = [];
  9181. }
  9182. body.push(branch);
  9183. }
  9184. if (side_effects.length && !exact_match) {
  9185. body.push(make_node(AST_Case, self, { expression: make_sequence(self, side_effects), body: [] }));
  9186. }
  9187. while (branch = body[body.length - 1]) {
  9188. var stat = branch.body[branch.body.length - 1];
  9189. if (is_break(stat, compressor)) branch.body.pop();
  9190. if (branch === default_branch) {
  9191. if (!has_declarations_only(branch)) break;
  9192. } else if (branch.expression.has_side_effects(compressor)) {
  9193. break;
  9194. } else if (default_branch) {
  9195. if (!has_declarations_only(default_branch)) break;
  9196. if (body[body.length - 2] !== default_branch) break;
  9197. default_branch.body = default_branch.body.concat(branch.body);
  9198. branch.body = [];
  9199. } else if (!has_declarations_only(branch)) break;
  9200. eliminate_branch(branch);
  9201. if (body.pop() === default_branch) default_branch = null;
  9202. }
  9203. if (!branch) {
  9204. decl.push(make_node(AST_SimpleStatement, self.expression, { body: self.expression }));
  9205. if (side_effects.length) decl.push(make_node(AST_SimpleStatement, self, {
  9206. body: make_sequence(self, side_effects),
  9207. }));
  9208. return make_node(AST_BlockStatement, self, { body: decl }).optimize(compressor);
  9209. }
  9210. if (branch === default_branch) while (branch = body[body.length - 2]) {
  9211. if (branch instanceof AST_Default) break;
  9212. if (!has_declarations_only(branch)) break;
  9213. var exp = branch.expression;
  9214. if (exp.has_side_effects(compressor)) {
  9215. var prev = body[body.length - 3];
  9216. if (prev && !aborts(prev)) break;
  9217. default_branch.body.unshift(make_node(AST_SimpleStatement, self, { body: exp }));
  9218. }
  9219. eliminate_branch(branch);
  9220. body.splice(-2, 1);
  9221. }
  9222. body[0].body = decl.concat(body[0].body);
  9223. self.body = body;
  9224. if (compressor.option("conditionals")) switch (body.length) {
  9225. case 1:
  9226. if (!no_break(body[0])) break;
  9227. var exp = body[0].expression;
  9228. var statements = body[0].body.slice();
  9229. if (body[0] !== default_branch && body[0] !== exact_match) return make_node(AST_If, self, {
  9230. condition: make_node(AST_Binary, self, {
  9231. operator: "===",
  9232. left: self.expression,
  9233. right: exp,
  9234. }),
  9235. body: make_node(AST_BlockStatement, self, { body: statements }),
  9236. alternative: null,
  9237. }).optimize(compressor);
  9238. if (exp) statements.unshift(make_node(AST_SimpleStatement, exp, { body: exp }));
  9239. statements.unshift(make_node(AST_SimpleStatement, self.expression, { body: self.expression }));
  9240. return make_node(AST_BlockStatement, self, { body: statements }).optimize(compressor);
  9241. case 2:
  9242. if (!member(default_branch, body) || !no_break(body[1])) break;
  9243. var statements = body[0].body.slice();
  9244. var exclusive = statements.length && is_break(statements[statements.length - 1], compressor);
  9245. if (exclusive) statements.pop();
  9246. if (!all(statements, no_break)) break;
  9247. var alternative = body[1].body.length && make_node(AST_BlockStatement, body[1], body[1]);
  9248. var node = make_node(AST_If, self, {
  9249. condition: make_node(AST_Binary, self, body[0] === default_branch ? {
  9250. operator: "!==",
  9251. left: self.expression,
  9252. right: body[1].expression,
  9253. } : {
  9254. operator: "===",
  9255. left: self.expression,
  9256. right: body[0].expression,
  9257. }),
  9258. body: make_node(AST_BlockStatement, body[0], { body: statements }),
  9259. alternative: exclusive && alternative || null,
  9260. });
  9261. if (!exclusive && alternative) node = make_node(AST_BlockStatement, self, { body: [ node, alternative ] });
  9262. return node.optimize(compressor);
  9263. }
  9264. return self;
  9265. function is_break(node, tw) {
  9266. return node instanceof AST_Break && tw.loopcontrol_target(node) === self;
  9267. }
  9268. function no_break(node) {
  9269. var found = false;
  9270. var tw = new TreeWalker(function(node) {
  9271. if (found
  9272. || node instanceof AST_Lambda
  9273. || node instanceof AST_SimpleStatement) return true;
  9274. if (is_break(node, tw)) found = true;
  9275. });
  9276. tw.push(self);
  9277. node.walk(tw);
  9278. return !found;
  9279. }
  9280. function eliminate_branch(branch, prev) {
  9281. if (prev && !aborts(prev)) {
  9282. prev.body = prev.body.concat(branch.body);
  9283. } else {
  9284. extract_declarations_from_unreachable_code(compressor, branch, decl);
  9285. }
  9286. }
  9287. });
  9288. OPT(AST_Try, function(self, compressor) {
  9289. self.body = tighten_body(self.body, compressor);
  9290. if (compressor.option("dead_code")) {
  9291. if (has_declarations_only(self)
  9292. && !(self.bcatch && self.bcatch.argname && self.bcatch.argname.match_symbol(function(node) {
  9293. return node instanceof AST_SymbolCatch && !can_drop_symbol(node);
  9294. }, true))) {
  9295. var body = [];
  9296. if (self.bcatch) {
  9297. extract_declarations_from_unreachable_code(compressor, self.bcatch, body);
  9298. body.forEach(function(stat) {
  9299. if (!(stat instanceof AST_Var)) return;
  9300. stat.definitions.forEach(function(var_def) {
  9301. var def = var_def.name.definition().redefined();
  9302. if (!def) return;
  9303. var_def.name = var_def.name.clone();
  9304. var_def.name.thedef = def;
  9305. });
  9306. });
  9307. }
  9308. body.unshift(make_node(AST_BlockStatement, self, self).optimize(compressor));
  9309. if (self.bfinally) {
  9310. body.push(make_node(AST_BlockStatement, self.bfinally, self.bfinally).optimize(compressor));
  9311. }
  9312. return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
  9313. }
  9314. if (self.bfinally && has_declarations_only(self.bfinally)) {
  9315. var body = make_node(AST_BlockStatement, self.bfinally, self.bfinally).optimize(compressor);
  9316. body = self.body.concat(body);
  9317. if (!self.bcatch) return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
  9318. self.body = body;
  9319. self.bfinally = null;
  9320. }
  9321. }
  9322. return self;
  9323. });
  9324. function remove_initializers(make_value) {
  9325. return function(compressor, defns) {
  9326. var dropped = false;
  9327. this.definitions.forEach(function(defn) {
  9328. if (defn.value) dropped = true;
  9329. defn.name.match_symbol(function(node) {
  9330. if (node instanceof AST_SymbolDeclaration) defns.push(make_node(AST_VarDef, node, {
  9331. name: node,
  9332. value: make_value(compressor, node)
  9333. }));
  9334. }, true);
  9335. });
  9336. return dropped;
  9337. };
  9338. }
  9339. AST_Const.DEFMETHOD("remove_initializers", remove_initializers(function(compressor, node) {
  9340. return make_node(AST_Undefined, node).optimize(compressor);
  9341. }));
  9342. AST_Let.DEFMETHOD("remove_initializers", remove_initializers(return_null));
  9343. AST_Var.DEFMETHOD("remove_initializers", remove_initializers(return_null));
  9344. AST_Definitions.DEFMETHOD("to_assignments", function() {
  9345. var assignments = this.definitions.reduce(function(a, defn) {
  9346. var def = defn.name.definition();
  9347. var value = defn.value;
  9348. if (value) {
  9349. if (value instanceof AST_Sequence) value = value.clone();
  9350. var name = make_node(AST_SymbolRef, defn.name, defn.name);
  9351. var assign = make_node(AST_Assign, defn, {
  9352. operator: "=",
  9353. left: name,
  9354. right: value,
  9355. });
  9356. a.push(assign);
  9357. var fixed = function() {
  9358. return assign.right;
  9359. };
  9360. fixed.assigns = [ assign ];
  9361. fixed.direct_access = def.direct_access;
  9362. fixed.escaped = def.escaped;
  9363. name.fixed = fixed;
  9364. def.references.forEach(function(ref) {
  9365. if (!ref.fixed) return;
  9366. var assigns = ref.fixed.assigns;
  9367. if (!assigns) return;
  9368. if (assigns[0] !== defn) return;
  9369. if (assigns.length > 1 || ref.fixed.to_binary || ref.fixed.to_prefix) {
  9370. assigns[0] = assign;
  9371. } else {
  9372. ref.fixed = fixed;
  9373. if (def.fixed === ref.fixed) def.fixed = fixed;
  9374. }
  9375. });
  9376. def.references.push(name);
  9377. }
  9378. def.assignments++;
  9379. def.eliminated++;
  9380. def.single_use = false;
  9381. return a;
  9382. }, []);
  9383. if (assignments.length == 0) return null;
  9384. return make_sequence(this, assignments);
  9385. });
  9386. function is_safe_lexical(def) {
  9387. return def.name != "arguments" && def.orig.length < (def.orig[0] instanceof AST_SymbolLambda ? 3 : 2);
  9388. }
  9389. function may_overlap(compressor, def) {
  9390. if (compressor.exposed(def)) return true;
  9391. var scope = def.scope.resolve();
  9392. for (var s = def.scope; s !== scope;) {
  9393. s = s.parent_scope;
  9394. if (s.var_names().has(def.name)) return true;
  9395. }
  9396. }
  9397. function to_var(stat) {
  9398. return make_node(AST_Var, stat, {
  9399. definitions: stat.definitions.map(function(defn) {
  9400. return make_node(AST_VarDef, defn, {
  9401. name: defn.name.convert_symbol(AST_SymbolVar, function(name, node) {
  9402. var def = name.definition();
  9403. def.orig[def.orig.indexOf(node)] = name;
  9404. var scope = def.scope.resolve();
  9405. if (def.scope === scope) return;
  9406. def.scope = scope;
  9407. scope.variables.set(def.name, def);
  9408. scope.enclosed.push(def);
  9409. scope.var_names().set(def.name, true);
  9410. }),
  9411. value: defn.value,
  9412. });
  9413. })
  9414. });
  9415. }
  9416. function can_varify(compressor, sym) {
  9417. if (!sym.fixed_value()) return false;
  9418. var def = sym.definition();
  9419. return is_safe_lexical(def) && same_scope(def) && !may_overlap(compressor, def);
  9420. }
  9421. function varify(self, compressor) {
  9422. return compressor.option("varify") && all(self.definitions, function(defn) {
  9423. return !defn.name.match_symbol(function(node) {
  9424. if (node instanceof AST_SymbolDeclaration) return !can_varify(compressor, node);
  9425. }, true);
  9426. }) ? to_var(self) : self;
  9427. }
  9428. OPT(AST_Const, varify);
  9429. OPT(AST_Let, varify);
  9430. function trim_optional_chain(node, compressor) {
  9431. if (!compressor.option("optional_chains")) return;
  9432. if (node.terminal) do {
  9433. var expr = node.expression;
  9434. if (node.optional) {
  9435. var ev = fuzzy_eval(compressor, expr, true);
  9436. if (ev == null) return make_node(AST_UnaryPrefix, node, {
  9437. operator: "void",
  9438. expression: expr,
  9439. }).optimize(compressor);
  9440. if (!(ev instanceof AST_Node)) node.optional = false;
  9441. }
  9442. node = expr;
  9443. } while ((node.TYPE == "Call" || node instanceof AST_PropAccess) && !node.terminal);
  9444. }
  9445. function lift_sequence_in_expression(node, compressor) {
  9446. var exp = node.expression;
  9447. if (!(exp instanceof AST_Sequence)) return node;
  9448. var x = exp.expressions.slice();
  9449. var e = node.clone();
  9450. e.expression = x.pop();
  9451. x.push(e);
  9452. return make_sequence(node, x);
  9453. }
  9454. function drop_unused_call_args(call, compressor, fns_with_marked_args) {
  9455. var exp = call.expression;
  9456. var fn = exp instanceof AST_SymbolRef ? exp.fixed_value() : exp;
  9457. if (!(fn instanceof AST_Lambda)) return;
  9458. if (fn.uses_arguments) return;
  9459. if (fn.pinned()) return;
  9460. if (fns_with_marked_args && fns_with_marked_args.indexOf(fn) < 0) return;
  9461. var args = call.args;
  9462. if (!all(args, function(arg) {
  9463. return !(arg instanceof AST_Spread);
  9464. })) return;
  9465. var argnames = fn.argnames;
  9466. var is_iife = fn === exp && !fn.name;
  9467. if (fn.rest) {
  9468. if (!(is_iife && compressor.option("rests"))) return;
  9469. var insert = argnames.length;
  9470. args = args.slice(0, insert);
  9471. while (args.length < insert) args.push(make_node(AST_Undefined, call).optimize(compressor));
  9472. args.push(make_node(AST_Array, call, { elements: call.args.slice(insert) }));
  9473. argnames = argnames.concat(fn.rest);
  9474. fn.rest = null;
  9475. } else {
  9476. args = args.slice();
  9477. argnames = argnames.slice();
  9478. }
  9479. var pos = 0, last = 0;
  9480. var drop_defaults = is_iife && compressor.option("default_values");
  9481. var drop_fargs = is_iife && compressor.drop_fargs(fn, call) ? function(argname, arg) {
  9482. if (!argname) return true;
  9483. if (argname instanceof AST_DestructuredArray) {
  9484. return argname.elements.length == 0 && !argname.rest && arg instanceof AST_Array;
  9485. }
  9486. if (argname instanceof AST_DestructuredObject) {
  9487. return argname.properties.length == 0 && !argname.rest && arg && !arg.may_throw_on_access(compressor);
  9488. }
  9489. return argname.unused;
  9490. } : return_false;
  9491. var side_effects = [];
  9492. for (var i = 0; i < args.length; i++) {
  9493. var argname = argnames[i];
  9494. if (drop_defaults && argname instanceof AST_DefaultValue && args[i].is_defined(compressor)) {
  9495. argnames[i] = argname = argname.name;
  9496. }
  9497. if (!argname || argname.unused !== undefined) {
  9498. var node = args[i].drop_side_effect_free(compressor);
  9499. if (drop_fargs(argname)) {
  9500. if (argname) argnames.splice(i, 1);
  9501. args.splice(i, 1);
  9502. if (node) side_effects.push(node);
  9503. i--;
  9504. continue;
  9505. } else if (node) {
  9506. side_effects.push(node);
  9507. args[pos++] = make_sequence(call, side_effects);
  9508. side_effects = [];
  9509. } else if (argname) {
  9510. if (side_effects.length) {
  9511. args[pos++] = make_sequence(call, side_effects);
  9512. side_effects = [];
  9513. } else {
  9514. args[pos++] = make_node(AST_Number, args[i], { value: 0 });
  9515. continue;
  9516. }
  9517. }
  9518. } else if (drop_fargs(argname, args[i])) {
  9519. var node = args[i].drop_side_effect_free(compressor);
  9520. argnames.splice(i, 1);
  9521. args.splice(i, 1);
  9522. if (node) side_effects.push(node);
  9523. i--;
  9524. continue;
  9525. } else {
  9526. side_effects.push(args[i]);
  9527. args[pos++] = make_sequence(call, side_effects);
  9528. side_effects = [];
  9529. }
  9530. last = pos;
  9531. }
  9532. for (; i < argnames.length; i++) {
  9533. if (drop_fargs(argnames[i])) argnames.splice(i--, 1);
  9534. }
  9535. fn.argnames = argnames;
  9536. args.length = last;
  9537. call.args = args;
  9538. if (!side_effects.length) return;
  9539. var arg = make_sequence(call, side_effects);
  9540. args.push(args.length < argnames.length ? make_node(AST_UnaryPrefix, call, {
  9541. operator: "void",
  9542. expression: arg,
  9543. }) : arg);
  9544. }
  9545. function avoid_await_yield(parent_scope) {
  9546. var avoid = [];
  9547. if (is_async(parent_scope)) avoid.push("await");
  9548. if (is_generator(parent_scope)) avoid.push("yield");
  9549. return avoid.length && makePredicate(avoid);
  9550. }
  9551. function safe_from_await_yield(fn, avoid) {
  9552. if (!avoid) return true;
  9553. var safe = true;
  9554. var tw = new TreeWalker(function(node) {
  9555. if (!safe) return true;
  9556. if (node instanceof AST_Scope) {
  9557. if (node === fn) return;
  9558. if (is_arrow(node)) {
  9559. for (var i = 0; safe && i < node.argnames.length; i++) node.argnames[i].walk(tw);
  9560. } else if (node instanceof AST_LambdaDefinition && avoid[node.name.name]) {
  9561. safe = false;
  9562. }
  9563. return true;
  9564. }
  9565. if (node instanceof AST_Symbol && avoid[node.name] && node !== fn.name) safe = false;
  9566. });
  9567. fn.walk(tw);
  9568. return safe;
  9569. }
  9570. OPT(AST_Call, function(self, compressor) {
  9571. var exp = self.expression;
  9572. var terminated = trim_optional_chain(self, compressor);
  9573. if (terminated) return terminated;
  9574. if (compressor.option("sequences")) {
  9575. if (exp instanceof AST_PropAccess) {
  9576. var seq = lift_sequence_in_expression(exp, compressor);
  9577. if (seq !== exp) {
  9578. var call = self.clone();
  9579. call.expression = seq.expressions.pop();
  9580. seq.expressions.push(call);
  9581. return seq.optimize(compressor);
  9582. }
  9583. } else if (!needs_unbinding(compressor, exp.tail_node())) {
  9584. var seq = lift_sequence_in_expression(self, compressor);
  9585. if (seq !== self) return seq.optimize(compressor);
  9586. }
  9587. }
  9588. if (compressor.option("unused")) drop_unused_call_args(self, compressor);
  9589. if (compressor.option("unsafe")) {
  9590. if (is_undeclared_ref(exp)) switch (exp.name) {
  9591. case "Array":
  9592. // Array(n) ---> [ , , ... , ]
  9593. if (self.args.length == 1) {
  9594. var first = self.args[0];
  9595. if (first instanceof AST_Number) try {
  9596. var length = first.value;
  9597. if (length > 6) break;
  9598. var elements = Array(length);
  9599. for (var i = 0; i < length; i++) elements[i] = make_node(AST_Hole, self);
  9600. return make_node(AST_Array, self, { elements: elements });
  9601. } catch (ex) {
  9602. AST_Node.warn("Invalid array length: {length} [{file}:{line},{col}]", {
  9603. length: length,
  9604. file: self.start.file,
  9605. line: self.start.line,
  9606. col: self.start.col,
  9607. });
  9608. break;
  9609. }
  9610. if (!first.is_boolean(compressor) && !first.is_string(compressor)) break;
  9611. }
  9612. // Array(...) ---> [ ... ]
  9613. return make_node(AST_Array, self, { elements: self.args });
  9614. case "Object":
  9615. // Object() ---> {}
  9616. if (self.args.length == 0) return make_node(AST_Object, self, { properties: [] });
  9617. break;
  9618. case "String":
  9619. // String() ---> ""
  9620. if (self.args.length == 0) return make_node(AST_String, self, { value: "" });
  9621. // String(x) ---> "" + x
  9622. if (self.args.length == 1) return make_node(AST_Binary, self, {
  9623. operator: "+",
  9624. left: make_node(AST_String, self, { value: "" }),
  9625. right: self.args[0],
  9626. }).optimize(compressor);
  9627. break;
  9628. case "Number":
  9629. // Number() ---> 0
  9630. if (self.args.length == 0) return make_node(AST_Number, self, { value: 0 });
  9631. // Number(x) ---> +("" + x)
  9632. if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
  9633. operator: "+",
  9634. expression: make_node(AST_Binary, self, {
  9635. operator: "+",
  9636. left: make_node(AST_String, self, { value: "" }),
  9637. right: self.args[0],
  9638. }),
  9639. }).optimize(compressor);
  9640. break;
  9641. case "Boolean":
  9642. // Boolean() ---> false
  9643. if (self.args.length == 0) return make_node(AST_False, self).optimize(compressor);
  9644. // Boolean(x) ---> !!x
  9645. if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
  9646. operator: "!",
  9647. expression: make_node(AST_UnaryPrefix, self, {
  9648. operator: "!",
  9649. expression: self.args[0],
  9650. }),
  9651. }).optimize(compressor);
  9652. break;
  9653. case "RegExp":
  9654. // attempt to convert RegExp(...) to literal
  9655. var params = [];
  9656. if (all(self.args, function(arg) {
  9657. var value = arg.evaluate(compressor);
  9658. params.unshift(value);
  9659. return arg !== value;
  9660. })) try {
  9661. return best_of(compressor, self, make_node(AST_RegExp, self, {
  9662. value: RegExp.apply(RegExp, params),
  9663. }));
  9664. } catch (ex) {
  9665. AST_Node.warn("Error converting {expr} [{file}:{line},{col}]", {
  9666. expr: self,
  9667. file: self.start.file,
  9668. line: self.start.line,
  9669. col: self.start.col,
  9670. });
  9671. }
  9672. break;
  9673. } else if (exp instanceof AST_Dot) switch (exp.property) {
  9674. case "toString":
  9675. // x.toString() ---> "" + x
  9676. var expr = exp.expression;
  9677. if (self.args.length == 0 && !(expr.may_throw_on_access(compressor) || expr instanceof AST_Super)) {
  9678. return make_node(AST_Binary, self, {
  9679. operator: "+",
  9680. left: make_node(AST_String, self, { value: "" }),
  9681. right: expr,
  9682. }).optimize(compressor);
  9683. }
  9684. break;
  9685. case "join":
  9686. if (exp.expression instanceof AST_Array && self.args.length < 2) EXIT: {
  9687. var separator = self.args[0];
  9688. // [].join() ---> ""
  9689. // [].join(x) ---> (x, "")
  9690. if (exp.expression.elements.length == 0 && !(separator instanceof AST_Spread)) {
  9691. return separator ? make_sequence(self, [
  9692. separator,
  9693. make_node(AST_String, self, { value: "" }),
  9694. ]).optimize(compressor) : make_node(AST_String, self, { value: "" });
  9695. }
  9696. if (separator) {
  9697. separator = separator.evaluate(compressor);
  9698. if (separator instanceof AST_Node) break EXIT; // not a constant
  9699. }
  9700. var elements = [];
  9701. var consts = [];
  9702. for (var i = 0; i < exp.expression.elements.length; i++) {
  9703. var el = exp.expression.elements[i];
  9704. var value = el.evaluate(compressor);
  9705. if (value !== el) {
  9706. consts.push(value);
  9707. } else if (el instanceof AST_Spread) {
  9708. break EXIT;
  9709. } else {
  9710. if (consts.length > 0) {
  9711. elements.push(make_node(AST_String, self, {
  9712. value: consts.join(separator),
  9713. }));
  9714. consts.length = 0;
  9715. }
  9716. elements.push(el);
  9717. }
  9718. }
  9719. if (consts.length > 0) elements.push(make_node(AST_String, self, {
  9720. value: consts.join(separator),
  9721. }));
  9722. // [ x ].join() ---> "" + x
  9723. // [ x ].join(".") ---> "" + x
  9724. // [ 1, 2, 3 ].join() ---> "1,2,3"
  9725. // [ 1, 2, 3 ].join(".") ---> "1.2.3"
  9726. if (elements.length == 1) {
  9727. if (elements[0].is_string(compressor)) return elements[0];
  9728. return make_node(AST_Binary, elements[0], {
  9729. operator: "+",
  9730. left: make_node(AST_String, self, { value: "" }),
  9731. right: elements[0],
  9732. });
  9733. }
  9734. // [ 1, 2, a, 3 ].join("") ---> "12" + a + "3"
  9735. if (separator == "") {
  9736. var first;
  9737. if (elements[0].is_string(compressor) || elements[1].is_string(compressor)) {
  9738. first = elements.shift();
  9739. } else {
  9740. first = make_node(AST_String, self, { value: "" });
  9741. }
  9742. return elements.reduce(function(prev, el) {
  9743. return make_node(AST_Binary, el, {
  9744. operator: "+",
  9745. left: prev,
  9746. right: el,
  9747. });
  9748. }, first).optimize(compressor);
  9749. }
  9750. // [ x, "foo", "bar", y ].join() ---> [ x, "foo,bar", y ].join()
  9751. // [ x, "foo", "bar", y ].join("-") ---> [ x, "foo-bar", y ].join("-")
  9752. // need this awkward cloning to not affect original element
  9753. // best_of will decide which one to get through.
  9754. var node = self.clone();
  9755. node.expression = node.expression.clone();
  9756. node.expression.expression = node.expression.expression.clone();
  9757. node.expression.expression.elements = elements;
  9758. return best_of(compressor, self, node);
  9759. }
  9760. break;
  9761. case "charAt":
  9762. if (self.args.length < 2) {
  9763. var node = make_node(AST_Binary, self, {
  9764. operator: "||",
  9765. left: make_node(AST_Sub, self, {
  9766. expression: exp.expression,
  9767. property: self.args.length ? make_node(AST_Binary, self.args[0], {
  9768. operator: "|",
  9769. left: make_node(AST_Number, self, { value: 0 }),
  9770. right: self.args[0],
  9771. }) : make_node(AST_Number, self, { value: 0 }),
  9772. }).optimize(compressor),
  9773. right: make_node(AST_String, self, { value: "" }),
  9774. });
  9775. node.is_string = return_true;
  9776. return node.optimize(compressor);
  9777. }
  9778. break;
  9779. case "apply":
  9780. if (self.args.length == 2 && self.args[1] instanceof AST_Array) {
  9781. var args = self.args[1].elements.slice();
  9782. args.unshift(self.args[0]);
  9783. return make_node(AST_Call, self, {
  9784. expression: make_node(AST_Dot, exp, {
  9785. expression: exp.expression,
  9786. property: "call",
  9787. }),
  9788. args: args,
  9789. }).optimize(compressor);
  9790. }
  9791. break;
  9792. case "call":
  9793. var func = exp.expression;
  9794. if (func instanceof AST_SymbolRef) {
  9795. func = func.fixed_value();
  9796. }
  9797. if (func instanceof AST_Lambda && !func.contains_this()) {
  9798. return (self.args.length ? make_sequence(this, [
  9799. self.args[0],
  9800. make_node(AST_Call, self, {
  9801. expression: exp.expression,
  9802. args: self.args.slice(1),
  9803. }),
  9804. ]) : make_node(AST_Call, self, {
  9805. expression: exp.expression,
  9806. args: [],
  9807. })).optimize(compressor);
  9808. }
  9809. break;
  9810. }
  9811. }
  9812. if (compressor.option("unsafe_Function")
  9813. && is_undeclared_ref(exp)
  9814. && exp.name == "Function") {
  9815. // new Function() ---> function(){}
  9816. if (self.args.length == 0) return make_node(AST_Function, self, {
  9817. argnames: [],
  9818. body: []
  9819. }).init_vars(exp.scope);
  9820. if (all(self.args, function(x) {
  9821. return x instanceof AST_String;
  9822. })) {
  9823. // quite a corner-case, but we can handle it:
  9824. // https://github.com/mishoo/UglifyJS/issues/203
  9825. // if the code argument is a constant, then we can minify it.
  9826. try {
  9827. var code = "n(function(" + self.args.slice(0, -1).map(function(arg) {
  9828. return arg.value;
  9829. }).join() + "){" + self.args[self.args.length - 1].value + "})";
  9830. var ast = parse(code);
  9831. var mangle = { ie: compressor.option("ie") };
  9832. ast.figure_out_scope(mangle);
  9833. var comp = new Compressor(compressor.options);
  9834. ast = ast.transform(comp);
  9835. ast.figure_out_scope(mangle);
  9836. ast.compute_char_frequency(mangle);
  9837. ast.mangle_names(mangle);
  9838. var fun;
  9839. ast.walk(new TreeWalker(function(node) {
  9840. if (fun) return true;
  9841. if (node instanceof AST_Lambda) {
  9842. fun = node;
  9843. return true;
  9844. }
  9845. }));
  9846. var code = OutputStream();
  9847. AST_BlockStatement.prototype._codegen.call(fun, code);
  9848. self.args = [
  9849. make_node(AST_String, self, {
  9850. value: fun.argnames.map(function(arg) {
  9851. return arg.print_to_string();
  9852. }).join(),
  9853. }),
  9854. make_node(AST_String, self.args[self.args.length - 1], {
  9855. value: code.get().replace(/^\{|\}$/g, "")
  9856. })
  9857. ];
  9858. return self;
  9859. } catch (ex) {
  9860. if (ex instanceof JS_Parse_Error) {
  9861. AST_Node.warn("Error parsing code passed to new Function [{file}:{line},{col}]", self.args[self.args.length - 1].start);
  9862. AST_Node.warn(ex.toString());
  9863. } else {
  9864. throw ex;
  9865. }
  9866. }
  9867. }
  9868. }
  9869. var fn = exp instanceof AST_SymbolRef ? exp.fixed_value() : exp;
  9870. var parent = compressor.parent(), current = compressor.self();
  9871. var is_func = fn instanceof AST_Lambda
  9872. && (!is_async(fn) || compressor.option("awaits") && parent instanceof AST_Await)
  9873. && (!is_generator(fn) || compressor.option("yields") && current instanceof AST_Yield && current.nested);
  9874. var stat = is_func && fn.first_statement();
  9875. var has_default = 0, has_destructured = false;
  9876. var has_spread = !all(self.args, function(arg) {
  9877. return !(arg instanceof AST_Spread);
  9878. });
  9879. var can_drop = is_func && all(fn.argnames, function(argname, index) {
  9880. if (has_default == 1 && self.args[index] instanceof AST_Spread) has_default = 2;
  9881. if (argname instanceof AST_DefaultValue) {
  9882. if (!has_default) has_default = 1;
  9883. var arg = has_default == 1 && self.args[index];
  9884. if (!is_undefined(arg)) has_default = 2;
  9885. if (has_arg_refs(fn, argname.value)) return false;
  9886. argname = argname.name;
  9887. }
  9888. if (argname instanceof AST_Destructured) {
  9889. has_destructured = true;
  9890. if (has_arg_refs(fn, argname)) return false;
  9891. }
  9892. return true;
  9893. }) && !(fn.rest instanceof AST_Destructured && has_arg_refs(fn, fn.rest));
  9894. var can_inline = can_drop && compressor.option("inline") && !self.is_expr_pure(compressor);
  9895. if (can_inline && stat instanceof AST_Return) {
  9896. var value = stat.value;
  9897. if (exp === fn
  9898. && !fn.name
  9899. && (!value || value.is_constant_expression())
  9900. && safe_from_await_yield(fn, avoid_await_yield(compressor.find_parent(AST_Scope)))) {
  9901. return make_sequence(self, convert_args(value)).optimize(compressor);
  9902. }
  9903. }
  9904. if (is_func && !fn.contains_this()) {
  9905. var def, value, var_assigned = false;
  9906. if (can_inline
  9907. && !fn.uses_arguments
  9908. && !fn.pinned()
  9909. && !(fn.name && fn instanceof AST_LambdaExpression)
  9910. && (exp === fn || !recursive_ref(compressor, def = exp.definition(), fn)
  9911. && fn.is_constant_expression(find_scope(compressor)))
  9912. && (value = can_flatten_body(stat))) {
  9913. var replacing = exp === fn || def.single_use && def.references.length - def.replaced == 1;
  9914. if (can_substitute_directly()) {
  9915. var args = self.args.slice();
  9916. var refs = [];
  9917. var retValue = value.clone(true).transform(new TreeTransformer(function(node) {
  9918. if (node instanceof AST_SymbolRef) {
  9919. var def = node.definition();
  9920. if (fn.variables.get(node.name) !== def) {
  9921. refs.push(node);
  9922. return node;
  9923. }
  9924. var index = resolve_index(def);
  9925. var arg = args[index];
  9926. if (!arg) return make_node(AST_Undefined, self);
  9927. args[index] = null;
  9928. var parent = this.parent();
  9929. return parent ? maintain_this_binding(compressor, parent, node, arg) : arg;
  9930. }
  9931. }));
  9932. var save_inlined = fn.inlined;
  9933. if (exp !== fn) fn.inlined = true;
  9934. var exprs = [];
  9935. args.forEach(function(arg) {
  9936. if (!arg) return;
  9937. arg = arg.clone(true);
  9938. arg.walk(new TreeWalker(function(node) {
  9939. if (node instanceof AST_SymbolRef) refs.push(node);
  9940. }));
  9941. exprs.push(arg);
  9942. }, []);
  9943. exprs.push(retValue);
  9944. var node = make_sequence(self, exprs).optimize(compressor);
  9945. fn.inlined = save_inlined;
  9946. node = maintain_this_binding(compressor, parent, current, node);
  9947. if (replacing || best_of_expression(node, self) === node) {
  9948. refs.forEach(function(ref) {
  9949. ref.scope = exp === fn ? fn.parent_scope : exp.scope;
  9950. ref.reference();
  9951. var def = ref.definition();
  9952. if (replacing) def.replaced++;
  9953. def.single_use = false;
  9954. });
  9955. return node;
  9956. } else if (!node.has_side_effects(compressor)) {
  9957. self.drop_side_effect_free = function(compressor, first_in_statement) {
  9958. var self = this;
  9959. var exprs = self.args.slice();
  9960. exprs.unshift(self.expression);
  9961. return make_sequence(self, exprs).drop_side_effect_free(compressor, first_in_statement);
  9962. };
  9963. }
  9964. }
  9965. var arg_used, insert, in_loop, scope;
  9966. if (replacing && can_inject_symbols()) {
  9967. fn._squeezed = true;
  9968. if (exp !== fn) fn.parent_scope = exp.scope;
  9969. var node = make_sequence(self, flatten_fn()).optimize(compressor);
  9970. return maintain_this_binding(compressor, parent, current, node);
  9971. }
  9972. }
  9973. if (compressor.option("side_effects")
  9974. && can_drop
  9975. && all(fn.body, is_empty)
  9976. && (fn === exp ? fn_name_unused(fn, compressor) : !has_default && !has_destructured && !fn.rest)
  9977. && !(is_arrow(fn) && fn.value)
  9978. && safe_from_await_yield(fn, avoid_await_yield(compressor.find_parent(AST_Scope)))) {
  9979. return make_sequence(self, convert_args()).optimize(compressor);
  9980. }
  9981. }
  9982. if (compressor.option("drop_console")) {
  9983. if (exp instanceof AST_PropAccess) {
  9984. var name = exp.expression;
  9985. while (name.expression) {
  9986. name = name.expression;
  9987. }
  9988. if (is_undeclared_ref(name) && name.name == "console") {
  9989. return make_node(AST_Undefined, self).optimize(compressor);
  9990. }
  9991. }
  9992. }
  9993. if (compressor.option("negate_iife") && parent instanceof AST_SimpleStatement && is_iife_call(current)) {
  9994. return self.negate(compressor, true);
  9995. }
  9996. return try_evaluate(compressor, self);
  9997. function make_void_lhs(orig) {
  9998. return make_node(AST_Dot, orig, {
  9999. expression: make_node(AST_Array, orig, { elements: [] }),
  10000. property: "e",
  10001. });
  10002. }
  10003. function convert_args(value) {
  10004. var args = self.args.slice();
  10005. var destructured = has_default > 1 || has_destructured || fn.rest;
  10006. if (destructured || has_spread) args = [ make_node(AST_Array, self, { elements: args }) ];
  10007. if (destructured) {
  10008. var tt = new TreeTransformer(function(node, descend) {
  10009. if (node instanceof AST_DefaultValue) return make_node(AST_DefaultValue, node, {
  10010. name: node.name.transform(tt) || make_void_lhs(node),
  10011. value: node.value,
  10012. });
  10013. if (node instanceof AST_DestructuredArray) {
  10014. var elements = [];
  10015. node.elements.forEach(function(node, index) {
  10016. node = node.transform(tt);
  10017. if (node) elements[index] = node;
  10018. });
  10019. fill_holes(node, elements);
  10020. return make_node(AST_DestructuredArray, node, { elements: elements });
  10021. }
  10022. if (node instanceof AST_DestructuredObject) {
  10023. var properties = [], side_effects = [];
  10024. node.properties.forEach(function(prop) {
  10025. var key = prop.key;
  10026. var value = prop.value.transform(tt);
  10027. if (value) {
  10028. if (side_effects.length) {
  10029. if (!(key instanceof AST_Node)) key = make_node_from_constant(key, prop);
  10030. side_effects.push(key);
  10031. key = make_sequence(node, side_effects);
  10032. side_effects = [];
  10033. }
  10034. properties.push(make_node(AST_DestructuredKeyVal, prop, {
  10035. key: key,
  10036. value: value,
  10037. }));
  10038. } else if (key instanceof AST_Node) {
  10039. side_effects.push(key);
  10040. }
  10041. });
  10042. if (side_effects.length) properties.push(make_node(AST_DestructuredKeyVal, node, {
  10043. key: make_sequence(node, side_effects),
  10044. value: make_void_lhs(node),
  10045. }));
  10046. return make_node(AST_DestructuredObject, node, { properties: properties });
  10047. }
  10048. if (node instanceof AST_SymbolFunarg) return null;
  10049. });
  10050. var lhs = [];
  10051. fn.argnames.forEach(function(argname, index) {
  10052. argname = argname.transform(tt);
  10053. if (argname) lhs[index] = argname;
  10054. });
  10055. var rest = fn.rest && fn.rest.transform(tt);
  10056. if (rest) lhs.length = fn.argnames.length;
  10057. fill_holes(fn, lhs);
  10058. args[0] = make_node(AST_Assign, self, {
  10059. operator: "=",
  10060. left: make_node(AST_DestructuredArray, fn, {
  10061. elements: lhs,
  10062. rest: rest,
  10063. }),
  10064. right: args[0],
  10065. });
  10066. } else fn.argnames.forEach(function(argname) {
  10067. if (argname instanceof AST_DefaultValue) args.push(argname.value);
  10068. });
  10069. args.push(value || make_node(AST_Undefined, self));
  10070. return args;
  10071. }
  10072. function noop_value() {
  10073. return self.call_only ? make_node(AST_Number, self, { value: 0 }) : make_node(AST_Undefined, self);
  10074. }
  10075. function return_value(stat) {
  10076. if (!stat) return noop_value();
  10077. if (stat instanceof AST_Return) return stat.value || noop_value();
  10078. if (stat instanceof AST_SimpleStatement) {
  10079. return self.call_only ? stat.body : make_node(AST_UnaryPrefix, stat, {
  10080. operator: "void",
  10081. expression: stat.body,
  10082. });
  10083. }
  10084. }
  10085. function can_flatten_body(stat) {
  10086. var len = fn.body.length;
  10087. if (len < 2) {
  10088. stat = return_value(stat);
  10089. if (stat) return stat;
  10090. }
  10091. if (compressor.option("inline") < 3) return false;
  10092. stat = null;
  10093. for (var i = 0; i < len; i++) {
  10094. var line = fn.body[i];
  10095. if (line instanceof AST_Var) {
  10096. if (var_assigned) {
  10097. if (!stat) continue;
  10098. if (!(stat instanceof AST_SimpleStatement)) return false;
  10099. if (!declarations_only(line)) stat = null;
  10100. } else if (!declarations_only(line)) {
  10101. if (stat && !(stat instanceof AST_SimpleStatement)) return false;
  10102. stat = null;
  10103. var_assigned = true;
  10104. }
  10105. } else if (line instanceof AST_AsyncDefun
  10106. || line instanceof AST_Defun
  10107. || line instanceof AST_EmptyStatement) {
  10108. continue;
  10109. } else if (stat) {
  10110. return false;
  10111. } else {
  10112. stat = line;
  10113. }
  10114. }
  10115. return return_value(stat);
  10116. }
  10117. function resolve_index(def) {
  10118. for (var i = fn.argnames.length; --i >= 0;) {
  10119. if (fn.argnames[i].definition() === def) return i;
  10120. }
  10121. }
  10122. function can_substitute_directly() {
  10123. if (has_default || has_destructured || has_spread || var_assigned || fn.rest) return;
  10124. if (compressor.option("inline") < 2 && fn.argnames.length) return;
  10125. if (!fn.variables.all(function(def) {
  10126. return def.references.length - def.replaced < 2 && def.orig[0] instanceof AST_SymbolFunarg;
  10127. })) return;
  10128. var scope = compressor.find_parent(AST_Scope);
  10129. var abort = false;
  10130. var avoid = avoid_await_yield(scope);
  10131. var begin;
  10132. var in_order = [];
  10133. var side_effects = false;
  10134. value.walk(new TreeWalker(function(node, descend) {
  10135. if (abort) return true;
  10136. if (node instanceof AST_Binary && lazy_op[node.operator]
  10137. || node instanceof AST_Conditional) {
  10138. in_order = null;
  10139. return;
  10140. }
  10141. if (node instanceof AST_Scope) return abort = true;
  10142. if (avoid && node instanceof AST_Symbol && avoid[node.name]) return abort = true;
  10143. if (node instanceof AST_SymbolRef) {
  10144. var def = node.definition();
  10145. if (fn.variables.get(node.name) !== def) {
  10146. in_order = null;
  10147. return;
  10148. }
  10149. if (def.init instanceof AST_LambdaDefinition) return abort = true;
  10150. if (is_lhs(node, this.parent())) return abort = true;
  10151. var index = resolve_index(def);
  10152. if (!(begin < index)) begin = index;
  10153. if (!in_order) return;
  10154. if (side_effects) {
  10155. in_order = null;
  10156. } else {
  10157. in_order.push(fn.argnames[index]);
  10158. }
  10159. return;
  10160. }
  10161. if (node.has_side_effects(compressor)) {
  10162. descend();
  10163. side_effects = true;
  10164. return true;
  10165. }
  10166. }));
  10167. if (abort) return;
  10168. var end = self.args.length;
  10169. if (in_order && fn.argnames.length >= end) {
  10170. end = fn.argnames.length;
  10171. while (end-- > begin && fn.argnames[end] === in_order.pop());
  10172. end++;
  10173. }
  10174. return end <= begin || all(self.args.slice(begin, end), side_effects && !in_order ? function(funarg) {
  10175. return funarg.is_constant_expression(scope);
  10176. } : function(funarg) {
  10177. return !funarg.has_side_effects(compressor);
  10178. });
  10179. }
  10180. function var_exists(defined, name) {
  10181. return defined.has(name) || identifier_atom[name] || scope.var_names().has(name);
  10182. }
  10183. function can_inject_args(defined, safe_to_inject) {
  10184. var abort = false;
  10185. fn.each_argname(function(arg) {
  10186. if (abort) return;
  10187. if (arg.unused) return;
  10188. if (!safe_to_inject || var_exists(defined, arg.name)) return abort = true;
  10189. arg_used.set(arg.name, true);
  10190. if (in_loop) in_loop.push(arg.definition());
  10191. });
  10192. return !abort;
  10193. }
  10194. function can_inject_vars(defined, safe_to_inject) {
  10195. for (var i = 0; i < fn.body.length; i++) {
  10196. var stat = fn.body[i];
  10197. if (stat instanceof AST_LambdaDefinition) {
  10198. var name = stat.name;
  10199. if (!safe_to_inject) return false;
  10200. if (arg_used.has(name.name)) return false;
  10201. if (var_exists(defined, name.name)) return false;
  10202. if (!all(stat.enclosed, function(def) {
  10203. return def.scope === scope || def.scope === stat || !defined.has(def.name);
  10204. })) return false;
  10205. if (in_loop) in_loop.push(name.definition());
  10206. continue;
  10207. }
  10208. if (!(stat instanceof AST_Var)) continue;
  10209. if (!safe_to_inject) return false;
  10210. for (var j = stat.definitions.length; --j >= 0;) {
  10211. var name = stat.definitions[j].name;
  10212. if (var_exists(defined, name.name)) return false;
  10213. if (in_loop) in_loop.push(name.definition());
  10214. }
  10215. }
  10216. return true;
  10217. }
  10218. function can_inject_symbols() {
  10219. var defined = new Dictionary();
  10220. var level = 0, child;
  10221. scope = current;
  10222. do {
  10223. if (scope.variables) scope.variables.each(function(def) {
  10224. defined.set(def.name, true);
  10225. });
  10226. child = scope;
  10227. scope = compressor.parent(level++);
  10228. if (scope instanceof AST_DWLoop) {
  10229. in_loop = [];
  10230. } else if (scope instanceof AST_For) {
  10231. if (scope.init === child) continue;
  10232. in_loop = [];
  10233. } else if (scope instanceof AST_ForEnumeration) {
  10234. if (scope.init === child) continue;
  10235. if (scope.object === child) continue;
  10236. in_loop = [];
  10237. }
  10238. } while (!(scope instanceof AST_Scope));
  10239. insert = scope.body.indexOf(child) + 1;
  10240. if (!insert) return false;
  10241. if (!safe_from_await_yield(fn, avoid_await_yield(scope))) return false;
  10242. var safe_to_inject = (exp !== fn || fn.parent_scope.resolve() === scope) && !scope.pinned();
  10243. if (scope instanceof AST_Toplevel) {
  10244. if (compressor.toplevel.vars) {
  10245. defined.set("arguments", true);
  10246. } else {
  10247. safe_to_inject = false;
  10248. }
  10249. }
  10250. arg_used = new Dictionary();
  10251. var inline = compressor.option("inline");
  10252. if (!can_inject_args(defined, inline >= 2 && safe_to_inject)) return false;
  10253. if (!can_inject_vars(defined, inline >= 3 && safe_to_inject)) return false;
  10254. return !in_loop || in_loop.length == 0 || !is_reachable(fn, in_loop);
  10255. }
  10256. function append_var(decls, expressions, name, value) {
  10257. var def = name.definition();
  10258. if (!scope.var_names().has(name.name)) {
  10259. scope.var_names().set(name.name, true);
  10260. decls.push(make_node(AST_VarDef, name, {
  10261. name: name,
  10262. value: null,
  10263. }));
  10264. }
  10265. scope.variables.set(name.name, def);
  10266. scope.enclosed.push(def);
  10267. if (!value) return;
  10268. var sym = make_node(AST_SymbolRef, name, name);
  10269. def.assignments++;
  10270. def.references.push(sym);
  10271. expressions.push(make_node(AST_Assign, self, {
  10272. operator: "=",
  10273. left: sym,
  10274. right: value,
  10275. }));
  10276. }
  10277. function flatten_args(decls, expressions) {
  10278. var len = fn.argnames.length;
  10279. for (var i = self.args.length; --i >= len;) {
  10280. expressions.push(self.args[i]);
  10281. }
  10282. var default_args = [];
  10283. for (i = len; --i >= 0;) {
  10284. var argname = fn.argnames[i];
  10285. var name;
  10286. if (argname instanceof AST_DefaultValue) {
  10287. default_args.push(argname);
  10288. name = argname.name;
  10289. } else {
  10290. name = argname;
  10291. }
  10292. var value = self.args[i];
  10293. if (name.unused || scope.var_names().has(name.name)) {
  10294. if (value) expressions.push(value);
  10295. } else {
  10296. var symbol = make_node(AST_SymbolVar, name, name);
  10297. var def = name.definition();
  10298. def.orig.push(symbol);
  10299. def.eliminated++;
  10300. if (name.unused !== undefined) {
  10301. append_var(decls, expressions, symbol);
  10302. if (value) expressions.push(value);
  10303. } else {
  10304. if (!value && in_loop && argname === name) value = make_node(AST_Undefined, self);
  10305. append_var(decls, expressions, symbol, value);
  10306. }
  10307. }
  10308. }
  10309. decls.reverse();
  10310. expressions.reverse();
  10311. for (i = default_args.length; --i >= 0;) {
  10312. var node = default_args[i];
  10313. if (node.name.unused !== undefined) {
  10314. expressions.push(node.value);
  10315. } else {
  10316. var sym = make_node(AST_SymbolRef, node.name, node.name);
  10317. node.name.definition().references.push(sym);
  10318. expressions.push(make_node(AST_Assign, node, {
  10319. operator: "=",
  10320. left: sym,
  10321. right: node.value,
  10322. }));
  10323. }
  10324. }
  10325. }
  10326. function flatten_destructured(decls, expressions) {
  10327. expressions.push(make_node(AST_Assign, self, {
  10328. operator: "=",
  10329. left: make_node(AST_DestructuredArray, self, {
  10330. elements: fn.argnames.map(function(argname) {
  10331. if (argname.unused) return make_node(AST_Hole, argname);
  10332. return argname.convert_symbol(AST_SymbolRef, process);
  10333. }),
  10334. rest: fn.rest && fn.rest.convert_symbol(AST_SymbolRef, process),
  10335. }),
  10336. right: make_node(AST_Array, self, { elements: self.args.slice() }),
  10337. }));
  10338. function process(ref, name) {
  10339. var def = name.definition();
  10340. def.assignments++;
  10341. def.references.push(ref);
  10342. var symbol = make_node(AST_SymbolVar, name, name);
  10343. def.orig.push(symbol);
  10344. def.eliminated++;
  10345. append_var(decls, expressions, symbol);
  10346. }
  10347. }
  10348. function flatten_vars(decls, expressions) {
  10349. var args = [ insert, 0 ];
  10350. var decl_var = [], expr_fn = [], expr_var = [], expr_loop = [], exprs = [];
  10351. fn.body.filter(in_loop ? function(stat) {
  10352. if (!(stat instanceof AST_LambdaDefinition)) return true;
  10353. var name = make_node(AST_SymbolVar, stat.name, flatten_var(stat.name));
  10354. var def = name.definition();
  10355. def.fixed = false;
  10356. def.orig.push(name);
  10357. def.eliminated++;
  10358. append_var(decls, expr_fn, name, to_func_expr(stat, true));
  10359. return false;
  10360. } : function(stat) {
  10361. if (!(stat instanceof AST_LambdaDefinition)) return true;
  10362. var def = stat.name.definition();
  10363. scope.functions.set(def.name, def);
  10364. scope.variables.set(def.name, def);
  10365. scope.enclosed.push(def);
  10366. scope.var_names().set(def.name, true);
  10367. args.push(stat);
  10368. return false;
  10369. }).forEach(function(stat) {
  10370. if (!(stat instanceof AST_Var)) {
  10371. if (stat instanceof AST_SimpleStatement) exprs.push(stat.body);
  10372. return;
  10373. }
  10374. for (var j = 0; j < stat.definitions.length; j++) {
  10375. var var_def = stat.definitions[j];
  10376. var name = flatten_var(var_def.name);
  10377. var value = var_def.value;
  10378. if (value && exprs.length > 0) {
  10379. exprs.push(value);
  10380. value = make_sequence(var_def, exprs);
  10381. exprs = [];
  10382. }
  10383. append_var(decl_var, expr_var, name, value);
  10384. if (!in_loop) continue;
  10385. if (arg_used.has(name.name)) continue;
  10386. if (name.definition().orig.length == 1 && fn.functions.has(name.name)) continue;
  10387. expr_loop.push(init_ref(compressor, name));
  10388. }
  10389. });
  10390. [].push.apply(decls, decl_var);
  10391. [].push.apply(expressions, expr_loop);
  10392. [].push.apply(expressions, expr_fn);
  10393. [].push.apply(expressions, expr_var);
  10394. return args;
  10395. }
  10396. function flatten_fn() {
  10397. var decls = [];
  10398. var expressions = [];
  10399. if (has_default > 1 || has_destructured || has_spread || fn.rest) {
  10400. flatten_destructured(decls, expressions);
  10401. } else {
  10402. flatten_args(decls, expressions);
  10403. }
  10404. var args = flatten_vars(decls, expressions);
  10405. expressions.push(value);
  10406. if (decls.length) args.push(make_node(AST_Var, fn, { definitions: decls }));
  10407. [].splice.apply(scope.body, args);
  10408. fn.enclosed.forEach(function(def) {
  10409. if (scope.var_names().has(def.name)) return;
  10410. scope.enclosed.push(def);
  10411. scope.var_names().set(def.name, true);
  10412. });
  10413. return expressions;
  10414. }
  10415. });
  10416. OPT(AST_New, function(self, compressor) {
  10417. if (compressor.option("sequences")) {
  10418. var seq = lift_sequence_in_expression(self, compressor);
  10419. if (seq !== self) return seq.optimize(compressor);
  10420. }
  10421. if (compressor.option("unused")) drop_unused_call_args(self, compressor);
  10422. if (compressor.option("unsafe")) {
  10423. var exp = self.expression;
  10424. if (is_undeclared_ref(exp)) {
  10425. switch (exp.name) {
  10426. case "Object":
  10427. case "RegExp":
  10428. case "Function":
  10429. case "Error":
  10430. case "Array":
  10431. return make_node(AST_Call, self, self).transform(compressor);
  10432. }
  10433. }
  10434. }
  10435. return self;
  10436. });
  10437. // (a = b, x && a = c) ---> a = x ? c : b
  10438. // (a = b, x || a = c) ---> a = x ? b : c
  10439. function to_conditional_assignment(compressor, def, value, node) {
  10440. if (!(node instanceof AST_Binary)) return;
  10441. if (!(node.operator == "&&" || node.operator == "||")) return;
  10442. if (!(node.right instanceof AST_Assign)) return;
  10443. if (node.right.operator != "=") return;
  10444. if (!(node.right.left instanceof AST_SymbolRef)) return;
  10445. if (node.right.left.definition() !== def) return;
  10446. if (value.has_side_effects(compressor)) return;
  10447. if (!safe_from_assignment(node.left)) return;
  10448. if (!safe_from_assignment(node.right.right)) return;
  10449. def.replaced++;
  10450. return node.operator == "&&" ? make_node(AST_Conditional, node, {
  10451. condition: node.left,
  10452. consequent: node.right.right,
  10453. alternative: value
  10454. }) : make_node(AST_Conditional, node, {
  10455. condition: node.left,
  10456. consequent: value,
  10457. alternative: node.right.right
  10458. });
  10459. function safe_from_assignment(node) {
  10460. if (node.has_side_effects(compressor)) return;
  10461. var hit = false;
  10462. node.walk(new TreeWalker(function(node) {
  10463. if (hit) return true;
  10464. if (node instanceof AST_SymbolRef && node.definition() === def) return hit = true;
  10465. }));
  10466. return !hit;
  10467. }
  10468. }
  10469. OPT(AST_Sequence, function(self, compressor) {
  10470. var expressions = filter_for_side_effects();
  10471. var end = expressions.length - 1;
  10472. merge_assignments();
  10473. trim_right_for_undefined();
  10474. if (end == 0) {
  10475. self = maintain_this_binding(compressor, compressor.parent(), compressor.self(), expressions[0]);
  10476. if (!(self instanceof AST_Sequence)) self = self.optimize(compressor);
  10477. return self;
  10478. }
  10479. self.expressions = expressions;
  10480. return self;
  10481. function filter_for_side_effects() {
  10482. if (!compressor.option("side_effects")) return self.expressions;
  10483. var expressions = [];
  10484. var first = first_in_statement(compressor);
  10485. var last = self.expressions.length - 1;
  10486. self.expressions.forEach(function(expr, index) {
  10487. if (index < last) expr = expr.drop_side_effect_free(compressor, first);
  10488. if (expr) {
  10489. merge_sequence(expressions, expr);
  10490. first = false;
  10491. }
  10492. });
  10493. return expressions;
  10494. }
  10495. function trim_right_for_undefined() {
  10496. if (!compressor.option("side_effects")) return;
  10497. while (end > 0 && is_undefined(expressions[end], compressor)) end--;
  10498. if (end < expressions.length - 1) {
  10499. expressions[end] = make_node(AST_UnaryPrefix, self, {
  10500. operator : "void",
  10501. expression : expressions[end]
  10502. });
  10503. expressions.length = end + 1;
  10504. }
  10505. }
  10506. function is_simple_assign(node) {
  10507. return node instanceof AST_Assign
  10508. && node.operator == "="
  10509. && node.left instanceof AST_SymbolRef
  10510. && node.left.definition();
  10511. }
  10512. function merge_assignments() {
  10513. for (var i = 1; i < end; i++) {
  10514. var prev = expressions[i - 1];
  10515. var def = is_simple_assign(prev);
  10516. if (!def) continue;
  10517. var expr = expressions[i];
  10518. if (compressor.option("conditionals")) {
  10519. var cond = to_conditional_assignment(compressor, def, prev.right, expr);
  10520. if (cond) {
  10521. prev.right = cond;
  10522. expressions.splice(i--, 1);
  10523. end--;
  10524. continue;
  10525. }
  10526. }
  10527. if (compressor.option("dead_code")
  10528. && is_simple_assign(expr) === def
  10529. && expr.right.is_constant_expression(def.scope.resolve())) {
  10530. expressions[--i] = prev.right;
  10531. }
  10532. }
  10533. }
  10534. });
  10535. OPT(AST_UnaryPostfix, function(self, compressor) {
  10536. if (compressor.option("sequences")) {
  10537. var seq = lift_sequence_in_expression(self, compressor);
  10538. if (seq !== self) return seq.optimize(compressor);
  10539. }
  10540. return try_evaluate(compressor, self);
  10541. });
  10542. var SIGN_OPS = makePredicate("+ -");
  10543. var MULTIPLICATIVE_OPS = makePredicate("* / %");
  10544. OPT(AST_UnaryPrefix, function(self, compressor) {
  10545. var op = self.operator;
  10546. var exp = self.expression;
  10547. if (compressor.option("sequences") && can_lift()) {
  10548. var seq = lift_sequence_in_expression(self, compressor);
  10549. if (seq !== self) return seq.optimize(compressor);
  10550. }
  10551. switch (op) {
  10552. case "+":
  10553. if (!compressor.option("evaluate")) break;
  10554. if (!exp.is_number(compressor, true)) break;
  10555. var parent = compressor.parent();
  10556. if (parent instanceof AST_UnaryPrefix && parent.operator == "delete") break;
  10557. return exp;
  10558. case "-":
  10559. if (exp instanceof AST_Infinity) exp = exp.transform(compressor);
  10560. // avoids infinite recursion of numerals
  10561. if (exp instanceof AST_Number || exp instanceof AST_Infinity) return self;
  10562. break;
  10563. case "!":
  10564. if (!compressor.option("booleans")) break;
  10565. if (exp.is_truthy()) return make_sequence(self, [ exp, make_node(AST_False, self) ]).optimize(compressor);
  10566. if (compressor.in_boolean_context()) {
  10567. // !!foo ---> foo, if we're in boolean context
  10568. if (exp instanceof AST_UnaryPrefix && exp.operator == "!") return exp.expression;
  10569. if (exp instanceof AST_Binary) {
  10570. self = best_of(compressor, self, exp.negate(compressor, first_in_statement(compressor)));
  10571. }
  10572. }
  10573. break;
  10574. case "delete":
  10575. if (!compressor.option("evaluate")) break;
  10576. if (may_not_delete(exp)) break;
  10577. return make_sequence(self, [ exp, make_node(AST_True, self) ]).optimize(compressor);
  10578. case "typeof":
  10579. if (!compressor.option("booleans")) break;
  10580. if (!compressor.in_boolean_context()) break;
  10581. // typeof always returns a non-empty string, thus always truthy
  10582. AST_Node.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
  10583. var exprs = [ make_node(AST_True, self) ];
  10584. if (!(exp instanceof AST_SymbolRef && can_drop_symbol(exp, compressor))) exprs.unshift(exp);
  10585. return make_sequence(self, exprs).optimize(compressor);
  10586. case "void":
  10587. if (!compressor.option("side_effects")) break;
  10588. exp = exp.drop_side_effect_free(compressor);
  10589. if (!exp) return make_node(AST_Undefined, self).optimize(compressor);
  10590. self.expression = exp;
  10591. return self;
  10592. }
  10593. if (compressor.option("evaluate")
  10594. && exp instanceof AST_Binary
  10595. && SIGN_OPS[op]
  10596. && MULTIPLICATIVE_OPS[exp.operator]
  10597. && (exp.left.is_constant() || !exp.right.has_side_effects(compressor))) {
  10598. return make_node(AST_Binary, self, {
  10599. operator: exp.operator,
  10600. left: make_node(AST_UnaryPrefix, exp.left, {
  10601. operator: op,
  10602. expression: exp.left,
  10603. }),
  10604. right: exp.right,
  10605. });
  10606. }
  10607. return try_evaluate(compressor, self);
  10608. function may_not_delete(node) {
  10609. return node instanceof AST_Infinity
  10610. || node instanceof AST_NaN
  10611. || node instanceof AST_NewTarget
  10612. || node instanceof AST_PropAccess
  10613. || node instanceof AST_SymbolRef
  10614. || node instanceof AST_Undefined;
  10615. }
  10616. function can_lift() {
  10617. switch (op) {
  10618. case "delete":
  10619. return !may_not_delete(exp.tail_node());
  10620. case "typeof":
  10621. return !is_undeclared_ref(exp.tail_node());
  10622. default:
  10623. return true;
  10624. }
  10625. }
  10626. });
  10627. OPT(AST_Await, function(self, compressor) {
  10628. if (!compressor.option("awaits")) return self;
  10629. if (compressor.option("sequences")) {
  10630. var seq = lift_sequence_in_expression(self, compressor);
  10631. if (seq !== self) return seq.optimize(compressor);
  10632. }
  10633. if (compressor.option("side_effects")) {
  10634. var exp = self.expression;
  10635. if (exp instanceof AST_Await) return exp.optimize(compressor);
  10636. if (exp instanceof AST_UnaryPrefix && exp.expression instanceof AST_Await) return exp.optimize(compressor);
  10637. for (var level = 0, node = self, parent; parent = compressor.parent(level++); node = parent) {
  10638. if (is_arrow(parent)) {
  10639. if (parent.value === node) return exp.optimize(compressor);
  10640. } else if (parent instanceof AST_Return) {
  10641. var drop = true;
  10642. do {
  10643. node = parent;
  10644. parent = compressor.parent(level++);
  10645. if (parent instanceof AST_Try && (parent.bfinally || parent.bcatch) !== node) {
  10646. drop = false;
  10647. break;
  10648. }
  10649. } while (parent && !(parent instanceof AST_Scope));
  10650. if (drop) return exp.optimize(compressor);
  10651. } else if (parent instanceof AST_Sequence) {
  10652. if (parent.tail_node() === node) continue;
  10653. }
  10654. break;
  10655. }
  10656. }
  10657. return self;
  10658. });
  10659. OPT(AST_Yield, function(self, compressor) {
  10660. if (!compressor.option("yields")) return self;
  10661. if (compressor.option("sequences")) {
  10662. var seq = lift_sequence_in_expression(self, compressor);
  10663. if (seq !== self) return seq.optimize(compressor);
  10664. }
  10665. var exp = self.expression;
  10666. if (self.nested && exp.TYPE == "Call") {
  10667. var inlined = exp.clone().optimize(compressor);
  10668. if (inlined.TYPE != "Call") return inlined;
  10669. }
  10670. return self;
  10671. });
  10672. AST_Binary.DEFMETHOD("lift_sequences", function(compressor) {
  10673. if (this.left instanceof AST_PropAccess) {
  10674. if (!(this.left.expression instanceof AST_Sequence)) return this;
  10675. var x = this.left.expression.expressions.slice();
  10676. var e = this.clone();
  10677. e.left = e.left.clone();
  10678. e.left.expression = x.pop();
  10679. x.push(e);
  10680. return make_sequence(this, x);
  10681. }
  10682. if (this.left instanceof AST_Sequence) {
  10683. var x = this.left.expressions.slice();
  10684. var e = this.clone();
  10685. e.left = x.pop();
  10686. x.push(e);
  10687. return make_sequence(this, x);
  10688. }
  10689. if (this.right instanceof AST_Sequence) {
  10690. if (this.left.has_side_effects(compressor)) return this;
  10691. var assign = this.operator == "=" && this.left instanceof AST_SymbolRef;
  10692. var x = this.right.expressions;
  10693. var last = x.length - 1;
  10694. for (var i = 0; i < last; i++) {
  10695. if (!assign && x[i].has_side_effects(compressor)) break;
  10696. }
  10697. if (i == last) {
  10698. x = x.slice();
  10699. var e = this.clone();
  10700. e.right = x.pop();
  10701. x.push(e);
  10702. return make_sequence(this, x);
  10703. }
  10704. if (i > 0) {
  10705. var e = this.clone();
  10706. e.right = make_sequence(this.right, x.slice(i));
  10707. x = x.slice(0, i);
  10708. x.push(e);
  10709. return make_sequence(this, x);
  10710. }
  10711. }
  10712. return this;
  10713. });
  10714. var indexFns = makePredicate("indexOf lastIndexOf");
  10715. var commutativeOperators = makePredicate("== === != !== * & | ^");
  10716. function is_object(node) {
  10717. if (node instanceof AST_Assign) return node.operator == "=" && is_object(node.right);
  10718. if (node instanceof AST_Sequence) return is_object(node.tail_node());
  10719. if (node instanceof AST_SymbolRef) return is_object(node.fixed_value());
  10720. return node instanceof AST_Array
  10721. || node instanceof AST_Class
  10722. || node instanceof AST_Lambda
  10723. || node instanceof AST_New
  10724. || node instanceof AST_Object;
  10725. }
  10726. function is_primitive(compressor, node) {
  10727. if (node.is_constant()) return true;
  10728. if (node instanceof AST_Assign) return node.operator != "=" || is_primitive(compressor, node.right);
  10729. if (node instanceof AST_Binary) {
  10730. return !lazy_op[node.operator]
  10731. || is_primitive(compressor, node.left) && is_primitive(compressor, node.right);
  10732. }
  10733. if (node instanceof AST_Conditional) {
  10734. return is_primitive(compressor, node.consequent) && is_primitive(compressor, node.alternative);
  10735. }
  10736. if (node instanceof AST_Sequence) return is_primitive(compressor, node.tail_node());
  10737. if (node instanceof AST_SymbolRef) {
  10738. var fixed = node.fixed_value();
  10739. return fixed && is_primitive(compressor, fixed);
  10740. }
  10741. if (node instanceof AST_Template) return !node.tag || is_raw_tag(compressor, node.tag);
  10742. if (node instanceof AST_Unary) return true;
  10743. }
  10744. function repeatable(compressor, node) {
  10745. if (node instanceof AST_Dot) return repeatable(compressor, node.expression);
  10746. if (node instanceof AST_Sub) {
  10747. return repeatable(compressor, node.expression) && repeatable(compressor, node.property);
  10748. }
  10749. if (node instanceof AST_Symbol) return true;
  10750. return !node.has_side_effects(compressor);
  10751. }
  10752. function swap_chain(self, compressor) {
  10753. var rhs = self.right;
  10754. self.left = make_node(AST_Binary, self, {
  10755. operator: self.operator,
  10756. left: self.left,
  10757. right: rhs.left,
  10758. start: self.left.start,
  10759. end: rhs.left.end
  10760. });
  10761. self.right = rhs.right;
  10762. if (compressor) {
  10763. self.left = self.left.transform(compressor);
  10764. } else if (self.operator == rhs.left.operator) {
  10765. swap_chain(self.left);
  10766. }
  10767. }
  10768. OPT(AST_Binary, function(self, compressor) {
  10769. if (commutativeOperators[self.operator]
  10770. && self.right.is_constant()
  10771. && !self.left.is_constant()
  10772. && !(self.left instanceof AST_Binary
  10773. && PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
  10774. // if right is a constant, whatever side effects the
  10775. // left side might have could not influence the
  10776. // result. hence, force switch.
  10777. reverse();
  10778. }
  10779. if (compressor.option("sequences")) {
  10780. var seq = self.lift_sequences(compressor);
  10781. if (seq !== self) return seq.optimize(compressor);
  10782. }
  10783. if (compressor.option("assignments") && lazy_op[self.operator]) {
  10784. var assign = self.right;
  10785. // a || (a = x) ---> a = a || x
  10786. // a && (a = x) ---> a = a && x
  10787. if (self.left instanceof AST_SymbolRef
  10788. && assign instanceof AST_Assign
  10789. && assign.operator == "="
  10790. && self.left.equivalent_to(assign.left)) {
  10791. self.right = assign.right;
  10792. assign.right = self;
  10793. return assign;
  10794. }
  10795. }
  10796. if (compressor.option("comparisons")) switch (self.operator) {
  10797. case "===":
  10798. case "!==":
  10799. if (is_undefined(self.left, compressor) && self.right.is_defined(compressor)) {
  10800. AST_Node.warn("Expression always defined [{file}:{line},{col}]", self.start);
  10801. return make_sequence(self, [
  10802. self.right,
  10803. make_node(self.operator == "===" ? AST_False : AST_True, self)
  10804. ]).optimize(compressor);
  10805. }
  10806. var is_strict_comparison = true;
  10807. if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||
  10808. (self.left.is_number(compressor) && self.right.is_number(compressor)) ||
  10809. (self.left.is_boolean(compressor) && self.right.is_boolean(compressor)) ||
  10810. repeatable(compressor, self.left) && self.left.equivalent_to(self.right)) {
  10811. self.operator = self.operator.slice(0, 2);
  10812. }
  10813. // XXX: intentionally falling down to the next case
  10814. case "==":
  10815. case "!=":
  10816. // void 0 == x ---> null == x
  10817. if (!is_strict_comparison && is_undefined(self.left, compressor)) {
  10818. self.left = make_node(AST_Null, self.left);
  10819. }
  10820. // "undefined" == typeof x ---> undefined === x
  10821. else if (compressor.option("typeofs")
  10822. && self.left instanceof AST_String
  10823. && self.left.value == "undefined"
  10824. && self.right instanceof AST_UnaryPrefix
  10825. && self.right.operator == "typeof") {
  10826. var expr = self.right.expression;
  10827. if (expr instanceof AST_SymbolRef ? expr.is_declared(compressor)
  10828. : !(expr instanceof AST_PropAccess && compressor.option("ie"))) {
  10829. self.right = expr;
  10830. self.left = make_node(AST_Undefined, self.left).optimize(compressor);
  10831. if (self.operator.length == 2) self.operator += "=";
  10832. }
  10833. }
  10834. // obj !== obj ---> false
  10835. else if (self.left instanceof AST_SymbolRef
  10836. && self.right instanceof AST_SymbolRef
  10837. && self.left.definition() === self.right.definition()
  10838. && is_object(self.left)) {
  10839. return make_node(self.operator[0] == "=" ? AST_True : AST_False, self).optimize(compressor);
  10840. }
  10841. break;
  10842. case "&&":
  10843. case "||":
  10844. // void 0 !== x && null !== x ---> null != x
  10845. // void 0 === x || null === x ---> null == x
  10846. var lhs = self.left;
  10847. if (lhs.operator == self.operator) lhs = lhs.right;
  10848. var expr = lhs.right;
  10849. if (expr instanceof AST_Assign && expr.operator == "=") expr = expr.left;
  10850. if (lhs instanceof AST_Binary
  10851. && lhs.operator == (self.operator == "&&" ? "!==" : "===")
  10852. && self.right instanceof AST_Binary
  10853. && lhs.operator == self.right.operator
  10854. && (is_undefined(lhs.left, compressor) && self.right.left instanceof AST_Null
  10855. || lhs.left instanceof AST_Null && is_undefined(self.right.left, compressor))
  10856. && !expr.has_side_effects(compressor)
  10857. && expr.equivalent_to(self.right.right)) {
  10858. lhs.operator = lhs.operator.slice(0, -1);
  10859. lhs.left = make_node(AST_Null, self);
  10860. return self.left;
  10861. }
  10862. break;
  10863. }
  10864. var in_bool = false;
  10865. var parent = compressor.parent();
  10866. if (compressor.option("booleans")) {
  10867. var lhs = self.left;
  10868. if (lazy_op[self.operator] && !lhs.has_side_effects(compressor)) {
  10869. if (lhs.equivalent_to(self.right)) {
  10870. return maintain_this_binding(compressor, parent, compressor.self(), lhs).optimize(compressor);
  10871. }
  10872. mark_duplicate_condition(compressor, lhs);
  10873. }
  10874. in_bool = compressor.in_boolean_context();
  10875. }
  10876. if (in_bool) switch (self.operator) {
  10877. case "+":
  10878. var ev = self.left.evaluate(compressor, true);
  10879. if (ev && typeof ev == "string" || (ev = self.right.evaluate(compressor, true)) && typeof ev == "string") {
  10880. AST_Node.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
  10881. var exprs = [];
  10882. if (self.left.evaluate(compressor) instanceof AST_Node) exprs.push(self.left);
  10883. if (self.right.evaluate(compressor) instanceof AST_Node) exprs.push(self.right);
  10884. if (exprs.length < 2) {
  10885. exprs.push(make_node(AST_True, self));
  10886. return make_sequence(self, exprs).optimize(compressor);
  10887. }
  10888. self.truthy = true;
  10889. }
  10890. break;
  10891. case "==":
  10892. if (self.left instanceof AST_String && self.left.value == "" && self.right.is_string(compressor)) {
  10893. return make_node(AST_UnaryPrefix, self, {
  10894. operator: "!",
  10895. expression: self.right
  10896. }).optimize(compressor);
  10897. }
  10898. break;
  10899. case "!=":
  10900. if (self.left instanceof AST_String && self.left.value == "" && self.right.is_string(compressor)) {
  10901. return self.right.optimize(compressor);
  10902. }
  10903. break;
  10904. }
  10905. if (compressor.option("comparisons") && self.is_boolean(compressor)) {
  10906. if (!(parent instanceof AST_Binary) || parent instanceof AST_Assign) {
  10907. var negated = best_of(compressor, self, make_node(AST_UnaryPrefix, self, {
  10908. operator: "!",
  10909. expression: self.negate(compressor, first_in_statement(compressor))
  10910. }));
  10911. if (negated !== self) return negated;
  10912. }
  10913. switch (self.operator) {
  10914. case ">": reverse("<"); break;
  10915. case ">=": reverse("<="); break;
  10916. }
  10917. }
  10918. // x && (y && z) ---> x && y && z
  10919. // x || (y || z) ---> x || y || z
  10920. if (compressor.option("conditionals")
  10921. && lazy_op[self.operator]
  10922. && self.right instanceof AST_Binary
  10923. && self.operator == self.right.operator) {
  10924. swap_chain(self, compressor);
  10925. }
  10926. if (compressor.option("strings") && self.operator == "+") {
  10927. // "foo" + 42 + "" ---> "foo" + 42
  10928. if (self.right instanceof AST_String
  10929. && self.right.value == ""
  10930. && self.left.is_string(compressor)) {
  10931. return self.left.optimize(compressor);
  10932. }
  10933. // "" + ("foo" + 42) ---> "foo" + 42
  10934. if (self.left instanceof AST_String
  10935. && self.left.value == ""
  10936. && self.right.is_string(compressor)) {
  10937. return self.right.optimize(compressor);
  10938. }
  10939. // "" + 42 + "foo" ---> 42 + "foo"
  10940. if (self.left instanceof AST_Binary
  10941. && self.left.operator == "+"
  10942. && self.left.left instanceof AST_String
  10943. && self.left.left.value == ""
  10944. && self.right.is_string(compressor)
  10945. && (self.left.right.is_constant() || !self.right.has_side_effects(compressor))) {
  10946. self.left = self.left.right;
  10947. return self.optimize(compressor);
  10948. }
  10949. // "x" + (y + "z") ---> "x" + y + "z"
  10950. // x + ("y" + z) ---> x + "y" + z
  10951. if (self.right instanceof AST_Binary
  10952. && self.operator == self.right.operator
  10953. && (self.left.is_string(compressor) && self.right.is_string(compressor)
  10954. || self.right.left.is_string(compressor)
  10955. && (self.left.is_constant() || !self.right.right.has_side_effects(compressor)))) {
  10956. swap_chain(self, compressor);
  10957. }
  10958. }
  10959. if (compressor.option("evaluate")) {
  10960. var associative = true;
  10961. switch (self.operator) {
  10962. case "&&":
  10963. var ll = fuzzy_eval(compressor, self.left);
  10964. if (!ll) {
  10965. AST_Node.warn("Condition left of && always false [{file}:{line},{col}]", self.start);
  10966. return maintain_this_binding(compressor, parent, compressor.self(), self.left).optimize(compressor);
  10967. } else if (!(ll instanceof AST_Node)) {
  10968. AST_Node.warn("Condition left of && always true [{file}:{line},{col}]", self.start);
  10969. return make_sequence(self, [ self.left, self.right ]).optimize(compressor);
  10970. }
  10971. if (!self.right.evaluate(compressor, true)) {
  10972. if (in_bool && !(self.right.evaluate(compressor) instanceof AST_Node)) {
  10973. AST_Node.warn("Boolean && always false [{file}:{line},{col}]", self.start);
  10974. return make_sequence(self, [ self.left, make_node(AST_False, self) ]).optimize(compressor);
  10975. } else self.falsy = true;
  10976. } else if ((in_bool || parent.operator == "&&" && parent.left === compressor.self())
  10977. && !(self.right.evaluate(compressor) instanceof AST_Node)) {
  10978. AST_Node.warn("Dropping side-effect-free && [{file}:{line},{col}]", self.start);
  10979. return self.left.optimize(compressor);
  10980. }
  10981. // (x || false) && y ---> x ? y : false
  10982. if (self.left.operator == "||") {
  10983. var lr = fuzzy_eval(compressor, self.left.right);
  10984. if (!lr) return make_node(AST_Conditional, self, {
  10985. condition: self.left.left,
  10986. consequent: self.right,
  10987. alternative: self.left.right
  10988. }).optimize(compressor);
  10989. }
  10990. break;
  10991. case "??":
  10992. var nullish = true;
  10993. case "||":
  10994. var ll = fuzzy_eval(compressor, self.left, nullish);
  10995. if (nullish ? ll == null : !ll) {
  10996. AST_Node.warn("Condition left of {operator} always {value} [{file}:{line},{col}]", {
  10997. operator: self.operator,
  10998. value: nullish ? "nulish" : "false",
  10999. file: self.start.file,
  11000. line: self.start.line,
  11001. col: self.start.col,
  11002. });
  11003. return make_sequence(self, [ self.left, self.right ]).optimize(compressor);
  11004. } else if (!(ll instanceof AST_Node)) {
  11005. AST_Node.warn("Condition left of {operator} always {value} [{file}:{line},{col}]", {
  11006. operator: self.operator,
  11007. value: nullish ? "defined" : "true",
  11008. file: self.start.file,
  11009. line: self.start.line,
  11010. col: self.start.col,
  11011. });
  11012. return maintain_this_binding(compressor, parent, compressor.self(), self.left).optimize(compressor);
  11013. }
  11014. var rr;
  11015. if (!nullish && (rr = self.right.evaluate(compressor, true)) && !(rr instanceof AST_Node)) {
  11016. if (in_bool && !(self.right.evaluate(compressor) instanceof AST_Node)) {
  11017. AST_Node.warn("Boolean || always true [{file}:{line},{col}]", self.start);
  11018. return make_sequence(self, [ self.left, make_node(AST_True, self) ]).optimize(compressor);
  11019. } else self.truthy = true;
  11020. } else if ((in_bool || parent.operator == "||" && parent.left === compressor.self())
  11021. && !self.right.evaluate(compressor)) {
  11022. AST_Node.warn("Dropping side-effect-free {operator} [{file}:{line},{col}]", {
  11023. operator: self.operator,
  11024. file: self.start.file,
  11025. line: self.start.line,
  11026. col: self.start.col,
  11027. });
  11028. return self.left.optimize(compressor);
  11029. }
  11030. // x && true || y ---> x ? true : y
  11031. if (!nullish && self.left.operator == "&&") {
  11032. var lr = fuzzy_eval(compressor, self.left.right);
  11033. if (lr && !(lr instanceof AST_Node)) return make_node(AST_Conditional, self, {
  11034. condition: self.left.left,
  11035. consequent: self.left.right,
  11036. alternative: self.right
  11037. }).optimize(compressor);
  11038. }
  11039. break;
  11040. case "+":
  11041. // "foo" + ("bar" + x) ---> "foobar" + x
  11042. if (self.left instanceof AST_Constant
  11043. && self.right instanceof AST_Binary
  11044. && self.right.operator == "+"
  11045. && self.right.left instanceof AST_Constant
  11046. && self.right.is_string(compressor)) {
  11047. self = make_node(AST_Binary, self, {
  11048. operator: "+",
  11049. left: make_node(AST_String, self.left, {
  11050. value: "" + self.left.value + self.right.left.value,
  11051. start: self.left.start,
  11052. end: self.right.left.end
  11053. }),
  11054. right: self.right.right
  11055. });
  11056. }
  11057. // (x + "foo") + "bar" ---> x + "foobar"
  11058. if (self.right instanceof AST_Constant
  11059. && self.left instanceof AST_Binary
  11060. && self.left.operator == "+"
  11061. && self.left.right instanceof AST_Constant
  11062. && self.left.is_string(compressor)) {
  11063. self = make_node(AST_Binary, self, {
  11064. operator: "+",
  11065. left: self.left.left,
  11066. right: make_node(AST_String, self.right, {
  11067. value: "" + self.left.right.value + self.right.value,
  11068. start: self.left.right.start,
  11069. end: self.right.end
  11070. })
  11071. });
  11072. }
  11073. // a + -b ---> a - b
  11074. if (self.right instanceof AST_UnaryPrefix
  11075. && self.right.operator == "-"
  11076. && self.left.is_number(compressor)) {
  11077. self = make_node(AST_Binary, self, {
  11078. operator: "-",
  11079. left: self.left,
  11080. right: self.right.expression
  11081. });
  11082. break;
  11083. }
  11084. // -a + b ---> b - a
  11085. if (self.left instanceof AST_UnaryPrefix
  11086. && self.left.operator == "-"
  11087. && reversible()
  11088. && self.right.is_number(compressor)) {
  11089. self = make_node(AST_Binary, self, {
  11090. operator: "-",
  11091. left: self.right,
  11092. right: self.left.expression
  11093. });
  11094. break;
  11095. }
  11096. // (a + b) + 3 ---> 3 + (a + b)
  11097. if (compressor.option("unsafe_math")
  11098. && self.left instanceof AST_Binary
  11099. && PRECEDENCE[self.left.operator] == PRECEDENCE[self.operator]
  11100. && self.right.is_constant()
  11101. && (self.right.is_boolean(compressor) || self.right.is_number(compressor))
  11102. && self.left.is_number(compressor)
  11103. && !self.left.right.is_constant()
  11104. && (self.left.left.is_boolean(compressor) || self.left.left.is_number(compressor))) {
  11105. self = make_node(AST_Binary, self, {
  11106. operator: self.left.operator,
  11107. left: make_node(AST_Binary, self, {
  11108. operator: self.operator,
  11109. left: self.right,
  11110. right: self.left.left
  11111. }),
  11112. right: self.left.right
  11113. });
  11114. break;
  11115. }
  11116. case "-":
  11117. // a - -b ---> a + b
  11118. if (self.right instanceof AST_UnaryPrefix
  11119. && self.right.operator == "-"
  11120. && self.left.is_number(compressor)
  11121. && self.right.expression.is_number(compressor)) {
  11122. self = make_node(AST_Binary, self, {
  11123. operator: "+",
  11124. left: self.left,
  11125. right: self.right.expression
  11126. });
  11127. break;
  11128. }
  11129. case "*":
  11130. case "/":
  11131. associative = compressor.option("unsafe_math");
  11132. // +a - b ---> a - b
  11133. // a - +b ---> a - b
  11134. if (self.operator != "+") [ "left", "right" ].forEach(function(operand) {
  11135. var node = self[operand];
  11136. if (node instanceof AST_UnaryPrefix && node.operator == "+") {
  11137. var exp = node.expression;
  11138. if (exp.is_boolean(compressor) || exp.is_number(compressor) || exp.is_string(compressor)) {
  11139. self[operand] = exp;
  11140. }
  11141. }
  11142. });
  11143. case "&":
  11144. case "|":
  11145. case "^":
  11146. // a + +b ---> +b + a
  11147. if (self.operator != "-"
  11148. && self.operator != "/"
  11149. && (self.left.is_boolean(compressor) || self.left.is_number(compressor))
  11150. && (self.right.is_boolean(compressor) || self.right.is_number(compressor))
  11151. && reversible()
  11152. && !(self.left instanceof AST_Binary
  11153. && self.left.operator != self.operator
  11154. && PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
  11155. var reversed = make_node(AST_Binary, self, {
  11156. operator: self.operator,
  11157. left: self.right,
  11158. right: self.left
  11159. });
  11160. if (self.right instanceof AST_Constant
  11161. && !(self.left instanceof AST_Constant)) {
  11162. self = best_of(compressor, reversed, self);
  11163. } else {
  11164. self = best_of(compressor, self, reversed);
  11165. }
  11166. }
  11167. if (!associative || !self.is_number(compressor)) break;
  11168. // a + (b + c) ---> (a + b) + c
  11169. if (self.right instanceof AST_Binary
  11170. && self.right.operator != "%"
  11171. && PRECEDENCE[self.right.operator] == PRECEDENCE[self.operator]
  11172. && self.right.is_number(compressor)
  11173. && (self.operator != "+"
  11174. || self.right.left.is_boolean(compressor)
  11175. || self.right.left.is_number(compressor))
  11176. && (self.operator != "-" || !self.left.is_negative_zero())
  11177. && (self.right.left.is_constant_expression()
  11178. || !self.right.right.has_side_effects(compressor))
  11179. && !is_modify_array(self.right.right)) {
  11180. self = make_node(AST_Binary, self, {
  11181. operator: align(self.operator, self.right.operator),
  11182. left: make_node(AST_Binary, self.left, {
  11183. operator: self.operator,
  11184. left: self.left,
  11185. right: self.right.left,
  11186. start: self.left.start,
  11187. end: self.right.left.end
  11188. }),
  11189. right: self.right.right
  11190. });
  11191. if (self.operator == "+"
  11192. && !self.right.is_boolean(compressor)
  11193. && !self.right.is_number(compressor)) {
  11194. self.right = make_node(AST_UnaryPrefix, self.right, {
  11195. operator: "+",
  11196. expression: self.right
  11197. });
  11198. }
  11199. }
  11200. // (2 * n) * 3 ---> 6 * n
  11201. // (n + 2) + 3 ---> n + 5
  11202. if (self.right instanceof AST_Constant
  11203. && self.left instanceof AST_Binary
  11204. && self.left.operator != "%"
  11205. && PRECEDENCE[self.left.operator] == PRECEDENCE[self.operator]
  11206. && self.left.is_number(compressor)) {
  11207. if (self.left.left instanceof AST_Constant) {
  11208. var lhs = make_binary(self.left, self.operator, self.left.left, self.right, self.left.left.start, self.right.end);
  11209. self = make_binary(self, self.left.operator, try_evaluate(compressor, lhs), self.left.right);
  11210. } else if (self.left.right instanceof AST_Constant) {
  11211. var op = align(self.left.operator, self.operator);
  11212. var rhs = try_evaluate(compressor, make_binary(self.left, op, self.left.right, self.right));
  11213. if (rhs.is_constant()
  11214. && !(self.left.operator == "-"
  11215. && self.right.value != 0
  11216. && +rhs.value == 0
  11217. && self.left.left.is_negative_zero())) {
  11218. self = make_binary(self, self.left.operator, self.left.left, rhs);
  11219. }
  11220. }
  11221. }
  11222. break;
  11223. }
  11224. if (!(parent instanceof AST_UnaryPrefix && parent.operator == "delete")) {
  11225. if (self.left instanceof AST_Number && !self.right.is_constant()) switch (self.operator) {
  11226. // 0 + n ---> n
  11227. case "+":
  11228. if (self.left.value == 0) {
  11229. if (self.right.is_boolean(compressor)) return make_node(AST_UnaryPrefix, self, {
  11230. operator: "+",
  11231. expression: self.right,
  11232. }).optimize(compressor);
  11233. if (self.right.is_number(compressor) && !self.right.is_negative_zero()) return self.right;
  11234. }
  11235. break;
  11236. // 1 * n ---> n
  11237. case "*":
  11238. if (self.left.value == 1) return make_node(AST_UnaryPrefix, self, {
  11239. operator: "+",
  11240. expression: self.right,
  11241. }).optimize(compressor);
  11242. break;
  11243. }
  11244. if (self.right instanceof AST_Number && !self.left.is_constant()) switch (self.operator) {
  11245. // n + 0 ---> n
  11246. case "+":
  11247. if (self.right.value == 0) {
  11248. if (self.left.is_boolean(compressor)) return make_node(AST_UnaryPrefix, self, {
  11249. operator: "+",
  11250. expression: self.left,
  11251. }).optimize(compressor);
  11252. if (self.left.is_number(compressor) && !self.left.is_negative_zero()) return self.left;
  11253. }
  11254. break;
  11255. // n - 0 ---> n
  11256. case "-":
  11257. if (self.right.value == 0) return make_node(AST_UnaryPrefix, self, {
  11258. operator: "+",
  11259. expression: self.left,
  11260. }).optimize(compressor);
  11261. break;
  11262. // n / 1 ---> n
  11263. case "/":
  11264. if (self.right.value == 1) return make_node(AST_UnaryPrefix, self, {
  11265. operator: "+",
  11266. expression: self.left,
  11267. }).optimize(compressor);
  11268. break;
  11269. }
  11270. }
  11271. }
  11272. if (compressor.option("typeofs")) switch (self.operator) {
  11273. case "&&":
  11274. mark_locally_defined(self.left, self.right, null);
  11275. break;
  11276. case "||":
  11277. mark_locally_defined(self.left, null, self.right);
  11278. break;
  11279. }
  11280. if (compressor.option("unsafe")) {
  11281. var indexRight = is_indexFn(self.right);
  11282. if (in_bool
  11283. && indexRight
  11284. && (self.operator == "==" || self.operator == "!=")
  11285. && self.left instanceof AST_Number
  11286. && self.left.value == 0) {
  11287. return (self.operator == "==" ? make_node(AST_UnaryPrefix, self, {
  11288. operator: "!",
  11289. expression: self.right
  11290. }) : self.right).optimize(compressor);
  11291. }
  11292. var indexLeft = is_indexFn(self.left);
  11293. if (compressor.option("comparisons") && is_indexOf_match_pattern()) {
  11294. var node = make_node(AST_UnaryPrefix, self, {
  11295. operator: "!",
  11296. expression: make_node(AST_UnaryPrefix, self, {
  11297. operator: "~",
  11298. expression: indexLeft ? self.left : self.right
  11299. })
  11300. });
  11301. switch (self.operator) {
  11302. case "<":
  11303. if (indexLeft) break;
  11304. case "<=":
  11305. case "!=":
  11306. node = make_node(AST_UnaryPrefix, self, {
  11307. operator: "!",
  11308. expression: node
  11309. });
  11310. break;
  11311. }
  11312. return node.optimize(compressor);
  11313. }
  11314. }
  11315. return try_evaluate(compressor, self);
  11316. function is_modify_array(node) {
  11317. var found = false;
  11318. node.walk(new TreeWalker(function(node) {
  11319. if (found) return true;
  11320. if (node instanceof AST_Assign) {
  11321. if (node.left instanceof AST_PropAccess) return found = true;
  11322. } else if (node instanceof AST_Unary) {
  11323. if (unary_side_effects[node.operator] && node.expression instanceof AST_PropAccess) {
  11324. return found = true;
  11325. }
  11326. }
  11327. }));
  11328. return found;
  11329. }
  11330. function align(ref, op) {
  11331. switch (ref) {
  11332. case "-":
  11333. return op == "+" ? "-" : "+";
  11334. case "/":
  11335. return op == "*" ? "/" : "*";
  11336. default:
  11337. return op;
  11338. }
  11339. }
  11340. function make_binary(orig, op, left, right, start, end) {
  11341. if (op == "+") {
  11342. if (!left.is_boolean(compressor) && !left.is_number(compressor)) {
  11343. left = make_node(AST_UnaryPrefix, left, {
  11344. operator: "+",
  11345. expression: left
  11346. });
  11347. }
  11348. if (!right.is_boolean(compressor) && !right.is_number(compressor)) {
  11349. right = make_node(AST_UnaryPrefix, right, {
  11350. operator: "+",
  11351. expression: right
  11352. });
  11353. }
  11354. }
  11355. return make_node(AST_Binary, orig, {
  11356. operator: op,
  11357. left: left,
  11358. right: right,
  11359. start: start,
  11360. end: end
  11361. });
  11362. }
  11363. function is_indexFn(node) {
  11364. return node.TYPE == "Call"
  11365. && node.expression instanceof AST_Dot
  11366. && indexFns[node.expression.property];
  11367. }
  11368. function is_indexOf_match_pattern() {
  11369. switch (self.operator) {
  11370. case "<=":
  11371. // 0 <= array.indexOf(string) ---> !!~array.indexOf(string)
  11372. return indexRight && self.left instanceof AST_Number && self.left.value == 0;
  11373. case "<":
  11374. // array.indexOf(string) < 0 ---> !~array.indexOf(string)
  11375. if (indexLeft && self.right instanceof AST_Number && self.right.value == 0) return true;
  11376. // -1 < array.indexOf(string) ---> !!~array.indexOf(string)
  11377. case "==":
  11378. case "!=":
  11379. // -1 == array.indexOf(string) ---> !~array.indexOf(string)
  11380. // -1 != array.indexOf(string) ---> !!~array.indexOf(string)
  11381. if (!indexRight) return false;
  11382. return self.left instanceof AST_Number && self.left.value == -1
  11383. || self.left instanceof AST_UnaryPrefix && self.left.operator == "-"
  11384. && self.left.expression instanceof AST_Number && self.left.expression.value == 1;
  11385. }
  11386. }
  11387. function reversible() {
  11388. return self.left.is_constant()
  11389. || self.right.is_constant()
  11390. || !self.left.has_side_effects(compressor)
  11391. && !self.right.has_side_effects(compressor);
  11392. }
  11393. function reverse(op) {
  11394. if (reversible()) {
  11395. if (op) self.operator = op;
  11396. var tmp = self.left;
  11397. self.left = self.right;
  11398. self.right = tmp;
  11399. }
  11400. }
  11401. });
  11402. OPT(AST_SymbolExport, function(self) {
  11403. return self;
  11404. });
  11405. function recursive_ref(compressor, def, fn) {
  11406. var level = 0, node = compressor.self();
  11407. do {
  11408. if (node === fn) return node;
  11409. if (is_lambda(node) && node.name && node.name.definition() === def) return node;
  11410. } while (node = compressor.parent(level++));
  11411. }
  11412. function same_scope(def) {
  11413. var scope = def.scope.resolve();
  11414. return all(def.references, function(ref) {
  11415. return scope === ref.scope.resolve();
  11416. });
  11417. }
  11418. OPT(AST_SymbolRef, function(self, compressor) {
  11419. if (!compressor.option("ie")
  11420. && is_undeclared_ref(self)
  11421. // testing against `self.scope.uses_with` is an optimization
  11422. && !(self.scope.resolve().uses_with && compressor.find_parent(AST_With))) {
  11423. switch (self.name) {
  11424. case "undefined":
  11425. return make_node(AST_Undefined, self).optimize(compressor);
  11426. case "NaN":
  11427. return make_node(AST_NaN, self).optimize(compressor);
  11428. case "Infinity":
  11429. return make_node(AST_Infinity, self).optimize(compressor);
  11430. }
  11431. }
  11432. var parent = compressor.parent();
  11433. if (compressor.option("reduce_vars") && is_lhs(compressor.self(), parent) !== compressor.self()) {
  11434. var def = self.definition();
  11435. var fixed = self.fixed_value();
  11436. var single_use = def.single_use && !(parent instanceof AST_Call && parent.is_expr_pure(compressor));
  11437. if (single_use) {
  11438. if (is_lambda(fixed)) {
  11439. if ((def.scope !== self.scope.resolve() || def.in_loop)
  11440. && (!compressor.option("reduce_funcs") || def.escaped.depth == 1 || fixed.inlined)) {
  11441. single_use = false;
  11442. } else if (recursive_ref(compressor, def, fixed)) {
  11443. single_use = false;
  11444. } else if (fixed.name && fixed.name.definition() !== def) {
  11445. single_use = false;
  11446. } else if (fixed.parent_scope !== self.scope || is_funarg(def)) {
  11447. single_use = fixed.is_constant_expression(self.scope);
  11448. if (single_use == "f") {
  11449. var scope = self.scope;
  11450. do {
  11451. if (scope instanceof AST_LambdaDefinition || scope instanceof AST_LambdaExpression) {
  11452. scope.inlined = true;
  11453. }
  11454. } while (scope = scope.parent_scope);
  11455. }
  11456. } else if (fixed.name && (fixed.name.name == "await" && is_async(fixed)
  11457. || fixed.name.name == "yield" && is_generator(fixed))) {
  11458. single_use = false;
  11459. } else if (fixed.has_side_effects(compressor)) {
  11460. single_use = false;
  11461. } else if (compressor.option("ie") && fixed instanceof AST_Class) {
  11462. single_use = false;
  11463. }
  11464. if (single_use) fixed.parent_scope = self.scope;
  11465. } else if (!fixed
  11466. || def.recursive_refs > 0
  11467. || !fixed.is_constant_expression()
  11468. || fixed.drop_side_effect_free(compressor)) {
  11469. single_use = false;
  11470. }
  11471. }
  11472. if (single_use) {
  11473. def.single_use = false;
  11474. fixed._squeezed = true;
  11475. fixed.single_use = true;
  11476. if (fixed instanceof AST_DefClass) fixed = to_class_expr(fixed);
  11477. if (fixed instanceof AST_LambdaDefinition) fixed = to_func_expr(fixed);
  11478. if (is_lambda(fixed)) {
  11479. var scope = self.scope.resolve();
  11480. fixed.enclosed.forEach(function(def) {
  11481. if (fixed.variables.has(def.name)) return;
  11482. if (scope.var_names().has(def.name)) return;
  11483. scope.enclosed.push(def);
  11484. scope.var_names().set(def.name, true);
  11485. });
  11486. }
  11487. var value;
  11488. if (def.recursive_refs > 0) {
  11489. value = fixed.clone(true);
  11490. var defun_def = value.name.definition();
  11491. var lambda_def = value.variables.get(value.name.name);
  11492. var name = lambda_def && lambda_def.orig[0];
  11493. var def_fn_name, symbol_type;
  11494. if (value instanceof AST_Class) {
  11495. def_fn_name = "def_function";
  11496. symbol_type = AST_SymbolClass;
  11497. } else {
  11498. def_fn_name = "def_variable";
  11499. symbol_type = AST_SymbolLambda;
  11500. }
  11501. if (!(name instanceof symbol_type)) {
  11502. name = make_node(symbol_type, value.name, value.name);
  11503. name.scope = value;
  11504. value.name = name;
  11505. lambda_def = value[def_fn_name](name);
  11506. lambda_def.recursive_refs = def.recursive_refs;
  11507. }
  11508. value.walk(new TreeWalker(function(node) {
  11509. if (node instanceof AST_SymbolDeclaration) {
  11510. if (node !== name) {
  11511. var def = node.definition();
  11512. def.orig.push(node);
  11513. def.eliminated++;
  11514. }
  11515. return;
  11516. }
  11517. if (!(node instanceof AST_SymbolRef)) return;
  11518. var def = node.definition();
  11519. if (def === defun_def) {
  11520. node.thedef = def = lambda_def;
  11521. } else {
  11522. def.single_use = false;
  11523. var fn = node.fixed_value();
  11524. if (is_lambda(fn)
  11525. && fn.name
  11526. && fn.name.definition() === def
  11527. && def.scope === fn.name.scope
  11528. && fixed.variables.get(fn.name.name) === def) {
  11529. fn.name = fn.name.clone();
  11530. node.thedef = def = value.variables.get(fn.name.name) || value[def_fn_name](fn.name);
  11531. }
  11532. }
  11533. def.references.push(node);
  11534. }));
  11535. } else {
  11536. if (fixed instanceof AST_Scope) {
  11537. compressor.push(fixed);
  11538. value = fixed.optimize(compressor);
  11539. compressor.pop();
  11540. } else {
  11541. value = fixed.optimize(compressor);
  11542. }
  11543. value = value.transform(new TreeTransformer(function(node, descend) {
  11544. if (node instanceof AST_Scope) return node;
  11545. node = node.clone();
  11546. descend(node, this);
  11547. return node;
  11548. }));
  11549. }
  11550. def.replaced++;
  11551. return value;
  11552. }
  11553. var state;
  11554. if (fixed && (state = self.fixed || def.fixed).should_replace !== false) {
  11555. var ev, init;
  11556. if (fixed instanceof AST_This) {
  11557. if (!is_funarg(def) && same_scope(def) && !cross_class(def)) init = fixed;
  11558. } else if ((ev = fixed.evaluate(compressor, true)) !== fixed
  11559. && typeof ev != "function"
  11560. && (ev === null
  11561. || typeof ev != "object"
  11562. || compressor.option("unsafe_regexp")
  11563. && ev instanceof RegExp && !def.cross_loop && same_scope(def))) {
  11564. init = make_node_from_constant(ev, fixed);
  11565. }
  11566. if (init) {
  11567. if (state.should_replace === undefined) {
  11568. var value_length = init.optimize(compressor).print_to_string().length;
  11569. if (!has_symbol_ref(fixed)) {
  11570. value_length = Math.min(value_length, fixed.print_to_string().length);
  11571. }
  11572. var name_length = def.name.length;
  11573. if (compressor.option("unused") && !compressor.exposed(def)) {
  11574. var refs = def.references.length - def.replaced - def.assignments;
  11575. refs = Math.min(refs, def.references.filter(function(ref) {
  11576. return ref.fixed === state;
  11577. }).length);
  11578. name_length += (name_length + 2 + value_length) / Math.max(1, refs);
  11579. }
  11580. state.should_replace = value_length - Math.floor(name_length) < compressor.eval_threshold;
  11581. }
  11582. if (state.should_replace) {
  11583. var value;
  11584. if (has_symbol_ref(fixed)) {
  11585. value = init.optimize(compressor);
  11586. if (value === init) value = value.clone(true);
  11587. } else {
  11588. value = best_of_expression(init.optimize(compressor), fixed);
  11589. if (value === init || value === fixed) value = value.clone(true);
  11590. }
  11591. def.replaced++;
  11592. return value;
  11593. }
  11594. }
  11595. }
  11596. }
  11597. return self;
  11598. function cross_class(def) {
  11599. var scope = self.scope;
  11600. while (scope !== def.scope) {
  11601. if (scope instanceof AST_Class) return true;
  11602. scope = scope.parent_scope;
  11603. }
  11604. }
  11605. function has_symbol_ref(value) {
  11606. var found;
  11607. value.walk(new TreeWalker(function(node) {
  11608. if (node instanceof AST_SymbolRef) found = true;
  11609. if (found) return true;
  11610. }));
  11611. return found;
  11612. }
  11613. });
  11614. function is_raw_tag(compressor, tag) {
  11615. return compressor.option("unsafe")
  11616. && tag instanceof AST_Dot
  11617. && tag.property == "raw"
  11618. && is_undeclared_ref(tag.expression)
  11619. && tag.expression.name == "String";
  11620. }
  11621. function decode_template(str) {
  11622. var malformed = false;
  11623. str = str.replace(/\\(u\{[^{}]*\}?|u[\s\S]{0,4}|x[\s\S]{0,2}|[0-9]+|[\s\S])/g, function(match, seq) {
  11624. var ch = decode_escape_sequence(seq);
  11625. if (typeof ch == "string") return ch;
  11626. malformed = true;
  11627. });
  11628. if (!malformed) return str;
  11629. }
  11630. OPT(AST_Template, function(self, compressor) {
  11631. if (!compressor.option("templates")) return self;
  11632. var tag = self.tag;
  11633. if (!tag || is_raw_tag(compressor, tag)) {
  11634. var exprs = [];
  11635. var strs = [];
  11636. for (var i = 0, status; i < self.strings.length; i++) {
  11637. var str = self.strings[i];
  11638. if (!tag) {
  11639. var trimmed = decode_template(str);
  11640. if (trimmed) str = escape_literal(trimmed);
  11641. }
  11642. if (i > 0) {
  11643. var node = self.expressions[i - 1];
  11644. var value = should_join(node);
  11645. if (value) {
  11646. var prev = strs[strs.length - 1];
  11647. var joined = prev + value + str;
  11648. var decoded;
  11649. if (tag || typeof (decoded = decode_template(joined)) == status) {
  11650. strs[strs.length - 1] = decoded ? escape_literal(decoded) : joined;
  11651. continue;
  11652. }
  11653. }
  11654. exprs.push(node);
  11655. }
  11656. strs.push(str);
  11657. if (!tag) status = typeof trimmed;
  11658. }
  11659. if (!tag && strs.length > 1) {
  11660. if (strs[strs.length - 1] == "") return make_node(AST_Binary, self, {
  11661. operator: "+",
  11662. left: make_node(AST_Template, self, {
  11663. expressions: exprs.slice(0, -1),
  11664. strings: strs.slice(0, -1),
  11665. tag: tag,
  11666. }).transform(compressor),
  11667. right: exprs[exprs.length - 1],
  11668. }).optimize(compressor);
  11669. if (strs[0] == "") {
  11670. var left = make_node(AST_Binary, self, {
  11671. operator: "+",
  11672. left: make_node(AST_String, self, { value: "" }),
  11673. right: exprs[0],
  11674. });
  11675. for (var i = 1; strs[i] == "" && i < exprs.length; i++) {
  11676. left = make_node(AST_Binary, self, {
  11677. operator: "+",
  11678. left: left,
  11679. right: exprs[i],
  11680. });
  11681. }
  11682. return best_of(compressor, self, make_node(AST_Binary, self, {
  11683. operator: "+",
  11684. left: left.transform(compressor),
  11685. right: make_node(AST_Template, self, {
  11686. expressions: exprs.slice(i),
  11687. strings: strs.slice(i),
  11688. tag: tag,
  11689. }).transform(compressor),
  11690. }).optimize(compressor));
  11691. }
  11692. }
  11693. self.expressions = exprs;
  11694. self.strings = strs;
  11695. }
  11696. return try_evaluate(compressor, self);
  11697. function escape_literal(str) {
  11698. return str.replace(/\r|\\|`|\${/g, function(s) {
  11699. return "\\" + (s == "\r" ? "r" : s);
  11700. });
  11701. }
  11702. function should_join(node) {
  11703. var ev = node.evaluate(compressor);
  11704. if (ev === node) return;
  11705. if (tag && /\r|\\|`/.test(ev)) return;
  11706. ev = escape_literal("" + ev);
  11707. if (ev.length > node.print_to_string().length + "${}".length) return;
  11708. return ev;
  11709. }
  11710. });
  11711. function is_atomic(lhs, self) {
  11712. return lhs instanceof AST_SymbolRef || lhs.TYPE === self.TYPE;
  11713. }
  11714. OPT(AST_Undefined, function(self, compressor) {
  11715. if (compressor.option("unsafe_undefined")) {
  11716. var undef = find_scope(compressor).find_variable("undefined");
  11717. if (undef) {
  11718. var ref = make_node(AST_SymbolRef, self, {
  11719. name : "undefined",
  11720. scope : undef.scope,
  11721. thedef : undef
  11722. });
  11723. ref.is_undefined = true;
  11724. return ref;
  11725. }
  11726. }
  11727. var lhs = is_lhs(compressor.self(), compressor.parent());
  11728. if (lhs && is_atomic(lhs, self)) return self;
  11729. return make_node(AST_UnaryPrefix, self, {
  11730. operator: "void",
  11731. expression: make_node(AST_Number, self, { value: 0 }),
  11732. });
  11733. });
  11734. OPT(AST_Infinity, function(self, compressor) {
  11735. var lhs = is_lhs(compressor.self(), compressor.parent());
  11736. if (lhs && is_atomic(lhs, self)) return self;
  11737. if (compressor.option("keep_infinity") && !lhs && !find_scope(compressor).find_variable("Infinity")) {
  11738. return self;
  11739. }
  11740. return make_node(AST_Binary, self, {
  11741. operator: "/",
  11742. left: make_node(AST_Number, self, { value: 1 }),
  11743. right: make_node(AST_Number, self, { value: 0 }),
  11744. });
  11745. });
  11746. OPT(AST_NaN, function(self, compressor) {
  11747. var lhs = is_lhs(compressor.self(), compressor.parent());
  11748. if (lhs && is_atomic(lhs, self)) return self;
  11749. if (!lhs && !find_scope(compressor).find_variable("NaN")) return self;
  11750. return make_node(AST_Binary, self, {
  11751. operator: "/",
  11752. left: make_node(AST_Number, self, { value: 0 }),
  11753. right: make_node(AST_Number, self, { value: 0 }),
  11754. });
  11755. });
  11756. function is_reachable(self, defs) {
  11757. var reachable = false;
  11758. var find_ref = new TreeWalker(function(node) {
  11759. if (reachable) return true;
  11760. if (node instanceof AST_SymbolRef && member(node.definition(), defs)) return reachable = true;
  11761. });
  11762. var scan_scope = new TreeWalker(function(node) {
  11763. if (reachable) return true;
  11764. if (node instanceof AST_Lambda && node !== self) {
  11765. if (!(node.name || is_async(node) || is_generator(node))) {
  11766. var parent = scan_scope.parent();
  11767. if (parent instanceof AST_Call && parent.expression === node) return;
  11768. }
  11769. node.walk(find_ref);
  11770. return true;
  11771. }
  11772. });
  11773. self.walk(scan_scope);
  11774. return reachable;
  11775. }
  11776. var ASSIGN_OPS = makePredicate("+ - * / % >> << >>> | ^ &");
  11777. var ASSIGN_OPS_COMMUTATIVE = makePredicate("* | ^ &");
  11778. OPT(AST_Assign, function(self, compressor) {
  11779. if (compressor.option("dead_code")) {
  11780. if (self.left instanceof AST_PropAccess) {
  11781. if (self.operator == "=") {
  11782. if (self.redundant) {
  11783. var exprs = [ self.left.expression ];
  11784. if (self.left instanceof AST_Sub) exprs.push(self.left.property);
  11785. exprs.push(self.right);
  11786. return make_sequence(self, exprs).optimize(compressor);
  11787. }
  11788. if (self.left.equivalent_to(self.right) && !self.left.has_side_effects(compressor)) {
  11789. return self.right;
  11790. }
  11791. var exp = self.left.expression;
  11792. if (exp instanceof AST_Lambda
  11793. || !compressor.has_directive("use strict")
  11794. && exp instanceof AST_Constant
  11795. && !exp.may_throw_on_access(compressor)) {
  11796. return self.left instanceof AST_Dot ? self.right : make_sequence(self, [
  11797. self.left.property,
  11798. self.right
  11799. ]).optimize(compressor);
  11800. }
  11801. }
  11802. } else if (self.left instanceof AST_SymbolRef && can_drop_symbol(self.left, compressor)) {
  11803. var parent;
  11804. if (self.operator == "=" && self.left.equivalent_to(self.right)
  11805. && !((parent = compressor.parent()) instanceof AST_UnaryPrefix && parent.operator == "delete")) {
  11806. return self.right;
  11807. }
  11808. if (self.left.is_immutable()) return strip_assignment();
  11809. var def = self.left.definition();
  11810. var scope = def.scope.resolve();
  11811. var local = scope === compressor.find_parent(AST_Lambda);
  11812. var level = 0, node;
  11813. parent = compressor.self();
  11814. if (!(scope.uses_arguments && is_funarg(def)) || compressor.has_directive("use strict")) do {
  11815. node = parent;
  11816. parent = compressor.parent(level++);
  11817. if (parent instanceof AST_Assign) {
  11818. if (parent.left instanceof AST_SymbolRef && parent.left.definition() === def) {
  11819. if (in_try(level, parent)) break;
  11820. return strip_assignment(def);
  11821. }
  11822. if (parent.left.match_symbol(function(node) {
  11823. if (node instanceof AST_PropAccess) return true;
  11824. })) break;
  11825. continue;
  11826. }
  11827. if (parent instanceof AST_Exit) {
  11828. if (!local) break;
  11829. if (in_try(level, parent)) break;
  11830. if (is_reachable(scope, [ def ])) break;
  11831. return strip_assignment(def);
  11832. }
  11833. if (parent instanceof AST_SimpleStatement) {
  11834. if (!local) break;
  11835. if (is_reachable(scope, [ def ])) break;
  11836. var stat;
  11837. do {
  11838. stat = parent;
  11839. parent = compressor.parent(level++);
  11840. if (parent === scope && is_last_statement(parent.body, stat)) return strip_assignment(def);
  11841. } while (is_tail_block(stat, parent));
  11842. break;
  11843. }
  11844. if (parent instanceof AST_VarDef) {
  11845. if (!(parent.name instanceof AST_SymbolDeclaration)) continue;
  11846. if (parent.name.definition() !== def) continue;
  11847. if (in_try(level, parent)) break;
  11848. return strip_assignment(def);
  11849. }
  11850. } while (is_tail(node, parent));
  11851. }
  11852. }
  11853. if (compressor.option("sequences")) {
  11854. var seq = self.lift_sequences(compressor);
  11855. if (seq !== self) return seq.optimize(compressor);
  11856. }
  11857. if (compressor.option("assignments")) {
  11858. if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) {
  11859. var ref;
  11860. // x = expr1 OP expr2
  11861. if ((ref = self.right.left) instanceof AST_SymbolRef
  11862. && ref.name == self.left.name
  11863. && ASSIGN_OPS[self.right.operator]) {
  11864. // x = x - 2 ---> x -= 2
  11865. if (self.left.fixed) self.left.fixed.to_binary = function() {
  11866. return ref;
  11867. };
  11868. return make_node(AST_Assign, self, {
  11869. operator: self.right.operator + "=",
  11870. left: self.left,
  11871. right: self.right.right,
  11872. });
  11873. }
  11874. if ((ref = self.right.right) instanceof AST_SymbolRef
  11875. && ref.name == self.left.name
  11876. && ASSIGN_OPS_COMMUTATIVE[self.right.operator]
  11877. && !self.right.left.has_side_effects(compressor)) {
  11878. // x = 2 & x ---> x &= 2
  11879. if (self.left.fixed) self.left.fixed.to_binary = function() {
  11880. return ref;
  11881. };
  11882. return make_node(AST_Assign, self, {
  11883. operator: self.right.operator + "=",
  11884. left: self.left,
  11885. right: self.right.left,
  11886. });
  11887. }
  11888. }
  11889. if ((self.operator == "-=" || self.operator == "+="
  11890. && (self.left.is_boolean(compressor) || self.left.is_number(compressor)))
  11891. && self.right instanceof AST_Number
  11892. && self.right.value == 1) {
  11893. var op = self.operator.slice(0, -1);
  11894. return make_node(AST_UnaryPrefix, self, {
  11895. operator: op + op,
  11896. expression: self.left
  11897. });
  11898. }
  11899. }
  11900. return try_evaluate(compressor, self);
  11901. function is_tail(node, parent) {
  11902. if (parent instanceof AST_Binary) {
  11903. return parent.right === node || parent.right.is_constant_expression(scope);
  11904. }
  11905. if (parent instanceof AST_Conditional) {
  11906. return parent.condition !== node
  11907. || parent.consequent.is_constant_expression(scope)
  11908. && parent.alternative.is_constant_expression(scope);
  11909. }
  11910. if (parent instanceof AST_Sequence) {
  11911. var exprs = parent.expressions;
  11912. var stop = exprs.indexOf(node);
  11913. if (stop < 0) return false;
  11914. for (var i = exprs.length; --i > stop;) {
  11915. if (!exprs[i].is_constant_expression(scope)) return false;
  11916. }
  11917. return true;
  11918. }
  11919. if (parent instanceof AST_UnaryPrefix) return true;
  11920. }
  11921. function is_tail_block(stat, parent) {
  11922. if (parent instanceof AST_BlockStatement) return is_last_statement(parent.body, stat);
  11923. if (parent instanceof AST_Catch) return is_last_statement(parent.body, stat);
  11924. if (parent instanceof AST_Finally) return is_last_statement(parent.body, stat);
  11925. if (parent instanceof AST_If) return parent.body === stat || parent.alternative === stat;
  11926. if (parent instanceof AST_Try) return parent.bfinally ? parent.bfinally === stat : parent.bcatch === stat;
  11927. }
  11928. function in_try(level, node) {
  11929. var right = self.right;
  11930. self.right = make_node(AST_Null, right);
  11931. var may_throw = node.may_throw(compressor);
  11932. self.right = right;
  11933. for (var parent; parent = compressor.parent(level++); node = parent) {
  11934. if (parent === scope) return false;
  11935. if (parent instanceof AST_Try) {
  11936. if (parent.bfinally && parent.bfinally !== node) return true;
  11937. if (may_throw && parent.bcatch && parent.bcatch !== node) return true;
  11938. }
  11939. }
  11940. }
  11941. function strip_assignment(def) {
  11942. if (def) def.fixed = false;
  11943. return (self.operator != "=" ? make_node(AST_Binary, self, {
  11944. operator: self.operator.slice(0, -1),
  11945. left: self.left,
  11946. right: self.right,
  11947. }) : maintain_this_binding(compressor, compressor.parent(), self, self.right)).optimize(compressor);
  11948. }
  11949. });
  11950. OPT(AST_Conditional, function(self, compressor) {
  11951. if (compressor.option("sequences") && self.condition instanceof AST_Sequence) {
  11952. var expressions = self.condition.expressions.slice();
  11953. self.condition = expressions.pop();
  11954. expressions.push(self);
  11955. return make_sequence(self, expressions);
  11956. }
  11957. if (!compressor.option("conditionals")) return self;
  11958. var condition = self.condition;
  11959. if (compressor.option("booleans") && !condition.has_side_effects(compressor)) {
  11960. mark_duplicate_condition(compressor, condition);
  11961. }
  11962. condition = fuzzy_eval(compressor, condition);
  11963. if (!condition) {
  11964. AST_Node.warn("Condition always false [{file}:{line},{col}]", self.start);
  11965. return make_sequence(self, [ self.condition, self.alternative ]).optimize(compressor);
  11966. } else if (!(condition instanceof AST_Node)) {
  11967. AST_Node.warn("Condition always true [{file}:{line},{col}]", self.start);
  11968. return make_sequence(self, [ self.condition, self.consequent ]).optimize(compressor);
  11969. }
  11970. var negated = condition.negate(compressor, first_in_statement(compressor));
  11971. if (best_of(compressor, condition, negated) === negated) {
  11972. self = make_node(AST_Conditional, self, {
  11973. condition: negated,
  11974. consequent: self.alternative,
  11975. alternative: self.consequent
  11976. });
  11977. negated = condition;
  11978. condition = self.condition;
  11979. }
  11980. var consequent = self.consequent;
  11981. var alternative = self.alternative;
  11982. if (repeatable(compressor, condition)) {
  11983. // x ? x : y ---> x || y
  11984. if (condition.equivalent_to(consequent)) return make_node(AST_Binary, self, {
  11985. operator: "||",
  11986. left: condition,
  11987. right: alternative,
  11988. }).optimize(compressor);
  11989. // x ? y : x ---> x && y
  11990. if (condition.equivalent_to(alternative)) return make_node(AST_Binary, self, {
  11991. operator: "&&",
  11992. left: condition,
  11993. right: consequent,
  11994. }).optimize(compressor);
  11995. }
  11996. // if (foo) exp = something; else exp = something_else;
  11997. // |
  11998. // v
  11999. // exp = foo ? something : something_else;
  12000. var seq_tail = consequent.tail_node();
  12001. if (seq_tail instanceof AST_Assign) {
  12002. var is_eq = seq_tail.operator == "=";
  12003. var alt_tail = is_eq ? alternative.tail_node() : alternative;
  12004. if ((is_eq || consequent === seq_tail)
  12005. && alt_tail instanceof AST_Assign
  12006. && seq_tail.operator == alt_tail.operator
  12007. && seq_tail.left.equivalent_to(alt_tail.left)
  12008. && (is_eq && seq_tail.left instanceof AST_SymbolRef
  12009. || !condition.has_side_effects(compressor)
  12010. && can_shift_lhs_of_tail(consequent)
  12011. && can_shift_lhs_of_tail(alternative))) {
  12012. return make_node(AST_Assign, self, {
  12013. operator: seq_tail.operator,
  12014. left: seq_tail.left,
  12015. right: make_node(AST_Conditional, self, {
  12016. condition: condition,
  12017. consequent: pop_lhs(consequent),
  12018. alternative: pop_lhs(alternative)
  12019. })
  12020. });
  12021. }
  12022. }
  12023. // x ? y : y ---> x, y
  12024. if (consequent.equivalent_to(alternative)) return make_sequence(self, [
  12025. condition,
  12026. consequent
  12027. ]).optimize(compressor);
  12028. // x ? y.p : z.p ---> (x ? y : z).p
  12029. // x ? y(a) : z(a) ---> (x ? y : z)(a)
  12030. // x ? y.f(a) : z.f(a) ---> (x ? y : z).f(a)
  12031. var combined = combine_tail(consequent, alternative, true);
  12032. if (combined) return combined;
  12033. // x ? y(a) : y(b) ---> y(x ? a : b)
  12034. var arg_index;
  12035. if (consequent instanceof AST_Call
  12036. && alternative.TYPE == consequent.TYPE
  12037. && (arg_index = arg_diff(consequent, alternative)) >= 0
  12038. && consequent.expression.equivalent_to(alternative.expression)
  12039. && !condition.has_side_effects(compressor)
  12040. && !consequent.expression.has_side_effects(compressor)) {
  12041. var node = consequent.clone();
  12042. var arg = consequent.args[arg_index];
  12043. node.args[arg_index] = arg instanceof AST_Spread ? make_node(AST_Spread, self, {
  12044. expression: make_node(AST_Conditional, self, {
  12045. condition: condition,
  12046. consequent: arg.expression,
  12047. alternative: alternative.args[arg_index].expression,
  12048. }),
  12049. }) : make_node(AST_Conditional, self, {
  12050. condition: condition,
  12051. consequent: arg,
  12052. alternative: alternative.args[arg_index],
  12053. });
  12054. return node;
  12055. }
  12056. // x ? (y ? a : b) : b ---> x && y ? a : b
  12057. if (consequent instanceof AST_Conditional
  12058. && consequent.alternative.equivalent_to(alternative)) {
  12059. return make_node(AST_Conditional, self, {
  12060. condition: make_node(AST_Binary, self, {
  12061. left: condition,
  12062. operator: "&&",
  12063. right: consequent.condition
  12064. }),
  12065. consequent: consequent.consequent,
  12066. alternative: alternative
  12067. });
  12068. }
  12069. // x ? (y ? a : b) : a ---> !x || y ? a : b
  12070. if (consequent instanceof AST_Conditional
  12071. && consequent.consequent.equivalent_to(alternative)) {
  12072. return make_node(AST_Conditional, self, {
  12073. condition: make_node(AST_Binary, self, {
  12074. left: negated,
  12075. operator: "||",
  12076. right: consequent.condition
  12077. }),
  12078. consequent: alternative,
  12079. alternative: consequent.alternative
  12080. });
  12081. }
  12082. // x ? a : (y ? a : b) ---> x || y ? a : b
  12083. if (alternative instanceof AST_Conditional
  12084. && consequent.equivalent_to(alternative.consequent)) {
  12085. return make_node(AST_Conditional, self, {
  12086. condition: make_node(AST_Binary, self, {
  12087. left: condition,
  12088. operator: "||",
  12089. right: alternative.condition
  12090. }),
  12091. consequent: consequent,
  12092. alternative: alternative.alternative
  12093. });
  12094. }
  12095. // x ? b : (y ? a : b) ---> !x && y ? a : b
  12096. if (alternative instanceof AST_Conditional
  12097. && consequent.equivalent_to(alternative.alternative)) {
  12098. return make_node(AST_Conditional, self, {
  12099. condition: make_node(AST_Binary, self, {
  12100. left: negated,
  12101. operator: "&&",
  12102. right: alternative.condition
  12103. }),
  12104. consequent: alternative.consequent,
  12105. alternative: consequent
  12106. });
  12107. }
  12108. // x ? (a, c) : (b, c) ---> x ? a : b, c
  12109. if ((consequent instanceof AST_Sequence || alternative instanceof AST_Sequence)
  12110. && consequent.tail_node().equivalent_to(alternative.tail_node())) {
  12111. return make_sequence(self, [
  12112. make_node(AST_Conditional, self, {
  12113. condition: condition,
  12114. consequent: pop_seq(consequent),
  12115. alternative: pop_seq(alternative)
  12116. }),
  12117. consequent.tail_node()
  12118. ]).optimize(compressor);
  12119. }
  12120. // x ? y && a : a ---> (!x || y) && a
  12121. if (consequent instanceof AST_Binary
  12122. && consequent.operator == "&&"
  12123. && consequent.right.equivalent_to(alternative)) {
  12124. return make_node(AST_Binary, self, {
  12125. operator: "&&",
  12126. left: make_node(AST_Binary, self, {
  12127. operator: "||",
  12128. left: negated,
  12129. right: consequent.left
  12130. }),
  12131. right: alternative
  12132. }).optimize(compressor);
  12133. }
  12134. // x ? y || a : a ---> x && y || a
  12135. if (consequent instanceof AST_Binary
  12136. && consequent.operator == "||"
  12137. && consequent.right.equivalent_to(alternative)) {
  12138. return make_node(AST_Binary, self, {
  12139. operator: "||",
  12140. left: make_node(AST_Binary, self, {
  12141. operator: "&&",
  12142. left: condition,
  12143. right: consequent.left
  12144. }),
  12145. right: alternative
  12146. }).optimize(compressor);
  12147. }
  12148. // x ? a : y && a ---> (x || y) && a
  12149. if (alternative instanceof AST_Binary
  12150. && alternative.operator == "&&"
  12151. && alternative.right.equivalent_to(consequent)) {
  12152. return make_node(AST_Binary, self, {
  12153. operator: "&&",
  12154. left: make_node(AST_Binary, self, {
  12155. operator: "||",
  12156. left: condition,
  12157. right: alternative.left
  12158. }),
  12159. right: consequent
  12160. }).optimize(compressor);
  12161. }
  12162. // x ? a : y || a ---> !x && y || a
  12163. if (alternative instanceof AST_Binary
  12164. && alternative.operator == "||"
  12165. && alternative.right.equivalent_to(consequent)) {
  12166. return make_node(AST_Binary, self, {
  12167. operator: "||",
  12168. left: make_node(AST_Binary, self, {
  12169. operator: "&&",
  12170. left: negated,
  12171. right: alternative.left
  12172. }),
  12173. right: consequent
  12174. }).optimize(compressor);
  12175. }
  12176. var in_bool = compressor.option("booleans") && compressor.in_boolean_context();
  12177. if (is_true(consequent)) {
  12178. if (is_false(alternative)) {
  12179. // c ? true : false ---> !!c
  12180. return booleanize(condition);
  12181. }
  12182. // c ? true : x ---> !!c || x
  12183. return make_node(AST_Binary, self, {
  12184. operator: "||",
  12185. left: booleanize(condition),
  12186. right: alternative
  12187. });
  12188. }
  12189. if (is_false(consequent)) {
  12190. if (is_true(alternative)) {
  12191. // c ? false : true ---> !c
  12192. return booleanize(condition.negate(compressor));
  12193. }
  12194. // c ? false : x ---> !c && x
  12195. return make_node(AST_Binary, self, {
  12196. operator: "&&",
  12197. left: booleanize(condition.negate(compressor)),
  12198. right: alternative
  12199. });
  12200. }
  12201. if (is_true(alternative)) {
  12202. // c ? x : true ---> !c || x
  12203. return make_node(AST_Binary, self, {
  12204. operator: "||",
  12205. left: booleanize(condition.negate(compressor)),
  12206. right: consequent
  12207. });
  12208. }
  12209. if (is_false(alternative)) {
  12210. // c ? x : false ---> !!c && x
  12211. return make_node(AST_Binary, self, {
  12212. operator: "&&",
  12213. left: booleanize(condition),
  12214. right: consequent
  12215. });
  12216. }
  12217. if (compressor.option("typeofs")) mark_locally_defined(condition, consequent, alternative);
  12218. return self;
  12219. function booleanize(node) {
  12220. if (node.is_boolean(compressor)) return node;
  12221. // !!expression
  12222. return make_node(AST_UnaryPrefix, node, {
  12223. operator: "!",
  12224. expression: node.negate(compressor)
  12225. });
  12226. }
  12227. // AST_True or !0
  12228. function is_true(node) {
  12229. return node instanceof AST_True
  12230. || in_bool
  12231. && node instanceof AST_Constant
  12232. && node.value
  12233. || (node instanceof AST_UnaryPrefix
  12234. && node.operator == "!"
  12235. && node.expression instanceof AST_Constant
  12236. && !node.expression.value);
  12237. }
  12238. // AST_False or !1 or void 0
  12239. function is_false(node) {
  12240. return node instanceof AST_False
  12241. || in_bool
  12242. && (node instanceof AST_Constant
  12243. && !node.value
  12244. || node instanceof AST_UnaryPrefix
  12245. && node.operator == "void"
  12246. && !node.expression.has_side_effects(compressor))
  12247. || (node instanceof AST_UnaryPrefix
  12248. && node.operator == "!"
  12249. && node.expression instanceof AST_Constant
  12250. && node.expression.value);
  12251. }
  12252. function arg_diff(consequent, alternative) {
  12253. var a = consequent.args;
  12254. var b = alternative.args;
  12255. var len = a.length;
  12256. if (len != b.length) return -2;
  12257. for (var i = 0; i < len; i++) {
  12258. if (!a[i].equivalent_to(b[i])) {
  12259. if (a[i] instanceof AST_Spread !== b[i] instanceof AST_Spread) return -3;
  12260. for (var j = i + 1; j < len; j++) {
  12261. if (!a[j].equivalent_to(b[j])) return -2;
  12262. }
  12263. return i;
  12264. }
  12265. }
  12266. return -1;
  12267. }
  12268. function is_tail_equivalent(consequent, alternative) {
  12269. if (consequent.TYPE != alternative.TYPE) return;
  12270. if (consequent.optional != alternative.optional) return;
  12271. if (consequent instanceof AST_Call) {
  12272. if (arg_diff(consequent, alternative) != -1) return;
  12273. return consequent.TYPE != "Call"
  12274. || !(consequent.expression instanceof AST_PropAccess
  12275. || alternative.expression instanceof AST_PropAccess)
  12276. || is_tail_equivalent(consequent.expression, alternative.expression);
  12277. }
  12278. if (!(consequent instanceof AST_PropAccess)) return;
  12279. var p = consequent.property;
  12280. var q = alternative.property;
  12281. return (p instanceof AST_Node ? p.equivalent_to(q) : p == q)
  12282. && !(consequent.expression instanceof AST_Super || alternative.expression instanceof AST_Super);
  12283. }
  12284. function combine_tail(consequent, alternative, top) {
  12285. if (!is_tail_equivalent(consequent, alternative)) return !top && make_node(AST_Conditional, self, {
  12286. condition: condition,
  12287. consequent: consequent,
  12288. alternative: alternative,
  12289. });
  12290. var node = consequent.clone();
  12291. node.expression = combine_tail(consequent.expression, alternative.expression);
  12292. return node;
  12293. }
  12294. function can_shift_lhs_of_tail(node) {
  12295. return node === node.tail_node() || all(node.expressions.slice(0, -1), function(expr) {
  12296. return !expr.has_side_effects(compressor);
  12297. });
  12298. }
  12299. function pop_lhs(node) {
  12300. if (!(node instanceof AST_Sequence)) return node.right;
  12301. var exprs = node.expressions.slice();
  12302. exprs.push(exprs.pop().right);
  12303. return make_sequence(node, exprs);
  12304. }
  12305. function pop_seq(node) {
  12306. if (!(node instanceof AST_Sequence)) return make_node(AST_Number, node, { value: 0 });
  12307. return make_sequence(node, node.expressions.slice(0, -1));
  12308. }
  12309. });
  12310. OPT(AST_Boolean, function(self, compressor) {
  12311. if (!compressor.option("booleans")) return self;
  12312. if (compressor.in_boolean_context()) return make_node(AST_Number, self, { value: +self.value });
  12313. var p = compressor.parent();
  12314. if (p instanceof AST_Binary && (p.operator == "==" || p.operator == "!=")) {
  12315. AST_Node.warn("Non-strict equality against boolean: {operator} {value} [{file}:{line},{col}]", {
  12316. operator : p.operator,
  12317. value : self.value,
  12318. file : p.start.file,
  12319. line : p.start.line,
  12320. col : p.start.col,
  12321. });
  12322. return make_node(AST_Number, self, { value: +self.value });
  12323. }
  12324. return make_node(AST_UnaryPrefix, self, {
  12325. operator: "!",
  12326. expression: make_node(AST_Number, self, { value: 1 - self.value }),
  12327. });
  12328. });
  12329. OPT(AST_Spread, function(self, compressor) {
  12330. var exp = self.expression;
  12331. if (compressor.option("spreads") && exp instanceof AST_Array && !(compressor.parent() instanceof AST_Object)) {
  12332. return List.splice(exp.elements.map(function(node) {
  12333. return node instanceof AST_Hole ? make_node(AST_Undefined, node).optimize(compressor) : node;
  12334. }));
  12335. }
  12336. return self;
  12337. });
  12338. function safe_to_flatten(value, compressor) {
  12339. if (!value) return false;
  12340. var parent = compressor.parent();
  12341. if (parent.TYPE != "Call") return true;
  12342. if (parent.expression !== compressor.self()) return true;
  12343. if (value instanceof AST_SymbolRef) {
  12344. value = value.fixed_value();
  12345. if (!value) return false;
  12346. }
  12347. return value instanceof AST_Lambda && !value.contains_this();
  12348. }
  12349. OPT(AST_Sub, function(self, compressor) {
  12350. var expr = self.expression;
  12351. var prop = self.property;
  12352. var terminated = trim_optional_chain(self, compressor);
  12353. if (terminated) return terminated;
  12354. if (compressor.option("properties")) {
  12355. var key = prop.evaluate(compressor);
  12356. if (key !== prop) {
  12357. if (typeof key == "string") {
  12358. if (key == "undefined") {
  12359. key = undefined;
  12360. } else {
  12361. var value = parseFloat(key);
  12362. if (value.toString() == key) {
  12363. key = value;
  12364. }
  12365. }
  12366. }
  12367. prop = self.property = best_of_expression(prop, make_node_from_constant(key, prop).transform(compressor));
  12368. var property = "" + key;
  12369. if (is_identifier_string(property)
  12370. && property.length <= prop.print_to_string().length + 1) {
  12371. return make_node(AST_Dot, self, {
  12372. optional: self.optional,
  12373. expression: expr,
  12374. property: property,
  12375. }).optimize(compressor);
  12376. }
  12377. }
  12378. }
  12379. var parent = compressor.parent();
  12380. var assigned = is_lhs(compressor.self(), parent);
  12381. var def, fn, fn_parent, index;
  12382. if (compressor.option("arguments")
  12383. && expr instanceof AST_SymbolRef
  12384. && is_arguments(def = expr.definition())
  12385. && !expr.in_arg
  12386. && prop instanceof AST_Number
  12387. && Math.floor(index = prop.value) == index
  12388. && (fn = def.scope) === find_lambda()
  12389. && fn.uses_arguments < (assigned ? 2 : 3)) {
  12390. if (parent instanceof AST_UnaryPrefix && parent.operator == "delete") {
  12391. if (!def.deleted) def.deleted = [];
  12392. def.deleted[index] = true;
  12393. }
  12394. var argname = fn.argnames[index];
  12395. if (def.deleted && def.deleted[index]) {
  12396. argname = null;
  12397. } else if (argname) {
  12398. var arg_def;
  12399. if (!(argname instanceof AST_SymbolFunarg)
  12400. || argname.name == "await"
  12401. || expr.scope.find_variable(argname.name) !== (arg_def = argname.definition())) {
  12402. argname = null;
  12403. } else if (compressor.has_directive("use strict")
  12404. || fn.name
  12405. || fn.rest
  12406. || !(fn_parent instanceof AST_Call
  12407. && index < fn_parent.args.length
  12408. && all(fn_parent.args.slice(0, index + 1), function(arg) {
  12409. return !(arg instanceof AST_Spread);
  12410. }))
  12411. || !all(fn.argnames, function(argname) {
  12412. return argname instanceof AST_SymbolFunarg;
  12413. })) {
  12414. if (has_reassigned() || arg_def.assignments || arg_def.orig.length > 1) argname = null;
  12415. }
  12416. } else if ((assigned || !has_reassigned())
  12417. && index < fn.argnames.length + 5
  12418. && compressor.drop_fargs(fn, fn_parent)
  12419. && !fn.rest) {
  12420. while (index >= fn.argnames.length) {
  12421. argname = fn.make_var(AST_SymbolFunarg, fn, "argument_" + fn.argnames.length);
  12422. fn.argnames.push(argname);
  12423. }
  12424. }
  12425. if (argname && find_if(function(node) {
  12426. return node.name === argname.name;
  12427. }, fn.argnames) === argname) {
  12428. if (assigned) def.reassigned--;
  12429. var sym = make_node(AST_SymbolRef, self, argname);
  12430. sym.reference();
  12431. argname.unused = undefined;
  12432. return sym;
  12433. }
  12434. }
  12435. if (assigned) return self;
  12436. if (compressor.option("sequences")
  12437. && parent.TYPE != "Call"
  12438. && !(parent instanceof AST_ForEnumeration && parent.init === self)) {
  12439. var seq = lift_sequence_in_expression(self, compressor);
  12440. if (seq !== self) return seq.optimize(compressor);
  12441. }
  12442. if (key !== prop) {
  12443. var sub = self.flatten_object(property, compressor);
  12444. if (sub) {
  12445. expr = self.expression = sub.expression;
  12446. prop = self.property = sub.property;
  12447. }
  12448. }
  12449. var elements;
  12450. if (compressor.option("properties")
  12451. && compressor.option("side_effects")
  12452. && prop instanceof AST_Number
  12453. && expr instanceof AST_Array
  12454. && all(elements = expr.elements, function(value) {
  12455. return !(value instanceof AST_Spread);
  12456. })) {
  12457. var index = prop.value;
  12458. var retValue = elements[index];
  12459. if (safe_to_flatten(retValue, compressor)) {
  12460. var is_hole = retValue instanceof AST_Hole;
  12461. var flatten = !is_hole;
  12462. var values = [];
  12463. for (var i = elements.length; --i > index;) {
  12464. var value = elements[i].drop_side_effect_free(compressor);
  12465. if (value) {
  12466. values.unshift(value);
  12467. if (flatten && value.has_side_effects(compressor)) flatten = false;
  12468. }
  12469. }
  12470. if (!flatten) values.unshift(retValue);
  12471. while (--i >= 0) {
  12472. var value = elements[i].drop_side_effect_free(compressor);
  12473. if (value) {
  12474. values.unshift(value);
  12475. } else if (is_hole) {
  12476. values.unshift(make_node(AST_Hole, elements[i]));
  12477. } else {
  12478. index--;
  12479. }
  12480. }
  12481. if (flatten) {
  12482. values.push(retValue);
  12483. return make_sequence(self, values).optimize(compressor);
  12484. }
  12485. return make_node(AST_Sub, self, {
  12486. expression: make_node(AST_Array, expr, { elements: values }),
  12487. property: make_node(AST_Number, prop, { value: index }),
  12488. });
  12489. }
  12490. }
  12491. return try_evaluate(compressor, self);
  12492. function find_lambda() {
  12493. var i = 0, p;
  12494. while (p = compressor.parent(i++)) {
  12495. if (p instanceof AST_Lambda) {
  12496. if (p instanceof AST_Accessor) return;
  12497. if (is_arrow(p)) continue;
  12498. fn_parent = compressor.parent(i);
  12499. return p;
  12500. }
  12501. }
  12502. }
  12503. function has_reassigned() {
  12504. return !compressor.option("reduce_vars") || def.reassigned;
  12505. }
  12506. });
  12507. AST_LambdaExpression.DEFMETHOD("contains_super", function() {
  12508. var result = false;
  12509. var self = this;
  12510. self.walk(new TreeWalker(function(node) {
  12511. if (result) return true;
  12512. if (node instanceof AST_Super) return result = true;
  12513. if (node !== self && node instanceof AST_Scope && !is_arrow(node)) return true;
  12514. }));
  12515. return result;
  12516. });
  12517. // contains_this()
  12518. // returns false only if context bound by the specified scope (or scope
  12519. // containing the specified expression) is not referenced by `this`
  12520. (function(def) {
  12521. // scope of arrow function cannot bind to any context
  12522. def(AST_Arrow, return_false);
  12523. def(AST_AsyncArrow, return_false);
  12524. def(AST_Node, function() {
  12525. var result = false;
  12526. var self = this;
  12527. self.walk(new TreeWalker(function(node) {
  12528. if (result) return true;
  12529. if (node instanceof AST_This) return result = true;
  12530. if (node !== self && node instanceof AST_Scope && !is_arrow(node)) return true;
  12531. }));
  12532. return result;
  12533. });
  12534. })(function(node, func) {
  12535. node.DEFMETHOD("contains_this", func);
  12536. });
  12537. function can_hoist_property(prop) {
  12538. return prop instanceof AST_ObjectKeyVal
  12539. && typeof prop.key == "string"
  12540. && !(prop instanceof AST_ObjectMethod && prop.value.contains_super());
  12541. }
  12542. AST_PropAccess.DEFMETHOD("flatten_object", function(key, compressor) {
  12543. if (!compressor.option("properties")) return;
  12544. if (key === "__proto__") return;
  12545. var self = this;
  12546. var expr = self.expression;
  12547. if (!(expr instanceof AST_Object)) return;
  12548. var props = expr.properties;
  12549. for (var i = props.length; --i >= 0;) {
  12550. var prop = props[i];
  12551. if (prop.key !== key) continue;
  12552. if (!all(props, can_hoist_property)) return;
  12553. if (!safe_to_flatten(prop.value, compressor)) return;
  12554. var call, scope, values = [];
  12555. for (var j = 0; j < props.length; j++) {
  12556. var value = props[j].value;
  12557. if (props[j] instanceof AST_ObjectMethod) {
  12558. var arrow = !(value.uses_arguments || is_generator(value) || value.contains_this());
  12559. if (arrow) {
  12560. if (!scope) scope = compressor.find_parent(AST_Scope);
  12561. var avoid = avoid_await_yield(scope);
  12562. value.each_argname(function(argname) {
  12563. if (avoid[argname.name]) arrow = false;
  12564. });
  12565. }
  12566. var ctor;
  12567. if (arrow) {
  12568. ctor = is_async(value) ? AST_AsyncArrow : AST_Arrow;
  12569. } else if (i != j
  12570. || (call = compressor.parent()) instanceof AST_Call && call.expression === self) {
  12571. ctor = value.CTOR;
  12572. } else {
  12573. return;
  12574. }
  12575. value = make_node(ctor, value, value);
  12576. }
  12577. values.push(value);
  12578. }
  12579. return make_node(AST_Sub, self, {
  12580. expression: make_node(AST_Array, expr, { elements: values }),
  12581. property: make_node(AST_Number, self, { value: i }),
  12582. });
  12583. }
  12584. });
  12585. OPT(AST_Dot, function(self, compressor) {
  12586. if (self.property == "arguments" || self.property == "caller") {
  12587. AST_Node.warn("Function.prototype.{prop} not supported [{file}:{line},{col}]", {
  12588. prop: self.property,
  12589. file: self.start.file,
  12590. line: self.start.line,
  12591. col: self.start.col,
  12592. });
  12593. }
  12594. var parent = compressor.parent();
  12595. if (is_lhs(compressor.self(), parent)) return self;
  12596. var terminated = trim_optional_chain(self, compressor);
  12597. if (terminated) return terminated;
  12598. if (compressor.option("sequences")
  12599. && parent.TYPE != "Call"
  12600. && !(parent instanceof AST_ForEnumeration && parent.init === self)) {
  12601. var seq = lift_sequence_in_expression(self, compressor);
  12602. if (seq !== self) return seq.optimize(compressor);
  12603. }
  12604. if (compressor.option("unsafe_proto")
  12605. && self.expression instanceof AST_Dot
  12606. && self.expression.property == "prototype") {
  12607. var exp = self.expression.expression;
  12608. if (is_undeclared_ref(exp)) switch (exp.name) {
  12609. case "Array":
  12610. self.expression = make_node(AST_Array, self.expression, { elements: [] });
  12611. break;
  12612. case "Function":
  12613. self.expression = make_node(AST_Function, self.expression, {
  12614. argnames: [],
  12615. body: [],
  12616. }).init_vars(exp.scope);
  12617. break;
  12618. case "Number":
  12619. self.expression = make_node(AST_Number, self.expression, { value: 0 });
  12620. break;
  12621. case "Object":
  12622. self.expression = make_node(AST_Object, self.expression, { properties: [] });
  12623. break;
  12624. case "RegExp":
  12625. self.expression = make_node(AST_RegExp, self.expression, { value: /t/ });
  12626. break;
  12627. case "String":
  12628. self.expression = make_node(AST_String, self.expression, { value: "" });
  12629. break;
  12630. }
  12631. }
  12632. var sub = self.flatten_object(self.property, compressor);
  12633. if (sub) return sub.optimize(compressor);
  12634. return try_evaluate(compressor, self);
  12635. });
  12636. OPT(AST_DestructuredArray, function(self, compressor) {
  12637. if (compressor.option("rests") && self.rest instanceof AST_DestructuredArray) {
  12638. return make_node(AST_DestructuredArray, self, {
  12639. elements: self.elements.concat(self.rest.elements),
  12640. rest: self.rest.rest,
  12641. });
  12642. }
  12643. return self;
  12644. });
  12645. OPT(AST_DestructuredKeyVal, function(self, compressor) {
  12646. if (compressor.option("objects")) {
  12647. var key = self.key;
  12648. if (key instanceof AST_Node) {
  12649. key = key.evaluate(compressor);
  12650. if (key !== self.key) self.key = "" + key;
  12651. }
  12652. }
  12653. return self;
  12654. });
  12655. OPT(AST_Object, function(self, compressor) {
  12656. if (!compressor.option("objects")) return self;
  12657. var changed = false;
  12658. var found = false;
  12659. var generated = false;
  12660. var keep_duplicate = compressor.has_directive("use strict");
  12661. var keys = [];
  12662. var map = new Dictionary();
  12663. var values = [];
  12664. self.properties.forEach(function(prop) {
  12665. if (!(prop instanceof AST_Spread)) return process(prop);
  12666. found = true;
  12667. var exp = prop.expression;
  12668. if (compressor.option("spreads") && exp instanceof AST_Object && all(exp.properties, function(prop) {
  12669. if (prop instanceof AST_ObjectGetter) return false;
  12670. if (prop instanceof AST_Spread) return false;
  12671. if (prop.key !== "__proto__") return true;
  12672. if (prop instanceof AST_ObjectSetter) return true;
  12673. return !prop.value.has_side_effects(compressor);
  12674. })) {
  12675. changed = true;
  12676. exp.properties.forEach(function(prop) {
  12677. var key = prop.key;
  12678. var setter = prop instanceof AST_ObjectSetter;
  12679. if (key === "__proto__") {
  12680. if (!setter) return;
  12681. key = make_node_from_constant(key, prop);
  12682. }
  12683. process(setter ? make_node(AST_ObjectKeyVal, prop, {
  12684. key: key,
  12685. value: make_node(AST_Undefined, prop).optimize(compressor),
  12686. }) : prop);
  12687. });
  12688. } else {
  12689. generated = true;
  12690. flush();
  12691. values.push(prop);
  12692. }
  12693. });
  12694. flush();
  12695. if (!changed) return self;
  12696. if (found && generated && values.length == 1) {
  12697. var value = values[0];
  12698. if (value instanceof AST_ObjectProperty && value.key instanceof AST_Number) {
  12699. value.key = "" + value.key.value;
  12700. }
  12701. }
  12702. return make_node(AST_Object, self, { properties: values });
  12703. function flush() {
  12704. keys.forEach(function(key) {
  12705. var props = map.get(key);
  12706. switch (props.length) {
  12707. case 0:
  12708. return;
  12709. case 1:
  12710. return values.push(props[0]);
  12711. }
  12712. changed = true;
  12713. var tail = keep_duplicate && !generated && props.pop();
  12714. values.push(props.length == 1 ? props[0] : make_node(AST_ObjectKeyVal, self, {
  12715. key: props[0].key,
  12716. value: make_sequence(self, props.map(function(prop) {
  12717. return prop.value;
  12718. })),
  12719. }));
  12720. if (tail) values.push(tail);
  12721. props.length = 0;
  12722. });
  12723. keys = [];
  12724. map = new Dictionary();
  12725. }
  12726. function process(prop) {
  12727. var key = prop.key;
  12728. if (key instanceof AST_Node) {
  12729. found = true;
  12730. key = key.evaluate(compressor);
  12731. if (key === prop.key || key === "__proto__") {
  12732. generated = true;
  12733. } else {
  12734. key = prop.key = "" + key;
  12735. }
  12736. }
  12737. if (can_hoist_property(prop)) {
  12738. if (prop.value.has_side_effects(compressor)) flush();
  12739. keys.push(key);
  12740. map.add(key, prop);
  12741. } else {
  12742. flush();
  12743. values.push(prop);
  12744. }
  12745. if (found && !generated && typeof key == "string" && RE_POSITIVE_INTEGER.test(key)) {
  12746. generated = true;
  12747. if (map.has(key)) prop = map.get(key)[0];
  12748. prop.key = make_node(AST_Number, prop, { value: +key });
  12749. }
  12750. }
  12751. });
  12752. function flatten_var(name) {
  12753. var redef = name.definition().redefined();
  12754. if (redef) {
  12755. name = name.clone();
  12756. name.thedef = redef;
  12757. }
  12758. return name;
  12759. }
  12760. function has_arg_refs(fn, node) {
  12761. var found = false;
  12762. node.walk(new TreeWalker(function(node) {
  12763. if (found) return true;
  12764. if (node instanceof AST_SymbolRef && fn.variables.get(node.name) === node.definition()) {
  12765. return found = true;
  12766. }
  12767. }));
  12768. return found;
  12769. }
  12770. function insert_assign(def, assign) {
  12771. var visited = [];
  12772. def.references.forEach(function(ref) {
  12773. var fixed = ref.fixed;
  12774. if (!fixed || !push_uniq(visited, fixed)) return;
  12775. if (fixed.assigns) {
  12776. fixed.assigns.unshift(assign);
  12777. } else {
  12778. fixed.assigns = [ assign ];
  12779. }
  12780. });
  12781. }
  12782. function init_ref(compressor, name) {
  12783. var sym = make_node(AST_SymbolRef, name, name);
  12784. var assign = make_node(AST_Assign, name, {
  12785. operator: "=",
  12786. left: sym,
  12787. right: make_node(AST_Undefined, name).transform(compressor),
  12788. });
  12789. var def = name.definition();
  12790. if (def.fixed) {
  12791. sym.fixed = function() {
  12792. return assign.right;
  12793. };
  12794. sym.fixed.assigns = [ assign ];
  12795. insert_assign(def, assign);
  12796. }
  12797. def.assignments++;
  12798. def.references.push(sym);
  12799. return assign;
  12800. }
  12801. (function(def) {
  12802. def(AST_Node, noop);
  12803. def(AST_Assign, noop);
  12804. def(AST_Await, function(compressor, scope, no_return, in_loop) {
  12805. var self = this;
  12806. var inlined = sync(self.expression).try_inline(compressor, scope, no_return, in_loop);
  12807. if (!inlined) return;
  12808. if (!no_return) scan_local_returns(inlined, function(node) {
  12809. node.in_bool = false;
  12810. var value = node.value;
  12811. if (value instanceof AST_Await) return;
  12812. node.value = make_node(AST_Await, self, {
  12813. expression: value || make_node(AST_Undefined, node).transform(compressor),
  12814. });
  12815. });
  12816. return aborts(inlined) ? inlined : make_node(AST_BlockStatement, self, {
  12817. body: [ inlined, make_node(AST_SimpleStatement, self, {
  12818. body: make_node(AST_Await, self, { expression: make_node(AST_Number, self, { value: 0 })}),
  12819. }) ],
  12820. });
  12821. function sync(node) {
  12822. if (!no_return) return node;
  12823. if (node.TYPE != "Call") return node;
  12824. var fn = node.expression;
  12825. switch (fn.CTOR) {
  12826. case AST_AsyncArrow:
  12827. fn = make_node(AST_Arrow, fn, fn);
  12828. break;
  12829. case AST_AsyncFunction:
  12830. fn = make_node(AST_Function, fn, fn);
  12831. break;
  12832. case AST_AsyncGeneratorFunction:
  12833. fn = make_node(AST_GeneratorFunction, fn, fn);
  12834. break;
  12835. default:
  12836. return node;
  12837. }
  12838. node = node.clone();
  12839. node.expression = fn;
  12840. return node;
  12841. }
  12842. });
  12843. def(AST_Binary, function(compressor, scope, no_return, in_loop) {
  12844. if (no_return === undefined) return;
  12845. var self = this;
  12846. var op = self.operator;
  12847. if (!lazy_op[op]) return;
  12848. var inlined = self.right.try_inline(compressor, scope, no_return, in_loop);
  12849. if (!inlined) return;
  12850. return make_node(AST_If, self, {
  12851. condition: make_condition(self.left),
  12852. body: inlined,
  12853. alternative: no_return ? null : make_node(AST_Return, self, { value: null }),
  12854. });
  12855. function make_condition(cond) {
  12856. switch (op) {
  12857. case "&&":
  12858. return cond;
  12859. case "||":
  12860. return cond.negate(compressor);
  12861. case "??":
  12862. return make_node(AST_Binary, self, {
  12863. operator: "==",
  12864. left: make_node(AST_Null, self),
  12865. right: cond,
  12866. });
  12867. }
  12868. }
  12869. });
  12870. def(AST_BlockStatement, function(compressor, scope, no_return, in_loop) {
  12871. if (no_return) return;
  12872. if (!this.variables) return;
  12873. var body = this.body;
  12874. var last = body.length - 1;
  12875. if (last < 0) return;
  12876. var inlined = body[last].try_inline(compressor, this, no_return, in_loop);
  12877. if (!inlined) return;
  12878. body[last] = inlined;
  12879. return this;
  12880. });
  12881. def(AST_Call, function(compressor, scope, no_return, in_loop) {
  12882. if (compressor.option("inline") < 4) return;
  12883. var call = this;
  12884. if (call.is_expr_pure(compressor)) return;
  12885. var fn = call.expression;
  12886. if (!(fn instanceof AST_LambdaExpression)) return;
  12887. if (fn.name) return;
  12888. if (fn.uses_arguments) return;
  12889. if (fn.pinned()) return;
  12890. if (is_generator(fn)) return;
  12891. var arrow = is_arrow(fn);
  12892. if (arrow && fn.value) return;
  12893. if (fn.body[0] instanceof AST_Directive) return;
  12894. if (fn.contains_this()) return;
  12895. if (!scope) scope = find_scope(compressor);
  12896. if (in_async_generator(scope)) return;
  12897. var defined = new Dictionary();
  12898. defined.set("NaN", true);
  12899. while (!(scope instanceof AST_Scope)) {
  12900. scope.variables.each(function(def) {
  12901. defined.set(def.name, true);
  12902. });
  12903. scope = scope.parent_scope;
  12904. }
  12905. if (!member(scope, compressor.stack)) return;
  12906. if (scope.pinned() && fn.variables.size() > (arrow ? 0 : 1)) return;
  12907. if (scope instanceof AST_Toplevel) {
  12908. if (fn.variables.size() > (arrow ? 0 : 1)) {
  12909. if (!compressor.toplevel.vars) return;
  12910. if (fn.functions.size() > 0 && !compressor.toplevel.funcs) return;
  12911. }
  12912. defined.set("arguments", true);
  12913. }
  12914. var async = is_async(fn);
  12915. if (async) {
  12916. if (!compressor.option("awaits")) return;
  12917. if (!is_async(scope)) return;
  12918. if (call.may_throw(compressor)) return;
  12919. }
  12920. var names = scope.var_names();
  12921. if (in_loop) in_loop = [];
  12922. if (!fn.variables.all(function(def, name) {
  12923. if (in_loop) in_loop.push(def);
  12924. if (!defined.has(name) && !names.has(name)) return true;
  12925. return !arrow && name == "arguments" && def.orig.length == 1;
  12926. })) return;
  12927. if (in_loop && in_loop.length > 0 && is_reachable(fn, in_loop)) return;
  12928. var simple_argnames = true;
  12929. if (!all(fn.argnames, function(argname) {
  12930. var abort = false;
  12931. var tw = new TreeWalker(function(node) {
  12932. if (abort) return true;
  12933. if (node instanceof AST_DefaultValue) {
  12934. if (has_arg_refs(fn, node.value)) return abort = true;
  12935. node.name.walk(tw);
  12936. return true;
  12937. }
  12938. if (node instanceof AST_DestructuredKeyVal) {
  12939. if (node.key instanceof AST_Node && has_arg_refs(fn, node.key)) return abort = true;
  12940. node.value.walk(tw);
  12941. return true;
  12942. }
  12943. if (node instanceof AST_SymbolFunarg && !all(node.definition().orig, function(sym) {
  12944. return !(sym instanceof AST_SymbolDefun);
  12945. })) return abort = true;
  12946. });
  12947. argname.walk(tw);
  12948. if (abort) return false;
  12949. if (!(argname instanceof AST_SymbolFunarg)) simple_argnames = false;
  12950. return true;
  12951. })) return;
  12952. if (fn.rest) {
  12953. if (has_arg_refs(fn, fn.rest)) return;
  12954. simple_argnames = false;
  12955. }
  12956. if (no_return && !all(fn.body, function(stat) {
  12957. var abort = false;
  12958. stat.walk(new TreeWalker(function(node) {
  12959. if (abort) return true;
  12960. if (async && node instanceof AST_Await || node instanceof AST_Return) return abort = true;
  12961. if (node instanceof AST_Scope && node !== fn) return true;
  12962. }));
  12963. return !abort;
  12964. })) return;
  12965. if (!safe_from_await_yield(fn, avoid_await_yield(scope))) return;
  12966. fn.functions.each(function(def, name) {
  12967. scope.functions.set(name, def);
  12968. });
  12969. var body = [];
  12970. fn.variables.each(function(def, name) {
  12971. if (name == "arguments") return;
  12972. names.set(name, true);
  12973. scope.enclosed.push(def);
  12974. scope.variables.set(name, def);
  12975. def.single_use = false;
  12976. if (!in_loop) return;
  12977. if (def.references.length == def.replaced) return;
  12978. if (def.orig.length == def.eliminated) return;
  12979. if (def.orig.length == 1 && fn.functions.has(name)) return;
  12980. if (!all(def.orig, function(sym) {
  12981. if (sym instanceof AST_SymbolConst) return false;
  12982. if (sym instanceof AST_SymbolFunarg) return def.scope.resolve() !== fn;
  12983. if (sym instanceof AST_SymbolLet) return false;
  12984. return true;
  12985. })) return;
  12986. var sym = def.orig[0];
  12987. if (sym instanceof AST_SymbolCatch) return;
  12988. body.push(make_node(AST_SimpleStatement, sym, { body: init_ref(compressor, flatten_var(sym)) }));
  12989. });
  12990. var defs = Object.create(null), syms = new Dictionary();
  12991. if (simple_argnames && all(call.args, function(arg) {
  12992. return !(arg instanceof AST_Spread);
  12993. })) {
  12994. var values = call.args.slice();
  12995. fn.argnames.forEach(function(argname) {
  12996. var value = values.shift();
  12997. if (argname.unused) {
  12998. if (value) body.push(make_node(AST_SimpleStatement, call, { body: value }));
  12999. return;
  13000. }
  13001. var defn = make_node(AST_VarDef, call, {
  13002. name: argname.convert_symbol(AST_SymbolVar, process),
  13003. value: value || make_node(AST_Undefined, call).transform(compressor),
  13004. });
  13005. if (argname instanceof AST_SymbolFunarg) insert_assign(argname.definition(), defn);
  13006. body.push(make_node(AST_Var, call, { definitions: [ defn ] }));
  13007. });
  13008. if (values.length) body.push(make_node(AST_SimpleStatement, call, {
  13009. body: make_sequence(call, values),
  13010. }));
  13011. } else {
  13012. body.push(make_node(AST_Var, call, {
  13013. definitions: [ make_node(AST_VarDef, call, {
  13014. name: make_node(AST_DestructuredArray, call, {
  13015. elements: fn.argnames.map(function(argname) {
  13016. if (argname.unused) return make_node(AST_Hole, argname);
  13017. return argname.convert_symbol(AST_SymbolVar, process);
  13018. }),
  13019. rest: fn.rest && fn.rest.convert_symbol(AST_SymbolVar, process),
  13020. }),
  13021. value: make_node(AST_Array, call, { elements: call.args.slice() }),
  13022. }) ],
  13023. }));
  13024. }
  13025. syms.each(function(orig, id) {
  13026. var def = defs[id];
  13027. [].unshift.apply(def.orig, orig);
  13028. def.eliminated += orig.length;
  13029. });
  13030. [].push.apply(body, in_loop ? fn.body.filter(function(stat) {
  13031. if (!(stat instanceof AST_LambdaDefinition)) return true;
  13032. var name = make_node(AST_SymbolVar, stat.name, flatten_var(stat.name));
  13033. var def = name.definition();
  13034. def.fixed = false;
  13035. def.orig.push(name);
  13036. def.eliminated++;
  13037. body.push(make_node(AST_Var, stat, {
  13038. definitions: [ make_node(AST_VarDef, stat, {
  13039. name: name,
  13040. value: to_func_expr(stat, true),
  13041. }) ],
  13042. }));
  13043. return false;
  13044. }) : fn.body);
  13045. var inlined = make_node(AST_BlockStatement, call, { body: body });
  13046. if (!no_return) {
  13047. if (async) scan_local_returns(inlined, function(node) {
  13048. var value = node.value;
  13049. if (is_undefined(value)) return;
  13050. node.value = make_node(AST_Await, call, { expression: value });
  13051. });
  13052. body.push(make_node(AST_Return, call, { value: null }));
  13053. }
  13054. return inlined;
  13055. function process(sym, argname) {
  13056. var def = argname.definition();
  13057. defs[def.id] = def;
  13058. syms.add(def.id, sym);
  13059. }
  13060. });
  13061. def(AST_Conditional, function(compressor, scope, no_return, in_loop) {
  13062. var self = this;
  13063. var body = self.consequent.try_inline(compressor, scope, no_return, in_loop);
  13064. var alt = self.alternative.try_inline(compressor, scope, no_return, in_loop);
  13065. if (!body && !alt) return;
  13066. return make_node(AST_If, self, {
  13067. condition: self.condition,
  13068. body: body || make_body(self.consequent),
  13069. alternative: alt || make_body(self.alternative),
  13070. });
  13071. function make_body(value) {
  13072. if (no_return) return make_node(AST_SimpleStatement, value, { body: value });
  13073. return make_node(AST_Return, value, { value: value });
  13074. }
  13075. });
  13076. def(AST_For, function(compressor, scope, no_return, in_loop) {
  13077. var body = this.body.try_inline(compressor, scope, true, true);
  13078. if (body) this.body = body;
  13079. var inlined = this.init;
  13080. if (inlined) {
  13081. inlined = inlined.try_inline(compressor, scope, true, in_loop);
  13082. if (inlined) {
  13083. this.init = null;
  13084. if (inlined instanceof AST_BlockStatement) {
  13085. inlined.body.push(this);
  13086. return inlined;
  13087. }
  13088. return make_node(AST_BlockStatement, inlined, { body: [ inlined, this ] });
  13089. }
  13090. }
  13091. return body && this;
  13092. });
  13093. def(AST_ForEnumeration, function(compressor, scope, no_return, in_loop) {
  13094. var body = this.body.try_inline(compressor, scope, true, true);
  13095. if (body) this.body = body;
  13096. var obj = this.object;
  13097. if (obj instanceof AST_Sequence) {
  13098. var inlined = inline_sequence(compressor, scope, true, in_loop, obj, 1);
  13099. if (inlined) {
  13100. this.object = obj.tail_node();
  13101. inlined.body.push(this);
  13102. return inlined;
  13103. }
  13104. }
  13105. return body && this;
  13106. });
  13107. def(AST_If, function(compressor, scope, no_return, in_loop) {
  13108. var body = this.body.try_inline(compressor, scope, no_return, in_loop);
  13109. if (body) this.body = body;
  13110. var alt = this.alternative;
  13111. if (alt) {
  13112. alt = alt.try_inline(compressor, scope, no_return, in_loop);
  13113. if (alt) this.alternative = alt;
  13114. }
  13115. var cond = this.condition;
  13116. if (cond instanceof AST_Sequence) {
  13117. var inlined = inline_sequence(compressor, scope, true, in_loop, cond, 1);
  13118. if (inlined) {
  13119. this.condition = cond.tail_node();
  13120. inlined.body.push(this);
  13121. return inlined;
  13122. }
  13123. }
  13124. return (body || alt) && this;
  13125. });
  13126. def(AST_IterationStatement, function(compressor, scope, no_return, in_loop) {
  13127. var body = this.body.try_inline(compressor, scope, true, true);
  13128. if (!body) return;
  13129. this.body = body;
  13130. return this;
  13131. });
  13132. def(AST_LabeledStatement, function(compressor, scope, no_return, in_loop) {
  13133. var body = this.body.try_inline(compressor, scope, no_return, in_loop);
  13134. if (!body) return;
  13135. if (this.body instanceof AST_IterationStatement && body instanceof AST_BlockStatement) {
  13136. var loop = body.body.pop();
  13137. this.body = loop;
  13138. body.body.push(this);
  13139. return body;
  13140. }
  13141. this.body = body;
  13142. return this;
  13143. });
  13144. def(AST_New, noop);
  13145. def(AST_Return, function(compressor, scope, no_return, in_loop) {
  13146. var value = this.value;
  13147. return value && value.try_inline(compressor, scope, undefined, in_loop === "try");
  13148. });
  13149. function inline_sequence(compressor, scope, no_return, in_loop, node, skip) {
  13150. var body = [], exprs = node.expressions, no_ret = no_return;
  13151. for (var i = exprs.length - (skip || 0), j = i; --i >= 0; no_ret = true) {
  13152. var inlined = exprs[i].try_inline(compressor, scope, no_ret, in_loop);
  13153. if (!inlined) continue;
  13154. flush();
  13155. body.push(inlined);
  13156. }
  13157. if (body.length == 0) return;
  13158. flush();
  13159. if (!no_return && body[0] instanceof AST_SimpleStatement) {
  13160. body[0] = make_node(AST_Return, node, { value: body[0].body });
  13161. }
  13162. return make_node(AST_BlockStatement, node, { body: body.reverse() });
  13163. function flush() {
  13164. if (j > i + 1) body.push(make_node(AST_SimpleStatement, node, {
  13165. body: make_sequence(node, exprs.slice(i + 1, j)),
  13166. }));
  13167. j = i;
  13168. }
  13169. }
  13170. def(AST_Sequence, function(compressor, scope, no_return, in_loop) {
  13171. return inline_sequence(compressor, scope, no_return, in_loop, this);
  13172. });
  13173. def(AST_SimpleStatement, function(compressor, scope, no_return, in_loop) {
  13174. var body = this.body;
  13175. while (body instanceof AST_UnaryPrefix) {
  13176. var op = body.operator;
  13177. if (unary_side_effects[op]) break;
  13178. if (op == "void") break;
  13179. body = body.expression;
  13180. }
  13181. if (!no_return && !is_undefined(body)) body = make_node(AST_UnaryPrefix, this, {
  13182. operator: "void",
  13183. expression: body,
  13184. });
  13185. return body.try_inline(compressor, scope, no_return || false, in_loop);
  13186. });
  13187. def(AST_UnaryPrefix, function(compressor, scope, no_return, in_loop) {
  13188. var self = this;
  13189. var op = self.operator;
  13190. if (unary_side_effects[op]) return;
  13191. if (!no_return && op == "void") no_return = false;
  13192. var inlined = self.expression.try_inline(compressor, scope, no_return, in_loop);
  13193. if (!inlined) return;
  13194. if (!no_return) scan_local_returns(inlined, function(node) {
  13195. node.in_bool = false;
  13196. var value = node.value;
  13197. if (op == "void" && is_undefined(value)) return;
  13198. node.value = make_node(AST_UnaryPrefix, self, {
  13199. operator: op,
  13200. expression: value || make_node(AST_Undefined, node).transform(compressor),
  13201. });
  13202. });
  13203. return inlined;
  13204. });
  13205. def(AST_With, function(compressor, scope, no_return, in_loop) {
  13206. var body = this.body.try_inline(compressor, scope, no_return, in_loop);
  13207. if (body) this.body = body;
  13208. var exp = this.expression;
  13209. if (exp instanceof AST_Sequence) {
  13210. var inlined = inline_sequence(compressor, scope, true, in_loop, exp, 1);
  13211. if (inlined) {
  13212. this.expression = exp.tail_node();
  13213. inlined.body.push(this);
  13214. return inlined;
  13215. }
  13216. }
  13217. return body && this;
  13218. });
  13219. def(AST_Yield, function(compressor, scope, no_return, in_loop) {
  13220. if (!compressor.option("yields")) return;
  13221. if (!this.nested) return;
  13222. var call = this.expression;
  13223. if (call.TYPE != "Call") return;
  13224. var fn = call.expression;
  13225. switch (fn.CTOR) {
  13226. case AST_AsyncGeneratorFunction:
  13227. fn = make_node(AST_AsyncFunction, fn, fn);
  13228. break;
  13229. case AST_GeneratorFunction:
  13230. fn = make_node(AST_Function, fn, fn);
  13231. break;
  13232. default:
  13233. return;
  13234. }
  13235. call = call.clone();
  13236. call.expression = fn;
  13237. return call.try_inline(compressor, scope, no_return, in_loop);
  13238. });
  13239. })(function(node, func) {
  13240. node.DEFMETHOD("try_inline", func);
  13241. });
  13242. OPT(AST_Return, function(self, compressor) {
  13243. var value = self.value;
  13244. if (value && compressor.option("side_effects")
  13245. && is_undefined(value, compressor)
  13246. && !in_async_generator(compressor.find_parent(AST_Scope))) {
  13247. self.value = null;
  13248. }
  13249. return self;
  13250. });
  13251. })(function(node, optimizer) {
  13252. node.DEFMETHOD("optimize", function(compressor) {
  13253. var self = this;
  13254. if (self._optimized) return self;
  13255. if (compressor.has_directive("use asm")) return self;
  13256. var opt = optimizer(self, compressor);
  13257. opt._optimized = true;
  13258. return opt;
  13259. });
  13260. });