feat(linter) catch more cases for misrefactored-assign-op (#5309)

changes from using `without_parenthesis` to using `get_inner_expression` to ignore type casts, non null assertions ect.

this helps catch more error cases
This commit is contained in:
camc314 2024-08-29 00:47:37 +00:00
parent 3acb3f6461
commit e5ead86d43
3 changed files with 37 additions and 4 deletions

View file

@ -98,6 +98,7 @@ fn assignment_target_eq_expr<'a>(
right_expr: &Expression<'_>,
ctx: &LintContext<'a>,
) -> bool {
let right_expr = right_expr.get_inner_expression();
if let Some(simple_assignment_target) = assignment_target.as_simple_assignment_target() {
return match simple_assignment_target {
SimpleAssignmentTarget::AssignmentTargetIdentifier(ident) => {
@ -216,6 +217,10 @@ fn test() {
//~^ ERROR: variable appears on both sides of an assignment operation
"a *= a * a;",
//~^ ERROR: variable appears on both sides of an assignment operation
"a *= a * (a as number);",
//~^ ERROR: variable appears on both sides of an assignment operation
"a *= (a as string) * (a as number);",
//~^ ERROR: variable appears on both sides of an assignment operation
];
Tester::new(MisrefactoredAssignOp::NAME, pass, fail).test_and_snapshot();

View file

@ -70,3 +70,31 @@ source: crates/oxc_linter/src/tester.rs
· ──────────
╰────
help: Did you mean `a *= a`?
⚠ oxc(misrefactored-assign-op): Misrefactored assign op. Variable appears on both sides of an assignment operation
╭─[misrefactored_assign_op.tsx:1:1]
1 │ a *= a * (a as number);
· ──────────────────────
╰────
help: Did you mean `a *= (a as number)`?
⚠ oxc(misrefactored-assign-op): Misrefactored assign op. Variable appears on both sides of an assignment operation
╭─[misrefactored_assign_op.tsx:1:1]
1 │ a *= a * (a as number);
· ──────────────────────
╰────
help: Did you mean `a *= a`?
⚠ oxc(misrefactored-assign-op): Misrefactored assign op. Variable appears on both sides of an assignment operation
╭─[misrefactored_assign_op.tsx:1:1]
1 │ a *= (a as string) * (a as number);
· ──────────────────────────────────
╰────
help: Did you mean `a *= (a as number)`?
⚠ oxc(misrefactored-assign-op): Misrefactored assign op. Variable appears on both sides of an assignment operation
╭─[misrefactored_assign_op.tsx:1:1]
1 │ a *= (a as string) * (a as number);
· ──────────────────────────────────
╰────
help: Did you mean `a *= (a as string)`?

View file

@ -237,8 +237,8 @@ pub fn is_same_member_expression(
) = (left, right)
{
if !is_same_reference(
left.expression.without_parenthesized(),
right.expression.without_parenthesized(),
left.expression.get_inner_expression(),
right.expression.get_inner_expression(),
ctx,
) {
return false;
@ -246,8 +246,8 @@ pub fn is_same_member_expression(
}
return is_same_reference(
left.object().without_parenthesized(),
right.object().without_parenthesized(),
left.object().get_inner_expression(),
right.object().get_inner_expression(),
ctx,
);
}