@aiready/pattern-detect 0.17.13 → 0.17.14
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/index.js +34 -1
- package/dist/analyzer-entry/index.mjs +4 -4
- package/dist/chunk-3CNHAYOD.mjs +499 -0
- package/dist/chunk-6JNGAY7M.mjs +514 -0
- package/dist/chunk-C4ZGC4KA.mjs +514 -0
- package/dist/chunk-G3GZFYRI.mjs +144 -0
- package/dist/chunk-RH5JPWEC.mjs +143 -0
- package/dist/chunk-UKQFCUQA.mjs +323 -0
- package/dist/cli.js +34 -1
- package/dist/cli.mjs +4 -4
- package/dist/context-rules-entry/index.js +18 -1
- package/dist/context-rules-entry/index.mjs +1 -1
- package/dist/detector-entry/index.js +18 -1
- package/dist/detector-entry/index.mjs +2 -2
- package/dist/index.js +63 -2
- package/dist/index.mjs +6 -6
- package/dist/scoring-entry/index.js +29 -1
- package/dist/scoring-entry/index.mjs +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import {
|
|
2
|
+
calculateSeverity
|
|
3
|
+
} from "./chunk-UKQFCUQA.mjs";
|
|
4
|
+
|
|
5
|
+
// src/detector.ts
|
|
6
|
+
import {
|
|
7
|
+
calculateStringSimilarity,
|
|
8
|
+
calculateHeuristicConfidence,
|
|
9
|
+
extractCodeBlocks
|
|
10
|
+
} from "@aiready/core";
|
|
11
|
+
|
|
12
|
+
// src/core/normalizer.ts
|
|
13
|
+
function normalizeCode(code, isPython = false) {
|
|
14
|
+
if (!code) return "";
|
|
15
|
+
let normalized = code;
|
|
16
|
+
if (isPython) {
|
|
17
|
+
normalized = normalized.replace(/#.*/g, "");
|
|
18
|
+
} else {
|
|
19
|
+
normalized = normalized.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
|
20
|
+
}
|
|
21
|
+
return normalized.replace(/"[^"]*"/g, '"STR"').replace(/'[^']*'/g, "'STR'").replace(/`[^`]*`/g, "`STR`").replace(/\b\d+\b/g, "NUM").replace(/\s+/g, " ").trim().toLowerCase();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/detector.ts
|
|
25
|
+
function extractBlocks(file, content) {
|
|
26
|
+
return extractCodeBlocks(file, content);
|
|
27
|
+
}
|
|
28
|
+
function calculateSimilarity(a, b) {
|
|
29
|
+
return calculateStringSimilarity(a, b);
|
|
30
|
+
}
|
|
31
|
+
function calculateConfidence(similarity, tokens, lines) {
|
|
32
|
+
return calculateHeuristicConfidence(similarity, tokens, lines);
|
|
33
|
+
}
|
|
34
|
+
async function detectDuplicatePatterns(fileContents, options) {
|
|
35
|
+
const {
|
|
36
|
+
minSimilarity,
|
|
37
|
+
minLines,
|
|
38
|
+
streamResults,
|
|
39
|
+
onProgress,
|
|
40
|
+
excludePatterns = [],
|
|
41
|
+
confidenceThreshold = 0,
|
|
42
|
+
ignoreWhitelist = []
|
|
43
|
+
} = options;
|
|
44
|
+
const allBlocks = [];
|
|
45
|
+
const excludeRegexes = excludePatterns.map((p) => new RegExp(p, "i"));
|
|
46
|
+
for (const { file, content } of fileContents) {
|
|
47
|
+
const blocks = extractBlocks(file, content);
|
|
48
|
+
for (const b of blocks) {
|
|
49
|
+
if (b.endLine - b.startLine + 1 < minLines) continue;
|
|
50
|
+
const isExcluded = excludeRegexes.some((regex) => regex.test(b.code));
|
|
51
|
+
if (isExcluded) continue;
|
|
52
|
+
allBlocks.push(b);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const duplicates = [];
|
|
56
|
+
const totalBlocks = allBlocks.length;
|
|
57
|
+
let comparisons = 0;
|
|
58
|
+
const totalComparisons = totalBlocks * (totalBlocks - 1) / 2;
|
|
59
|
+
if (onProgress) {
|
|
60
|
+
onProgress(
|
|
61
|
+
0,
|
|
62
|
+
totalComparisons,
|
|
63
|
+
`Starting duplicate detection on ${totalBlocks} blocks...`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
for (let i = 0; i < allBlocks.length; i++) {
|
|
67
|
+
if (i % 50 === 0 && i > 0) {
|
|
68
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
69
|
+
if (onProgress) {
|
|
70
|
+
onProgress(
|
|
71
|
+
comparisons,
|
|
72
|
+
totalComparisons,
|
|
73
|
+
`Analyzing blocks (${i}/${totalBlocks})...`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const b1 = allBlocks[i];
|
|
78
|
+
const isPython1 = b1.file.toLowerCase().endsWith(".py");
|
|
79
|
+
const norm1 = normalizeCode(b1.code, isPython1);
|
|
80
|
+
for (let j = i + 1; j < allBlocks.length; j++) {
|
|
81
|
+
comparisons++;
|
|
82
|
+
const b2 = allBlocks[j];
|
|
83
|
+
if (b1.file === b2.file) continue;
|
|
84
|
+
const isWhitelisted = ignoreWhitelist.some((pattern) => {
|
|
85
|
+
return b1.file.includes(pattern) && b2.file.includes(pattern) || pattern === `${b1.file}::${b2.file}` || pattern === `${b2.file}::${b1.file}`;
|
|
86
|
+
});
|
|
87
|
+
if (isWhitelisted) continue;
|
|
88
|
+
const isPython2 = b2.file.toLowerCase().endsWith(".py");
|
|
89
|
+
const norm2 = normalizeCode(b2.code, isPython2);
|
|
90
|
+
const sim = calculateSimilarity(norm1, norm2);
|
|
91
|
+
if (sim >= minSimilarity) {
|
|
92
|
+
const confidence = calculateConfidence(
|
|
93
|
+
sim,
|
|
94
|
+
b1.tokens,
|
|
95
|
+
b1.endLine - b1.startLine + 1
|
|
96
|
+
);
|
|
97
|
+
if (confidence < confidenceThreshold) continue;
|
|
98
|
+
const { severity, reason, suggestion, matchedRule } = calculateSeverity(
|
|
99
|
+
b1.file,
|
|
100
|
+
b2.file,
|
|
101
|
+
b1.code,
|
|
102
|
+
sim,
|
|
103
|
+
b1.endLine - b1.startLine + 1
|
|
104
|
+
);
|
|
105
|
+
const dup = {
|
|
106
|
+
file1: b1.file,
|
|
107
|
+
line1: b1.startLine,
|
|
108
|
+
endLine1: b1.endLine,
|
|
109
|
+
file2: b2.file,
|
|
110
|
+
line2: b2.startLine,
|
|
111
|
+
endLine2: b2.endLine,
|
|
112
|
+
code1: b1.code,
|
|
113
|
+
code2: b2.code,
|
|
114
|
+
similarity: sim,
|
|
115
|
+
confidence,
|
|
116
|
+
patternType: b1.patternType,
|
|
117
|
+
tokenCost: b1.tokens + b2.tokens,
|
|
118
|
+
severity,
|
|
119
|
+
reason,
|
|
120
|
+
suggestion,
|
|
121
|
+
matchedRule
|
|
122
|
+
};
|
|
123
|
+
duplicates.push(dup);
|
|
124
|
+
if (streamResults)
|
|
125
|
+
console.log(
|
|
126
|
+
`[DUPLICATE] ${dup.file1}:${dup.line1} <-> ${dup.file2}:${dup.line2} (${Math.round(sim * 100)}%, conf: ${Math.round(confidence * 100)}%)`
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (onProgress) {
|
|
132
|
+
onProgress(
|
|
133
|
+
totalComparisons,
|
|
134
|
+
totalComparisons,
|
|
135
|
+
`Duplicate detection complete. Found ${duplicates.length} patterns.`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
return duplicates.sort((a, b) => b.similarity - a.similarity);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export {
|
|
142
|
+
detectDuplicatePatterns
|
|
143
|
+
};
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
// src/context-rules.ts
|
|
2
|
+
import {
|
|
3
|
+
IssueType,
|
|
4
|
+
getSeverityLabel,
|
|
5
|
+
filterBySeverity,
|
|
6
|
+
Severity as Severity5
|
|
7
|
+
} from "@aiready/core";
|
|
8
|
+
|
|
9
|
+
// src/rules/categories/test-rules.ts
|
|
10
|
+
import { Severity } from "@aiready/core";
|
|
11
|
+
var TEST_RULES = [
|
|
12
|
+
// Test Fixtures - Intentional duplication for test isolation
|
|
13
|
+
{
|
|
14
|
+
name: "test-fixtures",
|
|
15
|
+
detect: (file, code) => {
|
|
16
|
+
const isTestFile = file.includes(".test.") || file.includes(".spec.") || file.includes("__tests__") || file.includes("/test/") || file.includes("/tests/");
|
|
17
|
+
const hasTestFixtures = code.includes("beforeAll") || code.includes("afterAll") || code.includes("beforeEach") || code.includes("afterEach") || code.includes("setUp") || code.includes("tearDown");
|
|
18
|
+
return isTestFile && hasTestFixtures;
|
|
19
|
+
},
|
|
20
|
+
severity: Severity.Info,
|
|
21
|
+
reason: "Test fixture duplication is intentional for test isolation",
|
|
22
|
+
suggestion: "Consider if shared test setup would improve maintainability without coupling tests"
|
|
23
|
+
},
|
|
24
|
+
// E2E/Integration Test Page Objects - Test independence
|
|
25
|
+
{
|
|
26
|
+
name: "e2e-page-objects",
|
|
27
|
+
detect: (file, code) => {
|
|
28
|
+
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/");
|
|
29
|
+
const hasPageObjectPatterns = code.includes("page.") || code.includes("await page") || code.includes("locator") || code.includes("getBy") || code.includes("selector") || code.includes("click(") || code.includes("fill(");
|
|
30
|
+
return isE2ETest && hasPageObjectPatterns;
|
|
31
|
+
},
|
|
32
|
+
severity: Severity.Info,
|
|
33
|
+
reason: "E2E test duplication ensures test independence and reduces coupling",
|
|
34
|
+
suggestion: "Consider page object pattern only if duplication causes maintenance issues"
|
|
35
|
+
},
|
|
36
|
+
// Mock Data - Test data intentionally duplicated
|
|
37
|
+
{
|
|
38
|
+
name: "mock-data",
|
|
39
|
+
detect: (file, code) => {
|
|
40
|
+
const isMockFile = file.includes("/mocks/") || file.includes("/__mocks__/") || file.includes("/fixtures/") || file.includes(".mock.") || file.includes(".fixture.");
|
|
41
|
+
const hasMockData = code.includes("mock") || code.includes("Mock") || code.includes("fixture") || code.includes("stub") || code.includes("export const");
|
|
42
|
+
return isMockFile && hasMockData;
|
|
43
|
+
},
|
|
44
|
+
severity: Severity.Info,
|
|
45
|
+
reason: "Mock data duplication is expected for comprehensive test coverage",
|
|
46
|
+
suggestion: "Consider shared factories only for complex mock generation"
|
|
47
|
+
}
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
// src/rules/categories/web-rules.ts
|
|
51
|
+
import { Severity as Severity2 } from "@aiready/core";
|
|
52
|
+
var WEB_RULES = [
|
|
53
|
+
// Email/Document Templates - Often intentionally similar for consistency
|
|
54
|
+
{
|
|
55
|
+
name: "templates",
|
|
56
|
+
detect: (file, code) => {
|
|
57
|
+
const isTemplate = file.includes("/templates/") || file.includes("-template") || file.includes("/email-templates/") || file.includes("/emails/");
|
|
58
|
+
const hasTemplateContent = (code.includes("return") || code.includes("export")) && (code.includes("html") || code.includes("subject") || code.includes("body"));
|
|
59
|
+
return isTemplate && hasTemplateContent;
|
|
60
|
+
},
|
|
61
|
+
severity: Severity2.Info,
|
|
62
|
+
reason: "Template duplication may be intentional for maintainability and branding consistency",
|
|
63
|
+
suggestion: "Extract shared structure only if templates become hard to maintain"
|
|
64
|
+
},
|
|
65
|
+
// Common UI Event Handlers - Very specific patterns only
|
|
66
|
+
{
|
|
67
|
+
name: "common-ui-handlers",
|
|
68
|
+
detect: (file, code) => {
|
|
69
|
+
const isUIFile = file.includes("/components/") || file.includes(".tsx") || file.includes(".jsx") || file.includes("/hooks/");
|
|
70
|
+
const hasCommonHandler = code.includes("handleClickOutside") && code.includes("dropdownRef.current") && code.includes("!dropdownRef.current.contains") || code.includes("handleEscape") && code.includes("event.key") && code.includes("=== 'Escape'") || code.includes("handleClickInside") && code.includes("event.stopPropagation");
|
|
71
|
+
return isUIFile && hasCommonHandler;
|
|
72
|
+
},
|
|
73
|
+
severity: Severity2.Info,
|
|
74
|
+
reason: "Common UI event handlers are boilerplate patterns that repeat across components",
|
|
75
|
+
suggestion: "Consider extracting to shared hooks (useClickOutside, useEscapeKey) only if causing maintenance issues"
|
|
76
|
+
},
|
|
77
|
+
// Next.js Route Handler Patterns - Boilerplate API route patterns
|
|
78
|
+
{
|
|
79
|
+
name: "nextjs-route-handlers",
|
|
80
|
+
detect: (file, code) => {
|
|
81
|
+
const isRouteFile = file.includes("/api/") && (file.endsWith("/route.ts") || file.endsWith("/route.js"));
|
|
82
|
+
const hasRoutePattern = code.includes("export async function POST") || code.includes("export async function GET") || code.includes("export async function PUT") || code.includes("export async function DELETE") || code.includes("NextResponse.json") || code.includes("NextRequest");
|
|
83
|
+
return isRouteFile && hasRoutePattern;
|
|
84
|
+
},
|
|
85
|
+
severity: Severity2.Info,
|
|
86
|
+
reason: "Next.js route handlers follow standard patterns and are intentionally similar across endpoints",
|
|
87
|
+
suggestion: "Route handler duplication is acceptable for API endpoint boilerplate"
|
|
88
|
+
}
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
// src/rules/categories/infra-rules.ts
|
|
92
|
+
import { Severity as Severity3 } from "@aiready/core";
|
|
93
|
+
var INFRA_RULES = [
|
|
94
|
+
// Configuration Files - Often necessarily similar by design
|
|
95
|
+
{
|
|
96
|
+
name: "config-files",
|
|
97
|
+
detect: (file) => {
|
|
98
|
+
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");
|
|
99
|
+
},
|
|
100
|
+
severity: Severity3.Info,
|
|
101
|
+
reason: "Configuration files often have similar structure by design",
|
|
102
|
+
suggestion: "Consider shared config base only if configurations become hard to maintain"
|
|
103
|
+
},
|
|
104
|
+
// Migration Scripts - One-off scripts that are similar by nature
|
|
105
|
+
{
|
|
106
|
+
name: "migration-scripts",
|
|
107
|
+
detect: (file) => {
|
|
108
|
+
return file.includes("/migrations/") || file.includes("/migrate/") || file.includes(".migration.");
|
|
109
|
+
},
|
|
110
|
+
severity: Severity3.Info,
|
|
111
|
+
reason: "Migration scripts are typically one-off and intentionally similar",
|
|
112
|
+
suggestion: "Duplication is acceptable for migration scripts"
|
|
113
|
+
},
|
|
114
|
+
// Tool Implementations - Structural Boilerplate
|
|
115
|
+
{
|
|
116
|
+
name: "tool-implementations",
|
|
117
|
+
detect: (file, code) => {
|
|
118
|
+
const isToolFile = file.includes("/tools/") || file.endsWith(".tool.ts") || code.includes("toolDefinitions");
|
|
119
|
+
const hasToolStructure = code.includes("execute") && (code.includes("try") || code.includes("catch"));
|
|
120
|
+
return isToolFile && hasToolStructure;
|
|
121
|
+
},
|
|
122
|
+
severity: Severity3.Info,
|
|
123
|
+
reason: "Tool implementations share structural boilerplate but have distinct business logic",
|
|
124
|
+
suggestion: "Tool duplication is acceptable for boilerplate interface wrappers"
|
|
125
|
+
},
|
|
126
|
+
// CLI Command Definitions - Commander.js boilerplate patterns
|
|
127
|
+
{
|
|
128
|
+
name: "cli-command-definitions",
|
|
129
|
+
detect: (file, code) => {
|
|
130
|
+
const basename = file.split("/").pop() || "";
|
|
131
|
+
const isCliFile = file.includes("/commands/") || file.includes("/cli/") || file.endsWith(".command.ts") || basename === "cli.ts" || basename === "cli.js" || basename === "cli.tsx" || basename === "cli-action.ts";
|
|
132
|
+
const hasCommandPattern = (code.includes(".command(") || code.includes("defineCommand")) && (code.includes(".description(") || code.includes(".option(")) && (code.includes(".action(") || code.includes("async"));
|
|
133
|
+
return isCliFile && hasCommandPattern;
|
|
134
|
+
},
|
|
135
|
+
severity: Severity3.Info,
|
|
136
|
+
reason: "CLI command definitions follow standard Commander.js patterns and are intentionally similar",
|
|
137
|
+
suggestion: "Command boilerplate duplication is acceptable for CLI interfaces"
|
|
138
|
+
}
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
// src/rules/categories/logic-rules.ts
|
|
142
|
+
import { Severity as Severity4 } from "@aiready/core";
|
|
143
|
+
var LOGIC_RULES = [
|
|
144
|
+
// Re-export / Barrel files - Intentional API surface consolidation
|
|
145
|
+
{
|
|
146
|
+
name: "re-export-files",
|
|
147
|
+
detect: (file, code) => {
|
|
148
|
+
const isIndexFile = file.endsWith("/index.ts") || file.endsWith("/index.js") || file.endsWith("/index.tsx") || file.endsWith("/index.jsx");
|
|
149
|
+
const lines = code.split("\n").filter((l) => l.trim());
|
|
150
|
+
if (lines.length === 0) return false;
|
|
151
|
+
const reExportLines = lines.filter(
|
|
152
|
+
(l) => /^export\s+(\{[^}]+\}|\*)\s+from\s+/.test(l.trim()) || /^export\s+\*\s+as\s+\w+\s+from\s+/.test(l.trim())
|
|
153
|
+
).length;
|
|
154
|
+
return isIndexFile && reExportLines > 0 && reExportLines / lines.length > 0.5;
|
|
155
|
+
},
|
|
156
|
+
severity: Severity4.Info,
|
|
157
|
+
reason: "Barrel/index files intentionally re-export for API surface consolidation",
|
|
158
|
+
suggestion: "Re-exports in barrel files are expected and not true duplication"
|
|
159
|
+
},
|
|
160
|
+
// Type Definitions - Duplication for type safety and module independence
|
|
161
|
+
{
|
|
162
|
+
name: "type-definitions",
|
|
163
|
+
detect: (file, code) => {
|
|
164
|
+
const isTypeFile = file.endsWith(".d.ts") || file.includes("/types/");
|
|
165
|
+
const hasOnlyTypeDefinitions = (code.includes("interface ") || code.includes("type ") || code.includes("enum ")) && !code.includes("function ") && !code.includes("class ") && !code.includes("const ") && !code.includes("let ") && !code.includes("export default");
|
|
166
|
+
const isInterfaceOnlySnippet = code.trim().startsWith("interface ") && code.includes("{") && code.includes("}") && !code.includes("function ") && !code.includes("const ") && !code.includes("return ");
|
|
167
|
+
return isTypeFile && hasOnlyTypeDefinitions || isInterfaceOnlySnippet;
|
|
168
|
+
},
|
|
169
|
+
severity: Severity4.Info,
|
|
170
|
+
reason: "Type/interface definitions are intentionally duplicated for module independence",
|
|
171
|
+
suggestion: "Extract to shared types package only if causing maintenance burden"
|
|
172
|
+
},
|
|
173
|
+
// Utility Functions - Small helpers in dedicated utility files
|
|
174
|
+
{
|
|
175
|
+
name: "utility-functions",
|
|
176
|
+
detect: (file, code) => {
|
|
177
|
+
const isUtilFile = file.endsWith(".util.ts") || file.endsWith(".helper.ts") || file.endsWith(".utils.ts");
|
|
178
|
+
const hasUtilPattern = code.includes("function format") || code.includes("function parse") || code.includes("function sanitize") || code.includes("function normalize") || code.includes("function convert");
|
|
179
|
+
return isUtilFile && hasUtilPattern;
|
|
180
|
+
},
|
|
181
|
+
severity: Severity4.Info,
|
|
182
|
+
reason: "Utility functions in dedicated utility files may be intentionally similar",
|
|
183
|
+
suggestion: "Consider extracting to shared utilities only if causing significant duplication"
|
|
184
|
+
},
|
|
185
|
+
// React/Vue Hooks - Standard patterns
|
|
186
|
+
{
|
|
187
|
+
name: "shared-hooks",
|
|
188
|
+
detect: (file, code) => {
|
|
189
|
+
const isHookFile = file.includes("/hooks/") || file.endsWith(".hook.ts") || file.endsWith(".hook.tsx");
|
|
190
|
+
const hasHookPattern = code.includes("function use") || code.includes("export function use") || code.includes("const use") || code.includes("export const use");
|
|
191
|
+
return isHookFile && hasHookPattern;
|
|
192
|
+
},
|
|
193
|
+
severity: Severity4.Info,
|
|
194
|
+
reason: "Hooks follow standard patterns and are often intentionally similar across components",
|
|
195
|
+
suggestion: "Consider extracting common hook logic only if hooks become complex"
|
|
196
|
+
},
|
|
197
|
+
// Score/Rating Helper Functions - Common threshold patterns
|
|
198
|
+
{
|
|
199
|
+
name: "score-helpers",
|
|
200
|
+
detect: (file, code) => {
|
|
201
|
+
const isHelperFile = file.includes("/utils/") || file.includes("/helpers/") || file.endsWith(".util.ts");
|
|
202
|
+
const hasScorePattern = (code.includes("if (score >=") || code.includes("if (score >")) && code.includes("return") && code.includes("'") && code.split("if (score").length >= 3;
|
|
203
|
+
return isHelperFile && hasScorePattern;
|
|
204
|
+
},
|
|
205
|
+
severity: Severity4.Info,
|
|
206
|
+
reason: "Score/rating helper functions use common threshold patterns that are intentionally similar",
|
|
207
|
+
suggestion: "Score formatting duplication is acceptable for consistent UI display"
|
|
208
|
+
},
|
|
209
|
+
// D3/Canvas Event Handlers - Standard visualization patterns
|
|
210
|
+
{
|
|
211
|
+
name: "visualization-handlers",
|
|
212
|
+
detect: (file, code) => {
|
|
213
|
+
const isVizFile = file.includes("/visualizer/") || file.includes("/charts/") || file.includes("GraphCanvas") || file.includes("ForceDirected");
|
|
214
|
+
const hasVizPattern = (code.includes("dragstarted") || code.includes("dragged") || code.includes("dragended")) && (code.includes("simulation") || code.includes("d3.") || code.includes("alphaTarget"));
|
|
215
|
+
return isVizFile && hasVizPattern;
|
|
216
|
+
},
|
|
217
|
+
severity: Severity4.Info,
|
|
218
|
+
reason: "D3/visualization event handlers follow standard patterns and are intentionally similar",
|
|
219
|
+
suggestion: "Visualization boilerplate duplication is acceptable for interactive charts"
|
|
220
|
+
},
|
|
221
|
+
// Icon/Switch Statement Helpers - Common enum-to-value patterns
|
|
222
|
+
{
|
|
223
|
+
name: "switch-helpers",
|
|
224
|
+
detect: (file, code) => {
|
|
225
|
+
const hasSwitchPattern = code.includes("switch (") && code.includes("case '") && code.includes("return") && code.split("case ").length >= 4;
|
|
226
|
+
const hasIconPattern = code.includes("getIcon") || code.includes("getColor") || code.includes("getLabel") || code.includes("getRating");
|
|
227
|
+
return hasSwitchPattern && hasIconPattern;
|
|
228
|
+
},
|
|
229
|
+
severity: Severity4.Info,
|
|
230
|
+
reason: "Switch statement helpers for enum-to-value mapping are inherently similar",
|
|
231
|
+
suggestion: "Switch duplication is acceptable for mapping enums to display values"
|
|
232
|
+
},
|
|
233
|
+
// Common API/Utility Functions - Legitimate duplication across modules
|
|
234
|
+
{
|
|
235
|
+
name: "common-api-functions",
|
|
236
|
+
detect: (file, code) => {
|
|
237
|
+
const isApiFile = file.includes("/api/") || file.includes("/lib/") || file.includes("/utils/") || file.endsWith(".ts");
|
|
238
|
+
const hasCommonApiPattern = code.includes("getStripe") && code.includes("process.env.STRIPE_SECRET_KEY") || code.includes("getUserByEmail") && code.includes("queryItems") || code.includes("updateUser") && code.includes("buildUpdateExpression") || code.includes("listUserRepositories") && code.includes("queryItems") || code.includes("listTeamRepositories") && code.includes("queryItems") || code.includes("getRemediation") && code.includes("queryItems") || code.includes("formatBreakdownKey") && code.includes(".replace(/([A-Z])/g") || code.includes("queryItems") && code.includes("KeyConditionExpression") || code.includes("putItem") && code.includes("createdAt") || code.includes("updateItem") && code.includes("buildUpdateExpression");
|
|
239
|
+
return isApiFile && hasCommonApiPattern;
|
|
240
|
+
},
|
|
241
|
+
severity: Severity4.Info,
|
|
242
|
+
reason: "Common API/utility functions are legitimately duplicated across modules for clarity and independence",
|
|
243
|
+
suggestion: "Consider extracting to shared utilities only if causing significant duplication"
|
|
244
|
+
},
|
|
245
|
+
// Validation Functions - Inherently similar patterns
|
|
246
|
+
{
|
|
247
|
+
name: "validation-functions",
|
|
248
|
+
detect: (file, code) => {
|
|
249
|
+
const hasValidationPattern = code.includes("isValid") || code.includes("validate") || code.includes("checkValid") || code.includes("isEmail") || code.includes("isPhone") || code.includes("isUrl") || code.includes("isNumeric") || code.includes("isAlpha") || code.includes("isAlphanumeric") || code.includes("isEmpty") || code.includes("isNotEmpty") || code.includes("isRequired") || code.includes("isOptional");
|
|
250
|
+
return hasValidationPattern;
|
|
251
|
+
},
|
|
252
|
+
severity: Severity4.Info,
|
|
253
|
+
reason: "Validation functions are inherently similar and often intentionally duplicated for domain clarity",
|
|
254
|
+
suggestion: "Consider extracting to shared validators only if validation logic becomes complex"
|
|
255
|
+
}
|
|
256
|
+
];
|
|
257
|
+
|
|
258
|
+
// src/context-rules.ts
|
|
259
|
+
var CONTEXT_RULES = [
|
|
260
|
+
...TEST_RULES,
|
|
261
|
+
...WEB_RULES,
|
|
262
|
+
...INFRA_RULES,
|
|
263
|
+
...LOGIC_RULES
|
|
264
|
+
];
|
|
265
|
+
function calculateSeverity(file1, file2, code, similarity, linesOfCode) {
|
|
266
|
+
for (const rule of CONTEXT_RULES) {
|
|
267
|
+
if (rule.detect(file1, code) || rule.detect(file2, code)) {
|
|
268
|
+
return {
|
|
269
|
+
severity: rule.severity,
|
|
270
|
+
reason: rule.reason,
|
|
271
|
+
suggestion: rule.suggestion,
|
|
272
|
+
matchedRule: rule.name
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (similarity >= 0.95 && linesOfCode >= 30) {
|
|
277
|
+
return {
|
|
278
|
+
severity: Severity5.Critical,
|
|
279
|
+
reason: "Large nearly-identical code blocks waste tokens and create maintenance burden",
|
|
280
|
+
suggestion: "Extract to shared utility module immediately"
|
|
281
|
+
};
|
|
282
|
+
} else if (similarity >= 0.95 && linesOfCode >= 15) {
|
|
283
|
+
return {
|
|
284
|
+
severity: Severity5.Major,
|
|
285
|
+
reason: "Nearly identical code should be consolidated",
|
|
286
|
+
suggestion: "Move to shared utility file"
|
|
287
|
+
};
|
|
288
|
+
} else if (similarity >= 0.85) {
|
|
289
|
+
return {
|
|
290
|
+
severity: Severity5.Major,
|
|
291
|
+
reason: "High similarity indicates significant duplication",
|
|
292
|
+
suggestion: "Extract common logic to shared function"
|
|
293
|
+
};
|
|
294
|
+
} else if (similarity >= 0.7) {
|
|
295
|
+
return {
|
|
296
|
+
severity: Severity5.Minor,
|
|
297
|
+
reason: "Moderate similarity detected",
|
|
298
|
+
suggestion: "Consider extracting shared patterns if code evolves together"
|
|
299
|
+
};
|
|
300
|
+
} else {
|
|
301
|
+
return {
|
|
302
|
+
severity: Severity5.Minor,
|
|
303
|
+
reason: "Minor similarity detected",
|
|
304
|
+
suggestion: "Monitor but refactoring may not be worthwhile"
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function getSeverityThreshold(severity) {
|
|
309
|
+
const thresholds = {
|
|
310
|
+
[Severity5.Critical]: 0.95,
|
|
311
|
+
[Severity5.Major]: 0.85,
|
|
312
|
+
[Severity5.Minor]: 0.5,
|
|
313
|
+
[Severity5.Info]: 0
|
|
314
|
+
};
|
|
315
|
+
return thresholds[severity] || 0;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export {
|
|
319
|
+
getSeverityLabel,
|
|
320
|
+
filterBySeverity,
|
|
321
|
+
calculateSeverity,
|
|
322
|
+
getSeverityThreshold
|
|
323
|
+
};
|
package/dist/cli.js
CHANGED
|
@@ -156,7 +156,8 @@ var INFRA_RULES = [
|
|
|
156
156
|
{
|
|
157
157
|
name: "cli-command-definitions",
|
|
158
158
|
detect: (file, code) => {
|
|
159
|
-
const
|
|
159
|
+
const basename = file.split("/").pop() || "";
|
|
160
|
+
const isCliFile = file.includes("/commands/") || file.includes("/cli/") || file.endsWith(".command.ts") || basename === "cli.ts" || basename === "cli.js" || basename === "cli.tsx" || basename === "cli-action.ts";
|
|
160
161
|
const hasCommandPattern = (code.includes(".command(") || code.includes("defineCommand")) && (code.includes(".description(") || code.includes(".option(")) && (code.includes(".action(") || code.includes("async"));
|
|
161
162
|
return isCliFile && hasCommandPattern;
|
|
162
163
|
},
|
|
@@ -169,6 +170,22 @@ var INFRA_RULES = [
|
|
|
169
170
|
// src/rules/categories/logic-rules.ts
|
|
170
171
|
var import_core4 = require("@aiready/core");
|
|
171
172
|
var LOGIC_RULES = [
|
|
173
|
+
// Re-export / Barrel files - Intentional API surface consolidation
|
|
174
|
+
{
|
|
175
|
+
name: "re-export-files",
|
|
176
|
+
detect: (file, code) => {
|
|
177
|
+
const isIndexFile = file.endsWith("/index.ts") || file.endsWith("/index.js") || file.endsWith("/index.tsx") || file.endsWith("/index.jsx");
|
|
178
|
+
const lines = code.split("\n").filter((l) => l.trim());
|
|
179
|
+
if (lines.length === 0) return false;
|
|
180
|
+
const reExportLines = lines.filter(
|
|
181
|
+
(l) => /^export\s+(\{[^}]+\}|\*)\s+from\s+/.test(l.trim()) || /^export\s+\*\s+as\s+\w+\s+from\s+/.test(l.trim())
|
|
182
|
+
).length;
|
|
183
|
+
return isIndexFile && reExportLines > 0 && reExportLines / lines.length > 0.5;
|
|
184
|
+
},
|
|
185
|
+
severity: import_core4.Severity.Info,
|
|
186
|
+
reason: "Barrel/index files intentionally re-export for API surface consolidation",
|
|
187
|
+
suggestion: "Re-exports in barrel files are expected and not true duplication"
|
|
188
|
+
},
|
|
172
189
|
// Type Definitions - Duplication for type safety and module independence
|
|
173
190
|
{
|
|
174
191
|
name: "type-definitions",
|
|
@@ -732,6 +749,22 @@ function getRefactoringSuggestion(patternType, similarity) {
|
|
|
732
749
|
return baseMessages[patternType] + urgency;
|
|
733
750
|
}
|
|
734
751
|
function generateSummary(results) {
|
|
752
|
+
if (!Array.isArray(results)) {
|
|
753
|
+
return {
|
|
754
|
+
totalPatterns: 0,
|
|
755
|
+
totalTokenCost: 0,
|
|
756
|
+
patternsByType: {
|
|
757
|
+
"api-handler": 0,
|
|
758
|
+
validator: 0,
|
|
759
|
+
utility: 0,
|
|
760
|
+
"class-method": 0,
|
|
761
|
+
component: 0,
|
|
762
|
+
function: 0,
|
|
763
|
+
unknown: 0
|
|
764
|
+
},
|
|
765
|
+
topDuplicates: []
|
|
766
|
+
};
|
|
767
|
+
}
|
|
735
768
|
const allIssues = results.flatMap((r) => r.issues || []);
|
|
736
769
|
const totalTokenCost = results.reduce(
|
|
737
770
|
(sum, r) => sum + (r.metrics?.tokenCost || 0),
|
package/dist/cli.mjs
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
import {
|
|
3
3
|
analyzePatterns,
|
|
4
4
|
generateSummary
|
|
5
|
-
} from "./chunk-
|
|
6
|
-
import "./chunk-
|
|
5
|
+
} from "./chunk-C4ZGC4KA.mjs";
|
|
6
|
+
import "./chunk-RH5JPWEC.mjs";
|
|
7
|
+
import "./chunk-G3GZFYRI.mjs";
|
|
7
8
|
import {
|
|
8
9
|
filterBySeverity
|
|
9
|
-
} from "./chunk-
|
|
10
|
-
import "./chunk-PHJE6A3J.mjs";
|
|
10
|
+
} from "./chunk-UKQFCUQA.mjs";
|
|
11
11
|
|
|
12
12
|
// src/cli.ts
|
|
13
13
|
import { Command } from "commander";
|
|
@@ -151,7 +151,8 @@ var INFRA_RULES = [
|
|
|
151
151
|
{
|
|
152
152
|
name: "cli-command-definitions",
|
|
153
153
|
detect: (file, code) => {
|
|
154
|
-
const
|
|
154
|
+
const basename = file.split("/").pop() || "";
|
|
155
|
+
const isCliFile = file.includes("/commands/") || file.includes("/cli/") || file.endsWith(".command.ts") || basename === "cli.ts" || basename === "cli.js" || basename === "cli.tsx" || basename === "cli-action.ts";
|
|
155
156
|
const hasCommandPattern = (code.includes(".command(") || code.includes("defineCommand")) && (code.includes(".description(") || code.includes(".option(")) && (code.includes(".action(") || code.includes("async"));
|
|
156
157
|
return isCliFile && hasCommandPattern;
|
|
157
158
|
},
|
|
@@ -164,6 +165,22 @@ var INFRA_RULES = [
|
|
|
164
165
|
// src/rules/categories/logic-rules.ts
|
|
165
166
|
var import_core4 = require("@aiready/core");
|
|
166
167
|
var LOGIC_RULES = [
|
|
168
|
+
// Re-export / Barrel files - Intentional API surface consolidation
|
|
169
|
+
{
|
|
170
|
+
name: "re-export-files",
|
|
171
|
+
detect: (file, code) => {
|
|
172
|
+
const isIndexFile = file.endsWith("/index.ts") || file.endsWith("/index.js") || file.endsWith("/index.tsx") || file.endsWith("/index.jsx");
|
|
173
|
+
const lines = code.split("\n").filter((l) => l.trim());
|
|
174
|
+
if (lines.length === 0) return false;
|
|
175
|
+
const reExportLines = lines.filter(
|
|
176
|
+
(l) => /^export\s+(\{[^}]+\}|\*)\s+from\s+/.test(l.trim()) || /^export\s+\*\s+as\s+\w+\s+from\s+/.test(l.trim())
|
|
177
|
+
).length;
|
|
178
|
+
return isIndexFile && reExportLines > 0 && reExportLines / lines.length > 0.5;
|
|
179
|
+
},
|
|
180
|
+
severity: import_core4.Severity.Info,
|
|
181
|
+
reason: "Barrel/index files intentionally re-export for API surface consolidation",
|
|
182
|
+
suggestion: "Re-exports in barrel files are expected and not true duplication"
|
|
183
|
+
},
|
|
167
184
|
// Type Definitions - Duplication for type safety and module independence
|
|
168
185
|
{
|
|
169
186
|
name: "type-definitions",
|
|
@@ -151,7 +151,8 @@ var INFRA_RULES = [
|
|
|
151
151
|
{
|
|
152
152
|
name: "cli-command-definitions",
|
|
153
153
|
detect: (file, code) => {
|
|
154
|
-
const
|
|
154
|
+
const basename = file.split("/").pop() || "";
|
|
155
|
+
const isCliFile = file.includes("/commands/") || file.includes("/cli/") || file.endsWith(".command.ts") || basename === "cli.ts" || basename === "cli.js" || basename === "cli.tsx" || basename === "cli-action.ts";
|
|
155
156
|
const hasCommandPattern = (code.includes(".command(") || code.includes("defineCommand")) && (code.includes(".description(") || code.includes(".option(")) && (code.includes(".action(") || code.includes("async"));
|
|
156
157
|
return isCliFile && hasCommandPattern;
|
|
157
158
|
},
|
|
@@ -164,6 +165,22 @@ var INFRA_RULES = [
|
|
|
164
165
|
// src/rules/categories/logic-rules.ts
|
|
165
166
|
var import_core4 = require("@aiready/core");
|
|
166
167
|
var LOGIC_RULES = [
|
|
168
|
+
// Re-export / Barrel files - Intentional API surface consolidation
|
|
169
|
+
{
|
|
170
|
+
name: "re-export-files",
|
|
171
|
+
detect: (file, code) => {
|
|
172
|
+
const isIndexFile = file.endsWith("/index.ts") || file.endsWith("/index.js") || file.endsWith("/index.tsx") || file.endsWith("/index.jsx");
|
|
173
|
+
const lines = code.split("\n").filter((l) => l.trim());
|
|
174
|
+
if (lines.length === 0) return false;
|
|
175
|
+
const reExportLines = lines.filter(
|
|
176
|
+
(l) => /^export\s+(\{[^}]+\}|\*)\s+from\s+/.test(l.trim()) || /^export\s+\*\s+as\s+\w+\s+from\s+/.test(l.trim())
|
|
177
|
+
).length;
|
|
178
|
+
return isIndexFile && reExportLines > 0 && reExportLines / lines.length > 0.5;
|
|
179
|
+
},
|
|
180
|
+
severity: import_core4.Severity.Info,
|
|
181
|
+
reason: "Barrel/index files intentionally re-export for API surface consolidation",
|
|
182
|
+
suggestion: "Re-exports in barrel files are expected and not true duplication"
|
|
183
|
+
},
|
|
167
184
|
// Type Definitions - Duplication for type safety and module independence
|
|
168
185
|
{
|
|
169
186
|
name: "type-definitions",
|