Skip to content

Commit e5bfa10

Browse files
committed
Prefer placing comment on function signature
This is specific to variable-functions, and is a balance between having comments supported on function declarations and comments preferring the signature to be more consistent with how regular function declarations work. Resolves #2824
1 parent 934281c commit e5bfa10

File tree

5 files changed

+49
-40
lines changed

5 files changed

+49
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ title: Changelog
2424
- Introduced `packagesRequiringDocumentation` option for `validation.notDocumented`, TypeDoc will expect comments to be present for symbols in the specified packages.
2525
- TypeDoc's `--entryPointStrategy merge` mode now requires JSON from at least version 0.28.0.
2626
- TypeDoc now exports a `typedoc/browser` entrypoint for parsing and using serialized JSON files, #2528.
27+
- Variable-functions will now prefer placing the comment on the signature if there is only one signature present, #2824.
2728

2829
TODO:
2930

src/lib/converter/symbols.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,18 @@ function convertVariableAsFunction(
11451145
);
11461146
}
11471147

1148+
// #2824 If there is only one signature, and there isn't a comment
1149+
// on the signature already, treat the comment on the variable
1150+
// as if it belongs to the signature instead.
1151+
if (
1152+
reflection.signatures.length === 1 &&
1153+
!reflection.signatures[0].comment &&
1154+
reflection.comment
1155+
) {
1156+
reflection.signatures[0].comment = reflection.comment;
1157+
delete reflection.comment;
1158+
}
1159+
11481160
return (
11491161
convertFunctionProperties(context.withScope(reflection), symbol, type) |
11501162
ts.SymbolFlags.Property

src/test/converter/function/specs.json

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,14 +1941,6 @@
19411941
"variant": "declaration",
19421942
"kind": 64,
19431943
"flags": {},
1944-
"comment": {
1945-
"summary": [
1946-
{
1947-
"kind": "text",
1948-
"text": "This is a function that is assigned to a variable."
1949-
}
1950-
]
1951-
},
19521944
"sources": [
19531945
{
19541946
"fileName": "function.ts",
@@ -1965,7 +1957,12 @@
19651957
"kind": 4096,
19661958
"flags": {},
19671959
"comment": {
1968-
"summary": [],
1960+
"summary": [
1961+
{
1962+
"kind": "text",
1963+
"text": "This is a function that is assigned to a variable."
1964+
}
1965+
],
19691966
"blockTags": [
19701967
{
19711968
"tag": "@returns",

src/test/converter/mixin/specs.json

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,14 +1572,6 @@
15721572
"variant": "declaration",
15731573
"kind": 64,
15741574
"flags": {},
1575-
"comment": {
1576-
"summary": [
1577-
{
1578-
"kind": "text",
1579-
"text": "The \"mixin function\" of the Mixin1"
1580-
}
1581-
]
1582-
},
15831575
"sources": [
15841576
{
15851577
"fileName": "mixin.ts",
@@ -1595,6 +1587,14 @@
15951587
"variant": "signature",
15961588
"kind": 4096,
15971589
"flags": {},
1590+
"comment": {
1591+
"summary": [
1592+
{
1593+
"kind": "text",
1594+
"text": "The \"mixin function\" of the Mixin1"
1595+
}
1596+
]
1597+
},
15981598
"sources": [
15991599
{
16001600
"fileName": "mixin.ts",
@@ -1751,14 +1751,6 @@
17511751
"variant": "declaration",
17521752
"kind": 64,
17531753
"flags": {},
1754-
"comment": {
1755-
"summary": [
1756-
{
1757-
"kind": "text",
1758-
"text": "The \"mixin function\" of the Mixin2"
1759-
}
1760-
]
1761-
},
17621754
"sources": [
17631755
{
17641756
"fileName": "mixin.ts",
@@ -1774,6 +1766,14 @@
17741766
"variant": "signature",
17751767
"kind": 4096,
17761768
"flags": {},
1769+
"comment": {
1770+
"summary": [
1771+
{
1772+
"kind": "text",
1773+
"text": "The \"mixin function\" of the Mixin2"
1774+
}
1775+
]
1776+
},
17771777
"sources": [
17781778
{
17791779
"fileName": "mixin.ts",
@@ -1941,14 +1941,6 @@
19411941
"variant": "declaration",
19421942
"kind": 64,
19431943
"flags": {},
1944-
"comment": {
1945-
"summary": [
1946-
{
1947-
"kind": "text",
1948-
"text": "The \"mixin function\" of the Mixin3"
1949-
}
1950-
]
1951-
},
19521944
"sources": [
19531945
{
19541946
"fileName": "mixin.ts",
@@ -1970,6 +1962,14 @@
19701962
"variant": "signature",
19711963
"kind": 4096,
19721964
"flags": {},
1965+
"comment": {
1966+
"summary": [
1967+
{
1968+
"kind": "text",
1969+
"text": "The \"mixin function\" of the Mixin3"
1970+
}
1971+
]
1972+
},
19731973
"sources": [
19741974
{
19751975
"fileName": "mixin.ts",

src/test/issues.c2.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ describe("Issue Tests", () => {
374374
querySig(project, "bar"),
375375
].map((r) => Comment.combineDisplayParts(r.comment?.summary));
376376

377-
equal(comments, ["bar", "metadata", "fn", ""]);
377+
equal(comments, ["", "metadata", "fn", "bar"]);
378378
});
379379

380380
it("#1660", () => {
@@ -429,7 +429,7 @@ describe("Issue Tests", () => {
429429

430430
it("#1770", () => {
431431
const project = convert();
432-
const sym1 = query(project, "sym1");
432+
const sym1 = querySig(project, "sym1");
433433
equal(
434434
Comment.combineDisplayParts(sym1.comment?.summary),
435435
"Docs for Sym1",
@@ -670,7 +670,7 @@ describe("Issue Tests", () => {
670670

671671
equal(comments, ["A override", "B module"]);
672672

673-
const comments2 = ["A.a", "B.b"].map((n) => Comment.combineDisplayParts(query(project, n).comment?.summary));
673+
const comments2 = ["A.a", "B.b"].map((n) => Comment.combineDisplayParts(querySig(project, n).comment?.summary));
674674

675675
equal(comments2, ["Comment for a", "Comment for b"]);
676676
});
@@ -742,7 +742,7 @@ describe("Issue Tests", () => {
742742

743743
it("#2008", () => {
744744
const project = convert();
745-
const fn = query(project, "myFn");
745+
const fn = querySig(project, "myFn");
746746
equal(Comment.combineDisplayParts(fn.comment?.summary), "Docs");
747747
});
748748

@@ -957,8 +957,7 @@ describe("Issue Tests", () => {
957957
it("#2156", () => {
958958
app.options.setValue("excludeNotDocumented", true);
959959
const project = convert();
960-
const foo = query(project, "foo");
961-
equal(foo.signatures?.length, 1);
960+
const foo = querySig(project, "foo");
962961
equal(
963962
Comment.combineDisplayParts(foo.comment?.summary),
964963
"Is documented",
@@ -1568,7 +1567,7 @@ describe("Issue Tests", () => {
15681567
equal(getComment(project, "f32"), "f32 comment");
15691568
equal(getComment(project, "f32.a"), "A comment");
15701569
equal(getComment(project, "f32.a.member"), "Member comment");
1571-
equal(getComment(project, "f32.a.fn"), "Fn comment");
1570+
equal(getSigComment(project, "f32.a.fn"), "Fn comment");
15721571
equal(getComment(project, "f32.b"), "B comment");
15731572
});
15741573

0 commit comments

Comments
 (0)