Compare commits

...

13 Commits

Author SHA1 Message Date
Alex Lam S.L
0d17c5b0fa v3.6.9 2019-11-12 22:50:52 +08:00
Alex Lam S.L
5b20bad4b3 fix corner case in dead_code (#3579)
fixes #3578
2019-11-12 05:16:14 +08:00
Alex Lam S.L
765a06340f enable cache on GitHub Actions (#3570) 2019-11-10 09:06:48 +08:00
Alex Lam S.L
5045e140b1 fix corner case in conditionals (#3577)
fixes #3576
2019-11-09 00:53:15 +08:00
Alex Lam S.L
10648c9af6 enhance dead_code (#3575) 2019-11-08 13:45:28 +08:00
Alex Lam S.L
87e67ec299 fix corner case in collapse_vars (#3574)
fixes #3573
2019-11-07 20:38:03 +08:00
Alex Lam S.L
61a0dad9fe v3.6.8 2019-11-06 13:52:58 +08:00
Alex Lam S.L
3e2c51a4da enhance collapse_vars (#3572) 2019-11-05 18:15:28 +08:00
Alex Lam S.L
0e29ad5eb9 fix corner case in evaluate (#3569) 2019-11-04 13:13:48 +08:00
Alex Lam S.L
0f2687ecfc v3.6.7 2019-11-02 13:32:05 +08:00
Alex Lam S.L
1c0defdc03 enhance unsafe evaluate (#3564) 2019-11-02 03:34:32 +08:00
Alex Lam S.L
dcbf2236c7 more tests for #3562 (#3565) 2019-11-02 03:34:20 +08:00
Alex Lam S.L
24bb288832 fix corner case in collapse_vars (#3563)
fixes #3562
2019-11-01 22:38:19 +08:00
9 changed files with 389 additions and 54 deletions

View File

@@ -14,6 +14,10 @@ jobs:
TYPE: ${{ matrix.script }}
steps:
- uses: actions/checkout@v1
- uses: actions/cache@v1
with:
path: tmp
key: tmp ${{ matrix.script }}
- shell: bash
run: |
git clone --branch v1.5.2 --depth 1 https://github.com/jasongin/nvs.git ~/.nvs

View File

@@ -1235,7 +1235,7 @@ merge(Compressor.prototype, {
var lvalues = get_lvalues(candidate);
var lhs_local = is_lhs_local(lhs);
if (!side_effects) side_effects = value_has_side_effects(candidate);
var replace_all = replace_all_symbols();
var replace_all = replace_all_symbols(candidate);
var may_throw = candidate.may_throw(compressor) ? in_try ? function(node) {
return node.has_side_effects(compressor);
} : side_effects_external : return_false;
@@ -1283,6 +1283,7 @@ merge(Compressor.prototype, {
}
branch.expression = branch.expression.transform(scanner);
if (!replace_all) break;
scan_rhs = false;
}
}
abort = true;
@@ -1312,6 +1313,7 @@ merge(Compressor.prototype, {
function in_conditional(node, parent) {
if (parent instanceof AST_Binary) return lazy_op[parent.operator] && parent.left !== node;
if (parent instanceof AST_Case) return parent.expression !== node;
if (parent instanceof AST_Conditional) return parent.condition !== node;
return parent instanceof AST_If && parent.condition !== node;
}
@@ -1477,19 +1479,43 @@ merge(Compressor.prototype, {
if (parent instanceof AST_Call) return node;
if (parent instanceof AST_Case) return node;
if (parent instanceof AST_Conditional) return node;
if (parent instanceof AST_Definitions) return find_stop(parent, level + 1);
if (parent instanceof AST_Definitions) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Exit) return node;
if (parent instanceof AST_If) return node;
if (parent instanceof AST_IterationStatement) return node;
if (parent instanceof AST_PropAccess) return node;
if (parent instanceof AST_Sequence) return find_stop(parent, level + 1);
if (parent instanceof AST_SimpleStatement) return find_stop(parent, level + 1);
if (parent instanceof AST_Sequence) {
return (parent.tail_node() === node ? find_stop : find_stop_unused)(parent, level + 1);
}
if (parent instanceof AST_SimpleStatement) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Switch) return node;
if (parent instanceof AST_Unary) return node;
if (parent instanceof AST_VarDef) return node;
return null;
}
function find_stop_unused(node, level) {
var parent = scanner.parent(level);
if (is_last_node(node, parent)) return node;
if (in_conditional(node, parent)) return node;
if (parent instanceof AST_Assign) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Binary) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Call) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Case) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Conditional) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Definitions) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Exit) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_If) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_IterationStatement) return node;
if (parent instanceof AST_PropAccess) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Sequence) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_SimpleStatement) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Switch) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Unary) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_VarDef) return find_stop_unused(parent, level + 1);
return null;
}
function mangleable_var(var_def) {
var value = var_def.value;
if (!(value instanceof AST_SymbolRef)) return;
@@ -1656,7 +1682,8 @@ merge(Compressor.prototype, {
return get_rvalue(expr).has_side_effects(compressor);
}
function replace_all_symbols() {
function replace_all_symbols(expr) {
if (expr instanceof AST_Unary) return false;
if (side_effects) return false;
if (value_def) return true;
if (lhs instanceof AST_SymbolRef) {
@@ -1671,7 +1698,7 @@ merge(Compressor.prototype, {
function symbol_in_lvalues(sym, parent) {
var lvalue = lvalues[sym.name];
if (!lvalue) return;
if (lvalue !== lhs) return !(parent instanceof AST_Call && parent.expression === sym);
if (lvalue !== lhs) return true;
scan_rhs = false;
}
@@ -2881,6 +2908,7 @@ merge(Compressor.prototype, {
if (!non_converting_binary[this.operator]) depth++;
var left = this.left._eval(compressor, cached, depth);
if (left === this.left) return this;
if (this.operator == (left ? "||" : "&&")) return left;
var right = this.right._eval(compressor, cached, depth);
if (right === this.right) return this;
var result;
@@ -2995,6 +3023,7 @@ merge(Compressor.prototype, {
],
};
convert_to_predicate(static_values);
var regexp_props = makePredicate("global ignoreCase multiline source");
def(AST_PropAccess, function(compressor, cached, depth) {
if (compressor.option("unsafe")) {
var key = this.property;
@@ -3010,9 +3039,12 @@ merge(Compressor.prototype, {
val = global_objs[exp.name];
} else {
val = exp._eval(compressor, cached, depth + 1);
if (!val || val === exp) return this;
if (typeof val == "object" && !HOP(val, key)) return this;
if (typeof val == "function") switch (key) {
if (val == null || val === exp) return this;
if (val instanceof RegExp) {
if (!regexp_props[key]) return this;
} else if (typeof val == "object") {
if (!HOP(val, key)) return this;
} else if (typeof val == "function") switch (key) {
case "name":
return val.node.name ? val.node.name.name : "";
case "length":
@@ -3045,7 +3077,10 @@ merge(Compressor.prototype, {
cached.push(node);
});
});
return stat.value ? stat.value._eval(compressor, cached, depth) : undefined;
if (!stat.value) return undefined;
var val = stat.value._eval(compressor, cached, depth);
if (val === stat.value) return this;
return val;
} else if (compressor.option("unsafe") && exp instanceof AST_PropAccess) {
var key = exp.property;
if (key instanceof AST_Node) {
@@ -3060,7 +3095,7 @@ merge(Compressor.prototype, {
val = global_objs[e.name];
} else {
val = e._eval(compressor, cached, depth + 1);
if (val === e || !val) return this;
if (val == null || val === e) return this;
var native_fn = native_fns[val.constructor.name];
if (!native_fn || !native_fn[key]) return this;
}
@@ -6508,23 +6543,30 @@ merge(Compressor.prototype, {
}
}
} else if (self.left instanceof AST_SymbolRef) {
if (self.left.is_immutable()) return strip_assignment();
var def = self.left.definition();
if (def.scope === compressor.find_parent(AST_Lambda)) {
if (self.left.is_immutable()) return strip_assignment();
var level = 0, node, parent = self;
do {
node = parent;
parent = compressor.parent(level++);
if (parent instanceof AST_Exit) {
if (in_try(level, parent)) break;
if (is_reachable(def.scope, [ def ])) break;
def.fixed = false;
return strip_assignment();
}
} while (parent instanceof AST_Binary && parent.right === node
|| parent instanceof AST_Sequence && parent.tail_node() === node
|| parent instanceof AST_UnaryPrefix);
}
var scope = def.scope.resolve();
var local = scope === compressor.find_parent(AST_Lambda);
var level = 0, node, parent = self;
do {
node = parent;
parent = compressor.parent(level++);
if (parent instanceof AST_Assign) {
if (!(parent.left instanceof AST_SymbolRef)) continue;
if (parent.left.definition() !== def) continue;
if (in_try(level, parent)) break;
def.fixed = false;
return strip_assignment();
} else if (parent instanceof AST_Exit) {
if (!local) break;
if (in_try(level, parent)) break;
if (is_reachable(scope, [ def ])) break;
def.fixed = false;
return strip_assignment();
}
} while (parent instanceof AST_Binary && parent.right === node
|| parent instanceof AST_Sequence && parent.tail_node() === node
|| parent instanceof AST_UnaryPrefix);
}
}
self = self.lift_sequences(compressor);
@@ -6564,9 +6606,9 @@ merge(Compressor.prototype, {
self.right = make_node(AST_Null, right);
var may_throw = node.may_throw(compressor);
self.right = right;
var scope = self.left.definition().scope;
var parent;
while ((parent = compressor.parent(level++)) !== scope) {
while (parent = compressor.parent(level++)) {
if (parent === scope) return false;
if (parent instanceof AST_Try) {
if (parent.bfinally) return true;
if (may_throw && parent.bcatch) return true;
@@ -6633,7 +6675,7 @@ merge(Compressor.prototype, {
&& alt_tail instanceof AST_Assign
&& seq_tail.operator == alt_tail.operator
&& seq_tail.left.equivalent_to(alt_tail.left)
&& (is_eq && !seq_tail.left.has_side_effects(compressor)
&& (is_eq && seq_tail.left instanceof AST_SymbolRef
|| !condition.has_side_effects(compressor)
&& can_shift_lhs_of_tail(consequent)
&& can_shift_lhs_of_tail(alternative))) {
@@ -6802,16 +6844,9 @@ merge(Compressor.prototype, {
}
function can_shift_lhs_of_tail(node) {
if (node === node.tail_node()) return true;
var exprs = node.expressions;
for (var i = exprs.length - 1; --i >= 0;) {
var expr = exprs[i];
if (!(expr instanceof AST_Assign) && expr.has_side_effects(compressor)
|| expr.operator != "="
|| expr.left.has_side_effects(compressor)
|| expr.right.has_side_effects(compressor)) return false;
}
return true;
return node === node.tail_node() || all(node.expressions.slice(0, -1), function(expr) {
return !expr.has_side_effects(compressor);
});
}
function pop_lhs(node) {

View File

@@ -3,7 +3,7 @@
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
"license": "BSD-2-Clause",
"version": "3.6.6",
"version": "3.6.9",
"engines": {
"node": ">=0.8.0"
},

View File

@@ -6348,3 +6348,123 @@ issue_3526_2: {
}
expect_stdout: "PASS"
}
issue_3562: {
options = {
collapse_vars: true,
conditionals: true,
sequences: true,
}
input: {
function f(a) {
console.log("PASS", a);
}
function g(b) {
console.log("FAIL", b);
}
var h;
var c;
if (console) {
h = f;
c = "PASS";
} else {
h = g;
c = "FAIL";
}
h(c);
}
expect: {
function f(a) {
console.log("PASS", a);
}
function g(b) {
console.log("FAIL", b);
}
var h;
var c;
c = console ? (h = f, "PASS") : (h = g, "FAIL"),
h(c);
}
expect_stdout: "PASS PASS"
}
dot_throw_assign_sequence: {
options = {
collapse_vars: true,
}
input: {
var a = "FAIL";
try {
var b;
b[0] = (a = "PASS", 0);
a = 1 + a;
} catch (c) {
}
console.log(a);
}
expect: {
var a = "FAIL";
try {
var b;
b[0] = (a = "PASS", 0);
a = 1 + a;
} catch (c) {
}
console.log(a);
}
expect_stdout: "PASS"
}
call_assign_order: {
options = {
collapse_vars: true,
}
input: {
var a, b = 1, c = 0, log = console.log;
(function() {
a = b = "PASS";
})((b = "FAIL", c++));
log(a, b);
}
expect: {
var a, b = 1, c = 0, log = console.log;
(function() {
a = b = "PASS";
})((b = "FAIL", c++));
log(a, b);
}
expect_stdout: "PASS PASS"
}
issue_3573: {
options = {
collapse_vars: true,
}
input: {
var c = 0;
(function(b) {
while (--b) {
b = NaN;
switch (0 / this < 0) {
case c++, false:
case c++, NaN:
}
}
})(3);
console.log(c);
}
expect: {
var c = 0;
(function(b) {
while (--b) {
b = NaN;
switch (0 / this < 0) {
case c++, false:
case c++, NaN:
}
}
})(3);
console.log(c);
}
expect_stdout: "1"
}

View File

@@ -1489,3 +1489,29 @@ angularjs_chain: {
}
}
}
issue_3576: {
options = {
conditionals: true,
evaluate: true,
pure_getters: "strict",
reduce_vars: true,
}
input: {
var c = "FAIL";
(function(a) {
(a = -1) ? (a && (a.a = 0)) : (a && (a.a = 0));
a && a[c = "PASS"]++;
})();
console.log(c);
}
expect: {
var c = "FAIL";
(function(a) {
a = -1, a, a.a = 0;
a, a[c = "PASS"]++;
})();
console.log(c);
}
expect_stdout: "PASS"
}

View File

@@ -1064,3 +1064,68 @@ issue_3552: {
}
expect_stdout: "PASS"
}
unreachable_assign: {
options = {
dead_code: true,
}
input: {
console.log(A = "P" + (A = "A" + (B = "S" + (A = B = "S"))), A, B);
}
expect: {
console.log(A = "P" + "A" + (B = "S" + "S"), A, B);
}
expect_stdout: "PASS PASS SS"
}
catch_return_assign: {
options = {
dead_code: true,
}
input: {
console.log(function() {
try {
throw "FAIL";
} catch (e) {
return e = "PASS";
}
}());
}
expect: {
console.log(function() {
try {
throw "FAIL";
} catch (e) {
return "PASS";
}
}());
}
expect_stdout: "PASS"
}
issue_3578: {
options = {
dead_code: true,
}
input: {
var a = "FAIL", b, c;
try {
b = c.p = b = 0;
} catch (e) {
b += 42;
b && (a = "PASS");
}
console.log(a);
}
expect: {
var a = "FAIL", b, c;
try {
b = c.p = b = 0;
} catch (e) {
b += 42;
b && (a = "PASS");
}
console.log(a);
}
expect_stdout: "PASS"
}

View File

@@ -237,22 +237,39 @@ unsafe_constant: {
unsafe: true,
}
input: {
console.log(
true.a,
false.a,
null.a,
undefined.a
);
console.log(true.a, false.a);
console.log(true.valueOf(), false.valueOf());
try {
console.log(null.a);
} catch (e) {
console.log("PASS");
}
try {
console.log(undefined.a);
} catch (e) {
console.log("PASS");
}
}
expect: {
console.log(
void 0,
false.a,
null.a,
(void 0).a
);
console.log(void 0, void 0);
console.log(true, false);
try {
console.log(null.a);
} catch (e) {
console.log("PASS");
}
try {
console.log((void 0).a);
} catch (e) {
console.log("PASS");
}
}
expect_stdout: true
expect_stdout: [
"undefined undefined",
"true false",
"PASS",
"PASS",
]
}
unsafe_object: {
@@ -1859,3 +1876,27 @@ issue_3558: {
}
expect_stdout: "1 0"
}
issue_3568: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
unsafe: true,
}
input: {
var a = 0;
function f(b) {
return b && b.p;
}
console.log(f(++a + f()));
}
expect: {
var a = 0;
function f(b) {
return b && b.p;
}
console.log(NaN);
}
expect_stdout: "NaN"
}

View File

@@ -3370,3 +3370,33 @@ issue_3512: {
}
expect_stdout: "PASS"
}
issue_3562: {
options = {
collapse_vars: true,
reduce_vars: true,
}
input: {
var a = "PASS";
function f(b) {
f = function() {
console.log(b);
};
return "FAIL";
}
a = f(a);
f(a);
}
expect: {
var a = "PASS";
function f(b) {
f = function() {
console.log(b);
};
return "FAIL";
}
a = f(a);
f(a);
}
expect_stdout: "PASS"
}

View File

@@ -36,6 +36,20 @@ regexp_2: {
expect_stdout: '["PASS","pass"]'
}
regexp_properties: {
options = {
evaluate: true,
unsafe: true,
}
input: {
console.log(/abc/g.source, /abc/g.global, /abc/g.ignoreCase, /abc/g.lastIndex, /abc/g.multiline);
}
expect: {
console.log("abc", true, false, /abc/g.lastIndex, false);
}
expect_stdout: "abc true false 0 false"
}
issue_3434_1: {
options = {
evaluate: true,