Skip to content

Commit 3711a8e

Browse files
committed
refactor(linter): rename is_same_reference to is_same_expression (#7654)
1 parent 2179b93 commit 3711a8e

10 files changed

Lines changed: 34 additions & 48 deletions

crates/oxc_linter/src/rules/eslint/yoda.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use oxc_macros::declare_oxc_lint;
1111
use oxc_span::{GetSpan, Span};
1212
use regex::Regex;
1313

14-
use crate::{context::LintContext, rule::Rule, utils::is_same_reference, AstNode};
14+
use crate::{context::LintContext, rule::Rule, utils::is_same_expression, AstNode};
1515

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

373373
if expr.operator == LogicalOperator::And {
374-
if !is_same_reference(&left.right, &right.left, ctx) {
374+
if !is_same_expression(&left.right, &right.left, ctx) {
375375
return false;
376376
}
377377

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

407407
if expr.operator == LogicalOperator::Or {
408-
if !is_same_reference(&left.left, &right.right, ctx) {
408+
if !is_same_expression(&left.left, &right.right, ctx) {
409409
return false;
410410
}
411411

crates/oxc_linter/src/rules/oxc/const_comparisons.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use oxc_macros::declare_oxc_lint;
1010
use oxc_span::{GetSpan, Span};
1111
use oxc_syntax::operator::{BinaryOperator, LogicalOperator};
1212

13-
use crate::{context::LintContext, rule::Rule, utils::is_same_reference, AstNode};
13+
use crate::{context::LintContext, rule::Rule, utils::is_same_expression, AstNode};
1414

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

147-
if !is_same_reference(left_expr, right_expr, ctx) {
147+
if !is_same_expression(left_expr, right_expr, ctx) {
148148
return;
149149
}
150150

@@ -206,7 +206,7 @@ impl ConstComparisons {
206206
return;
207207
}
208208

209-
if is_same_reference(
209+
if is_same_expression(
210210
logical_expr.left.get_inner_expression(),
211211
logical_expr.right.get_inner_expression(),
212212
ctx,
@@ -229,7 +229,7 @@ impl ConstComparisons {
229229
| BinaryOperator::GreaterEqualThan
230230
| BinaryOperator::LessThan
231231
| BinaryOperator::GreaterThan
232-
) && is_same_reference(
232+
) && is_same_expression(
233233
bin_expr.left.get_inner_expression(),
234234
bin_expr.right.get_inner_expression(),
235235
ctx,

crates/oxc_linter/src/rules/oxc/double_comparisons.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use oxc_macros::declare_oxc_lint;
44
use oxc_span::Span;
55
use oxc_syntax::operator::{BinaryOperator, LogicalOperator};
66

7-
use crate::{context::LintContext, rule::Rule, utils::is_same_reference, AstNode};
7+
use crate::{context::LintContext, rule::Rule, utils::is_same_expression, AstNode};
88

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

7171
// check that (LLHS === RLHS && LRHS === RRHS) || (LLHS === RRHS && LRHS === RLHS)
72-
if !((is_same_reference(llhs, rlhs, ctx) && is_same_reference(lrhs, rrhs, ctx))
73-
|| (is_same_reference(llhs, rrhs, ctx) && is_same_reference(lrhs, rlhs, ctx)))
72+
if !((is_same_expression(llhs, rlhs, ctx) && is_same_expression(lrhs, rrhs, ctx))
73+
|| (is_same_expression(llhs, rrhs, ctx) && is_same_expression(lrhs, rlhs, ctx)))
7474
{
7575
return;
7676
}

crates/oxc_linter/src/rules/oxc/misrefactored_assign_op.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use oxc_syntax::operator::{AssignmentOperator, BinaryOperator};
1111
use crate::{
1212
context::LintContext,
1313
rule::Rule,
14-
utils::{is_same_member_expression, is_same_reference},
14+
utils::{is_same_expression, is_same_member_expression},
1515
AstNode,
1616
};
1717

@@ -121,19 +121,19 @@ fn assignment_target_eq_expr<'a>(
121121
}
122122
}
123123
SimpleAssignmentTarget::TSAsExpression(ts_expr) => {
124-
is_same_reference(&ts_expr.expression, right_expr, ctx)
124+
is_same_expression(&ts_expr.expression, right_expr, ctx)
125125
}
126126
SimpleAssignmentTarget::TSSatisfiesExpression(ts_expr) => {
127-
is_same_reference(&ts_expr.expression, right_expr, ctx)
127+
is_same_expression(&ts_expr.expression, right_expr, ctx)
128128
}
129129
SimpleAssignmentTarget::TSNonNullExpression(ts_expr) => {
130-
is_same_reference(&ts_expr.expression, right_expr, ctx)
130+
is_same_expression(&ts_expr.expression, right_expr, ctx)
131131
}
132132
SimpleAssignmentTarget::TSTypeAssertion(ts_expr) => {
133-
is_same_reference(&ts_expr.expression, right_expr, ctx)
133+
is_same_expression(&ts_expr.expression, right_expr, ctx)
134134
}
135135
SimpleAssignmentTarget::TSInstantiationExpression(ts_expr) => {
136-
is_same_reference(&ts_expr.expression, right_expr, ctx)
136+
is_same_expression(&ts_expr.expression, right_expr, ctx)
137137
}
138138
};
139139
}

crates/oxc_linter/src/rules/unicorn/no_length_as_slice_end.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use oxc_macros::declare_oxc_lint;
44
use oxc_span::{GetSpan, Span};
55

66
use crate::{
7-
ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_reference, AstNode,
7+
ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_expression, AstNode,
88
};
99

1010
fn no_length_as_slice_end_diagnostic(call_span: Span, arg_span: Span) -> OxcDiagnostic {
@@ -72,7 +72,7 @@ impl Rule for NoLengthAsSliceEnd {
7272
return;
7373
}
7474

75-
if !is_same_reference(
75+
if !is_same_expression(
7676
call_expr.callee.as_member_expression().unwrap().object(),
7777
&second_argument.object,
7878
ctx,

crates/oxc_linter/src/rules/unicorn/prefer_logical_operator_over_ternary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use oxc_macros::declare_oxc_lint;
44
use oxc_span::{GetSpan, Span};
55
use oxc_syntax::operator::UnaryOperator;
66

7-
use crate::{context::LintContext, rule::Rule, utils::is_same_reference, AstNode};
7+
use crate::{context::LintContext, rule::Rule, utils::is_same_expression, AstNode};
88

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

7171
fn is_same_node(left: &Expression, right: &Expression, ctx: &LintContext) -> bool {
72-
if is_same_reference(left, right, ctx) {
72+
if is_same_expression(left, right, ctx) {
7373
return true;
7474
}
7575

crates/oxc_linter/src/rules/unicorn/prefer_math_min_max.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::borrow::Cow;
22

33
use oxc_ast::{
4-
ast::{BinaryExpression, BinaryOperator, Expression, UnaryOperator},
4+
ast::{BinaryExpression, BinaryOperator, Expression},
55
AstKind,
66
};
77
use oxc_diagnostics::OxcDiagnostic;
88
use oxc_macros::declare_oxc_lint;
99
use oxc_span::Span;
1010

11-
use crate::{context::LintContext, rule::Rule, utils::is_same_reference, AstNode};
11+
use crate::{context::LintContext, rule::Rule, utils::is_same_expression, AstNode};
1212

1313
fn prefer_math_min_max_diagnostic(span: Span) -> OxcDiagnostic {
1414
OxcDiagnostic::warn(
@@ -111,21 +111,6 @@ fn get_expr_value(expr: &Expression) -> Option<String> {
111111
}
112112
}
113113

114-
fn is_same_expression(left: &Expression, right: &Expression, ctx: &LintContext) -> bool {
115-
if is_same_reference(left, right, ctx) {
116-
return true;
117-
}
118-
119-
match (left, right) {
120-
(Expression::UnaryExpression(left_expr), Expression::UnaryExpression(right_expr)) => {
121-
left_expr.operator == UnaryOperator::UnaryNegation
122-
&& right_expr.operator == UnaryOperator::UnaryNegation
123-
&& is_same_reference(&left_expr.argument, &right_expr.argument, ctx)
124-
}
125-
_ => false,
126-
}
127-
}
128-
129114
fn is_min_max(
130115
condition: &BinaryExpression,
131116
consequent: &Expression,

crates/oxc_linter/src/rules/unicorn/prefer_modern_math_apis.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use oxc_span::Span;
88
use oxc_syntax::operator::BinaryOperator;
99

1010
use crate::{
11-
ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_reference, AstNode,
11+
ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_expression, AstNode,
1212
};
1313

1414
fn prefer_math_abs(span: Span) -> OxcDiagnostic {
@@ -249,7 +249,7 @@ fn is_pow_2_expression(expression: &Expression, ctx: &LintContext<'_>) -> bool {
249249
}
250250
}
251251
BinaryOperator::Multiplication => {
252-
is_same_reference(&bin_expr.left, &bin_expr.right, ctx)
252+
is_same_expression(&bin_expr.left, &bin_expr.right, ctx)
253253
}
254254
_ => false,
255255
}

crates/oxc_linter/src/rules/unicorn/prefer_negative_index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use oxc_diagnostics::OxcDiagnostic;
99
use oxc_macros::declare_oxc_lint;
1010
use oxc_span::{GetSpan, Span};
1111

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

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

181181
fn is_same_node(left: &Expression, right: &Expression, ctx: &LintContext) -> bool {
182-
if is_same_reference(left, right, ctx) {
182+
if is_same_expression(left, right, ctx) {
183183
return true;
184184
}
185185

crates/oxc_linter/src/utils/unicorn.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ pub fn get_return_identifier_name<'a>(body: &'a FunctionBody<'_>) -> Option<&'a
148148
}
149149
}
150150

151-
pub fn is_same_reference(left: &Expression, right: &Expression, ctx: &LintContext) -> bool {
151+
/// Compares two expressions to see if they are the same.
152+
pub fn is_same_expression(left: &Expression, right: &Expression, ctx: &LintContext) -> bool {
152153
if let Expression::ChainExpression(left_chain_expr) = left {
153154
if let Some(right_member_expr) = right.as_member_expression() {
154155
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
190191
.expressions
191192
.iter()
192193
.zip(right_str.expressions.iter())
193-
.all(|(left, right)| is_same_reference(left, right, ctx));
194+
.all(|(left, right)| is_same_expression(left, right, ctx));
194195
}
195196
(Expression::NumericLiteral(left_num), Expression::NumericLiteral(right_num)) => {
196197
return left_num.raw == right_num.raw;
@@ -209,12 +210,12 @@ pub fn is_same_reference(left: &Expression, right: &Expression, ctx: &LintContex
209210
Expression::BinaryExpression(right_bin_expr),
210211
) => {
211212
return left_bin_expr.operator == right_bin_expr.operator
212-
&& is_same_reference(
213+
&& is_same_expression(
213214
left_bin_expr.left.get_inner_expression(),
214215
right_bin_expr.left.get_inner_expression(),
215216
ctx,
216217
)
217-
&& is_same_reference(
218+
&& is_same_expression(
218219
left_bin_expr.right.get_inner_expression(),
219220
right_bin_expr.right.get_inner_expression(),
220221
ctx,
@@ -226,7 +227,7 @@ pub fn is_same_reference(left: &Expression, right: &Expression, ctx: &LintContex
226227
Expression::UnaryExpression(right_unary_expr),
227228
) => {
228229
return left_unary_expr.operator == right_unary_expr.operator
229-
&& is_same_reference(
230+
&& is_same_expression(
230231
left_unary_expr.argument.get_inner_expression(),
231232
right_unary_expr.argument.get_inner_expression(),
232233
ctx,
@@ -302,7 +303,7 @@ pub fn is_same_member_expression(
302303
}
303304
}
304305
_ => {
305-
if !is_same_reference(
306+
if !is_same_expression(
306307
left.expression.get_inner_expression(),
307308
right.expression.get_inner_expression(),
308309
ctx,
@@ -313,7 +314,7 @@ pub fn is_same_member_expression(
313314
}
314315
}
315316

316-
is_same_reference(
317+
is_same_expression(
317318
left.object().get_inner_expression(),
318319
right.object().get_inner_expression(),
319320
ctx,

0 commit comments

Comments
 (0)