@starci/eslint-canon-be 3.4.57 → 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 (197) hide show
  1. package/CHANGELOG.md +19 -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 +22 -14
  177. package/runtime/knowledge/hfs/rules.yaml +23 -0
  178. package/runtime/scripts/lib/package-at.mjs +5 -5
  179. package/async-discipline.mjs +0 -83
  180. package/comments.mjs +0 -416
  181. package/edge-hardening.mjs +0 -139
  182. package/health-ready.mjs +0 -539
  183. package/jwt-alg.mjs +0 -45
  184. package/lib/ast.mjs +0 -88
  185. package/lib/import-source.mjs +0 -104
  186. package/lib/specifier.mjs +0 -27
  187. package/lib/subject-values.mjs +0 -55
  188. package/lib/types.mjs +0 -133
  189. package/log-safety.mjs +0 -303
  190. package/module-layering.mjs +0 -270
  191. package/naming.mjs +0 -251
  192. package/observability.mjs +0 -241
  193. package/resilience.mjs +0 -304
  194. package/service-isolation.mjs +0 -75
  195. package/spec-no-skip.mjs +0 -85
  196. package/testing.mjs +0 -384
  197. package/type-safety.mjs +0 -323
@@ -38,283 +38,344 @@
38
38
  * lookalikes, spread handoffs, protected or replaced helpers, and callbacks handing work to another object are refused.
39
39
  * Effects hidden inside free functions or getters and helpers called past the first one stay review questions.
40
40
  */
41
- import ts from "typescript"
42
- import { hfsOf } from "./lib/hfs.mjs"
43
- import { keyName, walk } from "./lib/ast.mjs"
44
- import { baseName, isOwnedType, ownerNameOf } from "./lib/ports.mjs"
45
- import { resolveVariable } from "./lib/transactions.mjs"
46
- import { typeOrigins, typed } from "./lib/types.mjs"
47
- import { mutationIdempotent } from "./lib/client-idempotency.mjs"
48
-
41
+ import ts from "typescript";
42
+ import { hfsOf } from "./lib/hfs.js";
43
+ import { keyName, walk } from "./lib/ast.js";
44
+ import { baseName, isOwnedType, ownerNameOf } from "./lib/ports.js";
45
+ import { resolveVariable } from "./lib/transactions.js";
46
+ import { childNode, typeOrigins, typed } from "./lib/types.js";
47
+ import { mutationIdempotent } from "./lib/client-idempotency.js";
48
+ /** The function a class member holds: a method's own function, or a property initialised with a function; null for any other value. */
49
+ const functionOf = (member) => {
50
+ const value = member.value;
51
+ return value?.type === "ArrowFunctionExpression" || value?.type === "FunctionExpression" || value?.type === "TSEmptyBodyFunctionExpression" ? value : null;
52
+ };
53
+ /** The `body` a member's value holds (`member.value?.body`), undefined when the value holds none. */
54
+ const valueBody = (member) => {
55
+ const value = member.value;
56
+ return value && "body" in value ? value.body : undefined;
57
+ };
58
+ /** The `params` a member's value holds (`member.value?.params`), undefined when the value holds none. */
59
+ const valueParams = (member) => {
60
+ const value = member.value;
61
+ return value && "params" in value ? value.params : undefined;
62
+ };
49
63
  /** The `Inbox` port of `platform/inbox`. */
50
- const isInboxType = (context, node) => isOwnedType(context, node, { name: "Inbox", capability: "inbox", tier: "platform" })
51
-
64
+ const isInboxType = (context, node) => isOwnedType(context, node, { name: "Inbox", capability: "inbox", tier: "platform" });
52
65
  /** A `return` statement, or a block that ends in one. */
53
- const returns = (statement) =>
54
- statement?.type === "ReturnStatement"
55
- || (statement?.type === "BlockStatement" && statement.body.at(-1)?.type === "ReturnStatement")
56
-
66
+ const returns = (statement) => statement?.type === "ReturnStatement"
67
+ || (statement?.type === "BlockStatement" && statement.body.at(-1)?.type === "ReturnStatement");
57
68
  /** Whether `test` is `!<expression>` or `<expression> === false` around `target` (the awaited claim, or the identifier holding it). */
