Skip to content

Commit cdc1996

Browse files
authored
fix(49426): Object method snippet completions incorrectly add this parameters (#49757)
* fix(49426): omit this parameter * add OmitThisParameter to TypeFormatFlags * change flag value
1 parent 2f26088 commit cdc1996

File tree

6 files changed

+50
-6
lines changed

6 files changed

+50
-6
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5868,7 +5868,7 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
58685868
const expandedParams = getExpandedParameters(signature, /*skipUnionExpanding*/ true)[0];
58695869
// If the expanded parameter list had a variadic in a non-trailing position, don't expand it
58705870
const parameters = (some(expandedParams, p => p !== expandedParams[expandedParams.length - 1] && !!(getCheckFlags(p) & CheckFlags.RestParameter)) ? signature.parameters : expandedParams).map(parameter => symbolToParameterDeclaration(parameter, context, kind === SyntaxKind.Constructor, options?.privateSymbolVisitor, options?.bundledImports));
5871-
const thisParameter = tryGetThisParameterDeclaration(signature, context);
5871+
const thisParameter = context.flags & NodeBuilderFlags.OmitThisParameter ? undefined : tryGetThisParameterDeclaration(signature, context);
58725872
if (thisParameter) {
58735873
parameters.unshift(thisParameter);
58745874
}

src/compiler/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4664,10 +4664,11 @@ namespace ts {
46644664
UseAliasDefinedOutsideCurrentScope = 1 << 14, // Allow non-visible aliases
46654665
UseSingleQuotesForStringLiteralType = 1 << 28, // Use single quotes for string literal type
46664666
NoTypeReduction = 1 << 29, // Don't call getReducedType
4667+
OmitThisParameter = 1 << 25,
46674668

46684669
// Error handling
46694670
AllowThisInObjectLiteral = 1 << 15,
4670-
AllowQualifiedNameInPlaceOfIdentifier = 1 << 16,
4671+
AllowQualifiedNameInPlaceOfIdentifier = 1 << 16,
46714672
/** @deprecated AllowQualifedNameInPlaceOfIdentifier. Use AllowQualifiedNameInPlaceOfIdentifier instead. */
46724673
AllowQualifedNameInPlaceOfIdentifier = AllowQualifiedNameInPlaceOfIdentifier,
46734674
AllowAnonymousIdentifier = 1 << 17,
@@ -4709,6 +4710,7 @@ namespace ts {
47094710
UseAliasDefinedOutsideCurrentScope = 1 << 14, // For a `type T = ... ` defined in a different file, write `T` instead of its value, even though `T` can't be accessed in the current scope.
47104711
UseSingleQuotesForStringLiteralType = 1 << 28, // Use single quotes for string literal type
47114712
NoTypeReduction = 1 << 29, // Don't call getReducedType
4713+
OmitThisParameter = 1 << 25,
47124714

47134715
// Error Handling
47144716
AllowUniqueESSymbolType = 1 << 20, // This is bit 20 to align with the same bit in `NodeBuilderFlags`
@@ -4728,7 +4730,7 @@ namespace ts {
47284730
NodeBuilderFlagsMask = NoTruncation | WriteArrayAsGenericType | UseStructuralFallback | WriteTypeArgumentsOfSignature |
47294731
UseFullyQualifiedType | SuppressAnyReturnType | MultilineObjectLiterals | WriteClassExpressionAsTypeLiteral |
47304732
UseTypeOfFunction | OmitParameterModifiers | UseAliasDefinedOutsideCurrentScope | AllowUniqueESSymbolType | InTypeAlias |
4731-
UseSingleQuotesForStringLiteralType | NoTypeReduction,
4733+
UseSingleQuotesForStringLiteralType | NoTypeReduction | OmitThisParameter
47324734
}
47334735

47344736
export const enum SymbolFormatFlags {

src/services/completions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ namespace ts.Completions {
11261126
const name = getSynthesizedDeepClone(getNameOfDeclaration(declaration), /*includeTrivia*/ false) as PropertyName;
11271127
const type = checker.getWidenedType(checker.getTypeOfSymbolAtLocation(symbol, enclosingDeclaration));
11281128
const quotePreference = getQuotePreference(sourceFile, preferences);
1129-
const builderFlags = quotePreference === QuotePreference.Single ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : undefined;
1129+
const builderFlags = NodeBuilderFlags.OmitThisParameter | (quotePreference === QuotePreference.Single ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : NodeBuilderFlags.None);
11301130

11311131
switch (declaration.kind) {
11321132
case SyntaxKind.PropertySignature:

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,7 @@ declare namespace ts {
24202420
UseAliasDefinedOutsideCurrentScope = 16384,
24212421
UseSingleQuotesForStringLiteralType = 268435456,
24222422
NoTypeReduction = 536870912,
2423+
OmitThisParameter = 33554432,
24232424
AllowThisInObjectLiteral = 32768,
24242425
AllowQualifiedNameInPlaceOfIdentifier = 65536,
24252426
/** @deprecated AllowQualifedNameInPlaceOfIdentifier. Use AllowQualifiedNameInPlaceOfIdentifier instead. */
@@ -2450,6 +2451,7 @@ declare namespace ts {
24502451
UseAliasDefinedOutsideCurrentScope = 16384,
24512452
UseSingleQuotesForStringLiteralType = 268435456,
24522453
NoTypeReduction = 536870912,
2454+
OmitThisParameter = 33554432,
24532455
AllowUniqueESSymbolType = 1048576,
24542456
AddUndefined = 131072,
24552457
WriteArrowStyleSignature = 262144,
@@ -2458,7 +2460,7 @@ declare namespace ts {
24582460
InFirstTypeArgument = 4194304,
24592461
InTypeAlias = 8388608,
24602462
/** @deprecated */ WriteOwnNameForAnyLike = 0,
2461-
NodeBuilderFlagsMask = 814775659
2463+
NodeBuilderFlagsMask = 848330091
24622464
}
24632465
export enum SymbolFormatFlags {
24642466
None = 0,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,7 @@ declare namespace ts {
24202420
UseAliasDefinedOutsideCurrentScope = 16384,
24212421
UseSingleQuotesForStringLiteralType = 268435456,
24222422
NoTypeReduction = 536870912,
2423+
OmitThisParameter = 33554432,
24232424
AllowThisInObjectLiteral = 32768,
24242425
AllowQualifiedNameInPlaceOfIdentifier = 65536,
24252426
/** @deprecated AllowQualifedNameInPlaceOfIdentifier. Use AllowQualifiedNameInPlaceOfIdentifier instead. */
@@ -2450,6 +2451,7 @@ declare namespace ts {
24502451
UseAliasDefinedOutsideCurrentScope = 16384,
24512452
UseSingleQuotesForStringLiteralType = 268435456,
24522453
NoTypeReduction = 536870912,
2454+
OmitThisParameter = 33554432,
24532455
AllowUniqueESSymbolType = 1048576,
24542456
AddUndefined = 131072,
24552457
WriteArrowStyleSignature = 262144,
@@ -2458,7 +2460,7 @@ declare namespace ts {
24582460
InFirstTypeArgument = 4194304,
24592461
InTypeAlias = 8388608,
24602462
/** @deprecated */ WriteOwnNameForAnyLike = 0,
2461-
NodeBuilderFlagsMask = 814775659
2463+
NodeBuilderFlagsMask = 848330091
24622464
}
24632465
export enum SymbolFormatFlags {
24642466
None = 0,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @newline: LF
4+
// @Filename: a.ts
5+
////interface IFoo {
6+
//// bar(this: IFoo): void;
7+
////}
8+
////const obj: IFoo = {
9+
//// /*1*/
10+
////}
11+
12+
verify.completions({
13+
marker: "1",
14+
preferences: {
15+
includeCompletionsWithInsertText: true,
16+
includeCompletionsWithSnippetText: true,
17+
includeCompletionsWithObjectLiteralMethodSnippets: true,
18+
useLabelDetailsInCompletionEntries: true,
19+
},
20+
includes: [
21+
{
22+
name: "bar",
23+
sortText: completion.SortText.ObjectLiteralProperty(completion.SortText.LocationPriority, "bar"),
24+
insertText: undefined,
25+
},
26+
{
27+
name: "bar",
28+
sortText: completion.SortText.SortBelow(
29+
completion.SortText.ObjectLiteralProperty(completion.SortText.LocationPriority, "bar")),
30+
source: completion.CompletionSource.ObjectLiteralMethodSnippet,
31+
isSnippet: true,
32+
insertText: "bar() {\n $0\n},",
33+
labelDetails: {
34+
detail: "()",
35+
},
36+
},
37+
],
38+
});

0 commit comments

Comments
 (0)