Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed JSDoc comments and optional params parsing #1810

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lib/converter/factories/comment.ts
Expand Up @@ -238,6 +238,10 @@ export function parseComment(
if (param) {
paramName = param[0];
line = line.substr(paramName.length + 1).trim();
// "[optionalArg]" -> "optionalArg"
if (/^\[.*\]$/.test(paramName)) {
paramName = paramName.slice(1, -1);
}
}
line = consumeTypeData(line);
line = line.replace(/^-\s+/, "");
Expand Down
21 changes: 18 additions & 3 deletions src/lib/converter/factories/signature.ts
@@ -1,6 +1,7 @@
import * as ts from "typescript";
import * as assert from "assert";
import {
Comment,
DeclarationReflection,
IntrinsicType,
ParameterReflection,
Expand All @@ -15,6 +16,7 @@ import type { Context } from "../context";
import { ConverterEvents } from "../converter-events";
import { convertDefaultValue } from "../convert-expression";
import { removeUndefined } from "../utils/reflections";
import { getJsDocCommentText } from "./comment";

export function createSignature(
context: Context,
Expand Down Expand Up @@ -146,9 +148,15 @@ function convertParameters(

let isOptional = false;
if (declaration) {
isOptional = ts.isParameter(declaration)
? !!declaration.questionToken
: declaration.isBracketed;
if (ts.isParameter(declaration)) {
const paramTag = ts
.getJSDocTags(declaration)
.find(ts.isJSDocParameterTag);
isOptional =
!!declaration.questionToken || !!paramTag?.isBracketed;
} else {
isOptional = declaration.isBracketed;
}
}

if (isOptional) {
Expand Down Expand Up @@ -212,6 +220,13 @@ export function convertParameterNodes(
: !!param.typeExpression &&
ts.isJSDocVariadicType(param.typeExpression.type)
);

if (ts.isJSDocParameterTag(param)) {
const comment = getJsDocCommentText(param.comment);
if (comment) {
paramRefl.comment = new Comment(comment);
}
}
return paramRefl;
});
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/converter/js/specs.json
Expand Up @@ -491,6 +491,9 @@
"flags": {
"isOptional": true
},
"comment": {
"shortText": "an optional argument"
},
"type": {
"type": "intrinsic",
"name": "string"
Expand Down
4 changes: 4 additions & 0 deletions src/test/converter2/issues/gh1804.js
@@ -0,0 +1,4 @@
/**
* @param {number} [arg]
*/
export function foo(arg) {}
9 changes: 9 additions & 0 deletions src/test/converter2/issues/gh567.js
@@ -0,0 +1,9 @@
/**
* @param {number} [arg1] another optional argument
*/
function someFunc(arg1) {}

/**
* @callback SomeCallback
* @param {string} [arg0] an optional argument
*/
22 changes: 22 additions & 0 deletions src/test/issueTests.ts
Expand Up @@ -19,6 +19,22 @@ function query(project: ProjectReflection, name: string) {
export const issueTests: {
[issue: string]: (project: ProjectReflection) => void;
} = {
gh567(project) {
const callback = query(project, "SomeCallback");
ok(callback.type instanceof ReflectionType);
equal(
callback.type.declaration.signatures?.[0].parameters?.[0].comment
?.shortText,
"an optional argument"
);

const func = query(project, "someFunc");
equal(
func?.signatures?.[0].parameters?.[0].comment?.shortText,
"another optional argument\n"
);
},

gh869(project) {
const classFoo = project.children?.find(
(r) => r.name === "Foo" && r.kind === ReflectionKind.Class
Expand Down Expand Up @@ -290,4 +306,10 @@ export const issueTests: {
ok(project.children![0].kind === ReflectionKind.Reference);
ok(project.children![1].kind !== ReflectionKind.Reference);
},

gh1804(project) {
const foo = query(project, "foo");
ok(foo?.signatures?.[0].parameters?.[0].flags.isOptional);
equal(foo?.signatures?.[0].parameters?.[0].type?.toString(), "number");
},
};