symref
Version:
Static code checker for AI code agents (Windsurf, Cline, etc.)
93 lines • 3.74 kB
JavaScript
import * as path from 'node:path';
import * as fs from 'node:fs';
import { CallGraphAnalyzer } from '../../analyzer/CallGraphAnalyzer.js';
import { BaseCommand } from '../base/BaseCommand.js';
import { LogLevel } from '../types/LogLevel.js';
import { Project } from 'ts-morph';
/**
* テストコード生成を支援するコマンド
*/
export default class SuggestTestCommand extends BaseCommand {
constructor(options) {
super(options);
const analyzerOptions = {
basePath: options.dir,
tsConfigPath: options.project,
includePatterns: options.include,
excludePatterns: options.exclude
};
// Projectインスタンスを作成
const project = new Project({
tsConfigFilePath: analyzerOptions.tsConfigPath,
skipAddingFilesFromTsConfig: true
});
// ソースファイルを追加
if (analyzerOptions.includePatterns) {
project.addSourceFilesAtPaths(analyzerOptions.includePatterns);
}
else {
project.addSourceFilesAtPaths(path.join(analyzerOptions.basePath, '**/*.ts'));
}
this.analyzer = new CallGraphAnalyzer(project);
}
/**
* コマンドを実行する
* @param args コマンド引数("<from> --to=<to>"の形式)
*/
async execute(args) {
try {
// 引数の形式を検証
const match = args.match(/^"([^"]+)\s*--to=([^"]+)"$/);
if (!match) {
throw new Error('引数の形式が正しくありません。例: "UserService.updateUser --to=DatabaseService.saveUser"');
}
const [, fromSymbol, toSymbol] = match;
// ヘッダーを表示
this.log(`\n=== テストコード生成支援を開始します ===\n`, LogLevel.INFO);
this.log(`対象: ${fromSymbol} → ${toSymbol}\n`, LogLevel.INFO);
// 呼び出し経路を分析
const result = this.analyzer.findPathsFromTo(fromSymbol, toSymbol);
if (result.paths.length === 0) {
this.log('呼び出し経路が見つかりませんでした。', LogLevel.WARN);
return;
}
// テストコードを生成
const testCode = this.generateTestCode(result.paths[0], this.options.testFramework);
// 出力先を決定
const outputPath = this.options.output;
if (outputPath) {
// ファイルに出力
fs.writeFileSync(outputPath, testCode);
this.log(`\nテストコードを ${outputPath} に出力しました。`, LogLevel.INFO);
}
else {
// 標準出力に出力
this.log('\n=== 生成されたテストコード ===\n', LogLevel.INFO);
this.log(testCode, LogLevel.INFO);
}
}
catch (error) {
this.handleError(error);
throw error;
}
}
/**
* テストコードを生成
* @param path 呼び出し経路
* @param framework テストフレームワーク
*/
generateTestCode(path, framework = 'jest') {
// TODO: テストコード生成ロジックを実装
return '// テストコード生成機能は開発中です\n';
}
/**
* コマンドを実行する(静的メソッド)
* @param args コマンド引数
* @param options コマンドオプション
*/
static async execute(args, options) {
const command = new SuggestTestCommand(options);
await command.execute(args);
}
}
//# sourceMappingURL=SuggestTestCommand.js.map