@md-code/component-uniqueness 0.1.0 → 0.1.1

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 CHANGED
@@ -1,41 +1,17 @@
1
1
  # @md-code/component-uniqueness
2
2
 
3
- ESLint rule + CLI scanner that finds **duplicate components** in your app code by comparing them against your canonical component library. Cross-framework: React today, the same signature model is designed to carry over to Vue and Angular.
4
-
5
- ## How it works
3
+ ESLint rule and CLI that find components, raw HTML and copied JSX blocks that duplicate a component you already have in your design system.
6
4
 
7
5
  ```mermaid
8
- flowchart TD
9
- A["Canonical components<br/>(walk folders, extract signatures)"] --> B[("catalog.json")]
10
- A --> CD["canon-dup<br/>canonicals matched against each other (strict thresholds)<br/>duplicates dropped → unique canonical set"]
11
- CD --> A
12
- C["App code<br/>(every JSX element)"] --> D
13
-
14
- subgraph match["Match (4 matchers in parallel)"]
15
- D --> M1["name<br/>same or fuzzy name"]
16
- D --> M2["structural<br/>shared class tokens"]
17
- D --> M3["family<br/>token-profile cluster"]
18
- D --> M4["jsx-block<br/>subtree contains canonical"]
19
- B --> M1
20
- B --> M2
21
- B --> M3
22
- B --> M4
23
- end
24
-
25
- M1 --> F1
26
- M2 --> F1
27
- M3 --> F1
28
- M4 --> F1
29
-
30
- subgraph filter["Filter"]
31
- F1["drop-usages<br/>app already imports the canonical"]
32
- F1 --> F2["drop-child-element<br/>match is on a child, not the root"]
33
- end
34
-
35
- F2 --> E["report.md<br/>Markdown funnel of duplicates"]
36
- CD -. "own section" .-> E
6
+ flowchart LR
7
+ A["Canonical<br/>components"] --> M
8
+ B["App code<br/>components · elements · parts"] --> M
9
+ M["Match<br/>name · structure · behavior · a11y · framework"] --> F["Filter<br/>usages · child elements"]
10
+ F --> R["Report"]
37
11
  ```
38
12
 
13
+ Structure is compared as rendered HTML with the CSS your classes resolve to, so two differently written components with the same markup and styles match.
14
+
39
15
  ## Quick start
40
16
 
41
17
  ```sh
@@ -44,106 +20,74 @@ bun add -D @md-code/component-uniqueness
44
20
 
45
21
  ```js
46
22
  // eslint.config.js
47
- const uniqueness = require("@md-code/component-uniqueness/plugin");
23
+ const plugin = require("@md-code/component-uniqueness");
48
24
 
