Skip to content

Commit

Permalink
Additional fixes for #1973
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Jul 1, 2022
1 parent 92df4e7 commit c75aa02
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@
- TypeDoc will now detect attempted inheritance from accessors and inherit from the getter or setter, #1968.
- `intentionallyNotExported` will now properly respect qualified names, #1972.
- Fixed missing namespace comments on `export * as NS` declarations, #1973.
- Fixed missing comments on `export const x = () => 123` function variables, #1973.
- Validation warnings caused by missing documentation will now be formatted like other warnings which reference a declaration.
- TypeDoc will no longer warn if both the `get` and `set` signatures of an accessor have a comment.

Expand Down
46 changes: 35 additions & 11 deletions src/lib/converter/comments/discovery.ts
Expand Up @@ -167,6 +167,7 @@ export function discoverSignatureComment(
const comments = collectCommentRanges(
ts.getLeadingCommentRanges(text, node.pos)
);
comments.reverse();

const comment = comments.find((ranges) =>
permittedRange(text, ranges, commentStyle)
Expand Down Expand Up @@ -220,15 +221,42 @@ function getRootModuleDeclaration(node: ts.ModuleDeclaration): ts.Node {
}

function declarationToCommentNode(node: ts.Declaration): ts.Node | undefined {
if (node.parent?.kind === ts.SyntaxKind.VariableDeclarationList) {
if (!node.parent) return node;

// const abc = 123
// ^^^
if (node.parent.kind === ts.SyntaxKind.VariableDeclarationList) {
return node.parent.parent;
}

if (node.kind === ts.SyntaxKind.ModuleDeclaration) {
if (!isTopmostModuleDeclaration(<ts.ModuleDeclaration>node)) {
// const a = () => {}
// ^^^^^^^^
if (node.parent.kind === ts.SyntaxKind.VariableDeclaration) {
return node.parent.parent.parent;
}

// class X { y = () => {} }
// ^^^^^^^^
// function Z() {}
// Z.method = () => {}
// ^^^^^^^^
// export default () => {}
// ^^^^^^^^
if (
[
ts.SyntaxKind.PropertyDeclaration,
ts.SyntaxKind.BinaryExpression,
ts.SyntaxKind.ExportAssignment,
].includes(node.parent.kind)
) {
return node.parent;
}

if (ts.isModuleDeclaration(node)) {
if (!isTopmostModuleDeclaration(node)) {
return;
} else {
return getRootModuleDeclaration(<ts.ModuleDeclaration>node);
return getRootModuleDeclaration(node);
}
}

Expand All @@ -237,13 +265,9 @@ function declarationToCommentNode(node: ts.Declaration): ts.Node | undefined {
}

if (
[
ts.SyntaxKind.NamespaceExport,
ts.SyntaxKind.FunctionExpression,
ts.SyntaxKind.FunctionType,
ts.SyntaxKind.FunctionType,
ts.SyntaxKind.ArrowFunction,
].includes(node.kind)
[ts.SyntaxKind.NamespaceExport, ts.SyntaxKind.FunctionType].includes(
node.kind
)
) {
return node.parent;
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/converter/function/specs.json
Expand Up @@ -2213,13 +2213,40 @@
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"comment": {
"summary": [
{
"kind": "text",
"text": "This is a function that is assigned to a variable."
}
],
"blockTags": [
{
"tag": "@returns",
"content": [
{
"kind": "text",
"text": "This is the return value of the function."
}
]
}
]
},
"parameters": [
{
"id": 85,
"name": "someParam",
"kind": 32768,
"kindString": "Parameter",
"flags": {},
"comment": {
"summary": [
{
"kind": "text",
"text": "This is some numeric parameter."
}
]
},
"type": {
"type": "intrinsic",
"name": "number"
Expand Down
24 changes: 24 additions & 0 deletions src/test/converter/mixin/specs.json
Expand Up @@ -1352,6 +1352,14 @@
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"comment": {
"summary": [
{
"kind": "text",
"text": "The \"mixin function\" of the Mixin1"
}
]
},
"typeParameter": [
{
"id": 21,
Expand Down Expand Up @@ -1467,6 +1475,14 @@
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"comment": {
"summary": [
{
"kind": "text",
"text": "The \"mixin function\" of the Mixin2"
}
]
},
"typeParameter": [
{
"id": 35,
Expand Down Expand Up @@ -1598,6 +1614,14 @@
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"comment": {
"summary": [
{
"kind": "text",
"text": "The \"mixin function\" of the Mixin3"
}
]
},
"typeParameter": [
{
"id": 53,
Expand Down
2 changes: 1 addition & 1 deletion src/test/converter2/issues/gh1973/a.ts
Expand Up @@ -6,4 +6,4 @@
/**
* Comment for a
*/
export const a = "a";
export const a = () => "a";
2 changes: 1 addition & 1 deletion src/test/converter2/issues/gh1973/b.ts
Expand Up @@ -6,4 +6,4 @@
/**
* Comment for b
*/
export const b = "b";
export const b = () => "b";
8 changes: 8 additions & 0 deletions src/test/issueTests.ts
Expand Up @@ -562,5 +562,13 @@ export const issueTests: {
);

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

const comments2 = ["A.a", "B.b"].map((n) =>
Comment.combineDisplayParts(
query(project, n).signatures![0].comment?.summary
)
);

equal(comments2, ["Comment for a", "Comment for b"]);
},
};

0 comments on commit c75aa02

Please sign in to comment.