-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Components: Warn private API in auto-generated readmes #68317
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import fs from 'node:fs/promises'; | ||
import babel from '@babel/core'; | ||
|
||
/** | ||
* Returns `meta.tags` from a Storybook file. | ||
* | ||
* @param {string} filePath | ||
* @return {Promise<string[]>} Array of tags. | ||
*/ | ||
export async function getTagsFromStorybook( filePath ) { | ||
const fileContent = await fs.readFile( filePath, 'utf8' ); | ||
const parsedFile = babel.parse( fileContent, { | ||
filename: filePath, | ||
} ); | ||
|
||
const meta = parsedFile.program.body.find( | ||
( node ) => | ||
node.type === 'VariableDeclaration' && | ||
node.declarations[ 0 ].id.name === 'meta' | ||
); | ||
|
||
return ( | ||
meta.declarations[ 0 ].init.properties | ||
.find( ( node ) => node.key.name === 'tags' ) | ||
?.value.elements.map( ( node ) => node.value ) ?? [] | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,19 @@ function normalizeTrailingNewline( str ) { | |
return str.replace( /\n*$/, '\n' ); | ||
} | ||
|
||
export function generateMarkdownDocs( { typeDocs, subcomponentTypeDocs } ) { | ||
export function generateMarkdownDocs( { | ||
typeDocs, | ||
subcomponentTypeDocs, | ||
tags, | ||
} ) { | ||
const mainDocsJson = [ | ||
{ h1: typeDocs.displayName }, | ||
'<!-- This file is generated automatically and cannot be edited directly. Make edits via TypeScript types and TSDocs. -->', | ||
tags.includes( 'status-private' ) && [ | ||
{ | ||
p: '🔒 This component is locked as a [private API](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-private-apis/). We do not yet recommend using this outside of the Gutenberg project.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In an ideal world, and accoding to @jsnajdr's latest suggestions, a 3rd party developer simply should be unable to use private APIs outside of Gutenberg. |
||
}, | ||
], | ||
Comment on lines
+32
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using normal Markdown instead of a |
||
{ | ||
p: `<p class="callout callout-info">See the <a href="https://wordpress.github.io/gutenberg/?path=/docs/components-${ typeDocs.displayName.toLowerCase() }--docs">WordPress Storybook</a> for more detailed, interactive documentation.</p>`, | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
|
||
<!-- This file is generated automatically and cannot be edited directly. Make edits via TypeScript types and TSDocs. --> | ||
|
||
🔒 This component is locked as a [private API](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-private-apis/). We do not yet recommend using this outside of the Gutenberg project. | ||
|
||
|
||
Comment on lines
+6
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant newlines will be fixed in #68301. |
||
<p class="callout callout-info">See the <a href="https://wordpress.github.io/gutenberg/?path=/docs/components-badge--docs">WordPress Storybook</a> for more detailed, interactive documentation.</p> | ||
|
||
## Props | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,12 @@ import type { Meta, StoryObj } from '@storybook/react'; | |
*/ | ||
import Badge from '..'; | ||
|
||
const meta = { | ||
const meta: Meta< typeof Badge > = { | ||
component: Badge, | ||
title: 'Components/Containers/Badge', | ||
id: 'components-badge', | ||
tags: [ 'status-private' ], | ||
} satisfies Meta< typeof Badge >; | ||
}; | ||
Comment on lines
+11
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalizing this declaration to our standard format (not TS There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for this 👍 The |
||
|
||
export default meta; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These
bin
scripts aren't set up to process TS files yet (e.g.index.story.tsx
files), so we can't read data fromimport()
edmeta
objects. At the moment I think it's simpler to do a quick AST parse than set up TS transpilation just for this, but we can maybe revisit when there are more use cases.