Compare commits

...

9 Commits

Author SHA1 Message Date
Alex Lam S.L
a3dfeea144 v3.3.11 2018-02-15 19:24:35 +00:00
Alex Lam S.L
d316fb139d fix unsafe evaluate on type-converting operators (#2917)
fixes #2916
2018-02-14 16:48:47 +08:00
Alex Lam S.L
83d8aa8b12 fix collapse_vars within loops (#2915)
fixes #2914
2018-02-14 05:15:52 +08:00
Alex Lam S.L
4f1c12b6fd report options upon reminify input error (#2911) 2018-02-13 07:29:39 +08:00
Alex Lam S.L
d8e0e34354 collapse within unary expressions (#2910) 2018-02-13 07:10:37 +08:00
Alex Lam S.L
0c4f315c02 fix corner case in collapse_vars (#2909)
fixes #2908
2018-02-13 01:41:22 +08:00
Alex Lam S.L
0809699bdc simplify do-while into for (#2907)
fixes #2904
2018-02-12 23:28:28 +08:00
Alex Lam S.L
2088e1c19d fix AST corruption due to collapse_vars & inline (#2899)
fixes #2898
2018-02-09 06:54:37 +08:00
Alex Lam S.L
bf1d47180c fix join_vars on property accessors (#2895)
fixes #2893
2018-02-09 01:52:39 +08:00
9 changed files with 325 additions and 27 deletions

View File

@@ -914,6 +914,7 @@ merge(Compressor.prototype, {
function tighten_body(statements, compressor) {
var scope = compressor.find_parent(AST_Scope);
var in_loop = is_in_loop();
var CHANGED, max_iter = 10;
do {
CHANGED = false;
@@ -936,6 +937,14 @@ merge(Compressor.prototype, {
}
} while (CHANGED && max_iter-- > 0);
function is_in_loop() {
for (var node, level = 0; node = compressor.parent(level); level++) {
if (node instanceof AST_IterationStatement) return true;
if (node instanceof AST_Scope) break;
}
return false;
}
// Search from right to left for assignment-like expressions:
// - `var a = x;`
// - `a = x;`
@@ -972,13 +981,13 @@ merge(Compressor.prototype, {
|| node instanceof AST_Try
|| node instanceof AST_With
|| parent instanceof AST_For && node !== parent.init
|| (side_effects || !replace_all)
|| !replace_all
&& (node instanceof AST_SymbolRef && !node.is_declared(compressor))) {
abort = true;
return node;
}
// Stop only if candidate is found within conditional branches
if (!stop_if_hit && (side_effects || !replace_all)
if (!stop_if_hit && (!lhs_local || !replace_all)
&& (parent instanceof AST_Binary && lazy_op(parent.operator) && parent.left !== node
|| parent instanceof AST_Conditional && parent.condition !== node
|| parent instanceof AST_If && parent.condition !== node)) {
@@ -1098,15 +1107,10 @@ merge(Compressor.prototype, {
if (!lhs || is_lhs_read_only(lhs) || lhs.has_side_effects(compressor)) continue;
// Locate symbols which may execute code outside of scanning range
var lvalues = get_lvalues(candidate);
var lhs_local = is_lhs_local(lhs);
if (lhs instanceof AST_SymbolRef) lvalues[lhs.name] = false;
var replace_all = value_def;
if (!replace_all && lhs instanceof AST_SymbolRef) {
var def = lhs.definition();
if (def.references.length - def.replaced == (candidate instanceof AST_VarDef ? 1 : 2)) {
replace_all = true;
}
}
var side_effects = value_has_side_effects(candidate);
var replace_all = replace_all_symbols();
var may_throw = candidate.may_throw(compressor);
var funarg = candidate.name instanceof AST_SymbolFunarg;
var hit = funarg;
@@ -1151,7 +1155,7 @@ merge(Compressor.prototype, {
hit_index++;
}
branch.expression = branch.expression.transform(scanner);
if (side_effects || !replace_all) break;
if (!replace_all) break;
}
}
abort = true;
@@ -1265,6 +1269,8 @@ merge(Compressor.prototype, {
} else if (expr instanceof AST_Unary) {
if (expr.operator == "++" || expr.operator == "--") {
candidates.push(hit_stack.slice());
} else {
extract_candidates(expr.expression);
}
} else if (expr instanceof AST_VarDef) {
if (expr.value) {
@@ -1319,6 +1325,7 @@ merge(Compressor.prototype, {
return find_stop(parent, level + 1, true);
}
if (parent instanceof AST_Switch) return node;
if (parent instanceof AST_Unary) return node;
if (parent instanceof AST_VarDef) return node;
return null;
}
@@ -1393,11 +1400,33 @@ merge(Compressor.prototype, {
}));
}
function is_lhs_local(lhs) {
while (lhs instanceof AST_PropAccess) lhs = lhs.expression;
return lhs instanceof AST_SymbolRef
&& lhs.definition().scope === scope
&& !(in_loop
&& (lhs.name in lvalues
|| candidate instanceof AST_Unary
|| candidate instanceof AST_Assign && candidate.operator != "="));
}
function value_has_side_effects(expr) {
if (expr instanceof AST_Unary) return false;
return get_rvalue(expr).has_side_effects(compressor);
}
function replace_all_symbols() {
if (side_effects) return false;
if (value_def) return true;
if (lhs instanceof AST_SymbolRef) {
var def = lhs.definition();
if (def.references.length - def.replaced == (candidate instanceof AST_VarDef ? 1 : 2)) {
return true;
}
}
return false;
}
function may_modify(sym) {
var def = sym.definition();
if (def.orig.length == 1 && def.orig[0] instanceof AST_SymbolDefun) return false;
@@ -1816,11 +1845,12 @@ merge(Compressor.prototype, {
}
if (prop instanceof AST_Node) break;
prop = "" + prop;
if (compressor.has_directive("use strict")) {
if (!all(def.value.properties, function(node) {
return node.key != prop && node.key.name != prop;
})) break;
}
var diff = compressor.has_directive("use strict") ? function(node) {
return node.key != prop && node.key.name != prop;
} : function(node) {
return node.key.name != prop;
};
if (!all(def.value.properties, diff)) break;
def.value.properties.push(make_node(AST_ObjectKeyVal, node, {
key: prop,
value: node.right
@@ -2345,6 +2375,7 @@ merge(Compressor.prototype, {
}
return this;
});
var non_converting_unary = makePredicate("! typeof void");
def(AST_UnaryPrefix, function(compressor, depth) {
var e = this.expression;
// Function would be evaluated to an array and so typeof would
@@ -2356,6 +2387,7 @@ merge(Compressor.prototype, {
&& e.fixed_value() instanceof AST_Lambda)) {
return typeof function(){};
}
if (!non_converting_unary(this.operator)) depth++;
e = e._eval(compressor, depth);
if (e === this.expression) return this;
switch (this.operator) {
@@ -2372,7 +2404,9 @@ merge(Compressor.prototype, {
}
return this;
});
var non_converting_binary = makePredicate("&& || === !==");
def(AST_Binary, function(compressor, depth) {
if (!non_converting_binary(this.operator)) depth++;
var left = this.left._eval(compressor, depth);
if (left === this.left) return this;
var right = this.right._eval(compressor, depth);
@@ -3671,6 +3705,13 @@ merge(Compressor.prototype, {
]
}).optimize(compressor);
}
if (self.body instanceof AST_SimpleStatement) return make_node(AST_For, self, {
condition: make_sequence(self.condition, [
self.body.body,
self.condition
]),
body: make_node(AST_EmptyStatement, self)
}).optimize(compressor);
return self;
});
@@ -4378,6 +4419,7 @@ merge(Compressor.prototype, {
&& !self.pure
&& !fn.contains_this()
&& can_inject_symbols()) {
fn._squeezed = true;
return make_sequence(self, flatten_fn()).optimize(compressor);
}
if (compressor.option("side_effects") && all(fn.body, is_empty)) {

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": "3.3.10",
"version": "3.3.11",
"engines": {
"node": ">=0.8.0"
},

View File

@@ -4517,3 +4517,105 @@ issue_2891_2: {
}
expect_stdout: true
}
issue_2908: {
options = {
collapse_vars: true,
}
input: {
var a = 0, b = 0;
function f(c) {
if (1 == c) return;
a++;
if (2 == c) b = a;
}
f(0);
f(2);
console.log(b);
}
expect: {
var a = 0, b = 0;
function f(c) {
if (1 == c) return;
a++;
if (2 == c) b = a;
}
f(0);
f(2);
console.log(b);
}
expect_stdout: "2"
}
issue_2914_1: {
options = {
collapse_vars: true,
}
input: {
function read(input) {
var i = 0;
var e = 0;
var t = 0;
while (e < 32) {
var n = input[i++];
t |= (127 & n) << e;
if (0 === (128 & n))
return t;
e += 7;
}
}
console.log(read([129]));
}
expect: {
function read(input) {
var i = 0;
var e = 0;
var t = 0;
while (e < 32) {
var n = input[i++];
t |= (127 & n) << e;
if (0 === (128 & n))
return t;
e += 7;
}
}
console.log(read([129]));
}
expect_stdout: "1"
}
issue_2914_2: {
options = {
collapse_vars: true,
}
input: {
function read(input) {
var i = 0;
var e = 0;
var t = 0;
while (e < 32) {
var n = input[i++];
t = (127 & n) << e;
if (0 === (128 & n))
return t;
e += 7;
}
}
console.log(read([129]));
}
expect: {
function read(input) {
var i = 0;
var e = 0;
var t = 0;
while (e < 32) {
var n = input[i++];
if (0 === (128 & n))
return t = (127 & n) << e;
e += 7;
}
}
console.log(read([129]));
}
expect_stdout: "0"
}

View File

@@ -1429,3 +1429,63 @@ string_case: {
"199",
]
}
issue_2916_1: {
options = {
evaluate: true,
reduce_vars: true,
unsafe: true,
}
input: {
var c = "PASS";
(function(a, b) {
(function(d) {
d[0] = 1;
})(b);
a == b && (c = "FAIL");
})("", []);
console.log(c);
}
expect: {
var c = "PASS";
(function(a, b) {
(function(d) {
d[0] = 1;
})(b);
a == b && (c = "FAIL");
})("", []);
console.log(c);
}
expect_stdout: "PASS"
}
issue_2916_2: {
options = {
collapse_vars: true,
evaluate: true,
inline: true,
reduce_vars: true,
side_effects: true,
unsafe: true,
unused: true,
}
input: {
var c = "FAIL";
(function(b) {
(function(d) {
d[0] = 1;
})(b);
+b && (c = "PASS");
})([]);
console.log(c);
}
expect: {
var c = "FAIL";
(function(b) {
b[0] = 1;
+b && (c = "PASS");
})([]);
console.log(c);
}
expect_stdout: "PASS"
}

View File

@@ -1343,7 +1343,7 @@ issue_2630_4: {
var x = 3, a = 1, b = 2;
(function() {
(function() {
while (--x >= 0 && void (a++, b += a));
while (--x >= 0 && void (b += ++a));
})();
})();
console.log(a);
@@ -1989,3 +1989,33 @@ issue_2783: {
}
expect_stdout: "PASS"
}
issue_2898: {
options = {
collapse_vars: true,
inline: true,
reduce_vars: true,
sequences: true,
unused: true,
}
input: {
var c = 0;
(function() {
while (f());
function f() {
var b = (c = 1 + c, void (c = 1 + c));
b && b[0];
}
})();
console.log(c);
}
expect: {
var c = 0;
(function() {
while (b = void 0, void ((b = void (c = 1 + (c = 1 + c))) && b[0]));
var b;
})(),
console.log(c);
}
expect_stdout: "2"
}

View File

@@ -605,3 +605,20 @@ issue_2740_5: {
}
expect_stdout: "0 undefined"
}
issue_2904: {
options = {
join_vars: true,
loops: true,
}
input: {
var a = 1;
do {
console.log(a);
} while (--a);
}
expect: {
for (var a = 1; console.log(a), --a;);
}
expect_stdout: "1"
}

View File

@@ -1588,3 +1588,55 @@ issue_2816: {
}
expect_stdout: "3 2 4"
}
issue_2893_1: {
options = {
join_vars: true,
}
input: {
var o = {
get a() {
return "PASS";
},
};
o.a = "FAIL";
console.log(o.a);
}
expect: {
var o = {
get a() {
return "PASS";
},
};
o.a = "FAIL";
console.log(o.a);
}
expect_stdout: "PASS"
}
issue_2893_2: {
options = {
join_vars: true,
}
input: {
var o = {
set a(v) {
this.b = v;
},
b: "FAIL",
};
o.a = "PASS";
console.log(o.b);
}
expect: {
var o = {
set a(v) {
this.b = v;
},
b: "FAIL",
};
o.a = "PASS";
console.log(o.b);
}
expect_stdout: "PASS"
}

View File

@@ -1178,9 +1178,7 @@ toplevel_on_loops_1: {
console.log("bar:", --x);
}
var x = 3;
do
bar();
while (x);
for (;bar(), x;);
}
expect_stdout: true
}
@@ -1208,9 +1206,7 @@ toplevel_off_loops_1: {
console.log("bar:", --x);
}
var x = 3;
do
bar();
while (x);
for (;bar(), x;);
}
expect_stdout: true
}
@@ -1265,9 +1261,7 @@ toplevel_off_loops_2: {
console.log("bar:");
}
var x = 3;
do
bar();
while (x);
for (;bar(), x;);
}
}

View File

@@ -372,8 +372,9 @@ function reminify(orig_options, input_code, input_formatted, expect_stdout) {
var options_formatted = JSON.stringify(options, null, 4);
var result = U.minify(input_code, options);
if (result.error) {
log("!!! failed input reminify\n---INPUT---\n{input}\n--ERROR---\n{error}\n\n", {
log("!!! failed input reminify\n---INPUT---\n{input}\n---OPTIONS---\n{options}\n--ERROR---\n{error}\n\n", {
input: input_formatted,
options: options_formatted,
error: result.error,
});
return false;