Skip to content

Commit b346f57

Browse files
authored
prefix-unused-parameter-with-_ codefix now works in jsdoc @param (microsoft#36152)
* 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. * Replace FindAllReferences.Core.eachSymbolReferenceInFile function call to more ligher call of getJSDocParameterTags when searching for a parameter in jsdoc. * Remove redundant constant declaration. * Add test for prefix single unused parameter in jsdoc.
1 parent deb5bac commit b346f57

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/services/codefixes/fixUnusedIdentifier.ts

+7
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ namespace ts.codefix {
154154
}
155155
if (isIdentifier(token) && canPrefix(token)) {
156156
changes.replaceNode(sourceFile, token, createIdentifier(`_${token.text}`));
157+
if (isParameter(token.parent)) {
158+
getJSDocParameterTags(token.parent).forEach((tag) => {
159+
if (isIdentifier(tag.name)) {
160+
changes.replaceNode(sourceFile, tag.name, createIdentifier(`_${tag.name.text}`));
161+
}
162+
});
163+
}
157164
}
158165
}
159166

tests/cases/fourslash/codeFixUnusedIdentifier_all_prefix.ts

+9-1
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;`,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
/////**
7+
//// * @param a
8+
//// * @param b
9+
//// */
10+
////function f(a, b) {
11+
//// const x = a;
12+
////}
13+
14+
verify.codeFix({
15+
description: "Prefix 'b' with an underscore",
16+
index: 1,
17+
newFileContent:
18+
`/**
19+
* @param a
20+
* @param _b
21+
*/
22+
function f(a, _b) {
23+
const x = a;
24+
}`,
25+
});

0 commit comments

Comments
 (0)