Skip to content

Commit

Permalink
fix(49566): Implicit this.property completions not returned while wri…
Browse files Browse the repository at this point in the history
…ting property (#49574)

* fix(49566): show this.prop completions in class scoped property declaration

* remove duplicate default value
  • Loading branch information
a-tarasyuk committed Jun 16, 2022
1 parent 44d6b51 commit f83ce9b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,9 +672,9 @@ namespace ts {
return moduleSpecifier && resolveExternalModuleName(moduleSpecifier, moduleSpecifier, /*ignoreErrors*/ true);
},
resolveExternalModuleSymbol,
tryGetThisTypeAt: (nodeIn, includeGlobalThis) => {
tryGetThisTypeAt: (nodeIn, includeGlobalThis, container) => {
const node = getParseTreeNode(nodeIn);
return node && tryGetThisTypeAt(node, includeGlobalThis);
return node && tryGetThisTypeAt(node, includeGlobalThis, container);
},
getTypeArgumentConstraint: nodeIn => {
const node = getParseTreeNode(nodeIn, isTypeNode);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4597,7 +4597,7 @@ namespace ts {
*/
/* @internal */ resolveExternalModuleSymbol(symbol: Symbol): Symbol;
/** @param node A location where we might consider accessing `this`. Not necessarily a ThisExpression. */
/* @internal */ tryGetThisTypeAt(node: Node, includeGlobalThis?: boolean): Type | undefined;
/* @internal */ tryGetThisTypeAt(node: Node, includeGlobalThis?: boolean, container?: Node): Type | undefined;
/* @internal */ getTypeArgumentConstraint(node: TypeNode): Type | undefined;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2616,7 +2616,7 @@ namespace ts.Completions {

// Need to insert 'this.' before properties of `this` type, so only do that if `includeInsertTextCompletions`
if (preferences.includeCompletionsWithInsertText && scopeNode.kind !== SyntaxKind.SourceFile) {
const thisType = typeChecker.tryGetThisTypeAt(scopeNode, /*includeGlobalThis*/ false);
const thisType = typeChecker.tryGetThisTypeAt(scopeNode, /*includeGlobalThis*/ false, isClassLike(scopeNode.parent) ? scopeNode : undefined);
if (thisType && !isProbablyGlobalType(thisType, sourceFile, typeChecker)) {
for (const symbol of getPropertiesForCompletion(thisType, typeChecker)) {
symbolToOriginInfoMap[symbols.length] = { kind: SymbolOriginInfoKind.ThisType };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// <reference path="fourslash.ts" />

////class Foo {
//// private _prop = 1;
//// public a = [|_/*1*/|]
////
//// foo() {
//// [|_/*2*/|]
//// }
////}

verify.completions({
marker: ["1", "2"],
includes: [
{
name: "_prop",
insertText: "this._prop",
kind: "property",
sortText: completion.SortText.SuggestedClassMembers,
source: completion.CompletionSource.ThisProperty,
text: "(property) Foo._prop: number",
kindModifiers: "private"
},
],
preferences: {
includeInsertTextCompletions: true
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// <reference path="fourslash.ts" />

////class Foo {
//// private static _prop = 1;
//// public static a = [|_/*1*/|]
////
//// static foo() {
//// [|_/*2*/|]
//// }
////}

verify.completions({
marker: ["2"],
includes: [
{
name: "_prop",
insertText: "this._prop",
kind: "property",
sortText: completion.SortText.SuggestedClassMembers,
source: completion.CompletionSource.ThisProperty,
text: "(property) Foo._prop: number",
kindModifiers: "private,static"
},
],
preferences: {
includeInsertTextCompletions: true
}
});

0 comments on commit f83ce9b

Please sign in to comment.