Skip to content

Commit

Permalink
fix: better handling of union return types
Browse files Browse the repository at this point in the history
Fixes #93

It's non trivial to support this syntax so let's spit out a helpful error message instead.
  • Loading branch information
MarshallOfSound committed Sep 23, 2024
1 parent 0a1bd84 commit 8a71b50
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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

0 comments on commit 8a71b50

Please sign in to comment.