Skip to content

Commit

Permalink
fix(linter): Add missing fail cases in eslint/no-self-compare (#9693)
Browse files Browse the repository at this point in the history
Handles parentheses wrapping the LHS and RHS of the binary operation so
that the following missing test cases pass:

```js
x > (x)

(x) == x

(x) >= ((x))
```

Eslint playground for these fail cases above can be found
[here](https://eslint.org/play/#eyJ0ZXh0IjoiLyplc2xpbnQgbm8tc2VsZi1jb21wYXJlOiBcImVycm9yXCIqL1xuXG5pZiAoeCA+ICh4KSkge31cblxuaWYgKCh4KSA9PSB4KSB7fVxuXG5pZiAoKHgpID49ICgoeCkpKSB7fSIsIm9wdGlvbnMiOnsicnVsZXMiOnt9LCJsYW5ndWFnZU9wdGlvbnMiOnsicGFyc2VyT3B0aW9ucyI6eyJlY21hRmVhdHVyZXMiOnt9fX19fQ==).
  • Loading branch information
therewillbecode authored Mar 11, 2025
1 parent 835984f commit 2810e5b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion 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 @@ -84,6 +88,9 @@ fn test() {
("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 2810e5b

Please sign in to comment.