@md-code/component-uniqueness 0.1.2 → 0.1.4
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/README.md +10 -0
- package/config.js +28 -4
- package/filter/drop-child-element.js +1 -0
- package/filter/drop-no-structure.js +13 -0
- package/filter/missing-component.js +63 -25
- package/main.js +86 -18
- package/matcher/index.js +3 -3
- package/matcher/structure.js +131 -97
- package/normalize/dom.js +20 -0
- package/normalize/framework.js +0 -19
- package/normalize/style-resolver.js +24 -0
- package/normalize/style.js +1 -0
- package/package.json +13 -2
- package/playgrounds/framework-entrypoint.js +10 -3
- package/playgrounds/react.js +40 -9
- package/playgrounds/styled-components.js +132 -1
- package/report.js +162 -153
package/README.md
CHANGED
|
@@ -90,4 +90,14 @@ Options are read from the rule in `eslint.config.js`.
|
|
|
90
90
|
| `--app-roots a:b` | Folders to scan for the report (default: repo root) |
|
|
91
91
|
| `--repo-root <dir>` | Repo root (default: auto-detected) |
|
|
92
92
|
| `--out <file>` | Catalog path |
|
|
93
|
+
| `--include a.tsx:b.tsx` | Only scan matching app files |
|
|
94
|
+
| `--ignore-dirs a:b` | Extra directories to skip |
|
|
95
|
+
| `--thresholds dup:sim` | Confidence cut-offs, e.g. `0.5:0.3` |
|
|
96
|
+
| `--raw-html` | Also report raw HTML that should be a canonical component |
|
|
97
|
+
| `--parts` | Also report blocks inside components that duplicate a canonical |
|
|
98
|
+
| `--config file` | Read rule options from a specific `eslint.config.js` or options file |
|
|
99
|
+
| `--min-cluster N` | Min app components matching one canonical to flag it as a missing component (default 3) |
|
|
100
|
+
| `--log file` | Write a per-stage JSON log |
|
|
93
101
|
| `--verbose` | Also list what the filters dropped |
|
|
102
|
+
|
|
103
|
+
Rule options (`weights`, `thresholds`, `rawHtml`, `parts`, `include`, `ignoreDirs`) are always 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.
|
package/config.js
CHANGED
|
@@ -214,7 +214,7 @@ const pickOptions = (v) => {
|
|
|
214
214
|
return {};
|
|
215
215
|
};
|
|
216
216
|
|
|
217
|
-
const isRuleKey = (key) => key === RULE_NAME || key === RULE_SHORT || key === "component-uniqueness" || key.endsWith("/component-uniqueness");
|
|
217
|
+
const isRuleKey = (key) => typeof key === "string" && (key === RULE_NAME || key === RULE_SHORT || key === "component-uniqueness" || key.endsWith("/component-uniqueness"));
|
|
218
218
|
|
|
219
219
|
const ruleValue = (rules) => {
|
|
220
220
|
for (const [key, v] of Object.entries(rules)) {
|
|
@@ -225,13 +225,22 @@ const ruleValue = (rules) => {
|
|
|
225
225
|
return null;
|
|
226
226
|
};
|
|
227
227
|
|
|
228
|
+
const pluginRuleValue = (plugins) => {
|
|
229
|
+
for (const plugin of Object.values(plugins || {})) {
|
|
230
|
+
if (!plugin || typeof plugin !== "object") continue;
|
|
231
|
+
const v = ruleValue(plugin.rules || {});
|
|
232
|
+
if (v !== null && v !== undefined) return v;
|
|
233
|
+
}
|
|
234
|
+
return null;
|
|
235
|
+
};
|
|
236
|
+
|
|
228
237
|
const loadFlatOptions = async (file) => {
|
|
229
238
|
const mod = await loadModule(file);
|
|
230
239
|
const list = mod && mod.default !== undefined ? mod.default : mod;
|
|
231
240
|
if (!Array.isArray(list)) return null;
|
|
232
241
|
for (const entry of list) {
|
|
233
|
-
if (!entry || typeof entry !== "object"
|
|
234
|
-
const v = ruleValue(entry.rules);
|
|
242
|
+
if (!entry || typeof entry !== "object") continue;
|
|
243
|
+
const v = (entry.rules && ruleValue(entry.rules)) || (entry.plugins && pluginRuleValue(entry.plugins)) || null;
|
|
235
244
|
if (v === undefined || v === null) continue;
|
|
236
245
|
return pickOptions(v);
|
|
237
246
|
}
|
|
@@ -244,7 +253,22 @@ const loadLegacyOptions = async (file) => {
|
|
|
244
253
|
return v === undefined || v === null ? null : pickOptions(v);
|
|
245
254
|
};
|
|
246
255
|
|
|
247
|
-
const loadRuleOptions = async (dir) => {
|
|
256
|
+
const loadRuleOptions = async (dir, explicitFile = null) => {
|
|
257
|
+
if (explicitFile) {
|
|
258
|
+
const file = path.isAbsolute(explicitFile) ? explicitFile : path.resolve(dir, explicitFile);
|
|
259
|
+
if (!fs.existsSync(file)) return null;
|
|
260
|
+
if (file.endsWith(".json")) {
|
|
261
|
+
const options = pickOptions(JSON.parse(fs.readFileSync(file, "utf8")));
|
|
262
|
+
return options ? { options, source: file } : null;
|
|
263
|
+
}
|
|
264
|
+
try {
|
|
265
|
+
const isFlat = path.basename(file).startsWith("eslint.config");
|
|
266
|
+
const options = isFlat ? await loadFlatOptions(file) : pickOptions(await loadModule(file));
|
|
267
|
+
return options ? { options, source: file } : null;
|
|
268
|
+
} catch {
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
248
272
|
let d = dir;
|
|
249
273
|
for (;;) {
|
|
250
274
|
const standalone = path.join(d, "md-code-component-uniqueness.config.js");
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const test = (match) => {
|
|
4
4
|
if (match.tier === "name" || match.tier === "name-fuzzy" || match.tier === "jsx-block") return null;
|
|
5
|
+
if (match.app.partial) return null;
|
|
5
6
|
const appEls = (match.app.elementProps || []).length;
|
|
6
7
|
const canonEls = (match.canon.elementProps || []).length;
|
|
7
8
|
if (canonEls > 0 && appEls > 3 * canonEls) {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const test = (match) => {
|
|
4
|
+
if (!match.app.partial) return null;
|
|
5
|
+
const structure = match.decision.signals && match.decision.signals.structure;
|
|
6
|
+
if (!structure) return null;
|
|
7
|
+
if (structure.score <= 0) {
|
|
8
|
+
return "partial shares no structure with the canonical — match is driven only by framework/a11y/behavior";
|
|
9
|
+
}
|
|
10
|
+
return null;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
module.exports = { name: "drop-no-structure", test };
|
|
@@ -1,34 +1,68 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const { pascalWords, GENERIC_WORDS } = require("../normalize/words.js");
|
|
4
|
+
const { matchStructure } = require("../matcher/structure.js");
|
|
4
5
|
|
|
5
6
|
const DEFAULT_MIN_CLUSTER = 3;
|
|
7
|
+
const MIN_PAIRWISE_STRUCTURE = 0.4;
|
|
8
|
+
const MIN_PAIR_FLOOR = 0.3;
|
|
9
|
+
const CANON_CONF_FLOOR = 0.25;
|
|
6
10
|
|
|
7
11
|
const pascalCase = (words) => words.map((w) => w[0].toUpperCase() + w.slice(1).toLowerCase()).join("");
|
|
8
12
|
|
|
9
13
|
const suggestName = (appNames, canonName = null) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
for (const
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
counts.
|
|
17
|
-
|
|
14
|
+
if (canonName) {
|
|
15
|
+
const counts = new Map();
|
|
16
|
+
const order = [];
|
|
17
|
+
for (const name of appNames) {
|
|
18
|
+
for (const w of pascalWords(name)) {
|
|
19
|
+
const k = w.toLowerCase();
|
|
20
|
+
if (!counts.has(k)) {
|
|
21
|
+
counts.set(k, 0);
|
|
22
|
+
order.push(k);
|
|
23
|
+
}
|
|
24
|
+
counts.set(k, counts.get(k) + 1);
|
|
18
25
|
}
|
|
19
|
-
|
|
26
|
+
}
|
|
27
|
+
const common = order.filter((k) => counts.get(k) >= Math.ceil(appNames.length * 0.6));
|
|
28
|
+
const meaningful = common.filter((k) => !GENERIC_WORDS.has(k));
|
|
29
|
+
if (meaningful.length > 0) {
|
|
30
|
+
return pascalCase([meaningful[0], ...pascalWords(canonName).slice(0, 2)]);
|
|
20
31
|
}
|
|
21
32
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
if (canonName) return `Shared${canonName}`;
|
|
34
|
+
return "SharedComponent";
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const pairwiseAverage = (apps) => {
|
|
38
|
+
let total = 0;
|
|
39
|
+
let pairs = 0;
|
|
40
|
+
for (let i = 0; i < apps.length; i += 1) {
|
|
41
|
+
for (let j = i + 1; j < apps.length; j += 1) {
|
|
42
|
+
const s = matchStructure(apps[i], apps[j]);
|
|
43
|
+
total += s.score;
|
|
44
|
+
pairs += 1;
|
|
45
|
+
}
|
|
29
46
|
}
|
|
30
|
-
|
|
31
|
-
return
|
|
47
|
+
|
|
48
|
+
return pairs === 0 ? 0 : total / pairs;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const pairwiseAllAbove = (apps, floor) => {
|
|
52
|
+
for (let i = 0; i < apps.length; i += 1)
|
|
53
|
+
for (let j = i + 1; j < apps.length; j += 1) if (matchStructure(apps[i], apps[j]).score < floor) return false;
|
|
54
|
+
|
|
55
|
+
return true;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const resolveClusterMembers = (group) => {
|
|
59
|
+
const strong = group.filter((m) => m.decision.confidence >= CANON_CONF_FLOOR);
|
|
60
|
+
const apps = [...new Map(strong.map((m) => [m.app.name, m.app])).values()];
|
|
61
|
+
if (apps.length >= 2 && pairwiseAllAbove(apps, MIN_PAIR_FLOOR)) return strong;
|
|
62
|
+
const dups = strong.filter((m) => m.decision.tier === "duplicate");
|
|
63
|
+
const dupApps = [...new Map(dups.map((m) => [m.app.name, m.app])).values()];
|
|
64
|
+
|
|
65
|
+
return dups.length >= 2 && pairwiseAllAbove(dupApps, MIN_PAIR_FLOOR) ? dups : null;
|
|
32
66
|
};
|
|
33
67
|
|
|
34
68
|
const findMissingComponents = (matches, { minCluster = DEFAULT_MIN_CLUSTER } = {}) => {
|
|
@@ -39,19 +73,22 @@ const findMissingComponents = (matches, { minCluster = DEFAULT_MIN_CLUSTER } = {
|
|
|
39
73
|
byCanon.get(key).push(match);
|
|
40
74
|
}
|
|
41
75
|
const clusters = [];
|
|
42
|
-
const clusteredKeys = new Set();
|
|
43
76
|
for (const [canonName, group] of byCanon) {
|
|
44
|
-
const
|
|
45
|
-
if (
|
|
46
|
-
|
|
77
|
+
const members = resolveClusterMembers(group);
|
|
78
|
+
if (!members) continue;
|
|
79
|
+
if (new Set(members.map((m) => m.app.name)).size < minCluster) continue;
|
|
47
80
|
clusters.push({
|
|
48
|
-
name: suggestName(
|
|
81
|
+
name: suggestName(members.map((m) => m.app.name), canonName),
|
|
49
82
|
canon: group[0].canon,
|
|
50
|
-
matches: [...
|
|
83
|
+
matches: [...members].sort((a, b) => b.decision.confidence - a.decision.confidence),
|
|
51
84
|
});
|
|
52
85
|
}
|
|
53
86
|
clusters.sort((a, b) => b.matches.length - a.matches.length || a.name.localeCompare(b.name));
|
|
54
|
-
const
|
|
87
|
+
const clusteredKeys = new Set();
|
|
88
|
+
for (const c of clusters) {
|
|
89
|
+
for (const m of c.matches) clusteredKeys.add(m.app.name + "::" + m.canon.name);
|
|
90
|
+
}
|
|
91
|
+
const remaining = matches.filter((m) => !clusteredKeys.has(m.app.name + "::" + m.canon.name));
|
|
55
92
|
return { clusters, remaining };
|
|
56
93
|
};
|
|
57
94
|
|
|
@@ -59,4 +96,5 @@ module.exports = {
|
|
|
59
96
|
findMissingComponents,
|
|
60
97
|
suggestName,
|
|
61
98
|
DEFAULT_MIN_CLUSTER,
|
|
99
|
+
MIN_PAIRWISE_STRUCTURE,
|
|
62
100
|
};
|
package/main.js
CHANGED
|
@@ -19,16 +19,18 @@ const {
|
|
|
19
19
|
const { walk, writeCatalog } = require("./utils");
|
|
20
20
|
const { parseComponents: parseComponentsViaEntrypoint, parseElements: parseElementsViaEntrypoint, parseParts: parsePartsViaEntrypoint } = require("./playgrounds/framework-entrypoint.js");
|
|
21
21
|
const { buildCandidate, buildElementCandidate } = require("./normalize/candidate.js");
|
|
22
|
+
const { tagAffinity } = require("./normalize/dom.js");
|
|
22
23
|
const { matchSignals } = require("./matcher");
|
|
23
24
|
const { getFrameworkMatcher } = require("./matcher/framework");
|
|
24
25
|
const { decide, bestOf } = require("./decision-maker");
|
|
25
26
|
const dropUsages = require("./filter/drop-usages");
|
|
26
27
|
const dropChildElement = require("./filter/drop-child-element");
|
|
28
|
+
const dropNoStructure = require("./filter/drop-no-structure");
|
|
27
29
|
const { findMissingComponents } = require("./filter/missing-component");
|
|
28
30
|
const { renderComponentReport } = require("./report");
|
|
29
31
|
const log = require("./logger");
|
|
30
32
|
|
|
31
|
-
const FILTERS = [dropUsages, dropChildElement];
|
|
33
|
+
const FILTERS = [dropUsages, dropChildElement, dropNoStructure];
|
|
32
34
|
const frameworkMatcher = getFrameworkMatcher("react");
|
|
33
35
|
|
|
34
36
|
const treeSummary = (tree) => {
|
|
@@ -57,7 +59,7 @@ const fail = (message) => {
|
|
|
57
59
|
};
|
|
58
60
|
|
|
59
61
|
const parseArgs = (argv) => {
|
|
60
|
-
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 };
|
|
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 };
|
|
61
63
|
for (let i = 0; i < argv.length; i += 1) {
|
|
62
64
|
const a = argv[i];
|
|
63
65
|
if (a === "--check") {
|
|
@@ -87,6 +89,10 @@ const parseArgs = (argv) => {
|
|
|
87
89
|
} else if (a === "--ignore-dirs") {
|
|
88
90
|
if (!argv[i + 1]) fail("--ignore-dirs requires a value (colon-separated list of directory names)");
|
|
89
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;
|
|
90
96
|
} else if (a === "--verbose") {
|
|
91
97
|
cfg.verbose = true;
|
|
92
98
|
} else if (a === "--include") {
|
|
@@ -95,15 +101,23 @@ const parseArgs = (argv) => {
|
|
|
95
101
|
} else if (a === "--log") {
|
|
96
102
|
if (!argv[i + 1]) fail("--log requires a value (path to the stage log file)");
|
|
97
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 };
|
|
98
109
|
} else if (a === "--min-cluster") {
|
|
99
110
|
if (!argv[i + 1]) fail("--min-cluster requires a value (min app components matching one canonical to flag it as a missing component)");
|
|
100
111
|
const n = Number.parseInt(argv[i + 1], 10);
|
|
101
112
|
if (!Number.isFinite(n) || n < 2) fail("--min-cluster must be an integer >= 2");
|
|
102
113
|
cfg.minCluster = n;
|
|
103
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];
|
|
104
118
|
} else if (a === "--help" || a === "-h") {
|
|
105
|
-
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] [--log file] [--min-cluster N] [--verbose]");
|
|
106
|
-
console.log("
|
|
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.");
|
|
107
121
|
process.exit(0);
|
|
108
122
|
} else {
|
|
109
123
|
fail(`unknown argument: ${a} (see --help)`);
|
|
@@ -230,6 +244,23 @@ const dedupPairs = (matches) => {
|
|
|
230
244
|
return [...best.values()];
|
|
231
245
|
};
|
|
232
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
|
+
|
|
233
264
|
const collectRawHtml = (appFiles, canonComponents, config, canonNames) => {
|
|
234
265
|
if (!config.rawHtml) return [];
|
|
235
266
|
const rows = [];
|
|
@@ -262,7 +293,7 @@ const collectRawHtml = (appFiles, canonComponents, config, canonNames) => {
|
|
|
262
293
|
};
|
|
263
294
|
|
|
264
295
|
const SIZE_RATIO = 3;
|
|
265
|
-
const MIN_PART_CANON_NODES =
|
|
296
|
+
const MIN_PART_CANON_NODES = 1;
|
|
266
297
|
const sizeCache = new WeakMap();
|
|
267
298
|
|
|
268
299
|
const nodeCount = (tree) => {
|
|
@@ -306,14 +337,23 @@ const bagDice = (a, b) => {
|
|
|
306
337
|
|
|
307
338
|
const MIN_TAG_DICE = 0.5;
|
|
308
339
|
|
|
340
|
+
const rootTagMatches = (partTag, canonTag) => tagAffinity(partTag, canonTag) > 0;
|
|
341
|
+
|
|
309
342
|
const comparableCanons = (part, canonComponents) => {
|
|
310
343
|
const partSize = sizeOf(part);
|
|
311
344
|
const partBag = bagOf(part);
|
|
312
345
|
|
|
313
346
|
return canonComponents.filter((canon) => {
|
|
314
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
|
+
}
|
|
315
355
|
|
|
316
|
-
return size
|
|
356
|
+
return size <= partSize * SIZE_RATIO && partSize <= size * SIZE_RATIO && bagDice(partBag, canonBag) >= MIN_TAG_DICE;
|
|
317
357
|
});
|
|
318
358
|
};
|
|
319
359
|
|
|
@@ -343,8 +383,9 @@ const findParts = (file, rel, canonComponents, config, canonNames) => {
|
|
|
343
383
|
if (pool.length === 0) continue;
|
|
344
384
|
const candidate = { ...buildCandidate(part, config), path: rel };
|
|
345
385
|
const { kept } = applyFilters(matchPairs([candidate], pool, config));
|
|
346
|
-
const
|
|
347
|
-
if (
|
|
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 });
|
|
348
389
|
}
|
|
349
390
|
|
|
350
391
|
return largestParts(found);
|
|
@@ -354,16 +395,16 @@ const collectParts = (appFiles, canonComponents, config, canonNames) => {
|
|
|
354
395
|
if (!config.parts) return [];
|
|
355
396
|
const rows = [];
|
|
356
397
|
for (const { file, rel } of appFiles) {
|
|
357
|
-
for (const { part, best } of findParts(file, rel, canonComponents, config, canonNames)) {
|
|
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 });
|
|
358
401
|
rows.push({
|
|
359
402
|
owner: part.owner,
|
|
360
403
|
tag: part.rootTag,
|
|
361
404
|
path: rel,
|
|
362
405
|
line: part.line,
|
|
363
406
|
endLine: part.endLine,
|
|
364
|
-
|
|
365
|
-
confidence: best.decision.confidence,
|
|
366
|
-
reason: best.decision.reason,
|
|
407
|
+
suggestions,
|
|
367
408
|
});
|
|
368
409
|
}
|
|
369
410
|
}
|
|
@@ -384,19 +425,32 @@ const stageLog = (enabled) => {
|
|
|
384
425
|
const buildComponentReport = async (args, repoRoot, roots, shouldSkip, config, registry, catalogComponents) => {
|
|
385
426
|
const exts = config.exts || DEFAULT_EXTS;
|
|
386
427
|
const ignoreDirs = config.ignoreDirs;
|
|
387
|
-
const
|
|
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);
|
|
388
441
|
|
|
389
442
|
const shouldSkipCanon = (abs) => isIgnored(relPosix(repoRoot, abs), [], config.exclude);
|
|
390
|
-
const canonFiles = collectFiles(roots, repoRoot, shouldSkipCanon, null, exts, ignoreDirs).files;
|
|
443
|
+
const canonFiles = dedupeFiles(collectFiles(roots, repoRoot, shouldSkipCanon, null, exts, ignoreDirs).files);
|
|
391
444
|
const canonComponents = parseComponents(canonFiles, null, config);
|
|
392
445
|
const canonNames = new Set(canonComponents.map((c) => c.name));
|
|
393
446
|
const uniqueCanon = parseComponents(canonFiles, canonNames, config);
|
|
394
447
|
stage(`canonical parsed (${uniqueCanon.length})`);
|
|
395
448
|
|
|
449
|
+
const appInclude = args.appRoots ? [] : config.include;
|
|
396
450
|
const appShouldSkip = (abs) => {
|
|
397
451
|
const rel = relPosix(repoRoot, abs);
|
|
398
452
|
|
|
399
|
-
return isIgnored(rel,
|
|
453
|
+
return isIgnored(rel, appInclude, config.exclude);
|
|
400
454
|
};
|
|
401
455
|
const appFiles = collectFiles(args.appRoots || ["."], repoRoot, appShouldSkip, null, exts, ignoreDirs);
|
|
402
456
|
const appComponents = parseComponents(appFiles.files, canonNames, config, "CANDIDATE");
|
|
@@ -406,7 +460,11 @@ const buildComponentReport = async (args, repoRoot, roots, shouldSkip, config, r
|
|
|
406
460
|
const { kept, dropped } = applyFilters(matches);
|
|
407
461
|
const deduped = dedupPairs(kept);
|
|
408
462
|
stage("components matched");
|
|
409
|
-
const
|
|
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 });
|
|
410
468
|
if (log.enabled()) {
|
|
411
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 });
|
|
412
470
|
}
|
|
@@ -417,7 +475,7 @@ const buildComponentReport = async (args, repoRoot, roots, shouldSkip, config, r
|
|
|
417
475
|
stage("parts done");
|
|
418
476
|
|
|
419
477
|
return {
|
|
420
|
-
matches: remaining,
|
|
478
|
+
matches: [...remaining, ...layerMatches],
|
|
421
479
|
dropped,
|
|
422
480
|
missingClusters: clusters,
|
|
423
481
|
rawHtml,
|
|
@@ -433,10 +491,12 @@ const main = async () => {
|
|
|
433
491
|
const args = parseArgs(process.argv.slice(2));
|
|
434
492
|
const cwd = process.cwd();
|
|
435
493
|
|
|
436
|
-
const loaded =
|
|
494
|
+
const loaded = await loadRuleOptions(cwd, args.configFile);
|
|
437
495
|
const configOptions = loaded ? { ...loaded.options } : {};
|
|
438
496
|
if (loaded) {
|
|
439
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");
|
|
440
500
|
}
|
|
441
501
|
if (args.ignoreDirs) {
|
|
442
502
|
const base = Array.isArray(configOptions.ignoreDirs) ? configOptions.ignoreDirs : [];
|
|
@@ -445,6 +505,9 @@ const main = async () => {
|
|
|
445
505
|
|
|
446
506
|
const config = normalizeOptions(configOptions);
|
|
447
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;
|
|
448
511
|
if (args.log) log.enable(args.log);
|
|
449
512
|
await require("./playgrounds/style-entrypoint.js").whenReady(config);
|
|
450
513
|
const rootMarkers = config.rootMarkers;
|
|
@@ -455,6 +518,9 @@ const main = async () => {
|
|
|
455
518
|
if (!roots || roots.length === 0) {
|
|
456
519
|
fail("no scan roots: pass --roots <dir1>:<dir2> or set componentsFolder in the rule options (eslint.config.js / component-uniqueness.config.js)");
|
|
457
520
|
}
|
|
521
|
+
if (!config.componentsFolder || config.componentsFolder.length === 0) {
|
|
522
|
+
config.componentsFolder = roots;
|
|
523
|
+
}
|
|
458
524
|
|
|
459
525
|
const outPath = resolveAgainst(args.out || config.catalogPath, repoRoot);
|
|
460
526
|
const registry = args.registry ? loadRegistry({ ...config, registry: null, registryPath: args.registry }) : loadRegistry(config);
|
|
@@ -513,4 +579,6 @@ module.exports = {
|
|
|
513
579
|
collectRawHtml,
|
|
514
580
|
findParts,
|
|
515
581
|
frameworkMatcher,
|
|
582
|
+
comparableCanons,
|
|
583
|
+
buildComponentReport,
|
|
516
584
|
};
|
package/matcher/index.js
CHANGED
|
@@ -7,17 +7,17 @@ const { matchA11y, tokensOf } = require("./a11y");
|
|
|
7
7
|
|
|
8
8
|
const restrictTo = (candidate, canon) => {
|
|
9
9
|
const events = new Set(candidate.framework.events);
|
|
10
|
-
const props = new Set(candidate.framework.props);
|
|
11
10
|
const a11y = tokensOf(candidate, "a11y");
|
|
11
|
+
const openProps = canon.framework.open ? [] : canon.framework.props || [];
|
|
12
12
|
|
|
13
13
|
return {
|
|
14
14
|
...canon,
|
|
15
15
|
framework: {
|
|
16
|
-
props:
|
|
16
|
+
props: openProps,
|
|
17
17
|
events: (canon.framework.events || []).filter((e) => events.has(e)),
|
|
18
18
|
names: [],
|
|
19
19
|
},
|
|
20
|
-
a11y:
|
|
20
|
+
a11y: (canon.a11y || []).filter((t) => a11y.has(String(t).toLowerCase())),
|
|
21
21
|
};
|
|
22
22
|
};
|
|
23
23
|
|