mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
fix(linter): false positive in eslint/yoda (#7719)
This commit is contained in:
parent
b9a2b35e5a
commit
5e6053f35a
2 changed files with 27 additions and 11 deletions
|
|
@ -304,34 +304,35 @@ fn do_diagnostic_with_fix(expr: &BinaryExpression, ctx: &LintContext, never: boo
|
||||||
|
|
||||||
let left_start = expr.left.span().start;
|
let left_start = expr.left.span().start;
|
||||||
let left_prev_token = if left_start > 0 && (expr.right.is_literal() || expr.right.is_identifier_reference() ) {
|
let left_prev_token = if left_start > 0 && (expr.right.is_literal() || expr.right.is_identifier_reference() ) {
|
||||||
let token = ctx.source_range(Span::new(left_start - 1, left_start));
|
let tokens = ctx.source_range(Span::new(0, left_start));
|
||||||
|
let token = tokens.chars().last();
|
||||||
match_token(token)
|
match_token(token)
|
||||||
} else {
|
} else {
|
||||||
""
|
false
|
||||||
};
|
};
|
||||||
|
|
||||||
let right_end = expr.right.span().end;
|
let right_end = expr.right.span().end;
|
||||||
let source_size = u32::try_from(ctx.source_text().len()).unwrap();
|
let source_size = u32::try_from(ctx.source_text().len()).unwrap();
|
||||||
let right_next_token = if right_end < source_size && (expr.left.is_literal() || expr.left.is_identifier_reference()) {
|
let right_next_token = if right_end < source_size && (expr.left.is_literal() || expr.left.is_identifier_reference()) {
|
||||||
let token = ctx.source_range(Span::new(right_end, right_end + 1));
|
let tokens = ctx.source_range(Span::new(right_end, source_size));
|
||||||
|
let token = tokens.chars().next();
|
||||||
match_token(token)
|
match_token(token)
|
||||||
} else {
|
} else {
|
||||||
""
|
false
|
||||||
};
|
};
|
||||||
|
|
||||||
let replacement = format!(
|
let replacement = format!(
|
||||||
"{left_prev_token}{right_str}{str_between_left_and_operator}{flipped_operator_str}{str_between_operator_and_right}{left_str}{right_next_token}"
|
"{}{right_str}{str_between_left_and_operator}{flipped_operator_str}{str_between_operator_and_right}{left_str}{}",
|
||||||
|
if left_prev_token { " " } else { "" },
|
||||||
|
if right_next_token { " " } else { "" }
|
||||||
);
|
);
|
||||||
|
|
||||||
fix.replace(expr.span, replacement)
|
fix.replace(expr.span, replacement)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn match_token(token: &str) -> &str {
|
fn match_token(token: Option<char>) -> bool {
|
||||||
match token {
|
!matches!(token, Some(' ' | '(' | ')' | '/' | '=' | ';'))
|
||||||
" " | "(" | ")" | "/" | "=" | ";" => "",
|
|
||||||
_ => " ",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flip_operator(operator: BinaryOperator) -> BinaryOperator {
|
fn flip_operator(operator: BinaryOperator) -> BinaryOperator {
|
||||||
|
|
@ -840,6 +841,8 @@ fn test() {
|
||||||
),
|
),
|
||||||
("if('a' <= x && x < 1) {}", Some(serde_json::json!(["never", { "exceptRange": true }]))),
|
("if('a' <= x && x < 1) {}", Some(serde_json::json!(["never", { "exceptRange": true }]))),
|
||||||
("if (0 < a && b < max) {}", Some(serde_json::json!(["never", { "exceptRange": true }]))),
|
("if (0 < a && b < max) {}", Some(serde_json::json!(["never", { "exceptRange": true }]))),
|
||||||
|
// Issue: <https://github.com/oxc-project/oxc/issues/7714>
|
||||||
|
("{( t=='' )}", Some(serde_json::json!(["always", { "onlyEquality": true }]))),
|
||||||
];
|
];
|
||||||
|
|
||||||
let fix = vec![
|
let fix = vec![
|
||||||
|
|
@ -1162,6 +1165,13 @@ fn test() {
|
||||||
Some(serde_json::json!(["never", { "exceptRange": true }])),
|
Some(serde_json::json!(["never", { "exceptRange": true }])),
|
||||||
),
|
),
|
||||||
("y>E>1", "1<y>E", Some(serde_json::json!(["always", { "exceptRange": false }]))),
|
("y>E>1", "1<y>E", Some(serde_json::json!(["always", { "exceptRange": false }]))),
|
||||||
|
// Issue: <https://github.com/oxc-project/oxc/issues/7714>
|
||||||
|
(
|
||||||
|
"{( t=='' )}",
|
||||||
|
"{( ''==t )}",
|
||||||
|
Some(serde_json::json!(["always", { "onlyEquality": true }])),
|
||||||
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
Tester::new(Yoda::NAME, Yoda::CATEGORY, pass, fail).expect_fix(fix).test_and_snapshot();
|
Tester::new(Yoda::NAME, Yoda::CATEGORY, pass, fail).expect_fix(fix).test_and_snapshot();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
---
|
---
|
||||||
source: crates/oxc_linter/src/tester.rs
|
source: crates/oxc_linter/src/tester.rs
|
||||||
assertion_line: 356
|
|
||||||
snapshot_kind: text
|
snapshot_kind: text
|
||||||
---
|
---
|
||||||
⚠ eslint(yoda): Require or disallow "Yoda" conditions
|
⚠ eslint(yoda): Require or disallow "Yoda" conditions
|
||||||
|
|
@ -597,3 +596,10 @@ snapshot_kind: text
|
||||||
· ─────
|
· ─────
|
||||||
╰────
|
╰────
|
||||||
help: Expected literal to be on the right side of <.
|
help: Expected literal to be on the right side of <.
|
||||||
|
|
||||||
|
⚠ eslint(yoda): Require or disallow "Yoda" conditions
|
||||||
|
╭─[yoda.tsx:1:5]
|
||||||
|
1 │ {( t=='' )}
|
||||||
|
· ─────
|
||||||
|
╰────
|
||||||
|
help: Expected literal to be on the left side of ==.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue