Skip to content

Commit

Permalink
feat(linter) catch more cases for misrefactored-assign-op (#5309)
Browse files Browse the repository at this point in the history
changes from using `without_parenthesis` to using `get_inner_expression` to ignore type casts, non null assertions ect.

this helps catch more error cases
  • Loading branch information
camc314 committed Aug 29, 2024
1 parent 3acb3f6 commit e5ead86
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
5 changes: 5 additions & 0 deletions crates/oxc_linter/src/rules/oxc/misrefactored_assign_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions crates/oxc_linter/src/snapshots/misrefactored_assign_op.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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]
1a *= 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]
1a *= 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]
1a *= (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]
1a *= (a as string) * (a as number);
· ──────────────────────────────────
╰────
help: Did you mean `a *= (a as string)`?
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/utils/unicorn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,17 @@ 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;
}
}

return is_same_reference(
left.object().without_parenthesized(),
right.object().without_parenthesized(),
left.object().get_inner_expression(),
right.object().get_inner_expression(),
ctx,
);
}

0 comments on commit e5ead86

Please sign in to comment.