Support for defaults when destructuring parameters (#297)
Fixes #296
diff --git a/escodegen.js b/escodegen.js
index 0360521..e92b283 100644
--- a/escodegen.js
+++ b/escodegen.js
@@ -980,14 +980,20 @@
return result;
};
- CodeGenerator.prototype.generatePropertyKey = function (expr, computed) {
+ CodeGenerator.prototype.generatePropertyKey = function (expr, computed, value) {
var result = [];
if (computed) {
result.push('[');
}
- result.push(this.generateExpression(expr, Precedence.Sequence, E_TTT));
+ if (value.type === 'AssignmentPattern') {
+ result.push(this.AssignmentPattern(value, Precedence.Sequence, E_TTT));
+ }
+ else {
+ result.push(this.generateExpression(expr, Precedence.Sequence, E_TTT));
+ }
+
if (computed) {
result.push(']');
}
@@ -2113,13 +2119,13 @@
}
if (expr.kind === 'get' || expr.kind === 'set') {
fragment = [
- join(expr.kind, this.generatePropertyKey(expr.key, expr.computed)),
+ join(expr.kind, this.generatePropertyKey(expr.key, expr.computed, expr.value)),
this.generateFunctionBody(expr.value)
];
} else {
fragment = [
generateMethodPrefix(expr),
- this.generatePropertyKey(expr.key, expr.computed),
+ this.generatePropertyKey(expr.key, expr.computed, expr.value),
this.generateFunctionBody(expr.value)
];
}
@@ -2130,25 +2136,25 @@
if (expr.kind === 'get' || expr.kind === 'set') {
return [
expr.kind, noEmptySpace(),
- this.generatePropertyKey(expr.key, expr.computed),
+ this.generatePropertyKey(expr.key, expr.computed, expr.value),
this.generateFunctionBody(expr.value)
];
}
if (expr.shorthand) {
- return this.generatePropertyKey(expr.key, expr.computed);
+ return this.generatePropertyKey(expr.key, expr.computed, expr.value);
}
if (expr.method) {
return [
generateMethodPrefix(expr),
- this.generatePropertyKey(expr.key, expr.computed),
+ this.generatePropertyKey(expr.key, expr.computed, expr.value),
this.generateFunctionBody(expr.value)
];
}
return [
- this.generatePropertyKey(expr.key, expr.computed),
+ this.generatePropertyKey(expr.key, expr.computed, expr.value),
':' + space,
this.generateExpression(expr.value, Precedence.Assignment, E_TTT)
];
diff --git a/test/harmony.js b/test/harmony.js
index 2087e4f..9941f18 100644
--- a/test/harmony.js
+++ b/test/harmony.js
@@ -510,6 +510,103 @@
},
'Object destructuring (and aliasing)': {
+ '({a = "b", b = {\n c: "d",\n e: "f"\n }} = {}) => {\n};': {
+ generateFrom: {
+ type: "ExpressionStatement",
+ expression: {
+ type: "ArrowFunctionExpression",
+ id: null,
+ params: [{
+ type: "ObjectPattern",
+ properties: [{
+ type: "Property",
+ key: {
+ type: "Identifier",
+ name: "a"
+ },
+ computed: false,
+ value: {
+ type: "AssignmentPattern",
+ left: {
+ type: "Identifier",
+ name: "a"
+ },
+ right: {
+ type: "Literal",
+ value: "b",
+ raw: "\"b\""
+ }
+ },
+ kind: "init",
+ method: false,
+ shorthand: true
+ }, {
+ type: "Property",
+ key: {
+ type: "Identifier",
+ name: "b"
+ },
+ computed: false,
+ value: {
+ type: "AssignmentPattern",
+ left: {
+ type: "Identifier",
+ name: "b"
+ },
+ right: {
+ type: "ObjectExpression",
+ properties: [{
+ type: "Property",
+ key: {
+ type: "Identifier",
+ name: "c"
+ },
+ computed: false,
+ value: {
+ type: "Literal",
+ value: "d",
+ raw: "\"d\""
+ },
+ kind: "init",
+ method: false,
+ shorthand: false
+ }, {
+ type: "Property",
+ key: {
+ type: "Identifier",
+ name: "e"
+ },
+ computed: false,
+ value: {
+ type: "Literal",
+ value: "f",
+ raw: "\"f\""
+ },
+ kind: "init",
+ method: false,
+ shorthand: false
+ }]
+ }
+ },
+ kind: "init",
+ method: false,
+ shorthand: true
+ }]
+ }],
+ defaults: [{
+ type: "ObjectExpression",
+ properties: []
+ }],
+ body: {
+ type: "BlockStatement",
+ body: []
+ },
+ generator: false,
+ expression: false
+ }
+ }
+ },
+
'var {\n a,\n b: C\n} = {};' : {
generateFrom: {
type: 'Program',