Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/eslint-plugin/src/util/collectUnusedVariables.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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: '',
},
},
],
},
],
});