@starci/eslint-canon-be 3.5.0 → 3.6.0

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.
Files changed (195) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +11 -2
  3. package/dist/ambient-id.d.ts +9 -0
  4. package/{ambient-id.mjs → dist/ambient-id.js} +43 -39
  5. package/dist/app-composition.d.ts +21 -0
  6. package/{app-composition.mjs → dist/app-composition.js} +396 -300
  7. package/dist/async-discipline.d.ts +9 -0
  8. package/dist/async-discipline.js +77 -0
  9. package/dist/authorization.d.ts +12 -0
  10. package/{authorization.mjs → dist/authorization.js} +53 -59
  11. package/dist/boundary-schema.d.ts +15 -0
  12. package/{boundary-schema.mjs → dist/boundary-schema.js} +120 -105
  13. package/dist/builders.d.ts +21 -0
  14. package/{builders.mjs → dist/builders.js} +162 -146
  15. package/dist/cli.d.ts +15 -0
  16. package/{cli.mjs → dist/cli.js} +97 -82
  17. package/dist/comments.d.ts +52 -0
  18. package/dist/comments.js +412 -0
  19. package/dist/config-owner.d.ts +24 -0
  20. package/{config-owner.mjs → dist/config-owner.js} +256 -219
  21. package/dist/connections.d.ts +12 -0
  22. package/{connections.mjs → dist/connections.js} +138 -140
  23. package/dist/cookies.d.ts +9 -0
  24. package/{cookies.mjs → dist/cookies.js} +58 -58
  25. package/dist/cqrs.d.ts +27 -0
  26. package/{cqrs.mjs → dist/cqrs.js} +341 -282
  27. package/dist/data-access.d.ts +50 -0
  28. package/{data-access.mjs → dist/data-access.js} +238 -243
  29. package/dist/default-deny.d.ts +15 -0
  30. package/{default-deny.mjs → dist/default-deny.js} +103 -88
  31. package/dist/e2e-flow.d.ts +25 -0
  32. package/{e2e-flow.mjs → dist/e2e-flow.js} +113 -112
  33. package/dist/edge-hardening.d.ts +10 -0
  34. package/dist/edge-hardening.js +173 -0
  35. package/dist/error-handling.d.ts +21 -0
  36. package/{error-handling.mjs → dist/error-handling.js} +289 -239
  37. package/dist/event-bus.d.ts +14 -0
  38. package/{event-bus.mjs → dist/event-bus.js} +146 -129
  39. package/dist/feature-thin.d.ts +12 -0
  40. package/{feature-thin.mjs → dist/feature-thin.js} +208 -191
  41. package/dist/function-budget.d.ts +7 -0
  42. package/{function-budget.mjs → dist/function-budget.js} +24 -27
  43. package/dist/health-ready.d.ts +10 -0
  44. package/dist/health-ready.js +703 -0
  45. package/dist/idempotency.d.ts +10 -0
  46. package/{idempotency.mjs → dist/idempotency.js} +296 -226
  47. package/dist/index.d.ts +54 -0
  48. package/{index.mjs → dist/index.js} +91 -109
  49. package/dist/infra-owner.d.ts +18 -0
  50. package/{infra-owner.mjs → dist/infra-owner.js} +58 -51
  51. package/dist/injection.d.ts +30 -0
  52. package/{injection.mjs → dist/injection.js} +397 -344
  53. package/dist/input-bounds.d.ts +15 -0
  54. package/{input-bounds.mjs → dist/input-bounds.js} +143 -132
  55. package/dist/jobs.d.ts +17 -0
  56. package/{jobs.mjs → dist/jobs.js} +116 -99
  57. package/dist/jwt-alg.d.ts +6 -0
  58. package/dist/jwt-alg.js +49 -0
  59. package/dist/lib/app-roles.d.ts +25 -0
  60. package/{lib/app-roles.mjs → dist/lib/app-roles.js} +15 -17
  61. package/dist/lib/ast.d.ts +40 -0
  62. package/dist/lib/ast.js +100 -0
  63. package/dist/lib/client-idempotency.d.ts +3 -0
  64. package/{lib/client-idempotency.mjs → dist/lib/client-idempotency.js} +316 -268
  65. package/dist/lib/config.d.ts +36 -0
  66. package/{lib/config.mjs → dist/lib/config.js} +24 -27
  67. package/dist/lib/db-writes.d.ts +29 -0
  68. package/{lib/db-writes.mjs → dist/lib/db-writes.js} +27 -27
  69. package/dist/lib/declared.d.ts +77 -0
  70. package/{lib/declared.mjs → dist/lib/declared.js} +83 -78
  71. package/dist/lib/doors.d.ts +50 -0
  72. package/{lib/doors.mjs → dist/lib/doors.js} +45 -50
  73. package/dist/lib/edition.d.ts +25 -0
  74. package/{lib/edition.mjs → dist/lib/edition.js} +15 -20
  75. package/dist/lib/entity-writes.d.ts +7 -0
  76. package/{lib/entity-writes.mjs → dist/lib/entity-writes.js} +31 -22
  77. package/dist/lib/hfs.d.ts +34 -0
  78. package/{lib/hfs.mjs → dist/lib/hfs.js} +31 -29
  79. package/dist/lib/import-source.d.ts +63 -0
  80. package/dist/lib/import-source.js +134 -0
  81. package/dist/lib/path.d.ts +8 -0
  82. package/{lib/path.mjs → dist/lib/path.js} +4 -7
  83. package/dist/lib/persistence.d.ts +115 -0
  84. package/{lib/persistence.mjs → dist/lib/persistence.js} +55 -66
  85. package/dist/lib/ports.d.ts +70 -0
  86. package/{lib/ports.mjs → dist/lib/ports.js} +62 -66
  87. package/dist/lib/rule.d.ts +31 -0
  88. package/dist/lib/rule.js +2 -0
  89. package/dist/lib/runtime-root.d.ts +4 -0
  90. package/dist/lib/runtime-root.js +10 -0
  91. package/dist/lib/schema-values.d.ts +13 -0
  92. package/{lib/schema-values.mjs → dist/lib/schema-values.js} +67 -59
  93. package/dist/lib/specifier.d.ts +12 -0
  94. package/dist/lib/specifier.js +29 -0
  95. package/dist/lib/subject-values.d.ts +4 -0
  96. package/dist/lib/subject-values.js +61 -0
  97. package/dist/lib/transactions.d.ts +18 -0
  98. package/{lib/transactions.mjs → dist/lib/transactions.js} +17 -15
  99. package/dist/lib/transport-slots.d.ts +17 -0
  100. package/{lib/transport-slots.mjs → dist/lib/transport-slots.js} +2 -4
  101. package/dist/lib/types.d.ts +96 -0
  102. package/dist/lib/types.js +150 -0
  103. package/dist/lib/unit-spec.d.ts +48 -0
  104. package/{lib/unit-spec.mjs → dist/lib/unit-spec.js} +35 -44
  105. package/dist/lib/webhook-ack.d.ts +4 -0
  106. package/{lib/webhook-ack.mjs → dist/lib/webhook-ack.js} +78 -62
  107. package/dist/lib/webhook-handoff-inbox.d.ts +3 -0
  108. package/{lib/webhook-handoff-inbox.mjs → dist/lib/webhook-handoff-inbox.js} +195 -167
  109. package/dist/lib/webhook-handoff-intake.d.ts +4 -0
  110. package/{lib/webhook-handoff-intake.mjs → dist/lib/webhook-handoff-intake.js} +125 -102
  111. package/dist/lib/webhook-handoff.d.ts +15 -0
  112. package/{lib/webhook-handoff.mjs → dist/lib/webhook-handoff.js} +107 -100
  113. package/dist/lib/webhook-verifiers.d.ts +20 -0
  114. package/{lib/webhook-verifiers.mjs → dist/lib/webhook-verifiers.js} +108 -104
  115. package/dist/lib/why.d.ts +9 -0
  116. package/{lib/why.mjs → dist/lib/why.js} +6 -6
  117. package/dist/log-safety.d.ts +20 -0
  118. package/dist/log-safety.js +308 -0
  119. package/dist/module-layering.d.ts +42 -0
  120. package/dist/module-layering.js +257 -0
  121. package/dist/module-shape.d.ts +47 -0
  122. package/{module-shape.mjs → dist/module-shape.js} +278 -236
  123. package/dist/naming.d.ts +49 -0
  124. package/dist/naming.js +246 -0
  125. package/dist/observability.d.ts +25 -0
  126. package/dist/observability.js +245 -0
  127. package/dist/operations.d.ts +23 -0
  128. package/{operations.mjs → dist/operations.js} +410 -332
  129. package/dist/project-graph.d.ts +9 -0
  130. package/{project-graph.mjs → dist/project-graph.js} +8 -13
  131. package/dist/projections.d.ts +11 -0
  132. package/{projections.mjs → dist/projections.js} +60 -57
  133. package/dist/query-safety.d.ts +21 -0
  134. package/{query-safety.mjs → dist/query-safety.js} +92 -108
  135. package/dist/realtime.d.ts +15 -0
  136. package/{realtime.mjs → dist/realtime.js} +108 -106
  137. package/dist/resilience.d.ts +15 -0
  138. package/dist/resilience.js +319 -0
  139. package/dist/schema-authority.d.ts +15 -0
  140. package/{schema-authority.mjs → dist/schema-authority.js} +256 -220
  141. package/dist/service-deps.d.ts +17 -0
  142. package/{service-deps.mjs → dist/service-deps.js} +133 -130
  143. package/dist/service-isolation.d.ts +8 -0
  144. package/dist/service-isolation.js +76 -0
  145. package/dist/size-budget.d.ts +9 -0
  146. package/{size-budget.mjs → dist/size-budget.js} +27 -27
  147. package/dist/sonar-parity.d.ts +5 -0
  148. package/{sonar-parity.mjs → dist/sonar-parity.js} +7 -7
  149. package/dist/spec-no-skip.d.ts +9 -0
  150. package/dist/spec-no-skip.js +81 -0
  151. package/dist/spec-quality.d.ts +10 -0
  152. package/{spec-quality.mjs → dist/spec-quality.js} +103 -87
  153. package/dist/suppression.d.ts +26 -0
  154. package/{suppression.mjs → dist/suppression.js} +30 -45
  155. package/dist/temporal.d.ts +9 -0
  156. package/{temporal.mjs → dist/temporal.js} +75 -65
  157. package/dist/test-world.d.ts +28 -0
  158. package/{test-world.mjs → dist/test-world.js} +102 -83
  159. package/dist/testing.d.ts +51 -0
  160. package/dist/testing.js +368 -0
  161. package/dist/third-party-owner.d.ts +8 -0
  162. package/{third-party-owner.mjs → dist/third-party-owner.js} +105 -91
  163. package/dist/transport.d.ts +24 -0
  164. package/{transport.mjs → dist/transport.js} +279 -240
  165. package/dist/type-safety.d.ts +25 -0
  166. package/dist/type-safety.js +297 -0
  167. package/dist/unit-spec.d.ts +30 -0
  168. package/{unit-spec.mjs → dist/unit-spec.js} +473 -399
  169. package/dist/uploads.d.ts +9 -0
  170. package/{uploads.mjs → dist/uploads.js} +62 -54
  171. package/dist/user-copy.d.ts +9 -0
  172. package/{user-copy.mjs → dist/user-copy.js} +76 -67
  173. package/dist/webhooks.d.ts +16 -0
  174. package/{webhooks.mjs → dist/webhooks.js} +243 -231
  175. package/package.json +17 -8
  176. package/runtime/knowledge/hfs/canon-pins.yaml +17 -10
  177. package/async-discipline.mjs +0 -83
  178. package/comments.mjs +0 -416
  179. package/edge-hardening.mjs +0 -139
  180. package/health-ready.mjs +0 -539
  181. package/jwt-alg.mjs +0 -45
  182. package/lib/ast.mjs +0 -88
  183. package/lib/import-source.mjs +0 -104
  184. package/lib/specifier.mjs +0 -27
  185. package/lib/subject-values.mjs +0 -55
  186. package/lib/types.mjs +0 -133
  187. package/log-safety.mjs +0 -303
  188. package/module-layering.mjs +0 -270
  189. package/naming.mjs +0 -251
  190. package/observability.mjs +0 -241
  191. package/resilience.mjs +0 -304
  192. package/service-isolation.mjs +0 -75
  193. package/spec-no-skip.mjs +0 -85
  194. package/testing.mjs +0 -384
  195. package/type-safety.mjs +0 -323
