refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
112 lines • 4.78 kB
JavaScript
;
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.ASTService = void 0;
const ts_morph_1 = require("ts-morph");
const path = __importStar(require("path"));
class ASTService {
constructor(project) {
this.project = project || new ts_morph_1.Project();
}
loadSourceFile(filePath) {
const absolutePath = path.resolve(filePath);
const existingFile = this.project.getSourceFile(absolutePath);
if (existingFile) {
return existingFile;
}
return this.project.addSourceFileAtPath(absolutePath);
}
async saveSourceFile(sourceFile) {
await sourceFile.save();
}
getProject() {
return this.project;
}
findNodeByLocation(location) {
const sourceFile = this.loadSourceFile(location.file);
return this.findNodeInRange(sourceFile, location);
}
findNodeInRange(sourceFile, location) {
const startPos = this.getStartPosition(sourceFile, location);
const node = this.getNodeAtPosition(sourceFile, startPos, location);
return this.findBestMatchingNode(sourceFile, node, location) || node;
}
getStartPosition(sourceFile, location) {
try {
return sourceFile.compilerNode.getPositionOfLineAndCharacter(location.startLine - 1, location.startColumn - 1);
}
catch {
throw new Error(`No node found at position ${location.startLine}:${location.startColumn}`);
}
}
getNodeAtPosition(sourceFile, startPos, location) {
const node = sourceFile.getDescendantAtPos(startPos);
if (!node) {
throw new Error(`No node found at position ${location.startLine}:${location.startColumn}`);
}
return node;
}
findBestMatchingNode(sourceFile, node, location) {
const expectedEnd = this.calculateExpectedEnd(sourceFile, location);
const expectedStart = this.getStartPosition(sourceFile, location);
return this.traverseToFindMatchingNode(node, expectedStart, expectedEnd);
}
calculateExpectedEnd(sourceFile, location) {
return sourceFile.compilerNode.getPositionOfLineAndCharacter(location.endLine - 1, location.endColumn - 1);
}
traverseToFindMatchingNode(node, expectedStart, expectedEnd, bestMatch = null, bestScore = Infinity) {
if (!node)
return bestMatch;
if (this.isNodeRangeCloseToExpected(node, expectedStart, expectedEnd)) {
const score = this.calculateNodeScore(node, expectedStart, expectedEnd);
if (score < bestScore) {
return this.traverseToFindMatchingNode(node.getParent(), expectedStart, expectedEnd, node, score);
}
}
return this.traverseToFindMatchingNode(node.getParent(), expectedStart, expectedEnd, bestMatch, bestScore);
}
isNodeRangeCloseToExpected(node, expectedStart, expectedEnd) {
const startDiff = Math.abs(node.getStart() - expectedStart);
const endDiff = Math.abs(node.getEnd() - expectedEnd);
return startDiff <= 3 && endDiff <= 3;
}
calculateNodeScore(node, expectedStart, expectedEnd) {
const startDiff = Math.abs(node.getStart() - expectedStart);
const endDiff = Math.abs(node.getEnd() - expectedEnd);
return startDiff + endDiff;
}
}
exports.ASTService = ASTService;
//# sourceMappingURL=ast-service.js.map