fixes #358: correctly codegen AssignmentPattern in ObjectPattern (#361)
diff --git a/escodegen.js b/escodegen.js
index 00e5cc8..efcca69 100644
--- a/escodegen.js
+++ b/escodegen.js
@@ -972,18 +972,14 @@
return result;
};
- CodeGenerator.prototype.generatePropertyKey = function (expr, computed, value) {
+ CodeGenerator.prototype.generatePropertyKey = function (expr, computed) {
var result = [];
if (computed) {
result.push('[');
}
- if (value.type === 'AssignmentPattern') {
- result.push(this.AssignmentPattern(value, Precedence.Sequence, E_TTT));
- } else {
- result.push(this.generateExpression(expr, Precedence.Sequence, E_TTT));
- }
+ result.push(this.generateExpression(expr, Precedence.Sequence, E_TTT));
if (computed) {
result.push(']');
@@ -2110,13 +2106,13 @@
}
if (expr.kind === 'get' || expr.kind === 'set') {
fragment = [
- join(expr.kind, this.generatePropertyKey(expr.key, expr.computed, expr.value)),
+ join(expr.kind, this.generatePropertyKey(expr.key, expr.computed)),
this.generateFunctionBody(expr.value)
];
} else {
fragment = [
generateMethodPrefix(expr),
- this.generatePropertyKey(expr.key, expr.computed, expr.value),
+ this.generatePropertyKey(expr.key, expr.computed),
this.generateFunctionBody(expr.value)
];
}
@@ -2127,25 +2123,28 @@
if (expr.kind === 'get' || expr.kind === 'set') {
return [
expr.kind, noEmptySpace(),
- this.generatePropertyKey(expr.key, expr.computed, expr.value),
+ this.generatePropertyKey(expr.key, expr.computed),
this.generateFunctionBody(expr.value)
];
}
if (expr.shorthand) {
- return this.generatePropertyKey(expr.key, expr.computed, expr.value);
+ if (expr.value.type === "AssignmentPattern") {
+ return this.AssignmentPattern(expr.value, Precedence.Sequence, E_TTT);
+ }
+ return this.generatePropertyKey(expr.key, expr.computed);
}
if (expr.method) {
return [
generateMethodPrefix(expr),
- this.generatePropertyKey(expr.key, expr.computed, expr.value),
+ this.generatePropertyKey(expr.key, expr.computed),
this.generateFunctionBody(expr.value)
];
}
return [
- this.generatePropertyKey(expr.key, expr.computed, expr.value),
+ this.generatePropertyKey(expr.key, expr.computed),
':' + space,
this.generateExpression(expr.value, Precedence.Assignment, E_TTT)
];
diff --git a/test/harmony.js b/test/harmony.js
index 9941f18..fe836b8 100644
--- a/test/harmony.js
+++ b/test/harmony.js
@@ -510,6 +510,203 @@
},
'Object destructuring (and aliasing)': {
+ "let {\n test: myvar = 'a'\n} = { test: 'b' };": {
+ generateFrom: {
+ loc: {
+ start: {
+ line: 1,
+ column: 0
+ },
+ end: {
+ line: 3,
+ column: 23
+ }
+ },
+ type: "Program",
+ body: [
+ {
+ loc: {
+ start: {
+ line: 1,
+ column: 0
+ },
+ end: {
+ line: 3,
+ column: 23
+ }
+ },
+ type: "VariableDeclaration",
+ declarations: [
+ {
+ loc: {
+ start: {
+ line: 1,
+ column: 4
+ },
+ end: {
+ line: 3,
+ column: 22
+ }
+ },
+ type: "VariableDeclarator",
+ id: {
+ loc: {
+ start: {
+ line: 1,
+ column: 4
+ },
+ end: {
+ line: 3,
+ column: 1
+ }
+ },
+ type: "ObjectPattern",
+ properties: [
+ {
+ loc: {
+ start: {
+ line: 2,
+ column: 4
+ },
+ end: {
+ line: 2,
+ column: 27
+ }
+ },
+ type: "Property",
+ key: {
+ loc: {
+ start: {
+ line: 2,
+ column: 4
+ },
+ end: {
+ line: 2,
+ column: 8
+ }
+ },
+ type: "Identifier",
+ name: "test"
+ },
+ computed: false,
+ value: {
+ loc: {
+ start: {
+ line: 2,
+ column: 10
+ },
+ end: {
+ line: 2,
+ column: 27
+ }
+ },
+ type: "AssignmentPattern",
+ left: {
+ loc: {
+ start: {
+ line: 2,
+ column: 10
+ },
+ end: {
+ line: 2,
+ column: 15
+ }
+ },
+ type: "Identifier",
+ name: "myvar"
+ },
+ right: {
+ loc: {
+ start: {
+ line: 2,
+ column: 18
+ },
+ end: {
+ line: 2,
+ column: 27
+ }
+ },
+ type: "Literal",
+ value: "a",
+ raw: "'a'"
+ }
+ },
+ kind: "init",
+ method: false,
+ shorthand: false
+ }
+ ]
+ },
+ init: {
+ loc: {
+ start: {
+ line: 3,
+ column: 4
+ },
+ end: {
+ line: 3,
+ column: 22
+ }
+ },
+ type: "ObjectExpression",
+ properties: [
+ {
+ loc: {
+ start: {
+ line: 3,
+ column: 6
+ },
+ end: {
+ line: 3,
+ column: 20
+ }
+ },
+ type: "Property",
+ key: {
+ loc: {
+ start: {
+ line: 3,
+ column: 6
+ },
+ end: {
+ line: 3,
+ column: 10
+ }
+ },
+ type: "Identifier",
+ name: "test"
+ },
+ computed: false,
+ value: {
+ loc: {
+ start: {
+ line: 3,
+ column: 12
+ },
+ end: {
+ line: 3,
+ column: 20
+ }
+ },
+ type: "Literal",
+ value: "b",
+ raw: "'b'"
+ },
+ kind: "init",
+ method: false,
+ shorthand: false
+ }
+ ]
+ }
+ }
+ ],
+ kind: "let"
+ }
+ ],
+ sourceType: "script"
+ }
+ },
+
'({a = "b", b = {\n c: "d",\n e: "f"\n }} = {}) => {\n};': {
generateFrom: {
type: "ExpressionStatement",