Skip to content

Commit

Permalink
fix(linter): false positiver in private member expr in oxc/const-comp…
Browse files Browse the repository at this point in the history
…arison (#8164)

fixes #8163
  • Loading branch information
camc314 committed Dec 28, 2024
1 parent 37c9959 commit 1b9a5ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions crates/oxc_linter/src/rules/oxc/const_comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,11 @@ fn test() {
"status_code > 500 && foo() && bar || status_code < 400;",
// oxc specific
"a < b",
"a.b.c < b.b.c",
"a <= b",
"a > b",
"a >= b",
"class Foo { #a; #b; constructor() { this.#a = 1; }; test() { return this.#a > this.#b } }",
];

let fail = vec![
Expand Down Expand Up @@ -500,10 +502,12 @@ fn test() {
"a <= a",
"a > a",
"a >= a",
"a.b.c >= a.b.c",
"a == b && a == b",
"a == b || a == b",
"!foo && !foo",
"!foo || !foo",
"class Foo { #a; #b; constructor() { this.#a = 1; }; test() { return this.#a > this.#a } }",
];

Tester::new(ConstComparisons::NAME, ConstComparisons::CATEGORY, pass, fail).test_and_snapshot();
Expand Down
15 changes: 14 additions & 1 deletion crates/oxc_linter/src/snapshots/oxc_const_comparisons.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/oxc_linter/src/tester.rs
snapshot_kind: text
---
oxc(const-comparisons): Unexpected constant comparison
╭─[const_comparisons.tsx:1:1]
Expand Down Expand Up @@ -264,6 +263,13 @@ snapshot_kind: text
╰────
help: Because `a` will always be equal to itself

oxc(const-comparisons): This comparison will always evaluate to true
╭─[const_comparisons.tsx:1:1]
1a.b.c >= a.b.c
· ──────────────
╰────
help: Because `a.b.c` will always be equal to itself

oxc(const-comparisons): Both sides of the logical operator are the same
╭─[const_comparisons.tsx:1:1]
1a == b && a == b
Expand Down Expand Up @@ -299,3 +305,10 @@ snapshot_kind: text
· ╰── If this expression evaluates to true
╰────
help: This logical expression will always evaluate to the same value as the expression itself.

oxc(const-comparisons): This comparison will always evaluate to false
╭─[const_comparisons.tsx:1:69]
1class Foo { #a; #b; constructor() { this.#a = 1; }; test() { return this.#a > this.#a } }
· ─────────────────
╰────
help: Because `this.#a` will never be greater than itself
11 changes: 10 additions & 1 deletion crates/oxc_linter/src/utils/unicorn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,16 @@ pub fn is_same_member_expression(
(Some(_), None) | (None, Some(_)) => {
return false;
}
_ => {}
(None, None) => {
if let (
MemberExpression::PrivateFieldExpression(left),
MemberExpression::PrivateFieldExpression(right),
) = (left, right)
{
return left.field.name == right.field.name
&& is_same_expression(&left.object, &right.object, ctx);
}
}
}

if let (
Expand Down

0 comments on commit 1b9a5ba

Please sign in to comment.