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.

122 lines 4.83 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ExpressionLocator = void 0; const ts_morph_1 = require("ts-morph"); const tsquery_handler_1 = require("../tsquery-handler"); const extraction_scope_analyzer_1 = require("../services/extraction-scope-analyzer"); const path = __importStar(require("path")); class ExpressionLocator { constructor(project) { this.tsQueryHandler = new tsquery_handler_1.TSQueryHandler(); this.scopeAnalyzer = new extraction_scope_analyzer_1.ExtractionScopeAnalyzer(); this.project = project || new ts_morph_1.Project(); } async findExpressions(filePath, query) { try { const result = await this.processExpressionQuery(filePath, query); return result; } catch (error) { throw new Error(`Failed to process file ${filePath}: ${error}`); } } async findExpressionsByRegex(filePath, pattern) { try { const result = await this.processExpressionRegex(filePath, pattern); return result; } catch (error) { throw new Error(`Failed to process file ${filePath}: ${error}`); } } async processExpressionQuery(filePath, query) { const sourceFile = this.loadSourceFile(filePath); const matchingNodes = this.tsQueryHandler.findNodesByQuery(sourceFile, query); const matches = this.createExpressionMatches(sourceFile, matchingNodes); return { query, matches }; } async processExpressionRegex(filePath, pattern) { const sourceFile = this.loadSourceFile(filePath); const regex = new RegExp(pattern); const allNodes = this.getAllExpressionNodes(sourceFile); const matchingNodes = allNodes.filter(node => regex.test(node.getText())); const matches = this.createExpressionMatches(sourceFile, matchingNodes); return { query: pattern, matches }; } createExpressionMatches(sourceFile, nodes) { const matches = []; for (const node of nodes) { this.addExpressionMatch(sourceFile, node, matches); } return matches; } addExpressionMatch(sourceFile, node, matches) { if (ts_morph_1.Node.isExpression(node)) { matches.push(this.createExpressionMatch(sourceFile, node)); } } createExpressionMatch(sourceFile, node) { const location = sourceFile.getLineAndColumnAtPos(node.getStart()); const scope = this.scopeAnalyzer.findScopeName(node); return this.buildExpressionMatch(node, location, scope); } buildExpressionMatch(node, location, scope) { return { expression: node.getText(), type: node.getKindName(), line: location.line, column: location.column, scope: scope, node: node }; } loadSourceFile(filePath) { const absolutePath = path.resolve(filePath); if (this.project.getSourceFile(absolutePath)) { return this.project.getSourceFile(absolutePath); } return this.project.addSourceFileAtPath(absolutePath); } getAllExpressionNodes(sourceFile) { const nodes = []; sourceFile.forEachDescendant((node) => { nodes.push(node); }); return nodes; } } exports.ExpressionLocator = ExpressionLocator; //# sourceMappingURL=expression-locator.js.map