agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
114 lines (95 loc) • 4.17 kB
JavaScript
/**
* @file Recommendation generation utilities
* @description Responsible for generating actionable WET code recommendations
*/
/**
* Generates WET code recommendations for a single file
* @param {Array} duplicateGroups - Array of duplicate groups
* @returns {Array} Array of recommendation strings
*/
function generateWetRecommendations(duplicateGroups) {
const recommendations = [];
if (duplicateGroups.length === 0) {
recommendations.push('File shows good DRY principles - no significant duplication detected');
return recommendations;
}
const highOpportunity = duplicateGroups.filter(g => g.deduplicationOpportunity === 'HIGH');
const exactDuplicates = duplicateGroups.filter(g => g.type === 'exact_duplicate');
if (highOpportunity.length > 0) {
recommendations.push(`PRIORITY: ${highOpportunity.length} high-impact deduplication opportunities`);
}
if (exactDuplicates.length > 0) {
recommendations.push(`QUICK WINS: ${exactDuplicates.length} exact duplicate blocks can be easily extracted`);
}
// Pattern-specific recommendations
const patterns = [...new Set(duplicateGroups.map(g => g.type))];
patterns.forEach(pattern => {
const patternGroups = duplicateGroups.filter(g => g.type === pattern);
switch (pattern) {
case 'exact_duplicate':
recommendations.push(`Extract ${patternGroups.length} identical code blocks into reusable functions`);
break;
case 'similar_logic':
recommendations.push(`Parameterize ${patternGroups.length} similar logic patterns for reuse`);
break;
case 'repeated_pattern':
recommendations.push(`Create abstractions for ${patternGroups.length} repeated patterns`);
break;
}
});
const totalSavings = duplicateGroups.reduce((sum, group) =>
sum + (group.estimatedSavings?.linesReduced || 0), 0);
if (totalSavings > 0) {
recommendations.push(`Estimated reduction: ${totalSavings} lines of duplicate code`);
}
return recommendations;
}
/**
* Generates project-wide WET code recommendations
* @param {Array} duplicateGroups - Array of duplicate groups
* @param {number} totalLinesReduced - Total lines that can be reduced
* @param {number} totalEffort - Total effort required
* @returns {Array} Array of recommendation strings
*/
function generateProjectWetRecommendations(duplicateGroups, totalLinesReduced, totalEffort) {
const recommendations = [];
if (duplicateGroups.length === 0) {
recommendations.push('Project demonstrates excellent DRY principles');
return recommendations;
}
const crossFileGroups = duplicateGroups.filter(g =>
new Set(g.blocks.map(b => b.file)).size > 1);
if (crossFileGroups.length > 0) {
recommendations.push(`CRITICAL: ${crossFileGroups.length} duplicate patterns span multiple files`);
}
const highImpactGroups = duplicateGroups.filter(g =>
g.deduplicationOpportunity === 'HIGH');
if (highImpactGroups.length > 0) {
recommendations.push(`HIGH IMPACT: ${highImpactGroups.length} major deduplication opportunities`);
}
// Category recommendations
const categories = [...new Set(duplicateGroups.map(g => g.pattern.category))];
categories.forEach(category => {
const count = duplicateGroups.filter(g => g.pattern.category === category).length;
switch (category) {
case 'Exact Match':
recommendations.push(`Exact duplicates: Create shared utilities for ${count} identical code blocks`);
break;
case 'Similar Logic':
recommendations.push(`Similar logic: Parameterize ${count} near-duplicate patterns`);
break;
case 'Copy-Paste':
recommendations.push(`Copy-paste detection: Extract ${count} copied code sections`);
break;
}
});
if (totalLinesReduced > 0) {
const priority = totalEffort > 20 ? 'high' : totalEffort > 10 ? 'medium' : 'low';
recommendations.push(`Total potential reduction: ${totalLinesReduced} lines (${priority} effort: ${totalEffort} points)`);
}
return recommendations;
}
module.exports = {
generateWetRecommendations,
generateProjectWetRecommendations
};