UNPKG

archunit

Version:

ArchUnit TypeScript is an architecture testing library, to specify and assert architecture rules in your TypeScript app

91 lines 3.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.matchingAllPatterns_OLD = void 0; exports.extractFilename = extractFilename; exports.matchesPattern = matchesPattern; exports.matchesPatternClassInfo = matchesPatternClassInfo; exports.matchesAllPatterns = matchesAllPatterns; exports.matchesAnyPattern = matchesAnyPattern; const util_1 = require("./util"); /** * Extract filename from a file path */ function extractFilename(filePath) { const normalized = filePath.replace(/\\/g, '/'); const parts = normalized.split('/'); return parts[parts.length - 1]; } function normalizePath(inp) { return inp.replace(/\\/g, '/'); } function pathWithoutFilename(inp) { const normalized = inp.replace(/\\/g, '/'); const parts = normalized.split('/'); parts.pop(); return parts.join('/'); } function getTargetString(filePath, target, classInfo) { switch (target) { case 'filename': return extractFilename(filePath); case 'path': return normalizePath(filePath); case 'path-no-filename': return pathWithoutFilename(filePath); case 'classname': return classInfo ? classInfo.name : normalizePath(filePath); default: return normalizePath(filePath); } } function regexMatches(regExp, target) { regExp.lastIndex = 0; return regExp.test(target); } function matchesFilter(filePath, filter, options, classInfo) { const targetString = getTargetString(filePath, filter.options.target, classInfo); const matches = regexMatches(filter.regExp, targetString); const excluded = matches && filter.exclusions?.some((exclusion) => matchesFilter(filePath, exclusion, options, classInfo)); util_1.sharedLogger.info(options?.logging, `Testing file: ${filePath}`); util_1.sharedLogger.info(options?.logging, ` Target string (${filter.options.target}): "${targetString}"`); util_1.sharedLogger.info(options?.logging, ` Pattern: ${filter.regExp.source}`); util_1.sharedLogger.info(options?.logging, ` Matches: ${matches}`); util_1.sharedLogger.info(options?.logging, ` Excluded: ${Boolean(excluded)}`); return matches && !excluded; } function matchesPattern(file, filter, options) { const filePath = typeof file === 'string' ? file : file.label; return matchesFilter(filePath, filter, options); } function matchesPatternClassInfo(classInfo, filter, options) { return matchesFilter(classInfo.filePath, filter, options, classInfo); } /** * Enhanced pattern matching for multiple patterns (all must match) * * If a pattern is a string, glob logic is handled automatically. Do not handle glob logic yourself. */ function matchesAllPatterns(file, filters) { return filters.every((filter) => matchesPattern(file, filter)); } /** * Enhanced pattern matching for multiple patterns (at least one must match) */ function matchesAnyPattern(file, filters) { return filters.some((filter) => matchesPattern(file, filter)); } /** * OLD */ const matchingAllPatterns_OLD = (input, patterns) => { return patterns.every((pattern) => { if (typeof pattern === 'string') { const regex = new RegExp(pattern); return regex.test(input); } return pattern.test(input); }); }; exports.matchingAllPatterns_OLD = matchingAllPatterns_OLD; //# sourceMappingURL=pattern-matching.js.map