Refactoring emitter for emit ES6 features natively

This commit is contained in:
Yui T 2014-11-25 12:12:55 -08:00
parent 44e6bcf7ff
commit 460fc037e6
4 changed files with 42 additions and 32 deletions

View file

@ -8881,7 +8881,7 @@ module ts {
// This is necessary as an identifier in short-hand property assignment can contains two meaning: // This is necessary as an identifier in short-hand property assignment can contains two meaning:
// property name and property value. // property name and property value.
if (location && location.kind === SyntaxKind.ShorthandPropertyAssignment) { if (location && location.kind === SyntaxKind.ShorthandPropertyAssignment) {
return resolveEntityName(location, (<ShortHandPropertyDeclaration>location).name, SymbolFlags.Value); return resolveEntityName(location, (<ShorthandPropertyDeclaration>location).name, SymbolFlags.Value);
} }
return undefined; return undefined;
} }

View file

@ -2197,33 +2197,28 @@ module ts {
emitTrailingComments(node); emitTrailingComments(node);
} }
function emitShortHandPropertyAssignment(node: ShortHandPropertyDeclaration) { function emitShorthandPropertyAssignmentAsNormalPropertyAssignment(node: ShorthandPropertyDeclaration) {
function emitAsNormalPropertyAssignment() { emitLeadingComments(node);
emitLeadingComments(node); // Emit identifier as an identifier
// Emit identifier as an identifier emit(node.name);
emit(node.name); write(": ");
write(": "); // Even though this is stored as identified because it is in short-hand property assignment,
// Even though this is stored as identified because it is in short-hand property assignment, // treated it as expression
// treated it as expression emitExpressionIdentifier(node.name);
emitExpressionIdentifier(node.name); emitTrailingComments(node);
emitTrailingComments(node); }
}
if (compilerOptions.target < ScriptTarget.ES6) { function emitShorthandPropertyAssignment(node: ShorthandPropertyDeclaration) {
emitAsNormalPropertyAssignment(); // If short-hand property has a prefix, then regardless of the target version, we will emit it as normal property assignment
var prefix = resolver.getExpressionNamePrefix(node.name);
if (prefix) {
emitShorthandPropertyAssignmentAsNormalPropertyAssignment(node);
} }
else if (compilerOptions.target >= ScriptTarget.ES6) { // If short-hand property has no prefix, emit it as short-hand.
// If short-hand property has a prefix, then regardless of the target version, we will emit it as normal property assignment else {
var prefix = resolver.getExpressionNamePrefix(node.name); emitLeadingComments(node);
if (prefix) { emit(node.name);
emitAsNormalPropertyAssignment(); emitTrailingComments(node);
}
// If short-hand property has no prefix, emit it as short-hand.
else {
emitLeadingComments(node);
emit(node.name);
emitTrailingComments(node);
}
} }
} }
@ -3413,6 +3408,7 @@ module ts {
return emitPinnedOrTripleSlashComments(node); return emitPinnedOrTripleSlashComments(node);
} }
// Check if node has SyntaxKind which can be emitted the same in both version of ES5 and ES6
switch (node.kind) { switch (node.kind) {
case SyntaxKind.Identifier: case SyntaxKind.Identifier:
return emitIdentifier(<Identifier>node); return emitIdentifier(<Identifier>node);
@ -3451,8 +3447,6 @@ module ts {
return emitObjectLiteral(<ObjectLiteral>node); return emitObjectLiteral(<ObjectLiteral>node);
case SyntaxKind.PropertyAssignment: case SyntaxKind.PropertyAssignment:
return emitPropertyAssignment(<PropertyDeclaration>node); return emitPropertyAssignment(<PropertyDeclaration>node);
case SyntaxKind.ShorthandPropertyAssignment:
return emitShortHandPropertyAssignment(<ShortHandPropertyDeclaration>node);
case SyntaxKind.PropertyAccess: case SyntaxKind.PropertyAccess:
return emitPropertyAccess(<PropertyAccess>node); return emitPropertyAccess(<PropertyAccess>node);
case SyntaxKind.IndexedAccess: case SyntaxKind.IndexedAccess:
@ -3539,6 +3533,22 @@ module ts {
case SyntaxKind.SourceFile: case SyntaxKind.SourceFile:
return emitSourceFile(<SourceFile>node); return emitSourceFile(<SourceFile>node);
} }
// Emit node which needs to be emitted differently between ES5 and ES6
if (compilerOptions.target < ScriptTarget.ES6) {
// Emit node down-leveling
switch (node.kind) {
case SyntaxKind.ShorthandPropertyAssignment:
return emitShorthandPropertyAssignmentAsNormalPropertyAssignment(<ShorthandPropertyDeclaration>node);
}
}
else if (compilerOptions.target >= ScriptTarget.ES6) {
// Emit node natively in EcmaScript6
switch (node.kind) {
case SyntaxKind.ShorthandPropertyAssignment:
return emitShorthandPropertyAssignment(<ShorthandPropertyDeclaration>node);
}
}
} }
function hasDetachedComments(pos: number) { function hasDetachedComments(pos: number) {

View file

@ -2764,7 +2764,7 @@ module ts {
// Parse to check if it is short-hand property assignment or normal property assignment // Parse to check if it is short-hand property assignment or normal property assignment
if ((token === SyntaxKind.CommaToken || token === SyntaxKind.CloseBraceToken) && tokenIsIdentifier) { if ((token === SyntaxKind.CommaToken || token === SyntaxKind.CloseBraceToken) && tokenIsIdentifier) {
node = <ShortHandPropertyDeclaration>createNode(SyntaxKind.ShorthandPropertyAssignment, nodePos); node = <ShorthandPropertyDeclaration>createNode(SyntaxKind.ShorthandPropertyAssignment, nodePos);
node.name = propertyName; node.name = propertyName;
} }
else { else {
@ -3968,7 +3968,7 @@ module ts {
case SyntaxKind.ReturnStatement: return checkReturnStatement(<ReturnStatement>node); case SyntaxKind.ReturnStatement: return checkReturnStatement(<ReturnStatement>node);
case SyntaxKind.SetAccessor: return checkSetAccessor(<MethodDeclaration>node); case SyntaxKind.SetAccessor: return checkSetAccessor(<MethodDeclaration>node);
case SyntaxKind.SourceFile: return checkSourceFile(<SourceFile>node); case SyntaxKind.SourceFile: return checkSourceFile(<SourceFile>node);
case SyntaxKind.ShorthandPropertyAssignment: return checkShorthandPropertyAssignment(<ShortHandPropertyDeclaration>node); case SyntaxKind.ShorthandPropertyAssignment: return checkShorthandPropertyAssignment(<ShorthandPropertyDeclaration>node);
case SyntaxKind.SwitchStatement: return checkSwitchStatement(<SwitchStatement>node); case SyntaxKind.SwitchStatement: return checkSwitchStatement(<SwitchStatement>node);
case SyntaxKind.TaggedTemplateExpression: return checkTaggedTemplateExpression(<TaggedTemplateExpression>node); case SyntaxKind.TaggedTemplateExpression: return checkTaggedTemplateExpression(<TaggedTemplateExpression>node);
case SyntaxKind.TupleType: return checkTupleType(<TupleTypeNode>node); case SyntaxKind.TupleType: return checkTupleType(<TupleTypeNode>node);
@ -4832,7 +4832,7 @@ module ts {
return grammarErrorOnFirstToken(node, Diagnostics.A_declare_modifier_is_required_for_a_top_level_declaration_in_a_d_ts_file); return grammarErrorOnFirstToken(node, Diagnostics.A_declare_modifier_is_required_for_a_top_level_declaration_in_a_d_ts_file);
} }
function checkShorthandPropertyAssignment(node: ShortHandPropertyDeclaration): boolean { function checkShorthandPropertyAssignment(node: ShorthandPropertyDeclaration): boolean {
return checkForInvalidQuestionMark(node, Diagnostics.An_object_member_cannot_be_declared_optional); return checkForInvalidQuestionMark(node, Diagnostics.An_object_member_cannot_be_declared_optional);
} }

View file

@ -360,7 +360,7 @@ module ts {
initializer?: Expression; initializer?: Expression;
} }
export interface ShortHandPropertyDeclaration extends Declaration { export interface ShorthandPropertyDeclaration extends Declaration {
name: Identifier; name: Identifier;
} }