Skip to content

Commit

Permalink
fix(linter/no-unused-vars): writes to members triggering false positi…
Browse files Browse the repository at this point in the history
…ve (#5744)

close: #5246

similar to #5722
  • Loading branch information
Dunqing committed Sep 13, 2024
1 parent 19790db commit b4ed564
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
10 changes: 10 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ fn test_vars_simple() {
"
const a = 0
obj[a]++;
obj[a] += 1;
",
None,
),
(
"
const obj = 0
obj.a++;
obj.a += 1;
obj.b.c++;
",
None,
),
Expand Down
15 changes: 6 additions & 9 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,12 @@ impl<'s, 'a> Symbol<'s, 'a> {
}
// When symbol is being assigned a new value, we flag the reference
// as only affecting itself until proven otherwise.
AstKind::UpdateExpression(_) | AstKind::SimpleAssignmentTarget(_) => {
is_used_by_others = false;
AstKind::UpdateExpression(UpdateExpression { argument, .. })
| AstKind::SimpleAssignmentTarget(argument) => {
// `a.b++` or `a[b] + 1` are not reassignment of `a`
if !argument.is_member_expression() {
is_used_by_others = false;
}
}
// RHS usage when LHS != reference's symbol is definitely used by
// others
Expand Down Expand Up @@ -412,13 +416,6 @@ impl<'s, 'a> Symbol<'s, 'a> {
// a = yield a // <- still considered used b/c it's propagated to the caller
// }
AstKind::YieldExpression(_) => return false,
AstKind::MemberExpression(MemberExpression::ComputedMemberExpression(computed)) => {
// obj[a]++;
// ^ the reference is used as property
if computed.expression.span().contains_inclusive(ref_span) {
return false;
}
}
_ => { /* continue up tree */ }
}
}
Expand Down

0 comments on commit b4ed564

Please sign in to comment.