Skip to content

Commit 4d7666e

Browse files
committed
Fix rendering of handlers with comments and parameters
1 parent 28e12ba commit 4d7666e

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

src/render/comment-renderer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export default class CommentRenderer {
2424
const paramsSection: string[] = [];
2525
if (signature.parameters) {
2626
const paramComments = signature.parameters
27-
.filter(p => p.comment?.text)
28-
.map(p => this.renderMultilineComment(`@param ${p.name}`, p.comment!.text!))
27+
.filter(p => p.comment?.text || p.comment?.shortText)
28+
.map(p => this.renderMultilineComment(`@param ${p.name}`, p.comment?.text || p.comment?.shortText || ''))
2929
.join('\n');
3030

3131
if (paramComments.length) {

src/render/type-literal-renderer.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ export default class TypeLiteralRenderer extends ContainerRenderer {
1010

1111
public render(node: Reflection): string {
1212
const lines: string[] = [];
13+
const member = node as DeclarationReflection;
14+
let indent = false;
1315

14-
if (node.comment) {
16+
if (member.signatures && member.signatures[0]?.comment) {
17+
indent = true;
18+
} else if (node.comment) {
1519
this.pushIfTruthy(lines, this.renderComment(node));
1620
}
1721

18-
const member = node as DeclarationReflection;
19-
2022
if (member.children || member.indexSignature) {
2123
const children = [...(member.children || []), member.indexSignature].filter(c => c);
2224
if (node.parent?.kind === ReflectionKind.TypeAlias) {
@@ -28,7 +30,12 @@ export default class TypeLiteralRenderer extends ContainerRenderer {
2830
lines.push(join(' ', '{', children.map(c => ReflectionFormatter.instance().render(c)).join(', '), '}'));
2931
}
3032
} else if (member.signatures) {
31-
lines.push(ReflectionFormatter.instance().render(member.signatures[0]));
33+
let signature = ReflectionFormatter.instance().render(member.signatures[0]);
34+
if (indent) {
35+
lines.push('\n');
36+
signature = signature.split('\n').map(l => ` ${l}`).join('\n');
37+
}
38+
lines.push(signature);
3239
} else if (member.name === '__type') {
3340
lines.push('{}');
3441
}

tests/dynamic-tests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ function createApplication(logOutput: string[]) {
4040
return app;
4141
}
4242

43-
function removeBlankLines(input: string) {
44-
return input.replace(/^$\r?\n/gm, '');
43+
function normalizeWhitespace(input: string) {
44+
return input.replace(/^$\r?\n/gm, '').replace(/\s+/gm, '');
4545
}
4646

4747
describe('Dynamic test suite', () => {
@@ -98,7 +98,7 @@ describe('Dynamic test suite', () => {
9898
if (writeOutput) {
9999
fs.writeFileSync(outputDeclarationFile, result);
100100
}
101-
expect(removeBlankLines(result)).toBe(removeBlankLines(expectedOutput.trim()));
101+
expect(normalizeWhitespace(result)).toBe(normalizeWhitespace(expectedOutput.trim()));
102102
} else {
103103
console.log(logOutput.join('\n'));
104104
expect(logOutput).toEqual([]);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
declare module Handlers {
2+
type MyUncommentedHandler = (foo: any) => any;
3+
4+
/**
5+
* My comment
6+
*/
7+
type MyCommentedHandler =
8+
/**
9+
* Handler comment
10+
*
11+
* @param foo can be anything
12+
*/
13+
(foo: any) => any;
14+
}

0 commit comments

Comments
 (0)