@md-code/component-uniqueness 0.1.4 → 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 (189) 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 -71
  157. package/config.js +0 -372
  158. package/decision-maker.js +0 -109
  159. package/filter/drop-child-element.js +0 -14
  160. package/filter/drop-no-structure.js +0 -13
  161. package/filter/drop-usages.js +0 -13
  162. package/filter/missing-component.js +0 -100
  163. package/index.js +0 -184
  164. package/logger.js +0 -44
  165. package/main.js +0 -584
  166. package/matcher/a11y.js +0 -65
  167. package/matcher/behavior.js +0 -31
  168. package/matcher/framework/index.js +0 -15
  169. package/matcher/framework/react.js +0 -25
  170. package/matcher/index.js +0 -36
  171. package/matcher/name.js +0 -132
  172. package/matcher/structure.js +0 -242
  173. package/normalize/candidate.js +0 -22
  174. package/normalize/dom.js +0 -187
  175. package/normalize/framework.js +0 -110
  176. package/normalize/style-resolver.js +0 -24
  177. package/normalize/style.js +0 -175
  178. package/normalize/words.js +0 -40
  179. package/playgrounds/angular.js +0 -11
  180. package/playgrounds/framework-entrypoint.js +0 -90
  181. package/playgrounds/react.js +0 -998
  182. package/playgrounds/scss.js +0 -7
  183. package/playgrounds/style-entrypoint.js +0 -78
  184. package/playgrounds/styled-components.js +0 -138
  185. package/playgrounds/tailwind.js +0 -186
  186. package/playgrounds/vue.js +0 -11
  187. package/plugin.js +0 -3
  188. package/report.js +0 -162
  189. package/utils.js +0 -92
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TIER_LABEL = exports.KIND_TILES = exports.kindOf = exports.KINDS = exports.signalColor = exports.signalOf = exports.SIGNAL_KEYS = exports.SIGNALS = void 0;
4
+ const constants_1 = require("../../constants");
5
+ // The words the report uses for the five comparison groups. A signal is named
6
+ // once here and read back by the terminal report, the HTML page and the log, so
7
+ // "structure 0.80" in a message and the blue bar in the picture are the same
8
+ // thing. The colour is the ONE colour of that signal everywhere.
9
+ const SIGNALS = [
10
+ {
11
+ key: constants_1.SIGNAL.Structure,
12
+ label: "Structure",
13
+ short: "struct",
14
+ color: "#6ea8fe",
15
+ what: "the DOM tree and its styling (tags, CSS, text)",
16
+ },
17
+ {
18
+ key: constants_1.SIGNAL.Styles,
19
+ label: "Styles",
20
+ short: "styles",
21
+ color: "#e879a0",
22
+ what: "the CSS that matters per element: typography for text, layout for containers, offsets when positioned, colors and surfaces",
23
+ },
24
+ {
25
+ key: constants_1.SIGNAL.Behavior,
26
+ label: "Behavior",
27
+ short: "behav",
28
+ color: "#f0a35e",
29
+ what: "the events the component handles (onClick, onChange…)",
30
+ },
31
+ {
32
+ key: constants_1.SIGNAL.A11y,
33
+ label: "Accessibility",
34
+ short: "a11y",
35
+ color: "#56c8a8",
36
+ what: "the ARIA roles / attributes and test ids",
37
+ },
38
+ {
39
+ key: constants_1.SIGNAL.Framework,
40
+ label: "Framework",
41
+ short: "fw",
42
+ color: "#b48ef0",
43
+ what: "the props it takes and the child components it renders",
44
+ },
45
+ {
46
+ key: constants_1.SIGNAL.Name,
47
+ label: "Name",
48
+ short: "name",
49
+ color: "#ffd166",
50
+ what: "the component name and its file basename",
51
+ },
52
+ ];
53
+ exports.SIGNALS = SIGNALS;
54
+ const SIGNAL_KEYS = SIGNALS.map((s) => s.key);
55
+ exports.SIGNAL_KEYS = SIGNAL_KEYS;
56
+ const signalOf = (key) => SIGNALS.find((s) => s.key === key) ?? SIGNALS[0];
57
+ exports.signalOf = signalOf;
58
+ const signalColor = (key) => signalOf(key).color;
59
+ exports.signalColor = signalColor;
60
+ // What each KIND of finding is, its colour, and the repair verb.
61
+ const KINDS = {
62
+ [constants_1.KIND.Duplicate]: { label: "Duplicate", color: "#ff6b6b", action: "use the canonical component" },
63
+ [constants_1.KIND.Similar]: { label: "Similar", color: "#fbbf24", action: "consider unifying" },
64
+ [constants_1.KIND.Missing]: { label: "Missing component", color: "#f0a35e", action: "extract a shared component" },
65
+ [constants_1.KIND.RawHtml]: { label: "Raw HTML", color: "#7fdbca", action: "replace with the canonical component" },
66
+ [constants_1.KIND.Part]: { label: "Copied part", color: "#9aa7ff", action: "reuse the canonical component" },
67
+ [constants_1.KIND.Dropped]: { label: "Filtered out", color: "#6b7788", action: "none — a filter explained it away" },
68
+ };
69
+ exports.KINDS = KINDS;
70
+ const kindOf = (key) => KINDS[key] ?? KINDS[constants_1.KIND.Similar];
71
+ exports.kindOf = kindOf;
72
+ // The same kinds as a COUNTABLE list (a tile says "Duplicates: 12").
73
+ const KIND_TILES = Object.freeze([
74
+ { key: constants_1.KIND.Duplicate, label: "Duplicates" },
75
+ { key: constants_1.KIND.Similar, label: "Similar" },
76
+ { key: constants_1.KIND.Missing, label: "Missing components" },
77
+ { key: constants_1.KIND.RawHtml, label: "Raw HTML" },
78
+ { key: constants_1.KIND.Part, label: "Copied parts" },
79
+ { key: constants_1.KIND.Dropped, label: "Filtered out" },
80
+ ]);
81
+ exports.KIND_TILES = KIND_TILES;
82
+ const TIER_LABEL = {
83
+ [constants_1.TIER.Duplicate]: "High-confidence duplicates (confidence ≥ duplicate threshold)",
84
+ [constants_1.TIER.Similar]: "Weak similarity (similar ≤ confidence < duplicate)",
85
+ none: "Below every threshold",
86
+ };
87
+ exports.TIER_LABEL = TIER_LABEL;
@@ -0,0 +1,20 @@
1
+ declare const fill: (template: any, vars: any) => string;
2
+ declare const PHRASES: {
3
+ duplicate: string;
4
+ similar: string;
5
+ missing: string;
6
+ rawHtml: string;
7
+ part: string;
8
+ headline: string;
9
+ signal: string;
10
+ signalDisagrees: string;
11
+ signalOff: string;
12
+ confidence: string;
13
+ filtered: string;
14
+ fixUseCanonical: string;
15
+ fixExtractComponent: string;
16
+ fixReplaceRawHtml: string;
17
+ fixKeep: string;
18
+ notRead: string;
19
+ };
20
+ export { PHRASES, fill };
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fill = exports.PHRASES = void 0;
4
+ // Every sentence the report prints about a verdict lives here, as a template
5
+ // with `{placeholders}`. A producer states the FACTS (names, paths, numbers);
6
+ // the wording is chosen here, once, so the terminal report and the HTML page
7
+ // cannot drift apart.
8
+ const fill = (template, vars) => String(template).replace(/\{([A-Za-z][A-Za-z0-9_]*)\}/g, (_, key) => vars[key] != null ? String(vars[key]) : `{${key}}`);
9
+ exports.fill = fill;
10
+ const PHRASES = {
11
+ // ── Verdicts ────────────────────────────────────────────────────────────
12
+ duplicate: "{left} duplicates canonical {right} — {confidence} confidence. Use the canonical component instead.",
13
+ similar: "{left} looks like canonical {right}, but not closely enough to call it a copy — {confidence}.",
14
+ missing: "{count} distinct components all matched canonical {right}. A shared component is likely missing — weakest link {confidence}.",
15
+ rawHtml: "Raw <{tag}> outside the canonical packages — replace with {right}.",
16
+ part: "{left} copies a part of canonical {right} — {confidence}. Reuse the canonical component.",
17
+ // ── Why it matched ──────────────────────────────────────────────────────
18
+ headline: "{confidence} confidence — {top} contributes {share} of it.",
19
+ signal: "{label} agrees at {score} ({weight} of the verdict) — it compares {what}.",
20
+ signalDisagrees: "{label} agrees only at {score} ({weight} of the verdict) — it compares {what}.",
21
+ signalOff: "{label} has no evidence for this pair, its weight was redistributed.",
22
+ // ── The number itself ───────────────────────────────────────────────────
23
+ confidence: "confidence {confidence} ({signals})",
24
+ // ── Why a pair was dismissed (the filters) ──────────────────────────────
25
+ filtered: "{left} and {right} were filtered out — {reason}",
26
+ // ── Repairs ─────────────────────────────────────────────────────────────
27
+ fixUseCanonical: "Replace `{left}` with `{right}` from `{path}` — they are the same component.",
28
+ fixExtractComponent: "Extract a shared component for this group and have all {count} use it.",
29
+ fixReplaceRawHtml: "Replace the raw <{tag}> with `{right}` from `{path}`.",
30
+ fixKeep: "No action needed beyond the note.",
31
+ // ── Caveats ─────────────────────────────────────────────────────────────
32
+ notRead: "{path} was not read: {reason}",
33
+ };
34
+ exports.PHRASES = PHRASES;
@@ -0,0 +1,4 @@
1
+ declare const refreshCatalog: (config: any, { budgetMs }?: {
2
+ budgetMs?: number;
3
+ }) => boolean;
4
+ export { refreshCatalog };
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.refreshCatalog = void 0;
27
+ const fs = __importStar(require("node:fs"));
28
+ const os = __importStar(require("node:os"));
29
+ const path = __importStar(require("node:path"));
30
+ const crypto = __importStar(require("node:crypto"));
31
+ const main_1 = require("./main");
32
+ const utils_1 = require("./utils");
33
+ const config_1 = require("./config");
34
+ // Self-maintained catalog: the rule keeps its own catalog fresh by walking the
35
+ // configured componentsFolder on every lint and re-parsing only the files
36
+ // whose mtime+size changed since the last refresh (per-file cache in
37
+ // os.tmpdir()). This is the "cache WHAT to compare against" part — the file
38
+ // being linted is always judged from its live editor buffer by the rule
39
+ // itself, so the disk catalog only ever supplies the OTHER files.
40
+ //
41
+ // Two-pass parsing (same logic as the old external refresher): pass 1 parses
42
+ // changed files with canonNames=null to recover their component names; a
43
+ // file's `usesCanonical` fields depend on the GLOBAL name set, so when that
44
+ // set changes every file's final records are rebuilt (rare — only when a
45
+ // component is added, renamed, or removed).
46
+ //
47
+ // Budget guard: the refresh is capped at `budgetMs`; past it the partial
48
+ // catalog is written as-is (better a slightly stale catalog than a frozen
49
+ // editor). The per-file cache makes the fast path ~0ms and a single changed
50
+ // file ~100-300ms.
51
+ const relPosix = (repoRoot, abs) => path.relative(repoRoot, abs).split(path.sep).join("/");
52
+ const cachePathFor = (root) => {
53
+ const key = crypto.createHash("md5").update(path.resolve(root)).digest("hex");
54
+ return path.join(os.tmpdir(), ".md-code-cache", `components-${key}.json`);
55
+ };
56
+ const readCache = (cachePath) => {
57
+ try {
58
+ const parsed = JSON.parse(fs.readFileSync(cachePath, "utf8"));
59
+ return parsed && typeof parsed === "object" ? parsed : { files: {}, componentsSig: null };
60
+ }
61
+ catch {
62
+ return { files: {}, componentsSig: null };
63
+ }
64
+ };
65
+ // Refresh the on-disk catalog in place. `config` is the normalized rule
66
+ // config (see config.js normalizeOptions). Returns true when the catalog was
67
+ // rewritten (or created), false when nothing changed or the refresh was
68
+ // skipped entirely.
69
+ const refreshCatalog = (config, { budgetMs = 30000 } = {}) => {
70
+ let wrote = false;
71
+ const root = config.root;
72
+ if (!root)
73
+ return false;
74
+ const folders = config.componentsFolder || [];
75
+ if (folders.length === 0)
76
+ return false;
77
+ const catalogPath = path.isAbsolute(config.exportPath) ? config.exportPath : path.join(root, config.exportPath);
78
+ const cachePath = cachePathFor(root);
79
+ const started = Date.now();
80
+ const cache = readCache(cachePath);
81
+ const bucket = (cache.files = cache.files || {});
82
+ const shouldSkip = (abs) => (0, config_1.isIgnored)(relPosix(root, abs), [], config.exclude);
83
+ const files = [];
84
+ for (const relRoot of folders) {
85
+ const dir = (0, config_1.resolveAgainst)(relRoot, root);
86
+ if (!fs.existsSync(dir))
87
+ continue;
88
+ for (const file of (0, utils_1.walk)(dir, config.exts || config_1.DEFAULT_EXTS, config.ignoreDirs || config_1.DEFAULT_IGNORE_DIRS)) {
89
+ if (shouldSkip(file))
90
+ continue;
91
+ files.push({ file, rel: relPosix(root, file) });
92
+ }
93
+ }
94
+ // Pass 1: re-parse changed files with canonNames=null to recover names.
95
+ let reparsed = 0;
96
+ let pruned = 0;
97
+ let budgetHit = false;
98
+ for (const { file, rel } of files) {
99
+ let stat;
100
+ try {
101
+ stat = fs.statSync(file);
102
+ }
103
+ catch {
104
+ continue;
105
+ }
106
+ const prev = bucket[rel];
107
+ if (prev && prev.mtimeMs === stat.mtimeMs && prev.size === stat.size)
108
+ continue;
109
+ if (Date.now() - started > budgetMs) {
110
+ budgetHit = true;
111
+ break;
112
+ }
113
+ try {
114
+ const recs = (0, main_1.parseComponents)([{ file, rel }], null, config);
115
+ bucket[rel] = { mtimeMs: stat.mtimeMs, size: stat.size, names: recs.map((c) => c.name), finalRecords: null };
116
+ reparsed += 1;
117
+ }
118
+ catch (err) {
119
+ // Keep the previous records for a file that fails to parse — a
120
+ // transient error must not silently drop half the catalog.
121
+ if (prev)
122
+ bucket[rel] = prev;
123
+ else
124
+ bucket[rel] = { mtimeMs: stat.mtimeMs, size: stat.size, names: [], finalRecords: [] };
125
+ }
126
+ }
127
+ for (const rel of Object.keys(bucket)) {
128
+ if (!files.some((f) => f.rel === rel)) {
129
+ delete bucket[rel];
130
+ pruned += 1;
131
+ }
132
+ }
133
+ // Pass 2: final records, rebuilt for every file when the global name set
134
+ // changed (a component was added/renamed/removed), otherwise only for
135
+ // files that were reparsed in pass 1.
136
+ const allNames = new Set();
137
+ for (const { rel } of files)
138
+ for (const n of (bucket[rel] || {}).names || [])
139
+ allNames.add(n);
140
+ const canonNames = allNames;
141
+ const newSig = JSON.stringify([...allNames].sort());
142
+ const sigChanged = cache.componentsSig !== newSig;
143
+ for (const { file, rel } of files) {
144
+ const b = bucket[rel];
145
+ if (!b)
146
+ continue;
147
+ if (!sigChanged && b.finalRecords)
148
+ continue;
149
+ if (Date.now() - started > budgetMs) {
150
+ budgetHit = true;
151
+ break;
152
+ }
153
+ b.finalRecords = (0, main_1.parseComponents)([{ file, rel }], canonNames, config);
154
+ }
155
+ cache.componentsSig = newSig;
156
+ const final = files.flatMap(({ rel }) => (bucket[rel] && bucket[rel].finalRecords) || []);
157
+ if (reparsed > 0 || pruned > 0 || !fs.existsSync(catalogPath)) {
158
+ try {
159
+ (0, utils_1.writeCatalog)(catalogPath, (0, utils_1.dedupeAndSort)(final));
160
+ wrote = true;
161
+ }
162
+ catch (err) {
163
+ console.warn(`[component-uniqueness] self-catalog: could not write ${catalogPath}: ${err && err.message ? err.message : err}`);
164
+ return false;
165
+ }
166
+ }
167
+ try {
168
+ fs.mkdirSync(path.dirname(cachePath), { recursive: true });
169
+ fs.writeFileSync(cachePath, JSON.stringify(cache), "utf8");
170
+ }
171
+ catch {
172
+ // Cache write failure is non-fatal: the catalog is already written,
173
+ // the next lint simply re-parses more files.
174
+ }
175
+ if (budgetHit)
176
+ console.warn("[component-uniqueness] self-catalog: budget exhausted, catalog may be partially stale");
177
+ return wrote;
178
+ };
179
+ exports.refreshCatalog = refreshCatalog;
@@ -0,0 +1,6 @@
1
+ declare const walk: (dir: any, exts?: string[], ignoreDirs?: string[]) => any[];
2
+ declare const dedupeAndSort: (components: any) => any[];
3
+ declare const writeCatalog: (outPath: any, components: any) => void;
4
+ declare const buildDirNameMap: (repoRoot: any, registryPath: any) => Map<any, any>;
5
+ declare const componentNameFor: (fileAbs: any, repoRoot: any, dirNameMap: any) => string;
6
+ export { walk, dedupeAndSort, writeCatalog, buildDirNameMap, componentNameFor };
package/dist/utils.js ADDED
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.componentNameFor = exports.buildDirNameMap = exports.writeCatalog = exports.dedupeAndSort = exports.walk = void 0;
27
+ const fs = __importStar(require("node:fs"));
28
+ const path = __importStar(require("node:path"));
29
+ const config_1 = require("./config");
30
+ const walk = (dir, exts = config_1.DEFAULT_EXTS, ignoreDirs = config_1.DEFAULT_IGNORE_DIRS) => {
31
+ const extSet = new Set(exts);
32
+ const ignoreSet = new Set(ignoreDirs);
33
+ const out = [];
34
+ const visit = (d) => {
35
+ let list;
36
+ try {
37
+ list = fs.readdirSync(d, { withFileTypes: true });
38
+ }
39
+ catch {
40
+ return;
41
+ }
42
+ for (const entry of list) {
43
+ if (entry.name.startsWith(".") || ignoreSet.has(entry.name))
44
+ continue;
45
+ const full = path.join(d, entry.name);
46
+ if (entry.isDirectory()) {
47
+ visit(full);
48
+ }
49
+ else if ([...extSet].some((e) => entry.name.endsWith(e))) {
50
+ out.push(full);
51
+ }
52
+ }
53
+ };
54
+ visit(dir);
55
+ return out;
56
+ };
57
+ exports.walk = walk;
58
+ const dedupeAndSort = (components) => {
59
+ const seen = new Set();
60
+ const out = [];
61
+ for (const c of components) {
62
+ const key = JSON.stringify({ n: c.name, ...c });
63
+ if (seen.has(key))
64
+ continue;
65
+ seen.add(key);
66
+ out.push(c);
67
+ }
68
+ out.sort((a, b) => (a.name + a.tag + a.path).localeCompare(b.name + b.tag + b.path));
69
+ return out;
70
+ };
71
+ exports.dedupeAndSort = dedupeAndSort;
72
+ const writeCatalog = (outPath, components) => {
73
+ fs.mkdirSync(path.dirname(outPath), { recursive: true });
74
+ fs.writeFileSync(outPath, JSON.stringify({ generatedAt: new Date().toISOString(), components }, null, 2) + "\n", "utf8");
75
+ };
76
+ exports.writeCatalog = writeCatalog;
77
+ const buildDirNameMap = (repoRoot, registryPath) => {
78
+ const map = new Map();
79
+ const regPath = registryPath || path.join(repoRoot, "reports", "component-registry.json");
80
+ if (!fs.existsSync(regPath))
81
+ return map;
82
+ try {
83
+ const reg = JSON.parse(fs.readFileSync(regPath, "utf8"));
84
+ for (const [name, dirs] of Object.entries((reg && reg.names) || {})) {
85
+ for (const d of dirs) {
86
+ if (!map.has(d))
87
+ map.set(d, name);
88
+ }
89
+ }
90
+ }
91
+ catch { }
92
+ return map;
93
+ };
94
+ exports.buildDirNameMap = buildDirNameMap;
95
+ const componentNameFor = (fileAbs, repoRoot, dirNameMap) => {
96
+ const dir = path.dirname(fileAbs);
97
+ let name = path.basename(fileAbs).replace(/\.[^.]+$/, "");
98
+ const isFileScoped = /^(index|styles|types|constants|utils|hooks|test|stories)$/i.test(name);
99
+ let up = dir;
100
+ if (/^[A-Z][A-Za-z0-9]*$/.test(path.basename(dir)) && isFileScoped) {
101
+ name = path.basename(dir);
102
+ }
103
+ else {
104
+ for (let i = 0; i < 4; i += 1) {
105
+ const parent = path.dirname(up);
106
+ if (parent === up)
107
+ break;
108
+ const bn = path.basename(parent);
109
+ if (/^[A-Z][A-Za-z0-9]*$/.test(bn)) {
110
+ up = parent;
111
+ if (isFileScoped)
112
+ name = bn;
113
+ break;
114
+ }
115
+ up = parent;
116
+ }
117
+ }
118
+ const relDir = path.relative(repoRoot, dir).split(path.sep).join("/");
119
+ const relUp = path.relative(repoRoot, up).split(path.sep).join("/");
120
+ const regName = dirNameMap.get(relDir) || dirNameMap.get(relUp);
121
+ if (regName)
122
+ name = regName;
123
+ return name;
124
+ };
125
+ exports.componentNameFor = componentNameFor;
package/package.json CHANGED
@@ -1,73 +1,88 @@
1
1
  {
2
- "name": "@md-code/component-uniqueness",
3
- "version": "0.1.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
- "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
- "logger.js",
30
- "utils.js",
31
- "decision-maker.js",
32
- "report.js",
33
- "normalize/",
34
- "matcher/",
35
- "filter/",
36
- "playgrounds/",
37
- "README.md"
38
- ],
39
- "exports": {
40
- ".": "./index.js",
41
- "./plugin": "./index.js",
42
- "./config": "./config.js",
43
- "./report": "./report.js",
44
- "./package.json": "./package.json"
45
- },
46
- "dependencies": {
47
- "jsdom": "^29.1.1",
48
- "minimatch": "^9.0.4",
49
- "remeda": "^2.50.0",
50
- "tailwindcss": "^4.3.3",
51
- "typescript": ">=5.5.0 <7"
52
- },
53
- "scripts": {
54
- "gen:catalog": "node main.js",
55
- "test": "vitest run"
56
- },
57
- "peerDependencies": {
58
- "eslint": ">=8.40.0"
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
- },
70
- "publishConfig": {
71
- "access": "public"
72
- }
2
+ "name": "@md-code/component-uniqueness",
3
+ "version": "0.3.0",
4
+ "private": false,
5
+ "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.",
6
+ "license": "Apache-2.0",
7
+ "author": "DrBoria <dr.boria@gmail.com>",
8
+ "bugs": {
9
+ "url": "https://github.com/DrBoria/component-uniqueness/issues"
10
+ },
11
+ "homepage": "https://github.com/DrBoria/component-uniqueness#readme",
12
+ "keywords": [
13
+ "eslint",
14
+ "eslintplugin",
15
+ "eslint-plugin",
16
+ "duplicate-components",
17
+ "design-system"
18
+ ],
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/DrBoria/component-uniqueness.git"
22
+ },
23
+ "engines": {
24
+ "node": ">=18"
25
+ },
26
+ "main": "dist/index.js",
27
+ "types": "dist/index.d.ts",
28
+ "bin": {
29
+ "component-uniqueness": "dist/main.js"
30
+ },
31
+ "files": [
32
+ "dist/",
33
+ "README.md",
34
+ "CHANGELOG.md"
35
+ ],
36
+ "exports": {
37
+ ".": "./dist/index.js",
38
+ "./plugin": "./dist/index.js",
39
+ "./config": "./dist/config.js",
40
+ "./report": "./dist/report/index.js",
41
+ "./package.json": "./package.json"
42
+ },
43
+ "dependencies": {
44
+ "jsdom": "^29.1.1",
45
+ "minimatch": "^9.0.4",
46
+ "remeda": "^2.50.0",
47
+ "tailwindcss": "^4.3.3",
48
+ "ts-morph": "^24.0.0",
49
+ "typescript": ">=5.5.0 <7"
50
+ },
51
+ "scripts": {
52
+ "clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
53
+ "build": "npm run clean && tsc -p tsconfig.json && npm run prebuild:report",
54
+ "prebuild:report": "node scripts/build-report-bundle.mjs",
55
+ "typecheck": "tsc -p tsconfig.json --noEmit",
56
+ "gen:catalog": "node dist/main.js",
57
+ "overview": "node dist/main.js --overview",
58
+ "test": "vitest run",
59
+ "lint": "eslint .",
60
+ "prepublishOnly": "npm run build && vitest run --no-file-parallelism"
61
+ },
62
+ "peerDependencies": {
63
+ "eslint": ">=8.40.0"
64
+ },
65
+ "devDependencies": {
66
+ "@emotion/react": "^11.0.0",
67
+ "@emotion/styled": "^11.0.0",
68
+ "@md-code/report-shared": "workspace:*",
69
+ "@md-code/components": "^0.0.1",
70
+ "@md-code/sections": "^0.0.1",
71
+ "@md/styles": "workspace:*",
72
+ "@mui/icons-material": "^7.0.0",
73
+ "@mui/material": "^7.0.0",
74
+ "@types/node": "^26.6.6",
75
+ "@types/react": "^19.3.0",
76
+ "@types/react-dom": "^19.3.0",
77
+ "@typescript-eslint/parser": "8.56.0",
78
+ "esbuild": "^0.28.2",
79
+ "prismjs": "^1.30.0",
80
+ "react": "^19.0.0",
81
+ "react-dom": "^19.0.0",
82
+ "styled-components": "^6.1.0",
83
+ "vitest": "^4.1.0"
84
+ },
85
+ "publishConfig": {
86
+ "access": "public"
87
+ }
73
88
  }