archunit
Version:
ArchUnit TypeScript is an architecture testing library, to specify and assert architecture rules in your TypeScript app
89 lines • 3.21 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.sliceByFileSuffix = exports.sliceByRegex = exports.sliceByPattern = exports.identity = void 0;
const identity = () => {
return (edge) => ({
sourceLabel: edge.source,
targetLabel: edge.target,
});
};
exports.identity = identity;
const sliceByPattern = (pattern) => {
const index = pattern.indexOf('(**)');
if (index === -1) {
throw new Error("Could not find '(**)' inside slice pattern. It should contain exactly one occurance'(**)'");
}
const prefix = escapeForRegexp(pattern.substring(0, index));
const unescapedSuffix = pattern.substring(index + 4);
// WATCH-IMP: I replcae substr with substring for both above lines
if (unescapedSuffix.indexOf('(**)') !== -1) {
throw new Error("Found too many '(**)' inside slice pattern. It should contain exactly one occurance'(**)'");
}
const suffix = escapeForRegexp(unescapedSuffix);
const regexp = `^${prefix}([\\w]+)${suffix}.*$`;
return (0, exports.sliceByRegex)(new RegExp(regexp));
};
exports.sliceByPattern = sliceByPattern;
const escapeForRegexp = (input) => {
return input.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
};
const sliceByRegex = (regexp) => {
return (edge) => {
// Skip external edges
if (edge.external) {
return undefined;
}
const strippedSource = stripSlice(edge.source, regexp);
if (strippedSource === undefined) {
return undefined;
}
const strippedTarget = stripSlice(edge.target, regexp);
if (strippedTarget === undefined) {
return undefined;
}
// Skip self-references
if (strippedSource === strippedTarget) {
return undefined;
}
return {
sourceLabel: strippedSource,
targetLabel: strippedTarget,
};
};
};
exports.sliceByRegex = sliceByRegex;
const stripSlice = (relativeFileName, stripRegexp) => {
const strippedFileName = stripRegexp.exec(relativeFileName);
if (strippedFileName === null) {
return undefined;
}
// Return the first capture group if it exists
return strippedFileName[1];
};
const sliceByFileSuffix = (labeling) => {
return (edge) => {
// Find matching labels for source and target files
let sourceMatch;
let targetMatch;
for (const [suffix, label] of labeling.entries()) {
// Check if source file matches this suffix
if (edge.source.endsWith(`${suffix}.ts`)) {
sourceMatch = label;
}
// Check if target file matches this suffix
if (edge.target.endsWith(`${suffix}.ts`)) {
targetMatch = label;
}
// If we found both matches, return immediately
if (sourceMatch && targetMatch) {
return {
sourceLabel: sourceMatch,
targetLabel: targetMatch,
};
}
}
return undefined;
};
};
exports.sliceByFileSuffix = sliceByFileSuffix;
//# sourceMappingURL=slicing-projections.js.map
;