@@ -11,75 +11,78 @@
11
11
  * capability, a bus is a receiver typed `CommandBus` / `QueryBus`, an entity is a class decorated with `Entity` from
12
12
  * `typeorm`. No rule matches a variable name, a class name or a directory spelled in a path pattern.
13
13
  */
14
- import ts from "typescript"
15
- import { decoratorCallee, importOf, isImportedFrom, moduleReferences } from "./lib/import-source.mjs"
16
- import { hfsOf } from "./lib/hfs.mjs"
17
- import { walk } from "./lib/ast.mjs"
18
- import { isLoggerType } from "./lib/ports.mjs"
19
- import { isTransportSlot } from "./lib/transport-slots.mjs"
20
- import { isPackageType, typeOrigins, typed } from "./lib/types.mjs"
21
-
14
+ import ts from "typescript";
15
+ import { decoratorCallee, importOf, isImportedFrom, moduleReferences } from "./lib/import-source.js";
16
+ import { hfsOf } from "./lib/hfs.js";
17
+ import { walk } from "./lib/ast.js";
18
+ import { isLoggerType } from "./lib/ports.js";
19
+ import { isTransportSlot } from "./lib/transport-slots.js";
20
+ import { isPackageType, typeOrigins, typed } from "./lib/types.js";
21
+ import { isNode } from "./lib/rule.js";
22
22
  /** The slot that holds messages and handlers. */
23
- const APPLICATION = "be.feature.application"
24
-
23
+ const APPLICATION = "be.feature.application";
25
24
  /** The slots that hold application-layer code (the support folder is the same layer). */
26
- const APPLICATION_LAYER = new Set([APPLICATION, "be.feature.application.support"])
27
-
25
+ const APPLICATION_LAYER = new Set([APPLICATION, "be.feature.application.support"]);
28
26
  /** The package that owns the CQRS types. */
29
- const CQRS_PACKAGE = "@nestjs/cqrs"
30
-
27
+ const CQRS_PACKAGE = "@nestjs/cqrs";
31
28
  /** The base name of a filename. */
