Destructuring parameters with defaults. function x({ foo, bar } = {}) { }
This commit is contained in:
16
lib/ast.js
16
lib/ast.js
@@ -384,7 +384,7 @@ var AST_ArrowParametersOrSeq = DEFNODE("ArrowParametersOrSeq", "expressions", {
|
|||||||
as_params: function (croak) {
|
as_params: function (croak) {
|
||||||
// We don't want anything which doesn't belong in a destructuring
|
// We don't want anything which doesn't belong in a destructuring
|
||||||
var root = this;
|
var root = this;
|
||||||
return this.expressions.map(function to_fun_args(ex) {
|
return this.expressions.map(function to_fun_args(ex, _, __, default_seen_above) {
|
||||||
if (ex instanceof AST_Object) {
|
if (ex instanceof AST_Object) {
|
||||||
if (ex.properties.length == 0)
|
if (ex.properties.length == 0)
|
||||||
croak("Invalid destructuring function parameter", ex.start.line, ex.start.col);
|
croak("Invalid destructuring function parameter", ex.start.line, ex.start.col);
|
||||||
@@ -392,6 +392,7 @@ var AST_ArrowParametersOrSeq = DEFNODE("ArrowParametersOrSeq", "expressions", {
|
|||||||
start: ex.start,
|
start: ex.start,
|
||||||
end: ex.end,
|
end: ex.end,
|
||||||
is_array: false,
|
is_array: false,
|
||||||
|
default: default_seen_above,
|
||||||
names: ex.properties.map(to_fun_args)
|
names: ex.properties.map(to_fun_args)
|
||||||
});
|
});
|
||||||
} else if (ex instanceof AST_ObjectSymbol) {
|
} else if (ex instanceof AST_ObjectSymbol) {
|
||||||
@@ -408,6 +409,7 @@ var AST_ArrowParametersOrSeq = DEFNODE("ArrowParametersOrSeq", "expressions", {
|
|||||||
} else if (ex instanceof AST_SymbolRef) {
|
} else if (ex instanceof AST_SymbolRef) {
|
||||||
return new AST_SymbolFunarg({
|
return new AST_SymbolFunarg({
|
||||||
name: ex.name,
|
name: ex.name,
|
||||||
|
default: default_seen_above,
|
||||||
start: ex.start,
|
start: ex.start,
|
||||||
end: ex.end
|
end: ex.end
|
||||||
});
|
});
|
||||||
@@ -420,15 +422,11 @@ var AST_ArrowParametersOrSeq = DEFNODE("ArrowParametersOrSeq", "expressions", {
|
|||||||
start: ex.start,
|
start: ex.start,
|
||||||
end: ex.end,
|
end: ex.end,
|
||||||
is_array: true,
|
is_array: true,
|
||||||
|
default: default_seen_above,
|
||||||
names: ex.elements.map(to_fun_args)
|
names: ex.elements.map(to_fun_args)
|
||||||
});
|
});
|
||||||
} else if (ex instanceof AST_Assign && ex.left instanceof AST_Symbol) {
|
} else if (ex instanceof AST_Assign) {
|
||||||
return new AST_SymbolFunarg({
|
return to_fun_args(ex.left, undefined, undefined, ex.right);
|
||||||
name: ex.left.name,
|
|
||||||
default: ex.right,
|
|
||||||
start: ex.start,
|
|
||||||
end: ex.end
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
croak("Invalid function parameter", ex.start.line, ex.start.col);
|
croak("Invalid function parameter", ex.start.line, ex.start.col);
|
||||||
}
|
}
|
||||||
@@ -489,7 +487,7 @@ var AST_Defun = DEFNODE("Defun", null, {
|
|||||||
}, AST_Lambda);
|
}, AST_Lambda);
|
||||||
|
|
||||||
/* -----[ DESTRUCTURING ]----- */
|
/* -----[ DESTRUCTURING ]----- */
|
||||||
var AST_Destructuring = DEFNODE("Destructuring", "names is_array", {
|
var AST_Destructuring = DEFNODE("Destructuring", "names is_array default", {
|
||||||
$documentation: "A destructuring of several names. Used in destructuring assignment and with destructuring function argument names",
|
$documentation: "A destructuring of several names. Used in destructuring assignment and with destructuring function argument names",
|
||||||
_walk: function(visitor) {
|
_walk: function(visitor) {
|
||||||
return visitor._visit(this, function(){
|
return visitor._visit(this, function(){
|
||||||
|
|||||||
@@ -631,6 +631,12 @@ function OutputStream(options) {
|
|||||||
name.print(output);
|
name.print(output);
|
||||||
})
|
})
|
||||||
output.print(self.is_array ? "]" : "}");
|
output.print(self.is_array ? "]" : "}");
|
||||||
|
if (self.default) {
|
||||||
|
output.space();
|
||||||
|
output.print('=');
|
||||||
|
output.space();
|
||||||
|
self.default.print(output)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
DEFPRINT(AST_Debugger, function(self, output){
|
DEFPRINT(AST_Debugger, function(self, output){
|
||||||
|
|||||||
@@ -136,8 +136,9 @@ default_arguments: {
|
|||||||
input: {
|
input: {
|
||||||
function x(a = 6) { }
|
function x(a = 6) { }
|
||||||
function x(a = (6 + 5)) { }
|
function x(a = (6 + 5)) { }
|
||||||
|
function x({ foo } = {}, [ bar ] = [ 1 ]) { }
|
||||||
}
|
}
|
||||||
expect_exact: "function x(a=6){}function x(a=6+5){}"
|
expect_exact: "function x(a=6){}function x(a=6+5){}function x({foo}={},[bar]=[1]){}"
|
||||||
}
|
}
|
||||||
|
|
||||||
concise_methods: {
|
concise_methods: {
|
||||||
|
|||||||
Reference in New Issue
Block a user