Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linter): false positiver in private member expr in oxc/const-comparison #8164

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
1 │ a.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]
1 │ a == 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]
1 │ class 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
Loading