Navigation Menu

Skip to content

Commit

Permalink
fix(51170): Completing an unimplemented property overwrites rest of l…
Browse files Browse the repository at this point in the history
…ine (#51175)

* fix(51170): skip insertText for class members with existing initializer

* skip insertText for class members with existing tokens
  • Loading branch information
a-tarasyuk committed Oct 17, 2022
1 parent a1d82fc commit 7406ee9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/services/completions.ts
Expand Up @@ -764,7 +764,7 @@ namespace ts.Completions {
if (preferences.includeCompletionsWithClassMemberSnippets &&
preferences.includeCompletionsWithInsertText &&
completionKind === CompletionKind.MemberLike &&
isClassLikeMemberCompletion(symbol, location)) {
isClassLikeMemberCompletion(symbol, location, sourceFile)) {
let importAdder;
({ insertText, isSnippet, importAdder, replacementSpan } = getEntryForMemberCompletion(host, program, options, preferences, name, symbol, location, contextToken, formatContext));
sortText = SortText.ClassMemberSnippets; // sortText has to be lower priority than the sortText for keywords. See #47852.
Expand Down Expand Up @@ -846,7 +846,7 @@ namespace ts.Completions {
};
}

function isClassLikeMemberCompletion(symbol: Symbol, location: Node): boolean {
function isClassLikeMemberCompletion(symbol: Symbol, location: Node, sourceFile: SourceFile): boolean {
// TODO: support JS files.
if (isInJSFile(location)) {
return false;
Expand Down Expand Up @@ -884,6 +884,7 @@ namespace ts.Completions {
location.parent.parent &&
isClassElement(location.parent) &&
location === location.parent.name &&
location.parent.getLastToken(sourceFile) === location.parent.name &&
isClassLike(location.parent.parent)
) ||
(
Expand Down
21 changes: 21 additions & 0 deletions tests/cases/fourslash/completionsOverridingProperties2.ts
@@ -0,0 +1,21 @@
/// <reference path="fourslash.ts" />

////interface I {
//// prop: string;
////}
////class C implements I {
//// public pr/**/: string = 'foo';
////}

verify.completions({
marker: "",
includes: [
{ name: "prop", isSnippet: undefined, insertText: undefined }
],
isNewIdentifierLocation: true,
preferences: {
includeCompletionsWithInsertText: true,
includeCompletionsWithSnippetText: true,
includeCompletionsWithClassMemberSnippets: true,
}
});
21 changes: 21 additions & 0 deletions tests/cases/fourslash/completionsOverridingProperties3.ts
@@ -0,0 +1,21 @@
/// <reference path="fourslash.ts" />

////interface I {
//// prop: string;
////}
////class C implements I {
//// public pr/**/: string | number;
////}

verify.completions({
marker: "",
includes: [
{ name: "prop", isSnippet: undefined, insertText: undefined }
],
isNewIdentifierLocation: true,
preferences: {
includeCompletionsWithInsertText: true,
includeCompletionsWithSnippetText: true,
includeCompletionsWithClassMemberSnippets: true,
}
});

0 comments on commit 7406ee9

Please sign in to comment.