From 6fc07bd81d51cbfda9b8727d68d1ade21e999765 Mon Sep 17 00:00:00 2001 From: sverweij Date: Sat, 6 Jul 2024 14:21:02 +0200 Subject: [PATCH 1/2] refactor: replaces lodash' flattenDeep, isEmpty, reject, omit with native alternatives --- src/enrich/summarize/summarize-modules.mjs | 20 +++++++++---------- src/extract/transpile/vue-template-wrap.cjs | 7 ++++++- .../consolidate-module-dependencies.mjs | 8 ++++---- src/graph-utl/consolidate-modules.mjs | 6 ++++-- src/main/resolve-options/normalize.mjs | 8 +++++++- 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/enrich/summarize/summarize-modules.mjs b/src/enrich/summarize/summarize-modules.mjs index b7873c0a1..25fa19b66 100644 --- a/src/enrich/summarize/summarize-modules.mjs +++ b/src/enrich/summarize/summarize-modules.mjs @@ -1,4 +1,3 @@ -import flattenDeep from "lodash/flattenDeep.js"; import uniqWith from "lodash/uniqWith.js"; import isSameViolation from "./is-same-violation.mjs"; import { findRuleByName } from "#graph-utl/rule-set.mjs"; @@ -72,18 +71,17 @@ function toDependencyViolationSummary(pRule, pModule, pDependency, pRuleSet) { * @return {any} an array of violations */ function extractDependencyViolations(pModules, pRuleSet) { - return flattenDeep( - pModules - .map(cutNonTransgressions) - .filter((pModule) => pModule.dependencies.length > 0) - .map((pModule) => - pModule.dependencies.map((pDependency) => - pDependency.rules.map((pRule) => - toDependencyViolationSummary(pRule, pModule, pDependency, pRuleSet), - ), + return pModules + .map(cutNonTransgressions) + .filter((pModule) => pModule.dependencies.length > 0) + .map((pModule) => + pModule.dependencies.map((pDependency) => + pDependency.rules.map((pRule) => + toDependencyViolationSummary(pRule, pModule, pDependency, pRuleSet), ), ), - ); + ) + .flat(Infinity); } function toModuleViolationSummary(pRule, pModule, pRuleSet) { diff --git a/src/extract/transpile/vue-template-wrap.cjs b/src/extract/transpile/vue-template-wrap.cjs index 9fd120739..1d5f2df76 100644 --- a/src/extract/transpile/vue-template-wrap.cjs +++ b/src/extract/transpile/vue-template-wrap.cjs @@ -1,5 +1,4 @@ const { EOL } = require("node:os"); -const isEmpty = require("lodash/isEmpty"); const tryRequire = require("#utl/try-require.cjs"); const meta = require("#meta.cjs"); @@ -29,6 +28,12 @@ function getVueTemplateCompiler() { return { lCompiler, lIsVue3 }; } +function isEmpty(pObject) { + return ( + Object.entries(pObject).length === 0 && !pObject?.length && !pObject?.size + ); +} + const { lCompiler: vueTemplateCompiler, lIsVue3: isVue3 } = getVueTemplateCompiler(); diff --git a/src/graph-utl/consolidate-module-dependencies.mjs b/src/graph-utl/consolidate-module-dependencies.mjs index 69ebea2cd..41b516972 100644 --- a/src/graph-utl/consolidate-module-dependencies.mjs +++ b/src/graph-utl/consolidate-module-dependencies.mjs @@ -1,4 +1,3 @@ -import reject from "lodash/reject.js"; import compare from "./compare.mjs"; import { uniq } from "#utl/array-util.mjs"; @@ -42,9 +41,10 @@ function consolidateDependencies(pDependencies) { lReturnValue.push( mergeDependencies(lDependencies[0].resolved, lDependencies), ); - lDependencies = reject(lDependencies, { - resolved: lDependencies[0].resolved, - }); + lDependencies = lDependencies.filter( + // eslint-disable-next-line no-loop-func + (pDependency) => pDependency.resolved !== lDependencies[0].resolved, + ); } return lReturnValue; diff --git a/src/graph-utl/consolidate-modules.mjs b/src/graph-utl/consolidate-modules.mjs index 3cd21edcf..a2d00c581 100644 --- a/src/graph-utl/consolidate-modules.mjs +++ b/src/graph-utl/consolidate-modules.mjs @@ -1,4 +1,3 @@ -import _reject from "lodash/reject.js"; import uniqBy from "lodash/uniqBy.js"; import compare from "./compare.mjs"; @@ -39,7 +38,10 @@ export default function consolidateModules(pModules) { while (lModules.length > 0) { lReturnValue.push(mergeModules(lModules[0].source, lModules)); - lModules = _reject(lModules, { source: lModules[0].source }); + lModules = lModules.filter( + // eslint-disable-next-line no-loop-func + (pModule) => pModule.source !== lModules[0].source, + ); } return lReturnValue; } diff --git a/src/main/resolve-options/normalize.mjs b/src/main/resolve-options/normalize.mjs index 302d54bf0..7b5e43d8b 100644 --- a/src/main/resolve-options/normalize.mjs +++ b/src/main/resolve-options/normalize.mjs @@ -1,6 +1,5 @@ import fs from "node:fs"; import enhancedResolve from "enhanced-resolve"; -import omit from "lodash/omit.js"; import { scannableExtensions } from "#extract/transpile/meta.mjs"; import { ruleSetHasDeprecationRule, @@ -32,6 +31,13 @@ const DEFAULT_RESOLVE_OPTIONS = { exportsFields: [], }; +function omit(pObject, pProperty) { + const lObject = structuredClone(pObject); + // eslint-disable-next-line security/detect-object-injection + delete lObject[pProperty]; + return lObject; +} + /** * * @param {Number} pCacheDuration From ca94ad1b86dfd8d93fea0e2b430e71fb559c62fc Mon Sep 17 00:00:00 2001 From: sverweij Date: Sat, 6 Jul 2024 15:11:33 +0200 Subject: [PATCH 2/2] refactor: replaces lodash' merge with native alternative --- src/main/options/assert-validity.mjs | 27 ++++++++++++++++++++-- test/main/options/assert-validity.spec.mjs | 27 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/main/options/assert-validity.mjs b/src/main/options/assert-validity.mjs index b7544c0b7..c9e362eca 100644 --- a/src/main/options/assert-validity.mjs +++ b/src/main/options/assert-validity.mjs @@ -1,10 +1,33 @@ -import merge from "lodash/merge.js"; +/* eslint-disable security/detect-object-injection */ import safeRegex from "safe-regex"; import report from "#report/index.mjs"; const MODULE_SYSTEM_LIST_RE = /^(?:(?:cjs|amd|es6|tsd)(?:,|$)){1,4}/gi; const VALID_DEPTH_RE = /^\d{1,2}$/g; +function isObject(pObject) { + return ( + typeof pObject === "object" && !Array.isArray(pObject) && pObject !== null + ); +} +function deepMerge(pTarget, pSource) { + const lOutput = structuredClone(pTarget); + + for (const lKey in pSource) { + if (isObject(pSource[lKey])) { + if (lKey in pTarget) { + lOutput[lKey] = deepMerge(pTarget[lKey], pSource[lKey]); + } else { + Object.assign(lOutput, { [lKey]: pSource[lKey] }); + } + } else { + Object.assign(lOutput, { [lKey]: pSource[lKey] }); + } + } + + return lOutput; +} + function assertModuleSystemsValid(pModuleSystems) { if ( Boolean(pModuleSystems) && @@ -102,7 +125,7 @@ export function assertCruiseOptionsValid(pOptions) { if (pOptions?.ruleSet?.options) { lReturnValue = assertCruiseOptionsValid(pOptions.ruleSet.options); } - return merge({}, lReturnValue, pOptions); + return deepMerge(lReturnValue, pOptions); } return lReturnValue; } diff --git a/test/main/options/assert-validity.spec.mjs b/test/main/options/assert-validity.spec.mjs index 157fabf94..cca39e891 100644 --- a/test/main/options/assert-validity.spec.mjs +++ b/test/main/options/assert-validity.spec.mjs @@ -1,4 +1,5 @@ import { doesNotThrow, equal, throws } from "node:assert/strict"; +import { deepEqual } from "node:assert"; import { assertCruiseOptionsValid } from "#main/options/assert-validity.mjs"; describe("[U] main/options/validate - module systems", () => { @@ -185,3 +186,29 @@ describe("[U] main/options/validate - exclude", () => { equal(lOptions.doNotFollow, "from the commandline"); }); }); + +describe("[U] main/options/validate - enhancedResolveOptions", () => { + it("options passed in --validate rule-set drip down to the proper options (objects edition)", () => { + const lOptions = assertCruiseOptionsValid({ + enhancedResolveOptions: { + exportsFields: ["exports"], + conditionNames: ["import", "require"], + extensions: [".cjs", ".mjs"], + }, + ruleSet: { + options: { + enhancedResolveOptions: { + exportsFields: ["exports"], + conditionNames: ["import"], + }, + }, + }, + }); + + deepEqual(lOptions.enhancedResolveOptions, { + exportsFields: ["exports"], + conditionNames: ["import", "require"], + extensions: [".cjs", ".mjs"], + }); + }); +});