agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
78 lines (71 loc) • 2.66 kB
JavaScript
/**
* @file Detect promise anti-patterns
* @description Single responsibility: Identify common promise misuse patterns
*/
function detectPromiseAntiPatterns(lines, filePath) {
const issues = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const lineNumber = i + 1;
const trimmed = line.trim();
// Promise constructor anti-pattern
if (/new Promise\s*\(\s*async/.test(trimmed)) {
issues.push({
type: 'promise_constructor_antipattern',
severity: 'HIGH',
category: 'Async',
location: `${filePath}:${lineNumber}`,
line: lineNumber,
code: trimmed,
description: 'Promise constructor with async function is anti-pattern',
summary: 'Unnecessary Promise constructor with async',
recommendation: 'Return async function directly or use proper Promise constructor',
effort: 1,
impact: 'Simplifies code and prevents potential issues',
estimatedSavings: 'cleaner async patterns'
});
}
// .then() after await
if (/await.*\.then\s*\(/.test(trimmed)) {
issues.push({
type: 'await_then_mix',
severity: 'MEDIUM',
category: 'Async',
location: `${filePath}:${lineNumber}`,
line: lineNumber,
code: trimmed,
description: 'Mixing await with .then() is unnecessary',
summary: 'Mixed async/await and Promise syntax',
recommendation: 'Use either await or .then(), not both',
effort: 1,
impact: 'Cleaner async code patterns',
estimatedSavings: 'improved code clarity'
});
}
// Creating promises in loops
if (/for\s*\(|while\s*\(|\.forEach/.test(trimmed)) {
for (let j = i + 1; j < Math.min(i + 5, lines.length); j++) {
const innerLine = lines[j].trim();
if (/new Promise\s*\(/.test(innerLine)) {
issues.push({
type: 'promise_in_loop',
severity: 'MEDIUM',
category: 'Async',
location: `${filePath}:${j + 1}`,
line: j + 1,
code: innerLine,
description: 'Creating promises in loop can cause memory and performance issues',
summary: 'Promise creation inside loop',
recommendation: 'Collect promises in array and use Promise.all() for concurrent execution',
effort: 2,
impact: '30-50% memory and performance improvement',
estimatedSavings: '30-50% memory and performance improvement'
});
break;
}
}
}
}
return issues;
}
module.exports = detectPromiseAntiPatterns;