@@ -13,7 +13,9 @@ fn missing_parameters(span: Span) -> OxcDiagnostic {
13
13
}
14
14
15
15
fn missing_radix ( span : Span ) -> OxcDiagnostic {
16
- OxcDiagnostic :: warn ( "Missing radix parameter." ) . with_label ( span)
16
+ OxcDiagnostic :: warn ( "Missing radix parameter." )
17
+ . with_help ( "Add radix parameter `10' for parsing decimal numbers." )
18
+ . with_label ( span)
17
19
}
18
20
19
21
fn redundant_radix ( span : Span ) -> OxcDiagnostic {
@@ -51,7 +53,8 @@ declare_oxc_lint!(
51
53
/// ```
52
54
Radix ,
53
55
eslint,
54
- pedantic
56
+ pedantic,
57
+ conditional_fix_dangerous
55
58
) ;
56
59
57
60
impl Rule for Radix {
@@ -114,7 +117,18 @@ impl Radix {
114
117
0 => ctx. diagnostic ( missing_parameters ( call_expr. span ) ) ,
115
118
1 => {
116
119
if matches ! ( & self . radix_type, RadixType :: Always ) {
117
- ctx. diagnostic ( missing_radix ( call_expr. span ) ) ;
120
+ let first_arg = & call_expr. arguments [ 0 ] ;
121
+ let end = call_expr. span . end ;
122
+ let check_span = Span :: new ( first_arg. span ( ) . start , end) ;
123
+ let insert_param = ctx
124
+ . source_range ( check_span)
125
+ . chars ( )
126
+ . find_map ( |c| if c == ',' { Some ( " 10," ) } else { None } )
127
+ . unwrap_or ( ", 10" ) ;
128
+
129
+ ctx. diagnostic_with_dangerous_fix ( missing_radix ( call_expr. span ) , |fixer| {
130
+ fixer. insert_text_before_range ( Span :: new ( end - 1 , end - 1 ) , insert_param)
131
+ } ) ;
118
132
}
119
133
}
120
134
_ => {
@@ -240,5 +254,21 @@ fn test() {
240
254
( "{ let Number; } (Number?.parseInt)();" , None , None ) ,
241
255
] ;
242
256
243
- Tester :: new ( Radix :: NAME , Radix :: PLUGIN , pass, fail) . test_and_snapshot ( ) ;
257
+ let fix = vec ! [
258
+ ( "parseInt(10)" , "parseInt(10, 10)" , Some ( json!( [ "always" ] ) ) ) ,
259
+ ( "parseInt(10,)" , "parseInt(10, 10,)" , Some ( json!( [ "always" ] ) ) ) ,
260
+ ( "parseInt(10 )" , "parseInt(10 , 10)" , Some ( json!( [ "always" ] ) ) ) ,
261
+ ( "parseInt(10, )" , "parseInt(10, 10,)" , Some ( json!( [ "always" ] ) ) ) ,
262
+ (
263
+ r#"parseInt("123123" , )"# ,
264
+ r#"parseInt("123123" , 10,)"# ,
265
+ Some ( json!( [ "always" ] ) ) ,
266
+ ) ,
267
+ ( r#"Number.parseInt("10")"# , r#"Number.parseInt("10", 10)"# , Some ( json!( [ "always" ] ) ) ) ,
268
+ ( r#"Number.parseInt("10",)"# , r#"Number.parseInt("10", 10,)"# , Some ( json!( [ "always" ] ) ) ) ,
269
+ ( r#"Number.parseInt?.("10")"# , r#"Number.parseInt?.("10", 10)"# , Some ( json!( [ "always" ] ) ) ) ,
270
+ ( "parseInt(10, /** 213123 */)" , "parseInt(10, /** 213123 */ 10,)" , Some ( json!( [ "always" ] ) ) ) ,
271
+ ] ;
272
+
273
+ Tester :: new ( Radix :: NAME , Radix :: PLUGIN , pass, fail) . expect_fix ( fix) . test_and_snapshot ( ) ;
244
274
}
0 commit comments