Skip to content

Commit

Permalink
feat(validate): adds validation of required rules with 'reachable' at…
Browse files Browse the repository at this point in the history
…tributes (for transitive dependencies)
  • Loading branch information
sverweij committed Dec 10, 2024
1 parent 64957b9 commit e984d85
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/validate/violates-required-rule.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
matchesModulePath,
matchesModulePathNot,
} from "./matchers.mjs";
import { matchesReachesRule } from "./match-module-rule-helpers.mjs";
import { extractGroups } from "#utl/regex-util.mjs";

/**
Expand All @@ -20,11 +21,16 @@ export default function violatesRequiredRule(pRule, pModule) {
matchesModulePath(pRule, pModule) &&
matchesModulePathNot(pRule, pModule)
) {
const lGroups = extractGroups(pRule.module, pModule.source);
if (pRule.to.reachable) {
lReturnValue = !matchesReachesRule(pRule, pModule);
}

lReturnValue = !pModule.dependencies.some((pDependency) =>
matchesToPath(pRule, pDependency, lGroups),
);
if (lReturnValue || !pRule.to.reachable) {
const lGroups = extractGroups(pRule.module, pModule.source);
lReturnValue = !pModule.dependencies.some((pDependency) =>
matchesToPath(pRule, pDependency, lGroups),
);
}
}
return lReturnValue;
}
119 changes: 118 additions & 1 deletion test/validate/index.required-rules.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { deepEqual } from "node:assert/strict";
import parseRuleSet from "./parse-ruleset.utl.mjs";
import { validateDependency, validateModule } from "#validate/index.mjs";

describe("[I] validate/index - required rules", () => {
describe("[I] validate/index - required rules - direct dependencies", () => {
const lRequiredRuleSet = parseRuleSet({
required: [
{
Expand Down Expand Up @@ -101,3 +101,120 @@ describe("[I] validate/index - required rules", () => {
);
});
});

describe("[I] validate/index - required rules - transitive dependencies ('reachable')", () => {
const lRequiredRuleSet = parseRuleSet({
required: [
{
name: "thou-shalt-inherit-from-base",
module: {
path: ".+-controller\\.ts$",
pathNot: "random-trials",
},
to: {
path: "src/base-controller/index\\.ts$",
reachable: true,
},
},
],
});

it("modules not matching the module criteria from the required rule are okeliedokelie", () => {
deepEqual(
validateModule(lRequiredRuleSet, {
source: "something",
}),
{ valid: true },
);
});

it("modules matching the module criteria with no dependencies bork", () => {
deepEqual(
validateModule(lRequiredRuleSet, {
source: "grub-controller.ts",
dependencies: [],
}),
{
rules: [{ name: "thou-shalt-inherit-from-base", severity: "warn" }],
valid: false,
},
);
});

it("modules matching the module criteria with no matching dependencies bork", () => {
deepEqual(
validateModule(lRequiredRuleSet, {
source: "grub-controller.ts",
dependencies: [
{
resolved: "src/not-the-base-controller.ts",
},
{
resolved: "src/not-the-base-controller-either.ts",
},
],
}),
{
rules: [{ name: "thou-shalt-inherit-from-base", severity: "warn" }],
valid: false,
},
);
});

it("modules that transitively depend on the required module are okeliedokelie", () => {
deepEqual(
validateModule(lRequiredRuleSet, {
source: "grub-controller.ts",
dependencies: [
{
resolved: "src/not-the-base-controller.ts",
},
// {
// resolved: "src/base-controller/index.ts",
// },
{
resolved: "src/not-the-base-controller-either.ts",
},
],
reaches: [
{
asDefinedInRule: "thou-shalt-inherit-from-base",
modules: [
{
source: "src/base-controller/index.ts",
via: [
{
name: "src/not-the-base-controller.ts",
dependencyTypes: ["local", "import"],
},
],
},
],
},
],
}),
{
valid: true,
},
);
});

it("'required' violations don't get flagged as dependency transgressions", () => {
deepEqual(
validateDependency(lRequiredRuleSet, {
source: "grub-controller.ts",
dependencies: [
{
resolved: "src/not-the-base-controller.ts",
},
{
resolved: "src/not-the-base-controller-either.ts",
},
],
}),
{
valid: true,
},
);
});
});

0 comments on commit e984d85

Please sign in to comment.