mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-17 21:06:50 -05:00
Properly emit nested destructuring in rest elements (fixes #2587)
This commit is contained in:
@@ -2558,7 +2558,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
|
||||
return node;
|
||||
}
|
||||
|
||||
function createPropertyAccessForDestructuringProperty(object: Expression, propName: Identifier): Expression {
|
||||
function createPropertyAccessForDestructuringProperty(object: Expression, propName: Identifier | LiteralExpression): Expression {
|
||||
if (propName.kind !== SyntaxKind.Identifier) {
|
||||
return createElementAccessExpression(object, propName);
|
||||
}
|
||||
@@ -2566,6 +2566,16 @@ var __param = this.__param || function(index, decorator) { return function (targ
|
||||
return createPropertyAccessExpression(object, propName);
|
||||
}
|
||||
|
||||
function createSliceCall(value: Expression, sliceIndex: number): CallExpression {
|
||||
let call = <CallExpression>createSynthesizedNode(SyntaxKind.CallExpression);
|
||||
let sliceIdentifier = <Identifier>createSynthesizedNode(SyntaxKind.Identifier);
|
||||
sliceIdentifier.text = "slice";
|
||||
call.expression = createPropertyAccessExpression(value, sliceIdentifier);
|
||||
call.arguments = <NodeArray<LiteralExpression>>createSynthesizedNodeArray();
|
||||
call.arguments[0] = createNumericLiteral(sliceIndex);
|
||||
return call;
|
||||
}
|
||||
|
||||
function emitObjectLiteralAssignment(target: ObjectLiteralExpression, value: Expression) {
|
||||
let properties = target.properties;
|
||||
if (properties.length !== 1) {
|
||||
@@ -2576,7 +2586,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
|
||||
for (let p of properties) {
|
||||
if (p.kind === SyntaxKind.PropertyAssignment || p.kind === SyntaxKind.ShorthandPropertyAssignment) {
|
||||
// TODO(andersh): Computed property support
|
||||
let propName = <Identifier>((<PropertyAssignment>p).name);
|
||||
let propName = <Identifier | LiteralExpression>((<PropertyAssignment>p).name);
|
||||
emitDestructuringAssignment((<PropertyAssignment>p).initializer || propName, createPropertyAccessForDestructuringProperty(value, propName));
|
||||
}
|
||||
}
|
||||
@@ -2596,9 +2606,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
|
||||
emitDestructuringAssignment(e, createElementAccessExpression(value, createNumericLiteral(i)));
|
||||
}
|
||||
else if (i === elements.length - 1) {
|
||||
value = ensureIdentifier(value);
|
||||
emitAssignment(<Identifier>(<SpreadElementExpression>e).expression, value);
|
||||
write(".slice(" + i + ")");
|
||||
emitDestructuringAssignment((<SpreadElementExpression>e).expression, createSliceCall(value, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2670,9 +2678,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
|
||||
emitBindingElement(element, createElementAccessExpression(value, createNumericLiteral(i)));
|
||||
}
|
||||
else if (i === elements.length - 1) {
|
||||
value = ensureIdentifier(value);
|
||||
emitAssignment(<Identifier>element.name, value);
|
||||
write(".slice(" + i + ")");
|
||||
emitBindingElement(element, createSliceCall(value, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1379,6 +1379,13 @@ module ts {
|
||||
return node;
|
||||
}
|
||||
|
||||
export function createSynthesizedNodeArray(): NodeArray<any> {
|
||||
var array = <NodeArray<any>>[];
|
||||
array.pos = -1;
|
||||
array.end = -1;
|
||||
return array;
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
export function createDiagnosticCollection(): DiagnosticCollection {
|
||||
let nonFileDiagnostics: Diagnostic[] = [];
|
||||
|
||||
Reference in New Issue
Block a user