Spaces:
Build error
Build error
File size: 7,422 Bytes
d83e271 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
import { FileInfo, ImportInfo, ComponentInfo } from '@/types/file-manifest';
/**
* Parse a JavaScript/JSX file to extract imports, exports, and component info
*/
export function parseJavaScriptFile(content: string, filePath: string): Partial<FileInfo> {
const imports = extractImports(content);
const exports = extractExports(content);
const componentInfo = extractComponentInfo(content, filePath);
const fileType = determineFileType(filePath, content);
return {
imports,
exports,
componentInfo,
type: fileType,
};
}
/**
* Extract import statements from file content
*/
function extractImports(content: string): ImportInfo[] {
const imports: ImportInfo[] = [];
// Match import statements
const importRegex = /import\s+(?:(.+?)\s+from\s+)?['"](.+?)['"]/g;
const matches = content.matchAll(importRegex);
for (const match of matches) {
const [, importClause, source] = match;
const importInfo: ImportInfo = {
source,
imports: [],
isLocal: source.startsWith('./') || source.startsWith('../') || source.startsWith('@/'),
};
if (importClause) {
// Handle default import
const defaultMatch = importClause.match(/^(\w+)(?:,|$)/);
if (defaultMatch) {
importInfo.defaultImport = defaultMatch[1];
}
// Handle named imports
const namedMatch = importClause.match(/\{([^}]+)\}/);
if (namedMatch) {
importInfo.imports = namedMatch[1]
.split(',')
.map(imp => imp.trim())
.map(imp => imp.split(/\s+as\s+/)[0].trim());
}
}
imports.push(importInfo);
}
return imports;
}
/**
* Extract export statements from file content
*/
function extractExports(content: string): string[] {
const exports: string[] = [];
// Match default export
if (/export\s+default\s+/m.test(content)) {
// Try to find the name of the default export
const defaultExportMatch = content.match(/export\s+default\s+(?:function\s+)?(\w+)/);
if (defaultExportMatch) {
exports.push(`default:${defaultExportMatch[1]}`);
} else {
exports.push('default');
}
}
// Match named exports
const namedExportRegex = /export\s+(?:const|let|var|function|class)\s+(\w+)/g;
const namedMatches = content.matchAll(namedExportRegex);
for (const match of namedMatches) {
exports.push(match[1]);
}
// Match export { ... } statements
const exportBlockRegex = /export\s+\{([^}]+)\}/g;
const blockMatches = content.matchAll(exportBlockRegex);
for (const match of blockMatches) {
const names = match[1]
.split(',')
.map(exp => exp.trim())
.map(exp => exp.split(/\s+as\s+/)[0].trim());
exports.push(...names);
}
return exports;
}
/**
* Extract React component information
*/
function extractComponentInfo(content: string, filePath: string): ComponentInfo | undefined {
// Check if this is likely a React component
const hasJSX = /<[A-Z]\w*|<[a-z]+\s+[^>]*\/?>/.test(content);
if (!hasJSX && !content.includes('React')) return undefined;
// Try to find component name
let componentName = '';
// Check for function component
const funcComponentMatch = content.match(/(?:export\s+)?(?:default\s+)?function\s+([A-Z]\w*)\s*\(/);
if (funcComponentMatch) {
componentName = funcComponentMatch[1];
} else {
// Check for arrow function component
const arrowComponentMatch = content.match(/(?:export\s+)?(?:default\s+)?(?:const|let)\s+([A-Z]\w*)\s*=\s*(?:\([^)]*\)|[^=])*=>/);
if (arrowComponentMatch) {
componentName = arrowComponentMatch[1];
}
}
// If no component name found, try to get from filename
if (!componentName) {
const fileName = filePath.split('/').pop()?.replace(/\.(jsx?|tsx?)$/, '');
if (fileName && /^[A-Z]/.test(fileName)) {
componentName = fileName;
}
}
if (!componentName) return undefined;
// Extract hooks used
const hooks: string[] = [];
const hookRegex = /use[A-Z]\w*/g;
const hookMatches = content.matchAll(hookRegex);
for (const match of hookMatches) {
if (!hooks.includes(match[0])) {
hooks.push(match[0]);
}
}
// Check if component has state
const hasState = hooks.includes('useState') || hooks.includes('useReducer');
// Extract child components (rough approximation)
const childComponents: string[] = [];
const componentRegex = /<([A-Z]\w*)[^>]*(?:\/?>|>)/g;
const componentMatches = content.matchAll(componentRegex);
for (const match of componentMatches) {
const comp = match[1];
if (!childComponents.includes(comp) && comp !== componentName) {
childComponents.push(comp);
}
}
return {
name: componentName,
hooks,
hasState,
childComponents,
};
}
/**
* Determine file type based on path and content
*/
function determineFileType(
filePath: string,
content: string
): FileInfo['type'] {
const fileName = filePath.split('/').pop()?.toLowerCase() || '';
const dirPath = filePath.toLowerCase();
// Style files
if (fileName.endsWith('.css')) return 'style';
// Config files
if (fileName.includes('config') ||
fileName === 'vite.config.js' ||
fileName === 'tailwind.config.js' ||
fileName === 'postcss.config.js') {
return 'config';
}
// Hook files
if (dirPath.includes('/hooks/') || fileName.startsWith('use')) {
return 'hook';
}
// Context files
if (dirPath.includes('/context/') || fileName.includes('context')) {
return 'context';
}
// Layout components
if (fileName.includes('layout') || content.includes('children')) {
return 'layout';
}
// Page components (in pages directory or have routing)
if (dirPath.includes('/pages/') ||
content.includes('useRouter') ||
content.includes('useParams')) {
return 'page';
}
// Utility files
if (dirPath.includes('/utils/') ||
dirPath.includes('/lib/') ||
!content.includes('export default')) {
return 'utility';
}
// Default to component
return 'component';
}
/**
* Build component dependency tree
*/
export function buildComponentTree(files: Record<string, FileInfo>) {
const tree: Record<string, {
file: string;
imports: string[];
importedBy: string[];
type: 'page' | 'layout' | 'component';
}> = {};
// First pass: collect all components
for (const [path, fileInfo] of Object.entries(files)) {
if (fileInfo.componentInfo) {
const componentName = fileInfo.componentInfo.name;
tree[componentName] = {
file: path,
imports: [],
importedBy: [],
type: fileInfo.type === 'page' ? 'page' :
fileInfo.type === 'layout' ? 'layout' : 'component',
};
}
}
// Second pass: build relationships
for (const [path, fileInfo] of Object.entries(files)) {
if (fileInfo.componentInfo && fileInfo.imports) {
const componentName = fileInfo.componentInfo.name;
// Find imported components
for (const imp of fileInfo.imports) {
if (imp.isLocal && imp.defaultImport) {
// Check if this import is a component we know about
if (tree[imp.defaultImport]) {
tree[componentName].imports.push(imp.defaultImport);
tree[imp.defaultImport].importedBy.push(componentName);
}
}
}
}
}
return tree;
} |