refactor(linter): rename is_same_reference to is_same_expression (#7654)

This commit is contained in:
camc314 2024-12-05 04:15:15 +00:00
parent 2179b93729
commit 3711a8e342
10 changed files with 34 additions and 48 deletions

View file

@ -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" };
@ -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;
}
@ -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;
}

View file

@ -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.")
@ -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;
}
@ -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,
@ -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,

View file

@ -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.")
@ -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;
}

View file

@ -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,
};
@ -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)
}
};
}

View file

@ -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 {
@ -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,

View file

@ -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.")
@ -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;
}

View file

@ -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(
@ -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,

View file

@ -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 {
@ -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,
}

View file

@ -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)
@ -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;
}

View file

@ -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() {
@ -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;
@ -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,
@ -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,
@ -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,
@ -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,