From 88c61e89a8c67dacfdcb382ce7815195d3c38558 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 20 Feb 2015 18:02:14 -0800 Subject: [PATCH] Addressed CR comments. --- src/compiler/emitter.ts | 45 ++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 971efb2ea5f..1df706c60f7 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2528,6 +2528,7 @@ module ts { var node = createNode(kind); node.pos = -1; node.end = -1; + return node; } @@ -2548,7 +2549,8 @@ module ts { initialObjectLiteral.flags |= NodeFlags.MultiLine; // The comma expressions that will patch the object literal. - var propertyPatches = createBinaryExpression(SyntaxKind.EqualsToken, tempVar, initialObjectLiteral); + // This will end up being something like '_a = { ... }, _a.x = 10, _a.y = 20, _a'. + var propertyPatches = createBinaryExpression(tempVar, SyntaxKind.EqualsToken, initialObjectLiteral); ts.forEach(originalObjectLiteral.properties, property => { var patchedProperty = tryCreatePatchingPropertyAssignment(originalObjectLiteral, tempVar, property); @@ -2558,11 +2560,12 @@ module ts { //var trailingComments = getTrailingCommentRanges(currentSourceFile.text, property.end); //addCommentsToSynthesizedNode(patchedProperty, leadingComments, trailingComments); - propertyPatches = createBinaryExpression(SyntaxKind.CommaToken, propertyPatches, patchedProperty); + propertyPatches = createBinaryExpression(propertyPatches, SyntaxKind.CommaToken, patchedProperty); } }); - propertyPatches = createBinaryExpression(SyntaxKind.CommaToken, propertyPatches, tempVar); + // Finally, return the temp variable. + propertyPatches = createBinaryExpression(propertyPatches, SyntaxKind.CommaToken, tempVar); var result = createParenthesizedExpression(propertyPatches); @@ -2579,7 +2582,8 @@ module ts { node.trailingCommentRanges = trailingCommentRanges; } - // Returns 'undefined' if a property has already been accounted for. + // Returns 'undefined' if a property has already been accounted for + // (e.g. a 'get' accessor which has already been emitted along with its 'set' accessor). function tryCreatePatchingPropertyAssignment(objectLiteral: ObjectLiteralExpression, tempVar: Identifier, property: ObjectLiteralElement): Expression { var leftHandSide = createMemberAccessForPropertyName(tempVar, property.name); var rightHandSide: Expression; @@ -2592,25 +2596,25 @@ module ts { rightHandSide = createPropertyAccessExpression(prefix, (property).name); } else if (property.kind === SyntaxKind.MethodDeclaration) { - rightHandSide = createFunctionExpressionForFunctionLikeDeclaration((property)); + rightHandSide = createFunctionExpressionForFunctionLikeDeclaration((property).parameters, (property).body); } else if (property.kind === SyntaxKind.GetAccessor || property.kind === SyntaxKind.SetAccessor) { - var accessors = getAllAccessorDeclarations(objectLiteral.properties, property); + var { firstAccessor, setAccessor, getAccessor } = getAllAccessorDeclarations(objectLiteral.properties, property); // Only emit the first accessor. - if (accessors.firstAccessor !== property) { + if (firstAccessor !== property) { return undefined; } var propertyDescriptor = createSynthesizedNode(SyntaxKind.ObjectLiteralExpression); var descriptorProperties = >[]; - if (accessors.getAccessor) { - var getProperty = createPropertyAssignment(createIdentifier("get"), createFunctionExpressionForFunctionLikeDeclaration(accessors.getAccessor)); + if (getAccessor) { + var getProperty = createPropertyAssignment(createIdentifier("get"), createFunctionExpressionForFunctionLikeDeclaration(getAccessor.parameters, getAccessor.body)); descriptorProperties.push(getProperty); } - if (accessors.setAccessor) { - var setProperty = createPropertyAssignment(createIdentifier("set"), createFunctionExpressionForFunctionLikeDeclaration(accessors.setAccessor)); + if (setAccessor) { + var setProperty = createPropertyAssignment(createIdentifier("set"), createFunctionExpressionForFunctionLikeDeclaration(setAccessor.parameters, setAccessor.body)); descriptorProperties.push(setProperty); } @@ -2631,20 +2635,25 @@ module ts { Debug.fail(`ObjectLiteralElement kind ${property.kind} not accounted for.`); } - return createBinaryExpression(SyntaxKind.EqualsToken, leftHandSide, rightHandSide); + return createBinaryExpression(leftHandSide, SyntaxKind.EqualsToken, rightHandSide); } function createParenthesizedExpression(expression: Expression) { var result = createSynthesizedNode(SyntaxKind.ParenthesizedExpression); result.expression = expression; + return result; } function createNodeArray(...elements: T[]): NodeArray { - return >elements; + var result = >elements; + result.pos = -1; + result.end = -1; + + return result; } - function createBinaryExpression(operator: SyntaxKind, left: Expression, right: Expression): BinaryExpression { + function createBinaryExpression(left: Expression, operator: SyntaxKind, right: Expression): BinaryExpression { var result = createSynthesizedNode(SyntaxKind.BinaryExpression); result.operator = operator; result.left = left; @@ -2670,17 +2679,16 @@ module ts { function createPropertyAssignment(name: LiteralExpression | Identifier, initializer: Expression) { var result = createSynthesizedNode(SyntaxKind.PropertyAssignment); - result.name = name; result.initializer = initializer; return result; } - function createFunctionExpressionForFunctionLikeDeclaration(func: FunctionLikeDeclaration): FunctionExpression { + function createFunctionExpressionForFunctionLikeDeclaration(parameters: NodeArray, body: Block): FunctionExpression { var result = createSynthesizedNode(SyntaxKind.FunctionExpression); - result.parameters = func.parameters; - result.body = func.body; + result.parameters = parameters; + result.body = body; return result; } @@ -2704,6 +2712,7 @@ module ts { function createIdentifier(name: string) { var result = createSynthesizedNode(SyntaxKind.Identifier); result.text = name; + return result; }