noUnusedVariables (since v0.9.0)
This rule is recommended by Rome.
Disallow unused variables.
Examples
Invalid
const a = 4;
error[js/noUnusedVariables]: This variable is unused.
┌─ js/noUnusedVariables.js:1:7
│
1 │ const a = 4;
│ ^
Suggested fix: Remove this variable.
| @@ -1 +1 @@
0 | - const a = 4;
0 | +
= note: Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
let a = 4;
error[js/noUnusedVariables]: This variable is unused.
┌─ js/noUnusedVariables.js:1:5
│
1 │ let a = 4;
│ ^
Suggested fix: Remove this variable.
| @@ -1 +1 @@
0 | - let a = 4;
0 | +
= note: Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
function foo() {
};
error[js/noUnusedVariables]: This function is unused.
┌─ js/noUnusedVariables.js:1:10
│
1 │ function foo() {
│ ^^^
Suggested fix: Remove this function.
| @@ -1,2 +1 @@
0 | - function foo() {
1 | - };
0 | + ;
= note: Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
function foo(myVar) {
console.log('foo');
}
foo();
error[js/noUnusedVariables]: This parameter is unused.
┌─ js/noUnusedVariables.js:1:14
│
1 │ function foo(myVar) {
│ ^^^^^
Suggested fix: Remove this parameter.
| @@ -1,4 +1,4 @@
0 | - function foo(myVar) {
0 | + function foo() {
1 1 | console.log('foo');
2 2 | }
3 3 | foo();
= note: Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
const foo = () => {
};
error[js/noUnusedVariables]: This variable is unused.
┌─ js/noUnusedVariables.js:1:7
│
1 │ const foo = () => {
│ ^^^
Suggested fix: Remove this variable.
| @@ -1,2 +1 @@
0 | - const foo = () => {
1 | - };
0 | +
= note: Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
function foo() {
foo();
}
error[js/noUnusedVariables]: This function is unused.
┌─ js/noUnusedVariables.js:1:10
│
1 │ function foo() {
│ ^^^
Suggested fix: Remove this function.
| @@ -1,3 +1 @@
0 | - function foo() {
1 | - foo();
2 | - }
0 | +
= note: Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
const foo = () => {
foo();
console.log(this);
};
error[js/noUnusedVariables]: This variable is unused.
┌─ js/noUnusedVariables.js:1:7
│
1 │ const foo = () => {
│ ^^^
Suggested fix: Remove this variable.
| @@ -1,4 +1 @@
0 | - const foo = () => {
1 | - foo();
2 | - console.log(this);
3 | - };
0 | +
= note: Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
Valid
function foo(b) {
console.log(b)
};
foo();