rename_symbol_by_tsmorph
Renames TypeScript/JavaScript symbols across projects by analyzing the AST. Ensures consistent updates in all file references, ideal for cross-file refactoring. Includes dry-run mode for verification.
Instructions
[Uses ts-morph] Renames TypeScript/JavaScript symbols across the project.
Analyzes the AST (Abstract Syntax Tree) to track and update references throughout the project, not just the definition site. Useful for cross-file refactoring tasks during Vibe Coding.
Usage
Use this tool, for example, when you change a function name defined in one file
and want to reflect that change in other files that import and use it.
ts-morph parses the project based on tsconfig.json
to resolve symbol references
and perform the rename.
- Specify the exact location (file path, line, column) of the symbol (function name, variable name, class name, etc.) you want to rename. This is necessary for ts-morph to identify the target Identifier node in the AST.
- Specify the current symbol name and the new symbol name.
- Specify the symbol kind (
function
,variable
,class
). This allows additional validation to ensure the node identified by ts-morph is of the expected type (e.g., an Identifier within a FunctionDeclaration), preventing unintended renames. - It's recommended to first run with
dryRun: true
to check which files ts-morph will modify. - 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): Path to the project's root
tsconfig.json
file. Essential for ts-morph to correctly parse the project structure and file references. Must be an absolute path (relative paths can be misinterpreted). - targetFilePath (string, required): Path to the file where the symbol to be renamed is defined (or first appears). Must be an absolute path (relative paths can be misinterpreted).
- position (object, required): The exact position on the symbol to be renamed.
Serves as the starting point for ts-morph to locate the AST node.
- line (number, required): 1-based line number, typically obtained from an editor.
- column (number, required): 1-based column number (position of the first character of the symbol name), typically obtained from an editor.
- symbolName (string, required): The current name of the symbol before renaming. Used to verify against the node name found at the specified position.
- newName (string, required): The new name for the symbol after renaming.
- symbolKind (string, required): The kind of the symbol ("function", "variable", "class", etc.). Used to verify the type of the target by checking the kind of the parent node identified by ts-morph (e.g., FunctionDeclaration).
- 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 (or scheduled to be modified if dryRun) by the rename.
- On failure: Returns a message indicating the error.
Input Schema
Name | Required | Description | Default |
---|---|---|---|
dryRun | No | If true, only show intended changes without modifying files. | |
newName | Yes | The new name for the symbol. | |
position | Yes | The exact position of the symbol to rename. | |
symbolKind | Yes | The kind of the symbol (e.g., "function", "variable", "class"). | |
symbolName | Yes | The current name of the symbol. | |
targetFilePath | Yes | Path to the file containing the symbol to rename. | |
tsconfigPath | Yes | Path to the project's tsconfig.json file. |
Input Schema (JSON Schema)
You must be authenticated.
Other Tools from MCP ts-morph Refactoring Tools
Related Tools
- @SiroSuzume/mcp-ts-morph
- @SiroSuzume/mcp-ts-morph
- @SiroSuzume/mcp-ts-morph
- @bsmi021/mcp-node-omnibus-server
- @bsmi021/mcp-node-omnibus-server