Skip to main content
Glama
SiroSuzume

MCP ts-morph Refactoring Tools

by SiroSuzume
create-source-file-if-not-exists.test.ts1.96 kB
import { Project, IndentationText, QuoteKind, SourceFile } from "ts-morph"; import { describe, it, expect } from "vitest"; import { createSourceFileIfNotExists } from "./create-source-file-if-not-exists"; describe("createSourceFileIfNotExists", () => { const newFilePath = "/test/new-file.ts"; const existingFilePath = "/test/existing-file.ts"; const fileContent = 'export const hello = "world";'; const existingFileContent = "export const existing = true;"; const setupTestProject = () => { const project = new Project({ manipulationSettings: { indentationText: IndentationText.TwoSpaces, quoteKind: QuoteKind.Single, }, useInMemoryFileSystem: true, }); // 既存ファイルをセットアップ時に作成 const existingSourceFile = project.createSourceFile( existingFilePath, existingFileContent, ); return { project, existingSourceFile }; }; it("ファイルが存在しない場合、新しい SourceFile を作成して返す", () => { const { project } = setupTestProject(); // 存在しないことを確認 (任意) expect(project.getSourceFile(newFilePath)).toBeUndefined(); const result = createSourceFileIfNotExists( project, newFilePath, fileContent, ); expect(result).toBeInstanceOf(SourceFile); expect(result.getFilePath()).toBe(newFilePath); expect(project.getSourceFile(newFilePath)).toBe(result); // プロジェクト内のものと同一か expect(result.getText()).toBe(fileContent); // 内容も一応確認 }); it("ファイルが既に存在する場合、既存の SourceFile を返す", () => { const { project, existingSourceFile } = setupTestProject(); const initialFileCount = project.getSourceFiles().length; const result = createSourceFileIfNotExists( project, existingFilePath, "new content should be ignored", ); expect(result).toBe(existingSourceFile); expect(project.getSourceFiles().length).toBe(initialFileCount); }); });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/SiroSuzume/mcp-ts-morph'

If you have feedback or need assistance with the MCP directory API, please join our Discord server