Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/services/codefixes/fixMissingTypeAnnotationOnExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
factory,
FileTextChanges,
findAncestor,
forEachChild,
FunctionDeclaration,
GeneratedIdentifierFlags,
getEmitScriptTarget,
Expand Down Expand Up @@ -1097,6 +1098,11 @@ function withContext<T>(
return emptyInferenceResult;
}

function stripCommentsFromNode(node: Node): void {
setEmitFlags(node, EmitFlags.NoComments);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably this should just be removeAllComments?

forEachChild(node, stripCommentsFromNode);
}

function typeToTypeNode(type: Type, enclosingDeclaration: Node, flags = NodeBuilderFlags.None): TypeNode | undefined {
let isTruncated = false;
const minimizedTypeNode = typeToMinimizedReferenceType(typeChecker, type, enclosingDeclaration, declarationEmitNodeBuilderFlags | flags, declarationEmitInternalNodeBuilderFlags, {
Expand All @@ -1112,6 +1118,11 @@ function withContext<T>(
return undefined;
}
const result = typeNodeToAutoImportableTypeNode(minimizedTypeNode, importAdder, scriptTarget);

if (result) {
stripCommentsFromNode(result);
}

return isTruncated ? factory.createKeywordTypeNode(SyntaxKind.AnyKeyword) : result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/// <reference path='fourslash.ts'/>

// @isolatedDeclarations: true
// @declaration: true

//// export function f() {
//// const o = /** before */ { /* inline post-{ */ // end line post-{
//// // document first type
//// /* inline before */ x /* inline pre-colon */ : /* inline pre-type */ 5 /* inline post-type */ , // after comma1
//// // document second type
//// /** 2 before */ y : 'str' /** 2 after */, //after comma2
//// // pre-closing
//// } /** after */;
//// return o;
//// }

verify.codeFix({
description: `Add return type '{ /* inline post-{ */ // end line post-{
x: number; // after comma1
y: string; }'`,
index: 0,
newFileContent:
`export function f(): {
x: number;
y: string;
} {
const o = /** before */ { /* inline post-{ */ // end line post-{
// document first type
/* inline before */ x /* inline pre-colon */ : /* inline pre-type */ 5 /* inline post-type */ , // after comma1
// document second type
/** 2 before */ y : 'str' /** 2 after */, //after comma2
// pre-closing
} /** after */;
return o;
}`,
});