noUnusedVariables
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/correctness/noUnusedVariables¥Diagnostic Category:
lint/correctness/noUnusedVariables -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
-
与
no-unused-vars相同¥Same as
no-unused-vars -
与
@typescript-eslint/no-unused-vars相同¥Same as
@typescript-eslint/no-unused-vars -
与
unused-imports/no-unused-vars相同¥Same as
unused-imports/no-unused-vars
-
¥How to configure
{ "linter": { "rules": { "correctness": { "noUnusedVariables": "error" } } }}¥Description
禁止未使用的变量。
¥Disallow unused variables.
此规则有一个例外:以下划线开头的变量,例如 let _something;。
¥There is an exception to this rule: variables that start with underscore, e.g. let _something;.
在程序员中,使用下划线作为变量前缀是一种非常普遍的模式,Biome 也遵循这种模式。
¥The pattern of having an underscore as a prefix of a variable is a very diffuse pattern among programmers, and Biome follows it.
此规则不会报告未使用的导入。如果你想报告未使用的导入,请启用 noUnusedImports。
¥This rule won’t report unused imports. If you want to report unused imports, enable noUnusedImports.
¥Examples
¥Invalid
let a = 4;a++;code-block.js:1:5 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable a is unused.
> 1 │ let a = 4;
│ ^
2 │ a++;
3 │
ℹ Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
ℹ Unsafe fix: If this is intentional, prepend a with an underscore.
1 │ - let·a·=·4;
2 │ - a++;
1 │ + let·_a·=·4;
2 │ + _a++;
3 3 │
function foo() {}code-block.js:1:10 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function foo is unused.
> 1 │ function foo() {}
│ ^^^
2 │
ℹ Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
ℹ Unsafe fix: If this is intentional, prepend foo with an underscore.
1 │ - function·foo()·{}
1 │ + function·_foo()·{}
2 2 │
function foo() { foo();}code-block.js:1:10 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function foo is unused.
> 1 │ function foo() {
│ ^^^
2 │ foo();
3 │ }
ℹ Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
ℹ Unsafe fix: If this is intentional, prepend foo with an underscore.
1 │ - function·foo()·{
2 │ - ····foo();
1 │ + function·_foo()·{
2 │ + ····_foo();
3 3 │ }
4 4 │
const foo = () => { foo();};code-block.js:1:7 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable foo is unused.
> 1 │ const foo = () => {
│ ^^^
2 │ foo();
3 │ };
ℹ Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
ℹ Unsafe fix: If this is intentional, prepend foo with an underscore.
1 │ - const·foo·=·()·=>·{
2 │ - ····foo();
1 │ + const·_foo·=·()·=>·{
2 │ + ····_foo();
3 3 │ };
4 4 │
export function f<T>() {}code-block.ts:1:19 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This type parameter T is unused.
> 1 │ export function f<T>() {}
│ ^
2 │
ℹ Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
ℹ Unsafe fix: If this is intentional, prepend T with an underscore.
1 │ - export·function·f<T>()·{}
1 │ + export·function·f<_T>()·{}
2 2 │
const { brand } = car;code-block.js:1:9 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable brand is unused.
> 1 │ const { brand } = car;
│ ^^^^^
2 │
ℹ Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
¥Valid
function foo(b) { console.log(b)};foo();export function foo(_unused) {}function used_overloaded(): number;function used_overloaded(s: string): string;function used_overloaded(s?: string) { return s;}used_overloaded();默认情况下,如果解构模式包含 rest 属性,则会忽略在解构对象内部声明的未使用变量。(如果你想启用这些检查,请参阅 规则选项。)
¥By default, unused variables declared inside destructured objects are ignored if the destructuring pattern also contains a rest property. (See the rule options if you want to enable these checks).
const car = { brand: "Tesla", year: 2019, countryCode: "US" };const { brand, ...rest } = car;console.log(rest);¥Options
ignoreRestSiblings
Section titled “ignoreRestSiblings”是否忽略解构对象内部声明的未使用变量(例如 const { a, b, ...rest } = obj),这些对象包含剩余属性。
¥Whether to ignore unused variables declared inside destructured objects
containing rest properties (such as const { a, b, ...rest } = obj.
默认:true
¥Default: true
¥Example
{ "linter": { "rules": { "correctness": { "noUnusedVariables": { "options": { "ignoreRestSiblings": false } } } } }}const car = { brand: "Tesla", year: 2019, countryCode: "US" };const { brand, ...other } = car;console.log(other);code-block.js:2:9 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable brand is unused.
1 │ const car = { brand: “Tesla”, year: 2019, countryCode: “US” };
> 2 │ const { brand, …other } = car;
│ ^^^^^
3 │ console.log(other);
4 │
ℹ Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
ℹ You can enable the ignoreRestSiblings option to ignore unused variables inside destructured objects with rest properties.
const car = { brand: "Tesla", year: 2019, countryCode: "US" };const { brand: _, ...other } = car;console.log(other);¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号