@fragments-sdk/cli 0.11.1 → 0.13.0

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.
Files changed (89) hide show
  1. package/dist/ai-client-I6MDWNYA.js +21 -0
  2. package/dist/bin.js +419 -410
  3. package/dist/bin.js.map +1 -1
  4. package/dist/{chunk-HRFUSSZI.js → chunk-3SOAPJDX.js} +2 -2
  5. package/dist/{chunk-D5PYOXEI.js → chunk-4K7EAQ5L.js} +148 -13
  6. package/dist/{chunk-D5PYOXEI.js.map → chunk-4K7EAQ5L.js.map} +1 -1
  7. package/dist/chunk-DXX6HADE.js +443 -0
  8. package/dist/chunk-DXX6HADE.js.map +1 -0
  9. package/dist/chunk-EYXVAMEX.js +626 -0
  10. package/dist/chunk-EYXVAMEX.js.map +1 -0
  11. package/dist/{chunk-ZM4ZQZWZ.js → chunk-FO6EBJWP.js} +39 -37
  12. package/dist/chunk-FO6EBJWP.js.map +1 -0
  13. package/dist/{chunk-OQO55NKV.js → chunk-QM7SVOGF.js} +120 -12
  14. package/dist/chunk-QM7SVOGF.js.map +1 -0
  15. package/dist/{chunk-5G3VZH43.js → chunk-RF3C6LGA.js} +281 -351
  16. package/dist/chunk-RF3C6LGA.js.map +1 -0
  17. package/dist/{chunk-WXSR2II7.js → chunk-SM674YAS.js} +58 -6
  18. package/dist/chunk-SM674YAS.js.map +1 -0
  19. package/dist/chunk-SXTKFDCR.js +104 -0
  20. package/dist/chunk-SXTKFDCR.js.map +1 -0
  21. package/dist/{chunk-PW7QTQA6.js → chunk-UV5JQV3R.js} +2 -2
  22. package/dist/core/index.js +13 -1
  23. package/dist/{discovery-NEOY4MPN.js → discovery-VSGC76JN.js} +3 -3
  24. package/dist/{generate-FBHSXR3D.js → generate-QZXOXYFW.js} +4 -4
  25. package/dist/index.js +7 -6
  26. package/dist/index.js.map +1 -1
  27. package/dist/init-XK6PRUE5.js +636 -0
  28. package/dist/init-XK6PRUE5.js.map +1 -0
  29. package/dist/mcp-bin.js +2 -2
  30. package/dist/{scan-CJF2DOQW.js → scan-CHQHXWVD.js} +6 -6
  31. package/dist/scan-generate-U3RFVDTX.js +1115 -0
  32. package/dist/scan-generate-U3RFVDTX.js.map +1 -0
  33. package/dist/{service-TQYWY65E.js → service-MMEKG4MZ.js} +3 -3
  34. package/dist/{snapshot-SV2JOFZH.js → snapshot-53TUR3HW.js} +2 -2
  35. package/dist/{static-viewer-NUBFPKWH.js → static-viewer-KKCR4KXR.js} +3 -3
  36. package/dist/static-viewer-KKCR4KXR.js.map +1 -0
  37. package/dist/{test-Z5LVO724.js → test-5UCKXYSC.js} +4 -4
  38. package/dist/{tokens-CE46OTMD.js → tokens-L46MK5AW.js} +5 -5
  39. package/dist/{viewer-DLLJIMCK.js → viewer-M2EQQSGE.js} +14 -14
  40. package/dist/viewer-M2EQQSGE.js.map +1 -0
  41. package/package.json +11 -9
  42. package/src/ai-client.ts +156 -0
  43. package/src/bin.ts +99 -2
  44. package/src/build.ts +95 -33
  45. package/src/commands/__tests__/drift-sync.test.ts +252 -0
  46. package/src/commands/__tests__/scan-generate.test.ts +497 -45
  47. package/src/commands/enhance.ts +11 -35
  48. package/src/commands/govern.ts +122 -0
  49. package/src/commands/init.ts +288 -260
  50. package/src/commands/scan-generate.ts +740 -139
  51. package/src/commands/scan.ts +37 -32
  52. package/src/commands/setup.ts +143 -52
  53. package/src/commands/sync.ts +357 -0
  54. package/src/commands/validate.ts +43 -1
  55. package/src/core/component-extractor.test.ts +282 -0
  56. package/src/core/component-extractor.ts +1030 -0
  57. package/src/core/discovery.ts +93 -7
  58. package/src/service/enhance/props-extractor.ts +235 -13
  59. package/src/validators.ts +236 -0
  60. package/src/viewer/vite-plugin.ts +1 -1
  61. package/dist/chunk-5G3VZH43.js.map +0 -1
  62. package/dist/chunk-OQO55NKV.js.map +0 -1
  63. package/dist/chunk-WXSR2II7.js.map +0 -1
  64. package/dist/chunk-ZM4ZQZWZ.js.map +0 -1
  65. package/dist/init-UFGK5TCN.js +0 -867
  66. package/dist/init-UFGK5TCN.js.map +0 -1
  67. package/dist/scan-generate-SJAN5MVI.js +0 -691
  68. package/dist/scan-generate-SJAN5MVI.js.map +0 -1
  69. package/dist/viewer-DLLJIMCK.js.map +0 -1
  70. package/src/ai.ts +0 -266
  71. package/src/commands/init-framework.ts +0 -414
  72. package/src/mcp/bin.ts +0 -36
  73. package/src/migrate/bin.ts +0 -114
  74. package/src/theme/index.ts +0 -77
  75. package/src/viewer/bin.ts +0 -86
  76. package/src/viewer/cli/health.ts +0 -256
  77. package/src/viewer/cli/index.ts +0 -33
  78. package/src/viewer/cli/scan.ts +0 -124
  79. package/src/viewer/cli/utils.ts +0 -174
  80. /package/dist/{discovery-NEOY4MPN.js.map → ai-client-I6MDWNYA.js.map} +0 -0
  81. /package/dist/{chunk-HRFUSSZI.js.map → chunk-3SOAPJDX.js.map} +0 -0
  82. /package/dist/{chunk-PW7QTQA6.js.map → chunk-UV5JQV3R.js.map} +0 -0
  83. /package/dist/{scan-CJF2DOQW.js.map → discovery-VSGC76JN.js.map} +0 -0
  84. /package/dist/{generate-FBHSXR3D.js.map → generate-QZXOXYFW.js.map} +0 -0
  85. /package/dist/{service-TQYWY65E.js.map → scan-CHQHXWVD.js.map} +0 -0
  86. /package/dist/{static-viewer-NUBFPKWH.js.map → service-MMEKG4MZ.js.map} +0 -0
  87. /package/dist/{snapshot-SV2JOFZH.js.map → snapshot-53TUR3HW.js.map} +0 -0
  88. /package/dist/{test-Z5LVO724.js.map → test-5UCKXYSC.js.map} +0 -0
  89. /package/dist/{tokens-CE46OTMD.js.map → tokens-L46MK5AW.js.map} +0 -0
