Skip to content

Commit

Permalink
refactor(enrich): converts to esm (#786)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij authored Apr 9, 2023
1 parent 1adefbe commit 892cfa5
Show file tree
Hide file tree
Showing 39 changed files with 200 additions and 214 deletions.
13 changes: 7 additions & 6 deletions src/enrich/add-validations.js → src/enrich/add-validations.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const validate = require("../validate");
import validate from "../validate/index.js";

function addDependencyViolations(pModule, pDependency, pRuleSet, pValidate) {
return {
Expand All @@ -8,24 +8,25 @@ function addDependencyViolations(pModule, pDependency, pRuleSet, pValidate) {
: { valid: true }),
};
}

/**
* Runs through all dependencies, validates them
* - when there's a transgression: adds it
* - when everything is hunky-dory: adds the dependency is valid
*
* @param {Partial<import("../../types/cruise-result").IModule>[]} pModules array of modules
* @param {import("../../types/rule-set").IFlattenedRuleSet} pRuleSet normalized & validated rule set
* @param {Partial<import("../../types/cruise-result.js").IModule>[]} pModules array of modules
* @param {import("../../types/rule-set.js").IFlattenedRuleSet} pRuleSet normalized & validated rule set
* @param {boolean} pValidate - whether or not to validate (typically you want to pass 'true' here)
* @return {import("../../types/cruise-result").IModule[]} the same array of modules, with for each
* @return {import("../../types/cruise-result.js").IModule[]} the same array of modules, with for each
* of them added whether or not it is
* valid and if not which rules were violated
*/
module.exports = function addValidations(pModules, pRuleSet, pValidate) {
export default function addValidations(pModules, pRuleSet, pValidate) {
return pModules.map((pModule) => ({
...pModule,
...(pValidate ? validate.module(pRuleSet, pModule) : { valid: true }),
dependencies: pModule.dependencies.map((pDependency) =>
addDependencyViolations(pModule, pDependency, pRuleSet, pValidate)
),
}));
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function addCircularityCheckToDependency(
* whether it's (part of a) circular (relationship) and returns the
* dependencies with that added.
*/
module.exports = function detectAndAddCycles(
export default function detectAndAddCycles(
pNodes,
pIndexedNodes,
{ pSourceAttribute, pDependencyName }
Expand All @@ -48,4 +48,4 @@ module.exports = function detectAndAddCycles(
)
),
}));
};
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { isDependent } = require("../module-utl");
import { isDependent } from "../module-utl.mjs";

module.exports = function getDependents(pModule, pModules) {
export default function getDependents(pModule, pModules) {
// perf between O(n) in an unconnected graph and O(n^2) in a fully connected one
return pModules
.filter(isDependent(pModule.source))
.map((pDependentModule) => pDependentModule.source);
};
}
10 changes: 0 additions & 10 deletions src/enrich/derive/dependents/index.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/enrich/derive/dependents/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import getDependents from "./get-dependents.mjs";

export default function addDependents(pModules) {
return pModules.map((pModule) => ({
...pModule,
dependents: getDependents(pModule, pModules),
}));
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint-disable security/detect-object-injection */
const path = require("node:path").posix;
const { calculateInstability, metricsAreCalculable } = require("../module-utl");
const detectCycles = require("../circular");
const IndexedModuleGraph = require("../../../graph-utl/indexed-module-graph");
const {
import path from "node:path/posix";
import { calculateInstability, metricsAreCalculable } from "../module-utl.mjs";
import detectCycles from "../circular.mjs";
import IndexedModuleGraph from "../../../graph-utl/indexed-module-graph.js";
import {
findFolderByName,
getAfferentCouplings,
getEfferentCouplings,
getParentFolders,
object2Array,
} = require("./utl");
} from "./utl.mjs";

function upsertCouplings(pAllDependents, pNewDependents) {
pNewDependents.forEach((pNewDependent) => {
Expand Down Expand Up @@ -114,7 +114,7 @@ function getSinks(pFolders) {
return lReturnValue;
}

module.exports = function aggregateToFolders(pModules) {
export default function aggregateToFolders(pModules) {
let lFolders = object2Array(
pModules.filter(metricsAreCalculable).reduce(aggregateToFolder, {})
)
Expand All @@ -126,4 +126,4 @@ module.exports = function aggregateToFolders(pModules) {
pSourceAttribute: "name",
pDependencyName: "name",
});
};
}
45 changes: 0 additions & 45 deletions src/enrich/derive/folders/index.js

This file was deleted.

42 changes: 42 additions & 0 deletions src/enrich/derive/folders/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import validate from "../../../validate/index.js";
import aggregateToFolders from "./aggregate-to-folders.mjs";

/**
* @param {import("../../../../types/dependency-cruiser.js").IFolder} pFolder
* @param {import('../../../../types/dependency-cruiser.js').IOptions} pOptions
* @returns
*/
function validateFolderDependency(pFolder, pOptions) {
return (pDependency) => ({
...pDependency,
...validate.folder(pOptions.ruleSet || {}, pFolder, pDependency),
});
}

function addFolderDependencyViolations(pOptions) {
return (pFolder) => ({
...pFolder,

dependencies: pFolder.dependencies.map(
validateFolderDependency(pFolder, pOptions)
),
});
}

/**
*
* @param {import('../../../../types/dependency-cruiser.js').IModule[]} pModules
* @param {import('../../../../types/dependency-cruiser.js').IOptions} pOptions
* @returns {any}
*/
export default function deriveFolderMetrics(pModules, pOptions) {
let lReturnValue = {};
if (pOptions.metrics) {
lReturnValue = {
folders: aggregateToFolders(pModules).map(
addFolderDependencyViolations(pOptions)
),
};
}
return lReturnValue;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable security/detect-object-injection */
const path = require("node:path").posix;
import path from "node:path/posix";

function findFolderByName(pAllFolders, pName) {
export function findFolderByName(pAllFolders, pName) {
return pAllFolders.find((pFolder) => pFolder.name === pName);
}

function getAfferentCouplings(pModule, pDirname) {
export function getAfferentCouplings(pModule, pDirname) {
return pModule.dependents.filter(
(pDependent) => !pDependent.startsWith(pDirname.concat(path.sep))
);
}

function getEfferentCouplings(pModule, pDirname) {
export function getEfferentCouplings(pModule, pDirname) {
return pModule.dependencies.filter(
(pDependency) => !pDependency.resolved.startsWith(pDirname.concat(path.sep))
);
Expand All @@ -22,7 +22,7 @@ function getEfferentCouplings(pModule, pDirname) {
* @param {string} pPath
* @returns string[]
*/
function getParentFolders(pPath) {
export function getParentFolders(pPath) {
let lFragments = pPath.split("/");
let lReturnValue = [];

Expand All @@ -33,17 +33,9 @@ function getParentFolders(pPath) {
return lReturnValue.reverse();
}

function object2Array(pObject) {
export function object2Array(pObject) {
return Object.keys(pObject).map((pKey) => ({
name: pKey,
...pObject[pKey],
}));
}

module.exports = {
findFolderByName,
getAfferentCouplings,
getEfferentCouplings,
getParentFolders,
object2Array,
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const IndexedModuleGraph = require("../../../graph-utl/indexed-module-graph");
const { calculateInstability, metricsAreCalculable } = require("../module-utl");
/* eslint-disable import/exports-last */
import IndexedModuleGraph from "../../../graph-utl/indexed-module-graph.js";
import { calculateInstability, metricsAreCalculable } from "../module-utl.mjs";

function addInstabilityMetric(pModule) {
export function addInstabilityMetric(pModule) {
return {
...pModule,
...(metricsAreCalculable(pModule)
Expand All @@ -25,16 +26,15 @@ function addInstabilityToDependency(pAllModules) {
});
}

function deNormalizeInstabilityMetricsToDependencies(pModule, _, pAllModules) {
export function deNormalizeInstabilityMetricsToDependencies(
pModule,
_,
pAllModules
) {
return {
...pModule,
dependencies: pModule.dependencies.map(
addInstabilityToDependency(pAllModules)
),
};
}

module.exports = {
addInstabilityMetric,
deNormalizeInstabilityMetricsToDependencies,
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const {
import {
addInstabilityMetric,
deNormalizeInstabilityMetricsToDependencies,
} = require("./get-module-metrics");
} from "./get-module-metrics.mjs";

module.exports = function deriveModulesMetrics(pModules, pOptions) {
export default function deriveModulesMetrics(pModules, pOptions) {
if (pOptions.metrics) {
return pModules
.map(addInstabilityMetric)
.map(deNormalizeInstabilityMetricsToDependencies);
}
return pModules;
};
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function isDependent(pResolvedName) {
export function isDependent(pResolvedName) {
return (pModule) =>
pModule.dependencies.some(
(pDependency) => pDependency.resolved === pResolvedName
);
}

function metricsAreCalculable(pModule) {
export function metricsAreCalculable(pModule) {
return (
!pModule.coreModule &&
!pModule.couldNotResolve &&
Expand All @@ -21,7 +21,10 @@ function metricsAreCalculable(pModule) {
* @param {number} pAfferentCouplingCount
* @returns number
*/
function calculateInstability(pEfferentCouplingCount, pAfferentCouplingCount) {
export function calculateInstability(
pEfferentCouplingCount,
pAfferentCouplingCount
) {
// when both afferentCouplings and efferentCouplings equal 0 instability will
// yield NaN. Judging Bob Martin's intention, a component with no outgoing
// dependencies is maximum stable (0)
Expand All @@ -30,9 +33,3 @@ function calculateInstability(pEfferentCouplingCount, pAfferentCouplingCount) {
(pEfferentCouplingCount + pAfferentCouplingCount) || 0
);
}

module.exports = {
isDependent,
metricsAreCalculable,
calculateInstability,
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const isOrphan = require("./is-orphan");
import isOrphan from "./is-orphan.mjs";

module.exports = function deriveOrphans(pModules) {
export default function deriveOrphans(pModules) {
return pModules.map((pModule) => ({
...pModule,
orphan: isOrphan(pModule, pModules),
}));
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { isDependent } = require("../module-utl");
import { isDependent } from "../module-utl.mjs";

module.exports = function isOrphan(pModule, pGraph) {
export default function isOrphan(pModule, pGraph) {
if (pModule.dependencies.length > 0) {
return false;
}
Expand All @@ -11,4 +11,4 @@ module.exports = function isOrphan(pModule, pGraph) {
}
// ... otherwise calculate them
return !pGraph.some(isDependent(pModule.source));
};
}
Loading

0 comments on commit 892cfa5

Please sign in to comment.