Skip to content

Commit

Permalink
refactor(render): dedup a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij committed Jan 6, 2025
1 parent 4c5b276 commit 2507860
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 54 deletions.
22 changes: 10 additions & 12 deletions src/enrich/derive/circular.mjs
Original file line number Diff line number Diff line change
@@ -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)
);
}

Expand Down
35 changes: 16 additions & 19 deletions src/enrich/derive/dependents/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down
22 changes: 10 additions & 12 deletions src/enrich/derive/orphan/index.mjs
Original file line number Diff line number Diff line change
@@ -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)
);
}

Expand Down
18 changes: 7 additions & 11 deletions src/enrich/derive/reachable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2507860

Please sign in to comment.