Skip to content

Commit 9671e18

Browse files
committed
Fix comments for constructor properties
Resolves #1261
1 parent 1ebddd6 commit 9671e18

File tree

6 files changed

+206
-1
lines changed

6 files changed

+206
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Bug Fixes
44

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

src/lib/converter/comments/index.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,41 @@ export function getComment(
115115
);
116116
}
117117

118-
return getCommentImpl(
118+
const comment = getCommentImpl(
119119
discoverComment(symbol, kind, logger, commentStyle),
120120
config,
121121
logger,
122122
symbol.declarations?.some(ts.isSourceFile) || false
123123
);
124+
125+
if (!comment && kind === ReflectionKind.Property) {
126+
return getConstructorParamPropertyComment(
127+
symbol,
128+
config,
129+
logger,
130+
commentStyle
131+
);
132+
}
133+
134+
return comment;
135+
}
136+
137+
function getConstructorParamPropertyComment(
138+
symbol: ts.Symbol,
139+
config: CommentParserConfig,
140+
logger: Logger,
141+
commentStyle: CommentStyle
142+
): Comment | undefined {
143+
const decl = symbol.declarations?.find(ts.isParameter);
144+
if (!decl) return;
145+
146+
const ctor = decl.parent;
147+
const comment = getSignatureComment(ctor, config, logger, commentStyle);
148+
149+
const paramTag = comment?.getIdentifiedTag(symbol.name, "@param");
150+
if (paramTag) {
151+
return new Comment(paramTag.content);
152+
}
124153
}
125154

