@@ -6,7 +6,9 @@ use oxc_diagnostics::OxcDiagnostic;
6
6
use oxc_macros:: declare_oxc_lint;
7
7
use oxc_span:: Span ;
8
8
9
- use crate :: { ast_util:: get_declaration_of_variable, context:: LintContext , rule:: Rule , AstNode } ;
9
+ use crate :: {
10
+ ast_util:: get_declaration_of_variable, context:: LintContext , fixer:: Fix , rule:: Rule , AstNode ,
11
+ } ;
10
12
11
13
fn prefer_set_size_diagnostic ( span0 : Span ) -> OxcDiagnostic {
12
14
OxcDiagnostic :: warn (
@@ -38,7 +40,7 @@ declare_oxc_lint!(
38
40
/// ```
39
41
PreferSetSize ,
40
42
correctness,
41
- pending
43
+ fix
42
44
) ;
43
45
44
46
impl Rule for PreferSetSize {
@@ -68,13 +70,22 @@ impl Rule for PreferSetSize {
68
70
return ;
69
71
} ;
70
72
71
- let maybe_set = & spread_element. argument . without_parenthesized ( ) ;
73
+ let maybe_set = & spread_element. argument . get_inner_expression ( ) ;
72
74
73
75
if !is_set ( maybe_set, ctx) {
74
76
return ;
75
77
}
76
78
77
- ctx. diagnostic ( prefer_set_size_diagnostic ( span) ) ;
79
+ ctx. diagnostic_with_fix ( prefer_set_size_diagnostic ( span) , |_fixer| {
80
+ vec ! [
81
+ // remove [...
82
+ Fix :: delete( Span :: new( array_expr. span. start, spread_element. span. start + 3 ) ) ,
83
+ // remove everything after the end of the spread element (including the `]` )
84
+ Fix :: delete( Span :: new( spread_element. span. end, array_expr. span. end) ) ,
85
+ // replace .length with .size
86
+ Fix :: new( "size" , span) ,
87
+ ]
88
+ } ) ;
78
89
}
79
90
}
80
91
@@ -157,5 +168,17 @@ fn test() {
157
168
r"[...new /* comment */ Set(array)].length" ,
158
169
] ;
159
170
160
- Tester :: new ( PreferSetSize :: NAME , pass, fail) . test_and_snapshot ( ) ;
171
+ let fix = vec ! [
172
+ ( r"[...new Set(array)].length" , r"new Set(array).size" ) ,
173
+ ( r"[...new Set(array),].length" , r"new Set(array).size" ) ,
174
+ ( r"[...(( new Set(array) ))].length" , r"(( new Set(array) )).size" ) ,
175
+ ( r"[...(( new Set(array as foo) ))].length" , r"(( new Set(array as foo) )).size" ) ,
176
+ ( r"[...(( new Set(array) as foo ))].length" , r"(( new Set(array) as foo )).size" ) ,
177
+ (
178
+ r"[...(( new Set(array) as foo ) ) ] .length;" ,
179
+ r"(( new Set(array) as foo ) ) .size;" ,
180
+ ) ,
181
+ ] ;
182
+
183
+ Tester :: new ( PreferSetSize :: NAME , pass, fail) . expect_fix ( fix) . test_and_snapshot ( ) ;
161
184
}
0 commit comments