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.

52 lines 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StatementInserter = void 0; const ts_morph_1 = require("ts-morph"); const extraction_scope_analyzer_1 = require("./extraction-scope-analyzer"); class StatementInserter { constructor() { this.scopeAnalyzer = new extraction_scope_analyzer_1.ExtractionScopeAnalyzer(); } insertVariableDeclaration(beforeNode, variableName) { const statement = this.scopeAnalyzer.findContainingStatement(beforeNode); if (!statement) { throw new Error('Cannot find containing statement for variable declaration'); } const declarationText = this.createDeclarationText(beforeNode, variableName); this.insertDeclarationAtStatement(statement, declarationText); } createDeclarationText(node, variableName) { const expressionText = node.getText(); return `const ${variableName} = ${expressionText};`; } insertDeclarationAtStatement(statement, declarationText) { const parent = statement.getParent(); if (ts_morph_1.Node.isBlock(parent) || ts_morph_1.Node.isSourceFile(parent)) { this.insertAtStatementIndex(parent, statement, declarationText); } } insertAtStatementIndex(parent, statement, declarationText) { if (ts_morph_1.Node.isBlock(parent)) { this.insertInBlock(parent, statement, declarationText); } else if (ts_morph_1.Node.isSourceFile(parent)) { this.insertInSourceFile(parent, statement, declarationText); } } insertInBlock(parent, statement, declarationText) { const statements = parent.getStatements(); const index = statements.findIndex((s) => s === statement); if (index !== -1) { parent.insertStatements(index, [declarationText]); } } insertInSourceFile(parent, statement, declarationText) { const statements = parent.getStatements(); const index = statements.findIndex((s) => s === statement); if (index !== -1) { parent.insertStatements(index, [declarationText]); } } } exports.StatementInserter = StatementInserter; //# sourceMappingURL=statement-inserter.js.map