Skip to content

Commit

Permalink
refactor: replaces lodash' flattenDeep, isEmpty, reject, omit, merge …
Browse files Browse the repository at this point in the history
…with native alternatives (#944)

## Description

- replaces lodash' flattenDeep, isEmpty, reject, omit, merge with native
alternatives

## Motivation and Context

Part of a series of PR's to phase out lodash (which is not maintained
anymore) as a production dependency.

## How Has This Been Tested?

- [x] green ci
- [x] additional unit test

## 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 Jul 6, 2024
1 parent 98ec40e commit 03c34a6
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 21 deletions.
20 changes: 9 additions & 11 deletions src/enrich/summarize/summarize-modules.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion src/extract/transpile/vue-template-wrap.cjs
Original file line number Diff line number Diff line change
@@ -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");

Expand Down Expand Up @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions src/graph-utl/consolidate-module-dependencies.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import reject from "lodash/reject.js";
import compare from "./compare.mjs";
import { uniq } from "#utl/array-util.mjs";

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/graph-utl/consolidate-modules.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _reject from "lodash/reject.js";
import uniqBy from "lodash/uniqBy.js";
import compare from "./compare.mjs";

Expand Down Expand Up @@ -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;
}
27 changes: 25 additions & 2 deletions src/main/options/assert-validity.mjs
Original file line number Diff line number Diff line change
@@ -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) &&
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/resolve-options/normalize.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions test/main/options/assert-validity.spec.mjs
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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"],
});
});
});

0 comments on commit 03c34a6

Please sign in to comment.