Skip to content

Commit

Permalink
refactor(linter): rename is_same_reference to is_same_expression (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Dec 5, 2024
1 parent 2179b93 commit 3711a8e
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 48 deletions.
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/eslint/yoda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use regex::Regex;

use crate::{context::LintContext, rule::Rule, utils::is_same_reference, AstNode};
use crate::{context::LintContext, rule::Rule, utils::is_same_expression, AstNode};

fn yoda_diagnostic(span: Span, never: bool, operator: &str) -> OxcDiagnostic {
let expected_side = if never { "right" } else { "left" };
Expand Down Expand Up @@ -371,7 +371,7 @@ fn is_range(expr: &LogicalExpression, ctx: &LintContext) -> bool {
}

if expr.operator == LogicalOperator::And {
if !is_same_reference(&left.right, &right.left, ctx) {
if !is_same_expression(&left.right, &right.left, ctx) {
return false;
}

Expand Down Expand Up @@ -405,7 +405,7 @@ fn is_range(expr: &LogicalExpression, ctx: &LintContext) -> bool {
}

if expr.operator == LogicalOperator::Or {
if !is_same_reference(&left.left, &right.right, ctx) {
if !is_same_expression(&left.left, &right.right, ctx) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/rules/oxc/const_comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::{BinaryOperator, LogicalOperator};

use crate::{context::LintContext, rule::Rule, utils::is_same_reference, AstNode};
use crate::{context::LintContext, rule::Rule, utils::is_same_expression, AstNode};

fn redundant_left_hand_side(span: Span, span1: Span, help: String) -> OxcDiagnostic {
OxcDiagnostic::warn("Left-hand side of `&&` operator has no effect.")
Expand Down Expand Up @@ -144,7 +144,7 @@ impl ConstComparisons {
return;
}

if !is_same_reference(left_expr, right_expr, ctx) {
if !is_same_expression(left_expr, right_expr, ctx) {
return;
}

Expand Down Expand Up @@ -206,7 +206,7 @@ impl ConstComparisons {
return;
}

if is_same_reference(
if is_same_expression(
logical_expr.left.get_inner_expression(),
logical_expr.right.get_inner_expression(),
ctx,
Expand All @@ -229,7 +229,7 @@ impl ConstComparisons {
| BinaryOperator::GreaterEqualThan
| BinaryOperator::LessThan
| BinaryOperator::GreaterThan
) && is_same_reference(
) && is_same_expression(
bin_expr.left.get_inner_expression(),
bin_expr.right.get_inner_expression(),
ctx,
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/oxc/double_comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::operator::{BinaryOperator, LogicalOperator};

use crate::{context::LintContext, rule::Rule, utils::is_same_reference, AstNode};
use crate::{context::LintContext, rule::Rule, utils::is_same_expression, AstNode};

fn double_comparisons_diagnostic(span: Span, operator: &str) -> OxcDiagnostic {
OxcDiagnostic::warn("Unexpected double comparisons.")
Expand Down Expand Up @@ -69,8 +69,8 @@ impl Rule for DoubleComparisons {
};

// check that (LLHS === RLHS && LRHS === RRHS) || (LLHS === RRHS && LRHS === RLHS)
if !((is_same_reference(llhs, rlhs, ctx) && is_same_reference(lrhs, rrhs, ctx))
|| (is_same_reference(llhs, rrhs, ctx) && is_same_reference(lrhs, rlhs, ctx)))
if !((is_same_expression(llhs, rlhs, ctx) && is_same_expression(lrhs, rrhs, ctx))
|| (is_same_expression(llhs, rrhs, ctx) && is_same_expression(lrhs, rlhs, ctx)))
{
return;
}
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_linter/src/rules/oxc/misrefactored_assign_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use oxc_syntax::operator::{AssignmentOperator, BinaryOperator};
use crate::{
context::LintContext,
rule::Rule,
utils::{is_same_member_expression, is_same_reference},
utils::{is_same_expression, is_same_member_expression},
AstNode,
};

Expand Down Expand Up @@ -121,19 +121,19 @@ fn assignment_target_eq_expr<'a>(
}
}
SimpleAssignmentTarget::TSAsExpression(ts_expr) => {
is_same_reference(&ts_expr.expression, right_expr, ctx)
is_same_expression(&ts_expr.expression, right_expr, ctx)
}
SimpleAssignmentTarget::TSSatisfiesExpression(ts_expr) => {
is_same_reference(&ts_expr.expression, right_expr, ctx)
is_same_expression(&ts_expr.expression, right_expr, ctx)
}
SimpleAssignmentTarget::TSNonNullExpression(ts_expr) => {
is_same_reference(&ts_expr.expression, right_expr, ctx)
is_same_expression(&ts_expr.expression, right_expr, ctx)
}
SimpleAssignmentTarget::TSTypeAssertion(ts_expr) => {
is_same_reference(&ts_expr.expression, right_expr, ctx)
is_same_expression(&ts_expr.expression, right_expr, ctx)
}
SimpleAssignmentTarget::TSInstantiationExpression(ts_expr) => {
is_same_reference(&ts_expr.expression, right_expr, ctx)
is_same_expression(&ts_expr.expression, right_expr, ctx)
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/unicorn/no_length_as_slice_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};

use crate::{
ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_reference, AstNode,
ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_expression, AstNode,
};

fn no_length_as_slice_end_diagnostic(call_span: Span, arg_span: Span) -> OxcDiagnostic {
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Rule for NoLengthAsSliceEnd {
return;
}

if !is_same_reference(
if !is_same_expression(
call_expr.callee.as_member_expression().unwrap().object(),
&second_argument.object,
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::UnaryOperator;

use crate::{context::LintContext, rule::Rule, utils::is_same_reference, AstNode};
use crate::{context::LintContext, rule::Rule, utils::is_same_expression, AstNode};

fn prefer_logical_operator_over_ternary_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Prefer using a logical operator over a ternary.")
Expand Down Expand Up @@ -69,7 +69,7 @@ impl Rule for PreferLogicalOperatorOverTernary {
}

fn is_same_node(left: &Expression, right: &Expression, ctx: &LintContext) -> bool {
if is_same_reference(left, right, ctx) {
if is_same_expression(left, right, ctx) {
return true;
}

Expand Down
19 changes: 2 additions & 17 deletions crates/oxc_linter/src/rules/unicorn/prefer_math_min_max.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::borrow::Cow;

use oxc_ast::{
ast::{BinaryExpression, BinaryOperator, Expression, UnaryOperator},
ast::{BinaryExpression, BinaryOperator, Expression},
AstKind,
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

use crate::{context::LintContext, rule::Rule, utils::is_same_reference, AstNode};
use crate::{context::LintContext, rule::Rule, utils::is_same_expression, AstNode};

fn prefer_math_min_max_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(
Expand Down Expand Up @@ -111,21 +111,6 @@ fn get_expr_value(expr: &Expression) -> Option<String> {
}
}

fn is_same_expression(left: &Expression, right: &Expression, ctx: &LintContext) -> bool {
if is_same_reference(left, right, ctx) {
return true;
}

match (left, right) {
(Expression::UnaryExpression(left_expr), Expression::UnaryExpression(right_expr)) => {
left_expr.operator == UnaryOperator::UnaryNegation
&& right_expr.operator == UnaryOperator::UnaryNegation
&& is_same_reference(&left_expr.argument, &right_expr.argument, ctx)
}
_ => false,
}
}

fn is_min_max(
condition: &BinaryExpression,
consequent: &Expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use oxc_span::Span;
use oxc_syntax::operator::BinaryOperator;

use crate::{
ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_reference, AstNode,
ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_expression, AstNode,
};

fn prefer_math_abs(span: Span) -> OxcDiagnostic {
Expand Down Expand Up @@ -249,7 +249,7 @@ fn is_pow_2_expression(expression: &Expression, ctx: &LintContext<'_>) -> bool {
}
}
BinaryOperator::Multiplication => {
is_same_reference(&bin_expr.left, &bin_expr.right, ctx)
is_same_expression(&bin_expr.left, &bin_expr.right, ctx)
}
_ => false,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/unicorn/prefer_negative_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};

use crate::{context::LintContext, fixer::Fix, rule::Rule, utils::is_same_reference, AstNode};
use crate::{context::LintContext, fixer::Fix, rule::Rule, utils::is_same_expression, AstNode};

fn prefer_negative_index_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Prefer negative index over .length - index when possible").with_label(span)
Expand Down Expand Up @@ -179,7 +179,7 @@ impl Rule for PreferNegativeIndex {
}

fn is_same_node(left: &Expression, right: &Expression, ctx: &LintContext) -> bool {
if is_same_reference(left, right, ctx) {
if is_same_expression(left, right, ctx) {
return true;
}

Expand Down
15 changes: 8 additions & 7 deletions crates/oxc_linter/src/utils/unicorn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ pub fn get_return_identifier_name<'a>(body: &'a FunctionBody<'_>) -> Option<&'a
}
}

pub fn is_same_reference(left: &Expression, right: &Expression, ctx: &LintContext) -> bool {
/// Compares two expressions to see if they are the same.
pub fn is_same_expression(left: &Expression, right: &Expression, ctx: &LintContext) -> bool {
if let Expression::ChainExpression(left_chain_expr) = left {
if let Some(right_member_expr) = right.as_member_expression() {
if let Some(v) = left_chain_expr.expression.as_member_expression() {
Expand Down Expand Up @@ -190,7 +191,7 @@ pub fn is_same_reference(left: &Expression, right: &Expression, ctx: &LintContex
.expressions
.iter()
.zip(right_str.expressions.iter())
.all(|(left, right)| is_same_reference(left, right, ctx));
.all(|(left, right)| is_same_expression(left, right, ctx));
}
(Expression::NumericLiteral(left_num), Expression::NumericLiteral(right_num)) => {
return left_num.raw == right_num.raw;
Expand All @@ -209,12 +210,12 @@ pub fn is_same_reference(left: &Expression, right: &Expression, ctx: &LintContex
Expression::BinaryExpression(right_bin_expr),
) => {
return left_bin_expr.operator == right_bin_expr.operator
&& is_same_reference(
&& is_same_expression(
left_bin_expr.left.get_inner_expression(),
right_bin_expr.left.get_inner_expression(),
ctx,
)
&& is_same_reference(
&& is_same_expression(
left_bin_expr.right.get_inner_expression(),
right_bin_expr.right.get_inner_expression(),
ctx,
Expand All @@ -226,7 +227,7 @@ pub fn is_same_reference(left: &Expression, right: &Expression, ctx: &LintContex
Expression::UnaryExpression(right_unary_expr),
) => {
return left_unary_expr.operator == right_unary_expr.operator
&& is_same_reference(
&& is_same_expression(
left_unary_expr.argument.get_inner_expression(),
right_unary_expr.argument.get_inner_expression(),
ctx,
Expand Down Expand Up @@ -302,7 +303,7 @@ pub fn is_same_member_expression(
}
}
_ => {
if !is_same_reference(
if !is_same_expression(
left.expression.get_inner_expression(),
right.expression.get_inner_expression(),
ctx,
Expand All @@ -313,7 +314,7 @@ pub fn is_same_member_expression(
}
}

is_same_reference(
is_same_expression(
left.object().get_inner_expression(),
right.object().get_inner_expression(),
ctx,
Expand Down

0 comments on commit 3711a8e

Please sign in to comment.