@@ -13,7 +13,9 @@ fn missing_parameters(span: Span) -> OxcDiagnostic {
1313}
1414
1515fn 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)
1719}
1820
1921fn redundant_radix ( span : Span ) -> OxcDiagnostic {
@@ -51,7 +53,8 @@ declare_oxc_lint!(
5153 /// ```
5254 Radix ,
5355 eslint,
54- pedantic
56+ pedantic,
57+ conditional_fix_dangerous
5558) ;
5659
5760impl Rule for Radix {
@@ -114,7 +117,18 @@ impl Radix {
114117 0 => ctx. diagnostic ( missing_parameters ( call_expr. span ) ) ,
115118 1 => {
116119 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+ } ) ;
118132 }
119133 }
120134 _ => {
@@ -240,5 +254,21 @@ fn test() {
240254 ( "{ let Number; } (Number?.parseInt)();" , None , None ) ,
241255 ] ;
242256
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 ( ) ;
244274}
0 commit comments