Skip to content

Commit

Permalink
Deduplicate generateApiComponents dispatch table entries (#1315)
Browse files Browse the repository at this point in the history
This PR refactors the dispatch table used to prepare the props of each
API component in order to simplify the `generateApiComponents.ts`
script. The functions for "functions" and "methods" cannot be merged
trivially because they check in different ways the `isDedicatedPage`
prop. Using the same method for that gives us false positives on pages
like [this](https://docs.quantum.ibm.com/api/qiskit/0.36/execute).
Moreover, functions don't need any header in those cases.

Thanks @Eric-Arellano for the idea on #1283

---------

Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com>
  • Loading branch information
arnaucasau and Eric-Arellano committed May 7, 2024
1 parent c245ed2 commit 63295ee
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions scripts/lib/api/generateApiComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,29 @@ function prepareProps(
apiType: ApiType,
id: string,
): ComponentProps {
const prepClassOrException = () =>
prepareClassOrExceptionProps($, $child, $dl, githubSourceLink, id);
const prepFunction = () =>
prepareFunctionProps($, $child, $dl, githubSourceLink, id);
const prepMethod = () =>
prepareMethodProps($, $child, $dl, priorApiType, githubSourceLink, id);
const prepAttributeOrProperty = () =>
prepareAttributeOrPropertyProps(
$,
$child,
$dl,
priorApiType,
githubSourceLink,
id,
);

const preparePropsPerApiType: Record<string, () => ComponentProps> = {
class: () =>
prepareClassOrExceptionProps($, $child, $dl, githubSourceLink, id),
property: () =>
prepareAttributeProps($, $child, $dl, priorApiType, githubSourceLink, id),
method: () =>
prepareMethodProps($, $child, $dl, priorApiType, githubSourceLink, id),
attribute: () =>
prepareAttributeProps($, $child, $dl, priorApiType, githubSourceLink, id),
function: () => prepareFunctionProps($, $child, $dl, id, githubSourceLink),
exception: () =>
prepareClassOrExceptionProps($, $child, $dl, githubSourceLink, id),
class: prepClassOrException,
exception: prepClassOrException,
property: prepAttributeOrProperty,
attribute: prepAttributeOrProperty,
method: prepMethod,
function: prepFunction,
};

const githubSourceLink = prepareGitHubLink($child, apiType === "method");
Expand Down Expand Up @@ -173,7 +184,7 @@ function prepareMethodProps(
return props;
}

function prepareAttributeProps(
function prepareAttributeOrPropertyProps(
$: CheerioAPI,
$child: Cheerio<any>,
$dl: Cheerio<any>,
Expand Down Expand Up @@ -233,8 +244,8 @@ function prepareFunctionProps(
$: CheerioAPI,
$child: Cheerio<any>,
$dl: Cheerio<any>,
id: string,
githubSourceLink: string | undefined,
id: string,
): ComponentProps {
const props = {
id,
Expand Down

0 comments on commit 63295ee

Please sign in to comment.