Skip to content

Commit a36bcb6

Browse files
authored
fix: no-unused-vars false positive with logical assignment operators (#17320)
* fix: no-unused-vars should be corrected in logical assignment operator Fixes #17299 * refactor: use astUtils.isLogicalAssignmentOperator check exception
1 parent c8b1f4d commit a36bcb6

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

lib/rules/no-unused-vars.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ module.exports = {
466466
(
467467
parent.type === "AssignmentExpression" &&
468468
parent.left === id &&
469-
isUnusedExpression(parent)
469+
isUnusedExpression(parent) &&
470+
!astUtils.isLogicalAssignmentOperator(parent.operator)
470471
) ||
471472
(
472473
parent.type === "UpdateExpression" &&

tests/lib/rules/no-unused-vars.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,20 @@ ruleTester.run("no-unused-vars", rule, {
417417
{
418418
code: "import.meta",
419419
parserOptions: { ecmaVersion: 2020, sourceType: "module" }
420+
},
421+
422+
// https://github.com/eslint/eslint/issues/17299
423+
{
424+
code: "var a; a ||= 1;",
425+
parserOptions: { ecmaVersion: 2021 }
426+
},
427+
{
428+
code: "var a; a &&= 1;",
429+
parserOptions: { ecmaVersion: 2021 }
430+
},
431+
{
432+
code: "var a; a ??= 1;",
433+
parserOptions: { ecmaVersion: 2021 }
420434
}
421435
],
422436
invalid: [

0 commit comments

Comments
 (0)