From 6e74f7ee11ae47fb2481dd07b998bc523aec75a5 Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Wed, 6 Dec 2017 15:58:50 -0800 Subject: [PATCH 1/6] Fix #20523 --- src/services/codefixes/inferFromUsage.ts | 5 +++++ tests/cases/fourslash/incompleteFunctionCallCodefix.ts | 9 +++++++++ 2 files changed, 14 insertions(+) create mode 100644 tests/cases/fourslash/incompleteFunctionCallCodefix.ts diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 9782619d41d..d7bfd1b621f 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -148,6 +148,11 @@ namespace ts.codefix { containingFunction.parameters.map(p => isIdentifier(p.name) ? inferTypeForVariableFromUsage(p.name, sourceFile, program, cancellationToken) : undefined); if (!types) return undefined; + // We didn't actually find a set of type inference positions matching each parameter position + 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; diff --git a/tests/cases/fourslash/incompleteFunctionCallCodefix.ts b/tests/cases/fourslash/incompleteFunctionCallCodefix.ts new file mode 100644 index 00000000000..007eaa20f7f --- /dev/null +++ b/tests/cases/fourslash/incompleteFunctionCallCodefix.ts @@ -0,0 +1,9 @@ +/// + +// @noImplicitAny: true +//// function f(/*1*/x) { +//// } +//// f( + +verify.not.codeFixAvailable([]); + From 2cf7295c4ee1a1864742287138416c502ec84f55 Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Wed, 6 Dec 2017 16:21:18 -0800 Subject: [PATCH 2/6] Fix #20520 --- src/services/codefixes/inferFromUsage.ts | 9 +++++++-- tests/cases/fourslash/incompleteFunctionCallCodefix2.ts | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/incompleteFunctionCallCodefix2.ts diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index d7bfd1b621f..25236318612 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -196,8 +196,9 @@ namespace ts.codefix { sourceFile, token.getStart(sourceFile)); - Debug.assert(!!references, "Found no references!"); - Debug.assert(references.length === 1, "Found more references than expected"); + if (!references || references.length !== 1) { + return []; + } return references[0].references.map(r => getTokenAtPosition(program.getSourceFile(r.fileName), r.textSpan.start, /*includeJsDocComment*/ false)); } @@ -286,6 +287,10 @@ namespace ts.codefix { } export function inferTypeForParametersFromReferences(references: Identifier[], declaration: FunctionLikeDeclaration, checker: TypeChecker, cancellationToken: CancellationToken): (Type | undefined)[] | undefined { + if (references.length === 0) { + return undefined; + } + if (declaration.parameters) { const usageContext: UsageContext = {}; for (const reference of references) { diff --git a/tests/cases/fourslash/incompleteFunctionCallCodefix2.ts b/tests/cases/fourslash/incompleteFunctionCallCodefix2.ts new file mode 100644 index 00000000000..c1bc731541f --- /dev/null +++ b/tests/cases/fourslash/incompleteFunctionCallCodefix2.ts @@ -0,0 +1,7 @@ +/// + +// @noImplicitAny: true +//// function f(new C(100, 3, undefined) + +verify.not.codeFixAvailable([]); + From b7b43fe601babb77e55d5b6b0d94231e39ed0f12 Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Thu, 7 Dec 2017 12:16:29 -0800 Subject: [PATCH 3/6] Fixes #20527 --- src/services/codefixes/inferFromUsage.ts | 4 ++++ tests/cases/fourslash/incompleteFunctionCallCodefix3.ts | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 tests/cases/fourslash/incompleteFunctionCallCodefix3.ts diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 25236318612..992770dbd01 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -71,6 +71,10 @@ namespace ts.codefix { } 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: diff --git a/tests/cases/fourslash/incompleteFunctionCallCodefix3.ts b/tests/cases/fourslash/incompleteFunctionCallCodefix3.ts new file mode 100644 index 00000000000..3c0261e48a7 --- /dev/null +++ b/tests/cases/fourslash/incompleteFunctionCallCodefix3.ts @@ -0,0 +1,7 @@ +/// + +// @noImplicitAny: true +//// function ...q) {}} f(10); + +verify.not.codeFixAvailable([]); + From 5c99c67b4bd7b15f86db75909929b79d433e4b71 Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Thu, 7 Dec 2017 12:16:53 -0800 Subject: [PATCH 4/6] Fixes #20542 --- src/compiler/checker.ts | 7 ++++++- tests/cases/fourslash/typeToStringCrashInCodeFix.ts | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/typeToStringCrashInCodeFix.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 269c55145ec..5b481ef4721 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3951,7 +3951,12 @@ namespace ts { writePunctuation(writer, SyntaxKind.CloseBracketToken); writePunctuation(writer, SyntaxKind.ColonToken); writeSpace(writer); - buildTypeDisplay(info.type, writer, enclosingDeclaration, globalFlags, symbolStack); + if (info.type) { + buildTypeDisplay(info.type, writer, enclosingDeclaration, globalFlags, symbolStack); + } + else { + writeKeyword(writer, SyntaxKind.AnyKeyword); + } writePunctuation(writer, SyntaxKind.SemicolonToken); writer.writeLine(); } diff --git a/tests/cases/fourslash/typeToStringCrashInCodeFix.ts b/tests/cases/fourslash/typeToStringCrashInCodeFix.ts new file mode 100644 index 00000000000..05cf00cb4ce --- /dev/null +++ b/tests/cases/fourslash/typeToStringCrashInCodeFix.ts @@ -0,0 +1,6 @@ +/// + +// @noImplicitAny: true +//// function f(y, z = { p: y[ + +verify.getAndApplyCodeFix(); From fa988eacbdf9f17e6ef415db929efa2be650210f Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Thu, 7 Dec 2017 12:33:40 -0800 Subject: [PATCH 5/6] Fixes #20475 (no repro found yet) --- src/services/symbolDisplay.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/symbolDisplay.ts b/src/services/symbolDisplay.ts index fe04342b8f7..caf38306bec 100644 --- a/src/services/symbolDisplay.ts +++ b/src/services/symbolDisplay.ts @@ -496,7 +496,7 @@ namespace ts.SymbolDisplay { addNewLineIfDisplayPartsExist(); if (symbolKind) { pushTypePart(symbolKind); - if (!some(symbol.declarations, d => isArrowFunction(d) || (isFunctionExpression(d) || isClassExpression(d)) && !d.name)) { + if (symbol && !some(symbol.declarations, d => isArrowFunction(d) || (isFunctionExpression(d) || isClassExpression(d)) && !d.name)) { displayParts.push(spacePart()); addFullSymbolName(symbol); } From 92c3b23a32344037828e407bfc82818136c7d6bc Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Thu, 7 Dec 2017 14:27:46 -0800 Subject: [PATCH 6/6] 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;