32
- const baseName = (filename) => String(filename).replace(/\\/g, "/").split("/").pop()
33
-
29
+ const baseName = (filename) => String(filename).replace(/\\/g, "/").split("/").pop() ?? "";
30
+ /** Whether a value is a TypeScript type (it carries numeric `flags` and the `getSymbol` method). */
31
+ const isTsType = (value) => typeof value === "object" && value !== null && "flags" in value && typeof value.flags === "number" && "getSymbol" in value;
32
+ /** Whether an object type is a reference to a generic type (`Set<T>`, `Array<T>`), which carries type arguments. */
33
+ const isTypeReference = (type) => (type.flags & ts.TypeFlags.Object) !== 0 && "objectFlags" in type && typeof type.objectFlags === "number" && (type.objectFlags & ts.ObjectFlags.Reference) !== 0;
34
+ /** The key of a class member, or undefined for a member that has none (a static block, an index signature). */
35
+ const memberKeyOf = (member) => ("key" in member ? member.key : undefined);
34
36
  /** The bus receivers of `@nestjs/cqrs`. */
35
- const isBus = (context, node) => isPackageType(context, node, "CommandBus", CQRS_PACKAGE) || isPackageType(context, node, "QueryBus", CQRS_PACKAGE)
36
-
37
+ const isBus = (context, node) => isPackageType(context, node, "CommandBus", CQRS_PACKAGE) || isPackageType(context, node, "QueryBus", CQRS_PACKAGE);
37
38
  /** True when a declaration file belongs to the capability `capability` of a platform slot. */
38
39
  const declaredByCapability = (hfs, file, capability) => {
39
- if (hfs.slotOf(file) !== "be.platform") return false
40
- const owner = hfs.ownerOf(file)
41
- return owner !== null && owner.split("/").pop() === capability
42
- }
43
-
40
+ if (hfs.slotOf(file) !== "be.platform")
41
+ return false;
42
+ const owner = hfs.ownerOf(file);
43
+ return owner !== null && owner.split("/").pop() === capability;
44
+ };
44
45
  /** The classes a file declares. */
45
- const classVisitors = (visit) => ({ ClassDeclaration: visit, ClassExpression: visit })
46
-
46
+ const classVisitors = (visit) => ({ ClassDeclaration: visit, ClassExpression: visit });
47
47
  /** The constructor of a class body, or undefined. */
48
- const constructorOf = (node) => node.body.body.find((member) => member.type === "MethodDefinition" && member.kind === "constructor")
49
-
48
+ const constructorOf = (node) => node.body.body.find((member) => member.type === "MethodDefinition" && member.kind === "constructor");
50
49
  /** The message kind (`command` / `query`) of a file in the application slot, or null. */
51
50
  const messageKindOf = (context) => {
52
- const filename = context.filename
53
- const name = baseName(filename)
54
- const kind = name.endsWith(".command.ts") ? "command" : name.endsWith(".query.ts") ? "query" : null
55
- if (kind === null) return null
56
- return hfsOf(context).slotOf(filename) === APPLICATION ? kind : null
57
- }
58
-
51
+ const filename = context.filename;
52
+ const name = baseName(filename);
53
+ const kind = name.endsWith(".command.ts") ? "command" : name.endsWith(".query.ts") ? "query" : null;
54
+ if (kind === null)
55
+ return null;
56
+ return hfsOf(context).slotOf(filename) === APPLICATION ? kind : null;
57
+ };
59
58
  // -- handler-overrides-process -----------------------------------------------------------------------------------
60
-
61
59
  /** True when the class (transitively) extends the `ICQRSHandler` of `platform/cqrs`. */
62
60
  const extendsTemplate = (context, node, hfs) => {
63
- const { checker, toTs } = typed(context)
64
- const seen = new Set()
61
+ const { checker, toTs } = typed(context);
62
+ const seen = new Set();
65
63
  const reaches = (type) => {
66
- if (!type || seen.has(type)) return false
67
- seen.add(type)
68
- const symbol = type.getSymbol?.()
69
- if (symbol?.getName() === "ICQRSHandler" && (symbol.getDeclarations() ?? []).some((declaration) => declaredByCapability(hfs, declaration.getSourceFile().fileName, "cqrs"))) return true
70
- return (checker.getBaseTypes(type.target ?? type) ?? []).some(reaches)
71
- }
72
- return reaches(checker.getTypeAtLocation(toTs(node)))
73
- }
74
-
64
+ if (!type || seen.has(type))
65
+ return false;
66
+ seen.add(type);
67
+ const symbol = type.getSymbol?.();
68
+ if (symbol?.getName() === "ICQRSHandler" && (symbol.getDeclarations() ?? []).some((declaration) => declaredByCapability(hfs, declaration.getSourceFile().fileName, "cqrs")))
69
+ return true;
70
+ // `type.target ?? type`: a reference resolves to its generic class or interface, whose base types the checker knows.
71
+ const target = Reflect.get(type, "target");
72
+ const resolved = target ?? type;
73
+ if (!isTsType(resolved) || !resolved.isClassOrInterface())
74
+ return false;
75
+ return (checker.getBaseTypes(resolved) ?? []).some(reaches);
76
+ };
77
+ return reaches(checker.getTypeAtLocation(toTs(node)));
78
+ };
75
79
  /** True when `process` is still the template's abstract member (no class of the chain implemented it). */
76
80
  const processIsAbstract = (context, node) => {
77
- const { checker, toTs } = typed(context)
78
- const symbol = checker.getPropertyOfType(checker.getTypeAtLocation(toTs(node)), "process")
79
- const declarations = symbol?.getDeclarations() ?? []
80
- return declarations.length === 0 || declarations.every((declaration) => (ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Abstract) !== 0)
81
- }
82
-
81
+ const { checker, toTs } = typed(context);
82
+ const symbol = checker.getPropertyOfType(checker.getTypeAtLocation(toTs(node)), "process");
83
+ const declarations = symbol?.getDeclarations() ?? [];
84
+ return declarations.length === 0 || declarations.every((declaration) => (ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Abstract) !== 0);
85
+ };
83
86
  /** A handler implements the template's `process` and never overrides its public `execute`. */
84
87
  export const handlerOverridesProcess = {
85
88
  meta: {
@@ -93,50 +96,49 @@ export const handlerOverridesProcess = {
93
96
  },
94
97
  },
95
98
  create(context) {
96
- const hfs = hfsOf(context)
97
- if (hfs.slotOf(context.filename) !== APPLICATION) return {}
99
+ const hfs = hfsOf(context);
100
+ if (hfs.slotOf(context.filename) !== APPLICATION)
101
+ return {};
98
102
  return classVisitors((node) => {
99
- if (!(node.decorators ?? []).some((decorator) => isImportedFrom(context, decoratorCallee(decorator), CQRS_PACKAGE, ["CommandHandler", "QueryHandler"]))) return
100
- const name = node.id?.name ?? "this handler"
101
- const own = (member) => node.body.body.find((candidate) => candidate.type === "MethodDefinition" && candidate.key?.type === "Identifier" && candidate.key.name === member)
102
- const execute = own("execute")
103
+ if (!(node.decorators ?? []).some((decorator) => isImportedFrom(context, decoratorCallee(decorator), CQRS_PACKAGE, ["CommandHandler", "QueryHandler"])))
104
+ return;
105
+ const name = node.id?.name ?? "this handler";
106
+ const own = (member) => node.body.body.find((candidate) => candidate.type === "MethodDefinition" && candidate.key?.type === "Identifier" && candidate.key.name === member);
107
+ const execute = own("execute");
103
108
  if (execute) {
104
- context.report({ node: execute.key, messageId: "overridesExecute", data: { name } })
105
- return
109
+ context.report({ node: execute.key, messageId: "overridesExecute", data: { name } });
110
+ return;
106
111
  }
107
112
  if (!extendsTemplate(context, node, hfs)) {
108
- context.report({ node: node.id ?? node, messageId: "notTemplate", data: { name } })
109
- return
113
+ context.report({ node: node.id ?? node, messageId: "notTemplate", data: { name } });
114
+ return;
110
115
  }
111
- if (!own("process") && processIsAbstract(context, node)) context.report({ node: node.id ?? node, messageId: "noProcess", data: { name } })
112
- })
116
+ if (!own("process") && processIsAbstract(context, node))
117
+ context.report({ node: node.id ?? node, messageId: "noProcess", data: { name } });
118
+ });
113
119
  },
