refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
109 lines • 4.54 kB
JavaScript
"use strict";
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.ShadowingDetector = void 0;
const ts = __importStar(require("typescript"));
const typescript_scope_analyzer_1 = require("./typescript-scope-analyzer");
class ShadowingDetector {
constructor() {
this.scopeAnalyzer = new typescript_scope_analyzer_1.TypeScriptScopeAnalyzer();
}
isUsageInScope(usage, declaration) {
const declarationScope = this.scopeAnalyzer.getScope(declaration);
const usageScope = this.scopeAnalyzer.getScope(usage);
if (!this.scopeAnalyzer.isScopeContainedIn(usageScope, declarationScope)) {
return false;
}
return !this.isShadowedByDeclaration(usage, declaration);
}
isShadowedByDeclaration(usage, declaration) {
const variableName = this.getVariableName(declaration);
if (!variableName)
return false;
const scopes = this.getScopes(usage, declaration);
return this.checkForShadowing(scopes, variableName, declaration);
}
checkForShadowing(scopes, variableName, declaration) {
if (scopes.usage === scopes.declaration) {
return false;
}
return this.findShadowingInScopeChain(scopes.usage, scopes.declaration, variableName, declaration);
}
getScopes(usage, declaration) {
return {
usage: this.scopeAnalyzer.getScope(usage),
declaration: this.scopeAnalyzer.getScope(declaration)
};
}
findShadowingInScopeChain(usageScope, declarationScope, variableName, declaration) {
let current = usageScope;
while (current && current !== declarationScope) {
if (this.hasShadowingDeclaration(current, variableName, declaration)) {
return true;
}
current = this.scopeAnalyzer.getParentScope(current);
}
return false;
}
hasShadowingDeclaration(scope, variableName, originalDeclaration) {
let hasShadowing = false;
scope.forEachDescendant((child) => {
if (this.isShadowingDeclaration(child, variableName, originalDeclaration, scope)) {
hasShadowing = true;
}
});
return hasShadowing;
}
isShadowingDeclaration(child, variableName, originalDeclaration, scope) {
return this.isAnyDeclaration(child) &&
this.hasMatchingIdentifier(child, variableName) &&
child !== originalDeclaration &&
this.scopeAnalyzer.getScope(child) === scope;
}
getVariableName(declaration) {
const identifier = declaration.getFirstDescendantByKind(ts.SyntaxKind.Identifier);
return identifier?.getText();
}
isAnyDeclaration(node) {
return node.getKind() === ts.SyntaxKind.VariableDeclaration ||
node.getKind() === ts.SyntaxKind.Parameter;
}
hasMatchingIdentifier(node, variableName) {
const identifier = node.getFirstDescendantByKind(ts.SyntaxKind.Identifier);
return identifier?.getText() === variableName;
}
}
exports.ShadowingDetector = ShadowingDetector;
//# sourceMappingURL=shadowing-detector.js.map