mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
feat(linter/unicorn): add fixer to prefer-array-some (#5153)
This commit is contained in:
parent
f8bb0222b3
commit
ac7edccab3
2 changed files with 80 additions and 10 deletions
|
|
@ -46,7 +46,7 @@ declare_oxc_lint!(
|
|||
/// ```
|
||||
PreferArraySome,
|
||||
pedantic,
|
||||
pending
|
||||
fix
|
||||
);
|
||||
|
||||
impl Rule for PreferArraySome {
|
||||
|
|
@ -62,10 +62,26 @@ impl Rule for PreferArraySome {
|
|||
return;
|
||||
}
|
||||
|
||||
ctx.diagnostic(over_method(
|
||||
// SAFETY: `call_expr_method_callee_info` returns `Some` if `is_method_call` returns `true`.
|
||||
call_expr_method_callee_info(call_expr).unwrap().0,
|
||||
));
|
||||
ctx.diagnostic_with_fix(
|
||||
over_method(
|
||||
// SAFETY: `call_expr_method_callee_info` returns `Some` if `is_method_call` returns `true`.
|
||||
call_expr_method_callee_info(call_expr).unwrap().0,
|
||||
),
|
||||
|fixer| {
|
||||
let target_span = call_expr
|
||||
.callee
|
||||
.as_member_expression()
|
||||
.and_then(|v| v.static_property_info().map(|(span, _)| span));
|
||||
|
||||
debug_assert!(target_span.is_some());
|
||||
|
||||
if let Some(target_span) = target_span {
|
||||
fixer.replace(target_span, "some")
|
||||
} else {
|
||||
fixer.noop()
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
AstKind::BinaryExpression(bin_expr) => {
|
||||
if !matches!(
|
||||
|
|
@ -117,10 +133,26 @@ impl Rule for PreferArraySome {
|
|||
return;
|
||||
}
|
||||
|
||||
ctx.diagnostic(non_zero_filter(
|
||||
// SAFETY: `call_expr_method_callee_info` returns `Some` if `is_method_call` returns `true`.
|
||||
call_expr_method_callee_info(left_call_expr).unwrap().0,
|
||||
));
|
||||
ctx.diagnostic_with_fix(
|
||||
non_zero_filter(
|
||||
// SAFETY: `call_expr_method_callee_info` returns `Some` if `is_method_call` returns `true`.
|
||||
call_expr_method_callee_info(left_call_expr).unwrap().0,
|
||||
),
|
||||
|fixer| {
|
||||
let target_span = left_call_expr
|
||||
.callee
|
||||
.as_member_expression()
|
||||
.and_then(|v| v.static_property_info().map(|(span, _)| span));
|
||||
|
||||
debug_assert!(target_span.is_some());
|
||||
|
||||
if let Some(target_span) = target_span {
|
||||
fixer.replace(target_span, "some")
|
||||
} else {
|
||||
fixer.noop()
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
@ -264,5 +296,30 @@ fn test() {
|
|||
r#"a = (( ((foo.find(fn))) == ((null)) )) ? "no" : "yes";"#,
|
||||
];
|
||||
|
||||
Tester::new(PreferArraySome::NAME, pass, fail).test_and_snapshot();
|
||||
let fix = vec![
|
||||
(r"if (foo.find(fn)) {}", r"if (foo.some(fn)) {}"),
|
||||
(r"if (foo.findLast(fn)) {}", r"if (foo.some(fn)) {}"),
|
||||
(
|
||||
r#"if (array.find(element => element === "🦄")) {}"#,
|
||||
r#"if (array.some(element => element === "🦄")) {}"#,
|
||||
),
|
||||
(
|
||||
r#"const foo = array.find(element => element === "🦄") ? bar : baz;"#,
|
||||
r#"const foo = array.some(element => element === "🦄") ? bar : baz;"#,
|
||||
),
|
||||
(r"array.filter(fn).length > 0", r"array.some(fn).length > 0"),
|
||||
(r"array.filter(fn).length !== 0", r"array.some(fn).length !== 0"),
|
||||
(r"foo.find(fn) == null", r"foo.some(fn) == null"),
|
||||
(r"foo.find(fn) == undefined", r"foo.some(fn) == undefined"),
|
||||
(r"foo.find(fn) === undefined", r"foo.some(fn) === undefined"),
|
||||
(r"foo.find(fn) != null", r"foo.some(fn) != null"),
|
||||
(r"foo.find(fn) != undefined", r"foo.some(fn) != undefined"),
|
||||
(r"foo.find(fn) !== undefined", r"foo.some(fn) !== undefined"),
|
||||
(
|
||||
r#"a = (( ((foo.find(fn))) == ((null)) )) ? "no" : "yes";"#,
|
||||
r#"a = (( ((foo.some(fn))) == ((null)) )) ? "no" : "yes";"#,
|
||||
),
|
||||
];
|
||||
|
||||
Tester::new(PreferArraySome::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,75 +6,88 @@ source: crates/oxc_linter/src/tester.rs
|
|||
1 │ if (foo.find(fn)) {}
|
||||
· ────
|
||||
╰────
|
||||
help: Replace `find` with `some`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-array-some): Prefer `.some(…)` over `.find(…)`or `.findLast(…)`.
|
||||
╭─[prefer_array_some.tsx:1:9]
|
||||
1 │ if (foo.findLast(fn)) {}
|
||||
· ────────
|
||||
╰────
|
||||
help: Replace `findLast` with `some`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-array-some): Prefer `.some(…)` over `.find(…)`or `.findLast(…)`.
|
||||
╭─[prefer_array_some.tsx:1:11]
|
||||
1 │ if (array.find(element => element === "🦄")) {}
|
||||
· ────
|
||||
╰────
|
||||
help: Replace `find` with `some`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-array-some): Prefer `.some(…)` over `.find(…)`or `.findLast(…)`.
|
||||
╭─[prefer_array_some.tsx:1:19]
|
||||
1 │ const foo = array.find(element => element === "🦄") ? bar : baz;
|
||||
· ────
|
||||
╰────
|
||||
help: Replace `find` with `some`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-array-some): Prefer `.some(…)` over non-zero length check from `.filter(…)`.
|
||||
╭─[prefer_array_some.tsx:1:7]
|
||||
1 │ array.filter(fn).length > 0
|
||||
· ──────
|
||||
╰────
|
||||
help: Replace `filter` with `some`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-array-some): Prefer `.some(…)` over non-zero length check from `.filter(…)`.
|
||||
╭─[prefer_array_some.tsx:1:7]
|
||||
1 │ array.filter(fn).length !== 0
|
||||
· ──────
|
||||
╰────
|
||||
help: Replace `filter` with `some`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-array-some): Prefer `.some(…)` over `.find(…)`or `.findLast(…)`.
|
||||
╭─[prefer_array_some.tsx:1:5]
|
||||
1 │ foo.find(fn) == null
|
||||
· ────
|
||||
╰────
|
||||
help: Replace `find` with `some`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-array-some): Prefer `.some(…)` over `.find(…)`or `.findLast(…)`.
|
||||
╭─[prefer_array_some.tsx:1:5]
|
||||
1 │ foo.find(fn) == undefined
|
||||
· ────
|
||||
╰────
|
||||
help: Replace `find` with `some`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-array-some): Prefer `.some(…)` over `.find(…)`or `.findLast(…)`.
|
||||
╭─[prefer_array_some.tsx:1:5]
|
||||
1 │ foo.find(fn) === undefined
|
||||
· ────
|
||||
╰────
|
||||
help: Replace `find` with `some`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-array-some): Prefer `.some(…)` over `.find(…)`or `.findLast(…)`.
|
||||
╭─[prefer_array_some.tsx:1:5]
|
||||
1 │ foo.find(fn) != null
|
||||
· ────
|
||||
╰────
|
||||
help: Replace `find` with `some`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-array-some): Prefer `.some(…)` over `.find(…)`or `.findLast(…)`.
|
||||
╭─[prefer_array_some.tsx:1:5]
|
||||
1 │ foo.find(fn) != undefined
|
||||
· ────
|
||||
╰────
|
||||
help: Replace `find` with `some`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-array-some): Prefer `.some(…)` over `.find(…)`or `.findLast(…)`.
|
||||
╭─[prefer_array_some.tsx:1:5]
|
||||
1 │ foo.find(fn) !== undefined
|
||||
· ────
|
||||
╰────
|
||||
help: Replace `find` with `some`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-array-some): Prefer `.some(…)` over `.find(…)`or `.findLast(…)`.
|
||||
╭─[prefer_array_some.tsx:1:14]
|
||||
1 │ a = (( ((foo.find(fn))) == ((null)) )) ? "no" : "yes";
|
||||
· ────
|
||||
╰────
|
||||
help: Replace `find` with `some`.
|
||||
|
|
|
|||
Loading…
Reference in a new issue