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 b550383143
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:
// property name and property value.
if (location && location.kind === SyntaxKind.ShorthandPropertyAssignment) {
return resolveEntityName(location, (<ShortHandPropertyDeclaration>location).name, SymbolFlags.Value);
return resolveEntityName(location, (<ShorthandPropertyDeclaration>location).name, SymbolFlags.Value);
}
return undefined;
}

View file

@ -2197,33 +2197,28 @@ module ts {
emitTrailingComments(node);
}
function emitShortHandPropertyAssignment(node: ShortHandPropertyDeclaration) {
function emitAsNormalPropertyAssignment() {
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
emitExpressionIdentifier(node.name);
emitTrailingComments(node);
}
function emitShorthandPropertyAssignmentAsNormalPropertyAssignment(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
emitExpressionIdentifier(node.name);
emitTrailingComments(node);
}
if (compilerOptions.target < ScriptTarget.ES6) {
emitAsNormalPropertyAssignment();
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
var prefix = resolver.getExpressionNamePrefix(node.name);
if (prefix) {
emitShorthandPropertyAssignmentAsNormalPropertyAssignment(node);
}
else if (compilerOptions.target >= ScriptTarget.ES6) {
// 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) {
emitAsNormalPropertyAssignment();
}
// If short-hand property has no prefix, emit it as short-hand.
else {
emitLeadingComments(node);
emit(node.name);
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);
}
// Check if node has SyntaxKind which can be emitted regardless of the ScriptTarget
switch (node.kind) {
case SyntaxKind.Identifier:
return emitIdentifier(<Identifier>node);
@ -3451,8 +3447,6 @@ module ts {
return emitObjectLiteral(<ObjectLiteral>node);
case SyntaxKind.PropertyAssignment:
return emitPropertyAssignment(<PropertyDeclaration>node);
case SyntaxKind.ShorthandPropertyAssignment:
return emitShortHandPropertyAssignment(<ShortHandPropertyDeclaration>node);
case SyntaxKind.PropertyAccess:
return emitPropertyAccess(<PropertyAccess>node);
case SyntaxKind.IndexedAccess:
@ -3539,6 +3533,22 @@ module ts {
case SyntaxKind.SourceFile:
return emitSourceFile(<SourceFile>node);
}
// Emit node which needs to be emitted differently depended on ScriptTarget
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) {

View file

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

View file

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