Skip to content

Commit

Permalink
refactor(linter): replace ast "compare by hash" to "compare by conten…
Browse files Browse the repository at this point in the history
…t" (#5602)

relates #5600
  • Loading branch information
shulaoda authored Sep 8, 2024
1 parent 20d0068 commit 4e748b5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
5 changes: 3 additions & 2 deletions crates/oxc_linter/src/rules/eslint/no_dupe_else_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use oxc_ast::{
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::cmp::ContentEq;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::LogicalOperator;

use crate::{ast_util::calculate_hash, context::LintContext, rule::Rule, AstNode};
use crate::{context::LintContext, rule::Rule, AstNode};

fn no_dupe_else_if_diagnostic(first_test: Span, second_test: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("duplicate conditions in if-else-if chains")
Expand Down Expand Up @@ -154,7 +155,7 @@ fn is_equal<'a, 'b>(a: &'a Expression<'b>, b: &'a Expression<'b>) -> bool {
|| (is_equal(&a.left, &b.right) && is_equal(&a.right, &b.left))
}

(a, b) => calculate_hash(a) == calculate_hash(b),
(a, b) => a.content_eq(b),
}
}

Expand Down
7 changes: 3 additions & 4 deletions crates/oxc_linter/src/rules/eslint/no_self_compare.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::cmp::ContentEq;
use oxc_span::{GetSpan, Span};

use crate::{ast_util::calculate_hash, context::LintContext, rule::Rule, AstNode};
use crate::{context::LintContext, rule::Rule, AstNode};

fn no_self_compare_diagnostic(span: Span, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Disallow comparisons where both sides are exactly the same")
Expand Down Expand Up @@ -44,10 +45,8 @@ impl Rule for NoSelfCompare {
if !binary_expr.operator.is_compare() && !binary_expr.operator.is_equality() {
return;
}
let left = calculate_hash(&binary_expr.left);
let right = calculate_hash(&binary_expr.right);

if left == right {
if binary_expr.left.content_eq(&binary_expr.right) {
ctx.diagnostic(no_self_compare_diagnostic(
binary_expr.left.span(),
binary_expr.right.span(),
Expand Down

0 comments on commit 4e748b5

Please sign in to comment.