fix unused crashes (#1599)
- `AST_DefaultAssign` on `keep_fargs` - `AST_Expansion on` `keep_fargs` - `AST_Destructuring` on top-level declarations without `toplevel`
This commit is contained in:
@@ -814,9 +814,6 @@ var AST_VarDef = DEFNODE("VarDef", "name value", {
|
||||
name: "[AST_SymbolVar|AST_SymbolConst|AST_Destructuring] name of the variable",
|
||||
value: "[AST_Node?] initializer, or null of there's no initializer"
|
||||
},
|
||||
is_destructuring: function() {
|
||||
return this.name instanceof AST_Destructuring;
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function(){
|
||||
this.name._walk(visitor);
|
||||
|
||||
@@ -1789,14 +1789,18 @@ merge(Compressor.prototype, {
|
||||
if (node instanceof AST_Definitions && scope === self) {
|
||||
node.definitions.forEach(function(def){
|
||||
if (!drop_vars) {
|
||||
var node_def = def.name.definition();
|
||||
if (node_def.global && !(node_def.id in in_use_ids)) {
|
||||
in_use_ids[node_def.id] = true;
|
||||
in_use.push(node_def);
|
||||
}
|
||||
def.name.walk(new TreeWalker(function(node) {
|
||||
if (node instanceof AST_SymbolDeclaration) {
|
||||
var def = node.definition();
|
||||
if (def.global && !(def.id in in_use_ids)) {
|
||||
in_use_ids[def.id] = true;
|
||||
in_use.push(def);
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
if (def.value) {
|
||||
if (def.is_destructuring()) {
|
||||
if (def.name instanceof AST_Destructuring) {
|
||||
var destructuring_cache = destructuring_value;
|
||||
destructuring_value = def.value;
|
||||
in_definition = true;
|
||||
@@ -1907,7 +1911,10 @@ merge(Compressor.prototype, {
|
||||
for (var a = node.argnames, i = a.length; --i >= 0;) {
|
||||
var sym = a[i];
|
||||
if (sym instanceof AST_Expansion) {
|
||||
sym = sym.symbol;
|
||||
sym = sym.expression;
|
||||
}
|
||||
if (sym instanceof AST_DefaultAssign) {
|
||||
sym = sym.left;
|
||||
}
|
||||
// Do not drop destructuring arguments.
|
||||
// They constitute a type assertion, so dropping
|
||||
@@ -1947,7 +1954,7 @@ merge(Compressor.prototype, {
|
||||
if (node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn)) {
|
||||
var def = node.definitions.filter(function(def){
|
||||
if (def.value) def.value = def.value.transform(tt);
|
||||
if (def.is_destructuring()) return true;
|
||||
if (def.name instanceof AST_Destructuring) return true;
|
||||
if (def.name.definition().id in in_use_ids) return true;
|
||||
if (!drop_vars && def.name.definition().global) return true;
|
||||
|
||||
@@ -2090,7 +2097,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
if (node instanceof AST_Var && hoist_vars) {
|
||||
node.definitions.forEach(function(def){
|
||||
if (def.is_destructuring()) { return; }
|
||||
if (def.name instanceof AST_Destructuring) return;
|
||||
vars.set(def.name.name, def);
|
||||
++vars_found;
|
||||
});
|
||||
@@ -2698,7 +2705,7 @@ merge(Compressor.prototype, {
|
||||
|
||||
AST_Definitions.DEFMETHOD("to_assignments", function(){
|
||||
var assignments = this.definitions.reduce(function(a, def){
|
||||
if (def.value && !def.is_destructuring()) {
|
||||
if (def.value && !(def.name instanceof AST_Destructuring)) {
|
||||
var name = make_node(AST_SymbolRef, def.name, def.name);
|
||||
a.push(make_node(AST_Assign, def, {
|
||||
operator : "=",
|
||||
|
||||
@@ -293,3 +293,17 @@ reduce_vars: {
|
||||
for ([x,y] in pairs);
|
||||
}
|
||||
}
|
||||
|
||||
unused: {
|
||||
options = {
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
let { foo: [, , ...a] } = { foo: [1, 2, 3, 4], bar: 5 };
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
let { foo: [, , ...a] } = { foo: [1, 2, 3, 4], bar: 5 };
|
||||
console.log(a);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,3 +286,37 @@ fat_arrow_as_param: {
|
||||
}
|
||||
expect_exact: "foo(x=>x);foo(x=>x,y=>y);foo(x=>(x,x));foo(x=>(x,x),y=>(y,y));"
|
||||
}
|
||||
|
||||
default_assign: {
|
||||
options = {
|
||||
keep_fargs: false,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f(a, b = 3) {
|
||||
console.log(a);
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
function f(a) {
|
||||
console.log(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expansion: {
|
||||
options = {
|
||||
keep_fargs: false,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f(a, ...b) {
|
||||
console.log(a);
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
function f(a) {
|
||||
console.log(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user