From 98331b5b1a0248753b98c895d1b2168c31e3cee7 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sat, 10 Jul 2021 13:24:03 -0600 Subject: [PATCH] fix: Improve detection for "property methods" to convert as methods Resolves #1624 --- src/lib/converter/factories/comment.ts | 2 ++ src/lib/converter/symbols.ts | 19 +++++++++++++++++-- src/test/converter/alias/specs.json | 3 +++ src/test/converter/mixin/specs.json | 3 +++ src/test/converter2.test.ts | 7 +++++++ src/test/converter2/issues/gh1624.ts | 10 ++++++++++ .../classes/flattened.FlattenedClass.html | 10 ++++++++++ 7 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/test/converter2/issues/gh1624.ts diff --git a/src/lib/converter/factories/comment.ts b/src/lib/converter/factories/comment.ts index 3888642cc..a2aad06eb 100644 --- a/src/lib/converter/factories/comment.ts +++ b/src/lib/converter/factories/comment.ts @@ -119,6 +119,8 @@ export function getRawComment( node = node.parent; } else if (node.kind === ts.SyntaxKind.ExportSpecifier) { node = node.parent.parent; + } else if (node.kind === ts.SyntaxKind.FunctionType) { + node = node.parent; } const sourceFile = node.getSourceFile(); diff --git a/src/lib/converter/symbols.ts b/src/lib/converter/symbols.ts index be5fa4fb3..16cea538c 100644 --- a/src/lib/converter/symbols.ts +++ b/src/lib/converter/symbols.ts @@ -545,9 +545,10 @@ function convertProperty( return convertFunctionOrMethod(context, symbol, exportSymbol); } - // Special case: "arrow methods" should be treated as methods. if (declarations.length === 1) { const declaration = declarations[0]; + + // Special case: "arrow methods" should be treated as methods. if ( ts.isPropertyDeclaration(declaration) && !declaration.type && @@ -561,6 +562,20 @@ function convertProperty( exportSymbol ); } + + // Special case: "arrow properties" in type space should be treated as methods. + if ( + ts.isPropertySignature(declaration) && + declaration.type && + ts.isFunctionTypeNode(declaration.type) + ) { + return convertArrowAsMethod( + context, + symbol, + declaration.type, + exportSymbol + ); + } } const reflection = context.createDeclarationReflection( @@ -610,7 +625,7 @@ function convertProperty( function convertArrowAsMethod( context: Context, symbol: ts.Symbol, - arrow: ts.ArrowFunction, + arrow: ts.ArrowFunction | ts.FunctionTypeNode, exportSymbol?: ts.Symbol ) { const reflection = context.createDeclarationReflection( diff --git a/src/test/converter/alias/specs.json b/src/test/converter/alias/specs.json index 3f13dbb1e..f97cc6615 100644 --- a/src/test/converter/alias/specs.json +++ b/src/test/converter/alias/specs.json @@ -110,6 +110,9 @@ "kind": 65536, "kindString": "Type literal", "flags": {}, + "comment": { + "shortText": "A type that describes a compare function, e.g. for array.sort()." + }, "signatures": [ { "id": 3, diff --git a/src/test/converter/mixin/specs.json b/src/test/converter/mixin/specs.json index e6a596155..3091daaf9 100644 --- a/src/test/converter/mixin/specs.json +++ b/src/test/converter/mixin/specs.json @@ -916,6 +916,9 @@ "kind": 65536, "kindString": "Type literal", "flags": {}, + "comment": { + "shortText": "Any function" + }, "signatures": [ { "id": 3, diff --git a/src/test/converter2.test.ts b/src/test/converter2.test.ts index d5e80ed77..c94d34167 100644 --- a/src/test/converter2.test.ts +++ b/src/test/converter2.test.ts @@ -217,6 +217,13 @@ const issueTests: Record void> = { "Overwritten method with no comment should be inherited" ); }, + + gh1624(project) { + ok( + query(project, "Foo.baz").signatures?.[0]?.hasComment(), + "Property methods declared in interface should still allow comment inheritance" + ); + }, }; describe("Converter2", () => { diff --git a/src/test/converter2/issues/gh1624.ts b/src/test/converter2/issues/gh1624.ts new file mode 100644 index 000000000..3ccb2e2a7 --- /dev/null +++ b/src/test/converter2/issues/gh1624.ts @@ -0,0 +1,10 @@ +export interface Bar { + /** Some property style doc. */ + baz: () => number; +} + +export class Foo implements Bar { + baz(): number { + return 0; + } +} diff --git a/src/test/renderer/specs/classes/flattened.FlattenedClass.html b/src/test/renderer/specs/classes/flattened.FlattenedClass.html index 9e92baef6..6c78c4bbc 100644 --- a/src/test/renderer/specs/classes/flattened.FlattenedClass.html +++ b/src/test/renderer/specs/classes/flattened.FlattenedClass.html @@ -171,6 +171,11 @@

callback

callback: (param: number, optionalParam?: string) => string
+
+
+

A member that holds a callback that requires a typed function signature.

+
+

Type declaration

    @@ -356,6 +361,11 @@

    singleCallSignature

    singleCallSignature: (...args: string[]) => () => string
    +
    +
    +

    Single call signature.

    +
    +

    Type declaration