rename_identifier
Renames JavaScript/TypeScript identifiers throughout code while avoiding strings and comments. Use to refactor function, variable, or class names with preview option for safety.
Instructions
Intelligently rename all occurrences of an identifier throughout the code. Avoids renaming in strings/comments.
Examples: • Refactor function names: rename_identifier('fetchData', 'fetchUserData') • Improve variable names: rename_identifier('data', 'userData') • Update class names: rename_identifier('Manager', 'UserManager') • API consistency: rename_identifier('getUserInfo', 'fetchUserInfo') • Preview first: rename_identifier('oldName', 'newName', {preview: true}) • Legacy code update: rename_identifier('XMLHttpRequest', 'fetch')
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| oldName | Yes | Current identifier name to find and replace | |
| newName | Yes | New identifier name (should be valid JavaScript identifier) | |
| preview | No | Return preview only without applying changes (default: false). Always preview first for safety. |
Implementation Reference
- src/index.ts:781-828 (handler)The main handler function that performs the rename operation using tree-hugger-js transform API. Handles preview mode, updates AST and source code, records in transform history, and returns result or preview.private async renameIdentifier(args: { oldName: string; newName: string; preview?: boolean }) { if (!this.currentAST) { return { content: [{ type: "text", text: "No AST loaded. Please use parse_code first.", }], isError: true, }; } try { const transformed = this.currentAST.tree.transform() .rename(args.oldName, args.newName); const result = transformed.toString(); if (!args.preview) { this.currentAST.sourceCode = result; this.currentAST.tree = parse(result); this.currentAST.timestamp = new Date(); } const transformResult: TransformResult = { operation: "rename_identifier", parameters: { oldName: args.oldName, newName: args.newName }, preview: result.slice(0, 500) + (result.length > 500 ? '...' : ''), timestamp: new Date(), }; this.transformHistory.push(transformResult); return { content: [{ type: "text", text: `${args.preview ? 'Preview: ' : ''}Renamed "${args.oldName}" to "${args.newName}"\n\n${args.preview ? 'Preview:\n' : 'Result:\n'}${result}`, }], }; } catch (error) { return { content: [{ type: "text", text: `Error renaming identifier: ${error instanceof Error ? error.message : String(error)}`, }], isError: true, }; } }
- src/index.ts:284-301 (schema)Input schema defining parameters oldName (string, required), newName (string, required), preview (boolean, optional) for the rename_identifier tool.inputSchema: { type: "object", properties: { oldName: { type: "string", description: "Current identifier name to find and replace" }, newName: { type: "string", description: "New identifier name (should be valid JavaScript identifier)" }, preview: { type: "boolean", description: "Return preview only without applying changes (default: false). Always preview first for safety." } }, required: ["oldName", "newName"], },
- src/index.ts:281-302 (registration)Tool registration in ListToolsRequestSchema handler, including name, detailed description with examples, and input schema.{ name: "rename_identifier", description: "Intelligently rename all occurrences of an identifier throughout the code. Avoids renaming in strings/comments.\n\nExamples:\n• Refactor function names: rename_identifier('fetchData', 'fetchUserData')\n• Improve variable names: rename_identifier('data', 'userData')\n• Update class names: rename_identifier('Manager', 'UserManager')\n• API consistency: rename_identifier('getUserInfo', 'fetchUserInfo')\n• Preview first: rename_identifier('oldName', 'newName', {preview: true})\n• Legacy code update: rename_identifier('XMLHttpRequest', 'fetch')", inputSchema: { type: "object", properties: { oldName: { type: "string", description: "Current identifier name to find and replace" }, newName: { type: "string", description: "New identifier name (should be valid JavaScript identifier)" }, preview: { type: "boolean", description: "Return preview only without applying changes (default: false). Always preview first for safety." } }, required: ["oldName", "newName"], }, },
- src/index.ts:431-432 (registration)Dispatcher case in CallToolRequestSchema handler that routes the tool call to the renameIdentifier method.case "rename_identifier": return await this.renameIdentifier(args as { oldName: string; newName: string; preview?: boolean });