Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IsolatedDeclarations annotations can duplicate comments #61497

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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);
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;
}`,
});