@aiready/pattern-detect 0.9.2 → 0.9.3

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/index.js CHANGED
@@ -31,8 +31,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
33
  analyzePatterns: () => analyzePatterns,
34
+ calculateSeverity: () => calculateSeverity,
34
35
  detectDuplicatePatterns: () => detectDuplicatePatterns,
36
+ filterBySeverity: () => filterBySeverity,
35
37
  generateSummary: () => generateSummary,
38
+ getSeverityLabel: () => getSeverityLabel,
36
39
  getSmartDefaults: () => getSmartDefaults
37
40
  });
38
41
  module.exports = __toCommonJS(index_exports);
@@ -40,6 +43,154 @@ var import_core2 = require("@aiready/core");
40
43
 
41
44
  // src/detector.ts
42
45
  var import_core = require("@aiready/core");
46
+
47
+ // src/context-rules.ts
48
+ var CONTEXT_RULES = [
49
+ // Test Fixtures - Intentional duplication for test isolation
50
+ {
51
+ name: "test-fixtures",
52
+ detect: (file, code) => {
53
+ const isTestFile = file.includes(".test.") || file.includes(".spec.") || file.includes("__tests__") || file.includes("/test/") || file.includes("/tests/");
54
+ const hasTestFixtures = code.includes("beforeAll") || code.includes("afterAll") || code.includes("beforeEach") || code.includes("afterEach") || code.includes("setUp") || code.includes("tearDown");
55
+ return isTestFile && hasTestFixtures;
56
+ },
57
+ severity: "info",
58
+ reason: "Test fixture duplication is intentional for test isolation",
59
+ suggestion: "Consider if shared test setup would improve maintainability without coupling tests"
60
+ },
61
+ // Email/Document Templates - Often intentionally similar for consistency
62
+ {
63
+ name: "templates",
64
+ detect: (file, code) => {
65
+ const isTemplate = file.includes("/templates/") || file.includes("-template") || file.includes("/email-templates/") || file.includes("/emails/");
66
+ const hasTemplateContent = (code.includes("return") || code.includes("export")) && (code.includes("html") || code.includes("subject") || code.includes("body"));
67
+ return isTemplate && hasTemplateContent;
68
+ },
69
+ severity: "low",
70
+ reason: "Template duplication may be intentional for maintainability and branding consistency",
71
+ suggestion: "Extract shared structure only if templates become hard to maintain"
72
+ },
73
+ // E2E/Integration Test Page Objects - Test independence
74
+ {
75
+ name: "e2e-page-objects",
76
+ detect: (file, code) => {
77
+ 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/");
78
+ const hasPageObjectPatterns = code.includes("page.") || code.includes("await page") || code.includes("locator") || code.includes("getBy") || code.includes("selector") || code.includes("click(") || code.includes("fill(");
79
+ return isE2ETest && hasPageObjectPatterns;
80
+ },
81
+ severity: "low",
82
+ reason: "E2E test duplication ensures test independence and reduces coupling",
83
+ suggestion: "Consider page object pattern only if duplication causes maintenance issues"
84
+ },
85
+ // Configuration Files - Often necessarily similar by design
86
+ {
87
+ name: "config-files",
88
+ detect: (file) => {
89
+ 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");
90
+ },
91
+ severity: "low",
92
+ reason: "Configuration files often have similar structure by design",
93
+ suggestion: "Consider shared config base only if configurations become hard to maintain"
94
+ },
95
+ // Type Definitions - Duplication for type safety and module independence
96
+ {
97
+ name: "type-definitions",
98
+ detect: (file, code) => {
99
+ const isTypeFile = file.endsWith(".d.ts") || file.includes("/types/");
100
+ const hasTypeDefinitions = code.includes("interface ") || code.includes("type ") || code.includes("enum ");
101
+ return isTypeFile && hasTypeDefinitions;
102
+ },
103
+ severity: "info",
104
+ reason: "Type duplication may be intentional for module independence and type safety",
105
+ suggestion: "Extract to shared types package only if causing maintenance burden"
106
+ },
107
+ // Migration Scripts - One-off scripts that are similar by nature
108
+ {
109
+ name: "migration-scripts",
110
+ detect: (file) => {
111
+ return file.includes("/migrations/") || file.includes("/migrate/") || file.includes(".migration.");
112
+ },
113
+ severity: "info",
114
+ reason: "Migration scripts are typically one-off and intentionally similar",
115
+ suggestion: "Duplication is acceptable for migration scripts"
116
+ },
117
+ // Mock Data - Test data intentionally duplicated
118
+ {
119
+ name: "mock-data",
120
+ detect: (file, code) => {
121
+ const isMockFile = file.includes("/mocks/") || file.includes("/__mocks__/") || file.includes("/fixtures/") || file.includes(".mock.") || file.includes(".fixture.");
122
+ const hasMockData = code.includes("mock") || code.includes("Mock") || code.includes("fixture") || code.includes("stub") || code.includes("export const");
123
+ return isMockFile && hasMockData;
124
+ },
125
+ severity: "info",
126
+ reason: "Mock data duplication is expected for comprehensive test coverage",
127
+ suggestion: "Consider shared factories only for complex mock generation"
128
+ }
129
+ ];
130
+ function calculateSeverity(file1, file2, code, similarity, linesOfCode) {
131
+ for (const rule of CONTEXT_RULES) {
132
+ if (rule.detect(file1, code) || rule.detect(file2, code)) {
133
+ return {
134
+ severity: rule.severity,
135
+ reason: rule.reason,
136
+ suggestion: rule.suggestion,
137
+ matchedRule: rule.name
138
+ };
139
+ }
140
+ }
141
+ if (similarity >= 0.95 && linesOfCode >= 30) {
142
+ return {
143
+ severity: "critical",
144
+ reason: "Large nearly-identical code blocks waste tokens and create maintenance burden",
145
+ suggestion: "Extract to shared utility module immediately"
146
+ };
147
+ } else if (similarity >= 0.95 && linesOfCode >= 15) {
148
+ return {
149
+ severity: "high",
150
+ reason: "Nearly identical code should be consolidated",
151
+ suggestion: "Move to shared utility file"
152
+ };
153
+ } else if (similarity >= 0.85) {
154
+ return {
155
+ severity: "high",
156
+ reason: "High similarity indicates significant duplication",
157
+ suggestion: "Extract common logic to shared function"
158
+ };
159
+ } else if (similarity >= 0.7) {
160
+ return {
161
+ severity: "medium",
162
+ reason: "Moderate similarity detected",
163
+ suggestion: "Consider extracting shared patterns if code evolves together"
164
+ };
165
+ } else {
166
+ return {
167
+ severity: "low",
168
+ reason: "Minor similarity detected",
169
+ suggestion: "Monitor but refactoring may not be worthwhile"
170
+ };
171
+ }
172
+ }
173
+ function getSeverityLabel(severity) {
174
+ const labels = {
175
+ critical: "\u{1F534} CRITICAL",
176
+ high: "\u{1F7E1} HIGH",
177
+ medium: "\u{1F535} MEDIUM",
178
+ low: "\u26AA LOW",
179
+ info: "\u2139\uFE0F INFO"
180
+ };
181
+ return labels[severity];
182
+ }
183
+ function filterBySeverity(duplicates, minSeverity) {
184
+ const severityOrder = ["info", "low", "medium", "high", "critical"];
185
+ const minIndex = severityOrder.indexOf(minSeverity);
186
+ if (minIndex === -1) return duplicates;
187
+ return duplicates.filter((dup) => {
188
+ const dupIndex = severityOrder.indexOf(dup.severity);
189
+ return dupIndex >= minIndex;
190
+ });
191
+ }
192
+
193
+ // src/detector.ts
43
194
  function categorizePattern(code) {
44
195
  const lower = code.toLowerCase();
45
196
  if (lower.includes("request") && lower.includes("response") || lower.includes("router.") || lower.includes("app.get") || lower.includes("app.post") || lower.includes("express") || lower.includes("ctx.body")) {
@@ -255,6 +406,13 @@ async function detectDuplicatePatterns(files, options) {
255
406
  const block2 = allBlocks[j];
256
407
  const similarity = jaccardSimilarity(blockTokens[i], blockTokens[j]);
257
408
  if (similarity >= minSimilarity) {
409
+ const { severity, reason, suggestion, matchedRule } = calculateSeverity(
410
+ block1.file,
411
+ block2.file,
412
+ block1.content,
413
+ similarity,
414
+ block1.linesOfCode
415
+ );
258
416
  const duplicate = {
259
417
  file1: block1.file,
260
418
  file2: block2.file,
@@ -266,7 +424,11 @@ async function detectDuplicatePatterns(files, options) {
266
424
  snippet: block1.content.split("\n").slice(0, 5).join("\n") + "\n...",
267
425
  patternType: block1.patternType,
268
426
  tokenCost: block1.tokenCost + block2.tokenCost,
269
- linesOfCode: block1.linesOfCode
427
+ linesOfCode: block1.linesOfCode,
428
+ severity,
429
+ reason,
430
+ suggestion,
431
+ matchedRule
270
432
  };
271
433
  duplicates.push(duplicate);
272
434
  if (streamResults) {
@@ -285,6 +447,13 @@ async function detectDuplicatePatterns(files, options) {
285
447
  if (block1.file === block2.file) continue;
286
448
  const similarity = jaccardSimilarity(blockTokens[i], blockTokens[j]);
287
449
  if (similarity >= minSimilarity) {
450
+ const { severity, reason, suggestion, matchedRule } = calculateSeverity(
451
+ block1.file,
452
+ block2.file,
453
+ block1.content,
454
+ similarity,
455
+ block1.linesOfCode
456
+ );
288
457
  const duplicate = {
289
458
  file1: block1.file,
290
459
  file2: block2.file,
@@ -296,7 +465,11 @@ async function detectDuplicatePatterns(files, options) {
296
465
  snippet: block1.content.split("\n").slice(0, 5).join("\n") + "\n...",
297
466
  patternType: block1.patternType,
298
467
  tokenCost: block1.tokenCost + block2.tokenCost,
299
- linesOfCode: block1.linesOfCode
468
+ linesOfCode: block1.linesOfCode,
469
+ severity,
470
+ reason,
471
+ suggestion,
472
+ matchedRule
300
473
  };
301
474
  duplicates.push(duplicate);
302
475
  if (streamResults) {
@@ -528,7 +701,10 @@ function generateSummary(results) {
528
701
  // Annotate the CommonJS export names for ESM import in node:
529
702
  0 && (module.exports = {
530
703
  analyzePatterns,
704
+ calculateSeverity,
531
705
  detectDuplicatePatterns,
706
+ filterBySeverity,
532
707
  generateSummary,
708
+ getSeverityLabel,
533
709
  getSmartDefaults
534
710
  });
package/dist/index.mjs CHANGED
@@ -1,12 +1,18 @@
1
1
  import {
2
2
  analyzePatterns,
3
+ calculateSeverity,
3
4
  detectDuplicatePatterns,
5
+ filterBySeverity,
4
6
  generateSummary,
7
+ getSeverityLabel,
5
8
  getSmartDefaults
6
- } from "./chunk-65G3HXLQ.mjs";
9
+ } from "./chunk-H73HEG7M.mjs";
7
10
  export {
8
11
  analyzePatterns,
12
+ calculateSeverity,
9
13
  detectDuplicatePatterns,
14
+ filterBySeverity,
10
15
  generateSummary,
16
+ getSeverityLabel,
11
17
  getSmartDefaults
12
18
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiready/pattern-detect",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
4
4
  "description": "Semantic duplicate pattern detection for AI-generated code - finds similar implementations that waste AI context tokens",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",