agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
72 lines (64 loc) • 2.12 kB
JavaScript
/**
* @file Type coercion analysis
* @description Analyzes code for dangerous type coercion patterns
*/
/**
* Analyze type coercion issues in code content
* @param {string} content - Code content to analyze
* @returns {Array} Array of type coercion issues
*/
function analyzeTypeCoercion(content) {
const issues = [];
const lines = content.split('\n');
lines.forEach((line, index) => {
const trimmed = line.trim();
// Check for == instead of ===
if (trimmed.match(/[^=!]==[^=]/) && !trimmed.includes('===')) {
issues.push({
type: 'type_coercion',
line: index + 1,
content: trimmed,
severity: 'HIGH',
description: 'Use === instead of == to avoid type coercion',
suggestion: 'Replace == with === for strict equality comparison'
});
}
// Check for != instead of !==
if (trimmed.match(/!=[^=]/) && !trimmed.includes('!==')) {
issues.push({
type: 'type_coercion',
line: index + 1,
content: trimmed,
severity: 'HIGH',
description: 'Use !== instead of != to avoid type coercion',
suggestion: 'Replace != with !== for strict inequality comparison'
});
}
// Check for NaN comparison issues
if (trimmed.includes('== NaN') || trimmed.includes('=== NaN')) {
issues.push({
type: 'nan_comparison',
line: index + 1,
content: trimmed,
severity: 'HIGH',
description: 'Direct NaN comparison always returns false',
suggestion: 'Use Number.isNaN() or isNaN() function instead'
});
}
// Check for parseInt without radix
if (trimmed.match(/parseInt\s*\([^,)]+\)/) && !trimmed.match(/parseInt\s*\([^,]+,\s*\d+\)/)) {
issues.push({
type: 'missing_radix',
line: index + 1,
content: trimmed,
severity: 'MEDIUM',
description: 'parseInt should specify radix parameter',
suggestion: 'Add radix parameter: parseInt(value, 10) for decimal'
});
}
});
return issues;
}
module.exports = {
analyzeTypeCoercion
};