UNPKG

archunit

Version:

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

122 lines 4.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RegexFactory = void 0; exports.getPatternString = getPatternString; const minimatch_1 = require("minimatch"); /** * Helper function to extract readable pattern strings from regex * This function formats regex patterns for display purposes by removing excessive escaping */ function getPatternString(pattern) { // For display purposes, return the original regex source without double escaping const source = pattern.source; // Remove excessive escaping for common cases const result = source.replace(/\\\\(.)/g, '\\$1'); return result; } class RegexFactory { static escapeRegex(str) { return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); //.replace('/', '\\/'); } static patternToRegExp(pattern) { if (typeof pattern === 'string') { return this.globToRegExp(pattern); } return pattern; } static toPatternArray(patterns) { if (patterns === undefined) { return []; } return Array.isArray(patterns) ? patterns : [patterns]; } static isTargetedExclusion(exclusion) { return (typeof exclusion === 'object' && exclusion !== null && !Array.isArray(exclusion) && !(exclusion instanceof RegExp)); } static createSimpleFilter(pattern, target) { return { regExp: this.patternToRegExp(pattern), options: { target, }, }; } static createFilter(pattern, target, options) { const exclusions = this.createExclusionFilters(target, options?.except); const filter = this.createSimpleFilter(pattern, target); if (exclusions.length > 0) { filter.exclusions = exclusions; } return filter; } static createExclusionFilters(parentTarget, exclusion) { if (exclusion === undefined) { return []; } if (this.isTargetedExclusion(exclusion)) { return [ ...this.toPatternArray(exclusion.inPath).map((pattern) => this.createSimpleFilter(pattern, 'path')), ...this.toPatternArray(exclusion.inFolder).map((pattern) => this.createSimpleFilter(pattern, 'path-no-filename')), ...this.toPatternArray(exclusion.withName).map((pattern) => this.createSimpleFilter(pattern, 'filename')), ...this.toPatternArray(exclusion.forClassesMatching).map((pattern) => this.createSimpleFilter(pattern, 'classname')), ]; } const targets = this.getDefaultExclusionTargets(parentTarget); return this.toPatternArray(exclusion).flatMap((pattern) => targets.map((target) => this.createSimpleFilter(pattern, target))); } static getDefaultExclusionTargets(parentTarget) { switch (parentTarget) { case 'filename': return ['filename']; case 'path': return ['path', 'filename']; case 'path-no-filename': return ['path', 'path-no-filename', 'filename']; case 'classname': return ['classname']; default: return [parentTarget]; } } static fileNameMatcher(name, options) { return this.createFilter(name, 'filename', options); } static classNameMatcher(name, options) { return this.createFilter(name, 'classname', options); } static folderMatcher(folder, options) { return this.createFilter(folder, 'path-no-filename', options); } static pathMatcher(path, options) { return this.createFilter(path, 'path', options); } /** * Creates a filter for exact file path matching * @param filePath Exact file path to match */ static exactFileMatcher(filePath) { const escapedPath = this.escapeRegex(filePath.replace(/\\/g, '/')); const regExp = new RegExp(`^${escapedPath}$`); return { regExp, options: { target: 'path', }, }; } } exports.RegexFactory = RegexFactory; //private static containsGlobSyntax(inp: string): boolean { // return inp.includes('*') || inp.includes('?'); //} RegexFactory.globToRegExp = (pattern) => { const ret = minimatch_1.minimatch.makeRe(pattern); if (typeof ret === 'boolean') { throw new Error('invalid pattern'); } return ret; }; //# sourceMappingURL=regex-factory.js.map