Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replaces lodash' flattenDeep, isEmpty, reject, omit, merge with native alternatives #944

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"],
});
});
});