Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit 1bfc7c5

Browse files
committed
keep comment spacing for code block
1 parent b4b2fb2 commit 1bfc7c5

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/util/commentsToOpenApi.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,43 @@ function _(func: { (): void }): string {
99
.replace(/\r\n/g, '\n');
1010
}
1111

12+
describe('code blocks', () => {
13+
it('keeps spacing', () => {
14+
const comment = _(() => {
15+
/**
16+
* GET /
17+
* @description List API versions
18+
* ```xml
19+
* <Fun>
20+
* <InTheSun>😎</InTheSun>
21+
* </Fun>
22+
* ```
23+
* bye
24+
*
25+
* @response 200 - ok
26+
*/
27+
});
28+
29+
const expected = {
30+
paths: {
31+
'/': {
32+
get: {
33+
description:
34+
'List API versions\n```xml\n<Fun>\n <InTheSun>😎</InTheSun>\n</Fun>\n```\nbye',
35+
responses: {
36+
'200': {
37+
description: 'ok',
38+
},
39+
},
40+
},
41+
},
42+
},
43+
};
44+
const [specification] = commentsToOpenApi(comment).map((i) => i.spec);
45+
expect(specification).to.deep.equal(expected);
46+
});
47+
});
48+
1249
describe('commentsToOpenApi', () => {
1350
it('big stuff', () => {
1451
const comment = _(() => {

src/util/commentsToOpenApi.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function parseDescription(tag: any) {
104104
}
105105

106106
// remove the optional dash from the description.
107-
let description = tag.description.replace(/^- /, '');
107+
let description = tag.description.trim().replace(/^- /, '');
108108
if (description === '') {
109109
description = undefined;
110110
}
@@ -129,7 +129,7 @@ function tagsToObjects(tags: any[], verbose?: boolean) {
129129
nameAndDescription += parsedResponse.name;
130130
}
131131
if (parsedResponse.description) {
132-
nameAndDescription += ` ${parsedResponse.description}`;
132+
nameAndDescription += ` ${parsedResponse.description.trim()}`;
133133
}
134134

135135
switch (tag.tag) {
@@ -341,11 +341,11 @@ function commentsToOpenApi(
341341
): { spec: OpenApiObject; loc: number }[] {
342342
const openAPIRegex = /^(GET|PUT|POST|DELETE|OPTIONS|HEAD|PATCH|TRACE) \/.*$/;
343343

344-
const jsDocComments = parseComments(fileContents);
344+
const jsDocComments = parseComments(fileContents, { trim: false });
345345

346346
const filteredComments = jsDocComments
347347
.filter((comment) => {
348-
const validComment = openAPIRegex.test(comment.description);
348+
const validComment = openAPIRegex.test(comment.description.trim());
349349

350350
return validComment;
351351
})
@@ -364,8 +364,8 @@ function commentsToOpenApi(
364364
const [method, path] = comment.description.split(' ');
365365

366366
const pathsObject: PathsObject = {
367-
[path]: {
368-
[method.toLowerCase()]: {
367+
[path.trim()]: {
368+
[method.toLowerCase().trim()]: {
369369
...res,
370370
},
371371
},

0 commit comments

Comments
 (0)