Skip to content

Commit

Permalink
Fix comments for constructor properties
Browse files Browse the repository at this point in the history
Resolves #1261
  • Loading branch information
Gerrit0 committed Oct 23, 2022
1 parent 1ebddd6 commit 9671e18
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### Bug Fixes

- Constructor parameter-properties will now use the `@param` comment for the parameter if available, #1261.
- Fixed display of object types containing methods, #1788.
- Fixed conversion of intrinsic string mapping types when converting without a type node, #2079.

Expand Down
31 changes: 30 additions & 1 deletion src/lib/converter/comments/index.ts
Expand Up @@ -115,12 +115,41 @@ export function getComment(
);
}

return getCommentImpl(
const comment = getCommentImpl(
discoverComment(symbol, kind, logger, commentStyle),
config,
logger,
symbol.declarations?.some(ts.isSourceFile) || false
);

if (!comment && kind === ReflectionKind.Property) {
return getConstructorParamPropertyComment(
symbol,
config,
logger,
commentStyle
);
}

return comment;
}

function getConstructorParamPropertyComment(
symbol: ts.Symbol,
config: CommentParserConfig,
logger: Logger,
commentStyle: CommentStyle
): Comment | undefined {
const decl = symbol.declarations?.find(ts.isParameter);
if (!decl) return;

const ctor = decl.parent;
const comment = getSignatureComment(ctor, config, logger, commentStyle);

const paramTag = comment?.getIdentifiedTag(symbol.name, "@param");
if (paramTag) {
return new Comment(paramTag.content);
}
}

export function getSignatureComment(
Expand Down
80 changes: 80 additions & 0 deletions src/test/converter/class/specs-with-lump-categories.json
Expand Up @@ -1573,6 +1573,14 @@
"flags": {
"isPrivate": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Private string property"
}
]
},
"sources": [
{
"fileName": "class.ts",
Expand All @@ -1594,6 +1602,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Public number property"
}
]
},
"sources": [
{
"fileName": "class.ts",
Expand All @@ -1615,6 +1631,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Public implicit any property"
}
]
},
"sources": [
{
"fileName": "class.ts",
Expand Down Expand Up @@ -2265,6 +2289,14 @@
"flags": {
"isReadonly": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Vector name"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand All @@ -2286,6 +2318,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "X component of the Vector"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand All @@ -2307,6 +2347,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Y component of the Vector"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand Down Expand Up @@ -2493,6 +2541,14 @@
"flags": {
"isReadonly": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Vector name"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand All @@ -2519,6 +2575,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "X component of the Vector"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand All @@ -2545,6 +2609,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Y component of the Vector"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand All @@ -2571,6 +2643,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Z component of the Vector"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand Down
80 changes: 80 additions & 0 deletions src/test/converter/class/specs.json
Expand Up @@ -1569,6 +1569,14 @@
"flags": {
"isPrivate": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Private string property"
}
]
},
"sources": [
{
"fileName": "class.ts",
Expand All @@ -1590,6 +1598,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Public number property"
}
]
},
"sources": [
{
"fileName": "class.ts",
Expand All @@ -1611,6 +1627,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Public implicit any property"
}
]
},
"sources": [
{
"fileName": "class.ts",
Expand Down Expand Up @@ -2261,6 +2285,14 @@
"flags": {
"isReadonly": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Vector name"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand All @@ -2282,6 +2314,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "X component of the Vector"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand All @@ -2303,6 +2343,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Y component of the Vector"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand Down Expand Up @@ -2489,6 +2537,14 @@
"flags": {
"isReadonly": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Vector name"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand All @@ -2515,6 +2571,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "X component of the Vector"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand All @@ -2541,6 +2605,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Y component of the Vector"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand All @@ -2567,6 +2639,14 @@
"flags": {
"isPublic": true
},
"comment": {
"summary": [
{
"kind": "text",
"text": "Z component of the Vector"
}
]
},
"sources": [
{
"fileName": "constructor-properties.ts",
Expand Down
7 changes: 7 additions & 0 deletions src/test/converter2/issues/gh1261.ts
@@ -0,0 +1,7 @@
export class X {
/**
* Instantiates X.
* @param prop The property of X.
*/
constructor(readonly prop: number) {}
}
8 changes: 8 additions & 0 deletions src/test/issueTests.ts
Expand Up @@ -119,6 +119,14 @@ export const issueTests: {
equal(Comment.combineDisplayParts(foo.comment?.summary), "Docs!");
},

gh1261(project) {
const prop = query(project, "X.prop");
equal(
Comment.combineDisplayParts(prop.comment?.summary),
"The property of X."
);
},

gh1330(project) {
const example = query(project, "ExampleParam");
equal(example?.type?.type, "reference");
Expand Down

0 comments on commit 9671e18

Please sign in to comment.