Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): stop rendering 'undefined' for bare sub-component doc entries (#2918)

EJ Hammond4bad2b3ba766
diff --git a/packages/cli/src/lib/component-format.mjs b/packages/cli/src/lib/component-format.mjs
index b4008294c8..58b2010993 100644
--- a/packages/cli/src/lib/component-format.mjs
+++ b/packages/cli/src/lib/component-format.mjs
@@ -36,6 +36,30 @@ function formatPropsTable(props) {
return lines.join('\n');
}
+/**
+ * Render a sub-component block (the `### Name` sections under "## Components").
+ *
+ * Sub-components are sometimes declared as a bare reference, e.g.
+ * `{name: 'XDSRadioListItem'}`, with their description/props documented in
+ * their own `.doc.mjs`. Without guarding, `comp.description` is `undefined`
+ * and was being printed literally as the string "undefined". Emit the
+ * description only when present, and when there are no inline props point the
+ * reader at the sub-component's own docs instead of a blank/`undefined` block.
+ */
+function formatSubComponent(comp) {
+ const out = [`### ${comp.name}\n`];
+ if (comp.description) {
+ out.push(comp.description + '\n');
+ }
+ const table = formatPropsTable(comp.props);
+ if (table) {
+ out.push(table + '\n');
+ } else {
+ out.push(`See \`xds component ${comp.name}\` for props and usage.\n`);
+ }
+ return out;
+}
+
/**
* Get the variant values for a given theming target, extracting them from
* the visualProps and the variant prop in the component's props list.
@@ -162,9 +186,7 @@ export function formatFull(docs, options = {}) {
if ('components' in docs) {
sections.push('## Components\n');
for (const comp of docs.components) {
- sections.push(`### ${comp.name}\n`);
- sections.push(comp.description + '\n');
- sections.push(formatPropsTable(comp.props) + '\n');
+ sections.push(...formatSubComponent(comp));
if (comp.examples?.length) {
for (const ex of comp.examples) {
if (ex.label) sections.push(`#### ${ex.label}\n`);
@@ -317,9 +339,7 @@ export function formatCompact(docs, componentName, importHint) {
if ('components' in docs) {
for (const comp of docs.components) {
- sections.push(`### ${comp.name}\n`);
- sections.push(comp.description + '\n');
- sections.push(formatPropsTable(comp.props) + '\n');
+ sections.push(...formatSubComponent(comp));
}
}
diff --git a/packages/cli/src/lib/component-format.test.mjs b/packages/cli/src/lib/component-format.test.mjs
new file mode 100644
index 0000000000..07948b20ff
--- /dev/null
+++ b/packages/cli/src/lib/component-format.test.mjs
@@ -0,0 +1,53 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+import {describe, it, expect} from 'vitest';
+import {formatFull} from './component-format.mjs';
+
+describe('formatFull sub-component rendering', () => {
+ // Regression guard: sub-components are sometimes declared as a bare
+ // reference, e.g. {name: 'XDSRadioListItem'}, with props/description in
+ // their own .doc.mjs. Previously `comp.description` was undefined and got
+ // printed as the literal string "undefined", and the props area was blank.
+ it('does not print "undefined" for a bare {name} sub-component', () => {
+ const docs = {
+ name: 'RadioList',
+ description: 'Radio group container.',
+ components: [{name: 'XDSRadioListItem'}],
+ };
+ const out = formatFull(docs);
+
+ expect(out).toContain('### XDSRadioListItem');
+ expect(out).not.toContain('undefined');
+ // Points the reader at the sub-component's own docs instead of a blank.
+ expect(out).toContain('xds component XDSRadioListItem');
+ });
+
+ it('renders a full props table for a sub-component that has inline props', () => {
+ const docs = {
+ name: 'ButtonGroup',
+ description: 'Groups buttons.',
+ components: [
+ {
+ name: 'XDSButtonGroup',
+ description: 'Connected button styling.',
+ props: [
+ {
+ name: 'label',
+ type: 'string',
+ description: 'Accessible label.',
+ required: true,
+ },
+ ],
+ },
+ ],
+ };
+ const out = formatFull(docs);
+
+ expect(out).toContain('### XDSButtonGroup');
+ expect(out).toContain('Connected button styling.');
+ expect(out).toContain('| `label` |');
+ expect(out).not.toContain('undefined');
+ // With real props, it should NOT emit the "see docs" pointer.
+ expect(out).not.toContain('xds component XDSButtonGroup');
+ });
+});