Skip to content

Commit

Permalink
Fix bug: Support this. completions even when isGlobalCompletion is …
Browse files Browse the repository at this point in the history
…false (#21330) (#21407)
  • Loading branch information
Andy authored Jan 25, 2018
1 parent 3cffaa4 commit cac6052
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 };
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/fourslash/completionsThisType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
////class C {
//// "foo bar": number;
//// xyz() {
//// /**/
//// return (/**/)
//// }
////}
////
////function f(this: { x: number }) { /*f*/ }

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",
});
Expand Down

0 comments on commit cac6052

Please sign in to comment.