diff --git a/src/compiler/transformers/experimental.ts b/src/compiler/transformers/experimental.ts index 85af37709c7..2ee623db43a 100644 --- a/src/compiler/transformers/experimental.ts +++ b/src/compiler/transformers/experimental.ts @@ -1,80 +1,80 @@ -/// -/// - -/*@internal*/ -namespace ts { - export function transformExperimental(context: TransformationContext) { - return transformSourceFile; - - function transformSourceFile(node: SourceFile) { - return visitEachChild(node, visitor, context); - } - - function visitor(node: Node): VisitResult { - if (node.transformFlags & TransformFlags.Experimental) { - return visitorWorker(node); - } - else if (node.transformFlags & TransformFlags.ContainsExperimental) { - return visitEachChild(node, visitor, context); - } - else { - return node; - } - } - - function visitorWorker(node: Node): VisitResult { - switch (node.kind) { - case SyntaxKind.ObjectLiteralExpression: - return visitObjectLiteralExpression(node as ObjectLiteralExpression); - default: - Debug.failBadSyntaxKind(node); - return visitEachChild(node, visitor, context); - } - } - - function chunkObjectLiteralElements(elements: ObjectLiteralElement[]): Expression[] { - let chunkObject: (ShorthandPropertyAssignment | PropertyAssignment)[]; - const objects: Expression[] = []; - for (const e of elements) { - if (e.kind === SyntaxKind.SpreadElementExpression) { - if (chunkObject) { - objects.push(createObjectLiteral(chunkObject)); - chunkObject = undefined; - } - const target = (e as SpreadElementExpression).expression; - objects.push(visitNode(target, visitor, isExpression)); - } - else { - if (!chunkObject) { - chunkObject = []; - } - if (e.kind === SyntaxKind.PropertyAssignment) { - const p = e as PropertyAssignment; - chunkObject.push(createPropertyAssignment(p.name, visitNode(p.initializer, visitor, isExpression))); - } - else { - chunkObject.push(e as ShorthandPropertyAssignment); - } - } - } - if (chunkObject) { - objects.push(createObjectLiteral(chunkObject)); - } - - return objects; - } - - function visitObjectLiteralExpression(node: ObjectLiteralExpression): Expression { - // spread elements emit like so: - // non-spread elements are chunked together into object literals, and then all are passed to __assign: - // { a, ...o, b } => __assign({a}, o, {b}); - // If the first element is a spread element, then the first argument to __assign is {}: - // { ...o, a, b, ...o2 } => __assign({}, o, {a, b}, o2) - const objects = chunkObjectLiteralElements(node.properties); - if (objects.length && objects[0].kind !== SyntaxKind.ObjectLiteralExpression) { - objects.unshift(createObjectLiteral()); - } - return createCall(createIdentifier("__assign"), undefined, objects); - } - } -} +/// +/// + +/*@internal*/ +namespace ts { + export function transformExperimental(context: TransformationContext) { + return transformSourceFile; + + function transformSourceFile(node: SourceFile) { + return visitEachChild(node, visitor, context); + } + + function visitor(node: Node): VisitResult { + if (node.transformFlags & TransformFlags.Experimental) { + return visitorWorker(node); + } + else if (node.transformFlags & TransformFlags.ContainsExperimental) { + return visitEachChild(node, visitor, context); + } + else { + return node; + } + } + + function visitorWorker(node: Node): VisitResult { + switch (node.kind) { + case SyntaxKind.ObjectLiteralExpression: + return visitObjectLiteralExpression(node as ObjectLiteralExpression); + default: + Debug.failBadSyntaxKind(node); + return visitEachChild(node, visitor, context); + } + } + + function chunkObjectLiteralElements(elements: ObjectLiteralElement[]): Expression[] { + let chunkObject: (ShorthandPropertyAssignment | PropertyAssignment)[]; + const objects: Expression[] = []; + for (const e of elements) { + if (e.kind === SyntaxKind.SpreadElementExpression) { + if (chunkObject) { + objects.push(createObjectLiteral(chunkObject)); + chunkObject = undefined; + } + const target = (e as SpreadElementExpression).expression; + objects.push(visitNode(target, visitor, isExpression)); + } + else { + if (!chunkObject) { + chunkObject = []; + } + if (e.kind === SyntaxKind.PropertyAssignment) { + const p = e as PropertyAssignment; + chunkObject.push(createPropertyAssignment(p.name, visitNode(p.initializer, visitor, isExpression))); + } + else { + chunkObject.push(e as ShorthandPropertyAssignment); + } + } + } + if (chunkObject) { + objects.push(createObjectLiteral(chunkObject)); + } + + return objects; + } + + function visitObjectLiteralExpression(node: ObjectLiteralExpression): Expression { + // spread elements emit like so: + // non-spread elements are chunked together into object literals, and then all are passed to __assign: + // { a, ...o, b } => __assign({a}, o, {b}); + // If the first element is a spread element, then the first argument to __assign is {}: + // { ...o, a, b, ...o2 } => __assign({}, o, {a, b}, o2) + const objects = chunkObjectLiteralElements(node.properties); + if (objects.length && objects[0].kind !== SyntaxKind.ObjectLiteralExpression) { + objects.unshift(createObjectLiteral()); + } + return createCall(createIdentifier("__assign"), undefined, objects); + } + } +}