From ff3d64f83cf70fac24c90fc3a867330f3bb1e252 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 2 Dec 2014 13:29:49 -0800 Subject: [PATCH] Address codereview --- src/compiler/emitter.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 303ca6264cb..0a953877866 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2197,22 +2197,29 @@ module ts { emitTrailingComments(node); } - function emitShorthandPropertyAssignmentAsNormalPropertyAssignment(node: ShorthandPropertyDeclaration) { + function emitDownlevelShorthandPropertyAssignment(node: ShorthandPropertyDeclaration) { emitLeadingComments(node); // Emit identifier as an identifier emit(node.name); write(": "); - // Even though this is stored as identified because it is in short-hand property assignment, - // treated it as expression + // Even though this is stored as identifier treat it as an expression + // Short-hand, { x }, is equivalent of normal form { x: x } emitExpressionIdentifier(node.name); emitTrailingComments(node); } function emitShorthandPropertyAssignment(node: ShorthandPropertyDeclaration) { - // If short-hand property has a prefix, then regardless of the target version, we will emit it as normal property assignment + // If short-hand property has a prefix, then regardless of the target version, we will emit it as normal property assignment. For example: + // module m { + // export var y; + // } + // module m { + // export var obj = { y }; + // } + // The short-hand property in obj need to emit as such ... = { y : m.y } regardless of the TargetScript version var prefix = resolver.getExpressionNamePrefix(node.name); if (prefix) { - emitShorthandPropertyAssignmentAsNormalPropertyAssignment(node); + emitDownlevelShorthandPropertyAssignment(node); } // If short-hand property has no prefix, emit it as short-hand. else { @@ -3408,7 +3415,7 @@ module ts { return emitPinnedOrTripleSlashComments(node); } - // Check if node has SyntaxKind which can be emitted regardless of the ScriptTarget + // Check if the node can be emitted regardless of the ScriptTarget switch (node.kind) { case SyntaxKind.Identifier: return emitIdentifier(node); @@ -3539,11 +3546,12 @@ module ts { // Emit node down-level switch (node.kind) { case SyntaxKind.ShorthandPropertyAssignment: - return emitShorthandPropertyAssignmentAsNormalPropertyAssignment(node); + return emitDownlevelShorthandPropertyAssignment(node); } } - else if (compilerOptions.target >= ScriptTarget.ES6) { + else { // Emit node natively + Debug.assert(compilerOptions.target >= ScriptTarget.ES6, "Invalid ScriptTarget. We should emit as ES6 or above"); switch (node.kind) { case SyntaxKind.ShorthandPropertyAssignment: return emitShorthandPropertyAssignment(node);