@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.
Files changed (188) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +202 -0
  3. package/README.md +21 -3
  4. package/dist/config.d.ts +50 -0
  5. package/dist/config.js +402 -0
  6. package/dist/constants.d.ts +55 -0
  7. package/dist/constants.js +86 -0
  8. package/dist/decision-maker.d.ts +39 -0
  9. package/dist/decision-maker.js +119 -0
  10. package/dist/filter/child-element.d.ts +3 -0
  11. package/dist/filter/child-element.js +17 -0
  12. package/dist/filter/missing-component.d.ts +10 -0
  13. package/dist/filter/missing-component.js +96 -0
  14. package/dist/filter/no-structure.d.ts +3 -0
  15. package/dist/filter/no-structure.js +16 -0
  16. package/dist/filter/usages.d.ts +3 -0
  17. package/dist/filter/usages.js +14 -0
  18. package/dist/index.d.ts +105 -0
  19. package/dist/index.js +244 -0
  20. package/dist/lib-registry.d.ts +4 -0
  21. package/dist/lib-registry.js +376 -0
  22. package/dist/logger.d.ts +7 -0
  23. package/dist/logger.js +70 -0
  24. package/dist/main.d.ts +25 -0
  25. package/dist/main.js +666 -0
  26. package/dist/matcher/a11y.d.ts +19 -0
  27. package/dist/matcher/a11y.js +311 -0
  28. package/dist/matcher/behavior.d.ts +10 -0
  29. package/dist/matcher/behavior.js +32 -0
  30. package/dist/matcher/css-matching.d.ts +18 -0
  31. package/dist/matcher/css-matching.js +159 -0
  32. package/dist/matcher/framework/index.d.ts +2 -0
  33. package/dist/matcher/framework/index.js +37 -0
  34. package/dist/matcher/framework/react.d.ts +12 -0
  35. package/dist/matcher/framework/react.js +70 -0
  36. package/dist/matcher/index.d.ts +64 -0
  37. package/dist/matcher/index.js +38 -0
  38. package/dist/matcher/name.d.ts +15 -0
  39. package/dist/matcher/name.js +154 -0
  40. package/dist/matcher/structure.d.ts +22 -0
  41. package/dist/matcher/structure.js +329 -0
  42. package/dist/matcher/styles.d.ts +20 -0
  43. package/dist/matcher/styles.js +49 -0
  44. package/dist/normalize/candidate.d.ts +3 -0
  45. package/dist/normalize/candidate.js +21 -0
  46. package/dist/normalize/dom.d.ts +22 -0
  47. package/dist/normalize/dom.js +180 -0
  48. package/dist/normalize/external-html.d.ts +5 -0
  49. package/dist/normalize/external-html.js +40 -0
  50. package/dist/normalize/framework.d.ts +20 -0
  51. package/dist/normalize/framework.js +130 -0
  52. package/dist/normalize/resolver.d.ts +7 -0
  53. package/dist/normalize/resolver.js +28 -0
  54. package/dist/normalize/style-resolver.d.ts +7 -0
  55. package/dist/normalize/style-resolver.js +28 -0
  56. package/dist/normalize/style.d.ts +15 -0
  57. package/dist/normalize/style.js +174 -0
  58. package/dist/normalize/words.d.ts +3 -0
  59. package/dist/normalize/words.js +40 -0
  60. package/dist/package.json +88 -0
  61. package/dist/playgrounds/angular.d.ts +7 -0
  62. package/dist/playgrounds/angular.js +9 -0
  63. package/dist/playgrounds/framework-entrypoint.d.ts +10 -0
  64. package/dist/playgrounds/framework-entrypoint.js +102 -0
  65. package/dist/playgrounds/framework.d.ts +10 -0
  66. package/dist/playgrounds/framework.js +102 -0
  67. package/dist/playgrounds/react.d.ts +14 -0
  68. package/dist/playgrounds/react.js +1288 -0
  69. package/dist/playgrounds/scss.d.ts +6 -0
  70. package/dist/playgrounds/scss.js +9 -0
  71. package/dist/playgrounds/style-entrypoint.d.ts +13 -0
  72. package/dist/playgrounds/style-entrypoint.js +82 -0
  73. package/dist/playgrounds/style.d.ts +13 -0
  74. package/dist/playgrounds/style.js +82 -0
  75. package/dist/playgrounds/styled-components.d.ts +7 -0
  76. package/dist/playgrounds/styled-components.js +190 -0
  77. package/dist/playgrounds/tailwind.d.ts +15 -0
  78. package/dist/playgrounds/tailwind.js +256 -0
  79. package/dist/playgrounds/vue.d.ts +7 -0
  80. package/dist/playgrounds/vue.js +9 -0
  81. package/dist/report/asks.d.ts +27 -0
  82. package/dist/report/asks.js +28 -0
  83. package/dist/report/cli-report.d.ts +7 -0
  84. package/dist/report/cli-report.js +187 -0
  85. package/dist/report/codes/index.d.ts +37 -0
  86. package/dist/report/codes/index.js +31 -0
  87. package/dist/report/errorTypes/index.d.ts +92 -0
  88. package/dist/report/errorTypes/index.js +186 -0
  89. package/dist/report/evidence.d.ts +2 -0
  90. package/dist/report/evidence.js +8 -0
  91. package/dist/report/explain.d.ts +27 -0
  92. package/dist/report/explain.js +76 -0
  93. package/dist/report/index.d.ts +2 -0
  94. package/dist/report/index.js +24 -0
  95. package/dist/report/matchers/a11y/model.d.ts +20 -0
  96. package/dist/report/matchers/a11y/model.js +14 -0
  97. package/dist/report/matchers/a11y/text.d.ts +2 -0
  98. package/dist/report/matchers/a11y/text.js +22 -0
  99. package/dist/report/matchers/behavior/model.d.ts +20 -0
  100. package/dist/report/matchers/behavior/model.js +12 -0
  101. package/dist/report/matchers/behavior/text.d.ts +2 -0
  102. package/dist/report/matchers/behavior/text.js +22 -0
  103. package/dist/report/matchers/framework/model.d.ts +10 -0
  104. package/dist/report/matchers/framework/model.js +39 -0
  105. package/dist/report/matchers/framework/text.d.ts +2 -0
  106. package/dist/report/matchers/framework/text.js +26 -0
  107. package/dist/report/matchers/index.d.ts +5 -0
  108. package/dist/report/matchers/index.js +97 -0
  109. package/dist/report/matchers/name/model.d.ts +20 -0
  110. package/dist/report/matchers/name/model.js +30 -0
  111. package/dist/report/matchers/name/text.d.ts +2 -0
  112. package/dist/report/matchers/name/text.js +17 -0
  113. package/dist/report/matchers/set-model.d.ts +31 -0
  114. package/dist/report/matchers/set-model.js +42 -0
  115. package/dist/report/matchers/structure/model.d.ts +10 -0
  116. package/dist/report/matchers/structure/model.js +58 -0
  117. package/dist/report/matchers/structure/text.d.ts +2 -0
  118. package/dist/report/matchers/structure/text.js +40 -0
  119. package/dist/report/matchers/styles/model.d.ts +11 -0
  120. package/dist/report/matchers/styles/model.js +36 -0
  121. package/dist/report/matchers/styles/text.d.ts +2 -0
  122. package/dist/report/matchers/styles/text.js +40 -0
  123. package/dist/report/user-report/build/bundle.d.ts +3 -0
  124. package/dist/report/user-report/build/bundle.js +185 -0
  125. package/dist/report/user-report/build/bundle.prebuilt.js +955 -0
  126. package/dist/report/user-report/data/collect.d.ts +68 -0
  127. package/dist/report/user-report/data/collect.js +150 -0
  128. package/dist/report/user-report/data/folders.d.ts +3 -0
  129. package/dist/report/user-report/data/folders.js +73 -0
  130. package/dist/report/user-report/data/groups.d.ts +13 -0
  131. package/dist/report/user-report/data/groups.js +76 -0
  132. package/dist/report/user-report/data/links.d.ts +39 -0
  133. package/dist/report/user-report/data/links.js +222 -0
  134. package/dist/report/user-report/data/snapshot.d.ts +9 -0
  135. package/dist/report/user-report/data/snapshot.js +180 -0
  136. package/dist/report/user-report/data/source.d.ts +13 -0
  137. package/dist/report/user-report/data/source.js +58 -0
  138. package/dist/report/user-report/data/word.d.ts +3 -0
  139. package/dist/report/user-report/data/word.js +32 -0
  140. package/dist/report/user-report/html/shell.d.ts +2 -0
  141. package/dist/report/user-report/html/shell.js +27 -0
  142. package/dist/report/user-report/index.d.ts +7 -0
  143. package/dist/report/user-report/index.js +47 -0
  144. package/dist/report/vocabulary/evidence.d.ts +48 -0
  145. package/dist/report/vocabulary/evidence.js +66 -0
  146. package/dist/report/vocabulary/format.d.ts +4 -0
  147. package/dist/report/vocabulary/format.js +20 -0
  148. package/dist/report/vocabulary/labels.d.ts +134 -0
  149. package/dist/report/vocabulary/labels.js +87 -0
  150. package/dist/report/vocabulary/phrases.d.ts +20 -0
  151. package/dist/report/vocabulary/phrases.js +34 -0
  152. package/dist/self-catalog.d.ts +4 -0
  153. package/dist/self-catalog.js +179 -0
  154. package/dist/utils.d.ts +6 -0
  155. package/dist/utils.js +125 -0
  156. package/package.json +86 -60
  157. package/config.js +0 -362
  158. package/decision-maker.js +0 -109
  159. package/filter/drop-child-element.js +0 -13
  160. package/filter/drop-usages.js +0 -13
  161. package/filter/missing-component.js +0 -68
  162. package/index.js +0 -184
  163. package/logger.js +0 -44
  164. package/main.js +0 -539
  165. package/matcher/a11y.js +0 -65
  166. package/matcher/behavior.js +0 -31
  167. package/matcher/framework/index.js +0 -15
  168. package/matcher/framework/react.js +0 -25
  169. package/matcher/index.js +0 -36
  170. package/matcher/name.js +0 -132
  171. package/matcher/structure.js +0 -208
  172. package/normalize/candidate.js +0 -22
  173. package/normalize/dom.js +0 -167
  174. package/normalize/framework.js +0 -129
  175. package/normalize/style-resolver.js +0 -24
  176. package/normalize/style.js +0 -175
  177. package/normalize/words.js +0 -40
  178. package/playgrounds/angular.js +0 -11
  179. package/playgrounds/framework-entrypoint.js +0 -90
  180. package/playgrounds/react.js +0 -997
  181. package/playgrounds/scss.js +0 -7
  182. package/playgrounds/style-entrypoint.js +0 -78
  183. package/playgrounds/styled-components.js +0 -138
  184. package/playgrounds/tailwind.js +0 -186
  185. package/playgrounds/vue.js +0 -11
  186. package/plugin.js +0 -3
  187. package/report.js +0 -159
  188. package/utils.js +0 -92
