| 33 | } |
| 34 | |
| 35 | export class NextJsAnalyzer implements FrameworkAnalyzer { |
| 36 | readonly name = 'nextjs'; |
| 37 | readonly version = '1.0.0'; |
| 38 | readonly supportedExtensions = ['.tsx', '.jsx', '.ts', '.js', '.mjs', '.cjs', '.mts', '.cts']; |
| 39 | readonly priority = 90; |
| 40 | |
| 41 | canAnalyze(filePath: string, content?: string): boolean { |
| 42 | const extension = path.extname(filePath).toLowerCase(); |
| 43 | if (!this.supportedExtensions.includes(extension)) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | if (isInAppRouter(filePath) || isInPagesRouter(filePath)) { |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | return Boolean( |
| 52 | content && (/\bfrom\s+['"]next\//.test(content) || /\bfrom\s+['"]next['"]/.test(content)) |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | async analyze(filePath: string, content: string): Promise<AnalysisResult> { |
| 57 | const extension = path.extname(filePath).toLowerCase(); |
| 58 | const language = |
| 59 | extension === '.ts' || extension === '.tsx' || extension === '.mts' || extension === '.cts' |
| 60 | ? 'typescript' |
| 61 | : 'javascript'; |
| 62 | const relativePath = path.relative(process.cwd(), filePath); |
| 63 | const routing = analyzeRouting(filePath, content); |
| 64 | |
| 65 | const imports: ImportStatement[] = []; |
| 66 | const exports: ExportStatement[] = []; |
| 67 | const dependencyNames = new Set<string>(); |
| 68 | const components: CodeComponent[] = []; |
| 69 | const detectedPatterns: DetectedPattern[] = []; |
| 70 | |
| 71 | if (routing.router === 'app') { |
| 72 | detectedPatterns.push({ category: 'routing', name: 'Next.js App Router' }); |
| 73 | } |
| 74 | if (routing.router === 'pages') { |
| 75 | detectedPatterns.push({ category: 'routing', name: 'Next.js Pages Router' }); |
| 76 | } |
| 77 | if (routing.isClientComponent) { |
| 78 | detectedPatterns.push({ category: 'componentStyle', name: '"use client"' }); |
| 79 | } |
| 80 | if (routing.kind === 'route') { |
| 81 | detectedPatterns.push({ category: 'routing', name: 'Route Handler' }); |
| 82 | } |
| 83 | if (routing.kind === 'api') { |
| 84 | detectedPatterns.push({ category: 'routing', name: 'API Route' }); |
| 85 | } |
| 86 | if (routing.hasMetadata) { |
| 87 | detectedPatterns.push({ category: 'metadata', name: 'Next.js metadata' }); |
| 88 | } |
| 89 | |
| 90 | try { |
| 91 | const program = parse(content, { |
| 92 | loc: true, |
nothing calls this directly
no outgoing calls
no test coverage detected