Skip to content

Commit bd600cf

Browse files
author
Andy
authored
Fix duplicate "this" completion (microsoft#25900)
1 parent d60f498 commit bd600cf

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4670,7 +4670,7 @@ namespace ts {
46704670
}
46714671
}
46724672
// Use contextual parameter type if one is available
4673-
const type = declaration.symbol.escapedName === "this" ? getContextualThisParameterType(func) : getContextuallyTypedParameterType(declaration);
4673+
const type = declaration.symbol.escapedName === InternalSymbolName.This ? getContextualThisParameterType(func) : getContextuallyTypedParameterType(declaration);
46744674
if (type) {
46754675
return addOptionality(type, isOptional);
46764676
}
@@ -7488,7 +7488,7 @@ namespace ts {
74887488
const resolvedSymbol = resolveName(param, paramSymbol.escapedName, SymbolFlags.Value, undefined, undefined, /*isUse*/ false);
74897489
paramSymbol = resolvedSymbol!;
74907490
}
7491-
if (i === 0 && paramSymbol.escapedName === "this") {
7491+
if (i === 0 && paramSymbol.escapedName === InternalSymbolName.This) {
74927492
hasThisParameter = true;
74937493
thisParameter = param.symbol;
74947494
}
@@ -15373,7 +15373,7 @@ namespace ts {
1537315373
const jsDocFunctionType = <JSDocFunctionType>jsdocType;
1537415374
if (jsDocFunctionType.parameters.length > 0 &&
1537515375
jsDocFunctionType.parameters[0].name &&
15376-
(jsDocFunctionType.parameters[0].name as Identifier).escapedText === "this") {
15376+
(jsDocFunctionType.parameters[0].name as Identifier).escapedText === InternalSymbolName.This) {
1537715377
return getTypeFromTypeNode(jsDocFunctionType.parameters[0].type!);
1537815378
}
1537915379
}
@@ -26797,6 +26797,7 @@ namespace ts {
2679726797

2679826798
populateSymbols();
2679926799

26800+
symbols.delete(InternalSymbolName.This); // Not a symbol, a keyword
2680026801
return symbolsToArray(symbols);
2680126802

2680226803
function populateSymbols() {

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3559,6 +3559,7 @@ namespace ts {
35593559
Resolving = "__resolving__", // Indicator symbol used to mark partially resolved type aliases
35603560
ExportEquals = "export=", // Export assignment symbol
35613561
Default = "default", // Default export symbol (technically not wholly internal, but included here for usability)
3562+
This = "this",
35623563
}
35633564

35643565
/**

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,8 @@ declare namespace ts {
21062106
Computed = "__computed",
21072107
Resolving = "__resolving__",
21082108
ExportEquals = "export=",
2109-
Default = "default"
2109+
Default = "default",
2110+
This = "this"
21102111
}
21112112
/**
21122113
* This represents a string whose leading underscore have been escaped by adding extra leading underscores.

tests/baselines/reference/api/typescript.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,8 @@ declare namespace ts {
21062106
Computed = "__computed",
21072107
Resolving = "__resolving__",
21082108
ExportEquals = "export=",
2109-
Default = "default"
2109+
Default = "default",
2110+
This = "this"
21102111
}
21112112
/**
21122113
* This represents a string whose leading underscore have been escaped by adding extra leading underscores.

tests/cases/fourslash/completionsThisType.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,20 @@
99
////
1010
////function f(this: { x: number }) { /*f*/ }
1111

12-
goTo.marker("");
13-
14-
verify.completionListContains("xyz", "(method) C.xyz(): any", "", "method", undefined, undefined, {
15-
includeInsertTextCompletions: true,
16-
insertText: "this.xyz",
17-
});
18-
19-
verify.completionListContains("foo bar", '(property) C["foo bar"]: number', "", "property", undefined, undefined, {
20-
includeInsertTextCompletions: true,
21-
insertText: 'this["foo bar"]',
22-
});
23-
24-
goTo.marker("f");
25-
26-
verify.completionListContains("x", "(property) x: number", "", "property", undefined, undefined, {
27-
includeInsertTextCompletions: true,
28-
insertText: "this.x",
29-
});
12+
const preferences: FourSlashInterface.UserPreferences = { includeInsertTextCompletions: true };
13+
verify.completions(
14+
{
15+
marker: "",
16+
includes: [
17+
{ name: "xyz", text: "(method) C.xyz(): any", kind: "method", insertText: "this.xyz" },
18+
{ name: "foo bar", text: '(property) C["foo bar"]: number', kind: "property", insertText: 'this["foo bar"]' },
19+
],
20+
isNewIdentifierLocation: true,
21+
preferences,
22+
},
23+
{
24+
marker: "f",
25+
includes: { name: "x", text: "(property) x: number", kind: "property", insertText: "this.x" },
26+
preferences,
27+
},
28+
);

0 commit comments

Comments
 (0)