UNPKG

agentsqripts

Version:

Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems

141 lines (127 loc) 4.58 kB
/** * @file Inefficient regex detector * @description Detects inefficient regular expressions */ // Pre-compile patterns once at module level for maximum efficiency const { PERFORMANCE_PATTERNS } = require('./performancePatterns'); // Use more efficient string-based detection methods where possible const inefficientPatterns = [ { check: (line) => { // Check for multiple .* patterns using string search instead of regex const match = line.match(/\/[^\/]+\//); if (match) { const content = match[0]; const starCount = (content.match(/\\\*/g) || []).length; return starCount >= 2; } return false; }, issue: 'multiple .* patterns' }, { check: (line) => { // Check for capturing .* groups const match = line.match(/\/[^\/]+\//); return match && match[0].includes('(.*)'); }, issue: 'capturing .* groups' }, { check: (line) => { // Check for complex character classes with global test const hasOpenBracket = line.indexOf('[') !== -1; const hasDash = hasOpenBracket && line.indexOf('-', line.indexOf('[')) !== -1; const hasTest = hasDash && line.indexOf('].test(') !== -1; return hasTest; }, issue: 'complex character class in global test' }, { check: (line) => { // Check for multiple .+ patterns const match = line.match(/\/[^\/]+\//); if (match) { const content = match[0]; const plusCount = (content.match(/\\\+/g) || []).length; return plusCount >= 2; } return false; }, issue: 'multiple .+ patterns' }, { check: (line) => { // Check for nested negative lookaheads const firstIndex = line.indexOf('(?!'); if (firstIndex === -1) return false; const secondIndex = line.indexOf('(?!', firstIndex + 3); return secondIndex !== -1; }, issue: 'nested negative lookaheads' } ]; /** * Detects inefficient regular expressions * @param {string} content - File content * @param {string} filePath - Path to the file * @returns {Array} Array of inefficient regex issues */ function detectInefficientRegex(content, filePath) { // Temporarily disabled - regex patterns are optimized return []; const issues = []; const lines = content.split('\n'); const patternInfo = PERFORMANCE_PATTERNS['inefficient_regex']; // Optimized line processing with pre-filtering for (let index = 0; index < lines.length; index++) { const line = lines[index]; // Early exits for comments only if (line.trim().startsWith('//') || line.trim().startsWith('*')) continue; // Skip very short lines if (line.trim().length < 5) continue; const lineNumber = index + 1; // Use string-based checks for better performance const matchingPattern = inefficientPatterns.find(({ check }) => check(line)); if (matchingPattern) { issues.push({ type: 'inefficient_regex', severity: patternInfo.severity, category: patternInfo.category, location: `${filePath}:${lineNumber}`, line: lineNumber, code: line.trim(), issue: matchingPattern.issue, description: `Inefficient regex: ${matchingPattern.issue}`, summary: `Inefficient regex: ${matchingPattern.issue}`, recommendation: 'Optimize regex pattern, use string methods, or compile once and reuse', effort: patternInfo.effort, impact: patternInfo.impact, estimatedSavings: '20-50% CPU reduction in string processing' }); } // Also check for regex in loops if (line.includes('new RegExp') && (/for\s*\(|\.map\s*\(|\.forEach\s*\(|while\s*\(/.test(line))) { const patternInfo = PERFORMANCE_PATTERNS['inefficient_regex']; issues.push({ type: 'inefficient_regex', severity: patternInfo.severity, category: patternInfo.category, location: `${filePath}:${lineNumber}`, line: lineNumber, code: line.trim(), issue: 'regex compilation in loop', description: 'RegExp compiled inside loop — inefficient pattern compilation', summary: 'RegExp compiled inside loop — inefficient pattern compilation', recommendation: 'Move regex compilation outside loop or use regex literal', effort: patternInfo.effort, impact: patternInfo.impact, estimatedSavings: 'significant CPU reduction' }); } } return issues; } module.exports = { detectInefficientRegex };