From 0b7f6d5911fae749d9dd8e67a0f86dbc2ba8421e Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 25 Jan 2018 07:42:01 -0800 Subject: [PATCH] Fix bug: Support `this.` completions even when isGlobalCompletion is false (#21330) --- src/services/completions.ts | 5 +++-- tests/cases/fourslash/completionsThisType.ts | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/services/completions.ts b/src/services/completions.ts index 8b7f564ff0872..492aca5b26c7e 100644 --- a/src/services/completions.ts +++ b/src/services/completions.ts @@ -180,7 +180,7 @@ namespace ts.Completions { let insertText: string | undefined; let replacementSpan: TextSpan | undefined; - if (kind === CompletionKind.Global && origin && origin.type === "this-type") { + if (origin && origin.type === "this-type") { insertText = needsConvertPropertyAccess ? `this["${name}"]` : `this.${name}`; } else if (needsConvertPropertyAccess) { @@ -683,6 +683,7 @@ namespace ts.Completions { const enum CompletionKind { ObjectPropertyDeclaration, + /** Note that sometimes we access completions from global scope, but use "None" instead of this. See isGlobalCompletionScope. */ Global, PropertyAccess, MemberLike, @@ -2112,13 +2113,13 @@ namespace ts.Completions { const validIdentiferResult: CompletionEntryDisplayNameForSymbol = { name, needsConvertPropertyAccess: false }; if (isIdentifierText(name, target)) return validIdentiferResult; switch (kind) { - case CompletionKind.None: case CompletionKind.MemberLike: return undefined; case CompletionKind.ObjectPropertyDeclaration: // TODO: GH#18169 return { name: JSON.stringify(name), needsConvertPropertyAccess: false }; case CompletionKind.PropertyAccess: + case CompletionKind.None: case CompletionKind.Global: // Don't add a completion for a name starting with a space. See https://github.com/Microsoft/TypeScript/pull/20547 return name.charCodeAt(0) === CharacterCodes.space ? undefined : { name, needsConvertPropertyAccess: true }; diff --git a/tests/cases/fourslash/completionsThisType.ts b/tests/cases/fourslash/completionsThisType.ts index 58325182a22d1..471251d2134d1 100644 --- a/tests/cases/fourslash/completionsThisType.ts +++ b/tests/cases/fourslash/completionsThisType.ts @@ -3,7 +3,7 @@ ////class C { //// "foo bar": number; //// xyz() { -//// /**/ +//// return (/**/) //// } ////} //// @@ -11,7 +11,7 @@ goTo.marker(""); -verify.completionListContains("xyz", "(method) C.xyz(): void", "", "method", undefined, undefined, { +verify.completionListContains("xyz", "(method) C.xyz(): any", "", "method", undefined, undefined, { includeInsertTextCompletions: true, insertText: "this.xyz", });