archunit
Version:
ArchUnit TypeScript is an architecture testing library, to specify and assert architecture rules in your TypeScript app
105 lines • 3.69 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.projectToMetricResults = exports.projectToMetric = exports.CompositeFilter = exports.ClassFilter = void 0;
const common_1 = require("../../common");
/**
* Filter classes by class name using regex patterns
*/
class ClassFilter {
constructor(filter) {
this.filter = filter;
}
apply(classes, logger, options) {
const beforeCount = classes.length;
if (logger) {
logger.info(options?.logging, `Filter pattern: ${this.filter.regExp.source}`);
logger.info(options?.logging, `Filter target: ${this.filter.options.target}`);
logger.info(options?.logging, `Applying filter to ${classes.length} classes`);
}
const filtered = classes.filter((classInfo) => {
const matches = (0, common_1.matchesPatternClassInfo)(classInfo, this.filter, options);
return matches;
});
if (logger && beforeCount !== filtered.length) {
logger.info(options?.logging, ` Filter applied: ${beforeCount} -> ${filtered.length} classes (pattern: ${this.filter.regExp.source})`);
}
return filtered;
}
getFilter() {
return this.filter;
}
}
exports.ClassFilter = ClassFilter;
/**
* Combine multiple filters with AND logic
*/
class CompositeFilter {
constructor(filters) {
this.filters = filters;
}
apply(classes, logger, options) {
if (logger) {
logger.debug(options?.logging, `Applying ${this.filters.length} filter(s) to ${classes.length} classes`);
}
return this.filters.reduce((filteredClasses, filter, index) => {
if (logger) {
logger.debug(options?.logging, ` Applying filter ${index + 1}/${this.filters.length}`);
}
return filter.apply(filteredClasses, logger, options);
}, classes);
}
}
exports.CompositeFilter = CompositeFilter;
/**
* Creates a projection for a specific metric calculation
* @param metricCalculation Function that calculates the metric for a single class
*/
const projectToMetric = (metricCalculation) => {
return {
apply(classes) {
const result = {};
classes.forEach((classInfo) => {
result[classInfo.name] = metricCalculation(classInfo);
});
return result;
},
};
};
exports.projectToMetric = projectToMetric;
/**
* Projects class information into metric results for violation checking
*/
function projectToMetricResults(classes, metric, threshold, comparison) {
return classes.map((classInfo) => {
const metricValue = metric.calculate(classInfo);
let isViolation = false;
switch (comparison) {
case 'above':
isViolation = metricValue <= threshold;
break;
case 'below':
isViolation = metricValue >= threshold;
break;
case 'equal':
isViolation = metricValue !== threshold;
break;
case 'above-equal':
isViolation = metricValue < threshold;
break;
case 'below-equal':
isViolation = metricValue > threshold;
break;
}
return {
className: classInfo.name,
filePath: classInfo.filePath,
metricName: metric.name,
metricValue,
threshold,
comparison,
isViolation,
};
});
}
exports.projectToMetricResults = projectToMetricResults;
//# sourceMappingURL=project-metrics.js.map
;