react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / umd / node_modules / ruglify / node_modules / uglify-js / lib / compress.js
81166 views/***********************************************************************12A JavaScript tokenizer / parser / beautifier / compressor.3https://github.com/mishoo/UglifyJS245-------------------------------- (C) ---------------------------------67Author: Mihai Bazon8<[email protected]>9http://mihai.bazon.net/blog1011Distributed under the BSD license:1213Copyright 2012 (c) Mihai Bazon <[email protected]>1415Redistribution and use in source and binary forms, with or without16modification, are permitted provided that the following conditions17are met:1819* Redistributions of source code must retain the above20copyright notice, this list of conditions and the following21disclaimer.2223* Redistributions in binary form must reproduce the above24copyright notice, this list of conditions and the following25disclaimer in the documentation and/or other materials26provided with the distribution.2728THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY29EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE30IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR31PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE32LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,33OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,34PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR35PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY36THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR37TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF38THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF39SUCH DAMAGE.4041***********************************************************************/4243"use strict";4445function Compressor(options, false_by_default) {46if (!(this instanceof Compressor))47return new Compressor(options, false_by_default);48TreeTransformer.call(this, this.before, this.after);49this.options = defaults(options, {50sequences : !false_by_default,51properties : !false_by_default,52dead_code : !false_by_default,53drop_debugger : !false_by_default,54unsafe : !false_by_default,55unsafe_comps : false,56conditionals : !false_by_default,57comparisons : !false_by_default,58evaluate : !false_by_default,59booleans : !false_by_default,60loops : !false_by_default,61unused : !false_by_default,62hoist_funs : !false_by_default,63hoist_vars : false,64if_return : !false_by_default,65join_vars : !false_by_default,66cascade : !false_by_default,67side_effects : !false_by_default,6869warnings : true,70global_defs : {}71}, true);72};7374Compressor.prototype = new TreeTransformer;75merge(Compressor.prototype, {76option: function(key) { return this.options[key] },77warn: function() {78if (this.options.warnings)79AST_Node.warn.apply(AST_Node, arguments);80},81before: function(node, descend, in_list) {82if (node._squeezed) return node;83if (node instanceof AST_Scope) {84node.drop_unused(this);85node = node.hoist_declarations(this);86}87descend(node, this);88node = node.optimize(this);89if (node instanceof AST_Scope) {90// dead code removal might leave further unused declarations.91// this'll usually save very few bytes, but the performance92// hit seems negligible so I'll just drop it here.9394// no point to repeat warnings.95var save_warnings = this.options.warnings;96this.options.warnings = false;97node.drop_unused(this);98this.options.warnings = save_warnings;99}100node._squeezed = true;101return node;102}103});104105(function(){106107function OPT(node, optimizer) {108node.DEFMETHOD("optimize", function(compressor){109var self = this;110if (self._optimized) return self;111var opt = optimizer(self, compressor);112opt._optimized = true;113if (opt === self) return opt;114return opt.transform(compressor);115});116};117118OPT(AST_Node, function(self, compressor){119return self;120});121122AST_Node.DEFMETHOD("equivalent_to", function(node){123// XXX: this is a rather expensive way to test two node's equivalence:124return this.print_to_string() == node.print_to_string();125});126127function make_node(ctor, orig, props) {128if (!props) props = {};129if (orig) {130if (!props.start) props.start = orig.start;131if (!props.end) props.end = orig.end;132}133return new ctor(props);134};135136function make_node_from_constant(compressor, val, orig) {137// XXX: WIP.138// if (val instanceof AST_Node) return val.transform(new TreeTransformer(null, function(node){139// if (node instanceof AST_SymbolRef) {140// var scope = compressor.find_parent(AST_Scope);141// var def = scope.find_variable(node);142// node.thedef = def;143// return node;144// }145// })).transform(compressor);146147if (val instanceof AST_Node) return val.transform(compressor);148switch (typeof val) {149case "string":150return make_node(AST_String, orig, {151value: val152}).optimize(compressor);153case "number":154return make_node(isNaN(val) ? AST_NaN : AST_Number, orig, {155value: val156}).optimize(compressor);157case "boolean":158return make_node(val ? AST_True : AST_False, orig);159case "undefined":160return make_node(AST_Undefined, orig).optimize(compressor);161default:162if (val === null) {163return make_node(AST_Null, orig).optimize(compressor);164}165if (val instanceof RegExp) {166return make_node(AST_RegExp, orig).optimize(compressor);167}168throw new Error(string_template("Can't handle constant of type: {type}", {169type: typeof val170}));171}172};173174function as_statement_array(thing) {175if (thing === null) return [];176if (thing instanceof AST_BlockStatement) return thing.body;177if (thing instanceof AST_EmptyStatement) return [];178if (thing instanceof AST_Statement) return [ thing ];179throw new Error("Can't convert thing to statement array");180};181182function is_empty(thing) {183if (thing === null) return true;184if (thing instanceof AST_EmptyStatement) return true;185if (thing instanceof AST_BlockStatement) return thing.body.length == 0;186return false;187};188189function loop_body(x) {190if (x instanceof AST_Switch) return x;191if (x instanceof AST_For || x instanceof AST_ForIn || x instanceof AST_DWLoop) {192return (x.body instanceof AST_BlockStatement ? x.body : x);193}194return x;195};196197function tighten_body(statements, compressor) {198var CHANGED;199do {200CHANGED = false;201statements = eliminate_spurious_blocks(statements);202if (compressor.option("dead_code")) {203statements = eliminate_dead_code(statements, compressor);204}205if (compressor.option("if_return")) {206statements = handle_if_return(statements, compressor);207}208if (compressor.option("sequences")) {209statements = sequencesize(statements, compressor);210}211if (compressor.option("join_vars")) {212statements = join_consecutive_vars(statements, compressor);213}214} while (CHANGED);215return statements;216217function eliminate_spurious_blocks(statements) {218var seen_dirs = [];219return statements.reduce(function(a, stat){220if (stat instanceof AST_BlockStatement) {221CHANGED = true;222a.push.apply(a, eliminate_spurious_blocks(stat.body));223} else if (stat instanceof AST_EmptyStatement) {224CHANGED = true;225} else if (stat instanceof AST_Directive) {226if (seen_dirs.indexOf(stat.value) < 0) {227a.push(stat);228seen_dirs.push(stat.value);229} else {230CHANGED = true;231}232} else {233a.push(stat);234}235return a;236}, []);237};238239function handle_if_return(statements, compressor) {240var self = compressor.self();241var in_lambda = self instanceof AST_Lambda;242var ret = [];243loop: for (var i = statements.length; --i >= 0;) {244var stat = statements[i];245switch (true) {246case (in_lambda && stat instanceof AST_Return && !stat.value && ret.length == 0):247CHANGED = true;248// note, ret.length is probably always zero249// because we drop unreachable code before this250// step. nevertheless, it's good to check.251continue loop;252case stat instanceof AST_If:253if (stat.body instanceof AST_Return) {254//---255// pretty silly case, but:256// if (foo()) return; return; ==> foo(); return;257if (((in_lambda && ret.length == 0)258|| (ret[0] instanceof AST_Return && !ret[0].value))259&& !stat.body.value && !stat.alternative) {260CHANGED = true;261var cond = make_node(AST_SimpleStatement, stat.condition, {262body: stat.condition263});264ret.unshift(cond);265continue loop;266}267//---268// if (foo()) return x; return y; ==> return foo() ? x : y;269if (ret[0] instanceof AST_Return && stat.body.value && ret[0].value && !stat.alternative) {270CHANGED = true;271stat = stat.clone();272stat.alternative = ret[0];273ret[0] = stat.transform(compressor);274continue loop;275}276//---277// if (foo()) return x; [ return ; ] ==> return foo() ? x : undefined;278if ((ret.length == 0 || ret[0] instanceof AST_Return) && stat.body.value && !stat.alternative && in_lambda) {279CHANGED = true;280stat = stat.clone();281stat.alternative = ret[0] || make_node(AST_Return, stat, {282value: make_node(AST_Undefined, stat)283});284ret[0] = stat.transform(compressor);285continue loop;286}287//---288// if (foo()) return; [ else x... ]; y... ==> if (!foo()) { x...; y... }289if (!stat.body.value && in_lambda) {290CHANGED = true;291stat = stat.clone();292stat.condition = stat.condition.negate(compressor);293stat.body = make_node(AST_BlockStatement, stat, {294body: as_statement_array(stat.alternative).concat(ret)295});296stat.alternative = null;297ret = [ stat.transform(compressor) ];298continue loop;299}300//---301if (ret.length == 1 && in_lambda && ret[0] instanceof AST_SimpleStatement302&& (!stat.alternative || stat.alternative instanceof AST_SimpleStatement)) {303CHANGED = true;304ret.push(make_node(AST_Return, ret[0], {305value: make_node(AST_Undefined, ret[0])306}).transform(compressor));307ret = as_statement_array(stat.alternative).concat(ret);308ret.unshift(stat);309continue loop;310}311}312313var ab = aborts(stat.body);314var lct = ab instanceof AST_LoopControl ? compressor.loopcontrol_target(ab.label) : null;315if (ab && ((ab instanceof AST_Return && !ab.value && in_lambda)316|| (ab instanceof AST_Continue && self === loop_body(lct))317|| (ab instanceof AST_Break && lct instanceof AST_BlockStatement && self === lct))) {318if (ab.label) {319remove(ab.label.thedef.references, ab.label);320}321CHANGED = true;322var body = as_statement_array(stat.body).slice(0, -1);323stat = stat.clone();324stat.condition = stat.condition.negate(compressor);325stat.body = make_node(AST_BlockStatement, stat, {326body: ret327});328stat.alternative = make_node(AST_BlockStatement, stat, {329body: body330});331ret = [ stat.transform(compressor) ];332continue loop;333}334335var ab = aborts(stat.alternative);336var lct = ab instanceof AST_LoopControl ? compressor.loopcontrol_target(ab.label) : null;337if (ab && ((ab instanceof AST_Return && !ab.value && in_lambda)338|| (ab instanceof AST_Continue && self === loop_body(lct))339|| (ab instanceof AST_Break && lct instanceof AST_BlockStatement && self === lct))) {340if (ab.label) {341remove(ab.label.thedef.references, ab.label);342}343CHANGED = true;344stat = stat.clone();345stat.body = make_node(AST_BlockStatement, stat.body, {346body: as_statement_array(stat.body).concat(ret)347});348stat.alternative = make_node(AST_BlockStatement, stat.alternative, {349body: as_statement_array(stat.alternative).slice(0, -1)350});351ret = [ stat.transform(compressor) ];352continue loop;353}354355ret.unshift(stat);356break;357default:358ret.unshift(stat);359break;360}361}362return ret;363};364365function eliminate_dead_code(statements, compressor) {366var has_quit = false;367var orig = statements.length;368var self = compressor.self();369statements = statements.reduce(function(a, stat){370if (has_quit) {371extract_declarations_from_unreachable_code(compressor, stat, a);372} else {373if (stat instanceof AST_LoopControl) {374var lct = compressor.loopcontrol_target(stat.label);375if ((stat instanceof AST_Break376&& lct instanceof AST_BlockStatement377&& loop_body(lct) === self) || (stat instanceof AST_Continue378&& loop_body(lct) === self)) {379if (stat.label) {380remove(stat.label.thedef.references, stat.label);381}382} else {383a.push(stat);384}385} else {386a.push(stat);387}388if (aborts(stat)) has_quit = true;389}390return a;391}, []);392CHANGED = statements.length != orig;393return statements;394};395396function sequencesize(statements, compressor) {397if (statements.length < 2) return statements;398var seq = [], ret = [];399function push_seq() {400seq = AST_Seq.from_array(seq);401if (seq) ret.push(make_node(AST_SimpleStatement, seq, {402body: seq403}));404seq = [];405};406statements.forEach(function(stat){407if (stat instanceof AST_SimpleStatement) seq.push(stat.body);408else push_seq(), ret.push(stat);409});410push_seq();411ret = sequencesize_2(ret, compressor);412CHANGED = ret.length != statements.length;413return ret;414};415416function sequencesize_2(statements, compressor) {417function cons_seq(right) {418ret.pop();419var left = prev.body;420if (left instanceof AST_Seq) {421left.add(right);422} else {423left = AST_Seq.cons(left, right);424}425return left.transform(compressor);426};427var ret = [], prev = null;428statements.forEach(function(stat){429if (prev) {430if (stat instanceof AST_For) {431var opera = {};432try {433prev.body.walk(new TreeWalker(function(node){434if (node instanceof AST_Binary && node.operator == "in")435throw opera;436}));437if (stat.init && !(stat.init instanceof AST_Definitions)) {438stat.init = cons_seq(stat.init);439}440else if (!stat.init) {441stat.init = prev.body;442ret.pop();443}444} catch(ex) {445if (ex !== opera) throw ex;446}447}448else if (stat instanceof AST_If) {449stat.condition = cons_seq(stat.condition);450}451else if (stat instanceof AST_With) {452stat.expression = cons_seq(stat.expression);453}454else if (stat instanceof AST_Exit && stat.value) {455stat.value = cons_seq(stat.value);456}457else if (stat instanceof AST_Exit) {458stat.value = cons_seq(make_node(AST_Undefined, stat));459}460else if (stat instanceof AST_Switch) {461stat.expression = cons_seq(stat.expression);462}463}464ret.push(stat);465prev = stat instanceof AST_SimpleStatement ? stat : null;466});467return ret;468};469470function join_consecutive_vars(statements, compressor) {471var prev = null;472return statements.reduce(function(a, stat){473if (stat instanceof AST_Definitions && prev && prev.TYPE == stat.TYPE) {474prev.definitions = prev.definitions.concat(stat.definitions);475CHANGED = true;476}477else if (stat instanceof AST_For478&& prev instanceof AST_Definitions479&& (!stat.init || stat.init.TYPE == prev.TYPE)) {480CHANGED = true;481a.pop();482if (stat.init) {483stat.init.definitions = prev.definitions.concat(stat.init.definitions);484} else {485stat.init = prev;486}487a.push(stat);488prev = stat;489}490else {491prev = stat;492a.push(stat);493}494return a;495}, []);496};497498};499500function extract_declarations_from_unreachable_code(compressor, stat, target) {501compressor.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start);502stat.walk(new TreeWalker(function(node){503if (node instanceof AST_Definitions) {504compressor.warn("Declarations in unreachable code! [{file}:{line},{col}]", node.start);505node.remove_initializers();506target.push(node);507return true;508}509if (node instanceof AST_Defun) {510target.push(node);511return true;512}513if (node instanceof AST_Scope) {514return true;515}516}));517};518519/* -----[ boolean/negation helpers ]----- */520521// methods to determine whether an expression has a boolean result type522(function (def){523var unary_bool = [ "!", "delete" ];524var binary_bool = [ "in", "instanceof", "==", "!=", "===", "!==", "<", "<=", ">=", ">" ];525def(AST_Node, function(){ return false });526def(AST_UnaryPrefix, function(){527return member(this.operator, unary_bool);528});529def(AST_Binary, function(){530return member(this.operator, binary_bool) ||531( (this.operator == "&&" || this.operator == "||") &&532this.left.is_boolean() && this.right.is_boolean() );533});534def(AST_Conditional, function(){535return this.consequent.is_boolean() && this.alternative.is_boolean();536});537def(AST_Assign, function(){538return this.operator == "=" && this.right.is_boolean();539});540def(AST_Seq, function(){541return this.cdr.is_boolean();542});543def(AST_True, function(){ return true });544def(AST_False, function(){ return true });545})(function(node, func){546node.DEFMETHOD("is_boolean", func);547});548549// methods to determine if an expression has a string result type550(function (def){551def(AST_Node, function(){ return false });552def(AST_String, function(){ return true });553def(AST_UnaryPrefix, function(){554return this.operator == "typeof";555});556def(AST_Binary, function(compressor){557return this.operator == "+" &&558(this.left.is_string(compressor) || this.right.is_string(compressor));559});560def(AST_Assign, function(compressor){561return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor);562});563def(AST_Seq, function(compressor){564return this.cdr.is_string(compressor);565});566def(AST_Conditional, function(compressor){567return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);568});569def(AST_Call, function(compressor){570return compressor.option("unsafe")571&& this.expression instanceof AST_SymbolRef572&& this.expression.name == "String"573&& this.expression.undeclared();574});575})(function(node, func){576node.DEFMETHOD("is_string", func);577});578579function best_of(ast1, ast2) {580return ast1.print_to_string().length >581ast2.print_to_string().length582? ast2 : ast1;583};584585// methods to evaluate a constant expression586(function (def){587// The evaluate method returns an array with one or two588// elements. If the node has been successfully reduced to a589// constant, then the second element tells us the value;590// otherwise the second element is missing. The first element591// of the array is always an AST_Node descendant; when592// evaluation was successful it's a node that represents the593// constant; otherwise it's the original node.594AST_Node.DEFMETHOD("evaluate", function(compressor){595if (!compressor.option("evaluate")) return [ this ];596try {597var val = this._eval(), ast = make_node_from_constant(compressor, val, this);598return [ best_of(ast, this), val ];599} catch(ex) {600if (ex !== def) throw ex;601return [ this ];602}603});604def(AST_Statement, function(){605throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));606});607def(AST_Function, function(){608// XXX: AST_Function inherits from AST_Scope, which itself609// inherits from AST_Statement; however, an AST_Function610// isn't really a statement. This could byte in other611// places too. :-( Wish JS had multiple inheritance.612return [ this ];613});614function ev(node) {615return node._eval();616};617def(AST_Node, function(){618throw def; // not constant619});620def(AST_Constant, function(){621return this.getValue();622});623def(AST_UnaryPrefix, function(){624var e = this.expression;625switch (this.operator) {626case "!": return !ev(e);627case "typeof": return typeof ev(e);628case "void": return void ev(e);629case "~": return ~ev(e);630case "-":631e = ev(e);632if (e === 0) throw def;633return -e;634case "+": return +ev(e);635}636throw def;637});638def(AST_Binary, function(){639var left = this.left, right = this.right;640switch (this.operator) {641case "&&" : return ev(left) && ev(right);642case "||" : return ev(left) || ev(right);643case "|" : return ev(left) | ev(right);644case "&" : return ev(left) & ev(right);645case "^" : return ev(left) ^ ev(right);646case "+" : return ev(left) + ev(right);647case "*" : return ev(left) * ev(right);648case "/" : return ev(left) / ev(right);649case "%" : return ev(left) % ev(right);650case "-" : return ev(left) - ev(right);651case "<<" : return ev(left) << ev(right);652case ">>" : return ev(left) >> ev(right);653case ">>>" : return ev(left) >>> ev(right);654case "==" : return ev(left) == ev(right);655case "===" : return ev(left) === ev(right);656case "!=" : return ev(left) != ev(right);657case "!==" : return ev(left) !== ev(right);658case "<" : return ev(left) < ev(right);659case "<=" : return ev(left) <= ev(right);660case ">" : return ev(left) > ev(right);661case ">=" : return ev(left) >= ev(right);662case "in" : return ev(left) in ev(right);663case "instanceof" : return ev(left) instanceof ev(right);664}665throw def;666});667def(AST_Conditional, function(){668return ev(this.condition)669? ev(this.consequent)670: ev(this.alternative);671});672def(AST_SymbolRef, function(){673var d = this.definition();674if (d && d.constant && d.init) return ev(d.init);675throw def;676});677})(function(node, func){678node.DEFMETHOD("_eval", func);679});680681// method to negate an expression682(function(def){683function basic_negation(exp) {684return make_node(AST_UnaryPrefix, exp, {685operator: "!",686expression: exp687});688};689def(AST_Node, function(){690return basic_negation(this);691});692def(AST_Statement, function(){693throw new Error("Cannot negate a statement");694});695def(AST_Function, function(){696return basic_negation(this);697});698def(AST_UnaryPrefix, function(){699if (this.operator == "!")700return this.expression;701return basic_negation(this);702});703def(AST_Seq, function(compressor){704var self = this.clone();705self.cdr = self.cdr.negate(compressor);706return self;707});708def(AST_Conditional, function(compressor){709var self = this.clone();710self.consequent = self.consequent.negate(compressor);711self.alternative = self.alternative.negate(compressor);712return best_of(basic_negation(this), self);713});714def(AST_Binary, function(compressor){715var self = this.clone(), op = this.operator;716if (compressor.option("unsafe_comps")) {717switch (op) {718case "<=" : self.operator = ">" ; return self;719case "<" : self.operator = ">=" ; return self;720case ">=" : self.operator = "<" ; return self;721case ">" : self.operator = "<=" ; return self;722}723}724switch (op) {725case "==" : self.operator = "!="; return self;726case "!=" : self.operator = "=="; return self;727case "===": self.operator = "!=="; return self;728case "!==": self.operator = "==="; return self;729case "&&":730self.operator = "||";731self.left = self.left.negate(compressor);732self.right = self.right.negate(compressor);733return best_of(basic_negation(this), self);734case "||":735self.operator = "&&";736self.left = self.left.negate(compressor);737self.right = self.right.negate(compressor);738return best_of(basic_negation(this), self);739}740return basic_negation(this);741});742})(function(node, func){743node.DEFMETHOD("negate", function(compressor){744return func.call(this, compressor);745});746});747748// determine if expression has side effects749(function(def){750def(AST_Node, function(){ return true });751752def(AST_EmptyStatement, function(){ return false });753def(AST_Constant, function(){ return false });754def(AST_This, function(){ return false });755756def(AST_Block, function(){757for (var i = this.body.length; --i >= 0;) {758if (this.body[i].has_side_effects())759return true;760}761return false;762});763764def(AST_SimpleStatement, function(){765return this.body.has_side_effects();766});767def(AST_Defun, function(){ return true });768def(AST_Function, function(){ return false });769def(AST_Binary, function(){770return this.left.has_side_effects()771|| this.right.has_side_effects();772});773def(AST_Assign, function(){ return true });774def(AST_Conditional, function(){775return this.condition.has_side_effects()776|| this.consequent.has_side_effects()777|| this.alternative.has_side_effects();778});779def(AST_Unary, function(){780return this.operator == "delete"781|| this.operator == "++"782|| this.operator == "--"783|| this.expression.has_side_effects();784});785def(AST_SymbolRef, function(){ return false });786def(AST_Object, function(){787for (var i = this.properties.length; --i >= 0;)788if (this.properties[i].has_side_effects())789return true;790return false;791});792def(AST_ObjectProperty, function(){793return this.value.has_side_effects();794});795def(AST_Array, function(){796for (var i = this.elements.length; --i >= 0;)797if (this.elements[i].has_side_effects())798return true;799return false;800});801// def(AST_Dot, function(){802// return this.expression.has_side_effects();803// });804// def(AST_Sub, function(){805// return this.expression.has_side_effects()806// || this.property.has_side_effects();807// });808def(AST_PropAccess, function(){809return true;810});811def(AST_Seq, function(){812return this.car.has_side_effects()813|| this.cdr.has_side_effects();814});815})(function(node, func){816node.DEFMETHOD("has_side_effects", func);817});818819// tell me if a statement aborts820function aborts(thing) {821return thing && thing.aborts();822};823(function(def){824def(AST_Statement, function(){ return null });825def(AST_Jump, function(){ return this });826function block_aborts(){827var n = this.body.length;828return n > 0 && aborts(this.body[n - 1]);829};830def(AST_BlockStatement, block_aborts);831def(AST_SwitchBranch, block_aborts);832def(AST_If, function(){833return this.alternative && aborts(this.body) && aborts(this.alternative);834});835})(function(node, func){836node.DEFMETHOD("aborts", func);837});838839/* -----[ optimizers ]----- */840841OPT(AST_Directive, function(self, compressor){842if (self.scope.has_directive(self.value) !== self.scope) {843return make_node(AST_EmptyStatement, self);844}845return self;846});847848OPT(AST_Debugger, function(self, compressor){849if (compressor.option("drop_debugger"))850return make_node(AST_EmptyStatement, self);851return self;852});853854OPT(AST_LabeledStatement, function(self, compressor){855if (self.body instanceof AST_Break856&& compressor.loopcontrol_target(self.body.label) === self.body) {857return make_node(AST_EmptyStatement, self);858}859return self.label.references.length == 0 ? self.body : self;860});861862OPT(AST_Block, function(self, compressor){863self.body = tighten_body(self.body, compressor);864return self;865});866867OPT(AST_BlockStatement, function(self, compressor){868self.body = tighten_body(self.body, compressor);869switch (self.body.length) {870case 1: return self.body[0];871case 0: return make_node(AST_EmptyStatement, self);872}873return self;874});875876AST_Scope.DEFMETHOD("drop_unused", function(compressor){877var self = this;878if (compressor.option("unused")879&& !(self instanceof AST_Toplevel)880&& !self.uses_eval881) {882var in_use = [];883var initializations = new Dictionary();884// pass 1: find out which symbols are directly used in885// this scope (not in nested scopes).886var scope = this;887var tw = new TreeWalker(function(node, descend){888if (node !== self) {889if (node instanceof AST_Defun) {890initializations.add(node.name.name, node);891return true; // don't go in nested scopes892}893if (node instanceof AST_Definitions && scope === self) {894node.definitions.forEach(function(def){895if (def.value) {896initializations.add(def.name.name, def.value);897if (def.value.has_side_effects()) {898def.value.walk(tw);899}900}901});902return true;903}904if (node instanceof AST_SymbolRef) {905push_uniq(in_use, node.definition());906return true;907}908if (node instanceof AST_Scope) {909var save_scope = scope;910scope = node;911descend();912scope = save_scope;913return true;914}915}916});917self.walk(tw);918// pass 2: for every used symbol we need to walk its919// initialization code to figure out if it uses other920// symbols (that may not be in_use).921for (var i = 0; i < in_use.length; ++i) {922in_use[i].orig.forEach(function(decl){923// undeclared globals will be instanceof AST_SymbolRef924var init = initializations.get(decl.name);925if (init) init.forEach(function(init){926var tw = new TreeWalker(function(node){927if (node instanceof AST_SymbolRef) {928push_uniq(in_use, node.definition());929}930});931init.walk(tw);932});933});934}935// pass 3: we should drop declarations not in_use936var tt = new TreeTransformer(937function before(node, descend, in_list) {938if (node instanceof AST_Lambda) {939for (var a = node.argnames, i = a.length; --i >= 0;) {940var sym = a[i];941if (sym.unreferenced()) {942a.pop();943compressor.warn("Dropping unused function argument {name} [{file}:{line},{col}]", {944name : sym.name,945file : sym.start.file,946line : sym.start.line,947col : sym.start.col948});949}950else break;951}952}953if (node instanceof AST_Defun && node !== self) {954if (!member(node.name.definition(), in_use)) {955compressor.warn("Dropping unused function {name} [{file}:{line},{col}]", {956name : node.name.name,957file : node.name.start.file,958line : node.name.start.line,959col : node.name.start.col960});961return make_node(AST_EmptyStatement, node);962}963return node;964}965if (node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn)) {966var def = node.definitions.filter(function(def){967if (member(def.name.definition(), in_use)) return true;968var w = {969name : def.name.name,970file : def.name.start.file,971line : def.name.start.line,972col : def.name.start.col973};974if (def.value && def.value.has_side_effects()) {975def._unused_side_effects = true;976compressor.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", w);977return true;978}979compressor.warn("Dropping unused variable {name} [{file}:{line},{col}]", w);980return false;981});982// place uninitialized names at the start983def = mergeSort(def, function(a, b){984if (!a.value && b.value) return -1;985if (!b.value && a.value) return 1;986return 0;987});988// for unused names whose initialization has989// side effects, we can cascade the init. code990// into the next one, or next statement.991var side_effects = [];992for (var i = 0; i < def.length;) {993var x = def[i];994if (x._unused_side_effects) {995side_effects.push(x.value);996def.splice(i, 1);997} else {998if (side_effects.length > 0) {999side_effects.push(x.value);1000x.value = AST_Seq.from_array(side_effects);1001side_effects = [];1002}1003++i;1004}1005}1006if (side_effects.length > 0) {1007side_effects = make_node(AST_BlockStatement, node, {1008body: [ make_node(AST_SimpleStatement, node, {1009body: AST_Seq.from_array(side_effects)1010}) ]1011});1012} else {1013side_effects = null;1014}1015if (def.length == 0 && !side_effects) {1016return make_node(AST_EmptyStatement, node);1017}1018if (def.length == 0) {1019return side_effects;1020}1021node.definitions = def;1022if (side_effects) {1023side_effects.body.unshift(node);1024node = side_effects;1025}1026return node;1027}1028if (node instanceof AST_For && node.init instanceof AST_BlockStatement) {1029descend(node, this);1030// certain combination of unused name + side effect leads to:1031// https://github.com/mishoo/UglifyJS2/issues/441032// that's an invalid AST.1033// We fix it at this stage by moving the `var` outside the `for`.1034var body = node.init.body.slice(0, -1);1035node.init = node.init.body.slice(-1)[0].body;1036body.push(node);1037return in_list ? MAP.splice(body) : make_node(AST_BlockStatement, node, {1038body: body1039});1040}1041if (node instanceof AST_Scope && node !== self)1042return node;1043}1044);1045self.transform(tt);1046}1047});10481049AST_Scope.DEFMETHOD("hoist_declarations", function(compressor){1050var hoist_funs = compressor.option("hoist_funs");1051var hoist_vars = compressor.option("hoist_vars");1052var self = this;1053if (hoist_funs || hoist_vars) {1054var dirs = [];1055var hoisted = [];1056var vars = new Dictionary(), vars_found = 0, var_decl = 0;1057// let's count var_decl first, we seem to waste a lot of1058// space if we hoist `var` when there's only one.1059self.walk(new TreeWalker(function(node){1060if (node instanceof AST_Scope && node !== self)1061return true;1062if (node instanceof AST_Var) {1063++var_decl;1064return true;1065}1066}));1067hoist_vars = hoist_vars && var_decl > 1;1068var tt = new TreeTransformer(1069function before(node) {1070if (node !== self) {1071if (node instanceof AST_Directive) {1072dirs.push(node);1073return make_node(AST_EmptyStatement, node);1074}1075if (node instanceof AST_Defun && hoist_funs) {1076hoisted.push(node);1077return make_node(AST_EmptyStatement, node);1078}1079if (node instanceof AST_Var && hoist_vars) {1080node.definitions.forEach(function(def){1081vars.set(def.name.name, def);1082++vars_found;1083});1084var seq = node.to_assignments();1085var p = tt.parent();1086if (p instanceof AST_ForIn && p.init === node) {1087if (seq == null) return node.definitions[0].name;1088return seq;1089}1090if (p instanceof AST_For && p.init === node) {1091return seq;1092}1093if (!seq) return make_node(AST_EmptyStatement, node);1094return make_node(AST_SimpleStatement, node, {1095body: seq1096});1097}1098if (node instanceof AST_Scope)1099return node; // to avoid descending in nested scopes1100}1101}1102);1103self = self.transform(tt);1104if (vars_found > 0) {1105// collect only vars which don't show up in self's arguments list1106var defs = [];1107vars.each(function(def, name){1108if (self instanceof AST_Lambda1109&& find_if(function(x){ return x.name == def.name.name },1110self.argnames)) {1111vars.del(name);1112} else {1113def = def.clone();1114def.value = null;1115defs.push(def);1116vars.set(name, def);1117}1118});1119if (defs.length > 0) {1120// try to merge in assignments1121for (var i = 0; i < self.body.length;) {1122if (self.body[i] instanceof AST_SimpleStatement) {1123var expr = self.body[i].body, sym, assign;1124if (expr instanceof AST_Assign1125&& expr.operator == "="1126&& (sym = expr.left) instanceof AST_Symbol1127&& vars.has(sym.name))1128{1129var def = vars.get(sym.name);1130if (def.value) break;1131def.value = expr.right;1132remove(defs, def);1133defs.push(def);1134self.body.splice(i, 1);1135continue;1136}1137if (expr instanceof AST_Seq1138&& (assign = expr.car) instanceof AST_Assign1139&& assign.operator == "="1140&& (sym = assign.left) instanceof AST_Symbol1141&& vars.has(sym.name))1142{1143var def = vars.get(sym.name);1144if (def.value) break;1145def.value = assign.right;1146remove(defs, def);1147defs.push(def);1148self.body[i].body = expr.cdr;1149continue;1150}1151}1152if (self.body[i] instanceof AST_EmptyStatement) {1153self.body.splice(i, 1);1154continue;1155}1156if (self.body[i] instanceof AST_BlockStatement) {1157var tmp = [ i, 1 ].concat(self.body[i].body);1158self.body.splice.apply(self.body, tmp);1159continue;1160}1161break;1162}1163defs = make_node(AST_Var, self, {1164definitions: defs1165});1166hoisted.push(defs);1167};1168}1169self.body = dirs.concat(hoisted, self.body);1170}1171return self;1172});11731174OPT(AST_SimpleStatement, function(self, compressor){1175if (compressor.option("side_effects")) {1176if (!self.body.has_side_effects()) {1177compressor.warn("Dropping side-effect-free statement [{file}:{line},{col}]", self.start);1178return make_node(AST_EmptyStatement, self);1179}1180}1181return self;1182});11831184OPT(AST_DWLoop, function(self, compressor){1185var cond = self.condition.evaluate(compressor);1186self.condition = cond[0];1187if (!compressor.option("loops")) return self;1188if (cond.length > 1) {1189if (cond[1]) {1190return make_node(AST_For, self, {1191body: self.body1192});1193} else if (self instanceof AST_While) {1194if (compressor.option("dead_code")) {1195var a = [];1196extract_declarations_from_unreachable_code(compressor, self.body, a);1197return make_node(AST_BlockStatement, self, { body: a });1198}1199} else {1200return self.body;1201}1202}1203return self;1204});12051206function if_break_in_loop(self, compressor) {1207function drop_it(rest) {1208rest = as_statement_array(rest);1209if (self.body instanceof AST_BlockStatement) {1210self.body = self.body.clone();1211self.body.body = rest.concat(self.body.body.slice(1));1212self.body = self.body.transform(compressor);1213} else {1214self.body = make_node(AST_BlockStatement, self.body, {1215body: rest1216}).transform(compressor);1217}1218if_break_in_loop(self, compressor);1219}1220var first = self.body instanceof AST_BlockStatement ? self.body.body[0] : self.body;1221if (first instanceof AST_If) {1222if (first.body instanceof AST_Break1223&& compressor.loopcontrol_target(first.body.label) === self) {1224if (self.condition) {1225self.condition = make_node(AST_Binary, self.condition, {1226left: self.condition,1227operator: "&&",1228right: first.condition.negate(compressor),1229});1230} else {1231self.condition = first.condition.negate(compressor);1232}1233drop_it(first.alternative);1234}1235else if (first.alternative instanceof AST_Break1236&& compressor.loopcontrol_target(first.alternative.label) === self) {1237if (self.condition) {1238self.condition = make_node(AST_Binary, self.condition, {1239left: self.condition,1240operator: "&&",1241right: first.condition,1242});1243} else {1244self.condition = first.condition;1245}1246drop_it(first.body);1247}1248}1249};12501251OPT(AST_While, function(self, compressor) {1252if (!compressor.option("loops")) return self;1253self = AST_DWLoop.prototype.optimize.call(self, compressor);1254if (self instanceof AST_While) {1255if_break_in_loop(self, compressor);1256self = make_node(AST_For, self, self).transform(compressor);1257}1258return self;1259});12601261OPT(AST_For, function(self, compressor){1262var cond = self.condition;1263if (cond) {1264cond = cond.evaluate(compressor);1265self.condition = cond[0];1266}1267if (!compressor.option("loops")) return self;1268if (cond) {1269if (cond.length > 1 && !cond[1]) {1270if (compressor.option("dead_code")) {1271var a = [];1272if (self.init instanceof AST_Statement) {1273a.push(self.init);1274}1275else if (self.init) {1276a.push(make_node(AST_SimpleStatement, self.init, {1277body: self.init1278}));1279}1280extract_declarations_from_unreachable_code(compressor, self.body, a);1281return make_node(AST_BlockStatement, self, { body: a });1282}1283}1284}1285if_break_in_loop(self, compressor);1286return self;1287});12881289OPT(AST_If, function(self, compressor){1290if (!compressor.option("conditionals")) return self;1291// if condition can be statically determined, warn and drop1292// one of the blocks. note, statically determined implies1293// “has no side effects”; also it doesn't work for cases like1294// `x && true`, though it probably should.1295var cond = self.condition.evaluate(compressor);1296self.condition = cond[0];1297if (cond.length > 1) {1298if (cond[1]) {1299compressor.warn("Condition always true [{file}:{line},{col}]", self.condition.start);1300if (compressor.option("dead_code")) {1301var a = [];1302if (self.alternative) {1303extract_declarations_from_unreachable_code(compressor, self.alternative, a);1304}1305a.push(self.body);1306return make_node(AST_BlockStatement, self, { body: a }).transform(compressor);1307}1308} else {1309compressor.warn("Condition always false [{file}:{line},{col}]", self.condition.start);1310if (compressor.option("dead_code")) {1311var a = [];1312extract_declarations_from_unreachable_code(compressor, self.body, a);1313if (self.alternative) a.push(self.alternative);1314return make_node(AST_BlockStatement, self, { body: a }).transform(compressor);1315}1316}1317}1318if (is_empty(self.alternative)) self.alternative = null;1319var negated = self.condition.negate(compressor);1320var negated_is_best = best_of(self.condition, negated) === negated;1321if (self.alternative && negated_is_best) {1322negated_is_best = false; // because we already do the switch here.1323self.condition = negated;1324var tmp = self.body;1325self.body = self.alternative || make_node(AST_EmptyStatement);1326self.alternative = tmp;1327}1328if (is_empty(self.body) && is_empty(self.alternative)) {1329return make_node(AST_SimpleStatement, self.condition, {1330body: self.condition1331}).transform(compressor);1332}1333if (self.body instanceof AST_SimpleStatement1334&& self.alternative instanceof AST_SimpleStatement) {1335return make_node(AST_SimpleStatement, self, {1336body: make_node(AST_Conditional, self, {1337condition : self.condition,1338consequent : self.body.body,1339alternative : self.alternative.body1340})1341}).transform(compressor);1342}1343if (is_empty(self.alternative) && self.body instanceof AST_SimpleStatement) {1344if (negated_is_best) return make_node(AST_SimpleStatement, self, {1345body: make_node(AST_Binary, self, {1346operator : "||",1347left : negated,1348right : self.body.body1349})1350}).transform(compressor);1351return make_node(AST_SimpleStatement, self, {1352body: make_node(AST_Binary, self, {1353operator : "&&",1354left : self.condition,1355right : self.body.body1356})1357}).transform(compressor);1358}1359if (self.body instanceof AST_EmptyStatement1360&& self.alternative1361&& self.alternative instanceof AST_SimpleStatement) {1362return make_node(AST_SimpleStatement, self, {1363body: make_node(AST_Binary, self, {1364operator : "||",1365left : self.condition,1366right : self.alternative.body1367})1368}).transform(compressor);1369}1370if (self.body instanceof AST_Exit1371&& self.alternative instanceof AST_Exit1372&& self.body.TYPE == self.alternative.TYPE) {1373return make_node(self.body.CTOR, self, {1374value: make_node(AST_Conditional, self, {1375condition : self.condition,1376consequent : self.body.value || make_node(AST_Undefined, self.body).optimize(compressor),1377alternative : self.alternative.value || make_node(AST_Undefined, self.alternative).optimize(compressor)1378})1379}).transform(compressor);1380}1381if (self.body instanceof AST_If1382&& !self.body.alternative1383&& !self.alternative) {1384self.condition = make_node(AST_Binary, self.condition, {1385operator: "&&",1386left: self.condition,1387right: self.body.condition1388}).transform(compressor);1389self.body = self.body.body;1390}1391if (aborts(self.body)) {1392if (self.alternative) {1393var alt = self.alternative;1394self.alternative = null;1395return make_node(AST_BlockStatement, self, {1396body: [ self, alt ]1397}).transform(compressor);1398}1399}1400if (aborts(self.alternative)) {1401var body = self.body;1402self.body = self.alternative;1403self.condition = negated_is_best ? negated : self.condition.negate(compressor);1404self.alternative = null;1405return make_node(AST_BlockStatement, self, {1406body: [ self, body ]1407}).transform(compressor);1408}1409return self;1410});14111412OPT(AST_Switch, function(self, compressor){1413if (self.body.length == 0 && compressor.option("conditionals")) {1414return make_node(AST_SimpleStatement, self, {1415body: self.expression1416}).transform(compressor);1417}1418var last_branch = self.body[self.body.length - 1];1419if (last_branch) {1420var stat = last_branch.body[last_branch.body.length - 1]; // last statement1421if (stat instanceof AST_Break && loop_body(compressor.loopcontrol_target(stat.label)) === self)1422last_branch.body.pop();1423}1424var exp = self.expression.evaluate(compressor);1425out: if (exp.length == 2) try {1426// constant expression1427self.expression = exp[0];1428if (!compressor.option("dead_code")) break out;1429var value = exp[1];1430var in_if = false;1431var in_block = false;1432var started = false;1433var stopped = false;1434var ruined = false;1435var tt = new TreeTransformer(function(node, descend, in_list){1436if (node instanceof AST_Lambda || node instanceof AST_SimpleStatement) {1437// no need to descend these node types1438return node;1439}1440else if (node instanceof AST_Switch && node === self) {1441node = node.clone();1442descend(node, this);1443return ruined ? node : make_node(AST_BlockStatement, node, {1444body: node.body.reduce(function(a, branch){1445return a.concat(branch.body);1446}, [])1447}).transform(compressor);1448}1449else if (node instanceof AST_If || node instanceof AST_Try) {1450var save = in_if;1451in_if = !in_block;1452descend(node, this);1453in_if = save;1454return node;1455}1456else if (node instanceof AST_StatementWithBody || node instanceof AST_Switch) {1457var save = in_block;1458in_block = true;1459descend(node, this);1460in_block = save;1461return node;1462}1463else if (node instanceof AST_Break && this.loopcontrol_target(node.label) === self) {1464if (in_if) {1465ruined = true;1466return node;1467}1468if (in_block) return node;1469stopped = true;1470return in_list ? MAP.skip : make_node(AST_EmptyStatement, node);1471}1472else if (node instanceof AST_SwitchBranch && this.parent() === self) {1473if (stopped) return MAP.skip;1474if (node instanceof AST_Case) {1475var exp = node.expression.evaluate(compressor);1476if (exp.length < 2) {1477// got a case with non-constant expression, baling out1478throw self;1479}1480if (exp[1] === value || started) {1481started = true;1482if (aborts(node)) stopped = true;1483descend(node, this);1484return node;1485}1486return MAP.skip;1487}1488descend(node, this);1489return node;1490}1491});1492tt.stack = compressor.stack.slice(); // so that's able to see parent nodes1493self = self.transform(tt);1494} catch(ex) {1495if (ex !== self) throw ex;1496}1497return self;1498});14991500OPT(AST_Case, function(self, compressor){1501self.body = tighten_body(self.body, compressor);1502return self;1503});15041505OPT(AST_Try, function(self, compressor){1506self.body = tighten_body(self.body, compressor);1507return self;1508});15091510AST_Definitions.DEFMETHOD("remove_initializers", function(){1511this.definitions.forEach(function(def){ def.value = null });1512});15131514AST_Definitions.DEFMETHOD("to_assignments", function(){1515var assignments = this.definitions.reduce(function(a, def){1516if (def.value) {1517var name = make_node(AST_SymbolRef, def.name, def.name);1518a.push(make_node(AST_Assign, def, {1519operator : "=",1520left : name,1521right : def.value1522}));1523}1524return a;1525}, []);1526if (assignments.length == 0) return null;1527return AST_Seq.from_array(assignments);1528});15291530OPT(AST_Definitions, function(self, compressor){1531if (self.definitions.length == 0)1532return make_node(AST_EmptyStatement, self);1533return self;1534});15351536OPT(AST_Function, function(self, compressor){1537self = AST_Lambda.prototype.optimize.call(self, compressor);1538if (compressor.option("unused")) {1539if (self.name && self.name.unreferenced()) {1540self.name = null;1541}1542}1543return self;1544});15451546OPT(AST_Call, function(self, compressor){1547if (compressor.option("unsafe")) {1548var exp = self.expression;1549if (exp instanceof AST_SymbolRef && exp.undeclared()) {1550switch (exp.name) {1551case "Array":1552if (self.args.length != 1) {1553return make_node(AST_Array, self, {1554elements: self.args1555});1556}1557break;1558case "Object":1559if (self.args.length == 0) {1560return make_node(AST_Object, self, {1561properties: []1562});1563}1564break;1565case "String":1566if (self.args.length == 0) return make_node(AST_String, self, {1567value: ""1568});1569return make_node(AST_Binary, self, {1570left: self.args[0],1571operator: "+",1572right: make_node(AST_String, self, { value: "" })1573});1574}1575}1576else if (exp instanceof AST_Dot && exp.property == "toString" && self.args.length == 0) {1577return make_node(AST_Binary, self, {1578left: make_node(AST_String, self, { value: "" }),1579operator: "+",1580right: exp.expression1581}).transform(compressor);1582}1583}1584if (compressor.option("side_effects")) {1585if (self.expression instanceof AST_Function1586&& self.args.length == 01587&& !AST_Block.prototype.has_side_effects.call(self.expression)) {1588return make_node(AST_Undefined, self).transform(compressor);1589}1590}1591return self;1592});15931594OPT(AST_New, function(self, compressor){1595if (compressor.option("unsafe")) {1596var exp = self.expression;1597if (exp instanceof AST_SymbolRef && exp.undeclared()) {1598switch (exp.name) {1599case "Object":1600case "RegExp":1601case "Function":1602case "Error":1603case "Array":1604return make_node(AST_Call, self, self).transform(compressor);1605}1606}1607}1608return self;1609});16101611OPT(AST_Seq, function(self, compressor){1612if (!compressor.option("side_effects"))1613return self;1614if (!self.car.has_side_effects()) {1615// we shouldn't compress (1,eval)(something) to1616// eval(something) because that changes the meaning of1617// eval (becomes lexical instead of global).1618var p;1619if (!(self.cdr instanceof AST_SymbolRef1620&& self.cdr.name == "eval"1621&& self.cdr.undeclared()1622&& (p = compressor.parent()) instanceof AST_Call1623&& p.expression === self)) {1624return self.cdr;1625}1626}1627if (compressor.option("cascade")) {1628if (self.car instanceof AST_Assign1629&& !self.car.left.has_side_effects()1630&& self.car.left.equivalent_to(self.cdr)) {1631return self.car;1632}1633if (!self.car.has_side_effects()1634&& !self.cdr.has_side_effects()1635&& self.car.equivalent_to(self.cdr)) {1636return self.car;1637}1638}1639return self;1640});16411642AST_Unary.DEFMETHOD("lift_sequences", function(compressor){1643if (compressor.option("sequences")) {1644if (this.expression instanceof AST_Seq) {1645var seq = this.expression;1646var x = seq.to_array();1647this.expression = x.pop();1648x.push(this);1649seq = AST_Seq.from_array(x).transform(compressor);1650return seq;1651}1652}1653return this;1654});16551656OPT(AST_UnaryPostfix, function(self, compressor){1657return self.lift_sequences(compressor);1658});16591660OPT(AST_UnaryPrefix, function(self, compressor){1661self = self.lift_sequences(compressor);1662var e = self.expression;1663if (compressor.option("booleans") && compressor.in_boolean_context()) {1664switch (self.operator) {1665case "!":1666if (e instanceof AST_UnaryPrefix && e.operator == "!") {1667// !!foo ==> foo, if we're in boolean context1668return e.expression;1669}1670break;1671case "typeof":1672// typeof always returns a non-empty string, thus it's1673// always true in booleans1674compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);1675return make_node(AST_True, self);1676}1677if (e instanceof AST_Binary && self.operator == "!") {1678self = best_of(self, e.negate(compressor));1679}1680}1681return self.evaluate(compressor)[0];1682});16831684AST_Binary.DEFMETHOD("lift_sequences", function(compressor){1685if (compressor.option("sequences")) {1686if (this.left instanceof AST_Seq) {1687var seq = this.left;1688var x = seq.to_array();1689this.left = x.pop();1690x.push(this);1691seq = AST_Seq.from_array(x).transform(compressor);1692return seq;1693}1694if (this.right instanceof AST_Seq1695&& !(this.operator == "||" || this.operator == "&&")1696&& !this.left.has_side_effects()) {1697var seq = this.right;1698var x = seq.to_array();1699this.right = x.pop();1700x.push(this);1701seq = AST_Seq.from_array(x).transform(compressor);1702return seq;1703}1704}1705return this;1706});17071708var commutativeOperators = makePredicate("== === != !== * & | ^");17091710OPT(AST_Binary, function(self, compressor){1711function reverse(op) {1712if (!(self.left.has_side_effects() && self.right.has_side_effects())) {1713if (op) self.operator = op;1714var tmp = self.left;1715self.left = self.right;1716self.right = tmp;1717}1718};1719if (commutativeOperators(self.operator)) {1720if (self.right instanceof AST_Constant1721&& !(self.left instanceof AST_Constant)) {1722reverse();1723}1724}1725self = self.lift_sequences(compressor);1726if (compressor.option("comparisons")) switch (self.operator) {1727case "===":1728case "!==":1729if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||1730(self.left.is_boolean() && self.right.is_boolean())) {1731self.operator = self.operator.substr(0, 2);1732}1733// XXX: intentionally falling down to the next case1734case "==":1735case "!=":1736if (self.left instanceof AST_String1737&& self.left.value == "undefined"1738&& self.right instanceof AST_UnaryPrefix1739&& self.right.operator == "typeof"1740&& compressor.option("unsafe")) {1741if (!(self.right.expression instanceof AST_SymbolRef)1742|| !self.right.expression.undeclared()) {1743self.left = self.right.expression;1744self.right = make_node(AST_Undefined, self.left).optimize(compressor);1745if (self.operator.length == 2) self.operator += "=";1746}1747}1748break;1749}1750if (compressor.option("booleans") && compressor.in_boolean_context()) switch (self.operator) {1751case "&&":1752var ll = self.left.evaluate(compressor);1753var rr = self.right.evaluate(compressor);1754if ((ll.length > 1 && !ll[1]) || (rr.length > 1 && !rr[1])) {1755compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);1756return make_node(AST_False, self);1757}1758if (ll.length > 1 && ll[1]) {1759return rr[0];1760}1761if (rr.length > 1 && rr[1]) {1762return ll[0];1763}1764break;1765case "||":1766var ll = self.left.evaluate(compressor);1767var rr = self.right.evaluate(compressor);1768if ((ll.length > 1 && ll[1]) || (rr.length > 1 && rr[1])) {1769compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);1770return make_node(AST_True, self);1771}1772if (ll.length > 1 && !ll[1]) {1773return rr[0];1774}1775if (rr.length > 1 && !rr[1]) {1776return ll[0];1777}1778break;1779case "+":1780var ll = self.left.evaluate(compressor);1781var rr = self.right.evaluate(compressor);1782if ((ll.length > 1 && ll[0] instanceof AST_String && ll[1]) ||1783(rr.length > 1 && rr[0] instanceof AST_String && rr[1])) {1784compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);1785return make_node(AST_True, self);1786}1787break;1788}1789var exp = self.evaluate(compressor);1790if (exp.length > 1) {1791if (best_of(exp[0], self) !== self)1792return exp[0];1793}1794if (compressor.option("comparisons")) {1795if (!(compressor.parent() instanceof AST_Binary)1796|| compressor.parent() instanceof AST_Assign) {1797var negated = make_node(AST_UnaryPrefix, self, {1798operator: "!",1799expression: self.negate(compressor)1800});1801self = best_of(self, negated);1802}1803switch (self.operator) {1804case "<": reverse(">"); break;1805case "<=": reverse(">="); break;1806}1807}1808if (self.operator == "+" && self.right instanceof AST_String1809&& self.right.getValue() === "" && self.left instanceof AST_Binary1810&& self.left.operator == "+" && self.left.is_string(compressor)) {1811return self.left;1812}1813return self;1814});18151816OPT(AST_SymbolRef, function(self, compressor){1817if (self.undeclared()) {1818var defines = compressor.option("global_defs");1819if (defines && defines.hasOwnProperty(self.name)) {1820return make_node_from_constant(compressor, defines[self.name], self);1821}1822switch (self.name) {1823case "undefined":1824return make_node(AST_Undefined, self);1825case "NaN":1826return make_node(AST_NaN, self);1827case "Infinity":1828return make_node(AST_Infinity, self);1829}1830}1831return self;1832});18331834OPT(AST_Undefined, function(self, compressor){1835if (compressor.option("unsafe")) {1836var scope = compressor.find_parent(AST_Scope);1837var undef = scope.find_variable("undefined");1838if (undef) {1839var ref = make_node(AST_SymbolRef, self, {1840name : "undefined",1841scope : scope,1842thedef : undef1843});1844ref.reference();1845return ref;1846}1847}1848return self;1849});18501851var ASSIGN_OPS = [ '+', '-', '/', '*', '%', '>>', '<<', '>>>', '|', '^', '&' ];1852OPT(AST_Assign, function(self, compressor){1853self = self.lift_sequences(compressor);1854if (self.operator == "="1855&& self.left instanceof AST_SymbolRef1856&& self.right instanceof AST_Binary1857&& self.right.left instanceof AST_SymbolRef1858&& self.right.left.name == self.left.name1859&& member(self.right.operator, ASSIGN_OPS)) {1860self.operator = self.right.operator + "=";1861self.right = self.right.right;1862}1863return self;1864});18651866OPT(AST_Conditional, function(self, compressor){1867if (!compressor.option("conditionals")) return self;1868if (self.condition instanceof AST_Seq) {1869var car = self.condition.car;1870self.condition = self.condition.cdr;1871return AST_Seq.cons(car, self);1872}1873var cond = self.condition.evaluate(compressor);1874if (cond.length > 1) {1875if (cond[1]) {1876compressor.warn("Condition always true [{file}:{line},{col}]", self.start);1877return self.consequent;1878} else {1879compressor.warn("Condition always false [{file}:{line},{col}]", self.start);1880return self.alternative;1881}1882}1883var negated = cond[0].negate(compressor);1884if (best_of(cond[0], negated) === negated) {1885self = make_node(AST_Conditional, self, {1886condition: negated,1887consequent: self.alternative,1888alternative: self.consequent1889});1890}1891var consequent = self.consequent;1892var alternative = self.alternative;1893if (consequent instanceof AST_Assign1894&& alternative instanceof AST_Assign1895&& consequent.operator == alternative.operator1896&& consequent.left.equivalent_to(alternative.left)1897) {1898/*1899* Stuff like this:1900* if (foo) exp = something; else exp = something_else;1901* ==>1902* exp = foo ? something : something_else;1903*/1904self = make_node(AST_Assign, self, {1905operator: consequent.operator,1906left: consequent.left,1907right: make_node(AST_Conditional, self, {1908condition: self.condition,1909consequent: consequent.right,1910alternative: alternative.right1911})1912});1913}1914return self;1915});19161917OPT(AST_Boolean, function(self, compressor){1918if (compressor.option("booleans")) {1919var p = compressor.parent();1920if (p instanceof AST_Binary && (p.operator == "=="1921|| p.operator == "!=")) {1922compressor.warn("Non-strict equality against boolean: {operator} {value} [{file}:{line},{col}]", {1923operator : p.operator,1924value : self.value,1925file : p.start.file,1926line : p.start.line,1927col : p.start.col,1928});1929return make_node(AST_Number, self, {1930value: +self.value1931});1932}1933return make_node(AST_UnaryPrefix, self, {1934operator: "!",1935expression: make_node(AST_Number, self, {1936value: 1 - self.value1937})1938});1939}1940return self;1941});19421943OPT(AST_Sub, function(self, compressor){1944var prop = self.property;1945if (prop instanceof AST_String && compressor.option("properties")) {1946prop = prop.getValue();1947if (is_identifier(prop)) {1948return make_node(AST_Dot, self, {1949expression : self.expression,1950property : prop1951});1952}1953}1954return self;1955});19561957function literals_in_boolean_context(self, compressor) {1958if (compressor.option("booleans") && compressor.in_boolean_context()) {1959return make_node(AST_True, self);1960}1961return self;1962};1963OPT(AST_Array, literals_in_boolean_context);1964OPT(AST_Object, literals_in_boolean_context);1965OPT(AST_RegExp, literals_in_boolean_context);19661967})();196819691970