@@ -7,28 +7,51 @@ namespace ts.codefix {
7
7
errorCodes,
8
8
getCodeActions : function getCodeActionsToConvertConstToLet ( context ) {
9
9
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 ;
12
12
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
+ } ) ) ;
15
31
} ,
16
32
fixIds : [ fixId ]
17
33
} ) ;
18
34
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 {
20
41
const checker = program . getTypeChecker ( ) ;
21
42
const symbol = checker . getSymbolAtLocation ( getTokenAtPosition ( sourceFile , pos ) ) ;
43
+ if ( symbol === undefined ) return ;
44
+
22
45
const declaration = tryCast ( symbol ?. valueDeclaration ?. parent , isVariableDeclarationList ) ;
23
46
if ( declaration === undefined ) return ;
24
47
25
- const constToken = findChildOfKind ( declaration , SyntaxKind . ConstKeyword , sourceFile ) ;
48
+ const constToken = findChildOfKind < Token < SyntaxKind . ConstKeyword > > ( declaration , SyntaxKind . ConstKeyword , sourceFile ) ;
26
49
if ( constToken === undefined ) return ;
27
50
28
- return createRange ( constToken . pos , constToken . end ) ;
51
+ return { symbol , token : constToken } ;
29
52
}
30
53
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 ) ) ;
33
56
}
34
57
}
0 commit comments