Skip to content

Commit

Permalink
refactor(graph-utl): converts to esm (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij authored Apr 9, 2023
1 parent 8c78331 commit 13ae375
Show file tree
Hide file tree
Showing 38 changed files with 149 additions and 170 deletions.
4 changes: 1 addition & 3 deletions src/cache/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { createHash } from "node:crypto";
import { readFileSync } from "node:fs";
import path from "node:path";
import memoize from "lodash/memoize.js";
import matchFacade from "../graph-utl/match-facade.js";

const { filenameMatchesPattern } = matchFacade;
import { filenameMatchesPattern } from "../graph-utl/match-facade.mjs";

/**
* @param {string} pString
Expand Down
2 changes: 1 addition & 1 deletion src/enrich/derive/folders/aggregate-to-folders.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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 IndexedModuleGraph from "../../../graph-utl/indexed-module-graph.mjs";
import {
findFolderByName,
getAfferentCouplings,
Expand Down
2 changes: 1 addition & 1 deletion src/enrich/derive/metrics/get-module-metrics.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable import/exports-last */
import IndexedModuleGraph from "../../../graph-utl/indexed-module-graph.js";
import IndexedModuleGraph from "../../../graph-utl/indexed-module-graph.mjs";
import { calculateInstability, metricsAreCalculable } from "../module-utl.mjs";

export function addInstabilityMetric(pModule) {
Expand Down
2 changes: 1 addition & 1 deletion src/enrich/derive/reachable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import cloneDeep from "lodash/cloneDeep.js";
import has from "lodash/has.js";
import matchers from "../../validate/matchers.mjs";
import regexUtil from "../../utl/regex-util.js";
import IndexedModuleGraph from "../../graph-utl/indexed-module-graph.js";
import IndexedModuleGraph from "../../graph-utl/indexed-module-graph.mjs";

const { extractGroups } = regexUtil;

Expand Down
4 changes: 2 additions & 2 deletions src/enrich/enrich-modules.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bus from "../utl/bus.js";
import busLogLevels from "../utl/bus-log-levels.js";
import addFocus from "../graph-utl/add-focus.js";
import IndexedModuleGraph from "../graph-utl/indexed-module-graph.js";
import addFocus from "../graph-utl/add-focus.mjs";
import IndexedModuleGraph from "../graph-utl/indexed-module-graph.mjs";
import deriveCycles from "./derive/circular.mjs";
import deriveOrphans from "./derive/orphan/index.mjs";
import addDependents from "./derive/dependents/index.mjs";
Expand Down
2 changes: 1 addition & 1 deletion src/enrich/summarize/index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import compare from "../../graph-utl/compare.js";
import compare from "../../graph-utl/compare.mjs";
import addRuleSetUsed from "./add-rule-set-used.mjs";
import summarizeModules from "./summarize-modules.mjs";
import summarizeFolders from "./summarize-folders.mjs";
Expand Down
4 changes: 1 addition & 3 deletions src/enrich/summarize/summarize-folders.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import has from "lodash/has.js";
import graphUtlRuleSet from "../../graph-utl/rule-set.js";

const { findRuleByName } = graphUtlRuleSet;
import { findRuleByName } from "../../graph-utl/rule-set.mjs";

function classifyViolation(pRule, pRuleSet) {
const lRule = findRuleByName(pRuleSet, pRule.name);
Expand Down
6 changes: 2 additions & 4 deletions src/enrich/summarize/summarize-modules.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import flattenDeep from "lodash/flattenDeep.js";
import has from "lodash/has.js";
import uniqWith from "lodash/uniqWith.js";
import graphUtlRuleSet from "../../graph-utl/rule-set.js";
import compare from "../../graph-utl/compare.js";
import { findRuleByName } from "../../graph-utl/rule-set.mjs";
import compare from "../../graph-utl/compare.mjs";
import isSameViolation from "./is-same-violation.mjs";

const { findRuleByName } = graphUtlRuleSet;

function cutNonTransgressions(pModule) {
return {
...pModule,
Expand Down
2 changes: 1 addition & 1 deletion src/extract/gather-initial-sources.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readdirSync, statSync } from "node:fs";
import { join } from "node:path";
import { glob } from "glob";
import get from "lodash/get.js";
import { filenameMatchesPattern } from "../graph-utl/match-facade.js";
import { filenameMatchesPattern } from "../graph-utl/match-facade.mjs";
import getExtension from "../utl/get-extension.js";
import pathToPosix from "../utl/path-to-posix.js";
import { scannableExtensions } from "./transpile/meta.mjs";
Expand Down
2 changes: 1 addition & 1 deletion src/extract/transpile/vue-template-wrap.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { EOL } = require("os");
const { EOL } = require("node:os");
const isEmpty = require("lodash/isEmpty");
const tryRequire = require("semver-try-require");
const meta = require("../../meta.js");
Expand Down
15 changes: 8 additions & 7 deletions src/graph-utl/add-focus.js → src/graph-utl/add-focus.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const has = require("lodash/has");
const IndexedModuleGraph = require("./indexed-module-graph");
const { moduleMatchesFilter } = require("./match-facade");
import has from "lodash/has.js";
import IndexedModuleGraph from "./indexed-module-graph.mjs";
import { moduleMatchesFilter } from "./match-facade.mjs";

function getFocusModules(pModules, pFilter) {
return pModules.filter((pModule) => moduleMatchesFilter(pModule, pFilter));
Expand All @@ -21,13 +21,14 @@ function scrub(pModuleNamesSet) {
),
});
}

/**
*
* @param {import("../..").IModule[]} pModules
* @param {import("../../types/strict-filter-types").IStrictFocusType} pFilter
* @param {import("../../types/dependency-cruiser.js").IModule[]} pModules
* @param {import("../../types/strict-filter-types.js").IStrictFocusType} pFilter
* @returns
*/
module.exports = function addFocus(pModules, pFilter) {
export default function addFocus(pModules, pFilter) {
if (has(pFilter, "path")) {
const lDepth = typeof pFilter.depth === "undefined" ? 1 : pFilter.depth;
const lFocusedModuleNames = getFocusModules(pModules, pFilter).map(
Expand Down Expand Up @@ -55,4 +56,4 @@ module.exports = function addFocus(pModules, pFilter) {
.map(tag(lFocusedModuleNamesSet));
}
return pModules;
};
}
10 changes: 5 additions & 5 deletions src/graph-utl/compare.js → src/graph-utl/compare.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ function severity2number(pSeverity) {
return lSeverity2Number[pSeverity] || -1;
}

function severities(pFirstSeverity, pSecondSeverity) {
export function severities(pFirstSeverity, pSecondSeverity) {
return Math.sign(
severity2number(pFirstSeverity) - severity2number(pSecondSeverity)
);
}

function violations(pFirstViolation, pSecondViolation) {
export function violations(pFirstViolation, pSecondViolation) {
return (
severities(pFirstViolation.rule.severity, pSecondViolation.rule.severity) ||
pFirstViolation.rule.name.localeCompare(pSecondViolation.rule.name) ||
Expand All @@ -25,18 +25,18 @@ function violations(pFirstViolation, pSecondViolation) {
);
}

function rules(pLeftRule, pRightRule) {
export function rules(pLeftRule, pRightRule) {
return (
severities(pLeftRule.severity, pRightRule.severity) ||
pLeftRule.name.localeCompare(pRightRule.name)
);
}

function modules(pLeftModule, pRightModule) {
export function modules(pLeftModule, pRightModule) {
return pLeftModule.source > pRightModule.source ? 1 : -1;
}

module.exports = {
export default {
modules,
rules,
severities,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const clone = require("lodash/clone");
const _reject = require("lodash/reject");
const uniq = require("lodash/uniq");
const compare = require("./compare");
import clone from "lodash/clone.js";
import _reject from "lodash/reject.js";
import uniq from "lodash/uniq.js";
import compare from "./compare.mjs";

function mergeDependency(pLeftDependency, pRightDependency) {
return {
Expand Down Expand Up @@ -47,9 +47,9 @@ function consolidateDependencies(pDependencies) {
return lReturnValue;
}

module.exports = function consolidateModuleDependencies(pModule) {
export default function consolidateModuleDependencies(pModule) {
return {
...pModule,
dependencies: consolidateDependencies(pModule.dependencies),
};
};
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const clone = require("lodash/clone");
const _reject = require("lodash/reject");
const uniqBy = require("lodash/uniqBy");
const compare = require("./compare");
import clone from "lodash/clone.js";
import _reject from "lodash/reject.js";
import uniqBy from "lodash/uniqBy.js";
import compare from "./compare.mjs";

function mergeModule(pLeftModule, pRightModule) {
return {
Expand Down Expand Up @@ -34,7 +34,7 @@ function mergeModules(pSourceString, pModules) {
);
}

module.exports = function consolidateModules(pModules) {
export default function consolidateModules(pModules) {
let lModules = clone(pModules);
let lReturnValue = [];

Expand All @@ -43,4 +43,4 @@ module.exports = function consolidateModules(pModules) {
lModules = _reject(lModules, { source: lModules[0].source });
}
return lReturnValue;
};
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
const path = require("node:path");
const consolidateModules = require("./consolidate-modules");
const consolidateModuleDependencies = require("./consolidate-module-dependencies");
import { dirname } from "node:path";
import consolidateModules from "./consolidate-modules.mjs";
import consolidateModuleDependencies from "./consolidate-module-dependencies.mjs";

function squashDependencyToDirectory(pDependency) {
return {
...pDependency,
resolved: path.dirname(pDependency.resolved),
resolved: dirname(pDependency.resolved),
};
}
function squashModuleToDirectory(pModule) {
return {
...pModule,
source: path.dirname(pModule.source),
source: dirname(pModule.source),
consolidated: true,
dependencies: pModule.dependencies.map(squashDependencyToDirectory),
};
}

module.exports = function consolidateToFolder(pModules) {
export default function consolidateToFolder(pModules) {
return consolidateModules(pModules.map(squashModuleToDirectory)).map(
consolidateModuleDependencies
);
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const consolidateModules = require("./consolidate-modules");
const consolidateModuleDependencies = require("./consolidate-module-dependencies");
import consolidateModules from "./consolidate-modules.mjs";
import consolidateModuleDependencies from "./consolidate-module-dependencies.mjs";

function squashDependencyToPattern(pCollapsePattern) {
return (pDependency) => {
Expand Down Expand Up @@ -44,8 +44,8 @@ function squashModuleToPattern(pCollapsePattern) {
};
}

module.exports = function consolidateToPattern(pModules, pCollapsePattern) {
export default function consolidateToPattern(pModules, pCollapsePattern) {
return consolidateModules(
pModules.map(squashModuleToPattern(pCollapsePattern))
).map(consolidateModuleDependencies);
};
}
42 changes: 17 additions & 25 deletions src/graph-utl/filter-bank.js → src/graph-utl/filter-bank.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
const clone = require("lodash/clone");
const addFocus = require("./add-focus");
const IndexedModuleGraph = require("./indexed-module-graph");
const matchFacade = require("./match-facade");
import clone from "lodash/clone.js";
import addFocus from "./add-focus.mjs";
import IndexedModuleGraph from "./indexed-module-graph.mjs";
import {
moduleMatchesFilter,
dependencyMatchesFilter,
} from "./match-facade.mjs";

function includeOnly(pModules, pIncludeFilter) {
return pIncludeFilter.path
? pModules
.filter((pModule) =>
matchFacade.moduleMatchesFilter(pModule, pIncludeFilter)
)
.filter((pModule) => moduleMatchesFilter(pModule, pIncludeFilter))
.map((pModule) => ({
...pModule,
dependencies: pModule.dependencies.filter((pDependency) =>
matchFacade.dependencyMatchesFilter(pDependency, pIncludeFilter)
dependencyMatchesFilter(pDependency, pIncludeFilter)
),
}))
: pModules;
Expand All @@ -21,29 +22,25 @@ function includeOnly(pModules, pIncludeFilter) {
function exclude(pModules, pExcludeFilter) {
return pExcludeFilter.path
? pModules
.filter(
(pModule) => !matchFacade.moduleMatchesFilter(pModule, pExcludeFilter)
)
.filter((pModule) => !moduleMatchesFilter(pModule, pExcludeFilter))
.map((pModule) => ({
...pModule,
dependencies: pModule.dependencies.filter(
(pDependency) =>
!matchFacade.dependencyMatchesFilter(pDependency, pExcludeFilter)
!dependencyMatchesFilter(pDependency, pExcludeFilter)
),
}))
: pModules;
}
/**
*
* @param {import("../../types/cruise-result").IModule[]} pModules
* @param {import("../../types/strict-filter-types").IStrictReachesType} pReachesFilter
* @returns {import("../../types/cruise-result").IModule[]}
* @param {import("../../types/cruise-result.js").IModule[]} pModules
* @param {import("../../types/strict-filter-types.js").IStrictReachesType} pReachesFilter
* @returns {import("../../types/cruise-result.js").IModule[]}
*/
function filterReaches(pModules, pReachesFilter) {
const lModuleNamesToReach = pModules
.filter((pModule) =>
matchFacade.moduleMatchesFilter(pModule, pReachesFilter)
)
.filter((pModule) => moduleMatchesFilter(pModule, pReachesFilter))
.map(({ source }) => source);

/** @type {Array<string>} */
Expand All @@ -69,15 +66,12 @@ function filterReaches(pModules, pReachesFilter) {
function tagHighlight(pModules, pHighlightFilter) {
return pModules.map((pModule) => ({
...pModule,
matchesHighlight: matchFacade.moduleMatchesFilter(
pModule,
pHighlightFilter
),
matchesHighlight: moduleMatchesFilter(pModule, pHighlightFilter),
}));
}

// eslint-disable-next-line complexity
function applyFilters(pModules, pFilters) {
export function applyFilters(pModules, pFilters) {
if (pFilters) {
let lReturnValue = clone(pModules);

Expand All @@ -100,5 +94,3 @@ function applyFilters(pModules, pFilters) {
}
return pModules;
}

module.exports = { applyFilters };
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable security/detect-object-injection */
module.exports = class IndexedModuleGraph {
export default class IndexedModuleGraph {
init(pModules, pIndexAttribute) {
this.indexedGraph = new Map(
pModules.map((pModule) => [pModule[pIndexAttribute], pModule])
Expand Down Expand Up @@ -176,4 +176,4 @@ module.exports = class IndexedModuleGraph {
return pAll;
}, []);
}
};
}
17 changes: 0 additions & 17 deletions src/graph-utl/match-facade.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/graph-utl/match-facade.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function filenameMatchesPattern(pFullPathToFile, pPattern) {
return RegExp(pPattern, "g").test(pFullPathToFile);
}

export function moduleMatchesFilter(pModule, pFilter) {
return filenameMatchesPattern(pModule.source, pFilter.path);
}

export function dependencyMatchesFilter(pDependency, pFilter) {
return filenameMatchesPattern(pDependency.resolved, pFilter.path);
}
Loading

0 comments on commit 13ae375

Please sign in to comment.