@fragments-sdk/cli 0.5.2 → 0.6.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.
- package/dist/bin.js +712 -39
- package/dist/bin.js.map +1 -1
- package/dist/{chunk-ICAIQ57V.js → chunk-6JBGU74P.js} +5 -3
- package/dist/chunk-6JBGU74P.js.map +1 -0
- package/dist/{chunk-U4GQ2JTD.js → chunk-D35RGPAG.js} +412 -35
- package/dist/chunk-D35RGPAG.js.map +1 -0
- package/dist/{chunk-XNWDI6UT.js → chunk-F7ITZPDJ.js} +5 -5
- package/dist/{chunk-IOJE35DZ.js → chunk-NWQ4CJOQ.js} +3 -3
- package/dist/{chunk-V7YLRR4C.js → chunk-Q7GOHVOK.js} +3 -3
- package/dist/{chunk-2DJH4F4P.js → chunk-RVRTRESS.js} +3 -3
- package/dist/{chunk-2H2JAA3U.js → chunk-SSLQXHNX.js} +3 -3
- package/dist/{core-DKHB7FYV.js → core-SKRPJQZG.js} +4 -4
- package/dist/{generate-KL24VZVD.js → generate-7AF7WRVK.js} +5 -5
- package/dist/index.d.ts +1 -0
- package/dist/index.js +15 -7
- package/dist/index.js.map +1 -1
- package/dist/{init-NION5S3M.js → init-WKGDPYI4.js} +5 -5
- package/dist/mcp-bin.js +8 -220
- package/dist/mcp-bin.js.map +1 -1
- package/dist/scan-K6JNMCGM.js +12 -0
- package/dist/{service-RWUMZ3EW.js → service-F3E4JJM7.js} +5 -5
- package/dist/static-viewer-4LQZ5AGA.js +12 -0
- package/dist/{test-ECPEXFDN.js → test-CJDNJTPZ.js} +4 -4
- package/dist/{tokens-ITADYVPF.js → tokens-JAJABYXP.js} +6 -6
- package/dist/viewer-R3Q6WAMJ.js +1822 -0
- package/dist/viewer-R3Q6WAMJ.js.map +1 -0
- package/package.json +5 -4
- package/src/bin.ts +8 -0
- package/src/build.ts +104 -13
- package/src/cli-commands.ts +18 -0
- package/src/commands/__tests__/a11y-scoring.test.ts +278 -0
- package/src/commands/a11y-report.ts +625 -0
- package/src/commands/a11y.ts +168 -14
- package/src/commands/build.ts +16 -0
- package/src/core/auto-props.ts +464 -0
- package/src/core/schema.ts +2 -0
- package/src/core/types.ts +3 -1
- package/src/index.ts +4 -0
- package/src/mcp/server.ts +13 -220
- package/src/theme/__tests__/component-contrast.test.ts +338 -0
- package/src/theme/__tests__/contrast-validation.test.ts +326 -0
- package/src/theme/contrast.test.ts +331 -0
- package/src/theme/contrast.ts +246 -0
- package/src/theme/generator.ts +213 -1
- package/src/theme/index.ts +16 -0
- package/src/theme/types.ts +51 -0
- package/src/viewer/__tests__/a11y-fixes.test.ts +358 -0
- package/src/viewer/__tests__/viewer-integration.test.ts +2 -7
- package/src/viewer/components/AccessibilityPanel.tsx +493 -433
- package/src/viewer/components/ActionCapture.tsx +1 -1
- package/src/viewer/components/ActionsPanel.tsx +142 -183
- package/src/viewer/components/App.tsx +159 -164
- package/src/viewer/components/BottomPanel.tsx +40 -80
- package/src/viewer/components/CodePanel.tsx +9 -87
- package/src/viewer/components/CommandPalette.tsx +117 -74
- package/src/viewer/components/ComponentGraph.tsx +143 -126
- package/src/viewer/components/ComponentHeader.tsx +46 -43
- package/src/viewer/components/ContractPanel.tsx +124 -117
- package/src/viewer/components/ErrorBoundary.tsx +47 -35
- package/src/viewer/components/FigmaEmbed.tsx +18 -13
- package/src/viewer/components/FragmentEditor.tsx +126 -63
- package/src/viewer/components/HealthDashboard.tsx +146 -171
- package/src/viewer/components/HmrStatusIndicator.tsx +31 -41
- package/src/viewer/components/Icons.tsx +99 -98
- package/src/viewer/components/InteractionsPanel.tsx +317 -264
- package/src/viewer/components/IsolatedPreviewFrame.tsx +52 -27
- package/src/viewer/components/IsolatedRender.tsx +12 -6
- package/src/viewer/components/KeyboardShortcutsHelp.tsx +34 -70
- package/src/viewer/components/LandingPage.tsx +285 -305
- package/src/viewer/components/Layout.tsx +7 -9
- package/src/viewer/components/LeftSidebar.tsx +78 -108
- package/src/viewer/components/MultiViewportPreview.tsx +254 -63
- package/src/viewer/components/PreviewArea.tsx +113 -44
- package/src/viewer/components/PreviewFrameHost.tsx +6 -5
- package/src/viewer/components/PreviewPane.tsx +2 -3
- package/src/viewer/components/PreviewToolbar.tsx +61 -104
- package/src/viewer/components/PropsEditor.tsx +154 -74
- package/src/viewer/components/PropsTable.tsx +95 -82
- package/src/viewer/components/RelationsSection.tsx +71 -40
- package/src/viewer/components/ResizablePanel.tsx +158 -55
- package/src/viewer/components/RightSidebar.tsx +46 -56
- package/src/viewer/components/ScreenshotButton.tsx +12 -12
- package/src/viewer/components/SkeletonLoader.tsx +99 -83
- package/src/viewer/components/StoryRenderer.tsx +4 -11
- package/src/viewer/components/Toast.tsx +3 -67
- package/src/viewer/components/TokenStylePanel.tsx +136 -118
- package/src/viewer/components/UsageSection.tsx +26 -26
- package/src/viewer/components/VariantMatrix.tsx +140 -47
- package/src/viewer/components/VariantTabs.tsx +24 -68
- package/src/viewer/components/ViewportSelector.tsx +106 -110
- package/src/viewer/constants/ui.ts +19 -18
- package/src/viewer/entry.tsx +8 -3
- package/src/viewer/index.ts +3 -6
- package/src/viewer/preview-frame.html +21 -5
- package/src/viewer/server.ts +7 -16
- package/src/viewer/styles/globals.css +4 -4
- package/src/viewer/utils/a11y-fixes.ts +53 -30
- package/dist/chunk-ICAIQ57V.js.map +0 -1
- package/dist/chunk-U4GQ2JTD.js.map +0 -1
- package/dist/scan-ESEXV7LF.js +0 -12
- package/dist/static-viewer-O37MJ5B6.js +0 -12
- package/dist/viewer-YDGFDTK5.js +0 -11104
- package/dist/viewer-YDGFDTK5.js.map +0 -1
- package/src/viewer/postcss.config.js +0 -6
- package/src/viewer/tailwind.config.js +0 -37
- /package/dist/{chunk-XNWDI6UT.js.map → chunk-F7ITZPDJ.js.map} +0 -0
- /package/dist/{chunk-IOJE35DZ.js.map → chunk-NWQ4CJOQ.js.map} +0 -0
- /package/dist/{chunk-V7YLRR4C.js.map → chunk-Q7GOHVOK.js.map} +0 -0
- /package/dist/{chunk-2DJH4F4P.js.map → chunk-RVRTRESS.js.map} +0 -0
- /package/dist/{chunk-2H2JAA3U.js.map → chunk-SSLQXHNX.js.map} +0 -0
- /package/dist/{core-DKHB7FYV.js.map → core-SKRPJQZG.js.map} +0 -0
- /package/dist/{generate-KL24VZVD.js.map → generate-7AF7WRVK.js.map} +0 -0
- /package/dist/{init-NION5S3M.js.map → init-WKGDPYI4.js.map} +0 -0
- /package/dist/{scan-ESEXV7LF.js.map → scan-K6JNMCGM.js.map} +0 -0
- /package/dist/{service-RWUMZ3EW.js.map → service-F3E4JJM7.js.map} +0 -0
- /package/dist/{static-viewer-O37MJ5B6.js.map → static-viewer-4LQZ5AGA.js.map} +0 -0
- /package/dist/{test-ECPEXFDN.js.map → test-CJDNJTPZ.js.map} +0 -0
- /package/dist/{tokens-ITADYVPF.js.map → tokens-JAJABYXP.js.map} +0 -0
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { existsSync, statSync } from "node:fs";
|
|
2
|
+
import { dirname, extname, join, resolve } from "node:path";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import type { PropDefinition } from "./types.js";
|
|
5
|
+
|
|
6
|
+
export interface AutoDetectedPropDefinition {
|
|
7
|
+
type: PropDefinition["type"];
|
|
8
|
+
description: string;
|
|
9
|
+
required: boolean;
|
|
10
|
+
default?: unknown;
|
|
11
|
+
values?: readonly string[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface AutoPropsExtractionResult {
|
|
15
|
+
props: Record<string, AutoDetectedPropDefinition>;
|
|
16
|
+
warnings: string[];
|
|
17
|
+
resolved: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ResolvedComponentSignature {
|
|
21
|
+
propsTypeNode: ts.TypeNode | null;
|
|
22
|
+
componentNode: ts.FunctionLikeDeclarationBase | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function toPosixPath(filePath: string): string {
|
|
26
|
+
return filePath.replace(/\\/g, "/");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isFile(filePath: string): boolean {
|
|
30
|
+
if (!existsSync(filePath)) return false;
|
|
31
|
+
try {
|
|
32
|
+
return statSync(filePath).isFile();
|
|
33
|
+
} catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function resolveModulePath(basePath: string): string | null {
|
|
39
|
+
const candidates: string[] = [];
|
|
40
|
+
const extension = extname(basePath);
|
|
41
|
+
|
|
42
|
+
if (extension) {
|
|
43
|
+
candidates.push(basePath);
|
|
44
|
+
} else {
|
|
45
|
+
candidates.push(
|
|
46
|
+
`${basePath}.tsx`,
|
|
47
|
+
`${basePath}.ts`,
|
|
48
|
+
`${basePath}.jsx`,
|
|
49
|
+
`${basePath}.js`,
|
|
50
|
+
join(basePath, "index.tsx"),
|
|
51
|
+
join(basePath, "index.ts"),
|
|
52
|
+
join(basePath, "index.jsx"),
|
|
53
|
+
join(basePath, "index.js")
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (const candidate of candidates) {
|
|
58
|
+
if (isFile(candidate)) {
|
|
59
|
+
return resolve(candidate);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Resolve a component source file from the segment file path + component import path.
|
|
68
|
+
* Supports relative imports like ".", "./Button", "../components/Button".
|
|
69
|
+
*/
|
|
70
|
+
export function resolveComponentSourcePath(
|
|
71
|
+
segmentFileAbsolutePath: string,
|
|
72
|
+
componentImportPath: string | null
|
|
73
|
+
): string | null {
|
|
74
|
+
if (!componentImportPath) return null;
|
|
75
|
+
if (!componentImportPath.startsWith(".")) return null;
|
|
76
|
+
|
|
77
|
+
const segmentDir = dirname(segmentFileAbsolutePath);
|
|
78
|
+
const basePath = resolve(segmentDir, componentImportPath);
|
|
79
|
+
return resolveModulePath(basePath);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function collectTopLevelDeclarations(sourceFile: ts.SourceFile): {
|
|
83
|
+
typeDeclarations: Map<string, ts.InterfaceDeclaration | ts.TypeAliasDeclaration>;
|
|
84
|
+
functionDeclarations: Map<string, ts.FunctionDeclaration>;
|
|
85
|
+
variableDeclarations: Map<string, ts.VariableDeclaration>;
|
|
86
|
+
} {
|
|
87
|
+
const typeDeclarations = new Map<string, ts.InterfaceDeclaration | ts.TypeAliasDeclaration>();
|
|
88
|
+
const functionDeclarations = new Map<string, ts.FunctionDeclaration>();
|
|
89
|
+
const variableDeclarations = new Map<string, ts.VariableDeclaration>();
|
|
90
|
+
|
|
91
|
+
for (const node of sourceFile.statements) {
|
|
92
|
+
if (ts.isInterfaceDeclaration(node)) {
|
|
93
|
+
typeDeclarations.set(node.name.text, node);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (ts.isTypeAliasDeclaration(node)) {
|
|
98
|
+
typeDeclarations.set(node.name.text, node);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (ts.isFunctionDeclaration(node) && node.name) {
|
|
103
|
+
functionDeclarations.set(node.name.text, node);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (ts.isVariableStatement(node)) {
|
|
108
|
+
for (const declaration of node.declarationList.declarations) {
|
|
109
|
+
if (ts.isIdentifier(declaration.name)) {
|
|
110
|
+
variableDeclarations.set(declaration.name.text, declaration);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return { typeDeclarations, functionDeclarations, variableDeclarations };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function readDefaultValue(expression: ts.Expression): unknown {
|
|
120
|
+
if (ts.isStringLiteral(expression) || ts.isNoSubstitutionTemplateLiteral(expression)) {
|
|
121
|
+
return expression.text;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (ts.isNumericLiteral(expression)) {
|
|
125
|
+
return Number(expression.text);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (expression.kind === ts.SyntaxKind.TrueKeyword) return true;
|
|
129
|
+
if (expression.kind === ts.SyntaxKind.FalseKeyword) return false;
|
|
130
|
+
if (expression.kind === ts.SyntaxKind.NullKeyword) return null;
|
|
131
|
+
|
|
132
|
+
if (
|
|
133
|
+
ts.isPrefixUnaryExpression(expression) &&
|
|
134
|
+
expression.operator === ts.SyntaxKind.MinusToken &&
|
|
135
|
+
ts.isNumericLiteral(expression.operand)
|
|
136
|
+
) {
|
|
137
|
+
return -Number(expression.operand.text);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function extractDefaultValues(
|
|
144
|
+
componentNode: ts.FunctionLikeDeclarationBase | null
|
|
145
|
+
): Record<string, unknown> {
|
|
146
|
+
const defaults: Record<string, unknown> = {};
|
|
147
|
+
if (!componentNode?.parameters?.length) return defaults;
|
|
148
|
+
|
|
149
|
+
const firstParam = componentNode.parameters[0];
|
|
150
|
+
if (!ts.isObjectBindingPattern(firstParam.name)) return defaults;
|
|
151
|
+
|
|
152
|
+
for (const element of firstParam.name.elements) {
|
|
153
|
+
let propName: string | null = null;
|
|
154
|
+
|
|
155
|
+
if (element.propertyName) {
|
|
156
|
+
if (ts.isIdentifier(element.propertyName) || ts.isStringLiteral(element.propertyName)) {
|
|
157
|
+
propName = element.propertyName.text;
|
|
158
|
+
}
|
|
159
|
+
} else if (ts.isIdentifier(element.name)) {
|
|
160
|
+
propName = element.name.text;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (!propName || !element.initializer) continue;
|
|
164
|
+
|
|
165
|
+
const value = readDefaultValue(element.initializer);
|
|
166
|
+
if (value !== undefined) {
|
|
167
|
+
defaults[propName] = value;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return defaults;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function isNullishType(type: ts.Type): boolean {
|
|
175
|
+
return (
|
|
176
|
+
(type.flags & ts.TypeFlags.Null) !== 0 ||
|
|
177
|
+
(type.flags & ts.TypeFlags.Undefined) !== 0 ||
|
|
178
|
+
(type.flags & ts.TypeFlags.Void) !== 0
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function isBooleanLikeType(type: ts.Type): boolean {
|
|
183
|
+
return (
|
|
184
|
+
(type.flags & ts.TypeFlags.BooleanLike) !== 0 ||
|
|
185
|
+
type.flags === ts.TypeFlags.BooleanLiteral
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function inferPropType(
|
|
190
|
+
type: ts.Type,
|
|
191
|
+
checker: ts.TypeChecker
|
|
192
|
+
): Pick<AutoDetectedPropDefinition, "type" | "values"> {
|
|
193
|
+
const typeText = checker.typeToString(type, undefined, ts.TypeFormatFlags.NoTruncation);
|
|
194
|
+
|
|
195
|
+
if (typeText.includes("ReactNode")) {
|
|
196
|
+
return { type: "node" };
|
|
197
|
+
}
|
|
198
|
+
if (typeText.includes("ReactElement") || typeText.includes("JSX.Element")) {
|
|
199
|
+
return { type: "element" };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (type.getCallSignatures().length > 0) {
|
|
203
|
+
return { type: "function" };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (checker.isArrayType(type) || checker.isTupleType(type)) {
|
|
207
|
+
return { type: "array" };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (type.isUnion()) {
|
|
211
|
+
const nonNullableTypes = type.types.filter((unionType) => !isNullishType(unionType));
|
|
212
|
+
|
|
213
|
+
if (nonNullableTypes.length === 1) {
|
|
214
|
+
return inferPropType(nonNullableTypes[0], checker);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const stringLiteralValues = nonNullableTypes
|
|
218
|
+
.filter((unionType) => (unionType.flags & ts.TypeFlags.StringLiteral) !== 0)
|
|
219
|
+
.map((unionType) => (unionType as ts.StringLiteralType).value);
|
|
220
|
+
|
|
221
|
+
if (stringLiteralValues.length > 0 && stringLiteralValues.length === nonNullableTypes.length) {
|
|
222
|
+
return { type: "enum", values: stringLiteralValues };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (nonNullableTypes.every((unionType) => isBooleanLikeType(unionType))) {
|
|
226
|
+
return { type: "boolean" };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return { type: "union" };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if ((type.flags & ts.TypeFlags.StringLike) !== 0) {
|
|
233
|
+
return { type: "string" };
|
|
234
|
+
}
|
|
235
|
+
if ((type.flags & ts.TypeFlags.NumberLike) !== 0) {
|
|
236
|
+
return { type: "number" };
|
|
237
|
+
}
|
|
238
|
+
if ((type.flags & ts.TypeFlags.BooleanLike) !== 0) {
|
|
239
|
+
return { type: "boolean" };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if ((type.flags & ts.TypeFlags.Object) !== 0) {
|
|
243
|
+
return { type: "object" };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return { type: "custom" };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function resolveComponentSignature(
|
|
250
|
+
exportName: string,
|
|
251
|
+
declarations: ReturnType<typeof collectTopLevelDeclarations>,
|
|
252
|
+
sourceFile: ts.SourceFile
|
|
253
|
+
): ResolvedComponentSignature {
|
|
254
|
+
const visitedNames = new Set<string>();
|
|
255
|
+
|
|
256
|
+
const typeNodeFromFunction = (
|
|
257
|
+
node: ts.FunctionLikeDeclarationBase
|
|
258
|
+
): ResolvedComponentSignature => ({
|
|
259
|
+
propsTypeNode: node.parameters[0]?.type ?? null,
|
|
260
|
+
componentNode: node,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const resolveFromExpression = (expression: ts.Expression): ResolvedComponentSignature => {
|
|
264
|
+
if (ts.isParenthesizedExpression(expression)) {
|
|
265
|
+
return resolveFromExpression(expression.expression);
|
|
266
|
+
}
|
|
267
|
+
if (ts.isAsExpression(expression) || ts.isTypeAssertionExpression(expression)) {
|
|
268
|
+
return resolveFromExpression(expression.expression);
|
|
269
|
+
}
|
|
270
|
+
if (ts.isArrowFunction(expression) || ts.isFunctionExpression(expression)) {
|
|
271
|
+
return typeNodeFromFunction(expression);
|
|
272
|
+
}
|
|
273
|
+
if (ts.isIdentifier(expression)) {
|
|
274
|
+
return resolveFromIdentifier(expression.text);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (ts.isCallExpression(expression)) {
|
|
278
|
+
if (
|
|
279
|
+
ts.isPropertyAccessExpression(expression.expression) &&
|
|
280
|
+
expression.expression.name.text === "forwardRef"
|
|
281
|
+
) {
|
|
282
|
+
const forwardRefPropsType = expression.typeArguments?.[1] ?? null;
|
|
283
|
+
const innerArg = expression.arguments[0];
|
|
284
|
+
const inner = innerArg && (ts.isArrowFunction(innerArg) || ts.isFunctionExpression(innerArg))
|
|
285
|
+
? typeNodeFromFunction(innerArg)
|
|
286
|
+
: innerArg && ts.isIdentifier(innerArg)
|
|
287
|
+
? resolveFromIdentifier(innerArg.text)
|
|
288
|
+
: { propsTypeNode: null, componentNode: null };
|
|
289
|
+
|
|
290
|
+
return {
|
|
291
|
+
propsTypeNode: forwardRefPropsType ?? inner.propsTypeNode,
|
|
292
|
+
componentNode: inner.componentNode,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (
|
|
297
|
+
ts.isPropertyAccessExpression(expression.expression) &&
|
|
298
|
+
expression.expression.name.text === "memo" &&
|
|
299
|
+
expression.arguments[0]
|
|
300
|
+
) {
|
|
301
|
+
return resolveFromExpression(expression.arguments[0]);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (
|
|
305
|
+
ts.isPropertyAccessExpression(expression.expression) &&
|
|
306
|
+
expression.expression.expression.getText(sourceFile) === "Object" &&
|
|
307
|
+
expression.expression.name.text === "assign" &&
|
|
308
|
+
expression.arguments[0]
|
|
309
|
+
) {
|
|
310
|
+
return resolveFromExpression(expression.arguments[0]);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return { propsTypeNode: null, componentNode: null };
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const resolveFromVariable = (declaration: ts.VariableDeclaration): ResolvedComponentSignature => {
|
|
318
|
+
if (
|
|
319
|
+
declaration.type &&
|
|
320
|
+
ts.isTypeReferenceNode(declaration.type) &&
|
|
321
|
+
declaration.type.typeArguments?.length
|
|
322
|
+
) {
|
|
323
|
+
const typeName = declaration.type.typeName.getText(sourceFile);
|
|
324
|
+
if (typeName.includes("FC") || typeName.includes("FunctionComponent")) {
|
|
325
|
+
const componentNode =
|
|
326
|
+
declaration.initializer &&
|
|
327
|
+
(ts.isArrowFunction(declaration.initializer) || ts.isFunctionExpression(declaration.initializer))
|
|
328
|
+
? declaration.initializer
|
|
329
|
+
: null;
|
|
330
|
+
|
|
331
|
+
return {
|
|
332
|
+
propsTypeNode: declaration.type.typeArguments[0] ?? null,
|
|
333
|
+
componentNode,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (declaration.initializer) {
|
|
339
|
+
return resolveFromExpression(declaration.initializer);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return { propsTypeNode: null, componentNode: null };
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
const resolveFromIdentifier = (name: string): ResolvedComponentSignature => {
|
|
346
|
+
if (!name || visitedNames.has(name)) {
|
|
347
|
+
return { propsTypeNode: null, componentNode: null };
|
|
348
|
+
}
|
|
349
|
+
visitedNames.add(name);
|
|
350
|
+
|
|
351
|
+
const functionDeclaration = declarations.functionDeclarations.get(name);
|
|
352
|
+
if (functionDeclaration) {
|
|
353
|
+
return typeNodeFromFunction(functionDeclaration);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const variableDeclaration = declarations.variableDeclarations.get(name);
|
|
357
|
+
if (variableDeclaration) {
|
|
358
|
+
return resolveFromVariable(variableDeclaration);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return { propsTypeNode: null, componentNode: null };
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
return resolveFromIdentifier(exportName);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Extract custom component props from a source file.
|
|
369
|
+
* Custom props are identified as props whose declarations originate from the component's source file.
|
|
370
|
+
*/
|
|
371
|
+
export function extractCustomPropsFromComponentFile(
|
|
372
|
+
componentFilePath: string,
|
|
373
|
+
exportName: string
|
|
374
|
+
): AutoPropsExtractionResult {
|
|
375
|
+
const warnings: string[] = [];
|
|
376
|
+
const resolvedPath = resolve(componentFilePath);
|
|
377
|
+
|
|
378
|
+
if (!existsSync(resolvedPath)) {
|
|
379
|
+
return {
|
|
380
|
+
props: {},
|
|
381
|
+
warnings: [`Component file not found: ${resolvedPath}`],
|
|
382
|
+
resolved: false,
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const compilerOptions: ts.CompilerOptions = {
|
|
387
|
+
target: ts.ScriptTarget.ESNext,
|
|
388
|
+
module: ts.ModuleKind.ESNext,
|
|
389
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
390
|
+
jsx: ts.JsxEmit.ReactJSX,
|
|
391
|
+
allowSyntheticDefaultImports: true,
|
|
392
|
+
esModuleInterop: true,
|
|
393
|
+
skipLibCheck: true,
|
|
394
|
+
strict: false,
|
|
395
|
+
noEmit: true,
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
const program = ts.createProgram([resolvedPath], compilerOptions);
|
|
399
|
+
const sourceFile = program.getSourceFile(resolvedPath);
|
|
400
|
+
if (!sourceFile) {
|
|
401
|
+
return {
|
|
402
|
+
props: {},
|
|
403
|
+
warnings: [`Unable to parse component source: ${resolvedPath}`],
|
|
404
|
+
resolved: false,
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const checker = program.getTypeChecker();
|
|
409
|
+
const declarations = collectTopLevelDeclarations(sourceFile);
|
|
410
|
+
const signature = resolveComponentSignature(exportName, declarations, sourceFile);
|
|
411
|
+
|
|
412
|
+
if (!signature.propsTypeNode) {
|
|
413
|
+
return {
|
|
414
|
+
props: {},
|
|
415
|
+
warnings: [`Unable to resolve props type for export: ${exportName}`],
|
|
416
|
+
resolved: false,
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const propsType = checker.getTypeFromTypeNode(signature.propsTypeNode);
|
|
421
|
+
const defaultValues = extractDefaultValues(signature.componentNode);
|
|
422
|
+
const sourceFilePath = toPosixPath(sourceFile.fileName);
|
|
423
|
+
|
|
424
|
+
const extractedProps: Record<string, AutoDetectedPropDefinition> = {};
|
|
425
|
+
for (const symbol of checker.getPropertiesOfType(propsType)) {
|
|
426
|
+
const propName = symbol.getName();
|
|
427
|
+
if (propName.startsWith("_") || propName.startsWith("$")) {
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const declarationsForSymbol = symbol.getDeclarations() ?? [];
|
|
432
|
+
const localDeclarations = declarationsForSymbol.filter(
|
|
433
|
+
(declaration) => toPosixPath(declaration.getSourceFile().fileName) === sourceFilePath
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
if (localDeclarations.length === 0) {
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const referenceNode = localDeclarations[0];
|
|
441
|
+
const inferredType = inferPropType(checker.getTypeOfSymbolAtLocation(symbol, referenceNode), checker);
|
|
442
|
+
const description = ts
|
|
443
|
+
.displayPartsToString(symbol.getDocumentationComment(checker))
|
|
444
|
+
.trim();
|
|
445
|
+
|
|
446
|
+
extractedProps[propName] = {
|
|
447
|
+
type: inferredType.type,
|
|
448
|
+
description,
|
|
449
|
+
required: (symbol.getFlags() & ts.SymbolFlags.Optional) === 0,
|
|
450
|
+
...(inferredType.values && { values: inferredType.values }),
|
|
451
|
+
...(defaultValues[propName] !== undefined && { default: defaultValues[propName] }),
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (Object.keys(extractedProps).length === 0) {
|
|
456
|
+
warnings.push(`Resolved props type for ${exportName}, but no local custom props were found`);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
return {
|
|
460
|
+
props: extractedProps,
|
|
461
|
+
warnings,
|
|
462
|
+
resolved: true,
|
|
463
|
+
};
|
|
464
|
+
}
|
package/src/core/schema.ts
CHANGED
package/src/core/types.ts
CHANGED
|
@@ -185,7 +185,9 @@ export type RelationshipType =
|
|
|
185
185
|
| "sibling" // Related component at same level
|
|
186
186
|
| "parent" // This component should be wrapped by
|
|
187
187
|
| "child" // This component should contain
|
|
188
|
-
| "composition"
|
|
188
|
+
| "composition" // Used together as compound component
|
|
189
|
+
| "complementary" // Enhances or works alongside this component
|
|
190
|
+
| "used-by"; // This component is consumed by another
|
|
189
191
|
|
|
190
192
|
/**
|
|
191
193
|
* Relationship to another component
|
package/src/index.ts
CHANGED
|
@@ -41,3 +41,7 @@ export type { AnalyzeOptions, AnalyzeResult } from "./analyze.js";
|
|
|
41
41
|
|
|
42
42
|
// Static Viewer
|
|
43
43
|
export { generateStaticViewer, generateViewerFromJson } from "./static-viewer.js";
|
|
44
|
+
|
|
45
|
+
// CLI Command metadata (for docs)
|
|
46
|
+
export { CLI_COMMANDS, CLI_COMMAND_CATEGORIES } from "./cli-commands.js";
|
|
47
|
+
export type { CliCommandDef, CliOptionDef, CliCommandCategory, CliCategoryInfo } from "./cli-commands.js";
|