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: better handling of union return types #121

Merged
merged 1 commit into from
Sep 23, 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
11 changes: 11 additions & 0 deletions src/__tests__/markdown-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,17 @@ foo`),
});
});

it('should helpfully error for badly formatted union return types', () => {
const customTokens = getTokens(
`Returns \`WebContents\` | \`string\` - A WebContents instance with the given ID.`,
);
expect(() => extractReturnType(customTokens)).toThrowErrorMatchingInlineSnapshot(`
"Found a return type declaration that appears to be declaring a type union (A | B) but in the incorrect format. Type unions must be fully enclosed in backticks. For instance, instead of \`A\` | \`B\` you should specify \`A | B\`.
Specifically this error was encountered here:
"Returns \`WebContents\` | \`string\` - A WebContents instance with the given ID."..."
`);
});

it('should handle return types with no descriptions', () => {
const printerTokens = getTokens(`Returns [\`PrinterInfo[]\`](structures/printer-info.md)`);
const printerRet = extractReturnType(printerTokens);
Expand Down
8 changes: 8 additions & 0 deletions src/markdown-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,14 @@ export const extractReturnType = (
} catch {}
}

if (parsedDescription.trim().startsWith('|')) {
throw new Error(
`Found a return type declaration that appears to be declaring a type union (A | B) but in the incorrect format. Type unions must be fully enclosed in backticks. For instance, instead of \`A\` | \`B\` you should specify \`A | B\`.\nSpecifically this error was encountered here:\n "${rawDescription
.trim()
.slice(0, 100)}"...`,
);
}

return {
parsedDescription: parsedDescription.trim(),
parsedReturnType: rawTypeToTypeInformation(rawReturnType, parsedDescription, typedKeys),
Expand Down