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.

43 lines 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VariableReplacer = void 0; const context_analyzer_1 = require("./context-analyzer"); class VariableReplacer { constructor() { this.contextAnalyzer = new context_analyzer_1.ContextAnalyzer(); } replaceAllReferences(sourceFile, variableName, declaration, initializerText) { const references = this.findAllReferences(sourceFile, variableName, declaration); for (const reference of references) { reference.replaceWithText(initializerText); } } removeDeclaration(declaration) { const declarationStatement = declaration.getVariableStatement(); if (declarationStatement) { declarationStatement.remove(); } } findAllReferences(sourceFile, variableName, declaration) { const references = []; const declarationNode = declaration.getNameNode(); this.collectMatchingIdentifiers(sourceFile, variableName, declarationNode, references); return references; } collectMatchingIdentifiers(sourceFile, variableName, declarationNode, references) { sourceFile.forEachDescendant((node) => { if (this.isMatchingIdentifier(node, variableName, declarationNode)) { references.push(node); } }); } isMatchingIdentifier(node, variableName, declarationNode) { return node.getKind() === 80 && node.getText() === variableName && node !== declarationNode && !this.contextAnalyzer.isInTypeContext(node) && !this.contextAnalyzer.isInDestructuringPattern(node); } } exports.VariableReplacer = VariableReplacer; //# sourceMappingURL=variable-replacer.js.map