Skip to content

Commit 140b100

Browse files
antoinezanardicamc314
authored andcommitted
refactor(eqeqeq): improve readability and simplify operator handling in binary expressions
1 parent b37da22 commit 140b100

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

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

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,11 @@ impl Eqeqeq {
210210
return;
211211
}
212212
let operator = binary_expr.operator.as_str();
213+
let truncated_operator = &operator[..operator.len() - 1];
213214
// There are some uncontrolled cases to auto fix.
214215
// In ESLint, `null >= null` will be auto fixed to `null > null` which is also wrong.
215216
// So I just report it.
216-
ctx.diagnostic(eqeqeq_diagnostic(
217-
operator,
218-
&operator[0..operator.len() - 1],
219-
binary_expr.span,
220-
));
217+
ctx.diagnostic(eqeqeq_diagnostic(operator, truncated_operator, binary_expr.span));
221218
}
222219
}
223220

@@ -241,30 +238,30 @@ impl Rule for Eqeqeq {
241238
let AstKind::BinaryExpression(binary_expr) = node.kind() else {
242239
return;
243240
};
244-
let is_null = is_null_check(binary_expr);
245-
let enforce_rule_for_null = matches!(self.null_type, NullType::Always);
241+
let is_null_comparison = is_null_check(binary_expr);
242+
let must_enforce_null = matches!(self.null_type, NullType::Always);
246243

247244
if !matches!(binary_expr.operator, BinaryOperator::Equality | BinaryOperator::Inequality) {
248-
if is_null {
245+
if is_null_comparison {
249246
self.report_inverse_null_comparison(binary_expr, ctx);
250247
}
251248
return;
252249
}
253250

254-
let is_type_of_binary_bool = is_type_of_binary(binary_expr);
255-
let are_literals_and_same_type_bool =
251+
let is_typeof_check = is_type_of_binary(binary_expr);
252+
let is_same_type_literals =
256253
are_literals_and_same_type(&binary_expr.left, &binary_expr.right);
257254
// The "smart" option enforces the use of `===` and `!==` except for these cases:
258255
// - Comparing two literal values
259256
// - Evaluating the value of typeof
260257
// - Comparing against null
261258
if matches!(self.compare_type, CompareType::Smart)
262-
&& (is_type_of_binary_bool || are_literals_and_same_type_bool || is_null)
259+
&& (is_typeof_check || is_same_type_literals || is_null_comparison)
263260
{
264261
return;
265262
}
266263

267-
if !enforce_rule_for_null && is_null {
264+
if !must_enforce_null && is_null_comparison {
268265
return;
269266
}
270267

@@ -274,7 +271,7 @@ impl Rule for Eqeqeq {
274271

275272
let operator_span = get_operator_span(binary_expr, operator, ctx);
276273

277-
let fix_kind = if is_type_of_binary_bool || are_literals_and_same_type_bool {
274+
let fix_kind = if is_typeof_check || is_same_type_literals {
278275
FixKind::SafeFix
279276
} else {
280277
FixKind::DangerousFix
@@ -292,12 +289,12 @@ impl Rule for Eqeqeq {
292289
fn get_operator_span(binary_expr: &BinaryExpression, operator: &str, ctx: &LintContext) -> Span {
293290
let left_end = binary_expr.left.span().end;
294291
let right_start = binary_expr.right.span().start;
295-
let offset =
296-
Span::new(left_end, right_start).source_text(ctx.source_text()).find(operator).unwrap_or(0)
297-
as u32;
292+
let between_text = Span::new(left_end, right_start).source_text(ctx.source_text());
293+
let offset = between_text.find(operator).unwrap_or(0) as u32;
298294

299295
let operator_start = left_end + offset;
300296
let operator_end = operator_start + operator.len() as u32;
297+
301298
Span::new(operator_start, operator_end)
302299
}
303300

@@ -306,9 +303,7 @@ fn apply_rule_fix<'a>(
306303
binary_expr: &BinaryExpression,
307304
preferred_operator_with_padding: &'a str,
308305
) -> RuleFix<'a> {
309-
let start = binary_expr.left.span().end;
310-
let end = binary_expr.right.span().start;
311-
let span = Span::new(start, end);
306+
let span = Span::new(binary_expr.left.span().end, binary_expr.right.span().start);
312307

313308
fixer.replace(span, preferred_operator_with_padding)
314309
}
@@ -317,19 +312,20 @@ fn to_strict_eq_operator_str(operator: BinaryOperator) -> (&'static str, &'stati
317312
match operator {
318313
BinaryOperator::Equality => ("===", " === "),
319314
BinaryOperator::Inequality => ("!==", " !== "),
320-
_ => unreachable!(),
315+
_ => unreachable!("Unexpected operator passed to to_strict_eq_operator_str"),
321316
}
322317
}
323318

324-
/// Checks if either operand of a binary expression is a typeof operation
319+
fn is_type_of(expr: &Expression) -> bool {
320+
matches!(
321+
expr,
322+
Expression::UnaryExpression(unary_expr) if matches!(unary_expr.operator, UnaryOperator::Typeof)
323+
)
324+
}
325+
326+
/// Checks if either operand of a binary expression is a `typeof` operation.
325327
fn is_type_of_binary(binary_expr: &BinaryExpression) -> bool {
326-
match (&binary_expr.left, &binary_expr.right) {
327-
(Expression::UnaryExpression(unary_expr), _)
328-
| (_, Expression::UnaryExpression(unary_expr)) => {
329-
matches!(unary_expr.operator, UnaryOperator::Typeof)
330-
}
331-
_ => false,
332-
}
328+
is_type_of(&binary_expr.left) || is_type_of(&binary_expr.right)
333329
}
334330

335331
/// Checks if operands are literals of the same type

0 commit comments

Comments
 (0)