@md-code/component-uniqueness 0.1.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/README.md +149 -0
- package/config.js +337 -0
- package/decision-maker.js +113 -0
- package/filter/drop-child-element.js +13 -0
- package/filter/drop-usages.js +10 -0
- package/index.js +178 -0
- package/main.js +433 -0
- package/matcher/a11y.js +65 -0
- package/matcher/behavior.js +27 -0
- package/matcher/framework/index.js +15 -0
- package/matcher/framework/react.js +25 -0
- package/matcher/index.js +36 -0
- package/matcher/name.js +115 -0
- package/matcher/structure.js +168 -0
- package/normalize/candidate.js +20 -0
- package/normalize/dom.js +165 -0
- package/normalize/framework.js +100 -0
- package/normalize/style.js +174 -0
- package/package.json +61 -0
- package/playgrounds/angular.js +11 -0
- package/playgrounds/framework-entrypoint.js +71 -0
- package/playgrounds/react.js +928 -0
- package/playgrounds/scss.js +7 -0
- package/playgrounds/style-entrypoint.js +78 -0
- package/playgrounds/styled-components.js +7 -0
- package/playgrounds/tailwind.js +186 -0
- package/playgrounds/vue.js +11 -0
- package/plugin.js +14 -0
- package/report.js +126 -0
- package/utils.js +92 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { entries, keys } = require("remeda");
|
|
4
|
+
|
|
5
|
+
const LAYOUT_PROPS = new Set([
|
|
6
|
+
"display",
|
|
7
|
+
"position",
|
|
8
|
+
"top",
|
|
9
|
+
"right",
|
|
10
|
+
"bottom",
|
|
11
|
+
"left",
|
|
12
|
+
"z-index",
|
|
13
|
+
"flex-direction",
|
|
14
|
+
"flex-wrap",
|
|
15
|
+
"flex-grow",
|
|
16
|
+
"flex-shrink",
|
|
17
|
+
"flex-basis",
|
|
18
|
+
"flex",
|
|
19
|
+
"align-items",
|
|
20
|
+
"align-self",
|
|
21
|
+
"justify-content",
|
|
22
|
+
"gap",
|
|
23
|
+
"row-gap",
|
|
24
|
+
"column-gap",
|
|
25
|
+
"grid-template-columns",
|
|
26
|
+
"grid-template-rows",
|
|
27
|
+
"grid-template-areas",
|
|
28
|
+
"grid-auto-columns",
|
|
29
|
+
"grid-auto-rows",
|
|
30
|
+
"grid-auto-flow",
|
|
31
|
+
"grid-column",
|
|
32
|
+
"grid-column-start",
|
|
33
|
+
"grid-column-end",
|
|
34
|
+
"grid-row",
|
|
35
|
+
"grid-row-start",
|
|
36
|
+
"grid-row-end",
|
|
37
|
+
"grid-area",
|
|
38
|
+
"grid-gap",
|
|
39
|
+
"width",
|
|
40
|
+
"min-width",
|
|
41
|
+
"max-width",
|
|
42
|
+
"height",
|
|
43
|
+
"min-height",
|
|
44
|
+
"max-height",
|
|
45
|
+
"aspect-ratio",
|
|
46
|
+
"margin",
|
|
47
|
+
"margin-top",
|
|
48
|
+
"margin-right",
|
|
49
|
+
"margin-bottom",
|
|
50
|
+
"margin-left",
|
|
51
|
+
"margin-inline",
|
|
52
|
+
"margin-inline-start",
|
|
53
|
+
"margin-inline-end",
|
|
54
|
+
"margin-block",
|
|
55
|
+
"margin-block-start",
|
|
56
|
+
"margin-block-end",
|
|
57
|
+
"padding",
|
|
58
|
+
"padding-top",
|
|
59
|
+
"padding-right",
|
|
60
|
+
"padding-bottom",
|
|
61
|
+
"padding-left",
|
|
62
|
+
"padding-inline",
|
|
63
|
+
"padding-inline-start",
|
|
64
|
+
"padding-inline-end",
|
|
65
|
+
"padding-block",
|
|
66
|
+
"padding-block-start",
|
|
67
|
+
"padding-block-end",
|
|
68
|
+
"overflow",
|
|
69
|
+
"overflow-x",
|
|
70
|
+
"overflow-y",
|
|
71
|
+
"overflow-wrap",
|
|
72
|
+
"text-overflow",
|
|
73
|
+
"white-space",
|
|
74
|
+
"box-sizing",
|
|
75
|
+
"object-fit",
|
|
76
|
+
"object-position",
|
|
77
|
+
"transform",
|
|
78
|
+
"translate",
|
|
79
|
+
"scale",
|
|
80
|
+
"rotate",
|
|
81
|
+
"order",
|
|
82
|
+
"visibility",
|
|
83
|
+
"float",
|
|
84
|
+
"clear",
|
|
85
|
+
"inset",
|
|
86
|
+
"inset-block",
|
|
87
|
+
"inset-block-start",
|
|
88
|
+
"inset-block-end",
|
|
89
|
+
"inset-inline",
|
|
90
|
+
"inset-inline-start",
|
|
91
|
+
"inset-inline-end",
|
|
92
|
+
"contain",
|
|
93
|
+
"contain-intrinsic-size",
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
const isLayoutProp = (prop) => LAYOUT_PROPS.has(prop);
|
|
97
|
+
|
|
98
|
+
const UNRESOLVED_VALUE_RE = /var\(|url\(|@import|calc\(\s*var\(/i;
|
|
99
|
+
|
|
100
|
+
const cssTextToStyles = (text) => {
|
|
101
|
+
const css = {};
|
|
102
|
+
const unresolved = [];
|
|
103
|
+
if (typeof text !== "string") return { css, unresolved };
|
|
104
|
+
const clean = text.replace(/\/\*[\s\S]*?\*\//g, "");
|
|
105
|
+
for (const decl of clean.split(";")) {
|
|
106
|
+
const idx = decl.indexOf(":");
|
|
107
|
+
if (idx < 0) continue;
|
|
108
|
+
const prop = decl.slice(0, idx).trim();
|
|
109
|
+
const value = decl.slice(idx + 1).trim();
|
|
110
|
+
if (!prop || !value) continue;
|
|
111
|
+
if (prop.startsWith("@") || prop.startsWith("$") || value.includes("{")) continue;
|
|
112
|
+
if (!/^[a-zA-Z-]+$/.test(prop)) continue;
|
|
113
|
+
const p = prop.toLowerCase();
|
|
114
|
+
if (UNRESOLVED_VALUE_RE.test(value)) {
|
|
115
|
+
unresolved.push(`${p}: ${value}`);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (!Object.prototype.hasOwnProperty.call(css, p)) css[p] = [];
|
|
119
|
+
if (!css[p].includes(value)) css[p].push(value);
|
|
120
|
+
}
|
|
121
|
+
return { css, unresolved };
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const camelToKebab = (name) => String(name).replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
|
|
125
|
+
|
|
126
|
+
const styleObjectToText = (obj) => {
|
|
127
|
+
const parts = [];
|
|
128
|
+
for (const [k, v] of entries(obj || {})) {
|
|
129
|
+
if (v === undefined || v === null) continue;
|
|
130
|
+
const val = Array.isArray(v) ? v.join(", ") : String(v);
|
|
131
|
+
parts.push(`${camelToKebab(k)}: ${val}`);
|
|
132
|
+
}
|
|
133
|
+
return parts.join("; ");
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const mergeStyles = (a, b) => {
|
|
137
|
+
const out = {};
|
|
138
|
+
for (const [prop, values] of entries(a)) out[prop] = [...values];
|
|
139
|
+
for (const [prop, values] of entries(b)) {
|
|
140
|
+
if (!Object.prototype.hasOwnProperty.call(out, prop)) {
|
|
141
|
+
out[prop] = [...values];
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
for (const v of values) {
|
|
145
|
+
if (!out[prop].includes(v)) out[prop].push(v);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return out;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const mergeProfiles = (a, b) => ({
|
|
152
|
+
css: mergeStyles(a.css, b.css),
|
|
153
|
+
unresolved: [...a.unresolved, ...b.unresolved],
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const stylesSimilarity = (a, b, label) => {
|
|
157
|
+
const A = new Set(keys(a));
|
|
158
|
+
const B = new Set(keys(b));
|
|
159
|
+
const inter = [...A].filter((p) => B.has(p));
|
|
160
|
+
const union = A.size + B.size - inter.length;
|
|
161
|
+
const score = union === 0 ? 0 : inter.length / union;
|
|
162
|
+
return score;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
module.exports = {
|
|
166
|
+
LAYOUT_PROPS,
|
|
167
|
+
isLayoutProp,
|
|
168
|
+
cssTextToStyles,
|
|
169
|
+
camelToKebab,
|
|
170
|
+
styleObjectToText,
|
|
171
|
+
mergeStyles,
|
|
172
|
+
mergeProfiles,
|
|
173
|
+
stylesSimilarity,
|
|
174
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@md-code/component-uniqueness",
|
|
3
|
+
"version": "0.1.0",
|
|
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
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"eslint",
|
|
8
|
+
"eslintplugin",
|
|
9
|
+
"eslint-plugin",
|
|
10
|
+
"duplicate-components",
|
|
11
|
+
"design-system"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/DrBoria/component-uniqueness.git"
|
|
16
|
+
},
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=18"
|
|
19
|
+
},
|
|
20
|
+
"main": "index.js",
|
|
21
|
+
"bin": {
|
|
22
|
+
"component-uniqueness": "main.js"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"index.js",
|
|
26
|
+
"plugin.js",
|
|
27
|
+
"config.js",
|
|
28
|
+
"main.js",
|
|
29
|
+
"utils.js",
|
|
30
|
+
"decision-maker.js",
|
|
31
|
+
"report.js",
|
|
32
|
+
"normalize/",
|
|
33
|
+
"matcher/",
|
|
34
|
+
"filter/",
|
|
35
|
+
"playgrounds/",
|
|
36
|
+
"README.md"
|
|
37
|
+
],
|
|
38
|
+
"exports": {
|
|
39
|
+
".": "./index.js",
|
|
40
|
+
"./plugin": "./plugin.js",
|
|
41
|
+
"./config": "./config.js",
|
|
42
|
+
"./report": "./report.js",
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"jsdom": "^29.1.1",
|
|
47
|
+
"minimatch": "^9.0.4",
|
|
48
|
+
"remeda": "^2.50.0",
|
|
49
|
+
"tailwindcss": "^4.3.3",
|
|
50
|
+
"typescript": ">=5.5.0 <7"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"gen:catalog": "node main.js"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"eslint": ">=8.40.0"
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { normalizeOptions } = require("../config.js");
|
|
4
|
+
const { buildStylesheet } = require("./style-entrypoint.js");
|
|
5
|
+
|
|
6
|
+
const PLAYGROUNDS = {
|
|
7
|
+
react: () => require("./react.js"),
|
|
8
|
+
angular: () => require("./angular.js"),
|
|
9
|
+
vue: () => require("./vue.js"),
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const cache = new Map();
|
|
13
|
+
|
|
14
|
+
const getPlayground = (framework) => {
|
|
15
|
+
const key = String(framework || "react").toLowerCase();
|
|
16
|
+
if (!cache.has(key)) {
|
|
17
|
+
const loader = PLAYGROUNDS[key] || PLAYGROUNDS.react;
|
|
18
|
+
cache.set(key, loader());
|
|
19
|
+
}
|
|
20
|
+
return cache.get(key);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const getFramework = (opts) => normalizeOptions(opts).framework;
|
|
24
|
+
|
|
25
|
+
const parseComponents = (fileAbs, canonicalNames, opts) => {
|
|
26
|
+
const pg = getPlayground(getFramework(opts));
|
|
27
|
+
if (!pg.available || !pg.available()) return [];
|
|
28
|
+
return pg.parseComponents(fileAbs, canonicalNames, opts);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const parseElements = (fileAbs, canonicalNames, opts) => {
|
|
32
|
+
const pg = getPlayground(getFramework(opts));
|
|
33
|
+
if (!pg.available || !pg.available() || !pg.parseElements) return [];
|
|
34
|
+
return pg.parseElements(fileAbs, canonicalNames, opts);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const parseParts = (fileAbs, canonicalNames, opts) => {
|
|
38
|
+
const pg = getPlayground(getFramework(opts));
|
|
39
|
+
if (!pg.available || !pg.available() || !pg.parseParts) return [];
|
|
40
|
+
return pg.parseParts(fileAbs, canonicalNames, opts);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const resolveCss = (tree, opts) => {
|
|
44
|
+
const pg = getPlayground(getFramework(opts));
|
|
45
|
+
if (!pg.resolveCss) return { css: "", unresolved: [] };
|
|
46
|
+
return pg.resolveCss(tree);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const classTokensOf = (tree, out = new Set()) => {
|
|
50
|
+
if (!tree) return out;
|
|
51
|
+
if (tree.className) for (const token of String(tree.className).split(/\s+/)) if (token) out.add(token);
|
|
52
|
+
for (const child of tree.children || []) classTokensOf(child, out);
|
|
53
|
+
return out;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const render = (tree, opts) => {
|
|
57
|
+
const pg = getPlayground(getFramework(opts));
|
|
58
|
+
if (!pg.render) return { tree: null, available: false };
|
|
59
|
+
const stylesheet = buildStylesheet([...classTokensOf(tree)], opts);
|
|
60
|
+
return pg.render(tree, { stylesheet });
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
module.exports = {
|
|
64
|
+
getPlayground,
|
|
65
|
+
getFramework,
|
|
66
|
+
parseComponents,
|
|
67
|
+
parseElements,
|
|
68
|
+
parseParts,
|
|
69
|
+
resolveCss,
|
|
70
|
+
render,
|
|
71
|
+
};
|