Skip to content
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

feat(noUnusedFunctionParameters): add lint for unused function parameters, instead of using noUnusedVariables #2899

Merged
merged 1 commit into from
May 31, 2024

Conversation

printfn
Copy link
Contributor

@printfn printfn commented May 17, 2024

Summary

This adds a new lint noUnusedFunctionParameters that detects unused parameters. Previously these were detected by noUnusedVariables.

Test Plan

I added tests for the new lint.

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages A-Changelog Area: changelog labels May 17, 2024
@printfn printfn force-pushed the ignore-unused-args branch from 6107073 to 91d7d43 Compare May 17, 2024 10:44
Copy link

codspeed-hq bot commented May 17, 2024

CodSpeed Performance Report

Merging #2899 will not alter performance

Comparing printfn:ignore-unused-args (34b0b56) with main (9e4feb6)

Summary

✅ 92 untouched benchmarks

@printfn printfn force-pushed the ignore-unused-args branch from 91d7d43 to 3ccb9f3 Compare May 22, 2024 07:15
@github-actions github-actions bot added the A-CLI Area: CLI label May 22, 2024
@printfn
Copy link
Contributor Author

printfn commented May 22, 2024

Rebased and fixed merge conflicts. Would someone be able to review this?

@ematipico
Copy link
Member

What's the reasoning that drove you to implement the option? This reasoning will be very useful in explaining to the users why this option exists and how they can use it in their code.

@printfn
Copy link
Contributor Author

printfn commented May 22, 2024

I'm using biome on a large legacy codebase, slowly migrating from ESLint. Having the option to enable noUnusedVariables would be super helpful in improving code quality, but there are thousands of unused arguments that shouldn't have to meaninglessly be renamed to have an underscore in order to still benefit from linting for unused variables.

I think that having unused arguments is much less of a code smell than entirely unused variables. In fact, especially when creating and passing around lambda functions, having unused arguments present can be really useful to readers so they immediately know that another argument is available if the code needs to be modified in the future.

While prefixing arguments with an underscore is still an option, in my case this would require renaming thousands of parameters and introducing a non-trivial amount of line noise.

One pattern that frequently comes up is:

foo((a, b, c) => b);

In code like this, having the option to provide useful argument names without having to prefix everything is very useful.

Common examples:

return new Promise((accept, reject) => { ... }); // not using `reject`

let arr = loadArr(); // `arr` has type `[string, string][]`
arr.filter(([k, v]) => v.length > 0); // `k` is unused

componentDidUpdate(prevProps: Props, prevState: State) { ... } // not using `prevState`

While in many of the examples it might make sense to remove the extra argument or add an underscore, doing this thousands of times across a legacy codebase is unreasonable and shouldn't be necessary to still benefit from noUnusedVariables.

@printfn
Copy link
Contributor Author

printfn commented May 22, 2024

It's also worth pointing out that there are many repos on GitHub that set ESLint's args to none:

https://github.com/search?q=%2F%22no-unused-vars%22%3A%5Cs*%5C%5B%5Cs*%22%28warn%7Cerror%29%22%2C%5Cs*%5C%7B.*%22args%22%3A%5Cs*%22none%22.*%5C%7D%5C%5D%2F&type=code

@printfn printfn force-pushed the ignore-unused-args branch from 3ccb9f3 to 1813165 Compare May 22, 2024 08:41
@Conaclos
Copy link
Member

I think we could go beyond and create a new rule noUnusedFunctionParameters.
TSC has two "rule" for unused variables: noUnusedLocals and noUnusedParameters.
In the past we created noUnusedImports.

@printfn
Copy link
Contributor Author

printfn commented May 22, 2024

I'll give making a new noUnusedFunctionParameters rule a go.

@printfn printfn force-pushed the ignore-unused-args branch from 1813165 to 91598b5 Compare May 22, 2024 10:38
@github-actions github-actions bot added A-Project Area: project A-Diagnostic Area: diagnostocis labels May 22, 2024
@printfn printfn changed the title feat(noUnusedVariables): add option to ignore unused function arguments feat(noUnusedFunctionParameters): add lint for unused function parameters, instead of using noUnusedVariables May 22, 2024
@printfn printfn force-pushed the ignore-unused-args branch from 91598b5 to 3cf1a4f Compare May 22, 2024 11:20
@printfn
Copy link
Contributor Author

printfn commented May 22, 2024

Alright, pretty sure I got it all working now. I've added a new nursery lint noUnusedFunctionParameters and changed noUnusedVariables to no longer include function parameters.

@printfn printfn force-pushed the ignore-unused-args branch from 3cf1a4f to cefc988 Compare May 22, 2024 11:29
Comment on lines 176 to 190
AnyJsBindingDeclaration::TsPropertyParameter(_) => None,
AnyJsBindingDeclaration::JsFormalParameter(parameter) => {
if is_function_that_is_ok_parameter_not_be_used(&parameter.parent_function()) {
None
} else {
suggestion_for_binding(binding)
}
}
AnyJsBindingDeclaration::JsRestParameter(parameter) => {
if is_function_that_is_ok_parameter_not_be_used(&parameter.parent_function()) {
None
} else {
suggestion_for_binding(binding)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This changes the behaviour of the rule, and it's a breaking change. We should change this only when the new rule goes out of nursery.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've reverted the changes to noUnusedVariables now

Comment on lines 29 to 35
/// This rule won't report unused imports.
/// This rule won't report unused imports or unused function parameters.
/// If you want to report unused imports,
/// enable [noUnusedImports](https://biomejs.dev/linter/rules/no-unused-imports/).
/// enable [noUnusedImports](https://biomejs.dev/linter/rules/no-unused-imports/);
/// to report unused function parameters, enable
/// [noUnusedFunctionParameters](https://biomejs.dev/linter/rules/no-unused-function-parameters/)
Copy link
Member

Choose a reason for hiding this comment

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

We can't change the documentation like this. This change will go straight to production and users will be misled. I think we should add a new paragraph like this:

From `v1.9.0`, the rule won't check unused function parameters anymore, users should switch to [noUnusedFunctionParameters](https://biomejs.dev/linter/rules/no-unused-function-parameters/)

I wrote 1.9.0 because in v1.8.0 the new rule is nursery, and we can't suggest a rule that is still in their incubation period.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated docs as suggested

@printfn printfn force-pushed the ignore-unused-args branch 2 times, most recently from eee2e8d to 1169038 Compare May 23, 2024 06:31
@printfn printfn requested a review from ematipico May 23, 2024 06:46
@printfn printfn force-pushed the ignore-unused-args branch 2 times, most recently from bd770e2 to c37f596 Compare May 25, 2024 22:47
@printfn
Copy link
Contributor Author

printfn commented May 27, 2024

this is ready for another review @ematipico

@printfn printfn force-pushed the ignore-unused-args branch from c37f596 to bceefb3 Compare May 29, 2024 08:50
@printfn printfn force-pushed the ignore-unused-args branch from bceefb3 to 34b0b56 Compare May 31, 2024 11:02
@ematipico ematipico merged commit bc30892 into biomejs:main May 31, 2024
12 checks passed
@printfn printfn deleted the ignore-unused-args branch May 31, 2024 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Changelog Area: changelog A-CLI Area: CLI A-Diagnostic Area: diagnostocis A-Linter Area: linter A-Project Area: project L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants