Skip to content

Commit cd185f2

Browse files
committed
Add declaration emit support
1 parent 64d2698 commit cd185f2

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/compiler/declarationEmitter.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ namespace ts {
416416
case SyntaxKind.TypeOperator:
417417
return emitTypeOperator(<TypeOperatorNode>type);
418418
case SyntaxKind.IndexedAccessType:
419-
return emitPropertyAccessType(<IndexedAccessTypeNode>type);
419+
return emitIndexedAccessType(<IndexedAccessTypeNode>type);
420+
case SyntaxKind.MappedType:
421+
return emitMappedType(<MappedTypeNode>type);
420422
case SyntaxKind.FunctionType:
421423
case SyntaxKind.ConstructorType:
422424
return emitSignatureDeclarationWithJsDocComments(<FunctionOrConstructorTypeNode>type);
@@ -516,13 +518,39 @@ namespace ts {
516518
emitType(type.type);
517519
}
518520

519-
function emitPropertyAccessType(node: IndexedAccessTypeNode) {
521+
function emitIndexedAccessType(node: IndexedAccessTypeNode) {
520522
emitType(node.objectType);
521523
write("[");
522524
emitType(node.indexType);
523525
write("]");
524526
}
525527

528+
function emitMappedType(node: MappedTypeNode) {
529+
const prevEnclosingDeclaration = enclosingDeclaration;
530+
enclosingDeclaration = node;
531+
write("{");
532+
writeLine();
533+
increaseIndent();
534+
if (node.readonlyToken) {
535+
write("readonly ");
536+
}
537+
write("[");
538+
writeEntityName(node.typeParameter.name);
539+
write(" in ");
540+
emitType(node.typeParameter.constraint);
541+
write("]");
542+
if (node.questionToken) {
543+
write("?");
544+
}
545+
write(": ");
546+
emitType(node.type);
547+
write(";");
548+
writeLine();
549+
decreaseIndent();
550+
write("}");
551+
enclosingDeclaration = prevEnclosingDeclaration;
552+
}
553+
526554
function emitTypeLiteral(type: TypeLiteralNode) {
527555
write("{");
528556
if (type.members.length) {

src/compiler/emitter.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,9 @@ const _super = (function (geti, seti) {
606606
case SyntaxKind.TypeOperator:
607607
return emitTypeOperator(<TypeOperatorNode>node);
608608
case SyntaxKind.IndexedAccessType:
609-
return emitPropertyAccessType(<IndexedAccessTypeNode>node);
609+
return emitIndexedAccessType(<IndexedAccessTypeNode>node);
610+
case SyntaxKind.MappedType:
611+
return emitMappedType(<MappedTypeNode>node);
610612
case SyntaxKind.LiteralType:
611613
return emitLiteralType(<LiteralTypeNode>node);
612614

@@ -1109,13 +1111,36 @@ const _super = (function (geti, seti) {
11091111
emit(node.type);
11101112
}
11111113

1112-
function emitPropertyAccessType(node: IndexedAccessTypeNode) {
1114+
function emitIndexedAccessType(node: IndexedAccessTypeNode) {
11131115
emit(node.objectType);
11141116
write("[");
11151117
emit(node.indexType);
11161118
write("]");
11171119
}
11181120

1121+
function emitMappedType(node: MappedTypeNode) {
1122+
write("{");
1123+
writeLine();
1124+
increaseIndent();
1125+
if (node.readonlyToken) {
1126+
write("readonly ");
1127+
}
1128+
write("[");
1129+
emit(node.typeParameter.name);
1130+
write(" in ");
1131+
emit(node.typeParameter.constraint);
1132+
write("]");
1133+
if (node.questionToken) {
1134+
write("?");
1135+
}
1136+
write(": ");
1137+
emit(node.type);
1138+
write(";");
1139+
writeLine();
1140+
decreaseIndent();
1141+
write("}");
1142+
}
1143+
11191144
function emitLiteralType(node: LiteralTypeNode) {
11201145
emitExpression(node.literal);
11211146
}

0 commit comments

Comments
 (0)