@hublo/sentinel 1.7.0-alpha.17 → 1.7.0-alpha.19

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.
@@ -12,7 +12,7 @@ import {
12
12
  presetNotInstalledYet,
13
13
  registerAdapters,
14
14
  resolve
15
- } from "../chunk-CGPGCYVN.js";
15
+ } from "../chunk-YVUPZJCX.js";
16
16
  import {
17
17
  resolveContext,
18
18
  separateToolArgs
@@ -414,15 +414,21 @@ function compareWithBase(branch, base, options) {
414
414
  const inWorkspace = (path2) => options.modulePrefix === "." ? path2 : `${options.modulePrefix}/${path2}`;
415
415
  const unreadBeforeInWorkspace = new Set([...unreadBefore].map(inWorkspace));
416
416
  const wasThere = (violation2) => before.has(identity(violation2)) || unreadBefore.has(violation2.from) || unreadBeforeInWorkspace.has(violation2.to.split("#")[0] ?? "");
417
+ const added = branch.filter((violation2) => !wasThere(violation2));
418
+ const fixed = atBase.filter(
419
+ (violation2, index) => !after.has(identity(violation2)) && !(movedDirectory(index) && after.has(identity(base[index]))) && !unread.has(violation2.from)
420
+ );
421
+ const goneTargets = new Set(fixed.map((violation2) => `${violation2.rule}\0${violation2.to}`));
417
422
  return {
423
+ maybeMoved: added.filter(
424
+ (violation2) => goneTargets.has(`${violation2.rule}\0${violation2.to}`)
425
+ ).length,
418
426
  uncompared: branch.filter(
419
427
  (violation2) => !before.has(identity(violation2)) && wasThere(violation2)
420
428
  ).length,
421
429
  known: branch.filter(wasThere),
422
- added: branch.filter((violation2) => !wasThere(violation2)),
423
- fixed: atBase.filter(
424
- (violation2, index) => !after.has(identity(violation2)) && !(movedDirectory(index) && after.has(identity(base[index]))) && !unread.has(violation2.from)
425
- )
430
+ added,
431
+ fixed
426
432
  };
427
433
  }
428
434
 
@@ -836,9 +842,18 @@ function fileProblems(where, file) {
836
842
  return problems;
837
843
  }
838
844
  function classProblems(where, shape, roles) {
839
- const keys = keysProblems(where, shape, ["implements", "injects", "publicMethods"]);
845
+ const keys = keysProblems(where, shape, ["name", "implements", "injects", "publicMethods"]);
840
846
  if (keys.length > 0) return keys;
841
847
  const problems = [];
848
+ if (shape.implements === void 0 && shape.injects === void 0 && shape.publicMethods === void 0)
849
+ problems.push(
850
+ `${where}: give \`implements\`, \`injects\` or \`publicMethods\`; \`name\` only selects the classes`
851
+ );
852
+ if (shape.name !== void 0) {
853
+ if (typeof shape.name !== "string" || shape.name === "")
854
+ problems.push(`${where}: \`name\` must be a regular expression`);
855
+ else problems.push(...regexProblems(where, [shape.name]));
856
+ }
842
857
  for (const key of ["implements", "injects"]) {
843
858
  const count = shape[key];
844
859
  if (count === void 0) continue;
@@ -940,7 +955,7 @@ function endpointProblems(where, endpoint, roles, allowed = ENDPOINT_KEYS) {
940
955
  }
941
956
  var TARGET_KEYS = {
942
957
  "forbidden-binding": ["module", "names"],
943
- "forbidden-call": ["calls", "module"],
958
+ "forbidden-call": ["calls", "module", "typeImports"],
944
959
  "forbidden-class-name": ["classNames"],
945
960
  "forbidden-import": ["role", "path", "pathNot", "dependencyTypesNot", "slice"]
946
961
  };
@@ -955,7 +970,7 @@ function bindingProblems(where, to) {
955
970
  problems.push(`${where}.to: \`names\` needs the \`module\` they come from`);
956
971
  else if (/^\^|\$$/.test(to.module))
957
972
  problems.push(
958
- `${where}.to: a binding's \`module\` is the package name as written, not a regular expression`
973
+ `${where}.to: with \`names\`, \`module\` is the package name as written (\`@tanstack/react-query\`); it is a regular expression only with \`calls\` or alone`
959
974
  );
960
975
  const names = nameListProblem(where, "names", to.names);
961
976
  return names === void 0 ? problems : [...problems, names];
@@ -972,6 +987,12 @@ function callProblems(where, to) {
972
987
  }
973
988
  if (to.calls === void 0 && to.module === void 0)
974
989
  problems.push(`${where}.to: give \`calls\` or a \`module\``);
990
+ if (to.typeImports !== void 0 && to.typeImports !== true)
991
+ problems.push(`${where}.to: \`typeImports\` can only be true`);
992
+ if (to.typeImports !== void 0 && to.module === void 0)
993
+ problems.push(
994
+ `${where}.to: \`typeImports\` says which imports of \`module\` count, and there is none`
995
+ );
975
996
  return [...problems, ...regexProblems(`${where}.to`, to.module === void 0 ? [] : [to.module])];
976
997
  }
977
998
  function targetProblems(where, rule, roles) {
@@ -979,6 +1000,12 @@ function targetProblems(where, rule, roles) {
979
1000
  const kind = driverOf(rule);
980
1001
  const allowed = TARGET_KEYS[kind] ?? [];
981
1002
  const stray = Object.keys(to).filter((key) => !allowed.includes(key));
1003
+ const known = new Set(Object.values(TARGET_KEYS).flat());
1004
+ const unknown = stray.filter((key) => !known.has(key));
1005
+ if (unknown.length > 0)
1006
+ return [
1007
+ `${where}.to: ${unknown.map((key) => `\`${key}\``).join(", ")} is not a key of \`to\`: ${[...known].map((key) => `\`${key}\``).join(", ")}`
1008
+ ];
982
1009
  if (stray.length > 0)
983
1010
  return [
984
1011
  `${where}.to: ${stray.map((key) => `\`${key}\``).join(", ")} cannot go with ${allowed.map((key) => `\`${key}\``).join("/")}: one kind of rule per entry`
@@ -1024,15 +1051,15 @@ function loadRuleSet(file) {
1024
1051
  try {
1025
1052
  parsed2 = JSON.parse(readFileSync2(file, "utf8"));
1026
1053
  } catch (error) {
1027
- throw new Error(
1028
- `sentinel --arch: cannot read the rule set ${file}: ${error.message}`,
1029
- { cause: error }
1030
- );
1054
+ throw new Error(`cannot read the rule set ${file}: ${error.message}`, {
1055
+ cause: error
1056
+ });
1031
1057
  }
1032
1058
  const problems = ruleSetProblems(parsed2);
1033
1059
  if (problems.length > 0) {
1034
1060
  throw new Error(
1035
- `sentinel --arch: the rule set ${file} is not valid, so nothing was checked:
1061
+ // No `sentinel --arch:` here: the adapter says who is speaking, and the beta saw it twice.
1062
+ `the rule set ${file} is not valid, so nothing was checked:
1036
1063
  ` + problems.map((problem) => ` - ${problem}`).join("\n")
1037
1064
  );
1038
1065
  }
