@@ -70,7 +70,6 @@ namespace ts.codefix {
7070 return undefined ;
7171 }
7272
73- const containingFunction = getContainingFunction ( token ) ;
7473 switch ( errorCode ) {
7574 // Variable and Property declarations
7675 case Diagnostics . Member_0_implicitly_has_an_1_type . code :
@@ -81,6 +80,13 @@ namespace ts.codefix {
8180 const symbol = program . getTypeChecker ( ) . getSymbolAtLocation ( token ) ;
8281 return symbol && symbol . valueDeclaration && getCodeActionForVariableDeclaration ( < VariableDeclaration > symbol . valueDeclaration , sourceFile , program , cancellationToken ) ;
8382 }
83+ }
84+
85+ const containingFunction = getContainingFunction ( token ) ;
86+ if ( containingFunction === undefined ) {
87+ return undefined ;
88+ }
89+ switch ( errorCode ) {
8490
8591 // Parameter declarations
8692 case Diagnostics . Parameter_0_implicitly_has_an_1_type . code :
@@ -148,6 +154,11 @@ namespace ts.codefix {
148154 containingFunction . parameters . map ( p => isIdentifier ( p . name ) ? inferTypeForVariableFromUsage ( p . name , sourceFile , program , cancellationToken ) : undefined ) ;
149155 if ( ! types ) return undefined ;
150156
157+ // We didn't actually find a set of type inference positions matching each parameter position
158+ if ( containingFunction . parameters . length !== types . length ) {
159+ return undefined ;
160+ }
161+
151162 const textChanges = arrayFrom ( mapDefinedIterator ( zipToIterator ( containingFunction . parameters , types ) , ( [ parameter , type ] ) =>
152163 type && ! parameter . type && ! parameter . initializer ? makeChange ( containingFunction , parameter . end , type , program ) : undefined ) ) ;
153164 return textChanges . length ? { declaration : parameterDeclaration , textChanges } : undefined ;
@@ -191,8 +202,9 @@ namespace ts.codefix {
191202 sourceFile ,
192203 token . getStart ( sourceFile ) ) ;
193204
194- Debug . assert ( ! ! references , "Found no references!" ) ;
195- Debug . assert ( references . length === 1 , "Found more references than expected" ) ;
205+ if ( ! references || references . length !== 1 ) {
206+ return [ ] ;
207+ }
196208
197209 return references [ 0 ] . references . map ( r => < Identifier > getTokenAtPosition ( program . getSourceFile ( r . fileName ) , r . textSpan . start , /*includeJsDocComment*/ false ) ) ;
198210 }
@@ -281,6 +293,10 @@ namespace ts.codefix {
281293 }
282294
283295 export function inferTypeForParametersFromReferences ( references : Identifier [ ] , declaration : FunctionLikeDeclaration , checker : TypeChecker , cancellationToken : CancellationToken ) : ( Type | undefined ) [ ] | undefined {
296+ if ( references . length === 0 ) {
297+ return undefined ;
298+ }
299+
284300 if ( declaration . parameters ) {
285301 const usageContext : UsageContext = { } ;
286302 for ( const reference of references ) {
0 commit comments