Skip to content

Commit 951084f

Browse files
committed
Fixes emit of type predicated in delcaration files
1 parent 43febe5 commit 951084f

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/compiler/declarationEmitter.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ namespace ts {
188188
if (!moduleElementEmitInfo && asynchronousSubModuleDeclarationEmitInfo) {
189189
moduleElementEmitInfo = forEach(asynchronousSubModuleDeclarationEmitInfo, declEmitInfo => declEmitInfo.node === nodeToCheck ? declEmitInfo : undefined);
190190
}
191-
191+
192192
// If the alias was marked as not visible when we saw its declaration, we would have saved the aliasEmitInfo, but if we haven't yet visited the alias declaration
193193
// then we don't need to write it at this point. We will write it when we actually see its declaration
194194
// Eg.
@@ -198,7 +198,7 @@ namespace ts {
198198
// we would write alias foo declaration when we visit it since it would now be marked as visible
199199
if (moduleElementEmitInfo) {
200200
if (moduleElementEmitInfo.node.kind === SyntaxKind.ImportDeclaration) {
201-
// we have to create asynchronous output only after we have collected complete information
201+
// we have to create asynchronous output only after we have collected complete information
202202
// because it is possible to enable multiple bindings as asynchronously visible
203203
moduleElementEmitInfo.isVisible = true;
204204
}
@@ -353,6 +353,21 @@ namespace ts {
353353
return emitEntityName(<Identifier>type);
354354
case SyntaxKind.QualifiedName:
355355
return emitEntityName(<QualifiedName>type);
356+
case SyntaxKind.TypePredicate:
357+
return emitTypePredicate(<TypePredicateNode>type);
358+
}
359+
360+
function writeEntityName(entityName: EntityName | Expression) {
361+
if (entityName.kind === SyntaxKind.Identifier) {
362+
writeTextOfNode(currentSourceFile, entityName);
363+
}
364+
else {
365+
let left = entityName.kind === SyntaxKind.QualifiedName ? (<QualifiedName>entityName).left : (<PropertyAccessExpression>entityName).expression;
366+
let right = entityName.kind === SyntaxKind.QualifiedName ? (<QualifiedName>entityName).right : (<PropertyAccessExpression>entityName).name;
367+
writeEntityName(left);
368+
write(".");
369+
writeTextOfNode(currentSourceFile, right);
370+
}
356371
}
357372

358373
function emitEntityName(entityName: EntityName | PropertyAccessExpression) {
@@ -362,19 +377,6 @@ namespace ts {
362377

363378
handleSymbolAccessibilityError(visibilityResult);
364379
writeEntityName(entityName);
365-
366-
function writeEntityName(entityName: EntityName | Expression) {
367-
if (entityName.kind === SyntaxKind.Identifier) {
368-
writeTextOfNode(currentSourceFile, entityName);
369-
}
370-
else {
371-
let left = entityName.kind === SyntaxKind.QualifiedName ? (<QualifiedName>entityName).left : (<PropertyAccessExpression>entityName).expression;
372-
let right = entityName.kind === SyntaxKind.QualifiedName ? (<QualifiedName>entityName).right : (<PropertyAccessExpression>entityName).name;
373-
writeEntityName(left);
374-
write(".");
375-
writeTextOfNode(currentSourceFile, right);
376-
}
377-
}
378380
}
379381

380382
function emitExpressionWithTypeArguments(node: ExpressionWithTypeArguments) {
@@ -398,6 +400,12 @@ namespace ts {
398400
}
399401
}
400402

403+
function emitTypePredicate(type: TypePredicateNode) {
404+
writeEntityName(type.parameterName);
405+
write(" is ");
406+
emitType(type.type);
407+
}
408+
401409
function emitTypeQuery(type: TypeQueryNode) {
402410
write("typeof ");
403411
emitEntityName(type.exprName);
@@ -600,7 +608,7 @@ namespace ts {
600608
}
601609

602610
function writeImportEqualsDeclaration(node: ImportEqualsDeclaration) {
603-
// note usage of writer. methods instead of aliases created, just to make sure we are using
611+
// note usage of writer. methods instead of aliases created, just to make sure we are using
604612
// correct writer especially to handle asynchronous alias writing
605613
emitJsDocComments(node);
606614
if (node.flags & NodeFlags.Export) {
@@ -642,7 +650,7 @@ namespace ts {
642650

643651
function writeImportDeclaration(node: ImportDeclaration) {
644652
if (!node.importClause && !(node.flags & NodeFlags.Export)) {
645-
// do not write non-exported import declarations that don't have import clauses
653+
// do not write non-exported import declarations that don't have import clauses
646654
return;
647655
}
648656
emitJsDocComments(node);
@@ -1517,7 +1525,7 @@ namespace ts {
15171525
}
15181526
}
15191527
}
1520-
}
1528+
}
15211529
}
15221530

15231531
function emitNode(node: Node) {
@@ -1577,7 +1585,7 @@ namespace ts {
15771585
referencePathsOutput += "/// <reference path=\"" + declFileName + "\" />" + newLine;
15781586
}
15791587
}
1580-
1588+
15811589
/* @internal */
15821590
export function writeDeclarationFile(jsFilePath: string, sourceFile: SourceFile, host: EmitHost, resolver: EmitResolver, diagnostics: Diagnostic[]) {
15831591
let emitDeclarationResult = emitDeclarations(host, resolver, diagnostics, jsFilePath, sourceFile);

tests/cases/compiler/declFileFunctions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export function fooWithSingleOverload(a: any) {
2828
return a;
2929
}
3030

31+
export function fooWithTypePredicate(a: any): a is number {
32+
return true;
33+
}
34+
3135
/** This comment should appear for nonExportedFoo*/
3236
function nonExportedFoo() {
3337
}

0 commit comments

Comments
 (0)