Skip to content

Commit dde6f97

Browse files
committed
Fix prepending unused TypeScript variables with underscore doesn't rename JSDoc @param.
Fix test for quick fix "Prefix all unused declarations with '_' where possible". Fixes microsoft#33021.
1 parent d6c05a1 commit dde6f97

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/services/codefixes/fixUnusedIdentifier.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ namespace ts.codefix {
6060
}
6161
}
6262

63-
const prefix = textChanges.ChangeTracker.with(context, t => tryPrefixDeclaration(t, errorCode, sourceFile, token));
63+
const prefix = textChanges.ChangeTracker.with(context, t => tryPrefixDeclaration(t, errorCode, sourceFile, checker, token));
6464
if (prefix.length) {
6565
result.push(createCodeFixAction(fixName, prefix, [Diagnostics.Prefix_0_with_an_underscore, token.getText(sourceFile)], fixIdPrefix, Diagnostics.Prefix_all_unused_declarations_with_where_possible));
6666
}
@@ -76,7 +76,7 @@ namespace ts.codefix {
7676
const token = getTokenAtPosition(sourceFile, diag.start);
7777
switch (context.fixId) {
7878
case fixIdPrefix:
79-
tryPrefixDeclaration(changes, diag.code, sourceFile, token);
79+
tryPrefixDeclaration(changes, diag.code, sourceFile, checker, token);
8080
break;
8181
case fixIdDelete: {
8282
if (token.kind === SyntaxKind.InferKeyword) break; // Can't delete
@@ -146,14 +146,19 @@ namespace ts.codefix {
146146
return false;
147147
}
148148

149-
function tryPrefixDeclaration(changes: textChanges.ChangeTracker, errorCode: number, sourceFile: SourceFile, token: Node): void {
149+
function tryPrefixDeclaration(changes: textChanges.ChangeTracker, errorCode: number, sourceFile: SourceFile, checker: TypeChecker, token: Node): void {
150150
// Don't offer to prefix a property.
151151
if (errorCode === Diagnostics.Property_0_is_declared_but_its_value_is_never_read.code) return;
152152
if (token.kind === SyntaxKind.InferKeyword) {
153153
token = cast(token.parent, isInferTypeNode).typeParameter.name;
154154
}
155155
if (isIdentifier(token) && canPrefix(token)) {
156156
changes.replaceNode(sourceFile, token, createIdentifier(`_${token.text}`));
157+
FindAllReferences.Core.eachSymbolReferenceInFile(token, checker, sourceFile, (ref: Node) => {
158+
if (isIdentifier(ref)) {
159+
changes.replaceNode(sourceFile, ref, createIdentifier(`_${ref.text}`));
160+
}
161+
});
157162
}
158163
}
159164

tests/cases/fourslash/codeFixUnusedIdentifier_all_prefix.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// @noUnusedLocals: true
44
// @noUnusedParameters: true
55

6+
/////**
7+
//// * @param a First parameter.
8+
//// * @param b Second parameter.
9+
//// */
610
////function f(a, b) {
711
//// const x = 0; // Can't be prefixed, ignored
812
////}
@@ -12,7 +16,11 @@ verify.codeFixAll({
1216
fixId: "unusedIdentifier_prefix",
1317
fixAllDescription: "Prefix all unused declarations with '_' where possible",
1418
newFileContent:
15-
`function f(_a, _b) {
19+
`/**
20+
* @param _a First parameter.
21+
* @param _b Second parameter.
22+
*/
23+
function f(_a, _b) {
1624
const x = 0; // Can't be prefixed, ignored
1725
}
1826
type Length<T> = T extends ArrayLike<infer _U> ? number : never;`,

0 commit comments

Comments
 (0)