Skip to content

prefix-unused-parameter-with-_ codefix now works in jsdoc @param #36152

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

Merged
merged 6 commits into from
Apr 17, 2020
Merged
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
7 changes: 7 additions & 0 deletions src/services/codefixes/fixUnusedIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ namespace ts.codefix {
}
if (isIdentifier(token) && canPrefix(token)) {
changes.replaceNode(sourceFile, token, createIdentifier(`_${token.text}`));
if (isParameter(token.parent)) {
getJSDocParameterTags(token.parent).forEach((tag) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don’t you need to do this only for the JSDoc parameter tags whose name is the same as token.text? Try adding this test:

/**
 * @param a
 * @param b
 */
function f(a, b) {
    const x = a;
}

to ensure that b gets renamed but a does not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see getJSDocParameterTags already handles that. My bad. Thanks for adding the test anyway.

if (isIdentifier(tag.name)) {
changes.replaceNode(sourceFile, tag.name, createIdentifier(`_${tag.name.text}`));
}
});
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion tests/cases/fourslash/codeFixUnusedIdentifier_all_prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// @noUnusedLocals: true
// @noUnusedParameters: true

/////**
//// * @param a First parameter.
//// * @param b Second parameter.
//// */
////function f(a, b) {
//// const x = 0; // Can't be prefixed, ignored
////}
Expand All @@ -12,7 +16,11 @@ verify.codeFixAll({
fixId: "unusedIdentifier_prefix",
fixAllDescription: "Prefix all unused declarations with '_' where possible",
newFileContent:
`function f(_a, _b) {
`/**
* @param _a First parameter.
* @param _b Second parameter.
*/
function f(_a, _b) {
const x = 0; // Can't be prefixed, ignored
}
type Length<T> = T extends ArrayLike<infer _U> ? number : never;`,
Expand Down
25 changes: 25 additions & 0 deletions tests/cases/fourslash/codeFixUnusedIdentifier_prefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path='fourslash.ts' />

// @noUnusedLocals: true
// @noUnusedParameters: true

/////**
//// * @param a
//// * @param b
//// */
////function f(a, b) {
//// const x = a;
////}

verify.codeFix({
description: "Prefix 'b' with an underscore",
index: 1,
newFileContent:
`/**
* @param a
* @param _b
*/
function f(a, _b) {
const x = a;
}`,
});