From cdc1996e872f95fee54f6b93dd7622efe47d387c Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Tue, 5 Jul 2022 21:46:19 +0300 Subject: [PATCH] fix(49426): Object method snippet completions incorrectly add this parameters (#49757) * fix(49426): omit this parameter * add OmitThisParameter to TypeFormatFlags * change flag value --- src/compiler/checker.ts | 2 +- src/compiler/types.ts | 6 ++- src/services/completions.ts | 2 +- .../reference/api/tsserverlibrary.d.ts | 4 +- tests/baselines/reference/api/typescript.d.ts | 4 +- .../completionsObjectLiteralMethod4.ts | 38 +++++++++++++++++++ 6 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 tests/cases/fourslash/completionsObjectLiteralMethod4.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d5c80f9ca40..0f969629fdb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5868,7 +5868,7 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n") const expandedParams = getExpandedParameters(signature, /*skipUnionExpanding*/ true)[0]; // If the expanded parameter list had a variadic in a non-trailing position, don't expand it 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)); - const thisParameter = tryGetThisParameterDeclaration(signature, context); + const thisParameter = context.flags & NodeBuilderFlags.OmitThisParameter ? undefined : tryGetThisParameterDeclaration(signature, context); if (thisParameter) { parameters.unshift(thisParameter); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 8e18c407e78..fd7abdee8bc 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -4664,10 +4664,11 @@ namespace ts { UseAliasDefinedOutsideCurrentScope = 1 << 14, // Allow non-visible aliases UseSingleQuotesForStringLiteralType = 1 << 28, // Use single quotes for string literal type NoTypeReduction = 1 << 29, // Don't call getReducedType + OmitThisParameter = 1 << 25, // Error handling AllowThisInObjectLiteral = 1 << 15, - AllowQualifiedNameInPlaceOfIdentifier = 1 << 16, + AllowQualifiedNameInPlaceOfIdentifier = 1 << 16, /** @deprecated AllowQualifedNameInPlaceOfIdentifier. Use AllowQualifiedNameInPlaceOfIdentifier instead. */ AllowQualifedNameInPlaceOfIdentifier = AllowQualifiedNameInPlaceOfIdentifier, AllowAnonymousIdentifier = 1 << 17, @@ -4709,6 +4710,7 @@ namespace ts { 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. UseSingleQuotesForStringLiteralType = 1 << 28, // Use single quotes for string literal type NoTypeReduction = 1 << 29, // Don't call getReducedType + OmitThisParameter = 1 << 25, // Error Handling AllowUniqueESSymbolType = 1 << 20, // This is bit 20 to align with the same bit in `NodeBuilderFlags` @@ -4728,7 +4730,7 @@ namespace ts { NodeBuilderFlagsMask = NoTruncation | WriteArrayAsGenericType | UseStructuralFallback | WriteTypeArgumentsOfSignature | UseFullyQualifiedType | SuppressAnyReturnType | MultilineObjectLiterals | WriteClassExpressionAsTypeLiteral | UseTypeOfFunction | OmitParameterModifiers | UseAliasDefinedOutsideCurrentScope | AllowUniqueESSymbolType | InTypeAlias | - UseSingleQuotesForStringLiteralType | NoTypeReduction, + UseSingleQuotesForStringLiteralType | NoTypeReduction | OmitThisParameter } export const enum SymbolFormatFlags { diff --git a/src/services/completions.ts b/src/services/completions.ts index 253e7a52f4b..c24bc8dcaf1 100644 --- a/src/services/completions.ts +++ b/src/services/completions.ts @@ -1126,7 +1126,7 @@ namespace ts.Completions { const name = getSynthesizedDeepClone(getNameOfDeclaration(declaration), /*includeTrivia*/ false) as PropertyName; const type = checker.getWidenedType(checker.getTypeOfSymbolAtLocation(symbol, enclosingDeclaration)); const quotePreference = getQuotePreference(sourceFile, preferences); - const builderFlags = quotePreference === QuotePreference.Single ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : undefined; + const builderFlags = NodeBuilderFlags.OmitThisParameter | (quotePreference === QuotePreference.Single ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : NodeBuilderFlags.None); switch (declaration.kind) { case SyntaxKind.PropertySignature: diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index fb15ec4f011..8f78675d2b5 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2420,6 +2420,7 @@ declare namespace ts { UseAliasDefinedOutsideCurrentScope = 16384, UseSingleQuotesForStringLiteralType = 268435456, NoTypeReduction = 536870912, + OmitThisParameter = 33554432, AllowThisInObjectLiteral = 32768, AllowQualifiedNameInPlaceOfIdentifier = 65536, /** @deprecated AllowQualifedNameInPlaceOfIdentifier. Use AllowQualifiedNameInPlaceOfIdentifier instead. */ @@ -2450,6 +2451,7 @@ declare namespace ts { UseAliasDefinedOutsideCurrentScope = 16384, UseSingleQuotesForStringLiteralType = 268435456, NoTypeReduction = 536870912, + OmitThisParameter = 33554432, AllowUniqueESSymbolType = 1048576, AddUndefined = 131072, WriteArrowStyleSignature = 262144, @@ -2458,7 +2460,7 @@ declare namespace ts { InFirstTypeArgument = 4194304, InTypeAlias = 8388608, /** @deprecated */ WriteOwnNameForAnyLike = 0, - NodeBuilderFlagsMask = 814775659 + NodeBuilderFlagsMask = 848330091 } export enum SymbolFormatFlags { None = 0, diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index ed87f7ef891..d52463cf85d 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2420,6 +2420,7 @@ declare namespace ts { UseAliasDefinedOutsideCurrentScope = 16384, UseSingleQuotesForStringLiteralType = 268435456, NoTypeReduction = 536870912, + OmitThisParameter = 33554432, AllowThisInObjectLiteral = 32768, AllowQualifiedNameInPlaceOfIdentifier = 65536, /** @deprecated AllowQualifedNameInPlaceOfIdentifier. Use AllowQualifiedNameInPlaceOfIdentifier instead. */ @@ -2450,6 +2451,7 @@ declare namespace ts { UseAliasDefinedOutsideCurrentScope = 16384, UseSingleQuotesForStringLiteralType = 268435456, NoTypeReduction = 536870912, + OmitThisParameter = 33554432, AllowUniqueESSymbolType = 1048576, AddUndefined = 131072, WriteArrowStyleSignature = 262144, @@ -2458,7 +2460,7 @@ declare namespace ts { InFirstTypeArgument = 4194304, InTypeAlias = 8388608, /** @deprecated */ WriteOwnNameForAnyLike = 0, - NodeBuilderFlagsMask = 814775659 + NodeBuilderFlagsMask = 848330091 } export enum SymbolFormatFlags { None = 0, diff --git a/tests/cases/fourslash/completionsObjectLiteralMethod4.ts b/tests/cases/fourslash/completionsObjectLiteralMethod4.ts new file mode 100644 index 00000000000..f15a34838db --- /dev/null +++ b/tests/cases/fourslash/completionsObjectLiteralMethod4.ts @@ -0,0 +1,38 @@ +/// + +// @newline: LF +// @Filename: a.ts +////interface IFoo { +//// bar(this: IFoo): void; +////} +////const obj: IFoo = { +//// /*1*/ +////} + +verify.completions({ + marker: "1", + preferences: { + includeCompletionsWithInsertText: true, + includeCompletionsWithSnippetText: true, + includeCompletionsWithObjectLiteralMethodSnippets: true, + useLabelDetailsInCompletionEntries: true, + }, + includes: [ + { + name: "bar", + sortText: completion.SortText.ObjectLiteralProperty(completion.SortText.LocationPriority, "bar"), + insertText: undefined, + }, + { + name: "bar", + sortText: completion.SortText.SortBelow( + completion.SortText.ObjectLiteralProperty(completion.SortText.LocationPriority, "bar")), + source: completion.CompletionSource.ObjectLiteralMethodSnippet, + isSnippet: true, + insertText: "bar() {\n $0\n},", + labelDetails: { + detail: "()", + }, + }, + ], +});