Skip to content

Commit 26bbbda

Browse files
committed
Refactor
1 parent 41d93f3 commit 26bbbda

File tree

4 files changed

+108
-124
lines changed

4 files changed

+108
-124
lines changed

code/core/src/core-server/manifest.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,8 @@ function renderComponentCard(key: string, c: ComponentManifest, id: string) {
664664
: '';
665665

666666
// When there is no prop type error, try to read prop types from reactDocgen if present
667-
const hasDocgen = !a.hasPropTypeError && 'reactDocgen' in c && c.reactDocgen;
668-
const parsedDocgen = hasDocgen ? parseReactDocgen(c.reactDocgen) : undefined;
667+
const reactDocgen: any = !a.hasPropTypeError && 'reactDocgen' in c && c.reactDocgen;
668+
const parsedDocgen = reactDocgen ? parseReactDocgen(reactDocgen) : undefined;
669669
const propEntries = parsedDocgen ? Object.entries(parsedDocgen.props ?? {}) : [];
670670
const propTypesBadge =
671671
!a.hasPropTypeError && propEntries.length > 0
@@ -684,8 +684,10 @@ function renderComponentCard(key: string, c: ComponentManifest, id: string) {
684684
const optional = info?.required ? '' : '?';
685685
const defaultVal = (info?.defaultValue ?? '').trim();
686686
const def = defaultVal ? ` = ${defaultVal}` : '';
687-
const doc = description ? `/** ${description} */\n` : '';
688-
return `${doc}${propName}${optional}: ${t}${def}`;
687+
const doc =
688+
['/**', ...description.split('\n').map((line) => ` * ${line}`), ' */'].join('\n') +
689+
'\n';
690+
return `${description ? doc : ''}${propName}${optional}: ${t}${def}`;
689691
})
690692
.join('\n\n')
691693
: '';
@@ -757,6 +759,8 @@ function renderComponentCard(key: string, c: ComponentManifest, id: string) {
757759
<span class="ex-name">Prop types</span>
758760
<span class="badge ok">${propEntries.length} ${plural(propEntries.length, 'prop type')}</span>
759761
</div>
762+
<pre><code>File: ${reactDocgen?.definedInFile ? esc(reactDocgen.definedInFile) : ''}::${reactDocgen?.exportName ? esc(reactDocgen?.exportName) : ''}</code></pre>
763+
<pre><code>Props:</code></pre>
760764
<pre><code>${esc(propsCode)}</code></pre>
761765
</div>
762766
</div>`

code/renderers/react/src/componentManifest/generator.ts

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import {
1010
import path from 'pathe';
1111

1212
import { getCodeSnippet } from './generateCodeSnippet';
13-
import { getComponentImports } from './getComponentImports';
13+
import { getComponents, getImports } from './getComponentImports';
1414
import { extractJSDocInfo } from './jsdocTags';
15-
import { type DocObj, getReactDocgen } from './reactDocgen';
15+
import { type DocObj } from './reactDocgen';
1616
import { cachedFindUp, cachedReadFileSync, groupBy, invalidateCache, invariant } from './utils';
1717

1818
interface ReactComponentManifest extends ComponentManifest {
@@ -48,7 +48,7 @@ export const componentManifestGenerator: PresetPropertyFn<
4848
if (csf.meta.tags?.includes('!manifest')) {
4949
return;
5050
}
51-
let componentName = csf._meta?.component;
51+
const componentName = csf._meta?.component;
5252
const title = entry.title.replace(/\s+/g, '');
5353

5454
const id = entry.id.split('--')[0];
@@ -62,35 +62,19 @@ export const componentManifestGenerator: PresetPropertyFn<
6262
? JSON.parse(cachedReadFileSync(nearestPkg, 'utf-8') as string).name
6363
: undefined;
6464

65-
const fallbackImport =
66-
packageName && componentName ? `import { ${componentName} } from "${packageName}";` : '';
67-
const componentImports = getComponentImports({
65+
const components = getComponents({
6866
csf,
69-
packageName,
7067
storyFilePath: absoluteImportPath,
7168
});
7269

73-
const calculatedImports = componentImports.imports.join('\n').trim() ?? fallbackImport;
74-
75-
const component = componentImports.components.find((it) => {
76-
const nameMatch = componentName
77-
? it.componentName === componentName ||
78-
it.localImportName === componentName ||
79-
it.importName === componentName
80-
: false;
81-
const titleMatch = !componentName
82-
? (it.localImportName ? title.includes(it.localImportName) : false) ||
83-
(it.importName ? title.includes(it.importName) : false)
84-
: false;
85-
return nameMatch || titleMatch;
70+
const component = components.find((it) => {
71+
return componentName
72+
? [it.componentName, it.localImportName, it.importName].includes(componentName)
73+
: title.includes(it.componentName) ||
74+
(it.localImportName && title.includes(it.localImportName)) ||
75+
(it.importName && title.includes(it.importName));
8676
});
8777

88-
componentName ??=
89-
component?.componentName ?? component?.localImportName ?? component?.importName;
90-
91-
const componentPath = component?.path;
92-
const importName = component?.importName;
93-
9478
const stories = Object.keys(csf._stories)
9579
.map((storyName) => {
9680
const story = csf._stories[storyName];
@@ -104,7 +88,7 @@ export const componentManifestGenerator: PresetPropertyFn<
10488

10589
return {
10690
name: storyName,
107-
snippet: recast.print(getCodeSnippet(csf, storyName, componentName)).code,
91+
snippet: recast.print(getCodeSnippet(csf, storyName, component?.componentName)).code,
10892
description: finalDescription?.trim(),
10993
summary: tags.summary?.[0],
11094
};
@@ -118,24 +102,29 @@ export const componentManifestGenerator: PresetPropertyFn<
118102
})
119103
.filter((it) => it != null);
120104

105+
const fallbackImport =
106+
packageName && componentName ? `import { ${componentName} } from "${packageName}";` : '';
107+
108+
const imports = getImports({ components, packageName }).join('\n').trim() || fallbackImport;
109+
121110
const base = {
122111
id,
123112
name: componentName ?? title,
124113
path: importPath,
125114
stories,
126-
import: calculatedImports,
115+
import: imports,
127116
jsDocTags: {},
128117
} satisfies Partial<ComponentManifest>;
129118

130-
if (!componentPath) {
131-
const error = !componentName
119+
if (!component?.reactDocgen) {
120+
const error = !component
132121
? {
133122
name: 'No meta.component specified',
134123
message: 'Specify meta.component for the component to be included in the manifest.',
135124
}
136125
: {
137126
name: 'No component import found',
138-
message: `No component file found for the "${componentName}" component.`,
127+
message: `No component file found for the "${component.componentName}" component.`,
139128
};
140129
return {
141130
...base,
@@ -147,25 +136,21 @@ export const componentManifestGenerator: PresetPropertyFn<
147136
};
148137
}
149138

150-
const docgenResult = getReactDocgen(
151-
componentPath,
152-
component ? component : { componentName: componentName ?? title }
153-
);
139+
const docgenResult = component.reactDocgen;
154140

155141
const docgen = docgenResult.type === 'success' ? docgenResult.data : undefined;
156142
const error = docgenResult.type === 'error' ? docgenResult.error : undefined;
157143

158-
const metaDescription = extractDescription(csf._metaStatement);
159-
const jsdocComment = metaDescription || docgen?.description;
160-
const { tags = {}, description } = jsdocComment ? extractJSDocInfo(jsdocComment) : {};
161144

162-
const manifestDescription = (tags?.describe?.[0] || tags?.desc?.[0]) ?? description;
145+
146+
const jsdocComment = extractDescription(csf._metaStatement) || docgen?.description;
147+
const { tags = {}, description } = jsdocComment ? extractJSDocInfo(jsdocComment) : {};
163148

164149
return {
165150
...base,
166-
description: manifestDescription?.trim(),
151+
description: ((tags?.describe?.[0] || tags?.desc?.[0]) ?? description)?.trim(),
167152
summary: tags.summary?.[0],
168-
import: calculatedImports,
153+
import: imports,
169154
reactDocgen: docgen,
170155
jsDocTags: tags,
171156
error,

0 commit comments

Comments
 (0)