Fix compression with unused containing destructuring
This commit is contained in:
committed by
Richard van Velzen
parent
abbeb266b5
commit
52ce9a333c
@@ -1384,6 +1384,8 @@ merge(Compressor.prototype, {
|
||||
var in_use = [];
|
||||
var in_use_ids = {}; // avoid expensive linear scans of in_use
|
||||
var initializations = new Dictionary();
|
||||
var destructuring_value = null;
|
||||
var in_definition = false;
|
||||
// pass 1: find out which symbols are directly used in
|
||||
// this scope (not in nested scopes).
|
||||
var scope = this;
|
||||
@@ -1395,9 +1397,17 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
if (node instanceof AST_Definitions && scope === self) {
|
||||
node.definitions.forEach(function(def){
|
||||
if (def.is_destructuring()) return; /* Destructurings are type assertions! */
|
||||
if (def.value) {
|
||||
initializations.add(def.name.name, def.value);
|
||||
if (def.is_destructuring()) {
|
||||
var destructuring_cache = destructuring_value;
|
||||
destructuring_value = def.value;
|
||||
in_definition = true;
|
||||
def.walk(tw);
|
||||
in_definition = false;
|
||||
destructuring_value = destructuring_cache;
|
||||
} else {
|
||||
initializations.add(def.name.name, def.value);
|
||||
}
|
||||
if (def.value.has_side_effects(compressor)) {
|
||||
def.value.walk(tw);
|
||||
}
|
||||
@@ -1420,6 +1430,39 @@ merge(Compressor.prototype, {
|
||||
scope = save_scope;
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Destructuring) {
|
||||
if (!in_definition) {
|
||||
return true;
|
||||
}
|
||||
for (var i = 0; i < node.names.length; i++) {
|
||||
if (node.names[i] instanceof AST_Destructuring) {
|
||||
node.names[i].walk(tw);
|
||||
}
|
||||
else if (node.names[i] instanceof AST_Expansion) {
|
||||
if (node.names[i].expression instanceof AST_Symbol) {
|
||||
initializations.add(node.names[i].expression.name, destructuring_value);
|
||||
} else {
|
||||
throw new Error(string_template("Can't handle expansion of type: {type}", {
|
||||
type: Object.getPrototypeOf(node.names[i].expression).TYPE
|
||||
}));
|
||||
}
|
||||
}
|
||||
else if (node.names[i] instanceof AST_Hole) {
|
||||
continue;
|
||||
}
|
||||
else if (node.names[i] instanceof AST_ObjectKeyVal && typeof node.names[i].key === "string") {
|
||||
initializations.add(node.names[i].key, destructuring_value);
|
||||
}
|
||||
else if (node.names[i] instanceof AST_Symbol) {
|
||||
initializations.add(node.names[i].name, destructuring_value);
|
||||
} else {
|
||||
throw new Error(string_template("Unknown destructuring element of type: {type}", {
|
||||
type: Object.getPrototypeOf(node.names[i]).TYPE
|
||||
}));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
self.walk(tw);
|
||||
|
||||
Reference in New Issue
Block a user