Skip to content

Commit f9764d1

Browse files
author
Andy
authored
fixUnusedIdentifier: Support deleting @template tag (microsoft#25597)
* fixUnusedIdentifier: Support deleting @template tag * Just return createTextRangeFromNode instead of adjusting range
1 parent bd7b97c commit f9764d1

File tree

3 files changed

+96
-12
lines changed

3 files changed

+96
-12
lines changed

src/services/textChanges.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,18 +1053,29 @@ namespace ts.textChanges {
10531053
break;
10541054

10551055
case SyntaxKind.TypeParameter: {
1056-
const typeParameters = getEffectiveTypeParameterDeclarations(<DeclarationWithTypeParameters>node.parent);
1057-
if (typeParameters.length === 1) {
1058-
const { pos, end } = cast(typeParameters, isNodeArray);
1059-
const previousToken = getTokenAtPosition(sourceFile, pos - 1);
1060-
const nextToken = getTokenAtPosition(sourceFile, end);
1061-
Debug.assert(previousToken.kind === SyntaxKind.LessThanToken);
1062-
Debug.assert(nextToken.kind === SyntaxKind.GreaterThanToken);
1063-
1064-
changes.deleteNodeRange(sourceFile, previousToken, nextToken);
1065-
}
1066-
else {
1067-
deleteNodeInList(changes, deletedNodesInLists, sourceFile, node);
1056+
const typeParam = node as TypeParameterDeclaration;
1057+
switch (typeParam.parent.kind) {
1058+
case SyntaxKind.JSDocTemplateTag:
1059+
changes.deleteRange(sourceFile, getRangeToDeleteJsDocTag(typeParam.parent, sourceFile));
1060+
break;
1061+
case SyntaxKind.InferType:
1062+
// TODO: GH#25594
1063+
break;
1064+
default: {
1065+
const typeParameters = getEffectiveTypeParameterDeclarations(typeParam.parent);
1066+
if (typeParameters.length === 1) {
1067+
const { pos, end } = cast(typeParameters, isNodeArray);
1068+
const previousToken = getTokenAtPosition(sourceFile, pos - 1);
1069+
const nextToken = getTokenAtPosition(sourceFile, end);
1070+
Debug.assert(previousToken.kind === SyntaxKind.LessThanToken);
1071+
Debug.assert(nextToken.kind === SyntaxKind.GreaterThanToken);
1072+
1073+
changes.deleteNodeRange(sourceFile, previousToken, nextToken);
1074+
}
1075+
else {
1076+
deleteNodeInList(changes, deletedNodesInLists, sourceFile, node);
1077+
}
1078+
}
10681079
}
10691080
break;
10701081
}
@@ -1166,6 +1177,12 @@ namespace ts.textChanges {
11661177
Debug.assertNever(gp);
11671178
}
11681179
}
1180+
1181+
function getRangeToDeleteJsDocTag(node: JSDocTag, sourceFile: SourceFile): TextRange {
1182+
const { parent } = node;
1183+
const toDelete = parent.kind === SyntaxKind.JSDocComment && parent.comment === undefined && parent.tags!.length === 1 ? parent : node;
1184+
return createTextRangeFromNode(toDelete, sourceFile);
1185+
}
11691186
}
11701187

11711188
/** Warning: This deletes comments too. See `copyComments` in `convertFunctionToEs6Class`. */

src/services/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,10 @@ namespace ts {
11601160
return createTextSpanFromBounds(node.getStart(sourceFile), node.getEnd());
11611161
}
11621162

1163+
export function createTextRangeFromNode(node: Node, sourceFile: SourceFile): TextRange {
1164+
return createTextRange(node.getStart(sourceFile), node.end);
1165+
}
1166+
11631167
export function createTextSpanFromRange(range: TextRange): TextSpan {
11641168
return createTextSpanFromBounds(range.pos, range.end);
11651169
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowJs: true
4+
5+
// @Filename: /a.js
6+
/////** @template T Parameter doc comment */
7+
////function f() {}
8+
////
9+
/////**
10+
//// * Doc
11+
//// * @template T Comment
12+
//// */
13+
////function g() {}
14+
////
15+
/////**
16+
//// * Doc
17+
//// * @template T Comment
18+
//// * @template U Comment
19+
//// */
20+
////function h() {}
21+
////
22+
/////** @template T Comment @return {void} */
23+
////function i() {}
24+
////
25+
/////**
26+
////Doc
27+
////@template T comment
28+
////@template U comment
29+
////@param {number} x
30+
////*/
31+
////function j(x) { return x; }
32+
33+
verify.codeFixAll({
34+
fixId: "unusedIdentifier_delete",
35+
fixAllDescription: "Delete all unused declarations",
36+
newFileContent:
37+
`
38+
function f() {}
39+
40+
/**
41+
* Doc
42+
* Comment
43+
*/
44+
function g() {}
45+
46+
/**
47+
* Doc
48+
* Comment
49+
* Comment
50+
*/
51+
function h() {}
52+
53+
/** Comment @return {void} */
54+
function i() {}
55+
56+
/**
57+
Doc
58+
comment
59+
comment
60+
@param {number} x
61+
*/
62+
function j(x) { return x; }`,
63+
});

0 commit comments

Comments
 (0)