Skip to content

Commit

Permalink
fix(validate): in required rules also take the module _itself_ as a d…
Browse files Browse the repository at this point in the history
…ependency
  • Loading branch information
sverweij committed Dec 11, 2024
1 parent fb99edc commit fcba072
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/validate/violates-required-rule.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
matchesToPath,
matchesModulePath,
matchesModulePathNot,
matchToModulePath,
} from "./matchers.mjs";
import { matchesReachesRule } from "./match-module-rule-helpers.mjs";
import { extractGroups } from "#utl/regex-util.mjs";
Expand All @@ -27,9 +28,12 @@ export default function violatesRequiredRule(pRule, pModule) {

if (lReturnValue || !pRule.to.reachable) {
const lGroups = extractGroups(pRule.module, pModule.source);
lReturnValue = !pModule.dependencies.some((pDependency) =>
matchesToPath(pRule, pDependency, lGroups),
);
const lMatchesSelf = matchToModulePath(pRule, pModule, lGroups);
lReturnValue =
!lMatchesSelf &&
!pModule.dependencies.some((pDependency) =>
matchesToPath(pRule, pDependency, lGroups),
);
}
}
return lReturnValue;
Expand Down
34 changes: 29 additions & 5 deletions test/validate/index.required-rules.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ describe("[I] validate/index - required rules - transitive dependencies ('reacha
{
name: "thou-shalt-inherit-from-base",
module: {
path: ".+-controller\\.ts$",
path: ".+-controller[.]ts$",
pathNot: "random-trials",
},
to: {
path: "src/base-controller/index\\.ts$",
path: "src/base-controller/index[.]ts$",
reachable: true,
},
},
Expand Down Expand Up @@ -169,9 +169,6 @@ describe("[I] validate/index - required rules - transitive dependencies ('reacha
{
resolved: "src/not-the-base-controller.ts",
},
// {
// resolved: "src/base-controller/index.ts",
// },
{
resolved: "src/not-the-base-controller-either.ts",
},
Expand Down Expand Up @@ -199,6 +196,33 @@ describe("[I] validate/index - required rules - transitive dependencies ('reacha
);
});

it("the module itself doesn't get flagged as a transgression", () => {
const lRuleSet = parseRuleSet({
required: [
{
name: "do-depend-on-the-base-controller",
module: {
path: "src/",
},
to: {
path: "src/base-controller/index[.]ts$",
reachable: true,
},
},
],
});
deepEqual(
validateModule(lRuleSet, {
source: "src/base-controller/index.ts",
dependencies: [],
reaches: [],
}),
{
valid: true,
},
);
});

it("'required' violations don't get flagged as dependency transgressions", () => {
deepEqual(
validateDependency(lRequiredRuleSet, {
Expand Down

0 comments on commit fcba072

Please sign in to comment.