Skip to content

Commit

Permalink
Allow JSDoc tags in hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongarciah committed May 22, 2024
1 parent 60d512a commit 9611931
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"hookDescription": "Stabilizes the ListContext value for the MenuItem component, so it doesn't change when sibling items update.\n\n@param id The id of the MenuItem. If undefined, it will be generated with useId.\n@returns The stable ListContext value and the id of the MenuItem.",
"hookDescription": "Stabilizes the ListContext value for the MenuItem component, so it doesn't change when sibling items update.",
"parametersDescriptions": {},
"returnValueDescriptions": {}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"hookDescription": "Stabilizes the ListContext value for the Option component, so it doesn't change when sibling Options update.\n\n@param value The value of the Option.\n@returns The stable ListContext value.",
"hookDescription": "Stabilizes the ListContext value for the Option component, so it doesn't change when sibling Options update.",
"parametersDescriptions": {},
"returnValueDescriptions": {}
}
18 changes: 16 additions & 2 deletions packages/api-docs-builder/ApiBuilders/HookApiBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import traverse from '@babel/traverse';
import { defaultHandlers, parse as docgenParse, ReactDocgenApi } from 'react-docgen';
import kebabCase from 'lodash/kebabCase';
import upperFirst from 'lodash/upperFirst';
import { parse as parseDoctrine, Annotation } from 'doctrine';
import { renderMarkdown } from '@mui/internal-markdown';
import { ProjectSettings } from '../ProjectSettings';
import { computeApiDescription } from './ComponentApiBuilder';
Expand Down Expand Up @@ -108,7 +109,7 @@ export interface ReactApi extends ReactDocgenApi {
* *
* * - [useButton API](https://mui.com/base-ui/api/use-button/)
*/
async function annotateHookDefinition(api: ReactApi) {
async function annotateHookDefinition(api: ReactApi, hookJsdoc: Annotation) {
const HOST = 'https://mui.com';

const typesFilename = api.filename.replace(/\.js$/, '.d.ts');
Expand Down Expand Up @@ -285,6 +286,14 @@ async function annotateHookDefinition(api: ReactApi) {
})`,
);

if (hookJsdoc.tags.length > 0) {
markdownLines.push('');
}

hookJsdoc.tags.forEach((tag) => {
markdownLines.push(`@${tag.title}${tag.name ? ` ${tag.name} -` : ''} ${tag.description}`);
});

const jsdoc = `/**\n${markdownLines
.map((line) => (line.length > 0 ? ` * ${line}` : ` *`))
.join('\n')}\n */`;
Expand Down Expand Up @@ -546,6 +555,11 @@ export default async function generateHookApi(

const parameters = await extractInfoFromType(`${upperFirst(name)}Parameters`, project);
const returnValue = await extractInfoFromType(`${upperFirst(name)}ReturnValue`, project);
const hookJsdoc = parseDoctrine(reactApi.description);

// We override `reactApi.description` with `hookJsdoc.description` because
// the former can include JSDoc tags that we don't want to render in the docs.
reactApi.description = hookJsdoc.description;

// Ignore what we might have generated in `annotateHookDefinition`
const annotatedDescriptionMatch = reactApi.description.match(/(Demos|API):\r?\n\r?\n/);
Expand Down Expand Up @@ -588,7 +602,7 @@ export default async function generateHookApi(
await generateApiJson(apiPagesDirectory, reactApi);

// Add comment about demo & api links to the component hook file
await annotateHookDefinition(reactApi);
await annotateHookDefinition(reactApi, hookJsdoc);
}

return reactApi;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { ListContext, ListContextValue, ListItemState } from '../useList';
/**
* Stabilizes the ListContext value for the MenuItem component, so it doesn't change when sibling items update.
*
* @param id The id of the MenuItem. If undefined, it will be generated with useId.
* @returns The stable ListContext value and the id of the MenuItem.
*
* Demos:
*
* - [Menu](https://mui.com/base-ui/react-menu/#hooks)
*
* API:
*
* - [useMenuItemContextStabilizer API](https://mui.com/base-ui/react-menu/hooks-api/#use-menu-item-context-stabilizer)
*
* @param id - The id of the MenuItem. If undefined, it will be generated with useId.
* @returns The stable ListContext value and the id of the MenuItem.
*/
export function useMenuItemContextStabilizer(id: string | undefined) {
const listContext = React.useContext(ListContext as React.Context<ListContextValue<string>>);
Expand Down
6 changes: 3 additions & 3 deletions packages/mui-base/src/useOption/useOptionContextStabilizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { ListContext, ListContextValue } from '../useList';
/**
* Stabilizes the ListContext value for the Option component, so it doesn't change when sibling Options update.
*
* @param value The value of the Option.
* @returns The stable ListContext value.
*
* Demos:
*
* - [Select](https://mui.com/base-ui/react-select/#hooks)
*
* API:
*
* - [useOptionContextStabilizer API](https://mui.com/base-ui/react-select/hooks-api/#use-option-context-stabilizer)
*
* @param value - The value of the Option.
* @returns The stable ListContext value.
*/
export function useOptionContextStabilizer<OptionValue>(value: OptionValue) {
const listContext = React.useContext(ListContext as React.Context<ListContextValue<OptionValue>>);
Expand Down

0 comments on commit 9611931

Please sign in to comment.