@md-code/component-uniqueness 0.1.3 → 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 -60
- package/config.js +0 -362
- package/decision-maker.js +0 -109
- package/filter/drop-child-element.js +0 -13
- package/filter/drop-usages.js +0 -13
- package/filter/missing-component.js +0 -68
- package/index.js +0 -184
- package/logger.js +0 -44
- package/main.js +0 -539
- 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 -208
- package/normalize/candidate.js +0 -22
- package/normalize/dom.js +0 -167
- package/normalize/framework.js +0 -129
- 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 -997
- 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 -159
- package/utils.js +0 -92
package/matcher/index.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { matchName } = require("./name");
|
|
4
|
-
const { matchStructure } = require("./structure");
|
|
5
|
-
const { matchBehavior } = require("./behavior");
|
|
6
|
-
const { matchA11y, tokensOf } = require("./a11y");
|
|
7
|
-
|
|
8
|
-
const restrictTo = (candidate, canon) => {
|
|
9
|
-
const events = new Set(candidate.framework.events);
|
|
10
|
-
const props = new Set(candidate.framework.props);
|
|
11
|
-
const a11y = tokensOf(candidate, "a11y");
|
|
12
|
-
|
|
13
|
-
return {
|
|
14
|
-
...canon,
|
|
15
|
-
framework: {
|
|
16
|
-
props: canon.framework.open ? [...props] : (canon.framework.props || []).filter((p) => props.has(p)),
|
|
17
|
-
events: (canon.framework.events || []).filter((e) => events.has(e)),
|
|
18
|
-
names: [],
|
|
19
|
-
},
|
|
20
|
-
a11y: canon.framework.open ? candidate.a11y || [] : (canon.a11y || []).filter((t) => a11y.has(String(t).toLowerCase())),
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const matchSignals = (candidate, canon, { canonNames, frameworkMatcher }) => {
|
|
25
|
-
const target = candidate.partial ? restrictTo(candidate, canon) : canon;
|
|
26
|
-
|
|
27
|
-
return {
|
|
28
|
-
name: matchName(candidate, target, { canonNames }),
|
|
29
|
-
structure: matchStructure(candidate, target),
|
|
30
|
-
behavior: matchBehavior(candidate, target),
|
|
31
|
-
a11y: matchA11y(candidate, target),
|
|
32
|
-
framework: frameworkMatcher.matchFramework(candidate, target),
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
module.exports = { matchSignals };
|
package/matcher/name.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const path = require("node:path");
|
|
4
|
-
const { pascalWords, GENERIC_WORDS } = require("../normalize/words.js");
|
|
5
|
-
|
|
6
|
-
const FUZZY_NAME_MIN_LEN = 5;
|
|
7
|
-
const FUZZY_BASENAME_MIN_LEN = 4;
|
|
8
|
-
|
|
9
|
-
const kebabToPascal = (s) =>
|
|
10
|
-
s
|
|
11
|
-
.split(/[-_ ]+/)
|
|
12
|
-
.filter(Boolean)
|
|
13
|
-
.map((w) => w[0].toUpperCase() + w.slice(1))
|
|
14
|
-
.join("");
|
|
15
|
-
|
|
16
|
-
const basenameToPascal = (file) => kebabToPascal(path.basename(file, path.extname(file)));
|
|
17
|
-
|
|
18
|
-
const kebabWords = (s) => String(s || "").split(/[^A-Za-z0-9]+/).filter(Boolean);
|
|
19
|
-
const singular = (w) => (w.length > 3 && w.endsWith("s") && !w.endsWith("ss") ? w.slice(0, -1) : w);
|
|
20
|
-
|
|
21
|
-
const wordSim = (a, b) => {
|
|
22
|
-
const wa = new Set(pascalWords(a).map((w) => singular(w.toLowerCase())).filter((w) => w.length >= 3 && !GENERIC_WORDS.has(w)));
|
|
23
|
-
const wb = new Set(pascalWords(b).map((w) => singular(w.toLowerCase())).filter((w) => w.length >= 3 && !GENERIC_WORDS.has(w)));
|
|
24
|
-
if (wa.size === 0 || wb.size === 0) return 0;
|
|
25
|
-
let inter = 0;
|
|
26
|
-
for (const w of wa) if (wb.has(w)) inter += 1;
|
|
27
|
-
const union = wa.size + wb.size - inter;
|
|
28
|
-
|
|
29
|
-
return union === 0 ? 0 : inter / union;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const fuzzyNameHit = (appName, canonNames) => {
|
|
33
|
-
const words = pascalWords(appName);
|
|
34
|
-
if (words.length === 0) return null;
|
|
35
|
-
let best = null;
|
|
36
|
-
for (const cn of canonNames) {
|
|
37
|
-
if (cn.length < FUZZY_NAME_MIN_LEN) continue;
|
|
38
|
-
const lc = cn.toLowerCase();
|
|
39
|
-
for (const w of words) {
|
|
40
|
-
const lw = w.toLowerCase();
|
|
41
|
-
if (lw === lc || (lw.length >= FUZZY_NAME_MIN_LEN && (lw.startsWith(lc) || lw.endsWith(lc)))) {
|
|
42
|
-
if (!best || cn.length > best.name.length) best = { name: cn };
|
|
43
|
-
break;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return best;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const fuzzyBasenameHit = (appPath, canonNames) => {
|
|
51
|
-
const base = path.basename(appPath, path.extname(appPath));
|
|
52
|
-
const words = kebabWords(base).map((w) => w.toLowerCase());
|
|
53
|
-
if (words.length === 0) return null;
|
|
54
|
-
const fullKebab = words.join("-");
|
|
55
|
-
let best = null;
|
|
56
|
-
for (const cn of canonNames) {
|
|
57
|
-
if (cn.length < FUZZY_BASENAME_MIN_LEN) continue;
|
|
58
|
-
const cnWords = pascalWords(cn).map((w) => w.toLowerCase());
|
|
59
|
-
const cnKebab = cnWords.join("-");
|
|
60
|
-
if (fullKebab === cnKebab || fullKebab === singular(cnKebab) || fullKebab === cnKebab.replace(/s$/, "")) {
|
|
61
|
-
if (!best || cn.length > best.name.length) best = { name: cn };
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
const lc = cn.toLowerCase();
|
|
65
|
-
const lcs = singular(lc);
|
|
66
|
-
for (const w of words) {
|
|
67
|
-
if (w === lc || w === lcs || singular(w) === lc) {
|
|
68
|
-
if (!best || cn.length > best.name.length) best = { name: cn };
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (best && best.name === cn) continue;
|
|
73
|
-
for (const w of words) {
|
|
74
|
-
if (w.length < 4) continue;
|
|
75
|
-
const ws = singular(w);
|
|
76
|
-
if (cnWords.some((cw) => cw === w || cw === ws || singular(cw) === ws)) {
|
|
77
|
-
if (!best || cn.length > best.name.length) best = { name: cn };
|
|
78
|
-
break;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return best;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
const matchName = (candidate, canon, ctx) => {
|
|
86
|
-
const o = ctx || {};
|
|
87
|
-
const canonNames = o.canonNames || [canon.name];
|
|
88
|
-
|
|
89
|
-
const exactName = candidate.name === canon.name;
|
|
90
|
-
const exactBasename = !exactName && basenameToPascal(candidate.path) === canon.name;
|
|
91
|
-
|
|
92
|
-
let fuzzy = null;
|
|
93
|
-
if (!exactName && !exactBasename) {
|
|
94
|
-
fuzzy = fuzzyNameHit(candidate.name, [canon.name]) || fuzzyBasenameHit(candidate.path, [canon.name]);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const word = wordSim(candidate.name, canon.name);
|
|
98
|
-
|
|
99
|
-
let score = 0;
|
|
100
|
-
let matchType = null;
|
|
101
|
-
if (exactName) {
|
|
102
|
-
score = 1;
|
|
103
|
-
matchType = "exact-name";
|
|
104
|
-
} else if (exactBasename) {
|
|
105
|
-
score = 0.9;
|
|
106
|
-
matchType = "exact-basename";
|
|
107
|
-
} else if (fuzzy) {
|
|
108
|
-
const byName = !!fuzzyNameHit(candidate.name, [canon.name]);
|
|
109
|
-
score = (byName ? 0.7 : 0.6) * (0.5 + 0.5 * word);
|
|
110
|
-
matchType = byName ? "fuzzy-name" : "fuzzy-basename";
|
|
111
|
-
} else if (word > 0) {
|
|
112
|
-
score = 0.6 * word;
|
|
113
|
-
matchType = "word-overlap";
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const evidence = {
|
|
117
|
-
exactName,
|
|
118
|
-
exactBasename,
|
|
119
|
-
fuzzy: fuzzy ? fuzzy.name : null,
|
|
120
|
-
wordSim: Math.round(word * 100) / 100,
|
|
121
|
-
matchType,
|
|
122
|
-
};
|
|
123
|
-
return { score, evidence };
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
module.exports = {
|
|
127
|
-
matchName,
|
|
128
|
-
kebabToPascal,
|
|
129
|
-
basenameToPascal,
|
|
130
|
-
fuzzyNameHit,
|
|
131
|
-
fuzzyBasenameHit,
|
|
132
|
-
};
|
package/matcher/structure.js
DELETED
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { NON_SEMANTIC_TAGS, isPascalCase } = require("../normalize/dom.js");
|
|
4
|
-
|
|
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
|
-
const NEAR_IDENTICAL_CEILING = 0.98;
|
|
11
|
-
|
|
12
|
-
const isTextNode = (node) => node.tag === "children" || node.tag === "text";
|
|
13
|
-
const isIconNode = (node) => isPascalCase(node.tag);
|
|
14
|
-
|
|
15
|
-
const pairsOf = (css) => new Set(Object.entries(css || {}).map(([prop, value]) => `${prop}:${value}`));
|
|
16
|
-
|
|
17
|
-
const overlap = (a, b) => [...a].filter((pair) => b.has(pair)).length;
|
|
18
|
-
|
|
19
|
-
const jaccard = (a, b) => {
|
|
20
|
-
const union = a.size + b.size - overlap(a, b);
|
|
21
|
-
|
|
22
|
-
return union === 0 ? 0 : overlap(a, b) / union;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const coverage = (a, b) => (a.size === 0 ? 0 : overlap(a, b) / a.size);
|
|
26
|
-
|
|
27
|
-
const textSim = (a, b) => {
|
|
28
|
-
const ta = String(a.text || "").toLowerCase().trim();
|
|
29
|
-
const tb = String(b.text || "").toLowerCase().trim();
|
|
30
|
-
if (!ta && !tb) return 1;
|
|
31
|
-
if (!ta || !tb) return 0;
|
|
32
|
-
|
|
33
|
-
return ta === tb ? 1 : 0;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const tagMatch = (a, b) => {
|
|
37
|
-
if (a.tag === b.tag) return 1;
|
|
38
|
-
if (a.interactive && b.interactive) return 1;
|
|
39
|
-
|
|
40
|
-
return 0;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const nodeSimilarity = (a, b) => {
|
|
44
|
-
if (isTextNode(a) && isTextNode(b)) return textSim(a, b);
|
|
45
|
-
if (isIconNode(a) && isIconNode(b)) return a.tag === b.tag ? 1 : 0.1;
|
|
46
|
-
const tag = tagMatch(a, b);
|
|
47
|
-
const pa = pairsOf(a.css);
|
|
48
|
-
const pb = pairsOf(b.css);
|
|
49
|
-
let score = pa.size === 0 && pb.size === 0 ? tag : 0.6 * tag + 0.4 * jaccard(pa, pb);
|
|
50
|
-
const ta = String(a.text || "").trim();
|
|
51
|
-
const tb = String(b.text || "").trim();
|
|
52
|
-
if (ta || tb) score = 0.7 * score + 0.3 * textSim(a, b);
|
|
53
|
-
|
|
54
|
-
return score;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const forestOf = (node) => (node.tag === "fragment" ? (node.children || []).flatMap(forestOf) : [node]);
|
|
58
|
-
|
|
59
|
-
const align = (xs, ys) => {
|
|
60
|
-
const cells = xs.map((x) => ys.map((y) => compare(x, y)));
|
|
61
|
-
const table = Array.from({ length: xs.length + 1 }, () => new Array(ys.length + 1).fill(0));
|
|
62
|
-
for (let i = 1; i <= xs.length; i += 1) {
|
|
63
|
-
for (let j = 1; j <= ys.length; j += 1) {
|
|
64
|
-
table[i][j] = Math.max(table[i - 1][j], table[i][j - 1], table[i - 1][j - 1] + cells[i - 1][j - 1].score);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
const pairs = [];
|
|
68
|
-
let i = xs.length;
|
|
69
|
-
let j = ys.length;
|
|
70
|
-
while (i > 0 && j > 0) {
|
|
71
|
-
if (table[i][j] === table[i - 1][j]) i -= 1;
|
|
72
|
-
else if (table[i][j] === table[i][j - 1]) j -= 1;
|
|
73
|
-
else {
|
|
74
|
-
pairs.push(...cells[i - 1][j - 1].pairs);
|
|
75
|
-
i -= 1;
|
|
76
|
-
j -= 1;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return { total: table[xs.length][ys.length], pairs };
|
|
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 };
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const cssStats = (pairs) => {
|
|
101
|
-
let comparable = 0;
|
|
102
|
-
let conflicts = 0;
|
|
103
|
-
let shared = 0;
|
|
104
|
-
let union = 0;
|
|
105
|
-
for (const [a, b] of pairs) {
|
|
106
|
-
for (const [prop, value] of Object.entries(a.css || {})) {
|
|
107
|
-
if (!Object.prototype.hasOwnProperty.call(b.css || {}, prop)) continue;
|
|
108
|
-
comparable += 1;
|
|
109
|
-
if (b.css[prop] !== value) conflicts += 1;
|
|
110
|
-
}
|
|
111
|
-
const pa = pairsOf(a.css);
|
|
112
|
-
const pb = pairsOf(b.css);
|
|
113
|
-
shared += overlap(pa, pb);
|
|
114
|
-
union += pa.size + pb.size - overlap(pa, pb);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return { conflicts, ratio: comparable === 0 ? 0 : conflicts / comparable, agreement: union === 0 ? 0 : shared / union };
|
|
118
|
-
};
|
|
119
|
-
|
|
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
|
-
const isInformative = (node) => !!node && (node.tag !== "fragment" || (node.children || []).some(isInformative));
|
|
131
|
-
|
|
132
|
-
const shapeOf = (node) => {
|
|
133
|
-
const tags = new Set();
|
|
134
|
-
let nodes = 0;
|
|
135
|
-
const visit = (n) => {
|
|
136
|
-
if (n.tag !== "fragment") {
|
|
137
|
-
tags.add(n.tag);
|
|
138
|
-
nodes += 1;
|
|
139
|
-
}
|
|
140
|
-
for (const child of n.children || []) visit(child);
|
|
141
|
-
};
|
|
142
|
-
visit(node);
|
|
143
|
-
|
|
144
|
-
return { tags, nodes };
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
const componentNamesOf = (tree, out = new Set()) => {
|
|
148
|
-
if (!tree) return out;
|
|
149
|
-
for (const name of tree.via || []) out.add(name);
|
|
150
|
-
for (const child of tree.children || []) componentNamesOf(child, out);
|
|
151
|
-
|
|
152
|
-
return out;
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
const sizeOf = (candidate) => {
|
|
156
|
-
const { tags, nodes } = shapeOf(candidate.dom.tree);
|
|
157
|
-
const distinct = new Set([...tags, ...componentNamesOf(candidate.tree)]).size;
|
|
158
|
-
|
|
159
|
-
return { nodes, distinct };
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
const ceilingOf = (nodes, distinct, stats) => {
|
|
163
|
-
const base = CEILING_LADDER[Math.min(nodes - 1, CEILING_LADDER.length - 1)];
|
|
164
|
-
if (distinct >= RICH_DISTINCT_MIN && stats.conflicts > 0) return Math.min(base, CONFLICTED_RICH_CEILING);
|
|
165
|
-
|
|
166
|
-
return base;
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
const classTokensDiffer = (a, b) => {
|
|
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;
|
|
175
|
-
|
|
176
|
-
return false;
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
const matchStructure = (candidate, canon) => {
|
|
180
|
-
const cand = candidate.dom && candidate.dom.tree;
|
|
181
|
-
const target = canon.dom && canon.dom.tree;
|
|
182
|
-
const mode = candidate.partial ? "root-node" : "tree";
|
|
183
|
-
const applicable = isInformative(cand) || isInformative(target);
|
|
184
|
-
let score = 0;
|
|
185
|
-
let detail = null;
|
|
186
|
-
if (applicable && cand && target) {
|
|
187
|
-
if (candidate.partial) {
|
|
188
|
-
const classesDiffer = classTokensDiffer(candidate.rawClasses, canon.rawClasses);
|
|
189
|
-
const base = rootNodeScore(cand, target);
|
|
190
|
-
score = Math.min(base, classesDiffer ? NEAR_IDENTICAL_CEILING : 1);
|
|
191
|
-
detail = { similarity: base, ceiling: classesDiffer ? NEAR_IDENTICAL_CEILING : 1, classesDiffer };
|
|
192
|
-
} else {
|
|
193
|
-
const result = compareForests(cand, target);
|
|
194
|
-
const stats = cssStats(result.pairs);
|
|
195
|
-
const a = sizeOf(candidate);
|
|
196
|
-
const b = sizeOf(canon);
|
|
197
|
-
const classesDiffer = classTokensDiffer(candidate.rawClasses, canon.rawClasses);
|
|
198
|
-
const ceiling = Math.min(ceilingOf(Math.min(a.nodes, b.nodes), Math.min(a.distinct, b.distinct), stats), classesDiffer ? NEAR_IDENTICAL_CEILING : 1);
|
|
199
|
-
const missingRatio = Math.max(0, Math.max(a.nodes, b.nodes) - Math.min(a.nodes, b.nodes)) / Math.max(1, Math.max(a.nodes, b.nodes));
|
|
200
|
-
score = Math.max(0, Math.min(result.score, ceiling) - CONFLICT_PENALTY * stats.ratio - MISSING_CHILD_PENALTY * missingRatio);
|
|
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) };
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
return { score, applicable, evidence: { mode, ...(detail || {}) } };
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
module.exports = { matchStructure };
|
package/normalize/candidate.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { normalizeDom } = require("./dom.js");
|
|
4
|
-
const { normalizeFramework } = require("./framework.js");
|
|
5
|
-
const { rawClassTokens } = require("../playgrounds/framework-entrypoint.js");
|
|
6
|
-
|
|
7
|
-
const buildCandidate = (comp, config) => ({
|
|
8
|
-
...comp,
|
|
9
|
-
dom: normalizeDom(comp.tree, config, comp.name),
|
|
10
|
-
framework: normalizeFramework(comp.rawTree || comp.tree, comp.name),
|
|
11
|
-
rawClasses: [...rawClassTokens(comp.rawTree || comp.tree)],
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
const buildElementCandidate = (element, config) => {
|
|
15
|
-
const framework = normalizeFramework(element.tree, element.name);
|
|
16
|
-
if (framework.events.length === 0 && element.a11y.length === 0) return null;
|
|
17
|
-
const dom = normalizeDom(element.tree, config, element.name);
|
|
18
|
-
|
|
19
|
-
return { ...element, partial: true, dom, framework: { ...framework, names: [] }, rawClasses: [...rawClassTokens(element.tree)] };
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
module.exports = { buildCandidate, buildElementCandidate };
|
package/normalize/dom.js
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { render } = require("../playgrounds/framework-entrypoint.js");
|
|
4
|
-
const { isLayoutProp } = require("./style.js");
|
|
5
|
-
|
|
6
|
-
const HTML_TAGS = new Set([
|
|
7
|
-
"html",
|
|
8
|
-
"head",
|
|
9
|
-
"body",
|
|
10
|
-
"div",
|
|
11
|
-
"span",
|
|
12
|
-
"a",
|
|
13
|
-
"p",
|
|
14
|
-
"main",
|
|
15
|
-
"section",
|
|
16
|
-
"header",
|
|
17
|
-
"footer",
|
|
18
|
-
"nav",
|
|
19
|
-
"aside",
|
|
20
|
-
"article",
|
|
21
|
-
"ul",
|
|
22
|
-
"ol",
|
|
23
|
-
"li",
|
|
24
|
-
"table",
|
|
25
|
-
"thead",
|
|
26
|
-
"tbody",
|
|
27
|
-
"tr",
|
|
28
|
-
"td",
|
|
29
|
-
"th",
|
|
30
|
-
"form",
|
|
31
|
-
"label",
|
|
32
|
-
"input",
|
|
33
|
-
"button",
|
|
34
|
-
"select",
|
|
35
|
-
"textarea",
|
|
36
|
-
"img",
|
|
37
|
-
"figure",
|
|
38
|
-
"figcaption",
|
|
39
|
-
"svg",
|
|
40
|
-
"path",
|
|
41
|
-
"circle",
|
|
42
|
-
"rect",
|
|
43
|
-
"g",
|
|
44
|
-
"pre",
|
|
45
|
-
"code",
|
|
46
|
-
"strong",
|
|
47
|
-
"em",
|
|
48
|
-
"small",
|
|
49
|
-
"h1",
|
|
50
|
-
"h2",
|
|
51
|
-
"h3",
|
|
52
|
-
"h4",
|
|
53
|
-
"h5",
|
|
54
|
-
"h6",
|
|
55
|
-
"hr",
|
|
56
|
-
"br",
|
|
57
|
-
"i",
|
|
58
|
-
"b",
|
|
59
|
-
"u",
|
|
60
|
-
"s",
|
|
61
|
-
"sub",
|
|
62
|
-
"sup",
|
|
63
|
-
"blockquote",
|
|
64
|
-
"details",
|
|
65
|
-
"summary",
|
|
66
|
-
"dialog",
|
|
67
|
-
"canvas",
|
|
68
|
-
"video",
|
|
69
|
-
"audio",
|
|
70
|
-
"source",
|
|
71
|
-
"track",
|
|
72
|
-
"map",
|
|
73
|
-
"area",
|
|
74
|
-
"dl",
|
|
75
|
-
"dt",
|
|
76
|
-
"dd",
|
|
77
|
-
"fieldset",
|
|
78
|
-
"legend",
|
|
79
|
-
"data",
|
|
80
|
-
"output",
|
|
81
|
-
"progress",
|
|
82
|
-
"meter",
|
|
83
|
-
"time",
|
|
84
|
-
"mark",
|
|
85
|
-
"abbr",
|
|
86
|
-
"ruby",
|
|
87
|
-
"rt",
|
|
88
|
-
"rp",
|
|
89
|
-
"wbr",
|
|
90
|
-
"bdi",
|
|
91
|
-
"bdo",
|
|
92
|
-
"template",
|
|
93
|
-
"slot",
|
|
94
|
-
"picture",
|
|
95
|
-
"base",
|
|
96
|
-
"link",
|
|
97
|
-
"meta",
|
|
98
|
-
"style",
|
|
99
|
-
"title",
|
|
100
|
-
"script",
|
|
101
|
-
"noscript",
|
|
102
|
-
"address",
|
|
103
|
-
"hgroup",
|
|
104
|
-
"search",
|
|
105
|
-
"menu",
|
|
106
|
-
"kbd",
|
|
107
|
-
"samp",
|
|
108
|
-
"var",
|
|
109
|
-
"dfn",
|
|
110
|
-
"cite",
|
|
111
|
-
"q",
|
|
112
|
-
"del",
|
|
113
|
-
"ins",
|
|
114
|
-
"caption",
|
|
115
|
-
"col",
|
|
116
|
-
"colgroup",
|
|
117
|
-
"tfoot",
|
|
118
|
-
"optgroup",
|
|
119
|
-
"option",
|
|
120
|
-
"datalist",
|
|
121
|
-
"iframe",
|
|
122
|
-
"embed",
|
|
123
|
-
"object",
|
|
124
|
-
"math",
|
|
125
|
-
]);
|
|
126
|
-
|
|
127
|
-
const INTERACTIVE_TAGS = new Set(["a", "area", "audio", "button", "details", "embed", "iframe", "input", "label", "optgroup", "option", "select", "summary", "textarea", "video"]);
|
|
128
|
-
const NON_SEMANTIC_TAGS = new Set(["div", "span"]);
|
|
129
|
-
|
|
130
|
-
const isPascalCase = (name) => /^[A-Z][A-Za-z0-9]*$/.test(name);
|
|
131
|
-
|
|
132
|
-
const isHtmlTag = (tag) => HTML_TAGS.has(String(tag || ""));
|
|
133
|
-
|
|
134
|
-
const styleObjectToCssText = (obj) => {
|
|
135
|
-
const parts = [];
|
|
136
|
-
for (const [k, v] of Object.entries(obj || {})) {
|
|
137
|
-
if (Array.isArray(v)) parts.push(`${k}: ${v.join("; ")}`);
|
|
138
|
-
else parts.push(`${k}: ${v}`);
|
|
139
|
-
}
|
|
140
|
-
return parts.join("; ");
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
const layoutOnly = (node) => ({
|
|
145
|
-
tag: node.tag,
|
|
146
|
-
text: node.text || undefined,
|
|
147
|
-
interactive: !!node.interactive,
|
|
148
|
-
css: Object.fromEntries(Object.entries(node.css || {}).filter(([prop]) => isLayoutProp(prop))),
|
|
149
|
-
children: (node.children || []).map(layoutOnly),
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
const normalizeDom = (tree, opts, label) => {
|
|
153
|
-
const rendered = render(tree, opts);
|
|
154
|
-
const domTree = rendered.available && rendered.tree ? layoutOnly(rendered.tree) : null;
|
|
155
|
-
|
|
156
|
-
return { tree: domTree, available: rendered.available };
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
module.exports = {
|
|
160
|
-
HTML_TAGS,
|
|
161
|
-
INTERACTIVE_TAGS,
|
|
162
|
-
NON_SEMANTIC_TAGS,
|
|
163
|
-
isPascalCase,
|
|
164
|
-
isHtmlTag,
|
|
165
|
-
styleObjectToCssText,
|
|
166
|
-
normalizeDom,
|
|
167
|
-
};
|
package/normalize/framework.js
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { INTERACTIVE_TAGS, isPascalCase } = require("./dom.js");
|
|
4
|
-
|
|
5
|
-
const EVENT_PREFIX = /^(on|ng|v-on:|@)/;
|
|
6
|
-
|
|
7
|
-
const isEventName = (name) => EVENT_PREFIX.test(String(name || ""));
|
|
8
|
-
|
|
9
|
-
const eventNameOf = (name) => {
|
|
10
|
-
const raw = String(name || "");
|
|
11
|
-
if (raw.startsWith("on")) return raw.slice(2).toLowerCase();
|
|
12
|
-
if (raw.startsWith("ng")) return raw.slice(2).toLowerCase();
|
|
13
|
-
if (raw.startsWith("v-on:")) return raw.slice(5).toLowerCase();
|
|
14
|
-
if (raw.startsWith("@")) return raw.slice(1).toLowerCase();
|
|
15
|
-
return raw.toLowerCase();
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const walk = (tree, visit) => {
|
|
19
|
-
if (!tree) return;
|
|
20
|
-
visit(tree);
|
|
21
|
-
for (const child of tree.children || []) walk(child, visit);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const propsOf = (tree) => {
|
|
25
|
-
const out = new Set();
|
|
26
|
-
walk(tree, (node) => {
|
|
27
|
-
for (const name of Object.keys(node.attrs || {})) {
|
|
28
|
-
if (isEventName(name)) continue;
|
|
29
|
-
out.add(name);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
return [...out].sort();
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const eventsOf = (tree) => {
|
|
36
|
-
const out = new Set();
|
|
37
|
-
walk(tree, (node) => {
|
|
38
|
-
if (INTERACTIVE_TAGS.has(node.tag)) out.add("click");
|
|
39
|
-
for (const name of Object.keys(node.attrs || {})) {
|
|
40
|
-
if (isEventName(name)) out.add(eventNameOf(name));
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
return [...out].sort();
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const namesOf = (tree) => {
|
|
47
|
-
const out = [];
|
|
48
|
-
walk(tree, (node) => {
|
|
49
|
-
if (node.tag && isPascalCase(node.tag)) out.push(node.tag);
|
|
50
|
-
});
|
|
51
|
-
return out;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const normalizeFramework = (tree, label) => {
|
|
55
|
-
const profile = {
|
|
56
|
-
props: propsOf(tree),
|
|
57
|
-
events: eventsOf(tree),
|
|
58
|
-
names: namesOf(tree),
|
|
59
|
-
open: !!(tree && tree.spread),
|
|
60
|
-
};
|
|
61
|
-
return profile;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const jaccard = (a, b) => {
|
|
65
|
-
const A = new Set(a || []);
|
|
66
|
-
const B = new Set(b || []);
|
|
67
|
-
if (A.size === 0 || B.size === 0) return 0;
|
|
68
|
-
let inter = 0;
|
|
69
|
-
for (const item of A) if (B.has(item)) inter += 1;
|
|
70
|
-
const union = A.size + B.size - inter;
|
|
71
|
-
return union === 0 ? 0 : inter / union;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const shared = (a, b) => {
|
|
75
|
-
const B = new Set(b || []);
|
|
76
|
-
return [...new Set(a || [])].filter((item) => B.has(item)).sort();
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const smallSetDiscount = (arrA, arrB) => {
|
|
80
|
-
const A = new Set(arrA || []);
|
|
81
|
-
const B = new Set(arrB || []);
|
|
82
|
-
const inter = [...A].filter((x) => B.has(x)).length;
|
|
83
|
-
const union = A.size + B.size - inter;
|
|
84
|
-
return union === 0 ? 1 : Math.min(1, union / 2);
|
|
85
|
-
};
|
|
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
|
-
const frameworkSimilarity = (a, b, label) => {
|
|
100
|
-
const A = a || { props: [], events: [], names: [] };
|
|
101
|
-
const B = b || { props: [], events: [], names: [] };
|
|
102
|
-
const props = jaccard(A.props, B.props) * smallSetDiscount(A.props, B.props);
|
|
103
|
-
const events = jaccard(A.events, B.events) * smallSetDiscount(A.events, B.events);
|
|
104
|
-
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
|
-
const parts = [];
|
|
113
|
-
if (A.props.length || B.props.length) parts.push({ w: 0.5, v: props });
|
|
114
|
-
if (A.events.length || B.events.length) parts.push({ w: 0.2, v: events });
|
|
115
|
-
if (A.names.length || B.names.length) parts.push({ w: 0.3, v: names });
|
|
116
|
-
const totalW = parts.reduce((sum, part) => sum + part.w, 0);
|
|
117
|
-
const score = totalW === 0 ? 0 : parts.reduce((sum, part) => sum + part.w * part.v, 0) / totalW;
|
|
118
|
-
return { score, props, events, names };
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
module.exports = {
|
|
122
|
-
normalizeFramework,
|
|
123
|
-
frameworkSimilarity,
|
|
124
|
-
propsOf,
|
|
125
|
-
eventsOf,
|
|
126
|
-
namesOf,
|
|
127
|
-
isEventName,
|
|
128
|
-
eventNameOf,
|
|
129
|
-
};
|