Skip to content
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

Fix parsing of standalong jsdoc tags #764

Merged
merged 1 commit into from
May 13, 2024
Merged
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
39 changes: 18 additions & 21 deletions packages/openapi-generator/src/jsdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,27 @@ export function parseCommentBlock(comment: Block): JSDoc {
throw new Error('@example contains invalid JSON');
else continue;
}
} else {
if (summary.length === 0) {
if (line.tokens.description === '') {
continue;
} else if (line.tokens.tag !== undefined && line.tokens.tag.length > 0) {
if (line.tokens.tag === '@example') {
tags['example'] = line.source.split('@example')[1]?.trim();
if (tags['example'].startsWith('{') || tags['example'].startsWith('[')) {
try {
tags['example'] = JSON.parse(tags['example']);
} catch (e) {
writingExample = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

thought: This ad-hoc parsing is starting to get extremely hairy! Thank goodness for our test suite.

I wonder if an approach using something structured, like parser combinators, might offer any benefits?

}
summary = line.tokens.description;
} else {
if (line.tokens.tag !== undefined && line.tokens.tag.length > 0) {
if (line.tokens.tag === '@example') {
tags['example'] = line.source.split('@example')[1]?.trim();
if (tags['example'].startsWith('{') || tags['example'].startsWith('[')) {
try {
tags['example'] = JSON.parse(tags['example']);
} catch (e) {
writingExample = true;
}
}
} else
tags[line.tokens.tag.slice(1)] =
`${line.tokens.name} ${line.tokens.description}`.trim();
} else {
description = `${description ?? ''}\n${line.tokens.description}`;
}
tags[line.tokens.tag.slice(1)] =
`${line.tokens.name} ${line.tokens.description}`.trim();
}
} else if (summary.length === 0) {
if (line.tokens.description === '') {
continue;
}
summary = line.tokens.description;
} else {
description = `${description ?? ''}\n${line.tokens.description}`;
}
}

Expand Down
34 changes: 25 additions & 9 deletions packages/openapi-generator/test/jsdoc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ test('parameter with a comment and an example string', () => {
const comment = `
/**
* A variable with example
*
*
* @example foo
*/
`;
Expand All @@ -176,7 +176,7 @@ test('parameter with a comment and an example object', () => {
const comment = `
/**
* A variable with example
*
*
* @example { "test": "foo" }
*/
`;
Expand All @@ -196,7 +196,7 @@ test('parameter with a comment and an example object (multi-line)', () => {
const comment = `
/**
* A variable with example
*
*
* @example {
* "test": "foo"
* }
Expand All @@ -218,7 +218,7 @@ test('parameter with a comment and an example array', () => {
const comment = `
/**
* A variable with example
*
*
* @example ["foo", "bar", "baz"]
*/
`;
Expand All @@ -238,7 +238,7 @@ test('parameter with a comment and an invalid example object', () => {
const comment = `
/**
* A variable with example
*
*
* @example { "test": "foo"
*/
`;
Expand All @@ -252,7 +252,7 @@ test('parameter with a comment and an invalid example object (multi-line)', () =
const comment = `
/**
* A variable with example
*
*
* @example {
* "test": "foo"
*/
Expand All @@ -267,7 +267,7 @@ test('parameter with a comment and an invalid example array', () => {
const comment = `
/**
* A variable with example
*
*
* @example ["foo", "bar", "baz"
*/
`;
Expand All @@ -281,7 +281,7 @@ test('parameter with a comment, an example object and a tag', () => {
const comment = `
/**
* A variable with example
*
*
* @example { "test": "foo" }
* @tag api.example.test
*/
Expand All @@ -303,7 +303,7 @@ test('parameter with a comment, an example object (multi-line) and a tag', () =>
const comment = `
/**
* A variable with example
*
*
* @example {
* "test": "foo"
* }
Expand All @@ -322,3 +322,19 @@ test('parameter with a comment, an example object (multi-line) and a tag', () =>

assert.deepEqual(parseJSDoc(comment), expected);
});

test('a standalone tag with no summary or description', () => {
const comment = `
/**
* @tag
*/
`;

const expected: JSDoc = {
tags: {
tag: '',
},
};

assert.deepEqual(parseJSDoc(comment), expected);
});