diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_array_some.rs b/crates/oxc_linter/src/rules/unicorn/prefer_array_some.rs index 07dc81d8b..b48b298f4 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_array_some.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_array_some.rs @@ -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(); } diff --git a/crates/oxc_linter/src/snapshots/prefer_array_some.snap b/crates/oxc_linter/src/snapshots/prefer_array_some.snap index a5371b1d8..b4311aace 100644 --- a/crates/oxc_linter/src/snapshots/prefer_array_some.snap +++ b/crates/oxc_linter/src/snapshots/prefer_array_some.snap @@ -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`.