Skip to content

Commit fc8da34

Browse files
fix: better handling of union return types (#121)
1 parent 0a1bd84 commit fc8da34

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/__tests__/markdown-helpers.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,17 @@ foo`),
474474
});
475475
});
476476

477+
it('should helpfully error for badly formatted union return types', () => {
478+
const customTokens = getTokens(
479+
`Returns \`WebContents\` | \`string\` - A WebContents instance with the given ID.`,
480+
);
481+
expect(() => extractReturnType(customTokens)).toThrowErrorMatchingInlineSnapshot(`
482+
"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\`.
483+
Specifically this error was encountered here:
484+
"Returns \`WebContents\` | \`string\` - A WebContents instance with the given ID."..."
485+
`);
486+
});
487+
477488
it('should handle return types with no descriptions', () => {
478489
const printerTokens = getTokens(`Returns [\`PrinterInfo[]\`](structures/printer-info.md)`);
479490
const printerRet = extractReturnType(printerTokens);

src/markdown-helpers.ts

+8
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,14 @@ export const extractReturnType = (
530530
} catch {}
531531
}
532532

533+
if (parsedDescription.trim().startsWith('|')) {
534+
throw new Error(
535+
`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
536+
.trim()
537+
.slice(0, 100)}"...`,
538+
);
539+
}
540+
533541
return {
534542
parsedDescription: parsedDescription.trim(),
535543
parsedReturnType: rawTypeToTypeInformation(rawReturnType, parsedDescription, typedKeys),

0 commit comments

Comments
 (0)