58
69
  const refusesOn = (test, target) => {
59
- if (test.type === "UnaryExpression" && test.operator === "!") return test.argument === target || (test.argument.type === "Identifier" && target.type === "Identifier" && test.argument.name === target.name)
70
+ if (test.type === "UnaryExpression" && test.operator === "!")
71
+ return test.argument === target || (test.argument.type === "Identifier" && target.type === "Identifier" && test.argument.name === target.name);
60
72
  if (test.type === "BinaryExpression" && (test.operator === "===" || test.operator === "==")) {
61
- const [left, right] = [test.left, test.right]
62
- const isFalse = (node) => node.type === "Literal" && node.value === false
63
- return (left === target && isFalse(right)) || (right === target && isFalse(left))
73
+ const [left, right] = [test.left, test.right];
74
+ const isFalse = (node) => node.type === "Literal" && node.value === false;
75
+ return (left === target && isFalse(right)) || (right === target && isFalse(left));
64
76
  }
65
- return false
66
- }
67
-
77
+ return false;
78
+ };
68
79
  /** Where the awaited claim sits: the statement that tests it, or the declaration that stores it followed by that test. */
80
+ /** The statements of the block or program a node holds as its `body` list, else null. */
81
+ const statementsOf = (node) => {
82
+ if (!node || !("body" in node))
83
+ return null;
84
+ const body = node.body;
85
+ return Array.isArray(body) ? body : null;
86
+ };
69
87
  const returnsOnFalse = (awaited) => {
70
- const node = awaited
71
- const parent = node.parent
88
+ const node = awaited;
89
+ const parent = node.parent;
72
90
  if (parent.type === "UnaryExpression" || parent.type === "BinaryExpression") {
73
- const statement = parent.parent
74
- return statement.type === "IfStatement" && statement.test === parent && refusesOn(parent, node) && returns(statement.consequent)
91
+ const statement = parent.parent;
92
+ return statement.type === "IfStatement" && statement.test === parent && refusesOn(parent, node) && returns(statement.consequent);
75
93
  }
76
94
  if (parent.type === "VariableDeclarator" && parent.id.type === "Identifier") {
77
- const declaration = parent.parent
78
- const siblings = declaration.parent?.body
79
- if (!Array.isArray(siblings)) return false
80
- const next = siblings[siblings.indexOf(declaration) + 1]
81
- return next?.type === "IfStatement" && refusesOn(next.test, parent.id) && returns(next.consequent)
95
+ const declaration = parent.parent;
96
+ const siblings = statementsOf(declaration.parent);
97
+ if (!siblings)
98
+ return false;
99
+ const next = siblings[siblings.indexOf(declaration) + 1];
100
+ return next?.type === "IfStatement" && refusesOn(next.test, parent.id) && returns(next.consequent);
82
101
  }
83
- return false
84
- }
85
-
102
+ return false;
103
+ };
86
104
  /** The awaited expression that completes first in a body, ignoring nested functions; null when the body awaits nothing. */
87
105
  const firstAwait = (body) => {
88
- let first = null
106
+ const found = { first: null };
89
107
  walk(body, (node) => {
90
- if (node.type === "AwaitExpression" && (first === null || node.range[1] < first.range[1])) first = node
91
- }, { intoFunctions: false })
92
- return first
93
- }
94
-
108
+ if (node.type === "AwaitExpression" && (found.first === null || node.range[1] < found.first.range[1]))
109
+ found.first = node;
110
+ }, { intoFunctions: false });
111
+ return found.first;
112
+ };
95
113
  /** True when an expression evaluates a call, construction or write before the designated call completes. */
96
114
  const hasPriorEffect = (expression, call) => {
97
- let found = false
115
+ let found = false;
98
116
  walk(expression, (node) => {
99
117
  if (["CallExpression", "NewExpression", "AssignmentExpression", "UpdateExpression"].includes(node.type)
100
- && node !== call && node.range[1] < call.range[1]) found = true
101
- }, { intoFunctions: false })
102
- return found
103
- }
104
-
118
+ && node !== call && node.range[1] < call.range[1])
119
+ found = true;
120
+ }, { intoFunctions: false });
121
+ return found;
122
+ };
105
123
  /** A transaction port the `platform/database` owner declares: a receiver typed by that owner, never by its name. */
106
124
  const isDatabasePort = (context, node) => {
107
- const hfs = hfsOf(context)
108
- const origins = typeOrigins(context, node)
109
- return origins.length > 0 && origins.every((origin) =>
110
- origin.module === null && hfs.tierOf(origin.file) === "platform" && ownerNameOf(hfs, origin.file) === "database")
111
- }
112
-
125
+ const hfs = hfsOf(context);
126
+ const origins = typeOrigins(context, node);
127
+ return origins.length > 0 && origins.every((origin) => origin.module === null && hfs.tierOf(origin.file) === "platform" && ownerNameOf(hfs, origin.file) === "database");
128
+ };
113
129
  /** Every declared alternative must be TypeORM's manager: one genuine union member cannot vouch for a lookalike. */
