Skip to content

Commit

Permalink
refactor: replaces lodash/has & lodash/get with native options where …
Browse files Browse the repository at this point in the history
…possible (#924)

## Description, Motivation and Context

- replaces lodash/has & lodash/get with native options where possible
because it now often _is_ easily possible with native options

## How Has This Been Tested?

- [x] green ci

## 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 committed Mar 18, 2024
1 parent 24370fc commit fea1abe
Show file tree
Hide file tree
Showing 30 changed files with 145 additions and 149 deletions.
2 changes: 1 addition & 1 deletion .c8rc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"checkCoverage": true,
"statements": 99.89,
"branches": 98.86,
"branches": 98.41,
"functions": 100,
"lines": 99.89,
"exclude": [
Expand Down
19 changes: 9 additions & 10 deletions src/cli/init-config/environment-helpers.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @ts-check
import { readFileSync, readdirSync, accessSync, statSync, R_OK } from "node:fs";
import { join } from "node:path";
import has from "lodash/has.js";
import { DEFAULT_CONFIG_FILE_NAME } from "../defaults.mjs";

const LIKELY_SOURCE_FOLDERS = ["src", "lib", "app", "bin", "sources"];
Expand Down Expand Up @@ -47,7 +45,8 @@ function babelIsConfiguredInManifest() {
let lReturnValue = false;

try {
lReturnValue = has(readManifest(), "babel");
// @ts-expect-error defaultly tsc doesn't know about newfangled stuff like hasOwn
lReturnValue = Object.hasOwn(readManifest(), "babel");
} catch (pError) {
// silently ignore - we'll return false anyway then
}
Expand Down Expand Up @@ -75,7 +74,7 @@ export function isTypeModule() {
*/
function getFolderNames(pFolderName) {
return readdirSync(pFolderName, "utf8").filter((pFileName) =>
statSync(join(pFolderName, pFileName)).isDirectory()
statSync(join(pFolderName, pFileName)).isDirectory(),
);
}

Expand All @@ -88,7 +87,7 @@ function getMatchingFileNames(pPattern, pFolderName = process.cwd()) {
return readdirSync(pFolderName, "utf8").filter(
(pFileName) =>
statSync(join(pFolderName, pFileName)).isFile() &&
pFileName.match(pPattern)
pFileName.match(pPattern),
);
}

Expand All @@ -102,14 +101,14 @@ export function isLikelyMonoRepo(pFolderNames = getFolderNames(process.cwd())) {

export function hasTestsWithinSource(pTestLocations, pSourceLocations) {
return pTestLocations.every((pTestLocation) =>
pSourceLocations.includes(pTestLocation)
pSourceLocations.includes(pTestLocation),
);
}

export function getFolderCandidates(pCandidateFolderArray) {
return (pFolderNames = getFolderNames(process.cwd())) => {
return pFolderNames.filter((pFolderName) =>
pCandidateFolderArray.includes(pFolderName)
pCandidateFolderArray.includes(pFolderName),
);
};
}
Expand All @@ -134,7 +133,7 @@ function getManifestFilesWithABabelConfig() {

export const getBabelConfigCandidates = () =>
getManifestFilesWithABabelConfig().concat(
getMatchingFileNames(BABEL_CONFIG_CANDIDATE_PATTERN)
getMatchingFileNames(BABEL_CONFIG_CANDIDATE_PATTERN),
);
export const hasBabelConfigCandidates = () =>
getBabelConfigCandidates().length > 0;
Expand All @@ -154,12 +153,12 @@ export const hasWebpackConfigCandidates = () =>
getWebpackConfigCandidates().length > 0;

export const getSourceFolderCandidates = getFolderCandidates(
LIKELY_SOURCE_FOLDERS
LIKELY_SOURCE_FOLDERS,
);
export const getTestFolderCandidates = getFolderCandidates(LIKELY_TEST_FOLDERS);

export const getMonoRepoPackagesCandidates = getFolderCandidates(
LIKELY_PACKAGES_FOLDERS
LIKELY_PACKAGES_FOLDERS,
);

/**
Expand Down
3 changes: 1 addition & 2 deletions src/cli/init-config/normalize-init-options.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import has from "lodash/has.js";
import {
getSourceFolderCandidates,
getTestFolderCandidates,
Expand Down Expand Up @@ -72,7 +71,7 @@ function populate(pInitOptions) {
*/
export default function normalizeInitOptions(pInitOptions) {
let lReturnValue = populate(pInitOptions);
if (!has(lReturnValue, "hasTestsOutsideSource")) {
if (!Object.hasOwn(lReturnValue, "hasTestsOutsideSource")) {
lReturnValue.hasTestsOutsideSource =
!pInitOptions.isMonoRepo &&
!hasTestsWithinSource(
Expand Down
23 changes: 12 additions & 11 deletions src/cli/normalize-cli-options.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { accessSync, R_OK } from "node:fs";
import { isAbsolute } from "node:path";
import set from "lodash/set.js";
import get from "lodash/get.js";
import has from "lodash/has.js";
import {
RULES_FILE_NAME_SEARCH_ARRAY,
DEFAULT_BASELINE_FILE_NAME,
Expand All @@ -22,7 +20,7 @@ function getOptionValue(pDefault) {
function normalizeConfigFileName(pCliOptions, pConfigWrapperName, pDefault) {
let lOptions = structuredClone(pCliOptions);

if (has(lOptions, pConfigWrapperName)) {
if (Object.hasOwn(lOptions, pConfigWrapperName)) {
set(
lOptions,
`ruleSet.options.${pConfigWrapperName}.fileName`,
Expand All @@ -33,8 +31,8 @@ function normalizeConfigFileName(pCliOptions, pConfigWrapperName, pDefault) {
}

if (
get(lOptions, `ruleSet.options.${pConfigWrapperName}`, null) &&
!get(lOptions, `ruleSet.options.${pConfigWrapperName}.fileName`, null)
(lOptions?.ruleSet?.options?.[pConfigWrapperName] ?? null) &&
!(lOptions?.ruleSet?.options?.[pConfigWrapperName]?.fileName ?? null)
) {
set(lOptions, `ruleSet.options.${pConfigWrapperName}.fileName`, pDefault);
}
Expand Down Expand Up @@ -108,7 +106,10 @@ function validateAndGetKnownViolationsFileName(pKnownViolations) {
}

function normalizeKnownViolationsOption(pCliOptions) {
if (!has(pCliOptions, "ignoreKnown") || pCliOptions.ignoreKnown === false) {
if (
!Object.hasOwn(pCliOptions, "ignoreKnown") ||
pCliOptions.ignoreKnown === false
) {
return {};
}
return {
Expand All @@ -135,7 +136,7 @@ async function normalizeValidationOption(pCliOptions) {
}

function normalizeProgress(pCliOptions) {
if (!has(pCliOptions, "progress")) {
if (!Object.hasOwn(pCliOptions, "progress")) {
return {};
}

Expand All @@ -147,7 +148,7 @@ function normalizeProgress(pCliOptions) {
}

function normalizeCacheStrategy(pCliOptions) {
if (!has(pCliOptions, "cacheStrategy")) {
if (!Object.hasOwn(pCliOptions, "cacheStrategy")) {
return {};
}
const lStrategy =
Expand All @@ -169,7 +170,7 @@ function normalizeCacheStrategy(pCliOptions) {
}

function normalizeCache(pCliOptions) {
if (!has(pCliOptions, "cache")) {
if (!Object.hasOwn(pCliOptions, "cache")) {
return {};
}

Expand Down Expand Up @@ -203,13 +204,13 @@ export default async function normalizeOptions(pOptionsAsPassedFromCommander) {
...pOptionsAsPassedFromCommander,
};

if (has(lOptions, "moduleSystems")) {
if (Object.hasOwn(lOptions, "moduleSystems")) {
lOptions.moduleSystems = lOptions.moduleSystems
.split(",")
.map((pString) => pString.trim());
}

if (has(lOptions, "config")) {
if (Object.hasOwn(lOptions, "config")) {
lOptions.validate = lOptions.config;
}

Expand Down
3 changes: 1 addition & 2 deletions src/config-utl/extract-depcruise-config/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { dirname } from "node:path";
import has from "lodash/has.js";
import readConfig from "./read-config.mjs";
import mergeConfigs from "./merge-configs.mjs";
import normalizeResolveOptions from "#main/resolve-options/normalize.mjs";
Expand Down Expand Up @@ -80,7 +79,7 @@ export default async function extractDepcruiseConfig(

let lReturnValue = await readConfig(lResolvedFileName);

if (has(lReturnValue, "extends")) {
if (lReturnValue?.extends) {
lReturnValue = await processExtends(
lReturnValue,
pAlreadyVisited,
Expand Down
7 changes: 4 additions & 3 deletions src/enrich/derive/reachable.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-disable security/detect-object-injection, no-inline-comments */
import has from "lodash/has.js";
import matchers from "#validate/matchers.mjs";
import IndexedModuleGraph from "#graph-utl/indexed-module-graph.mjs";
import { extractGroups } from "#utl/regex-util.mjs";

function getReachableRules(pRuleSet) {
return (pRuleSet?.forbidden ?? [])
.filter((pRule) => has(pRule.to, "reachable"))
.filter((pRule) => Object.hasOwn(pRule?.to ?? {}, "reachable"))
.concat(
(pRuleSet?.allowed ?? []).filter((pRule) => has(pRule.to, "reachable")),
(pRuleSet?.allowed ?? []).filter((pRule) =>
Object.hasOwn(pRule?.to ?? {}, "reachable"),
),
);
}

Expand Down
5 changes: 2 additions & 3 deletions src/enrich/summarize/summarize-folders.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import has from "lodash/has.js";
import { findRuleByName } from "#graph-utl/rule-set.mjs";

function classifyViolation(pRule, pRuleSet) {
const lRule = findRuleByName(pRuleSet, pRule.name);
if (has(lRule, "to.moreUnstable")) {
if (Object.hasOwn(lRule?.to ?? {}, "moreUnstable")) {
return "instability";
}
if (has(lRule, "to.circular")) {
if (Object.hasOwn(lRule?.to ?? {}, "circular")) {
return "cycle";
}
return "folder";
Expand Down
13 changes: 8 additions & 5 deletions src/enrich/summarize/summarize-modules.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import flattenDeep from "lodash/flattenDeep.js";
import has from "lodash/has.js";
import uniqWith from "lodash/uniqWith.js";
import isSameViolation from "./is-same-violation.mjs";
import { findRuleByName } from "#graph-utl/rule-set.mjs";
Expand All @@ -14,6 +13,7 @@ function cutNonTransgressions(pModule) {
};
}

// eslint-disable-next-line complexity
function toDependencyViolationSummary(pRule, pModule, pDependency, pRuleSet) {
let lReturnValue = {
type: "dependency",
Expand All @@ -23,7 +23,7 @@ function toDependencyViolationSummary(pRule, pModule, pDependency, pRuleSet) {
};

if (
has(pDependency, "cycle") &&
Object.hasOwn(pDependency, "cycle") &&
findRuleByName(pRuleSet, pRule.name)?.to?.circular
) {
lReturnValue = {
Expand All @@ -34,9 +34,12 @@ function toDependencyViolationSummary(pRule, pModule, pDependency, pRuleSet) {
}

if (
has(pModule, "instability") &&
has(pDependency, "instability") &&
has(findRuleByName(pRuleSet, pRule.name), "to.moreUnstable")
Object.hasOwn(pModule, "instability") &&
Object.hasOwn(pDependency, "instability") &&
Object.hasOwn(
findRuleByName(pRuleSet, pRule.name)?.to ?? {},
"moreUnstable",
)
) {
lReturnValue = {
...lReturnValue,
Expand Down
11 changes: 5 additions & 6 deletions src/enrich/summarize/summarize-options.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import has from "lodash/has.js";

const SHAREABLE_OPTIONS = [
"babelConfig",
"baseDir",
Expand Down Expand Up @@ -35,22 +33,23 @@ const SHAREABLE_OPTIONS = [
function makeOptionsPresentable(pOptions) {
return SHAREABLE_OPTIONS.filter(
(pShareableOptionKey) =>
has(pOptions, pShareableOptionKey) && pOptions[pShareableOptionKey] !== 0
Object.hasOwn(pOptions, pShareableOptionKey) &&
pOptions?.[pShareableOptionKey] !== 0,
)
.filter(
(pShareableOptionKey) =>
pShareableOptionKey !== "doNotFollow" ||
Object.keys(pOptions.doNotFollow).length > 0
Object.keys(pOptions.doNotFollow).length > 0,
)
.filter(
(pShareableOptionKey) =>
pShareableOptionKey !== "exclude" ||
Object.keys(pOptions.exclude).length > 0
Object.keys(pOptions.exclude).length > 0,
)
.filter(
(pShareableOptionKey) =>
pShareableOptionKey !== "knownViolations" ||
pOptions.knownViolations.length > 0
pOptions.knownViolations.length > 0,
)
.reduce((pAll, pShareableOptionKey) => {
pAll[pShareableOptionKey] = pOptions[pShareableOptionKey];
Expand Down
3 changes: 1 addition & 2 deletions src/extract/index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import has from "lodash/has.js";
import getDependencies from "./get-dependencies.mjs";
import gatherInitialSources from "./gather-initial-sources.mjs";
import clearCaches from "./clear-caches.mjs";
Expand Down Expand Up @@ -123,7 +122,7 @@ function filterExcludedDynamicDependencies(pModule, pExclude) {
...pModule,
dependencies: pModule.dependencies.filter(
({ dynamic }) =>
!has(pExclude, "dynamic") || pExclude.dynamic !== dynamic,
!Object.hasOwn(pExclude, "dynamic") || pExclude.dynamic !== dynamic,
),
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/extract/resolve/determine-dependency-types.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { join } from "node:path/posix";
import has from "lodash/has.js";
import {
isRelativeModuleName,
isExternalModule,
Expand All @@ -17,7 +16,7 @@ function dependencyKeyHasModuleName(
) {
return (pKey) =>
pKey.includes("ependencies") &&
has(pPackageDependencies[pKey], join(pPrefix, pModuleName));
Object.hasOwn(pPackageDependencies[pKey], join(pPrefix, pModuleName));
}

const NPM2DEP_TYPE = new Map([
Expand Down
6 changes: 3 additions & 3 deletions src/extract/resolve/external-module-helpers.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { readFileSync } from "node:fs";
import { join } from "node:path";
import memoize, { memoizeClear } from "memoize";
import has from "lodash/has.js";
import { resolve } from "./resolve.mjs";
import { isScoped, isRelativeModuleName } from "./module-classifiers.mjs";

Expand Down Expand Up @@ -131,7 +130,8 @@ export function dependencyIsDeprecated(
);

if (Boolean(lPackageJson)) {
lReturnValue = has(lPackageJson, "deprecated") && lPackageJson.deprecated;
lReturnValue =
Object.hasOwn(lPackageJson, "deprecated") && lPackageJson.deprecated;
}
return lReturnValue;
}
Expand All @@ -155,7 +155,7 @@ export function getLicense(pModuleName, pFileDirectory, pResolveOptions) {

if (
Boolean(lPackageJson) &&
has(lPackageJson, "license") &&
Object.hasOwn(lPackageJson, "license") &&
typeof lPackageJson.license === "string"
) {
lReturnValue = lPackageJson.license;
Expand Down

0 comments on commit fea1abe

Please sign in to comment.