@@ -9,43 +9,44 @@ namespace ts.codefix {
9
9
registerCodeFix ( {
10
10
errorCodes,
11
11
getCodeActions : ( context ) => {
12
- const changes = textChanges . ChangeTracker . with ( context , t => makeChange ( t , context . sourceFile , context . span . start ) ) ;
12
+ const changes = textChanges . ChangeTracker . with ( context , t => makeChange ( t , context . sourceFile , context . span . start , context . program ) ) ;
13
13
if ( changes . length > 0 ) {
14
14
return [ createCodeFixAction ( fixId , changes , Diagnostics . Add_const_to_unresolved_variable , fixId , Diagnostics . Add_const_to_all_unresolved_variables ) ] ;
15
15
}
16
16
} ,
17
17
fixIds : [ fixId ] ,
18
18
getAllCodeActions : context => {
19
19
const fixedNodes = new NodeSet ( ) ;
20
- return codeFixAll ( context , errorCodes , ( changes , diag ) => makeChange ( changes , diag . file , diag . start , fixedNodes ) ) ;
20
+ return codeFixAll ( context , errorCodes , ( changes , diag ) => makeChange ( changes , diag . file , diag . start , context . program , fixedNodes ) ) ;
21
21
} ,
22
22
} ) ;
23
23
24
- function makeChange ( changeTracker : textChanges . ChangeTracker , sourceFile : SourceFile , pos : number , fixedNodes ?: NodeSet < Node > ) {
24
+ function makeChange ( changeTracker : textChanges . ChangeTracker , sourceFile : SourceFile , pos : number , program : Program , fixedNodes ?: NodeSet < Node > ) {
25
25
const token = getTokenAtPosition ( sourceFile , pos ) ;
26
-
27
26
const forInitializer = findAncestor ( token , node =>
28
- isForInOrOfStatement ( node . parent ) ? node . parent . initializer === node
29
- : isPossiblyPartOfDestructuring ( node ) ? false : "quit" ) ;
27
+ isForInOrOfStatement ( node . parent ) ? node . parent . initializer === node :
28
+ isPossiblyPartOfDestructuring ( node ) ? false : "quit"
29
+ ) ;
30
30
if ( forInitializer ) return applyChange ( changeTracker , forInitializer , sourceFile , fixedNodes ) ;
31
31
32
32
const parent = token . parent ;
33
- const standaloneInitializer = isExpressionStatement ( parent . parent ) ;
34
- if ( standaloneInitializer ) return applyChange ( changeTracker , parent , sourceFile , fixedNodes ) ;
33
+ if ( isBinaryExpression ( parent ) && isExpressionStatement ( parent . parent ) ) {
34
+ return applyChange ( changeTracker , token , sourceFile , fixedNodes ) ;
35
+ }
35
36
36
- const arrayLiteralInitializer = isArrayLiteralExpression ( token . parent ) ;
37
- if ( arrayLiteralInitializer ) {
38
- const availableIdentifiers : string [ ] = [ ] ; // TODO: where to get/gather this information from?
39
- const noIdentifiersDeclared = parent . forEachChild ( node => availableIdentifiers . indexOf ( node . getFullText ( ) ) < 0 ) ;
40
- if ( ! noIdentifiersDeclared ) return ;
37
+ if ( isArrayLiteralExpression ( parent ) ) {
38
+ const checker = program . getTypeChecker ( ) ;
39
+ if ( ! every ( parent . elements , element => arrayElementCouldBeVariableDeclaration ( element , checker ) ) ) {
40
+ return ;
41
+ }
41
42
42
43
return applyChange ( changeTracker , parent , sourceFile , fixedNodes ) ;
43
44
}
44
45
}
45
46
46
47
function applyChange ( changeTracker : textChanges . ChangeTracker , initializer : Node , sourceFile : SourceFile , fixedNodes ?: NodeSet < Node > ) {
47
48
if ( ! fixedNodes || fixedNodes . tryAdd ( initializer ) ) {
48
- changeTracker . insertNodeBefore ( sourceFile , initializer , createToken ( SyntaxKind . ConstKeyword ) ) ;
49
+ changeTracker . insertModifierBefore ( sourceFile , SyntaxKind . ConstKeyword , initializer ) ;
49
50
}
50
51
}
51
52
@@ -61,4 +62,12 @@ namespace ts.codefix {
61
62
return false ;
62
63
}
63
64
}
65
+
66
+ function arrayElementCouldBeVariableDeclaration ( expression : Expression , checker : TypeChecker ) {
67
+ const identifier =
68
+ isIdentifier ( expression ) ? expression :
69
+ isAssignmentExpression ( expression , /*excludeCompoundAssignment*/ true ) && isIdentifier ( expression . left ) ? expression . left :
70
+ undefined ;
71
+ return ! ! identifier && ! checker . getSymbolAtLocation ( identifier ) ;
72
+ }
64
73
}
0 commit comments