49
25
  module.exports = [
50
26
  {
51
- files: ["**/*.tsx"],
52
- plugins: { "md-code": uniqueness },
27
+ files: ["src/**/*.{ts,tsx}"],
28
+ plugins: { "md-code": plugin },
53
29
  rules: {
54
- "md-code/component-uniqueness": ["error", {
55
- componentsFolder: [
56
- { path: "core/ui/atoms", rank: 0 },
57
- { path: "core/ui/formation", rank: 1 },
58
- { path: "core/ui/partials", rank: 2 },
59
- ],
60
- catalogPath: "reports/component-catalog.json",
30
+ "md-code/duplicate-component": ["error", {
31
+ componentsFolder: ["src/ui/atoms", "src/ui/formation", "src/ui/partials"],
32
+ tsconfig: "src/ui/tsconfig.json",
33
+ rawHtml: true,
34
+ parts: true,
61
35
  }],
62
36
  },
63
37
  },
64
38
  ];
65
39
  ```
66
40
 
67
- The catalog is generated automatically on the first lint when the file is
68
- missing (from the `componentsFolder` option) — it is gitignored and never
69
- committed. You can also generate it explicitly:
41
+ Generate the catalog once before linting, and again when the canonical components change. It is read from `reports/component-catalog.json` and should be gitignored.
70
42
 
71
43
  ```sh
72
- bunx md-code-component-uniqueness
44
+ bunx component-uniqueness
73
45
  ```
74
46
 
75
- Run the linter as usual:
47
+ ## Layers
76
48
 
77
- ```sh
78
- bunx eslint "src/**/*.tsx"
79
- ```
49
+ `componentsFolder` is ordered: each folder is built from the ones before it. A file inside a folder is compared only with the folders to its left, so `formation` is checked against `atoms`, and `partials` against both. The first folder is the base and is not compared with anything. Files outside all of them are compared with every folder.
80
50
 
81
- ## CLI
51
+ ## What it reports
82
52
 
83
- ```sh
84
- bunx md-code-component-uniqueness [flags]
85
- ```
86
-
87
- | Flag | Description |
53
+ | messageId | Fires when |
88
54
  | --- | --- |
89
- | `--roots a:b` | Scan roots (overrides `componentsFolder` from config) |
90
- | `--out <file>` | Catalog output path (default `reports/component-catalog.json`) |
91
- | `--check` | CI gate — exit 1 if catalog is stale |
92
- | `--report [file]` | Write a Markdown duplicate report |
93
- | `--verbose` | Include the "Filtered out" section in the report |
94
- | `--app-roots a:b` | App-code roots for the report (default: repo root) |
95
- | `--repo-root <dir>` | Repo root (default: auto-discovered) |
55
+ | `duplicateComponent` | A component duplicates a canonical one |
56
+ | `rawHtml` | A raw element (`button`, `input`, `a`, …) should be a canonical component. Needs `rawHtml: true`. Not applied inside `componentsFolder` |
57
+ | `partOfComponent` | A block inside a component duplicates a canonical one. Needs `parts: true` |
96
58
 
97
- ## What it flags
59
+ A component that already renders the canonical one is a usage, not a duplicate, and is skipped.
98
60
 
99
- | Tier | Meaning |
100
- | --- | --- |
101
- | `name` | App component has the same name as a canonical one |
102
- | `name-fuzzy` | Name is a fuzzy match (e.g. `MyButton` vs `Button`) |
103
- | `structural-exact` | ≥ 80% of the canonical's distinctive class tokens are shared |
104
- | `structural-similar` | 40–80% shared |
105
- | `family` | Clusters by token profile (hand-built library pieces) |
106
- | `jsx-block` | A JSX subtree in app code contains the full tag-sequence of a canonical component |
61
+ ## Options
107
62
 
108
- ## What the ESLint rule reports
63
+ | Option | Default | Notes |
64
+ | --- | --- | --- |
65
+ | `componentsFolder` | `[]` | Canonical folders, lowest layer first |
66
+ | `tsconfig` | — | Used to resolve imports when a file has no `tsconfig` of its own |
67
+ | `rawHtml` | `false` | Also check raw elements |
68
+ | `parts` | `false` | Also check blocks inside components |
69
+ | `catalogPath` | `reports/component-catalog.json` | Where the catalog is read and written |
70
+ | `include` | `[]` | Lint only matching files. Files inside `componentsFolder` are always linted |
71
+ | `exclude` | stories, tests, `*.d.ts` | Globs to skip |
72
+ | `ignoreDirs` | `node_modules`, `dist`, `build` | Added to the defaults |
73
+ | `exts` | `.tsx` `.ts` `.jsx` `.js` | File extensions |
74
+ | `weights` | name 0.10, structure 0.5, behavior 0.2, a11y 0.1, framework 0.15 | Signal weights |
75
+ | `thresholds` | duplicate 0.6, similar 0.4 | Confidence cut-offs |
109
76
 
110
- | messageId | Fires when |
111
- | --- | --- |
112
- | `duplicateComponent` | A canonical component name is declared outside its registry directory |
113
- | `rawHtml` | A raw interactive element (`button`, `input`, `select`, …) is used outside the canonical packages — the element's behavior (its `type`, `role`, `aria-*`) maps to a canonical component |
114
- | `layoutPrimitive` | A hand-rolled element **behaves like** a canonical component: its behavioral signals (event handlers + behavioral `role`/`type`/`aria-*` tokens) are a subset of a canonical's. Derived entirely from the catalog — no hardcoded role/behavior lists |
115
- | `styledInApp` | A `styled.*` component is created outside the canonical packages |
116
- | `catalogDuplicate` | An element structurally duplicates a canonical one (shared a11y token, or ≥ 3 shared layout style keys with high similarity) — error level |
117
- | `similarComponent` | Same as above but below the error threshold — warning level |
77
+ ## CLI
118
78
 
119
- The behavioral check (`layoutPrimitive`) runs only outside the canonical
120
- folders — inside them, the canonical's own body is the source of truth. It
121
- keys off the **lowest-rank** canonical whose signal set contains the
122
- candidate's, so the most basic canonical wins.
79
+ ```sh
80
+ bunx component-uniqueness [flags]
81
+ ```
123
82
 
124
- ## Options (rule config)
83
+ Options are read from the rule in `eslint.config.js`.
125
84
 
126
- | Option | Default | Notes |
127
- | --- | --- | --- |
128
- | `componentsFolder` | `[]` | Canonical dirs. `{ path, rank }` objects or plain strings. Rank drives the behavioral check (lowest = most basic) |
129
- | `catalog` / `catalogPath` | — / `reports/component-catalog.json` | Inline catalog object, or path to load one |
130
- | `registry` / `registryPath` | — / `reports/component-registry.json` | Canonical name → allowed dirs, for `duplicateComponent` |
131
- | `includeDynamic` | `false` | Also load a dynamic (DOM-snapshot) catalog |
132
- | `dynamicCatalog` / `dynamicCatalogPath` | — | Inline / path for the dynamic catalog |
133
- | `tsconfig` | — | Fallback `tsconfig.json` (or any `.ts` file next to one) used to resolve imports when the file has no `tsconfig` of its own. Resolution is done by TypeScript; unresolved imports keep their component name |
134
- | `rawHtml` | `false` | Match every HTML element that carries behavior (an event handler, `role`/`aria-*`, or an interactive tag) against the canonical catalog with the same matchers and decision-maker as components. The best canonical at or above the duplicate threshold is suggested |
135
- | `exclude` | `*.stories.*`, `*.test.*`, `*.spec.*`, `*.d.ts`, `**/__tests__/**` | Globs, `^...$` regexes or `/.../ ` regexes to skip |
136
- | `include` | `[]` | If set, only matching files are linted |
137
- | `exts` | `.tsx`, `.ts`, `.jsx`, `.js` | File extensions to scan |
138
- | `ignoreDirs` | `node_modules`, `dist`, `build` | Directory names to skip while walking |
139
- | `rootMarkers` | `.git` | File names that mark the repo root (auto-discovery) |
140
- | `debt` | `{}` | Known-duplicate ledger (shrink-only) |
141
-
142
- ## Notes
143
-
144
- - The catalog is **generated on demand** by the rule when the file is missing
145
- (same code path as the CLI), so it can stay gitignored and CI works out of
146
- the box. The CLI is still useful for `--report` and `--check`.
147
- - The catalog is read once per ESLint process and cached.
148
- - A missing or stale catalog logs a warning; lint never crashes.
149
- - Everything is plain Node + `fs`/`path` — works identically on Linux, macOS, Windows.
85
+ | Flag | Description |
86
+ | --- | --- |
87
+ | `--report [file]` | Write a Markdown report of every finding |
88
+ | `--check` | Exit 1 if the catalog is stale |
89
+ | `--roots a:b` | Canonical folders, instead of `componentsFolder` |
90
+ | `--app-roots a:b` | Folders to scan for the report (default: repo root) |
91
+ | `--repo-root <dir>` | Repo root (default: auto-detected) |
92
+ | `--out <file>` | Catalog path |
93
+ | `--verbose` | Also list what the filters dropped |
package/config.js CHANGED
@@ -185,7 +185,7 @@ const isIgnored = (relPath, include, exclude) => {
185
185
  return false;
186
186
  };
187
187
 
188
- const RULE_NAME = "md-code/duplicate-component";
188
+ const RULE_NAME = "md-code/component-uniqueness";
189
189
  const RULE_SHORT = "md-code";
190
190
 
191
191
  const FLAT_CANDIDATES = ["eslint.config.js", "eslint.config.mjs", "eslint.config.cjs", "eslint.config.ts"];
package/index.js CHANGED
@@ -35,6 +35,14 @@ const isComponentBinding = (node) => {
35
35
  return true;
36
36
  };
37
37
 
38
+ const normalizedCanons = new WeakMap();
39
+
40
+ const normalizeCanon = (canon, config) => {
41
+ if (!normalizedCanons.has(canon)) normalizedCanons.set(canon, buildCandidate(canon, config));
42
+
43
+ return normalizedCanons.get(canon);
44
+ };
45
+
38
46
  const createRule = () => {
39
47
  return {
40
48
  meta: {
@@ -96,7 +104,6 @@ const createRule = () => {
96
104
  }
97
105
  const normalizeComp = (comp) => buildCandidate(comp, config);
98
106
 
99
- const normCanons = new Map();
100
107
  for (const comp of parsed) {
101
108
  if (!comp.isExported) continue;
102
109
  const candidate = normalizeComp({ ...comp, path: relPath });
@@ -104,11 +111,7 @@ const createRule = () => {
104
111
  for (const canon of catalogComponents) {
105
112
  if (canon.name === candidate.name) continue;
106
113
  if (!canSeeCanon(relPath, canon.path, config.componentsFolder)) continue;
107
- let normCanon = normCanons.get(canon.name);
108
- if (!normCanon) {
109
- normCanon = normalizeComp(canon);
110
- normCanons.set(canon.name, normCanon);
111
- }
114
+ const normCanon = normalizeCanon(canon, config);
112
115
  const signals = matchSignals(candidate, normCanon, { canonNames, frameworkMatcher });
113
116
  const decision = decide(candidate, normCanon, { signals, weights: config.weights, thresholds: config.thresholds });
114
117
  pairs.push({ canon: normCanon, signals, decision });
@@ -127,11 +130,7 @@ const createRule = () => {
127
130
  }
128
131
  }
129
132
  if (config.rawHtml && !isInComponentsFolder) {
130
- const canons = catalogComponents.map((canon) => {
131
- if (!normCanons.has(canon.name)) normCanons.set(canon.name, normalizeComp(canon));
132
-
133
- return normCanons.get(canon.name);
134
- });
133
+ const canons = catalogComponents.map((canon) => normalizeCanon(canon, config));
135
134
  let elements = [];
136
135
  try {
137
136
  elements = parseElements(filename, canonNames, config);
@@ -151,11 +150,7 @@ const createRule = () => {
151
150
  }
152
151
  }
153
152
  if (config.parts) {
154
- const canons = catalogComponents.map((canon) => {
155
- if (!normCanons.has(canon.name)) normCanons.set(canon.name, normalizeComp(canon));
156
-
157
- return normCanons.get(canon.name);
158
- });
153
+ const canons = catalogComponents.map((canon) => normalizeCanon(canon, config));
159
154
  for (const { part, best } of findParts(filename, relPath, canons, config, new Set(canonNames))) {
160
155
  context.report({
161
156
  loc: { start: { line: part.line, column: part.column }, end: { line: part.endLine, column: 0 } },
@@ -173,6 +168,17 @@ const createRule = () => {
173
168
  };
174
169
  };
175
170
 
176
- module.exports = createRule();
171
+ const rule = createRule();
172
+ const pkg = require("./package.json");
173
+
174
+ module.exports = {
175
+ meta: {
176
+ name: pkg.name,
177
+ version: pkg.version,
178
+ },
179
+ rules: {
180
+ "component-uniqueness": rule,
181
+ },
182
+ };
177
183
  module.exports.RULE_NAME = RULE_NAME;
178
184
  module.exports.RULE_SHORT = RULE_SHORT;
package/main.js CHANGED
@@ -69,7 +69,7 @@ const parseArgs = (argv) => {
69
69
  cfg.verbose = true;
70
70
  } else if (a === "--help" || a === "-h") {
71
71
  console.log("Usage: component-uniqueness [--roots a:b] [--out file] [--registry file] [--repo-root dir] [--check] [--report [file.md]] [--app-roots a:b] [--ignore-dirs a:b] [--verbose]");
72
- console.log("Without --roots, the options are read from the consumer's eslint.config.js (rule md-code/duplicate-component) or component-uniqueness.config.js.");
72
+ console.log("Without --roots, the options are read from the consumer's eslint.config.js (rule md-code/component-uniqueness) or component-uniqueness.config.js.");
73
73
  process.exit(0);
74
74
  } else {
75
75
  fail(`unknown argument: ${a} (see --help)`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@md-code/component-uniqueness",
3
- "version": "0.1.0",
3
+ "version": "0.1.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
5
  "license": "MIT",
6
6
  "keywords": [
@@ -37,7 +37,7 @@
37
37
  ],
38
38
  "exports": {
39
39
  ".": "./index.js",
40
- "./plugin": "./plugin.js",
40
+ "./plugin": "./index.js",
41
41
  "./config": "./config.js",
42
42
  "./report": "./report.js",
43
43
  "./package.json": "./package.json"
package/plugin.js CHANGED
@@ -1,14 +1,3 @@
1
1
  "use strict";
2
2
 
3
- const rule = require("./index");
4
- const pkg = require("./package.json");
5
-
6
- module.exports = {
7
- meta: {
8
- name: pkg.name,
9
- version: pkg.version,
10
- },
11
- rules: {
12
- "duplicate-component": rule,
13
- },
14
- };
3
+ module.exports = require("./index");