126155
export function getSignatureComment(

src/test/converter/class/specs-with-lump-categories.json

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,14 @@
15731573
"flags": {
15741574
"isPrivate": true
15751575
},
1576+
"comment": {
1577+
"summary": [
1578+
{
1579+
"kind": "text",
1580+
"text": "Private string property"
1581+
}
1582+
]
1583+
},
15761584
"sources": [
15771585
{
15781586
"fileName": "class.ts",
@@ -1594,6 +1602,14 @@
15941602
"flags": {
15951603
"isPublic": true
15961604
},
1605+
"comment": {
1606+
"summary": [
1607+
{
1608+
"kind": "text",
1609+
"text": "Public number property"
1610+
}
1611+
]
1612+
},
15971613
"sources": [
15981614
{
15991615
"fileName": "class.ts",
@@ -1615,6 +1631,14 @@
16151631
"flags": {
16161632
"isPublic": true
16171633
},
1634+
"comment": {
1635+
"summary": [
1636+
{
1637+
"kind": "text",
1638+
"text": "Public implicit any property"
1639+
}
1640+
]
1641+
},
16181642
"sources": [
16191643
{
16201644
"fileName": "class.ts",
@@ -2265,6 +2289,14 @@
22652289
"flags": {
22662290
"isReadonly": true
22672291
},
2292+
"comment": {
2293+
"summary": [
2294+
{
2295+
"kind": "text",
2296+
"text": "Vector name"
2297+
}
2298+
]
2299+
},
22682300
"sources": [
22692301
{
22702302
"fileName": "constructor-properties.ts",
@@ -2286,6 +2318,14 @@
22862318
"flags": {
22872319
"isPublic": true
22882320
},
2321+
"comment": {
2322+
"summary": [
2323+
{
2324+
"kind": "text",
2325+
"text": "X component of the Vector"
2326+
}
2327+
]
2328+
},
22892329
"sources": [
22902330
{
22912331
"fileName": "constructor-properties.ts",
@@ -2307,6 +2347,14 @@
23072347
"flags": {
23082348
"isPublic": true
23092349
},
2350+
"comment": {
2351+
"summary": [
2352+
{
2353+
"kind": "text",
2354+
"text": "Y component of the Vector"
2355+
}
2356+
]
2357+
},
23102358
"sources": [
23112359
{
23122360
"fileName": "constructor-properties.ts",
@@ -2493,6 +2541,14 @@
24932541
"flags": {
24942542
"isReadonly": true
24952543
},
2544+
"comment": {
2545+
"summary": [
2546+
{
2547+
"kind": "text",
2548+
"text": "Vector name"
2549+
}
2550+
]
2551+
},
24962552
"sources": [
24972553
{
24982554
"fileName": "constructor-properties.ts",
@@ -2519,6 +2575,14 @@
25192575
"flags": {
25202576
"isPublic": true
25212577
},
2578+
"comment": {
2579+
"summary": [
2580+
{
2581+
"kind": "text",
2582+
"text": "X component of the Vector"
2583+
}
2584+
]
2585+
},
25222586
"sources": [
25232587
{
25242588
"fileName": "constructor-properties.ts",
@@ -2545,6 +2609,14 @@
25452609
"flags": {
25462610
"isPublic": true
25472611
},
2612+
"comment": {
2613+
"summary": [
2614+
{
2615+
"kind": "text",
2616+
"text": "Y component of the Vector"
2617+
}
2618+
]
2619+
},
25482620
"sources": [
25492621
{
25502622
"fileName": "constructor-properties.ts",
@@ -2571,6 +2643,14 @@
25712643
"flags": {
25722644
"isPublic": true
25732645
},
2646+
"comment": {
2647+
"summary": [
2648+
{
2649+
"kind": "text",
2650+
"text": "Z component of the Vector"
2651+
}
2652+
]
2653+
},
25742654
"sources": [
25752655
{
25762656
"fileName": "constructor-properties.ts",

src/test/converter/class/specs.json

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,14 @@
15691569
"flags": {
15701570
"isPrivate": true
15711571
},
1572+
"comment": {
1573+
"summary": [
1574+
{
1575+
"kind": "text",
1576+
"text": "Private string property"
1577+
}
1578+
]
1579+
},
15721580
"sources": [
15731581
{
15741582
"fileName": "class.ts",
@@ -1590,6 +1598,14 @@
15901598
"flags": {
15911599
"isPublic": true
15921600
},
1601+
"comment": {
1602+
"summary": [
1603+
{
1604+
"kind": "text",
1605+
"text": "Public number property"
1606+
}
1607+
]
1608+
},
15931609
"sources": [
15941610
{
15951611
"fileName": "class.ts",
@@ -1611,6 +1627,14 @@
16111627
"flags": {
16121628
"isPublic": true
16131629
},
1630+
"comment": {
1631+
"summary": [
1632+
{
1633+
"kind": "text",
1634+
"text": "Public implicit any property"
1635+
}
1636+
]
1637+
},
16141638
"sources": [
16151639
{
16161640
"fileName": "class.ts",
@@ -2261,6 +2285,14 @@
22612285
"flags": {
22622286
"isReadonly": true
22632287
},
2288+
"comment": {
2289+
"summary": [
2290+
{
2291+
"kind": "text",
2292+
"text": "Vector name"
2293+
}
2294+
]
2295+
},
22642296
"sources": [
22652297
{
22662298
"fileName": "constructor-properties.ts",
@@ -2282,6 +2314,14 @@
22822314
"flags": {
22832315
"isPublic": true
22842316
},
2317+
"comment": {
2318+
"summary": [
2319+
{
2320+
"kind": "text",
2321+
"text": "X component of the Vector"
2322+
}
2323+
]
2324+
},
22852325
"sources": [
22862326
{
22872327
"fileName": "constructor-properties.ts",
@@ -2303,6 +2343,14 @@
23032343
"flags": {
23042344
"isPublic": true
23052345
},
2346+
"comment": {
2347+
"summary": [
2348+
{
2349+
"kind": "text",
2350+
"text": "Y component of the Vector"
2351+
}
2352+
]
2353+
},
23062354
"sources": [
23072355
{
23082356
"fileName": "constructor-properties.ts",
@@ -2489,6 +2537,14 @@
24892537
"flags": {
24902538
"isReadonly": true
24912539
},
2540+
"comment": {
2541+
"summary": [
2542+
{
2543+
"kind": "text",
2544+
"text": "Vector name"
2545+
}
2546+
]
2547+
},
24922548
"sources": [
24932549
{
24942550
"fileName": "constructor-properties.ts",
@@ -2515,6 +2571,14 @@
25152571
"flags": {
25162572
"isPublic": true
25172573
},
2574+
"comment": {
2575+
"summary": [
2576+
{
2577+
"kind": "text",
2578+
"text": "X component of the Vector"
2579+
}
2580+
]
2581+
},
25182582
"sources": [
25192583
{
25202584
"fileName": "constructor-properties.ts",
@@ -2541,6 +2605,14 @@
25412605
"flags": {
25422606
"isPublic": true
25432607
},
2608+
"comment": {
2609+
"summary": [
2610+
{
2611+
"kind": "text",
2612+
"text": "Y component of the Vector"
2613+
}
2614+
]
2615+
},
25442616
"sources": [
25452617
{
25462618
"fileName": "constructor-properties.ts",
@@ -2567,6 +2639,14 @@
25672639
"flags": {
25682640
"isPublic": true
25692641
},
2642+
"comment": {
2643+
"summary": [
2644+
{
2645+
"kind": "text",
2646+
"text": "Z component of the Vector"
2647+
}
2648+
]
2649+
},
25702650
"sources": [
25712651
{
25722652
"fileName": "constructor-properties.ts",

src/test/converter2/issues/gh1261.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class X {
2+
/**
3+
* Instantiates X.
4+
* @param prop The property of X.
5+
*/
6+
constructor(readonly prop: number) {}
7+
}

src/test/issueTests.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ export const issueTests: {
119119
equal(Comment.combineDisplayParts(foo.comment?.summary), "Docs!");
120120
},
121121

122+
gh1261(project) {
123+
const prop = query(project, "X.prop");
124+
equal(
125+
Comment.combineDisplayParts(prop.comment?.summary),
126+
"The property of X."
127+
);
128+
},
129+
122130
gh1330(project) {
123131
const example = query(project, "ExampleParam");
124132
equal(example?.type?.type, "reference");

0 commit comments

Comments
 (0)