Skip to content

noParametersOnlyUsedInRecursion

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"nursery": {
"noParametersOnlyUsedInRecursion": "error"
}
}
}
}

¥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

¥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 │ }

¥Valid

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