@aiready/pattern-detect 0.17.8 → 0.17.12

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 (190) hide show
  1. package/dist/analyzer-entry/index.d.mts +1 -1
  2. package/dist/analyzer-entry/index.d.ts +1 -1
  3. package/dist/analyzer-entry/index.js +370 -135
  4. package/dist/analyzer-entry/index.mjs +4 -3
  5. package/dist/chunk-2P7BQHGR.mjs +306 -0
  6. package/dist/{chunk-VGMM3L3O.mjs → chunk-3EORD7DC.mjs} +1 -1
  7. package/dist/{chunk-GREN7X5H.mjs → chunk-4PVPQMRT.mjs} +2 -2
  8. package/dist/{chunk-RS73WLNI.mjs → chunk-6VDL7TAS.mjs} +5 -113
  9. package/dist/chunk-AQIP4JGM.mjs +283 -0
  10. package/dist/{chunk-JBUZ6YHE.mjs → chunk-B4NLWKPZ.mjs} +85 -9
  11. package/dist/chunk-IPBGVPUX.mjs +143 -0
  12. package/dist/chunk-LUUJOUK5.mjs +283 -0
  13. package/dist/chunk-P3BOCGVV.mjs +498 -0
  14. package/dist/{scoring-entry.js → chunk-PHJE6A3J.mjs} +20 -37
  15. package/dist/chunk-PQXOORR4.mjs +234 -0
  16. package/dist/{chunk-GLKAGFKX.mjs → chunk-RDR75DVI.mjs} +85 -9
  17. package/dist/chunk-SXVLRPMF.mjs +143 -0
  18. package/dist/{chunk-DNZS4ESD.mjs → chunk-SY7RX5YQ.mjs} +85 -9
  19. package/dist/{context-rules-entry.js → chunk-TIBF7KST.mjs} +81 -78
  20. package/dist/chunk-WYYSQX5M.mjs +467 -0
  21. package/dist/{chunk-I6ETJC7L.mjs → chunk-X553BOMI.mjs} +56 -26
  22. package/dist/{chunk-K7BO57OO.mjs → chunk-Y6OB7K34.mjs} +80 -4
  23. package/dist/chunk-YLVV6YZ5.mjs +143 -0
  24. package/dist/chunk-ZUWPFVJV.mjs +115 -0
  25. package/dist/chunk-ZZMONVPE.mjs +467 -0
  26. package/dist/cli.js +402 -167
  27. package/dist/cli.mjs +4 -3
  28. package/dist/context-rules-entry/index.d.mts +35 -1
  29. package/dist/context-rules-entry/index.d.ts +35 -1
  30. package/dist/context-rules-entry/index.js +194 -48
  31. package/dist/context-rules-entry/index.mjs +1 -1
  32. package/dist/detector-entry/index.js +192 -46
  33. package/dist/detector-entry/index.mjs +2 -2
  34. package/dist/{analyzer-entry-BVz-HnZd.d.mts → index-B-pnXpgn.d.mts} +10 -1
  35. package/dist/{index-BwuoiCNm.d.ts → index-CWgYOKaK.d.ts} +35 -16
  36. package/dist/{index-BVz-HnZd.d.mts → index-Dl4BrGIT.d.mts} +35 -16
  37. package/dist/{analyzer-entry-BwuoiCNm.d.ts → index-DqS2e0kK.d.ts} +10 -1
  38. package/dist/index.d.mts +5 -6
  39. package/dist/index.d.ts +5 -6
  40. package/dist/index.js +467 -214
  41. package/dist/index.mjs +37 -22
  42. package/dist/scoring-entry/index.js +7 -3
  43. package/dist/scoring-entry/index.mjs +1 -1
  44. package/package.json +2 -2
  45. package/dist/analyzer-entry.d.mts +0 -100
  46. package/dist/analyzer-entry.d.ts +0 -100
  47. package/dist/analyzer-entry.js +0 -693
  48. package/dist/analyzer-entry.mjs +0 -12
  49. package/dist/chunk-262N2JB7.mjs +0 -497
  50. package/dist/chunk-2R7HOR5H.mjs +0 -777
  51. package/dist/chunk-3D7RVGHM.mjs +0 -64
  52. package/dist/chunk-3LS3E6MO.mjs +0 -508
  53. package/dist/chunk-3VRQYFW3.mjs +0 -782
  54. package/dist/chunk-3WK24ZOX.mjs +0 -860
  55. package/dist/chunk-3YYN6ZXN.mjs +0 -1038
  56. package/dist/chunk-4BPRGZRG.mjs +0 -1041
  57. package/dist/chunk-4UHDGB7U.mjs +0 -920
  58. package/dist/chunk-5LYDB7DY.mjs +0 -771
  59. package/dist/chunk-65G3HXLQ.mjs +0 -497
  60. package/dist/chunk-65UQ5J2J.mjs +0 -64
  61. package/dist/chunk-6JTVOBJX.mjs +0 -64
  62. package/dist/chunk-6OEHUI5J.mjs +0 -1045
  63. package/dist/chunk-6YUGU4P4.mjs +0 -914
  64. package/dist/chunk-7EJGNGXM.mjs +0 -771
  65. package/dist/chunk-7O2DUBSN.mjs +0 -1058
  66. package/dist/chunk-7S4AUL5S.mjs +0 -911
  67. package/dist/chunk-A76JUWER.mjs +0 -786
  68. package/dist/chunk-AJZUNNFH.mjs +0 -817
  69. package/dist/chunk-AXHGYYYZ.mjs +0 -404
  70. package/dist/chunk-BKRPSTT2.mjs +0 -64
  71. package/dist/chunk-BUBQ3W6W.mjs +0 -980
  72. package/dist/chunk-CCHM2VLK.mjs +0 -1051
  73. package/dist/chunk-CHFK6EBT.mjs +0 -419
  74. package/dist/chunk-CMT3MWWO.mjs +0 -948
  75. package/dist/chunk-CMWW24HW.mjs +0 -259
  76. package/dist/chunk-CTDBJP25.mjs +0 -1043
  77. package/dist/chunk-DGAKXYIP.mjs +0 -1041
  78. package/dist/chunk-DQSLTL7J.mjs +0 -788
  79. package/dist/chunk-DR5W7S3Z.mjs +0 -968
  80. package/dist/chunk-EFUKPMBE.mjs +0 -950
  81. package/dist/chunk-EVBFDILL.mjs +0 -927
  82. package/dist/chunk-EXORBAXR.mjs +0 -887
  83. package/dist/chunk-EZT3NZGB.mjs +0 -1057
  84. package/dist/chunk-FWUKMJEQ.mjs +0 -1133
  85. package/dist/chunk-GSJFORRO.mjs +0 -504
  86. package/dist/chunk-H4ADJYOG.mjs +0 -925
  87. package/dist/chunk-H5FB2USZ.mjs +0 -762
  88. package/dist/chunk-H73HEG7M.mjs +0 -670
  89. package/dist/chunk-HOS5Z2NC.mjs +0 -669
  90. package/dist/chunk-HXHQOQB5.mjs +0 -508
  91. package/dist/chunk-INEOYHUM.mjs +0 -911
  92. package/dist/chunk-INJ4SBTV.mjs +0 -754
  93. package/dist/chunk-J5CW6NYY.mjs +0 -64
  94. package/dist/chunk-JAFZCZAP.mjs +0 -776
  95. package/dist/chunk-JKVKOXYR.mjs +0 -407
  96. package/dist/chunk-JTHW7EYW.mjs +0 -1041
  97. package/dist/chunk-JWR3AHKO.mjs +0 -788
  98. package/dist/chunk-KC2CQMG2.mjs +0 -858
  99. package/dist/chunk-KDWGWBP5.mjs +0 -832
  100. package/dist/chunk-KPEK5REL.mjs +0 -919
  101. package/dist/chunk-KT6O2IAE.mjs +0 -861
  102. package/dist/chunk-KWMNN3TG.mjs +0 -391
  103. package/dist/chunk-LUA5FXSZ.mjs +0 -771
  104. package/dist/chunk-LYKRYBSM.mjs +0 -64
  105. package/dist/chunk-M4PQMW34.mjs +0 -480
  106. package/dist/chunk-MH6LBXZF.mjs +0 -816
  107. package/dist/chunk-MHU3CL4R.mjs +0 -64
  108. package/dist/chunk-MJWBS6SM.mjs +0 -1058
  109. package/dist/chunk-OFGMDX66.mjs +0 -402
  110. package/dist/chunk-P7B6Z4I2.mjs +0 -1043
  111. package/dist/chunk-PBCXSG7E.mjs +0 -658
  112. package/dist/chunk-PEEHSFDR.mjs +0 -1058
  113. package/dist/chunk-PSVG2NLH.mjs +0 -966
  114. package/dist/chunk-PWNQ6JZW.mjs +0 -508
  115. package/dist/chunk-QE4E3F7C.mjs +0 -410
  116. package/dist/chunk-QEP76HGK.mjs +0 -1039
  117. package/dist/chunk-QX2BQJEO.mjs +0 -1058
  118. package/dist/chunk-R2S73CVG.mjs +0 -503
  119. package/dist/chunk-RMGDSNLE.mjs +0 -770
  120. package/dist/chunk-S2KQFII2.mjs +0 -491
  121. package/dist/chunk-SLDK5PQK.mjs +0 -1129
  122. package/dist/chunk-SNSDVGWW.mjs +0 -783
  123. package/dist/chunk-SUUZMLPS.mjs +0 -391
  124. package/dist/chunk-SVCSIZ2A.mjs +0 -259
  125. package/dist/chunk-T2C6WS73.mjs +0 -670
  126. package/dist/chunk-TCG2G32F.mjs +0 -911
  127. package/dist/chunk-TGBZP7SB.mjs +0 -773
  128. package/dist/chunk-THF4RW63.mjs +0 -254
  129. package/dist/chunk-TJKDLVLN.mjs +0 -503
  130. package/dist/chunk-TXWPOVYU.mjs +0 -402
  131. package/dist/chunk-UB3CGOQ7.mjs +0 -64
  132. package/dist/chunk-UKIKN27B.mjs +0 -950
  133. package/dist/chunk-V5DP4FP6.mjs +0 -876
  134. package/dist/chunk-VRMXVYDZ.mjs +0 -419
  135. package/dist/chunk-WACZ5LFH.mjs +0 -1055
  136. package/dist/chunk-WC7CBAA7.mjs +0 -1058
  137. package/dist/chunk-WKBCNITM.mjs +0 -1072
  138. package/dist/chunk-WMOGJFME.mjs +0 -391
  139. package/dist/chunk-X4GR2N2M.mjs +0 -947
  140. package/dist/chunk-XCWY2DQY.mjs +0 -788
  141. package/dist/chunk-XJD35DS6.mjs +0 -1058
  142. package/dist/chunk-XNPID6FU.mjs +0 -391
  143. package/dist/chunk-XUUVS54V.mjs +0 -776
  144. package/dist/chunk-YCGV65F5.mjs +0 -508
  145. package/dist/chunk-YJYDBFT3.mjs +0 -780
  146. package/dist/chunk-YP3HEDQW.mjs +0 -859
  147. package/dist/chunk-YSDOUNJJ.mjs +0 -1142
  148. package/dist/chunk-Z6GBFFOV.mjs +0 -1040
  149. package/dist/cli.d.ts.map +0 -1
  150. package/dist/cli.js.map +0 -1
  151. package/dist/context-rules-entry-y2uJSngh.d.mts +0 -60
  152. package/dist/context-rules-entry-y2uJSngh.d.ts +0 -60
  153. package/dist/context-rules-entry.d.mts +0 -55
  154. package/dist/context-rules-entry.d.ts +0 -55
  155. package/dist/context-rules-entry.mjs +0 -12
  156. package/dist/context-rules.d.ts +0 -41
  157. package/dist/context-rules.d.ts.map +0 -1
  158. package/dist/context-rules.js +0 -225
  159. package/dist/context-rules.js.map +0 -1
  160. package/dist/detector-entry.d.mts +0 -14
  161. package/dist/detector-entry.d.ts +0 -14
  162. package/dist/detector-entry.js +0 -301
  163. package/dist/detector-entry.mjs +0 -7
  164. package/dist/detector.d.ts +0 -40
  165. package/dist/detector.d.ts.map +0 -1
  166. package/dist/detector.js +0 -385
  167. package/dist/detector.js.map +0 -1
  168. package/dist/extractors/python-extractor.d.ts +0 -19
  169. package/dist/extractors/python-extractor.d.ts.map +0 -1
  170. package/dist/extractors/python-extractor.js +0 -164
  171. package/dist/extractors/python-extractor.js.map +0 -1
  172. package/dist/grouping.d.ts +0 -54
  173. package/dist/grouping.d.ts.map +0 -1
  174. package/dist/grouping.js +0 -347
  175. package/dist/grouping.js.map +0 -1
  176. package/dist/index-y2uJSngh.d.mts +0 -60
  177. package/dist/index-y2uJSngh.d.ts +0 -60
  178. package/dist/index.d.ts.map +0 -1
  179. package/dist/index.js.map +0 -1
  180. package/dist/python-extractor-BGKGX6BK.mjs +0 -131
  181. package/dist/python-extractor-ELAKYK2W.mjs +0 -140
  182. package/dist/scoring-entry.d.mts +0 -23
  183. package/dist/scoring-entry.d.ts +0 -23
  184. package/dist/scoring-entry.mjs +0 -6
  185. package/dist/scoring.d.ts +0 -12
  186. package/dist/scoring.d.ts.map +0 -1
  187. package/dist/scoring.js +0 -116
  188. package/dist/scoring.js.map +0 -1
  189. package/dist/types-C4lmb2Yh.d.mts +0 -36
  190. package/dist/types-C4lmb2Yh.d.ts +0 -36
