@algotao/uiscope 0.0.0-stage → 0.1.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/.agents/plugins/marketplace.json +21 -0
- package/.cursor-plugin/plugin.json +8 -0
- package/LICENSE +21 -0
- package/README.md +255 -2
- package/README.zh-CN.md +256 -0
- package/bin/uiscope.js +8 -0
- package/package.json +89 -3
- package/plugin.json +17 -0
- package/presets/README.md +11 -0
- package/presets/ocpui/index.ts +107 -0
- package/skills/README.md +7 -0
- package/skills/uiscope-ux-optimize/SKILL.md +267 -0
- package/skills/uiscope-ux-optimize/scripts/uiscope.mjs +40 -0
- package/src/__tests__/cli-parity.spec.ts +59 -0
- package/src/__tests__/cli-read.spec.ts +141 -0
- package/src/__tests__/component-tree.spec.ts +72 -0
- package/src/__tests__/enhance-core-ux.spec.ts +72 -0
- package/src/__tests__/enhance-feedback-states.spec.ts +104 -0
- package/src/__tests__/facet-analyze.spec.ts +55 -0
- package/src/__tests__/fixtures/exmarket-integration.config.ts +19 -0
- package/src/__tests__/format-scan-stats.spec.ts +32 -0
- package/src/__tests__/install-skill.spec.ts +41 -0
- package/src/__tests__/merge-cli-config.spec.ts +56 -0
- package/src/__tests__/react-tree.spec.ts +29 -0
- package/src/__tests__/read-scan-report-boundaries.spec.ts +139 -0
- package/src/__tests__/read-scan-report.spec.ts +91 -0
- package/src/__tests__/resolve-enhancements.spec.ts +21 -0
- package/src/__tests__/resolve-presets.spec.ts +51 -0
- package/src/__tests__/resolve-scan-scope.spec.ts +28 -0
- package/src/__tests__/run-scan-ux.spec.ts +51 -0
- package/src/__tests__/scan-progress.spec.ts +52 -0
- package/src/__tests__/style-path.spec.ts +11 -0
- package/src/__tests__/style-rules.spec.ts +25 -0
- package/src/__tests__/template-hints.spec.ts +19 -0
- package/src/__tests__/wrapper-roles.spec.ts +31 -0
- package/src/adapters/react/collect-react-files.ts +17 -0
- package/src/adapters/react/component-tree.tsx.ts +79 -0
- package/src/adapters/react/index.ts +3 -0
- package/src/adapters/react/parse-jsx-imports.ts +38 -0
- package/src/adapters/vue/index.ts +4 -0
- package/src/cli/brand.ts +35 -0
- package/src/cli/install-skill.ts +90 -0
- package/src/cli/main.ts +578 -0
- package/src/cli/merge-cli-config.ts +86 -0
- package/src/config/defaults.ts +65 -0
- package/src/config/frameworks.ts +20 -0
- package/src/config/load-config.ts +81 -0
- package/src/config/normalize-legacy-config.ts +39 -0
- package/src/config/preset-types.ts +16 -0
- package/src/config/resolve-preset-library-roots.ts +49 -0
- package/src/config/resolve-presets.ts +142 -0
- package/src/config/resolve-scan-scope.ts +12 -0
- package/src/config/resolve-thresholds.ts +12 -0
- package/src/enhance/dir-utils.ts +25 -0
- package/src/enhance/registry.ts +107 -0
- package/src/enhance/resolve-enhancements.ts +56 -0
- package/src/enhance/rules/action-semantics.ts +60 -0
- package/src/enhance/rules/core-ux.ts +139 -0
- package/src/enhance/rules/facet-hygiene.ts +44 -0
- package/src/enhance/rules/feedback-states.ts +39 -0
- package/src/enhance/rules/form-a11y.ts +62 -0
- package/src/enhance/rules/hollow-shell.ts +39 -0
- package/src/enhance/rules/i18n-hints.ts +29 -0
- package/src/enhance/rules/list-templating.ts +38 -0
- package/src/enhance/rules/page-rollup.ts +82 -0
- package/src/enhance/tree-walk.ts +29 -0
- package/src/enhance/types.ts +48 -0
- package/src/enhance/ux-rule-pack.ts +40 -0
- package/src/enhance/workbench-defaults.ts +28 -0
- package/src/index.ts +80 -0
- package/src/manifest/consumer-wrapper-roles.ts +34 -0
- package/src/manifest/library-components.ts +46 -0
- package/src/manifest/library-style-classes.ts +31 -0
- package/src/manifest/library-style-whitelist.ts +82 -0
- package/src/manifest/library-ux-taxonomy.ts +103 -0
- package/src/paths/monorepo-root.ts +13 -0
- package/src/report/facet-output-limits.ts +56 -0
- package/src/report/format-scan-stats.ts +18 -0
- package/src/report/format-ux-scan-report.ts +59 -0
- package/src/report/markdown-report.ts +108 -0
- package/src/report/print-facets.ts +129 -0
- package/src/report/print-styles.ts +20 -0
- package/src/report/print-tree.ts +70 -0
- package/src/report/print-ux-findings.ts +53 -0
- package/src/report/read-scan-report.ts +201 -0
- package/src/report/run-report-scenarios.ts +139 -0
- package/src/run-scan.ts +341 -0
- package/src/scan/collect-source-files.ts +36 -0
- package/src/scan/component-tree.ts +155 -0
- package/src/scan/extract-node-attrs.ts +49 -0
- package/src/scan/facet-analyze.ts +318 -0
- package/src/scan/file-uses-library.ts +37 -0
- package/src/scan/library-meta.ts +71 -0
- package/src/scan/parse-imports.ts +88 -0
- package/src/scan/scan-progress.ts +89 -0
- package/src/scan/style-path.ts +26 -0
- package/src/scan/style-rules.ts +127 -0
- package/src/scan/style-trace.ts +181 -0
- package/src/scan/template-hints.ts +70 -0
- package/src/types.ts +274 -0
package/src/run-scan.ts
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { getLibraryStyleClassManifest } from './manifest/library-style-classes.js';
|
|
4
|
+
import type {
|
|
5
|
+
UiscopeConfig,
|
|
6
|
+
UiscopeFramework,
|
|
7
|
+
ScanResult,
|
|
8
|
+
UxEnhancementId,
|
|
9
|
+
UxEnhancementOptionalId,
|
|
10
|
+
} from './types.js';
|
|
11
|
+
import { resolveRoots } from './config/load-config.js';
|
|
12
|
+
import { resolveOnlyLibraryConsumers } from './config/resolve-scan-scope.js';
|
|
13
|
+
import { frameworkIncludes, resolveFrameworks } from './config/frameworks.js';
|
|
14
|
+
import {
|
|
15
|
+
collectStyleFiles,
|
|
16
|
+
collectVueFiles,
|
|
17
|
+
isUnderLibraryRoots,
|
|
18
|
+
} from './scan/collect-source-files.js';
|
|
19
|
+
import {
|
|
20
|
+
buildReactComponentTree,
|
|
21
|
+
collectReactFiles,
|
|
22
|
+
reactFileUsesLibrary,
|
|
23
|
+
} from './adapters/react/index.js';
|
|
24
|
+
import { buildFileComponentTree } from './scan/component-tree.js';
|
|
25
|
+
import { fileUsesLibrary } from './scan/file-uses-library.js';
|
|
26
|
+
import {
|
|
27
|
+
analyzeStyleOverrides,
|
|
28
|
+
collectStyleOccurrences,
|
|
29
|
+
} from './scan/style-rules.js';
|
|
30
|
+
import { buildStyleReloadChains } from './scan/style-trace.js';
|
|
31
|
+
import { resolveThresholds } from './config/resolve-thresholds.js';
|
|
32
|
+
import { analyzeFileFacets, facetSlice } from './scan/facet-analyze.js';
|
|
33
|
+
import { buildUxRoleResolver } from './manifest/consumer-wrapper-roles.js';
|
|
34
|
+
import { resolveUxRulePack } from './enhance/ux-rule-pack.js';
|
|
35
|
+
import { discoverLibraryComponentNames } from './manifest/library-components.js';
|
|
36
|
+
import { runEnhancementBundle } from './enhance/registry.js';
|
|
37
|
+
import { modeSupportsEnhancements, resolveEnhancements } from './enhance/resolve-enhancements.js';
|
|
38
|
+
import { emitScanProgress, type ScanProgressHandler } from './scan/scan-progress.js';
|
|
39
|
+
|
|
40
|
+
export type ScanMode = 'all' | 'tree' | 'styles' | 'facets' | 'ux';
|
|
41
|
+
|
|
42
|
+
export interface ScanOptions {
|
|
43
|
+
cwd: string;
|
|
44
|
+
config: UiscopeConfig;
|
|
45
|
+
mode: ScanMode;
|
|
46
|
+
onlyLibraryTrees: boolean;
|
|
47
|
+
/** 可选增强项(core-ux 由 mode 自动包含) */
|
|
48
|
+
enhance?: UxEnhancementOptionalId[];
|
|
49
|
+
enhanceAll?: boolean;
|
|
50
|
+
/** ux 模式下是否仍跑样式(默认 false,避免重复 IO) */
|
|
51
|
+
withStyles?: boolean;
|
|
52
|
+
/** 扫描进度(CLI / 脚本可挂终端 reporter) */
|
|
53
|
+
onProgress?: ScanProgressHandler;
|
|
54
|
+
/** CLI 覆盖 config.frameworks */
|
|
55
|
+
frameworks?: UiscopeFramework[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function runScan(options: ScanOptions): Promise<ScanResult> {
|
|
59
|
+
const roots = resolveRoots(options.cwd, options.config.roots);
|
|
60
|
+
|
|
61
|
+
const libraryRoots =
|
|
62
|
+
options.config.libraryRoots.length > 0
|
|
63
|
+
? options.config.libraryRoots.map((r) => path.resolve(options.cwd, r))
|
|
64
|
+
: [];
|
|
65
|
+
|
|
66
|
+
const catalogRoot = libraryRoots.length > 0 ? libraryRoots[0] : '';
|
|
67
|
+
|
|
68
|
+
const onlyLibraryConsumers = resolveOnlyLibraryConsumers(options.config);
|
|
69
|
+
|
|
70
|
+
const config: UiscopeConfig = {
|
|
71
|
+
...options.config,
|
|
72
|
+
libraryRoots,
|
|
73
|
+
onlyLibraryConsumers,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const frameworks = resolveFrameworks(config, options.frameworks);
|
|
77
|
+
const scanVue = frameworkIncludes(frameworks, 'vue');
|
|
78
|
+
const scanReact = frameworkIncludes(frameworks, 'react');
|
|
79
|
+
|
|
80
|
+
const vuePaths = scanVue ? await collectVueFiles(roots, config.ignore) : [];
|
|
81
|
+
const reactPaths = scanReact ? await collectReactFiles(roots, config.ignore) : [];
|
|
82
|
+
const cssPaths = await collectStyleFiles(roots, config.ignore);
|
|
83
|
+
emitScanProgress(options.onProgress, {
|
|
84
|
+
phase: 'collect',
|
|
85
|
+
current: 1,
|
|
86
|
+
total: 1,
|
|
87
|
+
label: `${vuePaths.length} vue, ${reactPaths.length} react, ${cssPaths.length} css`,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const thresholds = resolveThresholds(config);
|
|
91
|
+
const facetCfg = facetSlice(thresholds.facet);
|
|
92
|
+
const libraryNames = new Set(discoverLibraryComponentNames(catalogRoot));
|
|
93
|
+
const classNames = new Set(getLibraryStyleClassManifest());
|
|
94
|
+
const styleExclude = thresholds.style.excludeGlobs;
|
|
95
|
+
|
|
96
|
+
const needTrees = options.mode !== 'styles';
|
|
97
|
+
const needFacets =
|
|
98
|
+
options.mode === 'facets' || options.mode === 'all' || options.mode === 'ux';
|
|
99
|
+
const needStyles =
|
|
100
|
+
options.mode === 'styles'
|
|
101
|
+
|| options.mode === 'all'
|
|
102
|
+
|| (options.mode === 'ux' && options.withStyles);
|
|
103
|
+
|
|
104
|
+
const trees = [];
|
|
105
|
+
const consumerSources: Array<{ file: string; source: string }> = [];
|
|
106
|
+
let vueFilesLibraryConsumers = 0;
|
|
107
|
+
let reactFilesLibraryConsumers = 0;
|
|
108
|
+
|
|
109
|
+
for (let vi = 0; vi < vuePaths.length; vi++) {
|
|
110
|
+
const file = vuePaths[vi];
|
|
111
|
+
emitScanProgress(options.onProgress, {
|
|
112
|
+
phase: 'vue',
|
|
113
|
+
current: vi + 1,
|
|
114
|
+
total: vuePaths.length,
|
|
115
|
+
label: file,
|
|
116
|
+
});
|
|
117
|
+
const source = fs.readFileSync(file, 'utf8');
|
|
118
|
+
const usesLibrary = fileUsesLibrary(source, config, libraryNames);
|
|
119
|
+
if (usesLibrary) vueFilesLibraryConsumers++;
|
|
120
|
+
if (config.onlyLibraryConsumers && !usesLibrary) continue;
|
|
121
|
+
|
|
122
|
+
consumerSources.push({ file, source });
|
|
123
|
+
if (!needTrees) continue;
|
|
124
|
+
|
|
125
|
+
const report = buildFileComponentTree(file, source, config, libraryNames, catalogRoot);
|
|
126
|
+
if (!report) continue;
|
|
127
|
+
const shouldIncludeTree = !options.onlyLibraryTrees || report.usesLibrary || usesLibrary;
|
|
128
|
+
if (shouldIncludeTree) trees.push(report);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
for (let ri = 0; ri < reactPaths.length; ri++) {
|
|
132
|
+
const file = reactPaths[ri];
|
|
133
|
+
emitScanProgress(options.onProgress, {
|
|
134
|
+
phase: 'vue',
|
|
135
|
+
current: ri + 1,
|
|
136
|
+
total: reactPaths.length,
|
|
137
|
+
label: file,
|
|
138
|
+
});
|
|
139
|
+
const source = fs.readFileSync(file, 'utf8');
|
|
140
|
+
const usesLibrary = reactFileUsesLibrary(source, config, libraryNames);
|
|
141
|
+
if (usesLibrary) reactFilesLibraryConsumers++;
|
|
142
|
+
if (config.onlyLibraryConsumers && !usesLibrary) continue;
|
|
143
|
+
|
|
144
|
+
consumerSources.push({ file, source });
|
|
145
|
+
if (!needTrees) continue;
|
|
146
|
+
|
|
147
|
+
const report = buildReactComponentTree(file, source, config, libraryNames);
|
|
148
|
+
const shouldIncludeTree = !options.onlyLibraryTrees || report.usesLibrary || usesLibrary;
|
|
149
|
+
if (shouldIncludeTree) trees.push(report);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let styleOverrides: ScanResult['styleOverrides'] = [];
|
|
153
|
+
let styleChains: ScanResult['styleChains'] = [];
|
|
154
|
+
let facetReports: ScanResult['facetReports'] = [];
|
|
155
|
+
let uxFindings: ScanResult['uxFindings'] = [];
|
|
156
|
+
let enhancementsRun: UxEnhancementId[] = [];
|
|
157
|
+
let warnings: string[] = [];
|
|
158
|
+
|
|
159
|
+
const roleFn = buildUxRoleResolver(config);
|
|
160
|
+
|
|
161
|
+
if (needFacets) {
|
|
162
|
+
for (let fi = 0; fi < trees.length; fi++) {
|
|
163
|
+
const report = trees[fi];
|
|
164
|
+
emitScanProgress(options.onProgress, {
|
|
165
|
+
phase: 'facets',
|
|
166
|
+
current: fi + 1,
|
|
167
|
+
total: trees.length,
|
|
168
|
+
label: report.file,
|
|
169
|
+
});
|
|
170
|
+
facetReports.push(analyzeFileFacets(report.file, report.tree, facetCfg, config, roleFn));
|
|
171
|
+
}
|
|
172
|
+
for (const fr of facetReports) {
|
|
173
|
+
for (const facet of fr.facets) {
|
|
174
|
+
if (
|
|
175
|
+
facet.templateSignals.length
|
|
176
|
+
&& facet.templatingRate >= thresholds.facet.templateWarnRate
|
|
177
|
+
) {
|
|
178
|
+
warnings.push(
|
|
179
|
+
`[uiscope:facet] ${relFile(fr.file)} 切面 \`${facet.id}\` (${facet.kind}) 模板化率 ${facet.templatingRate}`,
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (fr.facetCount >= thresholds.facet.multiFacetWarnCount) {
|
|
184
|
+
warnings.push(
|
|
185
|
+
`[uiscope:facet] ${relFile(fr.file)} 含 ${fr.facetCount} 个交互切面(筛选/列表/内容等可能并存)`,
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
enhancementsRun = resolveEnhancements({
|
|
191
|
+
mode: options.mode,
|
|
192
|
+
cliEnhance: options.enhance,
|
|
193
|
+
cliEnhanceAll: options.enhanceAll,
|
|
194
|
+
configDefault: options.config.defaultEnhancements,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
if (enhancementsRun.length) {
|
|
198
|
+
emitScanProgress(options.onProgress, {
|
|
199
|
+
phase: 'ux-enhance',
|
|
200
|
+
current: 0,
|
|
201
|
+
total: enhancementsRun.length,
|
|
202
|
+
label: enhancementsRun.join(','),
|
|
203
|
+
});
|
|
204
|
+
const rulePack = await resolveUxRulePack(options.cwd, config);
|
|
205
|
+
uxFindings = runEnhancementBundle(
|
|
206
|
+
{ cwd: options.cwd, config, trees, facetReports, rulePack },
|
|
207
|
+
enhancementsRun,
|
|
208
|
+
);
|
|
209
|
+
emitScanProgress(options.onProgress, {
|
|
210
|
+
phase: 'ux-enhance',
|
|
211
|
+
current: enhancementsRun.length,
|
|
212
|
+
total: enhancementsRun.length,
|
|
213
|
+
label: `${uxFindings.length} findings`,
|
|
214
|
+
});
|
|
215
|
+
for (const f of uxFindings) {
|
|
216
|
+
if (f.severity === 'warn') {
|
|
217
|
+
warnings.push(`[uiscope:ux] ${f.message}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (needStyles) {
|
|
224
|
+
const cssSources: Array<{ file: string; source: string }> = [];
|
|
225
|
+
for (let ci = 0; ci < cssPaths.length; ci++) {
|
|
226
|
+
const file = cssPaths[ci];
|
|
227
|
+
emitScanProgress(options.onProgress, {
|
|
228
|
+
phase: 'styles',
|
|
229
|
+
current: ci + 1,
|
|
230
|
+
total: cssPaths.length,
|
|
231
|
+
label: file,
|
|
232
|
+
});
|
|
233
|
+
cssSources.push({ file, source: fs.readFileSync(file, 'utf8') });
|
|
234
|
+
}
|
|
235
|
+
const librarySourcesOnly = config.onlyLibraryConsumers
|
|
236
|
+
? consumerSources
|
|
237
|
+
: consumerSources.filter(
|
|
238
|
+
(v) =>
|
|
239
|
+
v.file.endsWith('.vue')
|
|
240
|
+
? fileUsesLibrary(v.source, config, libraryNames)
|
|
241
|
+
: reactFileUsesLibrary(v.source, config, libraryNames),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
const vueSourcesOnly = librarySourcesOnly.filter((v) => v.file.endsWith('.vue'));
|
|
245
|
+
const byClass = collectStyleOccurrences(vueSourcesOnly, cssSources, config, styleExclude);
|
|
246
|
+
const analysis = analyzeStyleOverrides(byClass, thresholds.style.overrideRuleCount);
|
|
247
|
+
styleOverrides = analysis.hits;
|
|
248
|
+
warnings = warnings.concat(analysis.warnings);
|
|
249
|
+
|
|
250
|
+
styleChains = buildStyleReloadChains(vueSourcesOnly, cssSources, config, classNames, styleExclude);
|
|
251
|
+
for (const link of styleChains) {
|
|
252
|
+
if (
|
|
253
|
+
thresholds.style.warnOrphanOverrides
|
|
254
|
+
&& link.overrideCount > thresholds.style.overrideRuleCount
|
|
255
|
+
&& link.usageAnchors.length === 0
|
|
256
|
+
) {
|
|
257
|
+
warnings.push(
|
|
258
|
+
`[uiscope] ".${link.className}" 无消费方引用锚点但有 ${link.overrideCount} 条外部重载规则`,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (options.mode !== 'styles' && needTrees) {
|
|
264
|
+
for (const report of trees) {
|
|
265
|
+
if (!report.usesLibrary && !fileUsesLibrary(fs.readFileSync(report.file, 'utf8'), config, libraryNames)) {
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
const nestedLibrary = findNestedLibraryInTree(report.tree);
|
|
269
|
+
for (const item of nestedLibrary) {
|
|
270
|
+
if (item.depth > thresholds.tree.maxLibraryNestDepth) {
|
|
271
|
+
warnings.push(
|
|
272
|
+
`[uiscope] ${report.file}: 设计库组件 "${item.name}" 嵌套深度 ${item.depth},请检查组合是否合理`,
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
} else if (needTrees && options.mode === 'ux') {
|
|
279
|
+
for (const report of trees) {
|
|
280
|
+
const nestedLibrary = findNestedLibraryInTree(report.tree);
|
|
281
|
+
for (const item of nestedLibrary) {
|
|
282
|
+
if (item.depth > thresholds.tree.maxLibraryNestDepth) {
|
|
283
|
+
warnings.push(
|
|
284
|
+
`[uiscope] ${report.file}: 设计库组件 "${item.name}" 嵌套深度 ${item.depth},请检查组合是否合理`,
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
emitScanProgress(options.onProgress, {
|
|
292
|
+
phase: 'done',
|
|
293
|
+
current: 1,
|
|
294
|
+
total: 1,
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
return {
|
|
298
|
+
trees,
|
|
299
|
+
styleOverrides,
|
|
300
|
+
styleChains,
|
|
301
|
+
facetReports,
|
|
302
|
+
uxFindings,
|
|
303
|
+
enhancementsRun,
|
|
304
|
+
warnings,
|
|
305
|
+
stats: {
|
|
306
|
+
vueFilesScanned: config.onlyLibraryConsumers
|
|
307
|
+
? consumerSources.filter((f) => f.file.endsWith('.vue')).length
|
|
308
|
+
: vuePaths.length,
|
|
309
|
+
vueFilesLibraryConsumers,
|
|
310
|
+
reactFilesScanned: config.onlyLibraryConsumers
|
|
311
|
+
? consumerSources.filter((f) => /\.(tsx|jsx)$/.test(f.file)).length
|
|
312
|
+
: reactPaths.length,
|
|
313
|
+
reactFilesLibraryConsumers,
|
|
314
|
+
cssFilesScanned: needStyles ? cssPaths.length : 0,
|
|
315
|
+
facetRegions: facetReports.reduce((n, r) => n + r.facetCount, 0),
|
|
316
|
+
uxFindings: uxFindings.length,
|
|
317
|
+
},
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function relFile(file: string): string {
|
|
322
|
+
return file.replace(/\\/g, '/').split('/').slice(-3).join('/');
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function findNestedLibraryInTree(
|
|
326
|
+
root: ScanResult['trees'][0]['tree'],
|
|
327
|
+
): Array<{ name: string; depth: number }> {
|
|
328
|
+
const out: Array<{ name: string; depth: number }> = [];
|
|
329
|
+
const walk = (node: typeof root, depth: number) => {
|
|
330
|
+
if (node.kind === 'library') out.push({ name: node.name, depth });
|
|
331
|
+
for (const child of node.children) walk(child, depth + 1);
|
|
332
|
+
};
|
|
333
|
+
walk(root, 0);
|
|
334
|
+
return out;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export function fileIsScannable(file: string, libraryRoots: string[]): boolean {
|
|
338
|
+
return !isUnderLibraryRoots(file, libraryRoots);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export { modeSupportsEnhancements };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fg from 'fast-glob';
|
|
3
|
+
|
|
4
|
+
function globPattern(root: string, tail: string): string {
|
|
5
|
+
return `${path.resolve(root).replace(/\\/g, '/')}/${tail}`;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export async function collectVueFiles(roots: string[], ignore: string[]): Promise<string[]> {
|
|
9
|
+
const patterns = roots.map((root) => globPattern(root, '**/*.vue'));
|
|
10
|
+
const files = await fg(patterns, {
|
|
11
|
+
ignore,
|
|
12
|
+
absolute: true,
|
|
13
|
+
onlyFiles: true,
|
|
14
|
+
dot: false,
|
|
15
|
+
});
|
|
16
|
+
return [...new Set(files)].sort();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function collectStyleFiles(roots: string[], ignore: string[]): Promise<string[]> {
|
|
20
|
+
const patterns = roots.map((root) => globPattern(root, '**/*.{css,scss,less}'));
|
|
21
|
+
const files = await fg(patterns, {
|
|
22
|
+
ignore,
|
|
23
|
+
absolute: true,
|
|
24
|
+
onlyFiles: true,
|
|
25
|
+
dot: false,
|
|
26
|
+
});
|
|
27
|
+
return [...new Set(files)].sort();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function isUnderLibraryRoots(file: string, libraryRoots: string[]): boolean {
|
|
31
|
+
const norm = path.normalize(file);
|
|
32
|
+
return libraryRoots.some((root) => {
|
|
33
|
+
const base = path.normalize(root);
|
|
34
|
+
return norm === base || norm.startsWith(`${base}${path.sep}`);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { compileTemplate, parse as parseSfc } from '@vue/compiler-sfc';
|
|
2
|
+
import {
|
|
3
|
+
type ElementNode,
|
|
4
|
+
type TemplateChildNode,
|
|
5
|
+
NodeTypes,
|
|
6
|
+
} from '@vue/compiler-dom';
|
|
7
|
+
import type { ComponentTreeNode, FileComponentReport } from '../types.js';
|
|
8
|
+
import type { UiscopeConfig } from '../types.js';
|
|
9
|
+
import { extractNodeAttrs } from './extract-node-attrs.js';
|
|
10
|
+
import { extractSfcHeaderComment, libraryComponentDoc } from './library-meta.js';
|
|
11
|
+
import { buildTagResolver, parseScriptImports } from './parse-imports.js';
|
|
12
|
+
import { analyzeTemplateHints } from './template-hints.js';
|
|
13
|
+
|
|
14
|
+
function walkTemplateChild(
|
|
15
|
+
child: TemplateChildNode,
|
|
16
|
+
resolve: ReturnType<typeof buildTagResolver>,
|
|
17
|
+
libraryRoot: string,
|
|
18
|
+
): ComponentTreeNode | null {
|
|
19
|
+
if (child.type === NodeTypes.ELEMENT) {
|
|
20
|
+
return walkElement(child as ElementNode, resolve, libraryRoot);
|
|
21
|
+
}
|
|
22
|
+
if (child.type === NodeTypes.IF) {
|
|
23
|
+
const union: ComponentTreeNode[] = [];
|
|
24
|
+
for (const branch of child.branches) {
|
|
25
|
+
for (const branchChild of branch.children) {
|
|
26
|
+
const walked = walkTemplateChild(branchChild, resolve, libraryRoot);
|
|
27
|
+
if (walked) union.push(walked);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (!union.length) return null;
|
|
31
|
+
if (union.length === 1) return union[0]!;
|
|
32
|
+
return { kind: 'html', name: 'if-union', children: union };
|
|
33
|
+
}
|
|
34
|
+
if (child.type === NodeTypes.FOR) {
|
|
35
|
+
const union: ComponentTreeNode[] = [];
|
|
36
|
+
for (const loopChild of child.children) {
|
|
37
|
+
const walked = walkTemplateChild(loopChild, resolve, libraryRoot);
|
|
38
|
+
if (walked) union.push(walked);
|
|
39
|
+
}
|
|
40
|
+
if (!union.length) return null;
|
|
41
|
+
if (union.length === 1) return union[0]!;
|
|
42
|
+
return { kind: 'html', name: 'for-union', children: union };
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function walkElement(
|
|
48
|
+
node: ElementNode,
|
|
49
|
+
resolve: ReturnType<typeof buildTagResolver>,
|
|
50
|
+
libraryRoot: string,
|
|
51
|
+
): ComponentTreeNode | null {
|
|
52
|
+
if (node.tag === 'template') {
|
|
53
|
+
const children: ComponentTreeNode[] = [];
|
|
54
|
+
for (const child of node.children) {
|
|
55
|
+
const walked = walkTemplateChild(child, resolve, libraryRoot);
|
|
56
|
+
if (walked) children.push(walked);
|
|
57
|
+
}
|
|
58
|
+
return children.length
|
|
59
|
+
? { kind: 'html', name: 'template', attrs: extractNodeAttrs(node), children }
|
|
60
|
+
: null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const info = resolve(node.tag);
|
|
64
|
+
const attrs = extractNodeAttrs(node);
|
|
65
|
+
const children: ComponentTreeNode[] = [];
|
|
66
|
+
for (const child of node.children) {
|
|
67
|
+
const walked = walkTemplateChild(child, resolve, libraryRoot);
|
|
68
|
+
if (walked) children.push(walked);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const libraryDoc =
|
|
72
|
+
info.kind === 'library' ? libraryComponentDoc(libraryRoot, info.name) : undefined;
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
kind: info.kind,
|
|
76
|
+
name: info.name,
|
|
77
|
+
source: info.source,
|
|
78
|
+
attrs: Object.keys(attrs).length ? attrs : undefined,
|
|
79
|
+
libraryDoc,
|
|
80
|
+
children,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function buildFileComponentTree(
|
|
85
|
+
filePath: string,
|
|
86
|
+
source: string,
|
|
87
|
+
config: UiscopeConfig,
|
|
88
|
+
libraryComponentNames: Set<string>,
|
|
89
|
+
libraryRoot: string,
|
|
90
|
+
): FileComponentReport | null {
|
|
91
|
+
const { descriptor, errors } = parseSfc(source, { filename: filePath });
|
|
92
|
+
if (errors.length) return null;
|
|
93
|
+
|
|
94
|
+
const script =
|
|
95
|
+
descriptor.scriptSetup?.content
|
|
96
|
+
?? descriptor.script?.content
|
|
97
|
+
?? '';
|
|
98
|
+
const bindings = parseScriptImports(script, config);
|
|
99
|
+
const usesLibrary = bindings.some((b) => b.fromLibrary);
|
|
100
|
+
const importSources = [...new Set(bindings.filter((b) => b.fromLibrary).map((b) => b.source))];
|
|
101
|
+
const fileDoc = extractSfcHeaderComment(source);
|
|
102
|
+
const templateHints = analyzeTemplateHints(descriptor.template?.content);
|
|
103
|
+
|
|
104
|
+
if (!descriptor.template) {
|
|
105
|
+
return {
|
|
106
|
+
file: filePath,
|
|
107
|
+
usesLibrary,
|
|
108
|
+
importSources,
|
|
109
|
+
fileDoc,
|
|
110
|
+
templateHints,
|
|
111
|
+
tree: { kind: 'root', name: pathBasename(filePath), children: [] },
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const compiled = compileTemplate({
|
|
116
|
+
filename: filePath,
|
|
117
|
+
source: descriptor.template.content,
|
|
118
|
+
id: filePath,
|
|
119
|
+
});
|
|
120
|
+
if (compiled.errors.length || !compiled.ast) {
|
|
121
|
+
return {
|
|
122
|
+
file: filePath,
|
|
123
|
+
usesLibrary,
|
|
124
|
+
importSources,
|
|
125
|
+
fileDoc,
|
|
126
|
+
templateHints,
|
|
127
|
+
tree: { kind: 'root', name: pathBasename(filePath), children: [] },
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const resolve = buildTagResolver(bindings, libraryComponentNames);
|
|
132
|
+
const templateChildren: ComponentTreeNode[] = [];
|
|
133
|
+
for (const child of compiled.ast.children as TemplateChildNode[]) {
|
|
134
|
+
const walked = walkTemplateChild(child, resolve, libraryRoot);
|
|
135
|
+
if (walked) templateChildren.push(walked);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
file: filePath,
|
|
140
|
+
usesLibrary,
|
|
141
|
+
importSources,
|
|
142
|
+
fileDoc,
|
|
143
|
+
templateHints,
|
|
144
|
+
tree: {
|
|
145
|
+
kind: 'root',
|
|
146
|
+
name: pathBasename(filePath),
|
|
147
|
+
children: templateChildren,
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function pathBasename(file: string): string {
|
|
153
|
+
const parts = file.replace(/\\/g, '/').split('/');
|
|
154
|
+
return parts[parts.length - 1] ?? file;
|
|
155
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type AttributeNode, type ElementNode, NodeTypes } from '@vue/compiler-dom';
|
|
2
|
+
|
|
3
|
+
const ATTR_KEYS = new Set([
|
|
4
|
+
'name',
|
|
5
|
+
'alt',
|
|
6
|
+
'id',
|
|
7
|
+
'role',
|
|
8
|
+
'title',
|
|
9
|
+
'type',
|
|
10
|
+
'href',
|
|
11
|
+
'src',
|
|
12
|
+
'placeholder',
|
|
13
|
+
'aria-label',
|
|
14
|
+
'aria-labelledby',
|
|
15
|
+
'aria-describedby',
|
|
16
|
+
'aria-hidden',
|
|
17
|
+
'for',
|
|
18
|
+
'value',
|
|
19
|
+
'label',
|
|
20
|
+
'variant',
|
|
21
|
+
'size',
|
|
22
|
+
'disabled',
|
|
23
|
+
'custom-class',
|
|
24
|
+
'customClass',
|
|
25
|
+
'class',
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
export function extractNodeAttrs(node: ElementNode): Record<string, string> {
|
|
29
|
+
const out: Record<string, string> = {};
|
|
30
|
+
for (const prop of node.props) {
|
|
31
|
+
if (prop.type === NodeTypes.ATTRIBUTE) {
|
|
32
|
+
const attr = prop as AttributeNode;
|
|
33
|
+
const key = attr.name;
|
|
34
|
+
if (!ATTR_KEYS.has(key)) continue;
|
|
35
|
+
out[key] = attr.value?.content ?? '';
|
|
36
|
+
}
|
|
37
|
+
if (prop.type === NodeTypes.DIRECTIVE && prop.name === 'bind' && prop.arg?.type === NodeTypes.SIMPLE_EXPRESSION) {
|
|
38
|
+
const key = prop.arg.content;
|
|
39
|
+
if (!ATTR_KEYS.has(key)) continue;
|
|
40
|
+
if (prop.exp?.type === NodeTypes.SIMPLE_EXPRESSION) {
|
|
41
|
+
const raw = prop.exp.content.trim();
|
|
42
|
+
if (/^['"].*['"]$/.test(raw) || /^[\w.-]+$/.test(raw)) {
|
|
43
|
+
out[key] = raw.replace(/^['"]|['"]$/g, '');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return Object.keys(out).length ? out : {};
|
|
49
|
+
}
|