Merge pull request #1766 from alexlamsl/harmony-v2.8.21

Merging from master for 2.8.21
This commit is contained in:
Alex Lam S.L
2017-04-02 18:10:58 +08:00
committed by GitHub
25 changed files with 1388 additions and 680 deletions

View File

@@ -443,6 +443,9 @@ to set `true`; it's effectively a shortcut for `foo=true`).
integer argument larger than 1 to further reduce code size in some cases.
Note: raising the number of passes will increase uglify compress time.
- `keep_infinity` -- default `false`. Pass `true` to prevent `Infinity` from
being compressed into `1/0`, which may cause performance issues on Chrome.
### The `unsafe` option
It enables some transformations that *might* break code logic in certain

View File

@@ -48,43 +48,45 @@ function Compressor(options, false_by_default) {
return new Compressor(options, false_by_default);
TreeTransformer.call(this, this.before, this.after);
this.options = defaults(options, {
sequences : !false_by_default,
properties : !false_by_default,
angular : false,
booleans : !false_by_default,
cascade : !false_by_default,
collapse_vars : !false_by_default,
comparisons : !false_by_default,
conditionals : !false_by_default,
dead_code : !false_by_default,
drop_console : false,
drop_debugger : !false_by_default,
ecma : 5,
evaluate : !false_by_default,
expression : false,
global_defs : {},
hoist_funs : !false_by_default,
hoist_vars : false,
if_return : !false_by_default,
join_vars : !false_by_default,
keep_fargs : true,
keep_fnames : false,
keep_infinity : false,
loops : !false_by_default,
negate_iife : !false_by_default,
passes : 1,
properties : !false_by_default,
pure_getters : false,
pure_funcs : null,
reduce_vars : !false_by_default,
screw_ie8 : true,
sequences : !false_by_default,
side_effects : !false_by_default,
switches : !false_by_default,
top_retain : null,
toplevel : !!(options && options["top_retain"]),
unsafe : false,
unsafe_comps : false,
unsafe_math : false,
unsafe_proto : false,
conditionals : !false_by_default,
comparisons : !false_by_default,
evaluate : !false_by_default,
booleans : !false_by_default,
loops : !false_by_default,
unused : !false_by_default,
toplevel : !!(options && options["top_retain"]),
top_retain : null,
hoist_funs : !false_by_default,
keep_fargs : true,
keep_fnames : false,
hoist_vars : false,
if_return : !false_by_default,
join_vars : !false_by_default,
collapse_vars : !false_by_default,
reduce_vars : !false_by_default,
cascade : !false_by_default,
side_effects : !false_by_default,
pure_getters : false,
pure_funcs : null,
negate_iife : !false_by_default,
screw_ie8 : true,
ecma : 5,
drop_console : false,
angular : false,
expression : false,
warnings : true,
global_defs : {},
passes : 1,
}, true);
var pure_funcs = this.options["pure_funcs"];
if (typeof pure_funcs == "function") {
@@ -216,7 +218,12 @@ merge(Compressor.prototype, {
}) : make_node(AST_EmptyStatement, node);
}
return make_node(AST_SimpleStatement, node, {
body: node.value || make_node(AST_Undefined, node)
body: node.value || make_node(AST_UnaryPrefix, node, {
operator: "void",
expression: make_node(AST_Number, node, {
value: 0
})
})
});
}
if (node instanceof AST_Lambda && node !== self) {
@@ -407,6 +414,18 @@ merge(Compressor.prototype, {
}
});
function find_variable(compressor, name) {
var scope, i = 0;
while (scope = compressor.parent(i++)) {
if (scope instanceof AST_Scope) break;
if (scope instanceof AST_Catch) {
scope = scope.argname.definition().scope;
break;
}
}
return scope.find_variable(name);
}
function make_node(ctor, orig, props) {
if (!props) props = {};
if (orig) {
@@ -1071,7 +1090,7 @@ merge(Compressor.prototype, {
stat.value = cons_seq(stat.value);
}
else if (stat instanceof AST_Exit) {
stat.value = cons_seq(make_node(AST_Undefined, stat));
stat.value = cons_seq(make_node(AST_Undefined, stat).transform(compressor));
}
else if (stat instanceof AST_Switch) {
stat.expression = cons_seq(stat.expression);
@@ -1146,8 +1165,12 @@ merge(Compressor.prototype, {
}));
};
function is_undefined(node) {
return node instanceof AST_Undefined || node.is_undefined;
function is_undefined(node, compressor) {
return node.is_undefined
|| node instanceof AST_Undefined
|| node instanceof AST_UnaryPrefix
&& node.operator == "void"
&& !node.expression.has_side_effects(compressor);
}
/* -----[ boolean/negation helpers ]----- */
@@ -1339,7 +1362,7 @@ merge(Compressor.prototype, {
return this;
}
});
var unaryPrefix = makePredicate("! ~ - +");
var unaryPrefix = makePredicate("! ~ - + void");
AST_Node.DEFMETHOD("is_constant", function(){
// Accomodate when compress option evaluate=false
// as well as the common constant expressions !0 and -1
@@ -2638,6 +2661,7 @@ merge(Compressor.prototype, {
});
OPT(AST_Switch, function(self, compressor){
if (!compressor.option("switches")) return self;
var branch;
var value = self.expression.evaluate(compressor);
if (value !== self.expression) {
@@ -2649,49 +2673,39 @@ merge(Compressor.prototype, {
var body = [];
var default_branch;
var exact_match;
var fallthrough;
for (var i = 0, len = self.body.length; i < len && !exact_match; i++) {
branch = self.body[i];
if (branch instanceof AST_Default) {
if (!default_branch) default_branch = branch;
else if (!fallthrough) {
extract_declarations_from_unreachable_code(compressor, branch, decl);
continue;
if (!default_branch) {
default_branch = branch;
} else {
eliminate_branch(branch, body[body.length - 1]);
}
} else if (value !== self.expression) {
var exp = branch.expression.evaluate(compressor);
if (exp === value) {
exact_match = branch;
if (default_branch) {
body.splice(body.indexOf(default_branch), 1);
extract_declarations_from_unreachable_code(compressor, default_branch, decl);
var default_index = body.indexOf(default_branch);
body.splice(default_index, 1);
eliminate_branch(default_branch, body[default_index - 1]);
default_branch = null;
}
} else if (exp !== branch.expression && !fallthrough) {
extract_declarations_from_unreachable_code(compressor, branch, decl);
} else if (exp !== branch.expression) {
eliminate_branch(branch, body[body.length - 1]);
continue;
}
}
if (aborts(branch)) {
if (body.length > 0 && !fallthrough) {
var prev = body[body.length - 1];
if (prev.body.length == branch.body.length
&& make_node(AST_BlockStatement, prev, prev).equivalent_to(make_node(AST_BlockStatement, branch, branch)))
prev.body = [];
var prev = body[body.length - 1];
if (aborts(prev) && prev.body.length == branch.body.length
&& make_node(AST_BlockStatement, prev, prev).equivalent_to(make_node(AST_BlockStatement, branch, branch))) {
prev.body = [];
}
body.push(branch);
fallthrough = false;
} else {
body.push(branch);
fallthrough = true;
}
body.push(branch);
}
for (; i < len && fallthrough; i++) {
branch = self.body[i];
exact_match.body = exact_match.body.concat(branch.body);
fallthrough = !aborts(exact_match);
}
while (i < len) extract_declarations_from_unreachable_code(compressor, self.body[i++], decl);
while (i < len) eliminate_branch(self.body[i++], body[body.length - 1]);
if (body.length > 0) {
body[0].body = decl.concat(body[0].body);
}
@@ -2721,9 +2735,25 @@ merge(Compressor.prototype, {
has_break = true;
});
self.walk(tw);
if (!has_break) return make_node(AST_BlockStatement, self, body[0]).optimize(compressor);
if (!has_break) {
body = body[0].body.slice();
body.unshift(make_node(AST_SimpleStatement, self.expression, {
body: self.expression
}));
return make_node(AST_BlockStatement, self, {
body: body
}).optimize(compressor);
}
}
return self;
function eliminate_branch(branch, prev) {
if (prev && !aborts(prev)) {
prev.body = prev.body.concat(branch.body);
} else {
extract_declarations_from_unreachable_code(compressor, branch, decl);
}
}
});
OPT(AST_Try, function(self, compressor){
@@ -3036,7 +3066,7 @@ merge(Compressor.prototype, {
if (name instanceof AST_SymbolRef
&& name.name == "console"
&& name.undeclared()) {
return make_node(AST_Undefined, self).transform(compressor);
return make_node(AST_Undefined, self).optimize(compressor);
}
}
}
@@ -3112,7 +3142,7 @@ merge(Compressor.prototype, {
}
}
}
if (is_undefined(self.cdr)) {
if (is_undefined(self.cdr, compressor)) {
return make_node(AST_UnaryPrefix, self, {
operator : "void",
expression : self.car
@@ -3151,7 +3181,7 @@ merge(Compressor.prototype, {
self.expression = e;
return self;
} else {
return make_node(AST_Undefined, self).transform(compressor);
return make_node(AST_Undefined, self).optimize(compressor);
}
}
if (compressor.option("booleans") && compressor.in_boolean_context()) {
@@ -3175,6 +3205,9 @@ merge(Compressor.prototype, {
})).optimize(compressor);
}
}
if (self.operator == "-" && e instanceof AST_Infinity) {
e = e.transform(compressor);
}
if (e instanceof AST_Binary
&& (self.operator == "+" || self.operator == "-")
&& (e.operator == "*" || e.operator == "/" || e.operator == "%")) {
@@ -3184,8 +3217,7 @@ merge(Compressor.prototype, {
}
// avoids infinite recursion of numerals
if (self.operator != "-"
|| !(self.expression instanceof AST_Number
|| self.expression instanceof AST_Infinity)) {
|| !(e instanceof AST_Number || e instanceof AST_Infinity)) {
var ev = self.evaluate(compressor);
if (ev !== self) {
ev = make_node_from_constant(ev, self).optimize(compressor);
@@ -3228,8 +3260,8 @@ merge(Compressor.prototype, {
OPT(AST_Binary, function(self, compressor){
function reversible() {
return self.left instanceof AST_Constant
|| self.right instanceof AST_Constant
return self.left.is_constant()
|| self.right.is_constant()
|| !self.left.has_side_effects(compressor)
&& !self.right.has_side_effects(compressor);
}
@@ -3242,8 +3274,8 @@ merge(Compressor.prototype, {
}
}
if (commutativeOperators(self.operator)) {
if (self.right instanceof AST_Constant
&& !(self.left instanceof AST_Constant)) {
if (self.right.is_constant()
&& !self.left.is_constant()) {
// if right is a constant, whatever side effects the
// left side might have could not influence the
// result. hence, force switch.
@@ -3281,42 +3313,7 @@ merge(Compressor.prototype, {
}
break;
}
if (compressor.option("booleans") && compressor.in_boolean_context()) switch (self.operator) {
case "&&":
var ll = self.left.evaluate(compressor);
var rr = self.right.evaluate(compressor);
if (!ll || !rr) {
compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
return make_node(AST_Seq, self, {
car: self.left,
cdr: make_node(AST_False, self)
}).optimize(compressor);
}
if (ll !== self.left && ll) {
return self.right.optimize(compressor);
}
if (rr !== self.right && rr) {
return self.left.optimize(compressor);
}
break;
case "||":
var ll = self.left.evaluate(compressor);
var rr = self.right.evaluate(compressor);
if (ll !== self.left && ll || rr !== self.right && rr) {
compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
return make_node(AST_Seq, self, {
car: self.left,
cdr: make_node(AST_True, self)
}).optimize(compressor);
}
if (!ll) {
return self.right.optimize(compressor);
}
if (!rr) {
return self.left.optimize(compressor);
}
break;
case "+":
if (compressor.option("booleans") && self.operator == "+" && compressor.in_boolean_context()) {
var ll = self.left.evaluate(compressor);
var rr = self.right.evaluate(compressor);
if (ll && typeof ll == "string") {
@@ -3333,7 +3330,6 @@ merge(Compressor.prototype, {
cdr: make_node(AST_True, self)
}).optimize(compressor);
}
break;
}
if (compressor.option("comparisons") && self.is_boolean()) {
if (!(compressor.parent() instanceof AST_Binary)
@@ -3374,24 +3370,48 @@ merge(Compressor.prototype, {
if (compressor.option("evaluate")) {
switch (self.operator) {
case "&&":
if (self.left.is_constant()) {
if (self.left.constant_value(compressor)) {
compressor.warn("Condition left of && always true [{file}:{line},{col}]", self.start);
return maintain_this_binding(compressor.parent(), self, self.right);
} else {
compressor.warn("Condition left of && always false [{file}:{line},{col}]", self.start);
return maintain_this_binding(compressor.parent(), self, self.left);
var ll = self.left.evaluate(compressor);
if (!ll) {
compressor.warn("Condition left of && always false [{file}:{line},{col}]", self.start);
return maintain_this_binding(compressor.parent(), self, self.left).optimize(compressor);
} else if (ll !== self.left) {
compressor.warn("Condition left of && always true [{file}:{line},{col}]", self.start);
return maintain_this_binding(compressor.parent(), self, self.right).optimize(compressor);
}
if (compressor.option("booleans") && compressor.in_boolean_context()) {
var rr = self.right.evaluate(compressor);
if (!rr) {
compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
return make_node(AST_Seq, self, {
car: self.left,
cdr: make_node(AST_False, self)
}).optimize(compressor);
} else if (rr !== self.right) {
compressor.warn("Dropping side-effect-free && in boolean context [{file}:{line},{col}]", self.start);
return self.left.optimize(compressor);
}
}
break;
case "||":
if (self.left.is_constant()) {
if (self.left.constant_value(compressor)) {
compressor.warn("Condition left of || always true [{file}:{line},{col}]", self.start);
return maintain_this_binding(compressor.parent(), self, self.left);
} else {
compressor.warn("Condition left of || always false [{file}:{line},{col}]", self.start);
return maintain_this_binding(compressor.parent(), self, self.right);
var ll = self.left.evaluate(compressor);
if (!ll) {
compressor.warn("Condition left of || always false [{file}:{line},{col}]", self.start);
return maintain_this_binding(compressor.parent(), self, self.right).optimize(compressor);
} else if (ll !== self.left) {
compressor.warn("Condition left of || always true [{file}:{line},{col}]", self.start);
return maintain_this_binding(compressor.parent(), self, self.left).optimize(compressor);
}
if (compressor.option("booleans") && compressor.in_boolean_context()) {
var rr = self.right.evaluate(compressor);
if (!rr) {
compressor.warn("Dropping side-effect-free || in boolean context [{file}:{line},{col}]", self.start);
return self.left.optimize(compressor);
} else if (rr !== self.right) {
compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
return make_node(AST_Seq, self, {
car: self.left,
cdr: make_node(AST_True, self)
}).optimize(compressor);
}
}
break;
@@ -3617,9 +3637,9 @@ merge(Compressor.prototype, {
case "undefined":
return make_node(AST_Undefined, self).optimize(compressor);
case "NaN":
return make_node(AST_NaN, self);
return make_node(AST_NaN, self).optimize(compressor);
case "Infinity":
return make_node(AST_Infinity, self);
return make_node(AST_Infinity, self).optimize(compressor);
}
}
if (compressor.option("evaluate") && compressor.option("reduce_vars")) {
@@ -3649,19 +3669,48 @@ merge(Compressor.prototype, {
OPT(AST_Undefined, function(self, compressor){
if (compressor.option("unsafe")) {
var scope = compressor.find_parent(AST_Scope);
var undef = scope.find_variable("undefined");
var undef = find_variable(compressor, "undefined");
if (undef) {
var ref = make_node(AST_SymbolRef, self, {
name : "undefined",
scope : scope,
scope : undef.scope,
thedef : undef
});
ref.is_undefined = true;
return ref;
}
}
return self;
return make_node(AST_UnaryPrefix, self, {
operator: "void",
expression: make_node(AST_Number, self, {
value: 0
})
});
});
OPT(AST_Infinity, function(self, compressor){
var retain = compressor.option("keep_infinity") && !find_variable(compressor, "Infinity");
return retain ? self : make_node(AST_Binary, self, {
operator: "/",
left: make_node(AST_Number, self, {
value: 1
}),
right: make_node(AST_Number, self, {
value: 0
})
});
});
OPT(AST_NaN, function(self, compressor){
return find_variable(compressor, "NaN") ? make_node(AST_Binary, self, {
operator: "/",
left: make_node(AST_Number, self, {
value: 0
}),
right: make_node(AST_Number, self, {
value: 0
})
}) : self;
});
var ASSIGN_OPS = [ '+', '-', '/', '*', '%', '>>', '<<', '>>>', '|', '^', '&' ];
@@ -3979,7 +4028,7 @@ merge(Compressor.prototype, {
OPT(AST_RegExp, literals_in_boolean_context);
OPT(AST_Return, function(self, compressor){
if (self.value && is_undefined(self.value)) {
if (self.value && is_undefined(self.value, compressor)) {
self.value = null;
}
return self;
@@ -4000,7 +4049,7 @@ merge(Compressor.prototype, {
});
OPT(AST_Yield, function(self, compressor){
if (!self.is_star && self.expression instanceof AST_Undefined) {
if (self.expression && !self.is_star && is_undefined(self.expression, compressor)) {
self.expression = null;
}
return self;

View File

@@ -53,29 +53,29 @@ function is_some_comments(comment) {
function OutputStream(options) {
options = defaults(options, {
indent_start : 0,
indent_level : 4,
quote_keys : false,
space_colon : true,
ascii_only : false,
ascii_identifiers: undefined,
unescape_regexps : false,
inline_script : false,
width : 80,
max_line_len : false,
beautify : false,
source_map : null,
bracketize : false,
semicolons : true,
comments : false,
shebang : true,
preserve_line : false,
screw_ie8 : true,
preamble : null,
quote_style : 0,
keep_quoted_props: false,
shorthand : undefined,
ecma : 5,
indent_level : 4,
indent_start : 0,
inline_script : false,
keep_quoted_props: false,
max_line_len : false,
preamble : null,
preserve_line : false,
quote_keys : false,
quote_style : 0,
screw_ie8 : true,
semicolons : true,
shebang : true,
shorthand : undefined,
source_map : null,
space_colon : true,
unescape_regexps : false,
width : 80,
wrap_iife : false,
}, true);
@@ -559,8 +559,8 @@ function OutputStream(options) {
}));
}
if (comments.length > 0 && output.pos() == 0) {
if (output.option("shebang") && comments[0].type == "comment5") {
if (output.pos() == 0) {
if (comments.length > 0 && output.option("shebang") && comments[0].type == "comment5") {
output.print("#!" + comments.shift().value + "\n");
output.indent();
}
@@ -640,7 +640,7 @@ function OutputStream(options) {
return first_in_statement(output);
});
PARENS([ AST_Unary, AST_Undefined ], function(output){
PARENS(AST_Unary, function(output){
var p = output.parent();
return p instanceof AST_PropAccess && p.expression === this
|| p instanceof AST_Call && p.expression === this
@@ -652,15 +652,6 @@ function OutputStream(options) {
&& this.operator !== "--";
});
PARENS([ AST_Infinity, AST_NaN ], function(output){
var p = output.parent();
return p instanceof AST_PropAccess && p.expression === this
|| p instanceof AST_Call && p.expression === this
|| p instanceof AST_Unary && p.operator != "+" && p.operator != "-"
|| p instanceof AST_Binary && p.right === this
&& (p.operator == "/" || p.operator == "%");
});
PARENS(AST_Seq, function(output){
var p = output.parent();
return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4)
@@ -1625,24 +1616,7 @@ function OutputStream(options) {
DEFPRINT(AST_SymbolDeclaration, function(self, output){
self._do_print(output);
});
DEFPRINT(AST_Undefined, function(self, output){
output.print("void 0");
});
DEFPRINT(AST_Hole, noop);
DEFPRINT(AST_Infinity, function(self, output){
output.print("1");
output.space();
output.print("/");
output.space();
output.print("0");
});
DEFPRINT(AST_NaN, function(self, output){
output.print("0");
output.space();
output.print("/");
output.space();
output.print("0");
});
DEFPRINT(AST_This, function(self, output){
output.print("this");
});

View File

@@ -845,14 +845,14 @@ var ATOMIC_START_TOKEN = array_to_hash([ "atom", "num", "string", "regexp", "nam
function parse($TEXT, options) {
options = defaults(options, {
strict : false,
filename : null,
toplevel : null,
expression : false,
html5_comments : true,
bare_returns : false,
shebang : true,
cli : false,
expression : false,
filename : null,
html5_comments : true,
shebang : true,
strict : false,
toplevel : null,
});
var S = {

View File

@@ -79,12 +79,12 @@ function find_builtins() {
function mangle_properties(ast, options) {
options = defaults(options, {
reserved : null,
cache : null,
only_cache : false,
regex : null,
ignore_quoted : false,
debug : false
cache: null,
debug: false,
ignore_quoted: false,
only_cache: false,
regex: null,
reserved: null,
});
var reserved = options.reserved;

View File

@@ -100,8 +100,8 @@ SymbolDef.prototype = {
AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
options = defaults(options, {
cache: null,
screw_ie8: true,
cache: null
});
// pass 1: setup scope chaining and handle definitions
@@ -490,13 +490,13 @@ AST_Symbol.DEFMETHOD("global", function(){
AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
return defaults(options, {
except : [],
eval : false,
except : [],
keep_classnames: false,
keep_fnames : false,
screw_ie8 : true,
sort : false, // Ignored. Flag retained for backwards compatibility.
toplevel : false,
screw_ie8 : true,
keep_fnames : false,
keep_classnames : false
});
});
@@ -681,12 +681,12 @@ var base54 = (function() {
AST_Toplevel.DEFMETHOD("scope_warnings", function(options){
options = defaults(options, {
undeclared : false, // this makes a lot of noise
unreferenced : true,
assign_to_global : true,
eval : true,
func_arguments : true,
nested_defuns : true,
eval : true
undeclared : false, // this makes a lot of noise
unreferenced : true,
});
var tw = new TreeWalker(function(node){
if (options.undeclared

View File

@@ -4,7 +4,7 @@
"homepage": "http://lisperator.net/uglifyjs",
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
"license": "BSD-2-Clause",
"version": "2.8.19",
"version": "2.8.21",
"engines": {
"node": ">=0.8.0"
},

View File

@@ -840,8 +840,8 @@ equality_conditionals_false: {
f(0, true, 0),
f(1, 2, 3),
f(1, null, 3),
f(0/0),
f(0/0, "foo");
f(NaN),
f(NaN, "foo");
}
expect_stdout: true
}
@@ -888,8 +888,8 @@ equality_conditionals_true: {
f(0, true, 0),
f(1, 2, 3),
f(1, null, 3),
f(0/0),
f(0/0, "foo");
f(NaN),
f(NaN, "foo");
}
expect_stdout: true
}

View File

@@ -52,7 +52,7 @@ and: {
a = 7;
a = false;
a = 0/0;
a = NaN;
a = 0;
a = void 0;
a = null;
@@ -67,7 +67,7 @@ and: {
a = 6 << condition && -4.5;
a = condition && false;
a = console.log("b") && 0/0;
a = console.log("b") && NaN;
a = console.log("c") && 0;
a = 2 * condition && void 0;
a = condition + 3 && null;
@@ -149,7 +149,7 @@ or: {
a = 6 << condition || -4.5;
a = condition || false;
a = console.log("b") || 0/0;
a = console.log("b") || NaN;
a = console.log("c") || 0;
a = 2 * condition || void 0;
a = condition + 3 || null;
@@ -302,13 +302,13 @@ pow_with_number_constants: {
var m = 3 ** -10; // Result will be 0.000016935087808430286, which is too long
}
expect: {
var a = 0/0;
var a = NaN;
var b = 1;
var c = 1;
var d = 0/0;
var d = NaN;
var e = 1/0;
var f = 0;
var g = 0/0;
var g = NaN;
var h = 1/0;
var i = -1/0;
var j = .125;
@@ -627,7 +627,7 @@ unsafe_array: {
[1, 2, 3, a][0] + 1,
2,
3,
0/0,
NaN,
"1,21",
5,
(void 0)[1] + 1
@@ -896,3 +896,58 @@ issue_1649: {
}
expect_stdout: "-2";
}
issue_1760_1: {
options = {
evaluate: true,
}
input: {
!function(a) {
try {
throw 0;
} catch (NaN) {
a = +"foo";
}
console.log(a);
}();
}
expect: {
!function(a) {
try {
throw 0;
} catch (NaN) {
a = 0 / 0;
}
console.log(a);
}();
}
expect_stdout: "NaN"
}
issue_1760_2: {
options = {
evaluate: true,
keep_infinity: true,
}
input: {
!function(a) {
try {
throw 0;
} catch (Infinity) {
a = 123456789 / 0;
}
console.log(a);
}();
}
expect: {
!function(a) {
try {
throw 0;
} catch (Infinity) {
a = 1 / 0;
}
console.log(a);
}();
}
expect_stdout: "Infinity"
}

View File

@@ -21,13 +21,12 @@ pow_with_number_constants: {
var f = 2 ** -Infinity;
}
expect: {
// TODO: may need parentheses
var a = 5 ** 0/0;
var a = 5 ** NaN;
var b = 42 ** +0;
var c = 42 ** -0;
var d = 0/0 ** 1;
var e = 2 ** 1/0;
var f = 2 ** -1/0;
var d = NaN ** 1;
var e = 2 ** (1/0);
var f = 2 ** (-1/0);
}
}

View File

@@ -193,6 +193,7 @@ assorted_Infinity_NaN_undefined_in_with_scope: {
cascade: true,
side_effects: true,
sequences: false,
keep_infinity: false,
}
input: {
var f = console.log;
@@ -224,10 +225,73 @@ assorted_Infinity_NaN_undefined_in_with_scope: {
};
if (o) {
f(void 0, void 0);
f(0/0, 0/0);
f(NaN, NaN);
f(1/0, 1/0);
f(-1/0, -1/0);
f(0/0, 0/0);
f(NaN, NaN);
}
with (o) {
f(undefined, void 0);
f(NaN, 0/0);
f(Infinity, 1/0);
f(-Infinity, -1/0);
f(9 + undefined, 9 + void 0);
}
}
expect_stdout: true
}
assorted_Infinity_NaN_undefined_in_with_scope_keep_infinity: {
options = {
unused: true,
evaluate: true,
dead_code: true,
conditionals: true,
comparisons: true,
booleans: true,
hoist_funs: true,
keep_fargs: true,
if_return: true,
join_vars: true,
cascade: true,
side_effects: true,
sequences: false,
keep_infinity: true,
}
input: {
var f = console.log;
var o = {
undefined : 3,
NaN : 4,
Infinity : 5,
};
if (o) {
f(undefined, void 0);
f(NaN, 0/0);
f(Infinity, 1/0);
f(-Infinity, -(1/0));
f(2 + 7 + undefined, 2 + 7 + void 0);
}
with (o) {
f(undefined, void 0);
f(NaN, 0/0);
f(Infinity, 1/0);
f(-Infinity, -(1/0));
f(2 + 7 + undefined, 2 + 7 + void 0);
}
}
expect: {
var f = console.log, o = {
undefined : 3,
NaN : 4,
Infinity : 5
};
if (o) {
f(void 0, void 0);
f(NaN, NaN);
f(Infinity, 1/0);
f(-Infinity, -1/0);
f(NaN, NaN);
}
with (o) {
f(undefined, void 0);

View File

@@ -154,12 +154,12 @@ should_warn: {
"WARN: Boolean || always true [test/compress/issue-1261.js:129,23]",
"WARN: Dropping __PURE__ call [test/compress/issue-1261.js:129,23]",
"WARN: Condition always true [test/compress/issue-1261.js:129,23]",
"WARN: Boolean || always true [test/compress/issue-1261.js:130,8]",
"WARN: Condition left of || always true [test/compress/issue-1261.js:130,8]",
"WARN: Condition always true [test/compress/issue-1261.js:130,8]",
"WARN: Boolean && always false [test/compress/issue-1261.js:131,23]",
"WARN: Dropping __PURE__ call [test/compress/issue-1261.js:131,23]",
"WARN: Condition always false [test/compress/issue-1261.js:131,23]",
"WARN: Boolean && always false [test/compress/issue-1261.js:132,8]",
"WARN: Condition left of && always false [test/compress/issue-1261.js:132,8]",
"WARN: Condition always false [test/compress/issue-1261.js:132,8]",
"WARN: + in boolean context always true [test/compress/issue-1261.js:133,23]",
"WARN: Dropping __PURE__ call [test/compress/issue-1261.js:133,23]",

View File

@@ -0,0 +1,54 @@
case_1: {
options = {
dead_code: true,
evaluate: true,
switches: true,
}
input: {
var a = 0, b = 1;
switch (true) {
case a, true:
default:
b = 2;
case true:
}
console.log(a, b);
}
expect: {
var a = 0, b = 1;
switch (true) {
case a, true:
b = 2;
}
console.log(a, b);
}
expect_stdout: "0 2"
}
case_2: {
options = {
dead_code: true,
evaluate: true,
switches: true,
}
input: {
var a = 0, b = 1;
switch (0) {
default:
b = 2;
case a:
a = 3;
case 0:
}
console.log(a, b);
}
expect: {
var a = 0, b = 1;
switch (0) {
case a:
a = 3;
}
console.log(a, b);
}
expect_stdout: "3 1"
}

View File

@@ -6,7 +6,7 @@ NaN_and_Infinity_must_have_parens: {
}
expect: {
(1/0).toString();
(0/0).toString();
NaN.toString();
}
}
@@ -24,6 +24,36 @@ NaN_and_Infinity_should_not_be_replaced_when_they_are_redefined: {
}
}
NaN_and_Infinity_must_have_parens_evaluate: {
options = {
evaluate: true,
}
input: {
(123456789 / 0).toString();
(+"foo").toString();
}
expect: {
(1/0).toString();
NaN.toString();
}
}
NaN_and_Infinity_should_not_be_replaced_when_they_are_redefined_evaluate: {
options = {
evaluate: true,
}
input: {
var Infinity, NaN;
(123456789 / 0).toString();
(+"foo").toString();
}
expect: {
var Infinity, NaN;
(1/0).toString();
(0/0).toString();
}
}
beautify_off_1: {
options = {
evaluate: true,

View File

@@ -186,7 +186,7 @@ unary_binary_parenthesis: {
});
}
expect: {
var v = [ 0, 1, 0/0, 1/0, null, void 0, true, false, "", "foo", /foo/ ];
var v = [ 0, 1, NaN, 1/0, null, void 0, true, false, "", "foo", /foo/ ];
v.forEach(function(x) {
v.forEach(function(y) {
console.log(

View File

@@ -77,7 +77,7 @@ sub_properties: {
a[3.14] = 3;
a.if = 4;
a["foo bar"] = 5;
a[0/0] = 6;
a[NaN] = 6;
a[null] = 7;
a[void 0] = 8;
}

View File

@@ -1399,6 +1399,8 @@ issue_1670_1: {
evaluate: true,
dead_code: true,
reduce_vars: true,
side_effects: true,
switches: true,
unused: true,
}
input: {
@@ -1429,6 +1431,8 @@ issue_1670_2: {
dead_code: true,
passes: 2,
reduce_vars: true,
side_effects: true,
switches: true,
unused: true,
}
input: {
@@ -1458,6 +1462,8 @@ issue_1670_3: {
evaluate: true,
dead_code: true,
reduce_vars: true,
side_effects: true,
switches: true,
unused: true,
}
input: {
@@ -1488,6 +1494,8 @@ issue_1670_4: {
dead_code: true,
passes: 2,
reduce_vars: true,
side_effects: true,
switches: true,
unused: true,
}
input: {
@@ -1516,6 +1524,8 @@ issue_1670_5: {
evaluate: true,
keep_fargs: false,
reduce_vars: true,
side_effects: true,
switches: true,
unused: true,
}
input: {
@@ -1544,6 +1554,8 @@ issue_1670_6: {
evaluate: true,
keep_fargs: false,
reduce_vars: true,
side_effects: true,
switches: true,
unused: true,
}
input: {

View File

@@ -440,3 +440,29 @@ func_def_5: {
}
expect_stdout: "true"
}
issue_1758: {
options = {
sequences: true,
side_effects: true,
}
input: {
console.log(function(c) {
var undefined = 42;
return function() {
c--;
c--, c.toString();
return;
}();
}());
}
expect: {
console.log(function(c) {
var undefined = 42;
return function() {
return c--, c--, c.toString(), void 0;
}();
}());
}
expect_stdout: "undefined"
}

View File

@@ -1,5 +1,10 @@
constant_switch_1: {
options = { dead_code: true, evaluate: true };
options = {
dead_code: true,
evaluate: true,
side_effects: true,
switches: true,
}
input: {
switch (1+1) {
case 1: foo(); break;
@@ -13,7 +18,12 @@ constant_switch_1: {
}
constant_switch_2: {
options = { dead_code: true, evaluate: true };
options = {
dead_code: true,
evaluate: true,
side_effects: true,
switches: true,
}
input: {
switch (1) {
case 1: foo();
@@ -28,7 +38,12 @@ constant_switch_2: {
}
constant_switch_3: {
options = { dead_code: true, evaluate: true };
options = {
dead_code: true,
evaluate: true,
side_effects: true,
switches: true,
}
input: {
switch (10) {
case 1: foo();
@@ -44,7 +59,12 @@ constant_switch_3: {
}
constant_switch_4: {
options = { dead_code: true, evaluate: true };
options = {
dead_code: true,
evaluate: true,
side_effects: true,
switches: true,
}
input: {
switch (2) {
case 1:
@@ -65,7 +85,12 @@ constant_switch_4: {
}
constant_switch_5: {
options = { dead_code: true, evaluate: true };
options = {
dead_code: true,
evaluate: true,
side_effects: true,
switches: true,
}
input: {
switch (1) {
case 1:
@@ -94,7 +119,12 @@ constant_switch_5: {
}
constant_switch_6: {
options = { dead_code: true, evaluate: true };
options = {
dead_code: true,
evaluate: true,
side_effects: true,
switches: true,
}
input: {
OUT: {
foo();
@@ -123,7 +153,12 @@ constant_switch_6: {
}
constant_switch_7: {
options = { dead_code: true, evaluate: true };
options = {
dead_code: true,
evaluate: true,
side_effects: true,
switches: true,
}
input: {
OUT: {
foo();
@@ -161,7 +196,12 @@ constant_switch_7: {
}
constant_switch_8: {
options = { dead_code: true, evaluate: true };
options = {
dead_code: true,
evaluate: true,
side_effects: true,
switches: true,
}
input: {
OUT: switch (1) {
case 1:
@@ -185,7 +225,12 @@ constant_switch_8: {
}
constant_switch_9: {
options = { dead_code: true, evaluate: true };
options = {
dead_code: true,
evaluate: true,
side_effects: true,
switches: true,
}
input: {
OUT: switch (1) {
case 1:
@@ -210,7 +255,10 @@ constant_switch_9: {
}
drop_default_1: {
options = { dead_code: true };
options = {
dead_code: true,
switches: true,
}
input: {
switch (foo) {
case 'bar': baz();
@@ -225,7 +273,10 @@ drop_default_1: {
}
drop_default_2: {
options = { dead_code: true };
options = {
dead_code: true,
switches: true,
}
input: {
switch (foo) {
case 'bar': baz(); break;
@@ -241,7 +292,10 @@ drop_default_2: {
}
keep_default: {
options = { dead_code: true };
options = {
dead_code: true,
switches: true,
}
input: {
switch (foo) {
case 'bar': baz();
@@ -263,6 +317,8 @@ issue_1663: {
options = {
dead_code: true,
evaluate: true,
side_effects: true,
switches: true,
}
input: {
var a = 100, b = 10;
@@ -294,6 +350,7 @@ issue_1663: {
drop_case: {
options = {
dead_code: true,
switches: true,
}
input: {
switch (foo) {
@@ -312,6 +369,7 @@ drop_case: {
keep_case: {
options = {
dead_code: true,
switches: true,
}
input: {
switch (foo) {
@@ -332,6 +390,7 @@ issue_376: {
options = {
dead_code: true,
evaluate: true,
switches: true,
}
input: {
switch (true) {
@@ -354,6 +413,7 @@ issue_376: {
issue_441_1: {
options = {
dead_code: true,
switches: true,
}
input: {
switch (foo) {
@@ -381,6 +441,7 @@ issue_441_1: {
issue_441_2: {
options = {
dead_code: true,
switches: true,
}
input: {
switch (foo) {
@@ -414,6 +475,8 @@ issue_1674: {
options = {
dead_code: true,
evaluate: true,
side_effects: true,
switches: true,
}
input: {
switch (0) {
@@ -435,6 +498,7 @@ issue_1679: {
options = {
dead_code: true,
evaluate: true,
switches: true,
}
input: {
var a = 100, b = 10;
@@ -482,6 +546,7 @@ issue_1680_1: {
options = {
dead_code: true,
evaluate: true,
switches: true,
}
input: {
function f(x) {
@@ -522,6 +587,7 @@ issue_1680_1: {
issue_1680_2: {
options = {
dead_code: true,
switches: true,
}
input: {
var a = 100, b = 10;
@@ -557,6 +623,7 @@ issue_1680_2: {
issue_1690_1: {
options = {
dead_code: true,
switches: true,
}
input: {
switch (console.log("PASS")) {}
@@ -570,6 +637,7 @@ issue_1690_1: {
issue_1690_2: {
options = {
dead_code: false,
switches: true,
}
input: {
switch (console.log("PASS")) {}
@@ -585,6 +653,7 @@ if_switch_typeof: {
conditionals: true,
dead_code: true,
side_effects: true,
switches: true,
}
input: {
if (a) switch(typeof b) {}
@@ -597,6 +666,7 @@ if_switch_typeof: {
issue_1698: {
options = {
side_effects: true,
switches: true,
}
input: {
var a = 1;
@@ -618,6 +688,7 @@ issue_1698: {
issue_1705_1: {
options = {
dead_code: true,
switches: true,
}
input: {
var a = 0;
@@ -646,6 +717,7 @@ issue_1705_2: {
reduce_vars: true,
sequences: true,
side_effects: true,
switches: true,
toplevel: true,
unused: true,
}
@@ -666,6 +738,7 @@ issue_1705_2: {
issue_1705_3: {
options = {
dead_code: true,
switches: true,
}
input: {
switch (a) {
@@ -721,3 +794,25 @@ beautify: {
"}",
]
}
issue_1758: {
options = {
dead_code: true,
switches: true,
}
input: {
var a = 1, b = 2;
switch (a--) {
default:
b++;
}
console.log(a, b);
}
expect: {
var a = 1, b = 2;
a--;
b++;
console.log(a, b);
}
expect_stdout: "0 3"
}

View File

@@ -79,5 +79,13 @@ describe("comment filters", function() {
output: { preamble: "/* Build */" }
}).code;
assert.strictEqual(code, "#!/usr/bin/node\n/* Build */\nvar x=10;");
})
});
it("Should handle preamble without shebang correctly", function() {
var code = UglifyJS.minify("var x = 10;", {
fromString: true,
output: { preamble: "/* Build */" }
}).code;
assert.strictEqual(code, "/* Build */\nvar x=10;");
});
});

View File

@@ -4,22 +4,11 @@ var U = require("../tools/node");
var path = require("path");
var fs = require("fs");
var assert = require("assert");
var vm = require("vm");
var sandbox = require("./sandbox");
var tests_dir = path.dirname(module.filename);
var failures = 0;
var failed_files = {};
var same_stdout = ~process.version.lastIndexOf("v0.12.", 0) ? function(expected, actual) {
if (typeof expected != typeof actual) return false;
if (typeof expected != "string") {
if (expected.name != actual.name) return false;
expected = expected.message.slice(expected.message.lastIndexOf("\n") + 1);
actual = actual.message.slice(actual.message.lastIndexOf("\n") + 1);
}
return expected == actual;
} : function(expected, actual) {
return typeof expected == typeof actual && expected.toString() == actual.toString();
};
run_compress_tests();
if (failures) {
@@ -182,11 +171,11 @@ function run_compress_tests() {
}
}
if (test.expect_stdout) {
var stdout = run_code(input_code);
var stdout = sandbox.run_code(input_code);
if (test.expect_stdout === true) {
test.expect_stdout = stdout;
}
if (!same_stdout(test.expect_stdout, stdout)) {
if (!sandbox.same_stdout(test.expect_stdout, stdout)) {
log("!!! Invalid input or expected stdout\n---INPUT---\n{input}\n---EXPECTED {expected_type}---\n{expected}\n---ACTUAL {actual_type}---\n{actual}\n\n", {
input: input_formatted,
expected_type: typeof test.expect_stdout == "string" ? "STDOUT" : "ERROR",
@@ -197,8 +186,8 @@ function run_compress_tests() {
failures++;
failed_files[file] = 1;
} else {
stdout = run_code(output);
if (!same_stdout(test.expect_stdout, stdout)) {
stdout = sandbox.run_code(output);
if (!sandbox.same_stdout(test.expect_stdout, stdout)) {
log("!!! failed\n---INPUT---\n{input}\n---EXPECTED {expected_type}---\n{expected}\n---ACTUAL {actual_type}---\n{actual}\n\n", {
input: input_formatted,
expected_type: typeof test.expect_stdout == "string" ? "STDOUT" : "ERROR",
@@ -330,19 +319,3 @@ function evaluate(code) {
code = make_code(code, { beautify: true });
return new Function("return(" + code + ")")();
}
function run_code(code) {
var stdout = "";
var original_write = process.stdout.write;
process.stdout.write = function(chunk) {
stdout += chunk;
};
try {
new vm.Script(code).runInNewContext({ console: console }, { timeout: 5000 });
return stdout;
} catch (ex) {
return ex;
} finally {
process.stdout.write = original_write;
}
}

50
test/sandbox.js Normal file
View File

@@ -0,0 +1,50 @@
var vm = require("vm");
var FUNC_TOSTRING = [
"Function.prototype.toString = Function.prototype.valueOf = function() {",
" var ids = [];",
" return function() {",
" var i = ids.indexOf(this);",
" if (i < 0) {",
" i = ids.length;",
" ids.push(this);",
" }",
' return "[Function: __func_" + i + "__]";',
" }",
"}();",
""
].join("\n");
exports.run_code = function(code) {
var stdout = "";
var original_write = process.stdout.write;
process.stdout.write = function(chunk) {
stdout += chunk;
};
try {
new vm.Script(FUNC_TOSTRING + code).runInNewContext({
console: {
log: function() {
return console.log.apply(console, [].map.call(arguments, function(arg) {
return typeof arg == "function" ? arg.toString() : arg;
}));
}
}
}, { timeout: 30000 });
return stdout;
} catch (ex) {
return ex;
} finally {
process.stdout.write = original_write;
}
};
exports.same_stdout = ~process.version.lastIndexOf("v0.12.", 0) ? function(expected, actual) {
if (typeof expected != typeof actual) return false;
if (typeof expected != "string") {
if (expected.name != actual.name) return false;
expected = expected.message.slice(expected.message.lastIndexOf("\n") + 1);
actual = actual.message.slice(actual.message.lastIndexOf("\n") + 1);
}
return expected == actual;
} : function(expected, actual) {
return typeof expected == typeof actual && expected.toString() == actual.toString();
};

File diff suppressed because it is too large Load Diff

33
test/ufuzz.json Normal file
View File

@@ -0,0 +1,33 @@
[
{
"compress": {
"warnings": false
}
},
{
"compress": {
"warnings": false
},
"mangle": false
},
{
"compress": false,
"mangle": true
},
{
"compress": false,
"mangle": false,
"output": {
"beautify": true,
"bracketize": true
}
},
{
"compress": {
"keep_fargs": false,
"passes": 3,
"pure_getters": true,
"warnings": false
}
}
]

View File

@@ -46,21 +46,21 @@ function read_source_map(code) {
UglifyJS.minify = function(files, options) {
options = UglifyJS.defaults(options, {
spidermonkey : false,
outSourceMap : null,
outFileName : null,
sourceRoot : null,
inSourceMap : null,
sourceMapUrl : null,
sourceMapInline : false,
compress : {},
fromString : false,
warnings : false,
inSourceMap : null,
mangle : {},
mangleProperties : false,
nameCache : null,
outFileName : null,
output : null,
compress : {},
parse : {}
outSourceMap : null,
parse : {},
sourceMapInline : false,
sourceMapUrl : null,
sourceRoot : null,
spidermonkey : false,
warnings : false,
});
UglifyJS.base54.reset();