lib/uglify.js in uglifier-1.2.6 vs lib/uglify.js in uglifier-1.2.7

- old
+ new

@@ -2630,10 +2630,14 @@ exports.is_identifier_start = is_identifier_start; exports.is_identifier_char = is_identifier_char; exports.set_logger = function(logger) { warn = logger; }; + +// Local variables: +// js-indent-level: 8 +// End: }, "process": function(exports, require, module) {/*********************************************************************** A JavaScript tokenizer / parser / beautifier / compressor. This version is suitable for Node.js. With minimal changes (the @@ -2691,10 +2695,11 @@ SUCH DAMAGE. ***********************************************************************/ var jsp = require("./parse-js"), + curry = jsp.curry, slice = jsp.slice, member = jsp.member, is_identifier_char = jsp.is_identifier_char, PRECEDENCE = jsp.PRECEDENCE, OPERATORS = jsp.OPERATORS; @@ -3021,12 +3026,12 @@ if (type == "var" || !HOP(this.names, name)) this.names[name] = type || "var"; return name; } }, - active: function(dir) { - return member(dir, this.directives) || this.parent && this.parent.active(dir); + active_directive: function(dir) { + return member(dir, this.directives) || this.parent && this.parent.active_directive(dir); } }; function ast_add_scope(ast) { @@ -3469,13 +3474,13 @@ for (var i = 0; i < statements.length; ++i) { var fi = statements[i]; if (fi[0] != "if") continue; - if (fi[3] && walk(fi[3])) continue; + if (fi[3]) continue; - var t = walk(fi[2]); + var t = fi[2]; if (!aborts(t)) continue; var conditional = walk(fi[1]); var e_body = redo_if(statements.slice(i + 1)); @@ -3530,10 +3535,14 @@ function found(){ return handler.call(this, this, w, stop, restart) }; function unary(op) { if (op == "++" || op == "--") return found.apply(this, arguments); }; + function binary(op) { + if (op == "&&" || op == "||") + return found.apply(this, arguments); + }; return w.with_walkers({ "try": found, "throw": found, "return": found, "new": found, @@ -3548,10 +3557,12 @@ "while": found, "do": found, "return": found, "unary-prefix": unary, "unary-postfix": unary, + "conditional": found, + "binary": binary, "defun": found }, function(){ while (true) try { walk(ast); break; @@ -3623,11 +3634,11 @@ if (!d[1]) continue; d = [ "assign", true, [ "name", d[0] ], d[1] ]; if (ret == null) ret = d; else ret = [ "seq", d, ret ]; } - if (ret == null) { + if (ret == null && w.parent()[0] != "for") { if (w.parent()[0] == "for-in") return [ "name", defs[0][0] ]; return MAP.skip; } return [ "stat", ret ]; @@ -3654,10 +3665,16 @@ return walk(ast_add_scope(ast)); }); }; function ast_squeeze(ast, options) { + ast = squeeze_1(ast, options); + ast = squeeze_2(ast, options); + return ast; +}; + +function squeeze_1(ast, options) { options = defaults(options, { make_seqs : true, dead_code : true, no_warnings : false, keep_comps : true, @@ -3725,23 +3742,13 @@ } return block; }; function _lambda(name, args, body) { - return [ this[0], name, args, with_scope(body.scope, function() { - return tighten(body, "lambda"); - }) ]; + return [ this[0], name, args, tighten(body, "lambda") ]; }; - function with_scope(s, cont) { - var _scope = scope; - scope = s; - var ret = cont(); - scope = _scope; - return ret; - }; - // this function does a few things: // 1. discard useless blocks // 2. join consecutive var declarations // 3. remove obviously dead code // 4. transform consecutive statements using the comma operator @@ -3955,13 +3962,11 @@ return [ "sub", walk(expr), [ "num", parseInt(name, 10) ] ]; } }, "if": make_if, "toplevel": function(body) { - return with_scope(this.scope, function() { - return [ "toplevel", tighten(body) ]; - }); + return [ "toplevel", tighten(body) ]; }, "switch": function(expr, body) { var last = body.length - 1; return [ "switch", walk(expr), MAP(body, function(branch, i){ var block = tighten(branch[1]); @@ -4029,16 +4034,10 @@ rvalue[2][1] === lvalue[1]) { return [ this[0], rvalue[1], lvalue, rvalue[3] ] } return [ this[0], op, lvalue, rvalue ]; }, - "directive": function(dir) { - if (scope.active(dir)) - return [ "block" ]; - scope.directives.push(dir); - return [ this[0], dir ]; - }, "call": function(expr, args) { expr = walk(expr); if (options.unsafe && expr[0] == "dot" && expr[1][0] == "string" && expr[2] == "toString") { return expr[1]; } @@ -4052,18 +4051,42 @@ : [ "num", 0 ], [ "num", 0 ] ]; return [ this[0], num ]; } }, function() { - for (var i = 0; i < 2; ++i) { - ast = prepare_ifs(ast); - ast = walk(ast_add_scope(ast)); - } - return ast; + return walk(prepare_ifs(walk(prepare_ifs(ast)))); }); }; +function squeeze_2(ast, options) { + var w = ast_walker(), walk = w.walk, scope; + function with_scope(s, cont) { + var save = scope, ret; + scope = s; + ret = cont(); + scope = save; + return ret; + }; + function lambda(name, args, body) { + return [ this[0], name, args, with_scope(body.scope, curry(MAP, body, walk)) ]; + }; + return w.with_walkers({ + "directive": function(dir) { + if (scope.active_directive(dir)) + return [ "block" ]; + scope.directives.push(dir); + }, + "toplevel": function(body) { + return [ this[0], with_scope(this.scope, curry(MAP, body, walk)) ]; + }, + "function": lambda, + "defun": lambda + }, function(){ + return walk(ast_add_scope(ast)); + }); +}; + /* -----[ re-generate code from the AST ]----- */ var DOT_CALL_NO_PARENS = jsp.array_to_hash([ "name", "array", @@ -4465,11 +4488,13 @@ if (!beautify && el[0] == "atom" && el[1] == "undefined") return i === elements.length - 1 ? "," : ""; return parenthesize(el, "seq"); })), "]" ]); }, "stat": function(stmt) { - return make(stmt).replace(/;*\s*$/, ";"); + return stmt != null + ? make(stmt).replace(/;*\s*$/, ";") + : ";"; }, "seq": function() { return add_commas(MAP(slice(arguments), make)); }, "label": function(name, block) { @@ -4721,10 +4746,14 @@ exports.split_lines = split_lines; exports.MAP = MAP; // keep this last! exports.ast_squeeze_more = require("./squeeze-more").ast_squeeze_more; + +// Local variables: +// js-indent-level: 8 +// End: }, "squeeze-more": function(exports, require, module) {var jsp = require("./parse-js"), pro = require("./process"), slice = jsp.slice, member = jsp.member, curry = jsp.curry, @@ -4795,9 +4824,13 @@ return walk(pro.ast_add_scope(ast)); }); }; exports.ast_squeeze_more = ast_squeeze_more; + +// Local variables: +// js-indent-level: 8 +// End: }}); ; global.UglifyJS = {}; global.UglifyJS.parser = this.require('parse-js'); global.UglifyJS.uglify = this.require('process');