UNPKG

symref

Version:

Static code checker for AI code agents (Windsurf, Cline, etc.)

76 lines 3.53 kB
import path from 'path'; import fs from 'fs/promises'; import { TestPromptGenerator } from '../utils/test-prompt-generator.js'; import { SymbolReferenceAnalyzer } from '../analyzer/SymbolReferenceAnalyzer.js'; export class SuggestTestCommand { static async execute(fromSymbol, options) { try { // アナライザーを初期化 const analyzerOptions = { basePath: '.', tsConfigPath: 'tsconfig.json', includePatterns: ['**/*.{ts,tsx}'], excludePatterns: ['**/node_modules/**', '**/*.d.ts'] }; const analyzer = new SymbolReferenceAnalyzer(analyzerOptions); // 呼び出しグラフを構築 analyzer.buildCallGraph(); // 呼び出し経路を分析 const callGraph = analyzer.traceCallPath(fromSymbol, options.to); // シンボル参照を分析 const refs = analyzer.analyzeSymbol(options.to, { includeInternalReferences: true }); const symbolRefs = { symbol: refs.symbol, definition: refs.definition, declarations: [refs.definition], references: refs.references.map(ref => ({ symbol: refs.symbol, location: ref, type: 'usage' })), dependencies: [] }; // テストコード生成用プロンプトの構築 const prompt = await this.generateTestPrompt(callGraph, symbolRefs, options.testFramework || 'jest'); // 出力処理 await this.writeOutput(prompt, fromSymbol, options.to, options.output); return { success: true, message: 'Test suggestion generated successfully' }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { success: false, message: `Failed to generate test suggestion: ${errorMessage}` }; } } static async generateTestPrompt(callGraph, refs, framework) { const context = { callGraph, symbolRefs: refs, framework }; return TestPromptGenerator.generate(context); } static async writeOutput(prompt, fromSymbol, toSymbol, outputPath) { const timestamp = new Date().toISOString().replace(/[:.]/g, '').slice(0, 15); const defaultFileName = `suggest_test_${fromSymbol.replace(/\./g, '_')}_to_${toSymbol.replace(/\./g, '_')}_${timestamp}.md`; const finalPath = outputPath || path.join(process.cwd(), '.symbols', defaultFileName); await fs.mkdir(path.dirname(finalPath), { recursive: true }); await fs.writeFile(finalPath, prompt, 'utf-8'); } } export function registerSuggestTestCommand(program) { program .command('suggest-test <from>') .description('Generate test suggestions for verifying call paths') .requiredOption('--to <symbol>', 'Target symbol to trace to') .option('--test-framework <framework>', 'Specify test framework (jest/mocha)', 'jest') .option('--output <path>', 'Output file path') .action(async (from, options) => { const result = await SuggestTestCommand.execute(from, options); if (!result.success) { console.error(result.message); process.exit(1); } console.log(result.message); }); } //# sourceMappingURL=suggest-test.js.map