114
130
  const isEntityManager = (context, node) => {
115
- const origins = typeOrigins(context, node)
116
- return origins.length > 0 && origins.every((origin) => origin.name === "EntityManager" && origin.module === "typeorm")
117
- }
118
-
131
+ const origins = typeOrigins(context, node);
132
+ return origins.length > 0 && origins.every((origin) => origin.name === "EntityManager" && origin.module === "typeorm");
133
+ };
119
134
  /** TypeScript erases a `this` parameter, so only these parameters receive call arguments at runtime. */
120
- const runtimeParams = (method) => method.value.params.filter((param) => !(param.type === "Identifier" && param.name === "this"))
121
-
135
+ const runtimeParams = (params) => params.filter((param) => !(param.type === "Identifier" && param.name === "this"));
122
136
  /** The callback a sole, returned or awaited `.transaction(...)` runs on an EntityManager or the database port, and its manager parameter. */
137
+ const ISOLATION_LEVELS = ["READ UNCOMMITTED", "READ COMMITTED", "REPEATABLE READ", "SERIALIZABLE"];
123
138
  const soleTransactionCallback = (context, body) => {
124
- if (body.body.length !== 1) return null
125
- const outer = body.body[0]
139
+ const outer = body.body[0];
140
+ if (body.body.length !== 1 || !outer)
141
+ return null;
126
142
  const expression = outer.type === "ReturnStatement" ? outer.argument
127
- : outer.type === "ExpressionStatement" && outer.expression.type === "AwaitExpression" ? outer.expression : null
128
- const call = expression?.type === "AwaitExpression" ? expression.argument : expression
129
- const callee = call?.callee
130
- if (call?.type !== "CallExpression" || callee.type !== "MemberExpression" || callee.computed
131
- || callee.property.type !== "Identifier" || callee.property.name !== "transaction" || hasPriorEffect(call, call)) return null
132
- let callback = null
143
+ : outer.type === "ExpressionStatement" && outer.expression.type === "AwaitExpression" ? outer.expression : null;
144
+ const call = expression?.type === "AwaitExpression" ? expression.argument : expression;
145
+ if (call?.type !== "CallExpression")
146
+ return null;
147
+ const callee = call.callee;
148
+ if (callee.type !== "MemberExpression" || callee.computed
149
+ || callee.property.type !== "Identifier" || callee.property.name !== "transaction" || hasPriorEffect(call, call))
150
+ return null;
151
+ let callback = null;
133
152
  if (isEntityManager(context, callee.object)) {
134
153
  // TypeORM runs arg0 when it is a function, otherwise arg1. Never accept a later, ignored callback.
135
- const isolation = call.arguments[0]
154
+ const isolation = call.arguments[0];
136
155
  callback = call.arguments.length === 1 ? isolation
137
- : call.arguments.length === 2 && isolation.type === "Literal"
138
- && ["READ UNCOMMITTED", "READ COMMITTED", "REPEATABLE READ", "SERIALIZABLE"].includes(isolation.value)
139
- ? call.arguments[1] : null
140
- } else if (isDatabasePort(context, callee.object) && call.arguments.length === 1) callback = call.arguments[0]
141
- if (!callback || !["ArrowFunctionExpression", "FunctionExpression"].includes(callback.type) || callback.params.length !== 1) return null
142
- const manager = callback.params[0]
143
- if (manager.type !== "Identifier" || !isEntityManager(context, manager)) return null
144
- return { callback, manager }
145
- }
146
-
156
+ : call.arguments.length === 2 && isolation?.type === "Literal"
157
+ && ISOLATION_LEVELS.includes(isolation.value)
158
+ ? call.arguments[1] : null;
159
+ }
160
+ else if (isDatabasePort(context, callee.object) && call.arguments.length === 1)
161
+ callback = call.arguments[0];
162
+ if (!callback || (callback.type !== "ArrowFunctionExpression" && callback.type !== "FunctionExpression") || callback.params.length !== 1)
163
+ return null;
164
+ const manager = callback.params[0];
165
+ if (manager?.type !== "Identifier" || !isEntityManager(context, manager))
166
+ return null;
167
+ return { callback, manager };
168
+ };
147
169
  /** True when `argument` is the identifier bound to the parameter `param`; with `unwritten`, that parameter is also never reassigned. */
