-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.ts
More file actions
550 lines (500 loc) · 15.8 KB
/
index.ts
File metadata and controls
550 lines (500 loc) · 15.8 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/**
* Generic code analyzer - handles any programming language as fallback
* Provides basic AST parsing and chunking for languages without specialized analyzers
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
import { promises as fs } from 'fs';
import path from 'path';
import {
FrameworkAnalyzer,
AnalysisResult,
CodebaseMetadata,
CodeChunk,
CodeComponent,
ImportStatement,
ExportStatement,
Dependency
} from '../../types/index.js';
import { createChunksFromCode } from '../../utils/chunking.js';
import {
createASTAlignedChunks,
MAX_AST_CHUNK_FILE_SIZE,
MAX_AST_CHUNK_FILE_LINES
} from '../../utils/ast-chunker.js';
import { detectLanguage } from '../../utils/language-detection.js';
import { extractTreeSitterSymbols, type TreeSitterSymbol } from '../../utils/tree-sitter.js';
import {
detectWorkspaceType,
scanWorkspacePackageJsons,
aggregateWorkspaceDependencies,
categorizeDependency
} from '../../utils/dependency-detection.js';
export class GenericAnalyzer implements FrameworkAnalyzer {
readonly name = 'generic';
readonly version = '1.0.0';
readonly supportedExtensions = [
// JavaScript/TypeScript
'.js',
'.jsx',
'.ts',
'.tsx',
'.mjs',
'.cjs',
// Python
'.py',
'.pyi',
// Java/Kotlin
'.java',
'.kt',
'.kts',
// C/C++
'.c',
'.cpp',
'.cc',
'.cxx',
'.h',
'.hpp',
// C#
'.cs',
// Go
'.go',
// Rust
'.rs',
// PHP
'.php',
// Ruby
'.rb',
// Swift
'.swift',
// Scala
'.scala',
// Shell
'.sh',
'.bash',
'.zsh',
// Config
'.json',
'.yaml',
'.yml',
'.toml',
'.xml',
// Markup
'.html',
'.htm',
'.md',
'.mdx',
// Styles
'.css',
'.scss',
'.sass',
'.less'
];
readonly priority = 10; // Low priority - fallback analyzer
canAnalyze(filePath: string, _content?: string): boolean {
const ext = path.extname(filePath).toLowerCase();
return this.supportedExtensions.includes(ext);
}
async analyze(filePath: string, content: string): Promise<AnalysisResult> {
const language = detectLanguage(filePath);
const relativePath = path.relative(process.cwd(), filePath);
// Parse based on language
let components: CodeComponent[] = [];
let imports: ImportStatement[] = [];
let exports: ExportStatement[] = [];
let treeSitterGrammar: string | undefined;
let usesTreeSitterSymbols = false;
let treeSitterSymbols: TreeSitterSymbol[] = [];
try {
const treeSitterResult = await extractTreeSitterSymbols(content, language);
if (treeSitterResult && treeSitterResult.symbols.length > 0) {
treeSitterSymbols = treeSitterResult.symbols;
// Legacy: replaced by createASTAlignedChunks for AST-aligned chunking
components = this.convertTreeSitterSymbolsToComponents(treeSitterResult.symbols);
treeSitterGrammar = treeSitterResult.grammarFile;
usesTreeSitterSymbols = true;
}
if (language === 'typescript' || language === 'javascript') {
const parsed = await this.parseJSTSFile(filePath, content, language);
imports = parsed.imports;
exports = parsed.exports;
// Keep legacy parser as fallback if Tree-sitter produced nothing.
if (components.length === 0) {
components = parsed.components;
usesTreeSitterSymbols = false;
treeSitterGrammar = undefined;
}
} else {
// For other languages, use regex fallback if Tree-sitter produced nothing.
if (components.length === 0) {
components = this.parseGenericFile(content);
}
}
} catch (error) {
console.warn(`Failed to parse ${filePath}:`, error);
}
const metadata: Record<string, any> = {
analyzer: this.name,
fileSize: content.length,
lineCount: content.split('\n').length,
chunkStrategy: usesTreeSitterSymbols ? 'ast-aligned' : 'line-or-component'
};
if (usesTreeSitterSymbols && treeSitterGrammar) {
metadata.treeSitterGrammar = treeSitterGrammar;
metadata.symbolAware = true;
}
// Create chunks — use AST-aligned chunker when Tree-sitter symbols are available
// File ceiling pre-check: skip AST chunking for very large files
const lineCount = content.split('\n').length;
const byteSize = Buffer.byteLength(content, 'utf8');
const useASTChunking =
usesTreeSitterSymbols &&
treeSitterSymbols.length > 0 &&
byteSize <= MAX_AST_CHUNK_FILE_SIZE &&
lineCount <= MAX_AST_CHUNK_FILE_LINES;
let chunks: CodeChunk[];
if (useASTChunking) {
try {
chunks = createASTAlignedChunks(content, treeSitterSymbols, {
minChunkLines: 10,
maxChunkLines: 150,
filePath,
language,
framework: 'generic',
componentType: 'module'
});
// Enrich AST chunks with the correct relativePath
for (const chunk of chunks) {
chunk.relativePath = relativePath;
}
} catch (err) {
if (process.env.CODEBASE_CONTEXT_DEBUG) {
console.error(
`[ast-chunker] AST chunking failed for ${filePath}, falling back to line chunks:`,
err
);
}
// Fall through to line-based chunking
chunks = await createChunksFromCode(
content,
filePath,
relativePath,
language,
components,
metadata
);
}
} else {
chunks = await createChunksFromCode(
content,
filePath,
relativePath,
language,
components,
metadata
);
}
return {
filePath,
language,
components,
imports,
exports,
dependencies: [],
metadata,
chunks
};
}
private convertTreeSitterSymbolsToComponents(symbols: TreeSitterSymbol[]): CodeComponent[] {
return symbols.map((symbol) => ({
name: symbol.name,
type: symbol.kind,
componentType: symbol.kind,
startLine: symbol.startLine,
endLine: symbol.endLine,
metadata: {
extraction: 'tree-sitter',
nodeType: symbol.nodeType,
startIndex: symbol.startIndex,
endIndex: symbol.endIndex
}
}));
}
async detectCodebaseMetadata(rootPath: string): Promise<CodebaseMetadata> {
let projectName = path.basename(rootPath);
let dependencies: Dependency[] = [];
let workspaceType: string = 'single';
let workspacePackages: any[] = [];
try {
workspaceType = await detectWorkspaceType(rootPath);
workspacePackages =
workspaceType !== 'single' ? await scanWorkspacePackageJsons(rootPath) : [];
const pkgPath = path.join(rootPath, 'package.json');
let packageJson: any = {};
try {
packageJson = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));
projectName = packageJson.name || projectName;
} catch {
// no root package.json
}
const rawDeps =
workspaceType !== 'single'
? aggregateWorkspaceDependencies(workspacePackages)
: { ...packageJson.dependencies, ...packageJson.devDependencies };
dependencies = Object.entries(rawDeps).map(([name, version]) => ({
name,
version: version as string,
category: categorizeDependency(name)
}));
} catch (_error) {
// skip
}
const metadata: CodebaseMetadata = {
name: projectName,
rootPath,
languages: [],
dependencies: dependencies as any,
architecture: {
type: 'mixed',
layers: {
presentation: 0,
business: 0,
data: 0,
state: 0,
core: 0,
shared: 0,
feature: 0,
infrastructure: 0,
unknown: 0
},
patterns: []
},
styleGuides: [],
documentation: [],
projectStructure: {
type: workspaceType === 'single' ? 'single-app' : 'monorepo',
packages: workspacePackages.map((p) => ({
name: p.name || path.basename(path.dirname(p.filePath)),
path: path.relative(rootPath, path.dirname(p.filePath)),
type: 'app' // default to app
})),
workspaces: workspaceType !== 'single' ? [workspaceType] : undefined
},
statistics: {
totalFiles: 0,
totalLines: 0,
totalComponents: 0,
componentsByType: {},
componentsByLayer: {
presentation: 0,
business: 0,
data: 0,
state: 0,
core: 0,
shared: 0,
feature: 0,
infrastructure: 0,
unknown: 0
}
},
customMetadata: {
monorepoType: workspaceType !== 'single' ? workspaceType : undefined
}
};
return metadata;
}
private async parseJSTSFile(
filePath: string,
content: string,
_language: 'typescript' | 'javascript'
): Promise<{
components: CodeComponent[];
imports: ImportStatement[];
exports: ExportStatement[];
}> {
const components: CodeComponent[] = [];
const imports: ImportStatement[] = [];
const exports: ExportStatement[] = [];
try {
// Use typescript-estree for parsing
const { parse } = await import('@typescript-eslint/typescript-estree');
const ast = parse(content, {
loc: true,
range: true,
comment: true,
jsx: filePath.endsWith('x')
});
// Extract imports
for (const node of ast.body) {
if (node.type === 'ImportDeclaration' && node.source.value) {
imports.push({
source: node.source.value as string,
imports: node.specifiers.map((s: any) => {
if (s.type === 'ImportDefaultSpecifier') return 'default';
if (s.type === 'ImportNamespaceSpecifier') return '*';
return s.imported?.name || s.local.name;
}),
isDefault: node.specifiers.some((s: any) => s.type === 'ImportDefaultSpecifier'),
isDynamic: false,
line: node.loc?.start.line
});
}
// Extract components (classes, functions, interfaces)
if (node.type === 'ClassDeclaration' && node.id) {
components.push({
name: node.id.name,
type: 'class',
startLine: node.loc!.start.line,
endLine: node.loc!.end.line,
metadata: {}
});
}
if (node.type === 'FunctionDeclaration' && node.id) {
components.push({
name: node.id.name,
type: 'function',
startLine: node.loc!.start.line,
endLine: node.loc!.end.line,
metadata: {}
});
}
if (node.type === 'VariableDeclaration') {
for (const decl of node.declarations) {
if (decl.id.type === 'Identifier') {
// Check if it's an arrow function or function expression
const isFunction =
decl.init &&
(decl.init.type === 'ArrowFunctionExpression' ||
decl.init.type === 'FunctionExpression');
components.push({
name: decl.id.name,
type: isFunction ? 'function' : 'variable',
startLine: decl.loc!.start.line,
endLine: decl.loc!.end.line,
metadata: {}
});
}
}
}
// Extract exports
if (node.type === 'ExportNamedDeclaration') {
if (node.declaration) {
if (node.declaration.type === 'VariableDeclaration') {
for (const decl of node.declaration.declarations) {
if (decl.id.type === 'Identifier') {
exports.push({
name: decl.id.name,
isDefault: false,
type: 'named'
});
}
}
} else if ('id' in node.declaration && node.declaration.id) {
exports.push({
name: (node.declaration.id as any).name,
isDefault: false,
type: 'named'
});
}
}
if (node.specifiers) {
for (const spec of node.specifiers) {
if (spec.type === 'ExportSpecifier') {
exports.push({
name: spec.exported.name,
isDefault: false,
type: 'named'
});
}
}
}
}
if (node.type === 'ExportDefaultDeclaration') {
const name = node.declaration.type === 'Identifier' ? node.declaration.name : 'default';
exports.push({
name,
isDefault: true,
type: 'default'
});
}
}
} catch (error) {
console.warn(`Failed to parse JS/TS file ${filePath}:`, error);
}
return { components, imports, exports };
}
private parseGenericFile(content: string): CodeComponent[] {
const components: CodeComponent[] = [];
const lines = content.split('\n');
// Basic pattern matching for functions, classes, etc.
const patterns = [
// Functions: def, function, func, fn
{ regex: /(?:^|\s)(?:def|function|func|fn)\s+(\w+)/i, type: 'function' },
// Classes: class, struct
{ regex: /(?:^|\s)(?:class|struct|interface|trait)\s+(\w+)/i, type: 'class' },
// Methods: pub fn, pub fn, private func
{
regex: /(?:pub|public|private|protected)?\s*(?:fn|func|function|def|method)\s+(\w+)/i,
type: 'method'
}
];
lines.forEach((line, index) => {
for (const pattern of patterns) {
const match = line.match(pattern.regex);
if (match && match[1]) {
components.push({
name: match[1],
type: pattern.type,
startLine: index + 1,
endLine: index + 1, // Will be updated if we find end
metadata: {}
});
}
}
});
return components;
}
/**
* Generate generic summary for any code chunk
*/
summarize(chunk: CodeChunk): string {
const fileName = path.basename(chunk.filePath);
const { language, componentType, content } = chunk;
if (!content) {
return `${language} ${componentType || 'code'} in ${fileName}`;
}
// Try to extract meaningful information
const firstComment = this.extractFirstComment(content);
if (firstComment) {
return `${language} ${componentType || 'code'} in ${fileName}: ${firstComment}`;
}
// Extract class/function names
const classMatch = content.match(/(?:class|struct|interface|trait)\s+(\w+)/);
const funcMatch = content.match(/(?:function|fn|func|def|method)\s+(\w+)/);
if (classMatch) {
return `${language} ${classMatch[0].split(/\s+/)[0]} '${classMatch[1]}' in ${fileName}.`;
}
if (funcMatch) {
return `${language} ${funcMatch[0].split(/\s+/)[0]} '${funcMatch[1]}' in ${fileName}.`;
}
// Fallback to first meaningful line
const firstLine = content
.split('\n')
.find(
(line) => line.trim() && !line.trim().startsWith('import') && !line.trim().startsWith('//')
);
return `${language} code in ${fileName}: ${firstLine ? firstLine.trim().slice(0, 60) + '...' : 'code definition'}`;
}
private extractFirstComment(content: string | null | undefined): string {
if (!content) return '';
// Try JSDoc style
const jsdocMatch = content.match(/\/\*\*\s*\n?\s*\*\s*(.+?)(?:\n|\*\/)/);
if (jsdocMatch) return jsdocMatch[1].trim();
// Try Python docstring
const pythonMatch = content.match(/^[\s]*"""(.+?)"""/s);
if (pythonMatch) return pythonMatch[1].trim().split('\n')[0];
// Try single-line comment
const singleMatch = content.match(/^[\s]*\/\/\s*(.+?)$/m);
if (singleMatch) return singleMatch[1].trim();
return '';
}
}