@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
package/index.js DELETED
@@ -1,184 +0,0 @@
1
- "use strict";
2
-
3
- const path = require("node:path");
4
-
5
- const {
6
- normalizeOptions,
7
- inComponentsFolder,
8
- canSeeCanon,
9
- isIgnored,
10
- loadRegistry,
11
- loadCatalog,
12
- RULE_NAME,
13
- RULE_SHORT,
14
- } = require("./config");
15
- const { parseComponents, parseElements } = require("./playgrounds/framework-entrypoint.js");
16
- const { buildCandidate, buildElementCandidate } = require("./normalize/candidate.js");
17
- const { matchSignals } = require("./matcher");
18
- const { getFrameworkMatcher } = require("./matcher/framework");
19
- const { decide, bestOf } = require("./decision-maker");
20
- const { matchPairs, applyFilters, findParts } = require("./main");
21
-
22
- const frameworkMatcher = getFrameworkMatcher("react");
23
-
24
- const isComponentBinding = (node) => {
25
- if (!node || node.type !== "Identifier") return false;
26
- if (!/^[A-Z]/.test(node.name)) return false;
27
- let parent = node.parent;
28
- while (parent) {
29
- if (parent.type === "TSInterfaceDeclaration") return false;
30
- if (parent.type === "TSTypeAliasDeclaration") return false;
31
- if (parent.type === "TSModuleDeclaration") return false;
32
- if (parent.type === "ExportNamedDeclaration" || parent.type === "ExportDefaultDeclaration") break;
33
- parent = parent.parent;
34
- }
35
- return true;
36
- };
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
-
46
- const createRule = () => {
47
- return {
48
- meta: {
49
- type: "problem",
50
- docs: {
51
- description: "Flag components that duplicate a canonical component, and raw HTML elements that should use the canonical packages.",
52
- },
53
- messages: {
54
- duplicateComponent: "Component \"{{name}}\" duplicates the canonical \"{{canonName}}\" ({{reason}}).",
55
- rawHtml: "Raw <{{tag}}> outside the canonical packages → replace with `{{component}}` from {{path}} ({{reason}}).",
56
- partOfComponent: "This <{{tag}}> block in {{owner}} duplicates `{{component}}` from {{path}} ({{reason}}).",
57
- },
58
- schema: [
59
- {
60
- type: "object",
61
- properties: {
62
- registry: { type: "string" },
63
- registryPath: { type: "string" },
64
- catalog: { type: "array" },
65
- catalogPath: { type: "string" },
66
- includeDynamic: { type: "boolean" },
67
- dynamicCatalog: { type: "array" },
68
- dynamicCatalogPath: { type: "string" },
69
- rawHtml: { type: "boolean" },
70
- parts: { type: "boolean" },
71
- tsconfig: { type: "string" },
72
- weights: { type: "object" },
73
- thresholds: { type: "object" },
74
- componentsFolder: { type: "array" },
75
- include: { type: "array" },
76
- exclude: { type: "array" },
77
- exts: { type: "array" },
78
- ignoreDirs: { type: "array" },
79
- rootMarkers: { type: "array" },
80
- },
81
- additionalProperties: false,
82
- },
83
- ],
84
- },
85
- create(context) {
86
- const options = context.options && context.options[0] ? context.options[0] : {};
87
- const config = normalizeOptions(options);
88
- const filename = context.filename || (context.getFilename ? context.getFilename() : "");
89
- const relPath = path.relative(context.cwd || process.cwd(), filename).split(path.sep).join("/");
90
-
91
- const isInComponentsFolder = inComponentsFolder(relPath, config.componentsFolder);
92
- if (isIgnored(relPath, isInComponentsFolder ? [] : config.include, config.exclude)) return {};
93
-
94
- const registry = loadRegistry(config);
95
- const catalogComponents = ((loadCatalog(config) || {}).components) || [];
96
- const canonNames = [...new Set(catalogComponents.map((c) => c.name))];
97
-
98
- const checkDuplicates = () => {
99
- let parsed;
100
- try {
101
- parsed = parseComponents(filename, canonNames, config);
102
- } catch {
103
- return;
104
- }
105
- const normalizeComp = (comp) => buildCandidate(comp, config);
106
-
107
- for (const comp of parsed) {
108
- if (!comp.isExported) continue;
109
- const candidate = normalizeComp({ ...comp, path: relPath });
110
- const pairs = [];
111
- for (const canon of catalogComponents) {
112
- if (canon.name === candidate.name) continue;
113
- if (!canSeeCanon(relPath, canon.path, config.componentsFolder)) continue;
114
- const normCanon = normalizeCanon(canon, config);
115
- const signals = matchSignals(candidate, normCanon, { canonNames, frameworkMatcher });
116
- const decision = decide(candidate, normCanon, { signals, weights: config.weights, thresholds: config.thresholds });
117
- pairs.push({ canon: normCanon, signals, decision });
118
- }
119
- pairs.sort((a, b) => b.decision.confidence - a.decision.confidence);
120
- for (const p of pairs) {
121
- if (!p.decision.isDuplicate) continue;
122
- const { kept } = applyFilters([{ app: candidate, canon: p.canon, decision: p.decision, tier: p.decision.tier, reason: p.decision.reason }]);
123
- if (kept.length === 0) continue;
124
- const line = comp.line || 1;
125
- context.report({
126
- loc: { start: { line, column: 0 }, end: { line, column: 0 } },
127
- messageId: "duplicateComponent",
128
- data: { name: candidate.name, canonName: p.canon.name, reason: p.decision.reason },
129
- });
130
- }
131
- }
132
- if (config.rawHtml && !isInComponentsFolder) {
133
- const canons = catalogComponents.map((canon) => normalizeCanon(canon, config));
134
- let elements = [];
135
- try {
136
- elements = parseElements(filename, canonNames, config);
137
- } catch {
138
- elements = [];
139
- }
140
- for (const element of elements) {
141
- const candidate = buildElementCandidate(element, config);
142
- if (!candidate) continue;
143
- const best = bestOf(matchPairs([{ ...candidate, path: relPath }], canons, config));
144
- if (!best) continue;
145
- context.report({
146
- loc: { start: { line: element.line, column: element.column }, end: { line: element.line, column: element.column } },
147
- messageId: "rawHtml",
148
- data: { tag: element.name, component: best.canon.name, path: best.canon.path, reason: best.decision.reason },
149
- });
150
- }
151
- }
152
- if (config.parts) {
153
- const canons = catalogComponents.map((canon) => normalizeCanon(canon, config));
154
- for (const { part, best } of findParts(filename, relPath, canons, config, new Set(canonNames))) {
155
- context.report({
156
- loc: { start: { line: part.line, column: part.column }, end: { line: part.endLine, column: 0 } },
157
- messageId: "partOfComponent",
158
- data: { owner: part.owner, tag: part.rootTag, component: best.canon.name, path: best.canon.path, reason: best.decision.reason },
159
- });
160
- }
161
- }
162
- };
163
-
164
- return {
165
- "Program:exit": checkDuplicates,
166
- };
167
- },
168
- };
169
- };
170
-
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
- };
183
- module.exports.RULE_NAME = RULE_NAME;
184
- module.exports.RULE_SHORT = RULE_SHORT;
package/logger.js DELETED
@@ -1,44 +0,0 @@
1
- "use strict";
2
-
3
- const fs = require("node:fs");
4
- const path = require("node:path");
5
-
6
- const state = {
7
- enabled: false,
8
- stream: null,
9
- file: null,
10
- };
11
-
12
- const enabled = () => state.enabled;
13
-
14
- const enable = (file) => {
15
- if (!file) return;
16
- const abs = path.resolve(process.cwd(), file);
17
- fs.mkdirSync(path.dirname(abs), { recursive: true });
18
- if (state.stream) state.stream.end();
19
- state.stream = fs.createWriteStream(abs, { flags: "w" });
20
- state.file = abs;
21
- state.enabled = true;
22
- };
23
-
24
- const disable = () => {
25
- if (state.stream) state.stream.end();
26
- state.stream = null;
27
- state.file = null;
28
- state.enabled = false;
29
- };
30
-
31
- const write = (entry) => {
32
- if (!state.enabled || !state.stream) return;
33
- state.stream.write(`${JSON.stringify(entry)}\n`);
34
- };
35
-
36
- const stage = (name, data) => {
37
- write({ ts: Date.now(), stage: name, ...data });
38
- };
39
-
40
- const flush = () => {
41
- if (state.stream) state.stream.end();
42
- };
43
-
44
- module.exports = { enable, disable, enabled, stage, write, flush };