MCP ts-morph Refactoring Tools

rename_filesystem_entry_by_tsmorph

Renames a TypeScript/JavaScript file or folder and updates all import/export paths referencing it across the project. Analyzes tsconfig.json to ensure accurate path corrections. Supports dry-run for previewing changes. Use for consistent renaming with minimal manual intervention.

Instructions

[Uses ts-morph] Renames a single TypeScript/JavaScript file OR FOLDER and updates all import/export paths referencing it throughout the project.

Analyzes the project based on tsconfig.json to find all references to the file/folder being renamed and automatically corrects its paths. Includes a remark about potential issues with path aliases and relative index imports.

Usage

Use this tool when you want to rename a file (e.g., utils.ts -> helpers.ts) or a folder (e.g., src/data -> src/coreData) and need all the import statements in other files that point to it to be automatically updated.

  1. Specify the path to the project's tsconfig.json file. Must be an absolute path.
  2. Specify the current absolute path of the file or folder to rename.
  3. Specify the new desired absolute path for the file or folder.
  4. It's recommended to first run with dryRun: true to check which files will be affected.
  5. If the preview looks correct, run with dryRun: false (or omit it) to actually save the changes to the file system.

Parameters

  • tsconfigPath (string, required): Absolute path to the project's root tsconfig.json file. Essential for ts-morph to parse the project. Must be an absolute path.
  • oldPath (string, required): The current absolute path of the file or folder to rename. Must be an absolute path.
  • newPath (string, required): The new desired absolute path for the file or folder. Must be an absolute path.
  • dryRun (boolean, optional): If set to true, prevents ts-morph from making and saving file changes, returning only the list of files that would be affected. Useful for verification. Defaults to false.

Result

  • On success: Returns a message containing the list of file paths modified (the renamed file/folder and files with updated imports) or scheduled to be modified if dryRun.
  • On failure: Returns a message indicating the error.

Remarks (Added)

  • Caution: Updating import/export statements containing path aliases (like @/) or relative paths referencing a directory's index.ts (like import from '.' ) might be incomplete in the current ts-morph implementation. Manual verification and correction might be necessary after renaming.

Input Schema

NameRequiredDescriptionDefault
dryRunNoIf true, only show intended changes without modifying files.
newPathYesThe new desired absolute path for the file or folder.
oldPathYesThe current absolute path of the file or folder to rename.
tsconfigPathYesAbsolute path to the project's tsconfig.json file.

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "dryRun": { "default": false, "description": "If true, only show intended changes without modifying files.", "type": "boolean" }, "newPath": { "description": "The new desired absolute path for the file or folder.", "type": "string" }, "oldPath": { "description": "The current absolute path of the file or folder to rename.", "type": "string" }, "tsconfigPath": { "description": "Absolute path to the project's tsconfig.json file.", "type": "string" } }, "required": [ "tsconfigPath", "oldPath", "newPath" ], "type": "object" }

You must be authenticated.

Other Tools from MCP ts-morph Refactoring Tools

Related Tools

ID: byunmqla3h