@aiready/pattern-detect 0.16.18 → 0.16.19
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/dist/analyzer-entry-BVz-HnZd.d.mts +119 -0
- package/dist/analyzer-entry-BwuoiCNm.d.ts +119 -0
- package/dist/analyzer-entry.d.mts +3 -0
- package/dist/analyzer-entry.d.ts +3 -0
- package/dist/analyzer-entry.js +810 -0
- package/dist/analyzer-entry.mjs +12 -0
- package/dist/chunk-I6ETJC7L.mjs +179 -0
- package/dist/chunk-THF4RW63.mjs +254 -0
- package/dist/chunk-UB3CGOQ7.mjs +64 -0
- package/dist/chunk-WBBO35SC.mjs +112 -0
- package/dist/chunk-WMOGJFME.mjs +391 -0
- package/dist/cli.js +37 -76
- package/dist/cli.mjs +52 -79
- package/dist/context-rules-entry-y2uJSngh.d.mts +60 -0
- package/dist/context-rules-entry-y2uJSngh.d.ts +60 -0
- package/dist/context-rules-entry.d.mts +2 -0
- package/dist/context-rules-entry.d.ts +2 -0
- package/dist/context-rules-entry.js +207 -0
- package/dist/context-rules-entry.mjs +12 -0
- package/dist/detector-entry.d.mts +14 -0
- package/dist/detector-entry.d.ts +14 -0
- package/dist/detector-entry.js +418 -0
- package/dist/detector-entry.mjs +7 -0
- package/dist/index.d.mts +7 -235
- package/dist/index.d.ts +7 -235
- package/dist/index.mjs +17 -9
- package/dist/scoring-entry.d.mts +23 -0
- package/dist/scoring-entry.d.ts +23 -0
- package/dist/scoring-entry.js +133 -0
- package/dist/scoring-entry.mjs +6 -0
- package/dist/types-DU2mmhwb.d.mts +36 -0
- package/dist/types-DU2mmhwb.d.ts +36 -0
- package/package.json +24 -4
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/detector-entry.ts
|
|
21
|
+
var detector_entry_exports = {};
|
|
22
|
+
__export(detector_entry_exports, {
|
|
23
|
+
detectDuplicatePatterns: () => detectDuplicatePatterns
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(detector_entry_exports);
|
|
26
|
+
|
|
27
|
+
// src/detector.ts
|
|
28
|
+
var import_core2 = require("@aiready/core");
|
|
29
|
+
|
|
30
|
+
// src/context-rules.ts
|
|
31
|
+
var import_core = require("@aiready/core");
|
|
32
|
+
var CONTEXT_RULES = [
|
|
33
|
+
// Test Fixtures - Intentional duplication for test isolation
|
|
34
|
+
{
|
|
35
|
+
name: "test-fixtures",
|
|
36
|
+
detect: (file, code) => {
|
|
37
|
+
const isTestFile = file.includes(".test.") || file.includes(".spec.") || file.includes("__tests__") || file.includes("/test/") || file.includes("/tests/");
|
|
38
|
+
const hasTestFixtures = code.includes("beforeAll") || code.includes("afterAll") || code.includes("beforeEach") || code.includes("afterEach") || code.includes("setUp") || code.includes("tearDown");
|
|
39
|
+
return isTestFile && hasTestFixtures;
|
|
40
|
+
},
|
|
41
|
+
severity: import_core.Severity.Info,
|
|
42
|
+
reason: "Test fixture duplication is intentional for test isolation",
|
|
43
|
+
suggestion: "Consider if shared test setup would improve maintainability without coupling tests"
|
|
44
|
+
},
|
|
45
|
+
// Email/Document Templates - Often intentionally similar for consistency
|
|
46
|
+
{
|
|
47
|
+
name: "templates",
|
|
48
|
+
detect: (file, code) => {
|
|
49
|
+
const isTemplate = file.includes("/templates/") || file.includes("-template") || file.includes("/email-templates/") || file.includes("/emails/");
|
|
50
|
+
const hasTemplateContent = (code.includes("return") || code.includes("export")) && (code.includes("html") || code.includes("subject") || code.includes("body"));
|
|
51
|
+
return isTemplate && hasTemplateContent;
|
|
52
|
+
},
|
|
53
|
+
severity: import_core.Severity.Minor,
|
|
54
|
+
reason: "Template duplication may be intentional for maintainability and branding consistency",
|
|
55
|
+
suggestion: "Extract shared structure only if templates become hard to maintain"
|
|
56
|
+
},
|
|
57
|
+
// E2E/Integration Test Page Objects - Test independence
|
|
58
|
+
{
|
|
59
|
+
name: "e2e-page-objects",
|
|
60
|
+
detect: (file, code) => {
|
|
61
|
+
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/");
|
|
62
|
+
const hasPageObjectPatterns = code.includes("page.") || code.includes("await page") || code.includes("locator") || code.includes("getBy") || code.includes("selector") || code.includes("click(") || code.includes("fill(");
|
|
63
|
+
return isE2ETest && hasPageObjectPatterns;
|
|
64
|
+
},
|
|
65
|
+
severity: import_core.Severity.Minor,
|
|
66
|
+
reason: "E2E test duplication ensures test independence and reduces coupling",
|
|
67
|
+
suggestion: "Consider page object pattern only if duplication causes maintenance issues"
|
|
68
|
+
},
|
|
69
|
+
// Configuration Files - Often necessarily similar by design
|
|
70
|
+
{
|
|
71
|
+
name: "config-files",
|
|
72
|
+
detect: (file) => {
|
|
73
|
+
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");
|
|
74
|
+
},
|
|
75
|
+
severity: import_core.Severity.Minor,
|
|
76
|
+
reason: "Configuration files often have similar structure by design",
|
|
77
|
+
suggestion: "Consider shared config base only if configurations become hard to maintain"
|
|
78
|
+
},
|
|
79
|
+
// Type Definitions - Duplication for type safety and module independence
|
|
80
|
+
{
|
|
81
|
+
name: "type-definitions",
|
|
82
|
+
detect: (file, code) => {
|
|
83
|
+
const isTypeFile = file.endsWith(".d.ts") || file.includes("/types/");
|
|
84
|
+
const hasTypeDefinitions = code.includes("interface ") || code.includes("type ") || code.includes("enum ");
|
|
85
|
+
return isTypeFile && hasTypeDefinitions;
|
|
86
|
+
},
|
|
87
|
+
severity: import_core.Severity.Info,
|
|
88
|
+
reason: "Type duplication may be intentional for module independence and type safety",
|
|
89
|
+
suggestion: "Extract to shared types package only if causing maintenance burden"
|
|
90
|
+
},
|
|
91
|
+
// Migration Scripts - One-off scripts that are similar by nature
|
|
92
|
+
{
|
|
93
|
+
name: "migration-scripts",
|
|
94
|
+
detect: (file) => {
|
|
95
|
+
return file.includes("/migrations/") || file.includes("/migrate/") || file.includes(".migration.");
|
|
96
|
+
},
|
|
97
|
+
severity: import_core.Severity.Info,
|
|
98
|
+
reason: "Migration scripts are typically one-off and intentionally similar",
|
|
99
|
+
suggestion: "Duplication is acceptable for migration scripts"
|
|
100
|
+
},
|
|
101
|
+
// Mock Data - Test data intentionally duplicated
|
|
102
|
+
{
|
|
103
|
+
name: "mock-data",
|
|
104
|
+
detect: (file, code) => {
|
|
105
|
+
const isMockFile = file.includes("/mocks/") || file.includes("/__mocks__/") || file.includes("/fixtures/") || file.includes(".mock.") || file.includes(".fixture.");
|
|
106
|
+
const hasMockData = code.includes("mock") || code.includes("Mock") || code.includes("fixture") || code.includes("stub") || code.includes("export const");
|
|
107
|
+
return isMockFile && hasMockData;
|
|
108
|
+
},
|
|
109
|
+
severity: import_core.Severity.Info,
|
|
110
|
+
reason: "Mock data duplication is expected for comprehensive test coverage",
|
|
111
|
+
suggestion: "Consider shared factories only for complex mock generation"
|
|
112
|
+
},
|
|
113
|
+
// Tool Implementations - Structural Boilerplate
|
|
114
|
+
{
|
|
115
|
+
name: "tool-implementations",
|
|
116
|
+
detect: (file, code) => {
|
|
117
|
+
const isToolFile = file.includes("/tools/") || file.endsWith(".tool.ts") || code.includes("toolDefinitions");
|
|
118
|
+
const hasToolStructure = code.includes("execute") && (code.includes("try") || code.includes("catch"));
|
|
119
|
+
return isToolFile && hasToolStructure;
|
|
120
|
+
},
|
|
121
|
+
severity: import_core.Severity.Info,
|
|
122
|
+
reason: "Tool implementations share structural boilerplate but have distinct business logic",
|
|
123
|
+
suggestion: "Tool duplication is acceptable for boilerplate interface wrappers"
|
|
124
|
+
}
|
|
125
|
+
];
|
|
126
|
+
function calculateSeverity(file1, file2, code, similarity, linesOfCode) {
|
|
127
|
+
for (const rule of CONTEXT_RULES) {
|
|
128
|
+
if (rule.detect(file1, code) || rule.detect(file2, code)) {
|
|
129
|
+
return {
|
|
130
|
+
severity: rule.severity,
|
|
131
|
+
reason: rule.reason,
|
|
132
|
+
suggestion: rule.suggestion,
|
|
133
|
+
matchedRule: rule.name
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (similarity >= 0.95 && linesOfCode >= 30) {
|
|
138
|
+
return {
|
|
139
|
+
severity: import_core.Severity.Critical,
|
|
140
|
+
reason: "Large nearly-identical code blocks waste tokens and create maintenance burden",
|
|
141
|
+
suggestion: "Extract to shared utility module immediately"
|
|
142
|
+
};
|
|
143
|
+
} else if (similarity >= 0.95 && linesOfCode >= 15) {
|
|
144
|
+
return {
|
|
145
|
+
severity: import_core.Severity.Major,
|
|
146
|
+
reason: "Nearly identical code should be consolidated",
|
|
147
|
+
suggestion: "Move to shared utility file"
|
|
148
|
+
};
|
|
149
|
+
} else if (similarity >= 0.85) {
|
|
150
|
+
return {
|
|
151
|
+
severity: import_core.Severity.Major,
|
|
152
|
+
reason: "High similarity indicates significant duplication",
|
|
153
|
+
suggestion: "Extract common logic to shared function"
|
|
154
|
+
};
|
|
155
|
+
} else if (similarity >= 0.7) {
|
|
156
|
+
return {
|
|
157
|
+
severity: import_core.Severity.Minor,
|
|
158
|
+
reason: "Moderate similarity detected",
|
|
159
|
+
suggestion: "Consider extracting shared patterns if code evolves together"
|
|
160
|
+
};
|
|
161
|
+
} else {
|
|
162
|
+
return {
|
|
163
|
+
severity: import_core.Severity.Minor,
|
|
164
|
+
reason: "Minor similarity detected",
|
|
165
|
+
suggestion: "Monitor but refactoring may not be worthwhile"
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// src/detector.ts
|
|
171
|
+
function normalizeCode(code, isPython = false) {
|
|
172
|
+
let normalized = code;
|
|
173
|
+
if (isPython) {
|
|
174
|
+
normalized = normalized.replace(/#.*/g, "");
|
|
175
|
+
} else {
|
|
176
|
+
normalized = normalized.replace(/\/\/.*/g, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
|
177
|
+
}
|
|
178
|
+
return normalized.replace(/['"`]/g, '"').replace(/\s+/g, " ").trim().toLowerCase();
|
|
179
|
+
}
|
|
180
|
+
function extractBlocks(file, content) {
|
|
181
|
+
const isPython = file.toLowerCase().endsWith(".py");
|
|
182
|
+
if (isPython) {
|
|
183
|
+
return extractBlocksPython(file, content);
|
|
184
|
+
}
|
|
185
|
+
const blocks = [];
|
|
186
|
+
const lines = content.split("\n");
|
|
187
|
+
const blockRegex = /^\s*(?:export\s+)?(?:async\s+)?(?:public\s+|private\s+|protected\s+|internal\s+|static\s+|readonly\s+|virtual\s+|abstract\s+|override\s+)*(function|class|interface|type|enum|record|struct|void|func|[a-zA-Z0-9_<>[]]+)\s+([a-zA-Z0-9_]+)(?:\s*\(|(?:\s+extends|\s+implements|\s+where)?\s*\{)|^\s*(?:export\s+)?const\s+([a-zA-Z0-9_]+)\s*=\s*[a-zA-Z0-9_.]+\.object\(|^\s*(app\.(?:get|post|put|delete|patch|use))\(/gm;
|
|
188
|
+
let match;
|
|
189
|
+
while ((match = blockRegex.exec(content)) !== null) {
|
|
190
|
+
const startLine = content.substring(0, match.index).split("\n").length;
|
|
191
|
+
let type;
|
|
192
|
+
let name;
|
|
193
|
+
if (match[1]) {
|
|
194
|
+
type = match[1];
|
|
195
|
+
name = match[2];
|
|
196
|
+
} else if (match[3]) {
|
|
197
|
+
type = "const";
|
|
198
|
+
name = match[3];
|
|
199
|
+
} else {
|
|
200
|
+
type = "handler";
|
|
201
|
+
name = match[4];
|
|
202
|
+
}
|
|
203
|
+
let endLine = -1;
|
|
204
|
+
let openBraces = 0;
|
|
205
|
+
let foundStart = false;
|
|
206
|
+
for (let i = match.index; i < content.length; i++) {
|
|
207
|
+
if (content[i] === "{") {
|
|
208
|
+
openBraces++;
|
|
209
|
+
foundStart = true;
|
|
210
|
+
} else if (content[i] === "}") {
|
|
211
|
+
openBraces--;
|
|
212
|
+
}
|
|
213
|
+
if (foundStart && openBraces === 0) {
|
|
214
|
+
endLine = content.substring(0, i + 1).split("\n").length;
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (endLine === -1) {
|
|
219
|
+
const remaining = content.slice(match.index);
|
|
220
|
+
const nextLineMatch = remaining.indexOf("\n");
|
|
221
|
+
if (nextLineMatch !== -1) {
|
|
222
|
+
endLine = startLine;
|
|
223
|
+
} else {
|
|
224
|
+
endLine = lines.length;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
endLine = Math.max(startLine, endLine);
|
|
228
|
+
const blockCode = lines.slice(startLine - 1, endLine).join("\n");
|
|
229
|
+
const tokens = (0, import_core2.estimateTokens)(blockCode);
|
|
230
|
+
blocks.push({
|
|
231
|
+
file,
|
|
232
|
+
startLine,
|
|
233
|
+
endLine,
|
|
234
|
+
code: blockCode,
|
|
235
|
+
tokens,
|
|
236
|
+
patternType: inferPatternType(type, name)
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
return blocks;
|
|
240
|
+
}
|
|
241
|
+
function extractBlocksPython(file, content) {
|
|
242
|
+
const blocks = [];
|
|
243
|
+
const lines = content.split("\n");
|
|
244
|
+
const blockRegex = /^\s*(?:async\s+)?(def|class)\s+([a-zA-Z0-9_]+)/gm;
|
|
245
|
+
let match;
|
|
246
|
+
while ((match = blockRegex.exec(content)) !== null) {
|
|
247
|
+
const startLinePos = content.substring(0, match.index).split("\n").length;
|
|
248
|
+
const startLineIdx = startLinePos - 1;
|
|
249
|
+
const initialIndent = lines[startLineIdx].search(/\S/);
|
|
250
|
+
let endLineIdx = startLineIdx;
|
|
251
|
+
for (let i = startLineIdx + 1; i < lines.length; i++) {
|
|
252
|
+
const line = lines[i];
|
|
253
|
+
if (line.trim().length === 0) {
|
|
254
|
+
endLineIdx = i;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
const currentIndent = line.search(/\S/);
|
|
258
|
+
if (currentIndent <= initialIndent) {
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
endLineIdx = i;
|
|
262
|
+
}
|
|
263
|
+
while (endLineIdx > startLineIdx && lines[endLineIdx].trim().length === 0) {
|
|
264
|
+
endLineIdx--;
|
|
265
|
+
}
|
|
266
|
+
const blockCode = lines.slice(startLineIdx, endLineIdx + 1).join("\n");
|
|
267
|
+
const tokens = (0, import_core2.estimateTokens)(blockCode);
|
|
268
|
+
blocks.push({
|
|
269
|
+
file,
|
|
270
|
+
startLine: startLinePos,
|
|
271
|
+
endLine: endLineIdx + 1,
|
|
272
|
+
code: blockCode,
|
|
273
|
+
tokens,
|
|
274
|
+
patternType: inferPatternType(match[1], match[2])
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
return blocks;
|
|
278
|
+
}
|
|
279
|
+
function inferPatternType(keyword, name) {
|
|
280
|
+
const n = name.toLowerCase();
|
|
281
|
+
if (keyword === "handler" || n.includes("handler") || n.includes("controller") || n.startsWith("app.")) {
|
|
282
|
+
return "api-handler";
|
|
283
|
+
}
|
|
284
|
+
if (n.includes("validate") || n.includes("schema")) return "validator";
|
|
285
|
+
if (n.includes("util") || n.includes("helper")) return "utility";
|
|
286
|
+
if (keyword === "class") return "class-method";
|
|
287
|
+
if (n.match(/^[A-Z]/)) return "component";
|
|
288
|
+
if (keyword === "function") return "function";
|
|
289
|
+
return "unknown";
|
|
290
|
+
}
|
|
291
|
+
function calculateSimilarity(a, b) {
|
|
292
|
+
if (a === b) return 1;
|
|
293
|
+
const tokensA = a.split(/[^a-zA-Z0-9]+/).filter((t) => t.length > 0);
|
|
294
|
+
const tokensB = b.split(/[^a-zA-Z0-9]+/).filter((t) => t.length > 0);
|
|
295
|
+
if (tokensA.length === 0 || tokensB.length === 0) return 0;
|
|
296
|
+
const setA = new Set(tokensA);
|
|
297
|
+
const setB = new Set(tokensB);
|
|
298
|
+
const intersection = new Set([...setA].filter((x) => setB.has(x)));
|
|
299
|
+
const union = /* @__PURE__ */ new Set([...setA, ...setB]);
|
|
300
|
+
return intersection.size / union.size;
|
|
301
|
+
}
|
|
302
|
+
function calculateConfidence(similarity, tokens, lines) {
|
|
303
|
+
let confidence = similarity;
|
|
304
|
+
if (lines > 20) confidence += 0.05;
|
|
305
|
+
if (tokens > 200) confidence += 0.05;
|
|
306
|
+
if (lines < 5) confidence -= 0.1;
|
|
307
|
+
return Math.max(0, Math.min(1, confidence));
|
|
308
|
+
}
|
|
309
|
+
async function detectDuplicatePatterns(fileContents, options) {
|
|
310
|
+
const {
|
|
311
|
+
minSimilarity,
|
|
312
|
+
minLines,
|
|
313
|
+
streamResults,
|
|
314
|
+
onProgress,
|
|
315
|
+
excludePatterns = [],
|
|
316
|
+
confidenceThreshold = 0,
|
|
317
|
+
ignoreWhitelist = []
|
|
318
|
+
} = options;
|
|
319
|
+
const allBlocks = [];
|
|
320
|
+
const excludeRegexes = excludePatterns.map((p) => new RegExp(p, "i"));
|
|
321
|
+
for (const { file, content } of fileContents) {
|
|
322
|
+
const blocks = extractBlocks(file, content);
|
|
323
|
+
for (const b of blocks) {
|
|
324
|
+
if (b.endLine - b.startLine + 1 < minLines) continue;
|
|
325
|
+
const isExcluded = excludeRegexes.some((regex) => regex.test(b.code));
|
|
326
|
+
if (isExcluded) continue;
|
|
327
|
+
allBlocks.push(b);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const duplicates = [];
|
|
331
|
+
const totalBlocks = allBlocks.length;
|
|
332
|
+
let comparisons = 0;
|
|
333
|
+
const totalComparisons = totalBlocks * (totalBlocks - 1) / 2;
|
|
334
|
+
if (onProgress) {
|
|
335
|
+
onProgress(
|
|
336
|
+
0,
|
|
337
|
+
totalComparisons,
|
|
338
|
+
`Starting duplicate detection on ${totalBlocks} blocks...`
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
for (let i = 0; i < allBlocks.length; i++) {
|
|
342
|
+
if (i % 50 === 0 && i > 0) {
|
|
343
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
344
|
+
if (onProgress) {
|
|
345
|
+
onProgress(
|
|
346
|
+
comparisons,
|
|
347
|
+
totalComparisons,
|
|
348
|
+
`Analyzing blocks (${i}/${totalBlocks})...`
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const b1 = allBlocks[i];
|
|
353
|
+
const isPython1 = b1.file.toLowerCase().endsWith(".py");
|
|
354
|
+
const norm1 = normalizeCode(b1.code, isPython1);
|
|
355
|
+
for (let j = i + 1; j < allBlocks.length; j++) {
|
|
356
|
+
comparisons++;
|
|
357
|
+
const b2 = allBlocks[j];
|
|
358
|
+
if (b1.file === b2.file) continue;
|
|
359
|
+
const isWhitelisted = ignoreWhitelist.some((pattern) => {
|
|
360
|
+
return b1.file.includes(pattern) && b2.file.includes(pattern) || pattern === `${b1.file}::${b2.file}` || pattern === `${b2.file}::${b1.file}`;
|
|
361
|
+
});
|
|
362
|
+
if (isWhitelisted) continue;
|
|
363
|
+
const isPython2 = b2.file.toLowerCase().endsWith(".py");
|
|
364
|
+
const norm2 = normalizeCode(b2.code, isPython2);
|
|
365
|
+
const sim = calculateSimilarity(norm1, norm2);
|
|
366
|
+
if (sim >= minSimilarity) {
|
|
367
|
+
const confidence = calculateConfidence(
|
|
368
|
+
sim,
|
|
369
|
+
b1.tokens,
|
|
370
|
+
b1.endLine - b1.startLine + 1
|
|
371
|
+
);
|
|
372
|
+
if (confidence < confidenceThreshold) continue;
|
|
373
|
+
const { severity, reason, suggestion, matchedRule } = calculateSeverity(
|
|
374
|
+
b1.file,
|
|
375
|
+
b2.file,
|
|
376
|
+
b1.code,
|
|
377
|
+
sim,
|
|
378
|
+
b1.endLine - b1.startLine + 1
|
|
379
|
+
);
|
|
380
|
+
const dup = {
|
|
381
|
+
file1: b1.file,
|
|
382
|
+
line1: b1.startLine,
|
|
383
|
+
endLine1: b1.endLine,
|
|
384
|
+
file2: b2.file,
|
|
385
|
+
line2: b2.startLine,
|
|
386
|
+
endLine2: b2.endLine,
|
|
387
|
+
code1: b1.code,
|
|
388
|
+
code2: b2.code,
|
|
389
|
+
similarity: sim,
|
|
390
|
+
confidence,
|
|
391
|
+
patternType: b1.patternType,
|
|
392
|
+
tokenCost: b1.tokens + b2.tokens,
|
|
393
|
+
severity,
|
|
394
|
+
reason,
|
|
395
|
+
suggestion,
|
|
396
|
+
matchedRule
|
|
397
|
+
};
|
|
398
|
+
duplicates.push(dup);
|
|
399
|
+
if (streamResults)
|
|
400
|
+
console.log(
|
|
401
|
+
`[DUPLICATE] ${dup.file1}:${dup.line1} <-> ${dup.file2}:${dup.line2} (${Math.round(sim * 100)}%, conf: ${Math.round(confidence * 100)}%)`
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
if (onProgress) {
|
|
407
|
+
onProgress(
|
|
408
|
+
totalComparisons,
|
|
409
|
+
totalComparisons,
|
|
410
|
+
`Duplicate detection complete. Found ${duplicates.length} patterns.`
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
return duplicates.sort((a, b) => b.similarity - a.similarity);
|
|
414
|
+
}
|
|
415
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
416
|
+
0 && (module.exports = {
|
|
417
|
+
detectDuplicatePatterns
|
|
418
|
+
});
|
package/dist/index.d.mts
CHANGED
|
@@ -1,242 +1,14 @@
|
|
|
1
|
-
import { ToolProvider
|
|
1
|
+
import { ToolProvider } from '@aiready/core';
|
|
2
2
|
export { Severity } from '@aiready/core';
|
|
3
|
+
export { D as DuplicateGroup, P as PatternDetectOptions, a as PatternSummary, R as RefactorCluster, b as analyzePatterns, c as createRefactorClusters, f as filterClustersByImpact, g as generateSummary, d as getSmartDefaults, e as groupDuplicatesByFilePair } from './analyzer-entry-BVz-HnZd.mjs';
|
|
4
|
+
export { detectDuplicatePatterns } from './detector-entry.mjs';
|
|
5
|
+
export { calculatePatternScore } from './scoring-entry.mjs';
|
|
6
|
+
export { C as CONTEXT_RULES, a as ContextRule, c as calculateSeverity, f as filterBySeverity, g as getSeverityLabel, b as getSeverityThreshold } from './context-rules-entry-y2uJSngh.mjs';
|
|
7
|
+
export { D as DuplicatePattern, P as PatternType } from './types-DU2mmhwb.mjs';
|
|
3
8
|
|
|
4
9
|
/**
|
|
5
10
|
* Pattern Detection Tool Provider
|
|
6
11
|
*/
|
|
7
12
|
declare const PatternDetectProvider: ToolProvider;
|
|
8
13
|
|
|
9
|
-
|
|
10
|
-
interface DuplicatePattern {
|
|
11
|
-
file1: string;
|
|
12
|
-
line1: number;
|
|
13
|
-
endLine1: number;
|
|
14
|
-
file2: string;
|
|
15
|
-
line2: number;
|
|
16
|
-
endLine2: number;
|
|
17
|
-
code1: string;
|
|
18
|
-
code2: string;
|
|
19
|
-
similarity: number;
|
|
20
|
-
confidence: number;
|
|
21
|
-
patternType: PatternType;
|
|
22
|
-
tokenCost: number;
|
|
23
|
-
severity: Severity;
|
|
24
|
-
reason?: string;
|
|
25
|
-
suggestion?: string;
|
|
26
|
-
matchedRule?: string;
|
|
27
|
-
}
|
|
28
|
-
interface DetectionOptions {
|
|
29
|
-
minSimilarity: number;
|
|
30
|
-
minLines: number;
|
|
31
|
-
batchSize: number;
|
|
32
|
-
approx: boolean;
|
|
33
|
-
minSharedTokens: number;
|
|
34
|
-
maxCandidatesPerBlock: number;
|
|
35
|
-
streamResults: boolean;
|
|
36
|
-
excludePatterns?: string[];
|
|
37
|
-
confidenceThreshold?: number;
|
|
38
|
-
ignoreWhitelist?: string[];
|
|
39
|
-
onProgress?: (processed: number, total: number, message: string) => void;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Detect duplicate patterns across files
|
|
44
|
-
*
|
|
45
|
-
* @param fileContents - Array of file contents to analyze.
|
|
46
|
-
* @param options - Configuration for duplicate detection (thresholds, progress, etc).
|
|
47
|
-
* @returns Promise resolving to an array of detected duplicate patterns sorted by similarity.
|
|
48
|
-
*/
|
|
49
|
-
declare function detectDuplicatePatterns(fileContents: FileContent[], options: DetectionOptions): Promise<DuplicatePattern[]>;
|
|
50
|
-
|
|
51
|
-
interface DuplicateGroup {
|
|
52
|
-
filePair: string;
|
|
53
|
-
severity: Severity;
|
|
54
|
-
occurrences: number;
|
|
55
|
-
totalTokenCost: number;
|
|
56
|
-
averageSimilarity: number;
|
|
57
|
-
patternTypes: Set<PatternType>;
|
|
58
|
-
lineRanges: Array<{
|
|
59
|
-
file1: {
|
|
60
|
-
start: number;
|
|
61
|
-
end: number;
|
|
62
|
-
};
|
|
63
|
-
file2: {
|
|
64
|
-
start: number;
|
|
65
|
-
end: number;
|
|
66
|
-
};
|
|
67
|
-
}>;
|
|
68
|
-
}
|
|
69
|
-
interface RefactorCluster {
|
|
70
|
-
id: string;
|
|
71
|
-
name: string;
|
|
72
|
-
files: string[];
|
|
73
|
-
severity: Severity;
|
|
74
|
-
duplicateCount: number;
|
|
75
|
-
totalTokenCost: number;
|
|
76
|
-
averageSimilarity: number;
|
|
77
|
-
reason?: string;
|
|
78
|
-
suggestion?: string;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Group raw duplicates by file pairs to reduce noise
|
|
82
|
-
*/
|
|
83
|
-
declare function groupDuplicatesByFilePair(duplicates: DuplicatePattern[]): DuplicateGroup[];
|
|
84
|
-
/**
|
|
85
|
-
* Create clusters of highly related files (refactor targets)
|
|
86
|
-
* Uses a simple connected components algorithm
|
|
87
|
-
* @param duplicates - Array of duplicate patterns to cluster
|
|
88
|
-
* @returns Array of refactor clusters
|
|
89
|
-
*/
|
|
90
|
-
declare function createRefactorClusters(duplicates: DuplicatePattern[]): RefactorCluster[];
|
|
91
|
-
/**
|
|
92
|
-
* Filter clusters by impact threshold
|
|
93
|
-
* @param clusters - Array of refactor clusters to filter
|
|
94
|
-
* @param minTokenCost - Minimum token cost threshold (default: 1000)
|
|
95
|
-
* @param minFiles - Minimum number of files in cluster (default: 3)
|
|
96
|
-
* @returns Filtered array of refactor clusters
|
|
97
|
-
*/
|
|
98
|
-
declare function filterClustersByImpact(clusters: RefactorCluster[], minTokenCost?: number, minFiles?: number): RefactorCluster[];
|
|
99
|
-
|
|
100
|
-
interface PatternDetectOptions extends ScanOptions {
|
|
101
|
-
minSimilarity?: number;
|
|
102
|
-
minLines?: number;
|
|
103
|
-
batchSize?: number;
|
|
104
|
-
approx?: boolean;
|
|
105
|
-
minSharedTokens?: number;
|
|
106
|
-
maxCandidatesPerBlock?: number;
|
|
107
|
-
streamResults?: boolean;
|
|
108
|
-
severity?: string;
|
|
109
|
-
includeTests?: boolean;
|
|
110
|
-
useSmartDefaults?: boolean;
|
|
111
|
-
groupByFilePair?: boolean;
|
|
112
|
-
createClusters?: boolean;
|
|
113
|
-
minClusterTokenCost?: number;
|
|
114
|
-
minClusterFiles?: number;
|
|
115
|
-
excludePatterns?: string[];
|
|
116
|
-
confidenceThreshold?: number;
|
|
117
|
-
ignoreWhitelist?: string[];
|
|
118
|
-
onProgress?: (processed: number, total: number, message: string) => void;
|
|
119
|
-
}
|
|
120
|
-
interface PatternSummary {
|
|
121
|
-
totalPatterns: number;
|
|
122
|
-
totalTokenCost: number;
|
|
123
|
-
patternsByType: Record<PatternType, number>;
|
|
124
|
-
topDuplicates: Array<{
|
|
125
|
-
files: Array<{
|
|
126
|
-
path: string;
|
|
127
|
-
startLine: number;
|
|
128
|
-
endLine: number;
|
|
129
|
-
}>;
|
|
130
|
-
similarity: number;
|
|
131
|
-
patternType: PatternType;
|
|
132
|
-
tokenCost: number;
|
|
133
|
-
}>;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Determine smart defaults based on repository size estimation.
|
|
137
|
-
*
|
|
138
|
-
* @param directory - The directory to analyze for size.
|
|
139
|
-
* @param userOptions - User-provided option overrides.
|
|
140
|
-
* @returns Promise resolving to optimal detection options.
|
|
141
|
-
*/
|
|
142
|
-
declare function getSmartDefaults(directory: string, userOptions: Partial<PatternDetectOptions>): Promise<PatternDetectOptions>;
|
|
143
|
-
/**
|
|
144
|
-
* Main entry point for pattern detection analysis.
|
|
145
|
-
*
|
|
146
|
-
* @param options - Configuration including rootDir and detection parameters.
|
|
147
|
-
* @returns Promise resolving to the comprehensive pattern detect report.
|
|
148
|
-
* @lastUpdated 2026-03-18
|
|
149
|
-
*/
|
|
150
|
-
declare function analyzePatterns(options: PatternDetectOptions): Promise<{
|
|
151
|
-
results: AnalysisResult[];
|
|
152
|
-
duplicates: DuplicatePattern[];
|
|
153
|
-
files: string[];
|
|
154
|
-
groups?: DuplicateGroup[];
|
|
155
|
-
clusters?: RefactorCluster[];
|
|
156
|
-
config: PatternDetectOptions;
|
|
157
|
-
}>;
|
|
158
|
-
/**
|
|
159
|
-
* Generate a summary of pattern detection results.
|
|
160
|
-
*
|
|
161
|
-
* @param results - Array of file-level analysis results.
|
|
162
|
-
* @returns Consolidated pattern summary object.
|
|
163
|
-
*/
|
|
164
|
-
declare function generateSummary(results: AnalysisResult[]): PatternSummary;
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Calculate AI Readiness Score for pattern duplication (0-100)
|
|
168
|
-
*
|
|
169
|
-
* Based on:
|
|
170
|
-
* - Number of duplicates per file
|
|
171
|
-
* - Token waste per file
|
|
172
|
-
* - High-impact duplicates (>1000 tokens or >70% similarity)
|
|
173
|
-
*
|
|
174
|
-
* Includes business value metrics:
|
|
175
|
-
* - Estimated monthly cost of token waste
|
|
176
|
-
* - Estimated developer hours to fix
|
|
177
|
-
*
|
|
178
|
-
* @param duplicates - Array of detected duplicate patterns.
|
|
179
|
-
* @param totalFilesAnalyzed - Total count of files scanned.
|
|
180
|
-
* @param costConfig - Optional configuration for business value calculations.
|
|
181
|
-
* @returns Standardized scoring output for pattern detection.
|
|
182
|
-
*/
|
|
183
|
-
declare function calculatePatternScore(duplicates: DuplicatePattern[], totalFilesAnalyzed: number, costConfig?: Partial<CostConfig>): ToolScoringOutput;
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Context-aware severity detection for duplicate patterns
|
|
187
|
-
* Identifies intentional duplication patterns and adjusts severity accordingly
|
|
188
|
-
*/
|
|
189
|
-
interface ContextRule {
|
|
190
|
-
name: string;
|
|
191
|
-
detect: (file: string, code: string) => boolean;
|
|
192
|
-
severity: Severity;
|
|
193
|
-
reason: string;
|
|
194
|
-
suggestion?: string;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Context rules for detecting intentional or acceptable duplication patterns
|
|
198
|
-
* Rules are checked in order - first match wins
|
|
199
|
-
*/
|
|
200
|
-
declare const CONTEXT_RULES: ContextRule[];
|
|
201
|
-
/**
|
|
202
|
-
* Calculate severity based on context rules and code characteristics
|
|
203
|
-
*
|
|
204
|
-
* @param file1 - First file path in the duplicate pair.
|
|
205
|
-
* @param file2 - Second file path in the duplicate pair.
|
|
206
|
-
* @param code - Snippet of the duplicated code.
|
|
207
|
-
* @param similarity - The calculated similarity score (0-1).
|
|
208
|
-
* @param linesOfCode - Number of lines in the duplicated block.
|
|
209
|
-
* @returns An object containing the severity level and reasoning.
|
|
210
|
-
*/
|
|
211
|
-
declare function calculateSeverity(file1: string, file2: string, code: string, similarity: number, linesOfCode: number): {
|
|
212
|
-
severity: Severity;
|
|
213
|
-
reason?: string;
|
|
214
|
-
suggestion?: string;
|
|
215
|
-
matchedRule?: string;
|
|
216
|
-
};
|
|
217
|
-
/**
|
|
218
|
-
* Get a human-readable severity label with emoji
|
|
219
|
-
*
|
|
220
|
-
* @param severity - The severity level to label.
|
|
221
|
-
* @returns Formatted label string for UI display.
|
|
222
|
-
*/
|
|
223
|
-
declare function getSeverityLabel(severity: Severity): string;
|
|
224
|
-
/**
|
|
225
|
-
* Filter duplicates by minimum severity threshold
|
|
226
|
-
*
|
|
227
|
-
* @param duplicates - List of items with a severity property.
|
|
228
|
-
* @param minSeverity - Minimum threshold for inclusion.
|
|
229
|
-
* @returns Filtered list of items.
|
|
230
|
-
*/
|
|
231
|
-
declare function filterBySeverity<T extends {
|
|
232
|
-
severity: Severity;
|
|
233
|
-
}>(duplicates: T[], minSeverity: Severity): T[];
|
|
234
|
-
/**
|
|
235
|
-
* Get numerical similarity threshold associated with a severity level
|
|
236
|
-
*
|
|
237
|
-
* @param severity - The severity level to look up.
|
|
238
|
-
* @returns Minimum similarity value for this severity.
|
|
239
|
-
*/
|
|
240
|
-
declare function getSeverityThreshold(severity: Severity): number;
|
|
241
|
-
|
|
242
|
-
export { CONTEXT_RULES, type ContextRule, type DuplicateGroup, type DuplicatePattern, type PatternDetectOptions, PatternDetectProvider, type PatternSummary, type PatternType, type RefactorCluster, analyzePatterns, calculatePatternScore, calculateSeverity, createRefactorClusters, detectDuplicatePatterns, filterBySeverity, filterClustersByImpact, generateSummary, getSeverityLabel, getSeverityThreshold, getSmartDefaults, groupDuplicatesByFilePair };
|
|
14
|
+
export { PatternDetectProvider };
|