index.js 137 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS2
  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. import {
  34. AST_Accessor,
  35. AST_Array,
  36. AST_Arrow,
  37. AST_Assign,
  38. AST_BigInt,
  39. AST_Binary,
  40. AST_Block,
  41. AST_BlockStatement,
  42. AST_Boolean,
  43. AST_Break,
  44. AST_Call,
  45. AST_Catch,
  46. AST_Chain,
  47. AST_Class,
  48. AST_ClassProperty,
  49. AST_ClassStaticBlock,
  50. AST_ConciseMethod,
  51. AST_Conditional,
  52. AST_Const,
  53. AST_Constant,
  54. AST_Debugger,
  55. AST_Default,
  56. AST_DefaultAssign,
  57. AST_Definitions,
  58. AST_Defun,
  59. AST_Destructuring,
  60. AST_Directive,
  61. AST_Do,
  62. AST_Dot,
  63. AST_DWLoop,
  64. AST_EmptyStatement,
  65. AST_Exit,
  66. AST_Expansion,
  67. AST_Export,
  68. AST_False,
  69. AST_For,
  70. AST_ForIn,
  71. AST_Function,
  72. AST_Hole,
  73. AST_If,
  74. AST_Import,
  75. AST_Infinity,
  76. AST_LabeledStatement,
  77. AST_Lambda,
  78. AST_Let,
  79. AST_NaN,
  80. AST_New,
  81. AST_Node,
  82. AST_Null,
  83. AST_Number,
  84. AST_Object,
  85. AST_ObjectKeyVal,
  86. AST_ObjectProperty,
  87. AST_PrefixedTemplateString,
  88. AST_PropAccess,
  89. AST_RegExp,
  90. AST_Return,
  91. AST_Scope,
  92. AST_Sequence,
  93. AST_SimpleStatement,
  94. AST_Statement,
  95. AST_String,
  96. AST_Sub,
  97. AST_Switch,
  98. AST_SwitchBranch,
  99. AST_Symbol,
  100. AST_SymbolClassProperty,
  101. AST_SymbolDeclaration,
  102. AST_SymbolDefun,
  103. AST_SymbolExport,
  104. AST_SymbolFunarg,
  105. AST_SymbolLambda,
  106. AST_SymbolLet,
  107. AST_SymbolMethod,
  108. AST_SymbolRef,
  109. AST_TemplateString,
  110. AST_This,
  111. AST_Toplevel,
  112. AST_True,
  113. AST_Try,
  114. AST_Unary,
  115. AST_UnaryPostfix,
  116. AST_UnaryPrefix,
  117. AST_Undefined,
  118. AST_Var,
  119. AST_VarDef,
  120. AST_While,
  121. AST_With,
  122. AST_Yield,
  123. TreeTransformer,
  124. TreeWalker,
  125. walk,
  126. walk_abort,
  127. _NOINLINE,
  128. } from "../ast.js";
  129. import {
  130. defaults,
  131. HOP,
  132. make_node,
  133. makePredicate,
  134. MAP,
  135. remove,
  136. return_false,
  137. return_true,
  138. regexp_source_fix,
  139. has_annotation,
  140. regexp_is_safe,
  141. } from "../utils/index.js";
  142. import { first_in_statement } from "../utils/first_in_statement.js";
  143. import { equivalent_to } from "../equivalent-to.js";
  144. import {
  145. is_basic_identifier_string,
  146. JS_Parse_Error,
  147. parse,
  148. PRECEDENCE,
  149. } from "../parse.js";
  150. import { OutputStream } from "../output.js";
  151. import { base54, format_mangler_options } from "../scope.js";
  152. import "../size.js";
  153. import "./evaluate.js";
  154. import "./drop-side-effect-free.js";
  155. import "./drop-unused.js";
  156. import "./reduce-vars.js";
  157. import {
  158. is_undeclared_ref,
  159. lazy_op,
  160. is_nullish,
  161. is_undefined,
  162. is_lhs,
  163. aborts,
  164. } from "./inference.js";
  165. import {
  166. SQUEEZED,
  167. OPTIMIZED,
  168. CLEAR_BETWEEN_PASSES,
  169. TOP,
  170. UNDEFINED,
  171. UNUSED,
  172. TRUTHY,
  173. FALSY,
  174. has_flag,
  175. set_flag,
  176. clear_flag,
  177. } from "./compressor-flags.js";
  178. import {
  179. make_sequence,
  180. best_of,
  181. best_of_expression,
  182. make_node_from_constant,
  183. merge_sequence,
  184. get_simple_key,
  185. has_break_or_continue,
  186. maintain_this_binding,
  187. is_empty,
  188. is_identifier_atom,
  189. is_reachable,
  190. can_be_evicted_from_block,
  191. as_statement_array,
  192. is_func_expr,
  193. } from "./common.js";
  194. import { tighten_body, trim_unreachable_code } from "./tighten-body.js";
  195. import { inline_into_symbolref, inline_into_call } from "./inline.js";
  196. import "./global-defs.js";
  197. class Compressor extends TreeWalker {
  198. constructor(options, { false_by_default = false, mangle_options = false }) {
  199. super();
  200. if (options.defaults !== undefined && !options.defaults) false_by_default = true;
  201. this.options = defaults(options, {
  202. arguments : false,
  203. arrows : !false_by_default,
  204. booleans : !false_by_default,
  205. booleans_as_integers : false,
  206. collapse_vars : !false_by_default,
  207. comparisons : !false_by_default,
  208. computed_props: !false_by_default,
  209. conditionals : !false_by_default,
  210. dead_code : !false_by_default,
  211. defaults : true,
  212. directives : !false_by_default,
  213. drop_console : false,
  214. drop_debugger : !false_by_default,
  215. ecma : 5,
  216. evaluate : !false_by_default,
  217. expression : false,
  218. global_defs : false,
  219. hoist_funs : false,
  220. hoist_props : !false_by_default,
  221. hoist_vars : false,
  222. ie8 : false,
  223. if_return : !false_by_default,
  224. inline : !false_by_default,
  225. join_vars : !false_by_default,
  226. keep_classnames: false,
  227. keep_fargs : true,
  228. keep_fnames : false,
  229. keep_infinity : false,
  230. lhs_constants : !false_by_default,
  231. loops : !false_by_default,
  232. module : false,
  233. negate_iife : !false_by_default,
  234. passes : 1,
  235. properties : !false_by_default,
  236. pure_getters : !false_by_default && "strict",
  237. pure_funcs : null,
  238. reduce_funcs : !false_by_default,
  239. reduce_vars : !false_by_default,
  240. sequences : !false_by_default,
  241. side_effects : !false_by_default,
  242. switches : !false_by_default,
  243. top_retain : null,
  244. toplevel : !!(options && options["top_retain"]),
  245. typeofs : !false_by_default,
  246. unsafe : false,
  247. unsafe_arrows : false,
  248. unsafe_comps : false,
  249. unsafe_Function: false,
  250. unsafe_math : false,
  251. unsafe_symbols: false,
  252. unsafe_methods: false,
  253. unsafe_proto : false,
  254. unsafe_regexp : false,
  255. unsafe_undefined: false,
  256. unused : !false_by_default,
  257. warnings : false // legacy
  258. }, true);
  259. var global_defs = this.options["global_defs"];
  260. if (typeof global_defs == "object") for (var key in global_defs) {
  261. if (key[0] === "@" && HOP(global_defs, key)) {
  262. global_defs[key.slice(1)] = parse(global_defs[key], {
  263. expression: true
  264. });
  265. }
  266. }
  267. if (this.options["inline"] === true) this.options["inline"] = 3;
  268. var pure_funcs = this.options["pure_funcs"];
  269. if (typeof pure_funcs == "function") {
  270. this.pure_funcs = pure_funcs;
  271. } else {
  272. this.pure_funcs = pure_funcs ? function(node) {
  273. return !pure_funcs.includes(node.expression.print_to_string());
  274. } : return_true;
  275. }
  276. var top_retain = this.options["top_retain"];
  277. if (top_retain instanceof RegExp) {
  278. this.top_retain = function(def) {
  279. return top_retain.test(def.name);
  280. };
  281. } else if (typeof top_retain == "function") {
  282. this.top_retain = top_retain;
  283. } else if (top_retain) {
  284. if (typeof top_retain == "string") {
  285. top_retain = top_retain.split(/,/);
  286. }
  287. this.top_retain = function(def) {
  288. return top_retain.includes(def.name);
  289. };
  290. }
  291. if (this.options["module"]) {
  292. this.directives["use strict"] = true;
  293. this.options["toplevel"] = true;
  294. }
  295. var toplevel = this.options["toplevel"];
  296. this.toplevel = typeof toplevel == "string" ? {
  297. funcs: /funcs/.test(toplevel),
  298. vars: /vars/.test(toplevel)
  299. } : {
  300. funcs: toplevel,
  301. vars: toplevel
  302. };
  303. var sequences = this.options["sequences"];
  304. this.sequences_limit = sequences == 1 ? 800 : sequences | 0;
  305. this.evaluated_regexps = new Map();
  306. this._toplevel = undefined;
  307. this.mangle_options = mangle_options
  308. ? format_mangler_options(mangle_options)
  309. : mangle_options;
  310. }
  311. option(key) {
  312. return this.options[key];
  313. }
  314. exposed(def) {
  315. if (def.export) return true;
  316. if (def.global) for (var i = 0, len = def.orig.length; i < len; i++)
  317. if (!this.toplevel[def.orig[i] instanceof AST_SymbolDefun ? "funcs" : "vars"])
  318. return true;
  319. return false;
  320. }
  321. in_boolean_context() {
  322. if (!this.option("booleans")) return false;
  323. var self = this.self();
  324. for (var i = 0, p; p = this.parent(i); i++) {
  325. if (p instanceof AST_SimpleStatement
  326. || p instanceof AST_Conditional && p.condition === self
  327. || p instanceof AST_DWLoop && p.condition === self
  328. || p instanceof AST_For && p.condition === self
  329. || p instanceof AST_If && p.condition === self
  330. || p instanceof AST_UnaryPrefix && p.operator == "!" && p.expression === self) {
  331. return true;
  332. }
  333. if (
  334. p instanceof AST_Binary
  335. && (
  336. p.operator == "&&"
  337. || p.operator == "||"
  338. || p.operator == "??"
  339. )
  340. || p instanceof AST_Conditional
  341. || p.tail_node() === self
  342. ) {
  343. self = p;
  344. } else {
  345. return false;
  346. }
  347. }
  348. }
  349. get_toplevel() {
  350. return this._toplevel;
  351. }
  352. compress(toplevel) {
  353. toplevel = toplevel.resolve_defines(this);
  354. this._toplevel = toplevel;
  355. if (this.option("expression")) {
  356. this._toplevel.process_expression(true);
  357. }
  358. var passes = +this.options.passes || 1;
  359. var min_count = 1 / 0;
  360. var stopping = false;
  361. var nth_identifier = this.mangle_options && this.mangle_options.nth_identifier || base54;
  362. var mangle = { ie8: this.option("ie8"), nth_identifier: nth_identifier };
  363. for (var pass = 0; pass < passes; pass++) {
  364. this._toplevel.figure_out_scope(mangle);
  365. if (pass === 0 && this.option("drop_console")) {
  366. // must be run before reduce_vars and compress pass
  367. this._toplevel = this._toplevel.drop_console(this.option("drop_console"));
  368. }
  369. if (pass > 0 || this.option("reduce_vars")) {
  370. this._toplevel.reset_opt_flags(this);
  371. }
  372. this._toplevel = this._toplevel.transform(this);
  373. if (passes > 1) {
  374. let count = 0;
  375. walk(this._toplevel, () => { count++; });
  376. if (count < min_count) {
  377. min_count = count;
  378. stopping = false;
  379. } else if (stopping) {
  380. break;
  381. } else {
  382. stopping = true;
  383. }
  384. }
  385. }
  386. if (this.option("expression")) {
  387. this._toplevel.process_expression(false);
  388. }
  389. toplevel = this._toplevel;
  390. this._toplevel = undefined;
  391. return toplevel;
  392. }
  393. before(node, descend) {
  394. if (has_flag(node, SQUEEZED)) return node;
  395. var was_scope = false;
  396. if (node instanceof AST_Scope) {
  397. node = node.hoist_properties(this);
  398. node = node.hoist_declarations(this);
  399. was_scope = true;
  400. }
  401. // Before https://github.com/mishoo/UglifyJS2/pull/1602 AST_Node.optimize()
  402. // would call AST_Node.transform() if a different instance of AST_Node is
  403. // produced after def_optimize().
  404. // This corrupts TreeWalker.stack, which cause AST look-ups to malfunction.
  405. // Migrate and defer all children's AST_Node.transform() to below, which
  406. // will now happen after this parent AST_Node has been properly substituted
  407. // thus gives a consistent AST snapshot.
  408. descend(node, this);
  409. // Existing code relies on how AST_Node.optimize() worked, and omitting the
  410. // following replacement call would result in degraded efficiency of both
  411. // output and performance.
  412. descend(node, this);
  413. var opt = node.optimize(this);
  414. if (was_scope && opt instanceof AST_Scope) {
  415. opt.drop_unused(this);
  416. descend(opt, this);
  417. }
  418. if (opt === node) set_flag(opt, SQUEEZED);
  419. return opt;
  420. }
  421. /** Alternative to plain is_lhs() which doesn't work within .optimize() */
  422. is_lhs() {
  423. const self = this.stack[this.stack.length - 1];
  424. const parent = this.stack[this.stack.length - 2];
  425. return is_lhs(self, parent);
  426. }
  427. }
  428. function def_optimize(node, optimizer) {
  429. node.DEFMETHOD("optimize", function(compressor) {
  430. var self = this;
  431. if (has_flag(self, OPTIMIZED)) return self;
  432. if (compressor.has_directive("use asm")) return self;
  433. var opt = optimizer(self, compressor);
  434. set_flag(opt, OPTIMIZED);
  435. return opt;
  436. });
  437. }
  438. def_optimize(AST_Node, function(self) {
  439. return self;
  440. });
  441. AST_Toplevel.DEFMETHOD("drop_console", function(options) {
  442. var isArray = Array.isArray(options);
  443. return this.transform(new TreeTransformer(function(self) {
  444. if (self.TYPE !== "Call") {
  445. return;
  446. }
  447. var exp = self.expression;
  448. if (!(exp instanceof AST_PropAccess)) {
  449. return;
  450. }
  451. if (isArray && options.indexOf(exp.property) === -1) {
  452. return;
  453. }
  454. var name = exp.expression;
  455. while (name.expression) {
  456. name = name.expression;
  457. }
  458. if (is_undeclared_ref(name) && name.name == "console") {
  459. return make_node(AST_Undefined, self);
  460. }
  461. }));
  462. });
  463. AST_Node.DEFMETHOD("equivalent_to", function(node) {
  464. return equivalent_to(this, node);
  465. });
  466. AST_Scope.DEFMETHOD("process_expression", function(insert, compressor) {
  467. var self = this;
  468. var tt = new TreeTransformer(function(node) {
  469. if (insert && node instanceof AST_SimpleStatement) {
  470. return make_node(AST_Return, node, {
  471. value: node.body
  472. });
  473. }
  474. if (!insert && node instanceof AST_Return) {
  475. if (compressor) {
  476. var value = node.value && node.value.drop_side_effect_free(compressor, true);
  477. return value
  478. ? make_node(AST_SimpleStatement, node, { body: value })
  479. : make_node(AST_EmptyStatement, node);
  480. }
  481. return make_node(AST_SimpleStatement, node, {
  482. body: node.value || make_node(AST_UnaryPrefix, node, {
  483. operator: "void",
  484. expression: make_node(AST_Number, node, {
  485. value: 0
  486. })
  487. })
  488. });
  489. }
  490. if (node instanceof AST_Class || node instanceof AST_Lambda && node !== self) {
  491. return node;
  492. }
  493. if (node instanceof AST_Block) {
  494. var index = node.body.length - 1;
  495. if (index >= 0) {
  496. node.body[index] = node.body[index].transform(tt);
  497. }
  498. } else if (node instanceof AST_If) {
  499. node.body = node.body.transform(tt);
  500. if (node.alternative) {
  501. node.alternative = node.alternative.transform(tt);
  502. }
  503. } else if (node instanceof AST_With) {
  504. node.body = node.body.transform(tt);
  505. }
  506. return node;
  507. });
  508. self.transform(tt);
  509. });
  510. AST_Toplevel.DEFMETHOD("reset_opt_flags", function(compressor) {
  511. const self = this;
  512. const reduce_vars = compressor.option("reduce_vars");
  513. const preparation = new TreeWalker(function(node, descend) {
  514. clear_flag(node, CLEAR_BETWEEN_PASSES);
  515. if (reduce_vars) {
  516. if (compressor.top_retain
  517. && node instanceof AST_Defun // Only functions are retained
  518. && preparation.parent() === self
  519. ) {
  520. set_flag(node, TOP);
  521. }
  522. return node.reduce_vars(preparation, descend, compressor);
  523. }
  524. });
  525. // Stack of look-up tables to keep track of whether a `SymbolDef` has been
  526. // properly assigned before use:
  527. // - `push()` & `pop()` when visiting conditional branches
  528. preparation.safe_ids = Object.create(null);
  529. preparation.in_loop = null;
  530. preparation.loop_ids = new Map();
  531. preparation.defs_to_safe_ids = new Map();
  532. self.walk(preparation);
  533. });
  534. AST_Symbol.DEFMETHOD("fixed_value", function() {
  535. var fixed = this.thedef.fixed;
  536. if (!fixed || fixed instanceof AST_Node) return fixed;
  537. return fixed();
  538. });
  539. AST_SymbolRef.DEFMETHOD("is_immutable", function() {
  540. var orig = this.definition().orig;
  541. return orig.length == 1 && orig[0] instanceof AST_SymbolLambda;
  542. });
  543. function find_variable(compressor, name) {
  544. var scope, i = 0;
  545. while (scope = compressor.parent(i++)) {
  546. if (scope instanceof AST_Scope) break;
  547. if (scope instanceof AST_Catch && scope.argname) {
  548. scope = scope.argname.definition().scope;
  549. break;
  550. }
  551. }
  552. return scope.find_variable(name);
  553. }
  554. var global_names = makePredicate("Array Boolean clearInterval clearTimeout console Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error escape eval EvalError Function isFinite isNaN JSON Math Number parseFloat parseInt RangeError ReferenceError RegExp Object setInterval setTimeout String SyntaxError TypeError unescape URIError");
  555. AST_SymbolRef.DEFMETHOD("is_declared", function(compressor) {
  556. return !this.definition().undeclared
  557. || compressor.option("unsafe") && global_names.has(this.name);
  558. });
  559. /* -----[ optimizers ]----- */
  560. var directives = new Set(["use asm", "use strict"]);
  561. def_optimize(AST_Directive, function(self, compressor) {
  562. if (compressor.option("directives")
  563. && (!directives.has(self.value) || compressor.has_directive(self.value) !== self)) {
  564. return make_node(AST_EmptyStatement, self);
  565. }
  566. return self;
  567. });
  568. def_optimize(AST_Debugger, function(self, compressor) {
  569. if (compressor.option("drop_debugger"))
  570. return make_node(AST_EmptyStatement, self);
  571. return self;
  572. });
  573. def_optimize(AST_LabeledStatement, function(self, compressor) {
  574. if (self.body instanceof AST_Break
  575. && compressor.loopcontrol_target(self.body) === self.body) {
  576. return make_node(AST_EmptyStatement, self);
  577. }
  578. return self.label.references.length == 0 ? self.body : self;
  579. });
  580. def_optimize(AST_Block, function(self, compressor) {
  581. tighten_body(self.body, compressor);
  582. return self;
  583. });
  584. function can_be_extracted_from_if_block(node) {
  585. return !(
  586. node instanceof AST_Const
  587. || node instanceof AST_Let
  588. || node instanceof AST_Class
  589. );
  590. }
  591. def_optimize(AST_BlockStatement, function(self, compressor) {
  592. tighten_body(self.body, compressor);
  593. switch (self.body.length) {
  594. case 1:
  595. if (!compressor.has_directive("use strict")
  596. && compressor.parent() instanceof AST_If
  597. && can_be_extracted_from_if_block(self.body[0])
  598. || can_be_evicted_from_block(self.body[0])) {
  599. return self.body[0];
  600. }
  601. break;
  602. case 0: return make_node(AST_EmptyStatement, self);
  603. }
  604. return self;
  605. });
  606. function opt_AST_Lambda(self, compressor) {
  607. tighten_body(self.body, compressor);
  608. if (compressor.option("side_effects")
  609. && self.body.length == 1
  610. && self.body[0] === compressor.has_directive("use strict")) {
  611. self.body.length = 0;
  612. }
  613. return self;
  614. }
  615. def_optimize(AST_Lambda, opt_AST_Lambda);
  616. AST_Scope.DEFMETHOD("hoist_declarations", function(compressor) {
  617. var self = this;
  618. if (compressor.has_directive("use asm")) return self;
  619. var hoist_funs = compressor.option("hoist_funs");
  620. var hoist_vars = compressor.option("hoist_vars");
  621. if (hoist_funs || hoist_vars) {
  622. var dirs = [];
  623. var hoisted = [];
  624. var vars = new Map(), vars_found = 0, var_decl = 0;
  625. // let's count var_decl first, we seem to waste a lot of
  626. // space if we hoist `var` when there's only one.
  627. walk(self, node => {
  628. if (node instanceof AST_Scope && node !== self)
  629. return true;
  630. if (node instanceof AST_Var) {
  631. ++var_decl;
  632. return true;
  633. }
  634. });
  635. hoist_vars = hoist_vars && var_decl > 1;
  636. var tt = new TreeTransformer(
  637. function before(node) {
  638. if (node !== self) {
  639. if (node instanceof AST_Directive) {
  640. dirs.push(node);
  641. return make_node(AST_EmptyStatement, node);
  642. }
  643. if (hoist_funs && node instanceof AST_Defun
  644. && !(tt.parent() instanceof AST_Export)
  645. && tt.parent() === self) {
  646. hoisted.push(node);
  647. return make_node(AST_EmptyStatement, node);
  648. }
  649. if (
  650. hoist_vars
  651. && node instanceof AST_Var
  652. && !node.definitions.some(def => def.name instanceof AST_Destructuring)
  653. ) {
  654. node.definitions.forEach(function(def) {
  655. vars.set(def.name.name, def);
  656. ++vars_found;
  657. });
  658. var seq = node.to_assignments(compressor);
  659. var p = tt.parent();
  660. if (p instanceof AST_ForIn && p.init === node) {
  661. if (seq == null) {
  662. var def = node.definitions[0].name;
  663. return make_node(AST_SymbolRef, def, def);
  664. }
  665. return seq;
  666. }
  667. if (p instanceof AST_For && p.init === node) {
  668. return seq;
  669. }
  670. if (!seq) return make_node(AST_EmptyStatement, node);
  671. return make_node(AST_SimpleStatement, node, {
  672. body: seq
  673. });
  674. }
  675. if (node instanceof AST_Scope)
  676. return node; // to avoid descending in nested scopes
  677. }
  678. }
  679. );
  680. self = self.transform(tt);
  681. if (vars_found > 0) {
  682. // collect only vars which don't show up in self's arguments list
  683. var defs = [];
  684. const is_lambda = self instanceof AST_Lambda;
  685. const args_as_names = is_lambda ? self.args_as_names() : null;
  686. vars.forEach((def, name) => {
  687. if (is_lambda && args_as_names.some((x) => x.name === def.name.name)) {
  688. vars.delete(name);
  689. } else {
  690. def = def.clone();
  691. def.value = null;
  692. defs.push(def);
  693. vars.set(name, def);
  694. }
  695. });
  696. if (defs.length > 0) {
  697. // try to merge in assignments
  698. for (var i = 0; i < self.body.length;) {
  699. if (self.body[i] instanceof AST_SimpleStatement) {
  700. var expr = self.body[i].body, sym, assign;
  701. if (expr instanceof AST_Assign
  702. && expr.operator == "="
  703. && (sym = expr.left) instanceof AST_Symbol
  704. && vars.has(sym.name)
  705. ) {
  706. var def = vars.get(sym.name);
  707. if (def.value) break;
  708. def.value = expr.right;
  709. remove(defs, def);
  710. defs.push(def);
  711. self.body.splice(i, 1);
  712. continue;
  713. }
  714. if (expr instanceof AST_Sequence
  715. && (assign = expr.expressions[0]) instanceof AST_Assign
  716. && assign.operator == "="
  717. && (sym = assign.left) instanceof AST_Symbol
  718. && vars.has(sym.name)
  719. ) {
  720. var def = vars.get(sym.name);
  721. if (def.value) break;
  722. def.value = assign.right;
  723. remove(defs, def);
  724. defs.push(def);
  725. self.body[i].body = make_sequence(expr, expr.expressions.slice(1));
  726. continue;
  727. }
  728. }
  729. if (self.body[i] instanceof AST_EmptyStatement) {
  730. self.body.splice(i, 1);
  731. continue;
  732. }
  733. if (self.body[i] instanceof AST_BlockStatement) {
  734. self.body.splice(i, 1, ...self.body[i].body);
  735. continue;
  736. }
  737. break;
  738. }
  739. defs = make_node(AST_Var, self, {
  740. definitions: defs
  741. });
  742. hoisted.push(defs);
  743. }
  744. }
  745. self.body = dirs.concat(hoisted, self.body);
  746. }
  747. return self;
  748. });
  749. AST_Scope.DEFMETHOD("hoist_properties", function(compressor) {
  750. var self = this;
  751. if (!compressor.option("hoist_props") || compressor.has_directive("use asm")) return self;
  752. var top_retain = self instanceof AST_Toplevel && compressor.top_retain || return_false;
  753. var defs_by_id = new Map();
  754. var hoister = new TreeTransformer(function(node, descend) {
  755. if (node instanceof AST_VarDef) {
  756. const sym = node.name;
  757. let def;
  758. let value;
  759. if (sym.scope === self
  760. && (def = sym.definition()).escaped != 1
  761. && !def.assignments
  762. && !def.direct_access
  763. && !def.single_use
  764. && !compressor.exposed(def)
  765. && !top_retain(def)
  766. && (value = sym.fixed_value()) === node.value
  767. && value instanceof AST_Object
  768. && !value.properties.some(prop =>
  769. prop instanceof AST_Expansion || prop.computed_key()
  770. )
  771. ) {
  772. descend(node, this);
  773. const defs = new Map();
  774. const assignments = [];
  775. value.properties.forEach(({ key, value }) => {
  776. const scope = hoister.find_scope();
  777. const symbol = self.create_symbol(sym.CTOR, {
  778. source: sym,
  779. scope,
  780. conflict_scopes: new Set([
  781. scope,
  782. ...sym.definition().references.map(ref => ref.scope)
  783. ]),
  784. tentative_name: sym.name + "_" + key
  785. });
  786. defs.set(String(key), symbol.definition());
  787. assignments.push(make_node(AST_VarDef, node, {
  788. name: symbol,
  789. value
  790. }));
  791. });
  792. defs_by_id.set(def.id, defs);
  793. return MAP.splice(assignments);
  794. }
  795. } else if (node instanceof AST_PropAccess
  796. && node.expression instanceof AST_SymbolRef
  797. ) {
  798. const defs = defs_by_id.get(node.expression.definition().id);
  799. if (defs) {
  800. const def = defs.get(String(get_simple_key(node.property)));
  801. const sym = make_node(AST_SymbolRef, node, {
  802. name: def.name,
  803. scope: node.expression.scope,
  804. thedef: def
  805. });
  806. sym.reference({});
  807. return sym;
  808. }
  809. }
  810. });
  811. return self.transform(hoister);
  812. });
  813. def_optimize(AST_SimpleStatement, function(self, compressor) {
  814. if (compressor.option("side_effects")) {
  815. var body = self.body;
  816. var node = body.drop_side_effect_free(compressor, true);
  817. if (!node) {
  818. return make_node(AST_EmptyStatement, self);
  819. }
  820. if (node !== body) {
  821. return make_node(AST_SimpleStatement, self, { body: node });
  822. }
  823. }
  824. return self;
  825. });
  826. def_optimize(AST_While, function(self, compressor) {
  827. return compressor.option("loops") ? make_node(AST_For, self, self).optimize(compressor) : self;
  828. });
  829. def_optimize(AST_Do, function(self, compressor) {
  830. if (!compressor.option("loops")) return self;
  831. var cond = self.condition.tail_node().evaluate(compressor);
  832. if (!(cond instanceof AST_Node)) {
  833. if (cond) return make_node(AST_For, self, {
  834. body: make_node(AST_BlockStatement, self.body, {
  835. body: [
  836. self.body,
  837. make_node(AST_SimpleStatement, self.condition, {
  838. body: self.condition
  839. })
  840. ]
  841. })
  842. }).optimize(compressor);
  843. if (!has_break_or_continue(self, compressor.parent())) {
  844. return make_node(AST_BlockStatement, self.body, {
  845. body: [
  846. self.body,
  847. make_node(AST_SimpleStatement, self.condition, {
  848. body: self.condition
  849. })
  850. ]
  851. }).optimize(compressor);
  852. }
  853. }
  854. return self;
  855. });
  856. function if_break_in_loop(self, compressor) {
  857. var first = self.body instanceof AST_BlockStatement ? self.body.body[0] : self.body;
  858. if (compressor.option("dead_code") && is_break(first)) {
  859. var body = [];
  860. if (self.init instanceof AST_Statement) {
  861. body.push(self.init);
  862. } else if (self.init) {
  863. body.push(make_node(AST_SimpleStatement, self.init, {
  864. body: self.init
  865. }));
  866. }
  867. if (self.condition) {
  868. body.push(make_node(AST_SimpleStatement, self.condition, {
  869. body: self.condition
  870. }));
  871. }
  872. trim_unreachable_code(compressor, self.body, body);
  873. return make_node(AST_BlockStatement, self, {
  874. body: body
  875. });
  876. }
  877. if (first instanceof AST_If) {
  878. if (is_break(first.body)) {
  879. if (self.condition) {
  880. self.condition = make_node(AST_Binary, self.condition, {
  881. left: self.condition,
  882. operator: "&&",
  883. right: first.condition.negate(compressor),
  884. });
  885. } else {
  886. self.condition = first.condition.negate(compressor);
  887. }
  888. drop_it(first.alternative);
  889. } else if (is_break(first.alternative)) {
  890. if (self.condition) {
  891. self.condition = make_node(AST_Binary, self.condition, {
  892. left: self.condition,
  893. operator: "&&",
  894. right: first.condition,
  895. });
  896. } else {
  897. self.condition = first.condition;
  898. }
  899. drop_it(first.body);
  900. }
  901. }
  902. return self;
  903. function is_break(node) {
  904. return node instanceof AST_Break
  905. && compressor.loopcontrol_target(node) === compressor.self();
  906. }
  907. function drop_it(rest) {
  908. rest = as_statement_array(rest);
  909. if (self.body instanceof AST_BlockStatement) {
  910. self.body = self.body.clone();
  911. self.body.body = rest.concat(self.body.body.slice(1));
  912. self.body = self.body.transform(compressor);
  913. } else {
  914. self.body = make_node(AST_BlockStatement, self.body, {
  915. body: rest
  916. }).transform(compressor);
  917. }
  918. self = if_break_in_loop(self, compressor);
  919. }
  920. }
  921. def_optimize(AST_For, function(self, compressor) {
  922. if (!compressor.option("loops")) return self;
  923. if (compressor.option("side_effects") && self.init) {
  924. self.init = self.init.drop_side_effect_free(compressor);
  925. }
  926. if (self.condition) {
  927. var cond = self.condition.evaluate(compressor);
  928. if (!(cond instanceof AST_Node)) {
  929. if (cond) self.condition = null;
  930. else if (!compressor.option("dead_code")) {
  931. var orig = self.condition;
  932. self.condition = make_node_from_constant(cond, self.condition);
  933. self.condition = best_of_expression(self.condition.transform(compressor), orig);
  934. }
  935. }
  936. if (compressor.option("dead_code")) {
  937. if (cond instanceof AST_Node) cond = self.condition.tail_node().evaluate(compressor);
  938. if (!cond) {
  939. var body = [];
  940. trim_unreachable_code(compressor, self.body, body);
  941. if (self.init instanceof AST_Statement) {
  942. body.push(self.init);
  943. } else if (self.init) {
  944. body.push(make_node(AST_SimpleStatement, self.init, {
  945. body: self.init
  946. }));
  947. }
  948. body.push(make_node(AST_SimpleStatement, self.condition, {
  949. body: self.condition
  950. }));
  951. return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
  952. }
  953. }
  954. }
  955. return if_break_in_loop(self, compressor);
  956. });
  957. def_optimize(AST_If, function(self, compressor) {
  958. if (is_empty(self.alternative)) self.alternative = null;
  959. if (!compressor.option("conditionals")) return self;
  960. // if condition can be statically determined, drop
  961. // one of the blocks. note, statically determined implies
  962. // “has no side effects”; also it doesn't work for cases like
  963. // `x && true`, though it probably should.
  964. var cond = self.condition.evaluate(compressor);
  965. if (!compressor.option("dead_code") && !(cond instanceof AST_Node)) {
  966. var orig = self.condition;
  967. self.condition = make_node_from_constant(cond, orig);
  968. self.condition = best_of_expression(self.condition.transform(compressor), orig);
  969. }
  970. if (compressor.option("dead_code")) {
  971. if (cond instanceof AST_Node) cond = self.condition.tail_node().evaluate(compressor);
  972. if (!cond) {
  973. var body = [];
  974. trim_unreachable_code(compressor, self.body, body);
  975. body.push(make_node(AST_SimpleStatement, self.condition, {
  976. body: self.condition
  977. }));
  978. if (self.alternative) body.push(self.alternative);
  979. return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
  980. } else if (!(cond instanceof AST_Node)) {
  981. var body = [];
  982. body.push(make_node(AST_SimpleStatement, self.condition, {
  983. body: self.condition
  984. }));
  985. body.push(self.body);
  986. if (self.alternative) {
  987. trim_unreachable_code(compressor, self.alternative, body);
  988. }
  989. return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
  990. }
  991. }
  992. var negated = self.condition.negate(compressor);
  993. var self_condition_length = self.condition.size();
  994. var negated_length = negated.size();
  995. var negated_is_best = negated_length < self_condition_length;
  996. if (self.alternative && negated_is_best) {
  997. negated_is_best = false; // because we already do the switch here.
  998. // no need to swap values of self_condition_length and negated_length
  999. // here because they are only used in an equality comparison later on.
  1000. self.condition = negated;
  1001. var tmp = self.body;
  1002. self.body = self.alternative || make_node(AST_EmptyStatement, self);
  1003. self.alternative = tmp;
  1004. }
  1005. if (is_empty(self.body) && is_empty(self.alternative)) {
  1006. return make_node(AST_SimpleStatement, self.condition, {
  1007. body: self.condition.clone()
  1008. }).optimize(compressor);
  1009. }
  1010. if (self.body instanceof AST_SimpleStatement
  1011. && self.alternative instanceof AST_SimpleStatement) {
  1012. return make_node(AST_SimpleStatement, self, {
  1013. body: make_node(AST_Conditional, self, {
  1014. condition : self.condition,
  1015. consequent : self.body.body,
  1016. alternative : self.alternative.body
  1017. })
  1018. }).optimize(compressor);
  1019. }
  1020. if (is_empty(self.alternative) && self.body instanceof AST_SimpleStatement) {
  1021. if (self_condition_length === negated_length && !negated_is_best
  1022. && self.condition instanceof AST_Binary && self.condition.operator == "||") {
  1023. // although the code length of self.condition and negated are the same,
  1024. // negated does not require additional surrounding parentheses.
  1025. // see https://github.com/mishoo/UglifyJS2/issues/979
  1026. negated_is_best = true;
  1027. }
  1028. if (negated_is_best) return make_node(AST_SimpleStatement, self, {
  1029. body: make_node(AST_Binary, self, {
  1030. operator : "||",
  1031. left : negated,
  1032. right : self.body.body
  1033. })
  1034. }).optimize(compressor);
  1035. return make_node(AST_SimpleStatement, self, {
  1036. body: make_node(AST_Binary, self, {
  1037. operator : "&&",
  1038. left : self.condition,
  1039. right : self.body.body
  1040. })
  1041. }).optimize(compressor);
  1042. }
  1043. if (self.body instanceof AST_EmptyStatement
  1044. && self.alternative instanceof AST_SimpleStatement) {
  1045. return make_node(AST_SimpleStatement, self, {
  1046. body: make_node(AST_Binary, self, {
  1047. operator : "||",
  1048. left : self.condition,
  1049. right : self.alternative.body
  1050. })
  1051. }).optimize(compressor);
  1052. }
  1053. if (self.body instanceof AST_Exit
  1054. && self.alternative instanceof AST_Exit
  1055. && self.body.TYPE == self.alternative.TYPE) {
  1056. return make_node(self.body.CTOR, self, {
  1057. value: make_node(AST_Conditional, self, {
  1058. condition : self.condition,
  1059. consequent : self.body.value || make_node(AST_Undefined, self.body),
  1060. alternative : self.alternative.value || make_node(AST_Undefined, self.alternative)
  1061. }).transform(compressor)
  1062. }).optimize(compressor);
  1063. }
  1064. if (self.body instanceof AST_If
  1065. && !self.body.alternative
  1066. && !self.alternative) {
  1067. self = make_node(AST_If, self, {
  1068. condition: make_node(AST_Binary, self.condition, {
  1069. operator: "&&",
  1070. left: self.condition,
  1071. right: self.body.condition
  1072. }),
  1073. body: self.body.body,
  1074. alternative: null
  1075. });
  1076. }
  1077. if (aborts(self.body)) {
  1078. if (self.alternative) {
  1079. var alt = self.alternative;
  1080. self.alternative = null;
  1081. return make_node(AST_BlockStatement, self, {
  1082. body: [ self, alt ]
  1083. }).optimize(compressor);
  1084. }
  1085. }
  1086. if (aborts(self.alternative)) {
  1087. var body = self.body;
  1088. self.body = self.alternative;
  1089. self.condition = negated_is_best ? negated : self.condition.negate(compressor);
  1090. self.alternative = null;
  1091. return make_node(AST_BlockStatement, self, {
  1092. body: [ self, body ]
  1093. }).optimize(compressor);
  1094. }
  1095. return self;
  1096. });
  1097. def_optimize(AST_Switch, function(self, compressor) {
  1098. if (!compressor.option("switches")) return self;
  1099. var branch;
  1100. var value = self.expression.evaluate(compressor);
  1101. if (!(value instanceof AST_Node)) {
  1102. var orig = self.expression;
  1103. self.expression = make_node_from_constant(value, orig);
  1104. self.expression = best_of_expression(self.expression.transform(compressor), orig);
  1105. }
  1106. if (!compressor.option("dead_code")) return self;
  1107. if (value instanceof AST_Node) {
  1108. value = self.expression.tail_node().evaluate(compressor);
  1109. }
  1110. var decl = [];
  1111. var body = [];
  1112. var default_branch;
  1113. var exact_match;
  1114. for (var i = 0, len = self.body.length; i < len && !exact_match; i++) {
  1115. branch = self.body[i];
  1116. if (branch instanceof AST_Default) {
  1117. if (!default_branch) {
  1118. default_branch = branch;
  1119. } else {
  1120. eliminate_branch(branch, body[body.length - 1]);
  1121. }
  1122. } else if (!(value instanceof AST_Node)) {
  1123. var exp = branch.expression.evaluate(compressor);
  1124. if (!(exp instanceof AST_Node) && exp !== value) {
  1125. eliminate_branch(branch, body[body.length - 1]);
  1126. continue;
  1127. }
  1128. if (exp instanceof AST_Node) exp = branch.expression.tail_node().evaluate(compressor);
  1129. if (exp === value) {
  1130. exact_match = branch;
  1131. if (default_branch) {
  1132. var default_index = body.indexOf(default_branch);
  1133. body.splice(default_index, 1);
  1134. eliminate_branch(default_branch, body[default_index - 1]);
  1135. default_branch = null;
  1136. }
  1137. }
  1138. }
  1139. body.push(branch);
  1140. }
  1141. while (i < len) eliminate_branch(self.body[i++], body[body.length - 1]);
  1142. self.body = body;
  1143. let default_or_exact = default_branch || exact_match;
  1144. default_branch = null;
  1145. exact_match = null;
  1146. // group equivalent branches so they will be located next to each other,
  1147. // that way the next micro-optimization will merge them.
  1148. // ** bail micro-optimization if not a simple switch case with breaks
  1149. if (body.every((branch, i) =>
  1150. (branch === default_or_exact || branch.expression instanceof AST_Constant)
  1151. && (branch.body.length === 0 || aborts(branch) || body.length - 1 === i))
  1152. ) {
  1153. for (let i = 0; i < body.length; i++) {
  1154. const branch = body[i];
  1155. for (let j = i + 1; j < body.length; j++) {
  1156. const next = body[j];
  1157. if (next.body.length === 0) continue;
  1158. const last_branch = j === (body.length - 1);
  1159. const equivalentBranch = branches_equivalent(next, branch, false);
  1160. if (equivalentBranch || (last_branch && branches_equivalent(next, branch, true))) {
  1161. if (!equivalentBranch && last_branch) {
  1162. next.body.push(make_node(AST_Break));
  1163. }
  1164. // let's find previous siblings with inert fallthrough...
  1165. let x = j - 1;
  1166. let fallthroughDepth = 0;
  1167. while (x > i) {
  1168. if (is_inert_body(body[x--])) {
  1169. fallthroughDepth++;
  1170. } else {
  1171. break;
  1172. }
  1173. }
  1174. const plucked = body.splice(j - fallthroughDepth, 1 + fallthroughDepth);
  1175. body.splice(i + 1, 0, ...plucked);
  1176. i += plucked.length;
  1177. }
  1178. }
  1179. }
  1180. }
  1181. // merge equivalent branches in a row
  1182. for (let i = 0; i < body.length; i++) {
  1183. let branch = body[i];
  1184. if (branch.body.length === 0) continue;
  1185. if (!aborts(branch)) continue;
  1186. for (let j = i + 1; j < body.length; i++, j++) {
  1187. let next = body[j];
  1188. if (next.body.length === 0) continue;
  1189. if (
  1190. branches_equivalent(next, branch, false)
  1191. || (j === body.length - 1 && branches_equivalent(next, branch, true))
  1192. ) {
  1193. branch.body = [];
  1194. branch = next;
  1195. continue;
  1196. }
  1197. break;
  1198. }
  1199. }
  1200. // Prune any empty branches at the end of the switch statement.
  1201. {
  1202. let i = body.length - 1;
  1203. for (; i >= 0; i--) {
  1204. let bbody = body[i].body;
  1205. if (is_break(bbody[bbody.length - 1], compressor)) bbody.pop();
  1206. if (!is_inert_body(body[i])) break;
  1207. }
  1208. // i now points to the index of a branch that contains a body. By incrementing, it's
  1209. // pointing to the first branch that's empty.
  1210. i++;
  1211. if (!default_or_exact || body.indexOf(default_or_exact) >= i) {
  1212. // The default behavior is to do nothing. We can take advantage of that to
  1213. // remove all case expressions that are side-effect free that also do
  1214. // nothing, since they'll default to doing nothing. But we can't remove any
  1215. // case expressions before one that would side-effect, since they may cause
  1216. // the side-effect to be skipped.
  1217. for (let j = body.length - 1; j >= i; j--) {
  1218. let branch = body[j];
  1219. if (branch === default_or_exact) {
  1220. default_or_exact = null;
  1221. body.pop();
  1222. } else if (!branch.expression.has_side_effects(compressor)) {
  1223. body.pop();
  1224. } else {
  1225. break;
  1226. }
  1227. }
  1228. }
  1229. }
  1230. // Prune side-effect free branches that fall into default.
  1231. DEFAULT: if (default_or_exact) {
  1232. let default_index = body.indexOf(default_or_exact);
  1233. let default_body_index = default_index;
  1234. for (; default_body_index < body.length - 1; default_body_index++) {
  1235. if (!is_inert_body(body[default_body_index])) break;
  1236. }
  1237. if (default_body_index < body.length - 1) {
  1238. break DEFAULT;
  1239. }
  1240. let side_effect_index = body.length - 1;
  1241. for (; side_effect_index >= 0; side_effect_index--) {
  1242. let branch = body[side_effect_index];
  1243. if (branch === default_or_exact) continue;
  1244. if (branch.expression.has_side_effects(compressor)) break;
  1245. }
  1246. // If the default behavior comes after any side-effect case expressions,
  1247. // then we can fold all side-effect free cases into the default branch.
  1248. // If the side-effect case is after the default, then any side-effect
  1249. // free cases could prevent the side-effect from occurring.
  1250. if (default_body_index > side_effect_index) {
  1251. let prev_body_index = default_index - 1;
  1252. for (; prev_body_index >= 0; prev_body_index--) {
  1253. if (!is_inert_body(body[prev_body_index])) break;
  1254. }
  1255. let before = Math.max(side_effect_index, prev_body_index) + 1;
  1256. let after = default_index;
  1257. if (side_effect_index > default_index) {
  1258. // If the default falls into the same body as a side-effect
  1259. // case, then we need preserve that case and only prune the
  1260. // cases after it.
  1261. after = side_effect_index;
  1262. body[side_effect_index].body = body[default_body_index].body;
  1263. } else {
  1264. // The default will be the last branch.
  1265. default_or_exact.body = body[default_body_index].body;
  1266. }
  1267. // Prune everything after the default (or last side-effect case)
  1268. // until the next case with a body.
  1269. body.splice(after + 1, default_body_index - after);
  1270. // Prune everything before the default that falls into it.
  1271. body.splice(before, default_index - before);
  1272. }
  1273. }
  1274. // See if we can remove the switch entirely if all cases (the default) fall into the same case body.
  1275. DEFAULT: if (default_or_exact) {
  1276. let i = body.findIndex(branch => !is_inert_body(branch));
  1277. let caseBody;
  1278. // `i` is equal to one of the following:
  1279. // - `-1`, there is no body in the switch statement.
  1280. // - `body.length - 1`, all cases fall into the same body.
  1281. // - anything else, there are multiple bodies in the switch.
  1282. if (i === body.length - 1) {
  1283. // All cases fall into the case body.
  1284. let branch = body[i];
  1285. if (has_nested_break(self)) break DEFAULT;
  1286. // This is the last case body, and we've already pruned any breaks, so it's
  1287. // safe to hoist.
  1288. caseBody = make_node(AST_BlockStatement, branch, {
  1289. body: branch.body
  1290. });
  1291. branch.body = [];
  1292. } else if (i !== -1) {
  1293. // If there are multiple bodies, then we cannot optimize anything.
  1294. break DEFAULT;
  1295. }
  1296. let sideEffect = body.find(branch => {
  1297. return (
  1298. branch !== default_or_exact
  1299. && branch.expression.has_side_effects(compressor)
  1300. );
  1301. });
  1302. // If no cases cause a side-effect, we can eliminate the switch entirely.
  1303. if (!sideEffect) {
  1304. return make_node(AST_BlockStatement, self, {
  1305. body: decl.concat(
  1306. statement(self.expression),
  1307. default_or_exact.expression ? statement(default_or_exact.expression) : [],
  1308. caseBody || []
  1309. )
  1310. }).optimize(compressor);
  1311. }
  1312. // If we're this far, either there was no body or all cases fell into the same body.
  1313. // If there was no body, then we don't need a default branch (because the default is
  1314. // do nothing). If there was a body, we'll extract it to after the switch, so the
  1315. // switch's new default is to do nothing and we can still prune it.
  1316. const default_index = body.indexOf(default_or_exact);
  1317. body.splice(default_index, 1);
  1318. default_or_exact = null;
  1319. if (caseBody) {
  1320. // Recurse into switch statement one more time so that we can append the case body
  1321. // outside of the switch. This recursion will only happen once since we've pruned
  1322. // the default case.
  1323. return make_node(AST_BlockStatement, self, {
  1324. body: decl.concat(self, caseBody)
  1325. }).optimize(compressor);
  1326. }
  1327. // If we fall here, there is a default branch somewhere, there are no case bodies,
  1328. // and there's a side-effect somewhere. Just let the below paths take care of it.
  1329. }
  1330. if (body.length > 0) {
  1331. body[0].body = decl.concat(body[0].body);
  1332. }
  1333. if (body.length == 0) {
  1334. return make_node(AST_BlockStatement, self, {
  1335. body: decl.concat(statement(self.expression))
  1336. }).optimize(compressor);
  1337. }
  1338. if (body.length == 1 && !has_nested_break(self)) {
  1339. // This is the last case body, and we've already pruned any breaks, so it's
  1340. // safe to hoist.
  1341. let branch = body[0];
  1342. return make_node(AST_If, self, {
  1343. condition: make_node(AST_Binary, self, {
  1344. operator: "===",
  1345. left: self.expression,
  1346. right: branch.expression,
  1347. }),
  1348. body: make_node(AST_BlockStatement, branch, {
  1349. body: branch.body
  1350. }),
  1351. alternative: null
  1352. }).optimize(compressor);
  1353. }
  1354. if (body.length === 2 && default_or_exact && !has_nested_break(self)) {
  1355. let branch = body[0] === default_or_exact ? body[1] : body[0];
  1356. let exact_exp = default_or_exact.expression && statement(default_or_exact.expression);
  1357. if (aborts(body[0])) {
  1358. // Only the first branch body could have a break (at the last statement)
  1359. let first = body[0];
  1360. if (is_break(first.body[first.body.length - 1], compressor)) {
  1361. first.body.pop();
  1362. }
  1363. return make_node(AST_If, self, {
  1364. condition: make_node(AST_Binary, self, {
  1365. operator: "===",
  1366. left: self.expression,
  1367. right: branch.expression,
  1368. }),
  1369. body: make_node(AST_BlockStatement, branch, {
  1370. body: branch.body
  1371. }),
  1372. alternative: make_node(AST_BlockStatement, default_or_exact, {
  1373. body: [].concat(
  1374. exact_exp || [],
  1375. default_or_exact.body
  1376. )
  1377. })
  1378. }).optimize(compressor);
  1379. }
  1380. let operator = "===";
  1381. let consequent = make_node(AST_BlockStatement, branch, {
  1382. body: branch.body,
  1383. });
  1384. let always = make_node(AST_BlockStatement, default_or_exact, {
  1385. body: [].concat(
  1386. exact_exp || [],
  1387. default_or_exact.body
  1388. )
  1389. });
  1390. if (body[0] === default_or_exact) {
  1391. operator = "!==";
  1392. let tmp = always;
  1393. always = consequent;
  1394. consequent = tmp;
  1395. }
  1396. return make_node(AST_BlockStatement, self, {
  1397. body: [
  1398. make_node(AST_If, self, {
  1399. condition: make_node(AST_Binary, self, {
  1400. operator: operator,
  1401. left: self.expression,
  1402. right: branch.expression,
  1403. }),
  1404. body: consequent,
  1405. alternative: null
  1406. })
  1407. ].concat(always)
  1408. }).optimize(compressor);
  1409. }
  1410. return self;
  1411. function eliminate_branch(branch, prev) {
  1412. if (prev && !aborts(prev)) {
  1413. prev.body = prev.body.concat(branch.body);
  1414. } else {
  1415. trim_unreachable_code(compressor, branch, decl);
  1416. }
  1417. }
  1418. function branches_equivalent(branch, prev, insertBreak) {
  1419. let bbody = branch.body;
  1420. let pbody = prev.body;
  1421. if (insertBreak) {
  1422. bbody = bbody.concat(make_node(AST_Break));
  1423. }
  1424. if (bbody.length !== pbody.length) return false;
  1425. let bblock = make_node(AST_BlockStatement, branch, { body: bbody });
  1426. let pblock = make_node(AST_BlockStatement, prev, { body: pbody });
  1427. return bblock.equivalent_to(pblock);
  1428. }
  1429. function statement(expression) {
  1430. return make_node(AST_SimpleStatement, expression, {
  1431. body: expression
  1432. });
  1433. }
  1434. function has_nested_break(root) {
  1435. let has_break = false;
  1436. let tw = new TreeWalker(node => {
  1437. if (has_break) return true;
  1438. if (node instanceof AST_Lambda) return true;
  1439. if (node instanceof AST_SimpleStatement) return true;
  1440. if (!is_break(node, tw)) return;
  1441. let parent = tw.parent();
  1442. if (
  1443. parent instanceof AST_SwitchBranch
  1444. && parent.body[parent.body.length - 1] === node
  1445. ) {
  1446. return;
  1447. }
  1448. has_break = true;
  1449. });
  1450. root.walk(tw);
  1451. return has_break;
  1452. }
  1453. function is_break(node, stack) {
  1454. return node instanceof AST_Break
  1455. && stack.loopcontrol_target(node) === self;
  1456. }
  1457. function is_inert_body(branch) {
  1458. return !aborts(branch) && !make_node(AST_BlockStatement, branch, {
  1459. body: branch.body
  1460. }).has_side_effects(compressor);
  1461. }
  1462. });
  1463. def_optimize(AST_Try, function(self, compressor) {
  1464. if (self.bcatch && self.bfinally && self.bfinally.body.every(is_empty)) self.bfinally = null;
  1465. if (compressor.option("dead_code") && self.body.body.every(is_empty)) {
  1466. var body = [];
  1467. if (self.bcatch) {
  1468. trim_unreachable_code(compressor, self.bcatch, body);
  1469. }
  1470. if (self.bfinally) body.push(...self.bfinally.body);
  1471. return make_node(AST_BlockStatement, self, {
  1472. body: body
  1473. }).optimize(compressor);
  1474. }
  1475. return self;
  1476. });
  1477. AST_Definitions.DEFMETHOD("to_assignments", function(compressor) {
  1478. var reduce_vars = compressor.option("reduce_vars");
  1479. var assignments = [];
  1480. for (const def of this.definitions) {
  1481. if (def.value) {
  1482. var name = make_node(AST_SymbolRef, def.name, def.name);
  1483. assignments.push(make_node(AST_Assign, def, {
  1484. operator : "=",
  1485. logical: false,
  1486. left : name,
  1487. right : def.value
  1488. }));
  1489. if (reduce_vars) name.definition().fixed = false;
  1490. }
  1491. const thedef = def.name.definition();
  1492. thedef.eliminated++;
  1493. thedef.replaced--;
  1494. }
  1495. if (assignments.length == 0) return null;
  1496. return make_sequence(this, assignments);
  1497. });
  1498. def_optimize(AST_Definitions, function(self) {
  1499. if (self.definitions.length == 0) {
  1500. return make_node(AST_EmptyStatement, self);
  1501. }
  1502. return self;
  1503. });
  1504. def_optimize(AST_VarDef, function(self, compressor) {
  1505. if (
  1506. self.name instanceof AST_SymbolLet
  1507. && self.value != null
  1508. && is_undefined(self.value, compressor)
  1509. ) {
  1510. self.value = null;
  1511. }
  1512. return self;
  1513. });
  1514. def_optimize(AST_Import, function(self) {
  1515. return self;
  1516. });
  1517. def_optimize(AST_Call, function(self, compressor) {
  1518. var exp = self.expression;
  1519. var fn = exp;
  1520. inline_array_like_spread(self.args);
  1521. var simple_args = self.args.every((arg) => !(arg instanceof AST_Expansion));
  1522. if (compressor.option("reduce_vars") && fn instanceof AST_SymbolRef) {
  1523. fn = fn.fixed_value();
  1524. }
  1525. var is_func = fn instanceof AST_Lambda;
  1526. if (is_func && fn.pinned()) return self;
  1527. if (compressor.option("unused")
  1528. && simple_args
  1529. && is_func
  1530. && !fn.uses_arguments) {
  1531. var pos = 0, last = 0;
  1532. for (var i = 0, len = self.args.length; i < len; i++) {
  1533. if (fn.argnames[i] instanceof AST_Expansion) {
  1534. if (has_flag(fn.argnames[i].expression, UNUSED)) while (i < len) {
  1535. var node = self.args[i++].drop_side_effect_free(compressor);
  1536. if (node) {
  1537. self.args[pos++] = node;
  1538. }
  1539. } else while (i < len) {
  1540. self.args[pos++] = self.args[i++];
  1541. }
  1542. last = pos;
  1543. break;
  1544. }
  1545. var trim = i >= fn.argnames.length;
  1546. if (trim || has_flag(fn.argnames[i], UNUSED)) {
  1547. var node = self.args[i].drop_side_effect_free(compressor);
  1548. if (node) {
  1549. self.args[pos++] = node;
  1550. } else if (!trim) {
  1551. self.args[pos++] = make_node(AST_Number, self.args[i], {
  1552. value: 0
  1553. });
  1554. continue;
  1555. }
  1556. } else {
  1557. self.args[pos++] = self.args[i];
  1558. }
  1559. last = pos;
  1560. }
  1561. self.args.length = last;
  1562. }
  1563. if (compressor.option("unsafe")) {
  1564. if (exp instanceof AST_Dot && exp.start.value === "Array" && exp.property === "from" && self.args.length === 1) {
  1565. const [argument] = self.args;
  1566. if (argument instanceof AST_Array) {
  1567. return make_node(AST_Array, argument, {
  1568. elements: argument.elements
  1569. }).optimize(compressor);
  1570. }
  1571. }
  1572. if (is_undeclared_ref(exp)) switch (exp.name) {
  1573. case "Array":
  1574. if (self.args.length != 1) {
  1575. return make_node(AST_Array, self, {
  1576. elements: self.args
  1577. }).optimize(compressor);
  1578. } else if (self.args[0] instanceof AST_Number && self.args[0].value <= 11) {
  1579. const elements = [];
  1580. for (let i = 0; i < self.args[0].value; i++) elements.push(new AST_Hole);
  1581. return new AST_Array({ elements });
  1582. }
  1583. break;
  1584. case "Object":
  1585. if (self.args.length == 0) {
  1586. return make_node(AST_Object, self, {
  1587. properties: []
  1588. });
  1589. }
  1590. break;
  1591. case "String":
  1592. if (self.args.length == 0) return make_node(AST_String, self, {
  1593. value: ""
  1594. });
  1595. if (self.args.length <= 1) return make_node(AST_Binary, self, {
  1596. left: self.args[0],
  1597. operator: "+",
  1598. right: make_node(AST_String, self, { value: "" })
  1599. }).optimize(compressor);
  1600. break;
  1601. case "Number":
  1602. if (self.args.length == 0) return make_node(AST_Number, self, {
  1603. value: 0
  1604. });
  1605. if (self.args.length == 1 && compressor.option("unsafe_math")) {
  1606. return make_node(AST_UnaryPrefix, self, {
  1607. expression: self.args[0],
  1608. operator: "+"
  1609. }).optimize(compressor);
  1610. }
  1611. break;
  1612. case "Symbol":
  1613. if (self.args.length == 1 && self.args[0] instanceof AST_String && compressor.option("unsafe_symbols"))
  1614. self.args.length = 0;
  1615. break;
  1616. case "Boolean":
  1617. if (self.args.length == 0) return make_node(AST_False, self);
  1618. if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
  1619. expression: make_node(AST_UnaryPrefix, self, {
  1620. expression: self.args[0],
  1621. operator: "!"
  1622. }),
  1623. operator: "!"
  1624. }).optimize(compressor);
  1625. break;
  1626. case "RegExp":
  1627. var params = [];
  1628. if (self.args.length >= 1
  1629. && self.args.length <= 2
  1630. && self.args.every((arg) => {
  1631. var value = arg.evaluate(compressor);
  1632. params.push(value);
  1633. return arg !== value;
  1634. })
  1635. && regexp_is_safe(params[0])
  1636. ) {
  1637. let [ source, flags ] = params;
  1638. source = regexp_source_fix(new RegExp(source).source);
  1639. const rx = make_node(AST_RegExp, self, {
  1640. value: { source, flags }
  1641. });
  1642. if (rx._eval(compressor) !== rx) {
  1643. return rx;
  1644. }
  1645. }
  1646. break;
  1647. } else if (exp instanceof AST_Dot) switch(exp.property) {
  1648. case "toString":
  1649. if (self.args.length == 0 && !exp.expression.may_throw_on_access(compressor)) {
  1650. return make_node(AST_Binary, self, {
  1651. left: make_node(AST_String, self, { value: "" }),
  1652. operator: "+",
  1653. right: exp.expression
  1654. }).optimize(compressor);
  1655. }
  1656. break;
  1657. case "join":
  1658. if (exp.expression instanceof AST_Array) EXIT: {
  1659. var separator;
  1660. if (self.args.length > 0) {
  1661. separator = self.args[0].evaluate(compressor);
  1662. if (separator === self.args[0]) break EXIT; // not a constant
  1663. }
  1664. var elements = [];
  1665. var consts = [];
  1666. for (var i = 0, len = exp.expression.elements.length; i < len; i++) {
  1667. var el = exp.expression.elements[i];
  1668. if (el instanceof AST_Expansion) break EXIT;
  1669. var value = el.evaluate(compressor);
  1670. if (value !== el) {
  1671. consts.push(value);
  1672. } else {
  1673. if (consts.length > 0) {
  1674. elements.push(make_node(AST_String, self, {
  1675. value: consts.join(separator)
  1676. }));
  1677. consts.length = 0;
  1678. }
  1679. elements.push(el);
  1680. }
  1681. }
  1682. if (consts.length > 0) {
  1683. elements.push(make_node(AST_String, self, {
  1684. value: consts.join(separator)
  1685. }));
  1686. }
  1687. if (elements.length == 0) return make_node(AST_String, self, { value: "" });
  1688. if (elements.length == 1) {
  1689. if (elements[0].is_string(compressor)) {
  1690. return elements[0];
  1691. }
  1692. return make_node(AST_Binary, elements[0], {
  1693. operator : "+",
  1694. left : make_node(AST_String, self, { value: "" }),
  1695. right : elements[0]
  1696. });
  1697. }
  1698. if (separator == "") {
  1699. var first;
  1700. if (elements[0].is_string(compressor)
  1701. || elements[1].is_string(compressor)) {
  1702. first = elements.shift();
  1703. } else {
  1704. first = make_node(AST_String, self, { value: "" });
  1705. }
  1706. return elements.reduce(function(prev, el) {
  1707. return make_node(AST_Binary, el, {
  1708. operator : "+",
  1709. left : prev,
  1710. right : el
  1711. });
  1712. }, first).optimize(compressor);
  1713. }
  1714. // need this awkward cloning to not affect original element
  1715. // best_of will decide which one to get through.
  1716. var node = self.clone();
  1717. node.expression = node.expression.clone();
  1718. node.expression.expression = node.expression.expression.clone();
  1719. node.expression.expression.elements = elements;
  1720. return best_of(compressor, self, node);
  1721. }
  1722. break;
  1723. case "charAt":
  1724. if (exp.expression.is_string(compressor)) {
  1725. var arg = self.args[0];
  1726. var index = arg ? arg.evaluate(compressor) : 0;
  1727. if (index !== arg) {
  1728. return make_node(AST_Sub, exp, {
  1729. expression: exp.expression,
  1730. property: make_node_from_constant(index | 0, arg || exp)
  1731. }).optimize(compressor);
  1732. }
  1733. }
  1734. break;
  1735. case "apply":
  1736. if (self.args.length == 2 && self.args[1] instanceof AST_Array) {
  1737. var args = self.args[1].elements.slice();
  1738. args.unshift(self.args[0]);
  1739. return make_node(AST_Call, self, {
  1740. expression: make_node(AST_Dot, exp, {
  1741. expression: exp.expression,
  1742. optional: false,
  1743. property: "call"
  1744. }),
  1745. args: args
  1746. }).optimize(compressor);
  1747. }
  1748. break;
  1749. case "call":
  1750. var func = exp.expression;
  1751. if (func instanceof AST_SymbolRef) {
  1752. func = func.fixed_value();
  1753. }
  1754. if (func instanceof AST_Lambda && !func.contains_this()) {
  1755. return (self.args.length ? make_sequence(this, [
  1756. self.args[0],
  1757. make_node(AST_Call, self, {
  1758. expression: exp.expression,
  1759. args: self.args.slice(1)
  1760. })
  1761. ]) : make_node(AST_Call, self, {
  1762. expression: exp.expression,
  1763. args: []
  1764. })).optimize(compressor);
  1765. }
  1766. break;
  1767. }
  1768. }
  1769. if (compressor.option("unsafe_Function")
  1770. && is_undeclared_ref(exp)
  1771. && exp.name == "Function") {
  1772. // new Function() => function(){}
  1773. if (self.args.length == 0) return make_node(AST_Function, self, {
  1774. argnames: [],
  1775. body: []
  1776. }).optimize(compressor);
  1777. var nth_identifier = compressor.mangle_options && compressor.mangle_options.nth_identifier || base54;
  1778. if (self.args.every((x) => x instanceof AST_String)) {
  1779. // quite a corner-case, but we can handle it:
  1780. // https://github.com/mishoo/UglifyJS2/issues/203
  1781. // if the code argument is a constant, then we can minify it.
  1782. try {
  1783. var code = "n(function(" + self.args.slice(0, -1).map(function(arg) {
  1784. return arg.value;
  1785. }).join(",") + "){" + self.args[self.args.length - 1].value + "})";
  1786. var ast = parse(code);
  1787. var mangle = { ie8: compressor.option("ie8"), nth_identifier: nth_identifier };
  1788. ast.figure_out_scope(mangle);
  1789. var comp = new Compressor(compressor.options, {
  1790. mangle_options: compressor.mangle_options
  1791. });
  1792. ast = ast.transform(comp);
  1793. ast.figure_out_scope(mangle);
  1794. ast.compute_char_frequency(mangle);
  1795. ast.mangle_names(mangle);
  1796. var fun;
  1797. walk(ast, node => {
  1798. if (is_func_expr(node)) {
  1799. fun = node;
  1800. return walk_abort;
  1801. }
  1802. });
  1803. var code = OutputStream();
  1804. AST_BlockStatement.prototype._codegen.call(fun, fun, code);
  1805. self.args = [
  1806. make_node(AST_String, self, {
  1807. value: fun.argnames.map(function(arg) {
  1808. return arg.print_to_string();
  1809. }).join(",")
  1810. }),
  1811. make_node(AST_String, self.args[self.args.length - 1], {
  1812. value: code.get().replace(/^{|}$/g, "")
  1813. })
  1814. ];
  1815. return self;
  1816. } catch (ex) {
  1817. if (!(ex instanceof JS_Parse_Error)) {
  1818. throw ex;
  1819. }
  1820. // Otherwise, it crashes at runtime. Or maybe it's nonstandard syntax.
  1821. }
  1822. }
  1823. }
  1824. return inline_into_call(self, compressor);
  1825. });
  1826. def_optimize(AST_New, function(self, compressor) {
  1827. if (
  1828. compressor.option("unsafe") &&
  1829. is_undeclared_ref(self.expression) &&
  1830. ["Object", "RegExp", "Function", "Error", "Array"].includes(self.expression.name)
  1831. ) return make_node(AST_Call, self, self).transform(compressor);
  1832. return self;
  1833. });
  1834. def_optimize(AST_Sequence, function(self, compressor) {
  1835. if (!compressor.option("side_effects")) return self;
  1836. var expressions = [];
  1837. filter_for_side_effects();
  1838. var end = expressions.length - 1;
  1839. trim_right_for_undefined();
  1840. if (end == 0) {
  1841. self = maintain_this_binding(compressor.parent(), compressor.self(), expressions[0]);
  1842. if (!(self instanceof AST_Sequence)) self = self.optimize(compressor);
  1843. return self;
  1844. }
  1845. self.expressions = expressions;
  1846. return self;
  1847. function filter_for_side_effects() {
  1848. var first = first_in_statement(compressor);
  1849. var last = self.expressions.length - 1;
  1850. self.expressions.forEach(function(expr, index) {
  1851. if (index < last) expr = expr.drop_side_effect_free(compressor, first);
  1852. if (expr) {
  1853. merge_sequence(expressions, expr);
  1854. first = false;
  1855. }
  1856. });
  1857. }
  1858. function trim_right_for_undefined() {
  1859. while (end > 0 && is_undefined(expressions[end], compressor)) end--;
  1860. if (end < expressions.length - 1) {
  1861. expressions[end] = make_node(AST_UnaryPrefix, self, {
  1862. operator : "void",
  1863. expression : expressions[end]
  1864. });
  1865. expressions.length = end + 1;
  1866. }
  1867. }
  1868. });
  1869. AST_Unary.DEFMETHOD("lift_sequences", function(compressor) {
  1870. if (compressor.option("sequences")) {
  1871. if (this.expression instanceof AST_Sequence) {
  1872. var x = this.expression.expressions.slice();
  1873. var e = this.clone();
  1874. e.expression = x.pop();
  1875. x.push(e);
  1876. return make_sequence(this, x).optimize(compressor);
  1877. }
  1878. }
  1879. return this;
  1880. });
  1881. def_optimize(AST_UnaryPostfix, function(self, compressor) {
  1882. return self.lift_sequences(compressor);
  1883. });
  1884. def_optimize(AST_UnaryPrefix, function(self, compressor) {
  1885. var e = self.expression;
  1886. if (
  1887. self.operator == "delete" &&
  1888. !(
  1889. e instanceof AST_SymbolRef ||
  1890. e instanceof AST_PropAccess ||
  1891. e instanceof AST_Chain ||
  1892. is_identifier_atom(e)
  1893. )
  1894. ) {
  1895. return make_sequence(self, [e, make_node(AST_True, self)]).optimize(compressor);
  1896. }
  1897. var seq = self.lift_sequences(compressor);
  1898. if (seq !== self) {
  1899. return seq;
  1900. }
  1901. if (compressor.option("side_effects") && self.operator == "void") {
  1902. e = e.drop_side_effect_free(compressor);
  1903. if (e) {
  1904. self.expression = e;
  1905. return self;
  1906. } else {
  1907. return make_node(AST_Undefined, self).optimize(compressor);
  1908. }
  1909. }
  1910. if (compressor.in_boolean_context()) {
  1911. switch (self.operator) {
  1912. case "!":
  1913. if (e instanceof AST_UnaryPrefix && e.operator == "!") {
  1914. // !!foo ==> foo, if we're in boolean context
  1915. return e.expression;
  1916. }
  1917. if (e instanceof AST_Binary) {
  1918. self = best_of(compressor, self, e.negate(compressor, first_in_statement(compressor)));
  1919. }
  1920. break;
  1921. case "typeof":
  1922. // typeof always returns a non-empty string, thus it's
  1923. // always true in booleans
  1924. // And we don't need to check if it's undeclared, because in typeof, that's OK
  1925. return (e instanceof AST_SymbolRef ? make_node(AST_True, self) : make_sequence(self, [
  1926. e,
  1927. make_node(AST_True, self)
  1928. ])).optimize(compressor);
  1929. }
  1930. }
  1931. if (self.operator == "-" && e instanceof AST_Infinity) {
  1932. e = e.transform(compressor);
  1933. }
  1934. if (e instanceof AST_Binary
  1935. && (self.operator == "+" || self.operator == "-")
  1936. && (e.operator == "*" || e.operator == "/" || e.operator == "%")) {
  1937. return make_node(AST_Binary, self, {
  1938. operator: e.operator,
  1939. left: make_node(AST_UnaryPrefix, e.left, {
  1940. operator: self.operator,
  1941. expression: e.left
  1942. }),
  1943. right: e.right
  1944. });
  1945. }
  1946. // avoids infinite recursion of numerals
  1947. if (self.operator != "-"
  1948. || !(e instanceof AST_Number || e instanceof AST_Infinity || e instanceof AST_BigInt)) {
  1949. var ev = self.evaluate(compressor);
  1950. if (ev !== self) {
  1951. ev = make_node_from_constant(ev, self).optimize(compressor);
  1952. return best_of(compressor, ev, self);
  1953. }
  1954. }
  1955. return self;
  1956. });
  1957. AST_Binary.DEFMETHOD("lift_sequences", function(compressor) {
  1958. if (compressor.option("sequences")) {
  1959. if (this.left instanceof AST_Sequence) {
  1960. var x = this.left.expressions.slice();
  1961. var e = this.clone();
  1962. e.left = x.pop();
  1963. x.push(e);
  1964. return make_sequence(this, x).optimize(compressor);
  1965. }
  1966. if (this.right instanceof AST_Sequence && !this.left.has_side_effects(compressor)) {
  1967. var assign = this.operator == "=" && this.left instanceof AST_SymbolRef;
  1968. var x = this.right.expressions;
  1969. var last = x.length - 1;
  1970. for (var i = 0; i < last; i++) {
  1971. if (!assign && x[i].has_side_effects(compressor)) break;
  1972. }
  1973. if (i == last) {
  1974. x = x.slice();
  1975. var e = this.clone();
  1976. e.right = x.pop();
  1977. x.push(e);
  1978. return make_sequence(this, x).optimize(compressor);
  1979. } else if (i > 0) {
  1980. var e = this.clone();
  1981. e.right = make_sequence(this.right, x.slice(i));
  1982. x = x.slice(0, i);
  1983. x.push(e);
  1984. return make_sequence(this, x).optimize(compressor);
  1985. }
  1986. }
  1987. }
  1988. return this;
  1989. });
  1990. var commutativeOperators = makePredicate("== === != !== * & | ^");
  1991. function is_object(node) {
  1992. return node instanceof AST_Array
  1993. || node instanceof AST_Lambda
  1994. || node instanceof AST_Object
  1995. || node instanceof AST_Class;
  1996. }
  1997. def_optimize(AST_Binary, function(self, compressor) {
  1998. function reversible() {
  1999. return self.left.is_constant()
  2000. || self.right.is_constant()
  2001. || !self.left.has_side_effects(compressor)
  2002. && !self.right.has_side_effects(compressor);
  2003. }
  2004. function reverse(op) {
  2005. if (reversible()) {
  2006. if (op) self.operator = op;
  2007. var tmp = self.left;
  2008. self.left = self.right;
  2009. self.right = tmp;
  2010. }
  2011. }
  2012. if (compressor.option("lhs_constants") && commutativeOperators.has(self.operator)) {
  2013. if (self.right.is_constant()
  2014. && !self.left.is_constant()) {
  2015. // if right is a constant, whatever side effects the
  2016. // left side might have could not influence the
  2017. // result. hence, force switch.
  2018. if (!(self.left instanceof AST_Binary
  2019. && PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
  2020. reverse();
  2021. }
  2022. }
  2023. }
  2024. self = self.lift_sequences(compressor);
  2025. if (compressor.option("comparisons")) switch (self.operator) {
  2026. case "===":
  2027. case "!==":
  2028. var is_strict_comparison = true;
  2029. if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||
  2030. (self.left.is_number(compressor) && self.right.is_number(compressor)) ||
  2031. (self.left.is_boolean() && self.right.is_boolean()) ||
  2032. self.left.equivalent_to(self.right)) {
  2033. self.operator = self.operator.substr(0, 2);
  2034. }
  2035. // XXX: intentionally falling down to the next case
  2036. case "==":
  2037. case "!=":
  2038. // void 0 == x => null == x
  2039. if (!is_strict_comparison && is_undefined(self.left, compressor)) {
  2040. self.left = make_node(AST_Null, self.left);
  2041. // x == void 0 => x == null
  2042. } else if (!is_strict_comparison && is_undefined(self.right, compressor)) {
  2043. self.right = make_node(AST_Null, self.right);
  2044. } else if (compressor.option("typeofs")
  2045. // "undefined" == typeof x => undefined === x
  2046. && self.left instanceof AST_String
  2047. && self.left.value == "undefined"
  2048. && self.right instanceof AST_UnaryPrefix
  2049. && self.right.operator == "typeof") {
  2050. var expr = self.right.expression;
  2051. if (expr instanceof AST_SymbolRef ? expr.is_declared(compressor)
  2052. : !(expr instanceof AST_PropAccess && compressor.option("ie8"))) {
  2053. self.right = expr;
  2054. self.left = make_node(AST_Undefined, self.left).optimize(compressor);
  2055. if (self.operator.length == 2) self.operator += "=";
  2056. }
  2057. } else if (compressor.option("typeofs")
  2058. // typeof x === "undefined" => x === undefined
  2059. && self.left instanceof AST_UnaryPrefix
  2060. && self.left.operator == "typeof"
  2061. && self.right instanceof AST_String
  2062. && self.right.value == "undefined") {
  2063. var expr = self.left.expression;
  2064. if (expr instanceof AST_SymbolRef ? expr.is_declared(compressor)
  2065. : !(expr instanceof AST_PropAccess && compressor.option("ie8"))) {
  2066. self.left = expr;
  2067. self.right = make_node(AST_Undefined, self.right).optimize(compressor);
  2068. if (self.operator.length == 2) self.operator += "=";
  2069. }
  2070. } else if (self.left instanceof AST_SymbolRef
  2071. // obj !== obj => false
  2072. && self.right instanceof AST_SymbolRef
  2073. && self.left.definition() === self.right.definition()
  2074. && is_object(self.left.fixed_value())) {
  2075. return make_node(self.operator[0] == "=" ? AST_True : AST_False, self);
  2076. }
  2077. break;
  2078. case "&&":
  2079. case "||":
  2080. var lhs = self.left;
  2081. if (lhs.operator == self.operator) {
  2082. lhs = lhs.right;
  2083. }
  2084. if (lhs instanceof AST_Binary
  2085. && lhs.operator == (self.operator == "&&" ? "!==" : "===")
  2086. && self.right instanceof AST_Binary
  2087. && lhs.operator == self.right.operator
  2088. && (is_undefined(lhs.left, compressor) && self.right.left instanceof AST_Null
  2089. || lhs.left instanceof AST_Null && is_undefined(self.right.left, compressor))
  2090. && !lhs.right.has_side_effects(compressor)
  2091. && lhs.right.equivalent_to(self.right.right)) {
  2092. var combined = make_node(AST_Binary, self, {
  2093. operator: lhs.operator.slice(0, -1),
  2094. left: make_node(AST_Null, self),
  2095. right: lhs.right
  2096. });
  2097. if (lhs !== self.left) {
  2098. combined = make_node(AST_Binary, self, {
  2099. operator: self.operator,
  2100. left: self.left.left,
  2101. right: combined
  2102. });
  2103. }
  2104. return combined;
  2105. }
  2106. break;
  2107. }
  2108. if (self.operator == "+" && compressor.in_boolean_context()) {
  2109. var ll = self.left.evaluate(compressor);
  2110. var rr = self.right.evaluate(compressor);
  2111. if (ll && typeof ll == "string") {
  2112. return make_sequence(self, [
  2113. self.right,
  2114. make_node(AST_True, self)
  2115. ]).optimize(compressor);
  2116. }
  2117. if (rr && typeof rr == "string") {
  2118. return make_sequence(self, [
  2119. self.left,
  2120. make_node(AST_True, self)
  2121. ]).optimize(compressor);
  2122. }
  2123. }
  2124. if (compressor.option("comparisons") && self.is_boolean()) {
  2125. if (!(compressor.parent() instanceof AST_Binary)
  2126. || compressor.parent() instanceof AST_Assign) {
  2127. var negated = make_node(AST_UnaryPrefix, self, {
  2128. operator: "!",
  2129. expression: self.negate(compressor, first_in_statement(compressor))
  2130. });
  2131. self = best_of(compressor, self, negated);
  2132. }
  2133. if (compressor.option("unsafe_comps")) {
  2134. switch (self.operator) {
  2135. case "<": reverse(">"); break;
  2136. case "<=": reverse(">="); break;
  2137. }
  2138. }
  2139. }
  2140. if (self.operator == "+") {
  2141. if (self.right instanceof AST_String
  2142. && self.right.getValue() == ""
  2143. && self.left.is_string(compressor)) {
  2144. return self.left;
  2145. }
  2146. if (self.left instanceof AST_String
  2147. && self.left.getValue() == ""
  2148. && self.right.is_string(compressor)) {
  2149. return self.right;
  2150. }
  2151. if (self.left instanceof AST_Binary
  2152. && self.left.operator == "+"
  2153. && self.left.left instanceof AST_String
  2154. && self.left.left.getValue() == ""
  2155. && self.right.is_string(compressor)) {
  2156. self.left = self.left.right;
  2157. return self;
  2158. }
  2159. }
  2160. if (compressor.option("evaluate")) {
  2161. switch (self.operator) {
  2162. case "&&":
  2163. var ll = has_flag(self.left, TRUTHY)
  2164. ? true
  2165. : has_flag(self.left, FALSY)
  2166. ? false
  2167. : self.left.evaluate(compressor);
  2168. if (!ll) {
  2169. return maintain_this_binding(compressor.parent(), compressor.self(), self.left).optimize(compressor);
  2170. } else if (!(ll instanceof AST_Node)) {
  2171. return make_sequence(self, [ self.left, self.right ]).optimize(compressor);
  2172. }
  2173. var rr = self.right.evaluate(compressor);
  2174. if (!rr) {
  2175. if (compressor.in_boolean_context()) {
  2176. return make_sequence(self, [
  2177. self.left,
  2178. make_node(AST_False, self)
  2179. ]).optimize(compressor);
  2180. } else {
  2181. set_flag(self, FALSY);
  2182. }
  2183. } else if (!(rr instanceof AST_Node)) {
  2184. var parent = compressor.parent();
  2185. if (parent.operator == "&&" && parent.left === compressor.self() || compressor.in_boolean_context()) {
  2186. return self.left.optimize(compressor);
  2187. }
  2188. }
  2189. // x || false && y ---> x ? y : false
  2190. if (self.left.operator == "||") {
  2191. var lr = self.left.right.evaluate(compressor);
  2192. if (!lr) return make_node(AST_Conditional, self, {
  2193. condition: self.left.left,
  2194. consequent: self.right,
  2195. alternative: self.left.right
  2196. }).optimize(compressor);
  2197. }
  2198. break;
  2199. case "||":
  2200. var ll = has_flag(self.left, TRUTHY)
  2201. ? true
  2202. : has_flag(self.left, FALSY)
  2203. ? false
  2204. : self.left.evaluate(compressor);
  2205. if (!ll) {
  2206. return make_sequence(self, [ self.left, self.right ]).optimize(compressor);
  2207. } else if (!(ll instanceof AST_Node)) {
  2208. return maintain_this_binding(compressor.parent(), compressor.self(), self.left).optimize(compressor);
  2209. }
  2210. var rr = self.right.evaluate(compressor);
  2211. if (!rr) {
  2212. var parent = compressor.parent();
  2213. if (parent.operator == "||" && parent.left === compressor.self() || compressor.in_boolean_context()) {
  2214. return self.left.optimize(compressor);
  2215. }
  2216. } else if (!(rr instanceof AST_Node)) {
  2217. if (compressor.in_boolean_context()) {
  2218. return make_sequence(self, [
  2219. self.left,
  2220. make_node(AST_True, self)
  2221. ]).optimize(compressor);
  2222. } else {
  2223. set_flag(self, TRUTHY);
  2224. }
  2225. }
  2226. if (self.left.operator == "&&") {
  2227. var lr = self.left.right.evaluate(compressor);
  2228. if (lr && !(lr instanceof AST_Node)) return make_node(AST_Conditional, self, {
  2229. condition: self.left.left,
  2230. consequent: self.left.right,
  2231. alternative: self.right
  2232. }).optimize(compressor);
  2233. }
  2234. break;
  2235. case "??":
  2236. if (is_nullish(self.left, compressor)) {
  2237. return self.right;
  2238. }
  2239. var ll = self.left.evaluate(compressor);
  2240. if (!(ll instanceof AST_Node)) {
  2241. // if we know the value for sure we can simply compute right away.
  2242. return ll == null ? self.right : self.left;
  2243. }
  2244. if (compressor.in_boolean_context()) {
  2245. const rr = self.right.evaluate(compressor);
  2246. if (!(rr instanceof AST_Node) && !rr) {
  2247. return self.left;
  2248. }
  2249. }
  2250. }
  2251. var associative = true;
  2252. switch (self.operator) {
  2253. case "+":
  2254. // (x + "foo") + "bar" => x + "foobar"
  2255. if (self.right instanceof AST_Constant
  2256. && self.left instanceof AST_Binary
  2257. && self.left.operator == "+"
  2258. && self.left.is_string(compressor)) {
  2259. var binary = make_node(AST_Binary, self, {
  2260. operator: "+",
  2261. left: self.left.right,
  2262. right: self.right,
  2263. });
  2264. var r = binary.optimize(compressor);
  2265. if (binary !== r) {
  2266. self = make_node(AST_Binary, self, {
  2267. operator: "+",
  2268. left: self.left.left,
  2269. right: r
  2270. });
  2271. }
  2272. }
  2273. // (x + "foo") + ("bar" + y) => (x + "foobar") + y
  2274. if (self.left instanceof AST_Binary
  2275. && self.left.operator == "+"
  2276. && self.left.is_string(compressor)
  2277. && self.right instanceof AST_Binary
  2278. && self.right.operator == "+"
  2279. && self.right.is_string(compressor)) {
  2280. var binary = make_node(AST_Binary, self, {
  2281. operator: "+",
  2282. left: self.left.right,
  2283. right: self.right.left,
  2284. });
  2285. var m = binary.optimize(compressor);
  2286. if (binary !== m) {
  2287. self = make_node(AST_Binary, self, {
  2288. operator: "+",
  2289. left: make_node(AST_Binary, self.left, {
  2290. operator: "+",
  2291. left: self.left.left,
  2292. right: m
  2293. }),
  2294. right: self.right.right
  2295. });
  2296. }
  2297. }
  2298. // a + -b => a - b
  2299. if (self.right instanceof AST_UnaryPrefix
  2300. && self.right.operator == "-"
  2301. && self.left.is_number(compressor)) {
  2302. self = make_node(AST_Binary, self, {
  2303. operator: "-",
  2304. left: self.left,
  2305. right: self.right.expression
  2306. });
  2307. break;
  2308. }
  2309. // -a + b => b - a
  2310. if (self.left instanceof AST_UnaryPrefix
  2311. && self.left.operator == "-"
  2312. && reversible()
  2313. && self.right.is_number(compressor)) {
  2314. self = make_node(AST_Binary, self, {
  2315. operator: "-",
  2316. left: self.right,
  2317. right: self.left.expression
  2318. });
  2319. break;
  2320. }
  2321. // `foo${bar}baz` + 1 => `foo${bar}baz1`
  2322. if (self.left instanceof AST_TemplateString) {
  2323. var l = self.left;
  2324. var r = self.right.evaluate(compressor);
  2325. if (r != self.right) {
  2326. l.segments[l.segments.length - 1].value += String(r);
  2327. return l;
  2328. }
  2329. }
  2330. // 1 + `foo${bar}baz` => `1foo${bar}baz`
  2331. if (self.right instanceof AST_TemplateString) {
  2332. var r = self.right;
  2333. var l = self.left.evaluate(compressor);
  2334. if (l != self.left) {
  2335. r.segments[0].value = String(l) + r.segments[0].value;
  2336. return r;
  2337. }
  2338. }
  2339. // `1${bar}2` + `foo${bar}baz` => `1${bar}2foo${bar}baz`
  2340. if (self.left instanceof AST_TemplateString
  2341. && self.right instanceof AST_TemplateString) {
  2342. var l = self.left;
  2343. var segments = l.segments;
  2344. var r = self.right;
  2345. segments[segments.length - 1].value += r.segments[0].value;
  2346. for (var i = 1; i < r.segments.length; i++) {
  2347. segments.push(r.segments[i]);
  2348. }
  2349. return l;
  2350. }
  2351. case "*":
  2352. associative = compressor.option("unsafe_math");
  2353. case "&":
  2354. case "|":
  2355. case "^":
  2356. // a + +b => +b + a
  2357. if (self.left.is_number(compressor)
  2358. && self.right.is_number(compressor)
  2359. && reversible()
  2360. && !(self.left instanceof AST_Binary
  2361. && self.left.operator != self.operator
  2362. && PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
  2363. var reversed = make_node(AST_Binary, self, {
  2364. operator: self.operator,
  2365. left: self.right,
  2366. right: self.left
  2367. });
  2368. if (self.right instanceof AST_Constant
  2369. && !(self.left instanceof AST_Constant)) {
  2370. self = best_of(compressor, reversed, self);
  2371. } else {
  2372. self = best_of(compressor, self, reversed);
  2373. }
  2374. }
  2375. if (associative && self.is_number(compressor)) {
  2376. // a + (b + c) => (a + b) + c
  2377. if (self.right instanceof AST_Binary
  2378. && self.right.operator == self.operator) {
  2379. self = make_node(AST_Binary, self, {
  2380. operator: self.operator,
  2381. left: make_node(AST_Binary, self.left, {
  2382. operator: self.operator,
  2383. left: self.left,
  2384. right: self.right.left,
  2385. start: self.left.start,
  2386. end: self.right.left.end
  2387. }),
  2388. right: self.right.right
  2389. });
  2390. }
  2391. // (n + 2) + 3 => 5 + n
  2392. // (2 * n) * 3 => 6 + n
  2393. if (self.right instanceof AST_Constant
  2394. && self.left instanceof AST_Binary
  2395. && self.left.operator == self.operator) {
  2396. if (self.left.left instanceof AST_Constant) {
  2397. self = make_node(AST_Binary, self, {
  2398. operator: self.operator,
  2399. left: make_node(AST_Binary, self.left, {
  2400. operator: self.operator,
  2401. left: self.left.left,
  2402. right: self.right,
  2403. start: self.left.left.start,
  2404. end: self.right.end
  2405. }),
  2406. right: self.left.right
  2407. });
  2408. } else if (self.left.right instanceof AST_Constant) {
  2409. self = make_node(AST_Binary, self, {
  2410. operator: self.operator,
  2411. left: make_node(AST_Binary, self.left, {
  2412. operator: self.operator,
  2413. left: self.left.right,
  2414. right: self.right,
  2415. start: self.left.right.start,
  2416. end: self.right.end
  2417. }),
  2418. right: self.left.left
  2419. });
  2420. }
  2421. }
  2422. // (a | 1) | (2 | d) => (3 | a) | b
  2423. if (self.left instanceof AST_Binary
  2424. && self.left.operator == self.operator
  2425. && self.left.right instanceof AST_Constant
  2426. && self.right instanceof AST_Binary
  2427. && self.right.operator == self.operator
  2428. && self.right.left instanceof AST_Constant) {
  2429. self = make_node(AST_Binary, self, {
  2430. operator: self.operator,
  2431. left: make_node(AST_Binary, self.left, {
  2432. operator: self.operator,
  2433. left: make_node(AST_Binary, self.left.left, {
  2434. operator: self.operator,
  2435. left: self.left.right,
  2436. right: self.right.left,
  2437. start: self.left.right.start,
  2438. end: self.right.left.end
  2439. }),
  2440. right: self.left.left
  2441. }),
  2442. right: self.right.right
  2443. });
  2444. }
  2445. }
  2446. }
  2447. }
  2448. // x && (y && z) ==> x && y && z
  2449. // x || (y || z) ==> x || y || z
  2450. // x + ("y" + z) ==> x + "y" + z
  2451. // "x" + (y + "z")==> "x" + y + "z"
  2452. if (self.right instanceof AST_Binary
  2453. && self.right.operator == self.operator
  2454. && (lazy_op.has(self.operator)
  2455. || (self.operator == "+"
  2456. && (self.right.left.is_string(compressor)
  2457. || (self.left.is_string(compressor)
  2458. && self.right.right.is_string(compressor)))))
  2459. ) {
  2460. self.left = make_node(AST_Binary, self.left, {
  2461. operator : self.operator,
  2462. left : self.left.transform(compressor),
  2463. right : self.right.left.transform(compressor)
  2464. });
  2465. self.right = self.right.right.transform(compressor);
  2466. return self.transform(compressor);
  2467. }
  2468. var ev = self.evaluate(compressor);
  2469. if (ev !== self) {
  2470. ev = make_node_from_constant(ev, self).optimize(compressor);
  2471. return best_of(compressor, ev, self);
  2472. }
  2473. return self;
  2474. });
  2475. def_optimize(AST_SymbolExport, function(self) {
  2476. return self;
  2477. });
  2478. def_optimize(AST_SymbolRef, function(self, compressor) {
  2479. if (
  2480. !compressor.option("ie8")
  2481. && is_undeclared_ref(self)
  2482. && !compressor.find_parent(AST_With)
  2483. ) {
  2484. switch (self.name) {
  2485. case "undefined":
  2486. return make_node(AST_Undefined, self).optimize(compressor);
  2487. case "NaN":
  2488. return make_node(AST_NaN, self).optimize(compressor);
  2489. case "Infinity":
  2490. return make_node(AST_Infinity, self).optimize(compressor);
  2491. }
  2492. }
  2493. if (compressor.option("reduce_vars") && !compressor.is_lhs()) {
  2494. return inline_into_symbolref(self, compressor);
  2495. } else {
  2496. return self;
  2497. }
  2498. });
  2499. function is_atomic(lhs, self) {
  2500. return lhs instanceof AST_SymbolRef || lhs.TYPE === self.TYPE;
  2501. }
  2502. def_optimize(AST_Undefined, function(self, compressor) {
  2503. if (compressor.option("unsafe_undefined")) {
  2504. var undef = find_variable(compressor, "undefined");
  2505. if (undef) {
  2506. var ref = make_node(AST_SymbolRef, self, {
  2507. name : "undefined",
  2508. scope : undef.scope,
  2509. thedef : undef
  2510. });
  2511. set_flag(ref, UNDEFINED);
  2512. return ref;
  2513. }
  2514. }
  2515. var lhs = compressor.is_lhs();
  2516. if (lhs && is_atomic(lhs, self)) return self;
  2517. return make_node(AST_UnaryPrefix, self, {
  2518. operator: "void",
  2519. expression: make_node(AST_Number, self, {
  2520. value: 0
  2521. })
  2522. });
  2523. });
  2524. def_optimize(AST_Infinity, function(self, compressor) {
  2525. var lhs = compressor.is_lhs();
  2526. if (lhs && is_atomic(lhs, self)) return self;
  2527. if (
  2528. compressor.option("keep_infinity")
  2529. && !(lhs && !is_atomic(lhs, self))
  2530. && !find_variable(compressor, "Infinity")
  2531. ) {
  2532. return self;
  2533. }
  2534. return make_node(AST_Binary, self, {
  2535. operator: "/",
  2536. left: make_node(AST_Number, self, {
  2537. value: 1
  2538. }),
  2539. right: make_node(AST_Number, self, {
  2540. value: 0
  2541. })
  2542. });
  2543. });
  2544. def_optimize(AST_NaN, function(self, compressor) {
  2545. var lhs = compressor.is_lhs();
  2546. if (lhs && !is_atomic(lhs, self)
  2547. || find_variable(compressor, "NaN")) {
  2548. return make_node(AST_Binary, self, {
  2549. operator: "/",
  2550. left: make_node(AST_Number, self, {
  2551. value: 0
  2552. }),
  2553. right: make_node(AST_Number, self, {
  2554. value: 0
  2555. })
  2556. });
  2557. }
  2558. return self;
  2559. });
  2560. const ASSIGN_OPS = makePredicate("+ - / * % >> << >>> | ^ &");
  2561. const ASSIGN_OPS_COMMUTATIVE = makePredicate("* | ^ &");
  2562. def_optimize(AST_Assign, function(self, compressor) {
  2563. if (self.logical) {
  2564. return self.lift_sequences(compressor);
  2565. }
  2566. var def;
  2567. // x = x ---> x
  2568. if (
  2569. self.operator === "="
  2570. && self.left instanceof AST_SymbolRef
  2571. && self.left.name !== "arguments"
  2572. && !(def = self.left.definition()).undeclared
  2573. && self.right.equivalent_to(self.left)
  2574. ) {
  2575. return self.right;
  2576. }
  2577. if (compressor.option("dead_code")
  2578. && self.left instanceof AST_SymbolRef
  2579. && (def = self.left.definition()).scope === compressor.find_parent(AST_Lambda)) {
  2580. var level = 0, node, parent = self;
  2581. do {
  2582. node = parent;
  2583. parent = compressor.parent(level++);
  2584. if (parent instanceof AST_Exit) {
  2585. if (in_try(level, parent)) break;
  2586. if (is_reachable(def.scope, [ def ])) break;
  2587. if (self.operator == "=") return self.right;
  2588. def.fixed = false;
  2589. return make_node(AST_Binary, self, {
  2590. operator: self.operator.slice(0, -1),
  2591. left: self.left,
  2592. right: self.right
  2593. }).optimize(compressor);
  2594. }
  2595. } while (parent instanceof AST_Binary && parent.right === node
  2596. || parent instanceof AST_Sequence && parent.tail_node() === node);
  2597. }
  2598. self = self.lift_sequences(compressor);
  2599. if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) {
  2600. // x = expr1 OP expr2
  2601. if (self.right.left instanceof AST_SymbolRef
  2602. && self.right.left.name == self.left.name
  2603. && ASSIGN_OPS.has(self.right.operator)) {
  2604. // x = x - 2 ---> x -= 2
  2605. self.operator = self.right.operator + "=";
  2606. self.right = self.right.right;
  2607. } else if (self.right.right instanceof AST_SymbolRef
  2608. && self.right.right.name == self.left.name
  2609. && ASSIGN_OPS_COMMUTATIVE.has(self.right.operator)
  2610. && !self.right.left.has_side_effects(compressor)) {
  2611. // x = 2 & x ---> x &= 2
  2612. self.operator = self.right.operator + "=";
  2613. self.right = self.right.left;
  2614. }
  2615. }
  2616. return self;
  2617. function in_try(level, node) {
  2618. function may_assignment_throw() {
  2619. const right = self.right;
  2620. self.right = make_node(AST_Null, right);
  2621. const may_throw = node.may_throw(compressor);
  2622. self.right = right;
  2623. return may_throw;
  2624. }
  2625. var stop_at = self.left.definition().scope.get_defun_scope();
  2626. var parent;
  2627. while ((parent = compressor.parent(level++)) !== stop_at) {
  2628. if (parent instanceof AST_Try) {
  2629. if (parent.bfinally) return true;
  2630. if (parent.bcatch && may_assignment_throw()) return true;
  2631. }
  2632. }
  2633. }
  2634. });
  2635. def_optimize(AST_DefaultAssign, function(self, compressor) {
  2636. if (!compressor.option("evaluate")) {
  2637. return self;
  2638. }
  2639. var evaluateRight = self.right.evaluate(compressor);
  2640. // `[x = undefined] = foo` ---> `[x] = foo`
  2641. // `(arg = undefined) => ...` ---> `(arg) => ...` (unless `keep_fargs`)
  2642. // `((arg = undefined) => ...)()` ---> `((arg) => ...)()`
  2643. let lambda, iife;
  2644. if (evaluateRight === undefined) {
  2645. if (
  2646. (lambda = compressor.parent()) instanceof AST_Lambda
  2647. ? (
  2648. compressor.option("keep_fargs") === false
  2649. || (iife = compressor.parent(1)).TYPE === "Call"
  2650. && iife.expression === lambda
  2651. )
  2652. : true
  2653. ) {
  2654. self = self.left;
  2655. }
  2656. } else if (evaluateRight !== self.right) {
  2657. evaluateRight = make_node_from_constant(evaluateRight, self.right);
  2658. self.right = best_of_expression(evaluateRight, self.right);
  2659. }
  2660. return self;
  2661. });
  2662. function is_nullish_check(check, check_subject, compressor) {
  2663. if (check_subject.may_throw(compressor)) return false;
  2664. let nullish_side;
  2665. // foo == null
  2666. if (
  2667. check instanceof AST_Binary
  2668. && check.operator === "=="
  2669. // which side is nullish?
  2670. && (
  2671. (nullish_side = is_nullish(check.left, compressor) && check.left)
  2672. || (nullish_side = is_nullish(check.right, compressor) && check.right)
  2673. )
  2674. // is the other side the same as the check_subject
  2675. && (
  2676. nullish_side === check.left
  2677. ? check.right
  2678. : check.left
  2679. ).equivalent_to(check_subject)
  2680. ) {
  2681. return true;
  2682. }
  2683. // foo === null || foo === undefined
  2684. if (check instanceof AST_Binary && check.operator === "||") {
  2685. let null_cmp;
  2686. let undefined_cmp;
  2687. const find_comparison = cmp => {
  2688. if (!(
  2689. cmp instanceof AST_Binary
  2690. && (cmp.operator === "===" || cmp.operator === "==")
  2691. )) {
  2692. return false;
  2693. }
  2694. let found = 0;
  2695. let defined_side;
  2696. if (cmp.left instanceof AST_Null) {
  2697. found++;
  2698. null_cmp = cmp;
  2699. defined_side = cmp.right;
  2700. }
  2701. if (cmp.right instanceof AST_Null) {
  2702. found++;
  2703. null_cmp = cmp;
  2704. defined_side = cmp.left;
  2705. }
  2706. if (is_undefined(cmp.left, compressor)) {
  2707. found++;
  2708. undefined_cmp = cmp;
  2709. defined_side = cmp.right;
  2710. }
  2711. if (is_undefined(cmp.right, compressor)) {
  2712. found++;
  2713. undefined_cmp = cmp;
  2714. defined_side = cmp.left;
  2715. }
  2716. if (found !== 1) {
  2717. return false;
  2718. }
  2719. if (!defined_side.equivalent_to(check_subject)) {
  2720. return false;
  2721. }
  2722. return true;
  2723. };
  2724. if (!find_comparison(check.left)) return false;
  2725. if (!find_comparison(check.right)) return false;
  2726. if (null_cmp && undefined_cmp && null_cmp !== undefined_cmp) {
  2727. return true;
  2728. }
  2729. }
  2730. return false;
  2731. }
  2732. def_optimize(AST_Conditional, function(self, compressor) {
  2733. if (!compressor.option("conditionals")) return self;
  2734. // This looks like lift_sequences(), should probably be under "sequences"
  2735. if (self.condition instanceof AST_Sequence) {
  2736. var expressions = self.condition.expressions.slice();
  2737. self.condition = expressions.pop();
  2738. expressions.push(self);
  2739. return make_sequence(self, expressions);
  2740. }
  2741. var cond = self.condition.evaluate(compressor);
  2742. if (cond !== self.condition) {
  2743. if (cond) {
  2744. return maintain_this_binding(compressor.parent(), compressor.self(), self.consequent);
  2745. } else {
  2746. return maintain_this_binding(compressor.parent(), compressor.self(), self.alternative);
  2747. }
  2748. }
  2749. var negated = cond.negate(compressor, first_in_statement(compressor));
  2750. if (best_of(compressor, cond, negated) === negated) {
  2751. self = make_node(AST_Conditional, self, {
  2752. condition: negated,
  2753. consequent: self.alternative,
  2754. alternative: self.consequent
  2755. });
  2756. }
  2757. var condition = self.condition;
  2758. var consequent = self.consequent;
  2759. var alternative = self.alternative;
  2760. // x?x:y --> x||y
  2761. if (condition instanceof AST_SymbolRef
  2762. && consequent instanceof AST_SymbolRef
  2763. && condition.definition() === consequent.definition()) {
  2764. return make_node(AST_Binary, self, {
  2765. operator: "||",
  2766. left: condition,
  2767. right: alternative
  2768. });
  2769. }
  2770. // if (foo) exp = something; else exp = something_else;
  2771. // |
  2772. // v
  2773. // exp = foo ? something : something_else;
  2774. if (
  2775. consequent instanceof AST_Assign
  2776. && alternative instanceof AST_Assign
  2777. && consequent.operator === alternative.operator
  2778. && consequent.logical === alternative.logical
  2779. && consequent.left.equivalent_to(alternative.left)
  2780. && (!self.condition.has_side_effects(compressor)
  2781. || consequent.operator == "="
  2782. && !consequent.left.has_side_effects(compressor))
  2783. ) {
  2784. return make_node(AST_Assign, self, {
  2785. operator: consequent.operator,
  2786. left: consequent.left,
  2787. logical: consequent.logical,
  2788. right: make_node(AST_Conditional, self, {
  2789. condition: self.condition,
  2790. consequent: consequent.right,
  2791. alternative: alternative.right
  2792. })
  2793. });
  2794. }
  2795. // x ? y(a) : y(b) --> y(x ? a : b)
  2796. var arg_index;
  2797. if (consequent instanceof AST_Call
  2798. && alternative.TYPE === consequent.TYPE
  2799. && consequent.args.length > 0
  2800. && consequent.args.length == alternative.args.length
  2801. && consequent.expression.equivalent_to(alternative.expression)
  2802. && !self.condition.has_side_effects(compressor)
  2803. && !consequent.expression.has_side_effects(compressor)
  2804. && typeof (arg_index = single_arg_diff()) == "number") {
  2805. var node = consequent.clone();
  2806. node.args[arg_index] = make_node(AST_Conditional, self, {
  2807. condition: self.condition,
  2808. consequent: consequent.args[arg_index],
  2809. alternative: alternative.args[arg_index]
  2810. });
  2811. return node;
  2812. }
  2813. // a ? b : c ? b : d --> (a || c) ? b : d
  2814. if (alternative instanceof AST_Conditional
  2815. && consequent.equivalent_to(alternative.consequent)) {
  2816. return make_node(AST_Conditional, self, {
  2817. condition: make_node(AST_Binary, self, {
  2818. operator: "||",
  2819. left: condition,
  2820. right: alternative.condition
  2821. }),
  2822. consequent: consequent,
  2823. alternative: alternative.alternative
  2824. }).optimize(compressor);
  2825. }
  2826. // a == null ? b : a -> a ?? b
  2827. if (
  2828. compressor.option("ecma") >= 2020 &&
  2829. is_nullish_check(condition, alternative, compressor)
  2830. ) {
  2831. return make_node(AST_Binary, self, {
  2832. operator: "??",
  2833. left: alternative,
  2834. right: consequent
  2835. }).optimize(compressor);
  2836. }
  2837. // a ? b : (c, b) --> (a || c), b
  2838. if (alternative instanceof AST_Sequence
  2839. && consequent.equivalent_to(alternative.expressions[alternative.expressions.length - 1])) {
  2840. return make_sequence(self, [
  2841. make_node(AST_Binary, self, {
  2842. operator: "||",
  2843. left: condition,
  2844. right: make_sequence(self, alternative.expressions.slice(0, -1))
  2845. }),
  2846. consequent
  2847. ]).optimize(compressor);
  2848. }
  2849. // a ? b : (c && b) --> (a || c) && b
  2850. if (alternative instanceof AST_Binary
  2851. && alternative.operator == "&&"
  2852. && consequent.equivalent_to(alternative.right)) {
  2853. return make_node(AST_Binary, self, {
  2854. operator: "&&",
  2855. left: make_node(AST_Binary, self, {
  2856. operator: "||",
  2857. left: condition,
  2858. right: alternative.left
  2859. }),
  2860. right: consequent
  2861. }).optimize(compressor);
  2862. }
  2863. // x?y?z:a:a --> x&&y?z:a
  2864. if (consequent instanceof AST_Conditional
  2865. && consequent.alternative.equivalent_to(alternative)) {
  2866. return make_node(AST_Conditional, self, {
  2867. condition: make_node(AST_Binary, self, {
  2868. left: self.condition,
  2869. operator: "&&",
  2870. right: consequent.condition
  2871. }),
  2872. consequent: consequent.consequent,
  2873. alternative: alternative
  2874. });
  2875. }
  2876. // x ? y : y --> x, y
  2877. if (consequent.equivalent_to(alternative)) {
  2878. return make_sequence(self, [
  2879. self.condition,
  2880. consequent
  2881. ]).optimize(compressor);
  2882. }
  2883. // x ? y || z : z --> x && y || z
  2884. if (consequent instanceof AST_Binary
  2885. && consequent.operator == "||"
  2886. && consequent.right.equivalent_to(alternative)) {
  2887. return make_node(AST_Binary, self, {
  2888. operator: "||",
  2889. left: make_node(AST_Binary, self, {
  2890. operator: "&&",
  2891. left: self.condition,
  2892. right: consequent.left
  2893. }),
  2894. right: alternative
  2895. }).optimize(compressor);
  2896. }
  2897. const in_bool = compressor.in_boolean_context();
  2898. if (is_true(self.consequent)) {
  2899. if (is_false(self.alternative)) {
  2900. // c ? true : false ---> !!c
  2901. return booleanize(self.condition);
  2902. }
  2903. // c ? true : x ---> !!c || x
  2904. return make_node(AST_Binary, self, {
  2905. operator: "||",
  2906. left: booleanize(self.condition),
  2907. right: self.alternative
  2908. });
  2909. }
  2910. if (is_false(self.consequent)) {
  2911. if (is_true(self.alternative)) {
  2912. // c ? false : true ---> !c
  2913. return booleanize(self.condition.negate(compressor));
  2914. }
  2915. // c ? false : x ---> !c && x
  2916. return make_node(AST_Binary, self, {
  2917. operator: "&&",
  2918. left: booleanize(self.condition.negate(compressor)),
  2919. right: self.alternative
  2920. });
  2921. }
  2922. if (is_true(self.alternative)) {
  2923. // c ? x : true ---> !c || x
  2924. return make_node(AST_Binary, self, {
  2925. operator: "||",
  2926. left: booleanize(self.condition.negate(compressor)),
  2927. right: self.consequent
  2928. });
  2929. }
  2930. if (is_false(self.alternative)) {
  2931. // c ? x : false ---> !!c && x
  2932. return make_node(AST_Binary, self, {
  2933. operator: "&&",
  2934. left: booleanize(self.condition),
  2935. right: self.consequent
  2936. });
  2937. }
  2938. return self;
  2939. function booleanize(node) {
  2940. if (node.is_boolean()) return node;
  2941. // !!expression
  2942. return make_node(AST_UnaryPrefix, node, {
  2943. operator: "!",
  2944. expression: node.negate(compressor)
  2945. });
  2946. }
  2947. // AST_True or !0
  2948. function is_true(node) {
  2949. return node instanceof AST_True
  2950. || in_bool
  2951. && node instanceof AST_Constant
  2952. && node.getValue()
  2953. || (node instanceof AST_UnaryPrefix
  2954. && node.operator == "!"
  2955. && node.expression instanceof AST_Constant
  2956. && !node.expression.getValue());
  2957. }
  2958. // AST_False or !1
  2959. function is_false(node) {
  2960. return node instanceof AST_False
  2961. || in_bool
  2962. && node instanceof AST_Constant
  2963. && !node.getValue()
  2964. || (node instanceof AST_UnaryPrefix
  2965. && node.operator == "!"
  2966. && node.expression instanceof AST_Constant
  2967. && node.expression.getValue());
  2968. }
  2969. function single_arg_diff() {
  2970. var a = consequent.args;
  2971. var b = alternative.args;
  2972. for (var i = 0, len = a.length; i < len; i++) {
  2973. if (a[i] instanceof AST_Expansion) return;
  2974. if (!a[i].equivalent_to(b[i])) {
  2975. if (b[i] instanceof AST_Expansion) return;
  2976. for (var j = i + 1; j < len; j++) {
  2977. if (a[j] instanceof AST_Expansion) return;
  2978. if (!a[j].equivalent_to(b[j])) return;
  2979. }
  2980. return i;
  2981. }
  2982. }
  2983. }
  2984. });
  2985. def_optimize(AST_Boolean, function(self, compressor) {
  2986. if (compressor.in_boolean_context()) return make_node(AST_Number, self, {
  2987. value: +self.value
  2988. });
  2989. var p = compressor.parent();
  2990. if (compressor.option("booleans_as_integers")) {
  2991. if (p instanceof AST_Binary && (p.operator == "===" || p.operator == "!==")) {
  2992. p.operator = p.operator.replace(/=$/, "");
  2993. }
  2994. return make_node(AST_Number, self, {
  2995. value: +self.value
  2996. });
  2997. }
  2998. if (compressor.option("booleans")) {
  2999. if (p instanceof AST_Binary && (p.operator == "=="
  3000. || p.operator == "!=")) {
  3001. return make_node(AST_Number, self, {
  3002. value: +self.value
  3003. });
  3004. }
  3005. return make_node(AST_UnaryPrefix, self, {
  3006. operator: "!",
  3007. expression: make_node(AST_Number, self, {
  3008. value: 1 - self.value
  3009. })
  3010. });
  3011. }
  3012. return self;
  3013. });
  3014. function safe_to_flatten(value, compressor) {
  3015. if (value instanceof AST_SymbolRef) {
  3016. value = value.fixed_value();
  3017. }
  3018. if (!value) return false;
  3019. if (!(value instanceof AST_Lambda || value instanceof AST_Class)) return true;
  3020. if (!(value instanceof AST_Lambda && value.contains_this())) return true;
  3021. return compressor.parent() instanceof AST_New;
  3022. }
  3023. AST_PropAccess.DEFMETHOD("flatten_object", function(key, compressor) {
  3024. if (!compressor.option("properties")) return;
  3025. if (key === "__proto__") return;
  3026. var arrows = compressor.option("unsafe_arrows") && compressor.option("ecma") >= 2015;
  3027. var expr = this.expression;
  3028. if (expr instanceof AST_Object) {
  3029. var props = expr.properties;
  3030. for (var i = props.length; --i >= 0;) {
  3031. var prop = props[i];
  3032. if ("" + (prop instanceof AST_ConciseMethod ? prop.key.name : prop.key) == key) {
  3033. const all_props_flattenable = props.every((p) =>
  3034. (p instanceof AST_ObjectKeyVal
  3035. || arrows && p instanceof AST_ConciseMethod && !p.is_generator
  3036. )
  3037. && !p.computed_key()
  3038. );
  3039. if (!all_props_flattenable) return;
  3040. if (!safe_to_flatten(prop.value, compressor)) return;
  3041. return make_node(AST_Sub, this, {
  3042. expression: make_node(AST_Array, expr, {
  3043. elements: props.map(function(prop) {
  3044. var v = prop.value;
  3045. if (v instanceof AST_Accessor) {
  3046. v = make_node(AST_Function, v, v);
  3047. }
  3048. var k = prop.key;
  3049. if (k instanceof AST_Node && !(k instanceof AST_SymbolMethod)) {
  3050. return make_sequence(prop, [ k, v ]);
  3051. }
  3052. return v;
  3053. })
  3054. }),
  3055. property: make_node(AST_Number, this, {
  3056. value: i
  3057. })
  3058. });
  3059. }
  3060. }
  3061. }
  3062. });
  3063. def_optimize(AST_Sub, function(self, compressor) {
  3064. var expr = self.expression;
  3065. var prop = self.property;
  3066. if (compressor.option("properties")) {
  3067. var key = prop.evaluate(compressor);
  3068. if (key !== prop) {
  3069. if (typeof key == "string") {
  3070. if (key == "undefined") {
  3071. key = undefined;
  3072. } else {
  3073. var value = parseFloat(key);
  3074. if (value.toString() == key) {
  3075. key = value;
  3076. }
  3077. }
  3078. }
  3079. prop = self.property = best_of_expression(
  3080. prop,
  3081. make_node_from_constant(key, prop).transform(compressor)
  3082. );
  3083. var property = "" + key;
  3084. if (is_basic_identifier_string(property)
  3085. && property.length <= prop.size() + 1) {
  3086. return make_node(AST_Dot, self, {
  3087. expression: expr,
  3088. optional: self.optional,
  3089. property: property,
  3090. quote: prop.quote,
  3091. }).optimize(compressor);
  3092. }
  3093. }
  3094. }
  3095. var fn;
  3096. OPT_ARGUMENTS: if (compressor.option("arguments")
  3097. && expr instanceof AST_SymbolRef
  3098. && expr.name == "arguments"
  3099. && expr.definition().orig.length == 1
  3100. && (fn = expr.scope) instanceof AST_Lambda
  3101. && fn.uses_arguments
  3102. && !(fn instanceof AST_Arrow)
  3103. && prop instanceof AST_Number) {
  3104. var index = prop.getValue();
  3105. var params = new Set();
  3106. var argnames = fn.argnames;
  3107. for (var n = 0; n < argnames.length; n++) {
  3108. if (!(argnames[n] instanceof AST_SymbolFunarg)) {
  3109. break OPT_ARGUMENTS; // destructuring parameter - bail
  3110. }
  3111. var param = argnames[n].name;
  3112. if (params.has(param)) {
  3113. break OPT_ARGUMENTS; // duplicate parameter - bail
  3114. }
  3115. params.add(param);
  3116. }
  3117. var argname = fn.argnames[index];
  3118. if (argname && compressor.has_directive("use strict")) {
  3119. var def = argname.definition();
  3120. if (!compressor.option("reduce_vars") || def.assignments || def.orig.length > 1) {
  3121. argname = null;
  3122. }
  3123. } else if (!argname && !compressor.option("keep_fargs") && index < fn.argnames.length + 5) {
  3124. while (index >= fn.argnames.length) {
  3125. argname = fn.create_symbol(AST_SymbolFunarg, {
  3126. source: fn,
  3127. scope: fn,
  3128. tentative_name: "argument_" + fn.argnames.length,
  3129. });
  3130. fn.argnames.push(argname);
  3131. }
  3132. }
  3133. if (argname) {
  3134. var sym = make_node(AST_SymbolRef, self, argname);
  3135. sym.reference({});
  3136. clear_flag(argname, UNUSED);
  3137. return sym;
  3138. }
  3139. }
  3140. if (compressor.is_lhs()) return self;
  3141. if (key !== prop) {
  3142. var sub = self.flatten_object(property, compressor);
  3143. if (sub) {
  3144. expr = self.expression = sub.expression;
  3145. prop = self.property = sub.property;
  3146. }
  3147. }
  3148. if (compressor.option("properties") && compressor.option("side_effects")
  3149. && prop instanceof AST_Number && expr instanceof AST_Array) {
  3150. var index = prop.getValue();
  3151. var elements = expr.elements;
  3152. var retValue = elements[index];
  3153. FLATTEN: if (safe_to_flatten(retValue, compressor)) {
  3154. var flatten = true;
  3155. var values = [];
  3156. for (var i = elements.length; --i > index;) {
  3157. var value = elements[i].drop_side_effect_free(compressor);
  3158. if (value) {
  3159. values.unshift(value);
  3160. if (flatten && value.has_side_effects(compressor)) flatten = false;
  3161. }
  3162. }
  3163. if (retValue instanceof AST_Expansion) break FLATTEN;
  3164. retValue = retValue instanceof AST_Hole ? make_node(AST_Undefined, retValue) : retValue;
  3165. if (!flatten) values.unshift(retValue);
  3166. while (--i >= 0) {
  3167. var value = elements[i];
  3168. if (value instanceof AST_Expansion) break FLATTEN;
  3169. value = value.drop_side_effect_free(compressor);
  3170. if (value) values.unshift(value);
  3171. else index--;
  3172. }
  3173. if (flatten) {
  3174. values.push(retValue);
  3175. return make_sequence(self, values).optimize(compressor);
  3176. } else return make_node(AST_Sub, self, {
  3177. expression: make_node(AST_Array, expr, {
  3178. elements: values
  3179. }),
  3180. property: make_node(AST_Number, prop, {
  3181. value: index
  3182. })
  3183. });
  3184. }
  3185. }
  3186. var ev = self.evaluate(compressor);
  3187. if (ev !== self) {
  3188. ev = make_node_from_constant(ev, self).optimize(compressor);
  3189. return best_of(compressor, ev, self);
  3190. }
  3191. return self;
  3192. });
  3193. def_optimize(AST_Chain, function (self, compressor) {
  3194. if (is_nullish(self.expression, compressor)) {
  3195. let parent = compressor.parent();
  3196. // It's valid to delete a nullish optional chain, but if we optimized
  3197. // this to `delete undefined` then it would appear to be a syntax error
  3198. // when we try to optimize the delete. Thankfully, `delete 0` is fine.
  3199. if (parent instanceof AST_UnaryPrefix && parent.operator === "delete") {
  3200. return make_node_from_constant(0, self);
  3201. }
  3202. return make_node(AST_Undefined, self);
  3203. }
  3204. return self;
  3205. });
  3206. def_optimize(AST_Dot, function(self, compressor) {
  3207. const parent = compressor.parent();
  3208. if (compressor.is_lhs()) return self;
  3209. if (compressor.option("unsafe_proto")
  3210. && self.expression instanceof AST_Dot
  3211. && self.expression.property == "prototype") {
  3212. var exp = self.expression.expression;
  3213. if (is_undeclared_ref(exp)) switch (exp.name) {
  3214. case "Array":
  3215. self.expression = make_node(AST_Array, self.expression, {
  3216. elements: []
  3217. });
  3218. break;
  3219. case "Function":
  3220. self.expression = make_node(AST_Function, self.expression, {
  3221. argnames: [],
  3222. body: []
  3223. });
  3224. break;
  3225. case "Number":
  3226. self.expression = make_node(AST_Number, self.expression, {
  3227. value: 0
  3228. });
  3229. break;
  3230. case "Object":
  3231. self.expression = make_node(AST_Object, self.expression, {
  3232. properties: []
  3233. });
  3234. break;
  3235. case "RegExp":
  3236. self.expression = make_node(AST_RegExp, self.expression, {
  3237. value: { source: "t", flags: "" }
  3238. });
  3239. break;
  3240. case "String":
  3241. self.expression = make_node(AST_String, self.expression, {
  3242. value: ""
  3243. });
  3244. break;
  3245. }
  3246. }
  3247. if (!(parent instanceof AST_Call) || !has_annotation(parent, _NOINLINE)) {
  3248. const sub = self.flatten_object(self.property, compressor);
  3249. if (sub) return sub.optimize(compressor);
  3250. }
  3251. if (self.expression instanceof AST_PropAccess
  3252. && parent instanceof AST_PropAccess) {
  3253. return self;
  3254. }
  3255. let ev = self.evaluate(compressor);
  3256. if (ev !== self) {
  3257. ev = make_node_from_constant(ev, self).optimize(compressor);
  3258. return best_of(compressor, ev, self);
  3259. }
  3260. return self;
  3261. });
  3262. function literals_in_boolean_context(self, compressor) {
  3263. if (compressor.in_boolean_context()) {
  3264. return best_of(compressor, self, make_sequence(self, [
  3265. self,
  3266. make_node(AST_True, self)
  3267. ]).optimize(compressor));
  3268. }
  3269. return self;
  3270. }
  3271. function inline_array_like_spread(elements) {
  3272. for (var i = 0; i < elements.length; i++) {
  3273. var el = elements[i];
  3274. if (el instanceof AST_Expansion) {
  3275. var expr = el.expression;
  3276. if (
  3277. expr instanceof AST_Array
  3278. && !expr.elements.some(elm => elm instanceof AST_Hole)
  3279. ) {
  3280. elements.splice(i, 1, ...expr.elements);
  3281. // Step back one, as the element at i is now new.
  3282. i--;
  3283. }
  3284. // In array-like spread, spreading a non-iterable value is TypeError.
  3285. // We therefore can’t optimize anything else, unlike with object spread.
  3286. }
  3287. }
  3288. }
  3289. def_optimize(AST_Array, function(self, compressor) {
  3290. var optimized = literals_in_boolean_context(self, compressor);
  3291. if (optimized !== self) {
  3292. return optimized;
  3293. }
  3294. inline_array_like_spread(self.elements);
  3295. return self;
  3296. });
  3297. function inline_object_prop_spread(props, compressor) {
  3298. for (var i = 0; i < props.length; i++) {
  3299. var prop = props[i];
  3300. if (prop instanceof AST_Expansion) {
  3301. const expr = prop.expression;
  3302. if (
  3303. expr instanceof AST_Object
  3304. && expr.properties.every(prop => prop instanceof AST_ObjectKeyVal)
  3305. ) {
  3306. props.splice(i, 1, ...expr.properties);
  3307. // Step back one, as the property at i is now new.
  3308. i--;
  3309. } else if (expr instanceof AST_Constant
  3310. && !(expr instanceof AST_String)) {
  3311. // Unlike array-like spread, in object spread, spreading a
  3312. // non-iterable value silently does nothing; it is thus safe
  3313. // to remove. AST_String is the only iterable AST_Constant.
  3314. props.splice(i, 1);
  3315. i--;
  3316. } else if (is_nullish(expr, compressor)) {
  3317. // Likewise, null and undefined can be silently removed.
  3318. props.splice(i, 1);
  3319. i--;
  3320. }
  3321. }
  3322. }
  3323. }
  3324. def_optimize(AST_Object, function(self, compressor) {
  3325. var optimized = literals_in_boolean_context(self, compressor);
  3326. if (optimized !== self) {
  3327. return optimized;
  3328. }
  3329. inline_object_prop_spread(self.properties, compressor);
  3330. return self;
  3331. });
  3332. def_optimize(AST_RegExp, literals_in_boolean_context);
  3333. def_optimize(AST_Return, function(self, compressor) {
  3334. if (self.value && is_undefined(self.value, compressor)) {
  3335. self.value = null;
  3336. }
  3337. return self;
  3338. });
  3339. def_optimize(AST_Arrow, opt_AST_Lambda);
  3340. def_optimize(AST_Function, function(self, compressor) {
  3341. self = opt_AST_Lambda(self, compressor);
  3342. if (compressor.option("unsafe_arrows")
  3343. && compressor.option("ecma") >= 2015
  3344. && !self.name
  3345. && !self.is_generator
  3346. && !self.uses_arguments
  3347. && !self.pinned()) {
  3348. const uses_this = walk(self, node => {
  3349. if (node instanceof AST_This) return walk_abort;
  3350. });
  3351. if (!uses_this) return make_node(AST_Arrow, self, self).optimize(compressor);
  3352. }
  3353. return self;
  3354. });
  3355. def_optimize(AST_Class, function(self) {
  3356. // HACK to avoid compress failure.
  3357. // AST_Class is not really an AST_Scope/AST_Block as it lacks a body.
  3358. return self;
  3359. });
  3360. def_optimize(AST_ClassStaticBlock, function(self, compressor) {
  3361. tighten_body(self.body, compressor);
  3362. return self;
  3363. });
  3364. def_optimize(AST_Yield, function(self, compressor) {
  3365. if (self.expression && !self.is_star && is_undefined(self.expression, compressor)) {
  3366. self.expression = null;
  3367. }
  3368. return self;
  3369. });
  3370. def_optimize(AST_TemplateString, function(self, compressor) {
  3371. if (
  3372. !compressor.option("evaluate")
  3373. || compressor.parent() instanceof AST_PrefixedTemplateString
  3374. ) {
  3375. return self;
  3376. }
  3377. var segments = [];
  3378. for (var i = 0; i < self.segments.length; i++) {
  3379. var segment = self.segments[i];
  3380. if (segment instanceof AST_Node) {
  3381. var result = segment.evaluate(compressor);
  3382. // Evaluate to constant value
  3383. // Constant value shorter than ${segment}
  3384. if (result !== segment && (result + "").length <= segment.size() + "${}".length) {
  3385. // There should always be a previous and next segment if segment is a node
  3386. segments[segments.length - 1].value = segments[segments.length - 1].value + result + self.segments[++i].value;
  3387. continue;
  3388. }
  3389. // `before ${`innerBefore ${any} innerAfter`} after` => `before innerBefore ${any} innerAfter after`
  3390. // TODO:
  3391. // `before ${'test' + foo} after` => `before innerBefore ${any} innerAfter after`
  3392. // `before ${foo + 'test} after` => `before innerBefore ${any} innerAfter after`
  3393. if (segment instanceof AST_TemplateString) {
  3394. var inners = segment.segments;
  3395. segments[segments.length - 1].value += inners[0].value;
  3396. for (var j = 1; j < inners.length; j++) {
  3397. segment = inners[j];
  3398. segments.push(segment);
  3399. }
  3400. continue;
  3401. }
  3402. }
  3403. segments.push(segment);
  3404. }
  3405. self.segments = segments;
  3406. // `foo` => "foo"
  3407. if (segments.length == 1) {
  3408. return make_node(AST_String, self, segments[0]);
  3409. }
  3410. if (
  3411. segments.length === 3
  3412. && segments[1] instanceof AST_Node
  3413. && (
  3414. segments[1].is_string(compressor)
  3415. || segments[1].is_number(compressor)
  3416. || is_nullish(segments[1], compressor)
  3417. || compressor.option("unsafe")
  3418. )
  3419. ) {
  3420. // `foo${bar}` => "foo" + bar
  3421. if (segments[2].value === "") {
  3422. return make_node(AST_Binary, self, {
  3423. operator: "+",
  3424. left: make_node(AST_String, self, {
  3425. value: segments[0].value,
  3426. }),
  3427. right: segments[1],
  3428. });
  3429. }
  3430. // `${bar}baz` => bar + "baz"
  3431. if (segments[0].value === "") {
  3432. return make_node(AST_Binary, self, {
  3433. operator: "+",
  3434. left: segments[1],
  3435. right: make_node(AST_String, self, {
  3436. value: segments[2].value,
  3437. }),
  3438. });
  3439. }
  3440. }
  3441. return self;
  3442. });
  3443. def_optimize(AST_PrefixedTemplateString, function(self) {
  3444. return self;
  3445. });
  3446. // ["p"]:1 ---> p:1
  3447. // [42]:1 ---> 42:1
  3448. function lift_key(self, compressor) {
  3449. if (!compressor.option("computed_props")) return self;
  3450. // save a comparison in the typical case
  3451. if (!(self.key instanceof AST_Constant)) return self;
  3452. // allow certain acceptable props as not all AST_Constants are true constants
  3453. if (self.key instanceof AST_String || self.key instanceof AST_Number) {
  3454. if (self.key.value === "__proto__") return self;
  3455. if (self.key.value == "constructor"
  3456. && compressor.parent() instanceof AST_Class) return self;
  3457. if (self instanceof AST_ObjectKeyVal) {
  3458. self.quote = self.key.quote;
  3459. self.key = self.key.value;
  3460. } else if (self instanceof AST_ClassProperty) {
  3461. self.quote = self.key.quote;
  3462. self.key = make_node(AST_SymbolClassProperty, self.key, {
  3463. name: self.key.value
  3464. });
  3465. } else {
  3466. self.quote = self.key.quote;
  3467. self.key = make_node(AST_SymbolMethod, self.key, {
  3468. name: self.key.value
  3469. });
  3470. }
  3471. }
  3472. return self;
  3473. }
  3474. def_optimize(AST_ObjectProperty, lift_key);
  3475. def_optimize(AST_ConciseMethod, function(self, compressor) {
  3476. lift_key(self, compressor);
  3477. // p(){return x;} ---> p:()=>x
  3478. if (compressor.option("arrows")
  3479. && compressor.parent() instanceof AST_Object
  3480. && !self.is_generator
  3481. && !self.value.uses_arguments
  3482. && !self.value.pinned()
  3483. && self.value.body.length == 1
  3484. && self.value.body[0] instanceof AST_Return
  3485. && self.value.body[0].value
  3486. && !self.value.contains_this()) {
  3487. var arrow = make_node(AST_Arrow, self.value, self.value);
  3488. arrow.async = self.async;
  3489. arrow.is_generator = self.is_generator;
  3490. return make_node(AST_ObjectKeyVal, self, {
  3491. key: self.key instanceof AST_SymbolMethod ? self.key.name : self.key,
  3492. value: arrow,
  3493. quote: self.quote,
  3494. });
  3495. }
  3496. return self;
  3497. });
  3498. def_optimize(AST_ObjectKeyVal, function(self, compressor) {
  3499. lift_key(self, compressor);
  3500. // p:function(){} ---> p(){}
  3501. // p:function*(){} ---> *p(){}
  3502. // p:async function(){} ---> async p(){}
  3503. // p:()=>{} ---> p(){}
  3504. // p:async()=>{} ---> async p(){}
  3505. var unsafe_methods = compressor.option("unsafe_methods");
  3506. if (unsafe_methods
  3507. && compressor.option("ecma") >= 2015
  3508. && (!(unsafe_methods instanceof RegExp) || unsafe_methods.test(self.key + ""))) {
  3509. var key = self.key;
  3510. var value = self.value;
  3511. var is_arrow_with_block = value instanceof AST_Arrow
  3512. && Array.isArray(value.body)
  3513. && !value.contains_this();
  3514. if ((is_arrow_with_block || value instanceof AST_Function) && !value.name) {
  3515. return make_node(AST_ConciseMethod, self, {
  3516. async: value.async,
  3517. is_generator: value.is_generator,
  3518. key: key instanceof AST_Node ? key : make_node(AST_SymbolMethod, self, {
  3519. name: key,
  3520. }),
  3521. value: make_node(AST_Accessor, value, value),
  3522. quote: self.quote,
  3523. });
  3524. }
  3525. }
  3526. return self;
  3527. });
  3528. def_optimize(AST_Destructuring, function(self, compressor) {
  3529. if (compressor.option("pure_getters") == true
  3530. && compressor.option("unused")
  3531. && !self.is_array
  3532. && Array.isArray(self.names)
  3533. && !is_destructuring_export_decl(compressor)
  3534. && !(self.names[self.names.length - 1] instanceof AST_Expansion)) {
  3535. var keep = [];
  3536. for (var i = 0; i < self.names.length; i++) {
  3537. var elem = self.names[i];
  3538. if (!(elem instanceof AST_ObjectKeyVal
  3539. && typeof elem.key == "string"
  3540. && elem.value instanceof AST_SymbolDeclaration
  3541. && !should_retain(compressor, elem.value.definition()))) {
  3542. keep.push(elem);
  3543. }
  3544. }
  3545. if (keep.length != self.names.length) {
  3546. self.names = keep;
  3547. }
  3548. }
  3549. return self;
  3550. function is_destructuring_export_decl(compressor) {
  3551. var ancestors = [/^VarDef$/, /^(Const|Let|Var)$/, /^Export$/];
  3552. for (var a = 0, p = 0, len = ancestors.length; a < len; p++) {
  3553. var parent = compressor.parent(p);
  3554. if (!parent) return false;
  3555. if (a === 0 && parent.TYPE == "Destructuring") continue;
  3556. if (!ancestors[a].test(parent.TYPE)) {
  3557. return false;
  3558. }
  3559. a++;
  3560. }
  3561. return true;
  3562. }
  3563. function should_retain(compressor, def) {
  3564. if (def.references.length) return true;
  3565. if (!def.global) return false;
  3566. if (compressor.toplevel.vars) {
  3567. if (compressor.top_retain) {
  3568. return compressor.top_retain(def);
  3569. }
  3570. return false;
  3571. }
  3572. return true;
  3573. }
  3574. });
  3575. export {
  3576. Compressor,
  3577. };