compress.js 107 KB

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