Skip to content

Commit d759359

Browse files
fixes estools#358: correctly codegen AssignmentPattern in ObjectPattern (estools#361)
1 parent 612e75a commit d759359

File tree

2 files changed

+208
-12
lines changed

2 files changed

+208
-12
lines changed

escodegen.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -972,18 +972,14 @@
972972
return result;
973973
};
974974

975-
CodeGenerator.prototype.generatePropertyKey = function (expr, computed, value) {
975+
CodeGenerator.prototype.generatePropertyKey = function (expr, computed) {
976976
var result = [];
977977

978978
if (computed) {
979979
result.push('[');
980980
}
981981

982-
if (value.type === 'AssignmentPattern') {
983-
result.push(this.AssignmentPattern(value, Precedence.Sequence, E_TTT));
984-
} else {
985-
result.push(this.generateExpression(expr, Precedence.Sequence, E_TTT));
986-
}
982+
result.push(this.generateExpression(expr, Precedence.Sequence, E_TTT));
987983

988984
if (computed) {
989985
result.push(']');
@@ -2110,13 +2106,13 @@
21102106
}
21112107
if (expr.kind === 'get' || expr.kind === 'set') {
21122108
fragment = [
2113-
join(expr.kind, this.generatePropertyKey(expr.key, expr.computed, expr.value)),
2109+
join(expr.kind, this.generatePropertyKey(expr.key, expr.computed)),
21142110
this.generateFunctionBody(expr.value)
21152111
];
21162112
} else {
21172113
fragment = [
21182114
generateMethodPrefix(expr),
2119-
this.generatePropertyKey(expr.key, expr.computed, expr.value),
2115+
this.generatePropertyKey(expr.key, expr.computed),
21202116
this.generateFunctionBody(expr.value)
21212117
];
21222118
}
@@ -2127,25 +2123,28 @@
21272123
if (expr.kind === 'get' || expr.kind === 'set') {
21282124
return [
21292125
expr.kind, noEmptySpace(),
2130-
this.generatePropertyKey(expr.key, expr.computed, expr.value),
2126+
this.generatePropertyKey(expr.key, expr.computed),
21312127
this.generateFunctionBody(expr.value)
21322128
];
21332129
}
21342130

21352131
if (expr.shorthand) {
2136-
return this.generatePropertyKey(expr.key, expr.computed, expr.value);
2132+
if (expr.value.type === "AssignmentPattern") {
2133+
return this.AssignmentPattern(expr.value, Precedence.Sequence, E_TTT);
2134+
}
2135+
return this.generatePropertyKey(expr.key, expr.computed);
21372136
}
21382137

21392138
if (expr.method) {
21402139
return [
21412140
generateMethodPrefix(expr),
2142-
this.generatePropertyKey(expr.key, expr.computed, expr.value),
2141+
this.generatePropertyKey(expr.key, expr.computed),
21432142
this.generateFunctionBody(expr.value)
21442143
];
21452144
}
21462145

21472146
return [
2148-
this.generatePropertyKey(expr.key, expr.computed, expr.value),
2147+
this.generatePropertyKey(expr.key, expr.computed),
21492148
':' + space,
21502149
this.generateExpression(expr.value, Precedence.Assignment, E_TTT)
21512150
];

test/harmony.js

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

512512
'Object destructuring (and aliasing)': {
513+
"let {\n test: myvar = 'a'\n} = { test: 'b' };": {
514+
generateFrom: {
515+
loc: {
516+
start: {
517+
line: 1,
518+
column: 0
519+
},
520+
end: {
521+
line: 3,
522+
column: 23
523+
}
524+
},
525+
type: "Program",
526+
body: [
527+
{
528+
loc: {
529+
start: {
530+
line: 1,
531+
column: 0
532+
},
533+
end: {
534+
line: 3,
535+
column: 23
536+
}
537+
},
538+
type: "VariableDeclaration",
539+
declarations: [
540+
{
541+
loc: {
542+
start: {
543+
line: 1,
544+
column: 4
545+
},
546+
end: {
547+
line: 3,
548+
column: 22
549+
}
550+
},
551+
type: "VariableDeclarator",
552+
id: {
553+
loc: {
554+
start: {
555+
line: 1,
556+
column: 4
557+
},
558+
end: {
559+
line: 3,
560+
column: 1
561+
}
562+
},
563+
type: "ObjectPattern",
564+
properties: [
565+
{
566+
loc: {
567+
start: {
568+
line: 2,
569+
column: 4
570+
},
571+
end: {
572+
line: 2,
573+
column: 27
574+
}
575+
},
576+
type: "Property",
577+
key: {
578+
loc: {
579+
start: {
580+
line: 2,
581+
column: 4
582+
},
583+
end: {
584+
line: 2,
585+
column: 8
586+
}
587+
},
588+
type: "Identifier",
589+
name: "test"
590+
},
591+
computed: false,
592+
value: {
593+
loc: {
594+
start: {
595+
line: 2,
596+
column: 10
597+
},
598+
end: {
599+
line: 2,
600+
column: 27
601+
}
602+
},
603+
type: "AssignmentPattern",
604+
left: {
605+
loc: {
606+
start: {
607+
line: 2,
608+
column: 10
609+
},
610+
end: {
611+
line: 2,
612+
column: 15
613+
}
614+
},
615+
type: "Identifier",
616+
name: "myvar"
617+
},
618+
right: {
619+
loc: {
620+
start: {
621+
line: 2,
622+
column: 18
623+
},
624+
end: {
625+
line: 2,
626+
column: 27
627+
}
628+
},
629+
type: "Literal",
630+
value: "a",
631+
raw: "'a'"
632+
}
633+
},
634+
kind: "init",
635+
method: false,
636+
shorthand: false
637+
}
638+
]
639+
},
640+
init: {
641+
loc: {
642+
start: {
643+
line: 3,
644+
column: 4
645+
},
646+
end: {
647+
line: 3,
648+
column: 22
649+
}
650+
},
651+
type: "ObjectExpression",
652+
properties: [
653+
{
654+
loc: {
655+
start: {
656+
line: 3,
657+
column: 6
658+
},
659+
end: {
660+
line: 3,
661+
column: 20
662+
}
663+
},
664+
type: "Property",
665+
key: {
666+
loc: {
667+
start: {
668+
line: 3,
669+
column: 6
670+
},
671+
end: {
672+
line: 3,
673+
column: 10
674+
}
675+
},
676+
type: "Identifier",
677+
name: "test"
678+
},
679+
computed: false,
680+
value: {
681+
loc: {
682+
start: {
683+
line: 3,
684+
column: 12
685+
},
686+
end: {
687+
line: 3,
688+
column: 20
689+
}
690+
},
691+
type: "Literal",
692+
value: "b",
693+
raw: "'b'"
694+
},
695+
kind: "init",
696+
method: false,
697+
shorthand: false
698+
}
699+
]
700+
}
701+
}
702+
],
703+
kind: "let"
704+
}
705+
],
706+
sourceType: "script"
707+
}
708+
},
709+
513710
'({a = "b", b = {\n c: "d",\n e: "f"\n }} = {}) => {\n};': {
514711
generateFrom: {
515712
type: "ExpressionStatement",

0 commit comments

Comments
 (0)