148
170
  const isParameter = (context, argument, param, { unwritten = false } = {}) => {
149
- const binding = resolveVariable(context.sourceCode, param)
150
- const argumentBinding = argument?.type === "Identifier" ? resolveVariable(context.sourceCode, argument) : null
171
+ const binding = resolveVariable(context.sourceCode, param);
172
+ const argumentBinding = argument?.type === "Identifier" ? resolveVariable(context.sourceCode, argument) : null;
151
173
  return !!binding && binding.defs[0]?.name === param && argumentBinding === binding
152
- && !(unwritten && binding.references.some((reference) => reference.isWrite()))
153
- }
154
-
174
+ && !(unwritten && binding.references.some((reference) => reference.isWrite()));
175
+ };
155
176
  /** The statement of a block that holds its first await as the claim it tests, stores or awaits; null when that await sits deeper. */
156
177
  const claimStatement = (block) => {
157
- const awaited = firstAwait(block)
158
- const parent = awaited?.parent
178
+ const awaited = firstAwait(block);
179
+ const parent = awaited?.parent;
159
180
  const statement = parent?.type === "VariableDeclarator" && parent.init === awaited
160
181
  && parent.parent.declarations.length === 1 ? parent.parent
161
- : ["UnaryExpression", "BinaryExpression"].includes(parent?.type) ? parent.parent
162
- : parent?.type === "ExpressionStatement" ? parent : null
163
- return statement?.parent === block ? { awaited, statement } : null
164
- }
165
-
182
+ : parent?.type === "UnaryExpression" || parent?.type === "BinaryExpression" ? parent.parent
183
+ : parent?.type === "ExpressionStatement" ? parent : null;
184
+ return awaited && statement?.parent === block ? { awaited, statement } : null;
185
+ };
166
186
  /** The claim that opens the inline async callback with its own manager, as its first statement. */
167
187
  const inlineClaim = (context, { callback, manager }) => {
168
- if (!callback.async || callback.body.type !== "BlockStatement") return null
169
- const found = claimStatement(callback.body)
170
- if (!found || found.statement !== callback.body.body[0]) return null
171
- const claim = found.awaited.argument
172
- if (claim?.type !== "CallExpression" || !isParameter(context, claim.arguments[2], manager) || hasPriorEffect(found.statement, claim)) return null
173
- return found.awaited
174
- }
175
-
188
+ if (!callback.async || callback.body.type !== "BlockStatement")
189
+ return null;
190
+ const found = claimStatement(callback.body);
191
+ if (!found || found.statement !== callback.body.body[0])
192
+ return null;
193
+ const claim = found.awaited.argument;
194
+ if (claim?.type !== "CallExpression" || !isParameter(context, claim.arguments[2], manager) || hasPriorEffect(found.statement, claim))
195
+ return null;
196
+ return found.awaited;
197
+ };
176
198
  /** The `this.<method>(...)` call an arrow callback hands its whole work to: its expression body, or its sole returned or awaited statement. */
177
199
  const handedCall = (callback) => {
178
- if (callback.type !== "ArrowFunctionExpression") return null
179
- let expression = callback.body
200
+ if (callback.type !== "ArrowFunctionExpression")
201
+ return null;
202
+ let expression = callback.body;
180
203
  if (expression.type === "BlockStatement") {
181
- const [sole] = expression.body
182
- if (expression.body.length !== 1) return null
183
- expression = sole.type === "ReturnStatement" ? sole.argument
184
- : sole.type === "ExpressionStatement" && sole.expression.type === "AwaitExpression" ? sole.expression : null
204
+ const [sole] = expression.body;
205
+ if (expression.body.length !== 1)
206
+ return null;
207
+ expression = sole?.type === "ReturnStatement" ? sole.argument
208
+ : sole?.type === "ExpressionStatement" && sole.expression.type === "AwaitExpression" ? sole.expression : null;
185
209
  }
186
- const call = expression?.type === "AwaitExpression" ? expression.argument : expression
187
- const callee = call?.callee
188
- return call?.type === "CallExpression" && callee.type === "MemberExpression" && !callee.computed
189
- && callee.object.type === "ThisExpression" && ["Identifier", "PrivateIdentifier"].includes(callee.property.type) ? call : null
190
- }
191
-
210
+ const call = expression?.type === "AwaitExpression" ? expression.argument : expression;
211
+ if (call?.type !== "CallExpression")
212
+ return null;
213
+ const callee = call.callee;
214
+ return callee.type === "MemberExpression" && !callee.computed
215
+ && callee.object.type === "ThisExpression" && ["Identifier", "PrivateIdentifier"].includes(callee.property.type) ? call : null;
216
+ };
192
217
  /** Argument preparation cannot start instance or transactional work before the inbox claim. */
