fix(linter/no-unused-vars): writes to members triggering false positive (#5744)

close: #5246

similar to #5722
This commit is contained in:
Dunqing 2024-09-13 06:00:48 +00:00
parent 19790dbc9c
commit b4ed564a35
2 changed files with 16 additions and 9 deletions

View file

@ -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,
),

View file

@ -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
@ -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 */ }
}
}