@@ -7,26 +7,28 @@ namespace ts.codefix {
77 errorCodes,
88 getCodeActions : context => {
99 const { sourceFile, span, program } = context ;
10- const variableStatement = getVariableStatement ( sourceFile , span . start , program ) ;
11- const changes = textChanges . ChangeTracker . with ( context , t => doChange ( t , sourceFile , variableStatement ) ) ;
10+ const range = getConstTokenRange ( sourceFile , span . start , program ) ;
11+ if ( range === undefined ) return ;
12+
13+ const changes = textChanges . ChangeTracker . with ( context , t => doChange ( t , sourceFile , range ) ) ;
1214 return [ createCodeFixAction ( fixId , changes , Diagnostics . Convert_const_to_let , fixId , Diagnostics . Convert_const_to_let ) ] ;
1315 } ,
1416 fixIds : [ fixId ]
1517 } ) ;
1618
17- function getVariableStatement ( sourceFile : SourceFile , pos : number , program : Program ) {
18- const token = getTokenAtPosition ( sourceFile , pos ) ;
19+ function getConstTokenRange ( sourceFile : SourceFile , pos : number , program : Program ) {
1920 const checker = program . getTypeChecker ( ) ;
20- const symbol = checker . getSymbolAtLocation ( token ) ;
21- if ( symbol ?. valueDeclaration ) {
22- return symbol . valueDeclaration . parent . parent as VariableStatement ;
23- }
21+ const symbol = checker . getSymbolAtLocation ( getTokenAtPosition ( sourceFile , pos ) ) ;
22+ const declaration = tryCast ( symbol ?. valueDeclaration ?. parent , isVariableDeclarationList ) ;
23+ if ( declaration === undefined ) return ;
24+
25+ const constToken = findChildOfKind ( declaration , SyntaxKind . ConstKeyword , sourceFile ) ;
26+ if ( constToken === undefined ) return ;
27+
28+ return createRange ( constToken . pos , constToken . end ) ;
2429 }
25- function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , variableStatement ?: VariableStatement ) {
26- if ( ! variableStatement ) {
27- return ;
28- }
29- const start = variableStatement . getStart ( ) ;
30- changes . replaceRangeWithText ( sourceFile , { pos : start , end : start + 5 } , "let" ) ;
30+
31+ function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , range : TextRange ) {
32+ changes . replaceRangeWithText ( sourceFile , range , "let" ) ;
3133 }
3234}
0 commit comments