package/config.js DELETED
@@ -1,362 +0,0 @@
1
- "use strict";
2
-
3
- const fs = require("node:fs");
4
- const path = require("node:path");
5
- const { minimatch } = require("minimatch");
6
-
7
- const DEFAULT_EXCLUDE = [
8
- "*.stories.*",
9
- "*.test.*",
10
- "*.spec.*",
11
- "*.d.ts",
12
- "**/__tests__/**",
13
- ];
14
- const DEFAULT_EXTS = [".tsx", ".ts", ".jsx", ".js"];
15
- const DEFAULT_IGNORE_DIRS = ["node_modules", "dist", "build"];
16
- const DEFAULT_ROOT_MARKERS = [".git"];
17
-
18
- const findRepoRoot = (startDir, markers = DEFAULT_ROOT_MARKERS) => {
19
- const markerSet = new Set(markers);
20
- let dir = path.resolve(startDir);
21
- for (;;) {
22
- if (
23
- fs.existsSync(path.join(dir, "package.json")) &&
24
- fs.readdirSync(dir).some((name) => markerSet.has(name))
25
- ) {
26
- return dir;
27
- }
28
- const parent = path.dirname(dir);
29
- if (parent === dir) return null;
30
- dir = parent;
31
- }
32
- };
33
-
34
- const ACTION_ATTR_NAMES = new Set([
35
- "onClick",
36
- "onMouseDown",
37
- "onMouseUp",
38
- "onMouseEnter",
39
- "onMouseLeave",
40
- "onMouseMove",
41
- "onTouchStart",
42
- "onTouchEnd",
43
- "onTouchMove",
44
- "onFocus",
45
- "onBlur",
46
- "onChange",
47
- "onInput",
48
- "onSubmit",
49
- "onKeyDown",
50
- "onKeyUp",
51
- "onKeyPress",
52
- "onDragStart",
53
- "onDragEnd",
54
- "onDrop",
55
- "onDragOver",
56
- "onDoubleClick",
57
- "onContextMenu",
58
- ]);
59
-
60
- const SUPPORTED_FRAMEWORKS = new Set(["react", "angular", "vue"]);
61
-
62
- const resolveFramework = (f) => {
63
- const s = String(f || "react").trim().toLowerCase();
64
- return SUPPORTED_FRAMEWORKS.has(s) ? s : "react";
65
- };
66
-
67
- const SUPPORTED_STYLE_SYSTEMS = new Set(["tailwind", "scss", "styled-components"]);
68
-
69
- const resolveStyleSystem = (s) => {
70
- const k = String(s || "tailwind").trim().toLowerCase();
71
- return SUPPORTED_STYLE_SYSTEMS.has(k) ? k : "tailwind";
72
- };
73
-
74
- const normalizeFolders = (folders) => {
75
- return (folders || []).map((f) => {
76
- const raw = f && typeof f === "object" ? f.path : f;
77
- let s = String(raw).trim().split(path.sep).join("/");
78
- while (s.startsWith("./")) s = s.slice(2);
79
- if (!s.endsWith("/")) s += "/";
80
- return s;
81
- });
82
- };
83
-
84
- const normalizeOptions = (opts) => {
85
- const o = opts && typeof opts === "object" ? opts : {};
86
- const rootMarkers = o.rootMarkers || DEFAULT_ROOT_MARKERS;
87
- const root = o.root || findRepoRoot(process.cwd(), rootMarkers);
88
-
89
- return {
90
- root,
91
- registry: o.registry || null,
92
- registryPath: o.registryPath || "reports/component-registry.json",
93
- catalog: o.catalog || null,
94
- catalogPath: o.catalogPath || "reports/component-catalog.json",
95
- includeDynamic: o.includeDynamic === true,
96
- dynamicCatalog: o.dynamicCatalog || null,
97
- dynamicCatalogPath: o.dynamicCatalogPath || "reports/component-catalog-dynamic.json",
98
- rawHtml: o.rawHtml === true,
99
- parts: o.parts === true,
100
- framework: resolveFramework(o.framework),
101
- tsconfig: typeof o.tsconfig === "string" ? o.tsconfig : null,
102
- styleSystem: resolveStyleSystem(o.styleSystem),
103
- weights: o.weights && typeof o.weights === "object" ? o.weights : null,
104
- thresholds: o.thresholds && typeof o.thresholds === "object" ? o.thresholds : null,
105
- componentsFolder: normalizeFolders(o.componentsFolder || []),
106
- include: o.include || [],
107
- exclude: o.exclude || DEFAULT_EXCLUDE,
108
- exts: o.exts || DEFAULT_EXTS,
109
- ignoreDirs: o.ignoreDirs ? [...new Set([...DEFAULT_IGNORE_DIRS, ...o.ignoreDirs])] : DEFAULT_IGNORE_DIRS,
110
- rootMarkers,
111
- };
112
- };
113
-
114
- const folderRank = (folders) => {
115
- const map = new Map();
116
- (folders || []).forEach((f, i) => {
117
- const isObj = f && typeof f === "object";
118
- const raw = isObj ? f.path : f;
119
- let s = String(raw).trim().split(path.sep).join("/");
120
- while (s.startsWith("./")) s = s.slice(2);
121
- if (!s.endsWith("/")) s += "/";
122
- const r = isObj && typeof f.rank === "number" ? f.rank : i;
123
- if (!map.has(s)) map.set(s, r);
124
- });
125
- return map;
126
- };
127
-
128
- const inComponentsFolder = (relPath, componentsFolder) => {
129
- return componentsFolder.some((d) => relPath.startsWith(d));
130
- };
131
-
132
- const layerOf = (relPath, componentsFolder) => {
133
- return componentsFolder.findIndex((d) => relPath.startsWith(d));
134
- };
135
-
136
- const canSeeCanon = (appPath, canonPath, componentsFolder) => {
137
- const appLayer = layerOf(appPath, componentsFolder);
138
-
139
- return appLayer < 0 || layerOf(canonPath, componentsFolder) < appLayer;
140
- };
141
-
142
- const inCanonicalDir = (relPath, dirs) => {
143
- return dirs.some((d) => relPath === d || relPath.startsWith(d + "/"));
144
- };
145
-
146
- const compileMatcher = (pattern) => {
147
- if (pattern instanceof RegExp) return (p) => pattern.test(p);
148
- if (typeof pattern !== "string" || pattern.length === 0) return null;
149
- if (pattern.startsWith("/") && pattern.endsWith("/") && pattern.length > 2) {
150
- try {
151
- const re = new RegExp(pattern.slice(1, -1));
152
- return (p) => re.test(p);
153
- } catch {
154
- return null;
155
- }
156
- }
157
- if (pattern.startsWith("^") && pattern.endsWith("$")) {
158
- try {
159
- const re = new RegExp(pattern);
160
- return (p) => re.test(p);
161
- } catch {
162
- return null;
163
- }
164
- }
165
- return (p) => minimatch(p, pattern);
166
- };
167
-
168
- const matchesPath = (m, relPath) => {
169
- if (m(relPath)) return true;
170
- const base = relPath.slice(relPath.lastIndexOf("/") + 1);
171
- return base !== relPath && m(base);
172
- };
173
-
174
- const isIgnored = (relPath, include, exclude) => {
175
- for (const pattern of exclude || []) {
176
- const m = compileMatcher(pattern);
177
- if (m && matchesPath(m, relPath)) return true;
178
- }
179
- if (include && include.length > 0) {
180
- return !include.some((pattern) => {
181
- const m = compileMatcher(pattern);
182
- return m ? matchesPath(m, relPath) : false;
183
- });
184
- }
185
- return false;
186
- };
187
-
188
- const RULE_NAME = "md-code/component-uniqueness";
189
- const RULE_SHORT = "md-code";
190
-
191
- const FLAT_CANDIDATES = ["eslint.config.js", "eslint.config.mjs", "eslint.config.cjs", "eslint.config.ts"];
192
- const LEGACY_CANDIDATES = [".eslintrc.js", ".eslintrc.cjs", ".eslintrc.json", ".eslintrc.yaml", ".eslintrc.yml"];
193
-
194
- const loadModule = (file) => {
195
- const src = fs.readFileSync(file, "utf8");
196
- if (file.endsWith(".json")) return Promise.resolve(JSON.parse(src));
197
- if (/^\s*(exports?\s+(default|const|let|var)|import\s)/m.test(src)) {
198
- const { pathToFileURL } = require("node:url");
199
- return import(pathToFileURL(file).href).catch((esmErr) => {
200
- try {
201
- return require(file);
202
- } catch {
203
- throw esmErr;
204
- }
205
- });
206
- }
207
- return Promise.resolve().then(() => require(file));
208
- };
209
-
210
- const pickOptions = (v) => {
211
- if (v === undefined || v === null) return null;
212
- if (Array.isArray(v)) return v[1] && typeof v[1] === "object" ? v[1] : {};
213
- if (typeof v === "object") return v;
214
- return {};
215
- };
216
-
217
- const isRuleKey = (key) => key === RULE_NAME || key === RULE_SHORT || key === "component-uniqueness" || key.endsWith("/component-uniqueness");
218
-
219
- const ruleValue = (rules) => {
220
- for (const [key, v] of Object.entries(rules)) {
221
- if (!isRuleKey(key)) continue;
222
- if (v === undefined || v === null) continue;
223
- return v;
224
- }
225
- return null;
226
- };
227
-
228
- const loadFlatOptions = async (file) => {
229
- const mod = await loadModule(file);
230
- const list = mod && mod.default !== undefined ? mod.default : mod;
231
- if (!Array.isArray(list)) return null;
232
- for (const entry of list) {
233
- if (!entry || typeof entry !== "object" || !entry.rules) continue;
234
- const v = ruleValue(entry.rules);
235
- if (v === undefined || v === null) continue;
236
- return pickOptions(v);
237
- }
238
- return null;
239
- };
240
-
241
- const loadLegacyOptions = async (file) => {
242
- const mod = await loadModule(file);
243
- const v = ruleValue((mod && mod.rules) || {});
244
- return v === undefined || v === null ? null : pickOptions(v);
245
- };
246
-
247
- const loadRuleOptions = async (dir, explicitFile = null) => {
248
- if (explicitFile) {
249
- const file = path.isAbsolute(explicitFile) ? explicitFile : path.resolve(dir, explicitFile);
250
- if (!fs.existsSync(file)) return null;
251
- if (file.endsWith(".json")) {
252
- const options = pickOptions(JSON.parse(fs.readFileSync(file, "utf8")));
253
- return options ? { options, source: file } : null;
254
- }
255
- try {
256
- const options = file.startsWith("eslint.config") ? await loadFlatOptions(file) : pickOptions(await loadModule(file));
257
- return options ? { options, source: file } : null;
258
- } catch {
259
- return null;
260
- }
261
- }
262
- let d = dir;
263
- for (;;) {
264
- const standalone = path.join(d, "md-code-component-uniqueness.config.js");
265
- if (fs.existsSync(standalone)) {
266
- const mod = await loadModule(standalone);
267
- const options = mod && mod.default !== undefined ? mod.default : mod;
268
- if (options && typeof options === "object") return { options, source: standalone };
269
- }
270
- for (const name of FLAT_CANDIDATES) {
271
- const file = path.join(d, name);
272
- if (!fs.existsSync(file)) continue;
273
- try {
274
- const options = await loadFlatOptions(file);
275
- if (options) return { options, source: file };
276
- } catch {}
277
- }
278
- for (const name of LEGACY_CANDIDATES) {
279
- const file = path.join(d, name);
280
- if (!fs.existsSync(file)) continue;
281
- try {
282
- const options = await loadLegacyOptions(file);
283
- if (options) return { options, source: file };
284
- } catch {}
285
- }
286
- const parent = path.dirname(d);
287
- if (parent === d) break;
288
- d = parent;
289
- }
290
- return null;
291
- };
292
-
293
- const jsonCache = new Map();
294
-
295
- const loadJson = (absPath, label) => {
296
- if (jsonCache.has(absPath)) return jsonCache.get(absPath).value;
297
-
298
- let entry = { loaded: false, value: null, warned: false };
299
- try {
300
- const raw = fs.readFileSync(absPath, "utf8");
301
- const parsed = JSON.parse(raw);
302
- entry.value = parsed && typeof parsed === "object" ? parsed : null;
303
- entry.loaded = true;
304
- } catch (err) {
305
- entry.value = null;
306
- entry.loaded = false;
307
- const reason = err && err.code === "ENOENT" ? "file not found" : `could not be read/parsed (${err && err.message ? err.message : err})`;
308
- if (!entry.warned) {
309
- entry.warned = true;
310
- const rel = path.isAbsolute(absPath) ? absPath : path.resolve(absPath);
311
- console.warn(
312
- `[component-uniqueness] ${label} ${reason} at ${rel}. ` +
313
- `The ${label} is disabled for this run. ` +
314
- `Generate it first (see the package README) or pass it via the rule options.`
315
- );
316
- }
317
- }
318
-
319
- jsonCache.set(absPath, entry);
320
- return entry.value;
321
- };
322
-
323
- const resolveAgainst = (p, root) => (path.isAbsolute(p) ? p : path.join(root, p));
324
-
325
- const loadRegistry = (config) => {
326
- if (config.registry) return config.registry;
327
- return loadJson(resolveAgainst(config.registryPath, config.root), "component registry");
328
- };
329
-
330
- const loadCatalog = (config) => {
331
- if (config.catalog) return config.catalog;
332
- return loadJson(resolveAgainst(config.catalogPath, config.root), "component catalog");
333
- };
334
-
335
- const loadDynamicCatalog = (config) => {
336
- if (config.dynamicCatalog) return config.dynamicCatalog;
337
- return loadJson(resolveAgainst(config.dynamicCatalogPath, config.root), "dynamic component catalog");
338
- };
339
-
340
- module.exports = {
341
- DEFAULT_EXCLUDE,
342
- DEFAULT_EXTS,
343
- DEFAULT_IGNORE_DIRS,
344
- DEFAULT_ROOT_MARKERS,
345
- ACTION_ATTR_NAMES,
346
- findRepoRoot,
347
- normalizeOptions,
348
- normalizeFolders,
349
- inComponentsFolder,
350
- canSeeCanon,
351
- inCanonicalDir,
352
- isIgnored,
353
- folderRank,
354
- loadRuleOptions,
355
- RULE_NAME,
356
- RULE_SHORT,
357
- loadJson,
358
- resolveAgainst,
359
- loadRegistry,
360
- loadCatalog,
361
- loadDynamicCatalog,
362
- };
package/decision-maker.js DELETED
@@ -1,109 +0,0 @@
1
- "use strict";
2
-
3
- const { tokensOf } = require("./matcher/a11y");
4
- const { NON_SEMANTIC_TAGS } = require("./normalize/dom.js");
5
-
6
- const DEFAULT_WEIGHTS = {
7
- name: 0.10,
8
- structure: 0.5,
9
- behavior: 0.2,
10
- a11y: 0.1,
11
- framework: 0.15,
12
- };
13
-
14
- const DEFAULT_THRESHOLDS = {
15
- duplicate: 0.4,
16
- similar: 0.2,
17
- };
18
-
19
- const clamp01 = (x) => Math.max(0, Math.min(1, x));
20
-
21
- const resolveWeights = (opts) => {
22
- const w = (opts && opts.weights) || {};
23
- return {
24
- name: w.name ?? DEFAULT_WEIGHTS.name,
25
- structure: w.structure ?? DEFAULT_WEIGHTS.structure,
26
- behavior: w.behavior ?? DEFAULT_WEIGHTS.behavior,
27
- a11y: w.a11y ?? DEFAULT_WEIGHTS.a11y,
28
- framework: w.framework ?? DEFAULT_WEIGHTS.framework,
29
- };
30
- };
31
-
32
- const resolveThresholds = (opts) => {
33
- const t = (opts && opts.thresholds) || {};
34
- return {
35
- duplicate: t.duplicate ?? DEFAULT_THRESHOLDS.duplicate,
36
- similar: t.similar ?? DEFAULT_THRESHOLDS.similar,
37
- };
38
- };
39
-
40
- const applicableOf = (candidate) => ({
41
- name: false,
42
- behavior: candidate.framework.events.length > 0,
43
- a11y: tokensOf(candidate, "a11y").size > 0,
44
- structure: !!candidate.dom.tree && (Object.keys(candidate.dom.tree.css).length > 0 || !NON_SEMANTIC_TAGS.has(candidate.dom.tree.tag)),
45
- framework: candidate.framework.props.length > 0 || candidate.framework.events.length > 0,
46
- });
47
-
48
- const applicableWeights = (weights, applicable) => Object.fromEntries(Object.entries(weights).map(([key, w]) => [key, applicable[key] === false ? 0 : w]));
49
-
50
- const decide = (candidate, canon, ctx) => {
51
- const o = ctx || {};
52
- const flagged = Object.fromEntries(Object.entries(o.signals || {}).filter(([, s]) => s && s.applicable === false).map(([key]) => [key, false]));
53
- const weights = applicableWeights(resolveWeights(o), { ...(candidate.partial ? applicableOf(candidate) : {}), ...flagged });
54
- const thresholds = resolveThresholds(o);
55
-
56
- const signals = {};
57
- for (const key of ["name", "structure", "behavior", "a11y", "framework"]) {
58
- const s = o.signals && o.signals[key];
59
- signals[key] = { score: s ? clamp01(s.score) : 0, evidence: s && s.evidence ? s.evidence : {} };
60
- }
61
-
62
- const totalW = weights.name + weights.structure + weights.behavior + weights.a11y + weights.framework;
63
- const weighted =
64
- weights.name * signals.name.score +
65
- weights.structure * signals.structure.score +
66
- weights.behavior * signals.behavior.score +
67
- weights.a11y * signals.a11y.score +
68
- weights.framework * signals.framework.score;
69
- const confidence = totalW === 0 ? 0 : clamp01(weighted / totalW);
70
-
71
- let isDuplicate = false;
72
- let tier = null;
73
- if (confidence >= thresholds.duplicate) {
74
- isDuplicate = true;
75
- tier = "duplicate";
76
- } else if (confidence >= thresholds.similar) {
77
- tier = "similar";
78
- }
79
-
80
- const parts = ["name", "structure", "behavior", "a11y", "framework"].map((key) => `${key} ${signals[key].score.toFixed(2)}`);
81
- const reason = `confidence ${confidence.toFixed(2)} (${parts.join(", ")})`;
82
-
83
- return {
84
- isDuplicate,
85
- confidence,
86
- tier,
87
- reason,
88
- signals,
89
- thresholds,
90
- weights,
91
- };
92
- };
93
-
94
- const nodeCount = (node) => (node ? 1 + (node.children || []).reduce((sum, child) => sum + nodeCount(child), 0) : 0);
95
-
96
- const tagCount = (match) => nodeCount(match.canon.dom && match.canon.dom.tree);
97
-
98
- const bestOf = (matches) =>
99
- [...matches].sort((a, b) => b.decision.confidence - a.decision.confidence || tagCount(a) - tagCount(b) || a.canon.name.localeCompare(b.canon.name))[0] || null;
100
-
101
- module.exports = {
102
- decide,
103
- bestOf,
104
- resolveWeights,
105
- resolveThresholds,
106
- clamp01,
107
- DEFAULT_WEIGHTS,
108
- DEFAULT_THRESHOLDS,
109
- };
@@ -1,13 +0,0 @@
1
- "use strict";
2
-
3
- const test = (match) => {
4
- if (match.tier === "name" || match.tier === "name-fuzzy" || match.tier === "jsx-block") return null;
5
- const appEls = (match.app.elementProps || []).length;
6
- const canonEls = (match.canon.elementProps || []).length;
7
- if (canonEls > 0 && appEls > 3 * canonEls) {
8
- return `app has ${appEls} elements vs canonical's ${canonEls} — match is on a child element, not the component itself`;
9
- }
10
- return null;
11
- };
12
-
13
- module.exports = { name: "drop-child-element", test };
@@ -1,13 +0,0 @@
1
- "use strict";
2
-
3
- const test = (match) => {
4
- if ((match.app.usesCanonical || []).includes(match.canon.name)) {
5
- return `${match.app.name} renders ${match.canon.name} (composition, not a duplicate)`;
6
- }
7
- if ((match.canon.usesCanonical || []).includes(match.app.name)) {
8
- return `${match.canon.name} renders ${match.app.name} (composition, not a duplicate)`;
9
- }
10
- return null;
11
- };
12
-
13
- module.exports = { name: "drop-usages", test };
@@ -1,68 +0,0 @@
1
- "use strict";
2
-
3
- const { pascalWords, GENERIC_WORDS } = require("../normalize/words.js");
4
-
5
- const DEFAULT_MIN_CLUSTER = 3;
6
-
7
- const pascalCase = (words) => words.map((w) => w[0].toUpperCase() + w.slice(1).toLowerCase()).join("");
8
-
9
- const suggestName = (appNames, canonName = null) => {
10
- const counts = new Map();
11
- const order = [];
12
- for (const name of appNames) {
13
- for (const w of pascalWords(name)) {
14
- const k = w.toLowerCase();
15
- if (!counts.has(k)) {
16
- counts.set(k, 0);
17
- order.push(k);
18
- }
19
- counts.set(k, counts.get(k) + 1);
20
- }
21
- }
22
- const common = order.filter((k) => counts.get(k) >= 2);
23
- const meaningful = common.filter((k) => !GENERIC_WORDS.has(k));
24
- const base = (meaningful.length > 0 ? meaningful : common).slice(0, 3).map((k) => k[0].toUpperCase() + k.slice(1));
25
- if (base.length > 0) {
26
- const name = pascalCase(base);
27
- if (meaningful.length > 0) return name;
28
- return canonName ? `${name}${canonName}` : name;
29
- }
30
- if (canonName) {
31
- const canonWords = pascalWords(canonName).filter((w) => !GENERIC_WORDS.has(w.toLowerCase()));
32
- if (canonWords.length > 0) {
33
- return pascalCase(["shared", ...canonWords.slice(0, 2)]);
34
- }
35
- return `Shared${canonName}`;
36
- }
37
- return "SharedComponent";
38
- };
39
-
40
- const findMissingComponents = (matches, { minCluster = DEFAULT_MIN_CLUSTER } = {}) => {
41
- const byCanon = new Map();
42
- for (const match of matches) {
43
- const key = match.canon.name;
44
- if (!byCanon.has(key)) byCanon.set(key, []);
45
- byCanon.get(key).push(match);
46
- }
47
- const clusters = [];
48
- const clusteredKeys = new Set();
49
- for (const [canonName, group] of byCanon) {
50
- const distinctApps = new Set(group.map((m) => m.app.name));
51
- if (distinctApps.size < minCluster) continue;
52
- clusteredKeys.add(canonName);
53
- clusters.push({
54
- name: suggestName(group.map((m) => m.app.name), canonName),
55
- canon: group[0].canon,
56
- matches: [...group].sort((a, b) => b.decision.confidence - a.decision.confidence),
57
- });
58
- }
59
- clusters.sort((a, b) => b.matches.length - a.matches.length || a.name.localeCompare(b.name));
60
- const remaining = matches.filter((m) => !clusteredKeys.has(m.canon.name));
61
- return { clusters, remaining };
62
- };
63
-
64
- module.exports = {
65
- findMissingComponents,
66
- suggestName,
67
- DEFAULT_MIN_CLUSTER,
68
- };