agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
75 lines (66 loc) • 2.41 kB
JavaScript
/**
* @file Unreachable code checker
* @description Checks for unreachable code patterns
*/
/**
* Check for unreachable code
* @param {string} content - File content
* @param {string} filePath - File path
* @returns {Array} Unreachable code issues
*/
function checkUnreachableCode(content, filePath) {
const issues = [];
const lines = content.split('\n');
lines.forEach((line, index) => {
const trimmed = line.trim();
if (trimmed.startsWith('return') && index < lines.length - 1) {
// Check if this is a multi-line return statement
let isMultiLineReturn = false;
let checkIndex = index;
// Look for continuation of return statement
while (checkIndex < lines.length - 1) {
const currentLine = lines[checkIndex].trim();
const nextLine = lines[checkIndex + 1];
const nextTrimmed = nextLine ? nextLine.trim() : '';
// If current line ends with operators or open parentheses, it continues
if (currentLine.endsWith('||') || currentLine.endsWith('&&') ||
currentLine.endsWith(',') || currentLine.endsWith('(') ||
currentLine.endsWith('+') || currentLine.endsWith('-') ||
!currentLine.endsWith(';')) {
isMultiLineReturn = true;
checkIndex++;
// If next line is closing brace or semicolon, stop
if (nextTrimmed.startsWith('}') || nextTrimmed === ';') {
break;
}
} else {
// Return statement is complete
break;
}
}
// Only flag as unreachable if there's code after complete return
if (!isMultiLineReturn) {
const nextLine = lines[index + 1];
if (nextLine && nextLine.trim() && !nextLine.trim().startsWith('}')) {
issues.push({
type: 'unreachable_code',
line: index + 2,
severity: 'MEDIUM',
category: 'Logic Error',
description: 'Code after return statement is unreachable',
summary: 'Unreachable code detected',
location: `${filePath}:${index + 2}`,
recommendation: 'Remove unreachable code or restructure control flow',
effort: 1,
impact: 'low',
file: filePath
});
}
}
}
});
return issues;
}
module.exports = {
checkUnreachableCode
};