114
- }
115
-
120
+ };
116
121
  // -- handler-is-thin ---------------------------------------------------------------------------------------------
117
-
118
122
  /** Statements and expressions that are a decision or a repetition: none of them belongs in a handler. */
119
- const DECISION_NODES = new Set(["IfStatement", "SwitchStatement", "ConditionalExpression", "LogicalExpression", "ForStatement", "ForInStatement", "ForOfStatement", "WhileStatement", "DoWhileStatement", "TryStatement", "ThrowStatement"])
120
-
123
+ const DECISION_NODES = new Set(["IfStatement", "SwitchStatement", "ConditionalExpression", "LogicalExpression", "ForStatement", "ForInStatement", "ForOfStatement", "WhileStatement", "DoWhileStatement", "TryStatement", "ThrowStatement"]);
121
124
  /** The one collaborator a handler may hold: a domain service (a class named `*Service` declared in a `*.service.ts` file) or a projection (a class declared in a `*.projection.ts` file of slot `be.projections`). */
122
125
  const isServiceType = (context, hfs, annotation) => typeOrigins(context, annotation).some((origin) => origin.module === null
123
- && ((origin.name.endsWith("Service") && origin.file.endsWith(".service.ts")) || (origin.file.endsWith(".projection.ts") && hfs.slotOf(origin.file) === "be.projections")))
124
-
126
+ && ((origin.name.endsWith("Service") && origin.file.endsWith(".service.ts")) || (origin.file.endsWith(".projection.ts") && hfs.slotOf(origin.file) === "be.projections")));
125
127
  /** The injected members of a class: constructor parameters and decorated properties, with the type annotation of each. */
126
128
  const injectedMembers = (node) => {
127
- const members = []
128
- const constructor = constructorOf(node)
129
+ const members = [];
130
+ const constructor = constructorOf(node);
129
131
  for (const original of constructor?.value.params ?? []) {
130
- const param = original.type === "TSParameterProperty" ? original.parameter : original
131
- members.push({ report: original, param, name: original.type === "TSParameterProperty" && param.type === "Identifier" ? param.name : null, annotation: param.typeAnnotation?.typeAnnotation })
132
+ const param = original.type === "TSParameterProperty" ? original.parameter : original;
133
+ members.push({ report: original, param, name: original.type === "TSParameterProperty" && param.type === "Identifier" ? param.name : null, annotation: param.typeAnnotation?.typeAnnotation });
132
134
  }
133
135
  for (const member of node.body.body) {
134
- if (member.type !== "PropertyDefinition" || (member.decorators ?? []).length === 0) continue
135
- members.push({ report: member, param: member, name: member.key.type === "Identifier" ? member.key.name : null, annotation: member.typeAnnotation?.typeAnnotation })
136
+ if (member.type !== "PropertyDefinition" || (member.decorators ?? []).length === 0)
137
+ continue;
138
+ members.push({ report: member, param: member, name: member.key.type === "Identifier" ? member.key.name : null, annotation: member.typeAnnotation?.typeAnnotation });
136
139
  }
137
- return members
138
- }
139
-
140
+ return members;
141
+ };
140
142
  /** A handler maps its input, calls exactly one method of an injected service and returns its result. */
141
143
  export const handlerIsThin = {
142
144
  meta: {
@@ -151,43 +153,50 @@ export const handlerIsThin = {
151
153
  },
152
154
  },
153
155
  create(context) {
154
- const hfs = hfsOf(context)
155
- if (hfs.slotOf(context.filename) !== APPLICATION) return {}
156
- const sourceCode = context.sourceCode
156
+ const hfs = hfsOf(context);
157
+ if (hfs.slotOf(context.filename) !== APPLICATION)
158
+ return {};
159
+ const sourceCode = context.sourceCode;
157
160
  return classVisitors((node) => {
158
- if (!(node.decorators ?? []).some((decorator) => isImportedFrom(context, decoratorCallee(decorator), CQRS_PACKAGE, ["CommandHandler", "QueryHandler"]))) return
159
- const name = node.id?.name ?? "this handler"
160
- const services = new Set()
161
+ if (!(node.decorators ?? []).some((decorator) => isImportedFrom(context, decoratorCallee(decorator), CQRS_PACKAGE, ["CommandHandler", "QueryHandler"])))
162
+ return;
163
+ const name = node.id?.name ?? "this handler";
164
+ const services = new Set();
161
165
  for (const injected of injectedMembers(node)) {
162
166
  if (injected.annotation && isServiceType(context, hfs, injected.annotation)) {
163
- if (injected.name !== null) services.add(injected.name)
164
- } else if (!injected.annotation || !isLoggerType(context, injected.annotation)) {
165
- context.report({ node: injected.report, messageId: "dependency", data: { what: sourceCode.getText(injected.param) } })
167
+ if (injected.name !== null)
168
+ services.add(injected.name);
169
+ }
170
+ else if (!injected.annotation || !isLoggerType(context, injected.annotation)) {
171
+ context.report({ node: injected.report, messageId: "dependency", data: { what: sourceCode.getText(injected.param) } });
166
172
  }
167
173
  }
168
- const method = node.body.body.find((member) => member.type === "MethodDefinition" && member.kind === "method" && member.key.type === "Identifier" && member.key.name === "process")
169
- const body = method?.value.body
170
- if (!body) return
171
- let calls = 0
174
+ const method = node.body.body.find((member) => member.type === "MethodDefinition" && member.kind === "method" && member.key.type === "Identifier" && member.key.name === "process");
175
+ const body = method?.value.body;
176
+ if (!method || !body)
177
+ return;
178
+ let calls = 0;
172
179
  walk(body, (child) => {
173
- if (DECISION_NODES.has(child.type)) context.report({ node: child, messageId: "branch", data: { what: child.type === "LogicalExpression" ? child.operator : child.type.replace(/(Statement|Expression)$/, "").toLowerCase() } })
174
- else if (child.type === "CallExpression") calls += 1
175
- })
176
- const only = body.body.length === 1 && body.body[0].type === "ReturnStatement" ? body.body[0].argument : null
180
+ if (DECISION_NODES.has(child.type))
181
+ context.report({ node: child, messageId: "branch", data: { what: child.type === "LogicalExpression" ? child.operator : child.type.replace(/(Statement|Expression)$/, "").toLowerCase() } });
182
+ else if (child.type === "CallExpression")
183
+ calls += 1;
184
+ });
185
+ const [head] = body.body;
186
+ const only = body.body.length === 1 && head?.type === "ReturnStatement" ? head.argument : null;
177
187
  if (only === null) {
178
- context.report({ node: method.key, messageId: "body", data: { name } })
179
- return
188
+ context.report({ node: method.key, messageId: "body", data: { name } });
189
+ return;
180
190
  }
181
- const call = only.type === "AwaitExpression" ? only.argument : only
182
- const callee = call.type === "CallExpression" ? call.callee : null
183
- const onService = callee?.type === "MemberExpression" && !callee.computed && callee.object.type === "MemberExpression" && !callee.object.computed && callee.object.object.type === "ThisExpression" && callee.object.property.type === "Identifier" && services.has(callee.object.property.name)
184
- if (calls !== 1 || !onService) context.report({ node: method.key, messageId: "call", data: { name, count: String(calls) } })
185
- })
191
+ const call = only.type === "AwaitExpression" ? only.argument : only;
192
+ const callee = call.type === "CallExpression" ? call.callee : null;
193
+ const onService = callee?.type === "MemberExpression" && !callee.computed && callee.object.type === "MemberExpression" && !callee.object.computed && callee.object.object.type === "ThisExpression" && callee.object.property.type === "Identifier" && services.has(callee.object.property.name);
194
+ if (calls !== 1 || !onService)
195
+ context.report({ node: method.key, messageId: "call", data: { name, count: String(calls) } });
196
+ });
186
197
  },
