noParametersOnlyUsedInRecursion
此内容尚不支持你的语言。
Summary
Section titled “Summary”- Rule available since:
v2.3.3 - Diagnostic Category:
lint/nursery/noParametersOnlyUsedInRecursion - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
- Inspired from
only_used_in_recursion
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noParametersOnlyUsedInRecursion": "error" } } }}Description
Section titled “Description”Disallow function parameters that are only used in recursive calls.
A parameter that is only passed to recursive calls is effectively unused and can be removed or replaced with a constant, simplifying the function.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”function factorial(n, acc) { if (n === 0) return 1; return factorial(n - 1, acc);}code-block.js:1:23 lint/nursery/noParametersOnlyUsedInRecursion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This parameter is only used in recursive calls.
> 1 │ function factorial(n, acc) {
│ ^^^
2 │ if (n === 0) return 1;
3 │ return factorial(n - 1, acc);
ℹ Parameters that are only used in recursive calls are effectively unused and can be removed.
ℹ If the parameter is needed for the recursion to work, consider if the function can be refactored to avoid it.
ℹ Unsafe fix: If this is intentional, prepend acc with an underscore.
1 │ - function·factorial(n,·acc)·{
1 │ + function·factorial(n,·_acc)·{
2 2 │ if (n === 0) return 1;
3 │ - ····return·factorial(n·-·1,·acc);
3 │ + ····return·factorial(n·-·1,·_acc);
4 4 │ }
5 5 │
function countdown(n, step) { if (n === 0) return 0; return countdown(n - step, step);}code-block.js:1:23 lint/nursery/noParametersOnlyUsedInRecursion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This parameter is only used in recursive calls.
> 1 │ function countdown(n, step) {
│ ^^^^
2 │ if (n === 0) return 0;
3 │ return countdown(n - step, step);
ℹ Parameters that are only used in recursive calls are effectively unused and can be removed.
ℹ If the parameter is needed for the recursion to work, consider if the function can be refactored to avoid it.
ℹ Unsafe fix: If this is intentional, prepend step with an underscore.
1 │ - function·countdown(n,·step)·{
1 │ + function·countdown(n,·_step)·{
2 2 │ if (n === 0) return 0;
3 │ - ····return·countdown(n·-·step,·step);
3 │ + ····return·countdown(n·-·_step,·_step);
4 4 │ }
5 5 │
class Counter { count(n, acc) { if (n === 0) return 0; return this.count(n - 1, acc); }}code-block.js:2:14 lint/nursery/noParametersOnlyUsedInRecursion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This parameter is only used in recursive calls.
1 │ class Counter {
> 2 │ count(n, acc) {
│ ^^^
3 │ if (n === 0) return 0;
4 │ return this.count(n - 1, acc);
ℹ Parameters that are only used in recursive calls are effectively unused and can be removed.
ℹ If the parameter is needed for the recursion to work, consider if the function can be refactored to avoid it.
ℹ Unsafe fix: If this is intentional, prepend acc with an underscore.
1 1 │ class Counter {
2 │ - ····count(n,·acc)·{
2 │ + ····count(n,·_acc)·{
3 3 │ if (n === 0) return 0;
4 │ - ········return·this.count(n·-·1,·acc);
4 │ + ········return·this.count(n·-·1,·_acc);
5 5 │ }
6 6 │ }
function fn(n, acc) { if (n === 0) return 0; return fn(n - 1, acc || 0);}code-block.js:1:16 lint/nursery/noParametersOnlyUsedInRecursion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This parameter is only used in recursive calls.
> 1 │ function fn(n, acc) {
│ ^^^
2 │ if (n === 0) return 0;
3 │ return fn(n - 1, acc || 0);
ℹ Parameters that are only used in recursive calls are effectively unused and can be removed.
ℹ If the parameter is needed for the recursion to work, consider if the function can be refactored to avoid it.
ℹ Unsafe fix: If this is intentional, prepend acc with an underscore.
1 │ - function·fn(n,·acc)·{
1 │ + function·fn(n,·_acc)·{
2 2 │ if (n === 0) return 0;
3 │ - ····return·fn(n·-·1,·acc·||·0);
3 │ + ····return·fn(n·-·1,·_acc·||·0);
4 4 │ }
5 5 │
class Counter { count(n, acc) { if (n === 0) return 0; return this?.count(n - 1, acc); }}code-block.js:2:14 lint/nursery/noParametersOnlyUsedInRecursion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This parameter is only used in recursive calls.
1 │ class Counter {
> 2 │ count(n, acc) {
│ ^^^
3 │ if (n === 0) return 0;
4 │ return this?.count(n - 1, acc);
ℹ Parameters that are only used in recursive calls are effectively unused and can be removed.
ℹ If the parameter is needed for the recursion to work, consider if the function can be refactored to avoid it.
ℹ Unsafe fix: If this is intentional, prepend acc with an underscore.
1 1 │ class Counter {
2 │ - ····count(n,·acc)·{
2 │ + ····count(n,·_acc)·{
3 3 │ if (n === 0) return 0;
4 │ - ········return·this?.count(n·-·1,·acc);
4 │ + ········return·this?.count(n·-·1,·_acc);
5 5 │ }
6 6 │ }
function factorial(n, acc) { if (n === 0) return acc; return factorial(n - 1, acc * n);}function countdown(n, step) { console.log(step); if (n === 0) return 0; return countdown(n - step, step);}function fn(n, threshold) { if (n > threshold) return n; return fn(n + 1, threshold);}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.