Skip to content

Commit

Permalink
Remove default throws
Browse files Browse the repository at this point in the history
  • Loading branch information
JCQuintas committed Aug 16, 2024
1 parent acb938b commit f9267b1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 42 deletions.
19 changes: 2 additions & 17 deletions packages/api-docs-builder/ApiBuilders/ComponentApiBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ const attachPropsTable = (reactApi: ComponentReactApi) => {
const propErrors: Array<[propName: string, error: Error]> = [];
type Pair = [string, ComponentReactApi['propsTable'][string]];
const componentProps: ComponentReactApi['propsTable'] = _.fromPairs(
Object.entries(reactApi.props!).map(([propName, propDescriptor]): Pair => {
Object.entries(reactApi.props).map(([propName, propDescriptor]): Pair => {
let prop: DescribablePropDescriptor | null;
try {
prop = createDescribableProp(propDescriptor, propName);
Expand Down Expand Up @@ -645,8 +645,6 @@ export default async function generateComponentApi(
}

const filename = componentInfo.filename;
let reactApi: ComponentReactApi | undefined;

const options = {
savePropValueAsString: false,
shouldExtractLiteralValuesFromEnum: true,
Expand All @@ -655,20 +653,7 @@ export default async function generateComponentApi(
shouldIncludePropTagMap: true,
};

if (componentInfo.isSystemComponent || componentInfo.name === 'Grid2') {
try {
reactApi = componentDocToComponentApi(docgenParse(src, options)?.at(0));
} catch (error) {
// fallback to default logic if there is no `create*` definition.
if ((error as Error).message === 'No suitable component definition found.') {
reactApi = componentDocToComponentApi(docgenParse(src, options)?.at(0));
} else {
throw error;
}
}
} else {
reactApi = componentDocToComponentApi(docgenParse(src, options)?.at(0));
}
const reactApi = componentDocToComponentApi(docgenParse(componentInfo.filename, options)?.at(0));

if (!reactApi) {
throw new Error(`No suitable component definition found in ${filename}`);
Expand Down
27 changes: 4 additions & 23 deletions packages/api-docs-builder/utils/createDescribableProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function createDescribableProp(
prop: PropItem,
propName: string,
): DescribablePropDescriptor | null {
const { defaultValue, description, required, type } = prop;
const { defaultValue, description, required, type, tags } = prop;

const renderedDefaultValue = defaultValue?.value.replace(/\r?\n/g, '');
const renderDefaultValue = Boolean(
Expand All @@ -44,32 +44,13 @@ export default function createDescribableProp(
});

if (
annotation.description.trim() === '' ||
annotation.tags.some((tag) => tag.title === 'ignore')
description.trim() === '' ||
// @ts-expect-error empty object type
tags?.ignore
) {
return null;
}

if (defaultValue === undefined) {
// Assume that this prop:
// 1. Is typed by another component
// 2. Is forwarded to that component
// Then validation is handled by the other component.
// Though this does break down if the prop is used in other capacity in the implementation.
// So let's hope we don't make this mistake too often.
} else if (defaultValue !== undefined && renderDefaultValue) {
const shouldHaveDefaultAnnotation =
// Discriminator for polymorphism which is not documented at the component level.
// The documentation of `component` does not know in which component it is used.
propName !== 'component';

if (shouldHaveDefaultAnnotation) {
throw new Error(
`JSDoc @default annotation not found. Add \`@default ${defaultValue.value}\` to the JSDoc of this prop.`,
);
}
}

return {
annotation,
defaultValue: renderDefaultValue ? renderedDefaultValue! : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ export default function generatePropTypeDescription(type: PropItemType): string

case 'union':
return (
type.value
(type.value as { value: string }[])
.map((type2) => {
return generatePropTypeDescription(type2);
return escapeCell(type2.value);
})
// Display one value per line as it's better for visibility.
.join('<br>&#124;&nbsp;')
Expand Down

0 comments on commit f9267b1

Please sign in to comment.