From 2507860dec8c986687fb597577ad72118a1970c4 Mon Sep 17 00:00:00 2001 From: sverweij Date: Mon, 6 Jan 2025 19:37:33 +0100 Subject: [PATCH] refactor(render): dedup a bit --- src/enrich/derive/circular.mjs | 22 ++++++++-------- src/enrich/derive/dependents/index.mjs | 35 ++++++++++++-------------- src/enrich/derive/orphan/index.mjs | 22 ++++++++-------- src/enrich/derive/reachable.mjs | 18 ++++++------- 4 files changed, 43 insertions(+), 54 deletions(-) diff --git a/src/enrich/derive/circular.mjs b/src/enrich/derive/circular.mjs index 66452b9e6..f2e59d4b2 100644 --- a/src/enrich/derive/circular.mjs +++ b/src/enrich/derive/circular.mjs @@ -1,24 +1,22 @@ /* eslint-disable security/detect-object-injection */ /** @import { IFlattenedRuleSet } from "../../../types/rule-set.mjs" */ + +function isCycleRule(pRule) { + return ( + /* c8 ignore start */ + Object.hasOwn(pRule?.to ?? {}, "circular") + /* c8 ignore stop */ + ); +} /** * @param {IFlattenedRuleSet} pRuleSet * @returns {boolean} */ export function hasCycleRule(pRuleSet) { return ( - (pRuleSet?.forbidden ?? []).some( - (pRule) => - /* c8 ignore start */ - Object.hasOwn(pRule?.to ?? {}, "circular"), - /* c8 ignore stop */ - ) || - (pRuleSet?.allowed ?? []).some( - (pRule) => - /* c8 ignore start */ - Object.hasOwn(pRule?.to ?? {}, "circular"), - /* c8 ignore stop */ - ) + (pRuleSet?.forbidden ?? []).some(isCycleRule) || + (pRuleSet?.allowed ?? []).some(isCycleRule) ); } diff --git a/src/enrich/derive/dependents/index.mjs b/src/enrich/derive/dependents/index.mjs index f1bbb090f..6ba12cea5 100644 --- a/src/enrich/derive/dependents/index.mjs +++ b/src/enrich/derive/dependents/index.mjs @@ -2,31 +2,28 @@ import getDependents from "./get-dependents.mjs"; /** @import { IFlattenedRuleSet } from "../../../../types/rule-set.mjs" */ +function isDependentsRule(pRule) { + // used in folder rules and when moreUnstable is in the 'to' => governed by + // the 'metrics' flag in options, sot not going to repeat that here + + // dependents are used in the orphans analsys. hHwever, there is a fall back + // where it does its own analysis, so not going to repeat that check here. + return ( + /* c8 ignore start */ + Object.hasOwn(pRule?.module ?? {}, "numberOfDependentsLessThan") || + Object.hasOwn(pRule?.module ?? {}, "numberOfDependentsMoreThan") + /* c8 ignore stop */ + ); +} + /** * @param {IFlattenedRuleSet} pRuleSet * @returns {boolean} */ export function hasDependentsRule(pRuleSet) { - // used in folder rules and when moreUnstable is in the 'to' => governed by - // the 'metrics' flag in options, sot not going to repeat that here - - // dependents are used in the orphans analsys, however, there is a fall back - // where it does its own analysis, so not going to repeat that check here. return ( - (pRuleSet?.forbidden ?? []).some( - (pRule) => - /* c8 ignore start */ - Object.hasOwn(pRule?.module ?? {}, "numberOfDependentsLessThan") || - Object.hasOwn(pRule?.module ?? {}, "numberOfDependentsMoreThan"), - /* c8 ignore stop */ - ) || - (pRuleSet?.allowed ?? []).some( - (pRule) => - /* c8 ignore start */ - Object.hasOwn(pRule?.module ?? {}, "numberOfDependentsLessThan") || - Object.hasOwn(pRule?.module ?? {}, "numberOfDependentsMoreThan"), - /* c8 ignore stop */ - ) + (pRuleSet?.forbidden ?? []).some(isDependentsRule) || + (pRuleSet?.allowed ?? []).some(isDependentsRule) ); } diff --git a/src/enrich/derive/orphan/index.mjs b/src/enrich/derive/orphan/index.mjs index 3e2be08f4..c1095f606 100644 --- a/src/enrich/derive/orphan/index.mjs +++ b/src/enrich/derive/orphan/index.mjs @@ -1,24 +1,22 @@ import isOrphan from "./is-orphan.mjs"; /** @import { IFlattenedRuleSet } from "../../../../types/rule-set.mjs" */ + +function isOrphanRule(pRule) { + return ( + /* c8 ignore start */ + Object.hasOwn(pRule?.from ?? {}, "orphan") + /* c8 ignore stop */ + ); +} /** * @param {IFlattenedRuleSet} pRuleSet * @returns {boolean} */ export function hasOrphanRule(pRuleSet) { return ( - (pRuleSet?.forbidden ?? []).some( - (pRule) => - /* c8 ignore start */ - Object.hasOwn(pRule?.from ?? {}, "orphan"), - /* c8 ignore stop */ - ) || - (pRuleSet?.allowed ?? []).some( - (pRule) => - /* c8 ignore start */ - Object.hasOwn(pRule?.from ?? {}, "orphan"), - /* c8 ignore stop */ - ) + (pRuleSet?.forbidden ?? []).some(isOrphanRule) || + (pRuleSet?.allowed ?? []).some(isOrphanRule) ); } diff --git a/src/enrich/derive/reachable.mjs b/src/enrich/derive/reachable.mjs index 95b8d9118..a384cdcb0 100644 --- a/src/enrich/derive/reachable.mjs +++ b/src/enrich/derive/reachable.mjs @@ -6,19 +6,15 @@ import { import IndexedModuleGraph from "#graph-utl/indexed-module-graph.mjs"; import { extractGroups } from "#utl/regex-util.mjs"; +function isReachableRule(pRule) { + return Object.hasOwn(pRule?.to ?? {}, "reachable"); +} + function getReachableRules(pRuleSet) { return (pRuleSet?.forbidden ?? []) - .filter((pRule) => Object.hasOwn(pRule?.to ?? {}, "reachable")) - .concat( - (pRuleSet?.allowed ?? []).filter((pRule) => - Object.hasOwn(pRule?.to ?? {}, "reachable"), - ), - ) - .concat( - (pRuleSet?.required ?? []).filter((pRule) => - Object.hasOwn(pRule?.to ?? {}, "reachable"), - ), - ); + .filter(isReachableRule) + .concat((pRuleSet?.allowed ?? []).filter(isReachableRule)) + .concat((pRuleSet?.required ?? []).filter(isReachableRule)); } function isModuleInRuleFrom(pRule) {