187
- }
188
-
198
+ };
189
199
  // -- message-carries-params-only ---------------------------------------------------------------------------------
190
-
191
200
  /** A message carries request context and nothing else. */
192
201
  export const messageCarriesParamsOnly = {
193
202
  meta: {
@@ -201,32 +210,36 @@ export const messageCarriesParamsOnly = {
201
210
  },
202
211
  },
203
212
  create(context) {
204
- if (messageKindOf(context) === null) return {}
213
+ if (messageKindOf(context) === null)
214
+ return {};
205
215
  return classVisitors((node) => {
206
- const name = node.id?.name ?? "this message"
207
- const members = node.body.body
208
- const constructor = constructorOf(node)
216
+ const name = node.id?.name ?? "this message";
217
+ const members = node.body.body;
218
+ const constructor = constructorOf(node);
209
219
  for (const member of members) {
210
- if (member === constructor) continue
211
- const key = member.key?.type === "Identifier" ? member.key.name : "a member"
212
- context.report({ node: member.key ?? member, messageId: "member", data: { name, member: key } })
220
+ if (member === constructor)
221
+ continue;
222
+ const memberKey = memberKeyOf(member);
223
+ const key = memberKey?.type === "Identifier" ? memberKey.name : "a member";
224
+ context.report({ node: memberKey ?? member, messageId: "member", data: { name, member: key } });
213
225
  }
214
226
  if (!constructor) {
215
- context.report({ node: node.id ?? node, messageId: "shape", data: { name } })
216
- return
227
+ context.report({ node: node.id ?? node, messageId: "shape", data: { name } });
228
+ return;
217
229
  }
218
- const params = constructor.value.params
219
- const first = params[0]?.type === "TSParameterProperty" ? params[0].parameter : params[0]
220
- if (params.length !== 1 || first?.type !== "Identifier" || first.name !== "params") context.report({ node: constructor, messageId: "shape", data: { name } })
221
- const statements = constructor.value.body?.body ?? []
222
- const onlySuper = statements.every((statement) => statement.type === "ExpressionStatement" && statement.expression.type === "CallExpression" && statement.expression.callee.type === "Super")
223
- if (!onlySuper) context.report({ node: constructor, messageId: "body", data: { name } })
224
- })
230
+ const params = constructor.value.params;
231
+ const [firstParam] = params;
232
+ const first = firstParam?.type === "TSParameterProperty" ? firstParam.parameter : firstParam;
233
+ if (params.length !== 1 || first?.type !== "Identifier" || first.name !== "params")
234
+ context.report({ node: constructor, messageId: "shape", data: { name } });
235
+ const statements = constructor.value.body?.body ?? [];
236
+ const onlySuper = statements.every((statement) => statement.type === "ExpressionStatement" && statement.expression.type === "CallExpression" && statement.expression.callee.type === "Super");
237
+ if (!onlySuper)
238
+ context.report({ node: constructor, messageId: "body", data: { name } });
239
+ });
225
240
  },
226
- }
227
-
241
+ };
228
242
  // -- message-typed-result ----------------------------------------------------------------------------------------
229
-
230
243
  /** A message states the type of its result: `Command<R>` / `Query<R>` from `@nestjs/cqrs`, one readonly `params`. */
231
244
  export const messageTypedResult = {
232
245
  meta: {
@@ -240,86 +253,99 @@ export const messageTypedResult = {
240
253
  },
241
254
  },
242
255
  create(context) {
243
- const kind = messageKindOf(context)
244
- if (kind === null) return {}
245
- const base = kind === "command" ? "Command" : "Query"
256
+ const kind = messageKindOf(context);
257
+ if (kind === null)
258
+ return {};
259
+ const base = kind === "command" ? "Command" : "Query";
246
260
  return classVisitors((node) => {
247
- const name = node.id?.name ?? "this message"
261
+ const name = node.id?.name ?? "this message";
248
262
  if (!node.superClass || !isImportedFrom(context, node.superClass, CQRS_PACKAGE, base)) {
249
- context.report({ node: node.id ?? node, messageId: "base", data: { name, base } })
250
- } else if ((node.superTypeArguments ?? node.superTypeParameters)?.params.length !== 1) {
251
- context.report({ node: node.superClass, messageId: "result", data: { name, base } })
263
+ context.report({ node: node.id ?? node, messageId: "base", data: { name, base } });
264
+ }
265
+ else if (node.superTypeArguments?.params.length !== 1) {
266
+ context.report({ node: node.superClass, messageId: "result", data: { name, base } });
252
267
  }
253
- const constructor = constructorOf(node)
254
- if (!constructor) return
255
- const param = constructor.value.params[0]
268
+ const constructor = constructorOf(node);
269
+ if (!constructor)
270
+ return;
271
+ const param = constructor.value.params[0];
256
272
  const valid = constructor.value.params.length === 1
257
- && param.type === "TSParameterProperty"
273
+ && param?.type === "TSParameterProperty"
258
274
  && param.readonly === true
259
275
  && (param.accessibility === undefined || param.accessibility === "public")
260
276
  && param.parameter.type === "Identifier"
261
277
  && param.parameter.name === "params"
262
- && param.parameter.optional !== true
263
- if (!valid) context.report({ node: param ?? constructor, messageId: "params", data: { name } })
264
- })
278
+ && param.parameter.optional !== true;
279
+ if (!valid)
280
+ context.report({ node: param ?? constructor, messageId: "params", data: { name } });
281
+ });
265
282
  },
266
- }
267
-
283
+ };
268
284
  // -- execute-params-shape ----------------------------------------------------------------------------------------
269
-
270
285
  /** True for a class declaration decorated with `Entity` from `typeorm`. */
