@renr/parcel-reporter-docgen 0.1.0-beta.7 → 0.1.0-beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -70,35 +70,41 @@ async function generateProps(pagesPattern, rootDir, outputPath = "docgen", log =
|
|
|
70
70
|
log(`parsing file: : ${relFile}`);
|
|
71
71
|
const fileDocs = docsByFilePath.get(path.resolve(absPath)) || [];
|
|
72
72
|
const assignedInfo = getAssignedInfo(await fs.readFile(absPath, "utf-8"), componentName);
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
const usedDocs = /* @__PURE__ */ new Set();
|
|
74
|
+
let primaryDoc = fileDocs.find((d) => d.displayName === componentName) || (assignedInfo.baseIdentifier ? fileDocs.find((d) => d.displayName === assignedInfo.baseIdentifier) : void 0);
|
|
75
|
+
if (primaryDoc) usedDocs.add(primaryDoc);
|
|
76
76
|
let primaryProps = primaryDoc ? primaryDoc.props : {};
|
|
77
|
-
if (assignedInfo.baseIdentifier
|
|
77
|
+
if (assignedInfo.baseIdentifier) {
|
|
78
78
|
const baseDoc = fileDocs.find((d) => d.displayName === assignedInfo.baseIdentifier);
|
|
79
|
-
if (baseDoc
|
|
79
|
+
if (baseDoc) {
|
|
80
|
+
if (assignedInfo.baseIdentifier !== assignedInfo.subcomponentsMap.get("Root")) usedDocs.add(baseDoc);
|
|
81
|
+
if (primaryDoc && Object.keys(baseDoc.props).length > Object.keys(primaryProps).length) primaryProps = baseDoc.props;
|
|
82
|
+
}
|
|
80
83
|
}
|
|
81
|
-
const formattedPrimaryProps = formatProps(primaryProps);
|
|
82
84
|
const subcomponents = {};
|
|
83
85
|
for (const [subName, targetName] of assignedInfo.subcomponentsMap) {
|
|
84
|
-
if (
|
|
86
|
+
if (subName === componentName) continue;
|
|
85
87
|
const subDoc = fileDocs.find((d) => d.displayName === targetName || d.displayName === `${componentName}.${subName}` || d.displayName === `${componentName}${subName}` || d.displayName === subName);
|
|
86
|
-
if (subDoc)
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
88
|
+
if (subDoc) {
|
|
89
|
+
usedDocs.add(subDoc);
|
|
90
|
+
subcomponents[subName] = {
|
|
91
|
+
name: `${componentName}.${subName}`,
|
|
92
|
+
description: subDoc.description || void 0,
|
|
93
|
+
props: formatProps(subDoc.props)
|
|
94
|
+
};
|
|
95
|
+
}
|
|
91
96
|
}
|
|
97
|
+
const hasRoot = Boolean(subcomponents.Root) || fileDocs.some((d) => d.displayName === "Root" || d.displayName === `${componentName}Root` || d.displayName === `${componentName}.Root`);
|
|
92
98
|
for (const doc of fileDocs) {
|
|
93
|
-
if (doc
|
|
94
|
-
if (assignedInfo.baseIdentifier && doc.displayName === assignedInfo.baseIdentifier) continue;
|
|
99
|
+
if (usedDocs.has(doc)) continue;
|
|
95
100
|
let subName;
|
|
96
101
|
if (doc.displayName.startsWith(`${componentName}.`)) subName = doc.displayName.slice(componentName.length + 1);
|
|
97
|
-
else if (doc.displayName.startsWith(componentName)) subName = doc.displayName.slice(componentName.length);
|
|
98
|
-
if (
|
|
99
|
-
|
|
102
|
+
else if (doc.displayName.startsWith(componentName) && doc.displayName.length > componentName.length) subName = doc.displayName.slice(componentName.length);
|
|
103
|
+
else if (hasRoot) subName = doc.displayName;
|
|
104
|
+
if (subName && subName !== componentName) {
|
|
100
105
|
const existing = subcomponents[subName];
|
|
101
106
|
if (existing && Object.keys(existing.props).length >= Object.keys(doc.props).length) continue;
|
|
107
|
+
usedDocs.add(doc);
|
|
102
108
|
subcomponents[subName] = {
|
|
103
109
|
name: `${componentName}.${subName}`,
|
|
104
110
|
description: doc.description || void 0,
|
|
@@ -106,9 +112,16 @@ async function generateProps(pagesPattern, rootDir, outputPath = "docgen", log =
|
|
|
106
112
|
};
|
|
107
113
|
}
|
|
108
114
|
}
|
|
115
|
+
const promotedRoot = !(Boolean(primaryDoc) && Object.keys(primaryProps).length > 0 && (primaryDoc === null || primaryDoc === void 0 ? void 0 : primaryDoc.displayName) !== "Root" && (primaryDoc === null || primaryDoc === void 0 ? void 0 : primaryDoc.displayName) !== `${componentName}Root` && (primaryDoc === null || primaryDoc === void 0 ? void 0 : primaryDoc.displayName) !== `${componentName}.Root` && assignedInfo.baseIdentifier !== assignedInfo.subcomponentsMap.get("Root")) && subcomponents.Root ? subcomponents.Root : void 0;
|
|
116
|
+
if (promotedRoot) delete subcomponents.Root;
|
|
117
|
+
else if (!primaryDoc) {
|
|
118
|
+
primaryDoc = fileDocs.find((d) => Object.keys(d.props).length > 0) || fileDocs[0];
|
|
119
|
+
primaryProps = primaryDoc ? primaryDoc.props : {};
|
|
120
|
+
}
|
|
121
|
+
const formattedPrimaryProps = promotedRoot ? promotedRoot.props : formatProps(primaryProps);
|
|
109
122
|
const componentJSON = JSON.stringify({
|
|
110
|
-
name:
|
|
111
|
-
description: (primaryDoc === null || primaryDoc === void 0 ? void 0 : primaryDoc.description) || void 0,
|
|
123
|
+
name: promotedRoot ? promotedRoot.name : componentName,
|
|
124
|
+
description: (promotedRoot ? promotedRoot.description : primaryDoc === null || primaryDoc === void 0 ? void 0 : primaryDoc.description) || void 0,
|
|
112
125
|
path: relFile.replace(/\\/g, "/"),
|
|
113
126
|
fileName: path.basename(relFile),
|
|
114
127
|
props: formattedPrimaryProps,
|
|
@@ -171,13 +184,11 @@ function getAssignedInfo(sourceText, compName) {
|
|
|
171
184
|
ts.forEachChild(sourceFile, (node) => {
|
|
172
185
|
if (ts.isExportDeclaration(node)) {
|
|
173
186
|
if (!node.isTypeOnly && node.exportClause && ts.isNamedExports(node.exportClause)) {
|
|
174
|
-
for (const element of node.exportClause.elements) if (!element.isTypeOnly) {
|
|
187
|
+
for (const element of node.exportClause.elements) if (!element.isTypeOnly && element.propertyName) {
|
|
175
188
|
const exportName = element.name.getText(sourceFile);
|
|
176
|
-
const localName = element.propertyName
|
|
177
|
-
if (
|
|
178
|
-
|
|
179
|
-
if (exportName === "Root") baseIdentifier = localName;
|
|
180
|
-
}
|
|
189
|
+
const localName = element.propertyName.getText(sourceFile);
|
|
190
|
+
if (exportName === compName) baseIdentifier = localName;
|
|
191
|
+
else if (/^[A-Z]/.test(exportName)) subcomponentsMap.set(exportName, localName);
|
|
181
192
|
}
|
|
182
193
|
}
|
|
183
194
|
}
|
|
@@ -193,11 +204,9 @@ function getAssignedInfo(sourceText, compName) {
|
|
|
193
204
|
const propName = prop.name.getText(sourceFile);
|
|
194
205
|
const initName = prop.initializer.getText(sourceFile);
|
|
195
206
|
subcomponentsMap.set(propName, initName);
|
|
196
|
-
if (propName === "Root") baseIdentifier = initName;
|
|
197
207
|
} else if (ts.isShorthandPropertyAssignment(prop)) {
|
|
198
208
|
const propName = prop.name.getText(sourceFile);
|
|
199
209
|
subcomponentsMap.set(propName, propName);
|
|
200
|
-
if (propName === "Root") baseIdentifier = propName;
|
|
201
210
|
}
|
|
202
211
|
}
|
|
203
212
|
}
|
|
@@ -211,27 +220,6 @@ function getAssignedInfo(sourceText, compName) {
|
|
|
211
220
|
if (objText === compName && /^[A-Z]/.test(propText)) {
|
|
212
221
|
const targetName = bin.right.getText(sourceFile);
|
|
213
222
|
subcomponentsMap.set(propText, targetName);
|
|
214
|
-
if (propText === "Root") baseIdentifier = targetName;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
if (ts.isFunctionDeclaration(node) && node.name) {
|
|
219
|
-
var _node$modifiers;
|
|
220
|
-
if ((_node$modifiers = node.modifiers) === null || _node$modifiers === void 0 ? void 0 : _node$modifiers.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)) {
|
|
221
|
-
const fnName = node.name.getText(sourceFile);
|
|
222
|
-
if (/^[A-Z]/.test(fnName)) {
|
|
223
|
-
subcomponentsMap.set(fnName, fnName);
|
|
224
|
-
if (fnName === "Root") baseIdentifier = fnName;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
if (ts.isVariableStatement(node)) {
|
|
229
|
-
var _node$modifiers2;
|
|
230
|
-
if ((_node$modifiers2 = node.modifiers) === null || _node$modifiers2 === void 0 ? void 0 : _node$modifiers2.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)) for (const decl of node.declarationList.declarations) {
|
|
231
|
-
const varName = decl.name.getText(sourceFile);
|
|
232
|
-
if (varName !== compName && /^[A-Z]/.test(varName)) {
|
|
233
|
-
subcomponentsMap.set(varName, varName);
|
|
234
|
-
if (varName === "Root") baseIdentifier = varName;
|
|
235
223
|
}
|
|
236
224
|
}
|
|
237
225
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate-props.mjs","names":[],"sources":["../../src/generate-props.ts"],"sourcesContent":["import fs from \"fs/promises\";\nimport path from \"path\";\nimport { PropItem, withCustomConfig } from \"react-docgen-typescript\";\nimport { glob } from \"tinyglobby\";\nimport ts from \"typescript\";\nimport { fileURLToPath } from \"url\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\n// cache for watch mode across buildStart events\nconst fileMtimeCache = new Map<string, number>();\n\nexport async function generateProps(\n pagesPattern: string,\n rootDir: string,\n outputPath: string = \"docgen\",\n log: (...args: unknown[]) => void = console.log,\n customTsconfigPath?: string\n) {\n const files = await glob(pagesPattern, {\n cwd: rootDir,\n });\n\n if (files.length === 0) {\n log(`No files found for pattern: ${pagesPattern}`);\n return;\n }\n\n const resolvedOutputPath = path.resolve(rootDir, outputPath);\n await fs.mkdir(resolvedOutputPath, { recursive: true });\n\n const filesToProcess: {\n relFile: string;\n absPath: string;\n componentName: string;\n }[] = [];\n\n for (const file of files) {\n const filePath = path.resolve(rootDir, file);\n const componentName = path.basename(file, path.extname(file));\n const outputFilePath = path.join(\n resolvedOutputPath,\n `${componentName}.json`\n );\n\n try {\n const sourceStat = await fs.stat(filePath);\n const cachedMtime = fileMtimeCache.get(filePath);\n\n let isUpToDate = false;\n try {\n const outStat = await fs.stat(outputFilePath);\n if (\n (cachedMtime !== undefined && cachedMtime === sourceStat.mtimeMs) ||\n outStat.mtimeMs >= sourceStat.mtimeMs\n ) {\n isUpToDate = true;\n fileMtimeCache.set(filePath, sourceStat.mtimeMs);\n }\n } catch {\n // output file does not exist\n }\n\n if (isUpToDate) {\n continue;\n }\n\n filesToProcess.push({\n relFile: file,\n absPath: filePath,\n componentName,\n });\n fileMtimeCache.set(filePath, sourceStat.mtimeMs);\n } catch {\n // source file not accessible\n }\n }\n\n if (filesToProcess.length === 0) {\n return;\n }\n\n log(\n `Found ${filesToProcess.length} changed file(s) for pattern: ${pagesPattern}`\n );\n\n const resolvedTsconfigPath = customTsconfigPath\n ? path.resolve(rootDir, customTsconfigPath)\n : path.resolve(rootDir, \"./tsconfig.json\");\n\n const docgenParser = withCustomConfig(resolvedTsconfigPath, {\n shouldExtractLiteralValuesFromEnum: true,\n savePropValueAsString: true,\n propFilter: (prop) => {\n if (Array.isArray(prop.declarations) && prop.declarations[0]) {\n const declaration = prop.declarations[0];\n\n if (declaration.fileName.includes(\"node_modules/@types/react\")) {\n if (prop.name !== \"ref\" && prop.name !== \"className\") {\n return false;\n }\n }\n }\n return true;\n },\n });\n\n try {\n const absPaths = filesToProcess.map((f) => f.absPath);\n const allDocs = docgenParser.parse(absPaths);\n\n const docsByFilePath = new Map<string, typeof allDocs>();\n for (const doc of allDocs) {\n if (doc.filePath) {\n const key = path.resolve(doc.filePath);\n const list = docsByFilePath.get(key) || [];\n list.push(doc);\n docsByFilePath.set(key, list);\n }\n }\n\n await Promise.all(\n filesToProcess.map(async ({ relFile, absPath, componentName }) => {\n log(`parsing file: : ${relFile}`);\n\n const fileDocs = docsByFilePath.get(path.resolve(absPath)) || [];\n\n // Check if the component was composed using Object.assign or exports compound parts\n const sourceText = await fs.readFile(absPath, \"utf-8\");\n const assignedInfo = getAssignedInfo(sourceText, componentName);\n\n // Determine if this component has a .Root part\n const hasRoot =\n assignedInfo.subcomponentsMap.has(\"Root\") ||\n fileDocs.some(\n (d) =>\n d.displayName === \"Root\" ||\n d.displayName === `${componentName}Root` ||\n d.displayName === `${componentName}.Root`\n );\n\n // 1. Identify primary component doc matching componentName or assigned base / Root\n const rootTarget = assignedInfo.subcomponentsMap.get(\"Root\");\n const primaryDoc = hasRoot\n ? (rootTarget\n ? fileDocs.find((d) => d.displayName === rootTarget)\n : undefined) ||\n (assignedInfo.baseIdentifier\n ? fileDocs.find(\n (d) => d.displayName === assignedInfo.baseIdentifier\n )\n : undefined) ||\n fileDocs.find(\n (d) =>\n d.displayName === `${componentName}.Root` ||\n d.displayName === `${componentName}Root` ||\n d.displayName === \"Root\"\n ) ||\n fileDocs.find((d) => d.displayName === componentName) ||\n fileDocs.find((d) => Object.keys(d.props).length > 0) ||\n fileDocs[0]\n : fileDocs.find((d) => d.displayName === componentName) ||\n (assignedInfo.baseIdentifier\n ? fileDocs.find(\n (d) => d.displayName === assignedInfo.baseIdentifier\n )\n : undefined) ||\n fileDocs.find((d) => Object.keys(d.props).length > 0) ||\n fileDocs[0];\n\n // If primaryDoc was created by Object.assign and lost props, recover from base\n let primaryProps = primaryDoc ? primaryDoc.props : {};\n if (\n assignedInfo.baseIdentifier &&\n primaryDoc &&\n primaryDoc.displayName === componentName\n ) {\n const baseDoc = fileDocs.find(\n (d) => d.displayName === assignedInfo.baseIdentifier\n );\n if (\n baseDoc &&\n Object.keys(baseDoc.props).length > Object.keys(primaryProps).length\n ) {\n primaryProps = baseDoc.props;\n }\n }\n\n const formattedPrimaryProps = formatProps(primaryProps);\n\n // 2. Identify subcomponents\n const subcomponents: Record<\n string,\n {\n name: string;\n description?: string;\n props: ReturnType<typeof formatProps>;\n }\n > = {};\n\n // a) Handle subcomponents explicitly assigned or exported\n for (const [subName, targetName] of assignedInfo.subcomponentsMap) {\n if (hasRoot && subName === \"Root\") {\n continue;\n }\n const subDoc = fileDocs.find(\n (d) =>\n d.displayName === targetName ||\n d.displayName === `${componentName}.${subName}` ||\n d.displayName === `${componentName}${subName}` ||\n d.displayName === subName\n );\n if (subDoc) {\n subcomponents[subName] = {\n name: `${componentName}.${subName}`,\n description: subDoc.description || undefined,\n props: formatProps(subDoc.props),\n };\n }\n }\n\n // b) Handle subcomponents matching standard React dot notation or component prefix\n for (const doc of fileDocs) {\n if (doc === primaryDoc) continue;\n if (\n assignedInfo.baseIdentifier &&\n doc.displayName === assignedInfo.baseIdentifier\n ) {\n continue;\n }\n\n let subName: string | undefined;\n if (doc.displayName.startsWith(`${componentName}.`)) {\n subName = doc.displayName.slice(componentName.length + 1);\n } else if (doc.displayName.startsWith(componentName)) {\n subName = doc.displayName.slice(componentName.length);\n }\n\n if (subName) {\n if (hasRoot && subName === \"Root\") continue;\n const existing = subcomponents[subName];\n if (\n existing &&\n Object.keys(existing.props).length >=\n Object.keys(doc.props).length\n ) {\n continue;\n }\n\n subcomponents[subName] = {\n name: `${componentName}.${subName}`,\n description: doc.description || undefined,\n props: formatProps(doc.props),\n };\n }\n }\n\n const componentJSON = JSON.stringify(\n {\n name: hasRoot ? `${componentName}.Root` : componentName,\n description: primaryDoc?.description || undefined,\n path: relFile.replace(/\\\\/g, \"/\"),\n fileName: path.basename(relFile),\n props: formattedPrimaryProps,\n subcomponents:\n Object.keys(subcomponents).length > 0 ? subcomponents : undefined,\n },\n null,\n 2\n );\n\n const outputFilePath = path.join(\n resolvedOutputPath,\n `${componentName}.json`\n );\n await fs.writeFile(outputFilePath, componentJSON + \"\\n\", \"utf-8\");\n\n log(`Generated props for ${componentName}`);\n })\n );\n } catch (err) {\n log(`Error generating props: ${(err as Error).message}`);\n }\n}\n\n// based on https://github.com/mui/base-ui/blob/master/docs/src/components/ReferenceTable/PropsReferenceAccordion.tsx\nfunction getShortPropType(name: string, type: string) {\n if (/^(on|get)[A-Z].*/.test(name)) {\n return { type: \"function\", detailedType: true };\n }\n\n if (type === undefined || type === null) {\n return { type: String(type), detailedType: false };\n }\n\n if (name === \"className\") {\n return { type: \"string | function\", detailedType: true };\n }\n\n if (name === \"render\") {\n return { type: \"ReactElement | function\", detailedType: true };\n }\n\n if (\n name.endsWith(\"Ref\") ||\n name === \"children\" ||\n type === \"boolean\" ||\n type === \"string\" ||\n type === \"number\" ||\n type.indexOf(\" | \") === -1 ||\n (type.split(\"|\").length < 3 && type.length < 30)\n ) {\n return { type, detailedType: false };\n }\n\n return { type: \"Union\", detailedType: true };\n}\n\nfunction formatProps(rawProps: Record<string, PropItem>) {\n const props: Record<\n string,\n PropItem & {\n shortPropTypeName: string | null;\n }\n > = {};\n\n for (const propName in rawProps) {\n const rawProp = rawProps[propName];\n if (!rawProp) {\n continue;\n }\n\n const prop = { ...rawProp } as PropItem & {\n shortPropTypeName: string | null;\n };\n\n if (prop.type && prop.type.name) {\n const { type: shortPropTypeName, detailedType } = getShortPropType(\n propName,\n prop.type.name\n );\n\n const hasExpandedType = Boolean(detailedType);\n\n prop.type.name =\n hasExpandedType && prop.type.name.split(\"|\").length > 3\n ? prop.type.name\n .split(\"|\")\n .map((line) => `| ${line}\\n`)\n .join(\"\")\n : prop.type.name;\n\n prop.shortPropTypeName = hasExpandedType ? shortPropTypeName : null;\n }\n\n props[propName] = prop;\n }\n\n return props;\n}\n\nfunction getAssignedInfo(sourceText: string, compName: string) {\n const sourceFile = ts.createSourceFile(\n \"temp.tsx\",\n sourceText,\n ts.ScriptTarget.Latest,\n true\n );\n let baseIdentifier: string | null = null;\n const subcomponentsMap = new Map<string, string>();\n\n ts.forEachChild(sourceFile, (node) => {\n // 1. export { Local as Exported, ... }\n if (ts.isExportDeclaration(node)) {\n if (\n !node.isTypeOnly &&\n node.exportClause &&\n ts.isNamedExports(node.exportClause)\n ) {\n for (const element of node.exportClause.elements) {\n if (!element.isTypeOnly) {\n const exportName = element.name.getText(sourceFile);\n const localName = element.propertyName\n ? element.propertyName.getText(sourceFile)\n : exportName;\n if (/^[A-Z]/.test(exportName)) {\n subcomponentsMap.set(exportName, localName);\n if (exportName === \"Root\") {\n baseIdentifier = localName;\n }\n }\n }\n }\n }\n }\n\n // 2. export const Comp = Object.assign(Base, { SubName: Target, ... })\n if (ts.isVariableStatement(node)) {\n for (const decl of node.declarationList.declarations) {\n if (\n decl.name.getText(sourceFile) === compName &&\n decl.initializer &&\n ts.isCallExpression(decl.initializer)\n ) {\n const call = decl.initializer;\n const exprText = call.expression.getText(sourceFile);\n if (\n (exprText === \"Object.assign\" || exprText.endsWith(\".assign\")) &&\n call.arguments.length > 0\n ) {\n const firstArg = call.arguments[0];\n if (firstArg) {\n baseIdentifier = firstArg.getText(sourceFile);\n }\n if (\n call.arguments.length > 1 &&\n ts.isObjectLiteralExpression(call.arguments[1]!)\n ) {\n for (const prop of call.arguments[1]!.properties) {\n if (ts.isPropertyAssignment(prop)) {\n const propName = prop.name.getText(sourceFile);\n const initName = prop.initializer.getText(sourceFile);\n subcomponentsMap.set(propName, initName);\n if (propName === \"Root\") {\n baseIdentifier = initName;\n }\n } else if (ts.isShorthandPropertyAssignment(prop)) {\n const propName = prop.name.getText(sourceFile);\n subcomponentsMap.set(propName, propName);\n if (propName === \"Root\") {\n baseIdentifier = propName;\n }\n }\n }\n }\n }\n }\n }\n }\n\n // 3. Comp.Sub = Target\n if (\n ts.isExpressionStatement(node) &&\n ts.isBinaryExpression(node.expression)\n ) {\n const bin = node.expression;\n if (\n bin.operatorToken.kind === ts.SyntaxKind.EqualsToken &&\n ts.isPropertyAccessExpression(bin.left)\n ) {\n const objText = bin.left.expression.getText(sourceFile);\n const propText = bin.left.name.getText(sourceFile);\n if (objText === compName && /^[A-Z]/.test(propText)) {\n const targetName = bin.right.getText(sourceFile);\n subcomponentsMap.set(propText, targetName);\n if (propText === \"Root\") {\n baseIdentifier = targetName;\n }\n }\n }\n }\n\n // 4. export function Root(...) or export const Root = (...)\n if (ts.isFunctionDeclaration(node) && node.name) {\n const isExported = node.modifiers?.some(\n (m) => m.kind === ts.SyntaxKind.ExportKeyword\n );\n if (isExported) {\n const fnName = node.name.getText(sourceFile);\n if (/^[A-Z]/.test(fnName)) {\n subcomponentsMap.set(fnName, fnName);\n if (fnName === \"Root\") {\n baseIdentifier = fnName;\n }\n }\n }\n }\n\n if (ts.isVariableStatement(node)) {\n const isExported = node.modifiers?.some(\n (m) => m.kind === ts.SyntaxKind.ExportKeyword\n );\n if (isExported) {\n for (const decl of node.declarationList.declarations) {\n const varName = decl.name.getText(sourceFile);\n if (varName !== compName && /^[A-Z]/.test(varName)) {\n subcomponentsMap.set(varName, varName);\n if (varName === \"Root\") {\n baseIdentifier = varName;\n }\n }\n }\n }\n }\n });\n\n return { baseIdentifier, subcomponentsMap };\n}\n"],"mappings":";;;;;;;AAOA,MAAM,aAAa,cAAc,YAAY,GAAG;AAC9B,KAAK,QAAQ,UAAU;AAGzC,MAAM,iCAAiB,IAAI,IAAoB;AAE/C,eAAsB,cACpB,cACA,SACA,aAAqB,UACrB,MAAoC,QAAQ,KAC5C,oBACA;CACA,MAAM,QAAQ,MAAM,KAAK,cAAc,EACrC,KAAK,QACP,CAAC;CAED,IAAI,MAAM,WAAW,GAAG;EACtB,IAAI,+BAA+B,cAAc;EACjD;CACF;CAEA,MAAM,qBAAqB,KAAK,QAAQ,SAAS,UAAU;CAC3D,MAAM,GAAG,MAAM,oBAAoB,EAAE,WAAW,KAAK,CAAC;CAEtD,MAAM,iBAIA,CAAC;CAEP,KAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,WAAW,KAAK,QAAQ,SAAS,IAAI;EAC3C,MAAM,gBAAgB,KAAK,SAAS,MAAM,KAAK,QAAQ,IAAI,CAAC;EAC5D,MAAM,iBAAiB,KAAK,KAC1B,oBACA,GAAG,cAAc,MACnB;EAEA,IAAI;GACF,MAAM,aAAa,MAAM,GAAG,KAAK,QAAQ;GACzC,MAAM,cAAc,eAAe,IAAI,QAAQ;GAE/C,IAAI,aAAa;GACjB,IAAI;IACF,MAAM,UAAU,MAAM,GAAG,KAAK,cAAc;IAC5C,IACG,gBAAgB,KAAA,KAAa,gBAAgB,WAAW,WACzD,QAAQ,WAAW,WAAW,SAC9B;KACA,aAAa;KAC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AM,QAAQ,KAAK,gBAAgB,cAAc;IACpD,MAAM,UAAU,KAAK,KAAK,QAAQ,UAAU;IAC5C,IAAI,YAAY,YAAY,SAAS,KAAK,OAAO,GAAG;KAClD,iBAAiB,IAAI,SAAS,OAAO;KACrC,IAAI,YAAY,QACd,iBAAiB;IAErB;GACF;EAEJ;CACF,CAAC;CAED,OAAO;EAAE;EAAgB;CAAiB;AAC5C"}
|
|
1
|
+
{"version":3,"file":"generate-props.mjs","names":[],"sources":["../../src/generate-props.ts"],"sourcesContent":["import fs from \"fs/promises\";\nimport path from \"path\";\nimport { PropItem, withCustomConfig } from \"react-docgen-typescript\";\nimport { glob } from \"tinyglobby\";\nimport ts from \"typescript\";\nimport { fileURLToPath } from \"url\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\n// cache for watch mode across buildStart events\nconst fileMtimeCache = new Map<string, number>();\n\nexport async function generateProps(\n pagesPattern: string,\n rootDir: string,\n outputPath: string = \"docgen\",\n log: (...args: unknown[]) => void = console.log,\n customTsconfigPath?: string\n) {\n const files = await glob(pagesPattern, {\n cwd: rootDir,\n });\n\n if (files.length === 0) {\n log(`No files found for pattern: ${pagesPattern}`);\n return;\n }\n\n const resolvedOutputPath = path.resolve(rootDir, outputPath);\n await fs.mkdir(resolvedOutputPath, { recursive: true });\n\n const filesToProcess: {\n relFile: string;\n absPath: string;\n componentName: string;\n }[] = [];\n\n for (const file of files) {\n const filePath = path.resolve(rootDir, file);\n const componentName = path.basename(file, path.extname(file));\n const outputFilePath = path.join(\n resolvedOutputPath,\n `${componentName}.json`\n );\n\n try {\n const sourceStat = await fs.stat(filePath);\n const cachedMtime = fileMtimeCache.get(filePath);\n\n let isUpToDate = false;\n try {\n const outStat = await fs.stat(outputFilePath);\n if (\n (cachedMtime !== undefined && cachedMtime === sourceStat.mtimeMs) ||\n outStat.mtimeMs >= sourceStat.mtimeMs\n ) {\n isUpToDate = true;\n fileMtimeCache.set(filePath, sourceStat.mtimeMs);\n }\n } catch {\n // output file does not exist\n }\n\n if (isUpToDate) {\n continue;\n }\n\n filesToProcess.push({\n relFile: file,\n absPath: filePath,\n componentName,\n });\n fileMtimeCache.set(filePath, sourceStat.mtimeMs);\n } catch {\n // source file not accessible\n }\n }\n\n if (filesToProcess.length === 0) {\n return;\n }\n\n log(\n `Found ${filesToProcess.length} changed file(s) for pattern: ${pagesPattern}`\n );\n\n const resolvedTsconfigPath = customTsconfigPath\n ? path.resolve(rootDir, customTsconfigPath)\n : path.resolve(rootDir, \"./tsconfig.json\");\n\n const docgenParser = withCustomConfig(resolvedTsconfigPath, {\n shouldExtractLiteralValuesFromEnum: true,\n savePropValueAsString: true,\n propFilter: (prop) => {\n if (Array.isArray(prop.declarations) && prop.declarations[0]) {\n const declaration = prop.declarations[0];\n\n if (declaration.fileName.includes(\"node_modules/@types/react\")) {\n if (prop.name !== \"ref\" && prop.name !== \"className\") {\n return false;\n }\n }\n }\n return true;\n },\n });\n\n try {\n const absPaths = filesToProcess.map((f) => f.absPath);\n const allDocs = docgenParser.parse(absPaths);\n\n const docsByFilePath = new Map<string, typeof allDocs>();\n for (const doc of allDocs) {\n if (doc.filePath) {\n const key = path.resolve(doc.filePath);\n const list = docsByFilePath.get(key) || [];\n list.push(doc);\n docsByFilePath.set(key, list);\n }\n }\n\n await Promise.all(\n filesToProcess.map(async ({ relFile, absPath, componentName }) => {\n log(`parsing file: : ${relFile}`);\n\n const fileDocs = docsByFilePath.get(path.resolve(absPath)) || [];\n\n // Check if the component was composed using Object.assign or exports compound parts\n const sourceText = await fs.readFile(absPath, \"utf-8\");\n const assignedInfo = getAssignedInfo(sourceText, componentName);\n\n const usedDocs = new Set<(typeof fileDocs)[number]>();\n\n // 1. Identify explicit primary component doc matching componentName or assigned base\n let primaryDoc =\n fileDocs.find((d) => d.displayName === componentName) ||\n (assignedInfo.baseIdentifier\n ? fileDocs.find(\n (d) => d.displayName === assignedInfo.baseIdentifier\n )\n : undefined);\n\n if (primaryDoc) {\n usedDocs.add(primaryDoc);\n }\n\n // If primaryDoc was created by Object.assign and lost props, recover from base\n let primaryProps = primaryDoc ? primaryDoc.props : {};\n if (assignedInfo.baseIdentifier) {\n const baseDoc = fileDocs.find(\n (d) => d.displayName === assignedInfo.baseIdentifier\n );\n if (baseDoc) {\n if (\n assignedInfo.baseIdentifier !==\n assignedInfo.subcomponentsMap.get(\"Root\")\n ) {\n usedDocs.add(baseDoc);\n }\n if (\n primaryDoc &&\n Object.keys(baseDoc.props).length >\n Object.keys(primaryProps).length\n ) {\n primaryProps = baseDoc.props;\n }\n }\n }\n\n // 2. Identify subcomponents (including Root if present)\n const subcomponents: Record<\n string,\n {\n name: string;\n description?: string;\n props: ReturnType<typeof formatProps>;\n }\n > = {};\n\n // a) Handle subcomponents explicitly assigned or exported\n for (const [subName, targetName] of assignedInfo.subcomponentsMap) {\n if (subName === componentName) continue;\n const subDoc = fileDocs.find(\n (d) =>\n d.displayName === targetName ||\n d.displayName === `${componentName}.${subName}` ||\n d.displayName === `${componentName}${subName}` ||\n d.displayName === subName\n );\n if (subDoc) {\n usedDocs.add(subDoc);\n subcomponents[subName] = {\n name: `${componentName}.${subName}`,\n description: subDoc.description || undefined,\n props: formatProps(subDoc.props),\n };\n }\n }\n\n const hasRoot =\n Boolean(subcomponents.Root) ||\n fileDocs.some(\n (d) =>\n d.displayName === \"Root\" ||\n d.displayName === `${componentName}Root` ||\n d.displayName === `${componentName}.Root`\n );\n\n // b) Handle subcomponents matching standard React dot notation or component prefix\n for (const doc of fileDocs) {\n if (usedDocs.has(doc)) continue;\n\n let subName: string | undefined;\n if (doc.displayName.startsWith(`${componentName}.`)) {\n subName = doc.displayName.slice(componentName.length + 1);\n } else if (\n doc.displayName.startsWith(componentName) &&\n doc.displayName.length > componentName.length\n ) {\n subName = doc.displayName.slice(componentName.length);\n } else if (hasRoot) {\n subName = doc.displayName;\n }\n\n if (subName && subName !== componentName) {\n const existing = subcomponents[subName];\n if (\n existing &&\n Object.keys(existing.props).length >=\n Object.keys(doc.props).length\n ) {\n continue;\n }\n\n usedDocs.add(doc);\n subcomponents[subName] = {\n name: `${componentName}.${subName}`,\n description: doc.description || undefined,\n props: formatProps(doc.props),\n };\n }\n }\n\n const hasDistinctPrimary =\n Boolean(primaryDoc) &&\n Object.keys(primaryProps).length > 0 &&\n primaryDoc?.displayName !== \"Root\" &&\n primaryDoc?.displayName !== `${componentName}Root` &&\n primaryDoc?.displayName !== `${componentName}.Root` &&\n assignedInfo.baseIdentifier !==\n assignedInfo.subcomponentsMap.get(\"Root\");\n\n // Promote Root subcomponent to primary component only when no distinct primary component exists\n const promotedRoot =\n !hasDistinctPrimary && subcomponents.Root\n ? subcomponents.Root\n : undefined;\n if (promotedRoot) {\n delete subcomponents.Root;\n } else if (!primaryDoc) {\n primaryDoc =\n fileDocs.find((d) => Object.keys(d.props).length > 0) ||\n fileDocs[0];\n primaryProps = primaryDoc ? primaryDoc.props : {};\n }\n\n const formattedPrimaryProps = promotedRoot\n ? promotedRoot.props\n : formatProps(primaryProps);\n\n const componentJSON = JSON.stringify(\n {\n name: promotedRoot ? promotedRoot.name : componentName,\n description:\n (promotedRoot\n ? promotedRoot.description\n : primaryDoc?.description) || undefined,\n path: relFile.replace(/\\\\/g, \"/\"),\n fileName: path.basename(relFile),\n props: formattedPrimaryProps,\n subcomponents:\n Object.keys(subcomponents).length > 0 ? subcomponents : undefined,\n },\n null,\n 2\n );\n\n const outputFilePath = path.join(\n resolvedOutputPath,\n `${componentName}.json`\n );\n await fs.writeFile(outputFilePath, componentJSON + \"\\n\", \"utf-8\");\n\n log(`Generated props for ${componentName}`);\n })\n );\n } catch (err) {\n log(`Error generating props: ${(err as Error).message}`);\n }\n}\n\n// based on https://github.com/mui/base-ui/blob/master/docs/src/components/ReferenceTable/PropsReferenceAccordion.tsx\nfunction getShortPropType(name: string, type: string) {\n if (/^(on|get)[A-Z].*/.test(name)) {\n return { type: \"function\", detailedType: true };\n }\n\n if (type === undefined || type === null) {\n return { type: String(type), detailedType: false };\n }\n\n if (name === \"className\") {\n return { type: \"string | function\", detailedType: true };\n }\n\n if (name === \"render\") {\n return { type: \"ReactElement | function\", detailedType: true };\n }\n\n if (\n name.endsWith(\"Ref\") ||\n name === \"children\" ||\n type === \"boolean\" ||\n type === \"string\" ||\n type === \"number\" ||\n type.indexOf(\" | \") === -1 ||\n (type.split(\"|\").length < 3 && type.length < 30)\n ) {\n return { type, detailedType: false };\n }\n\n return { type: \"Union\", detailedType: true };\n}\n\nfunction formatProps(rawProps: Record<string, PropItem>) {\n const props: Record<\n string,\n PropItem & {\n shortPropTypeName: string | null;\n }\n > = {};\n\n for (const propName in rawProps) {\n const rawProp = rawProps[propName];\n if (!rawProp) {\n continue;\n }\n\n const prop = { ...rawProp } as PropItem & {\n shortPropTypeName: string | null;\n };\n\n if (prop.type && prop.type.name) {\n const { type: shortPropTypeName, detailedType } = getShortPropType(\n propName,\n prop.type.name\n );\n\n const hasExpandedType = Boolean(detailedType);\n\n prop.type.name =\n hasExpandedType && prop.type.name.split(\"|\").length > 3\n ? prop.type.name\n .split(\"|\")\n .map((line) => `| ${line}\\n`)\n .join(\"\")\n : prop.type.name;\n\n prop.shortPropTypeName = hasExpandedType ? shortPropTypeName : null;\n }\n\n props[propName] = prop;\n }\n\n return props;\n}\n\nfunction getAssignedInfo(sourceText: string, compName: string) {\n const sourceFile = ts.createSourceFile(\n \"temp.tsx\",\n sourceText,\n ts.ScriptTarget.Latest,\n true\n );\n let baseIdentifier: string | null = null;\n const subcomponentsMap = new Map<string, string>();\n\n ts.forEachChild(sourceFile, (node) => {\n // 1. export { Local as Exported, ... }\n if (ts.isExportDeclaration(node)) {\n if (\n !node.isTypeOnly &&\n node.exportClause &&\n ts.isNamedExports(node.exportClause)\n ) {\n for (const element of node.exportClause.elements) {\n if (!element.isTypeOnly && element.propertyName) {\n const exportName = element.name.getText(sourceFile);\n const localName = element.propertyName.getText(sourceFile);\n if (exportName === compName) {\n baseIdentifier = localName;\n } else if (/^[A-Z]/.test(exportName)) {\n subcomponentsMap.set(exportName, localName);\n }\n }\n }\n }\n }\n\n // 2. export const Comp = Object.assign(Base, { SubName: Target, ... })\n if (ts.isVariableStatement(node)) {\n for (const decl of node.declarationList.declarations) {\n if (\n decl.name.getText(sourceFile) === compName &&\n decl.initializer &&\n ts.isCallExpression(decl.initializer)\n ) {\n const call = decl.initializer;\n const exprText = call.expression.getText(sourceFile);\n if (\n (exprText === \"Object.assign\" || exprText.endsWith(\".assign\")) &&\n call.arguments.length > 0\n ) {\n const firstArg = call.arguments[0];\n if (firstArg) {\n baseIdentifier = firstArg.getText(sourceFile);\n }\n if (\n call.arguments.length > 1 &&\n ts.isObjectLiteralExpression(call.arguments[1]!)\n ) {\n for (const prop of call.arguments[1]!.properties) {\n if (ts.isPropertyAssignment(prop)) {\n const propName = prop.name.getText(sourceFile);\n const initName = prop.initializer.getText(sourceFile);\n subcomponentsMap.set(propName, initName);\n } else if (ts.isShorthandPropertyAssignment(prop)) {\n const propName = prop.name.getText(sourceFile);\n subcomponentsMap.set(propName, propName);\n }\n }\n }\n }\n }\n }\n }\n\n // 3. Comp.Sub = Target\n if (\n ts.isExpressionStatement(node) &&\n ts.isBinaryExpression(node.expression)\n ) {\n const bin = node.expression;\n if (\n bin.operatorToken.kind === ts.SyntaxKind.EqualsToken &&\n ts.isPropertyAccessExpression(bin.left)\n ) {\n const objText = bin.left.expression.getText(sourceFile);\n const propText = bin.left.name.getText(sourceFile);\n if (objText === compName && /^[A-Z]/.test(propText)) {\n const targetName = bin.right.getText(sourceFile);\n subcomponentsMap.set(propText, targetName);\n }\n }\n }\n });\n\n return { baseIdentifier, subcomponentsMap };\n}\n"],"mappings":";;;;;;;AAOA,MAAM,aAAa,cAAc,YAAY,GAAG;AAC9B,KAAK,QAAQ,UAAU;AAGzC,MAAM,iCAAiB,IAAI,IAAoB;AAE/C,eAAsB,cACpB,cACA,SACA,aAAqB,UACrB,MAAoC,QAAQ,KAC5C,oBACA;CACA,MAAM,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AAU,GACrC;GACA,MAAM,MAAM,KAAK;GACjB,IACE,IAAI,cAAc,SAAS,GAAG,WAAW,eACzC,GAAG,2BAA2B,IAAI,IAAI,GACtC;IACA,MAAM,UAAU,IAAI,KAAK,WAAW,QAAQ,UAAU;IACtD,MAAM,WAAW,IAAI,KAAK,KAAK,QAAQ,UAAU;IACjD,IAAI,YAAY,YAAY,SAAS,KAAK,QAAQ,GAAG;KACnD,MAAM,aAAa,IAAI,MAAM,QAAQ,UAAU;KAC/C,iBAAiB,IAAI,UAAU,UAAU;IAC3C;GACF;EACF;CACF,CAAC;CAED,OAAO;EAAE;EAAgB;CAAiB;AAC5C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@renr/parcel-reporter-docgen",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.9",
|
|
4
4
|
"author": "Ren Rizzolo",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Parcel reporter that generates ui docs via react-docgen-typescript",
|
|
@@ -28,12 +28,11 @@
|
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"react-docgen-typescript": "^2.4.0",
|
|
32
31
|
"@parcel/diagnostic": "^2.16.4",
|
|
33
32
|
"@parcel/plugin": "^2.16.4",
|
|
34
33
|
"dedent": "^1.7.2",
|
|
34
|
+
"react-docgen-typescript": "^2.4.0",
|
|
35
35
|
"tinyglobby": "^0.2.17",
|
|
36
|
-
"prettier": "^3.9.9",
|
|
37
36
|
"typescript": "npm:@typescript/typescript6@^6.0.2"
|
|
38
37
|
},
|
|
39
38
|
"devDependencies": {
|