index.js 137 KB

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