agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
77 lines (75 loc) • 3.99 kB
JavaScript
/**
* @file Analyze dependency between async await calls
* @description Single responsibility: Detect data dependencies between consecutive await operations
*
* This utility analyzes whether two await calls have data dependencies that require
* sequential execution. It helps identify opportunities for parallelizing independent
* async operations using Promise.all() or similar patterns for performance optimization.
*
* Design rationale:
* - Heuristic approach balances accuracy with performance for pattern detection
* - Variable name extraction focuses on most common dependency patterns
* - Simple string matching avoids complex AST analysis overhead
* - Conservative approach prevents incorrect parallelization suggestions
*/
/**
* Determine if two await calls have data dependencies requiring sequential execution
*
* Technical function: Analyzes variable usage to detect dependencies between async calls
*
* Implementation rationale:
* - String manipulation approach provides fast dependency detection
* - Variable extraction from first call focuses on most common dependency patterns
* - Substring matching in second call catches various usage scenarios
* - Conservative heuristic prevents unsafe parallelization recommendations
*
* Dependency detection strategy:
* - Extracts variable name from first await call (before '.' or '(')
* - Checks if extracted variable appears anywhere in second call
* - Conservative approach: presence of variable name indicates potential dependency
* - Handles both direct variable usage and method chaining patterns
*
* Variable extraction logic:
* - Splits on '.' to handle method calls (e.g., "user.save()" → "user")
* - Splits on '(' to handle function calls (e.g., "fetchUser(id)" → "fetchUser")
* - Takes first part as potential variable name for dependency checking
* - Covers most common await patterns in JavaScript async code
*
* Limitations and trade-offs:
* - String-based analysis may have false positives with similar variable names
* - Doesn't perform deep data flow analysis (trade-off for performance)
* - May miss complex dependencies through object properties or scope
* - Conservative approach prefers safety over aggressive optimization
*
* Common dependency patterns detected:
* - Direct variable usage: `const user = await getUser(); await updateUser(user);`
* - Method chaining: `const user = await getUser(); await user.save();`
* - Property access: `const data = await fetchData(); await process(data.value);`
*
* Performance considerations:
* - O(n) time complexity for string operations
* - No regex compilation overhead for simple string methods
* - Minimal memory allocation for temporary string operations
* - Fast execution suitable for real-time analysis
*
* Alternative approaches considered:
* - AST-based data flow analysis: Rejected for performance overhead
* - Regular expression matching: Rejected as overkill for simple patterns
* - Complex variable scope tracking: Rejected for implementation complexity
*
* @param {string} firstCall - First await call statement to analyze
* @param {string} secondCall - Second await call to check for dependencies
* @param {string[]} lines - Code lines context (unused in current implementation)
* @param {number} startIndex - Starting index context (unused in current implementation)
* @returns {boolean} True if second call appears to depend on first call's result
* @example
* areDependent('const user = await getUser()', 'await user.save()') // returns true
* areDependent('await fetchA()', 'await fetchB()') // returns false
* areDependent('const data = await getData()', 'await process(data)') // returns true
*/
function areDependent(firstCall, secondCall, lines, startIndex) {
// Simple heuristic: if second call uses variable from first call
const firstVar = firstCall.split('.')[0].split('(')[0];
return secondCall.includes(firstVar);
}
module.exports = areDependent;