Skip to content

Commit 2d2c4a9

Browse files
committed
Merge pull request #853 from Microsoft/objectLiteralCompletion
Fix crash when getting member completion for an object literal
2 parents e5b6bfb + 29c5b40 commit 2d2c4a9

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

src/compiler/checker.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4508,8 +4508,21 @@ module ts {
45084508
}
45094509
else {
45104510
error(node, Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target);
4511-
return resolveErrorCall(node);
45124511
}
4512+
4513+
// No signature was applicable. We have already reported the errors for the invalid signature.
4514+
// If this is a type resolution session, e.g. Language Service, try to get better information that anySignature.
4515+
// Pick the first candidate that matches the arity. This way we can get a contextual type for cases like:
4516+
// declare function f(a: { xa: number; xb: number; });
4517+
// f({ |
4518+
if (!fullTypeCheck) {
4519+
for (var i = 0, n = candidates.length; i < n; i++) {
4520+
if (signatureHasCorrectArity(node, candidates[i])) {
4521+
return candidates[i];
4522+
}
4523+
}
4524+
}
4525+
45134526
return resolveErrorCall(node);
45144527

45154528
// The candidate list orders groups in reverse, but within a group signatures are kept in declaration order

src/compiler/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,8 @@ module ts {
12041204
}
12051205

12061206
public static toString(parts: SymbolDisplayPart[]) {
1207-
return parts.map(p => p.text).join("");
1207+
var result = map(parts, p => p.text);
1208+
return result ? result.join("") : "";
12081209
}
12091210
}
12101211

src/services/services.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,9 +2162,16 @@ module ts {
21622162
}
21632163

21642164
// TODO: this is a hack for now, we need a proper walking mechanism to verify that we have the correct node
2165-
var mappedNode = getTouchingToken(sourceFile, TypeScript.end(node) - 1);
2166-
if (isPunctuation(mappedNode.kind)) {
2167-
mappedNode = mappedNode.parent;
2165+
var precedingToken = findTokenOnLeftOfPosition(sourceFile, TypeScript.end(node));
2166+
var mappedNode: Node;
2167+
if (!precedingToken) {
2168+
mappedNode = sourceFile;
2169+
}
2170+
else if (isPunctuation(precedingToken.kind)) {
2171+
mappedNode = precedingToken.parent;
2172+
}
2173+
else {
2174+
mappedNode = precedingToken;
21682175
}
21692176

21702177
Debug.assert(mappedNode, "Could not map a Fidelity node to an AST node");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////function f(a: { xa: number; xb: number; }) { }
4+
////var xc;
5+
////f({
6+
//// /**/
7+
8+
goTo.marker()
9+
verify.memberListContains('xa');
10+
verify.memberListContains('xb');
11+
verify.memberListCount(2);

0 commit comments

Comments
 (0)