mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-14 10:55:15 -06:00
Merge pull request #1269 from Microsoft/refactorEmitter
Re-factoring emitter for emitting ES6 feature natively and down-level
This commit is contained in:
commit
bf4bf27aab
@ -9022,7 +9022,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;
|
||||
}
|
||||
|
||||
@ -2236,33 +2236,35 @@ 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 emitDownlevelShorthandPropertyAssignment(node: ShorthandPropertyDeclaration) {
|
||||
emitLeadingComments(node);
|
||||
// Emit identifier as an identifier
|
||||
emit(node.name);
|
||||
write(": ");
|
||||
// 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);
|
||||
}
|
||||
|
||||
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. 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) {
|
||||
emitDownlevelShorthandPropertyAssignment(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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3483,6 +3485,7 @@ module ts {
|
||||
return emitPinnedOrTripleSlashComments(node);
|
||||
}
|
||||
|
||||
// Check if the node can be emitted regardless of the ScriptTarget
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.Identifier:
|
||||
return emitIdentifier(<Identifier>node);
|
||||
@ -3521,8 +3524,6 @@ module ts {
|
||||
return emitObjectLiteral(<ObjectLiteralExpression>node);
|
||||
case SyntaxKind.PropertyAssignment:
|
||||
return emitPropertyAssignment(<PropertyDeclaration>node);
|
||||
case SyntaxKind.ShorthandPropertyAssignment:
|
||||
return emitShortHandPropertyAssignment(<ShortHandPropertyDeclaration>node);
|
||||
case SyntaxKind.ComputedPropertyName:
|
||||
return emitComputedPropertyName(<ComputedPropertyName>node);
|
||||
case SyntaxKind.PropertyAccessExpression:
|
||||
@ -3618,6 +3619,23 @@ 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-level
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ShorthandPropertyAssignment:
|
||||
return emitDownlevelShorthandPropertyAssignment(<ShorthandPropertyDeclaration>node);
|
||||
}
|
||||
}
|
||||
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(<ShorthandPropertyDeclaration>node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function hasDetachedComments(pos: number) {
|
||||
|
||||
@ -3190,7 +3190,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) {
|
||||
var shorthandDeclaration = <ShortHandPropertyDeclaration>createNode(SyntaxKind.ShorthandPropertyAssignment, nodePos);
|
||||
var shorthandDeclaration = <ShorthandPropertyDeclaration>createNode(SyntaxKind.ShorthandPropertyAssignment, nodePos);
|
||||
shorthandDeclaration.name = <Identifier>propertyName;
|
||||
shorthandDeclaration.questionToken = questionToken;
|
||||
return finishNode(shorthandDeclaration);
|
||||
@ -4438,7 +4438,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.ThrowStatement: return checkThrowStatement(<ThrowStatement>node);
|
||||
@ -5455,7 +5455,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, node.questionToken, Diagnostics.An_object_member_cannot_be_declared_optional);
|
||||
}
|
||||
|
||||
|
||||
@ -386,7 +386,7 @@ module ts {
|
||||
export type VariableOrParameterDeclaration = VariableDeclaration | ParameterDeclaration;
|
||||
export type VariableOrParameterOrPropertyDeclaration = VariableOrParameterDeclaration | PropertyDeclaration;
|
||||
|
||||
export interface ShortHandPropertyDeclaration extends Declaration {
|
||||
export interface ShorthandPropertyDeclaration extends Declaration {
|
||||
name: Identifier;
|
||||
questionToken?: Node;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user