@@ -7,28 +7,51 @@ namespace ts.codefix {
77 errorCodes,
88 getCodeActions : function getCodeActionsToConvertConstToLet ( context ) {
99 const { sourceFile, span, program } = context ;
10- const range = getConstTokenRange ( sourceFile , span . start , program ) ;
11- if ( range === undefined ) return ;
10+ const info = getInfo ( sourceFile , span . start , program ) ;
11+ if ( info === undefined ) return ;
1212
13- const changes = textChanges . ChangeTracker . with ( context , t => doChange ( t , sourceFile , range ) ) ;
14- return [ createCodeFixAction ( fixId , changes , Diagnostics . Convert_const_to_let , fixId , Diagnostics . Convert_const_to_let ) ] ;
13+ const changes = textChanges . ChangeTracker . with ( context , t => doChange ( t , sourceFile , info . token ) ) ;
14+ return [ createCodeFixActionMaybeFixAll ( fixId , changes , Diagnostics . Convert_const_to_let , fixId , Diagnostics . Convert_all_const_to_let ) ] ;
15+ } ,
16+ getAllCodeActions : context => {
17+ const { program } = context ;
18+ const seen = new Map < number , true > ( ) ;
19+
20+ return createCombinedCodeActions ( textChanges . ChangeTracker . with ( context , changes => {
21+ eachDiagnostic ( context , errorCodes , diag => {
22+ const info = getInfo ( diag . file , diag . start , program ) ;
23+ if ( info ) {
24+ if ( addToSeen ( seen , getSymbolId ( info . symbol ) ) ) {
25+ return doChange ( changes , diag . file , info . token ) ;
26+ }
27+ }
28+ return undefined ;
29+ } ) ;
30+ } ) ) ;
1531 } ,
1632 fixIds : [ fixId ]
1733 } ) ;
1834
19- function getConstTokenRange ( sourceFile : SourceFile , pos : number , program : Program ) {
35+ interface Info {
36+ symbol : Symbol ;
37+ token : Token < SyntaxKind . ConstKeyword > ;
38+ }
39+
40+ function getInfo ( sourceFile : SourceFile , pos : number , program : Program ) : Info | undefined {
2041 const checker = program . getTypeChecker ( ) ;
2142 const symbol = checker . getSymbolAtLocation ( getTokenAtPosition ( sourceFile , pos ) ) ;
43+ if ( symbol === undefined ) return ;
44+
2245 const declaration = tryCast ( symbol ?. valueDeclaration ?. parent , isVariableDeclarationList ) ;
2346 if ( declaration === undefined ) return ;
2447
25- const constToken = findChildOfKind ( declaration , SyntaxKind . ConstKeyword , sourceFile ) ;
48+ const constToken = findChildOfKind < Token < SyntaxKind . ConstKeyword > > ( declaration , SyntaxKind . ConstKeyword , sourceFile ) ;
2649 if ( constToken === undefined ) return ;
2750
28- return createRange ( constToken . pos , constToken . end ) ;
51+ return { symbol , token : constToken } ;
2952 }
3053
31- function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , range : TextRange ) {
32- changes . replaceRangeWithText ( sourceFile , range , "let" ) ;
54+ function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , token : Token < SyntaxKind . ConstKeyword > ) {
55+ changes . replaceNode ( sourceFile , token , factory . createToken ( SyntaxKind . LetKeyword ) ) ;
3356 }
3457}
0 commit comments