193
218
  const preparesArguments = (context, statement, manager) => {
194
- if (statement.type !== "VariableDeclaration" || statement.kind !== "const") return false
195
- let safe = true
219
+ if (statement.type !== "VariableDeclaration" || statement.kind !== "const")
220
+ return false;
221
+ let safe = true;
196
222
  walk(statement, (node) => {
197
223
  if (node.type === "ThisExpression" || (node.type === "Identifier" && isParameter(context, node, manager))
198
224
  || ["AwaitExpression", "NewExpression", "AssignmentExpression", "UpdateExpression"].includes(node.type)
199
- || (node.type === "CallExpression" && node.callee.type !== "Identifier")) safe = false
200
- }, { intoFunctions: false })
201
- return safe
202
- }
203
-
225
+ || (node.type === "CallExpression" && node.callee.type !== "Identifier"))
226
+ safe = false;
227
+ }, { intoFunctions: false });
228
+ return safe;
229
+ };
204
230
  /** An assignment anywhere in the class can replace the method that the checker resolved for the handoff. */
205
231
  const replacesHelper = (classBody, helper) => {
206
- let replaced = false
232
+ let replaced = false;
207
233
  walk(classBody, (node) => {
208
- if (node.type !== "AssignmentExpression") return
209
- const target = node.left
234
+ if (node.type !== "AssignmentExpression")
235
+ return;
236
+ const target = node.left;
210
237
  if (target.type === "MemberExpression" && target.object.type === "ThisExpression"
211
238
  && (!target.computed || target.property.type === "Literal")
212
239
  && (target.property.type === "PrivateIdentifier") === (helper.key.type === "PrivateIdentifier")
213
- && keyName(target.property) === keyName(helper.key)) replaced = true
214
- })
215
- return replaced
216
- }
217
-
240
+ && keyName(target.property) === keyName(helper.key))
241
+ replaced = true;
242
+ });
243
+ return replaced;
244
+ };
218
245
  /** The helper body must claim first with its unchanged runtime manager, after only argument preparation. */
219
246
  const helperClaim = (context, helper, param) => {
220
- if (!helper.value?.async || helper.value.body?.type !== "BlockStatement"
221
- || param?.type !== "Identifier" || !isEntityManager(context, param)) return null
222
- const body = helper.value.body
223
- const found = claimStatement(body)
224
- if (!found) return null
225
- const before = body.body.slice(0, body.body.indexOf(found.statement))
226
- if (before.some((statement) => !preparesArguments(context, statement, param))) return null
227
- const claim = found.awaited.argument
247
+ const fn = functionOf(helper);
248
+ if (!fn?.async || fn.body?.type !== "BlockStatement"
249
+ || param?.type !== "Identifier" || !isEntityManager(context, param))
250
+ return null;
251
+ const body = fn.body;
252
+ const found = claimStatement(body);
253
+ if (!found)
254
+ return null;
255
+ const statements = body.body;
256
+ const before = statements.slice(0, statements.indexOf(found.statement));
257
+ if (before.some((statement) => !preparesArguments(context, statement, param)))
258
+ return null;
259
+ const claim = found.awaited.argument;
228
260
  if (claim?.type !== "CallExpression" || !isParameter(context, claim.arguments[2], param, { unwritten: true })
229
- || hasPriorEffect(found.statement, claim)) return null
230
- return found.awaited
231
- }
232
-
261
+ || hasPriorEffect(found.statement, claim))
262
+ return null;
263
+ return found.awaited;
264
+ };
233
265
  /**
234
266
  * The claim of a typed helper the callback delegates to: a method of the delivery's own class, resolved by the checker, that receives
235
267
  * the callback's manager, awaits nothing before the claim, claims with that parameter at its top level and runs no statement but `const`
236
268
  * argument preparation before it.
237
269
  */
