Skip to content

Commit 9844f64

Browse files
tswatersmichaelficarra
authored andcommitted
Support for defaults when destructuring parameters (estools#297)
Fixes estools#296
1 parent 2b8d6ed commit 9844f64

2 files changed

Lines changed: 111 additions & 8 deletions

File tree

escodegen.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -980,14 +980,20 @@
980980
return result;
981981
};
982982

983-
CodeGenerator.prototype.generatePropertyKey = function (expr, computed) {
983+
CodeGenerator.prototype.generatePropertyKey = function (expr, computed, value) {
984984
var result = [];
985985

986986
if (computed) {
987987
result.push('[');
988988
}
989989

990-
result.push(this.generateExpression(expr, Precedence.Sequence, E_TTT));
990+
if (value.type === 'AssignmentPattern') {
991+
result.push(this.AssignmentPattern(value, Precedence.Sequence, E_TTT));
992+
}
993+
else {
994+
result.push(this.generateExpression(expr, Precedence.Sequence, E_TTT));
995+
}
996+
991997
if (computed) {
992998
result.push(']');
993999
}
@@ -2113,13 +2119,13 @@
21132119
}
21142120
if (expr.kind === 'get' || expr.kind === 'set') {
21152121
fragment = [
2116-
join(expr.kind, this.generatePropertyKey(expr.key, expr.computed)),
2122+
join(expr.kind, this.generatePropertyKey(expr.key, expr.computed, expr.value)),
21172123
this.generateFunctionBody(expr.value)
21182124
];
21192125
} else {
21202126
fragment = [
21212127
generateMethodPrefix(expr),
2122-
this.generatePropertyKey(expr.key, expr.computed),
2128+
this.generatePropertyKey(expr.key, expr.computed, expr.value),
21232129
this.generateFunctionBody(expr.value)
21242130
];
21252131
}
@@ -2130,25 +2136,25 @@
21302136
if (expr.kind === 'get' || expr.kind === 'set') {
21312137
return [
21322138
expr.kind, noEmptySpace(),
2133-
this.generatePropertyKey(expr.key, expr.computed),
2139+
this.generatePropertyKey(expr.key, expr.computed, expr.value),
21342140
this.generateFunctionBody(expr.value)
21352141
];
21362142
}
21372143

21382144
if (expr.shorthand) {
2139-
return this.generatePropertyKey(expr.key, expr.computed);
2145+
return this.generatePropertyKey(expr.key, expr.computed, expr.value);
21402146
}
21412147

21422148
if (expr.method) {
21432149
return [
21442150
generateMethodPrefix(expr),
2145-
this.generatePropertyKey(expr.key, expr.computed),
2151+
this.generatePropertyKey(expr.key, expr.computed, expr.value),
21462152
this.generateFunctionBody(expr.value)
21472153
];
21482154
}
21492155

21502156
return [
2151-
this.generatePropertyKey(expr.key, expr.computed),
2157+
this.generatePropertyKey(expr.key, expr.computed, expr.value),
21522158
':' + space,
21532159
this.generateExpression(expr.value, Precedence.Assignment, E_TTT)
21542160
];

test/harmony.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,103 @@ data = {
510510
},
511511

512512
'Object destructuring (and aliasing)': {
513+
'({a = "b", b = {\n c: "d",\n e: "f"\n }} = {}) => {\n};': {
514+
generateFrom: {
515+
type: "ExpressionStatement",
516+
expression: {
517+
type: "ArrowFunctionExpression",
518+
id: null,
519+
params: [{
520+
type: "ObjectPattern",
521+
properties: [{
522+
type: "Property",
523+
key: {
524+
type: "Identifier",
525+
name: "a"
526+
},
527+
computed: false,
528+
value: {
529+
type: "AssignmentPattern",
530+
left: {
531+
type: "Identifier",
532+
name: "a"
533+
},
534+
right: {
535+
type: "Literal",
536+
value: "b",
537+
raw: "\"b\""
538+
}
539+
},
540+
kind: "init",
541+
method: false,
542+
shorthand: true
543+
}, {
544+
type: "Property",
545+
key: {
546+
type: "Identifier",
547+
name: "b"
548+
},
549+
computed: false,
550+
value: {
551+
type: "AssignmentPattern",
552+
left: {
553+
type: "Identifier",
554+
name: "b"
555+
},
556+
right: {
557+
type: "ObjectExpression",
558+
properties: [{
559+
type: "Property",
560+
key: {
561+
type: "Identifier",
562+
name: "c"
563+
},
564+
computed: false,
565+
value: {
566+
type: "Literal",
567+
value: "d",
568+
raw: "\"d\""
569+
},
570+
kind: "init",
571+
method: false,
572+
shorthand: false
573+
}, {
574+
type: "Property",
575+
key: {
576+
type: "Identifier",
577+
name: "e"
578+
},
579+
computed: false,
580+
value: {
581+
type: "Literal",
582+
value: "f",
583+
raw: "\"f\""
584+
},
585+
kind: "init",
586+
method: false,
587+
shorthand: false
588+
}]
589+
}
590+
},
591+
kind: "init",
592+
method: false,
593+
shorthand: true
594+
}]
595+
}],
596+
defaults: [{
597+
type: "ObjectExpression",
598+
properties: []
599+
}],
600+
body: {
601+
type: "BlockStatement",
602+
body: []
603+
},
604+
generator: false,
605+
expression: false
606+
}
607+
}
608+
},
609+
513610
'var {\n a,\n b: C\n} = {};' : {
514611
generateFrom: {
515612
type: 'Program',

0 commit comments

Comments
 (0)