Skip to content

Commit

Permalink
Add support for TypeScript 5's @overload
Browse files Browse the repository at this point in the history
Ref: #2201
  • Loading branch information
Gerrit0 committed Mar 19, 2023
1 parent 642dd2e commit aa36d85
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 8 deletions.
17 changes: 17 additions & 0 deletions src/lib/converter/comments/discovery.ts
Expand Up @@ -3,6 +3,7 @@ import { ReflectionKind } from "../../models";
import { assertNever, Logger } from "../../utils";
import { CommentStyle } from "../../utils/options/declaration";
import { nicePath } from "../../utils/paths";
import { ok } from "assert";

// Note: This does NOT include JSDoc syntax kinds. This is important!
// Comments from @typedef and @callback tags are handled specially by
Expand Down Expand Up @@ -172,6 +173,22 @@ export function discoverSignatureComment(
return;
}

if (ts.isJSDocSignature(node)) {
const comment = node.parent.parent;
ok(ts.isJSDoc(comment));

return [
node.getSourceFile(),
[
{
kind: ts.SyntaxKind.MultiLineCommentTrivia,
pos: comment.pos,
end: comment.end,
},
],
];
}

const text = node.getSourceFile().text;

const comments = collectCommentRanges(
Expand Down
9 changes: 2 additions & 7 deletions src/lib/converter/symbols.ts
Expand Up @@ -422,13 +422,8 @@ function convertFunctionOrMethod(

// Can't use zip here. We might have less declarations than signatures
// or less signatures than declarations.
for (let i = 0; i < signatures.length; i++) {
createSignature(
scope,
ReflectionKind.CallSignature,
signatures[i],
declarations[i]
);
for (const sig of signatures) {
createSignature(scope, ReflectionKind.CallSignature, sig);
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/lib/utils/options/sources/typedoc.ts
Expand Up @@ -301,7 +301,12 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
name: "excludeTags",
help: "Remove the listed block/modifier tags from doc comments.",
type: ParameterType.Array,
defaultValue: ["@override", "@virtual", "@privateRemarks"],
defaultValue: [
"@override",
"@virtual",
"@privateRemarks",
"@satisfies",
],
validate(value) {
if (!Validation.validate([Array, Validation.isTagString], value)) {
throw new Error(
Expand Down
2 changes: 2 additions & 0 deletions src/lib/utils/options/tsdoc-defaults.ts
Expand Up @@ -25,6 +25,7 @@ export const blockTags = [
"@callback",
"@prop",
"@property",
"@satisfies",
] as const;

export const tsdocInlineTags = ["@link", "@inheritDoc", "@label"] as const;
Expand Down Expand Up @@ -52,4 +53,5 @@ export const modifierTags = [
"@ignore",
"@enum",
"@event",
"@overload",
] as const;
19 changes: 19 additions & 0 deletions src/test/behaviorTests.ts
Expand Up @@ -577,6 +577,25 @@ export const behaviorTests: {
logger.expectNoOtherMessages();
},

overloadTags(project) {
const printValue = query(project, "printValue");
equal(printValue.signatures?.length, 2);

const [first, second] = printValue.signatures;

equal(first.parameters?.length, 1);
equal(
Comment.combineDisplayParts(first.parameters[0].comment?.summary),
"first docs"
);

equal(second.parameters?.length, 2);
equal(
Comment.combineDisplayParts(second.parameters[0].comment?.summary),
"second docs"
);
},

readonlyTag(project) {
const title = query(project, "Book.title");
const author = query(project, "Book.author");
Expand Down
18 changes: 18 additions & 0 deletions src/test/converter2/behavior/overloadTags.js
@@ -0,0 +1,18 @@
/**
* @overload
* @param {string} value first docs
* @return {void}
*/

/**
* @overload
* @param {number} value second docs
* @param {number} [maximumFractionDigits] second docs
* @return {void}
*/

/**
* @param {string | number} value impl docs
* @param {number} [maximumFractionDigits] impl docs
*/
export function printValue(value, maximumFractionDigits) {}
8 changes: 8 additions & 0 deletions tsdoc.json
Expand Up @@ -74,6 +74,14 @@
{
"tagName": "@protected",
"syntaxKind": "modifier"
},
{
"tagName": "@satisfies",
"syntaxKind": "block"
},
{
"tagName": "@overload",
"syntaxKind": "modifier"
}
]
}

0 comments on commit aa36d85

Please sign in to comment.