271
286
  const isEntityClass = (checker, declaration) => {
272
- if (!ts.isClassDeclaration(declaration)) return false
287
+ if (!ts.isClassDeclaration(declaration))
288
+ return false;
273
289
  return (ts.getDecorators(declaration) ?? []).some((decorator) => {
274
- const call = ts.isCallExpression(decorator.expression) ? decorator.expression.expression : decorator.expression
275
- if (!ts.isIdentifier(call)) return false
276
- let symbol = checker.getSymbolAtLocation(call)
277
- if (symbol && (symbol.flags & ts.SymbolFlags.Alias) !== 0) symbol = checker.getAliasedSymbol(symbol)
278
- return symbol?.getName() === "Entity" && (symbol.getDeclarations() ?? []).some((entity) => /\/node_modules\/typeorm\//.test(entity.getSourceFile().fileName.replace(/\\/g, "/")))
279
- })
280
- }
281
-
290
+ const call = ts.isCallExpression(decorator.expression) ? decorator.expression.expression : decorator.expression;
291
+ if (!ts.isIdentifier(call))
292
+ return false;
293
+ let symbol = checker.getSymbolAtLocation(call);
294
+ if (symbol && (symbol.flags & ts.SymbolFlags.Alias) !== 0)
295
+ symbol = checker.getAliasedSymbol(symbol);
296
+ return symbol?.getName() === "Entity" && (symbol.getDeclarations() ?? []).some((entity) => /\/node_modules\/typeorm\//.test(entity.getSourceFile().fileName.replace(/\\/g, "/")));
297
+ });
298
+ };
282
299
  /** The name of an `@Entity` class reachable from `root` through properties, index signatures, unions and type arguments, or null. */
283
300
  const reachableEntity = (context, root) => {
284
- const { checker, program } = typed(context)
285
- const seen = new Set()
286
- const stack = [root]
287
- let steps = 0
301
+ const { checker, program } = typed(context);
302
+ const seen = new Set();
303
+ const stack = [root];
304
+ let steps = 0;
288
305
  while (stack.length > 0 && steps < 4000) {
289
- steps += 1
290
- const type = stack.pop()
291
- if (seen.has(type)) continue
292
- seen.add(type)
306
+ steps += 1;
307
+ const type = stack.pop();
308
+ if (type === undefined)
309
+ break;
310
+ if (seen.has(type))
311
+ continue;
312
+ seen.add(type);
293
313
  if (type.isUnionOrIntersection()) {
294
- stack.push(...type.types)
295
- continue
314
+ stack.push(...type.types);
315
+ continue;
296
316
  }
297
- if ((type.flags & ts.TypeFlags.Object) !== 0 && (type.objectFlags & ts.ObjectFlags.Reference) !== 0) stack.push(...checker.getTypeArguments(type))
298
- const symbol = type.getSymbol() ?? type.aliasSymbol
299
- const declarations = symbol?.getDeclarations() ?? []
300
- const entity = declarations.find((declaration) => isEntityClass(checker, declaration))
301
- if (entity) return symbol.getName()
317
+ if (isTypeReference(type))
318
+ stack.push(...checker.getTypeArguments(type));
319
+ const symbol = type.getSymbol() ?? type.aliasSymbol;
320
+ const declarations = symbol?.getDeclarations() ?? [];
321
+ const entity = declarations.find((declaration) => isEntityClass(checker, declaration));
322
+ if (entity && symbol)
323
+ return symbol.getName();
302
324
  const own = declarations.some((declaration) => {
303
- const file = declaration.getSourceFile()
304
- return !program.isSourceFileFromExternalLibrary(file) && !program.isSourceFileDefaultLibrary(file)
305
- })
306
- if (!own) continue
307
- for (const property of checker.getPropertiesOfType(type)) stack.push(checker.getTypeOfSymbol(property))
308
- for (const info of checker.getIndexInfosOfType(type)) stack.push(info.type)
325
+ const file = declaration.getSourceFile();
326
+ return !program.isSourceFileFromExternalLibrary(file) && !program.isSourceFileDefaultLibrary(file);
327
+ });
328
+ if (!own)
329
+ continue;
330
+ for (const property of checker.getPropertiesOfType(type))
331
+ stack.push(checker.getTypeOfSymbol(property));
332
+ for (const info of checker.getIndexInfosOfType(type))
333
+ stack.push(info.type);
309
334
  }
310
- return null
311
- }
312
-
335
+ return null;
336
+ };
313
337
  /** True when a type annotation is a reference to the `ExecuteParams` or `PublicExecuteParams` that `platform/cqrs` declares (the symbol the name resolves to, so a local alias or lookalike is not it). */
314
338
  const namesPlatformParams = (context, hfs, annotation) => {
315
- if (annotation.type !== "TSTypeReference") return false
316
- const { checker, toTs } = typed(context)
317
- let symbol = checker.getSymbolAtLocation(toTs(annotation.typeName))
318
- if (symbol && (symbol.flags & ts.SymbolFlags.Alias) !== 0) symbol = checker.getAliasedSymbol(symbol)
319
- if (!symbol || (symbol.getName() !== "ExecuteParams" && symbol.getName() !== "PublicExecuteParams")) return false
320
- return (symbol.getDeclarations() ?? []).some((declaration) => declaredByCapability(hfs, declaration.getSourceFile().fileName, "cqrs"))
321
- }
322
-
339
+ if (annotation.type !== "TSTypeReference")
340
+ return false;
341
+ const { checker, toTs } = typed(context);
342
+ let symbol = checker.getSymbolAtLocation(toTs(annotation.typeName));
343
+ if (symbol && (symbol.flags & ts.SymbolFlags.Alias) !== 0)
344
+ symbol = checker.getAliasedSymbol(symbol);
345
+ if (!symbol || (symbol.getName() !== "ExecuteParams" && symbol.getName() !== "PublicExecuteParams"))
346
+ return false;
347
+ return (symbol.getDeclarations() ?? []).some((declaration) => declaredByCapability(hfs, declaration.getSourceFile().fileName, "cqrs"));
348
+ };
323
349
  /** A message's `params` is `ExecuteParams<X>` / `PublicExecuteParams<X>` of `platform/cqrs` and X holds no entity. */
324
350
  export const executeParamsShape = {
325
351
  meta: {
@@ -332,41 +358,47 @@ export const executeParamsShape = {
332
358
  },
333
359
  },
334
360
  create(context) {
335
- if (messageKindOf(context) === null) return {}
336
- const hfs = hfsOf(context)
361
+ if (messageKindOf(context) === null)
362
+ return {};
363
+ const hfs = hfsOf(context);
337
364
  return classVisitors((node) => {
338
- const name = node.id?.name ?? "this message"
339
- const constructor = constructorOf(node)
340
- const param = constructor?.value.params[0]
341
- const annotation = (param?.type === "TSParameterProperty" ? param.parameter : param)?.typeAnnotation?.typeAnnotation
342
- if (!annotation) return
365
+ const name = node.id?.name ?? "this message";
366
+ const constructor = constructorOf(node);
367
+ const param = constructor?.value.params[0];
368
+ const annotation = (param?.type === "TSParameterProperty" ? param.parameter : param)?.typeAnnotation?.typeAnnotation;
369
+ if (!annotation)
370
+ return;
343
371
  if (!namesPlatformParams(context, hfs, annotation)) {
344
- context.report({ node: annotation, messageId: "shape", data: { name } })
345
- return
372
+ context.report({ node: annotation, messageId: "shape", data: { name } });
373
+ return;
346
374
  }
347
- const argument = (annotation.typeArguments ?? annotation.typeParameters)?.params[0]
348
- if (!argument) return
349
- const { checker, toTs } = typed(context)
350
- const entity = reachableEntity(context, checker.getTypeFromTypeNode(toTs(argument)))
351
- if (entity !== null) context.report({ node: argument, messageId: "entity", data: { name, entity } })
352
- })
375
+ const argument = annotation.typeArguments?.params[0];
376
+ if (!argument)
377
+ return;
378
+ const { checker, toTs } = typed(context);
379
+ const tsArgument = toTs(argument);
380
+ // A type argument maps to a TS type node; the declared map also admits an identifier, read as typeOrigins reads one.
381
+ const entity = reachableEntity(context, ts.isTypeNode(tsArgument) ? checker.getTypeFromTypeNode(tsArgument) : checker.getTypeAtLocation(tsArgument));
382
+ if (entity !== null)
383
+ context.report({ node: argument, messageId: "entity", data: { name, entity } });
384
+ });
353
385
  },
