Skip to content

feat: Add --raw-jsdoc option to include full JSDoc in schema #2224

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

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ By default, the command-line generator will use the `tsconfig.json` file in the
-e, --expose <expose> Type exposing (choices: "all", "none", "export", default: "export")
-j, --jsDoc <extended> Read JsDoc annotations (choices: "none", "basic", "extended", default: "extended")
--markdown-description Generate `markdownDescription` in addition to `description`.
--raw-jsdoc Include the full raw JSDoc comment as `rawJsDoc` in the schema.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be --raw-jsDoc since we have --jsDoc already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I would replace --jsDoc with --jsdoc or --js-doc, but that's up to you guys

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd too if we started over but let's not break it now.

--functions <functions> How to handle functions. `fail` will throw an error. `comment` will add a comment. `hide` will treat the function like a NeverType or HiddenType.
(choices: "fail", "comment", "hide", default: "comment")
--minify Minify generated schema (default: false)
Expand Down
2 changes: 1 addition & 1 deletion factory/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function createParser(program: ts.Program, config: CompletedConfig, augme
if (config.jsDoc === "extended") {
return new AnnotatedNodeParser(
nodeParser,
new ExtendedAnnotationsReader(typeChecker, extraTags, config.markdownDescription),
new ExtendedAnnotationsReader(typeChecker, extraTags, config.markdownDescription, config.rawJsDoc),
);
} else if (config.jsDoc === "basic") {
return new AnnotatedNodeParser(nodeParser, new BasicAnnotationsReader(extraTags));
Expand Down
35 changes: 25 additions & 10 deletions src/AnnotationsReader/ExtendedAnnotationsReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import json5 from "json5";
import type ts from "typescript";
import type { Annotations } from "../Type/AnnotatedType.js";
import { symbolAtNode } from "../Utils/symbolAtNode.js";
import { getRawJsDoc } from "../Utils/getRawJsDoc.js";
import { BasicAnnotationsReader } from "./BasicAnnotationsReader.js";

export class ExtendedAnnotationsReader extends BasicAnnotationsReader {
public constructor(
private typeChecker: ts.TypeChecker,
extraTags?: Set<string>,
private markdownDescription?: boolean,
private rawJsDoc?: boolean,
) {
super(extraTags);
}
Expand Down Expand Up @@ -44,21 +46,34 @@ export class ExtendedAnnotationsReader extends BasicAnnotationsReader {
return undefined;
}

const annotations: { description?: string; markdownDescription?: string; rawJsDoc?: string } = {};

const comments: ts.SymbolDisplayPart[] = symbol.getDocumentationComment(this.typeChecker);
if (!comments || !comments.length) {
return undefined;
}

const markdownDescription = comments
.map((comment) => comment.text)
.join(" ")
.replace(/\r/g, "")
.trim();
if (comments && comments.length) {
const markdownDescription = comments
.map((comment) => comment.text)
.join(" ")
.replace(/\r/g, "")
.trim();

const description = markdownDescription.replace(/(?<=[^\n])\n(?=[^\n*-])/g, " ").trim();
annotations.description = markdownDescription.replace(/(?<=[^\n])\n(?=[^\n*-])/g, " ").trim();

if (this.markdownDescription) {
annotations.markdownDescription = markdownDescription;
}
}

return this.markdownDescription ? { description, markdownDescription } : { description };
if (this.rawJsDoc) {
const rawJsDoc = getRawJsDoc(node)?.trim();
if (rawJsDoc) {
annotations.rawJsDoc = rawJsDoc;
}
}

return Object.keys(annotations).length ? annotations : undefined;
}

private getTypeAnnotation(node: ts.Node): Annotations | undefined {
const symbol = symbolAtNode(node);
if (!symbol) {
Expand Down
2 changes: 2 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Config {
topRef?: boolean;
jsDoc?: "none" | "extended" | "basic";
markdownDescription?: boolean;
rawJsDoc?: boolean;
sortProps?: boolean;
strictTuples?: boolean;
skipTypeCheck?: boolean;
Expand All @@ -27,6 +28,7 @@ export const DEFAULT_CONFIG: Omit<Required<Config>, "path" | "type" | "schemaId"
topRef: true,
jsDoc: "extended",
markdownDescription: false,
rawJsDoc: false,
sortProps: true,
strictTuples: false,
skipTypeCheck: false,
Expand Down
39 changes: 39 additions & 0 deletions src/Utils/getRawJsDoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import ts from "typescript";

export function getRawJsDoc(node: ts.Node): string | undefined {
const sourceFile = node.getSourceFile();
const jsDocNodes = ts.getJSDocCommentsAndTags(node);

if (!jsDocNodes || jsDocNodes.length === 0) {
return undefined;
}

let rawText = "";

for (const jsDoc of jsDocNodes) {
rawText += jsDoc.getFullText(sourceFile) + "\n";
}

rawText = rawText.trim();

return getTextWithoutStars(rawText).trim();
}

function getTextWithoutStars(inputText: string) {
const innerTextWithStars = inputText.replace(/^\/\*\*[^\S\n]*\n?/, "").replace(/(\r?\n)?[^\S\n]*\*\/$/, "");

return innerTextWithStars
.split(/\n/)
.map((line) => {
const trimmedLine = line.trimStart();

if (trimmedLine[0] !== "*") {
return line;
}

const textStartPos = trimmedLine[1] === " " ? 2 : 1;

return trimmedLine.substring(textStartPos);
})
.join("\n");
}
18 changes: 17 additions & 1 deletion test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ function assertSchema(
expect(typeof actual).toBe("object");
expect(actual).toEqual(expected);

const keywords: string[] = [];
if (config.markdownDescription) keywords.push("markdownDescription");
if (config.rawJsDoc) keywords.push("rawJsDoc");

const validator = new Ajv({
// skip full check if we are not encoding refs
validateFormats: config.encodeRefs === false ? undefined : true,
keywords: config.markdownDescription ? ["markdownDescription"] : undefined,
keywords: keywords.length ? keywords : undefined,
});

addFormats(validator);
Expand Down Expand Up @@ -341,6 +345,18 @@ describe("config", () => {
markdownDescription: true,
}),
);
it(
"jsdoc-raw",
assertSchema("jsdoc-raw", {
type: "MyObject",
expose: "export",
topRef: false,
jsDoc: "extended",
sortProps: true,
markdownDescription: true,
rawJsDoc: true,
}),
);
it(
"tsconfig-support",
assertSchema(
Expand Down
169 changes: 169 additions & 0 deletions test/config/jsdoc-raw/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/**
* @title Raw Test Schema Interface
* @description Top-level interface: This interface is used to test the rawJsDoc output.
* It includes various formatting quirks, inline tags such as {@link SomeReference}, and multiple JSDoc sections.
*
* @markdownDescription **Markdown version:** Markdown description which should be <b>preserved</b>
*
* Additional info: Top-level details should be completely preserved in raw form.
*/
export interface MyObject {
/**
* @title Single-line Title and Description
* @description Single-line comment for raw extraction.
*/
singleLine: string;

/**
* @title Multiline Field Title
* @description This is a multiline description.
* It spans multiple lines to test the preservation of newlines.
*
* @note 123
* @format date-time
*/
multilineField: number;

/**
* This field has a comment without explicit tags.
* It includes an inline reference: {@link ExampleReference} and extra text on several lines.
*
* The raw output should preserve this whole block as is.
*/
noTagField: boolean;

/**
* @title Field with Special Formatting
* @description Extra spaces and indentation should be preserved.
*
* @pattern /^\w+$/
*/
specialFormat: string;

/**
* Some initial descriptive text that is not tagged.
*
* @description Field with initial untagged text followed by a tag.
* @default 42
*
* Further comments in the same block should be preserved entirely.
*/
initialText: number;

/**
* Some *code block*:
* ```yaml
* name: description
* length: 42
* ```
*
* Some list:
* - one
* - two
* - and three...
*
* @description This field tests `inline code` and <b>bold text</b>.
*
* Also includes an inline link: {@link https://example.com} and additional commentary.
*/
markdownField: string;

/**
* @title Tag Only Field
* @note Only raw content should be available.
* @customTag Tag only content!
*/
tagOnlyField: string;

/**
* @title Tag Only Field
* @description
* @note Only raw content should be available.
* @customTag Tag only content!
*/
tagOnlyFieldWithDescription: string;

/** Some text */
oneLineJsDoc: string;

/** Some *text* - {@link https://example.com} - @see the `link` */
oneLineJsDocComplex: string;

/** */
emptyJsDoc1?: null;

/**
*
*/
emptyJsDoc2?: null;

noJsDoc?: null;

/**
* Some ignored comment description
*
* @description Export field description
* @default {"length": 10}
* @nullable
*/
exportString: MyExportString;
/**
* @description Export field description
* @default "private"
*/
privateString: MyPrivateString;

/**
* @title Non empty array
*/
numberArray: MyNonEmptyArray<number>;

/**
* @nullable
*/
number: number;

/**
* Some more examples:
* ```yaml
* name: description
* length: 42
* ```
*/
description: InheritedExample["description"];

/**
* @default ""
*/
inheritedDescription: InheritedExample["description"];
}

/**
* @title My export string
*/
export type MyExportString = string;
/**
* @title My private string
*/
type MyPrivateString = string;
/**
* @minItems 1
*/
export type MyNonEmptyArray<T> = T[];

/**
* @title Inherited Example Interface
* @description This interface is used to test inherited descriptions.
*/
export interface InheritedExample {
/**
* This is an inherited description.
*
* It may include multiple at-tags:
* @title Inherited Title
* @description Inherited description text.
*
* It contains an inline link: {@link https://example.com} and additional commentary.
*/
description: string;
}
Loading