Skip to content

Commit

Permalink
fix: no-unused-vars should be corrected in the logical assignment ope…
Browse files Browse the repository at this point in the history
…rator

Fixes #17299
  • Loading branch information
gweesin committed Jun 29, 2023
1 parent c8b1f4d commit 9aff278
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,17 @@ module.exports = {
);
}

/**
* Checks whether a given operator is a logical assignment operator or not.
*
* Logical assignment operators are `&&=`, `||=`, and `??=`.
* @param {string} operator The operator to check.
* @returns {boolean} `true` if the operator is a logical assignment operator.
*/
function isLogicalAssignmentOperator(operator) {
return /^(?:\?\?|&&|\|\|)=/u.test(operator);
}

/**
* Checks whether a given reference is a read to update itself or not.
* @param {eslint-scope.Reference} ref A reference to check.
Expand All @@ -466,7 +477,8 @@ module.exports = {
(
parent.type === "AssignmentExpression" &&
parent.left === id &&
isUnusedExpression(parent)
isUnusedExpression(parent) &&
!isLogicalAssignmentOperator(parent.operator)
) ||
(
parent.type === "UpdateExpression" &&
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,20 @@ ruleTester.run("no-unused-vars", rule, {
{
code: "import.meta",
parserOptions: { ecmaVersion: 2020, sourceType: "module" }
},

// https://github.com/eslint/eslint/issues/17299
{
code: "var a; a ||= 1;",
parserOptions: { ecmaVersion: 2021 }
},
{
code: "var a; a &&= 1;",
parserOptions: { ecmaVersion: 2021 }
},
{
code: "var a; a ??= 1;",
parserOptions: { ecmaVersion: 2021 }
}
],
invalid: [
Expand Down

0 comments on commit 9aff278

Please sign in to comment.