@md-code/component-uniqueness 0.1.4 → 0.3.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 +17 -0
- package/LICENSE +202 -0
- package/README.md +21 -3
- package/dist/config.d.ts +50 -0
- package/dist/config.js +402 -0
- package/dist/constants.d.ts +55 -0
- package/dist/constants.js +86 -0
- package/dist/decision-maker.d.ts +39 -0
- package/dist/decision-maker.js +119 -0
- package/dist/filter/child-element.d.ts +3 -0
- package/dist/filter/child-element.js +17 -0
- package/dist/filter/missing-component.d.ts +10 -0
- package/dist/filter/missing-component.js +96 -0
- package/dist/filter/no-structure.d.ts +3 -0
- package/dist/filter/no-structure.js +16 -0
- package/dist/filter/usages.d.ts +3 -0
- package/dist/filter/usages.js +14 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.js +244 -0
- package/dist/lib-registry.d.ts +4 -0
- package/dist/lib-registry.js +376 -0
- package/dist/logger.d.ts +7 -0
- package/dist/logger.js +70 -0
- package/dist/main.d.ts +25 -0
- package/dist/main.js +666 -0
- package/dist/matcher/a11y.d.ts +19 -0
- package/dist/matcher/a11y.js +311 -0
- package/dist/matcher/behavior.d.ts +10 -0
- package/dist/matcher/behavior.js +32 -0
- package/dist/matcher/css-matching.d.ts +18 -0
- package/dist/matcher/css-matching.js +159 -0
- package/dist/matcher/framework/index.d.ts +2 -0
- package/dist/matcher/framework/index.js +37 -0
- package/dist/matcher/framework/react.d.ts +12 -0
- package/dist/matcher/framework/react.js +70 -0
- package/dist/matcher/index.d.ts +64 -0
- package/dist/matcher/index.js +38 -0
- package/dist/matcher/name.d.ts +15 -0
- package/dist/matcher/name.js +154 -0
- package/dist/matcher/structure.d.ts +22 -0
- package/dist/matcher/structure.js +329 -0
- package/dist/matcher/styles.d.ts +20 -0
- package/dist/matcher/styles.js +49 -0
- package/dist/normalize/candidate.d.ts +3 -0
- package/dist/normalize/candidate.js +21 -0
- package/dist/normalize/dom.d.ts +22 -0
- package/dist/normalize/dom.js +180 -0
- package/dist/normalize/external-html.d.ts +5 -0
- package/dist/normalize/external-html.js +40 -0
- package/dist/normalize/framework.d.ts +20 -0
- package/dist/normalize/framework.js +130 -0
- package/dist/normalize/resolver.d.ts +7 -0
- package/dist/normalize/resolver.js +28 -0
- package/dist/normalize/style-resolver.d.ts +7 -0
- package/dist/normalize/style-resolver.js +28 -0
- package/dist/normalize/style.d.ts +15 -0
- package/dist/normalize/style.js +174 -0
- package/dist/normalize/words.d.ts +3 -0
- package/dist/normalize/words.js +40 -0
- package/dist/package.json +88 -0
- package/dist/playgrounds/angular.d.ts +7 -0
- package/dist/playgrounds/angular.js +9 -0
- package/dist/playgrounds/framework-entrypoint.d.ts +10 -0
- package/dist/playgrounds/framework-entrypoint.js +102 -0
- package/dist/playgrounds/framework.d.ts +10 -0
- package/dist/playgrounds/framework.js +102 -0
- package/dist/playgrounds/react.d.ts +14 -0
- package/dist/playgrounds/react.js +1288 -0
- package/dist/playgrounds/scss.d.ts +6 -0
- package/dist/playgrounds/scss.js +9 -0
- package/dist/playgrounds/style-entrypoint.d.ts +13 -0
- package/dist/playgrounds/style-entrypoint.js +82 -0
- package/dist/playgrounds/style.d.ts +13 -0
- package/dist/playgrounds/style.js +82 -0
- package/dist/playgrounds/styled-components.d.ts +7 -0
- package/dist/playgrounds/styled-components.js +190 -0
- package/dist/playgrounds/tailwind.d.ts +15 -0
- package/dist/playgrounds/tailwind.js +256 -0
- package/dist/playgrounds/vue.d.ts +7 -0
- package/dist/playgrounds/vue.js +9 -0
- package/dist/report/asks.d.ts +27 -0
- package/dist/report/asks.js +28 -0
- package/dist/report/cli-report.d.ts +7 -0
- package/dist/report/cli-report.js +187 -0
- package/dist/report/codes/index.d.ts +37 -0
- package/dist/report/codes/index.js +31 -0
- package/dist/report/errorTypes/index.d.ts +92 -0
- package/dist/report/errorTypes/index.js +186 -0
- package/dist/report/evidence.d.ts +2 -0
- package/dist/report/evidence.js +8 -0
- package/dist/report/explain.d.ts +27 -0
- package/dist/report/explain.js +76 -0
- package/dist/report/index.d.ts +2 -0
- package/dist/report/index.js +24 -0
- package/dist/report/matchers/a11y/model.d.ts +20 -0
- package/dist/report/matchers/a11y/model.js +14 -0
- package/dist/report/matchers/a11y/text.d.ts +2 -0
- package/dist/report/matchers/a11y/text.js +22 -0
- package/dist/report/matchers/behavior/model.d.ts +20 -0
- package/dist/report/matchers/behavior/model.js +12 -0
- package/dist/report/matchers/behavior/text.d.ts +2 -0
- package/dist/report/matchers/behavior/text.js +22 -0
- package/dist/report/matchers/framework/model.d.ts +10 -0
- package/dist/report/matchers/framework/model.js +39 -0
- package/dist/report/matchers/framework/text.d.ts +2 -0
- package/dist/report/matchers/framework/text.js +26 -0
- package/dist/report/matchers/index.d.ts +5 -0
- package/dist/report/matchers/index.js +97 -0
- package/dist/report/matchers/name/model.d.ts +20 -0
- package/dist/report/matchers/name/model.js +30 -0
- package/dist/report/matchers/name/text.d.ts +2 -0
- package/dist/report/matchers/name/text.js +17 -0
- package/dist/report/matchers/set-model.d.ts +31 -0
- package/dist/report/matchers/set-model.js +42 -0
- package/dist/report/matchers/structure/model.d.ts +10 -0
- package/dist/report/matchers/structure/model.js +58 -0
- package/dist/report/matchers/structure/text.d.ts +2 -0
- package/dist/report/matchers/structure/text.js +40 -0
- package/dist/report/matchers/styles/model.d.ts +11 -0
- package/dist/report/matchers/styles/model.js +36 -0
- package/dist/report/matchers/styles/text.d.ts +2 -0
- package/dist/report/matchers/styles/text.js +40 -0
- package/dist/report/user-report/build/bundle.d.ts +3 -0
- package/dist/report/user-report/build/bundle.js +185 -0
- package/dist/report/user-report/build/bundle.prebuilt.js +955 -0
- package/dist/report/user-report/data/collect.d.ts +68 -0
- package/dist/report/user-report/data/collect.js +150 -0
- package/dist/report/user-report/data/folders.d.ts +3 -0
- package/dist/report/user-report/data/folders.js +73 -0
- package/dist/report/user-report/data/groups.d.ts +13 -0
- package/dist/report/user-report/data/groups.js +76 -0
- package/dist/report/user-report/data/links.d.ts +39 -0
- package/dist/report/user-report/data/links.js +222 -0
- package/dist/report/user-report/data/snapshot.d.ts +9 -0
- package/dist/report/user-report/data/snapshot.js +180 -0
- package/dist/report/user-report/data/source.d.ts +13 -0
- package/dist/report/user-report/data/source.js +58 -0
- package/dist/report/user-report/data/word.d.ts +3 -0
- package/dist/report/user-report/data/word.js +32 -0
- package/dist/report/user-report/html/shell.d.ts +2 -0
- package/dist/report/user-report/html/shell.js +27 -0
- package/dist/report/user-report/index.d.ts +7 -0
- package/dist/report/user-report/index.js +47 -0
- package/dist/report/vocabulary/evidence.d.ts +48 -0
- package/dist/report/vocabulary/evidence.js +66 -0
- package/dist/report/vocabulary/format.d.ts +4 -0
- package/dist/report/vocabulary/format.js +20 -0
- package/dist/report/vocabulary/labels.d.ts +134 -0
- package/dist/report/vocabulary/labels.js +87 -0
- package/dist/report/vocabulary/phrases.d.ts +20 -0
- package/dist/report/vocabulary/phrases.js +34 -0
- package/dist/self-catalog.d.ts +4 -0
- package/dist/self-catalog.js +179 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.js +125 -0
- package/package.json +86 -71
- package/config.js +0 -372
- package/decision-maker.js +0 -109
- package/filter/drop-child-element.js +0 -14
- package/filter/drop-no-structure.js +0 -13
- package/filter/drop-usages.js +0 -13
- package/filter/missing-component.js +0 -100
- package/index.js +0 -184
- package/logger.js +0 -44
- package/main.js +0 -584
- package/matcher/a11y.js +0 -65
- package/matcher/behavior.js +0 -31
- package/matcher/framework/index.js +0 -15
- package/matcher/framework/react.js +0 -25
- package/matcher/index.js +0 -36
- package/matcher/name.js +0 -132
- package/matcher/structure.js +0 -242
- package/normalize/candidate.js +0 -22
- package/normalize/dom.js +0 -187
- package/normalize/framework.js +0 -110
- package/normalize/style-resolver.js +0 -24
- package/normalize/style.js +0 -175
- package/normalize/words.js +0 -40
- package/playgrounds/angular.js +0 -11
- package/playgrounds/framework-entrypoint.js +0 -90
- package/playgrounds/react.js +0 -998
- package/playgrounds/scss.js +0 -7
- package/playgrounds/style-entrypoint.js +0 -78
- package/playgrounds/styled-components.js +0 -138
- package/playgrounds/tailwind.js +0 -186
- package/playgrounds/vue.js +0 -11
- package/plugin.js +0 -3
- package/report.js +0 -162
- package/utils.js +0 -92
package/main.js
DELETED
|
@@ -1,584 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
|
|
4
|
-
const fs = require("node:fs");
|
|
5
|
-
const path = require("node:path");
|
|
6
|
-
|
|
7
|
-
const {
|
|
8
|
-
findRepoRoot,
|
|
9
|
-
isIgnored,
|
|
10
|
-
inComponentsFolder,
|
|
11
|
-
canSeeCanon,
|
|
12
|
-
DEFAULT_EXTS,
|
|
13
|
-
DEFAULT_IGNORE_DIRS,
|
|
14
|
-
loadRuleOptions,
|
|
15
|
-
normalizeOptions,
|
|
16
|
-
loadRegistry,
|
|
17
|
-
loadCatalog,
|
|
18
|
-
} = require("./config");
|
|
19
|
-
const { walk, writeCatalog } = require("./utils");
|
|
20
|
-
const { parseComponents: parseComponentsViaEntrypoint, parseElements: parseElementsViaEntrypoint, parseParts: parsePartsViaEntrypoint } = require("./playgrounds/framework-entrypoint.js");
|
|
21
|
-
const { buildCandidate, buildElementCandidate } = require("./normalize/candidate.js");
|
|
22
|
-
const { tagAffinity } = require("./normalize/dom.js");
|
|
23
|
-
const { matchSignals } = require("./matcher");
|
|
24
|
-
const { getFrameworkMatcher } = require("./matcher/framework");
|
|
25
|
-
const { decide, bestOf } = require("./decision-maker");
|
|
26
|
-
const dropUsages = require("./filter/drop-usages");
|
|
27
|
-
const dropChildElement = require("./filter/drop-child-element");
|
|
28
|
-
const dropNoStructure = require("./filter/drop-no-structure");
|
|
29
|
-
const { findMissingComponents } = require("./filter/missing-component");
|
|
30
|
-
const { renderComponentReport } = require("./report");
|
|
31
|
-
const log = require("./logger");
|
|
32
|
-
|
|
33
|
-
const FILTERS = [dropUsages, dropChildElement, dropNoStructure];
|
|
34
|
-
const frameworkMatcher = getFrameworkMatcher("react");
|
|
35
|
-
|
|
36
|
-
const treeSummary = (tree) => {
|
|
37
|
-
if (!tree) return null;
|
|
38
|
-
const walk = (n) => {
|
|
39
|
-
const kids = (n.children || []).map(walk);
|
|
40
|
-
return { tag: n.tag, css: n.css ? Object.keys(n.css).length : 0, children: kids };
|
|
41
|
-
};
|
|
42
|
-
return walk(tree);
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const candidateSummary = (c, role) => ({
|
|
46
|
-
role,
|
|
47
|
-
name: c.name,
|
|
48
|
-
path: c.path,
|
|
49
|
-
rootTags: c.rootTags,
|
|
50
|
-
tree: treeSummary(c.tree),
|
|
51
|
-
domTree: treeSummary(c.dom && c.dom.tree),
|
|
52
|
-
framework: c.framework ? { props: c.framework.props, events: c.framework.events, names: c.framework.names, open: c.framework.open } : null,
|
|
53
|
-
a11y: c.a11y,
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
const fail = (message) => {
|
|
57
|
-
console.error(`[component-uniqueness] ${message}`);
|
|
58
|
-
process.exit(1);
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const parseArgs = (argv) => {
|
|
62
|
-
const cfg = { roots: null, out: null, registry: null, repoRoot: null, check: false, report: null, appRoots: null, ignoreDirs: null, verbose: false, include: null, log: null, minCluster: 3, thresholds: null, rawHtml: false, parts: false, configFile: null };
|
|
63
|
-
for (let i = 0; i < argv.length; i += 1) {
|
|
64
|
-
const a = argv[i];
|
|
65
|
-
if (a === "--check") {
|
|
66
|
-
cfg.check = true;
|
|
67
|
-
} else if (a === "--report") {
|
|
68
|
-
const next = argv[i + 1];
|
|
69
|
-
if (next && !next.startsWith("--")) {
|
|
70
|
-
cfg.report = argv[++i];
|
|
71
|
-
} else {
|
|
72
|
-
cfg.report = "reports/component-duplicates-components.md";
|
|
73
|
-
}
|
|
74
|
-
} else if (a === "--roots") {
|
|
75
|
-
if (!argv[i + 1]) fail("--roots requires a value (colon-separated list of directories)");
|
|
76
|
-
cfg.roots = argv[++i].split(":").filter(Boolean);
|
|
77
|
-
} else if (a === "--out") {
|
|
78
|
-
if (!argv[i + 1]) fail("--out requires a value");
|
|
79
|
-
cfg.out = argv[++i];
|
|
80
|
-
} else if (a === "--registry") {
|
|
81
|
-
if (!argv[i + 1]) fail("--registry requires a value");
|
|
82
|
-
cfg.registry = argv[++i];
|
|
83
|
-
} else if (a === "--repo-root") {
|
|
84
|
-
if (!argv[i + 1]) fail("--repo-root requires a value");
|
|
85
|
-
cfg.repoRoot = argv[++i];
|
|
86
|
-
} else if (a === "--app-roots") {
|
|
87
|
-
if (!argv[i + 1]) fail("--app-roots requires a value (colon-separated list of directories)");
|
|
88
|
-
cfg.appRoots = argv[++i].split(":").filter(Boolean);
|
|
89
|
-
} else if (a === "--ignore-dirs") {
|
|
90
|
-
if (!argv[i + 1]) fail("--ignore-dirs requires a value (colon-separated list of directory names)");
|
|
91
|
-
cfg.ignoreDirs = argv[++i].split(":").filter(Boolean);
|
|
92
|
-
} else if (a === "--raw-html") {
|
|
93
|
-
cfg.rawHtml = true;
|
|
94
|
-
} else if (a === "--parts") {
|
|
95
|
-
cfg.parts = true;
|
|
96
|
-
} else if (a === "--verbose") {
|
|
97
|
-
cfg.verbose = true;
|
|
98
|
-
} else if (a === "--include") {
|
|
99
|
-
if (!argv[i + 1]) fail("--include requires a value (colon-separated list of file globs, e.g. a.tsx:b.tsx)");
|
|
100
|
-
cfg.include = argv[++i].split(":").filter(Boolean);
|
|
101
|
-
} else if (a === "--log") {
|
|
102
|
-
if (!argv[i + 1]) fail("--log requires a value (path to the stage log file)");
|
|
103
|
-
cfg.log = argv[++i];
|
|
104
|
-
} else if (a === "--thresholds") {
|
|
105
|
-
if (!argv[i + 1]) fail("--thresholds requires a value (duplicate:similar, e.g. 0.5:0.2)");
|
|
106
|
-
const [dup, sim] = argv[++i].split(":").map((s) => Number.parseFloat(s));
|
|
107
|
-
if (!Number.isFinite(dup) || !Number.isFinite(sim)) fail("--thresholds must be two numbers separated by a colon (duplicate:similar)");
|
|
108
|
-
cfg.thresholds = { duplicate: dup, similar: sim };
|
|
109
|
-
} else if (a === "--min-cluster") {
|
|
110
|
-
if (!argv[i + 1]) fail("--min-cluster requires a value (min app components matching one canonical to flag it as a missing component)");
|
|
111
|
-
const n = Number.parseInt(argv[i + 1], 10);
|
|
112
|
-
if (!Number.isFinite(n) || n < 2) fail("--min-cluster must be an integer >= 2");
|
|
113
|
-
cfg.minCluster = n;
|
|
114
|
-
i += 1;
|
|
115
|
-
} else if (a === "--config") {
|
|
116
|
-
if (!argv[i + 1]) fail("--config requires a value (path to an eslint.config.js or a standalone options file)");
|
|
117
|
-
cfg.configFile = argv[++i];
|
|
118
|
-
} else if (a === "--help" || a === "-h") {
|
|
119
|
-
console.log("Usage: component-uniqueness [--roots a:b] [--out file] [--registry file] [--repo-root dir] [--check] [--report [file.md]] [--app-roots a:b] [--ignore-dirs a:b] [--include a.tsx:b.tsx] [--thresholds dup:sim] [--raw-html] [--parts] [--config file] [--log file] [--min-cluster N] [--verbose]");
|
|
120
|
-
console.log("Rule options (weights, thresholds, rawHtml, parts, include, ignoreDirs) are read from the consumer's eslint.config.js (rule md-code/component-uniqueness) or md-code-component-uniqueness.config.js; --config points to a specific file. CLI flags override the config.");
|
|
121
|
-
process.exit(0);
|
|
122
|
-
} else {
|
|
123
|
-
fail(`unknown argument: ${a} (see --help)`);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return cfg;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
const resolveAgainst = (value, repoRoot) => {
|
|
130
|
-
return path.isAbsolute(value) ? value : path.resolve(repoRoot, value);
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const relPosix = (repoRoot, abs) => {
|
|
134
|
-
return path.relative(repoRoot, abs).split(path.sep).join("/");
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
const collectFiles = (roots, repoRoot, shouldSkip, skipDirs, exts, ignoreDirs) => {
|
|
138
|
-
const files = [];
|
|
139
|
-
let count = 0;
|
|
140
|
-
for (const relRoot of roots) {
|
|
141
|
-
const rootDir = resolveAgainst(relRoot, repoRoot);
|
|
142
|
-
if (!fs.existsSync(rootDir)) continue;
|
|
143
|
-
for (const file of walk(rootDir, exts, ignoreDirs)) {
|
|
144
|
-
if (shouldSkip(file)) continue;
|
|
145
|
-
if (skipDirs && [...skipDirs].some((d) => file === d || file.startsWith(d + path.sep))) continue;
|
|
146
|
-
const rel = relPosix(repoRoot, file);
|
|
147
|
-
count += 1;
|
|
148
|
-
files.push({ file, rel });
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return { files, count };
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
const parseComponents = (files, canonNames, config, role = "CANONICAL") => {
|
|
155
|
-
const comps = [];
|
|
156
|
-
for (const { file, rel } of files) {
|
|
157
|
-
let parsed;
|
|
158
|
-
let source;
|
|
159
|
-
try {
|
|
160
|
-
source = fs.readFileSync(file, "utf8");
|
|
161
|
-
parsed = parseComponentsViaEntrypoint(file, canonNames, config);
|
|
162
|
-
} catch (err) {
|
|
163
|
-
console.warn(`[component-uniqueness] could not parse ${rel}: ${err && err.message ? err.message : err}`);
|
|
164
|
-
continue;
|
|
165
|
-
}
|
|
166
|
-
for (const c of parsed) {
|
|
167
|
-
if (log.enabled()) {
|
|
168
|
-
log.stage("normalizer:before", { role, file: rel, name: c.name, tree: treeSummary(c.tree), rootTags: c.rootTags, a11y: c.a11y });
|
|
169
|
-
}
|
|
170
|
-
const candidate = buildCandidate(c, config);
|
|
171
|
-
const withPath = { ...candidate, path: rel };
|
|
172
|
-
if (log.enabled()) {
|
|
173
|
-
log.stage("normalizer:after", { role, file: rel, name: c.name, tree: treeSummary(c.tree), domTree: treeSummary(candidate.dom && candidate.dom.tree), framework: { props: candidate.framework.props, events: candidate.framework.events, names: candidate.framework.names, open: candidate.framework.open } });
|
|
174
|
-
}
|
|
175
|
-
comps.push(withPath);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
return comps;
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
const matchPairs = (appComponents, canonComponents, config) => {
|
|
182
|
-
const canonNames = [...new Set(canonComponents.map((c) => c.name))];
|
|
183
|
-
const matches = [];
|
|
184
|
-
for (const app of appComponents) {
|
|
185
|
-
for (const canon of canonComponents) {
|
|
186
|
-
if (canon.path === app.path && canon.name === app.name) continue;
|
|
187
|
-
if (!canSeeCanon(app.path, canon.path, config.componentsFolder)) continue;
|
|
188
|
-
if (log.enabled()) {
|
|
189
|
-
log.stage("matcher:before", { app: candidateSummary(app, "candidate"), canon: candidateSummary(canon, "canonical") });
|
|
190
|
-
}
|
|
191
|
-
const signals = matchSignals(app, canon, { canonNames, frameworkMatcher });
|
|
192
|
-
if (log.enabled()) {
|
|
193
|
-
log.stage("matcher:after", { app: { name: app.name, path: app.path }, canon: { name: canon.name, path: canon.path }, signals });
|
|
194
|
-
}
|
|
195
|
-
if (log.enabled()) {
|
|
196
|
-
log.stage("decision:before", { app: { name: app.name, path: app.path }, canon: { name: canon.name, path: canon.path }, signals });
|
|
197
|
-
}
|
|
198
|
-
const decision = decide(app, canon, { signals, weights: config.weights, thresholds: config.thresholds });
|
|
199
|
-
if (log.enabled()) {
|
|
200
|
-
log.stage("decision:after", { app: { name: app.name, path: app.path }, canon: { name: canon.name, path: canon.path }, confidence: decision.confidence, tier: decision.tier, isDuplicate: decision.isDuplicate, weights: decision.weights, signals: Object.fromEntries(Object.entries(decision.signals).map(([k, s]) => [k, s.score])) });
|
|
201
|
-
}
|
|
202
|
-
if (!decision.tier) continue;
|
|
203
|
-
matches.push({ app, canon, decision, tier: decision.tier, reason: decision.reason });
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
return matches;
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
const applyFilters = (matches) => {
|
|
210
|
-
const kept = [];
|
|
211
|
-
const dropped = [];
|
|
212
|
-
for (const match of matches) {
|
|
213
|
-
if (log.enabled()) {
|
|
214
|
-
log.stage("filter:before", { app: { name: match.app.name, path: match.app.path }, canon: { name: match.canon.name, path: match.canon.path }, confidence: match.decision.confidence });
|
|
215
|
-
}
|
|
216
|
-
let hit = null;
|
|
217
|
-
for (const f of FILTERS) {
|
|
218
|
-
const reason = f.test(match);
|
|
219
|
-
if (reason) {
|
|
220
|
-
hit = { match, filter: f.name, reason };
|
|
221
|
-
break;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
if (hit) {
|
|
225
|
-
dropped.push(hit);
|
|
226
|
-
if (log.enabled()) log.stage("filter:after", { app: { name: match.app.name, path: match.app.path }, canon: { name: match.canon.name, path: match.canon.path }, outcome: "dropped", filter: hit.filter, reason: hit.reason });
|
|
227
|
-
} else {
|
|
228
|
-
kept.push(match);
|
|
229
|
-
if (log.enabled()) log.stage("filter:after", { app: { name: match.app.name, path: match.app.path }, canon: { name: match.canon.name, path: match.canon.path }, outcome: "kept", confidence: match.decision.confidence });
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
return { kept, dropped };
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
const pairKey = (match) => [match.app.name, match.canon.name].sort().join("::");
|
|
236
|
-
|
|
237
|
-
const dedupPairs = (matches) => {
|
|
238
|
-
const best = new Map();
|
|
239
|
-
for (const match of matches) {
|
|
240
|
-
const key = pairKey(match);
|
|
241
|
-
const prev = best.get(key);
|
|
242
|
-
if (!prev || match.decision.confidence > prev.decision.confidence) best.set(key, match);
|
|
243
|
-
}
|
|
244
|
-
return [...best.values()];
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
const SIMILAR_FANOUT_CAP = 2;
|
|
248
|
-
|
|
249
|
-
const capSimilarFanout = (matches) => {
|
|
250
|
-
const byApp = new Map();
|
|
251
|
-
for (const m of matches) {
|
|
252
|
-
if (m.decision.tier !== "similar") continue;
|
|
253
|
-
if (!byApp.has(m.app.name)) byApp.set(m.app.name, []);
|
|
254
|
-
byApp.get(m.app.name).push(m);
|
|
255
|
-
}
|
|
256
|
-
const keep = new Set(matches.filter((m) => m.decision.tier !== "similar"));
|
|
257
|
-
for (const group of byApp.values()) {
|
|
258
|
-
group.sort((a, b) => b.decision.confidence - a.decision.confidence || a.canon.name.localeCompare(b.canon.name));
|
|
259
|
-
for (const m of group.slice(0, SIMILAR_FANOUT_CAP)) keep.add(m);
|
|
260
|
-
}
|
|
261
|
-
return [...keep];
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
const collectRawHtml = (appFiles, canonComponents, config, canonNames) => {
|
|
265
|
-
if (!config.rawHtml) return [];
|
|
266
|
-
const rows = [];
|
|
267
|
-
for (const { file, rel } of appFiles) {
|
|
268
|
-
if (inComponentsFolder(rel, config.componentsFolder)) continue;
|
|
269
|
-
let elements;
|
|
270
|
-
try {
|
|
271
|
-
elements = parseElementsViaEntrypoint(file, canonNames, config);
|
|
272
|
-
} catch (err) {
|
|
273
|
-
console.warn(`[component-uniqueness] could not parse elements of ${rel}: ${err && err.message ? err.message : err}`);
|
|
274
|
-
continue;
|
|
275
|
-
}
|
|
276
|
-
for (const element of elements) {
|
|
277
|
-
const candidate = buildElementCandidate(element, config);
|
|
278
|
-
if (!candidate) continue;
|
|
279
|
-
const best = bestOf(matchPairs([{ ...candidate, path: rel }], canonComponents, config));
|
|
280
|
-
if (!best) continue;
|
|
281
|
-
rows.push({
|
|
282
|
-
tag: element.name,
|
|
283
|
-
path: rel,
|
|
284
|
-
line: element.line,
|
|
285
|
-
suggestion: { component: best.canon.name, path: best.canon.path },
|
|
286
|
-
confidence: best.decision.confidence,
|
|
287
|
-
reason: best.decision.reason,
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
return rows;
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
const SIZE_RATIO = 3;
|
|
296
|
-
const MIN_PART_CANON_NODES = 1;
|
|
297
|
-
const sizeCache = new WeakMap();
|
|
298
|
-
|
|
299
|
-
const nodeCount = (tree) => {
|
|
300
|
-
if (!tree) return 0;
|
|
301
|
-
const own = tree.tag === "fragment" || tree.tag === "children" ? 0 : 1;
|
|
302
|
-
|
|
303
|
-
return own + (tree.children || []).reduce((sum, child) => sum + nodeCount(child), 0);
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
const sizeOf = (item) => {
|
|
307
|
-
if (!sizeCache.has(item)) sizeCache.set(item, nodeCount(item.tree));
|
|
308
|
-
|
|
309
|
-
return sizeCache.get(item);
|
|
310
|
-
};
|
|
311
|
-
|
|
312
|
-
const tagBag = (tree, bag = new Map()) => {
|
|
313
|
-
if (!tree) return bag;
|
|
314
|
-
if (tree.tag !== "fragment" && tree.tag !== "children") bag.set(tree.tag, (bag.get(tree.tag) || 0) + 1);
|
|
315
|
-
for (const child of tree.children || []) tagBag(child, bag);
|
|
316
|
-
|
|
317
|
-
return bag;
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
const bagCache = new WeakMap();
|
|
321
|
-
|
|
322
|
-
const bagOf = (item) => {
|
|
323
|
-
if (!bagCache.has(item)) bagCache.set(item, tagBag(item.tree));
|
|
324
|
-
|
|
325
|
-
return bagCache.get(item);
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
const bagDice = (a, b) => {
|
|
329
|
-
let shared = 0;
|
|
330
|
-
let total = 0;
|
|
331
|
-
for (const n of a.values()) total += n;
|
|
332
|
-
for (const n of b.values()) total += n;
|
|
333
|
-
for (const [tag, n] of a) shared += Math.min(n, b.get(tag) || 0);
|
|
334
|
-
|
|
335
|
-
return total === 0 ? 0 : (2 * shared) / total;
|
|
336
|
-
};
|
|
337
|
-
|
|
338
|
-
const MIN_TAG_DICE = 0.5;
|
|
339
|
-
|
|
340
|
-
const rootTagMatches = (partTag, canonTag) => tagAffinity(partTag, canonTag) > 0;
|
|
341
|
-
|
|
342
|
-
const comparableCanons = (part, canonComponents) => {
|
|
343
|
-
const partSize = sizeOf(part);
|
|
344
|
-
const partBag = bagOf(part);
|
|
345
|
-
|
|
346
|
-
return canonComponents.filter((canon) => {
|
|
347
|
-
const size = sizeOf(canon);
|
|
348
|
-
if (size < MIN_PART_CANON_NODES) return false;
|
|
349
|
-
const canonBag = bagOf(canon);
|
|
350
|
-
if (size === 1) {
|
|
351
|
-
const tag = [...canonBag.keys()][0];
|
|
352
|
-
|
|
353
|
-
return rootTagMatches(part.rootTag, tag) && partBag.has(tag);
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
return size <= partSize * SIZE_RATIO && partSize <= size * SIZE_RATIO && bagDice(partBag, canonBag) >= MIN_TAG_DICE;
|
|
357
|
-
});
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
const largestParts = (found) => {
|
|
361
|
-
const sorted = [...found].sort((a, b) => b.part.endLine - b.part.line - (a.part.endLine - a.part.line));
|
|
362
|
-
const kept = [];
|
|
363
|
-
for (const item of sorted) {
|
|
364
|
-
const inside = kept.some((k) => k.best.canon.name === item.best.canon.name && k.part.line <= item.part.line && k.part.endLine >= item.part.endLine);
|
|
365
|
-
if (!inside) kept.push(item);
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
return kept.sort((a, b) => a.part.line - b.part.line);
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
const findParts = (file, rel, canonComponents, config, canonNames) => {
|
|
372
|
-
let parts;
|
|
373
|
-
try {
|
|
374
|
-
parts = parsePartsViaEntrypoint(file, canonNames, config);
|
|
375
|
-
} catch (err) {
|
|
376
|
-
console.warn(`[component-uniqueness] could not parse parts of ${rel}: ${err && err.message ? err.message : err}`);
|
|
377
|
-
|
|
378
|
-
return [];
|
|
379
|
-
}
|
|
380
|
-
const found = [];
|
|
381
|
-
for (const part of parts) {
|
|
382
|
-
const pool = comparableCanons(part, canonComponents);
|
|
383
|
-
if (pool.length === 0) continue;
|
|
384
|
-
const candidate = { ...buildCandidate(part, config), path: rel };
|
|
385
|
-
const { kept } = applyFilters(matchPairs([candidate], pool, config));
|
|
386
|
-
const ranked = [...kept].sort((a, b) => b.decision.confidence - a.decision.confidence);
|
|
387
|
-
if (ranked.length === 0) continue;
|
|
388
|
-
found.push({ part, best: ranked[0], alt: ranked[1] || null });
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
return largestParts(found);
|
|
392
|
-
};
|
|
393
|
-
|
|
394
|
-
const collectParts = (appFiles, canonComponents, config, canonNames) => {
|
|
395
|
-
if (!config.parts) return [];
|
|
396
|
-
const rows = [];
|
|
397
|
-
for (const { file, rel } of appFiles) {
|
|
398
|
-
for (const { part, best, alt } of findParts(file, rel, canonComponents, config, canonNames)) {
|
|
399
|
-
const suggestions = [{ component: best.canon.name, path: best.canon.path, confidence: best.decision.confidence, reason: best.decision.reason }];
|
|
400
|
-
if (alt) suggestions.push({ component: alt.canon.name, path: alt.canon.path, confidence: alt.decision.confidence, reason: alt.decision.reason });
|
|
401
|
-
rows.push({
|
|
402
|
-
owner: part.owner,
|
|
403
|
-
tag: part.rootTag,
|
|
404
|
-
path: rel,
|
|
405
|
-
line: part.line,
|
|
406
|
-
endLine: part.endLine,
|
|
407
|
-
suggestions,
|
|
408
|
-
});
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
return rows;
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
const stageLog = (enabled) => {
|
|
416
|
-
const started = Date.now();
|
|
417
|
-
|
|
418
|
-
return (label) => {
|
|
419
|
-
if (!enabled) return;
|
|
420
|
-
if (global.gc) global.gc();
|
|
421
|
-
console.log(`[component-uniqueness] ${label}: ${((Date.now() - started) / 1000).toFixed(1)}s, heap ${Math.round(process.memoryUsage().heapUsed / 1048576)} MB`);
|
|
422
|
-
};
|
|
423
|
-
};
|
|
424
|
-
|
|
425
|
-
const buildComponentReport = async (args, repoRoot, roots, shouldSkip, config, registry, catalogComponents) => {
|
|
426
|
-
const exts = config.exts || DEFAULT_EXTS;
|
|
427
|
-
const ignoreDirs = config.ignoreDirs;
|
|
428
|
-
const dedupeFiles = (files) => {
|
|
429
|
-
const seen = new Set();
|
|
430
|
-
const out = [];
|
|
431
|
-
for (const f of files) {
|
|
432
|
-
if (seen.has(f.file)) continue;
|
|
433
|
-
seen.add(f.file);
|
|
434
|
-
out.push(f);
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
return out;
|
|
438
|
-
};
|
|
439
|
-
|
|
440
|
-
const stage = stageLog(args.verbose);
|
|
441
|
-
|
|
442
|
-
const shouldSkipCanon = (abs) => isIgnored(relPosix(repoRoot, abs), [], config.exclude);
|
|
443
|
-
const canonFiles = dedupeFiles(collectFiles(roots, repoRoot, shouldSkipCanon, null, exts, ignoreDirs).files);
|
|
444
|
-
const canonComponents = parseComponents(canonFiles, null, config);
|
|
445
|
-
const canonNames = new Set(canonComponents.map((c) => c.name));
|
|
446
|
-
const uniqueCanon = parseComponents(canonFiles, canonNames, config);
|
|
447
|
-
stage(`canonical parsed (${uniqueCanon.length})`);
|
|
448
|
-
|
|
449
|
-
const appInclude = args.appRoots ? [] : config.include;
|
|
450
|
-
const appShouldSkip = (abs) => {
|
|
451
|
-
const rel = relPosix(repoRoot, abs);
|
|
452
|
-
|
|
453
|
-
return isIgnored(rel, appInclude, config.exclude);
|
|
454
|
-
};
|
|
455
|
-
const appFiles = collectFiles(args.appRoots || ["."], repoRoot, appShouldSkip, null, exts, ignoreDirs);
|
|
456
|
-
const appComponents = parseComponents(appFiles.files, canonNames, config, "CANDIDATE");
|
|
457
|
-
stage(`app parsed (${appComponents.length})`);
|
|
458
|
-
|
|
459
|
-
const matches = matchPairs(appComponents, uniqueCanon, config);
|
|
460
|
-
const { kept, dropped } = applyFilters(matches);
|
|
461
|
-
const deduped = dedupPairs(kept);
|
|
462
|
-
stage("components matched");
|
|
463
|
-
const inLayer = (p) => (config.componentsFolder || []).some((d) => p.startsWith(d));
|
|
464
|
-
const layerMatches = capSimilarFanout(deduped.filter((m) => inLayer(m.app.path)));
|
|
465
|
-
const appMatches = deduped.filter((m) => !inLayer(m.app.path));
|
|
466
|
-
const cappedAppMatches = capSimilarFanout(appMatches);
|
|
467
|
-
const { clusters, remaining } = findMissingComponents(cappedAppMatches, { minCluster: args.minCluster });
|
|
468
|
-
if (log.enabled()) {
|
|
469
|
-
log.stage("missing-component", { minCluster: args.minCluster, clusters: clusters.map((c) => ({ name: c.name, canon: c.canon.name, apps: c.matches.map((m) => m.app.name) })), remaining: remaining.length });
|
|
470
|
-
}
|
|
471
|
-
stage("missing-component clustered");
|
|
472
|
-
const rawHtml = collectRawHtml(appFiles.files, uniqueCanon, config, canonNames);
|
|
473
|
-
stage("raw html done");
|
|
474
|
-
const parts = collectParts(appFiles.files, uniqueCanon, config, canonNames);
|
|
475
|
-
stage("parts done");
|
|
476
|
-
|
|
477
|
-
return {
|
|
478
|
-
matches: [...remaining, ...layerMatches],
|
|
479
|
-
dropped,
|
|
480
|
-
missingClusters: clusters,
|
|
481
|
-
rawHtml,
|
|
482
|
-
parts,
|
|
483
|
-
layerFolders: config.componentsFolder,
|
|
484
|
-
files: appFiles.count,
|
|
485
|
-
canonCount: uniqueCanon.length,
|
|
486
|
-
appCount: appComponents.length,
|
|
487
|
-
};
|
|
488
|
-
};
|
|
489
|
-
|
|
490
|
-
const main = async () => {
|
|
491
|
-
const args = parseArgs(process.argv.slice(2));
|
|
492
|
-
const cwd = process.cwd();
|
|
493
|
-
|
|
494
|
-
const loaded = await loadRuleOptions(cwd, args.configFile);
|
|
495
|
-
const configOptions = loaded ? { ...loaded.options } : {};
|
|
496
|
-
if (loaded) {
|
|
497
|
-
console.log(`[component-uniqueness] config from ${loaded.source}`);
|
|
498
|
-
} else if (!args.configFile) {
|
|
499
|
-
console.log("[component-uniqueness] no rule options found in eslint.config.js / md-code-component-uniqueness.config.js; using defaults");
|
|
500
|
-
}
|
|
501
|
-
if (args.ignoreDirs) {
|
|
502
|
-
const base = Array.isArray(configOptions.ignoreDirs) ? configOptions.ignoreDirs : [];
|
|
503
|
-
configOptions.ignoreDirs = [...new Set([...base, ...args.ignoreDirs])];
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
const config = normalizeOptions(configOptions);
|
|
507
|
-
if (args.include) config.include = [...new Set([...(config.include || []), ...args.include])];
|
|
508
|
-
if (args.thresholds) config.thresholds = { ...(config.thresholds || {}), ...args.thresholds };
|
|
509
|
-
if (args.rawHtml) config.rawHtml = true;
|
|
510
|
-
if (args.parts) config.parts = true;
|
|
511
|
-
if (args.log) log.enable(args.log);
|
|
512
|
-
await require("./playgrounds/style-entrypoint.js").whenReady(config);
|
|
513
|
-
const rootMarkers = config.rootMarkers;
|
|
514
|
-
const repoRoot = args.repoRoot ? path.resolve(cwd, args.repoRoot) : findRepoRoot(cwd, rootMarkers) || cwd;
|
|
515
|
-
|
|
516
|
-
const configRoots = (config.componentsFolder || []).map((d) => String(d).replace(/\\/g, "/").replace(/\/+$/, ""));
|
|
517
|
-
const roots = args.roots || configRoots;
|
|
518
|
-
if (!roots || roots.length === 0) {
|
|
519
|
-
fail("no scan roots: pass --roots <dir1>:<dir2> or set componentsFolder in the rule options (eslint.config.js / component-uniqueness.config.js)");
|
|
520
|
-
}
|
|
521
|
-
if (!config.componentsFolder || config.componentsFolder.length === 0) {
|
|
522
|
-
config.componentsFolder = roots;
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
const outPath = resolveAgainst(args.out || config.catalogPath, repoRoot);
|
|
526
|
-
const registry = args.registry ? loadRegistry({ ...config, registry: null, registryPath: args.registry }) : loadRegistry(config);
|
|
527
|
-
const catalogComponents = ((loadCatalog(config) || {}).components) || [];
|
|
528
|
-
|
|
529
|
-
const include = config.include;
|
|
530
|
-
const exclude = config.exclude;
|
|
531
|
-
const shouldSkip = (abs) => isIgnored(relPosix(repoRoot, abs), include, exclude);
|
|
532
|
-
const exts = config.exts;
|
|
533
|
-
const ignoreDirs = config.ignoreDirs;
|
|
534
|
-
|
|
535
|
-
const shouldSkipCanon = (abs) => isIgnored(relPosix(repoRoot, abs), [], config.exclude);
|
|
536
|
-
const canonFiles = collectFiles(roots, repoRoot, shouldSkipCanon, null, exts, ignoreDirs).files;
|
|
537
|
-
const canonComponents = parseComponents(canonFiles, null, config);
|
|
538
|
-
const canonNames = new Set(canonComponents.map((c) => c.name));
|
|
539
|
-
const final = parseComponents(canonFiles, canonNames, config);
|
|
540
|
-
|
|
541
|
-
if (args.check) {
|
|
542
|
-
let current = null;
|
|
543
|
-
try {
|
|
544
|
-
current = JSON.parse(fs.readFileSync(outPath, "utf8"));
|
|
545
|
-
} catch {}
|
|
546
|
-
const same = current && JSON.stringify(current.components || []) === JSON.stringify(final);
|
|
547
|
-
if (!same) {
|
|
548
|
-
fail(`catalog is out of date (${final.length} component(s) expected at ${path.relative(repoRoot, outPath)}); regenerate it (node main.js)`);
|
|
549
|
-
}
|
|
550
|
-
console.log(`[component-uniqueness] catalog up to date (${final.length} component(s))`);
|
|
551
|
-
return;
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
writeCatalog(outPath, final);
|
|
555
|
-
const tagCount = new Set(final.map((c) => (c.rootTags || [])[0]).filter(Boolean)).size;
|
|
556
|
-
console.log(`[component-uniqueness] wrote ${path.relative(repoRoot, outPath)} (${final.length} component(s), ${tagCount} root tag(s), ${canonFiles.length} file(s) scanned)`);
|
|
557
|
-
|
|
558
|
-
if (args.report) {
|
|
559
|
-
const reportPath = path.isAbsolute(args.report) ? args.report : path.resolve(process.cwd(), args.report);
|
|
560
|
-
const comp = await buildComponentReport(args, repoRoot, roots, shouldSkip, config, registry, catalogComponents);
|
|
561
|
-
fs.mkdirSync(path.dirname(reportPath), { recursive: true });
|
|
562
|
-
fs.writeFileSync(reportPath, renderComponentReport(comp, args.verbose));
|
|
563
|
-
console.log(`[component-uniqueness] wrote report ${reportPath} (${comp.matches.length} duplicate component(s), ${comp.rawHtml.length} raw-html element(s) across ${comp.files} file(s))`);
|
|
564
|
-
}
|
|
565
|
-
log.flush();
|
|
566
|
-
};
|
|
567
|
-
|
|
568
|
-
if (require.main === module) {
|
|
569
|
-
main().catch((err) => {
|
|
570
|
-
fail(err && err.message ? err.message : String(err));
|
|
571
|
-
});
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
module.exports = {
|
|
575
|
-
buildCandidate,
|
|
576
|
-
parseComponents,
|
|
577
|
-
matchPairs,
|
|
578
|
-
applyFilters,
|
|
579
|
-
collectRawHtml,
|
|
580
|
-
findParts,
|
|
581
|
-
frameworkMatcher,
|
|
582
|
-
comparableCanons,
|
|
583
|
-
buildComponentReport,
|
|
584
|
-
};
|
package/matcher/a11y.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const TEST_ID_WEIGHT = 0.5;
|
|
5
|
-
|
|
6
|
-
const parseToken = (token) => {
|
|
7
|
-
const t = String(token || "").toLowerCase();
|
|
8
|
-
const m = t.match(/^(role|aria-[\w-]+)(?::(.*))?$/);
|
|
9
|
-
if (m) return { name: m[1], value: m[2] || "" };
|
|
10
|
-
return null;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const classify = (token) => {
|
|
14
|
-
const t = String(token || "").toLowerCase();
|
|
15
|
-
if (t.startsWith("data-test") || t.startsWith("data-testid") || t.startsWith("data-test-id")) return "testid";
|
|
16
|
-
const p = parseToken(t);
|
|
17
|
-
if (p) return "a11y";
|
|
18
|
-
return null;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const tokensOf = (c, kind) => {
|
|
22
|
-
const s = new Set();
|
|
23
|
-
for (const t of c.a11y || []) {
|
|
24
|
-
if (classify(t) === kind) s.add(String(t).toLowerCase());
|
|
25
|
-
}
|
|
26
|
-
return s;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const jaccard = (a, b) => {
|
|
30
|
-
if (a.size === 0 || b.size === 0) return 0;
|
|
31
|
-
let inter = 0;
|
|
32
|
-
for (const t of a) if (b.has(t)) inter += 1;
|
|
33
|
-
const union = a.size + b.size - inter;
|
|
34
|
-
return union === 0 ? 0 : inter / union;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const matchA11y = (candidate, canon) => {
|
|
38
|
-
const candA = tokensOf(candidate, "a11y");
|
|
39
|
-
const canonA = tokensOf(canon, "a11y");
|
|
40
|
-
const candT = tokensOf(candidate, "testid");
|
|
41
|
-
const canonT = tokensOf(canon, "testid");
|
|
42
|
-
|
|
43
|
-
const a11ySim = jaccard(candA, canonA);
|
|
44
|
-
const testSim = jaccard(candT, canonT);
|
|
45
|
-
|
|
46
|
-
const parts = [];
|
|
47
|
-
if (candA.size > 0 || canonA.size > 0) parts.push({ w: 1 - TEST_ID_WEIGHT, v: a11ySim });
|
|
48
|
-
if (candT.size > 0 || canonT.size > 0) parts.push({ w: TEST_ID_WEIGHT, v: testSim });
|
|
49
|
-
|
|
50
|
-
const totalW = parts.reduce((s, p) => s + p.w, 0);
|
|
51
|
-
const score = totalW === 0 ? 0 : parts.reduce((s, p) => s + p.w * p.v, 0) / totalW;
|
|
52
|
-
|
|
53
|
-
const evidence = {
|
|
54
|
-
candidateA11y: [...candA].sort(),
|
|
55
|
-
canonicalA11y: [...canonA].sort(),
|
|
56
|
-
candidateTestId: [...candT].sort(),
|
|
57
|
-
canonicalTestId: [...canonT].sort(),
|
|
58
|
-
a11ySimilarity: a11ySim,
|
|
59
|
-
testIdSimilarity: testSim,
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
return { score, evidence };
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
module.exports = { matchA11y, tokensOf, classify, TEST_ID_WEIGHT };
|
package/matcher/behavior.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const jaccard = (a, b) => {
|
|
5
|
-
if (a.size === 0 || b.size === 0) return 0;
|
|
6
|
-
let inter = 0;
|
|
7
|
-
for (const t of a) if (b.has(t)) inter += 1;
|
|
8
|
-
const union = a.size + b.size - inter;
|
|
9
|
-
return union === 0 ? 0 : inter / union;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const matchBehavior = (candidate, canon) => {
|
|
13
|
-
const candA = new Set((candidate.framework && candidate.framework.events) || []);
|
|
14
|
-
const canonA = new Set((canon.framework && canon.framework.events) || []);
|
|
15
|
-
const shared = new Set([...candA].filter((event) => canonA.has(event)));
|
|
16
|
-
const base = jaccard(candA, canonA);
|
|
17
|
-
const union = candA.size + canonA.size - shared.size;
|
|
18
|
-
const smallSetDiscount = Math.min(1, union / 2);
|
|
19
|
-
const score = base * smallSetDiscount;
|
|
20
|
-
return {
|
|
21
|
-
score,
|
|
22
|
-
evidence: {
|
|
23
|
-
candidateEvents: [...candA].sort(),
|
|
24
|
-
canonicalEvents: [...canonA].sort(),
|
|
25
|
-
sharedEvents: [...shared].sort(),
|
|
26
|
-
smallSetDiscount: Math.round(smallSetDiscount * 100) / 100,
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
module.exports = { matchBehavior };
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const react = require("./react.js");
|
|
4
|
-
|
|
5
|
-
const REGISTRY = {
|
|
6
|
-
react,
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
const getFrameworkMatcher = (framework) => {
|
|
10
|
-
if (!framework) return react;
|
|
11
|
-
const key = String(framework).toLowerCase();
|
|
12
|
-
return REGISTRY[key] || react;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
module.exports = { getFrameworkMatcher };
|