Skip to content

Commit e1ce2d6

Browse files
authored
Do not append trailing spaces to JSDoc tags (#1231)
* Fix pnpm format script * Do not append trailing spaces to JSDoc tags * Further remove trailing spaces * Update examples
1 parent 7d64d73 commit e1ce2d6

File tree

9 files changed

+20083
-20016
lines changed

9 files changed

+20083
-20016
lines changed

.changeset/funny-lies-tease.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Do not append trailing spaces to JSDoc tags

packages/openapi-typescript/examples/digital-ocean-api.ts

+1,549-1,549
Large diffs are not rendered by default.

packages/openapi-typescript/examples/github-api-next.ts

+7,746-7,746
Large diffs are not rendered by default.

packages/openapi-typescript/examples/github-api.ts

+7,607-7,607
Large diffs are not rendered by default.

packages/openapi-typescript/examples/octokit-ghes-3.6-diff-to-api.ts

+1,029-1,029
Large diffs are not rendered by default.

packages/openapi-typescript/examples/stripe-api.ts

+2,073-2,073
Large diffs are not rendered by default.

packages/openapi-typescript/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
"build": "del dist && tsc -p tsconfig.build.json",
4242
"dev": "tsc -p tsconfig.build.json --watch",
4343
"download:schemas": "vite-node ./scripts/download-schemas.ts",
44-
"format": "pnpm run prettier -w .",
44+
"format": "prettier --write 'src/**/*'",
4545
"lint": "run-p -s lint:*",
46-
"lint:js": "eslint \"src/**/*.{js,ts}\"",
47-
"lint:prettier": "prettier --check \"src/**/*\"",
46+
"lint:js": "eslint 'src/**/*.{js,ts}'",
47+
"lint:prettier": "prettier --check 'src/**/*'",
4848
"prepare": "pnpm run build",
4949
"test": "run-p -s test:*",
5050
"test:js": "vitest run",

packages/openapi-typescript/src/utils.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ export function getSchemaObjectComment(v: CommentObject, indentLv?: number): str
5555
const output: string[] = [];
5656

5757
// * Not JSDOC tags: [title, format]
58-
if (v.title) output.push(`${v.title} `);
59-
if (v.summary) output.push(`${v.summary} `);
60-
if (v.format) output.push(`Format: ${v.format} `);
58+
if (v.title) output.push(v.title);
59+
if (v.summary) output.push(v.summary);
60+
if (v.format) output.push(`Format: ${v.format}`);
6161

6262
// * JSDOC tags without value
6363
// 'Deprecated' without value
64-
if (v.deprecated) output.push(`@deprecated `);
64+
if (v.deprecated) output.push("@deprecated");
6565

6666
// * JSDOC tags with value
6767
const supportedJsDocTags: (keyof CommentObject)[] = ["description", "default", "example"];
@@ -74,11 +74,11 @@ export function getSchemaObjectComment(v: CommentObject, indentLv?: number): str
7474
continue;
7575
}
7676
const serialized = typeof v[field] === "object" ? JSON.stringify(v[field], null, 2) : v[field];
77-
output.push(`@${field} ${serialized} `);
77+
output.push(`@${field} ${serialized}`);
7878
}
7979

8080
// * JSDOC 'Constant' without value
81-
if ("const" in v) output.push(`@constant `);
81+
if ("const" in v) output.push("@constant");
8282

8383
// * JSDOC 'Enum' with type
8484
if (v.enum) {
@@ -99,8 +99,14 @@ export function comment(text: string, indentLv?: number): string {
9999
if (!commentText.includes("\n")) return `/** ${commentText} */`;
100100

101101
// if multi-line comment
102-
const ln = indent(" * ", indentLv ?? 0);
103-
return ["/**", `${ln}${commentText.replace(LB_RE, `\n${ln}`)}`, indent(" */", indentLv ?? 0)].join("\n");
102+
const star = indent(" *", indentLv ?? 0);
103+
104+
const body = commentText.split(LB_RE).map((ln) => {
105+
ln = ln.trimEnd();
106+
return ln.length > 0 ? `${star} ${ln}` : star;
107+
});
108+
109+
return ["/**", body.join("\n"), indent(" */", indentLv ?? 0)].join("\n");
104110
}
105111

106112
/** handle any valid $ref */

packages/openapi-typescript/test/utils.test.ts

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { bench } from "vitest";
2-
import { escObjKey, parseRef, tsIntersectionOf, tsUnionOf } from "../src/utils.js";
2+
import { comment, escObjKey, getSchemaObjectComment, parseRef, tsIntersectionOf, tsUnionOf } from "../src/utils.js";
33

44
describe("utils", () => {
55
describe("tsUnionOf", () => {
@@ -112,4 +112,60 @@ describe("utils", () => {
112112
expect(escObjKey("_ref_")).toStrictEqual("_ref_");
113113
});
114114
});
115+
116+
describe("comment", () => {
117+
it("basic", () => {
118+
expect(comment("A comment")).toStrictEqual("/** A comment */");
119+
expect(comment("A multi-line \n\n comment")).toStrictEqual(
120+
// prettier-ignore
121+
"/**\n" +
122+
" * A multi-line\n" +
123+
" *\n" +
124+
" * comment\n"+
125+
" */"
126+
);
127+
});
128+
});
129+
130+
describe("getSchemaObjectComment", () => {
131+
it("object with 1 property", () => {
132+
expect(
133+
getSchemaObjectComment({
134+
title: "A title",
135+
})
136+
).toStrictEqual("/** A title */");
137+
});
138+
139+
it("object with 2 properties", () => {
140+
expect(
141+
getSchemaObjectComment({
142+
title: "A title",
143+
description: "A description",
144+
})
145+
).toStrictEqual(
146+
// prettier-ignore
147+
"/**\n" +
148+
" * A title\n" +
149+
" * @description A description\n"+
150+
" */"
151+
);
152+
});
153+
154+
it("object with a multi-line property", () => {
155+
expect(
156+
getSchemaObjectComment({
157+
title: "A title",
158+
description: "A multi-line \n\n description",
159+
})
160+
).toStrictEqual(
161+
// prettier-ignore
162+
"/**\n" +
163+
" * A title\n" +
164+
" * @description A multi-line\n" +
165+
" *\n" +
166+
" * description\n" +
167+
" */"
168+
);
169+
});
170+
});
115171
});

0 commit comments

Comments
 (0)