UNPKG

refakts

Version:

TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.

57 lines 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RenameCommand = void 0; const ast_service_1 = require("../services/ast-service"); const variable_locator_1 = require("../locators/variable-locator"); const rename_variable_transformation_1 = require("../transformations/rename-variable-transformation"); class RenameCommand { constructor() { this.name = 'rename'; this.description = 'Rename a variable and all its references'; this.complete = true; this.astService = new ast_service_1.ASTService(); this.variableLocator = new variable_locator_1.VariableLocator(this.astService.getProject()); } async execute(file, options) { this.validateOptions(options); const sourceFile = this.astService.loadSourceFile(file); const node = this.findTargetNode(options); await this.performRename(node, options.to); await this.astService.saveSourceFile(sourceFile); } findTargetNode(options) { return this.astService.findNodeByLocation(options.location); } validateOptions(options) { if (!options.location) { throw new Error('Location format must be specified'); } if (!options.to) { throw new Error('--to must be specified for rename operations'); } } getHelpText() { return '\nExamples:\n refakts rename "[src/file.ts 5:8-5:18]" --to newName'; } async performRename(node, newName) { this.validateIdentifierNode(node); const sourceFile = node.getSourceFile(); const nodeResult = this.findVariableNodesAtPosition(node, sourceFile); const transformation = this.createRenameTransformation(nodeResult, newName); await transformation.transform(sourceFile); } validateIdentifierNode(node) { if (node.getKind() !== 80) { throw new Error(`Expected identifier, got ${node.getKindName()}`); } } findVariableNodesAtPosition(node, sourceFile) { const targetPosition = sourceFile.getLineAndColumnAtPos(node.getStart()); return this.variableLocator.findVariableNodesByPositionSync(sourceFile, targetPosition.line, targetPosition.column); } createRenameTransformation(nodeResult, newName) { return new rename_variable_transformation_1.RenameVariableTransformation(nodeResult.declaration, nodeResult.usages.map((u) => u.node), newName); } } exports.RenameCommand = RenameCommand; //# sourceMappingURL=rename-command.js.map