refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
117 lines • 4.63 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.BoundaryAnalyzer = void 0;
const ts_morph_1 = require("ts-morph");
const path = __importStar(require("path"));
class BoundaryAnalyzer {
findBoundaryMatches(sourceFile, options) {
const matchContext = this.prepareBoundaryContext(sourceFile, options);
return this.executeBoundaryMatching(matchContext);
}
prepareBoundaryContext(sourceFile, options) {
return {
sourceFile,
pattern: options.regex || '',
boundaryType: options.boundaries || '',
fileName: path.basename(sourceFile.getFilePath())
};
}
executeBoundaryMatching(context) {
if (context.boundaryType === 'function') {
return this.findFunctionBoundaryMatches(context.sourceFile, context.pattern, context.fileName);
}
return [];
}
findFunctionBoundaryMatches(sourceFile, pattern, fileName) {
const regex = new RegExp(pattern);
const allFunctions = this.getAllFunctions(sourceFile);
return this.filterAndFormatMatches(allFunctions, regex, fileName);
}
getAllFunctions(sourceFile) {
const functions = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.FunctionDeclaration);
const methods = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.MethodDeclaration);
const arrowFunctions = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.ArrowFunction);
return [...functions, ...methods, ...arrowFunctions];
}
filterAndFormatMatches(functions, regex, fileName) {
return this.processAllFunctions(functions, regex, fileName);
}
processAllFunctions(functions, regex, fileName) {
const results = [];
for (const func of functions) {
const result = this.processFunction(func, regex, fileName);
if (result)
results.push(result);
}
return results;
}
processFunction(func, regex, fileName) {
return this.functionMatchesPattern(func, regex)
? this.formatFunctionResult(func, fileName)
: null;
}
functionMatchesPattern(func, regex) {
return regex.test(func.getText());
}
formatFunctionResult(func, fileName) {
const positions = this.getFunctionPositions(func);
return {
location: `[${fileName} ${positions.startLine}:-${positions.endLine}:]`,
content: func.getText()
};
}
getFunctionPositions(func) {
const sourceFile = func.getSourceFile();
const nodePositions = this.getNodePositions(func);
return this.createLinePositions(sourceFile, nodePositions);
}
getNodePositions(func) {
return {
start: func.getStart(),
end: func.getEnd()
};
}
createLinePositions(sourceFile, nodePositions) {
const startPos = sourceFile.getLineAndColumnAtPos(nodePositions.start);
const endPos = sourceFile.getLineAndColumnAtPos(nodePositions.end);
return {
startLine: startPos.line,
endLine: endPos.line
};
}
}
exports.BoundaryAnalyzer = BoundaryAnalyzer;
//# sourceMappingURL=boundary-analyzer.js.map