@@ -1,693 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/analyzer-entry.ts
31
- var analyzer_entry_exports = {};
32
- __export(analyzer_entry_exports, {
33
- analyzePatterns: () => analyzePatterns,
34
- generateSummary: () => generateSummary,
35
- getSmartDefaults: () => getSmartDefaults
36
- });
37
- module.exports = __toCommonJS(analyzer_entry_exports);
38
-
39
- // src/analyzer.ts
40
- var import_core4 = require("@aiready/core");
41
-
42
- // src/detector.ts
43
- var import_core2 = require("@aiready/core");
44
-
45
- // src/context-rules.ts
46
- var import_core = require("@aiready/core");
47
- var CONTEXT_RULES = [
48
- // Test Fixtures - Intentional duplication for test isolation
49
- {
50
- name: "test-fixtures",
51
- detect: (file, code) => {
52
- const isTestFile = file.includes(".test.") || file.includes(".spec.") || file.includes("__tests__") || file.includes("/test/") || file.includes("/tests/");
53
- const hasTestFixtures = code.includes("beforeAll") || code.includes("afterAll") || code.includes("beforeEach") || code.includes("afterEach") || code.includes("setUp") || code.includes("tearDown");
54
- return isTestFile && hasTestFixtures;
55
- },
56
- severity: import_core.Severity.Info,
57
- reason: "Test fixture duplication is intentional for test isolation",
58
- suggestion: "Consider if shared test setup would improve maintainability without coupling tests"
59
- },
60
- // Email/Document Templates - Often intentionally similar for consistency
61
- {
62
- name: "templates",
63
- detect: (file, code) => {
64
- const isTemplate = file.includes("/templates/") || file.includes("-template") || file.includes("/email-templates/") || file.includes("/emails/");
65
- const hasTemplateContent = (code.includes("return") || code.includes("export")) && (code.includes("html") || code.includes("subject") || code.includes("body"));
66
- return isTemplate && hasTemplateContent;
67
- },
68
- severity: import_core.Severity.Minor,
69
- reason: "Template duplication may be intentional for maintainability and branding consistency",
70
- suggestion: "Extract shared structure only if templates become hard to maintain"
71
- },
72
- // E2E/Integration Test Page Objects - Test independence
73
- {
74
- name: "e2e-page-objects",
75
- detect: (file, code) => {
76
- const isE2ETest = file.includes("e2e/") || file.includes("/e2e/") || file.includes(".e2e.") || file.includes("/playwright/") || file.includes("playwright/") || file.includes("/cypress/") || file.includes("cypress/") || file.includes("/integration/") || file.includes("integration/");
77
- const hasPageObjectPatterns = code.includes("page.") || code.includes("await page") || code.includes("locator") || code.includes("getBy") || code.includes("selector") || code.includes("click(") || code.includes("fill(");
78
- return isE2ETest && hasPageObjectPatterns;
79
- },
80
- severity: import_core.Severity.Minor,
81
- reason: "E2E test duplication ensures test independence and reduces coupling",
82
- suggestion: "Consider page object pattern only if duplication causes maintenance issues"
83
- },
84
- // Configuration Files - Often necessarily similar by design
85
- {
86
- name: "config-files",
87
- detect: (file) => {
88
- return file.endsWith(".config.ts") || file.endsWith(".config.js") || file.includes("jest.config") || file.includes("vite.config") || file.includes("webpack.config") || file.includes("rollup.config") || file.includes("tsconfig");
89
- },
90
- severity: import_core.Severity.Minor,
91
- reason: "Configuration files often have similar structure by design",
92
- suggestion: "Consider shared config base only if configurations become hard to maintain"
93
- },
94
- // Type Definitions - Duplication for type safety and module independence
95
- {
96
- name: "type-definitions",
97
- detect: (file, code) => {
98
- const isTypeFile = file.endsWith(".d.ts") || file.includes("/types/");
99
- const hasTypeDefinitions = code.includes("interface ") || code.includes("type ") || code.includes("enum ");
100
- return isTypeFile && hasTypeDefinitions;
101
- },
102
- severity: import_core.Severity.Info,
103
- reason: "Type duplication may be intentional for module independence and type safety",
104
- suggestion: "Extract to shared types package only if causing maintenance burden"
105
- },
106
- // Migration Scripts - One-off scripts that are similar by nature
107
- {
108
- name: "migration-scripts",
109
- detect: (file) => {
110
- return file.includes("/migrations/") || file.includes("/migrate/") || file.includes(".migration.");
111
- },
112
- severity: import_core.Severity.Info,
113
- reason: "Migration scripts are typically one-off and intentionally similar",
114
- suggestion: "Duplication is acceptable for migration scripts"
115
- },
116
- // Mock Data - Test data intentionally duplicated
117
- {
118
- name: "mock-data",
119
- detect: (file, code) => {
120
- const isMockFile = file.includes("/mocks/") || file.includes("/__mocks__/") || file.includes("/fixtures/") || file.includes(".mock.") || file.includes(".fixture.");
121
- const hasMockData = code.includes("mock") || code.includes("Mock") || code.includes("fixture") || code.includes("stub") || code.includes("export const");
122
- return isMockFile && hasMockData;
123
- },
124
- severity: import_core.Severity.Info,
125
- reason: "Mock data duplication is expected for comprehensive test coverage",
126
- suggestion: "Consider shared factories only for complex mock generation"
127
- },
128
- // Tool Implementations - Structural Boilerplate
129
- {
130
- name: "tool-implementations",
131
- detect: (file, code) => {
132
- const isToolFile = file.includes("/tools/") || file.endsWith(".tool.ts") || code.includes("toolDefinitions");
133
- const hasToolStructure = code.includes("execute") && (code.includes("try") || code.includes("catch"));
134
- return isToolFile && hasToolStructure;
135
- },
136
- severity: import_core.Severity.Info,
137
- reason: "Tool implementations share structural boilerplate but have distinct business logic",
138
- suggestion: "Tool duplication is acceptable for boilerplate interface wrappers"
139
- }
140
- ];
141
- function calculateSeverity(file1, file2, code, similarity, linesOfCode) {
142
- for (const rule of CONTEXT_RULES) {
143
- if (rule.detect(file1, code) || rule.detect(file2, code)) {
144
- return {
145
- severity: rule.severity,
146
- reason: rule.reason,
147
- suggestion: rule.suggestion,
148
- matchedRule: rule.name
149
- };
150
- }
151
- }
152
- if (similarity >= 0.95 && linesOfCode >= 30) {
153
- return {
154
- severity: import_core.Severity.Critical,
155
- reason: "Large nearly-identical code blocks waste tokens and create maintenance burden",
156
- suggestion: "Extract to shared utility module immediately"
157
- };
158
- } else if (similarity >= 0.95 && linesOfCode >= 15) {
159
- return {
160
- severity: import_core.Severity.Major,
161
- reason: "Nearly identical code should be consolidated",
162
- suggestion: "Move to shared utility file"
163
- };
164
- } else if (similarity >= 0.85) {
165
- return {
166
- severity: import_core.Severity.Major,
167
- reason: "High similarity indicates significant duplication",
168
- suggestion: "Extract common logic to shared function"
169
- };
170
- } else if (similarity >= 0.7) {
171
- return {
172
- severity: import_core.Severity.Minor,
173
- reason: "Moderate similarity detected",
174
- suggestion: "Consider extracting shared patterns if code evolves together"
175
- };
176
- } else {
177
- return {
178
- severity: import_core.Severity.Minor,
179
- reason: "Minor similarity detected",
180
- suggestion: "Monitor but refactoring may not be worthwhile"
181
- };
182
- }
183
- }
184
-
185
- // src/core/normalizer.ts
186
- function normalizeCode(code, isPython = false) {
187
- if (!code) return "";
188
- let normalized = code;
189
- if (isPython) {
190
- normalized = normalized.replace(/#.*/g, "");
191
- } else {
192
- normalized = normalized.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
193
- }
194
- return normalized.replace(/"[^"]*"/g, '"STR"').replace(/'[^']*'/g, "'STR'").replace(/`[^`]*`/g, "`STR`").replace(/\b\d+\b/g, "NUM").replace(/\s+/g, " ").trim().toLowerCase();
195
- }
196
-
197
- // src/detector.ts
198
- function extractBlocks(file, content) {
199
- return (0, import_core2.extractCodeBlocks)(file, content);
200
- }
201
- function calculateSimilarity(a, b) {
202
- return (0, import_core2.calculateStringSimilarity)(a, b);
203
- }
204
- function calculateConfidence(similarity, tokens, lines) {
205
- return (0, import_core2.calculateHeuristicConfidence)(similarity, tokens, lines);
206
- }
207
- async function detectDuplicatePatterns(fileContents, options) {
208
- const {
209
- minSimilarity,
210
- minLines,
211
- streamResults,
212
- onProgress,
213
- excludePatterns = [],
214
- confidenceThreshold = 0,
215
- ignoreWhitelist = []
216
- } = options;
217
- const allBlocks = [];
218
- const excludeRegexes = excludePatterns.map((p) => new RegExp(p, "i"));
219
- for (const { file, content } of fileContents) {
220
- const blocks = extractBlocks(file, content);
221
- for (const b of blocks) {
222
- if (b.endLine - b.startLine + 1 < minLines) continue;
223
- const isExcluded = excludeRegexes.some((regex) => regex.test(b.code));
224
- if (isExcluded) continue;
225
- allBlocks.push(b);
226
- }
227
- }
228
- const duplicates = [];
229
- const totalBlocks = allBlocks.length;
230
- let comparisons = 0;
231
- const totalComparisons = totalBlocks * (totalBlocks - 1) / 2;
232
- if (onProgress) {
233
- onProgress(
234
- 0,
235
- totalComparisons,
236
- `Starting duplicate detection on ${totalBlocks} blocks...`
237
- );
238
- }
239
- for (let i = 0; i < allBlocks.length; i++) {
240
- if (i % 50 === 0 && i > 0) {
241
- await new Promise((resolve) => setImmediate(resolve));
242
- if (onProgress) {
243
- onProgress(
244
- comparisons,
245
- totalComparisons,
246
- `Analyzing blocks (${i}/${totalBlocks})...`
247
- );
248
- }
249
- }
250
- const b1 = allBlocks[i];
251
- const isPython1 = b1.file.toLowerCase().endsWith(".py");
252
- const norm1 = normalizeCode(b1.code, isPython1);
253
- for (let j = i + 1; j < allBlocks.length; j++) {
254
- comparisons++;
255
- const b2 = allBlocks[j];
256
- if (b1.file === b2.file) continue;
257
- const isWhitelisted = ignoreWhitelist.some((pattern) => {
258
- return b1.file.includes(pattern) && b2.file.includes(pattern) || pattern === `${b1.file}::${b2.file}` || pattern === `${b2.file}::${b1.file}`;
259
- });
260
- if (isWhitelisted) continue;
261
- const isPython2 = b2.file.toLowerCase().endsWith(".py");
262
- const norm2 = normalizeCode(b2.code, isPython2);
263
- const sim = calculateSimilarity(norm1, norm2);
264
- if (sim >= minSimilarity) {
265
- const confidence = calculateConfidence(
266
- sim,
267
- b1.tokens,
268
- b1.endLine - b1.startLine + 1
269
- );
270
- if (confidence < confidenceThreshold) continue;
271
- const { severity, reason, suggestion, matchedRule } = calculateSeverity(
272
- b1.file,
273
- b2.file,
274
- b1.code,
275
- sim,
276
- b1.endLine - b1.startLine + 1
277
- );
278
- const dup = {
279
- file1: b1.file,
280
- line1: b1.startLine,
281
- endLine1: b1.endLine,
282
- file2: b2.file,
283
- line2: b2.startLine,
284
- endLine2: b2.endLine,
285
- code1: b1.code,
286
- code2: b2.code,
287
- similarity: sim,
288
- confidence,
289
- patternType: b1.patternType,
290
- tokenCost: b1.tokens + b2.tokens,
291
- severity,
292
- reason,
293
- suggestion,
294
- matchedRule
295
- };
296
- duplicates.push(dup);
297
- if (streamResults)
298
- console.log(
299
- `[DUPLICATE] ${dup.file1}:${dup.line1} <-> ${dup.file2}:${dup.line2} (${Math.round(sim * 100)}%, conf: ${Math.round(confidence * 100)}%)`
300
- );
301
- }
302
- }
303
- }
304
- if (onProgress) {
305
- onProgress(
306
- totalComparisons,
307
- totalComparisons,
308
- `Duplicate detection complete. Found ${duplicates.length} patterns.`
309
- );
310
- }
311
- return duplicates.sort((a, b) => b.similarity - a.similarity);
312
- }
313
-
314
- // src/grouping.ts
315
- var import_core3 = require("@aiready/core");
316
- var import_path = __toESM(require("path"));
317
- function groupDuplicatesByFilePair(duplicates) {
318
- const groups = /* @__PURE__ */ new Map();
319
- for (const dup of duplicates) {
320
- const files = [dup.file1, dup.file2].sort();
321
- const key = files.join("::");
322
- if (!groups.has(key)) {
323
- groups.set(key, {
324
- filePair: key,
325
- severity: dup.severity,
326
- occurrences: 0,
327
- totalTokenCost: 0,
328
- averageSimilarity: 0,
329
- patternTypes: /* @__PURE__ */ new Set(),
330
- lineRanges: []
331
- });
332
- }
333
- const group = groups.get(key);
334
- group.occurrences++;
335
- group.totalTokenCost += dup.tokenCost;
336
- group.averageSimilarity += dup.similarity;
337
- group.patternTypes.add(dup.patternType);
338
- group.lineRanges.push({
339
- file1: { start: dup.line1, end: dup.endLine1 },
340
- file2: { start: dup.line2, end: dup.endLine2 }
341
- });
342
- const currentSev = dup.severity;
343
- if ((0, import_core3.getSeverityLevel)(currentSev) > (0, import_core3.getSeverityLevel)(group.severity)) {
344
- group.severity = currentSev;
345
- }
346
- }
347
- return Array.from(groups.values()).map((g) => ({
348
- ...g,
349
- averageSimilarity: g.averageSimilarity / g.occurrences
350
- }));
351
- }
352
- function createRefactorClusters(duplicates) {
353
- const adjacency = /* @__PURE__ */ new Map();
354
- const visited = /* @__PURE__ */ new Set();
355
- const components = [];
356
- for (const dup of duplicates) {
357
- if (!adjacency.has(dup.file1)) adjacency.set(dup.file1, /* @__PURE__ */ new Set());
358
- if (!adjacency.has(dup.file2)) adjacency.set(dup.file2, /* @__PURE__ */ new Set());
359
- adjacency.get(dup.file1).add(dup.file2);
360
- adjacency.get(dup.file2).add(dup.file1);
361
- }
362
- for (const file of adjacency.keys()) {
363
- if (visited.has(file)) continue;
364
- const component = [];
365
- const queue = [file];
366
- visited.add(file);
367
- while (queue.length > 0) {
368
- const curr = queue.shift();
369
- component.push(curr);
370
- for (const neighbor of adjacency.get(curr) || []) {
371
- if (!visited.has(neighbor)) {
372
- visited.add(neighbor);
373
- queue.push(neighbor);
374
- }
375
- }
376
- }
377
- components.push(component);
378
- }
379
- const clusters = [];
380
- for (const component of components) {
381
- if (component.length < 2) continue;
382
- const componentDups = duplicates.filter(
383
- (d) => component.includes(d.file1) && component.includes(d.file2)
384
- );
385
- const totalTokenCost = componentDups.reduce(
386
- (sum, d) => sum + d.tokenCost,
387
- 0
388
- );
389
- const avgSimilarity = componentDups.reduce((sum, d) => sum + d.similarity, 0) / Math.max(1, componentDups.length);
390
- const name = determineClusterName(component);
391
- const { severity, reason, suggestion } = calculateSeverity(
392
- component[0],
393
- component[1],
394
- "",
395
- // Code not available here
396
- avgSimilarity,
397
- 30
398
- // Assume substantial if clustered
399
- );
400
- clusters.push({
401
- id: `cluster-${clusters.length}`,
402
- name,
403
- files: component,
404
- severity,
405
- duplicateCount: componentDups.length,
406
- totalTokenCost,
407
- averageSimilarity: avgSimilarity,
408
- reason,
409
- suggestion
410
- });
411
- }
412
- return clusters;
413
- }
414
- function determineClusterName(files) {
415
- if (files.length === 0) return "Unknown Cluster";
416
- if (files.some((f) => f.includes("blog"))) return "Blog SEO Boilerplate";
417
- if (files.some((f) => f.includes("buttons")))
418
- return "Button Component Variants";
419
- if (files.some((f) => f.includes("cards"))) return "Card Component Variants";
420
- if (files.some((f) => f.includes("login.test"))) return "E2E Test Patterns";
421
- const first = files[0];
422
- const dirName = import_path.default.dirname(first).split(import_path.default.sep).pop();
423
- if (dirName && dirName !== "." && dirName !== "..") {
424
- return `${dirName.charAt(0).toUpperCase() + dirName.slice(1)} Domain Group`;
425
- }
426
- return "Shared Pattern Group";
427
- }
428
- function filterClustersByImpact(clusters, minTokenCost = 1e3, minFiles = 3) {
429
- return clusters.filter(
430
- (c) => c.totalTokenCost >= minTokenCost && c.files.length >= minFiles
431
- );
432
- }
433
-
434
- // src/analyzer.ts
435
- function getRefactoringSuggestion(patternType, similarity) {
436
- const baseMessages = {
437
- "api-handler": "Extract common middleware or create a base handler class",
438
- validator: "Consolidate validation logic into shared schema validators (Zod/Yup)",
439
- utility: "Move to a shared utilities file and reuse across modules",
440
- "class-method": "Consider inheritance or composition to share behavior",
441
- component: "Extract shared logic into a custom hook or HOC",
442
- function: "Extract into a shared helper function",
443
- unknown: "Extract common logic into a reusable module"
444
- };
445
- const urgency = similarity > 0.95 ? " (CRITICAL: Nearly identical code)" : similarity > 0.9 ? " (HIGH: Very similar, refactor soon)" : "";
446
- return baseMessages[patternType] + urgency;
447
- }
448
- async function getSmartDefaults(directory, userOptions) {
449
- if (userOptions.useSmartDefaults === false) {
450
- return {
451
- rootDir: directory,
452
- minSimilarity: 0.6,
453
- minLines: 8,
454
- batchSize: 100,
455
- approx: true,
456
- minSharedTokens: 12,
457
- maxCandidatesPerBlock: 5,
458
- streamResults: false,
459
- severity: "all",
460
- includeTests: false
461
- };
462
- }
463
- const scanOptions = {
464
- rootDir: directory,
465
- include: userOptions.include || ["**/*.{ts,tsx,js,jsx,py,java}"],
466
- exclude: userOptions.exclude
467
- };
468
- const files = await (0, import_core4.scanFiles)(scanOptions);
469
- const fileCount = files.length;
470
- const estimatedBlocks = fileCount * 5;
471
- const minLines = Math.max(
472
- 6,
473
- Math.min(20, 6 + Math.floor(estimatedBlocks / 1e3) * 2)
474
- );
475
- const minSimilarity = Math.min(0.85, 0.5 + estimatedBlocks / 5e3 * 0.3);
476
- const batchSize = estimatedBlocks > 1e3 ? 200 : 100;
477
- const severity = estimatedBlocks > 3e3 ? "high" : "all";
478
- const maxCandidatesPerBlock = Math.max(
479
- 5,
480
- Math.min(100, Math.floor(1e6 / estimatedBlocks))
481
- );
482
- const defaults = {
483
- rootDir: directory,
484
- minSimilarity,
485
- minLines,
486
- batchSize,
487
- approx: true,
488
- minSharedTokens: 10,
489
- maxCandidatesPerBlock,
490
- streamResults: false,
491
- severity,
492
- includeTests: false
493
- };
494
- const result = { ...defaults };
495
- for (const key of Object.keys(defaults)) {
496
- if (key in userOptions && userOptions[key] !== void 0) {
497
- result[key] = userOptions[key];
498
- }
499
- }
500
- return result;
501
- }
502
- function logConfiguration(config, estimatedBlocks) {
503
- if (config.suppressToolConfig) return;
504
- console.log("\u{1F4CB} Configuration:");
505
- console.log(` Repository size: ~${estimatedBlocks} code blocks`);
506
- console.log(` Similarity threshold: ${config.minSimilarity}`);
507
- console.log(` Minimum lines: ${config.minLines}`);
508
- console.log(` Approximate mode: ${config.approx ? "enabled" : "disabled"}`);
509
- console.log(` Max candidates per block: ${config.maxCandidatesPerBlock}`);
510
- console.log(` Min shared tokens: ${config.minSharedTokens}`);
511
- console.log(` Severity filter: ${config.severity}`);
512
- console.log(` Include tests: ${config.includeTests}`);
513
- if (config.excludePatterns && config.excludePatterns.length > 0) {
514
- console.log(` Exclude patterns: ${config.excludePatterns.length} active`);
515
- }
516
- if (config.confidenceThreshold && config.confidenceThreshold > 0) {
517
- console.log(` Confidence threshold: ${config.confidenceThreshold}`);
518
- }
519
- if (config.ignoreWhitelist && config.ignoreWhitelist.length > 0) {
520
- console.log(
521
- ` Ignore whitelist: ${config.ignoreWhitelist.length} entries`
522
- );
523
- }
524
- console.log("");
525
- }
526
- async function analyzePatterns(options) {
527
- const smartDefaults = await getSmartDefaults(options.rootDir || ".", options);
528
- const finalOptions = { ...smartDefaults, ...options };
529
- const {
530
- minSimilarity = 0.4,
531
- minLines = 5,
532
- batchSize = 100,
533
- approx = true,
534
- minSharedTokens = 8,
535
- maxCandidatesPerBlock = 100,
536
- streamResults = false,
537
- severity = "all",
538
- groupByFilePair = true,
539
- createClusters = true,
540
- minClusterTokenCost = 1e3,
541
- minClusterFiles = 3,
542
- excludePatterns = [],
543
- confidenceThreshold = 0,
544
- ignoreWhitelist = [],
545
- ...scanOptions
546
- } = finalOptions;
547
- const files = await (0, import_core4.scanFiles)(scanOptions);
548
- const estimatedBlocks = files.length * 3;
549
- logConfiguration(finalOptions, estimatedBlocks);
550
- const results = [];
551
- const READ_BATCH_SIZE = 50;
552
- const fileContents = [];
553
- for (let i = 0; i < files.length; i += READ_BATCH_SIZE) {
554
- const batch = files.slice(i, i + READ_BATCH_SIZE);
555
- const batchContents = await Promise.all(
556
- batch.map(async (file) => ({
557
- file,
558
- content: await (0, import_core4.readFileContent)(file)
559
- }))
560
- );
561
- fileContents.push(...batchContents);
562
- }
563
- const duplicates = await detectDuplicatePatterns(fileContents, {
564
- minSimilarity,
565
- minLines,
566
- batchSize,
567
- approx,
568
- minSharedTokens,
569
- maxCandidatesPerBlock,
570
- streamResults,
571
- excludePatterns,
572
- confidenceThreshold,
573
- ignoreWhitelist,
574
- onProgress: options.onProgress
575
- });
576
- for (const file of files) {
577
- const fileDuplicates = duplicates.filter(
578
- (dup) => dup.file1 === file || dup.file2 === file
579
- );
580
- const issues = fileDuplicates.map((dup) => {
581
- const otherFile = dup.file1 === file ? dup.file2 : dup.file1;
582
- const severity2 = dup.similarity > 0.95 ? import_core4.Severity.Critical : dup.similarity > 0.9 ? import_core4.Severity.Major : import_core4.Severity.Minor;
583
- return {
584
- type: import_core4.IssueType.DuplicatePattern,
585
- severity: severity2,
586
- message: `${dup.patternType} pattern ${Math.round(dup.similarity * 100)}% similar to ${otherFile} (${dup.tokenCost} tokens wasted)`,
587
- location: {
588
- file,
589
- line: dup.file1 === file ? dup.line1 : dup.line2
590
- },
591
- suggestion: getRefactoringSuggestion(dup.patternType, dup.similarity)
592
- };
593
- });
594
- let filteredIssues = issues;
595
- if (severity !== "all") {
596
- const severityMap = {
597
- critical: [import_core4.Severity.Critical],
598
- high: [import_core4.Severity.Critical, import_core4.Severity.Major],
599
- medium: [import_core4.Severity.Critical, import_core4.Severity.Major, import_core4.Severity.Minor]
600
- };
601
- const allowedSeverities = severityMap[severity] || [import_core4.Severity.Critical, import_core4.Severity.Major, import_core4.Severity.Minor];
602
- filteredIssues = issues.filter(
603
- (issue) => allowedSeverities.includes(issue.severity)
604
- );
605
- }
606
- const totalTokenCost = fileDuplicates.reduce(
607
- (sum, dup) => sum + dup.tokenCost,
608
- 0
609
- );
610
- results.push({
611
- fileName: file,
612
- issues: filteredIssues,
613
- metrics: {
614
- tokenCost: totalTokenCost,
615
- consistencyScore: Math.max(0, 1 - fileDuplicates.length * 0.1)
616
- }
617
- });
618
- }
619
- let groups;
620
- let clusters;
621
- if (groupByFilePair) {
622
- groups = groupDuplicatesByFilePair(duplicates);
623
- }
624
- if (createClusters) {
625
- const allClusters = createRefactorClusters(duplicates);
626
- clusters = filterClustersByImpact(
627
- allClusters,
628
- minClusterTokenCost,
629
- minClusterFiles
630
- );
631
- }
632
- return { results, duplicates, files, groups, clusters, config: finalOptions };
633
- }
634
- function generateSummary(results) {
635
- const allIssues = results.flatMap((r) => r.issues);
636
- const totalTokenCost = results.reduce(
637
- (sum, r) => sum + (r.metrics.tokenCost || 0),
638
- 0
639
- );
640
- const patternsByType = {
641
- "api-handler": 0,
642
- validator: 0,
643
- utility: 0,
644
- "class-method": 0,
645
- component: 0,
646
- function: 0,
647
- unknown: 0
648
- };
649
- allIssues.forEach((issue) => {
650
- const match = issue.message.match(/^(\S+(?:-\S+)*) pattern/);
651
- if (match) {
652
- const type = match[1];
653
- patternsByType[type] = (patternsByType[type] || 0) + 1;
654
- }
655
- });
656
- const topDuplicates = allIssues.slice(0, 10).map((issue) => {
657
- const similarityMatch = issue.message.match(/(\d+)% similar/);
658
- const tokenMatch = issue.message.match(/\((\d+) tokens/);
659
- const typeMatch = issue.message.match(/^(\S+(?:-\S+)*) pattern/);
660
- const fileMatch = issue.message.match(/similar to (.+?) \(/);
661
- return {
662
- files: [
663
- {
664
- path: issue.location.file,
665
- startLine: issue.location.line,
666
- endLine: 0
667
- },
668
- {
669
- path: fileMatch?.[1] || "unknown",
670
- startLine: 0,
671
- endLine: 0
672
- }
673
- ],
674
- similarity: similarityMatch ? parseInt(similarityMatch[1]) / 100 : 0,
675
- confidence: similarityMatch ? parseInt(similarityMatch[1]) / 100 : 0,
676
- // Fallback for summary
677
- patternType: typeMatch?.[1] || "unknown",
678
- tokenCost: tokenMatch ? parseInt(tokenMatch[1]) : 0
679
- };
680
- });
681
- return {
682
- totalPatterns: allIssues.length,
683
- totalTokenCost,
684
- patternsByType,
685
- topDuplicates
686
- };
687
- }
688
- // Annotate the CommonJS export names for ESM import in node:
689
- 0 && (module.exports = {
690
- analyzePatterns,
691
- generateSummary,
692
- getSmartDefaults
693
- });
@@ -1,12 +0,0 @@
1
- import {
2
- analyzePatterns,
3
- generateSummary,
4
- getSmartDefaults
5
- } from "./chunk-DNZS4ESD.mjs";
6
- import "./chunk-VGMM3L3O.mjs";
7
- import "./chunk-I6ETJC7L.mjs";
8
- export {
9
- analyzePatterns,
10
- generateSummary,
11
- getSmartDefaults
12
- };