@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
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { parse as parseSfc } from '@vue/compiler-sfc';
|
|
2
|
+
import postcss from 'postcss';
|
|
3
|
+
import safeParser from 'postcss-safe-parser';
|
|
4
|
+
import { matchLibraryClassesInSelector } from '../manifest/library-style-classes.js';
|
|
5
|
+
import type { UiscopeConfig, StyleOverrideHit } from '../types.js';
|
|
6
|
+
import { isUnderLibraryRoots } from './collect-source-files.js';
|
|
7
|
+
import { isStylePathExcluded } from './style-path.js';
|
|
8
|
+
|
|
9
|
+
export interface StyleRuleOccurrence {
|
|
10
|
+
file: string;
|
|
11
|
+
selector: string;
|
|
12
|
+
line?: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function extractStyleBlocksFromVue(
|
|
16
|
+
file: string,
|
|
17
|
+
source: string,
|
|
18
|
+
): Array<{ content: string; lineOffset: number }> {
|
|
19
|
+
const { descriptor } = parseSfc(source, { filename: file });
|
|
20
|
+
const blocks: Array<{ content: string; lineOffset: number }> = [];
|
|
21
|
+
for (const block of descriptor.styles) {
|
|
22
|
+
const lineOffset = block.loc?.start.line ?? 1;
|
|
23
|
+
blocks.push({ content: block.content, lineOffset });
|
|
24
|
+
}
|
|
25
|
+
return blocks;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function parseCssRules(
|
|
29
|
+
_file: string,
|
|
30
|
+
css: string,
|
|
31
|
+
lineOffset = 1,
|
|
32
|
+
): Array<[selector: string, line?: number]> {
|
|
33
|
+
const rules: Array<[string, number | undefined]> = [];
|
|
34
|
+
let root: postcss.Root;
|
|
35
|
+
try {
|
|
36
|
+
const processed = postcss().process(css, { parser: safeParser, from: _file });
|
|
37
|
+
root = (processed.root ?? postcss.parse(css)) as postcss.Root;
|
|
38
|
+
} catch {
|
|
39
|
+
return rules;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
root.walkRules((rule) => {
|
|
43
|
+
const selector = rule.selector.trim();
|
|
44
|
+
if (!matchLibraryClassesInSelector(selector).length) return;
|
|
45
|
+
const line = rule.source?.start
|
|
46
|
+
? rule.source.start.line + lineOffset - 1
|
|
47
|
+
: undefined;
|
|
48
|
+
rules.push([selector, line]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return rules;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function collectStyleOccurrences(
|
|
55
|
+
vueFiles: Array<{ file: string; source: string }>,
|
|
56
|
+
cssFiles: Array<{ file: string; source: string }>,
|
|
57
|
+
config: UiscopeConfig,
|
|
58
|
+
styleExcludeGlobs: string[] = [],
|
|
59
|
+
): Map<string, StyleRuleOccurrence[]> {
|
|
60
|
+
const byClass = new Map<string, StyleRuleOccurrence[]>();
|
|
61
|
+
|
|
62
|
+
const push = (cls: string, hit: StyleRuleOccurrence) => {
|
|
63
|
+
const list = byClass.get(cls) ?? [];
|
|
64
|
+
list.push(hit);
|
|
65
|
+
byClass.set(cls, list);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
for (const { file, source } of cssFiles) {
|
|
69
|
+
if (isUnderLibraryRoots(file, config.libraryRoots)) continue;
|
|
70
|
+
if (isStylePathExcluded(file, styleExcludeGlobs)) continue;
|
|
71
|
+
const rules = parseCssRules(file, source);
|
|
72
|
+
for (const [selector, line] of rules) {
|
|
73
|
+
for (const cls of matchLibraryClassesInSelector(selector)) {
|
|
74
|
+
push(cls, { file, selector, line });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
for (const { file, source } of vueFiles) {
|
|
80
|
+
if (isUnderLibraryRoots(file, config.libraryRoots)) continue;
|
|
81
|
+
if (isStylePathExcluded(file, styleExcludeGlobs)) continue;
|
|
82
|
+
for (const block of extractStyleBlocksFromVue(file, source)) {
|
|
83
|
+
const rules = parseCssRules(file, block.content, block.lineOffset);
|
|
84
|
+
for (const [selector, line] of rules) {
|
|
85
|
+
for (const cls of matchLibraryClassesInSelector(selector)) {
|
|
86
|
+
push(cls, { file, selector, line });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return byClass;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function analyzeStyleOverrides(
|
|
96
|
+
byClass: Map<string, StyleRuleOccurrence[]>,
|
|
97
|
+
threshold: number,
|
|
98
|
+
): { hits: StyleOverrideHit[]; warnings: string[] } {
|
|
99
|
+
const hits: StyleOverrideHit[] = [];
|
|
100
|
+
const warnings: string[] = [];
|
|
101
|
+
|
|
102
|
+
for (const [className, occurrences] of byClass) {
|
|
103
|
+
const uniqueRules = dedupeRules(occurrences);
|
|
104
|
+
const count = uniqueRules.length;
|
|
105
|
+
hits.push({ className, count, occurrences: uniqueRules });
|
|
106
|
+
if (count > threshold) {
|
|
107
|
+
warnings.push(
|
|
108
|
+
`[uiscope] 样式类 ".${className}" 在消费方被重载 ${count} 次(阈值 ${threshold})`,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
hits.sort((a, b) => b.count - a.count);
|
|
114
|
+
return { hits, warnings };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function dedupeRules(items: StyleRuleOccurrence[]): StyleRuleOccurrence[] {
|
|
118
|
+
const seen = new Set<string>();
|
|
119
|
+
const out: StyleRuleOccurrence[] = [];
|
|
120
|
+
for (const item of items) {
|
|
121
|
+
const key = `${item.file}::${item.selector}`;
|
|
122
|
+
if (seen.has(key)) continue;
|
|
123
|
+
seen.add(key);
|
|
124
|
+
out.push(item);
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { parse as parseSfc } from '@vue/compiler-sfc';
|
|
2
|
+
import { matchLibraryClassesInSelector } from '../manifest/library-style-classes.js';
|
|
3
|
+
import { whitelistByClass } from '../manifest/library-style-whitelist.js';
|
|
4
|
+
import type { UiscopeConfig, StyleReloadChain, StyleRuleRef, StyleUsageAnchor } from '../types.js';
|
|
5
|
+
import { isUnderLibraryRoots } from './collect-source-files.js';
|
|
6
|
+
import { isStylePathExcluded } from './style-path.js';
|
|
7
|
+
import { parseCssRules } from './style-rules.js';
|
|
8
|
+
|
|
9
|
+
function tokensInClassAttr(value: string): string[] {
|
|
10
|
+
return value.split(/\s+/).map((t) => t.trim()).filter(Boolean);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function findClassUsagesInVue(
|
|
14
|
+
file: string,
|
|
15
|
+
source: string,
|
|
16
|
+
classNames: Set<string>,
|
|
17
|
+
): StyleUsageAnchor[] {
|
|
18
|
+
const anchors: StyleUsageAnchor[] = [];
|
|
19
|
+
const { descriptor } = parseSfc(source, { filename: file });
|
|
20
|
+
const tpl = descriptor.template?.content ?? '';
|
|
21
|
+
const lines = tpl.split('\n');
|
|
22
|
+
|
|
23
|
+
const scanLine = (line: string, lineNo: number, kind: StyleUsageAnchor['kind']) => {
|
|
24
|
+
const matches = [...line.matchAll(/class\s*=\s*["']([^"']+)["']/g),
|
|
25
|
+
...line.matchAll(/:class\s*=\s*["']([^"']+)["']/g)];
|
|
26
|
+
for (const m of matches) {
|
|
27
|
+
for (const token of tokensInClassAttr(m[1])) {
|
|
28
|
+
if (classNames.has(token)) {
|
|
29
|
+
anchors.push({ file, kind, line: lineNo + 1, className: token, context: line.trim().slice(0, 120) });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
lines.forEach((line, i) => scanLine(line, i, 'vue-template'));
|
|
36
|
+
|
|
37
|
+
for (const style of descriptor.styles) {
|
|
38
|
+
const rules = parseCssRules(file, style.content, style.loc?.start.line ?? 1);
|
|
39
|
+
for (const [selector, line] of rules) {
|
|
40
|
+
for (const cls of matchLibraryClassesInSelector(selector)) {
|
|
41
|
+
if (classNames.has(cls)) {
|
|
42
|
+
anchors.push({
|
|
43
|
+
file,
|
|
44
|
+
kind: style.scoped ? 'vue-style-scoped' : 'vue-style-global',
|
|
45
|
+
line,
|
|
46
|
+
className: cls,
|
|
47
|
+
context: selector,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return anchors;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function buildStyleReloadChains(
|
|
58
|
+
consumerVue: Array<{ file: string; source: string }>,
|
|
59
|
+
cssSources: Array<{ file: string; source: string }>,
|
|
60
|
+
config: UiscopeConfig,
|
|
61
|
+
classNames: Set<string>,
|
|
62
|
+
styleExcludeGlobs: string[] = [],
|
|
63
|
+
): StyleReloadChain[] {
|
|
64
|
+
const rulesByClass = new Map<string, StyleRuleRef[]>();
|
|
65
|
+
|
|
66
|
+
const pushRule = (cls: string, rule: StyleRuleRef) => {
|
|
67
|
+
const list = rulesByClass.get(cls) ?? [];
|
|
68
|
+
list.push(rule);
|
|
69
|
+
rulesByClass.set(cls, list);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
for (const { file, source } of consumerVue) {
|
|
73
|
+
if (isUnderLibraryRoots(file, config.libraryRoots)) continue;
|
|
74
|
+
if (isStylePathExcluded(file, styleExcludeGlobs)) continue;
|
|
75
|
+
const { descriptor } = parseSfc(source, { filename: file });
|
|
76
|
+
for (const style of descriptor.styles) {
|
|
77
|
+
const rules = parseCssRules(file, style.content, style.loc?.start.line ?? 1);
|
|
78
|
+
for (const [selector, line] of rules) {
|
|
79
|
+
for (const cls of matchLibraryClassesInSelector(selector)) {
|
|
80
|
+
if (!classNames.has(cls)) continue;
|
|
81
|
+
pushRule(cls, {
|
|
82
|
+
file,
|
|
83
|
+
selector,
|
|
84
|
+
line,
|
|
85
|
+
origin: style.scoped ? 'consumer-vue-scoped' : 'consumer-vue-global',
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
for (const { file, source } of cssSources) {
|
|
93
|
+
if (isUnderLibraryRoots(file, config.libraryRoots)) continue;
|
|
94
|
+
if (isStylePathExcluded(file, styleExcludeGlobs)) continue;
|
|
95
|
+
const rules = parseCssRules(file, source, 1);
|
|
96
|
+
for (const [selector, line] of rules) {
|
|
97
|
+
for (const cls of matchLibraryClassesInSelector(selector)) {
|
|
98
|
+
if (!classNames.has(cls)) continue;
|
|
99
|
+
pushRule(cls, {
|
|
100
|
+
file,
|
|
101
|
+
selector,
|
|
102
|
+
line,
|
|
103
|
+
origin: 'consumer-stylesheet',
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const usagesByClass = new Map<string, StyleUsageAnchor[]>();
|
|
110
|
+
for (const v of consumerVue) {
|
|
111
|
+
if (isUnderLibraryRoots(v.file, config.libraryRoots)) continue;
|
|
112
|
+
for (const anchor of findClassUsagesInVue(v.file, v.source, classNames)) {
|
|
113
|
+
const list = usagesByClass.get(anchor.className) ?? [];
|
|
114
|
+
list.push(anchor);
|
|
115
|
+
usagesByClass.set(anchor.className, list);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const chains: StyleReloadChain[] = [];
|
|
120
|
+
const allClasses = new Set([...rulesByClass.keys(), ...usagesByClass.keys()]);
|
|
121
|
+
|
|
122
|
+
for (const className of [...allClasses].sort()) {
|
|
123
|
+
const rules = (rulesByClass.get(className) ?? []).sort((a, b) => {
|
|
124
|
+
const rank = (o: StyleRuleRef['origin']) =>
|
|
125
|
+
o === 'consumer-vue-scoped' ? 0 : o === 'consumer-vue-global' ? 1 : 2;
|
|
126
|
+
const d = rank(a.origin) - rank(b.origin);
|
|
127
|
+
if (d !== 0) return d;
|
|
128
|
+
return a.file.localeCompare(b.file) || (a.line ?? 0) - (b.line ?? 0);
|
|
129
|
+
});
|
|
130
|
+
const usageAnchors = usagesByClass.get(className) ?? [];
|
|
131
|
+
const overrideCount = rules.length;
|
|
132
|
+
const meta = whitelistByClass(className);
|
|
133
|
+
chains.push({
|
|
134
|
+
className,
|
|
135
|
+
category: meta?.category,
|
|
136
|
+
surface: meta?.surface,
|
|
137
|
+
owner: meta?.owner,
|
|
138
|
+
usageAnchors,
|
|
139
|
+
rules,
|
|
140
|
+
overrideCount,
|
|
141
|
+
chain: buildChainNarrative(className, usageAnchors, rules),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return chains.sort((a, b) => b.overrideCount - a.overrideCount);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function buildChainNarrative(
|
|
149
|
+
className: string,
|
|
150
|
+
usages: StyleUsageAnchor[],
|
|
151
|
+
rules: StyleRuleRef[],
|
|
152
|
+
): string[] {
|
|
153
|
+
const steps: string[] = [];
|
|
154
|
+
if (usages.length) {
|
|
155
|
+
const byFile = [...new Set(usages.map((u) => u.file))];
|
|
156
|
+
steps.push(
|
|
157
|
+
`引用:${className} 出现在 ${byFile.length} 个消费方 Vue(模板/样式锚点 ${usages.length} 处)`,
|
|
158
|
+
);
|
|
159
|
+
for (const u of usages.slice(0, 5)) {
|
|
160
|
+
steps.push(` · ${relPath(u.file)}:${u.line ?? '?'} [${u.kind}] ${u.context}`);
|
|
161
|
+
}
|
|
162
|
+
if (usages.length > 5) steps.push(` · … 另有 ${usages.length - 5} 处引用`);
|
|
163
|
+
} else {
|
|
164
|
+
steps.push(`引用:未发现模板/单文件样式直接书写 .${className}(仍可能被后代选择器命中)`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!rules.length) {
|
|
168
|
+
steps.push('重载:无消费方 CSS 规则命中');
|
|
169
|
+
return steps;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
steps.push(`重载链路(${rules.length} 条规则,由近及远):`);
|
|
173
|
+
for (const r of rules) {
|
|
174
|
+
steps.push(` → ${relPath(r.file)}:${r.line ?? '?'} [${r.origin}] ${r.selector.replace(/\s+/g, ' ').slice(0, 100)}`);
|
|
175
|
+
}
|
|
176
|
+
return steps;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function relPath(file: string): string {
|
|
180
|
+
return file.replace(/\\/g, '/').split('/').slice(-4).join('/');
|
|
181
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { type ElementNode, NodeTypes, parse } from '@vue/compiler-dom';
|
|
2
|
+
import type { FileTemplateHints } from '../types.js';
|
|
3
|
+
|
|
4
|
+
const REPEATABLE_TAGS = new Set(['li', 'div', 'article', 'tr', 'option']);
|
|
5
|
+
const CJK = /[\u4e00-\u9fff]/;
|
|
6
|
+
|
|
7
|
+
export function analyzeTemplateHints(template: string | undefined): FileTemplateHints {
|
|
8
|
+
const empty: FileTemplateHints = {
|
|
9
|
+
vForCount: 0,
|
|
10
|
+
vForOnRepeatableTags: 0,
|
|
11
|
+
staticChineseLiterals: [],
|
|
12
|
+
};
|
|
13
|
+
if (!template?.trim()) return empty;
|
|
14
|
+
|
|
15
|
+
let ast;
|
|
16
|
+
try {
|
|
17
|
+
ast = parse(template, { comments: false });
|
|
18
|
+
} catch {
|
|
19
|
+
return empty;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const hints = { ...empty, staticChineseLiterals: [] as FileTemplateHints['staticChineseLiterals'] };
|
|
23
|
+
|
|
24
|
+
const walk = (node: ElementNode) => {
|
|
25
|
+
let hasFor = false;
|
|
26
|
+
for (const prop of node.props) {
|
|
27
|
+
if (prop.type === NodeTypes.DIRECTIVE && prop.name === 'for') {
|
|
28
|
+
hasFor = true;
|
|
29
|
+
hints.vForCount++;
|
|
30
|
+
if (REPEATABLE_TAGS.has(node.tag.toLowerCase())) hints.vForOnRepeatableTags++;
|
|
31
|
+
}
|
|
32
|
+
if (prop.type === NodeTypes.ATTRIBUTE) {
|
|
33
|
+
const val = prop.value?.content ?? '';
|
|
34
|
+
if (val && CJK.test(val) && ['placeholder', 'title', 'aria-label', 'label'].includes(prop.name)) {
|
|
35
|
+
hints.staticChineseLiterals.push({
|
|
36
|
+
attr: prop.name,
|
|
37
|
+
preview: val.slice(0, 40),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (!hasFor) {
|
|
43
|
+
for (const prop of node.props) {
|
|
44
|
+
if (prop.type === NodeTypes.DIRECTIVE && prop.name === 'bind' && prop.exp?.type === NodeTypes.SIMPLE_EXPRESSION) {
|
|
45
|
+
const key = prop.arg?.type === NodeTypes.SIMPLE_EXPRESSION ? prop.arg.content : '';
|
|
46
|
+
const raw = prop.exp.content.trim();
|
|
47
|
+
if (
|
|
48
|
+
['placeholder', 'title', 'aria-label', 'label'].includes(key)
|
|
49
|
+
&& /^['"]/.test(raw)
|
|
50
|
+
&& CJK.test(raw)
|
|
51
|
+
) {
|
|
52
|
+
hints.staticChineseLiterals.push({
|
|
53
|
+
attr: key,
|
|
54
|
+
preview: raw.replace(/^['"]|['"]$/g, '').slice(0, 40),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
for (const child of node.children) {
|
|
61
|
+
if (child.type === NodeTypes.ELEMENT) walk(child as ElementNode);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
for (const child of ast.children) {
|
|
66
|
+
if (child.type === NodeTypes.ELEMENT) walk(child as ElementNode);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return hints;
|
|
70
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import type { LibraryStyleCategory, LibraryStyleSurface } from './manifest/library-style-whitelist.js';
|
|
2
|
+
import type { UxComponentRole } from './manifest/library-ux-taxonomy.js';
|
|
3
|
+
import type { UxRulePack } from './enhance/ux-rule-pack.js';
|
|
4
|
+
|
|
5
|
+
export interface UiscopeStyleThresholds {
|
|
6
|
+
/** 同一白名单类在消费方的去重规则条数上限,超出告警 */
|
|
7
|
+
overrideRuleCount: number;
|
|
8
|
+
/** 不参与重载统计的样式文件 glob(相对路径片段即可) */
|
|
9
|
+
excludeGlobs: string[];
|
|
10
|
+
/** 无模板引用锚点却有外部重载时是否告警 */
|
|
11
|
+
warnOrphanOverrides: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface UiscopeFacetThresholds {
|
|
15
|
+
minSubtreeNodes: number;
|
|
16
|
+
minInteractiveControls: number;
|
|
17
|
+
minFacetComponents: number;
|
|
18
|
+
minRepeatedSiblings: number;
|
|
19
|
+
classOverlapRatio: number;
|
|
20
|
+
/** 模板化率达到该值且存在信号时告警 */
|
|
21
|
+
templateWarnRate: number;
|
|
22
|
+
/** 单文件切面数达到该值时告警 */
|
|
23
|
+
multiFacetWarnCount: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface UiscopeTreeThresholds {
|
|
27
|
+
maxLibraryNestDepth: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface UiscopeThresholds {
|
|
31
|
+
style: UiscopeStyleThresholds;
|
|
32
|
+
facet: UiscopeFacetThresholds;
|
|
33
|
+
tree: UiscopeTreeThresholds;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 内置核心 UX 规则(facets / all / ux 默认执行) */
|
|
37
|
+
export type UxEnhancementCoreId = 'core-ux';
|
|
38
|
+
|
|
39
|
+
/** 可选增强扫描项(CLI `--enhance` 启用) */
|
|
40
|
+
export type UxEnhancementOptionalId =
|
|
41
|
+
| 'feedback-states'
|
|
42
|
+
| 'hollow-shell'
|
|
43
|
+
| 'page-rollup'
|
|
44
|
+
| 'facet-hygiene'
|
|
45
|
+
| 'form-a11y'
|
|
46
|
+
| 'action-semantics'
|
|
47
|
+
| 'list-templating'
|
|
48
|
+
| 'i18n-hints';
|
|
49
|
+
|
|
50
|
+
export type UxEnhancementId = UxEnhancementCoreId | UxEnhancementOptionalId;
|
|
51
|
+
|
|
52
|
+
export interface UiscopeEnhanceConfig {
|
|
53
|
+
/** 视作操作按钮的组件标签(并集 consumerWrapperTags) */
|
|
54
|
+
actionTags?: string[];
|
|
55
|
+
/** 视作加载/空态反馈的组件标签 */
|
|
56
|
+
feedbackTags?: string[];
|
|
57
|
+
/** 消费方封装组件 → UX 角色(并内置 BUILTIN_WRAPPER_ROLES) */
|
|
58
|
+
wrapperRoles?: Partial<Record<string, UxComponentRole>>;
|
|
59
|
+
/** 内联规则包 */
|
|
60
|
+
rulePack?: Partial<UxRulePack>;
|
|
61
|
+
/** 外置规则包路径(相对 cwd) */
|
|
62
|
+
rulePackPath?: string;
|
|
63
|
+
/** 目录级期望切面(path 片段匹配,如 `components/event`) */
|
|
64
|
+
workbenchExpectations?: Array<{
|
|
65
|
+
pathIncludes: string;
|
|
66
|
+
requireFacetKinds: FacetKind[];
|
|
67
|
+
label?: string;
|
|
68
|
+
}>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type UiscopeFramework = 'vue' | 'react';
|
|
72
|
+
|
|
73
|
+
export interface UiscopeConfig {
|
|
74
|
+
roots: string[];
|
|
75
|
+
ignore: string[];
|
|
76
|
+
/** 参与扫描的视图框架;默认 vue + react */
|
|
77
|
+
frameworks?: UiscopeFramework[];
|
|
78
|
+
/**
|
|
79
|
+
* 组件库扩展:短 id(先 `presets/<id>/`,再外置 npm `@algotao/preset-<id>`)。
|
|
80
|
+
* 具体库名不得写死在 core;由独立 preset 包提供白名单/角色/import 源。
|
|
81
|
+
*/
|
|
82
|
+
presets?: string[];
|
|
83
|
+
/** 短 id → npm 包名;用于非 `@algotao/preset-*` 命名的第三方 preset */
|
|
84
|
+
presetModules?: Record<string, string>;
|
|
85
|
+
libraryImportSources: string[];
|
|
86
|
+
libraryRoots: string[];
|
|
87
|
+
/** @deprecated 使用 thresholds.style.overrideRuleCount */
|
|
88
|
+
overrideThreshold?: number;
|
|
89
|
+
thresholds?: Partial<{
|
|
90
|
+
style: Partial<UiscopeStyleThresholds>;
|
|
91
|
+
facet: Partial<UiscopeFacetThresholds>;
|
|
92
|
+
tree: Partial<UiscopeTreeThresholds>;
|
|
93
|
+
}>;
|
|
94
|
+
onlyLibraryConsumers?: boolean;
|
|
95
|
+
consumerWrapperTags?: string[];
|
|
96
|
+
enhancements?: UiscopeEnhanceConfig;
|
|
97
|
+
/** 除 core-ux 外默认可选增强项 */
|
|
98
|
+
defaultEnhancements?: UxEnhancementOptionalId[];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type UxFindingSeverity = 'info' | 'warn' | 'suggest';
|
|
102
|
+
|
|
103
|
+
export interface UxOptimizationHint {
|
|
104
|
+
id: string;
|
|
105
|
+
title: string;
|
|
106
|
+
description: string;
|
|
107
|
+
/** 供 Agent 勾选后再改代码 */
|
|
108
|
+
selectable: boolean;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface UxFinding {
|
|
112
|
+
id: string;
|
|
113
|
+
enhancement: UxEnhancementId;
|
|
114
|
+
severity: UxFindingSeverity;
|
|
115
|
+
file: string;
|
|
116
|
+
facetId?: string;
|
|
117
|
+
ruleId: string;
|
|
118
|
+
message: string;
|
|
119
|
+
evidence?: Record<string, unknown>;
|
|
120
|
+
optimization?: UxOptimizationHint;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface ComponentTreeNode {
|
|
124
|
+
kind: 'root' | 'library' | 'local' | 'html' | 'unknown';
|
|
125
|
+
name: string;
|
|
126
|
+
source?: string;
|
|
127
|
+
attrs?: Record<string, string>;
|
|
128
|
+
/** 库组件顶部 HTML 注释(仅 kind=library 尽力填充) */
|
|
129
|
+
libraryDoc?: string;
|
|
130
|
+
children: ComponentTreeNode[];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface FileTemplateHints {
|
|
134
|
+
vForCount: number;
|
|
135
|
+
vForOnRepeatableTags: number;
|
|
136
|
+
staticChineseLiterals: Array<{ attr: string; preview: string }>;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface FileComponentReport {
|
|
140
|
+
file: string;
|
|
141
|
+
usesLibrary: boolean;
|
|
142
|
+
importSources: string[];
|
|
143
|
+
/** 本文件 SFC 顶部 HTML 注释 */
|
|
144
|
+
fileDoc?: string;
|
|
145
|
+
templateHints?: FileTemplateHints;
|
|
146
|
+
tree: ComponentTreeNode;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface StyleOverrideHit {
|
|
150
|
+
className: string;
|
|
151
|
+
count: number;
|
|
152
|
+
occurrences: Array<{ file: string; selector: string; line?: number }>;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export type StyleRuleOrigin =
|
|
156
|
+
| 'consumer-vue-scoped'
|
|
157
|
+
| 'consumer-vue-global'
|
|
158
|
+
| 'consumer-stylesheet';
|
|
159
|
+
|
|
160
|
+
export interface StyleRuleRef {
|
|
161
|
+
file: string;
|
|
162
|
+
selector: string;
|
|
163
|
+
line?: number;
|
|
164
|
+
origin: StyleRuleOrigin;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export type StyleUsageKind = 'vue-template' | 'vue-style-scoped' | 'vue-style-global';
|
|
168
|
+
|
|
169
|
+
export interface StyleUsageAnchor {
|
|
170
|
+
file: string;
|
|
171
|
+
kind: StyleUsageKind;
|
|
172
|
+
line?: number;
|
|
173
|
+
className: string;
|
|
174
|
+
context: string;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface StyleReloadChain {
|
|
178
|
+
className: string;
|
|
179
|
+
category?: LibraryStyleCategory;
|
|
180
|
+
surface?: LibraryStyleSurface;
|
|
181
|
+
owner?: string;
|
|
182
|
+
usageAnchors: StyleUsageAnchor[];
|
|
183
|
+
rules: StyleRuleRef[];
|
|
184
|
+
overrideCount: number;
|
|
185
|
+
/** 人类可读溯源步骤 */
|
|
186
|
+
chain: string[];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface ScanScenarioResult {
|
|
190
|
+
id: string;
|
|
191
|
+
label: string;
|
|
192
|
+
options: Record<string, unknown>;
|
|
193
|
+
result: ScanResult;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** 切面划分内核参数(与 UiscopeFacetThresholds 前 5 项一致) */
|
|
197
|
+
export type FacetScanConfig = Pick<
|
|
198
|
+
UiscopeFacetThresholds,
|
|
199
|
+
| 'minSubtreeNodes'
|
|
200
|
+
| 'minInteractiveControls'
|
|
201
|
+
| 'minFacetComponents'
|
|
202
|
+
| 'minRepeatedSiblings'
|
|
203
|
+
| 'classOverlapRatio'
|
|
204
|
+
>;
|
|
205
|
+
|
|
206
|
+
export type FacetKind =
|
|
207
|
+
| 'filter-bar'
|
|
208
|
+
| 'list-region'
|
|
209
|
+
| 'grid-region'
|
|
210
|
+
| 'form-region'
|
|
211
|
+
| 'mixed-media'
|
|
212
|
+
| 'action-cluster'
|
|
213
|
+
| 'composite-chrome'
|
|
214
|
+
| 'interactive-surface';
|
|
215
|
+
|
|
216
|
+
export interface TemplatePatternSignal {
|
|
217
|
+
kind: 'repeated-sibling' | 'head-tail-mirror' | 'similar-style-cluster';
|
|
218
|
+
signature: string;
|
|
219
|
+
count: number;
|
|
220
|
+
templatingRate: number;
|
|
221
|
+
positions: number[];
|
|
222
|
+
note: string;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export interface FacetRegion {
|
|
226
|
+
id: string;
|
|
227
|
+
anchorNode: string;
|
|
228
|
+
kind: FacetKind;
|
|
229
|
+
layoutHints: string[];
|
|
230
|
+
depth: number;
|
|
231
|
+
stats: {
|
|
232
|
+
nodes: number;
|
|
233
|
+
interactiveControls: number;
|
|
234
|
+
roles: Partial<Record<string, number>>;
|
|
235
|
+
};
|
|
236
|
+
templatingRate: number;
|
|
237
|
+
templateSignals: TemplatePatternSignal[];
|
|
238
|
+
aria?: Record<string, string>;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export interface FileFacetReport {
|
|
242
|
+
file: string;
|
|
243
|
+
facetCount: number;
|
|
244
|
+
avgTemplatingRate: number;
|
|
245
|
+
facets: FacetRegion[];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export interface ScanResult {
|
|
249
|
+
trees: FileComponentReport[];
|
|
250
|
+
styleOverrides: StyleOverrideHit[];
|
|
251
|
+
styleChains: StyleReloadChain[];
|
|
252
|
+
facetReports: FileFacetReport[];
|
|
253
|
+
uxFindings: UxFinding[];
|
|
254
|
+
/** 本次执行的增强项(含 core-ux) */
|
|
255
|
+
enhancementsRun: UxEnhancementId[];
|
|
256
|
+
warnings: string[];
|
|
257
|
+
stats: {
|
|
258
|
+
vueFilesScanned: number;
|
|
259
|
+
vueFilesLibraryConsumers: number;
|
|
260
|
+
reactFilesScanned: number;
|
|
261
|
+
reactFilesLibraryConsumers: number;
|
|
262
|
+
cssFilesScanned: number;
|
|
263
|
+
facetRegions: number;
|
|
264
|
+
uxFindings: number;
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export interface MarkdownReportInput {
|
|
269
|
+
generatedAt: string;
|
|
270
|
+
cwd: string;
|
|
271
|
+
scenarios: ScanScenarioResult[];
|
|
272
|
+
/** 切面章节每场景最多展示文件数;`0` 不截断;省略则用默认 15 */
|
|
273
|
+
facetFileLimit?: number;
|
|
274
|
+
}
|