238
270
  const delegatedClaim = (context, delivery, { callback, manager }) => {
239
- const call = handedCall(callback)
240
- if (!call || call.arguments.some((argument) => argument.type === "SpreadElement") || hasPriorEffect(call, call)) return null
241
- const position = call.arguments.findIndex((argument) => argument.type === "Identifier" && argument.name === manager.name)
242
- if (position < 0 || !isParameter(context, call.arguments[position], manager)) return null
243
- const { checker, toTs } = typed(context)
244
- const declarations = checker.getSymbolAtLocation(toTs(call.callee.property))?.getDeclarations?.() ?? []
271
+ const call = handedCall(callback);
272
+ if (!call || call.arguments.some((argument) => argument.type === "SpreadElement") || hasPriorEffect(call, call))
273
+ return null;
274
+ const position = call.arguments.findIndex((argument) => argument.type === "Identifier" && argument.name === manager.name);
275
+ if (position < 0 || !isParameter(context, call.arguments[position], manager))
276
+ return null;
277
+ // handedCall answers only a call whose callee is a member expression
278
+ const callee = call.callee;
279
+ if (callee.type !== "MemberExpression")
280
+ return null;
281
+ const { checker, toTs } = typed(context);
282
+ const declarations = checker.getSymbolAtLocation(toTs(callee.property))?.getDeclarations?.() ?? [];
245
283
  const helper = declarations.length === 1
246
- ? delivery.parent.body.find((member) => member.type === "MethodDefinition" && toTs(member) === declarations[0]) : undefined
247
- if (!helper || helper === delivery || helper.kind !== "method" || helper.static || !helper.value.async || !helper.value.body) return null
248
- const params = runtimeParams(helper)
284
+ ? delivery.parent.body.find((member) => member.type === "MethodDefinition" && toTs(member) === declarations[0]) : undefined;
285
+ if (!helper || helper === delivery || helper.kind !== "method" || helper.static || !helper.value.async || !helper.value.body)
286
+ return null;
287
+ const params = runtimeParams(helper.value.params);
249
288
  // A public delivery contract carries its claim and manager obligations into every override.
250
289
  if (helper.accessibility === "protected" || (helper.accessibility !== "private" && helper.key.type !== "PrivateIdentifier"
251
- && (!params[0] || !isDeliveryParam(context, params[0]))) || replacesHelper(delivery.parent, helper)) return null
252
- return helperClaim(context, helper, params[position])
253
- }
254
-
290
+ && (!params[0] || !isDeliveryParam(context, params[0]))) || replacesHelper(delivery.parent, helper))
291
+ return null;
292
+ return helperClaim(context, helper, params[position]);
293
+ };
255
294
  /** The first awaited claim of a delivery that runs as one transaction: inline in its callback, or in the typed helper that callback delegates to. */
256
295
  const transactionClaim = (context, delivery) => {
257
- const transaction = soleTransactionCallback(context, delivery.value.body)
258
- if (!transaction) return null
259
- return inlineClaim(context, transaction) ?? delegatedClaim(context, delivery, transaction)
260
- }
261
-
296
+ // judgeClaim asks only of a member whose value holds a block body
297
+ const body = valueBody(delivery);
298
+ if (body?.type !== "BlockStatement")
299
+ return null;
300
+ const transaction = soleTransactionCallback(context, body);
301
+ if (!transaction)
302
+ return null;
303
+ return inlineClaim(context, transaction) ?? delegatedClaim(context, delivery, transaction);
304
+ };
262
305
  /** A delivery's type, including a union alternative, exposes the sender's `eventId`. */
263
- const hasEventId = (checker, type) =>
264
- (type.isUnion() ? type.types : [type]).some((part) => checker.getPropertyOfType(part, "eventId") !== undefined)
265
-
306
+ const hasEventId = (checker, type) => (type.isUnion() ? type.types : [type]).some((part) => checker.getPropertyOfType(part, "eventId") !== undefined);
266
307
  /** Whether a parameter's type has an `eventId` property: the shape of a delivery. */
267
308
  const isDeliveryParam = (context, param) => {
268
- const target = param.type === "TSParameterProperty" ? param.parameter : param.type === "AssignmentPattern" ? param.left : param
269
- const { checker, toTs } = typed(context)
270
- const tsNode = toTs(target)
271
- if (!tsNode) return false
272
- return hasEventId(checker, checker.getTypeAtLocation(tsNode))
273
- }
274
-
309
+ const target = param.type === "TSParameterProperty" ? param.parameter : param.type === "AssignmentPattern" ? param.left : param;
310
+ const { checker, toTs } = typed(context);
311
+ const tsNode = toTs(target);
312
+ if (!tsNode)
313
+ return false;
314
+ return hasEventId(checker, checker.getTypeAtLocation(tsNode));
315
+ };
275
316
  /** The runtime parameter the declared signature's first awaited claim uses, including declarations in another source file. */
