Skip to content

Commit

Permalink
feat(linter/unicorn): add fixer to prefer-array-some (#5153)
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Aug 25, 2024
1 parent f8bb022 commit ac7edcc
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
77 changes: 67 additions & 10 deletions crates/oxc_linter/src/rules/unicorn/prefer_array_some.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ declare_oxc_lint!(
/// ```
PreferArraySome,
pedantic,
pending
fix
);

impl Rule for PreferArraySome {
Expand All @@ -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!(
Expand Down Expand Up @@ -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()
}
},
);
}
_ => {}
}
Expand Down Expand Up @@ -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();
}
13 changes: 13 additions & 0 deletions crates/oxc_linter/src/snapshots/prefer_array_some.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,88 @@ source: crates/oxc_linter/src/tester.rs
1if (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]
1if (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]
1if (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]
1const 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]
1array.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]
1array.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1a = (( ((foo.find(fn))) == ((null)) )) ? "no" : "yes";
· ────
╰────
help: Replace `find` with `some`.

0 comments on commit ac7edcc

Please sign in to comment.