Skip to content

Commit 2810e5b

Browse files
authoredMar 11, 2025··
fix(linter): Add missing fail cases in eslint/no-self-compare (#9693)
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==).
1 parent 835984f commit 2810e5b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed
 

‎crates/oxc_linter/src/rules/eslint/no_self_compare.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ impl Rule for NoSelfCompare {
4848
return;
4949
}
5050

51-
if binary_expr.left.content_eq(&binary_expr.right) {
51+
if binary_expr
52+
.left
53+
.without_parentheses()
54+
.content_eq(binary_expr.right.without_parentheses())
55+
{
5256
ctx.diagnostic(no_self_compare_diagnostic(
5357
binary_expr.left.span(),
5458
binary_expr.right.span(),
@@ -84,6 +88,9 @@ fn test() {
8488
("x < x", None),
8589
("x >= x", None),
8690
("x <= x", None),
91+
("x > (x)", None),
92+
("(x) == x", None),
93+
("(x) >= ((x))", None),
8794
("foo.bar().baz.qux >= foo.bar ().baz .qux", None),
8895
("class C { #field; foo() { this.#field === this.#field; } }", None),
8996
];

‎crates/oxc_linter/src/snapshots/eslint_no_self_compare.snap

+21
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ source: crates/oxc_linter/src/tester.rs
9292
╰────
9393
help: If you are testing for NaN, you can use Number.isNaN function.
9494

95+
⚠ eslint(no-self-compare): Both sides of this comparison are exactly the same
96+
╭─[no_self_compare.tsx:1:1]
97+
1 │ x > (x)
98+
· ─ ───
99+
╰────
100+
help: If you are testing for NaN, you can use Number.isNaN function.
101+
102+
⚠ eslint(no-self-compare): Both sides of this comparison are exactly the same
103+
╭─[no_self_compare.tsx:1:1]
104+
1 │ (x) == x
105+
· ─── ─
106+
╰────
107+
help: If you are testing for NaN, you can use Number.isNaN function.
108+
109+
⚠ eslint(no-self-compare): Both sides of this comparison are exactly the same
110+
╭─[no_self_compare.tsx:1:1]
111+
1 │ (x) >= ((x))
112+
· ─── ─────
113+
╰────
114+
help: If you are testing for NaN, you can use Number.isNaN function.
115+
95116
⚠ eslint(no-self-compare): Both sides of this comparison are exactly the same
96117
╭─[no_self_compare.tsx:1:1]
97118
1 │ foo.bar().baz.qux >= foo.bar ().baz .qux

0 commit comments

Comments
 (0)
Please sign in to comment.