compress.js 125 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929
  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. "use strict";
  34. function Compressor(options, false_by_default) {
  35. if (!(this instanceof Compressor))
  36. return new Compressor(options, false_by_default);
  37. TreeTransformer.call(this, this.before, this.after);
  38. this.options = defaults(options, {
  39. sequences : !false_by_default,
  40. properties : !false_by_default,
  41. dead_code : !false_by_default,
  42. drop_debugger : !false_by_default,
  43. unsafe : false,
  44. unsafe_comps : false,
  45. conditionals : !false_by_default,
  46. comparisons : !false_by_default,
  47. evaluate : !false_by_default,
  48. booleans : !false_by_default,
  49. loops : !false_by_default,
  50. unused : !false_by_default,
  51. hoist_funs : !false_by_default,
  52. keep_fargs : true,
  53. keep_fnames : false,
  54. hoist_vars : false,
  55. if_return : !false_by_default,
  56. join_vars : !false_by_default,
  57. collapse_vars : false,
  58. reduce_vars : false,
  59. cascade : !false_by_default,
  60. side_effects : !false_by_default,
  61. pure_getters : false,
  62. pure_funcs : null,
  63. negate_iife : !false_by_default,
  64. screw_ie8 : true,
  65. drop_console : false,
  66. angular : false,
  67. warnings : true,
  68. global_defs : {},
  69. passes : 1,
  70. }, true);
  71. var sequences = this.options["sequences"];
  72. this.sequences_limit = sequences == 1 ? 200 : sequences | 0;
  73. this.warnings_produced = {};
  74. };
  75. Compressor.prototype = new TreeTransformer;
  76. merge(Compressor.prototype, {
  77. option: function(key) { return this.options[key] },
  78. compress: function(node) {
  79. var passes = +this.options.passes || 1;
  80. for (var pass = 0; pass < passes && pass < 3; ++pass) {
  81. if (pass > 0) node.clear_opt_flags();
  82. node = node.transform(this);
  83. }
  84. return node;
  85. },
  86. warn: function(text, props) {
  87. if (this.options.warnings) {
  88. // only emit unique warnings
  89. var message = string_template(text, props);
  90. if (!(message in this.warnings_produced)) {
  91. this.warnings_produced[message] = true;
  92. AST_Node.warn.apply(AST_Node, arguments);
  93. }
  94. }
  95. },
  96. clear_warnings: function() {
  97. this.warnings_produced = {};
  98. },
  99. before: function(node, descend, in_list) {
  100. if (node._squeezed) return node;
  101. var was_scope = false;
  102. if (node instanceof AST_Scope) {
  103. node = node.hoist_declarations(this);
  104. was_scope = true;
  105. }
  106. descend(node, this);
  107. node = node.optimize(this);
  108. if (was_scope && node instanceof AST_Scope) {
  109. node.drop_unused(this);
  110. descend(node, this);
  111. }
  112. node._squeezed = true;
  113. return node;
  114. }
  115. });
  116. (function(){
  117. function OPT(node, optimizer) {
  118. node.DEFMETHOD("optimize", function(compressor){
  119. var self = this;
  120. if (self._optimized) return self;
  121. if (compressor.has_directive("use asm")) return self;
  122. var opt = optimizer(self, compressor);
  123. opt._optimized = true;
  124. if (opt === self) return opt;
  125. return opt.transform(compressor);
  126. });
  127. };
  128. OPT(AST_Node, function(self, compressor){
  129. return self;
  130. });
  131. AST_Node.DEFMETHOD("equivalent_to", function(node){
  132. // XXX: this is a rather expensive way to test two node's equivalence:
  133. return this.print_to_string() == node.print_to_string();
  134. });
  135. AST_Node.DEFMETHOD("clear_opt_flags", function(){
  136. this.walk(new TreeWalker(function(node){
  137. if (!(node instanceof AST_Directive || node instanceof AST_Constant)) {
  138. node._squeezed = false;
  139. node._optimized = false;
  140. }
  141. }));
  142. });
  143. function make_node(ctor, orig, props) {
  144. if (!props) props = {};
  145. if (orig) {
  146. if (!props.start) props.start = orig.start;
  147. if (!props.end) props.end = orig.end;
  148. }
  149. return new ctor(props);
  150. };
  151. function make_node_from_constant(compressor, val, orig) {
  152. // XXX: WIP.
  153. // if (val instanceof AST_Node) return val.transform(new TreeTransformer(null, function(node){
  154. // if (node instanceof AST_SymbolRef) {
  155. // var scope = compressor.find_parent(AST_Scope);
  156. // var def = scope.find_variable(node);
  157. // node.thedef = def;
  158. // return node;
  159. // }
  160. // })).transform(compressor);
  161. if (val instanceof AST_Node) return val.transform(compressor);
  162. switch (typeof val) {
  163. case "string":
  164. return make_node(AST_String, orig, {
  165. value: val
  166. }).optimize(compressor);
  167. case "number":
  168. if (isNaN(val)) {
  169. return make_node(AST_NaN, orig);
  170. }
  171. if ((1 / val) < 0) {
  172. return make_node(AST_UnaryPrefix, orig, {
  173. operator: "-",
  174. expression: make_node(AST_Number, orig, { value: -val })
  175. });
  176. }
  177. return make_node(AST_Number, orig, { value: val }).optimize(compressor);
  178. case "boolean":
  179. return make_node(val ? AST_True : AST_False, orig).optimize(compressor);
  180. case "undefined":
  181. return make_node(AST_Undefined, orig).optimize(compressor);
  182. default:
  183. if (val === null) {
  184. return make_node(AST_Null, orig, { value: null }).optimize(compressor);
  185. }
  186. if (val instanceof RegExp) {
  187. return make_node(AST_RegExp, orig, { value: val }).optimize(compressor);
  188. }
  189. throw new Error(string_template("Can't handle constant of type: {type}", {
  190. type: typeof val
  191. }));
  192. }
  193. };
  194. // we shouldn't compress (1,func)(something) to
  195. // func(something) because that changes the meaning of
  196. // the func (becomes lexical instead of global).
  197. function maintain_this_binding(parent, orig, val) {
  198. if (parent instanceof AST_Call && parent.expression === orig) {
  199. if (val instanceof AST_PropAccess || val instanceof AST_SymbolRef && val.name === "eval") {
  200. return make_node(AST_Seq, orig, {
  201. car: make_node(AST_Number, orig, {
  202. value: 0
  203. }),
  204. cdr: val
  205. });
  206. }
  207. }
  208. return val;
  209. }
  210. function as_statement_array(thing) {
  211. if (thing === null) return [];
  212. if (thing instanceof AST_BlockStatement) return thing.body;
  213. if (thing instanceof AST_EmptyStatement) return [];
  214. if (thing instanceof AST_Statement) return [ thing ];
  215. throw new Error("Can't convert thing to statement array");
  216. };
  217. function is_empty(thing) {
  218. if (thing === null) return true;
  219. if (thing instanceof AST_EmptyStatement) return true;
  220. if (thing instanceof AST_BlockStatement) return thing.body.length == 0;
  221. return false;
  222. };
  223. function loop_body(x) {
  224. if (x instanceof AST_Switch) return x;
  225. if (x instanceof AST_For || x instanceof AST_ForIn || x instanceof AST_DWLoop) {
  226. return (x.body instanceof AST_BlockStatement ? x.body : x);
  227. }
  228. return x;
  229. };
  230. function tighten_body(statements, compressor) {
  231. var CHANGED, max_iter = 10;
  232. do {
  233. CHANGED = false;
  234. if (compressor.option("angular")) {
  235. statements = process_for_angular(statements);
  236. }
  237. statements = eliminate_spurious_blocks(statements);
  238. if (compressor.option("dead_code")) {
  239. statements = eliminate_dead_code(statements, compressor);
  240. }
  241. if (compressor.option("if_return")) {
  242. statements = handle_if_return(statements, compressor);
  243. }
  244. if (compressor.sequences_limit > 0) {
  245. statements = sequencesize(statements, compressor);
  246. }
  247. if (compressor.option("join_vars")) {
  248. statements = join_consecutive_vars(statements, compressor);
  249. }
  250. if (compressor.option("collapse_vars")) {
  251. statements = collapse_single_use_vars(statements, compressor);
  252. }
  253. } while (CHANGED && max_iter-- > 0);
  254. if (compressor.option("negate_iife")) {
  255. negate_iifes(statements, compressor);
  256. }
  257. return statements;
  258. function collapse_single_use_vars(statements, compressor) {
  259. // Iterate statements backwards looking for a statement with a var/const
  260. // declaration immediately preceding it. Grab the rightmost var definition
  261. // and if it has exactly one reference then attempt to replace its reference
  262. // in the statement with the var value and then erase the var definition.
  263. var self = compressor.self();
  264. var var_defs_removed = false;
  265. for (var stat_index = statements.length; --stat_index >= 0;) {
  266. var stat = statements[stat_index];
  267. if (stat instanceof AST_Definitions) continue;
  268. // Process child blocks of statement if present.
  269. [stat, stat.body, stat.alternative, stat.bcatch, stat.bfinally].forEach(function(node) {
  270. node && node.body && collapse_single_use_vars(node.body, compressor);
  271. });
  272. // The variable definition must precede a statement.
  273. if (stat_index <= 0) break;
  274. var prev_stat_index = stat_index - 1;
  275. var prev_stat = statements[prev_stat_index];
  276. if (!(prev_stat instanceof AST_Definitions)) continue;
  277. var var_defs = prev_stat.definitions;
  278. if (var_defs == null) continue;
  279. var var_names_seen = {};
  280. var side_effects_encountered = false;
  281. var lvalues_encountered = false;
  282. var lvalues = {};
  283. // Scan variable definitions from right to left.
  284. for (var var_defs_index = var_defs.length; --var_defs_index >= 0;) {
  285. // Obtain var declaration and var name with basic sanity check.
  286. var var_decl = var_defs[var_defs_index];
  287. if (var_decl.value == null) break;
  288. var var_name = var_decl.name.name;
  289. if (!var_name || !var_name.length) break;
  290. // Bail if we've seen a var definition of same name before.
  291. if (var_name in var_names_seen) break;
  292. var_names_seen[var_name] = true;
  293. // Only interested in cases with just one reference to the variable.
  294. var def = self.find_variable && self.find_variable(var_name);
  295. if (!def || !def.references || def.references.length !== 1 || var_name == "arguments") {
  296. side_effects_encountered = true;
  297. continue;
  298. }
  299. var ref = def.references[0];
  300. // Don't replace ref if eval() or with statement in scope.
  301. if (ref.scope.uses_eval || ref.scope.uses_with) break;
  302. // Constant single use vars can be replaced in any scope.
  303. if (!(var_decl.value instanceof AST_RegExp) && var_decl.value.is_constant(compressor)) {
  304. var ctt = new TreeTransformer(function(node) {
  305. if (node === ref)
  306. return replace_var(node, ctt.parent(), true);
  307. });
  308. stat.transform(ctt);
  309. continue;
  310. }
  311. // Restrict var replacement to constants if side effects encountered.
  312. if (side_effects_encountered |= lvalues_encountered) continue;
  313. // Non-constant single use vars can only be replaced in same scope.
  314. if (ref.scope !== self) {
  315. side_effects_encountered |= var_decl.value.has_side_effects(compressor);
  316. continue;
  317. }
  318. // Detect lvalues in var value.
  319. var tw = new TreeWalker(function(node){
  320. if (node instanceof AST_SymbolRef && is_lvalue(node, tw.parent())) {
  321. lvalues[node.name] = lvalues_encountered = true;
  322. }
  323. });
  324. var_decl.value.walk(tw);
  325. // Replace the non-constant single use var in statement if side effect free.
  326. var unwind = false;
  327. var tt = new TreeTransformer(
  328. function preorder(node) {
  329. if (unwind) return node;
  330. var parent = tt.parent();
  331. if (node instanceof AST_Lambda
  332. || node instanceof AST_Try
  333. || node instanceof AST_With
  334. || node instanceof AST_Case
  335. || node instanceof AST_IterationStatement
  336. || (parent instanceof AST_If && node !== parent.condition)
  337. || (parent instanceof AST_Conditional && node !== parent.condition)
  338. || (parent instanceof AST_Binary
  339. && (parent.operator == "&&" || parent.operator == "||")
  340. && node === parent.right)
  341. || (parent instanceof AST_Switch && node !== parent.expression)) {
  342. return side_effects_encountered = unwind = true, node;
  343. }
  344. },
  345. function postorder(node) {
  346. if (unwind) return node;
  347. if (node === ref)
  348. return unwind = true, replace_var(node, tt.parent(), false);
  349. if (side_effects_encountered |= node.has_side_effects(compressor))
  350. return unwind = true, node;
  351. if (lvalues_encountered && node instanceof AST_SymbolRef && node.name in lvalues) {
  352. side_effects_encountered = true;
  353. return unwind = true, node;
  354. }
  355. }
  356. );
  357. stat.transform(tt);
  358. }
  359. }
  360. // Remove extraneous empty statments in block after removing var definitions.
  361. // Leave at least one statement in `statements`.
  362. if (var_defs_removed) for (var i = statements.length; --i >= 0;) {
  363. if (statements.length > 1 && statements[i] instanceof AST_EmptyStatement)
  364. statements.splice(i, 1);
  365. }
  366. return statements;
  367. function is_lvalue(node, parent) {
  368. return node instanceof AST_SymbolRef && (
  369. (parent instanceof AST_Assign && node === parent.left)
  370. || (parent instanceof AST_Unary && parent.expression === node
  371. && (parent.operator == "++" || parent.operator == "--")));
  372. }
  373. function replace_var(node, parent, is_constant) {
  374. if (is_lvalue(node, parent)) return node;
  375. // Remove var definition and return its value to the TreeTransformer to replace.
  376. var value = maintain_this_binding(parent, node, var_decl.value);
  377. var_decl.value = null;
  378. var_defs.splice(var_defs_index, 1);
  379. if (var_defs.length === 0) {
  380. statements[prev_stat_index] = make_node(AST_EmptyStatement, self);
  381. var_defs_removed = true;
  382. }
  383. // Further optimize statement after substitution.
  384. stat.clear_opt_flags();
  385. compressor.warn("Replacing " + (is_constant ? "constant" : "variable") +
  386. " " + var_name + " [{file}:{line},{col}]", node.start);
  387. CHANGED = true;
  388. return value;
  389. }
  390. }
  391. function process_for_angular(statements) {
  392. function has_inject(comment) {
  393. return /@ngInject/.test(comment.value);
  394. }
  395. function make_arguments_names_list(func) {
  396. return func.argnames.map(function(sym){
  397. return make_node(AST_String, sym, { value: sym.name });
  398. });
  399. }
  400. function make_array(orig, elements) {
  401. return make_node(AST_Array, orig, { elements: elements });
  402. }
  403. function make_injector(func, name) {
  404. return make_node(AST_SimpleStatement, func, {
  405. body: make_node(AST_Assign, func, {
  406. operator: "=",
  407. left: make_node(AST_Dot, name, {
  408. expression: make_node(AST_SymbolRef, name, name),
  409. property: "$inject"
  410. }),
  411. right: make_array(func, make_arguments_names_list(func))
  412. })
  413. });
  414. }
  415. function check_expression(body) {
  416. if (body && body.args) {
  417. // if this is a function call check all of arguments passed
  418. body.args.forEach(function(argument, index, array) {
  419. var comments = argument.start.comments_before;
  420. // if the argument is function preceded by @ngInject
  421. if (argument instanceof AST_Lambda && comments.length && has_inject(comments[0])) {
  422. // replace the function with an array of names of its parameters and function at the end
  423. array[index] = make_array(argument, make_arguments_names_list(argument).concat(argument));
  424. }
  425. });
  426. // if this is chained call check previous one recursively
  427. if (body.expression && body.expression.expression) {
  428. check_expression(body.expression.expression);
  429. }
  430. }
  431. }
  432. return statements.reduce(function(a, stat){
  433. a.push(stat);
  434. if (stat.body && stat.body.args) {
  435. check_expression(stat.body);
  436. } else {
  437. var token = stat.start;
  438. var comments = token.comments_before;
  439. if (comments && comments.length > 0) {
  440. var last = comments.pop();
  441. if (has_inject(last)) {
  442. // case 1: defun
  443. if (stat instanceof AST_Defun) {
  444. a.push(make_injector(stat, stat.name));
  445. }
  446. else if (stat instanceof AST_Definitions) {
  447. stat.definitions.forEach(function(def) {
  448. if (def.value && def.value instanceof AST_Lambda) {
  449. a.push(make_injector(def.value, def.name));
  450. }
  451. });
  452. }
  453. else {
  454. compressor.warn("Unknown statement marked with @ngInject [{file}:{line},{col}]", token);
  455. }
  456. }
  457. }
  458. }
  459. return a;
  460. }, []);
  461. }
  462. function eliminate_spurious_blocks(statements) {
  463. var seen_dirs = [];
  464. return statements.reduce(function(a, stat){
  465. if (stat instanceof AST_BlockStatement) {
  466. CHANGED = true;
  467. a.push.apply(a, eliminate_spurious_blocks(stat.body));
  468. } else if (stat instanceof AST_EmptyStatement) {
  469. CHANGED = true;
  470. } else if (stat instanceof AST_Directive) {
  471. if (seen_dirs.indexOf(stat.value) < 0) {
  472. a.push(stat);
  473. seen_dirs.push(stat.value);
  474. } else {
  475. CHANGED = true;
  476. }
  477. } else {
  478. a.push(stat);
  479. }
  480. return a;
  481. }, []);
  482. };
  483. function handle_if_return(statements, compressor) {
  484. var self = compressor.self();
  485. var multiple_if_returns = has_multiple_if_returns(statements);
  486. var in_lambda = self instanceof AST_Lambda;
  487. var ret = [];
  488. loop: for (var i = statements.length; --i >= 0;) {
  489. var stat = statements[i];
  490. switch (true) {
  491. case (in_lambda && stat instanceof AST_Return && !stat.value && ret.length == 0):
  492. CHANGED = true;
  493. // note, ret.length is probably always zero
  494. // because we drop unreachable code before this
  495. // step. nevertheless, it's good to check.
  496. continue loop;
  497. case stat instanceof AST_If:
  498. if (stat.body instanceof AST_Return) {
  499. //---
  500. // pretty silly case, but:
  501. // if (foo()) return; return; ==> foo(); return;
  502. if (((in_lambda && ret.length == 0)
  503. || (ret[0] instanceof AST_Return && !ret[0].value))
  504. && !stat.body.value && !stat.alternative) {
  505. CHANGED = true;
  506. var cond = make_node(AST_SimpleStatement, stat.condition, {
  507. body: stat.condition
  508. });
  509. ret.unshift(cond);
  510. continue loop;
  511. }
  512. //---
  513. // if (foo()) return x; return y; ==> return foo() ? x : y;
  514. if (ret[0] instanceof AST_Return && stat.body.value && ret[0].value && !stat.alternative) {
  515. CHANGED = true;
  516. stat = stat.clone();
  517. stat.alternative = ret[0];
  518. ret[0] = stat.transform(compressor);
  519. continue loop;
  520. }
  521. //---
  522. // if (foo()) return x; [ return ; ] ==> return foo() ? x : undefined;
  523. if (multiple_if_returns && (ret.length == 0 || ret[0] instanceof AST_Return)
  524. && stat.body.value && !stat.alternative && in_lambda) {
  525. CHANGED = true;
  526. stat = stat.clone();
  527. stat.alternative = ret[0] || make_node(AST_Return, stat, {
  528. value: make_node(AST_Undefined, stat)
  529. });
  530. ret[0] = stat.transform(compressor);
  531. continue loop;
  532. }
  533. //---
  534. // if (foo()) return; [ else x... ]; y... ==> if (!foo()) { x...; y... }
  535. if (!stat.body.value && in_lambda) {
  536. CHANGED = true;
  537. stat = stat.clone();
  538. stat.condition = stat.condition.negate(compressor);
  539. var body = as_statement_array(stat.alternative).concat(ret);
  540. var funs = extract_functions_from_statement_array(body);
  541. stat.body = make_node(AST_BlockStatement, stat, {
  542. body: body
  543. });
  544. stat.alternative = null;
  545. ret = funs.concat([ stat.transform(compressor) ]);
  546. continue loop;
  547. }
  548. //---
  549. // XXX: what was the intention of this case?
  550. // if sequences is not enabled, this can lead to an endless loop (issue #866).
  551. // however, with sequences on this helps producing slightly better output for
  552. // the example code.
  553. if (compressor.option("sequences")
  554. && ret.length == 1 && in_lambda && ret[0] instanceof AST_SimpleStatement
  555. && (!stat.alternative || stat.alternative instanceof AST_SimpleStatement)) {
  556. CHANGED = true;
  557. ret.push(make_node(AST_Return, ret[0], {
  558. value: make_node(AST_Undefined, ret[0])
  559. }).transform(compressor));
  560. ret = as_statement_array(stat.alternative).concat(ret);
  561. ret.unshift(stat);
  562. continue loop;
  563. }
  564. }
  565. var ab = aborts(stat.body);
  566. var lct = ab instanceof AST_LoopControl ? compressor.loopcontrol_target(ab.label) : null;
  567. if (ab && ((ab instanceof AST_Return && !ab.value && in_lambda)
  568. || (ab instanceof AST_Continue && self === loop_body(lct))
  569. || (ab instanceof AST_Break && lct instanceof AST_BlockStatement && self === lct))) {
  570. if (ab.label) {
  571. remove(ab.label.thedef.references, ab);
  572. }
  573. CHANGED = true;
  574. var body = as_statement_array(stat.body).slice(0, -1);
  575. stat = stat.clone();
  576. stat.condition = stat.condition.negate(compressor);
  577. stat.body = make_node(AST_BlockStatement, stat, {
  578. body: as_statement_array(stat.alternative).concat(ret)
  579. });
  580. stat.alternative = make_node(AST_BlockStatement, stat, {
  581. body: body
  582. });
  583. ret = [ stat.transform(compressor) ];
  584. continue loop;
  585. }
  586. var ab = aborts(stat.alternative);
  587. var lct = ab instanceof AST_LoopControl ? compressor.loopcontrol_target(ab.label) : null;
  588. if (ab && ((ab instanceof AST_Return && !ab.value && in_lambda)
  589. || (ab instanceof AST_Continue && self === loop_body(lct))
  590. || (ab instanceof AST_Break && lct instanceof AST_BlockStatement && self === lct))) {
  591. if (ab.label) {
  592. remove(ab.label.thedef.references, ab);
  593. }
  594. CHANGED = true;
  595. stat = stat.clone();
  596. stat.body = make_node(AST_BlockStatement, stat.body, {
  597. body: as_statement_array(stat.body).concat(ret)
  598. });
  599. stat.alternative = make_node(AST_BlockStatement, stat.alternative, {
  600. body: as_statement_array(stat.alternative).slice(0, -1)
  601. });
  602. ret = [ stat.transform(compressor) ];
  603. continue loop;
  604. }
  605. ret.unshift(stat);
  606. break;
  607. default:
  608. ret.unshift(stat);
  609. break;
  610. }
  611. }
  612. return ret;
  613. function has_multiple_if_returns(statements) {
  614. var n = 0;
  615. for (var i = statements.length; --i >= 0;) {
  616. var stat = statements[i];
  617. if (stat instanceof AST_If && stat.body instanceof AST_Return) {
  618. if (++n > 1) return true;
  619. }
  620. }
  621. return false;
  622. }
  623. };
  624. function eliminate_dead_code(statements, compressor) {
  625. var has_quit = false;
  626. var orig = statements.length;
  627. var self = compressor.self();
  628. statements = statements.reduce(function(a, stat){
  629. if (has_quit) {
  630. extract_declarations_from_unreachable_code(compressor, stat, a);
  631. } else {
  632. if (stat instanceof AST_LoopControl) {
  633. var lct = compressor.loopcontrol_target(stat.label);
  634. if ((stat instanceof AST_Break
  635. && lct instanceof AST_BlockStatement
  636. && loop_body(lct) === self) || (stat instanceof AST_Continue
  637. && loop_body(lct) === self)) {
  638. if (stat.label) {
  639. remove(stat.label.thedef.references, stat);
  640. }
  641. } else {
  642. a.push(stat);
  643. }
  644. } else {
  645. a.push(stat);
  646. }
  647. if (aborts(stat)) has_quit = true;
  648. }
  649. return a;
  650. }, []);
  651. CHANGED = statements.length != orig;
  652. return statements;
  653. };
  654. function sequencesize(statements, compressor) {
  655. if (statements.length < 2) return statements;
  656. var seq = [], ret = [];
  657. function push_seq() {
  658. seq = AST_Seq.from_array(seq);
  659. if (seq) ret.push(make_node(AST_SimpleStatement, seq, {
  660. body: seq
  661. }));
  662. seq = [];
  663. };
  664. statements.forEach(function(stat){
  665. if (stat instanceof AST_SimpleStatement && seqLength(seq) < compressor.sequences_limit) {
  666. seq.push(stat.body);
  667. } else {
  668. push_seq();
  669. ret.push(stat);
  670. }
  671. });
  672. push_seq();
  673. ret = sequencesize_2(ret, compressor);
  674. CHANGED = ret.length != statements.length;
  675. return ret;
  676. };
  677. function seqLength(a) {
  678. for (var len = 0, i = 0; i < a.length; ++i) {
  679. var stat = a[i];
  680. if (stat instanceof AST_Seq) {
  681. len += stat.len();
  682. } else {
  683. len++;
  684. }
  685. }
  686. return len;
  687. };
  688. function sequencesize_2(statements, compressor) {
  689. function cons_seq(right) {
  690. ret.pop();
  691. var left = prev.body;
  692. if (left instanceof AST_Seq) {
  693. left.add(right);
  694. } else {
  695. left = AST_Seq.cons(left, right);
  696. }
  697. return left.transform(compressor);
  698. };
  699. var ret = [], prev = null;
  700. statements.forEach(function(stat){
  701. if (prev) {
  702. if (stat instanceof AST_For) {
  703. var opera = {};
  704. try {
  705. prev.body.walk(new TreeWalker(function(node){
  706. if (node instanceof AST_Binary && node.operator == "in")
  707. throw opera;
  708. }));
  709. if (stat.init && !(stat.init instanceof AST_Definitions)) {
  710. stat.init = cons_seq(stat.init);
  711. }
  712. else if (!stat.init) {
  713. stat.init = prev.body;
  714. ret.pop();
  715. }
  716. } catch(ex) {
  717. if (ex !== opera) throw ex;
  718. }
  719. }
  720. else if (stat instanceof AST_If) {
  721. stat.condition = cons_seq(stat.condition);
  722. }
  723. else if (stat instanceof AST_With) {
  724. stat.expression = cons_seq(stat.expression);
  725. }
  726. else if (stat instanceof AST_Exit && stat.value) {
  727. stat.value = cons_seq(stat.value);
  728. }
  729. else if (stat instanceof AST_Exit) {
  730. stat.value = cons_seq(make_node(AST_Undefined, stat));
  731. }
  732. else if (stat instanceof AST_Switch) {
  733. stat.expression = cons_seq(stat.expression);
  734. }
  735. }
  736. ret.push(stat);
  737. prev = stat instanceof AST_SimpleStatement ? stat : null;
  738. });
  739. return ret;
  740. };
  741. function join_consecutive_vars(statements, compressor) {
  742. var prev = null;
  743. return statements.reduce(function(a, stat){
  744. if (stat instanceof AST_Definitions && prev && prev.TYPE == stat.TYPE) {
  745. prev.definitions = prev.definitions.concat(stat.definitions);
  746. CHANGED = true;
  747. }
  748. else if (stat instanceof AST_For
  749. && prev instanceof AST_Var
  750. && (!stat.init || stat.init.TYPE == prev.TYPE)) {
  751. CHANGED = true;
  752. a.pop();
  753. if (stat.init) {
  754. stat.init.definitions = prev.definitions.concat(stat.init.definitions);
  755. } else {
  756. stat.init = prev;
  757. }
  758. a.push(stat);
  759. prev = stat;
  760. }
  761. else {
  762. prev = stat;
  763. a.push(stat);
  764. }
  765. return a;
  766. }, []);
  767. };
  768. function negate_iifes(statements, compressor) {
  769. function is_iife_call(node) {
  770. if (node instanceof AST_Call) {
  771. return node.expression instanceof AST_Function || is_iife_call(node.expression);
  772. }
  773. return false;
  774. }
  775. statements.forEach(function(stat){
  776. if (stat instanceof AST_SimpleStatement) {
  777. stat.body = (function transform(thing) {
  778. return thing.transform(new TreeTransformer(function(node){
  779. if (node instanceof AST_New) {
  780. return node;
  781. }
  782. if (is_iife_call(node)) {
  783. return make_node(AST_UnaryPrefix, node, {
  784. operator: "!",
  785. expression: node
  786. });
  787. }
  788. else if (node instanceof AST_Call) {
  789. node.expression = transform(node.expression);
  790. }
  791. else if (node instanceof AST_Seq) {
  792. node.car = transform(node.car);
  793. }
  794. else if (node instanceof AST_Conditional) {
  795. var expr = transform(node.condition);
  796. if (expr !== node.condition) {
  797. // it has been negated, reverse
  798. node.condition = expr;
  799. var tmp = node.consequent;
  800. node.consequent = node.alternative;
  801. node.alternative = tmp;
  802. }
  803. }
  804. return node;
  805. }));
  806. })(stat.body);
  807. }
  808. });
  809. };
  810. };
  811. function extract_functions_from_statement_array(statements) {
  812. var funs = [];
  813. for (var i = statements.length - 1; i >= 0; --i) {
  814. var stat = statements[i];
  815. if (stat instanceof AST_Defun) {
  816. statements.splice(i, 1);
  817. funs.unshift(stat);
  818. }
  819. }
  820. return funs;
  821. }
  822. function extract_declarations_from_unreachable_code(compressor, stat, target) {
  823. if (!(stat instanceof AST_Defun)) {
  824. compressor.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start);
  825. }
  826. stat.walk(new TreeWalker(function(node){
  827. if (node instanceof AST_Definitions) {
  828. compressor.warn("Declarations in unreachable code! [{file}:{line},{col}]", node.start);
  829. node.remove_initializers();
  830. target.push(node);
  831. return true;
  832. }
  833. if (node instanceof AST_Defun) {
  834. target.push(node);
  835. return true;
  836. }
  837. if (node instanceof AST_Scope) {
  838. return true;
  839. }
  840. }));
  841. };
  842. /* -----[ boolean/negation helpers ]----- */
  843. // methods to determine whether an expression has a boolean result type
  844. (function (def){
  845. var unary_bool = [ "!", "delete" ];
  846. var binary_bool = [ "in", "instanceof", "==", "!=", "===", "!==", "<", "<=", ">=", ">" ];
  847. def(AST_Node, return_false);
  848. def(AST_UnaryPrefix, function(){
  849. return member(this.operator, unary_bool);
  850. });
  851. def(AST_Binary, function(){
  852. return member(this.operator, binary_bool) ||
  853. ( (this.operator == "&&" || this.operator == "||") &&
  854. this.left.is_boolean() && this.right.is_boolean() );
  855. });
  856. def(AST_Conditional, function(){
  857. return this.consequent.is_boolean() && this.alternative.is_boolean();
  858. });
  859. def(AST_Assign, function(){
  860. return this.operator == "=" && this.right.is_boolean();
  861. });
  862. def(AST_Seq, function(){
  863. return this.cdr.is_boolean();
  864. });
  865. def(AST_True, return_true);
  866. def(AST_False, return_true);
  867. })(function(node, func){
  868. node.DEFMETHOD("is_boolean", func);
  869. });
  870. // methods to determine if an expression has a string result type
  871. (function (def){
  872. def(AST_Node, return_false);
  873. def(AST_String, return_true);
  874. def(AST_UnaryPrefix, function(){
  875. return this.operator == "typeof";
  876. });
  877. def(AST_Binary, function(compressor){
  878. return this.operator == "+" &&
  879. (this.left.is_string(compressor) || this.right.is_string(compressor));
  880. });
  881. def(AST_Assign, function(compressor){
  882. return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor);
  883. });
  884. def(AST_Seq, function(compressor){
  885. return this.cdr.is_string(compressor);
  886. });
  887. def(AST_Conditional, function(compressor){
  888. return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
  889. });
  890. def(AST_Call, function(compressor){
  891. return compressor.option("unsafe")
  892. && this.expression instanceof AST_SymbolRef
  893. && this.expression.name == "String"
  894. && this.expression.undeclared();
  895. });
  896. })(function(node, func){
  897. node.DEFMETHOD("is_string", func);
  898. });
  899. function best_of(ast1, ast2) {
  900. return ast1.print_to_string().length >
  901. ast2.print_to_string().length
  902. ? ast2 : ast1;
  903. };
  904. // methods to evaluate a constant expression
  905. (function (def){
  906. // The evaluate method returns an array with one or two
  907. // elements. If the node has been successfully reduced to a
  908. // constant, then the second element tells us the value;
  909. // otherwise the second element is missing. The first element
  910. // of the array is always an AST_Node descendant; if
  911. // evaluation was successful it's a node that represents the
  912. // constant; otherwise it's the original or a replacement node.
  913. AST_Node.DEFMETHOD("evaluate", function(compressor){
  914. if (!compressor.option("evaluate")) return [ this ];
  915. try {
  916. var val = this._eval(compressor);
  917. return [ best_of(make_node_from_constant(compressor, val, this), this), val ];
  918. } catch(ex) {
  919. if (ex !== def) throw ex;
  920. return [ this ];
  921. }
  922. });
  923. AST_Node.DEFMETHOD("is_constant", function(compressor){
  924. // Accomodate when compress option evaluate=false
  925. // as well as the common constant expressions !0 and !1
  926. return this instanceof AST_Constant
  927. || (this instanceof AST_UnaryPrefix && this.operator == "!"
  928. && this.expression instanceof AST_Constant)
  929. || this.evaluate(compressor).length > 1;
  930. });
  931. // Obtain the constant value of an expression already known to be constant.
  932. // Result only valid iff this.is_constant(compressor) is true.
  933. AST_Node.DEFMETHOD("constant_value", function(compressor){
  934. // Accomodate when option evaluate=false.
  935. if (this instanceof AST_Constant) return this.value;
  936. // Accomodate the common constant expressions !0 and !1 when option evaluate=false.
  937. if (this instanceof AST_UnaryPrefix
  938. && this.operator == "!"
  939. && this.expression instanceof AST_Constant) {
  940. return !this.expression.value;
  941. }
  942. var result = this.evaluate(compressor)
  943. if (result.length > 1) {
  944. return result[1];
  945. }
  946. // should never be reached
  947. return undefined;
  948. });
  949. def(AST_Statement, function(){
  950. throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
  951. });
  952. def(AST_Function, function(){
  953. // XXX: AST_Function inherits from AST_Scope, which itself
  954. // inherits from AST_Statement; however, an AST_Function
  955. // isn't really a statement. This could byte in other
  956. // places too. :-( Wish JS had multiple inheritance.
  957. throw def;
  958. });
  959. function ev(node, compressor) {
  960. if (!compressor) throw new Error("Compressor must be passed");
  961. return node._eval(compressor);
  962. };
  963. def(AST_Node, function(){
  964. throw def; // not constant
  965. });
  966. def(AST_Constant, function(){
  967. return this.getValue();
  968. });
  969. def(AST_UnaryPrefix, function(compressor){
  970. var e = this.expression;
  971. switch (this.operator) {
  972. case "!": return !ev(e, compressor);
  973. case "typeof":
  974. // Function would be evaluated to an array and so typeof would
  975. // incorrectly return 'object'. Hence making is a special case.
  976. if (e instanceof AST_Function) return typeof function(){};
  977. e = ev(e, compressor);
  978. // typeof <RegExp> returns "object" or "function" on different platforms
  979. // so cannot evaluate reliably
  980. if (e instanceof RegExp) throw def;
  981. return typeof e;
  982. case "void": return void ev(e, compressor);
  983. case "~": return ~ev(e, compressor);
  984. case "-": return -ev(e, compressor);
  985. case "+": return +ev(e, compressor);
  986. }
  987. throw def;
  988. });
  989. def(AST_Binary, function(c){
  990. var left = this.left, right = this.right, result;
  991. switch (this.operator) {
  992. case "&&" : result = ev(left, c) && ev(right, c); break;
  993. case "||" : result = ev(left, c) || ev(right, c); break;
  994. case "|" : result = ev(left, c) | ev(right, c); break;
  995. case "&" : result = ev(left, c) & ev(right, c); break;
  996. case "^" : result = ev(left, c) ^ ev(right, c); break;
  997. case "+" : result = ev(left, c) + ev(right, c); break;
  998. case "*" : result = ev(left, c) * ev(right, c); break;
  999. case "/" : result = ev(left, c) / ev(right, c); break;
  1000. case "%" : result = ev(left, c) % ev(right, c); break;
  1001. case "-" : result = ev(left, c) - ev(right, c); break;
  1002. case "<<" : result = ev(left, c) << ev(right, c); break;
  1003. case ">>" : result = ev(left, c) >> ev(right, c); break;
  1004. case ">>>" : result = ev(left, c) >>> ev(right, c); break;
  1005. case "==" : result = ev(left, c) == ev(right, c); break;
  1006. case "===" : result = ev(left, c) === ev(right, c); break;
  1007. case "!=" : result = ev(left, c) != ev(right, c); break;
  1008. case "!==" : result = ev(left, c) !== ev(right, c); break;
  1009. case "<" : result = ev(left, c) < ev(right, c); break;
  1010. case "<=" : result = ev(left, c) <= ev(right, c); break;
  1011. case ">" : result = ev(left, c) > ev(right, c); break;
  1012. case ">=" : result = ev(left, c) >= ev(right, c); break;
  1013. default:
  1014. throw def;
  1015. }
  1016. if (isNaN(result) && c.find_parent(AST_With)) {
  1017. // leave original expression as is
  1018. throw def;
  1019. }
  1020. return result;
  1021. });
  1022. def(AST_Conditional, function(compressor){
  1023. return ev(this.condition, compressor)
  1024. ? ev(this.consequent, compressor)
  1025. : ev(this.alternative, compressor);
  1026. });
  1027. def(AST_SymbolRef, function(compressor){
  1028. if (this._evaluating) throw def;
  1029. this._evaluating = true;
  1030. try {
  1031. var d = this.definition();
  1032. if (d && (d.constant || compressor.option("reduce_vars") && !d.modified) && d.init) {
  1033. return ev(d.init, compressor);
  1034. }
  1035. } finally {
  1036. this._evaluating = false;
  1037. }
  1038. throw def;
  1039. });
  1040. def(AST_Dot, function(compressor){
  1041. if (compressor.option("unsafe") && this.property == "length") {
  1042. var str = ev(this.expression, compressor);
  1043. if (typeof str == "string")
  1044. return str.length;
  1045. }
  1046. throw def;
  1047. });
  1048. })(function(node, func){
  1049. node.DEFMETHOD("_eval", func);
  1050. });
  1051. // method to negate an expression
  1052. (function(def){
  1053. function basic_negation(exp) {
  1054. return make_node(AST_UnaryPrefix, exp, {
  1055. operator: "!",
  1056. expression: exp
  1057. });
  1058. };
  1059. def(AST_Node, function(){
  1060. return basic_negation(this);
  1061. });
  1062. def(AST_Statement, function(){
  1063. throw new Error("Cannot negate a statement");
  1064. });
  1065. def(AST_Function, function(){
  1066. return basic_negation(this);
  1067. });
  1068. def(AST_UnaryPrefix, function(){
  1069. if (this.operator == "!")
  1070. return this.expression;
  1071. return basic_negation(this);
  1072. });
  1073. def(AST_Seq, function(compressor){
  1074. var self = this.clone();
  1075. self.cdr = self.cdr.negate(compressor);
  1076. return self;
  1077. });
  1078. def(AST_Conditional, function(compressor){
  1079. var self = this.clone();
  1080. self.consequent = self.consequent.negate(compressor);
  1081. self.alternative = self.alternative.negate(compressor);
  1082. return best_of(basic_negation(this), self);
  1083. });
  1084. def(AST_Binary, function(compressor){
  1085. var self = this.clone(), op = this.operator;
  1086. if (compressor.option("unsafe_comps")) {
  1087. switch (op) {
  1088. case "<=" : self.operator = ">" ; return self;
  1089. case "<" : self.operator = ">=" ; return self;
  1090. case ">=" : self.operator = "<" ; return self;
  1091. case ">" : self.operator = "<=" ; return self;
  1092. }
  1093. }
  1094. switch (op) {
  1095. case "==" : self.operator = "!="; return self;
  1096. case "!=" : self.operator = "=="; return self;
  1097. case "===": self.operator = "!=="; return self;
  1098. case "!==": self.operator = "==="; return self;
  1099. case "&&":
  1100. self.operator = "||";
  1101. self.left = self.left.negate(compressor);
  1102. self.right = self.right.negate(compressor);
  1103. return best_of(basic_negation(this), self);
  1104. case "||":
  1105. self.operator = "&&";
  1106. self.left = self.left.negate(compressor);
  1107. self.right = self.right.negate(compressor);
  1108. return best_of(basic_negation(this), self);
  1109. }
  1110. return basic_negation(this);
  1111. });
  1112. })(function(node, func){
  1113. node.DEFMETHOD("negate", function(compressor){
  1114. return func.call(this, compressor);
  1115. });
  1116. });
  1117. // determine if expression has side effects
  1118. (function(def){
  1119. def(AST_Node, return_true);
  1120. def(AST_EmptyStatement, return_false);
  1121. def(AST_Constant, return_false);
  1122. def(AST_This, return_false);
  1123. def(AST_Call, function(compressor){
  1124. var pure = compressor.option("pure_funcs");
  1125. if (!pure) return true;
  1126. if (typeof pure == "function") return pure(this);
  1127. return pure.indexOf(this.expression.print_to_string()) < 0;
  1128. });
  1129. def(AST_Block, function(compressor){
  1130. for (var i = this.body.length; --i >= 0;) {
  1131. if (this.body[i].has_side_effects(compressor))
  1132. return true;
  1133. }
  1134. return false;
  1135. });
  1136. def(AST_SimpleStatement, function(compressor){
  1137. return this.body.has_side_effects(compressor);
  1138. });
  1139. def(AST_Defun, return_true);
  1140. def(AST_Function, return_false);
  1141. def(AST_Binary, function(compressor){
  1142. return this.left.has_side_effects(compressor)
  1143. || this.right.has_side_effects(compressor);
  1144. });
  1145. def(AST_Assign, return_true);
  1146. def(AST_Conditional, function(compressor){
  1147. return this.condition.has_side_effects(compressor)
  1148. || this.consequent.has_side_effects(compressor)
  1149. || this.alternative.has_side_effects(compressor);
  1150. });
  1151. def(AST_Unary, function(compressor){
  1152. return this.operator == "delete"
  1153. || this.operator == "++"
  1154. || this.operator == "--"
  1155. || this.expression.has_side_effects(compressor);
  1156. });
  1157. def(AST_SymbolRef, function(compressor){
  1158. return this.global() && this.undeclared();
  1159. });
  1160. def(AST_Object, function(compressor){
  1161. for (var i = this.properties.length; --i >= 0;)
  1162. if (this.properties[i].has_side_effects(compressor))
  1163. return true;
  1164. return false;
  1165. });
  1166. def(AST_ObjectProperty, function(compressor){
  1167. return this.value.has_side_effects(compressor);
  1168. });
  1169. def(AST_Array, function(compressor){
  1170. for (var i = this.elements.length; --i >= 0;)
  1171. if (this.elements[i].has_side_effects(compressor))
  1172. return true;
  1173. return false;
  1174. });
  1175. def(AST_Dot, function(compressor){
  1176. if (!compressor.option("pure_getters")) return true;
  1177. return this.expression.has_side_effects(compressor);
  1178. });
  1179. def(AST_Sub, function(compressor){
  1180. if (!compressor.option("pure_getters")) return true;
  1181. return this.expression.has_side_effects(compressor)
  1182. || this.property.has_side_effects(compressor);
  1183. });
  1184. def(AST_PropAccess, function(compressor){
  1185. return !compressor.option("pure_getters");
  1186. });
  1187. def(AST_Seq, function(compressor){
  1188. return this.car.has_side_effects(compressor)
  1189. || this.cdr.has_side_effects(compressor);
  1190. });
  1191. })(function(node, func){
  1192. node.DEFMETHOD("has_side_effects", func);
  1193. });
  1194. // tell me if a statement aborts
  1195. function aborts(thing) {
  1196. return thing && thing.aborts();
  1197. };
  1198. (function(def){
  1199. def(AST_Statement, function(){ return null });
  1200. def(AST_Jump, function(){ return this });
  1201. function block_aborts(){
  1202. var n = this.body.length;
  1203. return n > 0 && aborts(this.body[n - 1]);
  1204. };
  1205. def(AST_BlockStatement, block_aborts);
  1206. def(AST_SwitchBranch, block_aborts);
  1207. def(AST_If, function(){
  1208. return this.alternative && aborts(this.body) && aborts(this.alternative) && this;
  1209. });
  1210. })(function(node, func){
  1211. node.DEFMETHOD("aborts", func);
  1212. });
  1213. /* -----[ optimizers ]----- */
  1214. OPT(AST_Directive, function(self, compressor){
  1215. if (compressor.has_directive(self.value) === "up") {
  1216. return make_node(AST_EmptyStatement, self);
  1217. }
  1218. return self;
  1219. });
  1220. OPT(AST_Debugger, function(self, compressor){
  1221. if (compressor.option("drop_debugger"))
  1222. return make_node(AST_EmptyStatement, self);
  1223. return self;
  1224. });
  1225. OPT(AST_LabeledStatement, function(self, compressor){
  1226. if (self.body instanceof AST_Break
  1227. && compressor.loopcontrol_target(self.body.label) === self.body) {
  1228. return make_node(AST_EmptyStatement, self);
  1229. }
  1230. return self.label.references.length == 0 ? self.body : self;
  1231. });
  1232. OPT(AST_Block, function(self, compressor){
  1233. self.body = tighten_body(self.body, compressor);
  1234. return self;
  1235. });
  1236. OPT(AST_BlockStatement, function(self, compressor){
  1237. self.body = tighten_body(self.body, compressor);
  1238. switch (self.body.length) {
  1239. case 1: return self.body[0];
  1240. case 0: return make_node(AST_EmptyStatement, self);
  1241. }
  1242. return self;
  1243. });
  1244. AST_Scope.DEFMETHOD("drop_unused", function(compressor){
  1245. var self = this;
  1246. if (compressor.has_directive("use asm")) return self;
  1247. if (compressor.option("unused")
  1248. && !(self instanceof AST_Toplevel)
  1249. && !self.uses_eval
  1250. && !self.uses_with
  1251. ) {
  1252. var in_use = [];
  1253. var in_use_ids = {}; // avoid expensive linear scans of in_use
  1254. var initializations = new Dictionary();
  1255. // pass 1: find out which symbols are directly used in
  1256. // this scope (not in nested scopes).
  1257. var scope = this;
  1258. var tw = new TreeWalker(function(node, descend){
  1259. if (node !== self) {
  1260. if (node instanceof AST_Defun) {
  1261. initializations.add(node.name.name, node);
  1262. return true; // don't go in nested scopes
  1263. }
  1264. if (node instanceof AST_Definitions && scope === self) {
  1265. node.definitions.forEach(function(def){
  1266. if (def.value) {
  1267. initializations.add(def.name.name, def.value);
  1268. if (def.value.has_side_effects(compressor)) {
  1269. def.value.walk(tw);
  1270. }
  1271. }
  1272. });
  1273. return true;
  1274. }
  1275. if (node instanceof AST_SymbolRef) {
  1276. var node_def = node.definition();
  1277. if (!(node_def.id in in_use_ids)) {
  1278. in_use_ids[node_def.id] = true;
  1279. in_use.push(node_def);
  1280. }
  1281. return true;
  1282. }
  1283. if (node instanceof AST_Scope) {
  1284. var save_scope = scope;
  1285. scope = node;
  1286. descend();
  1287. scope = save_scope;
  1288. return true;
  1289. }
  1290. }
  1291. });
  1292. self.walk(tw);
  1293. // pass 2: for every used symbol we need to walk its
  1294. // initialization code to figure out if it uses other
  1295. // symbols (that may not be in_use).
  1296. for (var i = 0; i < in_use.length; ++i) {
  1297. in_use[i].orig.forEach(function(decl){
  1298. // undeclared globals will be instanceof AST_SymbolRef
  1299. var init = initializations.get(decl.name);
  1300. if (init) init.forEach(function(init){
  1301. var tw = new TreeWalker(function(node){
  1302. if (node instanceof AST_SymbolRef) {
  1303. var node_def = node.definition();
  1304. if (!(node_def.id in in_use_ids)) {
  1305. in_use_ids[node_def.id] = true;
  1306. in_use.push(node_def);
  1307. }
  1308. }
  1309. });
  1310. init.walk(tw);
  1311. });
  1312. });
  1313. }
  1314. // pass 3: we should drop declarations not in_use
  1315. var tt = new TreeTransformer(
  1316. function before(node, descend, in_list) {
  1317. if (node instanceof AST_Lambda && !(node instanceof AST_Accessor)) {
  1318. if (!compressor.option("keep_fargs")) {
  1319. for (var a = node.argnames, i = a.length; --i >= 0;) {
  1320. var sym = a[i];
  1321. if (sym.unreferenced()) {
  1322. a.pop();
  1323. compressor.warn("Dropping unused function argument {name} [{file}:{line},{col}]", {
  1324. name : sym.name,
  1325. file : sym.start.file,
  1326. line : sym.start.line,
  1327. col : sym.start.col
  1328. });
  1329. }
  1330. else break;
  1331. }
  1332. }
  1333. }
  1334. if (node instanceof AST_Defun && node !== self) {
  1335. if (!(node.name.definition().id in in_use_ids)) {
  1336. compressor.warn("Dropping unused function {name} [{file}:{line},{col}]", {
  1337. name : node.name.name,
  1338. file : node.name.start.file,
  1339. line : node.name.start.line,
  1340. col : node.name.start.col
  1341. });
  1342. return make_node(AST_EmptyStatement, node);
  1343. }
  1344. return node;
  1345. }
  1346. if (node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn)) {
  1347. var def = node.definitions.filter(function(def){
  1348. if (def.name.definition().id in in_use_ids) return true;
  1349. var w = {
  1350. name : def.name.name,
  1351. file : def.name.start.file,
  1352. line : def.name.start.line,
  1353. col : def.name.start.col
  1354. };
  1355. if (def.value && def.value.has_side_effects(compressor)) {
  1356. def._unused_side_effects = true;
  1357. compressor.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", w);
  1358. return true;
  1359. }
  1360. compressor.warn("Dropping unused variable {name} [{file}:{line},{col}]", w);
  1361. return false;
  1362. });
  1363. // place uninitialized names at the start
  1364. def = mergeSort(def, function(a, b){
  1365. if (!a.value && b.value) return -1;
  1366. if (!b.value && a.value) return 1;
  1367. return 0;
  1368. });
  1369. // for unused names whose initialization has
  1370. // side effects, we can cascade the init. code
  1371. // into the next one, or next statement.
  1372. var side_effects = [];
  1373. for (var i = 0; i < def.length;) {
  1374. var x = def[i];
  1375. if (x._unused_side_effects) {
  1376. side_effects.push(x.value);
  1377. def.splice(i, 1);
  1378. } else {
  1379. if (side_effects.length > 0) {
  1380. side_effects.push(x.value);
  1381. x.value = AST_Seq.from_array(side_effects);
  1382. side_effects = [];
  1383. }
  1384. ++i;
  1385. }
  1386. }
  1387. if (side_effects.length > 0) {
  1388. side_effects = make_node(AST_BlockStatement, node, {
  1389. body: [ make_node(AST_SimpleStatement, node, {
  1390. body: AST_Seq.from_array(side_effects)
  1391. }) ]
  1392. });
  1393. } else {
  1394. side_effects = null;
  1395. }
  1396. if (def.length == 0 && !side_effects) {
  1397. return make_node(AST_EmptyStatement, node);
  1398. }
  1399. if (def.length == 0) {
  1400. return in_list ? MAP.splice(side_effects.body) : side_effects;
  1401. }
  1402. node.definitions = def;
  1403. if (side_effects) {
  1404. side_effects.body.unshift(node);
  1405. return in_list ? MAP.splice(side_effects.body) : side_effects;
  1406. }
  1407. return node;
  1408. }
  1409. if (node instanceof AST_For) {
  1410. descend(node, this);
  1411. if (node.init instanceof AST_BlockStatement) {
  1412. // certain combination of unused name + side effect leads to:
  1413. // https://github.com/mishoo/UglifyJS2/issues/44
  1414. // that's an invalid AST.
  1415. // We fix it at this stage by moving the `var` outside the `for`.
  1416. var body = node.init.body.slice(0, -1);
  1417. node.init = node.init.body.slice(-1)[0].body;
  1418. body.push(node);
  1419. return in_list ? MAP.splice(body) : make_node(AST_BlockStatement, node, {
  1420. body: body
  1421. });
  1422. }
  1423. }
  1424. if (node instanceof AST_Scope && node !== self)
  1425. return node;
  1426. }
  1427. );
  1428. self.transform(tt);
  1429. }
  1430. });
  1431. AST_Scope.DEFMETHOD("hoist_declarations", function(compressor){
  1432. var self = this;
  1433. if (compressor.has_directive("use asm")) return self;
  1434. var hoist_funs = compressor.option("hoist_funs");
  1435. var hoist_vars = compressor.option("hoist_vars");
  1436. if (hoist_funs || hoist_vars) {
  1437. var dirs = [];
  1438. var hoisted = [];
  1439. var vars = new Dictionary(), vars_found = 0, var_decl = 0;
  1440. // let's count var_decl first, we seem to waste a lot of
  1441. // space if we hoist `var` when there's only one.
  1442. self.walk(new TreeWalker(function(node){
  1443. if (node instanceof AST_Scope && node !== self)
  1444. return true;
  1445. if (node instanceof AST_Var) {
  1446. ++var_decl;
  1447. return true;
  1448. }
  1449. }));
  1450. hoist_vars = hoist_vars && var_decl > 1;
  1451. var tt = new TreeTransformer(
  1452. function before(node) {
  1453. if (node !== self) {
  1454. if (node instanceof AST_Directive) {
  1455. dirs.push(node);
  1456. return make_node(AST_EmptyStatement, node);
  1457. }
  1458. if (node instanceof AST_Defun && hoist_funs) {
  1459. hoisted.push(node);
  1460. return make_node(AST_EmptyStatement, node);
  1461. }
  1462. if (node instanceof AST_Var && hoist_vars) {
  1463. node.definitions.forEach(function(def){
  1464. vars.set(def.name.name, def);
  1465. ++vars_found;
  1466. });
  1467. var seq = node.to_assignments();
  1468. var p = tt.parent();
  1469. if (p instanceof AST_ForIn && p.init === node) {
  1470. if (seq == null) {
  1471. var def = node.definitions[0].name;
  1472. return make_node(AST_SymbolRef, def, def);
  1473. }
  1474. return seq;
  1475. }
  1476. if (p instanceof AST_For && p.init === node) {
  1477. return seq;
  1478. }
  1479. if (!seq) return make_node(AST_EmptyStatement, node);
  1480. return make_node(AST_SimpleStatement, node, {
  1481. body: seq
  1482. });
  1483. }
  1484. if (node instanceof AST_Scope)
  1485. return node; // to avoid descending in nested scopes
  1486. }
  1487. }
  1488. );
  1489. self = self.transform(tt);
  1490. if (vars_found > 0) {
  1491. // collect only vars which don't show up in self's arguments list
  1492. var defs = [];
  1493. vars.each(function(def, name){
  1494. if (self instanceof AST_Lambda
  1495. && find_if(function(x){ return x.name == def.name.name },
  1496. self.argnames)) {
  1497. vars.del(name);
  1498. } else {
  1499. def = def.clone();
  1500. def.value = null;
  1501. defs.push(def);
  1502. vars.set(name, def);
  1503. }
  1504. });
  1505. if (defs.length > 0) {
  1506. // try to merge in assignments
  1507. for (var i = 0; i < self.body.length;) {
  1508. if (self.body[i] instanceof AST_SimpleStatement) {
  1509. var expr = self.body[i].body, sym, assign;
  1510. if (expr instanceof AST_Assign
  1511. && expr.operator == "="
  1512. && (sym = expr.left) instanceof AST_Symbol
  1513. && vars.has(sym.name))
  1514. {
  1515. var def = vars.get(sym.name);
  1516. if (def.value) break;
  1517. def.value = expr.right;
  1518. remove(defs, def);
  1519. defs.push(def);
  1520. self.body.splice(i, 1);
  1521. continue;
  1522. }
  1523. if (expr instanceof AST_Seq
  1524. && (assign = expr.car) instanceof AST_Assign
  1525. && assign.operator == "="
  1526. && (sym = assign.left) instanceof AST_Symbol
  1527. && vars.has(sym.name))
  1528. {
  1529. var def = vars.get(sym.name);
  1530. if (def.value) break;
  1531. def.value = assign.right;
  1532. remove(defs, def);
  1533. defs.push(def);
  1534. self.body[i].body = expr.cdr;
  1535. continue;
  1536. }
  1537. }
  1538. if (self.body[i] instanceof AST_EmptyStatement) {
  1539. self.body.splice(i, 1);
  1540. continue;
  1541. }
  1542. if (self.body[i] instanceof AST_BlockStatement) {
  1543. var tmp = [ i, 1 ].concat(self.body[i].body);
  1544. self.body.splice.apply(self.body, tmp);
  1545. continue;
  1546. }
  1547. break;
  1548. }
  1549. defs = make_node(AST_Var, self, {
  1550. definitions: defs
  1551. });
  1552. hoisted.push(defs);
  1553. };
  1554. }
  1555. self.body = dirs.concat(hoisted, self.body);
  1556. }
  1557. return self;
  1558. });
  1559. OPT(AST_SimpleStatement, function(self, compressor){
  1560. if (compressor.option("side_effects")) {
  1561. if (!self.body.has_side_effects(compressor)) {
  1562. compressor.warn("Dropping side-effect-free statement [{file}:{line},{col}]", self.start);
  1563. return make_node(AST_EmptyStatement, self);
  1564. }
  1565. }
  1566. return self;
  1567. });
  1568. OPT(AST_DWLoop, function(self, compressor){
  1569. var cond = self.condition.evaluate(compressor);
  1570. self.condition = cond[0];
  1571. if (!compressor.option("loops")) return self;
  1572. if (cond.length > 1) {
  1573. if (cond[1]) {
  1574. return make_node(AST_For, self, {
  1575. body: self.body
  1576. });
  1577. } else if (self instanceof AST_While) {
  1578. if (compressor.option("dead_code")) {
  1579. var a = [];
  1580. extract_declarations_from_unreachable_code(compressor, self.body, a);
  1581. return make_node(AST_BlockStatement, self, { body: a });
  1582. }
  1583. }
  1584. }
  1585. return self;
  1586. });
  1587. function if_break_in_loop(self, compressor) {
  1588. function drop_it(rest) {
  1589. rest = as_statement_array(rest);
  1590. if (self.body instanceof AST_BlockStatement) {
  1591. self.body = self.body.clone();
  1592. self.body.body = rest.concat(self.body.body.slice(1));
  1593. self.body = self.body.transform(compressor);
  1594. } else {
  1595. self.body = make_node(AST_BlockStatement, self.body, {
  1596. body: rest
  1597. }).transform(compressor);
  1598. }
  1599. if_break_in_loop(self, compressor);
  1600. }
  1601. var first = self.body instanceof AST_BlockStatement ? self.body.body[0] : self.body;
  1602. if (first instanceof AST_If) {
  1603. if (first.body instanceof AST_Break
  1604. && compressor.loopcontrol_target(first.body.label) === self) {
  1605. if (self.condition) {
  1606. self.condition = make_node(AST_Binary, self.condition, {
  1607. left: self.condition,
  1608. operator: "&&",
  1609. right: first.condition.negate(compressor),
  1610. });
  1611. } else {
  1612. self.condition = first.condition.negate(compressor);
  1613. }
  1614. drop_it(first.alternative);
  1615. }
  1616. else if (first.alternative instanceof AST_Break
  1617. && compressor.loopcontrol_target(first.alternative.label) === self) {
  1618. if (self.condition) {
  1619. self.condition = make_node(AST_Binary, self.condition, {
  1620. left: self.condition,
  1621. operator: "&&",
  1622. right: first.condition,
  1623. });
  1624. } else {
  1625. self.condition = first.condition;
  1626. }
  1627. drop_it(first.body);
  1628. }
  1629. }
  1630. };
  1631. OPT(AST_While, function(self, compressor) {
  1632. if (!compressor.option("loops")) return self;
  1633. self = AST_DWLoop.prototype.optimize.call(self, compressor);
  1634. if (self instanceof AST_While) {
  1635. if_break_in_loop(self, compressor);
  1636. self = make_node(AST_For, self, self).transform(compressor);
  1637. }
  1638. return self;
  1639. });
  1640. OPT(AST_For, function(self, compressor){
  1641. var cond = self.condition;
  1642. if (cond) {
  1643. cond = cond.evaluate(compressor);
  1644. self.condition = cond[0];
  1645. }
  1646. if (!compressor.option("loops")) return self;
  1647. if (cond) {
  1648. if (cond.length > 1 && !cond[1]) {
  1649. if (compressor.option("dead_code")) {
  1650. var a = [];
  1651. if (self.init instanceof AST_Statement) {
  1652. a.push(self.init);
  1653. }
  1654. else if (self.init) {
  1655. a.push(make_node(AST_SimpleStatement, self.init, {
  1656. body: self.init
  1657. }));
  1658. }
  1659. extract_declarations_from_unreachable_code(compressor, self.body, a);
  1660. return make_node(AST_BlockStatement, self, { body: a });
  1661. }
  1662. }
  1663. }
  1664. if_break_in_loop(self, compressor);
  1665. return self;
  1666. });
  1667. OPT(AST_If, function(self, compressor){
  1668. if (!compressor.option("conditionals")) return self;
  1669. // if condition can be statically determined, warn and drop
  1670. // one of the blocks. note, statically determined implies
  1671. // “has no side effects”; also it doesn't work for cases like
  1672. // `x && true`, though it probably should.
  1673. var cond = self.condition.evaluate(compressor);
  1674. self.condition = cond[0];
  1675. if (cond.length > 1) {
  1676. if (cond[1]) {
  1677. compressor.warn("Condition always true [{file}:{line},{col}]", self.condition.start);
  1678. if (compressor.option("dead_code")) {
  1679. var a = [];
  1680. if (self.alternative) {
  1681. extract_declarations_from_unreachable_code(compressor, self.alternative, a);
  1682. }
  1683. a.push(self.body);
  1684. return make_node(AST_BlockStatement, self, { body: a }).transform(compressor);
  1685. }
  1686. } else {
  1687. compressor.warn("Condition always false [{file}:{line},{col}]", self.condition.start);
  1688. if (compressor.option("dead_code")) {
  1689. var a = [];
  1690. extract_declarations_from_unreachable_code(compressor, self.body, a);
  1691. if (self.alternative) a.push(self.alternative);
  1692. return make_node(AST_BlockStatement, self, { body: a }).transform(compressor);
  1693. }
  1694. }
  1695. }
  1696. if (is_empty(self.alternative)) self.alternative = null;
  1697. var negated = self.condition.negate(compressor);
  1698. var self_condition_length = self.condition.print_to_string().length;
  1699. var negated_length = negated.print_to_string().length;
  1700. var negated_is_best = negated_length < self_condition_length;
  1701. if (self.alternative && negated_is_best) {
  1702. negated_is_best = false; // because we already do the switch here.
  1703. // no need to swap values of self_condition_length and negated_length
  1704. // here because they are only used in an equality comparison later on.
  1705. self.condition = negated;
  1706. var tmp = self.body;
  1707. self.body = self.alternative || make_node(AST_EmptyStatement);
  1708. self.alternative = tmp;
  1709. }
  1710. if (is_empty(self.body) && is_empty(self.alternative)) {
  1711. return make_node(AST_SimpleStatement, self.condition, {
  1712. body: self.condition
  1713. }).transform(compressor);
  1714. }
  1715. if (self.body instanceof AST_SimpleStatement
  1716. && self.alternative instanceof AST_SimpleStatement) {
  1717. return make_node(AST_SimpleStatement, self, {
  1718. body: make_node(AST_Conditional, self, {
  1719. condition : self.condition,
  1720. consequent : self.body.body,
  1721. alternative : self.alternative.body
  1722. })
  1723. }).transform(compressor);
  1724. }
  1725. if (is_empty(self.alternative) && self.body instanceof AST_SimpleStatement) {
  1726. if (self_condition_length === negated_length && !negated_is_best
  1727. && self.condition instanceof AST_Binary && self.condition.operator == "||") {
  1728. // although the code length of self.condition and negated are the same,
  1729. // negated does not require additional surrounding parentheses.
  1730. // see https://github.com/mishoo/UglifyJS2/issues/979
  1731. negated_is_best = true;
  1732. }
  1733. if (negated_is_best) return make_node(AST_SimpleStatement, self, {
  1734. body: make_node(AST_Binary, self, {
  1735. operator : "||",
  1736. left : negated,
  1737. right : self.body.body
  1738. })
  1739. }).transform(compressor);
  1740. return make_node(AST_SimpleStatement, self, {
  1741. body: make_node(AST_Binary, self, {
  1742. operator : "&&",
  1743. left : self.condition,
  1744. right : self.body.body
  1745. })
  1746. }).transform(compressor);
  1747. }
  1748. if (self.body instanceof AST_EmptyStatement
  1749. && self.alternative
  1750. && self.alternative instanceof AST_SimpleStatement) {
  1751. return make_node(AST_SimpleStatement, self, {
  1752. body: make_node(AST_Binary, self, {
  1753. operator : "||",
  1754. left : self.condition,
  1755. right : self.alternative.body
  1756. })
  1757. }).transform(compressor);
  1758. }
  1759. if (self.body instanceof AST_Exit
  1760. && self.alternative instanceof AST_Exit
  1761. && self.body.TYPE == self.alternative.TYPE) {
  1762. return make_node(self.body.CTOR, self, {
  1763. value: make_node(AST_Conditional, self, {
  1764. condition : self.condition,
  1765. consequent : self.body.value || make_node(AST_Undefined, self.body).optimize(compressor),
  1766. alternative : self.alternative.value || make_node(AST_Undefined, self.alternative).optimize(compressor)
  1767. })
  1768. }).transform(compressor);
  1769. }
  1770. if (self.body instanceof AST_If
  1771. && !self.body.alternative
  1772. && !self.alternative) {
  1773. self.condition = make_node(AST_Binary, self.condition, {
  1774. operator: "&&",
  1775. left: self.condition,
  1776. right: self.body.condition
  1777. }).transform(compressor);
  1778. self.body = self.body.body;
  1779. }
  1780. if (aborts(self.body)) {
  1781. if (self.alternative) {
  1782. var alt = self.alternative;
  1783. self.alternative = null;
  1784. return make_node(AST_BlockStatement, self, {
  1785. body: [ self, alt ]
  1786. }).transform(compressor);
  1787. }
  1788. }
  1789. if (aborts(self.alternative)) {
  1790. var body = self.body;
  1791. self.body = self.alternative;
  1792. self.condition = negated_is_best ? negated : self.condition.negate(compressor);
  1793. self.alternative = null;
  1794. return make_node(AST_BlockStatement, self, {
  1795. body: [ self, body ]
  1796. }).transform(compressor);
  1797. }
  1798. return self;
  1799. });
  1800. OPT(AST_Switch, function(self, compressor){
  1801. if (self.body.length == 0 && compressor.option("conditionals")) {
  1802. return make_node(AST_SimpleStatement, self, {
  1803. body: self.expression
  1804. }).transform(compressor);
  1805. }
  1806. for(;;) {
  1807. var last_branch = self.body[self.body.length - 1];
  1808. if (last_branch) {
  1809. var stat = last_branch.body[last_branch.body.length - 1]; // last statement
  1810. if (stat instanceof AST_Break && loop_body(compressor.loopcontrol_target(stat.label)) === self)
  1811. last_branch.body.pop();
  1812. if (last_branch instanceof AST_Default && last_branch.body.length == 0) {
  1813. self.body.pop();
  1814. continue;
  1815. }
  1816. }
  1817. break;
  1818. }
  1819. var exp = self.expression.evaluate(compressor);
  1820. out: if (exp.length == 2) try {
  1821. // constant expression
  1822. self.expression = exp[0];
  1823. if (!compressor.option("dead_code")) break out;
  1824. var value = exp[1];
  1825. var in_if = false;
  1826. var in_block = false;
  1827. var started = false;
  1828. var stopped = false;
  1829. var ruined = false;
  1830. var tt = new TreeTransformer(function(node, descend, in_list){
  1831. if (node instanceof AST_Lambda || node instanceof AST_SimpleStatement) {
  1832. // no need to descend these node types
  1833. return node;
  1834. }
  1835. else if (node instanceof AST_Switch && node === self) {
  1836. node = node.clone();
  1837. descend(node, this);
  1838. return ruined ? node : make_node(AST_BlockStatement, node, {
  1839. body: node.body.reduce(function(a, branch){
  1840. return a.concat(branch.body);
  1841. }, [])
  1842. }).transform(compressor);
  1843. }
  1844. else if (node instanceof AST_If || node instanceof AST_Try) {
  1845. var save = in_if;
  1846. in_if = !in_block;
  1847. descend(node, this);
  1848. in_if = save;
  1849. return node;
  1850. }
  1851. else if (node instanceof AST_StatementWithBody || node instanceof AST_Switch) {
  1852. var save = in_block;
  1853. in_block = true;
  1854. descend(node, this);
  1855. in_block = save;
  1856. return node;
  1857. }
  1858. else if (node instanceof AST_Break && this.loopcontrol_target(node.label) === self) {
  1859. if (in_if) {
  1860. ruined = true;
  1861. return node;
  1862. }
  1863. if (in_block) return node;
  1864. stopped = true;
  1865. return in_list ? MAP.skip : make_node(AST_EmptyStatement, node);
  1866. }
  1867. else if (node instanceof AST_SwitchBranch && this.parent() === self) {
  1868. if (stopped) return MAP.skip;
  1869. if (node instanceof AST_Case) {
  1870. var exp = node.expression.evaluate(compressor);
  1871. if (exp.length < 2) {
  1872. // got a case with non-constant expression, baling out
  1873. throw self;
  1874. }
  1875. if (exp[1] === value || started) {
  1876. started = true;
  1877. if (aborts(node)) stopped = true;
  1878. descend(node, this);
  1879. return node;
  1880. }
  1881. return MAP.skip;
  1882. }
  1883. descend(node, this);
  1884. return node;
  1885. }
  1886. });
  1887. tt.stack = compressor.stack.slice(); // so that's able to see parent nodes
  1888. self = self.transform(tt);
  1889. } catch(ex) {
  1890. if (ex !== self) throw ex;
  1891. }
  1892. return self;
  1893. });
  1894. OPT(AST_Case, function(self, compressor){
  1895. self.body = tighten_body(self.body, compressor);
  1896. return self;
  1897. });
  1898. OPT(AST_Try, function(self, compressor){
  1899. self.body = tighten_body(self.body, compressor);
  1900. return self;
  1901. });
  1902. AST_Definitions.DEFMETHOD("remove_initializers", function(){
  1903. this.definitions.forEach(function(def){ def.value = null });
  1904. });
  1905. AST_Definitions.DEFMETHOD("to_assignments", function(){
  1906. var assignments = this.definitions.reduce(function(a, def){
  1907. if (def.value) {
  1908. var name = make_node(AST_SymbolRef, def.name, def.name);
  1909. a.push(make_node(AST_Assign, def, {
  1910. operator : "=",
  1911. left : name,
  1912. right : def.value
  1913. }));
  1914. }
  1915. return a;
  1916. }, []);
  1917. if (assignments.length == 0) return null;
  1918. return AST_Seq.from_array(assignments);
  1919. });
  1920. OPT(AST_Definitions, function(self, compressor){
  1921. if (self.definitions.length == 0)
  1922. return make_node(AST_EmptyStatement, self);
  1923. return self;
  1924. });
  1925. OPT(AST_Function, function(self, compressor){
  1926. self = AST_Lambda.prototype.optimize.call(self, compressor);
  1927. if (compressor.option("unused") && !compressor.option("keep_fnames")) {
  1928. if (self.name && self.name.unreferenced()) {
  1929. self.name = null;
  1930. }
  1931. }
  1932. return self;
  1933. });
  1934. OPT(AST_Call, function(self, compressor){
  1935. if (compressor.option("unsafe")) {
  1936. var exp = self.expression;
  1937. if (exp instanceof AST_SymbolRef && exp.undeclared()) {
  1938. switch (exp.name) {
  1939. case "Array":
  1940. if (self.args.length != 1) {
  1941. return make_node(AST_Array, self, {
  1942. elements: self.args
  1943. }).transform(compressor);
  1944. }
  1945. break;
  1946. case "Object":
  1947. if (self.args.length == 0) {
  1948. return make_node(AST_Object, self, {
  1949. properties: []
  1950. });
  1951. }
  1952. break;
  1953. case "String":
  1954. if (self.args.length == 0) return make_node(AST_String, self, {
  1955. value: ""
  1956. });
  1957. if (self.args.length <= 1) return make_node(AST_Binary, self, {
  1958. left: self.args[0],
  1959. operator: "+",
  1960. right: make_node(AST_String, self, { value: "" })
  1961. }).transform(compressor);
  1962. break;
  1963. case "Number":
  1964. if (self.args.length == 0) return make_node(AST_Number, self, {
  1965. value: 0
  1966. });
  1967. if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
  1968. expression: self.args[0],
  1969. operator: "+"
  1970. }).transform(compressor);
  1971. case "Boolean":
  1972. if (self.args.length == 0) return make_node(AST_False, self);
  1973. if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
  1974. expression: make_node(AST_UnaryPrefix, null, {
  1975. expression: self.args[0],
  1976. operator: "!"
  1977. }),
  1978. operator: "!"
  1979. }).transform(compressor);
  1980. break;
  1981. case "Function":
  1982. // new Function() => function(){}
  1983. if (self.args.length == 0) return make_node(AST_Function, self, {
  1984. argnames: [],
  1985. body: []
  1986. });
  1987. if (all(self.args, function(x){ return x instanceof AST_String })) {
  1988. // quite a corner-case, but we can handle it:
  1989. // https://github.com/mishoo/UglifyJS2/issues/203
  1990. // if the code argument is a constant, then we can minify it.
  1991. try {
  1992. var code = "(function(" + self.args.slice(0, -1).map(function(arg){
  1993. return arg.value;
  1994. }).join(",") + "){" + self.args[self.args.length - 1].value + "})()";
  1995. var ast = parse(code);
  1996. ast.figure_out_scope({ screw_ie8: compressor.option("screw_ie8") });
  1997. var comp = new Compressor(compressor.options);
  1998. ast = ast.transform(comp);
  1999. ast.figure_out_scope({ screw_ie8: compressor.option("screw_ie8") });
  2000. ast.mangle_names();
  2001. var fun;
  2002. try {
  2003. ast.walk(new TreeWalker(function(node){
  2004. if (node instanceof AST_Lambda) {
  2005. fun = node;
  2006. throw ast;
  2007. }
  2008. }));
  2009. } catch(ex) {
  2010. if (ex !== ast) throw ex;
  2011. };
  2012. if (!fun) return self;
  2013. var args = fun.argnames.map(function(arg, i){
  2014. return make_node(AST_String, self.args[i], {
  2015. value: arg.print_to_string()
  2016. });
  2017. });
  2018. var code = OutputStream();
  2019. AST_BlockStatement.prototype._codegen.call(fun, fun, code);
  2020. code = code.toString().replace(/^\{|\}$/g, "");
  2021. args.push(make_node(AST_String, self.args[self.args.length - 1], {
  2022. value: code
  2023. }));
  2024. self.args = args;
  2025. return self;
  2026. } catch(ex) {
  2027. if (ex instanceof JS_Parse_Error) {
  2028. compressor.warn("Error parsing code passed to new Function [{file}:{line},{col}]", self.args[self.args.length - 1].start);
  2029. compressor.warn(ex.toString());
  2030. } else {
  2031. console.log(ex);
  2032. throw ex;
  2033. }
  2034. }
  2035. }
  2036. break;
  2037. }
  2038. }
  2039. else if (exp instanceof AST_Dot && exp.property == "toString" && self.args.length == 0) {
  2040. return make_node(AST_Binary, self, {
  2041. left: make_node(AST_String, self, { value: "" }),
  2042. operator: "+",
  2043. right: exp.expression
  2044. }).transform(compressor);
  2045. }
  2046. else if (exp instanceof AST_Dot && exp.expression instanceof AST_Array && exp.property == "join") EXIT: {
  2047. var separator = self.args.length == 0 ? "," : self.args[0].evaluate(compressor)[1];
  2048. if (separator == null) break EXIT; // not a constant
  2049. var elements = exp.expression.elements.reduce(function(a, el){
  2050. el = el.evaluate(compressor);
  2051. if (a.length == 0 || el.length == 1) {
  2052. a.push(el);
  2053. } else {
  2054. var last = a[a.length - 1];
  2055. if (last.length == 2) {
  2056. // it's a constant
  2057. var val = "" + last[1] + separator + el[1];
  2058. a[a.length - 1] = [ make_node_from_constant(compressor, val, last[0]), val ];
  2059. } else {
  2060. a.push(el);
  2061. }
  2062. }
  2063. return a;
  2064. }, []);
  2065. if (elements.length == 0) return make_node(AST_String, self, { value: "" });
  2066. if (elements.length == 1) return elements[0][0];
  2067. if (separator == "") {
  2068. var first;
  2069. if (elements[0][0] instanceof AST_String
  2070. || elements[1][0] instanceof AST_String) {
  2071. first = elements.shift()[0];
  2072. } else {
  2073. first = make_node(AST_String, self, { value: "" });
  2074. }
  2075. return elements.reduce(function(prev, el){
  2076. return make_node(AST_Binary, el[0], {
  2077. operator : "+",
  2078. left : prev,
  2079. right : el[0],
  2080. });
  2081. }, first).transform(compressor);
  2082. }
  2083. // need this awkward cloning to not affect original element
  2084. // best_of will decide which one to get through.
  2085. var node = self.clone();
  2086. node.expression = node.expression.clone();
  2087. node.expression.expression = node.expression.expression.clone();
  2088. node.expression.expression.elements = elements.map(function(el){
  2089. return el[0];
  2090. });
  2091. return best_of(self, node);
  2092. }
  2093. }
  2094. if (compressor.option("side_effects")) {
  2095. if (self.expression instanceof AST_Function
  2096. && self.args.length == 0
  2097. && !AST_Block.prototype.has_side_effects.call(self.expression, compressor)) {
  2098. return make_node(AST_Undefined, self).transform(compressor);
  2099. }
  2100. }
  2101. if (compressor.option("drop_console")) {
  2102. if (self.expression instanceof AST_PropAccess) {
  2103. var name = self.expression.expression;
  2104. while (name.expression) {
  2105. name = name.expression;
  2106. }
  2107. if (name instanceof AST_SymbolRef
  2108. && name.name == "console"
  2109. && name.undeclared()) {
  2110. return make_node(AST_Undefined, self).transform(compressor);
  2111. }
  2112. }
  2113. }
  2114. return self.evaluate(compressor)[0];
  2115. });
  2116. OPT(AST_New, function(self, compressor){
  2117. if (compressor.option("unsafe")) {
  2118. var exp = self.expression;
  2119. if (exp instanceof AST_SymbolRef && exp.undeclared()) {
  2120. switch (exp.name) {
  2121. case "Object":
  2122. case "RegExp":
  2123. case "Function":
  2124. case "Error":
  2125. case "Array":
  2126. return make_node(AST_Call, self, self).transform(compressor);
  2127. }
  2128. }
  2129. }
  2130. return self;
  2131. });
  2132. OPT(AST_Seq, function(self, compressor){
  2133. if (!compressor.option("side_effects"))
  2134. return self;
  2135. if (!self.car.has_side_effects(compressor)) {
  2136. return maintain_this_binding(compressor.parent(), self, self.cdr);
  2137. }
  2138. if (compressor.option("cascade")) {
  2139. if (self.car instanceof AST_Assign
  2140. && !self.car.left.has_side_effects(compressor)) {
  2141. if (self.car.left.equivalent_to(self.cdr)) {
  2142. return self.car;
  2143. }
  2144. if (self.cdr instanceof AST_Call
  2145. && self.cdr.expression.equivalent_to(self.car.left)) {
  2146. self.cdr.expression = self.car;
  2147. return self.cdr;
  2148. }
  2149. }
  2150. if (!self.car.has_side_effects(compressor)
  2151. && !self.cdr.has_side_effects(compressor)
  2152. && self.car.equivalent_to(self.cdr)) {
  2153. return self.car;
  2154. }
  2155. }
  2156. if (self.cdr instanceof AST_UnaryPrefix
  2157. && self.cdr.operator == "void"
  2158. && !self.cdr.expression.has_side_effects(compressor)) {
  2159. self.cdr.expression = self.car;
  2160. return self.cdr;
  2161. }
  2162. if (self.cdr instanceof AST_Undefined) {
  2163. return make_node(AST_UnaryPrefix, self, {
  2164. operator : "void",
  2165. expression : self.car
  2166. });
  2167. }
  2168. return self;
  2169. });
  2170. AST_Unary.DEFMETHOD("lift_sequences", function(compressor){
  2171. if (compressor.option("sequences")) {
  2172. if (this.expression instanceof AST_Seq) {
  2173. var seq = this.expression;
  2174. var x = seq.to_array();
  2175. this.expression = x.pop();
  2176. x.push(this);
  2177. seq = AST_Seq.from_array(x).transform(compressor);
  2178. return seq;
  2179. }
  2180. }
  2181. return this;
  2182. });
  2183. OPT(AST_UnaryPostfix, function(self, compressor){
  2184. return self.lift_sequences(compressor);
  2185. });
  2186. OPT(AST_UnaryPrefix, function(self, compressor){
  2187. self = self.lift_sequences(compressor);
  2188. var e = self.expression;
  2189. if (compressor.option("booleans") && compressor.in_boolean_context()) {
  2190. switch (self.operator) {
  2191. case "!":
  2192. if (e instanceof AST_UnaryPrefix && e.operator == "!") {
  2193. // !!foo ==> foo, if we're in boolean context
  2194. return e.expression;
  2195. }
  2196. break;
  2197. case "typeof":
  2198. // typeof always returns a non-empty string, thus it's
  2199. // always true in booleans
  2200. compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
  2201. if (self.expression.has_side_effects(compressor)) {
  2202. return make_node(AST_Seq, self, {
  2203. car: self.expression,
  2204. cdr: make_node(AST_True, self)
  2205. });
  2206. }
  2207. return make_node(AST_True, self);
  2208. }
  2209. if (e instanceof AST_Binary && self.operator == "!") {
  2210. self = best_of(self, e.negate(compressor));
  2211. }
  2212. }
  2213. return self.evaluate(compressor)[0];
  2214. });
  2215. function has_side_effects_or_prop_access(node, compressor) {
  2216. var save_pure_getters = compressor.option("pure_getters");
  2217. compressor.options.pure_getters = false;
  2218. var ret = node.has_side_effects(compressor);
  2219. compressor.options.pure_getters = save_pure_getters;
  2220. return ret;
  2221. }
  2222. AST_Binary.DEFMETHOD("lift_sequences", function(compressor){
  2223. if (compressor.option("sequences")) {
  2224. if (this.left instanceof AST_Seq) {
  2225. var seq = this.left;
  2226. var x = seq.to_array();
  2227. this.left = x.pop();
  2228. x.push(this);
  2229. seq = AST_Seq.from_array(x).transform(compressor);
  2230. return seq;
  2231. }
  2232. if (this.right instanceof AST_Seq
  2233. && this instanceof AST_Assign
  2234. && !has_side_effects_or_prop_access(this.left, compressor)) {
  2235. var seq = this.right;
  2236. var x = seq.to_array();
  2237. this.right = x.pop();
  2238. x.push(this);
  2239. seq = AST_Seq.from_array(x).transform(compressor);
  2240. return seq;
  2241. }
  2242. }
  2243. return this;
  2244. });
  2245. var commutativeOperators = makePredicate("== === != !== * & | ^");
  2246. OPT(AST_Binary, function(self, compressor){
  2247. function reverse(op, force) {
  2248. if (force || !(self.left.has_side_effects(compressor) || self.right.has_side_effects(compressor))) {
  2249. if (op) self.operator = op;
  2250. var tmp = self.left;
  2251. self.left = self.right;
  2252. self.right = tmp;
  2253. }
  2254. }
  2255. if (commutativeOperators(self.operator)) {
  2256. if (self.right instanceof AST_Constant
  2257. && !(self.left instanceof AST_Constant)) {
  2258. // if right is a constant, whatever side effects the
  2259. // left side might have could not influence the
  2260. // result. hence, force switch.
  2261. if (!(self.left instanceof AST_Binary
  2262. && PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
  2263. reverse(null, true);
  2264. }
  2265. }
  2266. if (/^[!=]==?$/.test(self.operator)) {
  2267. if (self.left instanceof AST_SymbolRef && self.right instanceof AST_Conditional) {
  2268. if (self.right.consequent instanceof AST_SymbolRef
  2269. && self.right.consequent.definition() === self.left.definition()) {
  2270. if (/^==/.test(self.operator)) return self.right.condition;
  2271. if (/^!=/.test(self.operator)) return self.right.condition.negate(compressor);
  2272. }
  2273. if (self.right.alternative instanceof AST_SymbolRef
  2274. && self.right.alternative.definition() === self.left.definition()) {
  2275. if (/^==/.test(self.operator)) return self.right.condition.negate(compressor);
  2276. if (/^!=/.test(self.operator)) return self.right.condition;
  2277. }
  2278. }
  2279. if (self.right instanceof AST_SymbolRef && self.left instanceof AST_Conditional) {
  2280. if (self.left.consequent instanceof AST_SymbolRef
  2281. && self.left.consequent.definition() === self.right.definition()) {
  2282. if (/^==/.test(self.operator)) return self.left.condition;
  2283. if (/^!=/.test(self.operator)) return self.left.condition.negate(compressor);
  2284. }
  2285. if (self.left.alternative instanceof AST_SymbolRef
  2286. && self.left.alternative.definition() === self.right.definition()) {
  2287. if (/^==/.test(self.operator)) return self.left.condition.negate(compressor);
  2288. if (/^!=/.test(self.operator)) return self.left.condition;
  2289. }
  2290. }
  2291. }
  2292. }
  2293. self = self.lift_sequences(compressor);
  2294. if (compressor.option("comparisons")) switch (self.operator) {
  2295. case "===":
  2296. case "!==":
  2297. if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||
  2298. (self.left.is_boolean() && self.right.is_boolean())) {
  2299. self.operator = self.operator.substr(0, 2);
  2300. }
  2301. // XXX: intentionally falling down to the next case
  2302. case "==":
  2303. case "!=":
  2304. if (self.left instanceof AST_String
  2305. && self.left.value == "undefined"
  2306. && self.right instanceof AST_UnaryPrefix
  2307. && self.right.operator == "typeof"
  2308. && compressor.option("unsafe")) {
  2309. if (!(self.right.expression instanceof AST_SymbolRef)
  2310. || !self.right.expression.undeclared()) {
  2311. self.right = self.right.expression;
  2312. self.left = make_node(AST_Undefined, self.left).optimize(compressor);
  2313. if (self.operator.length == 2) self.operator += "=";
  2314. }
  2315. }
  2316. break;
  2317. }
  2318. if (compressor.option("conditionals")) {
  2319. if (self.operator == "&&") {
  2320. var ll = self.left.evaluate(compressor);
  2321. if (ll.length > 1) {
  2322. if (ll[1]) {
  2323. compressor.warn("Condition left of && always true [{file}:{line},{col}]", self.start);
  2324. return maintain_this_binding(compressor.parent(), self, self.right.evaluate(compressor)[0]);
  2325. } else {
  2326. compressor.warn("Condition left of && always false [{file}:{line},{col}]", self.start);
  2327. return maintain_this_binding(compressor.parent(), self, ll[0]);
  2328. }
  2329. }
  2330. }
  2331. else if (self.operator == "||") {
  2332. var ll = self.left.evaluate(compressor);
  2333. if (ll.length > 1) {
  2334. if (ll[1]) {
  2335. compressor.warn("Condition left of || always true [{file}:{line},{col}]", self.start);
  2336. return maintain_this_binding(compressor.parent(), self, ll[0]);
  2337. } else {
  2338. compressor.warn("Condition left of || always false [{file}:{line},{col}]", self.start);
  2339. return maintain_this_binding(compressor.parent(), self, self.right.evaluate(compressor)[0]);
  2340. }
  2341. }
  2342. }
  2343. }
  2344. if (compressor.option("booleans") && compressor.in_boolean_context()) switch (self.operator) {
  2345. case "&&":
  2346. var ll = self.left.evaluate(compressor);
  2347. var rr = self.right.evaluate(compressor);
  2348. if ((ll.length > 1 && !ll[1]) || (rr.length > 1 && !rr[1])) {
  2349. compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
  2350. if (self.left.has_side_effects(compressor)) {
  2351. return make_node(AST_Seq, self, {
  2352. car: self.left,
  2353. cdr: make_node(AST_False)
  2354. }).optimize(compressor);
  2355. }
  2356. return make_node(AST_False, self);
  2357. }
  2358. if (ll.length > 1 && ll[1]) {
  2359. return rr[0];
  2360. }
  2361. if (rr.length > 1 && rr[1]) {
  2362. return ll[0];
  2363. }
  2364. break;
  2365. case "||":
  2366. var ll = self.left.evaluate(compressor);
  2367. var rr = self.right.evaluate(compressor);
  2368. if ((ll.length > 1 && ll[1]) || (rr.length > 1 && rr[1])) {
  2369. compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
  2370. if (self.left.has_side_effects(compressor)) {
  2371. return make_node(AST_Seq, self, {
  2372. car: self.left,
  2373. cdr: make_node(AST_True)
  2374. }).optimize(compressor);
  2375. }
  2376. return make_node(AST_True, self);
  2377. }
  2378. if (ll.length > 1 && !ll[1]) {
  2379. return rr[0];
  2380. }
  2381. if (rr.length > 1 && !rr[1]) {
  2382. return ll[0];
  2383. }
  2384. break;
  2385. case "+":
  2386. var ll = self.left.evaluate(compressor);
  2387. var rr = self.right.evaluate(compressor);
  2388. if ((ll.length > 1 && ll[0] instanceof AST_String && ll[1] && !self.right.has_side_effects(compressor)) ||
  2389. (rr.length > 1 && rr[0] instanceof AST_String && rr[1] && !self.left.has_side_effects(compressor))) {
  2390. compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
  2391. return make_node(AST_True, self);
  2392. }
  2393. break;
  2394. }
  2395. if (compressor.option("comparisons") && self.is_boolean()) {
  2396. if (!(compressor.parent() instanceof AST_Binary)
  2397. || compressor.parent() instanceof AST_Assign) {
  2398. var negated = make_node(AST_UnaryPrefix, self, {
  2399. operator: "!",
  2400. expression: self.negate(compressor)
  2401. });
  2402. self = best_of(self, negated);
  2403. }
  2404. if (compressor.option("unsafe_comps")) {
  2405. switch (self.operator) {
  2406. case "<": reverse(">"); break;
  2407. case "<=": reverse(">="); break;
  2408. }
  2409. }
  2410. }
  2411. if (self.operator == "+" && self.right instanceof AST_String
  2412. && self.right.getValue() === "" && self.left instanceof AST_Binary
  2413. && self.left.operator == "+" && self.left.is_string(compressor)) {
  2414. return self.left;
  2415. }
  2416. if (compressor.option("evaluate")) {
  2417. if (self.operator == "+") {
  2418. if (self.left instanceof AST_Constant
  2419. && self.right instanceof AST_Binary
  2420. && self.right.operator == "+"
  2421. && self.right.left instanceof AST_Constant
  2422. && self.right.is_string(compressor)) {
  2423. self = make_node(AST_Binary, self, {
  2424. operator: "+",
  2425. left: make_node(AST_String, null, {
  2426. value: "" + self.left.getValue() + self.right.left.getValue(),
  2427. start: self.left.start,
  2428. end: self.right.left.end
  2429. }),
  2430. right: self.right.right
  2431. });
  2432. }
  2433. if (self.right instanceof AST_Constant
  2434. && self.left instanceof AST_Binary
  2435. && self.left.operator == "+"
  2436. && self.left.right instanceof AST_Constant
  2437. && self.left.is_string(compressor)) {
  2438. self = make_node(AST_Binary, self, {
  2439. operator: "+",
  2440. left: self.left.left,
  2441. right: make_node(AST_String, null, {
  2442. value: "" + self.left.right.getValue() + self.right.getValue(),
  2443. start: self.left.right.start,
  2444. end: self.right.end
  2445. })
  2446. });
  2447. }
  2448. if (self.left instanceof AST_Binary
  2449. && self.left.operator == "+"
  2450. && self.left.is_string(compressor)
  2451. && self.left.right instanceof AST_Constant
  2452. && self.right instanceof AST_Binary
  2453. && self.right.operator == "+"
  2454. && self.right.left instanceof AST_Constant
  2455. && self.right.is_string(compressor)) {
  2456. self = make_node(AST_Binary, self, {
  2457. operator: "+",
  2458. left: make_node(AST_Binary, self.left, {
  2459. operator: "+",
  2460. left: self.left.left,
  2461. right: make_node(AST_String, null, {
  2462. value: "" + self.left.right.getValue() + self.right.left.getValue(),
  2463. start: self.left.right.start,
  2464. end: self.right.left.end
  2465. })
  2466. }),
  2467. right: self.right.right
  2468. });
  2469. }
  2470. }
  2471. }
  2472. // x && (y && z) ==> x && y && z
  2473. // x || (y || z) ==> x || y || z
  2474. if (self.right instanceof AST_Binary
  2475. && self.right.operator == self.operator
  2476. && (self.operator == "&&" || self.operator == "||"))
  2477. {
  2478. self.left = make_node(AST_Binary, self.left, {
  2479. operator : self.operator,
  2480. left : self.left,
  2481. right : self.right.left
  2482. });
  2483. self.right = self.right.right;
  2484. return self.transform(compressor);
  2485. }
  2486. return self.evaluate(compressor)[0];
  2487. });
  2488. OPT(AST_SymbolRef, function(self, compressor){
  2489. function isLHS(symbol, parent) {
  2490. return (
  2491. parent instanceof AST_Binary &&
  2492. parent.operator === '=' &&
  2493. parent.left === symbol
  2494. );
  2495. }
  2496. if (self.undeclared() && !isLHS(self, compressor.parent())) {
  2497. var defines = compressor.option("global_defs");
  2498. if (defines && HOP(defines, self.name)) {
  2499. return make_node_from_constant(compressor, defines[self.name], self);
  2500. }
  2501. // testing against !self.scope.uses_with first is an optimization
  2502. if (!self.scope.uses_with || !compressor.find_parent(AST_With)) {
  2503. switch (self.name) {
  2504. case "undefined":
  2505. return make_node(AST_Undefined, self);
  2506. case "NaN":
  2507. return make_node(AST_NaN, self).transform(compressor);
  2508. case "Infinity":
  2509. return make_node(AST_Infinity, self).transform(compressor);
  2510. }
  2511. }
  2512. }
  2513. return self;
  2514. });
  2515. OPT(AST_Infinity, function (self, compressor) {
  2516. return make_node(AST_Binary, self, {
  2517. operator : '/',
  2518. left : make_node(AST_Number, self, {value: 1}),
  2519. right : make_node(AST_Number, self, {value: 0})
  2520. });
  2521. });
  2522. OPT(AST_Undefined, function(self, compressor){
  2523. if (compressor.option("unsafe")) {
  2524. var scope = compressor.find_parent(AST_Scope);
  2525. var undef = scope.find_variable("undefined");
  2526. if (undef) {
  2527. var ref = make_node(AST_SymbolRef, self, {
  2528. name : "undefined",
  2529. scope : scope,
  2530. thedef : undef
  2531. });
  2532. ref.reference();
  2533. return ref;
  2534. }
  2535. }
  2536. return self;
  2537. });
  2538. var ASSIGN_OPS = [ '+', '-', '/', '*', '%', '>>', '<<', '>>>', '|', '^', '&' ];
  2539. var ASSIGN_OPS_COMMUTATIVE = [ '*', '|', '^', '&' ];
  2540. OPT(AST_Assign, function(self, compressor){
  2541. self = self.lift_sequences(compressor);
  2542. if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) {
  2543. // x = expr1 OP expr2
  2544. if (self.right.left instanceof AST_SymbolRef
  2545. && self.right.left.name == self.left.name
  2546. && member(self.right.operator, ASSIGN_OPS)) {
  2547. // x = x - 2 ---> x -= 2
  2548. self.operator = self.right.operator + "=";
  2549. self.right = self.right.right;
  2550. }
  2551. else if (self.right.right instanceof AST_SymbolRef
  2552. && self.right.right.name == self.left.name
  2553. && member(self.right.operator, ASSIGN_OPS_COMMUTATIVE)
  2554. && !self.right.left.has_side_effects(compressor)) {
  2555. // x = 2 & x ---> x &= 2
  2556. self.operator = self.right.operator + "=";
  2557. self.right = self.right.left;
  2558. }
  2559. }
  2560. return self;
  2561. });
  2562. OPT(AST_Conditional, function(self, compressor){
  2563. if (!compressor.option("conditionals")) return self;
  2564. if (self.condition instanceof AST_Seq) {
  2565. var car = self.condition.car;
  2566. self.condition = self.condition.cdr;
  2567. return AST_Seq.cons(car, self);
  2568. }
  2569. var cond = self.condition.evaluate(compressor);
  2570. if (cond.length > 1) {
  2571. if (cond[1]) {
  2572. compressor.warn("Condition always true [{file}:{line},{col}]", self.start);
  2573. return maintain_this_binding(compressor.parent(), self, self.consequent);
  2574. } else {
  2575. compressor.warn("Condition always false [{file}:{line},{col}]", self.start);
  2576. return maintain_this_binding(compressor.parent(), self, self.alternative);
  2577. }
  2578. }
  2579. var negated = cond[0].negate(compressor);
  2580. if (best_of(cond[0], negated) === negated) {
  2581. self = make_node(AST_Conditional, self, {
  2582. condition: negated,
  2583. consequent: self.alternative,
  2584. alternative: self.consequent
  2585. });
  2586. }
  2587. var consequent = self.consequent;
  2588. var alternative = self.alternative;
  2589. if (consequent instanceof AST_Assign
  2590. && alternative instanceof AST_Assign
  2591. && consequent.operator == alternative.operator
  2592. && consequent.left.equivalent_to(alternative.left)
  2593. && !consequent.left.has_side_effects(compressor)
  2594. ) {
  2595. /*
  2596. * Stuff like this:
  2597. * if (foo) exp = something; else exp = something_else;
  2598. * ==>
  2599. * exp = foo ? something : something_else;
  2600. */
  2601. return make_node(AST_Assign, self, {
  2602. operator: consequent.operator,
  2603. left: consequent.left,
  2604. right: make_node(AST_Conditional, self, {
  2605. condition: self.condition,
  2606. consequent: consequent.right,
  2607. alternative: alternative.right
  2608. })
  2609. });
  2610. }
  2611. if (consequent instanceof AST_Call
  2612. && alternative.TYPE === consequent.TYPE
  2613. && consequent.args.length == alternative.args.length
  2614. && !consequent.expression.has_side_effects(compressor)
  2615. && consequent.expression.equivalent_to(alternative.expression)) {
  2616. if (consequent.args.length == 0) {
  2617. return make_node(AST_Seq, self, {
  2618. car: self.condition,
  2619. cdr: consequent
  2620. });
  2621. }
  2622. if (consequent.args.length == 1) {
  2623. consequent.args[0] = make_node(AST_Conditional, self, {
  2624. condition: self.condition,
  2625. consequent: consequent.args[0],
  2626. alternative: alternative.args[0]
  2627. });
  2628. return consequent;
  2629. }
  2630. }
  2631. // x?y?z:a:a --> x&&y?z:a
  2632. if (consequent instanceof AST_Conditional
  2633. && consequent.alternative.equivalent_to(alternative)) {
  2634. return make_node(AST_Conditional, self, {
  2635. condition: make_node(AST_Binary, self, {
  2636. left: self.condition,
  2637. operator: "&&",
  2638. right: consequent.condition
  2639. }),
  2640. consequent: consequent.consequent,
  2641. alternative: alternative
  2642. });
  2643. }
  2644. // y?1:1 --> 1
  2645. if (consequent.is_constant(compressor)
  2646. && alternative.is_constant(compressor)
  2647. && consequent.equivalent_to(alternative)) {
  2648. var consequent_value = consequent.constant_value(compressor);
  2649. if (self.condition.has_side_effects(compressor)) {
  2650. return AST_Seq.from_array([self.condition, make_node_from_constant(compressor, consequent_value, self)]);
  2651. } else {
  2652. return make_node_from_constant(compressor, consequent_value, self);
  2653. }
  2654. }
  2655. if (is_true(self.consequent)) {
  2656. if (is_false(self.alternative)) {
  2657. // c ? true : false ---> !!c
  2658. return booleanize(self.condition);
  2659. }
  2660. // c ? true : x ---> !!c || x
  2661. return make_node(AST_Binary, self, {
  2662. operator: "||",
  2663. left: booleanize(self.condition),
  2664. right: self.alternative
  2665. });
  2666. }
  2667. if (is_false(self.consequent)) {
  2668. if (is_true(self.alternative)) {
  2669. // c ? false : true ---> !c
  2670. return booleanize(self.condition.negate(compressor));
  2671. }
  2672. // c ? false : x ---> !c && x
  2673. return make_node(AST_Binary, self, {
  2674. operator: "&&",
  2675. left: booleanize(self.condition.negate(compressor)),
  2676. right: self.alternative
  2677. });
  2678. }
  2679. if (is_true(self.alternative)) {
  2680. // c ? x : true ---> !c || x
  2681. return make_node(AST_Binary, self, {
  2682. operator: "||",
  2683. left: booleanize(self.condition.negate(compressor)),
  2684. right: self.consequent
  2685. });
  2686. }
  2687. if (is_false(self.alternative)) {
  2688. // c ? x : false ---> !!c && x
  2689. return make_node(AST_Binary, self, {
  2690. operator: "&&",
  2691. left: booleanize(self.condition),
  2692. right: self.consequent
  2693. });
  2694. }
  2695. return self;
  2696. function booleanize(node) {
  2697. if (node.is_boolean()) return node;
  2698. // !!expression
  2699. return make_node(AST_UnaryPrefix, node, {
  2700. operator: "!",
  2701. expression: node.negate(compressor)
  2702. });
  2703. }
  2704. // AST_True or !0
  2705. function is_true(node) {
  2706. return node instanceof AST_True
  2707. || (node instanceof AST_UnaryPrefix
  2708. && node.operator == "!"
  2709. && node.expression instanceof AST_Constant
  2710. && !node.expression.value);
  2711. }
  2712. // AST_False or !1
  2713. function is_false(node) {
  2714. return node instanceof AST_False
  2715. || (node instanceof AST_UnaryPrefix
  2716. && node.operator == "!"
  2717. && node.expression instanceof AST_Constant
  2718. && !!node.expression.value);
  2719. }
  2720. });
  2721. OPT(AST_Boolean, function(self, compressor){
  2722. if (compressor.option("booleans")) {
  2723. var p = compressor.parent();
  2724. if (p instanceof AST_Binary && (p.operator == "=="
  2725. || p.operator == "!=")) {
  2726. compressor.warn("Non-strict equality against boolean: {operator} {value} [{file}:{line},{col}]", {
  2727. operator : p.operator,
  2728. value : self.value,
  2729. file : p.start.file,
  2730. line : p.start.line,
  2731. col : p.start.col,
  2732. });
  2733. return make_node(AST_Number, self, {
  2734. value: +self.value
  2735. });
  2736. }
  2737. return make_node(AST_UnaryPrefix, self, {
  2738. operator: "!",
  2739. expression: make_node(AST_Number, self, {
  2740. value: 1 - self.value
  2741. })
  2742. });
  2743. }
  2744. return self;
  2745. });
  2746. OPT(AST_Sub, function(self, compressor){
  2747. var prop = self.property;
  2748. if (prop instanceof AST_String && compressor.option("properties")) {
  2749. prop = prop.getValue();
  2750. if (RESERVED_WORDS(prop) ? compressor.option("screw_ie8") : is_identifier_string(prop)) {
  2751. return make_node(AST_Dot, self, {
  2752. expression : self.expression,
  2753. property : prop
  2754. }).optimize(compressor);
  2755. }
  2756. var v = parseFloat(prop);
  2757. if (!isNaN(v) && v.toString() == prop) {
  2758. self.property = make_node(AST_Number, self.property, {
  2759. value: v
  2760. });
  2761. }
  2762. }
  2763. return self;
  2764. });
  2765. OPT(AST_Dot, function(self, compressor){
  2766. var prop = self.property;
  2767. if (RESERVED_WORDS(prop) && !compressor.option("screw_ie8")) {
  2768. return make_node(AST_Sub, self, {
  2769. expression : self.expression,
  2770. property : make_node(AST_String, self, {
  2771. value: prop
  2772. })
  2773. }).optimize(compressor);
  2774. }
  2775. return self.evaluate(compressor)[0];
  2776. });
  2777. function literals_in_boolean_context(self, compressor) {
  2778. if (compressor.option("booleans") && compressor.in_boolean_context() && !self.has_side_effects(compressor)) {
  2779. return make_node(AST_True, self);
  2780. }
  2781. return self;
  2782. };
  2783. OPT(AST_Array, literals_in_boolean_context);
  2784. OPT(AST_Object, literals_in_boolean_context);
  2785. OPT(AST_RegExp, literals_in_boolean_context);
  2786. OPT(AST_Return, function(self, compressor){
  2787. if (self.value instanceof AST_Undefined) {
  2788. self.value = null;
  2789. }
  2790. return self;
  2791. });
  2792. })();