@@ -25,24 +25,24 @@ namespace ts.codefix {
25
25
registerCodeFix ( {
26
26
errorCodes,
27
27
getCodeActions ( context ) {
28
- const { sourceFile, program, span : { start } , errorCode, cancellationToken } = context ;
28
+ const { sourceFile, program, span : { start } , errorCode, cancellationToken, host } = context ;
29
29
if ( isSourceFileJS ( sourceFile ) ) {
30
30
return undefined ; // TODO: GH#20113
31
31
}
32
32
33
33
const token = getTokenAtPosition ( sourceFile , start ) ;
34
34
let declaration ! : Declaration | undefined ;
35
- const changes = textChanges . ChangeTracker . with ( context , changes => { declaration = doChange ( changes , sourceFile , token , errorCode , program , cancellationToken , /*markSeenseen */ returnTrue ) ; } ) ;
35
+ const changes = textChanges . ChangeTracker . with ( context , changes => { declaration = doChange ( changes , sourceFile , token , errorCode , program , cancellationToken , /*markSeen */ returnTrue , host ) ; } ) ;
36
36
const name = declaration && getNameOfDeclaration ( declaration ) ;
37
37
return ! name || changes . length === 0 ? undefined
38
38
: [ createCodeFixAction ( fixId , changes , [ getDiagnostic ( errorCode , token ) , name . getText ( sourceFile ) ] , fixId , Diagnostics . Infer_all_types_from_usage ) ] ;
39
39
} ,
40
40
fixIds : [ fixId ] ,
41
41
getAllCodeActions ( context ) {
42
- const { sourceFile, program, cancellationToken } = context ;
42
+ const { sourceFile, program, cancellationToken, host } = context ;
43
43
const markSeen = nodeSeenTracker ( ) ;
44
44
return codeFixAll ( context , errorCodes , ( changes , err ) => {
45
- doChange ( changes , sourceFile , getTokenAtPosition ( err . file , err . start ) , err . code , program , cancellationToken , markSeen ) ;
45
+ doChange ( changes , sourceFile , getTokenAtPosition ( err . file , err . start ) , err . code , program , cancellationToken , markSeen , host ) ;
46
46
} ) ;
47
47
} ,
48
48
} ) ;
@@ -58,7 +58,7 @@ namespace ts.codefix {
58
58
}
59
59
}
60
60
61
- function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , token : Node , errorCode : number , program : Program , cancellationToken : CancellationToken , markSeen : NodeSeenTracker ) : Declaration | undefined {
61
+ function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , token : Node , errorCode : number , program : Program , cancellationToken : CancellationToken , markSeen : NodeSeenTracker , host : LanguageServiceHost ) : Declaration | undefined {
62
62
if ( ! isParameterPropertyModifier ( token . kind ) && token . kind !== SyntaxKind . Identifier && token . kind !== SyntaxKind . DotDotDotToken ) {
63
63
return undefined ;
64
64
}
@@ -69,15 +69,15 @@ namespace ts.codefix {
69
69
case Diagnostics . Member_0_implicitly_has_an_1_type . code :
70
70
case Diagnostics . Variable_0_implicitly_has_type_1_in_some_locations_where_its_type_cannot_be_determined . code :
71
71
if ( ( isVariableDeclaration ( parent ) && markSeen ( parent ) ) || isPropertyDeclaration ( parent ) || isPropertySignature ( parent ) ) { // handle bad location
72
- annotateVariableDeclaration ( changes , sourceFile , parent , program , cancellationToken ) ;
72
+ annotateVariableDeclaration ( changes , sourceFile , parent , program , host , cancellationToken ) ;
73
73
return parent ;
74
74
}
75
75
return undefined ;
76
76
77
77
case Diagnostics . Variable_0_implicitly_has_an_1_type . code : {
78
78
const symbol = program . getTypeChecker ( ) . getSymbolAtLocation ( token ) ;
79
79
if ( symbol && symbol . valueDeclaration && isVariableDeclaration ( symbol . valueDeclaration ) && markSeen ( symbol . valueDeclaration ) ) {
80
- annotateVariableDeclaration ( changes , sourceFile , symbol . valueDeclaration , program , cancellationToken ) ;
80
+ annotateVariableDeclaration ( changes , sourceFile , symbol . valueDeclaration , program , host , cancellationToken ) ;
81
81
return symbol . valueDeclaration ;
82
82
}
83
83
return undefined ;
@@ -93,14 +93,14 @@ namespace ts.codefix {
93
93
// Parameter declarations
94
94
case Diagnostics . Parameter_0_implicitly_has_an_1_type . code :
95
95
if ( isSetAccessor ( containingFunction ) ) {
96
- annotateSetAccessor ( changes , sourceFile , containingFunction , program , cancellationToken ) ;
96
+ annotateSetAccessor ( changes , sourceFile , containingFunction , program , host , cancellationToken ) ;
97
97
return containingFunction ;
98
98
}
99
99
// falls through
100
100
case Diagnostics . Rest_parameter_0_implicitly_has_an_any_type . code :
101
101
if ( markSeen ( containingFunction ) ) {
102
102
const param = cast ( parent , isParameter ) ;
103
- annotateParameters ( changes , param , containingFunction , sourceFile , program , cancellationToken ) ;
103
+ annotateParameters ( changes , param , containingFunction , sourceFile , program , host , cancellationToken ) ;
104
104
return param ;
105
105
}
106
106
return undefined ;
@@ -109,15 +109,15 @@ namespace ts.codefix {
109
109
case Diagnostics . Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation . code :
110
110
case Diagnostics . _0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type . code :
111
111
if ( isGetAccessor ( containingFunction ) && isIdentifier ( containingFunction . name ) ) {
112
- annotate ( changes , sourceFile , containingFunction , inferTypeForVariableFromUsage ( containingFunction . name , program , cancellationToken ) , program ) ;
112
+ annotate ( changes , sourceFile , containingFunction , inferTypeForVariableFromUsage ( containingFunction . name , program , cancellationToken ) , program , host ) ;
113
113
return containingFunction ;
114
114
}
115
115
return undefined ;
116
116
117
117
// Set Accessor declarations
118
118
case Diagnostics . Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation . code :
119
119
if ( isSetAccessor ( containingFunction ) ) {
120
- annotateSetAccessor ( changes , sourceFile , containingFunction , program , cancellationToken ) ;
120
+ annotateSetAccessor ( changes , sourceFile , containingFunction , program , host , cancellationToken ) ;
121
121
return containingFunction ;
122
122
}
123
123
return undefined ;
@@ -127,9 +127,9 @@ namespace ts.codefix {
127
127
}
128
128
}
129
129
130
- function annotateVariableDeclaration ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , declaration : VariableDeclaration | PropertyDeclaration | PropertySignature , program : Program , cancellationToken : CancellationToken ) : void {
130
+ function annotateVariableDeclaration ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , declaration : VariableDeclaration | PropertyDeclaration | PropertySignature , program : Program , host : LanguageServiceHost , cancellationToken : CancellationToken ) : void {
131
131
if ( isIdentifier ( declaration . name ) ) {
132
- annotate ( changes , sourceFile , declaration , inferTypeForVariableFromUsage ( declaration . name , program , cancellationToken ) , program ) ;
132
+ annotate ( changes , sourceFile , declaration , inferTypeForVariableFromUsage ( declaration . name , program , cancellationToken ) , program , host ) ;
133
133
}
134
134
}
135
135
@@ -145,7 +145,7 @@ namespace ts.codefix {
145
145
return false ;
146
146
}
147
147
148
- function annotateParameters ( changes : textChanges . ChangeTracker , parameterDeclaration : ParameterDeclaration , containingFunction : FunctionLike , sourceFile : SourceFile , program : Program , cancellationToken : CancellationToken ) : void {
148
+ function annotateParameters ( changes : textChanges . ChangeTracker , parameterDeclaration : ParameterDeclaration , containingFunction : FunctionLike , sourceFile : SourceFile , program : Program , host : LanguageServiceHost , cancellationToken : CancellationToken ) : void {
149
149
if ( ! isIdentifier ( parameterDeclaration . name ) || ! isApplicableFunctionForInference ( containingFunction ) ) {
150
150
return ;
151
151
}
@@ -159,26 +159,27 @@ namespace ts.codefix {
159
159
160
160
zipWith ( containingFunction . parameters , types , ( parameter , type ) => {
161
161
if ( ! parameter . type && ! parameter . initializer ) {
162
- annotate ( changes , sourceFile , parameter , type , program ) ;
162
+ annotate ( changes , sourceFile , parameter , type , program , host ) ;
163
163
}
164
164
} ) ;
165
165
}
166
166
167
- function annotateSetAccessor ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , setAccessorDeclaration : SetAccessorDeclaration , program : Program , cancellationToken : CancellationToken ) : void {
167
+ function annotateSetAccessor ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , setAccessorDeclaration : SetAccessorDeclaration , program : Program , host : LanguageServiceHost , cancellationToken : CancellationToken ) : void {
168
168
const param = firstOrUndefined ( setAccessorDeclaration . parameters ) ;
169
169
if ( param && isIdentifier ( setAccessorDeclaration . name ) && isIdentifier ( param . name ) ) {
170
170
const type = inferTypeForVariableFromUsage ( setAccessorDeclaration . name , program , cancellationToken ) ||
171
171
inferTypeForVariableFromUsage ( param . name , program , cancellationToken ) ;
172
- annotate ( changes , sourceFile , param , type , program ) ;
172
+ annotate ( changes , sourceFile , param , type , program , host ) ;
173
173
}
174
174
}
175
175
176
- function annotate ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , declaration : textChanges . TypeAnnotatable , type : Type | undefined , program : Program ) : void {
177
- const typeNode = type && getTypeNodeIfAccessible ( type , declaration , program . getTypeChecker ( ) ) ;
176
+ function annotate ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , declaration : textChanges . TypeAnnotatable , type : Type | undefined , program : Program , host : LanguageServiceHost ) : void {
177
+ const typeNode = type && getTypeNodeIfAccessible ( type , declaration , program , host ) ;
178
178
if ( typeNode ) changes . tryInsertTypeAnnotation ( sourceFile , declaration , typeNode ) ;
179
179
}
180
180
181
- function getTypeNodeIfAccessible ( type : Type , enclosingScope : Node , checker : TypeChecker ) : TypeNode | undefined {
181
+ function getTypeNodeIfAccessible ( type : Type , enclosingScope : Node , program : Program , host : LanguageServiceHost ) : TypeNode | undefined {
182
+ const checker = program . getTypeChecker ( ) ;
182
183
let typeIsAccessible = true ;
183
184
const notAccessible = ( ) => { typeIsAccessible = false ; } ;
184
185
const res = checker . typeToTypeNode ( type , enclosingScope , /*flags*/ undefined , {
@@ -189,6 +190,14 @@ namespace ts.codefix {
189
190
reportInaccessibleThisError : notAccessible ,
190
191
reportPrivateInBaseOfClassExpression : notAccessible ,
191
192
reportInaccessibleUniqueSymbolError : notAccessible ,
193
+ moduleResolverHost : {
194
+ readFile : host . readFile ,
195
+ fileExists : host . fileExists ,
196
+ directoryExists : host . directoryExists ,
197
+ getSourceFiles : program . getSourceFiles ,
198
+ getCurrentDirectory : program . getCurrentDirectory ,
199
+ getCommonSourceDirectory : program . getCommonSourceDirectory ,
200
+ }
192
201
} ) ;
193
202
return typeIsAccessible ? res : undefined ;
194
203
}
0 commit comments