@@ -58,7 +58,9 @@ impl Rule for NoVar {
5858 && dec. kind == VariableDeclarationKind :: Var
5959 {
6060 let is_written_to = dec. declarations . iter ( ) . any ( |v| is_written_to ( & v. id , ctx) ) ;
61- let span = Span :: sized ( dec. span . start , 3 ) ;
61+ let var_offset = ctx. find_next_token_from ( dec. span . start , "var" ) . unwrap ( ) ;
62+ let var_start = dec. span . start + var_offset;
63+ let span = Span :: sized ( var_start, 3 ) ;
6264 ctx. diagnostic_with_fix ( no_var_diagnostic ( span) , |fixer| {
6365 let parent_span = ctx. nodes ( ) . parent_kind ( node. id ( ) ) . span ( ) ;
6466 if dec. declarations . iter ( ) . any ( |decl| {
@@ -74,7 +76,10 @@ impl Rule for NoVar {
7476
7577 fixer. replace (
7678 span,
77- if is_written_to || !dec. declarations . iter ( ) . all ( |v| v. init . is_some ( ) ) {
79+ if dec. declare
80+ || is_written_to
81+ || !dec. declarations . iter ( ) . all ( |v| v. init . is_some ( ) )
82+ {
7883 "let"
7984 } else {
8085 "const"
@@ -181,6 +186,7 @@ fn test() {
181186 "function play(index: number) { if (index > 1) { var a = undefined } else { var a = undefined } console.log(a) }" ,
182187 "function play(index: number) { if (index > 1) { var a = undefined } else { var a = undefined } console.log(a) }" ,
183188 ) ,
189+ ( "declare var foo = 2;" , "declare let foo = 2;" ) ,
184190 ] ;
185191
186192 Tester :: new ( NoVar :: NAME , NoVar :: PLUGIN , pass, fail) . expect_fix ( fix) . test_and_snapshot ( ) ;
0 commit comments