354
- }
355
-
386
+ };
356
387
  // -- no-use-case -------------------------------------------------------------------------------------------------
357
-
358
388
  /** True when every method body of the class is `return this.<bus>.execute(...)` (at least one method). */
359
389
  const isForwarder = (context, node) => {
360
- const methods = node.body.body.filter((member) => member.type === "MethodDefinition" && member.kind === "method")
361
- if (methods.length === 0) return false
390
+ const methods = node.body.body.filter((member) => member.type === "MethodDefinition" && member.kind === "method");
391
+ if (methods.length === 0)
392
+ return false;
362
393
  return methods.every((method) => {
363
- const statements = method.value.body?.body ?? []
364
- if (statements.length !== 1 || statements[0].type !== "ReturnStatement") return false
365
- const call = statements[0].argument?.type === "AwaitExpression" ? statements[0].argument.argument : statements[0].argument
366
- return call?.type === "CallExpression" && call.callee.type === "MemberExpression" && !call.callee.computed && call.callee.property.type === "Identifier" && call.callee.property.name === "execute" && isBus(context, call.callee.object)
367
- })
368
- }
369
-
394
+ const statements = method.value.body?.body ?? [];
395
+ const [statement] = statements;
396
+ if (statements.length !== 1 || statement?.type !== "ReturnStatement")
397
+ return false;
398
+ const call = statement.argument?.type === "AwaitExpression" ? statement.argument.argument : statement.argument;
399
+ return call?.type === "CallExpression" && call.callee.type === "MemberExpression" && !call.callee.computed && call.callee.property.type === "Identifier" && call.callee.property.name === "execute" && isBus(context, call.callee.object);
400
+ });
401
+ };
370
402
  /** No use-case class and no forwarder service: the transport dispatches on the bus itself. */
371
403
  export const noUseCase = {
372
404
  meta: {
@@ -380,52 +412,54 @@ export const noUseCase = {
380
412
  },
381
413
  },
382
414
  create(context) {
383
- const hfs = hfsOf(context)
384
- const filename = context.filename
385
- const slot = hfs.slotOf(filename)
386
- const visitors = {}
415
+ const hfs = hfsOf(context);
416
+ const filename = context.filename;
417
+ const slot = hfs.slotOf(filename);
418
+ const visitors = {};
387
419
  if (APPLICATION_LAYER.has(slot)) {
388
- const name = baseName(filename)
420
+ const name = baseName(filename);
389
421
  visitors.Program = (node) => {
390
- if (name.endsWith(".use-case.ts")) context.report({ node, loc: { line: 1, column: 0 }, messageId: "file", data: { file: name } })
391
- }
422
+ if (name.endsWith(".use-case.ts"))
423
+ context.report({ node, loc: { line: 1, column: 0 }, messageId: "file", data: { file: name } });
424
+ };
392
425
  visitors.ClassDeclaration = (node) => {
393
- if (node.id && node.id.name.endsWith("UseCase")) context.report({ node: node.id, messageId: "klass", data: { name: node.id.name } })
394
- }
426
+ if (node.id && node.id.name.endsWith("UseCase"))
427
+ context.report({ node: node.id, messageId: "klass", data: { name: node.id.name } });
428
+ };
395
429
  }
396
430
  if ((slot === APPLICATION || isTransportSlot(slot)) && baseName(filename).endsWith(".service.ts")) {
397
- const previous = visitors.ClassDeclaration
431
+ const previous = visitors.ClassDeclaration;
398
432
  visitors.ClassDeclaration = (node) => {
399
- previous?.(node)
400
- if (isForwarder(context, node)) context.report({ node: node.id ?? node, messageId: "forwarder", data: { name: node.id?.name ?? "this service" } })
401
- }
433
+ previous?.(node);
434
+ if (isForwarder(context, node))
435
+ context.report({ node: node.id ?? node, messageId: "forwarder", data: { name: node.id?.name ?? "this service" } });
436
+ };
402
437
  }
403
- return visitors
438
+ return visitors;
404
439
  },
405
- }
406
-
440
+ };
407
441
  // -- no-event-bus ------------------------------------------------------------------------------------------------
408
-
409
442
  /** The bus class a constructed or extended expression is declared as (by declaration origin), else null. `classes` is the `ruleParams.be.eventBus.classes` map: module specifier -> class names. */
410
443
  const busClassOf = (context, node, classes) => {
411
- const origin = typeOrigins(context, node).find((entry) => entry.module !== null && classes[entry.module]?.includes(entry.name))
412
- return origin ? origin.name : null
413
- }
414
-
444
+ const origin = typeOrigins(context, node).find((entry) => entry.module !== null && classes[entry.module]?.includes(entry.name));
445
+ return origin ? origin.name : null;
446
+ };
415
447
  /** The methods that register a subscriber on a collection. */
416
- const REGISTER_METHODS = new Set(["add", "push", "unshift", "set"])
417
-
448
+ const REGISTER_METHODS = new Set(["add", "push", "unshift", "set"]);
418
449
  /** True for a type that is a function: it has a call signature and no construct signature. */
419
- const isFunctionType = (type) => type.getCallSignatures().length > 0 && type.getConstructSignatures().length === 0
420
-
450
+ const isFunctionType = (type) => type.getCallSignatures().length > 0 && type.getConstructSignatures().length === 0;
421
451
  /** True when a collection type (Set, Array, Map) holds functions. */
422
452
  const holdsFunctions = (checker, type) => {
423
- const symbolName = type.getSymbol()?.getName()
424
- if (!["Set", "Array", "ReadonlyArray", "Map", "WeakSet"].includes(symbolName ?? "")) return false
425
- const args = checker.getTypeArguments(type)
426
- return args.length > 0 && isFunctionType(args[args.length - 1])
427
- }
428
-
453
+ const symbolName = type.getSymbol()?.getName();
454
+ if (!["Set", "Array", "ReadonlyArray", "Map", "WeakSet"].includes(symbolName ?? ""))
455
+ return false;
456
+ // A collection type that is not a generic reference has no type arguments: the checker answers [] for it.
457
+ if (!isTypeReference(type))
458
+ return false;
459
+ const args = checker.getTypeArguments(type);
460
+ const last = args[args.length - 1];
461
+ return args.length > 0 && last !== undefined && isFunctionType(last);
462
+ };
429
463
  /** No in-process events: a side effect that must happen anyway is an event published through the bus. */
