From 9fbcdb1edb0ea3f8b94ef91feb8731bf73069fab Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 7 Jan 2020 01:19:23 -0500 Subject: [PATCH] Resurrect `SuppressAnyReturnType`, but make it used only at the toplevel --- src/compiler/checker.ts | 12 ++++++++---- src/compiler/types.ts | 2 -- src/services/codefixes/helpers.ts | 2 +- tests/baselines/reference/api/tsserverlibrary.d.ts | 2 -- tests/baselines/reference/api/typescript.d.ts | 2 -- .../codeFixClassExtendAbstractMethodWithLongName.ts | 2 +- ...tInterfaceComputedPropertyNameWellKnownSymbols.ts | 4 ++-- ...mplementInterfaceMultipleMembersAndPunctuation.ts | 8 ++++---- 8 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ddf4a2ceadb..8100e62b127 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4501,6 +4501,8 @@ namespace ts { } function signatureToSignatureDeclarationHelper(signature: Signature, kind: SyntaxKind, context: NodeBuilderContext): SignatureDeclaration { + const suppressAny = context.flags & NodeBuilderFlags.SuppressAnyReturnType; + if (suppressAny) context.flags &= ~NodeBuilderFlags.SuppressAnyReturnType; // suppress only toplevel `any`s let typeParameters: TypeParameterDeclaration[] | undefined; let typeArguments: TypeNode[] | undefined; if (context.flags & NodeBuilderFlags.WriteTypeArgumentsOfSignature && signature.target && signature.mapper && signature.target.typeParameters) { @@ -4530,10 +4532,12 @@ namespace ts { } else { const returnType = getReturnTypeOfSignature(signature); - returnTypeNode = returnType && typeToTypeNodeHelper(returnType, context); - } - if (!returnTypeNode) { - returnTypeNode = createKeywordTypeNode(SyntaxKind.AnyKeyword); + if (returnType && !(suppressAny && isTypeAny(returnType))) { + returnTypeNode = typeToTypeNodeHelper(returnType, context); + } + else if (!suppressAny) { + returnTypeNode = createKeywordTypeNode(SyntaxKind.AnyKeyword); + } } context.approximateLength += 3; // Usually a signature contributes a few more characters than this, but 3 is the minimum return createSignatureDeclaration(kind, typeParameters, parameters, returnTypeNode, typeArguments); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 036064bb64d..98f2531b6c6 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3619,7 +3619,6 @@ namespace ts { WriteTypeArgumentsOfSignature = 1 << 5, // Write the type arguments instead of type parameters of the signature UseFullyQualifiedType = 1 << 6, // Write out the fully qualified type name (eg. Module.Type, instead of Type) UseOnlyExternalAliasing = 1 << 7, // Only use external aliases for a symbol - /** @deprecated Ignored */ SuppressAnyReturnType = 1 << 8, // If the return type is any-like, don't offer a return type. WriteTypeParametersInQualifiedName = 1 << 9, MultilineObjectLiterals = 1 << 10, // Always write object literals across multiple lines @@ -3661,7 +3660,6 @@ namespace ts { WriteTypeArgumentsOfSignature = 1 << 5, // Write the type arguments instead of type parameters of the signature UseFullyQualifiedType = 1 << 6, // Write out the fully qualified type name (eg. Module.Type, instead of Type) // hole because `UseOnlyExternalAliasing` is here in node builder flags, but functions which take old flags use `SymbolFormatFlags` instead - /** @deprecated Ignored */ SuppressAnyReturnType = 1 << 8, // If the return type is any-like, don't offer a return type. // hole because `WriteTypeParametersInQualifiedName` is here in node builder flags, but functions which take old flags use `SymbolFormatFlags` for this instead MultilineObjectLiterals = 1 << 10, // Always print object literals across multiple lines (only used to map into node builder flags) diff --git a/src/services/codefixes/helpers.ts b/src/services/codefixes/helpers.ts index ed226acfdc2..95fc499a2f9 100644 --- a/src/services/codefixes/helpers.ts +++ b/src/services/codefixes/helpers.ts @@ -155,7 +155,7 @@ namespace ts.codefix { body: Block | undefined, ): MethodDeclaration | undefined { const program = context.program; - const signatureDeclaration = program.getTypeChecker().signatureToSignatureDeclaration(signature, SyntaxKind.MethodDeclaration, enclosingDeclaration, NodeBuilderFlags.NoTruncation, getNoopSymbolTrackerWithResolver(context)); + const signatureDeclaration = program.getTypeChecker().signatureToSignatureDeclaration(signature, SyntaxKind.MethodDeclaration, enclosingDeclaration, NodeBuilderFlags.NoTruncation | NodeBuilderFlags.SuppressAnyReturnType, getNoopSymbolTrackerWithResolver(context)); if (!signatureDeclaration) { return undefined; } diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 21d8d214498..5ca50db8067 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2110,7 +2110,6 @@ declare namespace ts { WriteTypeArgumentsOfSignature = 32, UseFullyQualifiedType = 64, UseOnlyExternalAliasing = 128, - /** @deprecated Ignored */ SuppressAnyReturnType = 256, WriteTypeParametersInQualifiedName = 512, MultilineObjectLiterals = 1024, @@ -2139,7 +2138,6 @@ declare namespace ts { UseStructuralFallback = 8, WriteTypeArgumentsOfSignature = 32, UseFullyQualifiedType = 64, - /** @deprecated Ignored */ SuppressAnyReturnType = 256, MultilineObjectLiterals = 1024, WriteClassExpressionAsTypeLiteral = 2048, diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index cbbc5ff6d89..1143d4c6ebc 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2110,7 +2110,6 @@ declare namespace ts { WriteTypeArgumentsOfSignature = 32, UseFullyQualifiedType = 64, UseOnlyExternalAliasing = 128, - /** @deprecated Ignored */ SuppressAnyReturnType = 256, WriteTypeParametersInQualifiedName = 512, MultilineObjectLiterals = 1024, @@ -2139,7 +2138,6 @@ declare namespace ts { UseStructuralFallback = 8, WriteTypeArgumentsOfSignature = 32, UseFullyQualifiedType = 64, - /** @deprecated Ignored */ SuppressAnyReturnType = 256, MultilineObjectLiterals = 1024, WriteClassExpressionAsTypeLiteral = 2048, diff --git a/tests/cases/fourslash/codeFixClassExtendAbstractMethodWithLongName.ts b/tests/cases/fourslash/codeFixClassExtendAbstractMethodWithLongName.ts index bed8c7de913..dd2eb9c1709 100644 --- a/tests/cases/fourslash/codeFixClassExtendAbstractMethodWithLongName.ts +++ b/tests/cases/fourslash/codeFixClassExtendAbstractMethodWithLongName.ts @@ -43,7 +43,7 @@ abstract class AbstractCstVisitor { ): Set; } class CstVisitorImplementation extends AbstractCstVisitor { - Node(arg1: [some.really.long.generated.type.goes.here.you.know.this_.should.be.pretty.simple.Yah[], another.really.long.generated.type.goes.here.too.because.who.cares.about.space.do_.you.feel.me.Yah[]], arg2: [this_will_be_collapsed], arg3: any, arg4: this_is_fine): any { + Node(arg1: [some.really.long.generated.type.goes.here.you.know.this_.should.be.pretty.simple.Yah[], another.really.long.generated.type.goes.here.too.because.who.cares.about.space.do_.you.feel.me.Yah[]], arg2: [this_will_be_collapsed], arg3: any, arg4: this_is_fine) { throw new Error("Method not implemented."); } }` diff --git a/tests/cases/fourslash/codeFixClassImplementInterfaceComputedPropertyNameWellKnownSymbols.ts b/tests/cases/fourslash/codeFixClassImplementInterfaceComputedPropertyNameWellKnownSymbols.ts index 0c49f8b3d39..d87fcb73d4e 100644 --- a/tests/cases/fourslash/codeFixClassImplementInterfaceComputedPropertyNameWellKnownSymbols.ts +++ b/tests/cases/fourslash/codeFixClassImplementInterfaceComputedPropertyNameWellKnownSymbols.ts @@ -43,11 +43,11 @@ class C implements I { throw new Error("Method not implemented."); } [Symbol.isConcatSpreadable]: boolean; - [Symbol.iterator](): any { + [Symbol.iterator]() { throw new Error("Method not implemented."); } [Symbol.match]: boolean; - [Symbol.replace](...args: {}): any { + [Symbol.replace](...args: {}) { throw new Error("Method not implemented."); } [Symbol.search](str: string): number { diff --git a/tests/cases/fourslash/codeFixClassImplementInterfaceMultipleMembersAndPunctuation.ts b/tests/cases/fourslash/codeFixClassImplementInterfaceMultipleMembersAndPunctuation.ts index 5e7f9acd7d3..0f8249eed1c 100644 --- a/tests/cases/fourslash/codeFixClassImplementInterfaceMultipleMembersAndPunctuation.ts +++ b/tests/cases/fourslash/codeFixClassImplementInterfaceMultipleMembersAndPunctuation.ts @@ -5,7 +5,7 @@ //// y: number //// z: number; //// f(): number, -//// g() +//// g(): any //// h(); ////} //// @@ -19,7 +19,7 @@ verify.codeFix({ y: number z: number; f(): number, - g() + g(): any h(); } @@ -30,10 +30,10 @@ class C1 implements I1 { f(): number { throw new Error("Method not implemented."); } - g(): any { + g() { throw new Error("Method not implemented."); } - h(): any { + h() { throw new Error("Method not implemented."); } }`,