@@ -46,7 +46,7 @@ declare_oxc_lint!(
46
46
/// ```
47
47
PreferArraySome ,
48
48
pedantic,
49
- pending
49
+ fix
50
50
) ;
51
51
52
52
impl Rule for PreferArraySome {
@@ -62,10 +62,26 @@ impl Rule for PreferArraySome {
62
62
return ;
63
63
}
64
64
65
- ctx. diagnostic ( over_method (
66
- // SAFETY: `call_expr_method_callee_info` returns `Some` if `is_method_call` returns `true`.
67
- call_expr_method_callee_info ( call_expr) . unwrap ( ) . 0 ,
68
- ) ) ;
65
+ ctx. diagnostic_with_fix (
66
+ over_method (
67
+ // SAFETY: `call_expr_method_callee_info` returns `Some` if `is_method_call` returns `true`.
68
+ call_expr_method_callee_info ( call_expr) . unwrap ( ) . 0 ,
69
+ ) ,
70
+ |fixer| {
71
+ let target_span = call_expr
72
+ . callee
73
+ . as_member_expression ( )
74
+ . and_then ( |v| v. static_property_info ( ) . map ( |( span, _) | span) ) ;
75
+
76
+ debug_assert ! ( target_span. is_some( ) ) ;
77
+
78
+ if let Some ( target_span) = target_span {
79
+ fixer. replace ( target_span, "some" )
80
+ } else {
81
+ fixer. noop ( )
82
+ }
83
+ } ,
84
+ ) ;
69
85
}
70
86
AstKind :: BinaryExpression ( bin_expr) => {
71
87
if !matches ! (
@@ -117,10 +133,26 @@ impl Rule for PreferArraySome {
117
133
return ;
118
134
}
119
135
120
- ctx. diagnostic ( non_zero_filter (
121
- // SAFETY: `call_expr_method_callee_info` returns `Some` if `is_method_call` returns `true`.
122
- call_expr_method_callee_info ( left_call_expr) . unwrap ( ) . 0 ,
123
- ) ) ;
136
+ ctx. diagnostic_with_fix (
137
+ non_zero_filter (
138
+ // SAFETY: `call_expr_method_callee_info` returns `Some` if `is_method_call` returns `true`.
139
+ call_expr_method_callee_info ( left_call_expr) . unwrap ( ) . 0 ,
140
+ ) ,
141
+ |fixer| {
142
+ let target_span = left_call_expr
143
+ . callee
144
+ . as_member_expression ( )
145
+ . and_then ( |v| v. static_property_info ( ) . map ( |( span, _) | span) ) ;
146
+
147
+ debug_assert ! ( target_span. is_some( ) ) ;
148
+
149
+ if let Some ( target_span) = target_span {
150
+ fixer. replace ( target_span, "some" )
151
+ } else {
152
+ fixer. noop ( )
153
+ }
154
+ } ,
155
+ ) ;
124
156
}
125
157
_ => { }
126
158
}
@@ -264,5 +296,30 @@ fn test() {
264
296
r#"a = (( ((foo.find(fn))) == ((null)) )) ? "no" : "yes";"# ,
265
297
] ;
266
298
267
- Tester :: new ( PreferArraySome :: NAME , pass, fail) . test_and_snapshot ( ) ;
299
+ let fix = vec ! [
300
+ ( r"if (foo.find(fn)) {}" , r"if (foo.some(fn)) {}" ) ,
301
+ ( r"if (foo.findLast(fn)) {}" , r"if (foo.some(fn)) {}" ) ,
302
+ (
303
+ r#"if (array.find(element => element === "🦄")) {}"# ,
304
+ r#"if (array.some(element => element === "🦄")) {}"# ,
305
+ ) ,
306
+ (
307
+ r#"const foo = array.find(element => element === "🦄") ? bar : baz;"# ,
308
+ r#"const foo = array.some(element => element === "🦄") ? bar : baz;"# ,
309
+ ) ,
310
+ ( r"array.filter(fn).length > 0" , r"array.some(fn).length > 0" ) ,
311
+ ( r"array.filter(fn).length !== 0" , r"array.some(fn).length !== 0" ) ,
312
+ ( r"foo.find(fn) == null" , r"foo.some(fn) == null" ) ,
313
+ ( r"foo.find(fn) == undefined" , r"foo.some(fn) == undefined" ) ,
314
+ ( r"foo.find(fn) === undefined" , r"foo.some(fn) === undefined" ) ,
315
+ ( r"foo.find(fn) != null" , r"foo.some(fn) != null" ) ,
316
+ ( r"foo.find(fn) != undefined" , r"foo.some(fn) != undefined" ) ,
317
+ ( r"foo.find(fn) !== undefined" , r"foo.some(fn) !== undefined" ) ,
318
+ (
319
+ r#"a = (( ((foo.find(fn))) == ((null)) )) ? "no" : "yes";"# ,
320
+ r#"a = (( ((foo.some(fn))) == ((null)) )) ? "no" : "yes";"# ,
321
+ ) ,
322
+ ] ;
323
+
324
+ Tester :: new ( PreferArraySome :: NAME , pass, fail) . expect_fix ( fix) . test_and_snapshot ( ) ;
268
325
}
0 commit comments