agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
101 lines (93 loc) • 3.53 kB
JavaScript
/**
* @file Index file analyzer for re-export pattern assessment and module organization optimization
* @description Single responsibility: Analyze index files for re-export patterns and module organization improvements
*
* This analyzer examines index files to identify re-export patterns, barrel file organization,
* and module aggregation strategies that can improve code organization and developer experience.
* It provides recommendations for better module structure, export organization, and API design
* to enhance codebase maintainability and developer productivity.
*
* Design rationale:
* - Index file analysis enables improved module organization and API design
* - Re-export pattern detection identifies opportunities for better code structure
* - Barrel file optimization improves developer experience and import management
* - Module aggregation analysis supports strategic code organization improvements
* - Export pattern recognition enables consistent module interface design
*
* Index file analysis scope:
* - Re-export pattern identification for module aggregation and organization
* - Barrel file structure analysis for developer experience optimization
* - Export consistency validation across related modules and packages
* - Module interface design assessment for API usability improvements
* - Import/export pattern analysis for code organization and maintainability enhancement
*/
const fs = require('fs');
const { promises: fsPromises } = require('fs');
// Import patterns from atomic module
const { PATTERNS } = require('./patterns');
/**
* Analyze index file for re-export patterns
* @param {string} indexPath - Path to index file
* @returns {Object} Analysis results
*/
async function analyzeIndexFile(indexPath) {
try {
const content = await fsPromises.readFile(indexPath, 'utf8');
const lines = content.split('\n');
const reexports = [];
const imports = [];
for (let index = 0; index < lines.length; index++) {
const line = lines[index];
// Re-exports
const reexportMatch = line.match(PATTERNS.reexport);
if (reexportMatch) {
const exportMatch = line.match(/export\s+\{([^}]+)\}/);
const fromMatch = line.match(/from\s+['"]([^'"]+)['"]/);
if (exportMatch && fromMatch) {
const namesList = exportMatch[1].split(',');
// Process names without nested operations
for (let i = 0; i < namesList.length; i++) {
const name = namesList[i].trim();
reexports.push({
name: name.replace(/\s+as\s+.*/, ''),
from: fromMatch[1],
line: index + 1
});
}
}
}
// Imports
const importMatch = line.match(PATTERNS.importStatement);
if (importMatch) {
imports.push({
from: importMatch[1],
line: index + 1,
content: line.trim()
});
}
}
return {
file: indexPath,
reexports,
imports,
metrics: {
totalReexports: reexports.length,
totalImports: imports.length,
uniqueModules: (() => {
const moduleSet = new Set();
for (const r of reexports) moduleSet.add(r.from);
for (const i of imports) moduleSet.add(i.from);
return moduleSet.size;
})()
}
};
} catch (error) {
return {
file: indexPath,
error: error.message
};
}
}
module.exports = {
analyzeIndexFile
};