forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BaseControl: Auto-generate readme (WordPress#66500)
* Get subcomponent descriptions * Add BaseControl.VisualLabel as Storybook subcomponent * Display `children` as required prop * BaseControl: Auto-generate readme * Fixup * Fixup Co-authored-by: mirka <[email protected]> Co-authored-by: ciampo <[email protected]> Co-authored-by: tyxla <[email protected]>
- Loading branch information
1 parent
1c4c606
commit 3975cff
Showing
6 changed files
with
149 additions
and
51 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
bin/api-docs/gen-components-docs/get-subcomponent-descriptions.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import fs from 'node:fs/promises'; | ||
import babel from '@babel/core'; | ||
import { parse as commentParser } from 'comment-parser'; | ||
|
||
/** | ||
* Try to get subcomponent descriptions from the main component Object.assign() call. | ||
*/ | ||
export async function getDescriptionsForSubcomponents( | ||
filePath, | ||
mainComponentName | ||
) { | ||
const fileContent = await fs.readFile( filePath, 'utf8' ); | ||
const parsedFile = babel.parse( fileContent, { | ||
filename: filePath, | ||
} ); | ||
const mainComponent = parsedFile.program.body | ||
.filter( ( node ) => node.type === 'ExportNamedDeclaration' ) | ||
.flatMap( ( node ) => node.declaration?.declarations ) | ||
.find( ( node ) => node?.id.name === mainComponentName ); | ||
|
||
if ( | ||
! ( | ||
// If the main component export has `Object.assign( ... )` | ||
( | ||
mainComponent?.init?.type === 'CallExpression' && | ||
mainComponent?.init?.callee?.object?.name === 'Object' && | ||
mainComponent?.init?.callee?.property?.name === 'assign' | ||
) | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
const properties = mainComponent?.init?.arguments[ 1 ]?.properties.map( | ||
( node ) => [ | ||
node.key.name, | ||
commentParser( `/*${ node.leadingComments?.[ 0 ].value }*/`, { | ||
spacing: 'preserve', | ||
} )?.[ 0 ]?.description, | ||
] | ||
); | ||
return Object.fromEntries( properties ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"$schema": "../../schemas/docs-manifest.json", | ||
"displayName": "BaseControl", | ||
"filePath": "./index.tsx", | ||
"subcomponents": [ | ||
{ | ||
"displayName": "VisualLabel", | ||
"preferredDisplayName": "BaseControl.VisualLabel", | ||
"filePath": "./index.tsx" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters