debt-collector
Version:
a nodejs tool to identify, track and mesure technical debt
33 lines (32 loc) • 1.29 kB
JavaScript
import { minimatch } from 'minimatch';
const doesMatchPath = ({ include = [], exclude = [] }, file) => {
if (include.length === 0 && exclude.length === 0) {
return true;
}
const cleanPath = (str) => str.replace('./', '');
const isIncluded = include.some((glob) => minimatch(cleanPath(file), cleanPath(glob)));
const isExcluded = exclude.some((glob) => minimatch(cleanPath(file), cleanPath(glob)));
return isIncluded && !isExcluded;
};
export const getRulesForFile = (options, filePath) => {
const rulesForFile = {};
if (options.fileRules) {
rulesForFile.fileRules = options.fileRules.filter((rule) => doesMatchPath(rule, filePath));
}
return rulesForFile;
};
const filterRulesByTagAndId = (rules, ruleId, tags) => rules
.filter((rule) => (ruleId ? ruleId === rule.id : true))
.filter((rule) => tags && tags.length > 0
? tags.some((tag) => rule.tags?.includes(tag))
: true);
export const filtersRulesFromOptions = (options, ruleId = null, tags = null) => {
let { fileRules } = options;
const cleanTag = tags && tags.filter((tag) => !!tag);
if (ruleId || cleanTag?.length) {
fileRules = filterRulesByTagAndId(options.fileRules ?? [], ruleId, cleanTag);
}
return {
fileRules,
};
};