@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/matcher/structure.js
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const { isPascalCase, tagAffinity, GENERIC_CONTAINER_TAGS } = require("../normalize/dom.js");
|
|
4
4
|
|
|
5
5
|
const CONFLICT_PENALTY = 0.6;
|
|
6
|
-
const CONFLICTED_RICH_CEILING = 0.8;
|
|
7
|
-
const RICH_DISTINCT_MIN = 3;
|
|
8
|
-
const CEILING_LADDER = [0.3, 0.5, 0.7, 0.9, 1.0];
|
|
9
|
-
const MISSING_CHILD_PENALTY = 0.15;
|
|
10
6
|
const NEAR_IDENTICAL_CEILING = 0.98;
|
|
11
7
|
|
|
12
8
|
const isTextNode = (node) => node.tag === "children" || node.tag === "text";
|
|
13
9
|
const isIconNode = (node) => isPascalCase(node.tag);
|
|
14
10
|
|
|
11
|
+
const nodeCount = (tree) => {
|
|
12
|
+
if (!tree) return 0;
|
|
13
|
+
const own = tree.tag === "fragment" || tree.tag === "children" ? 0 : 1;
|
|
14
|
+
|
|
15
|
+
return own + (tree.children || []).reduce((sum, child) => sum + nodeCount(child), 0);
|
|
16
|
+
};
|
|
17
|
+
|
|
15
18
|
const pairsOf = (css) => new Set(Object.entries(css || {}).map(([prop, value]) => `${prop}:${value}`));
|
|
16
19
|
|
|
17
20
|
const overlap = (a, b) => [...a].filter((pair) => b.has(pair)).length;
|
|
@@ -22,8 +25,6 @@ const jaccard = (a, b) => {
|
|
|
22
25
|
return union === 0 ? 0 : overlap(a, b) / union;
|
|
23
26
|
};
|
|
24
27
|
|
|
25
|
-
const coverage = (a, b) => (a.size === 0 ? 0 : overlap(a, b) / a.size);
|
|
26
|
-
|
|
27
28
|
const textSim = (a, b) => {
|
|
28
29
|
const ta = String(a.text || "").toLowerCase().trim();
|
|
29
30
|
const tb = String(b.text || "").toLowerCase().trim();
|
|
@@ -34,8 +35,9 @@ const textSim = (a, b) => {
|
|
|
34
35
|
};
|
|
35
36
|
|
|
36
37
|
const tagMatch = (a, b) => {
|
|
37
|
-
if (a.tag === b.tag) return 1;
|
|
38
38
|
if (a.interactive && b.interactive) return 1;
|
|
39
|
+
const affinity = tagAffinity(a.tag, b.tag);
|
|
40
|
+
if (affinity > 0) return affinity;
|
|
39
41
|
|
|
40
42
|
return 0;
|
|
41
43
|
};
|
|
@@ -54,47 +56,62 @@ const nodeSimilarity = (a, b) => {
|
|
|
54
56
|
return score;
|
|
55
57
|
};
|
|
56
58
|
|
|
57
|
-
const
|
|
59
|
+
const chainList = (node) => {
|
|
60
|
+
const list = [];
|
|
61
|
+
const walk = (n, parentIdx) => {
|
|
62
|
+
if (!n) return;
|
|
63
|
+
if (n.tag === "fragment" || n.tag === "children") {
|
|
64
|
+
for (const child of n.children || []) walk(child, parentIdx);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (n.tag === "text") return;
|
|
68
|
+
const idx = list.length;
|
|
69
|
+
list.push({ node: n, parent: parentIdx });
|
|
70
|
+
for (const child of n.children || []) walk(child, idx);
|
|
71
|
+
};
|
|
72
|
+
walk(node, -1);
|
|
73
|
+
|
|
74
|
+
return list;
|
|
75
|
+
};
|
|
58
76
|
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
77
|
+
const bestChain = (a, b) => {
|
|
78
|
+
const xs = chainList(a);
|
|
79
|
+
const ys = chainList(b);
|
|
80
|
+
const refSize = Math.max(xs.length, ys.length);
|
|
81
|
+
if (xs.length === 0 || ys.length === 0) return { score: 0, quality: 0, chainSize: 0, refSize, pairs: [] };
|
|
82
|
+
const dp = xs.map(() => new Array(ys.length).fill(0));
|
|
83
|
+
const len = xs.map(() => new Array(ys.length).fill(0));
|
|
84
|
+
let best = { score: 0, quality: 0, chainSize: 0, i: -1, j: -1 };
|
|
85
|
+
for (let i = 0; i < xs.length; i += 1) {
|
|
86
|
+
for (let j = 0; j < ys.length; j += 1) {
|
|
87
|
+
const sim = nodeSimilarity(xs[i].node, ys[j].node);
|
|
88
|
+
let value = sim;
|
|
89
|
+
let length = 1;
|
|
90
|
+
const pi = xs[i].parent;
|
|
91
|
+
const pj = ys[j].parent;
|
|
92
|
+
if (pi >= 0 && pj >= 0 && dp[pi][pj] > 0) {
|
|
93
|
+
value += dp[pi][pj];
|
|
94
|
+
length += len[pi][pj];
|
|
95
|
+
}
|
|
96
|
+
dp[i][j] = value;
|
|
97
|
+
len[i][j] = length;
|
|
98
|
+
const quality = value / length;
|
|
99
|
+
const score = quality * (length / refSize);
|
|
100
|
+
if (score > best.score) best = { score, quality, chainSize: length, i, j };
|
|
65
101
|
}
|
|
66
102
|
}
|
|
67
103
|
const pairs = [];
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
i -= 1;
|
|
76
|
-
j -= 1;
|
|
104
|
+
if (best.i >= 0) {
|
|
105
|
+
let i = best.i;
|
|
106
|
+
let j = best.j;
|
|
107
|
+
while (i >= 0 && j >= 0) {
|
|
108
|
+
pairs.unshift([xs[i].node, ys[j].node]);
|
|
109
|
+
i = xs[i].parent;
|
|
110
|
+
j = ys[j].parent;
|
|
77
111
|
}
|
|
78
112
|
}
|
|
79
113
|
|
|
80
|
-
return {
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const compare = (a, b) => {
|
|
84
|
-
const kidsA = (a.children || []).flatMap(forestOf);
|
|
85
|
-
const kidsB = (b.children || []).flatMap(forestOf);
|
|
86
|
-
const { total, pairs } = align(kidsA, kidsB);
|
|
87
|
-
|
|
88
|
-
return { score: (nodeSimilarity(a, b) + total) / (1 + Math.max(kidsA.length, kidsB.length)), pairs: [[a, b], ...pairs] };
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const compareForests = (a, b) => {
|
|
92
|
-
const xs = forestOf(a);
|
|
93
|
-
const ys = forestOf(b);
|
|
94
|
-
const longest = Math.max(xs.length, ys.length);
|
|
95
|
-
const { total, pairs } = align(xs, ys);
|
|
96
|
-
|
|
97
|
-
return { score: longest === 0 ? 0 : total / longest, pairs };
|
|
114
|
+
return { score: best.score, quality: best.quality, chainSize: best.chainSize, refSize, pairs };
|
|
98
115
|
};
|
|
99
116
|
|
|
100
117
|
const cssStats = (pairs) => {
|
|
@@ -117,88 +134,105 @@ const cssStats = (pairs) => {
|
|
|
117
134
|
return { conflicts, ratio: comparable === 0 ? 0 : conflicts / comparable, agreement: union === 0 ? 0 : shared / union };
|
|
118
135
|
};
|
|
119
136
|
|
|
120
|
-
const rootNodeScore = (cand, target) => {
|
|
121
|
-
const parts = [];
|
|
122
|
-
if (!NON_SEMANTIC_TAGS.has(cand.tag)) parts.push({ w: 0.6, v: tagMatch(cand, target) });
|
|
123
|
-
const candPairs = pairsOf(cand.css);
|
|
124
|
-
if (candPairs.size > 0) parts.push({ w: 0.4, v: coverage(candPairs, pairsOf(target.css)) });
|
|
125
|
-
const totalW = parts.reduce((sum, part) => sum + part.w, 0);
|
|
126
|
-
|
|
127
|
-
return totalW === 0 ? 0 : parts.reduce((sum, part) => sum + part.w * part.v, 0) / totalW;
|
|
128
|
-
};
|
|
129
|
-
|
|
130
137
|
const isInformative = (node) => !!node && (node.tag !== "fragment" || (node.children || []).some(isInformative));
|
|
131
138
|
|
|
132
|
-
const
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
nodes += 1;
|
|
139
|
-
}
|
|
140
|
-
for (const child of n.children || []) visit(child);
|
|
141
|
-
};
|
|
142
|
-
visit(node);
|
|
139
|
+
const classTokensDiffer = (a, b) => {
|
|
140
|
+
const A = new Set(a || []);
|
|
141
|
+
const B = new Set(b || []);
|
|
142
|
+
if (A.size === 0 && B.size === 0) return false;
|
|
143
|
+
for (const token of A) if (!B.has(token)) return true;
|
|
144
|
+
for (const token of B) if (!A.has(token)) return true;
|
|
143
145
|
|
|
144
|
-
return
|
|
146
|
+
return false;
|
|
145
147
|
};
|
|
146
148
|
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
for (const child of tree.children || []) componentNamesOf(child, out);
|
|
151
|
-
|
|
152
|
-
return out;
|
|
149
|
+
const treeSimilarity = (a, b) => {
|
|
150
|
+
const result = bestChain(a, b);
|
|
151
|
+
return { score: result.score, quality: result.quality, detail: { similarity: result.score, quality: result.quality, chainSize: result.chainSize, refSize: result.refSize, pairs: result.pairs.length } };
|
|
153
152
|
};
|
|
154
153
|
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
154
|
+
const firstReal = (node) => {
|
|
155
|
+
if (!node) return null;
|
|
156
|
+
if (node.tag === "fragment" || node.tag === "children" || node.tag === "text") {
|
|
157
|
+
for (const child of node.children || []) {
|
|
158
|
+
const found = firstReal(child);
|
|
159
|
+
if (found) return found;
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
return node;
|
|
160
164
|
};
|
|
161
165
|
|
|
162
|
-
const
|
|
163
|
-
const
|
|
164
|
-
|
|
166
|
+
const rootSimilarity = (a, b) => {
|
|
167
|
+
const ra = firstReal(a);
|
|
168
|
+
const rb = firstReal(b);
|
|
169
|
+
if (!ra || !rb) return { score: 0, detail: { rootSim: 0 } };
|
|
165
170
|
|
|
166
|
-
return
|
|
171
|
+
return { score: nodeSimilarity(ra, rb), detail: { rootSim: nodeSimilarity(ra, rb) } };
|
|
167
172
|
};
|
|
168
173
|
|
|
169
|
-
const
|
|
170
|
-
const A = new Set(a || []);
|
|
171
|
-
const B = new Set(b || []);
|
|
172
|
-
if (A.size === 0 && B.size === 0) return false;
|
|
173
|
-
for (const token of A) if (!B.has(token)) return true;
|
|
174
|
-
for (const token of B) if (!A.has(token)) return true;
|
|
174
|
+
const MIN_CROSS_TAG_CLASS_OVERLAP = 3;
|
|
175
175
|
|
|
176
|
-
|
|
176
|
+
const rootClassOverlap = (candidate, canon) => {
|
|
177
|
+
const rt = firstReal(candidate.tree);
|
|
178
|
+
if (!rt) return 0;
|
|
179
|
+
const partToks = new Set(String(rt.className || "").split(/\s+/).filter(Boolean));
|
|
180
|
+
const canonToks = new Set(canon.rawClasses || []);
|
|
181
|
+
let n = 0;
|
|
182
|
+
for (const t of partToks) if (canonToks.has(t)) n += 1;
|
|
183
|
+
|
|
184
|
+
return n;
|
|
177
185
|
};
|
|
178
186
|
|
|
179
187
|
const matchStructure = (candidate, canon) => {
|
|
180
188
|
const cand = candidate.dom && candidate.dom.tree;
|
|
181
189
|
const target = canon.dom && canon.dom.tree;
|
|
182
|
-
const mode = candidate.partial ? "
|
|
190
|
+
const mode = candidate.partial ? "partial" : "tree";
|
|
183
191
|
const applicable = isInformative(cand) || isInformative(target);
|
|
184
192
|
let score = 0;
|
|
185
193
|
let detail = null;
|
|
186
194
|
if (applicable && cand && target) {
|
|
187
195
|
if (candidate.partial) {
|
|
196
|
+
const singleton = nodeCount(target) === 1;
|
|
197
|
+
const partNodes = nodeCount(cand);
|
|
188
198
|
const classesDiffer = classTokensDiffer(candidate.rawClasses, canon.rawClasses);
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
199
|
+
if (singleton) {
|
|
200
|
+
const ra = firstReal(cand);
|
|
201
|
+
const rb = firstReal(target);
|
|
202
|
+
const partTag = ra ? ra.tag : "";
|
|
203
|
+
const canonTag = rb ? rb.tag : "";
|
|
204
|
+
const crossTag = GENERIC_CONTAINER_TAGS.has(partTag) && GENERIC_CONTAINER_TAGS.has(canonTag) && partTag !== canonTag;
|
|
205
|
+
if (crossTag) {
|
|
206
|
+
const overlap = rootClassOverlap(candidate, canon);
|
|
207
|
+
if (overlap < MIN_CROSS_TAG_CLASS_OVERLAP) {
|
|
208
|
+
score = 0;
|
|
209
|
+
detail = { singleton, crossTag, overlap, rejected: true };
|
|
210
|
+
} else {
|
|
211
|
+
score = Math.min(0.9, 0.5 + 0.05 * overlap);
|
|
212
|
+
detail = { singleton, crossTag, overlap };
|
|
213
|
+
}
|
|
214
|
+
} else if (partNodes > 4) {
|
|
215
|
+
score = 0;
|
|
216
|
+
detail = { singleton, partNodes, leafVsWrapper: true };
|
|
217
|
+
} else {
|
|
218
|
+
const { score: q, detail: d } = rootSimilarity(cand, target);
|
|
219
|
+
score = Math.min(q, classesDiffer ? NEAR_IDENTICAL_CEILING : 1);
|
|
220
|
+
detail = { ...d, singleton, classesDiffer };
|
|
221
|
+
}
|
|
222
|
+
} else {
|
|
223
|
+
const { quality, detail: d } = treeSimilarity(cand, target);
|
|
224
|
+
const sizeFactor = Math.min(1, nodeCount(target) / nodeCount(cand));
|
|
225
|
+
const base = quality * sizeFactor;
|
|
226
|
+
score = Math.min(base, classesDiffer ? NEAR_IDENTICAL_CEILING : 1);
|
|
227
|
+
detail = { ...d, singleton, sizeFactor, classesDiffer };
|
|
228
|
+
}
|
|
192
229
|
} else {
|
|
193
|
-
const result =
|
|
230
|
+
const result = bestChain(cand, target);
|
|
194
231
|
const stats = cssStats(result.pairs);
|
|
195
|
-
const a = sizeOf(candidate);
|
|
196
|
-
const b = sizeOf(canon);
|
|
197
232
|
const classesDiffer = classTokensDiffer(candidate.rawClasses, canon.rawClasses);
|
|
198
|
-
const ceiling =
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
detail = { similarity: result.score, ceiling, classesDiffer, conflicts: stats.conflicts, conflictRatio: stats.ratio, agreement: stats.agreement, missingRatio: Math.round(missingRatio * 100) / 100, nodes: Math.min(a.nodes, b.nodes), distinct: Math.min(a.distinct, b.distinct) };
|
|
233
|
+
const ceiling = classesDiffer ? NEAR_IDENTICAL_CEILING : 1;
|
|
234
|
+
score = Math.max(0, Math.min(result.score, ceiling) - CONFLICT_PENALTY * stats.ratio);
|
|
235
|
+
detail = { similarity: result.score, quality: result.quality, chainSize: result.chainSize, refSize: result.refSize, ceiling, classesDiffer, conflicts: stats.conflicts, conflictRatio: stats.ratio, agreement: stats.agreement };
|
|
202
236
|
}
|
|
203
237
|
}
|
|
204
238
|
|
package/normalize/dom.js
CHANGED
|
@@ -126,6 +126,23 @@ const HTML_TAGS = new Set([
|
|
|
126
126
|
|
|
127
127
|
const INTERACTIVE_TAGS = new Set(["a", "area", "audio", "button", "details", "embed", "iframe", "input", "label", "optgroup", "option", "select", "summary", "textarea", "video"]);
|
|
128
128
|
const NON_SEMANTIC_TAGS = new Set(["div", "span"]);
|
|
129
|
+
const SEMANTIC_CONTAINER_TAGS = new Set(["section", "header", "footer", "article", "aside", "nav"]);
|
|
130
|
+
const GENERIC_CONTAINER_TAGS = new Set([...NON_SEMANTIC_TAGS, ...SEMANTIC_CONTAINER_TAGS]);
|
|
131
|
+
const TAG_SYNONYMS = new Map([
|
|
132
|
+
["b", "strong"],
|
|
133
|
+
["strong", "b"],
|
|
134
|
+
["i", "em"],
|
|
135
|
+
["em", "i"],
|
|
136
|
+
]);
|
|
137
|
+
|
|
138
|
+
const tagAffinity = (a, b) => {
|
|
139
|
+
const ta = String(a || "").toLowerCase();
|
|
140
|
+
const tb = String(b || "").toLowerCase();
|
|
141
|
+
if (ta === tb) return 1;
|
|
142
|
+
if (TAG_SYNONYMS.get(ta) === tb) return 1;
|
|
143
|
+
if (GENERIC_CONTAINER_TAGS.has(ta) && GENERIC_CONTAINER_TAGS.has(tb)) return 0.75;
|
|
144
|
+
return 0;
|
|
145
|
+
};
|
|
129
146
|
|
|
130
147
|
const isPascalCase = (name) => /^[A-Z][A-Za-z0-9]*$/.test(name);
|
|
131
148
|
|
|
@@ -160,6 +177,9 @@ module.exports = {
|
|
|
160
177
|
HTML_TAGS,
|
|
161
178
|
INTERACTIVE_TAGS,
|
|
162
179
|
NON_SEMANTIC_TAGS,
|
|
180
|
+
SEMANTIC_CONTAINER_TAGS,
|
|
181
|
+
GENERIC_CONTAINER_TAGS,
|
|
182
|
+
tagAffinity,
|
|
163
183
|
isPascalCase,
|
|
164
184
|
isHtmlTag,
|
|
165
185
|
styleObjectToCssText,
|
package/normalize/framework.js
CHANGED
|
@@ -84,31 +84,12 @@ const smallSetDiscount = (arrA, arrB) => {
|
|
|
84
84
|
return union === 0 ? 1 : Math.min(1, union / 2);
|
|
85
85
|
};
|
|
86
86
|
|
|
87
|
-
const setEqual = (a, b) => {
|
|
88
|
-
const A = new Set(a || []);
|
|
89
|
-
const B = new Set(b || []);
|
|
90
|
-
if (A.size !== B.size) return false;
|
|
91
|
-
for (const item of A) if (!B.has(item)) return false;
|
|
92
|
-
|
|
93
|
-
return true;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const SAME_NAMES_PROP_FLOOR = 0.95;
|
|
97
|
-
const SAME_NAMES_PROP_SPAN = 0.04;
|
|
98
|
-
|
|
99
87
|
const frameworkSimilarity = (a, b, label) => {
|
|
100
88
|
const A = a || { props: [], events: [], names: [] };
|
|
101
89
|
const B = b || { props: [], events: [], names: [] };
|
|
102
90
|
const props = jaccard(A.props, B.props) * smallSetDiscount(A.props, B.props);
|
|
103
91
|
const events = jaccard(A.events, B.events) * smallSetDiscount(A.events, B.events);
|
|
104
92
|
const names = jaccard(A.names, B.names);
|
|
105
|
-
const passed = (p) => [...new Set([...p.props, ...p.events])];
|
|
106
|
-
const passedA = passed(A);
|
|
107
|
-
const passedB = passed(B);
|
|
108
|
-
if (A.names.length > 0 && names >= 1) {
|
|
109
|
-
if (setEqual(passedA, passedB)) return { score: 1, props, events, names };
|
|
110
|
-
return { score: SAME_NAMES_PROP_FLOOR + SAME_NAMES_PROP_SPAN * jaccard(passedA, passedB), props, events, names };
|
|
111
|
-
}
|
|
112
93
|
const parts = [];
|
|
113
94
|
if (A.props.length || B.props.length) parts.push({ w: 0.5, v: props });
|
|
114
95
|
if (A.events.length || B.events.length) parts.push({ w: 0.2, v: events });
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { styledDeclarations, available: styledAvailable } = require("../playgrounds/styled-components.js");
|
|
4
|
+
const { cssTextToStyles } = require("./style.js");
|
|
5
|
+
|
|
6
|
+
const declarationsCache = new WeakMap();
|
|
7
|
+
|
|
8
|
+
const declarationsOf = (sf) => {
|
|
9
|
+
if (!declarationsCache.has(sf)) declarationsCache.set(sf, styledDeclarations(sf));
|
|
10
|
+
return declarationsCache.get(sf);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const buildStyledResolver = () => {
|
|
14
|
+
if (!styledAvailable()) return null;
|
|
15
|
+
return (tag, sf) => {
|
|
16
|
+
if (!sf || !tag) return null;
|
|
17
|
+
const info = declarationsOf(sf).get(tag);
|
|
18
|
+
if (!info) return null;
|
|
19
|
+
const { css, unresolved } = cssTextToStyles(info.cssText);
|
|
20
|
+
return { baseTag: info.baseTag, componentRef: info.componentRef, styleObj: css, unresolved: [...unresolved, ...info.unresolved] };
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
module.exports = { buildStyledResolver };
|
package/normalize/style.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@md-code/component-uniqueness",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "ESLint rule and CLI that find duplicate components, raw HTML that should be a canonical component, and copied parts of components, by comparing structure, CSS, behavior, accessibility and framework profile.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -51,11 +51,22 @@
|
|
|
51
51
|
"typescript": ">=5.5.0 <7"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
|
-
"gen:catalog": "node main.js"
|
|
54
|
+
"gen:catalog": "node main.js",
|
|
55
|
+
"test": "vitest run"
|
|
55
56
|
},
|
|
56
57
|
"peerDependencies": {
|
|
57
58
|
"eslint": ">=8.40.0"
|
|
58
59
|
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@mui/icons-material": "^7.0.0",
|
|
62
|
+
"@mui/material": "^7.0.0",
|
|
63
|
+
"@emotion/react": "^11.0.0",
|
|
64
|
+
"@emotion/styled": "^11.0.0",
|
|
65
|
+
"react": "^19.0.0",
|
|
66
|
+
"react-dom": "^19.0.0",
|
|
67
|
+
"styled-components": "^6.1.0",
|
|
68
|
+
"vitest": "^4.1.0"
|
|
69
|
+
},
|
|
59
70
|
"publishConfig": {
|
|
60
71
|
"access": "public"
|
|
61
72
|
}
|
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
const { normalizeOptions } = require("../config.js");
|
|
4
4
|
const { buildStylesheet } = require("./style-entrypoint.js");
|
|
5
|
+
const { buildStyledResolver } = require("../normalize/style-resolver.js");
|
|
6
|
+
|
|
7
|
+
let styledResolver = null;
|
|
8
|
+
const withStyledResolver = (opts) => {
|
|
9
|
+
if (!styledResolver) styledResolver = buildStyledResolver();
|
|
10
|
+
return styledResolver ? { ...opts, styledResolver } : opts;
|
|
11
|
+
};
|
|
5
12
|
|
|
6
13
|
const PLAYGROUNDS = {
|
|
7
14
|
react: () => require("./react.js"),
|
|
@@ -25,19 +32,19 @@ const getFramework = (opts) => normalizeOptions(opts).framework;
|
|
|
25
32
|
const parseComponents = (fileAbs, canonicalNames, opts) => {
|
|
26
33
|
const pg = getPlayground(getFramework(opts));
|
|
27
34
|
if (!pg.available || !pg.available()) return [];
|
|
28
|
-
return pg.parseComponents(fileAbs, canonicalNames, opts);
|
|
35
|
+
return pg.parseComponents(fileAbs, canonicalNames, withStyledResolver(opts));
|
|
29
36
|
};
|
|
30
37
|
|
|
31
38
|
const parseElements = (fileAbs, canonicalNames, opts) => {
|
|
32
39
|
const pg = getPlayground(getFramework(opts));
|
|
33
40
|
if (!pg.available || !pg.available() || !pg.parseElements) return [];
|
|
34
|
-
return pg.parseElements(fileAbs, canonicalNames, opts);
|
|
41
|
+
return pg.parseElements(fileAbs, canonicalNames, withStyledResolver(opts));
|
|
35
42
|
};
|
|
36
43
|
|
|
37
44
|
const parseParts = (fileAbs, canonicalNames, opts) => {
|
|
38
45
|
const pg = getPlayground(getFramework(opts));
|
|
39
46
|
if (!pg.available || !pg.available() || !pg.parseParts) return [];
|
|
40
|
-
return pg.parseParts(fileAbs, canonicalNames, opts);
|
|
47
|
+
return pg.parseParts(fileAbs, canonicalNames, withStyledResolver(opts));
|
|
41
48
|
};
|
|
42
49
|
|
|
43
50
|
const resolveCss = (tree, opts) => {
|
package/playgrounds/react.js
CHANGED
|
@@ -372,7 +372,7 @@ const memberTarget = (sf, head, prop) => {
|
|
|
372
372
|
return found;
|
|
373
373
|
};
|
|
374
374
|
|
|
375
|
-
const resolveTag = (name, fileAbs, depth, props) => {
|
|
375
|
+
const resolveTag = (name, fileAbs, depth, props, styledResolver) => {
|
|
376
376
|
const staticProps = Object.entries(props || {}).filter(([, v]) => typeof v === "string").sort();
|
|
377
377
|
const key = `${fileAbs}::${name}::${JSON.stringify(staticProps)}`;
|
|
378
378
|
if (resolvedTags.has(key)) return resolvedTags.get(key);
|
|
@@ -395,7 +395,7 @@ const resolveTag = (name, fileAbs, depth, props) => {
|
|
|
395
395
|
const inner = firstComponentNodeIn(decl.body);
|
|
396
396
|
if (!inner) return null;
|
|
397
397
|
resolvedTags.set(key, null);
|
|
398
|
-
const built = buildTree(inner, targetSf, { bodies: localComponentBodies(targetSf), depth: depth + 1, nodes: 0, props: Object.fromEntries(staticProps), resolveTag: (tag, nested) => resolveTag(tag, located.file, depth + 1, nested) });
|
|
398
|
+
const built = buildTree(inner, targetSf, { bodies: localComponentBodies(targetSf), depth: depth + 1, nodes: 0, props: Object.fromEntries(staticProps), styledResolver, resolveTag: (tag, nested) => resolveTag(tag, located.file, depth + 1, nested, styledResolver) });
|
|
399
399
|
const tree = built && { ...built, via: [declName, ...(built.via || [])] };
|
|
400
400
|
resolvedTags.set(key, tree);
|
|
401
401
|
return tree;
|
|
@@ -489,6 +489,8 @@ const buildTree = (node, sf, ctx) => {
|
|
|
489
489
|
}
|
|
490
490
|
const resolved = ctx.resolveTag ? ctx.resolveTag(tag, attrs) : null;
|
|
491
491
|
if (resolved) return graftChildren(resolved, children, tree);
|
|
492
|
+
const styledInfo = ctx.styledResolver ? ctx.styledResolver(tag, sf) : null;
|
|
493
|
+
if (styledInfo) return styledNode(tag, styledInfo, tree, children, sf, ctx);
|
|
492
494
|
dynamic = true;
|
|
493
495
|
dynamicRefs.push(tag);
|
|
494
496
|
}
|
|
@@ -552,6 +554,34 @@ const graftChildren = (resolved, children, usage) => {
|
|
|
552
554
|
return fillSlot(clone, children).node;
|
|
553
555
|
};
|
|
554
556
|
|
|
557
|
+
const styledNode = (tag, info, usage, children, sf, ctx) => {
|
|
558
|
+
let base = null;
|
|
559
|
+
if (info.componentRef) {
|
|
560
|
+
const body = ctx.bodies.get(info.componentRef);
|
|
561
|
+
if (body && ctx.depth < MAX_UNFOLD_DEPTH) {
|
|
562
|
+
const inner = firstComponentNodeIn(body);
|
|
563
|
+
if (inner) base = buildTree(inner, sf, { ...ctx, depth: ctx.depth + 1, props: {} });
|
|
564
|
+
}
|
|
565
|
+
if (!base) base = ctx.resolveTag ? ctx.resolveTag(info.componentRef, {}) : null;
|
|
566
|
+
}
|
|
567
|
+
if (base) {
|
|
568
|
+
const merged = { ...(base.style || {}) };
|
|
569
|
+
for (const [k, v] of Object.entries(info.styleObj || {})) merged[k] = v;
|
|
570
|
+
const clone = { ...base, style: Object.keys(merged).length > 0 ? merged : null, cssText: styleObjectToCssText(merged), via: [tag, ...(base.via || [])] };
|
|
571
|
+
return graftChildren(clone, children, usage);
|
|
572
|
+
}
|
|
573
|
+
return {
|
|
574
|
+
tag: info.baseTag || tag,
|
|
575
|
+
spread: usage.spread,
|
|
576
|
+
attrs: usage.attrs,
|
|
577
|
+
className: usage.className,
|
|
578
|
+
style: Object.keys(info.styleObj || {}).length > 0 ? info.styleObj : null,
|
|
579
|
+
cssText: styleObjectToCssText(info.styleObj || {}),
|
|
580
|
+
children,
|
|
581
|
+
via: [tag],
|
|
582
|
+
};
|
|
583
|
+
};
|
|
584
|
+
|
|
555
585
|
const a11yOfOpening = (opening, sf, out) => {
|
|
556
586
|
for (const attr of opening.attributes.properties) {
|
|
557
587
|
if (!ts.isJsxAttribute(attr) || !attr.name) continue;
|
|
@@ -711,7 +741,7 @@ const parseElements = (fileAbs, canonicalNames, opts) => {
|
|
|
711
741
|
const opening = openingOf(n);
|
|
712
742
|
const tag = tagOf(opening.tagName);
|
|
713
743
|
if (tag && isHtmlTag(tag)) {
|
|
714
|
-
const tree = buildTree(n, sf, { bodies, depth: 0, nodes: 0, props: {}, resolveTag: (name, props) => resolveTag(name, fileAbs, 0, props) });
|
|
744
|
+
const tree = buildTree(n, sf, { bodies, depth: 0, nodes: 0, props: {}, styledResolver: opts.styledResolver, resolveTag: (name, props) => resolveTag(name, fileAbs, 0, props, opts.styledResolver) });
|
|
715
745
|
if (tree) {
|
|
716
746
|
const a11y = new Set();
|
|
717
747
|
a11yOfOpening(opening, sf, a11y);
|
|
@@ -755,7 +785,7 @@ const parseParts = (fileAbs, canonicalNames, opts) => {
|
|
|
755
785
|
if (n !== root && isJsxNode(n)) {
|
|
756
786
|
const size = jsxCount(n);
|
|
757
787
|
if (size >= MIN_PART_NODES && size <= MAX_PART_NODES) {
|
|
758
|
-
const tree = buildTree(n, sf, { bodies, depth: 0, nodes: 0, props: {}, resolveTag: (name, props) => resolveTag(name, fileAbs, 0, props) });
|
|
788
|
+
const tree = buildTree(n, sf, { bodies, depth: 0, nodes: 0, props: {}, styledResolver: opts.styledResolver, resolveTag: (name, props) => resolveTag(name, fileAbs, 0, props, opts.styledResolver) });
|
|
759
789
|
if (tree && treeSize(tree) >= MIN_PART_NODES) {
|
|
760
790
|
const rootTag = tagOf(openingOf(n).tagName);
|
|
761
791
|
const a11y = new Set();
|
|
@@ -771,8 +801,7 @@ const parseParts = (fileAbs, canonicalNames, opts) => {
|
|
|
771
801
|
endLine: end.line + 1,
|
|
772
802
|
tree,
|
|
773
803
|
rootTags: [rootTag],
|
|
774
|
-
a11y: [...a11y].sort(),
|
|
775
|
-
usesCanonical: names.size > 0 ? [...new Set([...canonicalTagsUsed(n, names), ...viaNames(tree, names)])].sort() : [],
|
|
804
|
+
a11y: [...a11y].sort(), partial: true, usesCanonical: names.size > 0 ? [...new Set([...canonicalTagsUsed(n, names), ...viaNames(tree, names)])].sort() : [],
|
|
776
805
|
});
|
|
777
806
|
}
|
|
778
807
|
}
|
|
@@ -832,7 +861,7 @@ const parseComponents = (fileAbs, canonicalNames, opts) => {
|
|
|
832
861
|
for (const d of decls) {
|
|
833
862
|
if (!hasComponentMarkup(d.body)) continue;
|
|
834
863
|
const root = firstComponentNodeIn(d.body);
|
|
835
|
-
const tree = root ? buildTree(root, sf, { bodies, depth: 0, nodes: 0, resolveTag: (tag, props) => resolveTag(tag, fileAbs, 0, props) }) : null;
|
|
864
|
+
const tree = root ? buildTree(root, sf, { bodies, depth: 0, nodes: 0, styledResolver: opts.styledResolver, resolveTag: (tag, props) => resolveTag(tag, fileAbs, 0, props, opts.styledResolver) }) : null;
|
|
836
865
|
const rawTree = root ? buildTree(root, sf, { bodies, depth: 0, nodes: 0, noUnfold: true }) : null;
|
|
837
866
|
const a11y = new Set();
|
|
838
867
|
gatherA11y(d.body, sf, a11y);
|
|
@@ -876,6 +905,7 @@ const buildDom = (tree, doc) => {
|
|
|
876
905
|
}
|
|
877
906
|
const tag = String(tree.tag || "").toLowerCase();
|
|
878
907
|
const el = isHtmlTag(tag) ? doc.createElement(tag) : doc.createElement("cu-root");
|
|
908
|
+
if (!isHtmlTag(tag)) el.__cuTag = String(tree.tag);
|
|
879
909
|
for (const [key, value] of entries(tree.attrs || {})) {
|
|
880
910
|
if (value === true) el.setAttribute(key, "");
|
|
881
911
|
else if (value !== null && value !== undefined) el.setAttribute(key, String(value));
|
|
@@ -897,7 +927,8 @@ const buildDom = (tree, doc) => {
|
|
|
897
927
|
|
|
898
928
|
const nodesOf = (el, win, baselineOf) => {
|
|
899
929
|
const kids = [...el.children].flatMap((child) => nodesOf(child, win, baselineOf));
|
|
900
|
-
|
|
930
|
+
const compTag = el.tagName === "CU-ROOT" ? el.__cuTag || null : null;
|
|
931
|
+
if (el.tagName === "CU-ROOT" && !compTag) return kids;
|
|
901
932
|
const computed = win.getComputedStyle(el);
|
|
902
933
|
const base = baselineOf(el);
|
|
903
934
|
const css = {};
|
|
@@ -905,7 +936,7 @@ const nodesOf = (el, win, baselineOf) => {
|
|
|
905
936
|
const value = computed.getPropertyValue(prop).trim();
|
|
906
937
|
if (value && value !== base.getPropertyValue(prop).trim()) css[prop] = value;
|
|
907
938
|
}
|
|
908
|
-
const tag = el.tagName.toLowerCase();
|
|
939
|
+
const tag = compTag || el.tagName.toLowerCase();
|
|
909
940
|
const interactive = INTERACTIVE_TAGS.has(tag) || el.getAttribute("role") === "button" || el.getAttribute("role") === "link" || [...el.attributes].some((attr) => /^(on|@)/.test(attr.name));
|
|
910
941
|
const text = [...el.childNodes].filter((n) => n.nodeType === 3).map((n) => n.nodeValue).join("").replace(/\s+/g, " ").trim();
|
|
911
942
|
|