refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
71 lines • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RegexNodeFinder = void 0;
const node_finder_1 = require("./node-finder");
const ast_service_1 = require("../services/ast-service");
const expression_locator_1 = require("../locators/expression-locator");
class RegexNodeFinder extends node_finder_1.NodeFinder {
constructor() {
super();
this.astService = new ast_service_1.ASTService();
this.expressionLocator = new expression_locator_1.ExpressionLocator(this.astService.getProject());
}
findNodes(file, pattern) {
const sourceFile = this.astService.loadSourceFile(file);
const matchingNodes = this.filterNodesByRegex(sourceFile, pattern);
const matches = this.createNodeMatches(sourceFile, matchingNodes);
return {
query: pattern,
matches
};
}
async findExpressions(file, pattern) {
const result = await this.expressionLocator.findExpressionsByRegex(file, pattern);
const serializableMatches = this.createSerializableExpressionMatches(result.matches);
return {
query: result.query,
matches: serializableMatches
};
}
filterNodesByRegex(sourceFile, pattern) {
const regex = new RegExp(pattern);
const allNodes = this.getAllNodes(sourceFile);
return allNodes.filter(node => regex.test(node.getText()));
}
getAllNodes(sourceFile) {
const nodes = [];
sourceFile.forEachDescendant((node) => {
nodes.push(node);
});
return nodes;
}
createNodeMatches(sourceFile, nodes) {
return nodes.map(node => this.createNodeMatch(sourceFile, node));
}
createNodeMatch(sourceFile, node) {
const location = sourceFile.getLineAndColumnAtPos(node.getStart());
return {
kind: node.getKindName(),
text: this.truncateText(node.getText()),
line: location.line,
column: location.column
};
}
createSerializableExpressionMatches(matches) {
return matches.map(match => ({
expression: match.expression,
type: match.type,
line: match.line,
column: match.column,
scope: match.scope
}));
}
truncateText(text) {
const maxLength = 50;
if (text.length <= maxLength)
return text;
return text.substring(0, maxLength) + '...';
}
}
exports.RegexNodeFinder = RegexNodeFinder;
//# sourceMappingURL=regex-node-finder.js.map