refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
97 lines • 4.26 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.VariableNodeMatcher = void 0;
const ts = __importStar(require("typescript"));
const source_file_helper_1 = require("./source-file-helper");
const shadowing_detector_1 = require("./shadowing-detector");
class VariableNodeMatcher {
constructor() {
this.shadowingDetector = new shadowing_detector_1.ShadowingDetector();
}
findDeclaration(sourceFile, variableName) {
return source_file_helper_1.SourceFileHelper.findDescendant(sourceFile, node => this.isMatchingDeclaration(node, variableName));
}
findUsages(sourceFile, variableName, declaration) {
const usages = [];
const declarationIdentifier = this.getDeclarationIdentifier(declaration);
this.collectUsages(sourceFile, usages, variableName, declarationIdentifier, declaration);
return usages;
}
collectUsages(sourceFile, usages, variableName, declarationIdentifier, declaration) {
sourceFile.forEachDescendant((node) => {
if (this.isValidUsage(node, variableName, declarationIdentifier, declaration)) {
usages.push(node);
}
});
}
getVariableName(declaration) {
const identifier = declaration.getFirstDescendantByKind(ts.SyntaxKind.Identifier);
if (!identifier) {
throw new Error('Declaration node does not contain an identifier');
}
return identifier.getText();
}
isMatchingDeclaration(node, variableName) {
return this.isVariableDeclaration(node, variableName) ||
this.isParameterDeclaration(node, variableName);
}
isVariableDeclaration(node, variableName) {
return node.getKind() === ts.SyntaxKind.VariableDeclaration &&
this.hasMatchingIdentifier(node, variableName);
}
isParameterDeclaration(node, variableName) {
return node.getKind() === ts.SyntaxKind.Parameter &&
this.hasMatchingIdentifier(node, variableName);
}
hasMatchingIdentifier(node, variableName) {
const identifier = node.getFirstDescendantByKind(ts.SyntaxKind.Identifier);
return identifier?.getText() === variableName;
}
isValidUsage(node, variableName, declarationIdentifier, declaration) {
return this.isUsageNode(node, variableName, declarationIdentifier) &&
this.shadowingDetector.isUsageInScope(node, declaration);
}
getDeclarationIdentifier(declaration) {
return declaration.getFirstDescendantByKind(ts.SyntaxKind.Identifier);
}
isUsageNode(node, variableName, declarationIdentifier) {
return node.getKind() === ts.SyntaxKind.Identifier &&
node.getText() === variableName &&
node !== declarationIdentifier;
}
}
exports.VariableNodeMatcher = VariableNodeMatcher;
//# sourceMappingURL=variable-node-matcher.js.map