@finedesign/cli 0.8.1 → 0.10.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/CHANGELOG.md +14 -0
- package/README.md +4 -4
- package/data/1.25.1/catalog.json +1 -0
- package/data/1.25.1/manifest.json +16 -0
- package/data/index.json +5 -1
- package/package.json +1 -1
- package/skills/fine-design/references/imports-and-setup.md +4 -7
- package/src/cli.mjs +62 -5
- package/src/core/contracts.mjs +4 -1
- package/src/core/figma.mjs +93 -2
- package/src/core/lint.mjs +7 -179
- package/src/core/runtime.mjs +21 -10
- package/src/core/source-code.mjs +204 -0
- package/src/mcp.mjs +9 -2
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { parse } from '@babel/parser';
|
|
4
|
+
|
|
5
|
+
const CODE_FILE_PATTERN = /\.(?:[cm]?[jt]sx?|html?)$/i;
|
|
6
|
+
const IGNORED_DIRS = new Set(['node_modules', '.git', 'dist', 'lib', 'es', '.dumi', 'coverage', 'artifacts']);
|
|
7
|
+
const FINE_DESIGN_GLOBALS = new Set(['FineDesign', 'jdyDesign']);
|
|
8
|
+
|
|
9
|
+
export function isCodeFile(file) {
|
|
10
|
+
return CODE_FILE_PATTERN.test(file);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function isTypeScriptFile(file) {
|
|
14
|
+
return /\.[cm]?tsx?$/i.test(file);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function listCodeFiles(file) {
|
|
18
|
+
const stat = fs.statSync(file);
|
|
19
|
+
if (stat.isFile()) return isCodeFile(file) ? [file] : [];
|
|
20
|
+
if (!stat.isDirectory()) return [];
|
|
21
|
+
|
|
22
|
+
const results = [];
|
|
23
|
+
function walk(dir) {
|
|
24
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
25
|
+
if (entry.isDirectory()) {
|
|
26
|
+
if (!IGNORED_DIRS.has(entry.name) && !entry.name.startsWith('.')) walk(path.join(dir, entry.name));
|
|
27
|
+
} else if (entry.isFile() && isCodeFile(entry.name)) {
|
|
28
|
+
results.push(path.join(dir, entry.name));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
walk(file);
|
|
33
|
+
return results;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function lineOfIndex(content, index) {
|
|
37
|
+
let line = 1;
|
|
38
|
+
for (let i = 0; i < index; i++) {
|
|
39
|
+
if (content.charCodeAt(i) === 10) line++;
|
|
40
|
+
}
|
|
41
|
+
return line;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function codeSources(file, content) {
|
|
45
|
+
if (!/\.html?$/i.test(file)) return [{ content, startLine: 1 }];
|
|
46
|
+
return [...content.matchAll(/<!--[\s\S]*?-->|<script\b([^>]*)>([\s\S]*?)<\/script\s*>/gi)]
|
|
47
|
+
.filter((match) => {
|
|
48
|
+
if (match[1] === undefined) return false;
|
|
49
|
+
const type = match[1].match(/\btype\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i);
|
|
50
|
+
return !type || /^(?:module|(?:text|application)\/(?:javascript|ecmascript|babel|typescript))$/i.test(type[1] ?? type[2] ?? type[3]);
|
|
51
|
+
})
|
|
52
|
+
.map((match) => ({ content: match[2], startLine: lineOfIndex(content, match.index + match[0].indexOf('>') + 1) }));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function bindPattern(pattern, scope) {
|
|
56
|
+
if (!pattern) return;
|
|
57
|
+
if (pattern.type === 'Identifier') scope.bindings.set(pattern.name, null);
|
|
58
|
+
else if (pattern.type === 'ObjectPattern') {
|
|
59
|
+
for (const prop of pattern.properties) bindPattern(prop.type === 'RestElement' ? prop.argument : prop.value, scope);
|
|
60
|
+
} else if (pattern.type === 'ArrayPattern') {
|
|
61
|
+
for (const element of pattern.elements) bindPattern(element, scope);
|
|
62
|
+
} else if (pattern.type === 'AssignmentPattern') bindPattern(pattern.left, scope);
|
|
63
|
+
else if (pattern.type === 'RestElement') bindPattern(pattern.argument, scope);
|
|
64
|
+
else if (pattern.type === 'TSParameterProperty') bindPattern(pattern.parameter, scope);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function collectScopes(program) {
|
|
68
|
+
const scopes = new WeakMap();
|
|
69
|
+
function collect(node, scope) {
|
|
70
|
+
if (node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') bindPattern(node.id, scope);
|
|
71
|
+
const isFunction = /Function(?:Declaration|Expression)$|Method$/.test(node.type);
|
|
72
|
+
if (isFunction || ['Program', 'BlockStatement', 'CatchClause', 'ForStatement', 'ForInStatement', 'ForOfStatement', 'SwitchStatement', 'StaticBlock', 'ClassDeclaration', 'ClassExpression'].includes(node.type)) {
|
|
73
|
+
scope = { parent: scope, bindings: new Map(), varScope: isFunction || node.type === 'Program' || node.type === 'StaticBlock' ? null : scope.varScope };
|
|
74
|
+
scope.varScope ||= scope;
|
|
75
|
+
if (isFunction || node.type === 'ClassExpression') bindPattern(node.id, scope);
|
|
76
|
+
for (const param of node.params || []) bindPattern(param, scope);
|
|
77
|
+
if (node.type === 'CatchClause') bindPattern(node.param, scope);
|
|
78
|
+
}
|
|
79
|
+
scopes.set(node, scope);
|
|
80
|
+
if (node.type === 'ImportDeclaration') {
|
|
81
|
+
for (const spec of node.specifiers) {
|
|
82
|
+
bindPattern(spec.local, scope);
|
|
83
|
+
if (node.source.value === '@fx-ui/fine-design' && node.importKind !== 'type' && spec.importKind !== 'type') {
|
|
84
|
+
if (spec.type === 'ImportSpecifier') scope.bindings.set(spec.local.name, { component: spec.imported.name ?? spec.imported.value });
|
|
85
|
+
else if (spec.type === 'ImportNamespaceSpecifier') scope.bindings.set(spec.local.name, { namespace: true });
|
|
86
|
+
}
|
|
87
|
+
if (node.source.value === 'react' && node.importKind !== 'type' && spec.importKind !== 'type') {
|
|
88
|
+
if (spec.type === 'ImportDefaultSpecifier' || spec.type === 'ImportNamespaceSpecifier') scope.bindings.set(spec.local.name, { reactNamespace: true });
|
|
89
|
+
else if ((spec.imported.name ?? spec.imported.value) === 'createElement') scope.bindings.set(spec.local.name, { reactFactory: true });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
} else if (node.type === 'VariableDeclaration') {
|
|
93
|
+
const targetScope = node.kind === 'var' ? scope.varScope : scope;
|
|
94
|
+
for (const declaration of node.declarations) {
|
|
95
|
+
bindPattern(declaration.id, targetScope);
|
|
96
|
+
if (declaration.id.type === 'Identifier' && declaration.init) {
|
|
97
|
+
targetScope.bindings.set(declaration.id.name, { initializer: declaration.init, scope });
|
|
98
|
+
}
|
|
99
|
+
if (declaration.id.type !== 'ObjectPattern') continue;
|
|
100
|
+
for (const prop of declaration.id.properties) {
|
|
101
|
+
if (prop.type !== 'ObjectProperty' || prop.computed) continue;
|
|
102
|
+
const local = prop.value.type === 'AssignmentPattern' ? prop.value.left : prop.value;
|
|
103
|
+
if (local.type === 'Identifier') targetScope.bindings.set(local.name, { component: String(prop.key.name ?? prop.key.value), source: declaration.init, scope });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
for (const child of Object.values(node)) {
|
|
108
|
+
if (Array.isArray(child)) child.filter((item) => item?.type).forEach((item) => collect(item, scope));
|
|
109
|
+
else if (child?.type) collect(child, scope);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
collect(program);
|
|
113
|
+
return scopes;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function findBinding(scope, name) {
|
|
117
|
+
for (; scope; scope = scope.parent) {
|
|
118
|
+
if (scope.bindings.has(name)) return scope.bindings.get(name);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function isGlobalMember(node, scope, object, property) {
|
|
123
|
+
return ['MemberExpression', 'JSXMemberExpression'].includes(node?.type) && !node.computed &&
|
|
124
|
+
node.object.name === object && node.property.name === property && findBinding(scope, object) === undefined;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function isFineDesignNamespace(node, scope, visited = new Set()) {
|
|
128
|
+
if (node?.type === 'Identifier' || node?.type === 'JSXIdentifier') {
|
|
129
|
+
const binding = findBinding(scope, node.name);
|
|
130
|
+
if (binding === undefined) return FINE_DESIGN_GLOBALS.has(node.name);
|
|
131
|
+
if (binding?.namespace) return true;
|
|
132
|
+
if (!binding?.initializer || visited.has(binding)) return false;
|
|
133
|
+
visited.add(binding);
|
|
134
|
+
return isFineDesignNamespace(binding.initializer, binding.scope, visited);
|
|
135
|
+
}
|
|
136
|
+
return FINE_DESIGN_GLOBALS.has(node?.property?.name) && isGlobalMember(node, scope, 'window', node.property.name);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// shortcut: resolve imports and direct initializers in the same script, add data-flow analysis if reassigned or cross-script aliases need checking.
|
|
140
|
+
export function componentName(node, scope, visited = new Set()) {
|
|
141
|
+
if (node?.type === 'JSXIdentifier' || node?.type === 'Identifier') {
|
|
142
|
+
const binding = findBinding(scope, node.name);
|
|
143
|
+
if (!binding || visited.has(binding)) return;
|
|
144
|
+
visited.add(binding);
|
|
145
|
+
if (binding.component) {
|
|
146
|
+
if (!Object.hasOwn(binding, 'source') || isFineDesignNamespace(binding.source, binding.scope)) return binding.component;
|
|
147
|
+
const parent = componentName(binding.source, binding.scope, visited);
|
|
148
|
+
if (parent) return `${parent}.${binding.component}`;
|
|
149
|
+
}
|
|
150
|
+
return componentName(binding.initializer, binding.scope, visited);
|
|
151
|
+
}
|
|
152
|
+
if (['MemberExpression', 'JSXMemberExpression'].includes(node?.type) && !node.computed) {
|
|
153
|
+
if (isFineDesignNamespace(node.object, scope)) return node.property.name;
|
|
154
|
+
const parent = componentName(node.object, scope, visited);
|
|
155
|
+
if (parent) return `${parent}.${node.property.name}`;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function isReactCreateElement(node, scope) {
|
|
160
|
+
if (node.type === 'Identifier') {
|
|
161
|
+
const binding = findBinding(scope, node.name);
|
|
162
|
+
if (binding?.reactFactory) return true;
|
|
163
|
+
node = binding?.initializer;
|
|
164
|
+
scope = binding?.scope;
|
|
165
|
+
}
|
|
166
|
+
if (node?.type !== 'MemberExpression' || node.computed || node.property.name !== 'createElement') return false;
|
|
167
|
+
if (isGlobalMember(node.object, scope, 'window', 'React')) return true;
|
|
168
|
+
if (node.object.type !== 'Identifier') return false;
|
|
169
|
+
const binding = findBinding(scope, node.object.name);
|
|
170
|
+
return (node.object.name === 'React' && binding === undefined) ||
|
|
171
|
+
binding?.reactNamespace ||
|
|
172
|
+
isGlobalMember(binding?.initializer, binding?.scope, 'window', 'React');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function visitCode(file, content, visit, reportParseError) {
|
|
176
|
+
const typescript = isTypeScriptFile(file);
|
|
177
|
+
for (const source of codeSources(file, content)) {
|
|
178
|
+
let ast;
|
|
179
|
+
try {
|
|
180
|
+
ast = parse(source.content, {
|
|
181
|
+
sourceType: 'unambiguous', startLine: source.startLine, attachComment: false,
|
|
182
|
+
plugins: [
|
|
183
|
+
'decorators-legacy',
|
|
184
|
+
...(typescript ? [['typescript', { dts: /\.d\.[cm]?ts$/i.test(file) }]] : []),
|
|
185
|
+
...(!typescript || /tsx$/i.test(file) ? ['jsx'] : [])
|
|
186
|
+
]
|
|
187
|
+
});
|
|
188
|
+
} catch (error) {
|
|
189
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
190
|
+
reportParseError(error);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const scopes = collectScopes(ast.program);
|
|
195
|
+
function walk(node) {
|
|
196
|
+
visit(node, scopes.get(node));
|
|
197
|
+
for (const child of Object.values(node)) {
|
|
198
|
+
if (Array.isArray(child)) child.filter((item) => item?.type).forEach(walk);
|
|
199
|
+
else if (child?.type) walk(child);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
walk(ast.program);
|
|
203
|
+
}
|
|
204
|
+
}
|
package/src/mcp.mjs
CHANGED
|
@@ -31,7 +31,8 @@ export async function startMcp() {
|
|
|
31
31
|
list_browser_runtimes: 'Read the one currently hosted UMD version from its online manifest. Requires network access.',
|
|
32
32
|
get_browser_runtime: 'Resolve a component version to versioned current UMD URLs and SRI loading tags. Version is optional (defaults to hosted version). Requires network access; never falls back to another version.',
|
|
33
33
|
resolve_figma_component: 'Look up a Fine Design library node URL, library node ID, or component name in the offline Code Connect snapshot. Use Figma MCP first for page, frame, or instance links.',
|
|
34
|
-
list_figma_components: 'List all Figma-connected Fine Design components, node IDs, and Code Connect template IDs.'
|
|
34
|
+
list_figma_components: 'List all Figma-connected Fine Design components, node IDs, and Code Connect template IDs.',
|
|
35
|
+
scan_figma_components: 'Scan source files for Fine Design components used in JSX or React.createElement and return their Figma library mappings. Parse errors return an incomplete manifest with diagnostics.'
|
|
35
36
|
};
|
|
36
37
|
server.setRequestHandler(ListToolsRequestSchema, () => ({ tools: Object.entries(schemas).map(([name, schema]) => ({
|
|
37
38
|
name, description: descriptions[name], inputSchema: z.toJSONSchema(schema, { target: 'draft-07' }),
|
|
@@ -69,11 +70,17 @@ export async function startMcp() {
|
|
|
69
70
|
} else if (toolName === 'list_figma_components') {
|
|
70
71
|
const { listFigmaComponents } = await import('./core/figma.mjs');
|
|
71
72
|
result = listFigmaComponents();
|
|
73
|
+
} else if (toolName === 'scan_figma_components') {
|
|
74
|
+
const { scanFigmaComponents } = await import('./core/figma.mjs');
|
|
75
|
+
result = scanFigmaComponents(args.path);
|
|
72
76
|
} else {
|
|
73
77
|
const ver = resolveMcpVersion(args.version);
|
|
74
78
|
result = queryCatalog(loadCatalog(ver), toolName, { ...args, version: ver });
|
|
75
79
|
}
|
|
76
|
-
return {
|
|
80
|
+
return {
|
|
81
|
+
...(toolName === 'scan_figma_components' && !result.passed ? { isError: true } : {}),
|
|
82
|
+
content: [{ type: 'text', text: JSON.stringify(result) }]
|
|
83
|
+
};
|
|
77
84
|
} catch (error) {
|
|
78
85
|
return { isError: true, content: [{ type: 'text', text: JSON.stringify(errorResult(error)) }] };
|
|
79
86
|
}
|