Skip to content

Commit

Permalink
refactor: uses explicit exports over default where possible (#956)
Browse files Browse the repository at this point in the history
## Description

- uses explicit exports over default exports where possible

## Motivation and Context

These were leftovers from the cjs -> mjs conversion that added a level
of indirection we don't need.

## How Has This Been Tested?

- [x] green ci
- [x] updated unit tests where applicable

## Types of changes

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] Documentation only change
- [x] Refactor (non-breaking change which fixes an issue without
changing functionality)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
  • Loading branch information
sverweij authored Sep 20, 2024
1 parent 6d1b8bb commit 4314428
Show file tree
Hide file tree
Showing 45 changed files with 555 additions and 629 deletions.
6 changes: 3 additions & 3 deletions src/enrich/add-validations.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import validate from "#validate/index.mjs";
import { validateDependency, validateModule } from "#validate/index.mjs";

function addDependencyViolations(pModule, pDependency, pRuleSet, pValidate) {
return {
...pDependency,
...(pValidate
? validate.dependency(pRuleSet, pModule, pDependency)
? validateDependency(pRuleSet, pModule, pDependency)
: { valid: true }),
};
}
Expand All @@ -24,7 +24,7 @@ function addDependencyViolations(pModule, pDependency, pRuleSet, pValidate) {
export default function addValidations(pModules, pRuleSet, pValidate) {
return pModules.map((pModule) => ({
...pModule,
...(pValidate ? validate.module(pRuleSet, pModule) : { valid: true }),
...(pValidate ? validateModule(pRuleSet, pModule) : { valid: true }),
dependencies: pModule.dependencies.map((pDependency) =>
addDependencyViolations(pModule, pDependency, pRuleSet, pValidate),
),
Expand Down
4 changes: 2 additions & 2 deletions src/enrich/derive/folders/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import aggregateToFolders from "./aggregate-to-folders.mjs";
import validate from "#validate/index.mjs";
import { validateFolder } from "#validate/index.mjs";

/**
* @param {import("../../../../types/dependency-cruiser.js").IFolder} pFolder
Expand All @@ -9,7 +9,7 @@ import validate from "#validate/index.mjs";
function validateFolderDependency(pFolder, pOptions) {
return (pDependency) => ({
...pDependency,
...validate.folder(pOptions.ruleSet || {}, pFolder, pDependency),
...validateFolder(pOptions.ruleSet || {}, pFolder, pDependency),
});
}

Expand Down
9 changes: 6 additions & 3 deletions src/enrich/derive/reachable.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* eslint-disable security/detect-object-injection, no-inline-comments */
import matchers from "#validate/matchers.mjs";
import {
matchToModulePath,
matchToModulePathNot,
} from "#validate/matchers.mjs";
import IndexedModuleGraph from "#graph-utl/indexed-module-graph.mjs";
import { extractGroups } from "#utl/regex-util.mjs";

Expand All @@ -25,8 +28,8 @@ function isModuleInRuleTo(pRule, pModuleTo, pModuleFrom) {
: [];

return (
matchers.toModulePath(pRule, pModuleTo, lGroups) &&
matchers.toModulePathNot(pRule, pModuleTo, lGroups)
matchToModulePath(pRule, pModuleTo, lGroups) &&
matchToModulePathNot(pRule, pModuleTo, lGroups)
);
}

Expand Down
4 changes: 0 additions & 4 deletions src/graph-utl/compare.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,3 @@ export function compareRules(pLeftRule, pRightRule) {
export function compareModules(pLeftModule, pRightModule) {
return pLeftModule.source > pRightModule.source ? 1 : -1;
}

export default {
violations: compareViolations,
};
4 changes: 2 additions & 2 deletions src/main/options/assert-validity.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable security/detect-object-injection */
import safeRegex from "safe-regex";
import report from "#report/index.mjs";
import { getAvailableReporters } from "#report/index.mjs";

const MODULE_SYSTEM_LIST_RE = /^(?:(?:cjs|amd|es6|tsd)(?:,|$)){1,4}/gi;
const VALID_DEPTH_RE = /^\d{1,2}$/g;
Expand Down Expand Up @@ -53,7 +53,7 @@ function assertRegExpSafety(pPattern) {
function assertOutputTypeValid(pOutputType) {
if (
Boolean(pOutputType) &&
!report.getAvailableReporters().includes(pOutputType) &&
!getAvailableReporters().includes(pOutputType) &&
!pOutputType.startsWith("plugin:")
) {
throw new Error(`'${pOutputType}' is not a valid output type.\n`);
Expand Down
4 changes: 2 additions & 2 deletions src/main/report-wrap.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import report from "#report/index.mjs";
import { getReporter } from "#report/index.mjs";
import summarize from "#enrich/summarize/index.mjs";
import { applyFilters } from "#graph-utl/filter-bank.mjs";
import consolidateToPattern from "#graph-utl/consolidate-to-pattern.mjs";
Expand Down Expand Up @@ -49,7 +49,7 @@ function getReporterSection(pOutputType) {
* @returns {import("../../types/dependency-cruiser.js").IReporterOutput}
*/
export default async function reportWrap(pResult, pFormatOptions) {
const lReportFunction = await report.getReporter(pFormatOptions.outputType);
const lReportFunction = await getReporter(pFormatOptions.outputType);
const lReportOptions =
pResult.summary.optionsUsed?.reporterOptions?.[
getReporterSection(pFormatOptions.outputType)
Expand Down
18 changes: 6 additions & 12 deletions src/report/dot/index.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable prefer-template */
import theming from "./theming.mjs";
import moduleUtl from "./module-utl.mjs";
import { normalizeTheme } from "./theming.mjs";
import { attributizeObject } from "./module-utl.mjs";
import prepareFolderLevel from "./prepare-folder-level.mjs";
import prepareCustomLevel from "./prepare-custom-level.mjs";
import prepareFlatLevel from "./prepare-flat-level.mjs";
Expand All @@ -25,21 +25,15 @@ const GRANULARITY2REPORTER_OPTIONS = new Map([
]);

function buildGraphAttributes(pGraph) {
return Boolean(pGraph)
? ` ${moduleUtl.attributizeObject(pGraph || {})}`
: "";
return Boolean(pGraph) ? ` ${attributizeObject(pGraph || {})}` : "";
}

function buildNodeAttributes(pNode) {
return Boolean(pNode)
? ` node [${moduleUtl.attributizeObject(pNode || {})}]`
: "";
return Boolean(pNode) ? ` node [${attributizeObject(pNode || {})}]` : "";
}

function buildEdgeAttributes(pEdge) {
return Boolean(pEdge)
? ` edge [${moduleUtl.attributizeObject(pEdge || {})}]`
: "";
return Boolean(pEdge) ? ` edge [${attributizeObject(pEdge || {})}]` : "";
}

function buildGeneralAttributes(pTheme) {
Expand Down Expand Up @@ -120,7 +114,7 @@ function report(
pGranularity,
{ theme, collapsePattern, filters, showMetrics },
) {
const lTheme = theming.normalizeTheme(theme);
const lTheme = normalizeTheme(theme);
const lResults = filters
? {
...pResults,
Expand Down
40 changes: 5 additions & 35 deletions src/report/dot/module-utl.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { basename, sep, dirname } from "node:path/posix";
import { formatPercentage, getURLForModule } from "../utl/index.mjs";
import theming from "./theming.mjs";

function attributizeObject(pObject) {
export function attributizeObject(pObject) {
return (
Object.keys(pObject)
// eslint-disable-next-line security/detect-object-injection
Expand All @@ -11,7 +10,7 @@ function attributizeObject(pObject) {
);
}

function extractFirstTransgression(pModule) {
export function extractFirstTransgression(pModule) {
return {
...(pModule?.rules?.[0]
? { ...pModule, tooltip: pModule.rules[0].name }
Expand All @@ -27,26 +26,6 @@ function extractFirstTransgression(pModule) {
};
}

function applyTheme(pTheme) {
return (pModule) => ({
...pModule,
dependencies: pModule.dependencies
.map((pDependency) => ({
...pDependency,
themeAttrs: attributizeObject(
theming.determineAttributes(pDependency, pTheme.dependencies),
),
}))
.map((pDependency) => ({
...pDependency,
hasExtraAttributes: Boolean(pDependency.rule || pDependency.themeAttrs),
})),
themeAttrs: attributizeObject(
theming.determineAttributes(pModule, pTheme.modules),
),
});
}

function toFullPath(pAll, pCurrent) {
return `${pAll}${pCurrent}${sep}`;
}
Expand Down Expand Up @@ -75,7 +54,7 @@ function makeInstabilityString(pModule, pShowMetrics = false) {
return lInstabilityString;
}

function folderify(pShowMetrics) {
export function folderify(pShowMetrics) {
/** @param {import("../../../types/cruise-result").IModule} pModule*/
return (pModule) => {
let lAdditions = {};
Expand Down Expand Up @@ -103,7 +82,7 @@ function folderify(pShowMetrics) {
* @param {string} pPrefix
* @returns {URL?: string}
*/
function addURL(pPrefix) {
export function addURL(pPrefix) {
return (pModule) => {
if (pModule.couldNotResolve) {
return pModule;
Expand All @@ -122,19 +101,10 @@ function makeLabel(pModule, pShowMetrics) {
)}</B>${makeInstabilityString(pModule, pShowMetrics)}>`;
}

function flatLabel(pShowMetrics) {
export function flatLabel(pShowMetrics) {
return (pModule) => ({
...pModule,
label: makeLabel(pModule, pShowMetrics),
tooltip: basename(pModule.source),
});
}

export default {
folderify,
applyTheme,
extractFirstTransgression,
attributizeObject,
addURL,
flatLabel,
};
11 changes: 6 additions & 5 deletions src/report/dot/prepare-custom-level.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import moduleUtl from "./module-utl.mjs";
import { folderify, addURL, extractFirstTransgression } from "./module-utl.mjs";
import { applyTheme } from "./theming.mjs";
import consolidateToPattern from "#graph-utl/consolidate-to-pattern.mjs";
import { compareModules } from "#graph-utl/compare.mjs";
import stripSelfTransitions from "#graph-utl/strip-self-transitions.mjs";
Expand All @@ -15,9 +16,9 @@ export default function prepareCustomLevel(
: pResults.modules
)
.sort(compareModules)
.map(moduleUtl.folderify(pShowMetrics))
.map(moduleUtl.extractFirstTransgression)
.map(folderify(pShowMetrics))
.map(extractFirstTransgression)
.map(stripSelfTransitions)
.map(moduleUtl.applyTheme(pTheme))
.map(moduleUtl.addURL(pResults.summary.optionsUsed?.prefix ?? ""));
.map(applyTheme(pTheme))
.map(addURL(pResults.summary.optionsUsed?.prefix ?? ""));
}
11 changes: 6 additions & 5 deletions src/report/dot/prepare-flat-level.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import moduleUtl from "./module-utl.mjs";
import { flatLabel, extractFirstTransgression, addURL } from "./module-utl.mjs";
import { applyTheme } from "./theming.mjs";
import { compareModules } from "#graph-utl/compare.mjs";

export default function prepareFlatLevel(pResults, pTheme, _, pShowMetrics) {
return pResults.modules
.sort(compareModules)
.map(moduleUtl.flatLabel(pShowMetrics))
.map(moduleUtl.extractFirstTransgression)
.map(moduleUtl.applyTheme(pTheme))
.map(moduleUtl.addURL(pResults.summary.optionsUsed?.prefix ?? ""));
.map(flatLabel(pShowMetrics))
.map(extractFirstTransgression)
.map(applyTheme(pTheme))
.map(addURL(pResults.summary.optionsUsed?.prefix ?? ""));
}
12 changes: 7 additions & 5 deletions src/report/dot/prepare-folder-level.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import moduleUtl from "./module-utl.mjs";
import { folderify, extractFirstTransgression, addURL } from "./module-utl.mjs";
import { applyTheme } from "./theming.mjs";
import consolidateToFolder from "#graph-utl/consolidate-to-folder.mjs";
import { compareModules } from "#graph-utl/compare.mjs";
import stripSelfTransitions from "#graph-utl/strip-self-transitions.mjs";
// fuk eslint

export default function prepareFolderLevel(pResults, pTheme, _, pShowMetrics) {
return consolidateToFolder(pResults.modules)
.sort(compareModules)
.map(moduleUtl.extractFirstTransgression)
.map(moduleUtl.folderify(pShowMetrics))
.map(extractFirstTransgression)
.map(folderify(pShowMetrics))
.map(stripSelfTransitions)
.map(moduleUtl.applyTheme(pTheme))
.map(moduleUtl.addURL(pResults.summary.optionsUsed?.prefix ?? ""));
.map(applyTheme(pTheme))
.map(addURL(pResults.summary.optionsUsed?.prefix ?? ""));
}
26 changes: 20 additions & 6 deletions src/report/dot/theming.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import DEFAULT_THEME from "./default-theme.mjs";
import { attributizeObject } from "./module-utl.mjs";
import { has, get } from "#utl/object-util.mjs";

function matchesRE(pValue, pRE) {
Expand Down Expand Up @@ -48,7 +49,7 @@ function moduleOrDependencyMatchesCriteria(pSchemeEntry, pModule) {
});
}

function determineAttributes(pModuleOrDependency, pAttributeCriteria) {
export function getThemeAttributes(pModuleOrDependency, pAttributeCriteria) {
return (pAttributeCriteria || [])
.filter((pSchemeEntry) =>
moduleOrDependencyMatchesCriteria(pSchemeEntry, pModuleOrDependency),
Expand All @@ -57,7 +58,7 @@ function determineAttributes(pModuleOrDependency, pAttributeCriteria) {
.reduce((pAll, pCurrent) => ({ ...pCurrent, ...pAll }), {});
}

function normalizeTheme(pTheme) {
export function normalizeTheme(pTheme) {
let lReturnValue = structuredClone(DEFAULT_THEME);

if (pTheme) {
Expand All @@ -78,7 +79,20 @@ function normalizeTheme(pTheme) {
return lReturnValue;
}

export default {
normalizeTheme,
determineAttributes,
};
export function applyTheme(pTheme) {
return (pModule) => ({
...pModule,
dependencies: pModule.dependencies
.map((pDependency) => ({
...pDependency,
themeAttrs: attributizeObject(
getThemeAttributes(pDependency, pTheme.dependencies),
),
}))
.map((pDependency) => ({
...pDependency,
hasExtraAttributes: Boolean(pDependency.rule || pDependency.themeAttrs),
})),
themeAttrs: attributizeObject(getThemeAttributes(pModule, pTheme.modules)),
});
}
9 changes: 2 additions & 7 deletions src/report/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const TYPE2MODULE = new Map([
* an options object (specific to that function)
* and returns an IReporterOutput
*/
async function getReporter(pOutputType) {
export async function getReporter(pOutputType) {
let lReturnValue = {};
if (pOutputType?.startsWith("plugin:")) {
lReturnValue = await getExternalPluginReporter(pOutputType);
Expand All @@ -52,11 +52,6 @@ async function getReporter(pOutputType) {
*
* @returns {import("../../types/shared-types.js").OutputType[]} -
*/
function getAvailableReporters() {
export function getAvailableReporters() {
return Array.from(TYPE2MODULE.keys());
}

export default {
getAvailableReporters,
getReporter,
};
9 changes: 2 additions & 7 deletions src/utl/regex-util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function extractGroups(pFromRestriction, pActualPath) {

if (lMatchResult && lMatchResult.length > 1) {
lReturnValue = lMatchResult.filter(
(pResult) => typeof pResult === "string"
(pResult) => typeof pResult === "string",
);
}
}
Expand Down Expand Up @@ -51,11 +51,6 @@ export function replaceGroupPlaceholders(pString, pExtractedGroups) {
(pAll, pThis, pIndex) =>
// eslint-disable-next-line security/detect-non-literal-regexp
pAll.replace(new RegExp(`\\$${pIndex}`, "g"), pThis),
pString
pString,
);
}

export default {
extractGroups,
replaceGroupPlaceholders,
};
Loading

0 comments on commit 4314428

Please sign in to comment.