Add declaration emit support

This commit is contained in:
Anders Hejlsberg 2016-11-10 15:20:29 -08:00
parent 64d269885c
commit cd185f2cf6
2 changed files with 57 additions and 4 deletions

View File

@ -416,7 +416,9 @@ namespace ts {
case SyntaxKind.TypeOperator:
return emitTypeOperator(<TypeOperatorNode>type);
case SyntaxKind.IndexedAccessType:
return emitPropertyAccessType(<IndexedAccessTypeNode>type);
return emitIndexedAccessType(<IndexedAccessTypeNode>type);
case SyntaxKind.MappedType:
return emitMappedType(<MappedTypeNode>type);
case SyntaxKind.FunctionType:
case SyntaxKind.ConstructorType:
return emitSignatureDeclarationWithJsDocComments(<FunctionOrConstructorTypeNode>type);
@ -516,13 +518,39 @@ namespace ts {
emitType(type.type);
}
function emitPropertyAccessType(node: IndexedAccessTypeNode) {
function emitIndexedAccessType(node: IndexedAccessTypeNode) {
emitType(node.objectType);
write("[");
emitType(node.indexType);
write("]");
}
function emitMappedType(node: MappedTypeNode) {
const prevEnclosingDeclaration = enclosingDeclaration;
enclosingDeclaration = node;
write("{");
writeLine();
increaseIndent();
if (node.readonlyToken) {
write("readonly ");
}
write("[");
writeEntityName(node.typeParameter.name);
write(" in ");
emitType(node.typeParameter.constraint);
write("]");
if (node.questionToken) {
write("?");
}
write(": ");
emitType(node.type);
write(";");
writeLine();
decreaseIndent();
write("}");
enclosingDeclaration = prevEnclosingDeclaration;
}
function emitTypeLiteral(type: TypeLiteralNode) {
write("{");
if (type.members.length) {

View File

@ -606,7 +606,9 @@ const _super = (function (geti, seti) {
case SyntaxKind.TypeOperator:
return emitTypeOperator(<TypeOperatorNode>node);
case SyntaxKind.IndexedAccessType:
return emitPropertyAccessType(<IndexedAccessTypeNode>node);
return emitIndexedAccessType(<IndexedAccessTypeNode>node);
case SyntaxKind.MappedType:
return emitMappedType(<MappedTypeNode>node);
case SyntaxKind.LiteralType:
return emitLiteralType(<LiteralTypeNode>node);
@ -1109,13 +1111,36 @@ const _super = (function (geti, seti) {
emit(node.type);
}
function emitPropertyAccessType(node: IndexedAccessTypeNode) {
function emitIndexedAccessType(node: IndexedAccessTypeNode) {
emit(node.objectType);
write("[");
emit(node.indexType);
write("]");
}
function emitMappedType(node: MappedTypeNode) {
write("{");
writeLine();
increaseIndent();
if (node.readonlyToken) {
write("readonly ");
}
write("[");
emit(node.typeParameter.name);
write(" in ");
emit(node.typeParameter.constraint);
write("]");
if (node.questionToken) {
write("?");
}
write(": ");
emit(node.type);
write(";");
writeLine();
decreaseIndent();
write("}");
}
function emitLiteralType(node: LiteralTypeNode) {
emitExpression(node.literal);
}