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
22 changes: 21 additions & 1 deletion src/services/codefixes/convertTypedefToType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import {
Diagnostics,
factory,
findAncestor,
flatMap,
getNewLineOrDefaultFromHost,
getSynthesizedDeepClone,
Expand Down Expand Up @@ -122,7 +123,26 @@ function doChange(
}
}

changes.replaceRange(sourceFile, { pos, end }, declaration, { prefix, suffix });
const classParent = findAncestor(node, n => n.kind === SyntaxKind.ClassDeclaration);

if (classParent) {
changes.insertNodeBefore(
sourceFile,
classParent,
declaration,
/*blankLineBetween*/ false,
);
// Replace the current node with an empty line
changes.replaceNodeWithText(sourceFile, node, "");

// Exit the function as the changes are already applied
return;
}

changes.replaceRange(sourceFile, { pos, end }, declaration, {
prefix,
suffix,
});
}

function getLeftAndRightSiblings(typedefNode: JSDocTypedefTag): { leftSibling?: Node; rightSibling?: Node; } {
Expand Down
31 changes: 31 additions & 0 deletions tests/cases/fourslash/codeFixConvertTypedefToType8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// <reference path='fourslash.ts' />

//// class Example {
//// /**
//// * List of items to be rendered in the bar chart
//// * @typedef {{ count: number }} Counter
//// * @returns {Counter}
//// */
//// get something() {
//// return { count: 0 };
//// }
//// }
////

verify.codeFix({
description: ts.Diagnostics.Convert_typedef_to_TypeScript_type.message,
index: 0,
newFileContent:
`type Counter = { count: number; };
class Example {
/**
* List of items to be rendered in the bar chart
*
* @returns {Counter}
*/
get something() {
return { count: 0 };
}
}
`,
});