@@ -151,7 +151,7 @@ merge(Compressor.prototype, {
|
|||||||
var last_count = 1 / 0;
|
var last_count = 1 / 0;
|
||||||
for (var pass = 0; pass < passes; pass++) {
|
for (var pass = 0; pass < passes; pass++) {
|
||||||
if (pass > 0 || this.option("reduce_vars"))
|
if (pass > 0 || this.option("reduce_vars"))
|
||||||
node.reset_opt_flags(this, true);
|
node.reset_opt_flags(this);
|
||||||
node = node.transform(this);
|
node = node.transform(this);
|
||||||
if (passes > 1) {
|
if (passes > 1) {
|
||||||
var count = 0;
|
var count = 0;
|
||||||
@@ -283,8 +283,9 @@ merge(Compressor.prototype, {
|
|||||||
self.transform(tt);
|
self.transform(tt);
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan) {
|
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor) {
|
||||||
var reduce_vars = rescan && compressor.option("reduce_vars");
|
var reduce_vars = compressor.option("reduce_vars");
|
||||||
|
var unused = compressor.option("unused");
|
||||||
// Stack of look-up tables to keep track of whether a `SymbolDef` has been
|
// Stack of look-up tables to keep track of whether a `SymbolDef` has been
|
||||||
// properly assigned before use:
|
// properly assigned before use:
|
||||||
// - `push()` & `pop()` when visiting conditional branches
|
// - `push()` & `pop()` when visiting conditional branches
|
||||||
@@ -308,9 +309,23 @@ merge(Compressor.prototype, {
|
|||||||
if (node instanceof AST_SymbolRef) {
|
if (node instanceof AST_SymbolRef) {
|
||||||
var d = node.definition();
|
var d = node.definition();
|
||||||
d.references.push(node);
|
d.references.push(node);
|
||||||
if (d.fixed === undefined || !safe_to_read(d)
|
if (d.fixed === undefined || !safe_to_read(d) || d.single_use == "m") {
|
||||||
|| is_modified(node, 0, is_immutable(node))) {
|
|
||||||
d.fixed = false;
|
d.fixed = false;
|
||||||
|
} else {
|
||||||
|
var value = node.fixed_value();
|
||||||
|
if (unused) {
|
||||||
|
d.single_use = value
|
||||||
|
&& d.references.length == 1
|
||||||
|
&& loop_ids[d.id] === in_loop
|
||||||
|
&& d.scope === node.scope
|
||||||
|
&& value.is_constant_expression();
|
||||||
|
}
|
||||||
|
if (is_modified(node, 0, is_immutable(value))) {
|
||||||
|
if (d.single_use) {
|
||||||
|
d.single_use = "m";
|
||||||
|
} else {
|
||||||
|
d.fixed = false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
var parent = tw.parent();
|
var parent = tw.parent();
|
||||||
if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right
|
if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right
|
||||||
@@ -321,6 +336,7 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (node instanceof AST_SymbolCatch) {
|
if (node instanceof AST_SymbolCatch) {
|
||||||
node.definition().fixed = false;
|
node.definition().fixed = false;
|
||||||
}
|
}
|
||||||
@@ -550,20 +566,8 @@ merge(Compressor.prototype, {
|
|||||||
def.single_use = undefined;
|
def.single_use = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_immutable(node) {
|
function is_immutable(value) {
|
||||||
var value = node.fixed_value();
|
return value && (value.is_constant() || value instanceof AST_Lambda);
|
||||||
if (!value) return false;
|
|
||||||
if (value.is_constant()) return true;
|
|
||||||
if (compressor.option("unused")) {
|
|
||||||
var d = node.definition();
|
|
||||||
if (d.single_use === undefined) {
|
|
||||||
d.single_use = loop_ids[d.id] === in_loop
|
|
||||||
&& d.scope === node.scope
|
|
||||||
&& value.is_constant_expression();
|
|
||||||
}
|
|
||||||
if (d.references.length == 1 && d.single_use) return true;
|
|
||||||
}
|
|
||||||
return value instanceof AST_Lambda;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_modified(node, level, immutable) {
|
function is_modified(node, level, immutable) {
|
||||||
@@ -4107,6 +4111,7 @@ merge(Compressor.prototype, {
|
|||||||
d.fixed = fixed = make_node(AST_Function, fixed, fixed);
|
d.fixed = fixed = make_node(AST_Function, fixed, fixed);
|
||||||
}
|
}
|
||||||
if (compressor.option("unused")
|
if (compressor.option("unused")
|
||||||
|
&& fixed
|
||||||
&& d.references.length == 1
|
&& d.references.length == 1
|
||||||
&& (d.single_use || fixed instanceof AST_Function
|
&& (d.single_use || fixed instanceof AST_Function
|
||||||
&& !(d.scope.uses_arguments && d.orig[0] instanceof AST_SymbolFunarg)
|
&& !(d.scope.uses_arguments && d.orig[0] instanceof AST_SymbolFunarg)
|
||||||
|
|||||||
@@ -1190,10 +1190,10 @@ defun_label: {
|
|||||||
!function() {
|
!function() {
|
||||||
console.log(function(a) {
|
console.log(function(a) {
|
||||||
L: {
|
L: {
|
||||||
if (a) break L;
|
if (2) break L;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}(2));
|
}());
|
||||||
}();
|
}();
|
||||||
}
|
}
|
||||||
expect_stdout: true
|
expect_stdout: true
|
||||||
@@ -2894,3 +2894,63 @@ array_forin_2: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "3"
|
expect_stdout: "3"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const_expr_1: {
|
||||||
|
options = {
|
||||||
|
evaluate: true,
|
||||||
|
reduce_vars: true,
|
||||||
|
toplevel: true,
|
||||||
|
unsafe: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var o = {
|
||||||
|
a: 1,
|
||||||
|
b: 2
|
||||||
|
};
|
||||||
|
o.a++;
|
||||||
|
console.log(o.a, o.b);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var o = {
|
||||||
|
a: 1,
|
||||||
|
b: 2
|
||||||
|
};
|
||||||
|
o.a++;
|
||||||
|
console.log(o.a, o.b);
|
||||||
|
}
|
||||||
|
expect_stdout: "2 2"
|
||||||
|
}
|
||||||
|
|
||||||
|
const_expr_2: {
|
||||||
|
options = {
|
||||||
|
evaluate: true,
|
||||||
|
reduce_vars: true,
|
||||||
|
toplevel: true,
|
||||||
|
unsafe: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
Object.prototype.c = function() {
|
||||||
|
this.a++;
|
||||||
|
};
|
||||||
|
var o = {
|
||||||
|
a: 1,
|
||||||
|
b: 2
|
||||||
|
};
|
||||||
|
o.c();
|
||||||
|
console.log(o.a, o.b);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
Object.prototype.c = function() {
|
||||||
|
this.a++;
|
||||||
|
};
|
||||||
|
var o = {
|
||||||
|
a: 1,
|
||||||
|
b: 2
|
||||||
|
};
|
||||||
|
o.c();
|
||||||
|
console.log(o.a, o.b);
|
||||||
|
}
|
||||||
|
expect_stdout: "2 2"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user