@samiyev/guardian
Version:
Research-backed code quality guardian for AI-assisted development. Detects hardcodes, secrets, circular deps, framework leaks, entity exposure, and 9 architecture violations. Enforces Clean Architecture/DDD principles. Works with GitHub Copilot, Cursor, W
100 lines • 2.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DuplicateValueTracker = void 0;
/**
* Tracks duplicate hardcoded values across files
*
* Helps identify values that are used in multiple places
* and should be extracted to a shared constant.
*/
class DuplicateValueTracker {
valueMap = new Map();
/**
* Adds a hardcoded value to tracking
*/
track(violation, filePath) {
const key = this.createKey(violation.value, violation.type);
const location = {
file: filePath,
line: violation.line,
context: violation.context,
};
const locations = this.valueMap.get(key);
if (!locations) {
this.valueMap.set(key, [location]);
}
else {
locations.push(location);
}
}
/**
* Gets all duplicate values (values used in 2+ places)
*/
getDuplicates() {
const duplicates = [];
for (const [key, locations] of this.valueMap.entries()) {
if (locations.length >= 2) {
const { value } = this.parseKey(key);
duplicates.push({
value,
locations,
count: locations.length,
});
}
}
return duplicates.sort((a, b) => b.count - a.count);
}
/**
* Gets duplicate locations for a specific value
*/
getDuplicateLocations(value, type) {
const key = this.createKey(value, type);
const locations = this.valueMap.get(key);
if (!locations || locations.length < 2) {
return null;
}
return locations;
}
/**
* Checks if a value is duplicated
*/
isDuplicate(value, type) {
const key = this.createKey(value, type);
const locations = this.valueMap.get(key);
return locations ? locations.length >= 2 : false;
}
/**
* Creates a unique key for a value
*/
createKey(value, type) {
return `${type}:${String(value)}`;
}
/**
* Parses a key back to value and type
*/
parseKey(key) {
const [type, ...valueParts] = key.split(":");
return { value: valueParts.join(":"), type };
}
/**
* Gets statistics about duplicates
*/
getStats() {
const totalValues = this.valueMap.size;
const duplicateValues = this.getDuplicates().length;
const duplicatePercentage = totalValues > 0 ? (duplicateValues / totalValues) * 100 : 0;
return {
totalValues,
duplicateValues,
duplicatePercentage,
};
}
/**
* Clears all tracked values
*/
clear() {
this.valueMap.clear();
}
}
exports.DuplicateValueTracker = DuplicateValueTracker;
//# sourceMappingURL=DuplicateValueTracker.js.map