@hublo/sentinel 1.7.0-alpha.12 → 1.7.0-alpha.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/sentinel.js +1 -1
- package/dist/{chunk-2ANSMW2Y.js → chunk-J7FBC7X7.js} +624 -58
- package/dist/index.js +1 -1
- package/docs/arch-adoption.md +26 -1
- package/package.json +1 -1
package/dist/bin/sentinel.js
CHANGED
|
@@ -639,11 +639,29 @@ var defaults2 = shared_default2;
|
|
|
639
639
|
// src/roles/arch/rules/rule-set.ts
|
|
640
640
|
import { readFileSync as readFileSync2 } from "node:fs";
|
|
641
641
|
var SEVERITIES = ["error", "warn"];
|
|
642
|
+
var CONDITIONS = [
|
|
643
|
+
"to",
|
|
644
|
+
"file",
|
|
645
|
+
"bindings",
|
|
646
|
+
"class",
|
|
647
|
+
"imports",
|
|
648
|
+
"inbound",
|
|
649
|
+
"directory"
|
|
650
|
+
];
|
|
642
651
|
function driverOf(rule) {
|
|
643
|
-
|
|
644
|
-
if (
|
|
652
|
+
const to = rule.to;
|
|
653
|
+
if (to === void 0) {
|
|
654
|
+
for (const key of CONDITIONS) if (key !== "to" && rule[key] !== void 0) return key;
|
|
655
|
+
return "forbidden-import";
|
|
656
|
+
}
|
|
657
|
+
if ("names" in to) return "forbidden-binding";
|
|
658
|
+
if ("classNames" in to) return "forbidden-class-name";
|
|
659
|
+
if ("calls" in to || "module" in to) return "forbidden-call";
|
|
645
660
|
return "forbidden-import";
|
|
646
661
|
}
|
|
662
|
+
function needsSlices(rule) {
|
|
663
|
+
return rule.to !== void 0 && "slice" in rule.to || rule.bindings?.binds === "own-slice" || rule.inbound !== void 0 || rule.directory?.under === "slice";
|
|
664
|
+
}
|
|
647
665
|
function asList(value) {
|
|
648
666
|
if (value === void 0) return [];
|
|
649
667
|
return Array.isArray(value) ? value : [value];
|
|
@@ -674,6 +692,7 @@ function ruleSetProblems(set) {
|
|
|
674
692
|
if (candidate.exclude !== void 0 && !Array.isArray(candidate.exclude))
|
|
675
693
|
problems.push("`exclude` must be a list of regular expressions");
|
|
676
694
|
else problems.push(...regexProblems("exclude", candidate.exclude ?? []));
|
|
695
|
+
problems.push(...sliceProblems(candidate.slices));
|
|
677
696
|
const ruleNames = /* @__PURE__ */ new Set();
|
|
678
697
|
for (const [index, rule] of (candidate.forbidden ?? []).entries()) {
|
|
679
698
|
const where = `forbidden[${index}]${typeof rule?.name === "string" ? ` (${rule.name})` : ""}`;
|
|
@@ -684,10 +703,173 @@ function ruleSetProblems(set) {
|
|
|
684
703
|
if (!SEVERITIES.includes(rule?.severity))
|
|
685
704
|
problems.push(`${where}: \`severity\` must be one of ${SEVERITIES.join(", ")}`);
|
|
686
705
|
if (!isObject(rule?.from)) problems.push(`${where}: \`from\` is missing`);
|
|
687
|
-
|
|
688
|
-
if (
|
|
689
|
-
|
|
690
|
-
|
|
706
|
+
const conditions = CONDITIONS.filter((key) => rule?.[key] !== void 0);
|
|
707
|
+
if (conditions.length !== 1) {
|
|
708
|
+
problems.push(
|
|
709
|
+
conditions.length === 0 ? `${where}: say what it forbids, under one of ${CONDITIONS.map((key) => `\`${key}\``).join(", ")}` : `${where}: ${conditions.map((key) => `\`${key}\``).join(" and ")} cannot go together: one condition per rule`
|
|
710
|
+
);
|
|
711
|
+
continue;
|
|
712
|
+
}
|
|
713
|
+
if (!isObject(rule.from)) continue;
|
|
714
|
+
const condition = conditions[0];
|
|
715
|
+
if (!isObject(rule[condition])) {
|
|
716
|
+
problems.push(`${where}: \`${condition}\` must be an object`);
|
|
717
|
+
continue;
|
|
718
|
+
}
|
|
719
|
+
if (condition === "directory") {
|
|
720
|
+
if (rule.from.role !== void 0)
|
|
721
|
+
problems.push(`${where}.from: a directory has no role, give a \`path\``);
|
|
722
|
+
problems.push(
|
|
723
|
+
...regexProblems(`${where}.from`, [
|
|
724
|
+
...asList(rule.from.path),
|
|
725
|
+
...asList(rule.from.pathNot)
|
|
726
|
+
])
|
|
727
|
+
);
|
|
728
|
+
} else problems.push(...endpointProblems(`${where}.from`, rule.from, roleNames));
|
|
729
|
+
problems.push(...conditionProblems(where, rule, condition, roleNames));
|
|
730
|
+
if (needsSlices(rule) && (candidate.slices ?? []).length === 0)
|
|
731
|
+
problems.push(`${where}: it reads slices, and the rule set declares no \`slices\``);
|
|
732
|
+
}
|
|
733
|
+
return problems;
|
|
734
|
+
}
|
|
735
|
+
function countProblems(where, count, roles) {
|
|
736
|
+
if (!isObject(count.to)) return [`${where}: \`to\` is missing`];
|
|
737
|
+
const problems = endpointProblems(`${where}.to`, count.to, roles);
|
|
738
|
+
for (const bound of ["min", "max"]) {
|
|
739
|
+
const value = count[bound];
|
|
740
|
+
if (value !== void 0 && (!Number.isInteger(value) || value < 0))
|
|
741
|
+
problems.push(`${where}: \`${bound}\` must be a whole number, 0 or more`);
|
|
742
|
+
}
|
|
743
|
+
if (count.min === void 0 && count.max === void 0)
|
|
744
|
+
problems.push(`${where}: give \`min\`, \`max\` or both`);
|
|
745
|
+
if (count.min !== void 0 && count.max !== void 0 && count.min > count.max)
|
|
746
|
+
problems.push(`${where}: \`min\` is above \`max\`, so every file would fail`);
|
|
747
|
+
return problems;
|
|
748
|
+
}
|
|
749
|
+
function keysProblems(where, value, allowed, atLeastOne = true) {
|
|
750
|
+
const stray = Object.keys(value).filter((key) => !allowed.includes(key));
|
|
751
|
+
if (stray.length > 0)
|
|
752
|
+
return [
|
|
753
|
+
`${where}: ${stray.map((key) => `\`${key}\``).join(", ")} is not one of ${allowed.map((key) => `\`${key}\``).join(", ")}`
|
|
754
|
+
];
|
|
755
|
+
return atLeastOne && Object.keys(value).length === 0 ? [`${where}: give one of ${allowed.map((key) => `\`${key}\``).join(", ")}`] : [];
|
|
756
|
+
}
|
|
757
|
+
function fileProblems(where, file) {
|
|
758
|
+
const keys = keysProblems(where, file, [
|
|
759
|
+
"name",
|
|
760
|
+
"exportsItsName",
|
|
761
|
+
"exportStar",
|
|
762
|
+
"allowed",
|
|
763
|
+
"interfaceAndSymbol"
|
|
764
|
+
]);
|
|
765
|
+
if (keys.length > 0) return keys;
|
|
766
|
+
if (Object.keys(file).length !== 1) return [`${where}: one requirement per rule`];
|
|
767
|
+
const problems = [];
|
|
768
|
+
if (file.name !== void 0) problems.push(...regexProblems(where, [file.name]));
|
|
769
|
+
if (file.exportStar !== void 0 && file.exportStar !== false)
|
|
770
|
+
problems.push(`${where}: \`exportStar\` can only be false`);
|
|
771
|
+
if (file.allowed !== void 0 && file.allowed !== false)
|
|
772
|
+
problems.push(`${where}: \`allowed\` can only be false`);
|
|
773
|
+
if (file.interfaceAndSymbol !== void 0 && file.interfaceAndSymbol !== true)
|
|
774
|
+
problems.push(`${where}: \`interfaceAndSymbol\` can only be true`);
|
|
775
|
+
const named = file.exportsItsName;
|
|
776
|
+
if (named !== void 0) {
|
|
777
|
+
if (!isObject(named)) return [`${where}: \`exportsItsName\` must be an object`];
|
|
778
|
+
problems.push(
|
|
779
|
+
...keysProblems(`${where}.exportsItsName`, named, ["strip", "case", "append"], false)
|
|
780
|
+
);
|
|
781
|
+
if (typeof named.strip !== "string")
|
|
782
|
+
problems.push(`${where}.exportsItsName: \`strip\` is missing`);
|
|
783
|
+
else problems.push(...regexProblems(`${where}.exportsItsName`, [named.strip]));
|
|
784
|
+
if (named.case !== "same" && named.case !== "pascal")
|
|
785
|
+
problems.push(`${where}.exportsItsName: \`case\` must be "same" or "pascal"`);
|
|
786
|
+
if (named.append !== void 0 && typeof named.append !== "string")
|
|
787
|
+
problems.push(`${where}.exportsItsName: \`append\` must be a string`);
|
|
788
|
+
}
|
|
789
|
+
return problems;
|
|
790
|
+
}
|
|
791
|
+
function classProblems(where, shape, roles) {
|
|
792
|
+
const keys = keysProblems(where, shape, ["implements", "injects", "publicMethods"]);
|
|
793
|
+
if (keys.length > 0) return keys;
|
|
794
|
+
const problems = [];
|
|
795
|
+
for (const key of ["implements", "injects"]) {
|
|
796
|
+
const count = shape[key];
|
|
797
|
+
if (count === void 0) continue;
|
|
798
|
+
if (!isObject(count)) problems.push(`${where}.${key} must be an object`);
|
|
799
|
+
else problems.push(...countProblems(`${where}.${key}`, count, roles));
|
|
800
|
+
}
|
|
801
|
+
if (shape.publicMethods !== void 0) {
|
|
802
|
+
const names = nameListProblem(where, "publicMethods", shape.publicMethods);
|
|
803
|
+
if (names !== void 0) problems.push(names.replace(".to:", ":"));
|
|
804
|
+
}
|
|
805
|
+
return problems;
|
|
806
|
+
}
|
|
807
|
+
function importsProblems(where, imports, roles) {
|
|
808
|
+
const keys = keysProblems(where, imports, ["count", "all"]);
|
|
809
|
+
if (keys.length > 0) return keys;
|
|
810
|
+
if (Object.keys(imports).length !== 1) return [`${where}: \`count\` or \`all\`, not both`];
|
|
811
|
+
if (imports.count !== void 0)
|
|
812
|
+
return isObject(imports.count) ? countProblems(`${where}.count`, imports.count, roles) : [`${where}.count must be an object`];
|
|
813
|
+
if (!Array.isArray(imports.all) || imports.all.length < 2)
|
|
814
|
+
return [`${where}.all: list at least two kinds of file, or use \`to\``];
|
|
815
|
+
return imports.all.flatMap(
|
|
816
|
+
(endpoint, index) => isObject(endpoint) ? endpointProblems(`${where}.all[${index}]`, endpoint, roles) : [`${where}.all[${index}] must be an object`]
|
|
817
|
+
);
|
|
818
|
+
}
|
|
819
|
+
function directoryProblems(where, directory) {
|
|
820
|
+
const keys = keysProblems(where, directory, ["names", "under", "withSibling"]);
|
|
821
|
+
if (keys.length > 0) return keys;
|
|
822
|
+
const problems = [];
|
|
823
|
+
const names = nameListProblem(where, "names", directory.names);
|
|
824
|
+
if (names !== void 0) problems.push(names.replace(".to:", ":"));
|
|
825
|
+
if (directory.under !== void 0 && directory.under !== "slice")
|
|
826
|
+
problems.push(`${where}: \`under\` can only be "slice"`);
|
|
827
|
+
if (directory.withSibling !== void 0) {
|
|
828
|
+
const siblings = nameListProblem(where, "withSibling", directory.withSibling);
|
|
829
|
+
if (siblings !== void 0) problems.push(siblings.replace(".to:", ":"));
|
|
830
|
+
}
|
|
831
|
+
return problems;
|
|
832
|
+
}
|
|
833
|
+
function conditionProblems(where, rule, condition, roles) {
|
|
834
|
+
switch (condition) {
|
|
835
|
+
case "to":
|
|
836
|
+
return targetProblems(where, rule, roles);
|
|
837
|
+
case "file":
|
|
838
|
+
return fileProblems(`${where}.file`, rule.file);
|
|
839
|
+
case "bindings": {
|
|
840
|
+
const binds = rule.bindings.binds;
|
|
841
|
+
return [
|
|
842
|
+
...keysProblems(`${where}.bindings`, rule.bindings, ["binds"]),
|
|
843
|
+
...binds === "nothing" || binds === "own-slice" ? [] : [`${where}.bindings: \`binds\` must be "nothing" or "own-slice"`]
|
|
844
|
+
];
|
|
845
|
+
}
|
|
846
|
+
case "class":
|
|
847
|
+
return classProblems(`${where}.class`, rule.class, roles);
|
|
848
|
+
case "imports":
|
|
849
|
+
return importsProblems(`${where}.imports`, rule.imports, roles);
|
|
850
|
+
case "inbound": {
|
|
851
|
+
const inbound = rule.inbound;
|
|
852
|
+
const min = inbound.slices?.min;
|
|
853
|
+
return [
|
|
854
|
+
...keysProblems(`${where}.inbound`, inbound, ["slices"]),
|
|
855
|
+
...Number.isInteger(min) && min >= 1 ? [] : [`${where}.inbound: \`slices.min\` must be a whole number, 1 or more`]
|
|
856
|
+
];
|
|
857
|
+
}
|
|
858
|
+
case "directory":
|
|
859
|
+
return directoryProblems(`${where}.directory`, rule.directory);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
function sliceProblems(slices) {
|
|
863
|
+
if (slices === void 0) return [];
|
|
864
|
+
if (!Array.isArray(slices)) return ["`slices` must be a list of regular expressions"];
|
|
865
|
+
const problems = regexProblems("slices", slices);
|
|
866
|
+
if (problems.length > 0) return problems;
|
|
867
|
+
for (const pattern of slices) {
|
|
868
|
+
const groups = (new RegExp(`${pattern}|`).exec("")?.length ?? 1) - 1;
|
|
869
|
+
if (groups !== 1)
|
|
870
|
+
problems.push(
|
|
871
|
+
`slices: "${pattern}" needs exactly one capture group, the slice root, and has ${groups}`
|
|
872
|
+
);
|
|
691
873
|
}
|
|
692
874
|
return problems;
|
|
693
875
|
}
|
|
@@ -707,7 +889,8 @@ function endpointProblems(where, endpoint, roles) {
|
|
|
707
889
|
var TARGET_KEYS = {
|
|
708
890
|
"forbidden-binding": ["module", "names"],
|
|
709
891
|
"forbidden-call": ["calls", "module"],
|
|
710
|
-
"forbidden-
|
|
892
|
+
"forbidden-class-name": ["classNames"],
|
|
893
|
+
"forbidden-import": ["role", "path", "pathNot", "dependencyTypesNot", "slice"]
|
|
711
894
|
};
|
|
712
895
|
function nameListProblem(where, key, value) {
|
|
713
896
|
if (!Array.isArray(value) || value.length === 0)
|
|
@@ -742,17 +925,22 @@ function callProblems(where, to) {
|
|
|
742
925
|
function targetProblems(where, rule, roles) {
|
|
743
926
|
const to = rule.to;
|
|
744
927
|
const kind = driverOf(rule);
|
|
745
|
-
const
|
|
928
|
+
const allowed = TARGET_KEYS[kind] ?? [];
|
|
929
|
+
const stray = Object.keys(to).filter((key) => !allowed.includes(key));
|
|
746
930
|
if (stray.length > 0)
|
|
747
931
|
return [
|
|
748
|
-
`${where}.to: ${stray.map((key) => `\`${key}\``).join(", ")} cannot go with ${
|
|
932
|
+
`${where}.to: ${stray.map((key) => `\`${key}\``).join(", ")} cannot go with ${allowed.map((key) => `\`${key}\``).join("/")}: one kind of rule per entry`
|
|
749
933
|
];
|
|
750
934
|
switch (kind) {
|
|
751
935
|
case "forbidden-binding":
|
|
752
936
|
return bindingProblems(where, to);
|
|
753
937
|
case "forbidden-call":
|
|
754
938
|
return callProblems(where, to);
|
|
939
|
+
case "forbidden-class-name":
|
|
940
|
+
return typeof to.classNames === "string" && to.classNames !== "" ? regexProblems(`${where}.to`, [to.classNames]) : [`${where}.to: \`classNames\` must be a regular expression`];
|
|
755
941
|
default: {
|
|
942
|
+
if (to.slice !== void 0 && to.slice !== "other")
|
|
943
|
+
return [`${where}.to: \`slice\` can only be "other"`];
|
|
756
944
|
const problems = endpointProblems(`${where}.to`, to, roles);
|
|
757
945
|
for (const type of to.dependencyTypesNot ?? []) {
|
|
758
946
|
if (type !== "type-only")
|
|
@@ -988,6 +1176,9 @@ function adopt2(exported) {
|
|
|
988
1176
|
// src/roles/arch/kernel/analyse.ts
|
|
989
1177
|
import { relative as relative3, sep as sep3 } from "node:path";
|
|
990
1178
|
|
|
1179
|
+
// src/roles/arch/drivers/drivers.ts
|
|
1180
|
+
import { basename, dirname as dirname2 } from "node:path/posix";
|
|
1181
|
+
|
|
991
1182
|
// src/roles/arch/kernel/roles.ts
|
|
992
1183
|
var UNCLASSIFIED = "unclassified";
|
|
993
1184
|
var compiled = /* @__PURE__ */ new Map();
|
|
@@ -1019,15 +1210,42 @@ function endpointMatches(endpoint, role, path2) {
|
|
|
1019
1210
|
}
|
|
1020
1211
|
|
|
1021
1212
|
// src/roles/arch/drivers/drivers.ts
|
|
1022
|
-
function violation(rule, file, to) {
|
|
1213
|
+
function violation(rule, file, to, detail) {
|
|
1023
1214
|
return {
|
|
1024
1215
|
rule: rule.name,
|
|
1025
1216
|
severity: rule.severity,
|
|
1026
1217
|
from: file.path,
|
|
1027
1218
|
to,
|
|
1028
|
-
...rule.source === void 0 ? {} : { source: rule.source }
|
|
1219
|
+
...rule.source === void 0 ? {} : { source: rule.source },
|
|
1220
|
+
...detail === void 0 ? {} : { detail }
|
|
1029
1221
|
};
|
|
1030
1222
|
}
|
|
1223
|
+
function resolveLocal(file, written) {
|
|
1224
|
+
const local = written.split(".")[0] ?? written;
|
|
1225
|
+
const binding = file.facts.locals.get(local);
|
|
1226
|
+
if (binding === void 0) {
|
|
1227
|
+
const declared = file.facts.classes.some((entry2) => entry2.name === local) || file.facts.interfaces.has(local) || file.facts.symbols.has(local);
|
|
1228
|
+
return declared ? {
|
|
1229
|
+
workspacePath: file.workspacePath,
|
|
1230
|
+
modulePath: file.path,
|
|
1231
|
+
role: file.role,
|
|
1232
|
+
typeOnly: false,
|
|
1233
|
+
...file.slice === void 0 ? {} : { slice: file.slice }
|
|
1234
|
+
} : void 0;
|
|
1235
|
+
}
|
|
1236
|
+
const entry = file.imports.find((candidate) => candidate.specifier === binding.specifier);
|
|
1237
|
+
return entry?.byName.get(binding.imported) ?? entry?.byName.get("*");
|
|
1238
|
+
}
|
|
1239
|
+
function outside(count, found) {
|
|
1240
|
+
return count.min !== void 0 && found < count.min || count.max !== void 0 && found > count.max;
|
|
1241
|
+
}
|
|
1242
|
+
function bounds(count) {
|
|
1243
|
+
if (count.min !== void 0 && count.min === count.max) return `exactly ${count.min}`;
|
|
1244
|
+
if (count.max === void 0) return `at least ${count.min}`;
|
|
1245
|
+
if (count.min === void 0) return `at most ${count.max}`;
|
|
1246
|
+
return `${count.min} to ${count.max}`;
|
|
1247
|
+
}
|
|
1248
|
+
var targetMatches = (endpoint, target) => endpointMatches(endpoint, target.role, target.modulePath);
|
|
1031
1249
|
var forbiddenImport = (rule, file) => {
|
|
1032
1250
|
const to = rule.to;
|
|
1033
1251
|
const skipTypes = (to.dependencyTypesNot ?? []).includes("type-only");
|
|
@@ -1036,6 +1254,8 @@ var forbiddenImport = (rule, file) => {
|
|
|
1036
1254
|
for (const target of entry.targets) {
|
|
1037
1255
|
if (skipTypes && (entry.typeOnly || target.typeOnly)) continue;
|
|
1038
1256
|
if (!endpointMatches(to, target.role, target.modulePath)) continue;
|
|
1257
|
+
if (to.slice === "other" && (file.slice === void 0 || target.slice === void 0)) continue;
|
|
1258
|
+
if (to.slice === "other" && file.slice === target.slice) continue;
|
|
1039
1259
|
found.push(violation(rule, file, target.workspacePath));
|
|
1040
1260
|
}
|
|
1041
1261
|
}
|
|
@@ -1068,20 +1288,203 @@ var forbiddenCall = (rule, file) => {
|
|
|
1068
1288
|
}
|
|
1069
1289
|
return found;
|
|
1070
1290
|
};
|
|
1291
|
+
var forbiddenClassName = (rule, file) => {
|
|
1292
|
+
const pattern = new RegExp(rule.to.classNames);
|
|
1293
|
+
const tokens = /* @__PURE__ */ new Set();
|
|
1294
|
+
for (const written of file.facts.classNames) {
|
|
1295
|
+
for (const token of written.split(/\s+/))
|
|
1296
|
+
if (token !== "" && pattern.test(token)) tokens.add(token);
|
|
1297
|
+
}
|
|
1298
|
+
if (tokens.size === 0) return [];
|
|
1299
|
+
const shown = [...tokens].slice(0, 3).join(" ");
|
|
1300
|
+
return [violation(rule, file, "utility classes", tokens.size > 3 ? `${shown} \u2026` : shown)];
|
|
1301
|
+
};
|
|
1302
|
+
function nameFrom(stem, wordCase) {
|
|
1303
|
+
if (wordCase === "same") return stem;
|
|
1304
|
+
return stem.split(/[-._\s]+/).filter((word) => word !== "").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
1305
|
+
}
|
|
1306
|
+
var fileShape = (rule, file) => {
|
|
1307
|
+
const want = rule.file ?? {};
|
|
1308
|
+
const name = basename(file.path);
|
|
1309
|
+
if (want.name !== void 0)
|
|
1310
|
+
return new RegExp(want.name).test(name) ? [] : [violation(rule, file, `file name ${name}`)];
|
|
1311
|
+
if (want.allowed === false) return [violation(rule, file, "a file that does not belong here")];
|
|
1312
|
+
if (want.exportStar === false)
|
|
1313
|
+
return file.facts.reexports.filter((entry) => entry.names === "*" && entry.namespace === void 0).map((entry) => violation(rule, file, `export * from '${entry.specifier}'`));
|
|
1314
|
+
if (want.exportsItsName !== void 0) {
|
|
1315
|
+
const { strip, append = "" } = want.exportsItsName;
|
|
1316
|
+
const stem = name.replace(new RegExp(strip), "");
|
|
1317
|
+
const expected = `${nameFrom(stem, want.exportsItsName.case)}${append}`;
|
|
1318
|
+
const reexported = file.facts.reexports.some(
|
|
1319
|
+
(entry) => entry.names !== "*" && entry.names.has(expected)
|
|
1320
|
+
);
|
|
1321
|
+
const exported = file.facts.localExports.has(expected) || file.facts.defaultName === expected || reexported;
|
|
1322
|
+
return exported ? [] : [violation(rule, file, `exports no ${expected}`)];
|
|
1323
|
+
}
|
|
1324
|
+
if (want.interfaceAndSymbol === true) {
|
|
1325
|
+
const { interfaces, symbols, localExports } = file.facts;
|
|
1326
|
+
const paired = [...interfaces].some((entry) => symbols.has(entry) && localExports.has(entry));
|
|
1327
|
+
return paired ? [] : [violation(rule, file, "no exported interface and Symbol of one name")];
|
|
1328
|
+
}
|
|
1329
|
+
return [];
|
|
1330
|
+
};
|
|
1331
|
+
var bindingsDriver = (rule, file) => {
|
|
1332
|
+
const found = [];
|
|
1333
|
+
for (const binding of file.facts.bindings) {
|
|
1334
|
+
if (rule.bindings?.binds === "nothing") {
|
|
1335
|
+
found.push(violation(rule, file, `binds ${binding.provide}`, `to ${binding.useClass}`));
|
|
1336
|
+
continue;
|
|
1337
|
+
}
|
|
1338
|
+
const token = resolveLocal(file, binding.provide);
|
|
1339
|
+
if (token?.slice === void 0 || file.slice === void 0 || token.slice === file.slice)
|
|
1340
|
+
continue;
|
|
1341
|
+
found.push(
|
|
1342
|
+
violation(
|
|
1343
|
+
rule,
|
|
1344
|
+
file,
|
|
1345
|
+
`${token.workspacePath}#${binding.provide}`,
|
|
1346
|
+
`bound to ${binding.useClass}`
|
|
1347
|
+
)
|
|
1348
|
+
);
|
|
1349
|
+
}
|
|
1350
|
+
return found;
|
|
1351
|
+
};
|
|
1352
|
+
function exportedClasses(file) {
|
|
1353
|
+
return file.facts.classes.filter(
|
|
1354
|
+
(entry) => file.facts.localExports.has(entry.name) || file.facts.defaultName === entry.name
|
|
1355
|
+
);
|
|
1356
|
+
}
|
|
1357
|
+
var classShape = (rule, file) => {
|
|
1358
|
+
const want = rule.class ?? {};
|
|
1359
|
+
const found = [];
|
|
1360
|
+
for (const entry of exportedClasses(file)) {
|
|
1361
|
+
for (const key of ["implements", "injects"]) {
|
|
1362
|
+
const count = want[key];
|
|
1363
|
+
if (count === void 0) continue;
|
|
1364
|
+
const matching2 = entry[key].filter((written) => {
|
|
1365
|
+
const target = resolveLocal(file, written);
|
|
1366
|
+
return target !== void 0 && targetMatches(count.to, target);
|
|
1367
|
+
});
|
|
1368
|
+
if (outside(count, matching2.length))
|
|
1369
|
+
found.push(
|
|
1370
|
+
violation(
|
|
1371
|
+
rule,
|
|
1372
|
+
file,
|
|
1373
|
+
`class ${entry.name}`,
|
|
1374
|
+
`${key} ${matching2.length === 0 ? "none" : matching2.join(", ")}, wants ${bounds(count)}`
|
|
1375
|
+
)
|
|
1376
|
+
);
|
|
1377
|
+
}
|
|
1378
|
+
if (want.publicMethods !== void 0) {
|
|
1379
|
+
const expected = [...want.publicMethods].sort();
|
|
1380
|
+
const actual = [...new Set(entry.publicMethods)].sort();
|
|
1381
|
+
if (expected.join() !== actual.join())
|
|
1382
|
+
found.push(
|
|
1383
|
+
violation(rule, file, `class ${entry.name}`, `public: ${actual.join(", ") || "none"}`)
|
|
1384
|
+
);
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
return found;
|
|
1388
|
+
};
|
|
1389
|
+
var importsDriver = (rule, file) => {
|
|
1390
|
+
const targets2 = file.imports.flatMap((entry) => entry.targets);
|
|
1391
|
+
const want = rule.imports ?? {};
|
|
1392
|
+
if (want.count !== void 0) {
|
|
1393
|
+
const count = want.count;
|
|
1394
|
+
const matching2 = new Set(
|
|
1395
|
+
targets2.filter((target) => targetMatches(count.to, target)).map((t) => t.workspacePath)
|
|
1396
|
+
);
|
|
1397
|
+
return outside(count, matching2.size) ? [violation(rule, file, "imports", `${matching2.size}, wants ${bounds(count)}`)] : [];
|
|
1398
|
+
}
|
|
1399
|
+
const reached = (want.all ?? []).map(
|
|
1400
|
+
(endpoint) => targets2.find((target) => targetMatches(endpoint, target))
|
|
1401
|
+
);
|
|
1402
|
+
if (reached.length === 0 || reached.some((target) => target === void 0)) return [];
|
|
1403
|
+
return [
|
|
1404
|
+
violation(
|
|
1405
|
+
rule,
|
|
1406
|
+
file,
|
|
1407
|
+
"imports both sides",
|
|
1408
|
+
reached.map((target) => target?.workspacePath).join(" + ")
|
|
1409
|
+
)
|
|
1410
|
+
];
|
|
1411
|
+
};
|
|
1071
1412
|
var DRIVERS = {
|
|
1072
1413
|
"forbidden-import": forbiddenImport,
|
|
1073
1414
|
"forbidden-binding": forbiddenBinding,
|
|
1074
|
-
"forbidden-call": forbiddenCall
|
|
1415
|
+
"forbidden-call": forbiddenCall,
|
|
1416
|
+
"forbidden-class-name": forbiddenClassName,
|
|
1417
|
+
file: fileShape,
|
|
1418
|
+
bindings: bindingsDriver,
|
|
1419
|
+
class: classShape,
|
|
1420
|
+
imports: importsDriver
|
|
1075
1421
|
};
|
|
1076
1422
|
function decide(rule, file) {
|
|
1423
|
+
const kind = driverOf(rule);
|
|
1424
|
+
if (kind === "inbound" || kind === "directory") return [];
|
|
1077
1425
|
if (!endpointMatches(rule.from, file.role, file.path)) return [];
|
|
1078
1426
|
const unique2 = /* @__PURE__ */ new Map();
|
|
1079
|
-
for (const found of DRIVERS[
|
|
1427
|
+
for (const found of DRIVERS[kind](rule, file)) unique2.set(found.to, found);
|
|
1080
1428
|
return [...unique2.values()];
|
|
1081
1429
|
}
|
|
1430
|
+
function sliceRoot(path2, slices) {
|
|
1431
|
+
for (const pattern of slices) {
|
|
1432
|
+
const match = pattern.exec(path2);
|
|
1433
|
+
if (match?.[1] !== void 0) return match[1];
|
|
1434
|
+
}
|
|
1435
|
+
return void 0;
|
|
1436
|
+
}
|
|
1437
|
+
function decideModule(rule, files, slices) {
|
|
1438
|
+
const kind = driverOf(rule);
|
|
1439
|
+
if (kind === "inbound") {
|
|
1440
|
+
const min = rule.inbound?.slices.min ?? 1;
|
|
1441
|
+
const importers = /* @__PURE__ */ new Map();
|
|
1442
|
+
for (const file of files) {
|
|
1443
|
+
if (file.slice === void 0) continue;
|
|
1444
|
+
for (const target of file.imports.flatMap((entry) => entry.targets)) {
|
|
1445
|
+
if (target.slice === void 0 || target.slice === file.slice) continue;
|
|
1446
|
+
const seen = importers.get(target.workspacePath) ?? /* @__PURE__ */ new Set();
|
|
1447
|
+
seen.add(file.slice);
|
|
1448
|
+
importers.set(target.workspacePath, seen);
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
return files.filter(
|
|
1452
|
+
(file) => file.slice !== void 0 && endpointMatches(rule.from, file.role, file.path)
|
|
1453
|
+
).flatMap((file) => {
|
|
1454
|
+
const count = importers.get(file.workspacePath)?.size ?? 0;
|
|
1455
|
+
return count >= min ? [] : [violation(rule, file, `imported by fewer than ${min} slices`, `${count}`)];
|
|
1456
|
+
});
|
|
1457
|
+
}
|
|
1458
|
+
if (kind !== "directory" || rule.directory === void 0) return [];
|
|
1459
|
+
const { names, under: under2, withSibling } = rule.directory;
|
|
1460
|
+
const directories = /* @__PURE__ */ new Set();
|
|
1461
|
+
for (const file of files) {
|
|
1462
|
+
for (let dir = dirname2(file.path); dir !== "." && dir !== "/"; dir = dirname2(dir))
|
|
1463
|
+
directories.add(dir);
|
|
1464
|
+
}
|
|
1465
|
+
const children = /* @__PURE__ */ new Map();
|
|
1466
|
+
for (const dir of directories) {
|
|
1467
|
+
const parent = dirname2(dir);
|
|
1468
|
+
const set = children.get(parent) ?? /* @__PURE__ */ new Set();
|
|
1469
|
+
set.add(basename(dir));
|
|
1470
|
+
children.set(parent, set);
|
|
1471
|
+
}
|
|
1472
|
+
const found = [];
|
|
1473
|
+
for (const dir of [...directories].sort()) {
|
|
1474
|
+
const name = basename(dir);
|
|
1475
|
+
if (!names.includes(name)) continue;
|
|
1476
|
+
if (!pathMatches(`${dir}/`, rule.from.path, rule.from.pathNot)) continue;
|
|
1477
|
+
const parent = dirname2(dir);
|
|
1478
|
+
if (under2 === "slice" && sliceRoot(`${dir}/`, slices) !== parent) continue;
|
|
1479
|
+
const siblings = children.get(parent) ?? /* @__PURE__ */ new Set();
|
|
1480
|
+
if (withSibling !== void 0 && !withSibling.some((sibling) => siblings.has(sibling))) continue;
|
|
1481
|
+
found.push(violation(rule, { path: dir }, `directory ${name}`));
|
|
1482
|
+
}
|
|
1483
|
+
return found;
|
|
1484
|
+
}
|
|
1082
1485
|
|
|
1083
1486
|
// src/roles/arch/kernel/facts.ts
|
|
1084
|
-
import { basename, extname } from "node:path";
|
|
1487
|
+
import { basename as basename2, extname } from "node:path";
|
|
1085
1488
|
var GLOBAL_OBJECTS = /* @__PURE__ */ new Set(["window", "globalThis", "self"]);
|
|
1086
1489
|
function nameOf(node) {
|
|
1087
1490
|
const named = node;
|
|
@@ -1129,9 +1532,11 @@ function visitImport(node, source, state) {
|
|
|
1129
1532
|
else if (specifier.type === "ImportNamespaceSpecifier") {
|
|
1130
1533
|
name = "*";
|
|
1131
1534
|
fact.members = [];
|
|
1132
|
-
const
|
|
1133
|
-
if (
|
|
1535
|
+
const local2 = nameOf(specifier.local);
|
|
1536
|
+
if (local2 !== void 0) state.namespaces.set(local2, fact);
|
|
1134
1537
|
} else name = nameOf(specifier.imported) ?? nameOf(specifier.local) ?? "?";
|
|
1538
|
+
const local = nameOf(specifier.local);
|
|
1539
|
+
if (local !== void 0) state.facts.locals.set(local, { specifier: source, imported: name });
|
|
1135
1540
|
fact.allNames.push(name);
|
|
1136
1541
|
if (specifier.importKind !== "type" && node.importKind !== "type") fact.names.push(name);
|
|
1137
1542
|
}
|
|
@@ -1147,7 +1552,12 @@ function visitExport(node, state) {
|
|
|
1147
1552
|
if (node.type === "ExportAllDeclaration") {
|
|
1148
1553
|
const alias = nameOf(node.exported);
|
|
1149
1554
|
if (alias !== void 0) state.facts.localExports.add(alias);
|
|
1150
|
-
state.facts.reexports.push({
|
|
1555
|
+
state.facts.reexports.push({
|
|
1556
|
+
specifier: source,
|
|
1557
|
+
typeOnly,
|
|
1558
|
+
names: "*",
|
|
1559
|
+
...alias === void 0 ? {} : { namespace: alias }
|
|
1560
|
+
});
|
|
1151
1561
|
state.facts.imports.push({ specifier: source, typeOnly, names: ["*"], allNames: ["*"] });
|
|
1152
1562
|
return;
|
|
1153
1563
|
}
|
|
@@ -1179,10 +1589,44 @@ function visit(node, state) {
|
|
|
1179
1589
|
case "ExportAllDeclaration":
|
|
1180
1590
|
visitExport(node, state);
|
|
1181
1591
|
break;
|
|
1182
|
-
case "ExportDefaultDeclaration":
|
|
1592
|
+
case "ExportDefaultDeclaration": {
|
|
1183
1593
|
state.facts.localExports.add("default");
|
|
1184
|
-
|
|
1594
|
+
const declaration = node.declaration;
|
|
1595
|
+
state.defaultIsComponent = defaultIsComponentLike(declaration);
|
|
1596
|
+
const named = nameOf(declaration?.id) ?? (declaration?.type === "Identifier" ? nameOf(declaration) : void 0);
|
|
1597
|
+
if (named !== void 0) state.facts.defaultName = named;
|
|
1598
|
+
break;
|
|
1599
|
+
}
|
|
1600
|
+
case "ClassDeclaration":
|
|
1601
|
+
case "ClassExpression": {
|
|
1602
|
+
const name = nameOf(node.id);
|
|
1603
|
+
if (name !== void 0) state.facts.classes.push(classFactOf(name, node));
|
|
1604
|
+
break;
|
|
1605
|
+
}
|
|
1606
|
+
case "TSInterfaceDeclaration": {
|
|
1607
|
+
const name = nameOf(node.id);
|
|
1608
|
+
if (name !== void 0) state.facts.interfaces.add(name);
|
|
1609
|
+
break;
|
|
1610
|
+
}
|
|
1611
|
+
case "VariableDeclarator": {
|
|
1612
|
+
const init = node.init;
|
|
1613
|
+
const callee = init?.type === "CallExpression" ? init.callee : void 0;
|
|
1614
|
+
const name = nameOf(node.id);
|
|
1615
|
+
if (callee?.type === "Identifier" && nameOf(callee) === "Symbol" && name !== void 0)
|
|
1616
|
+
state.facts.symbols.add(name);
|
|
1617
|
+
break;
|
|
1618
|
+
}
|
|
1619
|
+
case "ObjectExpression": {
|
|
1620
|
+
const binding = bindingOf(node);
|
|
1621
|
+
if (binding !== void 0) state.facts.bindings.push(binding);
|
|
1185
1622
|
break;
|
|
1623
|
+
}
|
|
1624
|
+
case "JSXAttribute": {
|
|
1625
|
+
const attribute = nameOf(node.name);
|
|
1626
|
+
if (attribute === "className" || attribute === "class")
|
|
1627
|
+
collectStrings(node.value, state.facts.classNames);
|
|
1628
|
+
break;
|
|
1629
|
+
}
|
|
1186
1630
|
case "ImportExpression": {
|
|
1187
1631
|
const source = literalOf(node.source);
|
|
1188
1632
|
if (source !== void 0)
|
|
@@ -1218,6 +1662,85 @@ function visit(node, state) {
|
|
|
1218
1662
|
break;
|
|
1219
1663
|
}
|
|
1220
1664
|
}
|
|
1665
|
+
function writtenName(node) {
|
|
1666
|
+
if (node?.type === "Identifier") return nameOf(node);
|
|
1667
|
+
if (node?.type === "MemberExpression" || node?.type === "TSQualifiedName") {
|
|
1668
|
+
const left = writtenName(node.object ?? node.left);
|
|
1669
|
+
const right = nameOf(node.property ?? node.right);
|
|
1670
|
+
return left === void 0 || right === void 0 ? void 0 : `${left}.${right}`;
|
|
1671
|
+
}
|
|
1672
|
+
return void 0;
|
|
1673
|
+
}
|
|
1674
|
+
function injectedBy(parameter) {
|
|
1675
|
+
const decorators = parameter.decorators ?? [];
|
|
1676
|
+
for (const decorator of decorators) {
|
|
1677
|
+
const call = decorator.expression;
|
|
1678
|
+
if (call?.type !== "CallExpression" || nameOf(call.callee) !== "Inject") continue;
|
|
1679
|
+
const token = writtenName(call.arguments?.[0]);
|
|
1680
|
+
if (token !== void 0) return token;
|
|
1681
|
+
}
|
|
1682
|
+
const inner = parameter.type === "TSParameterProperty" ? parameter.parameter : parameter;
|
|
1683
|
+
const annotation = inner?.typeAnnotation?.typeAnnotation;
|
|
1684
|
+
return annotation?.type === "TSTypeReference" ? writtenName(annotation.typeName) : void 0;
|
|
1685
|
+
}
|
|
1686
|
+
function isPublicMember(member) {
|
|
1687
|
+
if (member.static === true && member.type !== "MethodDefinition") return false;
|
|
1688
|
+
const key = member.key;
|
|
1689
|
+
if (key?.type === "PrivateIdentifier") return false;
|
|
1690
|
+
return member.accessibility !== "private" && member.accessibility !== "protected";
|
|
1691
|
+
}
|
|
1692
|
+
function classFactOf(name, node) {
|
|
1693
|
+
const fact = { name, implements: [], injects: [], publicMethods: [] };
|
|
1694
|
+
for (const clause of node.implements ?? []) {
|
|
1695
|
+
const written = writtenName(clause.expression);
|
|
1696
|
+
if (written !== void 0) fact.implements.push(written);
|
|
1697
|
+
}
|
|
1698
|
+
const members = node.body?.body ?? [];
|
|
1699
|
+
for (const member of members) {
|
|
1700
|
+
const key = nameOf(member.key);
|
|
1701
|
+
if (member.type === "MethodDefinition" && member.kind === "constructor") {
|
|
1702
|
+
const params = member.value?.params ?? [];
|
|
1703
|
+
for (const parameter of params) {
|
|
1704
|
+
const token = injectedBy(parameter);
|
|
1705
|
+
if (token !== void 0) fact.injects.push(token);
|
|
1706
|
+
}
|
|
1707
|
+
continue;
|
|
1708
|
+
}
|
|
1709
|
+
if (key === void 0 || !isPublicMember(member)) continue;
|
|
1710
|
+
const value = member.value;
|
|
1711
|
+
const isMethod = member.type === "MethodDefinition" && member.kind === "method" || member.type === "PropertyDefinition" && (value?.type === "ArrowFunctionExpression" || value?.type === "FunctionExpression");
|
|
1712
|
+
if (isMethod) fact.publicMethods.push(key);
|
|
1713
|
+
}
|
|
1714
|
+
return fact;
|
|
1715
|
+
}
|
|
1716
|
+
function bindingOf(node) {
|
|
1717
|
+
let provide;
|
|
1718
|
+
let useClass;
|
|
1719
|
+
for (const property of node.properties ?? []) {
|
|
1720
|
+
if (property.type !== "Property") continue;
|
|
1721
|
+
const key = nameOf(property.key);
|
|
1722
|
+
if (key === "provide") provide = writtenName(property.value);
|
|
1723
|
+
if (key === "useClass") useClass = writtenName(property.value);
|
|
1724
|
+
}
|
|
1725
|
+
return provide === void 0 || useClass === void 0 ? void 0 : { provide, useClass };
|
|
1726
|
+
}
|
|
1727
|
+
function collectStrings(node, into) {
|
|
1728
|
+
if (node === null || typeof node !== "object") return;
|
|
1729
|
+
if (Array.isArray(node)) {
|
|
1730
|
+
for (const child of node) collectStrings(child, into);
|
|
1731
|
+
return;
|
|
1732
|
+
}
|
|
1733
|
+
const current = node;
|
|
1734
|
+
if ((current.type === "Literal" || current.type === "StringLiteral") && typeof current.value === "string")
|
|
1735
|
+
into.push(current.value);
|
|
1736
|
+
if (current.type === "TemplateElement") {
|
|
1737
|
+
const cooked = current.value?.cooked;
|
|
1738
|
+
if (typeof cooked === "string") into.push(cooked);
|
|
1739
|
+
}
|
|
1740
|
+
for (const [field, value] of Object.entries(current)) {
|
|
1741
|
+
if (field !== "type" && field !== "start" && field !== "end") collectStrings(value, into);
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1221
1744
|
function defaultIsComponentLike(declaration) {
|
|
1222
1745
|
switch (declaration?.type) {
|
|
1223
1746
|
case "ObjectExpression":
|
|
@@ -1250,7 +1773,13 @@ function factsOf(path2, program) {
|
|
|
1250
1773
|
localExports: /* @__PURE__ */ new Set(),
|
|
1251
1774
|
reexports: [],
|
|
1252
1775
|
calls: /* @__PURE__ */ new Set(),
|
|
1253
|
-
exportsComponent: false
|
|
1776
|
+
exportsComponent: false,
|
|
1777
|
+
locals: /* @__PURE__ */ new Map(),
|
|
1778
|
+
classes: [],
|
|
1779
|
+
interfaces: /* @__PURE__ */ new Set(),
|
|
1780
|
+
symbols: /* @__PURE__ */ new Set(),
|
|
1781
|
+
bindings: [],
|
|
1782
|
+
classNames: []
|
|
1254
1783
|
};
|
|
1255
1784
|
const state = { facts, namespaces: /* @__PURE__ */ new Map(), defaultIsComponent: false };
|
|
1256
1785
|
const walk3 = (node) => {
|
|
@@ -1273,12 +1802,12 @@ function isComponentFile(path2, exported, defaultIsComponent) {
|
|
|
1273
1802
|
if (extension === ".svelte") return true;
|
|
1274
1803
|
if (extension !== ".tsx" && extension !== ".jsx") return false;
|
|
1275
1804
|
if (defaultIsComponent) return true;
|
|
1276
|
-
const stem =
|
|
1805
|
+
const stem = basename2(path2, extension).split(".")[0] ?? "";
|
|
1277
1806
|
return /^[A-Z]/.test(stem) && exported.has(stem);
|
|
1278
1807
|
}
|
|
1279
1808
|
|
|
1280
1809
|
// src/roles/arch/kernel/resolve.ts
|
|
1281
|
-
import { dirname as
|
|
1810
|
+
import { dirname as dirname3, posix } from "node:path";
|
|
1282
1811
|
var TS_BEHIND_JS = {
|
|
1283
1812
|
".js": [".ts", ".tsx"],
|
|
1284
1813
|
".mjs": [".mts"],
|
|
@@ -1398,7 +1927,7 @@ function createResolver(tree, moduleRoot) {
|
|
|
1398
1927
|
};
|
|
1399
1928
|
}
|
|
1400
1929
|
function moduleRootOf(file, tree, cache) {
|
|
1401
|
-
let dir =
|
|
1930
|
+
let dir = dirname3(file);
|
|
1402
1931
|
const seen = [];
|
|
1403
1932
|
while (dir !== "." && dir !== "/" && dir !== "") {
|
|
1404
1933
|
const cached = cache.get(dir);
|
|
@@ -1411,7 +1940,7 @@ function moduleRootOf(file, tree, cache) {
|
|
|
1411
1940
|
for (const visited of seen) cache.set(visited, dir);
|
|
1412
1941
|
return dir;
|
|
1413
1942
|
}
|
|
1414
|
-
dir =
|
|
1943
|
+
dir = dirname3(dir);
|
|
1415
1944
|
}
|
|
1416
1945
|
for (const visited of seen) cache.set(visited, ".");
|
|
1417
1946
|
return ".";
|
|
@@ -1592,6 +2121,8 @@ function analyseModule(options) {
|
|
|
1592
2121
|
const modulePrefix = toPosix2(relative3(workspaceRoot, cwd)) || ".";
|
|
1593
2122
|
const inModule = (workspacePath) => modulePrefix === "." ? workspacePath : workspacePath.startsWith(`${modulePrefix}/`) ? workspacePath.slice(modulePrefix.length + 1) : void 0;
|
|
1594
2123
|
const toWorkspace = (path2) => modulePrefix === "." ? path2 : `${modulePrefix}/${path2}`;
|
|
2124
|
+
const slices = (ruleSet.slices ?? []).map((pattern) => new RegExp(pattern));
|
|
2125
|
+
const sliceOf = (modulePath) => slices.length === 0 ? void 0 : sliceRoot(modulePath, slices);
|
|
1595
2126
|
const listed = moduleFiles(tree, modulePrefix, ruleSet);
|
|
1596
2127
|
const ownFacts = /* @__PURE__ */ new Map();
|
|
1597
2128
|
readModuleSources(
|
|
@@ -1653,7 +2184,24 @@ function analyseModule(options) {
|
|
|
1653
2184
|
};
|
|
1654
2185
|
const definingFile = (file, name) => name === "*" ? file : definition(file, name, 0, /* @__PURE__ */ new Set()) ?? file;
|
|
1655
2186
|
const unresolved = [];
|
|
1656
|
-
const
|
|
2187
|
+
const targetFile = (workspacePath, typeOnly) => {
|
|
2188
|
+
const target = knowAbout(workspacePath);
|
|
2189
|
+
if (target.role === void 0) return void 0;
|
|
2190
|
+
const slice = target.moduleRoot === modulePrefix ? sliceOf(target.modulePath) : void 0;
|
|
2191
|
+
return {
|
|
2192
|
+
workspacePath,
|
|
2193
|
+
modulePath: target.modulePath,
|
|
2194
|
+
role: target.role,
|
|
2195
|
+
typeOnly,
|
|
2196
|
+
...slice === void 0 ? {} : { slice }
|
|
2197
|
+
};
|
|
2198
|
+
};
|
|
2199
|
+
const resolved = (path2, entry) => {
|
|
2200
|
+
const byName = /* @__PURE__ */ new Map();
|
|
2201
|
+
const targets2 = targetsOf(path2, entry, byName);
|
|
2202
|
+
return { ...entry, targets: targets2, byName };
|
|
2203
|
+
};
|
|
2204
|
+
const targetsOf = (path2, entry, byName) => {
|
|
1657
2205
|
const resolution = resolverFor(modulePrefix).resolve(toWorkspace(path2), entry.specifier);
|
|
1658
2206
|
if (resolution.kind === "unresolved")
|
|
1659
2207
|
unresolved.push({ from: path2, specifier: entry.specifier });
|
|
@@ -1662,28 +2210,42 @@ function analyseModule(options) {
|
|
|
1662
2210
|
const all2 = members.length > 0 ? members : entry.allNames.length > 0 ? entry.allNames : ["*"];
|
|
1663
2211
|
const values = new Set(members.length > 0 ? members : entry.names);
|
|
1664
2212
|
const byFile = /* @__PURE__ */ new Map();
|
|
2213
|
+
const fileOfName = /* @__PURE__ */ new Map();
|
|
1665
2214
|
for (const name of all2) {
|
|
1666
2215
|
const file = definingFile(resolution.path, name);
|
|
1667
2216
|
const typeOnly = (byFile.get(file) ?? true) && !values.has(name);
|
|
1668
2217
|
byFile.set(file, typeOnly);
|
|
2218
|
+
fileOfName.set(name, file);
|
|
1669
2219
|
}
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
2220
|
+
const targets2 = /* @__PURE__ */ new Map();
|
|
2221
|
+
for (const [workspacePath, typeOnly] of byFile) {
|
|
2222
|
+
const target = targetFile(workspacePath, typeOnly);
|
|
2223
|
+
if (target !== void 0) targets2.set(workspacePath, target);
|
|
2224
|
+
}
|
|
2225
|
+
for (const [name, file] of fileOfName) {
|
|
2226
|
+
const target = targets2.get(file);
|
|
2227
|
+
if (target !== void 0) byName.set(name, target);
|
|
2228
|
+
}
|
|
2229
|
+
return [...targets2.values()];
|
|
1674
2230
|
};
|
|
1675
2231
|
const files = [];
|
|
1676
2232
|
for (const [path2, facts] of ownFacts) {
|
|
1677
2233
|
const role = knowAbout(toWorkspace(path2)).role ?? UNCLASSIFIED;
|
|
1678
|
-
const imports = facts.imports.map((entry) => (
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
2234
|
+
const imports = facts.imports.map((entry) => resolved(path2, entry));
|
|
2235
|
+
const slice = sliceOf(path2);
|
|
2236
|
+
files.push({
|
|
2237
|
+
path: path2,
|
|
2238
|
+
role,
|
|
2239
|
+
facts,
|
|
2240
|
+
imports,
|
|
2241
|
+
workspacePath: toWorkspace(path2),
|
|
2242
|
+
...slice === void 0 ? {} : { slice }
|
|
2243
|
+
});
|
|
2244
|
+
}
|
|
2245
|
+
const violations = [
|
|
2246
|
+
...files.flatMap((file) => ruleSet.forbidden.flatMap((rule) => decide(rule, file))),
|
|
2247
|
+
...ruleSet.forbidden.flatMap((rule) => decideModule(rule, files, slices))
|
|
2248
|
+
];
|
|
1687
2249
|
const roles = {};
|
|
1688
2250
|
for (const file of files) roles[file.role] = (roles[file.role] ?? 0) + 1;
|
|
1689
2251
|
return {
|
|
@@ -3242,7 +3804,7 @@ function status(rule) {
|
|
|
3242
3804
|
return rule.severity === "error" ? `<span class="status-fail">\u2717 ${rule.added.length} new, fails</span>` : `<span class="status-warn">\u26A0 ${rule.added.length} new, reported</span>`;
|
|
3243
3805
|
}
|
|
3244
3806
|
function place(violation2) {
|
|
3245
|
-
return `<li><code>${escape(violation2.from)}</code> \u2192 <code>${escape(violation2.to)}</code
|
|
3807
|
+
return `<li><code>${escape(violation2.from)}</code> \u2192 <code>${escape(violation2.to)}</code>${violation2.detail === void 0 ? "" : ` <span class="detail">(${escape(violation2.detail)})</span>`}</li>`;
|
|
3246
3808
|
}
|
|
3247
3809
|
function verdict(report) {
|
|
3248
3810
|
const newAtError = report.failing - report.unread.length;
|
|
@@ -3475,7 +4037,9 @@ function renderConsole2(report, colors) {
|
|
|
3475
4037
|
for (const rule of report.rules) {
|
|
3476
4038
|
lines.push(ruleLine(rule, colors));
|
|
3477
4039
|
for (const violation2 of rule.added)
|
|
3478
|
-
lines.push(
|
|
4040
|
+
lines.push(
|
|
4041
|
+
` ${violation2.from} ${colors.dim("\u2192")} ${violation2.to}${violation2.detail === void 0 ? "" : colors.dim(` (${violation2.detail})`)}`
|
|
4042
|
+
);
|
|
3479
4043
|
if (rule.added.length > 0 && rule.source !== void 0)
|
|
3480
4044
|
lines.push(colors.dim(` why: ${rule.source}`));
|
|
3481
4045
|
}
|
|
@@ -3504,7 +4068,9 @@ function renderMarkdown2(module, report) {
|
|
|
3504
4068
|
lines.push("", "**New in this change**", "");
|
|
3505
4069
|
for (const { rule, violation: violation2 } of fresh) {
|
|
3506
4070
|
const why = rule.source === void 0 ? "" : ` (${rule.source})`;
|
|
3507
|
-
lines.push(
|
|
4071
|
+
lines.push(
|
|
4072
|
+
`- ${rule.name}: \`${violation2.from}\` \u2192 \`${violation2.to}\`${violation2.detail === void 0 ? "" : ` (${violation2.detail})`}${why}`
|
|
4073
|
+
);
|
|
3508
4074
|
}
|
|
3509
4075
|
}
|
|
3510
4076
|
if (report.unread.length > 0)
|
|
@@ -4897,7 +5463,7 @@ import { existsSync as existsSync13, mkdirSync as mkdirSync3, unlinkSync, writeF
|
|
|
4897
5463
|
import { join as join18 } from "node:path";
|
|
4898
5464
|
|
|
4899
5465
|
// src/roles/env/collect.ts
|
|
4900
|
-
import { dirname as
|
|
5466
|
+
import { dirname as dirname4, join as join16 } from "node:path/posix";
|
|
4901
5467
|
|
|
4902
5468
|
// src/roles/env/checks.ts
|
|
4903
5469
|
var READER_VAR_NAME = String.raw`[A-Z0-9_]+`;
|
|
@@ -5508,7 +6074,7 @@ function dockerFindings(view, settings) {
|
|
|
5508
6074
|
}
|
|
5509
6075
|
}
|
|
5510
6076
|
for (const { source, line } of contextCopySources) {
|
|
5511
|
-
if (!view.exists(join16(".", source)) && !view.exists(join16(
|
|
6077
|
+
if (!view.exists(join16(".", source)) && !view.exists(join16(dirname4(file), source))) {
|
|
5512
6078
|
findings.push(finding("dead-copy-path", { name: source, file, line }));
|
|
5513
6079
|
}
|
|
5514
6080
|
}
|
|
@@ -7997,7 +8563,7 @@ function bandFor(valueKb, referenceKb) {
|
|
|
7997
8563
|
|
|
7998
8564
|
// src/roles/static-analysis/bundle/scan.ts
|
|
7999
8565
|
import { existsSync as existsSync22, readFileSync as readFileSync21 } from "node:fs";
|
|
8000
|
-
import { basename as
|
|
8566
|
+
import { basename as basename3, join as join31 } from "node:path";
|
|
8001
8567
|
import { pathToFileURL as pathToFileURL4 } from "node:url";
|
|
8002
8568
|
import { gzipSync } from "node:zlib";
|
|
8003
8569
|
import { globSync as globSync3 } from "tinyglobby";
|
|
@@ -8179,7 +8745,7 @@ function heaviestPerKind(references, sizeOf) {
|
|
|
8179
8745
|
const current = best.get(kind);
|
|
8180
8746
|
if (current !== void 0 && current.weight.parsed >= size.parsed) continue;
|
|
8181
8747
|
best.set(kind, {
|
|
8182
|
-
name:
|
|
8748
|
+
name: basename3(announced),
|
|
8183
8749
|
kind,
|
|
8184
8750
|
weight: { ...size, files: 1 },
|
|
8185
8751
|
band: bandFor(size.parsed / 1024, PER_SCRIPT_KB)
|
|
@@ -8188,7 +8754,7 @@ function heaviestPerKind(references, sizeOf) {
|
|
|
8188
8754
|
return [...best.values()].sort((a, b) => b.weight.parsed - a.weight.parsed);
|
|
8189
8755
|
}
|
|
8190
8756
|
function scanDynamicLayer(publicDir, references) {
|
|
8191
|
-
const referenced = new Set([...references].map((announced) =>
|
|
8757
|
+
const referenced = new Set([...references].map((announced) => basename3(announced)));
|
|
8192
8758
|
const groups = /* @__PURE__ */ new Map();
|
|
8193
8759
|
const all2 = emptyWeight();
|
|
8194
8760
|
for (const path2 of globSync3("assets/*.{js,mjs,cjs}", {
|
|
@@ -8196,7 +8762,7 @@ function scanDynamicLayer(publicDir, references) {
|
|
|
8196
8762
|
absolute: true,
|
|
8197
8763
|
dot: false
|
|
8198
8764
|
})) {
|
|
8199
|
-
const name =
|
|
8765
|
+
const name = basename3(path2);
|
|
8200
8766
|
if (referenced.has(name)) continue;
|
|
8201
8767
|
const buffer = readFileSync21(path2);
|
|
8202
8768
|
const size = { parsed: buffer.length, transfer: gzipSync(buffer, { level: 9 }).length };
|
|
@@ -8788,7 +9354,7 @@ var SCRIPT2 = `(function(){
|
|
|
8788
9354
|
})();`;
|
|
8789
9355
|
|
|
8790
9356
|
// src/roles/static-analysis/graph/imports.ts
|
|
8791
|
-
import { dirname as
|
|
9357
|
+
import { dirname as dirname5, join as join35 } from "node:path";
|
|
8792
9358
|
var TS_BEHIND_JS2 = {
|
|
8793
9359
|
".js": [".ts", ".tsx"],
|
|
8794
9360
|
".mjs": [".mts"],
|
|
@@ -8841,7 +9407,7 @@ function specifiersOf2(program) {
|
|
|
8841
9407
|
}
|
|
8842
9408
|
function resolveSpecifier2(from, specifier, known) {
|
|
8843
9409
|
if (!specifier.startsWith(".")) return void 0;
|
|
8844
|
-
const base = join35(
|
|
9410
|
+
const base = join35(dirname5(from), specifier);
|
|
8845
9411
|
if (known.has(base)) return base;
|
|
8846
9412
|
for (const extension of RESOLVABLE_EXTENSIONS) {
|
|
8847
9413
|
if (known.has(base + extension)) return base + extension;
|
|
@@ -9113,9 +9679,9 @@ var JscpdAdapter = class extends BaseAdapter {
|
|
|
9113
9679
|
}
|
|
9114
9680
|
const tests = runDuplication(jscpd, config, scanned, "tests", ctx.cwd);
|
|
9115
9681
|
if (tests.kind === "failed") return { ok: false, code: tests.code };
|
|
9116
|
-
const
|
|
9682
|
+
const outside2 = outsideOf(ctx.cwd);
|
|
9117
9683
|
const dead = runDeadCode(jscpd, config, paths2.length > 0 ? paths2 : ["."], ctx.cwd, [
|
|
9118
|
-
...
|
|
9684
|
+
...outside2.imported
|
|
9119
9685
|
]);
|
|
9120
9686
|
if (dead.kind === "failed") return { ok: false, code: dead.code };
|
|
9121
9687
|
if (dead.kind === "no-entry-point" && !unreadableCode) {
|
|
@@ -9146,7 +9712,7 @@ var JscpdAdapter = class extends BaseAdapter {
|
|
|
9146
9712
|
graph,
|
|
9147
9713
|
coverage,
|
|
9148
9714
|
size,
|
|
9149
|
-
outside,
|
|
9715
|
+
outside: outside2,
|
|
9150
9716
|
bundle
|
|
9151
9717
|
});
|
|
9152
9718
|
if (emitting) {
|
|
@@ -9720,7 +10286,7 @@ function assembleReport({
|
|
|
9720
10286
|
graph,
|
|
9721
10287
|
coverage,
|
|
9722
10288
|
size,
|
|
9723
|
-
outside,
|
|
10289
|
+
outside: outside2,
|
|
9724
10290
|
bundle
|
|
9725
10291
|
}) {
|
|
9726
10292
|
const unreadable = hasUnreadableCode(coverage);
|
|
@@ -9808,7 +10374,7 @@ function assembleReport({
|
|
|
9808
10374
|
{ metric: "unusedImports", value: dead.byCategory["unused-import"] ?? 0, unit: "imports" }
|
|
9809
10375
|
);
|
|
9810
10376
|
}
|
|
9811
|
-
const testOnly = /* @__PURE__ */ new Set([...toolTestOnly, ...
|
|
10377
|
+
const testOnly = /* @__PURE__ */ new Set([...toolTestOnly, ...outside2.testOnly]);
|
|
9812
10378
|
if (testOnly.size > 0 && !unreadable) {
|
|
9813
10379
|
rows.push({ metric: "usedOnlyByTests", value: testOnly.size, unit: "files" });
|
|
9814
10380
|
}
|
|
@@ -9922,7 +10488,7 @@ function assembleReport({
|
|
|
9922
10488
|
complexity,
|
|
9923
10489
|
graph,
|
|
9924
10490
|
size,
|
|
9925
|
-
|
|
10491
|
+
outside2.testOnly,
|
|
9926
10492
|
unreadable
|
|
9927
10493
|
),
|
|
9928
10494
|
byLanguage
|
|
@@ -10698,7 +11264,7 @@ function detectFramework(packageJson) {
|
|
|
10698
11264
|
}
|
|
10699
11265
|
|
|
10700
11266
|
// src/core/dispatch.ts
|
|
10701
|
-
import { basename as
|
|
11267
|
+
import { basename as basename4 } from "node:path";
|
|
10702
11268
|
|
|
10703
11269
|
// src/shared/text.ts
|
|
10704
11270
|
function ensureLines(current, lines) {
|
|
@@ -11118,7 +11684,7 @@ async function dispatch(opts) {
|
|
|
11118
11684
|
}
|
|
11119
11685
|
if (adapter.beforeInit) {
|
|
11120
11686
|
await adapter.beforeInit({
|
|
11121
|
-
module:
|
|
11687
|
+
module: basename4(opts.cwd),
|
|
11122
11688
|
cwd: opts.cwd,
|
|
11123
11689
|
preset,
|
|
11124
11690
|
ci: false,
|
|
@@ -11139,7 +11705,7 @@ async function dispatch(opts) {
|
|
|
11139
11705
|
process.stderr.write(` fixing what ${opts.target} can fix automatically...
|
|
11140
11706
|
`);
|
|
11141
11707
|
await adapter.afterInit({
|
|
11142
|
-
module:
|
|
11708
|
+
module: basename4(opts.cwd),
|
|
11143
11709
|
cwd: opts.cwd,
|
|
11144
11710
|
preset,
|
|
11145
11711
|
ci: false,
|
package/dist/index.js
CHANGED
package/docs/arch-adoption.md
CHANGED
|
@@ -133,6 +133,28 @@ Which kind of rule a `to` makes:
|
|
|
133
133
|
| `role` / `path` (`pathNot`, `dependencyTypesNot: ["type-only"]`) | importing a file of that side, inside the module or in a shared library |
|
|
134
134
|
| `module` + `names` | importing those names from that package, as a value |
|
|
135
135
|
| `calls` (+ `module`, a regular expression) | calling those globals, or importing a matching client as a value |
|
|
136
|
+
| `role` / `path` + `slice: "other"` | the same import, only when it lands in another slice of the module |
|
|
137
|
+
| `classNames` (a regular expression) | a class string written in a `className` or `class` attribute |
|
|
138
|
+
|
|
139
|
+
A rule that states what a file, a class or a directory must BE uses one of these keys instead of
|
|
140
|
+
`to`, one per rule:
|
|
141
|
+
|
|
142
|
+
| Key | Requires | Example |
|
|
143
|
+
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
|
|
144
|
+
| `file` | `name` (a regex on its base name), `exportsItsName`, `exportStar: false`, `interfaceAndSymbol: true`, or `allowed: false` (it does not belong there) | `{ "exportsItsName": { "strip": "\\.controller\\.ts$", "case": "pascal", "append": "Controller" } }` |
|
|
145
|
+
| `bindings` | `binds: "nothing"` (no `{ provide, useClass }`), or `"own-slice"` (only tokens of its own slice) | the application module binds no port |
|
|
146
|
+
| `class` | `implements` / `injects` counted (`to`, `min`, `max`), or exactly these `publicMethods` | `{ "publicMethods": ["execute"] }` |
|
|
147
|
+
| `imports` | `count` of distinct files of `to` (`min`, `max`), or `all`: it imports each kind at once | a route imports exactly one entry view |
|
|
148
|
+
| `inbound` | `slices.min`: at least that many other slices import it | a file in `shared` used by two slices |
|
|
149
|
+
| `directory` | `names` forbidden, `under: "slice"` (directly under a slice root), `withSibling` | no `domain/` beside an `infrastructure/` |
|
|
150
|
+
|
|
151
|
+
A slice is what the rule set's `slices` says: regular expressions with one capture group, the slice
|
|
152
|
+
root, first match wins. Only a file of the same module has a slice; a shared library has none, so it
|
|
153
|
+
is never "another slice". A rule that reads slices in a set that declares none is refused.
|
|
154
|
+
|
|
155
|
+
A violation of these rules names what is wrong in words that stay the same while the violation
|
|
156
|
+
does (`class FooAdapter`, `directory domain`), so a moved count is not a new violation; the count
|
|
157
|
+
is shown beside it.
|
|
136
158
|
|
|
137
159
|
The map (`sentinel.arch.json`, at the workspace root), first match wins:
|
|
138
160
|
|
|
@@ -144,7 +166,10 @@ The map (`sentinel.arch.json`, at the workspace root), first match wins:
|
|
|
144
166
|
"rules": "docs/adr/0050-....rules.json",
|
|
145
167
|
"severity": { "G3": "warn" }
|
|
146
168
|
},
|
|
147
|
-
{
|
|
169
|
+
{
|
|
170
|
+
"modules": ["apps/front/*", "libs/front/*"],
|
|
171
|
+
"rules": "docs/adr/0050-....rules.json"
|
|
172
|
+
}
|
|
148
173
|
],
|
|
149
174
|
"notices": [{ "modules": ["apps/nest/backends-for-frontends/planning"], "text": "..." }]
|
|
150
175
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hublo/sentinel",
|
|
3
|
-
"version": "1.7.0-alpha.
|
|
3
|
+
"version": "1.7.0-alpha.13",
|
|
4
4
|
"description": "One CLI that guards code health across Hublo repos: shared lint/typescript/build/test presets, static & dynamic analysis, and architecture checks.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|