enhance unused (#4090)
This commit is contained in:
@@ -4312,6 +4312,7 @@ merge(Compressor.prototype, {
|
|||||||
return sym;
|
return sym;
|
||||||
};
|
};
|
||||||
var assign_in_use = Object.create(null);
|
var assign_in_use = Object.create(null);
|
||||||
|
var for_ins = Object.create(null);
|
||||||
var in_use = [];
|
var in_use = [];
|
||||||
var in_use_ids = Object.create(null); // avoid expensive linear scans of in_use
|
var in_use_ids = Object.create(null); // avoid expensive linear scans of in_use
|
||||||
var value_read = Object.create(null);
|
var value_read = Object.create(null);
|
||||||
@@ -4602,23 +4603,37 @@ merge(Compressor.prototype, {
|
|||||||
return !def || fn.name && def === fn.name.definition();
|
return !def || fn.name && def === fn.name.definition();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (head.length == 0 && tail.length == duplicated) {
|
switch (head.length) {
|
||||||
[].unshift.apply(side_effects, tail.map(function(def) {
|
case 0:
|
||||||
AST_Node.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
|
if (tail.length == 0) break;
|
||||||
var sym = def.name.definition();
|
if (tail.length == duplicated) {
|
||||||
var ref = make_node(AST_SymbolRef, def.name, def.name);
|
[].unshift.apply(side_effects, tail.map(function(def) {
|
||||||
sym.references.push(ref);
|
AST_Node.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
|
||||||
var assign = make_node(AST_Assign, def, {
|
var sym = def.name.definition();
|
||||||
operator: "=",
|
var ref = make_node(AST_SymbolRef, def.name, def.name);
|
||||||
left: ref,
|
sym.references.push(ref);
|
||||||
right: def.value
|
var assign = make_node(AST_Assign, def, {
|
||||||
});
|
operator: "=",
|
||||||
var index = indexOf_assign(sym, def);
|
left: ref,
|
||||||
if (index >= 0) assign_in_use[sym.id][index] = assign;
|
right: def.value
|
||||||
sym.eliminated++;
|
});
|
||||||
return assign;
|
var index = indexOf_assign(sym, def);
|
||||||
}));
|
if (index >= 0) assign_in_use[sym.id][index] = assign;
|
||||||
} else if (head.length > 0 || tail.length > 0) {
|
sym.eliminated++;
|
||||||
|
return assign;
|
||||||
|
}));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (tail.length == 0) {
|
||||||
|
var id = head[0].name.definition().id;
|
||||||
|
if (id in for_ins) {
|
||||||
|
node.definitions = head;
|
||||||
|
for_ins[id].init = node;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
node.definitions = head.concat(tail);
|
node.definitions = head.concat(tail);
|
||||||
body.push(node);
|
body.push(node);
|
||||||
}
|
}
|
||||||
@@ -4814,6 +4829,10 @@ merge(Compressor.prototype, {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_ForIn) {
|
if (node instanceof AST_ForIn) {
|
||||||
|
if (node.init instanceof AST_SymbolRef && scope === self) {
|
||||||
|
var id = node.init.definition().id;
|
||||||
|
if (!(id in for_ins)) for_ins[id] = node;
|
||||||
|
}
|
||||||
if (!drop_vars || !compressor.option("loops")) return;
|
if (!drop_vars || !compressor.option("loops")) return;
|
||||||
if (!is_empty(node.body)) return;
|
if (!is_empty(node.body)) return;
|
||||||
if (node.init.has_side_effects(compressor)) return;
|
if (node.init.has_side_effects(compressor)) return;
|
||||||
|
|||||||
@@ -2848,3 +2848,60 @@ issue_4025: {
|
|||||||
"1 1 1",
|
"1 1 1",
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forin_var_1: {
|
||||||
|
options = {
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var k;
|
||||||
|
for (k in [ 1, 2 ])
|
||||||
|
console.log(k);
|
||||||
|
for (k in { PASS: 3 })
|
||||||
|
console.log(k);
|
||||||
|
console.log(k);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
for (var k in [ 1, 2 ])
|
||||||
|
console.log(k);
|
||||||
|
for (k in { PASS: 3 })
|
||||||
|
console.log(k);
|
||||||
|
console.log(k);
|
||||||
|
}
|
||||||
|
expect_stdout: [
|
||||||
|
"0",
|
||||||
|
"1",
|
||||||
|
"PASS",
|
||||||
|
"PASS",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
forin_var_2: {
|
||||||
|
options = {
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
console.log(function() {
|
||||||
|
switch (0) {
|
||||||
|
case function() {
|
||||||
|
for (a in 0);
|
||||||
|
}:
|
||||||
|
var b = 0;
|
||||||
|
}
|
||||||
|
for (var c = 0; a;);
|
||||||
|
var a;
|
||||||
|
}());
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
console.log(function() {
|
||||||
|
switch (0) {
|
||||||
|
case function() {
|
||||||
|
for (a in 0);
|
||||||
|
}:
|
||||||
|
}
|
||||||
|
for (; a;);
|
||||||
|
var a;
|
||||||
|
}());
|
||||||
|
}
|
||||||
|
expect_stdout: "undefined"
|
||||||
|
}
|
||||||
|
|||||||
@@ -1483,8 +1483,7 @@ issue_2663_2: {
|
|||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
(function() {
|
(function() {
|
||||||
var i;
|
for (var i in { a: 1, b: 2, c: 3 })
|
||||||
for (i in { a: 1, b: 2, c: 3 })
|
|
||||||
j = i, console.log(j);
|
j = i, console.log(j);
|
||||||
var j;
|
var j;
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user