276
317
  const claimedManagerPosition = (checker, signature) => {
277
- let awaited = null
318
+ const found = { awaited: null };
278
319
  const visit = (node) => {
279
- if (ts.isAwaitExpression(node) && (!awaited || node.end < awaited.end)) awaited = node
280
- ts.forEachChild(node, (child) => { if (!ts.isFunctionLike(child)) visit(child) })
281
- }
282
- const body = signature.getDeclaration()?.body
283
- if (body) visit(body)
284
- const claim = awaited?.expression
320
+ if (ts.isAwaitExpression(node) && (!found.awaited || node.end < found.awaited.end))
321
+ found.awaited = node;
322
+ ts.forEachChild(node, (child) => { if (!ts.isFunctionLike(child))
323
+ visit(child); });
324
+ };
325
+ const body = childNode(signature.getDeclaration(), "body");
326
+ if (body)
327
+ visit(body);
328
+ const claim = found.awaited?.expression;
285
329
  if (!claim || !ts.isCallExpression(claim) || !ts.isPropertyAccessExpression(claim.expression)
286
- || claim.expression.name.text !== "claim" || !claim.arguments[2]) return -1
287
- const manager = checker.getSymbolAtLocation(claim.arguments[2])
288
- return manager ? signature.parameters.indexOf(manager) : -1
289
- }
290
-
330
+ || claim.expression.name.text !== "claim")
331
+ return -1;
332
+ const managerArgument = claim.arguments[2];
333
+ if (!managerArgument)
334
+ return -1;
335
+ const manager = checker.getSymbolAtLocation(managerArgument);
336
+ return manager ? signature.parameters.indexOf(manager) : -1;
337
+ };
338
+ /** Whether the checker can list a type's base types: a tuple, or a class or interface type (what `getBaseTypes` accepts). */
339
+ const listsBaseTypes = (type) => ("objectFlags" in type && typeof type.objectFlags === "number" && (type.objectFlags & ts.ObjectFlags.Tuple) !== 0)
340
+ || ((type.getSymbol()?.flags ?? 0) & (ts.SymbolFlags.Class | ts.SymbolFlags.Interface)) !== 0;
341
+ /** The base types of a class or interface type; any other type stops the run as the checker itself does. */
342
+ const baseTypesOf = (checker, type) => {
343
+ if (!listsBaseTypes(type))
344
+ throw new TypeError("type must be class or interface");
345
+ return checker.getBaseTypes(type) ?? [];
346
+ };
291
347
  /** An override preserves inherited delivery and transaction-manager obligations even when it widens its own parameter. */
292
348
  const inheritedDelivery = (context, method) => {
293
- const name = keyName(method.key)
294
- const result = { delivery: false, managers: new Set() }
295
- if (name === null) return result
296
- const { checker, toTs } = typed(context)
297
- const location = toTs(method)
298
- const seen = new Set()
349
+ const name = keyName(method.key);
350
+ const result = { delivery: false, managers: new Set() };
351
+ if (name === null)
352
+ return result;
353
+ const { checker, toTs } = typed(context);
354
+ const location = toTs(method);
355
+ const seen = new Set();
299
356
  const visit = (type) => {
300
- if (seen.has(type)) return
301
- seen.add(type)
302
- const member = checker.getPropertyOfType(type, name)
303
- const signatures = member ? checker.getTypeOfSymbolAtLocation(member, location).getCallSignatures() : []
357
+ if (seen.has(type))
358
+ return;
359
+ seen.add(type);
360
+ const member = checker.getPropertyOfType(type, name);
361
+ const signatures = member ? checker.getTypeOfSymbolAtLocation(member, location).getCallSignatures() : [];
304
362
  for (const signature of signatures) {
305
- const param = signature.parameters[0]
306
- if (!param || !hasEventId(checker, checker.getTypeOfSymbolAtLocation(param, location))) continue
307
- result.delivery = true
308
- const manager = claimedManagerPosition(checker, signature)
309
- if (manager >= 0) result.managers.add(manager)
363
+ const param = signature.parameters[0];
364
+ if (!param || !hasEventId(checker, checker.getTypeOfSymbolAtLocation(param, location)))
365
+ continue;
366
+ result.delivery = true;
367
+ const manager = claimedManagerPosition(checker, signature);
368
+ if (manager >= 0)
369
+ result.managers.add(manager);
310
370
  }
311
- for (const base of checker.getBaseTypes(type) ?? []) visit(base)
312
- }
313
- const owner = checker.getTypeAtLocation(toTs(method.parent.parent))
314
- for (const base of checker.getBaseTypes(owner) ?? []) visit(base)
315
- return result
316
- }
317
-
371
+ for (const base of baseTypesOf(checker, type))
372
+ visit(base);
373
+ };
374
+ const owner = checker.getTypeAtLocation(toTs(method.parent.parent));
375
+ for (const base of baseTypesOf(checker, owner))
376
+ visit(base);
377
+ return result;
378
+ };
318
379
  /** A service method that takes a delivery claims the event through the shared inbox before it acts. */
