From 98f8ec5d5a3bdf4e59bf87686125f14519314657 Mon Sep 17 00:00:00 2001 From: Jason Freeman Date: Tue, 7 Oct 2014 15:36:55 -0700 Subject: [PATCH 1/3] Fix crash when getting member completion for an object literal --- src/services/services.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index e4d28f4700f..b4522849e2a 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2144,9 +2144,16 @@ module ts { } // TODO: this is a hack for now, we need a proper walking mechanism to verify that we have the correct node - var mappedNode = getTouchingToken(sourceFile, TypeScript.end(node) - 1); - if (isPunctuation(mappedNode.kind)) { - mappedNode = mappedNode.parent; + var precedingToken = findTokenOnLeftOfPosition(sourceFile, TypeScript.end(node)); + var mappedNode: Node; + if (!precedingToken) { + mappedNode = sourceFile; + } + else if (isPunctuation(precedingToken.kind)) { + mappedNode = precedingToken.parent; + } + else { + mappedNode = precedingToken; } Debug.assert(mappedNode, "Could not map a Fidelity node to an AST node"); From a8a1fa27d85440f5dce5648275472a0dd0ab5386 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Wed, 8 Oct 2014 15:47:30 -0700 Subject: [PATCH 2/3] Try to pick a good signature, if no signature matches --- src/compiler/checker.ts | 17 ++++++++++++++++- src/compiler/types.ts | 3 ++- ...bjectLiteralThatIsParameterOfFunctionCall.ts | 11 +++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/completionListInObjectLiteralThatIsParameterOfFunctionCall.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 33d4008885d..4a05021ce15 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4508,8 +4508,23 @@ module ts { } else { error(node, Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); - return resolveErrorCall(node); } + + // No signature was applicable. We have already reported the errors for the invalid signature. + // If this is a type resolution session, e.g. Language Service, try to get better information that anySignature. + // Pick the first candidate that matches the arity. This way we can get a contextual type for cases like: + // declare function f(a: { xa: number; xb: number; }); + // f({ | + if (!fullTypeCheck) { + if (candidates.length) { + for (var i = 0; i < candidates.length; i++) { + if (signatureHasCorrectArity(node, candidates[i])) { + return candidates[i]; + } + } + } + } + return resolveErrorCall(node); // The candidate list orders groups in reverse, but within a group signatures are kept in declaration order diff --git a/src/compiler/types.ts b/src/compiler/types.ts index da2cb9099c2..ee6ad334b3d 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1204,7 +1204,8 @@ module ts { } public static toString(parts: SymbolDisplayPart[]) { - return parts.map(p => p.text).join(""); + var result = map(parts, p => p.text); + return result ? result.join("") : ""; } } diff --git a/tests/cases/fourslash/completionListInObjectLiteralThatIsParameterOfFunctionCall.ts b/tests/cases/fourslash/completionListInObjectLiteralThatIsParameterOfFunctionCall.ts new file mode 100644 index 00000000000..9dcf7456226 --- /dev/null +++ b/tests/cases/fourslash/completionListInObjectLiteralThatIsParameterOfFunctionCall.ts @@ -0,0 +1,11 @@ +/// + +////function f(a: { xa: number; xb: number; }) { } +////var xc; +////f({ +//// /**/ + +goTo.marker() +verify.memberListContains('xa'); +verify.memberListContains('xb'); +verify.memberListCount(2); \ No newline at end of file From 2e9a20f8ce59289a5603385bc71538a2b09613d1 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Wed, 8 Oct 2014 17:29:14 -0700 Subject: [PATCH 3/3] Respond to code review comments --- src/compiler/checker.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4a05021ce15..68011b586f2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4516,11 +4516,9 @@ module ts { // declare function f(a: { xa: number; xb: number; }); // f({ | if (!fullTypeCheck) { - if (candidates.length) { - for (var i = 0; i < candidates.length; i++) { - if (signatureHasCorrectArity(node, candidates[i])) { - return candidates[i]; - } + for (var i = 0, n = candidates.length; i < n; i++) { + if (signatureHasCorrectArity(node, candidates[i])) { + return candidates[i]; } } }