agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
127 lines (114 loc) • 3.35 kB
JavaScript
/**
* @file Static bug analysis index
* @description Export static bug analysis functions
*/
// Import existing functions if available
let existingBugRecommendations;
try {
existingBugRecommendations = require('./bugRecommendationGenerator').generateBugRecommendations;
} catch (e) {
// Fallback if file doesn't exist
}
/**
* Analyze file for static bugs
* @param {string} filePath - Path to file
* @returns {Object} Analysis results
*/
async function analyzeFileStaticBugs(filePath) {
const fs = require('fs').promises;
try {
const content = await fs.readFile(filePath, 'utf8');
// Basic static bug detection
const bugs = [];
// Null reference detection - more specific patterns
if (content.match(/\w+\.\w+/) && content.includes('null')) {
bugs.push({
type: 'null_reference',
severity: 'HIGH',
category: 'Null Safety',
description: 'Potential null reference detected',
effort: 2,
line: 1
});
}
// Pattern for accessing properties without null checks
if (content.match(/\w+\.\w+.*==.*null/) || content.match(/if.*\..*\)/)) {
bugs.push({
type: 'null_reference',
severity: 'HIGH',
category: 'Null Safety',
description: 'Property access without null check',
effort: 2,
line: 1
});
}
// Undefined variable detection (simple heuristic)
if (content.includes('return') && !content.includes('var ') && !content.includes('let ') && !content.includes('const ')) {
bugs.push({
type: 'undefined_variable',
severity: 'HIGH',
category: 'Variables',
description: 'Variable used without declaration',
effort: 1,
line: 1
});
}
// Logic error detection (off-by-one patterns)
if (content.includes('.length') && content.includes('<')) {
bugs.push({
type: 'logic_error',
severity: 'MEDIUM',
category: 'Logic Error',
description: 'Potential off-by-one error in loop condition',
effort: 1,
line: 1
});
}
// Error handling detection
if (content.includes('try') && !content.includes('catch')) {
bugs.push({
type: 'error_handling',
severity: 'MEDIUM',
category: 'Error Handling',
description: 'Try block without catch statement',
effort: 1,
line: 1
});
}
return {
file: filePath,
bugs,
bugCount: bugs.length
};
} catch (error) {
return {
file: filePath,
bugs: [],
bugCount: 0,
error: error.message
};
}
}
/**
* Generate bug fix recommendations with proper structure
* @param {Array} bugs - Detected bugs
* @returns {Array} Recommendations with priority, fix, explanation
*/
function generateBugRecommendations(bugs) {
if (!bugs || bugs.length === 0) {
return [{
priority: 'LOW',
fix: 'No bugs detected',
explanation: 'Code appears to be bug-free'
}];
}
return bugs.map(bug => ({
priority: bug.severity || 'MEDIUM',
fix: `Fix ${bug.type}: ${bug.description}`,
explanation: `Address ${bug.category || 'unknown'} issue to improve code reliability`
}));
}
module.exports = {
analyzeFileStaticBugs,
generateBugRecommendations
};