dependency-cruiser
Version:
Validate and visualize dependencies. With your rules. JavaScript, TypeScript, CoffeeScript. ES6, CommonJS, AMD.
52 lines (47 loc) • 1.05 kB
JavaScript
/* eslint-disable security/detect-object-injection */
function addCircularityCheckToDependency(
pIndexedGraph,
pFrom,
pToDep,
pDependencyName
) {
let lReturnValue = {
...pToDep,
circular: false,
};
const lCycle = pIndexedGraph.getCycle(
pFrom,
pToDep[pDependencyName],
pDependencyName
);
if (lCycle.length > 0) {
lReturnValue = {
...lReturnValue,
circular: true,
cycle: lCycle,
};
}
return lReturnValue;
}
/**
* Runs through all dependencies of all pNodes, for each of them determines
* whether it's (part of a) circular (relationship) and returns the
* dependencies with that added.
*/
export default function detectAndAddCycles(
pNodes,
pIndexedNodes,
{ pSourceAttribute, pDependencyName }
) {
return pNodes.map((pModule) => ({
...pModule,
dependencies: pModule.dependencies.map((pToDep) =>
addCircularityCheckToDependency(
pIndexedNodes,
pModule[pSourceAttribute],
pToDep,
pDependencyName
)
),
}));
}