@md-code/component-uniqueness 0.1.1 → 0.1.3
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 +29 -4
- package/decision-maker.js +4 -8
- package/filter/drop-usages.js +4 -1
- package/filter/missing-component.js +68 -0
- package/logger.js +44 -0
- package/main.js +115 -9
- package/matcher/behavior.js +6 -2
- package/matcher/name.js +19 -2
- package/matcher/structure.js +55 -15
- package/normalize/candidate.js +4 -2
- package/normalize/dom.js +2 -0
- package/normalize/framework.js +37 -8
- package/normalize/style-resolver.js +24 -0
- package/normalize/style.js +1 -0
- package/normalize/words.js +40 -0
- package/package.json +2 -1
- package/playgrounds/framework-entrypoint.js +22 -3
- package/playgrounds/react.js +77 -8
- package/playgrounds/styled-components.js +132 -1
- package/report.js +159 -126
package/report.js
CHANGED
|
@@ -1,126 +1,159 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const TIERS = ["duplicate", "similar"];
|
|
4
|
-
|
|
5
|
-
const TIER_LABEL = {
|
|
6
|
-
duplicate: "Duplicate (confidence ≥ threshold)",
|
|
7
|
-
similar: "Similar (below duplicate threshold)",
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
const duplicateText = (match) => {
|
|
11
|
-
const d = match.decision;
|
|
12
|
-
return `Component \`${match.app.name}\` duplicates canonical \`${match.canon.name}\` (${match.canon.path}) — ${d.reason}. Use the canonical component instead.`;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const rawHtmlText = (tag, suggestion) => {
|
|
16
|
-
return `Raw <${tag}> outside the canonical packages → replace with \`${suggestion.component}\` from ${suggestion.path}.`;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const renderHeader = (comp) => {
|
|
20
|
-
const lines = [];
|
|
21
|
-
lines.push("# Component duplicates (component-level funnel)");
|
|
22
|
-
lines.push("");
|
|
23
|
-
lines.push(`Scanned ${comp.files} app file(s), ${comp.appCount} app component(s) against ${comp.canonCount} canonical component(s).`);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
lines
|
|
34
|
-
lines.push(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
lines
|
|
49
|
-
lines.push(
|
|
50
|
-
|
|
51
|
-
for
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
for (const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
lines.push(
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const TIERS = ["duplicate", "similar"];
|
|
4
|
+
|
|
5
|
+
const TIER_LABEL = {
|
|
6
|
+
duplicate: "Duplicate (confidence ≥ threshold)",
|
|
7
|
+
similar: "Similar (below duplicate threshold)",
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const duplicateText = (match) => {
|
|
11
|
+
const d = match.decision;
|
|
12
|
+
return `Component \`${match.app.name}\` duplicates canonical \`${match.canon.name}\` (${match.canon.path}) — ${d.reason}. Use the canonical component instead.`;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const rawHtmlText = (tag, suggestion) => {
|
|
16
|
+
return `Raw <${tag}> outside the canonical packages → replace with \`${suggestion.component}\` from ${suggestion.path}.`;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const renderHeader = (comp) => {
|
|
20
|
+
const lines = [];
|
|
21
|
+
lines.push("# Component duplicates (component-level funnel)");
|
|
22
|
+
lines.push("");
|
|
23
|
+
lines.push(`Scanned ${comp.files} app file(s), ${comp.appCount} app component(s) against ${comp.canonCount} canonical component(s).`);
|
|
24
|
+
const dupCount = comp.matches.filter((m) => m.decision.tier === "duplicate").length;
|
|
25
|
+
const simCount = comp.matches.filter((m) => m.decision.tier === "similar").length;
|
|
26
|
+
const missingCount = (comp.missingClusters || []).reduce((n, c) => n + c.matches.length, 0);
|
|
27
|
+
lines.push(`Found ${dupCount} duplicate component(s) and ${simCount} similar component(s)${comp.dropped && comp.dropped.length > 0 ? ` (after filtering out ${comp.dropped.length} match(es))` : ""}${missingCount > 0 ? `, plus ${missingCount} component(s) grouped under ${(comp.missingClusters || []).length} potentially missing component(s)` : ""}.`);
|
|
28
|
+
lines.push("");
|
|
29
|
+
return lines;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const renderTierSection = (tier, rows, title = TIER_LABEL[tier]) => {
|
|
33
|
+
const lines = [];
|
|
34
|
+
lines.push(`## ${title} (${rows.length})`);
|
|
35
|
+
lines.push("");
|
|
36
|
+
lines.push("| Local component | Location | Duplicates canonical | Canonical location | Confidence | Why |");
|
|
37
|
+
lines.push("| --- | --- | --- | --- | --- | --- |");
|
|
38
|
+
const sorted = [...rows].sort((a, b) => b.decision.confidence - a.decision.confidence || a.canon.name.localeCompare(b.canon.name));
|
|
39
|
+
for (const m of sorted) {
|
|
40
|
+
lines.push(`| ${m.app.name} | \`${m.app.path}:${m.app.line}\` | ${m.canon.name} | \`${m.canon.path}\` | ${m.decision.confidence.toFixed(2)} | ${m.decision.reason} |`);
|
|
41
|
+
}
|
|
42
|
+
lines.push("");
|
|
43
|
+
return lines;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const renderMissingSection = (clusters) => {
|
|
47
|
+
if (!clusters || clusters.length === 0) return [];
|
|
48
|
+
const lines = [];
|
|
49
|
+
lines.push(`## Potentially missing component (${clusters.length})`);
|
|
50
|
+
lines.push("");
|
|
51
|
+
lines.push("Several distinct components all matched the same canonical. This usually means the codebase is missing a shared component that would unify them — not that any single one is a duplicate. Consider extracting a new canonical component for each group below.");
|
|
52
|
+
lines.push("");
|
|
53
|
+
for (const cluster of clusters) {
|
|
54
|
+
lines.push(`### ${cluster.name}`);
|
|
55
|
+
lines.push("");
|
|
56
|
+
lines.push(`All ${cluster.matches.length} components below matched canonical \`${cluster.canon.name}\` (\`${cluster.canon.path}\`). They are structurally similar to each other but distinct enough that none is a clean duplicate — a shared \`${cluster.name}\` would likely cover them.`);
|
|
57
|
+
lines.push("");
|
|
58
|
+
lines.push("| Local component | Location | Matched canonical | Confidence | Why |");
|
|
59
|
+
lines.push("| --- | --- | --- | --- | --- |");
|
|
60
|
+
for (const m of cluster.matches) {
|
|
61
|
+
lines.push(`| ${m.app.name} | \`${m.app.path}:${m.app.line}\` | ${m.canon.name} | ${m.decision.confidence.toFixed(2)} | ${m.decision.reason} |`);
|
|
62
|
+
}
|
|
63
|
+
lines.push("");
|
|
64
|
+
}
|
|
65
|
+
return lines;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const renderRawHtmlSection = (rows) => {
|
|
69
|
+
if (rows.length === 0) return [];
|
|
70
|
+
const lines = [];
|
|
71
|
+
lines.push(`## Raw HTML outside canonical packages (${rows.length})`);
|
|
72
|
+
lines.push("");
|
|
73
|
+
lines.push("| Element | Location | Replace with | From | Why |");
|
|
74
|
+
lines.push("| --- | --- | --- | --- | --- |");
|
|
75
|
+
const sorted = [...rows].sort((a, b) => a.path.localeCompare(b.path));
|
|
76
|
+
for (const r of sorted) {
|
|
77
|
+
lines.push(`| <${r.tag}> | \`${r.path}:${r.line}\` | \`${r.suggestion.component}\` | ${r.suggestion.path} | ${r.reason} |`);
|
|
78
|
+
}
|
|
79
|
+
lines.push("");
|
|
80
|
+
return lines;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const renderPartsSection = (rows, title = "Parts of components that duplicate a canonical") => {
|
|
84
|
+
if (rows.length === 0) return [];
|
|
85
|
+
const lines = [];
|
|
86
|
+
lines.push(`## ${title} (${rows.length})`);
|
|
87
|
+
lines.push("");
|
|
88
|
+
lines.push("| Part | Location | Replace with | From | Why |");
|
|
89
|
+
lines.push("| --- | --- | --- | --- | --- |");
|
|
90
|
+
const sorted = [...rows].sort((a, b) => a.path.localeCompare(b.path) || a.line - b.line);
|
|
91
|
+
for (const r of sorted) {
|
|
92
|
+
lines.push(`| ${r.owner} \u203a <${r.tag}> | \`${r.path}:${r.line}-${r.endLine}\` | \`${r.suggestion.component}\` | ${r.suggestion.path} | ${r.reason} |`);
|
|
93
|
+
}
|
|
94
|
+
lines.push("");
|
|
95
|
+
return lines;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const renderFilteredSection = (dropped) => {
|
|
99
|
+
const lines = [];
|
|
100
|
+
const byFilter = new Map();
|
|
101
|
+
for (const d of dropped) {
|
|
102
|
+
if (!byFilter.has(d.filter)) byFilter.set(d.filter, []);
|
|
103
|
+
byFilter.get(d.filter).push(d);
|
|
104
|
+
}
|
|
105
|
+
for (const [filter, rows] of byFilter) {
|
|
106
|
+
lines.push(`## Filtered out by \`${filter}\` (${rows.length})`);
|
|
107
|
+
lines.push("");
|
|
108
|
+
lines.push("| Local component | Location | Canonical | Why dropped |");
|
|
109
|
+
lines.push("| --- | --- | --- | --- |");
|
|
110
|
+
const sorted = [...rows].sort((a, b) => a.match.canon.name.localeCompare(b.match.canon.name));
|
|
111
|
+
for (const d of sorted) {
|
|
112
|
+
lines.push(`| ${d.match.app.name} | \`${d.match.app.path}:${d.match.app.line}\` | ${d.match.canon.name} | ${d.reason} |`);
|
|
113
|
+
}
|
|
114
|
+
lines.push("");
|
|
115
|
+
}
|
|
116
|
+
return lines;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const renderComponentReport = (comp, verbose) => {
|
|
120
|
+
const lines = [];
|
|
121
|
+
const folders = comp.layerFolders || [];
|
|
122
|
+
const inLayer = (p) => folders.some((d) => p.startsWith(d));
|
|
123
|
+
const appMatches = comp.matches.filter((m) => !inLayer(m.app.path));
|
|
124
|
+
const layerMatches = comp.matches.filter((m) => inLayer(m.app.path));
|
|
125
|
+
const appParts = (comp.parts || []).filter((r) => !inLayer(r.path));
|
|
126
|
+
const layerParts = (comp.parts || []).filter((r) => inLayer(r.path));
|
|
127
|
+
lines.push(...renderHeader(comp));
|
|
128
|
+
for (const tier of TIERS) {
|
|
129
|
+
const rows = appMatches.filter((m) => m.decision.tier === tier);
|
|
130
|
+
if (rows.length === 0) continue;
|
|
131
|
+
lines.push(...renderTierSection(tier, rows));
|
|
132
|
+
}
|
|
133
|
+
const missingClusters = (comp.missingClusters || []).filter((c) => c.matches.every((m) => !inLayer(m.app.path)));
|
|
134
|
+
lines.push(...renderMissingSection(missingClusters));
|
|
135
|
+
if (comp.rawHtml && comp.rawHtml.length > 0) lines.push(...renderRawHtmlSection(comp.rawHtml));
|
|
136
|
+
lines.push(...renderPartsSection(appParts));
|
|
137
|
+
const layerIdx = (p) => folders.findIndex((d) => p.startsWith(d));
|
|
138
|
+
const layerName = (d) => d.split("/").filter(Boolean).pop();
|
|
139
|
+
for (let li = 0; li < folders.length; li += 1) {
|
|
140
|
+
const seen = folders.slice(0, li).map(layerName);
|
|
141
|
+
const suffix = seen.length > 0 ? ` (vs ${seen.join(" + ")})` : "";
|
|
142
|
+
for (const tier of TIERS) {
|
|
143
|
+
const rows = layerMatches.filter((m) => layerIdx(m.app.path) === li && m.decision.tier === tier);
|
|
144
|
+
if (rows.length === 0) continue;
|
|
145
|
+
lines.push(...renderTierSection(tier, rows, `Inside ${layerName(folders[li])}: ${TIER_LABEL[tier]}${suffix}`));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
lines.push(...renderPartsSection(layerParts, "Inside canonical layers: parts that duplicate a lower layer"));
|
|
149
|
+
if (verbose && comp.dropped && comp.dropped.length > 0) lines.push(...renderFilteredSection(comp.dropped));
|
|
150
|
+
return lines.join("\n");
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
module.exports = {
|
|
154
|
+
renderComponentReport,
|
|
155
|
+
duplicateText,
|
|
156
|
+
rawHtmlText,
|
|
157
|
+
TIERS,
|
|
158
|
+
TIER_LABEL,
|
|
159
|
+
};
|