rename_identifier
Refactor code by renaming identifiers intelligently, excluding strings and comments. Preview changes before applying to ensure accuracy and consistency in JavaScript/TypeScript projects.
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 |
|---|---|---|---|
| newName | Yes | New identifier name (should be valid JavaScript identifier) | |
| oldName | Yes | Current identifier name to find and replace | |
| preview | No | Return preview only without applying changes (default: false). Always preview first for safety. |
Input Schema (JSON Schema)
Implementation Reference
- src/index.ts:781-828 (handler)The core handler function that performs the identifier rename using the tree-hugger-js library's transform.rename method. Handles preview mode, updates the AST, and records the transformation.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 for the rename_identifier tool: oldName (required), newName (required), and optional preview boolean.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 the ListToolsRequestSchema handler, including name, description, 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)Switch case in CallToolRequestSchema handler that routes calls to the renameIdentifier method.case "rename_identifier": return await this.renameIdentifier(args as { oldName: string; newName: string; preview?: boolean });