@@ -1117,7 +1144,8 @@ function decisionFor(cwd) {
1117
1144
  if (answer.rules === void 0) {
1118
1145
  return {
1119
1146
  ok: false,
1120
- skipped: `${defaults2.map} applies no decision to ${modulePath}.`,
1147
+ // The beta: a module that is neither a front nor a BFF got this and an exit 0, and no way on.
1148
+ skipped: `${defaults2.map} applies no decision to ${modulePath}. To check it, give it one in ${defaults2.map}: { "modules": ["${modulePath}"], "rules": "<its rule set>.rules.json" }, as docs/arch-adoption.md shows.`,
1121
1149
  notices: answer.notices
1122
1150
  };
1123
1151
  }
@@ -1334,7 +1362,7 @@ var forbiddenCall = (rule, file) => {
1334
1362
  if (to.module !== void 0) {
1335
1363
  const client = new RegExp(to.module);
1336
1364
  for (const entry of file.imports) {
1337
- if (!entry.typeOnly && client.test(entry.specifier))
1365
+ if ((to.typeImports === true || !entry.typeOnly) && client.test(entry.specifier))
1338
1366
  found.push(violation(rule, file, entry.specifier));
1339
1367
  }
1340
1368
  }
@@ -1408,8 +1436,10 @@ function exportedClasses(file) {
1408
1436
  }
1409
1437
  var classShape = (rule, file) => {
1410
1438
  const want = rule.class ?? {};
1439
+ const named = want.name === void 0 ? void 0 : new RegExp(want.name);
1411
1440
  const found = [];
1412
1441
  for (const entry of exportedClasses(file)) {
1442
+ if (named !== void 0 && !named.test(entry.name)) continue;
1413
1443
  for (const key of ["implements", "injects"]) {
1414
1444
  const count = want[key];
1415
1445
  if (count === void 0) continue;
@@ -1499,7 +1529,8 @@ function fromMatchesSomething(rule, files) {
1499
1529
  return [...directoriesOf(files)].some(
1500
1530
  (dir) => pathMatches(`${dir}/`, rule.from.path, rule.from.pathNot)
1501
1531
  );
1502
- return files.some((file) => endpointMatches(rule.from, file.role, file.path));
1532
+ const from = rule.file?.allowed === false ? { role: rule.from.role, path: rule.from.path } : rule.from;
1533
+ return files.some((file) => endpointMatches(from, file.role, file.path));
1503
1534
  }
1504
1535
  function decideModule(rule, files, slices) {
1505
1536
  const kind = driverOf(rule);
@@ -1691,6 +1722,20 @@ function visit(node, state) {
1691
1722
  collectStrings(node.value, state.facts.classNames);
1692
1723
  break;
1693
1724
  }
1725
+ case "TSImportEqualsDeclaration": {
1726
+ const reference = node.moduleReference;
1727
+ const source = reference?.type === "TSExternalModuleReference" ? literalOf(reference.expression) : void 0;
1728
+ if (source !== void 0) {
1729
+ const typeOnly = node.importKind === "type";
1730
+ state.facts.imports.push({
1731
+ specifier: source,
1732
+ typeOnly,
1733
+ names: typeOnly ? [] : ["*"],
1734
+ allNames: ["*"]
1735
+ });
1736
+ }
1737
+ break;
1738
+ }
1694
1739
  case "ImportExpression": {
1695
1740
  const source = literalOf(node.source);
1696
1741
  if (source !== void 0)
@@ -1703,8 +1748,13 @@ function visit(node, state) {
1703
1748
  break;
1704
1749
  }
1705
1750
  case "CallExpression": {
1706
- const called = calledName(node.callee);
1751
+ const callee = node.callee;
1752
+ const called = calledName(callee);
1707
1753
  if (called !== void 0) state.facts.calls.add(called);
1754
+ else if (callee?.type === "MemberExpression" && callee.computed !== true) {
1755
+ const method = nameOf(callee.property);
1756
+ if (method !== void 0) state.facts.methods.add(method);
1757
+ }
1708
1758
  const argument = literalOf(node.arguments?.[0]);
1709
1759
  if (called === "require" && argument !== void 0)
1710
1760
  state.facts.imports.push({
@@ -1837,6 +1887,7 @@ function factsOf(path2, program) {
1837
1887
  localExports: /* @__PURE__ */ new Set(),
1838
1888
  reexports: [],
1839
1889
  calls: /* @__PURE__ */ new Set(),
1890
+ methods: /* @__PURE__ */ new Set(),
1840
1891
  exportsComponent: false,
1841
1892
  locals: /* @__PURE__ */ new Map(),
1842
1893
  classes: [],
@@ -2387,6 +2438,13 @@ function analyseModule(options) {
2387
2438
  read: [...known.keys()],
2388
2439
  matchedNothing: {
2389
2440
  rules: ruleSet.forbidden.filter((rule) => !fromMatchesSomething(rule, files)).map((rule) => rule.name),
2441
+ methodsOnly: ruleSet.forbidden.flatMap((rule) => {
2442
+ const calls = rule.to !== void 0 && "calls" in rule.to ? rule.to.calls ?? [] : [];
2443
+ const from = files.filter((file) => endpointMatches(rule.from, file.role, file.path));
2444
+ return calls.filter(
2445
+ (name) => from.some((file) => file.facts.methods.has(name)) && !from.some((file) => file.facts.calls.has(name))
2446
+ ).map((name) => ({ rule: rule.name, name }));
2447
+ }),
2390
2448
  // As written in the rule set: a RegExp's `source` escapes the `/` the author wrote.
2391
2449
  slices: (ruleSet.slices ?? []).filter(
2392
2450
  (_, index) => !files.some((file) => sliceRoot(file.path, [slices[index]]) !== void 0)
@@ -3939,6 +3997,7 @@ function buildReport(input) {
3939
3997
  rules,
3940
3998
  fixed: comparison.fixed.length,
3941
3999
  uncompared: comparison.uncompared,
4000
+ maybeMoved: comparison.maybeMoved,
3942
4001
  unreadOnMain: input.unreadOnMain,
3943
4002
  matchedNothing: analysis.matchedNothing,
3944
4003
  base: input.base,
@@ -3970,22 +4029,37 @@ function matchedNothingLines(report) {
3970
4029
  lines.push(
3971
4030
  `${rules.length} rule(s) found no file on their \`from\` side here, so they checked nothing: ${rules.join(", ")}. Expected when the module has none (a library has no routes); a typo in a path looks the same.`
3972
4031
  );
4032
+ for (const { rule, name } of report.matchedNothing.methodsOnly ?? [])
4033
+ lines.push(
4034
+ `${rule} lists \`${name}\` in \`calls\`, and its files call it only as a method: \`calls\` counts globals, so it found none.`
4035
+ );
3973
4036
  if (slices.length > 0)
3974
4037
  lines.push(
3975
4038
  `${slices.length} slice pattern(s) match no file here: ${slices.join(", ")}. The rules on slices do not see them.`
3976
4039
  );
3977
4040
  return lines;
3978
4041
  }
4042
+ function unresolvedSentence(unresolved) {
4043
+ const relative8 = unresolved.filter((entry) => entry.specifier.startsWith(".")).length;
4044
+ const aliases = unresolved.length - relative8;
4045
+ const parts = [
4046
+ ...relative8 > 0 ? [`${relative8} relative`] : [],
4047
+ ...aliases > 0 ? [`${aliases} that look like an alias`] : []
4048
+ ];
4049
+ return `${unresolved.length} import(s) resolve to nothing (${parts.join(", ")}), so no rule saw them`;
4050
+ }
3979
4051
  function footer(report, colors) {
3980
4052
  const lines = matchedNothingLines(report).map((line) => colors.warn(` ${line}`));
3981
4053
  if (report.unresolved.length > 0) {
3982
4054
  const first = report.unresolved.slice(0, 3).map((entry) => `${entry.specifier} (${entry.from})`);
4055
+ lines.push(colors.warn(` ${unresolvedSentence(report.unresolved)}: ${first.join(", ")}`));
4056
+ }
4057
+ if (report.maybeMoved > 0)
3983
4058
  lines.push(
3984
4059
  colors.warn(
3985
- ` ${report.unresolved.length} import(s) look like an alias and resolve to nothing, so no rule saw them: ${first.join(", ")}`
4060
+ ` ${report.maybeMoved} new violation(s) share their rule and target with one that is gone: a file moved without git seeing it? \`git add\` both paths, or commit the move.`
3986
4061
  )
3987
4062
  );
3988
- }
3989
4063
  if (report.fixed > 0)
3990
4064
  lines.push(
3991
4065
  colors.ok(` ${report.fixed} violation(s) that were there before this branch are gone.`)
@@ -4069,11 +4143,7 @@ function renderMarkdown2(module, report) {
4069
4143
  "",
4070
4144
  `This change made them unparsable, so they were not checked: ${report.unread.map((path2) => `\`${path2}\``).join(", ")}.`
4071
4145
  );
4072
- if (report.unresolved.length > 0)
4073
- lines.push(
4074
- "",
4075
- `${report.unresolved.length} import(s) look like an alias and resolve to nothing.`
4076
- );
4146
+ if (report.unresolved.length > 0) lines.push("", `${unresolvedSentence(report.unresolved)}.`);
4077
4147
  for (const line of matchedNothingLines(report)) lines.push("", line);
4078
4148
  return `${lines.join("\n")}
4079
4149
  `;
@@ -4207,7 +4277,7 @@ function problemsSection(report) {
4207
4277
  );
4208
4278
  if (report.unresolved.length > 0)
4209
4279
  parts.push(
4210
- `<p>Imports that look like an alias and resolve to nothing, so no rule saw them:</p><ul class="places">${report.unresolved.map((entry) => `<li><code>${escape(entry.specifier)}</code> in <code>${escape(entry.from)}</code></li>`).join("")}</ul>`
4280
+ `<p>Imports that resolve to nothing (a relative path that broke, or what looks like an alias), so no rule saw them:</p><ul class="places">${report.unresolved.map((entry) => `<li><code>${escape(entry.specifier)}</code> in <code>${escape(entry.from)}</code></li>`).join("")}</ul>`
4211
4281
  );
4212
4282
  for (const line of matchedNothingLines(report))
4213
4283
  parts.push(`<p>${escape(line).replace(/`([^`]+)`/g, "<code>$1</code>")}</p>`);
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  registerAdapters,
7
7
  resolve,
8
8
  setDefaultRunner
9
- } from "./chunk-CGPGCYVN.js";
9
+ } from "./chunk-YVUPZJCX.js";
10
10
  import {
11
11
  BaseAdapter
12
12
  } from "./chunk-2LLRSWG2.js";
@@ -128,13 +128,13 @@ the workspace's `tsconfig.base.json`, the module's own `tsconfig.json`, and Svel
128
128
 
129
129
  Which kind of rule a `to` makes:
130
130
 
131
- | `to` | Forbids |
132
- | ---------------------------------------------------------------- | ----------------------------------------------------------------------- |
133
- | `role` / `path` (`pathNot`, `dependencyTypesNot: ["type-only"]`) | importing a file of that side, inside the module or in a shared library |
134
- | `module` + `names` | importing those names from that package, as a value |
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 |
131
+ | `to` | Forbids |
132
+ | ---------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
133
+ | `role` / `path` (`pathNot`, `dependencyTypesNot: ["type-only"]`) | importing a file of that side, inside the module or in a shared library |
134
+ | `module` + `names` | importing those names from that package, as a value |
135
+ | `calls` (+ `module`, a regular expression) | calling those globals, or importing a matching client as a value; `typeImports: true` counts an `import type` of it too |
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
138
 
139
139
  A rule that states what a file, a class or a directory must BE uses one of these keys instead of
140
140
  `to`, one per rule:
@@ -143,7 +143,7 @@ A rule that states what a file, a class or a directory must BE uses one of these
143
143
  | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
144
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
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"] }` |
146
+ | `class` | `implements` / `injects` counted (`to`, `min`, `max`), or exactly these `publicMethods`; `name`, a regex, keeps only the exported classes it names | `{ "name": "UseCase$", "publicMethods": ["execute"] }` |
147
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
148
  | `inbound` | `slices.min`: at least that many other slices import it | a file in `shared` used by two slices |
149
149
  | `directory` | `names` forbidden, `under: "slice"` (directly under a slice root), `withSibling` | no `domain/` beside an `infrastructure/` |
@@ -181,6 +181,16 @@ it. Notices are said at adoption and change nothing.
181
181
 
182
182
  A rule set, a map or a module config with a defect is refused before anything runs.
183
183
 
184
+ `calls` names GLOBALS (`fetch`), never a method: `this.db.$transaction()` is not a call of
185
+ `$transaction`, and a rule listing it is said to have found it only as a method.
186
+
187
+ A module the map gives no decision (neither a front nor a BFF, say) is not checked, and `--init`
188
+ says so. To check it, write its rule set (next to the document that decides its architecture) and
189
+ give it a decision in the map: `{ "modules": ["apps/nest/microservices/network"], "rules":
190
+ "apps/nest/microservices/network/agents/implementation/architecture.rules.json" }`. Paths in a
191
+ violation are the module's for the importing file and the workspace's for its target, since a
192
+ target can be in a shared library.
193
+
184
194
  ## What the role will not do
185
195
 
186
196
  - Analyse a module whose files did not change: in CI, the modules analysed are those whose own
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hublo/sentinel",
3
- "version": "1.7.0-alpha.17",
3
+ "version": "1.7.0-alpha.19",
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",