Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61a0dad9fe | ||
|
|
3e2c51a4da | ||
|
|
0e29ad5eb9 | ||
|
|
0f2687ecfc | ||
|
|
1c0defdc03 | ||
|
|
dcbf2236c7 | ||
|
|
24bb288832 |
@@ -1235,7 +1235,7 @@ merge(Compressor.prototype, {
|
|||||||
var lvalues = get_lvalues(candidate);
|
var lvalues = get_lvalues(candidate);
|
||||||
var lhs_local = is_lhs_local(lhs);
|
var lhs_local = is_lhs_local(lhs);
|
||||||
if (!side_effects) side_effects = value_has_side_effects(candidate);
|
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) {
|
var may_throw = candidate.may_throw(compressor) ? in_try ? function(node) {
|
||||||
return node.has_side_effects(compressor);
|
return node.has_side_effects(compressor);
|
||||||
} : side_effects_external : return_false;
|
} : side_effects_external : return_false;
|
||||||
@@ -1312,6 +1312,7 @@ merge(Compressor.prototype, {
|
|||||||
|
|
||||||
function in_conditional(node, parent) {
|
function in_conditional(node, parent) {
|
||||||
if (parent instanceof AST_Binary) return lazy_op[parent.operator] && parent.left !== node;
|
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;
|
if (parent instanceof AST_Conditional) return parent.condition !== node;
|
||||||
return parent instanceof AST_If && parent.condition !== node;
|
return parent instanceof AST_If && parent.condition !== node;
|
||||||
}
|
}
|
||||||
@@ -1477,19 +1478,43 @@ merge(Compressor.prototype, {
|
|||||||
if (parent instanceof AST_Call) return node;
|
if (parent instanceof AST_Call) return node;
|
||||||
if (parent instanceof AST_Case) return node;
|
if (parent instanceof AST_Case) return node;
|
||||||
if (parent instanceof AST_Conditional) 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_Exit) return node;
|
||||||
if (parent instanceof AST_If) return node;
|
if (parent instanceof AST_If) return node;
|
||||||
if (parent instanceof AST_IterationStatement) return node;
|
if (parent instanceof AST_IterationStatement) return node;
|
||||||
if (parent instanceof AST_PropAccess) return node;
|
if (parent instanceof AST_PropAccess) return node;
|
||||||
if (parent instanceof AST_Sequence) return find_stop(parent, level + 1);
|
if (parent instanceof AST_Sequence) {
|
||||||
if (parent instanceof AST_SimpleStatement) return find_stop(parent, level + 1);
|
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_Switch) return node;
|
||||||
if (parent instanceof AST_Unary) return node;
|
if (parent instanceof AST_Unary) return node;
|
||||||
if (parent instanceof AST_VarDef) return node;
|
if (parent instanceof AST_VarDef) return node;
|
||||||
return null;
|
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) {
|
function mangleable_var(var_def) {
|
||||||
var value = var_def.value;
|
var value = var_def.value;
|
||||||
if (!(value instanceof AST_SymbolRef)) return;
|
if (!(value instanceof AST_SymbolRef)) return;
|
||||||
@@ -1656,7 +1681,8 @@ merge(Compressor.prototype, {
|
|||||||
return get_rvalue(expr).has_side_effects(compressor);
|
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 (side_effects) return false;
|
||||||
if (value_def) return true;
|
if (value_def) return true;
|
||||||
if (lhs instanceof AST_SymbolRef) {
|
if (lhs instanceof AST_SymbolRef) {
|
||||||
@@ -1671,7 +1697,7 @@ merge(Compressor.prototype, {
|
|||||||
function symbol_in_lvalues(sym, parent) {
|
function symbol_in_lvalues(sym, parent) {
|
||||||
var lvalue = lvalues[sym.name];
|
var lvalue = lvalues[sym.name];
|
||||||
if (!lvalue) return;
|
if (!lvalue) return;
|
||||||
if (lvalue !== lhs) return !(parent instanceof AST_Call && parent.expression === sym);
|
if (lvalue !== lhs) return true;
|
||||||
scan_rhs = false;
|
scan_rhs = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2881,6 +2907,7 @@ merge(Compressor.prototype, {
|
|||||||
if (!non_converting_binary[this.operator]) depth++;
|
if (!non_converting_binary[this.operator]) depth++;
|
||||||
var left = this.left._eval(compressor, cached, depth);
|
var left = this.left._eval(compressor, cached, depth);
|
||||||
if (left === this.left) return this;
|
if (left === this.left) return this;
|
||||||
|
if (this.operator == (left ? "||" : "&&")) return left;
|
||||||
var right = this.right._eval(compressor, cached, depth);
|
var right = this.right._eval(compressor, cached, depth);
|
||||||
if (right === this.right) return this;
|
if (right === this.right) return this;
|
||||||
var result;
|
var result;
|
||||||
@@ -2995,6 +3022,7 @@ merge(Compressor.prototype, {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
convert_to_predicate(static_values);
|
convert_to_predicate(static_values);
|
||||||
|
var regexp_props = makePredicate("global ignoreCase multiline source");
|
||||||
def(AST_PropAccess, function(compressor, cached, depth) {
|
def(AST_PropAccess, function(compressor, cached, depth) {
|
||||||
if (compressor.option("unsafe")) {
|
if (compressor.option("unsafe")) {
|
||||||
var key = this.property;
|
var key = this.property;
|
||||||
@@ -3010,9 +3038,12 @@ merge(Compressor.prototype, {
|
|||||||
val = global_objs[exp.name];
|
val = global_objs[exp.name];
|
||||||
} else {
|
} else {
|
||||||
val = exp._eval(compressor, cached, depth + 1);
|
val = exp._eval(compressor, cached, depth + 1);
|
||||||
if (!val || val === exp) return this;
|
if (val == null || val === exp) return this;
|
||||||
if (typeof val == "object" && !HOP(val, key)) return this;
|
if (val instanceof RegExp) {
|
||||||
if (typeof val == "function") switch (key) {
|
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":
|
case "name":
|
||||||
return val.node.name ? val.node.name.name : "";
|
return val.node.name ? val.node.name.name : "";
|
||||||
case "length":
|
case "length":
|
||||||
@@ -3045,7 +3076,10 @@ merge(Compressor.prototype, {
|
|||||||
cached.push(node);
|
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) {
|
} else if (compressor.option("unsafe") && exp instanceof AST_PropAccess) {
|
||||||
var key = exp.property;
|
var key = exp.property;
|
||||||
if (key instanceof AST_Node) {
|
if (key instanceof AST_Node) {
|
||||||
@@ -3060,7 +3094,7 @@ merge(Compressor.prototype, {
|
|||||||
val = global_objs[e.name];
|
val = global_objs[e.name];
|
||||||
} else {
|
} else {
|
||||||
val = e._eval(compressor, cached, depth + 1);
|
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];
|
var native_fn = native_fns[val.constructor.name];
|
||||||
if (!native_fn || !native_fn[key]) return this;
|
if (!native_fn || !native_fn[key]) return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
|
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
|
||||||
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
|
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
|
||||||
"license": "BSD-2-Clause",
|
"license": "BSD-2-Clause",
|
||||||
"version": "3.6.6",
|
"version": "3.6.8",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=0.8.0"
|
"node": ">=0.8.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6348,3 +6348,90 @@ issue_3526_2: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "PASS"
|
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"
|
||||||
|
}
|
||||||
|
|||||||
@@ -237,22 +237,39 @@ unsafe_constant: {
|
|||||||
unsafe: true,
|
unsafe: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
console.log(
|
console.log(true.a, false.a);
|
||||||
true.a,
|
console.log(true.valueOf(), false.valueOf());
|
||||||
false.a,
|
try {
|
||||||
null.a,
|
console.log(null.a);
|
||||||
undefined.a
|
} catch (e) {
|
||||||
);
|
console.log("PASS");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
console.log(undefined.a);
|
||||||
|
} catch (e) {
|
||||||
|
console.log("PASS");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
console.log(
|
console.log(void 0, void 0);
|
||||||
void 0,
|
console.log(true, false);
|
||||||
false.a,
|
try {
|
||||||
null.a,
|
console.log(null.a);
|
||||||
(void 0).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: {
|
unsafe_object: {
|
||||||
@@ -1859,3 +1876,27 @@ issue_3558: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "1 0"
|
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"
|
||||||
|
}
|
||||||
|
|||||||
@@ -3370,3 +3370,33 @@ issue_3512: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "PASS"
|
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"
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,6 +36,20 @@ regexp_2: {
|
|||||||
expect_stdout: '["PASS","pass"]'
|
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: {
|
issue_3434_1: {
|
||||||
options = {
|
options = {
|
||||||
evaluate: true,
|
evaluate: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user