430
464
  export const noEventBus = {
431
465
  meta: {
@@ -439,67 +473,93 @@ export const noEventBus = {
439
473
  },
440
474
  },
441
475
  create(context) {
442
- const report = (node, name) => context.report({ node, messageId: "event", data: { name } })
443
- const eventBus = hfsOf(context).ruleParams.eventBus
444
- const eventNames = new Set(eventBus.imports[CQRS_PACKAGE] ?? [])
445
- const { checker, toTs } = typed(context)
476
+ const report = (node, name) => context.report({ node, messageId: "event", data: { name } });
477
+ const eventBus = hfsOf(context).ruleParams.eventBus;
478
+ const eventNames = new Set(eventBus.imports[CQRS_PACKAGE] ?? []);
479
+ const { checker, toTs } = typed(context);
446
480
  /** Collects, per class, the functions-collection properties and the registrations made on them. */
447
481
  const checkClass = (classNode) => {
448
- const collections = new Map()
482
+ const collections = new Map();
449
483
  for (const member of classNode.body.body) {
450
- if (member.type !== "PropertyDefinition" || member.computed || member.key.type !== "Identifier") continue
451
- const tsKey = toTs(member.key)
452
- if (tsKey && holdsFunctions(checker, checker.getTypeAtLocation(tsKey))) collections.set(member.key.name, member.key)
484
+ if (member.type !== "PropertyDefinition" || member.computed || member.key.type !== "Identifier")
485
+ continue;
486
+ const tsKey = toTs(member.key);
487
+ if (tsKey && holdsFunctions(checker, checker.getTypeAtLocation(tsKey)))
488
+ collections.set(member.key.name, member.key);
453
489
  }
454
- if (collections.size === 0) return
455
- const registered = new Set()
490
+ if (collections.size === 0)
491
+ return;
492
+ const registered = new Set();
456
493
  const visit = (node) => {
457
- if (!node || typeof node.type !== "string") return
494
+ if (!isNode(node))
495
+ return;
458
496
  if (node.type === "CallExpression" && node.callee.type === "MemberExpression" && !node.callee.computed && node.callee.property.type === "Identifier" && REGISTER_METHODS.has(node.callee.property.name)) {
459
- const holder = node.callee.object
497
+ const holder = node.callee.object;
460
498
  if (holder.type === "MemberExpression" && holder.object.type === "ThisExpression" && !holder.computed && holder.property.type === "Identifier" && collections.has(holder.property.name)) {
461
- const callback = node.arguments[node.arguments.length - 1]
462
- const tsCallback = callback ? toTs(callback) : null
463
- if (tsCallback && isFunctionType(checker.getTypeAtLocation(tsCallback)) && callback.type === "Identifier") registered.add(holder.property.name)
499
+ const callback = node.arguments[node.arguments.length - 1];
500
+ const tsCallback = callback ? toTs(callback) : null;
501
+ if (tsCallback && isFunctionType(checker.getTypeAtLocation(tsCallback)) && callback?.type === "Identifier")
502
+ registered.add(holder.property.name);
464
503
  }
465
504
  }
466
505
  for (const key of Object.keys(node)) {
467
- if (key === "parent") continue
468
- const value = node[key]
469
- if (Array.isArray(value)) for (const child of value) visit(child)
470
- else if (value && typeof value.type === "string") visit(value)
506
+ if (key === "parent")
507
+ continue;
508
+ const value = Reflect.get(node, key);
509
+ if (Array.isArray(value)) {
510
+ const children = value;
511
+ for (const child of children)
512
+ visit(child);
513
+ }
514
+ else if (isNode(value))
515
+ visit(value);
471
516
  }
517
+ };
518
+ visit(classNode.body);
519
+ for (const name of registered) {
520
+ const key = collections.get(name);
521
+ if (key)
522
+ context.report({ node: key, messageId: "registry", data: { name } });
472
523
  }
473
- visit(classNode.body)
474
- for (const name of registered) context.report({ node: collections.get(name), messageId: "registry", data: { name } })
475
- }
524
+ };
476
525
  return {
477
526
  ClassDeclaration(node) {
478
- checkClass(node)
527
+ checkClass(node);
479
528
  if (node.superClass) {
480
- const name = busClassOf(context, node.superClass, eventBus.classes)
481
- if (name) context.report({ node: node.superClass, messageId: "bus", data: { name } })
529
+ const name = busClassOf(context, node.superClass, eventBus.classes);
530
+ if (name)
531
+ context.report({ node: node.superClass, messageId: "bus", data: { name } });
482
532
  }
483
533
  },
484
- ClassExpression(node) { checkClass(node) },
534
+ ClassExpression(node) { checkClass(node); },
485
535
  NewExpression(node) {
486
- const name = busClassOf(context, node.callee, eventBus.classes)
487
- if (name) context.report({ node, messageId: "bus", data: { name } })
536
+ const name = busClassOf(context, node.callee, eventBus.classes);
537
+ if (name)
538
+ context.report({ node, messageId: "bus", data: { name } });
488
539
  },
489
540
  ...moduleReferences(({ node, source, value, names }) => {
490
- const members = eventBus.imports[value]
491
- if (members === undefined) return
492
- if (members.length === 0) report(source, value)
493
- else for (const name of names) if (members.includes(name)) report(node, name)
541
+ const members = eventBus.imports[value];
542
+ if (members === undefined)
543
+ return;
544
+ if (members.length === 0)
545
+ report(source, value);
546
+ else
547
+ for (const name of names)
548
+ if (name !== null && members.includes(name))
549
+ report(node, name);
494
550
  }),
495
551
  MemberExpression(node) {
496
- if (node.computed || node.property.type !== "Identifier" || !eventNames.has(node.property.name)) return
497
- if (node.object.type === "Identifier" && importOf(context, node.object)?.source === CQRS_PACKAGE && importOf(context, node.object).imported === "*") report(node, node.property.name)
552
+ if (node.computed || node.property.type !== "Identifier" || !eventNames.has(node.property.name))
553
+ return;
554
+ if (node.object.type !== "Identifier")
555
+ return;
556
+ const binding = importOf(context, node.object);
557
+ if (binding?.source === CQRS_PACKAGE && binding.imported === "*")
558
+ report(node, node.property.name);
498
559
  },
499
- }
560
+ };
500
561
  },
501
- }
502
-
562
+ };
503
563
  /** The rules this law contributes to the plugin. */
504
564
  export const rules = {
505
565
  "handler-overrides-process": handlerOverridesProcess,
@@ -509,8 +569,7 @@ export const rules = {
509
569
  "execute-params-shape": executeParamsShape,
510
570
  "no-use-case": noUseCase,
511
571
  "no-event-bus": noEventBus,
512
- }
513
-
572
+ };
514
573
  /** Every rule of this law at `error`. */
515
574
  export const recommended = {
516
575
  "starci-be/handler-overrides-process": "error",
@@ -520,4 +579,4 @@ export const recommended = {
520
579
  "starci-be/execute-params-shape": "error",
521
580
  "starci-be/no-use-case": "error",
522
581
  "starci-be/no-event-bus": "error",
523
- }
582
+ };