compress.js 107 KB

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