Skip to content
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
26 changes: 16 additions & 10 deletions crates/oxc_linter/src/rules/unicorn/prefer_array_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ impl Rule for PreferArrayFind {

if let AstKind::CallExpression(call_expr) = node.kind() {
if is_method_call(call_expr, None, Some(&["shift"]), Some(0), Some(0)) {
if let Expression::CallExpression(filter_call_expr) =
call_expr.callee.as_member_expression().unwrap().object().get_inner_expression()
if let Some(Expression::CallExpression(filter_call_expr)) = call_expr
.callee
.get_inner_expression()
.as_member_expression()
.map(|expression| expression.object().get_inner_expression())
{
if is_filter_call(filter_call_expr) {
ctx.diagnostic(prefer_array_find_diagnostic(
Expand Down Expand Up @@ -197,12 +200,11 @@ impl Rule for PreferArrayFind {
})
})
{
if let Expression::CallExpression(filter_call_expr) = at_call_expr
if let Some(Expression::CallExpression(filter_call_expr)) = at_call_expr
.callee
.as_member_expression()
.unwrap()
.object()
.get_inner_expression()
.as_member_expression()
.map(|expression| expression.object().get_inner_expression())
{
if is_filter_call(filter_call_expr) {
ctx.diagnostic(prefer_array_find_diagnostic(
Expand All @@ -216,12 +218,11 @@ impl Rule for PreferArrayFind {
// `array.filter().pop()`
if let AstKind::CallExpression(pop_call_expr) = node.kind() {
if is_method_call(pop_call_expr, None, Some(&["pop"]), Some(0), Some(0)) {
if let Expression::CallExpression(filter_call_expr) = pop_call_expr
if let Some(Expression::CallExpression(filter_call_expr)) = pop_call_expr
.callee
.as_member_expression()
.unwrap()
.object()
.get_inner_expression()
.as_member_expression()
.map(|expression| expression.object().get_inner_expression())
{
if is_filter_call(filter_call_expr) {
ctx.diagnostic(prefer_array_find_diagnostic(
Expand Down Expand Up @@ -427,6 +428,8 @@ fn test() {
"array.filter().at(0)",
"array.filter(foo, thisArgument, extraArgument).at(0)",
"array.filter(...foo).at(0)",
// oxc-project/oxc#12399
"{a.pop!()}",
];

let fail = vec![
Expand Down Expand Up @@ -609,6 +612,9 @@ fn test() {
)
// comment 6
;",
// oxc-project/oxc#12399
"array.filter(foo).pop!()",
"array.filter(foo)?.pop()",
];

let _fix: Vec<(&'static str, &'static str, Option<serde_json::Value>)> = vec![
Expand Down
14 changes: 14 additions & 0 deletions crates/oxc_linter/src/snapshots/unicorn_prefer_array_find.snap
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,17 @@ source: crates/oxc_linter/src/tester.rs
4 │ // comment 2
╰────
help: Use 'find(predicate)' instead of 'filter(predicate)[0]' or similar patterns

⚠ eslint-plugin-unicorn(prefer-array-find): Prefer 'find' over filtering and accessing the first result
╭─[prefer_array_find.tsx:1:7]
1 │ array.filter(foo).pop!()
· ──────
╰────
help: Use 'find(predicate)' instead of 'filter(predicate)[0]' or similar patterns

⚠ eslint-plugin-unicorn(prefer-array-find): Prefer 'find' over filtering and accessing the first result
╭─[prefer_array_find.tsx:1:7]
1 │ array.filter(foo)?.pop()
· ──────
╰────
help: Use 'find(predicate)' instead of 'filter(predicate)[0]' or similar patterns
Loading