319
380
  export const inboxDedupeRequired = {
320
381
  meta: {
@@ -327,56 +388,65 @@ export const inboxDedupeRequired = {
327
388
  },
328
389
  },
329
390
  create(context) {
330
- const hfs = hfsOf(context)
331
- const filename = context.filename || context.getFilename()
332
- const slot = hfs.slotOf(filename)
333
- if (!baseName(filename).endsWith(".service.ts") || !slot || slot.startsWith("be.tests.")) return {}
391
+ const hfs = hfsOf(context);
392
+ const filename = context.filename || context.getFilename();
393
+ const slot = hfs.slotOf(filename);
394
+ if (!baseName(filename).endsWith(".service.ts") || !slot || slot.startsWith("be.tests."))
395
+ return {};
334
396
  // The event bus publishes events (their shape carries an `eventId`) and its runtime claims the inbox before a consumer runs: no delivery method here.
335
- if (slot.startsWith("be.platform") && ownerNameOf(hfs, filename) === "event-bus") return {}
397
+ if (slot.startsWith("be.platform") && ownerNameOf(hfs, filename) === "event-bus")
398
+ return {};
336
399
  const judgeClaim = (node, inherited) => {
337
- const body = node.value?.body
338
- let awaited = body?.type === "BlockStatement" ? transactionClaim(context, node) ?? firstAwait(body) : null
400
+ const body = valueBody(node);
401
+ let awaited = body?.type === "BlockStatement" ? transactionClaim(context, node) ?? firstAwait(body) : null;
339
402
  if (inherited.managers.size > 0) {
340
- const params = node.value?.params ? runtimeParams(node) : []
341
- if (![...inherited.managers].every((position) => helperClaim(context, node, params[position]) === awaited)) awaited = null
403
+ const fnParams = valueParams(node);
404
+ const params = fnParams ? runtimeParams(fnParams) : [];
405
+ if (![...inherited.managers].every((position) => helperClaim(context, node, params[position]) === awaited))
406
+ awaited = null;
342
407
  }
343
- const call = awaited?.argument
408
+ const call = awaited?.argument;
344
409
  const claims = call?.type === "CallExpression"
345
410
  && call.callee.type === "MemberExpression"
346
411
  && !call.callee.computed
347
412
  && call.callee.property.type === "Identifier"
348
413
  && call.callee.property.name === "claim"
349
414
  && call.arguments.length >= 2
350
- && isInboxType(context, call.callee.object)
351
- if (!claims) context.report({ node: node.key, messageId: "noClaim" })
352
- else if (!returnsOnFalse(awaited)) context.report({ node: node.key, messageId: "noEarlyReturn" })
353
- }
415
+ && isInboxType(context, call.callee.object);
416
+ if (!claims)
417
+ context.report({ node: node.key, messageId: "noClaim" });
418
+ else if (!awaited || !returnsOnFalse(awaited))
419
+ context.report({ node: node.key, messageId: "noEarlyReturn" });
420
+ };
354
421
  return {
355
422
  MethodDefinition(node) {
356
- if (node.kind !== "method" || node.static || node.key.type === "PrivateIdentifier" || !node.value.body) return
357
- if (node.accessibility === "private" || node.accessibility === "protected") return
358
- const first = runtimeParams(node)[0]
359
- const inherited = inheritedDelivery(context, node)
360
- if (!(first && isDeliveryParam(context, first)) && !inherited.delivery) return
361
- judgeClaim(node, inherited)
423
+ if (node.kind !== "method" || node.static || node.key.type === "PrivateIdentifier" || !node.value.body)
424
+ return;
425
+ if (node.accessibility === "private" || node.accessibility === "protected")
426
+ return;
427
+ const first = runtimeParams(node.value.params)[0];
428
+ const inherited = inheritedDelivery(context, node);
429
+ if (!(first && isDeliveryParam(context, first)) && !inherited.delivery)
430
+ return;
431
+ judgeClaim(node, inherited);
362
432
  },
363
433
  PropertyDefinition(node) {
364
- if (node.static || node.key.type === "PrivateIdentifier" || node.accessibility === "private" || node.accessibility === "protected") return
365
- const inherited = inheritedDelivery(context, node)
366
- if (inherited.delivery) judgeClaim(node, inherited)
434
+ if (node.static || node.key.type === "PrivateIdentifier" || node.accessibility === "private" || node.accessibility === "protected")
435
+ return;
436
+ const inherited = inheritedDelivery(context, node);
437
+ if (inherited.delivery)
438
+ judgeClaim(node, inherited);
367
439
  },
368
- }
440
+ };
369
441
  },
370
- }
371
-
442
+ };
372
443
  /** The rule this law contributes to the plugin. */
373
444
  export const rules = {
374
445
  "inbox-dedupe-required": inboxDedupeRequired,
375
446
  "mutation-idempotent": mutationIdempotent,
376
- }
377
-
447
+ };
378
448
  /** Starts at error: a duplicate charge or a duplicate side effect is never a warning. */
379
449
  export const recommended = {
380
450
  "starci-be/inbox-dedupe-required": "error",
381
451
  "starci-be/mutation-idempotent": "error",
382
- }
452
+ };