Skip to content

Commit

Permalink
Handle parentheses in no-self-compare
Browse files Browse the repository at this point in the history
  • Loading branch information
therewillbecode committed Mar 11, 2025
1 parent 4a773ff commit 20a71c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
11 changes: 8 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_self_compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ impl Rule for NoSelfCompare {
return;
}

if binary_expr.left.content_eq(&binary_expr.right) {
if binary_expr
.left
.without_parentheses()
.content_eq(&binary_expr.right.without_parentheses())
{
ctx.diagnostic(no_self_compare_diagnostic(
binary_expr.left.span(),
binary_expr.right.span(),
Expand Down Expand Up @@ -81,11 +85,12 @@ fn test() {
("x == x", None),
("x != x", None),
("x > x", None),
("x > (x)", None),
("(x) > x", None),
("x < x", None),
("x >= x", None),
("x <= x", None),
("x > (x)", None),
("(x) == x", None),
("(x) >= ((x))", None),
("foo.bar().baz.qux >= foo.bar ().baz .qux", None),
("class C { #field; foo() { this.#field === this.#field; } }", None),
];
Expand Down
21 changes: 21 additions & 0 deletions crates/oxc_linter/src/snapshots/eslint_no_self_compare.snap
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ source: crates/oxc_linter/src/tester.rs
╰────
help: If you are testing for NaN, you can use Number.isNaN function.

⚠ eslint(no-self-compare): Both sides of this comparison are exactly the same
╭─[no_self_compare.tsx:1:1]
1 │ x > (x)
· ─ ───
╰────
help: If you are testing for NaN, you can use Number.isNaN function.

⚠ eslint(no-self-compare): Both sides of this comparison are exactly the same
╭─[no_self_compare.tsx:1:1]
1 │ (x) == x
· ─── ─
╰────
help: If you are testing for NaN, you can use Number.isNaN function.

⚠ eslint(no-self-compare): Both sides of this comparison are exactly the same
╭─[no_self_compare.tsx:1:1]
1 │ (x) >= ((x))
· ─── ─────
╰────
help: If you are testing for NaN, you can use Number.isNaN function.

⚠ eslint(no-self-compare): Both sides of this comparison are exactly the same
╭─[no_self_compare.tsx:1:1]
1 │ foo.bar().baz.qux >= foo.bar ().baz .qux
Expand Down

0 comments on commit 20a71c7

Please sign in to comment.