From b63a136a151113b0c08516fc9dc1bcc7804c902e Mon Sep 17 00:00:00 2001 From: sverweij Date: Fri, 20 Sep 2024 20:27:38 +0200 Subject: [PATCH] and for match-module-rule as well --- src/validate/match-module-rule-helpers.mjs | 132 ++++++++++++++++ src/validate/match-module-rule.mjs | 141 +----------------- test/configs/no-orphans.spec.mjs | 20 +-- .../match-module-rule.dependents.spec.mjs | 121 ++++++--------- .../match-module-rule.orphan.spec.mjs | 28 ++-- .../match-module-rule.reachable.spec.mjs | 28 ++-- .../match-module-rule.reaches.spec.mjs | 10 +- 7 files changed, 223 insertions(+), 257 deletions(-) create mode 100644 src/validate/match-module-rule-helpers.mjs diff --git a/src/validate/match-module-rule-helpers.mjs b/src/validate/match-module-rule-helpers.mjs new file mode 100644 index 000000000..9590695db --- /dev/null +++ b/src/validate/match-module-rule-helpers.mjs @@ -0,0 +1,132 @@ +import { + matchToModulePath, + matchToModulePathNot, + matchesFromPath, + matchesFromPathNot, + matchesModulePath, + matchesModulePathNot, +} from "./matchers.mjs"; +import { extractGroups } from "#utl/regex-util.mjs"; +/** + * Returns true if pRule is an orphan rule and pModule is an orphan. + * Returns false in all other cases + * + * @param {import("../../types/rule-set.mjs").IAnyRuleType} pRule + * @param {import("../../types/cruise-result.mjs").IModule} pModule + * @returns {boolean} + */ +export function matchesOrphanRule(pRule, pModule) { + return ( + Object.hasOwn(pRule?.from ?? {}, "orphan") && + // @ts-expect-error the 'hasOwn' above guarantees there's a 'from.orphan' attribute + pModule.orphan === pRule.from.orphan && + matchesFromPath(pRule, pModule) && + matchesFromPathNot(pRule, pModule) + ); +} + +/** + * Returns true if pRule is a 'reachable' rule and pModule matches the reachability + * criteria. + * Returns false in all other cases + * + * @param {import("../../types/rule-set.mjs").IAnyRuleType} pRule + * @param {import("../../types/cruise-result.mjs").IModule} pModule + * @returns {boolean} + */ +export function matchesReachableRule(pRule, pModule) { + if ( + Object.hasOwn(pRule?.to ?? {}, "reachable") && + Object.hasOwn(pModule, "reachable") + ) { + // @ts-expect-error the 'hasOwn' above ensures the 'reachable' exists + const lReachableRecord = pModule.reachable.find( + (pReachable) => + pReachable.asDefinedInRule === pRule.name && + // @ts-expect-error the 'hasOwn' above ensures the 'to.reachable' exists + pReachable.value === pRule.to.reachable, + ); + if (lReachableRecord) { + const lGroups = extractGroups(pRule.from, lReachableRecord.matchedFrom); + + return ( + matchToModulePath(pRule, pModule, lGroups) && + matchToModulePathNot(pRule, pModule, lGroups) + ); + } + } + return false; +} + +/** + * Returns true if pRule is a 'reaches' rule and pModule matches the reachability + * criteria. + * Returns false in all other cases + * + * @param {import("../../types/rule-set.mjs").IAnyRuleType} pRule + * @param {import("../../types/cruise-result.mjs").IModule} pModule + * @returns {boolean} + */ +export function matchesReachesRule(pRule, pModule) { + return ( + Object.hasOwn(pRule?.to ?? {}, "reachable") && + Object.hasOwn(pModule, "reaches") && + // @ts-expect-error the 'hasOwn' above guarantees the .reaches exists + pModule.reaches.some( + (pReaches) => + pReaches.asDefinedInRule === pRule.name && + pReaches.modules.some( + (pReachesModule) => + matchToModulePath(pRule, pReachesModule) && + matchToModulePathNot(pRule, pReachesModule), + ), + ) + ); +} +/** + * + * @param {import("../../types/rule-set.mjs").IAnyRuleType} pRule + * @param {string[]} pDependents + * @returns {boolean} + */ +function dependentsCountsMatch(pRule, pDependents) { + const lMatchingDependentsCount = pDependents.filter( + (pDependent) => + Boolean(!pRule.from.path || pDependent.match(pRule.from.path)) && + Boolean(!pRule.from.pathNot || !pDependent.match(pRule.from.pathNot)), + ).length; + return ( + (!pRule.module.numberOfDependentsLessThan || + lMatchingDependentsCount < pRule.module.numberOfDependentsLessThan) && + (!pRule.module.numberOfDependentsMoreThan || + lMatchingDependentsCount > pRule.module.numberOfDependentsMoreThan) + ); +} + +/** + * + * @param {import("../../types/rule-set.mjs").IAnyRuleType} pRule + * @param {import("../../types/cruise-result.mjs").IModule} pModule + * @returns {boolean} + */ +// eslint-disable-next-line complexity +export function matchesDependentsRule(pRule, pModule) { + if ( + (Object.hasOwn(pModule, "dependents") && + Object.hasOwn(pRule?.module ?? {}, "numberOfDependentsLessThan")) || + Object.hasOwn(pRule?.module ?? {}, "numberOfDependentsMoreThan") + ) { + return ( + // group matching seems like a nice idea, however, the 'from' part of the + // rule is going to match not one module (as with regular dependency rules) + // but a whole bunch of them, being the 'dependents'. So that match is going + // to produce not one result, but one per matching dependent. To get meaningful + // results we'd probably have to loop over these and or the + // matchToModulePath together. + matchesModulePath(pRule, pModule) && + matchesModulePathNot(pRule, pModule) && + dependentsCountsMatch(pRule, pModule.dependents) + ); + } + return false; +} diff --git a/src/validate/match-module-rule.mjs b/src/validate/match-module-rule.mjs index 9c703c806..cba9fd608 100644 --- a/src/validate/match-module-rule.mjs +++ b/src/validate/match-module-rule.mjs @@ -1,137 +1,10 @@ import { isModuleOnlyRule, isFolderScope } from "./rule-classifiers.mjs"; import { - matchToModulePath, - matchToModulePathNot, - matchesFromPath, - matchesFromPathNot, - matchesModulePath, - matchesModulePathNot, -} from "./matchers.mjs"; -import { extractGroups } from "#utl/regex-util.mjs"; - -/** - * Returns true if pRule is an orphan rule and pModule is an orphan. - * Returns false in all other cases - * - * @param {import("../../types/rule-set.mjs").IAnyRuleType} pRule - * @param {import("../../types/cruise-result.mjs").IModule} pModule - * @returns {boolean} - */ -function matchesOrphanRule(pRule, pModule) { - return ( - Object.hasOwn(pRule?.from ?? {}, "orphan") && - // @ts-expect-error the 'hasOwn' above guarantees there's a 'from.orphan' attribute - pModule.orphan === pRule.from.orphan && - matchesFromPath(pRule, pModule) && - matchesFromPathNot(pRule, pModule) - ); -} - -/** - * Returns true if pRule is a 'reachable' rule and pModule matches the reachability - * criteria. - * Returns false in all other cases - * - * @param {import("../../types/rule-set.mjs").IAnyRuleType} pRule - * @param {import("../../types/cruise-result.mjs").IModule} pModule - * @returns {boolean} - */ -function matchesReachableRule(pRule, pModule) { - if ( - Object.hasOwn(pRule?.to ?? {}, "reachable") && - Object.hasOwn(pModule, "reachable") - ) { - // @ts-expect-error the 'hasOwn' above ensures the 'reachable' exists - const lReachableRecord = pModule.reachable.find( - (pReachable) => - pReachable.asDefinedInRule === pRule.name && - // @ts-expect-error the 'hasOwn' above ensures the 'to.reachable' exists - pReachable.value === pRule.to.reachable, - ); - if (lReachableRecord) { - const lGroups = extractGroups(pRule.from, lReachableRecord.matchedFrom); - - return ( - matchToModulePath(pRule, pModule, lGroups) && - matchToModulePathNot(pRule, pModule, lGroups) - ); - } - } - return false; -} - -/** - * Returns true if pRule is a 'reaches' rule and pModule matches the reachability - * criteria. - * Returns false in all other cases - * - * @param {import("../../types/rule-set.mjs").IAnyRuleType} pRule - * @param {import("../../types/cruise-result.mjs").IModule} pModule - * @returns {boolean} - */ -function matchesReachesRule(pRule, pModule) { - return ( - Object.hasOwn(pRule?.to ?? {}, "reachable") && - Object.hasOwn(pModule, "reaches") && - // @ts-expect-error the 'hasOwn' above guarantees the .reaches exists - pModule.reaches.some( - (pReaches) => - pReaches.asDefinedInRule === pRule.name && - pReaches.modules.some( - (pReachesModule) => - matchToModulePath(pRule, pReachesModule) && - matchToModulePathNot(pRule, pReachesModule), - ), - ) - ); -} -/** - * - * @param {import("../../types/rule-set.mjs").IAnyRuleType} pRule - * @param {string[]} pDependents - * @returns {boolean} - */ -function dependentsCountsMatch(pRule, pDependents) { - const lMatchingDependentsCount = pDependents.filter( - (pDependent) => - Boolean(!pRule.from.path || pDependent.match(pRule.from.path)) && - Boolean(!pRule.from.pathNot || !pDependent.match(pRule.from.pathNot)), - ).length; - return ( - (!pRule.module.numberOfDependentsLessThan || - lMatchingDependentsCount < pRule.module.numberOfDependentsLessThan) && - (!pRule.module.numberOfDependentsMoreThan || - lMatchingDependentsCount > pRule.module.numberOfDependentsMoreThan) - ); -} - -/** - * - * @param {import("../../types/rule-set.mjs").IAnyRuleType} pRule - * @param {import("../../types/cruise-result.mjs").IModule} pModule - * @returns {boolean} - */ -// eslint-disable-next-line complexity -function matchesDependentsRule(pRule, pModule) { - if ( - (Object.hasOwn(pModule, "dependents") && - Object.hasOwn(pRule?.module ?? {}, "numberOfDependentsLessThan")) || - Object.hasOwn(pRule?.module ?? {}, "numberOfDependentsMoreThan") - ) { - return ( - // group matching seems like a nice idea, however, the 'from' part of the - // rule is going to match not one module (as with regular dependency rules) - // but a whole bunch of them, being the 'dependents'. So that match is going - // to produce not one result, but one per matching dependent. To get meaningful - // results we'd probably have to loop over these and or the - // matchToModulePath together. - matchesModulePath(pRule, pModule) && - matchesModulePathNot(pRule, pModule) && - dependentsCountsMatch(pRule, pModule.dependents) - ); - } - return false; -} + matchesOrphanRule, + matchesReachableRule, + matchesReachesRule, + matchesDependentsRule, +} from "./match-module-rule-helpers.mjs"; /** * @@ -155,10 +28,6 @@ const isInteresting = (pRule) => isModuleOnlyRule(pRule) && !isFolderScope(pRule); export default { - matchesOrphanRule, - matchesReachableRule, - matchesReachesRule, - matchesDependentsRule, match, isInteresting, }; diff --git a/test/configs/no-orphans.spec.mjs b/test/configs/no-orphans.spec.mjs index 7328c08bd..3a5c14497 100644 --- a/test/configs/no-orphans.spec.mjs +++ b/test/configs/no-orphans.spec.mjs @@ -1,11 +1,11 @@ import { equal } from "node:assert/strict"; import noOrphansRule from "../../configs/rules/no-orphans.cjs"; -import matchModuleRule from "#validate/match-module-rule.mjs"; +import { matchesOrphanRule } from "#validate/match-module-rule-helpers.mjs"; describe("[I] configs/rules/no-orphans", () => { it("flags non-excepted orphans as orphan rule transgression", () => { equal( - matchModuleRule.matchesOrphanRule(noOrphansRule, { + matchesOrphanRule(noOrphansRule, { source: "Rémi.js", orphan: true, }), @@ -15,7 +15,7 @@ describe("[I] configs/rules/no-orphans", () => { it("flags files ending on a dotfile as orphan rule transgression", () => { equal( - matchModuleRule.matchesOrphanRule(noOrphansRule, { + matchesOrphanRule(noOrphansRule, { source: "looks-like-a-dot-sorta.Rémi.js", orphan: true, }), @@ -25,7 +25,7 @@ describe("[I] configs/rules/no-orphans", () => { it("does not flag dot files as orphan rule transgressions", () => { equal( - matchModuleRule.matchesOrphanRule(noOrphansRule, { + matchesOrphanRule(noOrphansRule, { source: ".Rémi.js", orphan: true, }), @@ -35,7 +35,7 @@ describe("[I] configs/rules/no-orphans", () => { it("does not flag dot files in the tree as orphan rule transgressions", () => { equal( - matchModuleRule.matchesOrphanRule(noOrphansRule, { + matchesOrphanRule(noOrphansRule, { source: "packages/thing/.Rémi.js", orphan: true, }), @@ -45,7 +45,7 @@ describe("[I] configs/rules/no-orphans", () => { it("does not flag dot files in the tree as orphan rule transgressions, regardless extension", () => { equal( - matchModuleRule.matchesOrphanRule(noOrphansRule, { + matchesOrphanRule(noOrphansRule, { source: "packages/thing/.Rémi.ts", orphan: true, }), @@ -55,14 +55,14 @@ describe("[I] configs/rules/no-orphans", () => { it("does not flag any .d.ts not as orphan rule transgressions", () => { equal( - matchModuleRule.matchesOrphanRule(noOrphansRule, { + matchesOrphanRule(noOrphansRule, { source: "packages/thing/types/lalalal.d.ts", orphan: true, }), false, ); equal( - matchModuleRule.matchesOrphanRule(noOrphansRule, { + matchesOrphanRule(noOrphansRule, { source: "lalalal.d.ts", orphan: true, }), @@ -72,7 +72,7 @@ describe("[I] configs/rules/no-orphans", () => { it("does not flag babel config files in the tree not as orphan rule transgressions", () => { equal( - matchModuleRule.matchesOrphanRule(noOrphansRule, { + matchesOrphanRule(noOrphansRule, { source: "packages/thing/babel.config.mjs", orphan: true, }), @@ -82,7 +82,7 @@ describe("[I] configs/rules/no-orphans", () => { it("does not flag babel config files as orphan rule transgressions", () => { equal( - matchModuleRule.matchesOrphanRule(noOrphansRule, { + matchesOrphanRule(noOrphansRule, { source: "babel.config.mjs", orphan: true, }), diff --git a/test/validate/match-module-rule.dependents.spec.mjs b/test/validate/match-module-rule.dependents.spec.mjs index 0ebfe6d2e..b94e437d7 100644 --- a/test/validate/match-module-rule.dependents.spec.mjs +++ b/test/validate/match-module-rule.dependents.spec.mjs @@ -1,5 +1,5 @@ import { equal } from "node:assert/strict"; -import matchModuleRule from "#validate/match-module-rule.mjs"; +import { matchesDependentsRule } from "#validate/match-module-rule-helpers.mjs"; const EMPTY_RULE = { from: {}, module: {} }; const ANY_DEPENDENTS = { @@ -55,26 +55,20 @@ const USED_FROM_SNACKBAR_BETWEEN = { describe("[I] validate/match-module-rule - dependents", () => { it("rule without dependents restriction doesn't flag (implicit)", () => { - equal(matchModuleRule.matchesDependentsRule(EMPTY_RULE, {}), false); + equal(matchesDependentsRule(EMPTY_RULE, {}), false); }); it("rule without dependents restriction doesn't flag (explicit)", () => { - equal( - matchModuleRule.matchesDependentsRule(EMPTY_RULE, { dependents: [] }), - false, - ); + equal(matchesDependentsRule(EMPTY_RULE, { dependents: [] }), false); }); it("rule with dependents doesn't match a module with no dependents attribute", () => { - equal(matchModuleRule.matchesDependentsRule(ANY_DEPENDENTS, {}), false); + equal(matchesDependentsRule(ANY_DEPENDENTS, {}), false); }); it("rule that matches any dependents does match a module with a dependents attribute", () => { - equal( - matchModuleRule.matchesDependentsRule(ANY_DEPENDENTS, { dependents: [] }), - true, - ); + equal(matchesDependentsRule(ANY_DEPENDENTS, { dependents: [] }), true); }); it("rule that matches any dependents does match a module with a dependents attribute (>1 dependent)", () => { equal( - matchModuleRule.matchesDependentsRule(ANY_DEPENDENTS, { + matchesDependentsRule(ANY_DEPENDENTS, { dependents: ["aap", "noot", "mies", "wim"], }), true, @@ -83,46 +77,37 @@ describe("[I] validate/match-module-rule - dependents", () => { it("must-share (>=2 dependents) rule doesn't flag when there's 2 dependents", () => { equal( - matchModuleRule.matchesDependentsRule( - MUST_BE_SHARED_DONT_CARE_FROM_WHERE, - { - source: "src/utensils/simsalabim.ts", - dependents: ["aap", "noot"], - }, - ), + matchesDependentsRule(MUST_BE_SHARED_DONT_CARE_FROM_WHERE, { + source: "src/utensils/simsalabim.ts", + dependents: ["aap", "noot"], + }), false, ); }); it("must-share (>=2 dependents) rule flags when there's 1 dependent", () => { equal( - matchModuleRule.matchesDependentsRule( - MUST_BE_SHARED_DONT_CARE_FROM_WHERE, - { - source: "src/utensils/simsalabim.ts", - dependents: ["aap"], - }, - ), + matchesDependentsRule(MUST_BE_SHARED_DONT_CARE_FROM_WHERE, { + source: "src/utensils/simsalabim.ts", + dependents: ["aap"], + }), true, ); }); it("must-share (>=2 dependents) rule flags when there's 0 dependents", () => { equal( - matchModuleRule.matchesDependentsRule( - MUST_BE_SHARED_DONT_CARE_FROM_WHERE, - { - source: "src/utensils/simsalabim.ts", - dependents: [], - }, - ), + matchesDependentsRule(MUST_BE_SHARED_DONT_CARE_FROM_WHERE, { + source: "src/utensils/simsalabim.ts", + dependents: [], + }), true, ); }); it("must-share (>=2 dependents) with a from doesn't flag when there's 2 dependents from that from", () => { equal( - matchModuleRule.matchesDependentsRule(MUST_BE_SHARED_FROM_SNACKBAR, { + matchesDependentsRule(MUST_BE_SHARED_FROM_SNACKBAR, { source: "src/utensils/frieten.ts", dependents: ["src/snackbar/kapsalon.ts", "src/snackbar/zijspan.ts"], }), @@ -132,7 +117,7 @@ describe("[I] validate/match-module-rule - dependents", () => { it("must-share (>=2 dependents) with a from doesn't flag when there's 2 dependents from that from (+ some others that don't matter)", () => { equal( - matchModuleRule.matchesDependentsRule(MUST_BE_SHARED_FROM_SNACKBAR, { + matchesDependentsRule(MUST_BE_SHARED_FROM_SNACKBAR, { source: "src/utensils/frieten.ts", dependents: [ "src/snackbar/kapsalon.ts", @@ -147,7 +132,7 @@ describe("[I] validate/match-module-rule - dependents", () => { it("must-share (>=2 dependents) with a from path when there's only 1 dependents from that from path", () => { equal( - matchModuleRule.matchesDependentsRule(MUST_BE_SHARED_FROM_SNACKBAR, { + matchesDependentsRule(MUST_BE_SHARED_FROM_SNACKBAR, { source: "src/utensils/frieten.ts", dependents: [ "src/snackbar/kapsalon.ts", @@ -160,67 +145,55 @@ describe("[I] validate/match-module-rule - dependents", () => { it("must-share (>=2 dependents) with a from pathNot when there's only 1 dependents from that from pathNot", () => { equal( - matchModuleRule.matchesDependentsRule( - MUST_BE_SHARED_BUT_NOT_FROM_SNACKBAR, - { - source: "src/utensils/frieten.ts", - dependents: [ - "src/snackbar/kapsalon.ts", - "src/fietsenwinkel/zijspan.ts", - ], - }, - ), + matchesDependentsRule(MUST_BE_SHARED_BUT_NOT_FROM_SNACKBAR, { + source: "src/utensils/frieten.ts", + dependents: [ + "src/snackbar/kapsalon.ts", + "src/fietsenwinkel/zijspan.ts", + ], + }), true, ); }); it("must-share (>=2 dependents) with a from pathNot when there's 0 dependents from that from pathNot", () => { equal( - matchModuleRule.matchesDependentsRule( - MUST_BE_SHARED_BUT_NOT_FROM_SNACKBAR, - { - source: "src/utensils/frieten.ts", - dependents: ["src/snackbar/kapsalon.ts", "src/snackbar/zijspan.ts"], - }, - ), + matchesDependentsRule(MUST_BE_SHARED_BUT_NOT_FROM_SNACKBAR, { + source: "src/utensils/frieten.ts", + dependents: ["src/snackbar/kapsalon.ts", "src/snackbar/zijspan.ts"], + }), true, ); }); it("must not be used more than 3 times from snackbar - happy scenario", () => { equal( - matchModuleRule.matchesDependentsRule( - CANNOT_BE_SHARED_MORE_THAN_THREE_TIMES, - { - source: "src/utensils/frieten.ts", - dependents: ["src/snackbar/kapsalon.ts", "src/snackbar/zijspan.ts"], - }, - ), + matchesDependentsRule(CANNOT_BE_SHARED_MORE_THAN_THREE_TIMES, { + source: "src/utensils/frieten.ts", + dependents: ["src/snackbar/kapsalon.ts", "src/snackbar/zijspan.ts"], + }), false, ); }); it("must not be used more than 3 times from snackbar - fail scenario", () => { equal( - matchModuleRule.matchesDependentsRule( - CANNOT_BE_SHARED_MORE_THAN_THREE_TIMES, - { - source: "src/utensils/frieten.ts", - dependents: [ - "src/snackbar/kapsalon.ts", - "src/snackbar/zijspan.ts", - "src/snackbar/dooierat.ts", - "src/snackbar/kipcorn.ts", - ], - }, - ), + matchesDependentsRule(CANNOT_BE_SHARED_MORE_THAN_THREE_TIMES, { + source: "src/utensils/frieten.ts", + dependents: [ + "src/snackbar/kapsalon.ts", + "src/snackbar/zijspan.ts", + "src/snackbar/dooierat.ts", + "src/snackbar/kipcorn.ts", + ], + }), false, ); }); it("combo breaker (3 < x < 5) - happy scenario", () => { equal( - matchModuleRule.matchesDependentsRule(USED_FROM_SNACKBAR_BETWEEN, { + matchesDependentsRule(USED_FROM_SNACKBAR_BETWEEN, { source: "src/utensils/frieten.ts", dependents: [ "src/snackbar/kapsalon.ts", @@ -235,7 +208,7 @@ describe("[I] validate/match-module-rule - dependents", () => { it("combo breaker (3 < x < 5) - fail scenario", () => { equal( - matchModuleRule.matchesDependentsRule(USED_FROM_SNACKBAR_BETWEEN, { + matchesDependentsRule(USED_FROM_SNACKBAR_BETWEEN, { source: "src/utensils/frieten.ts", dependents: [ "src/snackbar/kapsalon.ts", diff --git a/test/validate/match-module-rule.orphan.spec.mjs b/test/validate/match-module-rule.orphan.spec.mjs index b4dc1b156..1e10c5ff3 100644 --- a/test/validate/match-module-rule.orphan.spec.mjs +++ b/test/validate/match-module-rule.orphan.spec.mjs @@ -1,5 +1,6 @@ import { equal } from "node:assert/strict"; import matchModuleRule from "#validate/match-module-rule.mjs"; +import { matchesOrphanRule } from "#validate/match-module-rule-helpers.mjs"; const EMPTY_RULE = { from: {}, to: {} }; const ANY_ORPHAN = { from: { orphan: true }, to: {} }; @@ -8,32 +9,23 @@ const ORPHAN_IN_PATH_NOT = { from: { orphan: true, pathNot: "^src" }, to: {} }; describe("[I] validate/match-module-rule - orphan", () => { it("rule without orphan attribute doesn't non-orphans (implicit)", () => { - equal(matchModuleRule.matchesOrphanRule(EMPTY_RULE, {}), false); + equal(matchesOrphanRule(EMPTY_RULE, {}), false); }); it("rule without orphan attribute doesn't non-orphans (explicit)", () => { - equal( - matchModuleRule.matchesOrphanRule(EMPTY_RULE, { orphan: false }), - false, - ); + equal(matchesOrphanRule(EMPTY_RULE, { orphan: false }), false); }); it("rule without orphan attribute doesn't match orphan module", () => { - equal( - matchModuleRule.matchesOrphanRule(EMPTY_RULE, { orphan: true }), - false, - ); + equal(matchesOrphanRule(EMPTY_RULE, { orphan: true }), false); }); it("orphan match rule doesn't match non-orphans", () => { - equal(matchModuleRule.matchesOrphanRule(ANY_ORPHAN, {}), false); + equal(matchesOrphanRule(ANY_ORPHAN, {}), false); }); it("orphan match rule matches orphans", () => { - equal( - matchModuleRule.matchesOrphanRule(ANY_ORPHAN, { orphan: true }), - true, - ); + equal(matchesOrphanRule(ANY_ORPHAN, { orphan: true }), true); }); it("orphan match rule with path doesn't match orphans in other paths", () => { equal( - matchModuleRule.matchesOrphanRule(ORPHAN_IN_PATH, { + matchesOrphanRule(ORPHAN_IN_PATH, { orphan: true, source: "test/lalal.spec.ts", }), @@ -42,7 +34,7 @@ describe("[I] validate/match-module-rule - orphan", () => { }); it("orphan match rule with path matches orphans in that path", () => { equal( - matchModuleRule.matchesOrphanRule(ORPHAN_IN_PATH, { + matchesOrphanRule(ORPHAN_IN_PATH, { orphan: true, source: "src/lalal.ts", }), @@ -51,7 +43,7 @@ describe("[I] validate/match-module-rule - orphan", () => { }); it("orphan match rule with path matches orphans outside that path", () => { equal( - matchModuleRule.matchesOrphanRule(ORPHAN_IN_PATH_NOT, { + matchesOrphanRule(ORPHAN_IN_PATH_NOT, { orphan: true, source: "test/lalal.spec.ts", }), @@ -60,7 +52,7 @@ describe("[I] validate/match-module-rule - orphan", () => { }); it("orphan match rule with pathNot doesn't match orphans in that path", () => { equal( - matchModuleRule.matchesOrphanRule(ORPHAN_IN_PATH_NOT, { + matchesOrphanRule(ORPHAN_IN_PATH_NOT, { orphan: true, source: "src/lalal.ts", }), diff --git a/test/validate/match-module-rule.reachable.spec.mjs b/test/validate/match-module-rule.reachable.spec.mjs index 8431b2bf2..69abd3ab9 100644 --- a/test/validate/match-module-rule.reachable.spec.mjs +++ b/test/validate/match-module-rule.reachable.spec.mjs @@ -1,5 +1,5 @@ import { equal } from "node:assert/strict"; -import matchModuleRule from "#validate/match-module-rule.mjs"; +import { matchesReachableRule } from "#validate/match-module-rule-helpers.mjs"; const EMPTY_RULE = { from: {}, to: {} }; const ANY_UNREACHABLE = { @@ -25,22 +25,22 @@ const ANY_UNREACHABLE_IN_ALLOWED_SET = { describe("[I] validate/match-module-rule - reachable", () => { it("rule without reachable attribute doesn't match reachables (implicit)", () => { - equal(matchModuleRule.matchesReachableRule(EMPTY_RULE, {}), false); + equal(matchesReachableRule(EMPTY_RULE, {}), false); }); it("rule without reachable attribute doesn't match reachables (explicit)", () => { equal( - matchModuleRule.matchesReachableRule(EMPTY_RULE, { + matchesReachableRule(EMPTY_RULE, { reachable: [{ value: false, asDefinedInRule: "no-unreachable" }], }), false, ); }); it("rule with reachable attribute doesn't match reachables (implicit)", () => { - equal(matchModuleRule.matchesReachableRule(ANY_UNREACHABLE, {}), false); + equal(matchesReachableRule(ANY_UNREACHABLE, {}), false); }); it("rule with reachable attribute doesn't match reachables (explicit)", () => { equal( - matchModuleRule.matchesReachableRule(ANY_UNREACHABLE, { + matchesReachableRule(ANY_UNREACHABLE, { reachable: [{ value: true, asDefinedInRule: "no-unreachable" }], }), false, @@ -48,7 +48,7 @@ describe("[I] validate/match-module-rule - reachable", () => { }); it("rule with reachable attribute matches unreachables according to that rule name", () => { equal( - matchModuleRule.matchesReachableRule(ANY_UNREACHABLE, { + matchesReachableRule(ANY_UNREACHABLE, { reachable: [{ value: false, asDefinedInRule: "no-unreachable" }], }), true, @@ -56,7 +56,7 @@ describe("[I] validate/match-module-rule - reachable", () => { }); it("rule with reachable attribute does not match unreachables according to other rule name", () => { equal( - matchModuleRule.matchesReachableRule(ANY_UNREACHABLE, { + matchesReachableRule(ANY_UNREACHABLE, { reachable: [{ value: false, asDefinedInRule: "other-rule-name" }], }), false, @@ -64,7 +64,7 @@ describe("[I] validate/match-module-rule - reachable", () => { }); it("nameless rule with reachable attribute does not match unreachables according to other rule name", () => { equal( - matchModuleRule.matchesReachableRule(ANY_UNREACHABLE_IN_ALLOWED_SET, { + matchesReachableRule(ANY_UNREACHABLE_IN_ALLOWED_SET, { reachable: [{ value: false, asDefinedInRule: "other-rule-name" }], }), false, @@ -72,7 +72,7 @@ describe("[I] validate/match-module-rule - reachable", () => { }); it("nameless rule with reachable attribute matchs unreachables according to not-in-allowed", () => { equal( - matchModuleRule.matchesReachableRule(ANY_UNREACHABLE_IN_ALLOWED_SET, { + matchesReachableRule(ANY_UNREACHABLE_IN_ALLOWED_SET, { reachable: [{ value: false, asDefinedInRule: "not-in-allowed" }], }), true, @@ -80,7 +80,7 @@ describe("[I] validate/match-module-rule - reachable", () => { }); it("rule with reachable attribute & path matches unreachables according to that rule name in that path", () => { equal( - matchModuleRule.matchesReachableRule(ANY_UNREACHABLE_WITH_PATH, { + matchesReachableRule(ANY_UNREACHABLE_WITH_PATH, { source: "src/lalala.ts", reachable: [{ value: false, asDefinedInRule: "no-unreachable" }], }), @@ -89,7 +89,7 @@ describe("[I] validate/match-module-rule - reachable", () => { }); it("rule with reachable attribute & path does not match unreachables according to that rule name and not in that path", () => { equal( - matchModuleRule.matchesReachableRule(ANY_UNREACHABLE_WITH_PATH, { + matchesReachableRule(ANY_UNREACHABLE_WITH_PATH, { source: "test/lalala.ts", reachable: [{ value: false, asDefinedInRule: "no-unreachable" }], }), @@ -98,7 +98,7 @@ describe("[I] validate/match-module-rule - reachable", () => { }); it("rule with reachable attribute & path matches reachables according to that rule name in that path", () => { equal( - matchModuleRule.matchesReachableRule(ANY_REACHABLE_WITH_PATH, { + matchesReachableRule(ANY_REACHABLE_WITH_PATH, { source: "src/lalala.ts", reachable: [{ value: true, asDefinedInRule: "no-unreachable" }], }), @@ -107,7 +107,7 @@ describe("[I] validate/match-module-rule - reachable", () => { }); it("rule with reachable attribute & path does not match unreachables according to that rule name in that path (explicit)", () => { equal( - matchModuleRule.matchesReachableRule(ANY_REACHABLE_WITH_PATH, { + matchesReachableRule(ANY_REACHABLE_WITH_PATH, { source: "src/lalala.ts", reachable: [{ value: false, asDefinedInRule: "no-unreachable" }], }), @@ -116,7 +116,7 @@ describe("[I] validate/match-module-rule - reachable", () => { }); it("rule with reachable attribute & path does not match unreachables according to that rule name in that path (implicit)", () => { equal( - matchModuleRule.matchesReachableRule(ANY_REACHABLE_WITH_PATH, { + matchesReachableRule(ANY_REACHABLE_WITH_PATH, { source: "src/lalala.ts", }), false, diff --git a/test/validate/match-module-rule.reaches.spec.mjs b/test/validate/match-module-rule.reaches.spec.mjs index d03bcf98c..7f0369fb2 100644 --- a/test/validate/match-module-rule.reaches.spec.mjs +++ b/test/validate/match-module-rule.reaches.spec.mjs @@ -1,5 +1,5 @@ import { equal } from "node:assert/strict"; -import matchModuleRule from "#validate/match-module-rule.mjs"; +import { matchesReachesRule } from "#validate/match-module-rule-helpers.mjs"; const EMPTY_RULE = { from: {}, to: {} }; const ANY_REACHABLE = { @@ -15,11 +15,11 @@ const ANY_REACHES_IN_ALLOWED = { describe("[I] validate/match-module-rule - reaches", () => { it("rule without reachable attribute doesn't match modules with a reaches (implicit)", () => { - equal(matchModuleRule.matchesReachesRule(EMPTY_RULE, {}), false); + equal(matchesReachesRule(EMPTY_RULE, {}), false); }); it("rule without reachable attribute doesn't match modules with a reaches (explicit)", () => { equal( - matchModuleRule.matchesReachesRule(EMPTY_RULE, { + matchesReachesRule(EMPTY_RULE, { reaches: [ { modules: [{ source: "src/hoppetee.js" }], @@ -32,7 +32,7 @@ describe("[I] validate/match-module-rule - reaches", () => { }); it("rule without reachable attribute matches modules with a reaches (explicit)", () => { equal( - matchModuleRule.matchesReachesRule(ANY_REACHABLE, { + matchesReachesRule(ANY_REACHABLE, { reaches: [ { modules: [{ source: "src/hoppetee.js" }], @@ -45,7 +45,7 @@ describe("[I] validate/match-module-rule - reaches", () => { }); it("rule without reachable attribute matches modules with a reaches (explicit, nameless rule)", () => { equal( - matchModuleRule.matchesReachesRule(ANY_REACHES_IN_ALLOWED, { + matchesReachesRule(ANY_REACHES_IN_ALLOWED, { reaches: [ { modules: [{ source: "src/hoppetee.js" }],