Skip to content

Commit

Permalink
chore(linter): shorten eslint/eqeqeq rule error message's span (#3193)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven authored May 8, 2024
1 parent 7363e14 commit cba1e7f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
18 changes: 16 additions & 2 deletions crates/oxc_linter/src/rules/eslint/eqeqeq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,32 @@ impl Rule for Eqeqeq {
let (preferred_operator, preferred_operator_with_padding) =
to_strict_eq_operator_str(binary_expr.operator);

#[allow(clippy::cast_possible_truncation)]
let operator_span = {
let left_end = binary_expr.left.span().end;
let right_start = binary_expr.right.span().start;
let offset = Span::new(left_end, right_start)
.source_text(ctx.source_text())
.find(operator)
.unwrap_or(0) as u32;

let operator_start = left_end + offset;
let operator_end = operator_start + operator.len() as u32;
Span::new(operator_start, operator_end)
};

// If the comparison is a `typeof` comparison or both sides are literals with the same type, then it's safe to fix.
if is_type_of_binary_bool || are_literals_and_same_type_bool {
ctx.diagnostic_with_fix(
EqeqeqDiagnostic(operator, preferred_operator, binary_expr.span),
EqeqeqDiagnostic(operator, preferred_operator, operator_span),
|| {
let start = binary_expr.left.span().end;
let end = binary_expr.right.span().start;
Fix::new(preferred_operator_with_padding, Span::new(start, end))
},
);
} else {
ctx.diagnostic(EqeqeqDiagnostic(operator, preferred_operator, binary_expr.span));
ctx.diagnostic(EqeqeqDiagnostic(operator, preferred_operator, operator_span));
}
}
}
Expand Down
40 changes: 20 additions & 20 deletions crates/oxc_linter/src/snapshots/eqeqeq.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,71 +10,71 @@ expression: eqeqeq
help: Prefer > operator

eslint(eqeqeq): Expected === and instead saw ==
╭─[eqeqeq.tsx:1:1]
╭─[eqeqeq.tsx:1:12]
1typeof foo == 'undefined'
· ─────────────────────────
· ──
╰────
help: Prefer === operator

eslint(eqeqeq): Expected !== and instead saw !=
╭─[eqeqeq.tsx:1:1]
╭─[eqeqeq.tsx:1:9]
1'hello' != 'world'
· ──────────────────
· ──
╰────
help: Prefer !== operator

eslint(eqeqeq): Expected === and instead saw ==
╭─[eqeqeq.tsx:1:1]
╭─[eqeqeq.tsx:1:3]
10 == 0
· ──────
· ──
╰────
help: Prefer === operator

eslint(eqeqeq): Expected === and instead saw ==
╭─[eqeqeq.tsx:1:1]
╭─[eqeqeq.tsx:1:6]
1true == true
· ────────────
· ──
╰────
help: Prefer === operator

eslint(eqeqeq): Expected === and instead saw ==
╭─[eqeqeq.tsx:1:1]
╭─[eqeqeq.tsx:1:5]
1foo == null
· ───────────
· ──
╰────
help: Prefer === operator

eslint(eqeqeq): Expected === and instead saw ==
╭─[eqeqeq.tsx:1:1]
╭─[eqeqeq.tsx:1:3]
1a == b
· ──────
· ──
╰────
help: Prefer === operator

eslint(eqeqeq): Expected === and instead saw ==
╭─[eqeqeq.tsx:1:1]
╭─[eqeqeq.tsx:1:5]
1foo == true
· ───────────
· ──
╰────
help: Prefer === operator

eslint(eqeqeq): Expected !== and instead saw !=
╭─[eqeqeq.tsx:1:1]
╭─[eqeqeq.tsx:1:9]
1bananas != 1
· ────────────
· ──
╰────
help: Prefer !== operator

eslint(eqeqeq): Expected === and instead saw ==
╭─[eqeqeq.tsx:1:1]
╭─[eqeqeq.tsx:1:7]
1value == undefined
· ──────────────────
· ──
╰────
help: Prefer === operator

eslint(eqeqeq): Expected === and instead saw ==
╭─[eqeqeq.tsx:1:1]
╭─[eqeqeq.tsx:1:6]
1null == null
· ────────────
· ──
╰────
help: Prefer === operator

0 comments on commit cba1e7f

Please sign in to comment.