Skip to content

Commit 3a9b1bb

Browse files
committed
Do not append trailing spaces to JSDoc tags
1 parent b74f986 commit 3a9b1bb

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
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/src/utils.ts

+6-6
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) {

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

+55-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,58 @@ 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 comment")).toStrictEqual(
120+
// prettier-ignore
121+
"/**\n" +
122+
" * A multi-line \n" +
123+
" * comment\n"+
124+
" */"
125+
);
126+
});
127+
});
128+
129+
describe("getSchemaObjectComment", () => {
130+
it("object with 1 property", () => {
131+
expect(
132+
getSchemaObjectComment({
133+
title: "A title",
134+
})
135+
).toStrictEqual("/** A title */");
136+
});
137+
138+
it("object with 2 properties", () => {
139+
expect(
140+
getSchemaObjectComment({
141+
title: "A title",
142+
description: "A description",
143+
})
144+
).toStrictEqual(
145+
// prettier-ignore
146+
"/**\n" +
147+
" * A title\n" +
148+
" * @description A description\n"+
149+
" */"
150+
);
151+
});
152+
153+
it("object with a multi-line property", () => {
154+
expect(
155+
getSchemaObjectComment({
156+
title: "A title",
157+
description: "A multi-line \n description",
158+
})
159+
).toStrictEqual(
160+
// prettier-ignore
161+
"/**\n" +
162+
" * A title\n" +
163+
" * @description A multi-line \n" +
164+
" * description\n" +
165+
" */"
166+
);
167+
});
168+
});
115169
});

0 commit comments

Comments
 (0)