@@ -0,0 +1,1030 @@
1
+ /**
2
+ * ComponentExtractor — persistent LanguageService-based prop extraction.
3
+ *
4
+ * Replaces auto-props.ts with:
5
+ * - Persistent ts.LanguageService (not throwaway ts.createProgram())
6
+ * - Incremental invalidation via projectVersion
7
+ * - Compound component (Object.assign) sub-component prop extraction
8
+ * - Full type serialization to PropMeta format
9
+ *
10
+ * Zero additional dependencies — uses raw TypeScript APIs.
11
+ */
12
+
13
+ import ts from 'typescript';
14
+ import { existsSync, readFileSync, statSync, readdirSync } from 'node:fs';
15
+ import { resolve, dirname, join } from 'node:path';
16
+
17
+ // ---------------------------------------------------------------------------
18
+ // Public types
19
+ // ---------------------------------------------------------------------------
20
+
21
+ export interface ComponentExtractor {
22
+ /** Extract metadata for a single component by export name */
23
+ extract(filePath: string, exportName?: string): ComponentMeta | null;
24
+
25
+ /** Extract all exported components found in a file */
26
+ extractAll(filePath: string): ComponentMeta[];
27
+
28
+ /** Notify extractor that a file changed (incremental) */
29
+ invalidate(filePath: string): void;
30
+
31
+ /** Clean up resources */
32
+ dispose(): void;
33
+ }
34
+
35
+ export interface ComponentMeta {
36
+ name: string;
37
+ filePath: string;
38
+ description: string;
39
+ props: Record<string, PropMeta>;
40
+ composition: CompositionMeta | null;
41
+ exports: string[];
42
+ dependencies: string[];
43
+ }
44
+
45
+ export interface PropMeta {
46
+ name: string;
47
+ type: string;
48
+ typeKind: PropTypeKind;
49
+ values?: string[];
50
+ default?: string;
51
+ description?: string;
52
+ required: boolean;
53
+ source: 'local' | 'inherited';
54
+ }
55
+
56
+ export type PropTypeKind =
57
+ | 'string' | 'number' | 'boolean'
58
+ | 'enum'
59
+ | 'function'
60
+ | 'node'
61
+ | 'element'
62
+ | 'object' | 'array'
63
+ | 'union'
64
+ | 'custom';
65
+
66
+ export interface CompositionMeta {
67
+ pattern: 'compound' | 'controlled' | 'simple';
68
+ parts: PartMeta[];
69
+ required: string[];
70
+ }
71
+
72
+ export interface PartMeta {
73
+ name: string;
74
+ props: Record<string, PropMeta>;
75
+ }
76
+
77
+ // ---------------------------------------------------------------------------
78
+ // Internal types
79
+ // ---------------------------------------------------------------------------
80
+
81
+ interface ResolvedComponent {
82
+ name: string;
83
+ propsType: ts.Type | null;
84
+ componentNode: ts.Node | null;
85
+ compoundParts: Map<string, ts.Type> | null;
86
+ }
87
+
88
+ // ---------------------------------------------------------------------------
89
+ // Factory
90
+ // ---------------------------------------------------------------------------
91
+
92
+ export function createComponentExtractor(tsconfigPath?: string): ComponentExtractor {
93
+ let projectVersion = 0;
94
+ const fileVersions = new Map<string, number>();
95
+ const fileSnapshots = new Map<string, ts.IScriptSnapshot>();
96
+
97
+ // Parse tsconfig or use inferred settings
98
+ const rootDir = tsconfigPath ? dirname(resolve(tsconfigPath)) : process.cwd();
99
+ const parsedCommandLine = tsconfigPath
100
+ ? parseTsConfig(tsconfigPath)
101
+ : inferCompilerOptions(rootDir);
102
+
103
+ const scriptFileNames = new Set(parsedCommandLine.fileNames);
104
+
105
+ const host: ts.LanguageServiceHost = {
106
+ getProjectVersion: () => projectVersion.toString(),
107
+ getScriptVersion: (fileName) => (fileVersions.get(fileName) ?? 0).toString(),
108
+ getScriptSnapshot: (fileName) => {
109
+ const cached = fileSnapshots.get(fileName);
110
+ if (cached) return cached;
111
+
112
+ let text: string;
113
+ try {
114
+ text = readFileSync(fileName, 'utf-8');
115
+ } catch {
116
+ return undefined;
117
+ }
118
+ const snapshot = ts.ScriptSnapshot.fromString(text);
119
+ fileSnapshots.set(fileName, snapshot);
120
+ return snapshot;
121
+ },
122
+ getScriptFileNames: () => [...scriptFileNames],
123
+ getCompilationSettings: () => parsedCommandLine.options,
124
+ getCurrentDirectory: () => rootDir,
125
+ getDefaultLibFileName: ts.getDefaultLibFilePath,
126
+ fileExists: ts.sys.fileExists,
127
+ readFile: ts.sys.readFile,
128
+ readDirectory: ts.sys.readDirectory,
129
+ directoryExists: ts.sys.directoryExists,
130
+ getDirectories: ts.sys.getDirectories,
131
+ };
132
+
133
+ const languageService = ts.createLanguageService(host, ts.createDocumentRegistry());
134
+
135
+ function ensureFile(filePath: string): void {
136
+ const resolved = resolve(filePath);
137
+ if (!scriptFileNames.has(resolved)) {
138
+ scriptFileNames.add(resolved);
139
+ projectVersion++;
140
+ }
141
+ }
142
+
143
+ function getChecker(): ts.TypeChecker {
144
+ const program = languageService.getProgram();
145
+ if (!program) throw new Error('Failed to get program from LanguageService');
146
+ return program.getTypeChecker();
147
+ }
148
+
149
+ function getSourceFile(filePath: string): ts.SourceFile | undefined {
150
+ return languageService.getProgram()?.getSourceFile(resolve(filePath));
151
+ }
152
+
153
+ return {
154
+ extract(filePath: string, exportName?: string): ComponentMeta | null {
155
+ const resolved = resolve(filePath);
156
+ ensureFile(resolved);
157
+
158
+ const sourceFile = getSourceFile(resolved);
159
+ if (!sourceFile) return null;
160
+
161
+ const checker = getChecker();
162
+ const moduleSymbol = checker.getSymbolAtLocation(sourceFile);
163
+ if (!moduleSymbol) return null;
164
+
165
+ const exports = checker.getExportsOfModule(moduleSymbol);
166
+ const targetName = exportName ?? findPrimaryExport(exports, sourceFile);
167
+ if (!targetName) return null;
168
+
169
+ const exportSymbol = exports.find(s => s.getName() === targetName);
170
+ if (!exportSymbol) return null;
171
+
172
+ const component = resolveExportedComponent(checker, exportSymbol, sourceFile);
173
+ if (!component) return null;
174
+
175
+ return buildComponentMeta(checker, component, resolved, sourceFile, exports);
176
+ },
177
+
178
+ extractAll(filePath: string): ComponentMeta[] {
179
+ const resolved = resolve(filePath);
180
+ ensureFile(resolved);
181
+
182
+ const sourceFile = getSourceFile(resolved);
183
+ if (!sourceFile) return [];
184
+
185
+ const checker = getChecker();
186
+ const moduleSymbol = checker.getSymbolAtLocation(sourceFile);
187
+ if (!moduleSymbol) return [];
188
+
189
+ const exports = checker.getExportsOfModule(moduleSymbol);
190
+ const results: ComponentMeta[] = [];
191
+
192
+ for (const exportSymbol of exports) {
193
+ const name = exportSymbol.getName();
194
+ // Only consider PascalCase exports as potential components
195
+ if (!/^[A-Z]/.test(name)) continue;
196
+
197
+ const component = resolveExportedComponent(checker, exportSymbol, sourceFile);
198
+ if (!component) continue;
199
+
200
+ const meta = buildComponentMeta(checker, component, resolved, sourceFile, exports);
201
+ if (meta) results.push(meta);
202
+ }
203
+
204
+ return results;
205
+ },
206
+
207
+ invalidate(filePath: string): void {
208
+ const resolved = resolve(filePath);
209
+ fileVersions.set(resolved, (fileVersions.get(resolved) ?? 0) + 1);
210
+ fileSnapshots.delete(resolved);
211
+ projectVersion++;
212
+ },
213
+
214
+ dispose(): void {
215
+ languageService.dispose();
216
+ fileSnapshots.clear();
217
+ fileVersions.clear();
218
+ },
219
+ };
220
+ }
221
+
222
+ // ---------------------------------------------------------------------------
223
+ // tsconfig parsing
224
+ // ---------------------------------------------------------------------------
225
+
226
+ function parseTsConfig(tsconfigPath: string): ts.ParsedCommandLine {
227
+ const resolved = resolve(tsconfigPath);
228
+ const configFile = ts.readConfigFile(resolved, ts.sys.readFile);
229
+ if (configFile.error) {
230
+ throw new Error(`Failed to read tsconfig: ${ts.flattenDiagnosticMessageText(configFile.error.messageText, '\n')}`);
231
+ }
232
+
233
+ return ts.parseJsonConfigFileContent(
234
+ configFile.config,
235
+ ts.sys,
236
+ dirname(resolved),
237
+ undefined,
238
+ resolved,
239
+ );
240
+ }
241
+
242
+ function inferCompilerOptions(rootDir: string): ts.ParsedCommandLine {
243
+ return {
244
+ options: {
245
+ target: ts.ScriptTarget.ES2022,
246
+ module: ts.ModuleKind.ESNext,
247
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
248
+ jsx: ts.JsxEmit.ReactJSX,
249
+ allowSyntheticDefaultImports: true,
250
+ esModuleInterop: true,
251
+ skipLibCheck: true,
252
+ strict: false,
253
+ noEmit: true,
254
+ },
255
+ fileNames: [],
256
+ errors: [],
257
+ };
258
+ }
259
+
260
+ // ---------------------------------------------------------------------------
261
+ // Export resolution
262
+ // ---------------------------------------------------------------------------
263
+
264
+ /**
265
+ * Find the "primary" export — prefers the default export or the first PascalCase export.
266
+ */
267
+ function findPrimaryExport(exports: ts.Symbol[], sourceFile: ts.SourceFile): string | null {
268
+ // Default export
269
+ const defaultExport = exports.find(s => s.getName() === 'default');
270
+ if (defaultExport) return 'default';
271
+
272
+ // First PascalCase named export
273
+ for (const s of exports) {
274
+ if (/^[A-Z][a-zA-Z0-9]*$/.test(s.getName())) {
275
+ return s.getName();
276
+ }
277
+ }
278
+
279
+ return null;
280
+ }
281
+
282
+ /**
283
+ * Resolve an export symbol to a component with its props type.
284
+ * Handles: direct functions, forwardRef, memo, Object.assign, FC<Props>.
285
+ */
286
+ function resolveExportedComponent(
287
+ checker: ts.TypeChecker,
288
+ exportSymbol: ts.Symbol,
289
+ sourceFile: ts.SourceFile,
290
+ ): ResolvedComponent | null {
291
+ const name = exportSymbol.getName();
292
+
293
+ // Follow aliases (re-exports)
294
+ let symbol = exportSymbol;
295
+ if (symbol.flags & ts.SymbolFlags.Alias) {
296
+ symbol = checker.getAliasedSymbol(symbol);
297
+ }
298
+
299
+ const declarations = symbol.getDeclarations();
300
+ if (!declarations || declarations.length === 0) return null;
301
+
302
+ const declaration = declarations[0];
303
+
304
+ // Variable declaration: const X = ... (forwardRef, memo, Object.assign, arrow, FC)
305
+ if (ts.isVariableDeclaration(declaration) && declaration.initializer) {
306
+ return resolveFromExpression(checker, name, declaration.initializer, declaration, sourceFile);
307
+ }
308
+
309
+ // Function declaration: function X(props: Props) { ... }
310
+ if (ts.isFunctionDeclaration(declaration)) {
311
+ const propsType = extractPropsFromFunctionLike(checker, declaration);
312
+ return propsType ? { name, propsType, componentNode: declaration, compoundParts: null } : null;
313
+ }
314
+
315
+ // Export assignment: export default X
316
+ if (ts.isExportAssignment(declaration) && declaration.expression) {
317
+ return resolveFromExpression(checker, name, declaration.expression, null, sourceFile);
318
+ }
319
+
320
+ return null;
321
+ }
322
+
323
+ function resolveFromExpression(
324
+ checker: ts.TypeChecker,
325
+ name: string,
326
+ expression: ts.Expression,
327
+ variableDecl: ts.VariableDeclaration | null,
328
+ sourceFile: ts.SourceFile,
329
+ ): ResolvedComponent | null {
330
+ // Unwrap parentheses, type assertions
331
+ expression = unwrapExpression(expression);
332
+
333
+ // Arrow function or function expression
334
+ if (ts.isArrowFunction(expression) || ts.isFunctionExpression(expression)) {
335
+ const propsType = extractPropsFromFunctionLike(checker, expression);
336
+ return propsType ? { name, propsType, componentNode: expression, compoundParts: null } : null;
337
+ }
338
+
339
+ // Call expression: React.forwardRef, React.memo, Object.assign
340
+ if (ts.isCallExpression(expression)) {
341
+ return resolveCallExpression(checker, name, expression, variableDecl, sourceFile);
342
+ }
343
+
344
+ // Identifier reference — follow to its declaration
345
+ if (ts.isIdentifier(expression)) {
346
+ const sym = checker.getSymbolAtLocation(expression);
347
+ if (sym) {
348
+ const decls = sym.getDeclarations();
349
+ if (decls && decls.length > 0) {
350
+ const decl = decls[0];
351
+ if (ts.isVariableDeclaration(decl) && decl.initializer) {
352
+ return resolveFromExpression(checker, name, decl.initializer, decl, sourceFile);
353
+ }
354
+ if (ts.isFunctionDeclaration(decl)) {
355
+ const propsType = extractPropsFromFunctionLike(checker, decl);
356
+ return propsType ? { name, propsType, componentNode: decl, compoundParts: null } : null;
357
+ }
358
+ }
359
+ }
360
+ }
361
+
362
+ // FC<Props> typed variable
363
+ if (variableDecl?.type && ts.isTypeReferenceNode(variableDecl.type)) {
364
+ const typeName = variableDecl.type.typeName.getText(sourceFile);
365
+ if (typeName.includes('FC') || typeName.includes('FunctionComponent')) {
366
+ const typeArg = variableDecl.type.typeArguments?.[0];
367
+ if (typeArg) {
368
+ const propsType = checker.getTypeFromTypeNode(typeArg);
369
+ return { name, propsType, componentNode: expression, compoundParts: null };
370
+ }
371
+ }
372
+ }
373
+
374
+ return null;
375
+ }
376
+
377
+ function resolveCallExpression(
378
+ checker: ts.TypeChecker,
379
+ name: string,
380
+ call: ts.CallExpression,
381
+ variableDecl: ts.VariableDeclaration | null,
382
+ sourceFile: ts.SourceFile,
383
+ ): ResolvedComponent | null {
384
+ const callee = call.expression;
385
+
386
+ // Object.assign(Root, { Header, Body, ... })
387
+ if (isObjectAssignCall(callee, sourceFile)) {
388
+ return resolveObjectAssign(checker, name, call, sourceFile);
389
+ }
390
+
391
+ // React.forwardRef<Ref, Props>(fn) or forwardRef<Ref, Props>(fn)
392
+ if (isForwardRefCall(callee)) {
393
+ return resolveForwardRef(checker, name, call, sourceFile);
394
+ }
395
+
396
+ // React.memo(Component) or memo(Component)
397
+ if (isMemoCall(callee)) {
398
+ const innerArg = call.arguments[0];
399
+ if (innerArg) {
400
+ return resolveFromExpression(checker, name, innerArg, variableDecl, sourceFile);
401
+ }
402
+ }
403
+
404
+ return null;
405
+ }
406
+
407
+ // ---------------------------------------------------------------------------
408
+ // Object.assign compound component resolution
409
+ // ---------------------------------------------------------------------------
410
+
411
+ function resolveObjectAssign(
412
+ checker: ts.TypeChecker,
413
+ name: string,
414
+ call: ts.CallExpression,
415
+ sourceFile: ts.SourceFile,
416
+ ): ResolvedComponent | null {
417
+ if (call.arguments.length < 2) return null;
418
+
419
+ // First arg is the root component
420
+ const rootExpr = call.arguments[0];
421
+ const rootResult = resolveFromExpression(checker, name, rootExpr, null, sourceFile);
422
+
423
+ // Second arg is the object literal with sub-components
424
+ const subsArg = call.arguments[1];
425
+ const compoundParts = new Map<string, ts.Type>();
426
+
427
+ if (ts.isObjectLiteralExpression(subsArg)) {
428
+ for (const prop of subsArg.properties) {
429
+ let subName: string | null = null;
430
+ let subExpression: ts.Expression | null = null;
431
+
432
+ if (ts.isShorthandPropertyAssignment(prop)) {
433
+ subName = prop.name.text;
434
+ // Resolve the identifier to its type
435
+ const sym = checker.getSymbolAtLocation(prop.name);
436
+ if (sym) {
437
+ const subPropsType = extractPropsFromComponentSymbol(checker, sym);
438
+ if (subPropsType) {
439
+ compoundParts.set(subName, subPropsType);
440
+ }
441
+ }
442
+ } else if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name)) {
443
+ subName = prop.name.text;
444
+ subExpression = prop.initializer;
445
+ if (subExpression) {
446
+ const subType = extractPropsFromExpression(checker, subExpression, sourceFile);
447
+ if (subType) {
448
+ compoundParts.set(subName, subType);
449
+ }
450
+ }
451
+ }
452
+ }
453
+ }
454
+
455
+ return {
456
+ name,
457
+ propsType: rootResult?.propsType ?? null,
458
+ componentNode: rootResult?.componentNode ?? rootExpr,
459
+ compoundParts: compoundParts.size > 0 ? compoundParts : null,
460
+ };
461
+ }
462
+
463
+ /**
464
+ * Extract props type from a component symbol (function, variable, etc.)
465
+ */
466
+ function extractPropsFromComponentSymbol(checker: ts.TypeChecker, symbol: ts.Symbol): ts.Type | null {
467
+ // Follow aliases
468
+ let sym = symbol;
469
+ if (sym.flags & ts.SymbolFlags.Alias) {
470
+ sym = checker.getAliasedSymbol(sym);
471
+ }
472
+
473
+ const decls = sym.getDeclarations();
474
+ if (!decls || decls.length === 0) return null;
475
+
476
+ const decl = decls[0];
477
+
478
+ if (ts.isFunctionDeclaration(decl)) {
479
+ return extractPropsFromFunctionLike(checker, decl);
480
+ }
481
+
482
+ if (ts.isVariableDeclaration(decl) && decl.initializer) {
483
+ // forwardRef, memo, arrow function, etc.
484
+ return extractPropsFromExpressionDeep(checker, decl);
485
+ }
486
+
487
+ return null;
488
+ }
489
+
490
+ /**
491
+ * Extract props type from an arbitrary expression (used for Object.assign values).
492
+ */
493
+ function extractPropsFromExpression(checker: ts.TypeChecker, expr: ts.Expression, sourceFile: ts.SourceFile): ts.Type | null {
494
+ expr = unwrapExpression(expr);
495
+
496
+ if (ts.isIdentifier(expr)) {
497
+ const sym = checker.getSymbolAtLocation(expr);
498
+ if (sym) return extractPropsFromComponentSymbol(checker, sym);
499
+ }
500
+
501
+ if (ts.isArrowFunction(expr) || ts.isFunctionExpression(expr)) {
502
+ return extractPropsFromFunctionLike(checker, expr);
503
+ }
504
+
505
+ if (ts.isCallExpression(expr)) {
506
+ if (isForwardRefCall(expr.expression)) {
507
+ const typeArg = expr.typeArguments?.[1];
508
+ if (typeArg) return checker.getTypeFromTypeNode(typeArg);
509
+ const innerArg = expr.arguments[0];
510
+ if (innerArg && (ts.isArrowFunction(innerArg) || ts.isFunctionExpression(innerArg))) {
511
+ return extractPropsFromFunctionLike(checker, innerArg);
512
+ }
513
+ }
514
+ if (isMemoCall(expr.expression) && expr.arguments[0]) {
515
+ return extractPropsFromExpression(checker, expr.arguments[0], sourceFile);
516
+ }
517
+ }
518
+
519
+ return null;
520
+ }
521
+
522
+ /**
523
+ * Deep extraction from a variable declaration.
524
+ */
525
+ function extractPropsFromExpressionDeep(checker: ts.TypeChecker, decl: ts.VariableDeclaration): ts.Type | null {
526
+ if (!decl.initializer) return null;
527
+ const expr = unwrapExpression(decl.initializer);
528
+
529
+ if (ts.isArrowFunction(expr) || ts.isFunctionExpression(expr)) {
530
+ return extractPropsFromFunctionLike(checker, expr);
531
+ }
532
+
533
+ if (ts.isCallExpression(expr)) {
534
+ if (isForwardRefCall(expr.expression)) {
535
+ const typeArg = expr.typeArguments?.[1];
536
+ if (typeArg) return checker.getTypeFromTypeNode(typeArg);
537
+ const innerArg = expr.arguments[0];
538
+ if (innerArg && (ts.isArrowFunction(innerArg) || ts.isFunctionExpression(innerArg))) {
539
+ return extractPropsFromFunctionLike(checker, innerArg);
540
+ }
541
+ }
542
+ if (isMemoCall(expr.expression) && expr.arguments[0]) {
543
+ return extractPropsFromExpressionDeep(checker, {
544
+ ...decl,
545
+ initializer: expr.arguments[0],
546
+ } as ts.VariableDeclaration);
547
+ }
548
+ }
549
+
550
+ return null;
551
+ }
552
+
553
+ // ---------------------------------------------------------------------------
554
+ // forwardRef resolution
555
+ // ---------------------------------------------------------------------------
556
+
557
+ function resolveForwardRef(
558
+ checker: ts.TypeChecker,
559
+ name: string,
560
+ call: ts.CallExpression,
561
+ sourceFile: ts.SourceFile,
562
+ ): ResolvedComponent | null {
563
+ // Try type arguments first: forwardRef<Ref, Props>(...)
564
+ const propsTypeArg = call.typeArguments?.[1];
565
+ if (propsTypeArg) {
566
+ const propsType = checker.getTypeFromTypeNode(propsTypeArg);
567
+ const innerArg = call.arguments[0];
568
+ const componentNode = innerArg && (ts.isArrowFunction(innerArg) || ts.isFunctionExpression(innerArg))
569
+ ? innerArg : null;
570
+ return { name, propsType, componentNode, compoundParts: null };
571
+ }
572
+
573
+ // Fall back to inner function's parameter type
574
+ const innerArg = call.arguments[0];
575
+ if (innerArg) {
576
+ if (ts.isArrowFunction(innerArg) || ts.isFunctionExpression(innerArg)) {
577
+ const propsType = extractPropsFromFunctionLike(checker, innerArg);
578
+ return propsType ? { name, propsType, componentNode: innerArg, compoundParts: null } : null;
579
+ }
580
+ if (ts.isIdentifier(innerArg)) {
581
+ const sym = checker.getSymbolAtLocation(innerArg);
582
+ if (sym) {
583
+ const propsType = extractPropsFromComponentSymbol(checker, sym);
584
+ if (propsType) return { name, propsType, componentNode: innerArg, compoundParts: null };
585
+ }
586
+ }
587
+ }
588
+
589
+ return null;
590
+ }
591
+
592
+ // ---------------------------------------------------------------------------
593
+ // Props extraction from function-like declarations
594
+ // ---------------------------------------------------------------------------
595
+
596
+ /**
597
+ * Extract the props type from a function's first parameter.
598
+ */
599
+ function extractPropsFromFunctionLike(
600
+ checker: ts.TypeChecker,
601
+ func: ts.FunctionLikeDeclaration,
602
+ ): ts.Type | null {
603
+ const firstParam = func.parameters[0];
604
+ if (!firstParam) return null;
605
+
606
+ // If there's a type annotation, use it
607
+ if (firstParam.type) {
608
+ return checker.getTypeFromTypeNode(firstParam.type);
609
+ }
610
+
611
+ // Otherwise try to infer from the parameter's symbol
612
+ const paramSymbol = checker.getSymbolAtLocation(firstParam.name);
613
+ if (paramSymbol) {
614
+ return checker.getTypeOfSymbolAtLocation(paramSymbol, firstParam);
615
+ }
616
+
617
+ return null;
618
+ }
619
+
620
+ // ---------------------------------------------------------------------------
621
+ // Build ComponentMeta from resolved component
622
+ // ---------------------------------------------------------------------------
623
+
624
+ function buildComponentMeta(
625
+ checker: ts.TypeChecker,
626
+ component: ResolvedComponent,
627
+ filePath: string,
628
+ sourceFile: ts.SourceFile,
629
+ moduleExports: ts.Symbol[],
630
+ ): ComponentMeta | null {
631
+ const props: Record<string, PropMeta> = {};
632
+ const sourceFilePath = toPosixPath(sourceFile.fileName);
633
+
634
+ if (component.propsType) {
635
+ extractPropsFromType(checker, component.propsType, props, sourceFilePath);
636
+ }
637
+
638
+ // Extract defaults from the component function body
639
+ const defaults = component.componentNode
640
+ ? extractDefaultValues(component.componentNode)
641
+ : {};
642
+
643
+ // Apply defaults
644
+ for (const [propName, defaultVal] of Object.entries(defaults)) {
645
+ if (props[propName]) {
646
+ props[propName].default = defaultVal;
647
+ }
648
+ }
649
+
650
+ // Build composition meta
651
+ let composition: CompositionMeta | null = null;
652
+ if (component.compoundParts && component.compoundParts.size > 0) {
653
+ const parts: PartMeta[] = [];
654
+ for (const [partName, partType] of component.compoundParts) {
655
+ const partProps: Record<string, PropMeta> = {};
656
+ extractPropsFromType(checker, partType, partProps, sourceFilePath);
657
+ parts.push({ name: partName, props: partProps });
658
+ }
659
+
660
+ composition = {
661
+ pattern: 'compound',
662
+ parts,
663
+ required: [], // Could be inferred from usage patterns
664
+ };
665
+ }
666
+
667
+ // Extract description from JSDoc
668
+ let description = '';
669
+ const componentSymbol = moduleExports.find(s => s.getName() === component.name);
670
+ if (componentSymbol) {
671
+ description = extractJSDocDescription(checker, componentSymbol);
672
+ }
673
+
674
+ // Collect export names
675
+ const exportNames = moduleExports
676
+ .filter(s => /^[A-Z]/.test(s.getName()))
677
+ .map(s => s.getName());
678
+
679
+ // Collect import dependencies (other components imported)
680
+ const dependencies = extractDependencies(sourceFile);
681
+
682
+ return {
683
+ name: component.name,
684
+ filePath,
685
+ description,
686
+ props,
687
+ composition,
688
+ exports: exportNames,
689
+ dependencies,
690
+ };
691
+ }
692
+
693
+ // ---------------------------------------------------------------------------
694
+ // Type → PropMeta extraction
695
+ // ---------------------------------------------------------------------------
696
+
697
+ function extractPropsFromType(
698
+ checker: ts.TypeChecker,
699
+ propsType: ts.Type,
700
+ result: Record<string, PropMeta>,
701
+ sourceFilePath: string,
702
+ ): void {
703
+ for (const symbol of checker.getPropertiesOfType(propsType)) {
704
+ const propName = symbol.getName();
705
+
706
+ // Skip internal/private props
707
+ if (propName.startsWith('_') || propName.startsWith('$')) continue;
708
+
709
+ // Skip React internal props
710
+ if (propName === 'key' || propName === 'ref') continue;
711
+
712
+ const declarations = symbol.getDeclarations() ?? [];
713
+
714
+ // Determine source: local vs inherited
715
+ const isLocal = declarations.some(
716
+ d => toPosixPath(d.getSourceFile().fileName) === sourceFilePath,
717
+ );
718
+
719
+ const referenceNode = declarations[0];
720
+ if (!referenceNode) continue;
721
+
722
+ const propType = checker.getTypeOfSymbolAtLocation(symbol, referenceNode);
723
+ const serialized = serializePropType(checker, propType);
724
+ const description = ts.displayPartsToString(symbol.getDocumentationComment(checker)).trim();
725
+ const required = (symbol.flags & ts.SymbolFlags.Optional) === 0;
726
+
727
+ // Extract @default from JSDoc tags
728
+ let jsDocDefault: string | undefined;
729
+ const jsDocTags = symbol.getJsDocTags(checker);
730
+ const defaultTag = jsDocTags.find(t => t.name === 'default');
731
+ if (defaultTag?.text) {
732
+ jsDocDefault = ts.displayPartsToString(defaultTag.text).trim();
733
+ }
734
+
735
+ result[propName] = {
736
+ name: propName,
737
+ type: serialized.type,
738
+ typeKind: serialized.typeKind,
739
+ values: serialized.values,
740
+ default: jsDocDefault,
741
+ description: description || undefined,
742
+ required,
743
+ source: isLocal ? 'local' : 'inherited',
744
+ };
745
+ }
746
+ }
747
+
748
+ // ---------------------------------------------------------------------------
749
+ // Type serializer: ts.Type → PropMeta shape
750
+ // ---------------------------------------------------------------------------
751
+
752
+ function serializePropType(
753
+ checker: ts.TypeChecker,
754
+ type: ts.Type,
755
+ ): Pick<PropMeta, 'type' | 'typeKind' | 'values'> {
756
+ // Check for ReactNode/ReactElement BEFORE union decomposition,
757
+ // because ReactNode is defined as a large union in React's types.
758
+ const aliasSymbol = type.aliasSymbol;
759
+ if (aliasSymbol) {
760
+ const aliasName = aliasSymbol.getName();
761
+ if (aliasName === 'ReactNode') {
762
+ return { type: 'ReactNode', typeKind: 'node' };
763
+ }
764
+ if (aliasName === 'ReactElement') {
765
+ return { type: 'ReactElement', typeKind: 'element' };
766
+ }
767
+ }
768
+
769
+ // Handle union types (including optional which adds undefined)
770
+ if (type.isUnion()) {
771
+ const nonNullableTypes = type.types.filter(
772
+ t => !((t.flags & ts.TypeFlags.Undefined) || (t.flags & ts.TypeFlags.Null) || (t.flags & ts.TypeFlags.Void)),
773
+ );
774
+
775
+ // Single type after filtering nullable
776
+ if (nonNullableTypes.length === 1) {
777
+ return serializePropType(checker, nonNullableTypes[0]);
778
+ }
779
+
780
+ // All string literals → enum
781
+ if (nonNullableTypes.length > 0 && nonNullableTypes.every(t => t.isStringLiteral())) {
782
+ const values = nonNullableTypes.map(t => (t as ts.StringLiteralType).value);
783
+ return {
784
+ type: values.map(v => `"${v}"`).join(' | '),
785
+ typeKind: 'enum',
786
+ values,
787
+ };
788
+ }
789
+
790
+ // All boolean-like
791
+ if (nonNullableTypes.every(t => isBooleanLike(t))) {
792
+ return { type: 'boolean', typeKind: 'boolean' };
793
+ }
794
+
795
+ // Mixed union
796
+ const typeStr = checker.typeToString(type, undefined, ts.TypeFormatFlags.NoTruncation);
797
+ return { type: typeStr, typeKind: 'union' };
798
+ }
799
+
800
+ // Check for ReactNode / ReactElement
801
+ const typeStr = checker.typeToString(type, undefined, ts.TypeFormatFlags.NoTruncation);
802
+ if (typeStr.includes('ReactNode')) {
803
+ return { type: 'ReactNode', typeKind: 'node' };
804
+ }
805
+ if (typeStr.includes('ReactElement') || typeStr.includes('JSX.Element')) {
806
+ return { type: 'ReactElement', typeKind: 'element' };
807
+ }
808
+
809
+ // Function types
810
+ if (type.getCallSignatures().length > 0) {
811
+ return { type: typeStr, typeKind: 'function' };
812
+ }
813
+
814
+ // Primitives
815
+ if (type.flags & ts.TypeFlags.String) return { type: 'string', typeKind: 'string' };
816
+ if (type.flags & ts.TypeFlags.Number) return { type: 'number', typeKind: 'number' };
817
+ if (type.flags & ts.TypeFlags.Boolean || type.flags & ts.TypeFlags.BooleanLiteral) {
818
+ return { type: 'boolean', typeKind: 'boolean' };
819
+ }
820
+
821
+ // String literal (not in union)
822
+ if (type.isStringLiteral()) {
823
+ return { type: `"${type.value}"`, typeKind: 'enum', values: [type.value] };
824
+ }
825
+
826
+ // Array
827
+ if (checker.isArrayType(type) || checker.isTupleType(type)) {
828
+ return { type: typeStr, typeKind: 'array' };
829
+ }
830
+
831
+ // Object
832
+ if (type.flags & ts.TypeFlags.Object) {
833
+ return { type: typeStr, typeKind: 'object' };
834
+ }
835
+
836
+ return { type: typeStr, typeKind: 'custom' };
837
+ }
838
+
839
+ // ---------------------------------------------------------------------------
840
+ // Default value extraction
841
+ // ---------------------------------------------------------------------------
842
+
843
+ function extractDefaultValues(node: ts.Node): Record<string, string> {
844
+ const defaults: Record<string, string> = {};
845
+
846
+ // Find function-like node
847
+ let funcNode: ts.FunctionLikeDeclaration | null = null;
848
+ if (ts.isFunctionDeclaration(node) || ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
849
+ funcNode = node;
850
+ }
851
+
852
+ if (!funcNode?.parameters?.length) return defaults;
853
+
854
+ const firstParam = funcNode.parameters[0];
855
+ if (!ts.isObjectBindingPattern(firstParam.name)) return defaults;
856
+
857
+ for (const element of firstParam.name.elements) {
858
+ let propName: string | null = null;
859
+
860
+ if (element.propertyName) {
861
+ if (ts.isIdentifier(element.propertyName) || ts.isStringLiteral(element.propertyName)) {
862
+ propName = element.propertyName.text;
863
+ }
864
+ } else if (ts.isIdentifier(element.name)) {
865
+ propName = element.name.text;
866
+ }
867
+
868
+ if (!propName || !element.initializer) continue;
869
+
870
+ const value = readLiteralValue(element.initializer);
871
+ if (value !== undefined) {
872
+ defaults[propName] = value;
873
+ }
874
+ }
875
+
876
+ return defaults;
877
+ }
878
+
879
+ function readLiteralValue(expression: ts.Expression): string | undefined {
880
+ if (ts.isStringLiteral(expression) || ts.isNoSubstitutionTemplateLiteral(expression)) {
881
+ return expression.text;
882
+ }
883
+ if (ts.isNumericLiteral(expression)) {
884
+ return expression.text;
885
+ }
886
+ if (expression.kind === ts.SyntaxKind.TrueKeyword) return 'true';
887
+ if (expression.kind === ts.SyntaxKind.FalseKeyword) return 'false';
888
+ if (expression.kind === ts.SyntaxKind.NullKeyword) return 'null';
889
+ if (
890
+ ts.isPrefixUnaryExpression(expression) &&
891
+ expression.operator === ts.SyntaxKind.MinusToken &&
892
+ ts.isNumericLiteral(expression.operand)
893
+ ) {
894
+ return `-${expression.operand.text}`;
895
+ }
896
+
897
+ return undefined;
898
+ }
899
+
900
+ // ---------------------------------------------------------------------------
901
+ // JSDoc extraction
902
+ // ---------------------------------------------------------------------------
903
+
904
+ function extractJSDocDescription(checker: ts.TypeChecker, symbol: ts.Symbol): string {
905
+ // Follow aliases
906
+ let sym = symbol;
907
+ if (sym.flags & ts.SymbolFlags.Alias) {
908
+ sym = checker.getAliasedSymbol(sym);
909
+ }
910
+
911
+ const docComment = ts.displayPartsToString(sym.getDocumentationComment(checker)).trim();
912
+ if (docComment) return docComment;
913
+
914
+ // Try to get from the Props interface
915
+ const decls = sym.getDeclarations();
916
+ if (decls) {
917
+ for (const decl of decls) {
918
+ // Look for the Props type
919
+ const sourceFile = decl.getSourceFile();
920
+ for (const stmt of sourceFile.statements) {
921
+ if (
922
+ (ts.isInterfaceDeclaration(stmt) || ts.isTypeAliasDeclaration(stmt)) &&
923
+ stmt.name.text === `${symbol.getName()}Props`
924
+ ) {
925
+ const propsDoc = ts.displayPartsToString(
926
+ checker.getSymbolAtLocation(stmt.name)?.getDocumentationComment(checker) ?? [],
927
+ ).trim();
928
+ if (propsDoc) return propsDoc;
929
+ }
930
+ }
931
+ }
932
+ }
933
+
934
+ return '';
935
+ }
936
+
937
+ // ---------------------------------------------------------------------------
938
+ // Import dependency extraction
939
+ // ---------------------------------------------------------------------------
940
+
941
+ function extractDependencies(sourceFile: ts.SourceFile): string[] {
942
+ const deps: string[] = [];
943
+ for (const stmt of sourceFile.statements) {
944
+ if (!ts.isImportDeclaration(stmt)) continue;
945
+ if (!ts.isStringLiteral(stmt.moduleSpecifier)) continue;
946
+
947
+ const modulePath = stmt.moduleSpecifier.text;
948
+ // Only relative imports to other components
949
+ if (!modulePath.startsWith('.') && !modulePath.startsWith('/')) continue;
950
+
951
+ const clause = stmt.importClause;
952
+ if (!clause) continue;
953
+
954
+ // Default import
955
+ if (clause.name && /^[A-Z]/.test(clause.name.text)) {
956
+ deps.push(clause.name.text);
957
+ }
958
+
959
+ // Named imports
960
+ if (clause.namedBindings && ts.isNamedImports(clause.namedBindings)) {
961
+ for (const element of clause.namedBindings.elements) {
962
+ if (/^[A-Z]/.test(element.name.text)) {
963
+ deps.push(element.name.text);
964
+ }
965
+ }
966
+ }
967
+ }
968
+ return deps;
969
+ }
970
+
971
+ // ---------------------------------------------------------------------------
972
+ // Helpers
973
+ // ---------------------------------------------------------------------------
974
+
975
+ function unwrapExpression(expr: ts.Expression): ts.Expression {
976
+ while (true) {
977
+ if (ts.isParenthesizedExpression(expr)) {
978
+ expr = expr.expression;
979
+ } else if (ts.isAsExpression(expr) || ts.isTypeAssertionExpression(expr)) {
980
+ expr = expr.expression;
981
+ } else {
982
+ return expr;
983
+ }
984
+ }
985
+ }
986
+
987
+ function isObjectAssignCall(callee: ts.Expression, sourceFile: ts.SourceFile): boolean {
988
+ if (
989
+ ts.isPropertyAccessExpression(callee) &&
990
+ ts.isIdentifier(callee.expression) &&
991
+ callee.expression.text === 'Object' &&
992
+ callee.name.text === 'assign'
993
+ ) {
994
+ return true;
995
+ }
996
+ return false;
997
+ }
998
+
999
+ function isForwardRefCall(callee: ts.Expression): boolean {
1000
+ // React.forwardRef(...)
1001
+ if (ts.isPropertyAccessExpression(callee) && callee.name.text === 'forwardRef') {
1002
+ return true;
1003
+ }
1004
+ // forwardRef(...)
1005
+ if (ts.isIdentifier(callee) && callee.text === 'forwardRef') {
1006
+ return true;
1007
+ }
1008
+ return false;
1009
+ }
1010
+
1011
+ function isMemoCall(callee: ts.Expression): boolean {
1012
+ if (ts.isPropertyAccessExpression(callee) && callee.name.text === 'memo') {
1013
+ return true;
1014
+ }
1015
+ if (ts.isIdentifier(callee) && callee.text === 'memo') {
1016
+ return true;
1017
+ }
1018
+ return false;
1019
+ }
1020
+
1021
+ function isBooleanLike(type: ts.Type): boolean {
1022
+ return (
1023
+ (type.flags & ts.TypeFlags.BooleanLike) !== 0 ||
1024
+ type.flags === ts.TypeFlags.BooleanLiteral
1025
+ );
1026
+ }
1027
+
1028
+ function toPosixPath(filePath: string): string {
1029
+ return filePath.replace(/\\/g, '/');
1030
+ }