Skip to content

Commit c6aea02

Browse files
committed
fix: parse standalong jsdoc tags
1 parent 055b19c commit c6aea02

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

packages/openapi-generator/src/jsdoc.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,27 @@ export function parseCommentBlock(comment: Block): JSDoc {
2525
throw new Error('@example contains invalid JSON');
2626
else continue;
2727
}
28-
} else {
29-
if (summary.length === 0) {
30-
if (line.tokens.description === '') {
31-
continue;
28+
} else if (line.tokens.tag !== undefined && line.tokens.tag.length > 0) {
29+
if (line.tokens.tag === '@example') {
30+
tags['example'] = line.source.split('@example')[1]?.trim();
31+
if (tags['example'].startsWith('{') || tags['example'].startsWith('[')) {
32+
try {
33+
tags['example'] = JSON.parse(tags['example']);
34+
} catch (e) {
35+
writingExample = true;
36+
}
3237
}
33-
summary = line.tokens.description;
3438
} else {
35-
if (line.tokens.tag !== undefined && line.tokens.tag.length > 0) {
36-
if (line.tokens.tag === '@example') {
37-
tags['example'] = line.source.split('@example')[1]?.trim();
38-
if (tags['example'].startsWith('{') || tags['example'].startsWith('[')) {
39-
try {
40-
tags['example'] = JSON.parse(tags['example']);
41-
} catch (e) {
42-
writingExample = true;
43-
}
44-
}
45-
} else
46-
tags[line.tokens.tag.slice(1)] =
47-
`${line.tokens.name} ${line.tokens.description}`.trim();
48-
} else {
49-
description = `${description ?? ''}\n${line.tokens.description}`;
50-
}
39+
tags[line.tokens.tag.slice(1)] =
40+
`${line.tokens.name} ${line.tokens.description}`.trim();
5141
}
42+
} else if (summary.length === 0) {
43+
if (line.tokens.description === '') {
44+
continue;
45+
}
46+
summary = line.tokens.description;
47+
} else {
48+
description = `${description ?? ''}\n${line.tokens.description}`;
5249
}
5350
}
5451

packages/openapi-generator/test/jsdoc.test.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ test('parameter with a comment and an example string', () => {
157157
const comment = `
158158
/**
159159
* A variable with example
160-
*
160+
*
161161
* @example foo
162162
*/
163163
`;
@@ -176,7 +176,7 @@ test('parameter with a comment and an example object', () => {
176176
const comment = `
177177
/**
178178
* A variable with example
179-
*
179+
*
180180
* @example { "test": "foo" }
181181
*/
182182
`;
@@ -196,7 +196,7 @@ test('parameter with a comment and an example object (multi-line)', () => {
196196
const comment = `
197197
/**
198198
* A variable with example
199-
*
199+
*
200200
* @example {
201201
* "test": "foo"
202202
* }
@@ -218,7 +218,7 @@ test('parameter with a comment and an example array', () => {
218218
const comment = `
219219
/**
220220
* A variable with example
221-
*
221+
*
222222
* @example ["foo", "bar", "baz"]
223223
*/
224224
`;
@@ -238,7 +238,7 @@ test('parameter with a comment and an invalid example object', () => {
238238
const comment = `
239239
/**
240240
* A variable with example
241-
*
241+
*
242242
* @example { "test": "foo"
243243
*/
244244
`;
@@ -252,7 +252,7 @@ test('parameter with a comment and an invalid example object (multi-line)', () =
252252
const comment = `
253253
/**
254254
* A variable with example
255-
*
255+
*
256256
* @example {
257257
* "test": "foo"
258258
*/
@@ -267,7 +267,7 @@ test('parameter with a comment and an invalid example array', () => {
267267
const comment = `
268268
/**
269269
* A variable with example
270-
*
270+
*
271271
* @example ["foo", "bar", "baz"
272272
*/
273273
`;
@@ -281,7 +281,7 @@ test('parameter with a comment, an example object and a tag', () => {
281281
const comment = `
282282
/**
283283
* A variable with example
284-
*
284+
*
285285
* @example { "test": "foo" }
286286
* @tag api.example.test
287287
*/
@@ -303,7 +303,7 @@ test('parameter with a comment, an example object (multi-line) and a tag', () =>
303303
const comment = `
304304
/**
305305
* A variable with example
306-
*
306+
*
307307
* @example {
308308
* "test": "foo"
309309
* }
@@ -322,3 +322,19 @@ test('parameter with a comment, an example object (multi-line) and a tag', () =>
322322

323323
assert.deepEqual(parseJSDoc(comment), expected);
324324
});
325+
326+
test('a standalone tag with no summary or description', () => {
327+
const comment = `
328+
/**
329+
* @tag
330+
*/
331+
`;
332+
333+
const expected: JSDoc = {
334+
tags: {
335+
tag: '',
336+
},
337+
};
338+
339+
assert.deepEqual(parseJSDoc(comment), expected);
340+
});

0 commit comments

Comments
 (0)