Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linter/unicorn): add fixer to prefer-array-some #5153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
camc314 marked this conversation as resolved.
Show resolved Hide resolved
);

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
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`.