Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unused-vars] handle logical assignment (#7854)
Browse files Browse the repository at this point in the history
* fix(eslint-plugin): [no-unused-vars] handle logical assignment

* refactor
  • Loading branch information
yeonjuan committed Nov 1, 2023
1 parent c426884 commit 11e57c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/eslint-plugin/src/util/collectUnusedVariables.ts
Expand Up @@ -439,6 +439,8 @@ function isExported(variable: TSESLint.Scope.Variable): boolean {
});
}

const LOGICAL_ASSIGNMENT_OPERATORS = new Set(['&&=', '||=', '??=']);

/**
* Determines if the variable is used.
* @param variable The variable to check.
Expand Down Expand Up @@ -701,6 +703,7 @@ function isUsedVariable(variable: TSESLint.Scope.Variable): boolean {
ref.isRead() && // in RHS of an assignment for itself. e.g. `a = a + 1`
// self update. e.g. `a += 1`, `a++`
((parent.type === AST_NODE_TYPES.AssignmentExpression &&
!LOGICAL_ASSIGNMENT_OPERATORS.has(parent.operator) &&
grandparent.type === AST_NODE_TYPES.ExpressionStatement &&
parent.left === id) ||
(parent.type === AST_NODE_TYPES.UpdateExpression &&
Expand Down
Expand Up @@ -1099,6 +1099,18 @@ interface Foo {
bar: string;
}
`,
`
let foo = 1;
foo ??= 2;
`,
`
let foo = 1;
foo &&= 2;
`,
`
let foo = 1;
foo ||= 2;
`,
],

invalid: [
Expand Down Expand Up @@ -1844,5 +1856,23 @@ const Foo = 'bar';
},
],
},
{
code: `
let foo = 1;
foo += 1;
`,
errors: [
{
messageId: 'unusedVar',
line: 3,
column: 1,
data: {
varName: 'foo',
action: 'assigned a value',
additional: '',
},
},
],
},
],
});

0 comments on commit 11e57c5

Please sign in to comment.