()
| 75 | } |
| 76 | |
| 77 | async function main() { |
| 78 | const wasmBuf = readFileSync(join(PUBLIC_DIR, 'web-tree-sitter.wasm')); |
| 79 | await Parser.init({ |
| 80 | locateFile: () => join(PUBLIC_DIR, 'web-tree-sitter.wasm'), |
| 81 | wasmBinary: wasmBuf, |
| 82 | }); |
| 83 | |
| 84 | // Dynamically import the extractor (works if running via tsx or after build) |
| 85 | let extractGeneric; |
| 86 | try { |
| 87 | const mod = |
| 88 | await import('../src/runner/browser/parser/extractors/generic.ts'); |
| 89 | extractGeneric = mod.extractGeneric; |
| 90 | } catch { |
| 91 | console.error( |
| 92 | 'Could not import extractGeneric. Run with: npx tsx scripts/generate-generic-fixtures.mjs', |
| 93 | ); |
| 94 | process.exit(1); |
| 95 | } |
| 96 | |
| 97 | let totalGenerated = 0; |
| 98 | |
| 99 | for (const { lang, ext, wasm } of LANGUAGES) { |
| 100 | const extractionDir = join(FIXTURE_ROOT, lang, 'extraction'); |
| 101 | if (!existsSync(extractionDir)) continue; |
| 102 | |
| 103 | const parser = new Parser(); |
| 104 | const langBuf = readFileSync(join(PUBLIC_DIR, wasm)); |
| 105 | const language = await Language.load(langBuf); |
| 106 | parser.setLanguage(language); |
| 107 | |
| 108 | const sourceFiles = readdirSync(extractionDir).filter((f) => |
| 109 | f.endsWith(ext), |
| 110 | ); |
| 111 | |
| 112 | for (const sourceFile of sourceFiles) { |
| 113 | const name = sourceFile.slice(0, -ext.length); |
| 114 | const source = readFileSync(join(extractionDir, sourceFile), 'utf-8'); |
| 115 | const tree = parser.parse(source); |
| 116 | if (!tree) { |
| 117 | console.warn(` WARN: failed to parse ${sourceFile}`); |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | const result = extractGeneric(tree.rootNode, lang); |
| 122 | const normalized = result.symbols.map(normalize); |
| 123 | const outPath = join(extractionDir, `${name}.expected.json`); |
| 124 | writeFileSync(outPath, JSON.stringify(normalized, null, 2) + '\n'); |
| 125 | console.log(` ${lang}/${name}.expected.json`); |
| 126 | totalGenerated++; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | console.log(`\nGenerated ${totalGenerated} fixture files.`); |
| 131 | } |
| 132 | |
| 133 | main().catch(console.error); |
no test coverage detected