refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
85 lines • 2.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtractionScopeAnalyzer = void 0;
const ts_morph_1 = require("ts-morph");
class ExtractionScopeAnalyzer {
findExtractionScope(node) {
const scope = this.searchForValidScope(node);
return scope || node.getSourceFile();
}
findContainingStatement(node) {
let current = node;
while (current) {
if (this.isContainingStatement(current)) {
return current;
}
current = current.getParent();
}
return undefined;
}
isValidExtractionScope(parent) {
return parent !== undefined && (ts_morph_1.Node.isBlock(parent) || ts_morph_1.Node.isSourceFile(parent));
}
findScopeName(node) {
const scopeName = this.searchParentScopes(node.getParent());
return scopeName || 'unknown scope';
}
searchForValidScope(node) {
let current = node;
while (current) {
const validScope = this.checkCurrentNodeForValidScope(current);
if (validScope)
return validScope;
current = current.getParent();
}
return undefined;
}
checkCurrentNodeForValidScope(current) {
const parent = current.getParent();
return this.isValidExtractionScope(parent) ? parent : undefined;
}
isContainingStatement(current) {
const parent = current.getParent();
return this.isValidExtractionScope(parent);
}
searchParentScopes(current) {
while (current) {
const scopeName = this.getScopeNameForNode(current);
if (scopeName) {
return scopeName;
}
current = current.getParent();
}
return null;
}
getScopeNameForNode(node) {
if (ts_morph_1.Node.isFunctionDeclaration(node) || ts_morph_1.Node.isFunctionExpression(node)) {
return this.getFunctionScopeName(node);
}
if (ts_morph_1.Node.isMethodDeclaration(node)) {
return `method ${node.getName()}`;
}
return this.getOtherScopeNames(node);
}
getFunctionScopeName(node) {
const name = node.getName() || null;
return `function ${name || '<anonymous>'}`;
}
getOtherScopeNames(node) {
if (ts_morph_1.Node.isArrowFunction(node)) {
return 'arrow function';
}
return this.getSpecialScopeNames(node);
}
getSpecialScopeNames(node) {
if (ts_morph_1.Node.isConstructorDeclaration(node)) {
return 'constructor';
}
if (ts_morph_1.Node.isSourceFile(node)) {
return 'file scope';
}
return null;
}
}
exports.ExtractionScopeAnalyzer = ExtractionScopeAnalyzer;
//# sourceMappingURL=extraction-scope-analyzer.js.map