refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
46 lines • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VariableDeclarationFinder = void 0;
const ts_morph_1 = require("ts-morph");
class VariableDeclarationFinder {
findVariableDeclaration(sourceFile, variableName) {
const declaration = this.searchForDeclaration(sourceFile, variableName);
if (!declaration) {
throw new Error(`Variable declaration for '${variableName}' not found`);
}
return declaration;
}
searchForDeclaration(sourceFile, variableName) {
const variableDeclarations = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.VariableDeclaration);
for (const decl of variableDeclarations) {
if (this.matchesVariableName(decl, variableName)) {
return decl;
}
}
return undefined;
}
matchesVariableName(declaration, variableName) {
if (declaration.getName() === variableName) {
return true;
}
return this.matchesDestructuredVariable(declaration, variableName);
}
matchesDestructuredVariable(declaration, variableName) {
const nameNode = declaration.getNameNode();
if (!ts_morph_1.Node.isObjectBindingPattern(nameNode)) {
return false;
}
return this.hasMatchingElement(nameNode, variableName);
}
hasMatchingElement(nameNode, variableName) {
const bindingPattern = nameNode.asKindOrThrow(ts_morph_1.SyntaxKind.ObjectBindingPattern);
for (const element of bindingPattern.getElements()) {
if (element.getName() === variableName) {
return true;
}
}
return false;
}
}
exports.VariableDeclarationFinder = VariableDeclarationFinder;
//# sourceMappingURL=variable-declaration-finder.js.map