From 92c3b23a32344037828e407bfc82818136c7d6bc Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Thu, 7 Dec 2017 14:27:46 -0800 Subject: [PATCH] Bail at the correct point when containingFunction is undefined --- src/services/codefixes/inferFromUsage.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 992770dbd01..e5177991b34 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -70,11 +70,6 @@ namespace ts.codefix { return undefined; } - const containingFunction = getContainingFunction(token); - if (containingFunction === undefined) { - // Possible in certain syntax error cases - return undefined; - } switch (errorCode) { // Variable and Property declarations case Diagnostics.Member_0_implicitly_has_an_1_type.code: @@ -85,6 +80,13 @@ namespace ts.codefix { const symbol = program.getTypeChecker().getSymbolAtLocation(token); return symbol && symbol.valueDeclaration && getCodeActionForVariableDeclaration(symbol.valueDeclaration, sourceFile, program, cancellationToken); } + } + + const containingFunction = getContainingFunction(token); + if (containingFunction === undefined) { + return undefined; + } + switch (errorCode) { // Parameter declarations case Diagnostics.Parameter_0_implicitly_has_an_1_type.code: @@ -156,7 +158,7 @@ namespace ts.codefix { if (containingFunction.parameters.length !== types.length) { return undefined; } - + const textChanges = arrayFrom(mapDefinedIterator(zipToIterator(containingFunction.parameters, types), ([parameter, type]) => type && !parameter.type && !parameter.initializer ? makeChange(containingFunction, parameter.end, type, program) : undefined)); return textChanges.length ? { declaration: parameterDeclaration, textChanges } : undefined;