Skip to content

Commit

Permalink
fix: Improve detection for "property methods" to convert as methods
Browse files Browse the repository at this point in the history
Resolves #1624
  • Loading branch information
Gerrit0 committed Jul 10, 2021
1 parent 5fd7f87 commit 98331b5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/lib/converter/factories/comment.ts
Expand Up @@ -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();
Expand Down
19 changes: 17 additions & 2 deletions src/lib/converter/symbols.ts
Expand Up @@ -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 &&
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions src/test/converter/alias/specs.json
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/test/converter/mixin/specs.json
Expand Up @@ -916,6 +916,9 @@
"kind": 65536,
"kindString": "Type literal",
"flags": {},
"comment": {
"shortText": "Any function"
},
"signatures": [
{
"id": 3,
Expand Down
7 changes: 7 additions & 0 deletions src/test/converter2.test.ts
Expand Up @@ -217,6 +217,13 @@ const issueTests: Record<string, (project: ProjectReflection) => 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", () => {
Expand Down
10 changes: 10 additions & 0 deletions 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;
}
}
10 changes: 10 additions & 0 deletions src/test/renderer/specs/classes/flattened.FlattenedClass.html
Expand Up @@ -171,6 +171,11 @@ <h3>callback</h3>
<div class="tsd-signature tsd-kind-icon">callback<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">(</span>param<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">number</span>, optionalParam<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol"> =&gt; </span><span class="tsd-signature-type">string</span></div>
<aside class="tsd-sources">
</aside>
<div class="tsd-comment tsd-typography">
<div class="lead">
<p>A member that holds a callback that requires a typed function signature.</p>
</div>
</div>
<div class="tsd-type-declaration">
<h4>Type declaration</h4>
<ul class="tsd-parameters">
Expand Down Expand Up @@ -356,6 +361,11 @@ <h3>single<wbr>Call<wbr>Signature</h3>
<div class="tsd-signature tsd-kind-icon">single<wbr>Call<wbr>Signature<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">...</span>args<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">[]</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol"> =&gt; </span><span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol"> =&gt; </span><span class="tsd-signature-type">string</span></div>
<aside class="tsd-sources">
</aside>
<div class="tsd-comment tsd-typography">
<div class="lead">
<p>Single call signature.</p>
</div>
</div>
<div class="tsd-type-declaration">
<h4>Type declaration</h4>
<ul class="tsd-parameters">
Expand Down

0 comments on commit 98331b5

Please sign in to comment.