Starting destructuring.

This commit is contained in:
Fábio Santos
2015-01-15 03:03:38 +00:00
committed by Richard van Velzen
parent 252fc65558
commit 32f76f7ff8
7 changed files with 317 additions and 41 deletions

View File

@@ -231,6 +231,7 @@ merge(Compressor.prototype, {
}
function make_arguments_names_list(func) {
return func.argnames.map(function(sym){
// TODO not sure what to do here with destructuring
return make_node(AST_String, sym, { value: sym.name });
});
}
@@ -1089,17 +1090,26 @@ merge(Compressor.prototype, {
if (node instanceof AST_Lambda && !(node instanceof AST_Accessor)) {
if (compressor.option("unsafe") && !compressor.option("keep_fargs")) {
for (var a = node.argnames, i = a.length; --i >= 0;) {
var sym = a[i];
if (sym.unreferenced()) {
a.pop();
compressor.warn("Dropping unused function argument {name} [{file}:{line},{col}]", {
name : sym.name,
file : sym.start.file,
line : sym.start.line,
col : sym.start.col
});
if (a[i] instanceof AST_Destructuring) {
// Do not drop destructuring arguments.
// They constitute a type assertion, so dropping
// them would stop that TypeError which would happen
// if someone called it with an incorrectly formatted
// parameter.
break;
} else {
var sym = a[i];
if (sym.unreferenced()) {
a.pop();
compressor.warn("Dropping unused function argument {name} [{file}:{line},{col}]", {
name : sym.name,
file : sym.start.file,
line : sym.start.line,
col : sym.start.col
});
}
else break;
}
else break;
}
}
}
@@ -1263,9 +1273,10 @@ merge(Compressor.prototype, {
// collect only vars which don't show up in self's arguments list
var defs = [];
vars.each(function(def, name){
// TODO test this too
if (self instanceof AST_Lambda
&& find_if(function(x){ return x.name == def.name.name },
self.argnames)) {
self.args_as_names())) {
vars.del(name);
} else {
def = def.clone();
@@ -1785,6 +1796,7 @@ merge(Compressor.prototype, {
if (ex !== ast) throw ex;
};
if (!fun) return self;
// TODO does this work with destructuring? Test it.
var args = fun.argnames.map(function(arg, i){
return make_node(AST_String, self.args[i], {
value: arg.print_to_string()