refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
83 lines • 2.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RenameVariableTransformation = void 0;
class RenameVariableTransformation {
constructor(_declaration, _usages, _newName) {
this._declaration = _declaration;
this._usages = _usages;
this._newName = _newName;
}
async transform(_sourceFile) {
const result = await this.transformWithResult();
if (!result.success) {
throw new Error(result.message || 'Rename transformation failed');
}
}
async transformWithResult() {
try {
const variableName = this._declaration.getText();
const changesCount = this.performDirectRename();
return this.buildSuccessResult(variableName, changesCount);
}
catch (error) {
return this.buildErrorResult(error);
}
}
performDirectRename() {
const declarationChanges = this.renameDeclaration();
const usageChanges = this.renameUsages();
return declarationChanges + usageChanges;
}
renameDeclaration() {
const declarationIdentifier = this.findIdentifierInNode(this._declaration);
if (declarationIdentifier) {
declarationIdentifier.replaceWithText(this._newName);
return 1;
}
return 0;
}
renameUsages() {
return this.renameAllUsageNodes();
}
renameAllUsageNodes() {
return this.processUsageNodes();
}
processUsageNodes() {
let changesCount = 0;
for (const usage of this._usages) {
if (this.renameUsageNode(usage))
changesCount++;
}
return changesCount;
}
renameUsageNode(usage) {
const usageIdentifier = this.findIdentifierInNode(usage);
if (usageIdentifier) {
usageIdentifier.replaceWithText(this._newName);
return true;
}
return false;
}
findIdentifierInNode(node) {
if (node.getKind() === 80) {
return node;
}
return node.getFirstDescendantByKind(80);
}
buildSuccessResult(variableName, changesCount) {
return {
success: true,
changesCount,
message: `Renamed ${changesCount} occurrences of '${variableName}' to '${this._newName}'`
};
}
buildErrorResult(error) {
return {
success: false,
changesCount: 0,
message: error instanceof Error ? error.message : 'Unknown error during rename'
};
}
}
exports.RenameVariableTransformation = RenameVariableTransformation;
//# sourceMappingURL=rename-variable-transformation.js.map