|
| 1 | +/** |
| 2 | + * @module basic-astro |
| 3 | + * Extract TypeScript/JavaScript code from Astro frontmatter |
| 4 | + * Reuses basic-node extractors by parsing frontmatter as TypeScript |
| 5 | + */ |
| 6 | + |
| 7 | +import { Project, ts } from 'ts-morph'; |
| 8 | +import { createLogger } from '../../../core/logger.js'; |
| 9 | +import { extractClasses } from '../basic-node/class-extractor.js'; |
| 10 | +import { |
| 11 | + extractFunctions, |
| 12 | + extractArrowFunctions, |
| 13 | +} from '../basic-node/function-extractor.js'; |
| 14 | +import { extractTypeAliases, extractInterfaces } from '../basic-node/type-extractor.js'; |
| 15 | +import type { |
| 16 | + ExtractedClass, |
| 17 | + ExtractedFunction, |
| 18 | + ExtractedType, |
| 19 | + ExtractedInterface, |
| 20 | +} from '../basic-node/types.js'; |
| 21 | + |
| 22 | +const log = createLogger({ context: 'AstroCodeExtractor' }); |
| 23 | + |
| 24 | +export interface CodeExtractionResult { |
| 25 | + classes: ExtractedClass[]; |
| 26 | + functions: ExtractedFunction[]; |
| 27 | + types: ExtractedType[]; |
| 28 | + interfaces: ExtractedInterface[]; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Extract TypeScript/JavaScript code from Astro frontmatter |
| 33 | + * Parses the frontmatter as TypeScript and uses basic-node extractors |
| 34 | + * |
| 35 | + * @param frontmatter - The frontmatter content (code between --- markers) |
| 36 | + * @param filePath - Original Astro file path (for error reporting) |
| 37 | + * @returns Extracted code items (classes, functions, types, interfaces) |
| 38 | + */ |
| 39 | +export function extractCodeFromFrontmatter( |
| 40 | + frontmatter: string, |
| 41 | + filePath: string, |
| 42 | +): CodeExtractionResult { |
| 43 | + // Return empty result if no frontmatter |
| 44 | + if (!frontmatter || frontmatter.trim().length === 0) { |
| 45 | + return { |
| 46 | + classes: [], |
| 47 | + functions: [], |
| 48 | + types: [], |
| 49 | + interfaces: [], |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + try { |
| 54 | + // Create a ts-morph project to parse the frontmatter |
| 55 | + const project = new Project({ |
| 56 | + compilerOptions: { |
| 57 | + allowJs: true, |
| 58 | + jsx: ts.JsxEmit.React, |
| 59 | + target: ts.ScriptTarget.ESNext, |
| 60 | + module: ts.ModuleKind.ESNext, |
| 61 | + }, |
| 62 | + skipAddingFilesFromTsConfig: true, |
| 63 | + useInMemoryFileSystem: true, |
| 64 | + }); |
| 65 | + |
| 66 | + // Create a virtual .ts file with the frontmatter content |
| 67 | + // Use original file path with .ts extension for better error messages |
| 68 | + const virtualPath = filePath.replace(/\.astro$/, '.frontmatter.ts'); |
| 69 | + const sourceFile = project.createSourceFile(virtualPath, frontmatter); |
| 70 | + |
| 71 | + // Extract code using basic-node extractors |
| 72 | + const classes = extractClasses(sourceFile); |
| 73 | + const regularFunctions = extractFunctions(sourceFile); |
| 74 | + const arrowFunctions = extractArrowFunctions(sourceFile); |
| 75 | + const functions = [...regularFunctions, ...arrowFunctions]; |
| 76 | + const types = extractTypeAliases(sourceFile); |
| 77 | + const interfaces = extractInterfaces(sourceFile); |
| 78 | + |
| 79 | + log.debug( |
| 80 | + `Extracted from ${filePath} frontmatter: ${classes.length} classes, ${functions.length} functions, ${types.length} types, ${interfaces.length} interfaces`, |
| 81 | + ); |
| 82 | + |
| 83 | + return { |
| 84 | + classes, |
| 85 | + functions, |
| 86 | + types, |
| 87 | + interfaces, |
| 88 | + }; |
| 89 | + } catch (error) { |
| 90 | + const errorMsg = error instanceof Error ? error.message : String(error); |
| 91 | + log.warn(`Failed to extract code from ${filePath} frontmatter: ${errorMsg}`); |
| 92 | + |
| 93 | + // Return empty result on error |
| 94 | + return { |
| 95 | + classes: [], |
| 96 | + functions: [], |
| 97 | + types: [], |
| 98 | + interfaces: [], |
| 99 | + }; |
| 100 | + } |
| 101 | +} |
0 commit comments