@cosmicdrift/kumiko-framework 0.2.2 → 0.3.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 (191) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/package.json +124 -38
  3. package/src/__tests__/full-stack.integration.ts +2 -2
  4. package/src/api/auth-routes.ts +5 -5
  5. package/src/api/jwt.ts +2 -2
  6. package/src/api/route-registrars.ts +1 -1
  7. package/src/api/routes.ts +3 -3
  8. package/src/api/server.ts +6 -7
  9. package/src/auth/__tests__/roles.test.ts +24 -0
  10. package/src/auth/index.ts +7 -0
  11. package/src/auth/roles.ts +42 -0
  12. package/src/compliance/__tests__/duration-spec.test.ts +72 -0
  13. package/src/compliance/__tests__/profiles.test.ts +308 -0
  14. package/src/compliance/__tests__/sub-processors.test.ts +139 -0
  15. package/src/compliance/duration-spec.ts +44 -0
  16. package/src/compliance/index.ts +31 -0
  17. package/src/compliance/override-schema.ts +136 -0
  18. package/src/compliance/profiles.ts +427 -0
  19. package/src/compliance/sub-processors.ts +152 -0
  20. package/src/db/__tests__/big-int-field.test.ts +131 -0
  21. package/src/db/assert-exists-in.ts +2 -2
  22. package/src/db/cursor.ts +3 -3
  23. package/src/db/event-store-executor.ts +19 -13
  24. package/src/db/located-timestamp.ts +1 -1
  25. package/src/db/money.ts +12 -2
  26. package/src/db/pg-error.ts +1 -1
  27. package/src/db/row-helpers.ts +1 -1
  28. package/src/db/table-builder.ts +20 -5
  29. package/src/db/tenant-db.ts +9 -9
  30. package/src/engine/__tests__/_pipeline-test-utils.ts +23 -0
  31. package/src/engine/__tests__/boot-validator-api-exposure.test.ts +142 -0
  32. package/src/engine/__tests__/boot-validator-pii-retention.test.ts +570 -0
  33. package/src/engine/__tests__/boot-validator-s0-integration.test.ts +160 -0
  34. package/src/engine/__tests__/build-target.test.ts +135 -0
  35. package/src/engine/__tests__/codemod-pipeline.test.ts +551 -0
  36. package/src/engine/__tests__/entity-handlers.test.ts +3 -3
  37. package/src/engine/__tests__/event-helpers.test.ts +4 -4
  38. package/src/engine/__tests__/pipeline-engine.test.ts +215 -0
  39. package/src/engine/__tests__/pipeline-handler.integration.ts +894 -0
  40. package/src/engine/__tests__/pipeline-observability.integration.ts +142 -0
  41. package/src/engine/__tests__/pipeline-performance.integration.ts +152 -0
  42. package/src/engine/__tests__/pipeline-sub-pipelines.test.ts +288 -0
  43. package/src/engine/__tests__/raw-table.test.ts +2 -2
  44. package/src/engine/__tests__/steps-aggregate-append-event.test.ts +115 -0
  45. package/src/engine/__tests__/steps-aggregate-create.test.ts +92 -0
  46. package/src/engine/__tests__/steps-aggregate-update.test.ts +127 -0
  47. package/src/engine/__tests__/steps-call-feature.test.ts +123 -0
  48. package/src/engine/__tests__/steps-mail-send.test.ts +136 -0
  49. package/src/engine/__tests__/steps-read.test.ts +142 -0
  50. package/src/engine/__tests__/steps-resolver-utils.test.ts +50 -0
  51. package/src/engine/__tests__/steps-unsafe-projection-delete.test.ts +69 -0
  52. package/src/engine/__tests__/steps-unsafe-projection-upsert.test.ts +117 -0
  53. package/src/engine/__tests__/steps-webhook-send.test.ts +135 -0
  54. package/src/engine/__tests__/steps-workflow.test.ts +198 -0
  55. package/src/engine/__tests__/validate-projection-allowlist.test.ts +491 -0
  56. package/src/engine/__tests__/visual-tree-patterns.test.ts +251 -0
  57. package/src/engine/boot-validator/api-ext.ts +77 -0
  58. package/src/engine/boot-validator/config-deps.ts +163 -0
  59. package/src/engine/boot-validator/entity-handler.ts +466 -0
  60. package/src/engine/boot-validator/index.ts +159 -0
  61. package/src/engine/boot-validator/ownership.ts +198 -0
  62. package/src/engine/boot-validator/pii-retention.ts +155 -0
  63. package/src/engine/boot-validator/screens-nav.ts +624 -0
  64. package/src/engine/boot-validator.ts +1 -1528
  65. package/src/engine/build-app-schema.ts +1 -1
  66. package/src/engine/build-target.ts +99 -0
  67. package/src/engine/codemod/index.ts +15 -0
  68. package/src/engine/codemod/pipeline-codemod.ts +641 -0
  69. package/src/engine/config-helpers.ts +9 -19
  70. package/src/engine/constants.ts +1 -1
  71. package/src/engine/define-feature.ts +127 -9
  72. package/src/engine/define-handler.ts +89 -3
  73. package/src/engine/define-roles.ts +2 -2
  74. package/src/engine/define-step.ts +28 -0
  75. package/src/engine/define-workflow.ts +110 -0
  76. package/src/engine/entity-handlers.ts +10 -9
  77. package/src/engine/event-helpers.ts +4 -4
  78. package/src/engine/extension-names.ts +105 -0
  79. package/src/engine/extensions/user-data.ts +106 -0
  80. package/src/engine/factories.ts +26 -16
  81. package/src/engine/feature-ast/__tests__/visual-tree-parse.test.ts +184 -0
  82. package/src/engine/feature-ast/extractors/index.ts +74 -0
  83. package/src/engine/feature-ast/extractors/round1.ts +110 -0
  84. package/src/engine/feature-ast/extractors/round2.ts +253 -0
  85. package/src/engine/feature-ast/extractors/round3.ts +471 -0
  86. package/src/engine/feature-ast/extractors/round4.ts +1365 -0
  87. package/src/engine/feature-ast/extractors/round5.ts +72 -0
  88. package/src/engine/feature-ast/extractors/round6.ts +66 -0
  89. package/src/engine/feature-ast/extractors/shared.ts +177 -0
  90. package/src/engine/feature-ast/parse.ts +13 -0
  91. package/src/engine/feature-ast/patch.ts +9 -1
  92. package/src/engine/feature-ast/patcher.ts +10 -3
  93. package/src/engine/feature-ast/patterns.ts +71 -1
  94. package/src/engine/feature-ast/render.ts +31 -1
  95. package/src/engine/index.ts +66 -2
  96. package/src/engine/pattern-library/__tests__/library.test.ts +11 -0
  97. package/src/engine/pattern-library/library.ts +78 -2
  98. package/src/engine/pipeline.ts +88 -0
  99. package/src/engine/projection-helpers.ts +1 -1
  100. package/src/engine/read-claim.ts +1 -1
  101. package/src/engine/registry.ts +30 -2
  102. package/src/engine/resolve-config-or-param.ts +4 -0
  103. package/src/engine/run-pipeline.ts +162 -0
  104. package/src/engine/schema-builder.ts +10 -4
  105. package/src/engine/state-machine.ts +1 -1
  106. package/src/engine/steps/_drizzle-boundary.ts +19 -0
  107. package/src/engine/steps/_duration-utils.ts +33 -0
  108. package/src/engine/steps/_no-return-guard.ts +21 -0
  109. package/src/engine/steps/_resolver-utils.ts +42 -0
  110. package/src/engine/steps/_step-dispatch-constants.ts +38 -0
  111. package/src/engine/steps/aggregate-append-event.ts +56 -0
  112. package/src/engine/steps/aggregate-create.ts +56 -0
  113. package/src/engine/steps/aggregate-update.ts +68 -0
  114. package/src/engine/steps/branch.ts +84 -0
  115. package/src/engine/steps/call-feature.ts +49 -0
  116. package/src/engine/steps/compute.ts +41 -0
  117. package/src/engine/steps/for-each.ts +111 -0
  118. package/src/engine/steps/mail-send.ts +44 -0
  119. package/src/engine/steps/read-find-many.ts +51 -0
  120. package/src/engine/steps/read-find-one.ts +58 -0
  121. package/src/engine/steps/retry.ts +87 -0
  122. package/src/engine/steps/return.ts +34 -0
  123. package/src/engine/steps/unsafe-projection-delete.ts +46 -0
  124. package/src/engine/steps/unsafe-projection-upsert.ts +69 -0
  125. package/src/engine/steps/wait-for-event.ts +71 -0
  126. package/src/engine/steps/wait.ts +69 -0
  127. package/src/engine/steps/webhook-send.ts +71 -0
  128. package/src/engine/system-user.ts +1 -1
  129. package/src/engine/types/feature.ts +143 -1
  130. package/src/engine/types/fields.ts +134 -10
  131. package/src/engine/types/handlers.ts +18 -10
  132. package/src/engine/types/identifiers.ts +1 -0
  133. package/src/engine/types/index.ts +15 -1
  134. package/src/engine/types/step.ts +334 -0
  135. package/src/engine/types/target-ref.ts +21 -0
  136. package/src/engine/types/tree-node.ts +130 -0
  137. package/src/engine/types/workspace.ts +7 -0
  138. package/src/engine/validate-projection-allowlist.ts +161 -0
  139. package/src/event-store/snapshot.ts +1 -1
  140. package/src/event-store/upcaster-dead-letter.ts +1 -1
  141. package/src/event-store/upcaster.ts +1 -1
  142. package/src/files/__tests__/read-stream.test.ts +105 -0
  143. package/src/files/__tests__/write-stream.test.ts +233 -0
  144. package/src/files/__tests__/zip-stream.test.ts +357 -0
  145. package/src/files/file-routes.ts +1 -1
  146. package/src/files/in-memory-provider.ts +38 -0
  147. package/src/files/index.ts +3 -0
  148. package/src/files/local-provider.ts +58 -1
  149. package/src/files/types.ts +36 -8
  150. package/src/files/zip-stream.ts +251 -0
  151. package/src/jobs/job-runner.ts +10 -10
  152. package/src/lifecycle/lifecycle.ts +0 -3
  153. package/src/logging/index.ts +1 -0
  154. package/src/logging/pino-logger.ts +11 -7
  155. package/src/logging/utils.ts +24 -0
  156. package/src/observability/prometheus-meter.ts +7 -5
  157. package/src/pipeline/__tests__/archive-stream.integration.ts +1 -1
  158. package/src/pipeline/__tests__/causation-chain.integration.ts +1 -1
  159. package/src/pipeline/__tests__/domain-events-projections.integration.ts +3 -3
  160. package/src/pipeline/__tests__/event-define-event-strict.integration.ts +4 -4
  161. package/src/pipeline/__tests__/load-aggregate-query.integration.ts +1 -1
  162. package/src/pipeline/__tests__/msp-multi-hop.integration.ts +3 -3
  163. package/src/pipeline/__tests__/msp-rebuild.integration.ts +3 -3
  164. package/src/pipeline/__tests__/multi-stream-projection.integration.ts +2 -2
  165. package/src/pipeline/__tests__/query-projection.integration.ts +5 -5
  166. package/src/pipeline/append-event-core.ts +22 -6
  167. package/src/pipeline/dispatcher-utils.ts +188 -0
  168. package/src/pipeline/dispatcher.ts +63 -283
  169. package/src/pipeline/distributed-lock.ts +1 -1
  170. package/src/pipeline/entity-cache.ts +2 -2
  171. package/src/pipeline/event-consumer-state.ts +0 -13
  172. package/src/pipeline/event-dispatcher.ts +4 -4
  173. package/src/pipeline/index.ts +0 -2
  174. package/src/pipeline/lifecycle-pipeline.ts +6 -12
  175. package/src/pipeline/msp-rebuild.ts +5 -5
  176. package/src/pipeline/multi-stream-apply-context.ts +6 -7
  177. package/src/pipeline/projection-rebuild.ts +2 -2
  178. package/src/pipeline/projection-state.ts +0 -12
  179. package/src/rate-limit/__tests__/resolver.integration.ts +8 -4
  180. package/src/rate-limit/resolver.ts +1 -1
  181. package/src/search/in-memory-adapter.ts +1 -1
  182. package/src/search/meilisearch-adapter.ts +3 -3
  183. package/src/search/types.ts +1 -1
  184. package/src/secrets/leak-guard.ts +2 -2
  185. package/src/stack/request-helper.ts +9 -5
  186. package/src/stack/test-stack.ts +1 -1
  187. package/src/testing/handler-context.ts +4 -4
  188. package/src/testing/http-cookies.ts +1 -1
  189. package/src/time/tz-context.ts +1 -2
  190. package/src/ui-types/index.ts +4 -0
  191. package/src/engine/feature-ast/extractors.ts +0 -2562
@@ -1,2562 +0,0 @@
1
- // Per-pattern extractors — read the arguments of an `r.<method>(...)`
2
- // call and produce the matching FeaturePattern. Each extractor is a
3
- // pure function on (CallExpression, SourceFile) and either returns a
4
- // pattern or a ParseError describing why the arguments could not be
5
- // read statically.
6
- //
7
- // **Implementation order (C1.5):**
8
- // - Round 1 (this file's first slice): the simplest static patterns —
9
- // requires, optionalRequires, readsConfig, systemScope, toggleable.
10
- // - Round 2: object-literal-based statics — entity, relation, nav,
11
- // workspace.
12
- // - Round 3: complex statics — config, translations, metric, secret,
13
- // claimKey, referenceData, useExtension.
14
- // - Round 4: mixed (header + body) — screen, writeHandler,
15
- // queryHandler, hook, entityHook, job, notification, httpRoute,
16
- // defineEvent, eventMigration, projection, multiStreamProjection.
17
- // - Round 5: opaque — authClaims, extendsRegistrar.
18
- //
19
- // Until a pattern's extractor lands, the dispatcher in parse.ts falls
20
- // back to UnknownPattern with the right method name. That's why the
21
- // dispatcher's switch lists all method names — the catch-all default
22
- // is reserved for r.* calls we have no pattern type for at all.
23
-
24
- import type { CallExpression, Node, SourceFile } from "ts-morph";
25
- import { SyntaxKind } from "ts-morph";
26
- import type { LifecycleHookType } from "../constants";
27
- import type {
28
- ConfigKeyDefinition,
29
- ConfigKeyType,
30
- JobDefinition,
31
- RunIn,
32
- TranslationKeys,
33
- } from "../types/config";
34
- import type { MetricOptions, SecretOptions } from "../types/feature";
35
- import type { EntityDefinition } from "../types/fields";
36
- import type { AccessRule, ClaimKeyType, RateLimitOption } from "../types/handlers";
37
- import type { HookPhase } from "../types/hooks";
38
- import type { HttpRouteMethod } from "../types/http-route";
39
- import type { NavDefinition } from "../types/nav";
40
- import type { MspErrorMode } from "../types/projection";
41
- import type { RelationDefinition } from "../types/relations";
42
- import type { ScreenDefinition } from "../types/screen";
43
- import type { WorkspaceDefinition } from "../types/workspace";
44
- import type { ParseError } from "./parse";
45
- import type {
46
- AuthClaimsPattern,
47
- ClaimKeyPattern,
48
- ConfigPattern,
49
- DefineEventPattern,
50
- EntityHookPattern,
51
- EntityPattern,
52
- EventMigrationPattern,
53
- ExtendsRegistrarPattern,
54
- HookPattern,
55
- HttpRoutePattern,
56
- JobPattern,
57
- MetricPattern,
58
- MultiStreamProjectionPattern,
59
- NavPattern,
60
- NotificationPattern,
61
- OpaquePropMap,
62
- OptionalRequiresPattern,
63
- ProjectionPattern,
64
- QueryHandlerPattern,
65
- ReadsConfigPattern,
66
- ReferenceDataPattern,
67
- RelationPattern,
68
- RequiresPattern,
69
- ScreenPattern,
70
- SecretPattern,
71
- SystemScopePattern,
72
- ToggleablePattern,
73
- TranslationsPattern,
74
- UseExtensionPattern,
75
- WorkspacePattern,
76
- WriteHandlerPattern,
77
- } from "./patterns";
78
- import { SCREEN_OPAQUE_MARKER } from "./patterns";
79
- import type { SourceLocation } from "./source-location";
80
- import { sourceLocationFromNode } from "./source-location";
81
-
82
- // =============================================================================
83
- // Result helpers — every extractor returns ExtractOutput so the
84
- // dispatcher can route patterns vs errors uniformly.
85
- // =============================================================================
86
-
87
- export type ExtractOutput<TPattern> =
88
- | { readonly kind: "pattern"; readonly pattern: TPattern }
89
- | { readonly kind: "error"; readonly error: ParseError };
90
-
91
- function ok<TPattern>(pattern: TPattern): ExtractOutput<TPattern> {
92
- return { kind: "pattern", pattern };
93
- }
94
-
95
- // Narrow return type lets fail() flow through both ExtractOutput<T> (where
96
- // the error variant is always valid) and through helpers like
97
- // readNamedOptions that expose the error-half directly to their callers.
98
- function fail(
99
- methodName: string,
100
- source: ParseError["source"],
101
- reason: string,
102
- ): { readonly kind: "error"; readonly error: ParseError } {
103
- return { kind: "error", error: { methodName, source, reason } };
104
- }
105
-
106
- // =============================================================================
107
- // Argument readers — small primitives reused across extractors.
108
- // =============================================================================
109
-
110
- /**
111
- * Read a list of arguments where every entry must be a string literal.
112
- * Returns the list of literal values or undefined when any argument is
113
- * not a literal (e.g. spread of a const, identifier).
114
- */
115
- function readStringLiteralArgs(call: CallExpression): readonly string[] | undefined {
116
- const out: string[] = [];
117
- for (const arg of call.getArguments()) {
118
- const literal = arg.asKind(SyntaxKind.StringLiteral);
119
- if (!literal) return undefined;
120
- out.push(literal.getLiteralValue());
121
- }
122
- return out;
123
- }
124
-
125
- /**
126
- * Read a property from an object-literal node by name and return the
127
- * boolean literal it points at. Returns undefined when the property is
128
- * missing or not a `true`/`false` literal.
129
- */
130
- function readBooleanProperty(objectLiteral: Node, propertyName: string): boolean | undefined {
131
- const obj = objectLiteral.asKind(SyntaxKind.ObjectLiteralExpression);
132
- if (!obj) return undefined;
133
- const prop = obj.getProperty(propertyName);
134
- if (!prop) return undefined;
135
- const assignment = prop.asKind(SyntaxKind.PropertyAssignment);
136
- if (!assignment) return undefined;
137
- const initializer = assignment.getInitializer();
138
- if (!initializer) return undefined;
139
- const kind = initializer.getKind();
140
- if (kind === SyntaxKind.TrueKeyword) return true;
141
- if (kind === SyntaxKind.FalseKeyword) return false;
142
- return undefined;
143
- }
144
-
145
- /**
146
- * Best-effort reader that turns a TypeScript expression into a JSON-like
147
- * value. Recurses through arrays, object literals, parenthesised
148
- * expressions, and `as`/`satisfies` wrappers. Returns undefined as
149
- * "could not read" — used as the failure signal because no legitimate
150
- * JSON value is undefined (we forbid `{ x: undefined }` shapes by
151
- * rejecting any unreadable property).
152
- *
153
- * Accepts: string / number (incl. negative literals) / boolean / null,
154
- * array literals, object literals (with PropertyAssignment props only),
155
- * `as const`, `as Type`, `satisfies Type`, parenthesised expressions.
156
- *
157
- * Rejects (returns undefined): identifiers, function calls, arrow
158
- * functions, template literals with substitutions, spread props,
159
- * shorthand props, methods, computed keys.
160
- */
161
- function readDataLiteralNode(node: Node): unknown {
162
- const kind = node.getKind();
163
- switch (kind) {
164
- case SyntaxKind.StringLiteral:
165
- return node.asKindOrThrow(SyntaxKind.StringLiteral).getLiteralValue();
166
- case SyntaxKind.NoSubstitutionTemplateLiteral:
167
- return node.asKindOrThrow(SyntaxKind.NoSubstitutionTemplateLiteral).getLiteralValue();
168
- case SyntaxKind.NumericLiteral:
169
- return Number(node.asKindOrThrow(SyntaxKind.NumericLiteral).getText());
170
- case SyntaxKind.TrueKeyword:
171
- return true;
172
- case SyntaxKind.FalseKeyword:
173
- return false;
174
- case SyntaxKind.NullKeyword:
175
- return null;
176
- case SyntaxKind.PrefixUnaryExpression: {
177
- // Negative number literals: -1, -2.5
178
- const expr = node.asKindOrThrow(SyntaxKind.PrefixUnaryExpression);
179
- if (expr.getOperatorToken() !== SyntaxKind.MinusToken) return undefined;
180
- const inner = readDataLiteralNode(expr.getOperand());
181
- if (typeof inner !== "number") return undefined;
182
- return -inner;
183
- }
184
- case SyntaxKind.ArrayLiteralExpression: {
185
- const arr = node.asKindOrThrow(SyntaxKind.ArrayLiteralExpression);
186
- const out: unknown[] = [];
187
- for (const el of arr.getElements()) {
188
- const value = readDataLiteralNode(el);
189
- if (value === undefined) return undefined;
190
- out.push(value);
191
- }
192
- return out;
193
- }
194
- case SyntaxKind.ObjectLiteralExpression: {
195
- const obj = node.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);
196
- const out: Record<string, unknown> = {};
197
- for (const prop of obj.getProperties()) {
198
- const propAssign = prop.asKind(SyntaxKind.PropertyAssignment);
199
- if (!propAssign) return undefined; // shorthand / spread / method
200
- const initializer = propAssign.getInitializer();
201
- if (!initializer) return undefined;
202
- const value = readDataLiteralNode(initializer);
203
- if (value === undefined) return undefined;
204
- out[readPropertyKey(propAssign)] = value;
205
- }
206
- return out;
207
- }
208
- case SyntaxKind.AsExpression:
209
- return readDataLiteralNode(node.asKindOrThrow(SyntaxKind.AsExpression).getExpression());
210
- case SyntaxKind.SatisfiesExpression:
211
- return readDataLiteralNode(
212
- node.asKindOrThrow(SyntaxKind.SatisfiesExpression).getExpression(),
213
- );
214
- case SyntaxKind.ParenthesizedExpression:
215
- return readDataLiteralNode(
216
- node.asKindOrThrow(SyntaxKind.ParenthesizedExpression).getExpression(),
217
- );
218
- default:
219
- return undefined;
220
- }
221
- }
222
-
223
- function isPlainObject(value: unknown): value is Record<string, unknown> {
224
- return typeof value === "object" && value !== null && !Array.isArray(value);
225
- }
226
-
227
- /**
228
- * Read a PropertyAssignment's key as the unquoted string. ts-morph's
229
- * getName() returns the source text including quote chars for keys like
230
- * `"task.created"`; we strip them so consumers see the same literal
231
- * value whether the author used identifier or string-key form.
232
- */
233
- function readPropertyKey(propAssign: import("ts-morph").PropertyAssignment): string {
234
- const nameNode = propAssign.getNameNode();
235
- const literal = nameNode.asKind(SyntaxKind.StringLiteral);
236
- if (literal) return literal.getLiteralValue();
237
- return propAssign.getName();
238
- }
239
-
240
- /**
241
- * Read a NameOrRef argument: either a string literal or an inline
242
- * object literal `{ name: "..." }`. Identifier references (e.g. a
243
- * captured const) cannot be resolved statically and return undefined.
244
- */
245
- function readNameOrRef(node: Node): string | undefined {
246
- const literal = node.asKind(SyntaxKind.StringLiteral);
247
- if (literal) return literal.getLiteralValue();
248
- const obj = readDataLiteralNode(node);
249
- if (isPlainObject(obj) && typeof obj["name"] === "string") return obj["name"];
250
- return undefined;
251
- }
252
-
253
- /**
254
- * Match a node that looks like a function literal — arrow function,
255
- * function expression, or one of those wrapped in parentheses. Returns
256
- * undefined for identifiers / call expressions / other shapes (a hook
257
- * registered by passing a const reference, for example, won't be
258
- * resolved statically).
259
- */
260
- function findFunctionLiteral(node: Node): Node | undefined {
261
- if (node.getKind() === SyntaxKind.ArrowFunction) return node;
262
- if (node.getKind() === SyntaxKind.FunctionExpression) return node;
263
- const paren = node.asKind(SyntaxKind.ParenthesizedExpression);
264
- if (paren) return findFunctionLiteral(paren.getExpression());
265
- return undefined;
266
- }
267
-
268
- /**
269
- * Read a NameOrRef argument or an array of them. Returns either the
270
- * single string or the list. undefined when neither shape matches.
271
- */
272
- function readNameOrRefOrList(node: Node): string | readonly string[] | undefined {
273
- const single = readNameOrRef(node);
274
- if (single) return single;
275
- const arr = node.asKind(SyntaxKind.ArrayLiteralExpression);
276
- if (!arr) return undefined;
277
- const out: string[] = [];
278
- for (const el of arr.getElements()) {
279
- const name = readNameOrRef(el);
280
- if (!name) return undefined;
281
- out.push(name);
282
- }
283
- return out;
284
- }
285
-
286
- // =============================================================================
287
- // Round 1 — simplest static patterns
288
- // =============================================================================
289
-
290
- // Reads either varargs string literals, or a single { features: string[] } /
291
- // { keys: string[] } object — covers both the legacy positional form and
292
- // the canonical Object-Form. `arrayPropName` controls which property name
293
- // the object form uses (`features` for requires, `keys` for readsConfig).
294
- function readVarargsOrArrayProp(
295
- call: CallExpression,
296
- arrayPropName: "features" | "keys",
297
- ): readonly string[] | undefined {
298
- const args = call.getArguments();
299
- // Object-Form: single object-literal arg with the named array property.
300
- if (args.length === 1) {
301
- const obj = args[0]?.asKind(SyntaxKind.ObjectLiteralExpression);
302
- if (obj) {
303
- const propInit = obj
304
- .getProperty(arrayPropName)
305
- ?.asKind(SyntaxKind.PropertyAssignment)
306
- ?.getInitializer();
307
- if (propInit) {
308
- const arr = propInit.asKind(SyntaxKind.ArrayLiteralExpression);
309
- if (!arr) return undefined;
310
- const out: string[] = [];
311
- for (const el of arr.getElements()) {
312
- const lit = el.asKind(SyntaxKind.StringLiteral);
313
- if (!lit) return undefined;
314
- out.push(lit.getLiteralValue());
315
- }
316
- return out;
317
- }
318
- }
319
- }
320
- // Legacy positional form: every arg must be a string literal.
321
- return readStringLiteralArgs(call);
322
- }
323
-
324
- export function extractRequires(
325
- call: CallExpression,
326
- sourceFile: SourceFile,
327
- ): ExtractOutput<RequiresPattern> {
328
- const names = readVarargsOrArrayProp(call, "features");
329
- if (!names) {
330
- return fail(
331
- "requires",
332
- sourceLocationFromNode(call, sourceFile),
333
- "expected positional string literals or { features: string[] }",
334
- );
335
- }
336
- return ok({
337
- kind: "requires",
338
- source: sourceLocationFromNode(call, sourceFile),
339
- featureNames: names,
340
- });
341
- }
342
-
343
- export function extractOptionalRequires(
344
- call: CallExpression,
345
- sourceFile: SourceFile,
346
- ): ExtractOutput<OptionalRequiresPattern> {
347
- const names = readVarargsOrArrayProp(call, "features");
348
- if (!names) {
349
- return fail(
350
- "optionalRequires",
351
- sourceLocationFromNode(call, sourceFile),
352
- "expected positional string literals or { features: string[] }",
353
- );
354
- }
355
- return ok({
356
- kind: "optionalRequires",
357
- source: sourceLocationFromNode(call, sourceFile),
358
- featureNames: names,
359
- });
360
- }
361
-
362
- export function extractReadsConfig(
363
- call: CallExpression,
364
- sourceFile: SourceFile,
365
- ): ExtractOutput<ReadsConfigPattern> {
366
- const keys = readVarargsOrArrayProp(call, "keys");
367
- if (!keys) {
368
- return fail(
369
- "readsConfig",
370
- sourceLocationFromNode(call, sourceFile),
371
- "expected positional string literals or { keys: string[] }",
372
- );
373
- }
374
- return ok({
375
- kind: "readsConfig",
376
- source: sourceLocationFromNode(call, sourceFile),
377
- qualifiedKeys: keys,
378
- });
379
- }
380
-
381
- export function extractSystemScope(
382
- call: CallExpression,
383
- sourceFile: SourceFile,
384
- ): ExtractOutput<SystemScopePattern> {
385
- // r.systemScope() takes no arguments. We don't fail when extras are
386
- // present — the runtime ignores them, and the Designer doesn't lose
387
- // anything by dropping them.
388
- return ok({
389
- kind: "systemScope",
390
- source: sourceLocationFromNode(call, sourceFile),
391
- });
392
- }
393
-
394
- export function extractToggleable(
395
- call: CallExpression,
396
- sourceFile: SourceFile,
397
- ): ExtractOutput<ToggleablePattern> {
398
- const arg = call.getArguments()[0];
399
- if (!arg) {
400
- return fail(
401
- "toggleable",
402
- sourceLocationFromNode(call, sourceFile),
403
- "expected an object argument with a `default` boolean",
404
- );
405
- }
406
- const defaultValue = readBooleanProperty(arg, "default");
407
- if (defaultValue === undefined) {
408
- return fail(
409
- "toggleable",
410
- sourceLocationFromNode(call, sourceFile),
411
- "argument must be `{ default: true | false }`",
412
- );
413
- }
414
- return ok({
415
- kind: "toggleable",
416
- source: sourceLocationFromNode(call, sourceFile),
417
- default: defaultValue,
418
- });
419
- }
420
-
421
- // =============================================================================
422
- // Round 2 — object-literal-based static patterns
423
- //
424
- // These read a definition object via readDataLiteralNode. The reader is
425
- // best-effort: function-typed properties (e.g. EntityDefinition with a
426
- // computed `default`) make the extractor fail with a ParseError that
427
- // the Designer/AI surface as "this entity has custom code, can't edit".
428
- // Plain-data shapes round-trip cleanly.
429
- // =============================================================================
430
-
431
- export function extractEntity(
432
- call: CallExpression,
433
- sourceFile: SourceFile,
434
- ): ExtractOutput<EntityPattern> {
435
- const args = call.getArguments();
436
- const first = args[0];
437
- if (!first) {
438
- return fail(
439
- "entity",
440
- sourceLocationFromNode(call, sourceFile),
441
- "expected at least one argument",
442
- );
443
- }
444
-
445
- // Object-Form: r.entity({ name, fields, ...rest })
446
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
447
- if (obj && args.length === 1) {
448
- const nameInit = obj
449
- .getProperty("name")
450
- ?.asKind(SyntaxKind.PropertyAssignment)
451
- ?.getInitializer()
452
- ?.asKind(SyntaxKind.StringLiteral);
453
- if (!nameInit) {
454
- return fail(
455
- "entity",
456
- sourceLocationFromNode(call, sourceFile),
457
- "object form requires a string-literal `name` property",
458
- );
459
- }
460
- const definition = readDataLiteralNode(obj);
461
- if (!isPlainObject(definition)) {
462
- return fail(
463
- "entity",
464
- sourceLocationFromNode(call, sourceFile),
465
- "definition could not be read as a plain object (contains functions or identifiers)",
466
- );
467
- }
468
- // Strip the `name` property — it lives on EntityPattern.entityName.
469
- const { name: _name, ...defWithoutName } = definition;
470
- return ok({
471
- kind: "entity",
472
- source: sourceLocationFromNode(call, sourceFile),
473
- entityName: nameInit.getLiteralValue(),
474
- definition: defWithoutName as EntityDefinition,
475
- });
476
- }
477
-
478
- // Legacy positional form: r.entity("name", { fields, ... })
479
- const nameArg = first.asKind(SyntaxKind.StringLiteral);
480
- if (!nameArg) {
481
- return fail(
482
- "entity",
483
- sourceLocationFromNode(call, sourceFile),
484
- "first argument must be a string literal name (or use the object form)",
485
- );
486
- }
487
- const defArg = args[1];
488
- if (!defArg) {
489
- return fail(
490
- "entity",
491
- sourceLocationFromNode(call, sourceFile),
492
- "expected a definition object as second argument",
493
- );
494
- }
495
- const definition = readDataLiteralNode(defArg);
496
- if (!isPlainObject(definition)) {
497
- return fail(
498
- "entity",
499
- sourceLocationFromNode(call, sourceFile),
500
- "definition could not be read as a plain object (contains functions or identifiers)",
501
- );
502
- }
503
- return ok({
504
- kind: "entity",
505
- source: sourceLocationFromNode(call, sourceFile),
506
- entityName: nameArg.getLiteralValue(),
507
- // The reader produced a JSON-like object whose runtime shape comes
508
- // from source code that already type-checks against EntityDefinition.
509
- // Downstream consumers (Designer, validator) may re-validate before use.
510
- definition: definition as EntityDefinition,
511
- });
512
- }
513
-
514
- export function extractRelation(
515
- call: CallExpression,
516
- sourceFile: SourceFile,
517
- ): ExtractOutput<RelationPattern> {
518
- const args = call.getArguments();
519
- const first = args[0];
520
- if (!first) {
521
- return fail(
522
- "relation",
523
- sourceLocationFromNode(call, sourceFile),
524
- "expected at least one argument",
525
- );
526
- }
527
-
528
- // Object-Form: r.relation({ entity, name, kind, to, ...rest })
529
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
530
- if (obj && args.length === 1) {
531
- const entityInit = obj
532
- .getProperty("entity")
533
- ?.asKind(SyntaxKind.PropertyAssignment)
534
- ?.getInitializer();
535
- if (!entityInit) {
536
- return fail(
537
- "relation",
538
- sourceLocationFromNode(call, sourceFile),
539
- "object form requires an `entity` property",
540
- );
541
- }
542
- const entityName = readNameOrRef(entityInit);
543
- if (!entityName) {
544
- return fail(
545
- "relation",
546
- sourceLocationFromNode(call, sourceFile),
547
- '`entity` must be a string literal or `{ name: "..." }` ref',
548
- );
549
- }
550
- const nameInit = obj
551
- .getProperty("name")
552
- ?.asKind(SyntaxKind.PropertyAssignment)
553
- ?.getInitializer()
554
- ?.asKind(SyntaxKind.StringLiteral);
555
- if (!nameInit) {
556
- return fail(
557
- "relation",
558
- sourceLocationFromNode(call, sourceFile),
559
- "object form requires a string-literal `name` property",
560
- );
561
- }
562
- const definition = readDataLiteralNode(obj);
563
- if (!isPlainObject(definition)) {
564
- return fail(
565
- "relation",
566
- sourceLocationFromNode(call, sourceFile),
567
- "definition could not be read as a plain object",
568
- );
569
- }
570
- // Strip the carrier-properties — `entity` and `name` live separately
571
- // on the pattern, the rest stays in `definition`.
572
- const { entity: _e, name: _n, ...defWithoutCarriers } = definition;
573
- return ok({
574
- kind: "relation",
575
- source: sourceLocationFromNode(call, sourceFile),
576
- entityName,
577
- relationName: nameInit.getLiteralValue(),
578
- definition: defWithoutCarriers as RelationDefinition,
579
- });
580
- }
581
-
582
- // Legacy positional: r.relation(entity, name, def)
583
- const entityName = readNameOrRef(first);
584
- if (!entityName) {
585
- return fail(
586
- "relation",
587
- sourceLocationFromNode(call, sourceFile),
588
- 'first argument must be a string literal or an inline { name: "..." } object (or use the object form)',
589
- );
590
- }
591
- const nameArg = args[1]?.asKind(SyntaxKind.StringLiteral);
592
- if (!nameArg) {
593
- return fail(
594
- "relation",
595
- sourceLocationFromNode(call, sourceFile),
596
- "second argument must be a string literal relation name",
597
- );
598
- }
599
- const defArg = args[2];
600
- if (!defArg) {
601
- return fail(
602
- "relation",
603
- sourceLocationFromNode(call, sourceFile),
604
- "expected a definition object as third argument",
605
- );
606
- }
607
- const definition = readDataLiteralNode(defArg);
608
- if (!isPlainObject(definition)) {
609
- return fail(
610
- "relation",
611
- sourceLocationFromNode(call, sourceFile),
612
- "definition could not be read as a plain object",
613
- );
614
- }
615
- return ok({
616
- kind: "relation",
617
- source: sourceLocationFromNode(call, sourceFile),
618
- entityName,
619
- relationName: nameArg.getLiteralValue(),
620
- definition: definition as RelationDefinition,
621
- });
622
- }
623
-
624
- export function extractNav(
625
- call: CallExpression,
626
- sourceFile: SourceFile,
627
- ): ExtractOutput<NavPattern> {
628
- const arg = call.getArguments()[0];
629
- if (!arg) {
630
- return fail(
631
- "nav",
632
- sourceLocationFromNode(call, sourceFile),
633
- "expected a NavDefinition object as first argument",
634
- );
635
- }
636
- const definition = readDataLiteralNode(arg);
637
- if (!isPlainObject(definition)) {
638
- return fail(
639
- "nav",
640
- sourceLocationFromNode(call, sourceFile),
641
- "definition could not be read as a plain object",
642
- );
643
- }
644
- return ok({
645
- kind: "nav",
646
- source: sourceLocationFromNode(call, sourceFile),
647
- definition: definition as NavDefinition,
648
- });
649
- }
650
-
651
- export function extractWorkspace(
652
- call: CallExpression,
653
- sourceFile: SourceFile,
654
- ): ExtractOutput<WorkspacePattern> {
655
- const arg = call.getArguments()[0];
656
- if (!arg) {
657
- return fail(
658
- "workspace",
659
- sourceLocationFromNode(call, sourceFile),
660
- "expected a WorkspaceDefinition object as first argument",
661
- );
662
- }
663
- const definition = readDataLiteralNode(arg);
664
- if (!isPlainObject(definition)) {
665
- return fail(
666
- "workspace",
667
- sourceLocationFromNode(call, sourceFile),
668
- "definition could not be read as a plain object",
669
- );
670
- }
671
- return ok({
672
- kind: "workspace",
673
- source: sourceLocationFromNode(call, sourceFile),
674
- definition: definition as WorkspaceDefinition,
675
- });
676
- }
677
-
678
- // =============================================================================
679
- // Round 3 — complex static patterns
680
- //
681
- // Two-argument extractors (metric, secret, claimKey) take a string-literal
682
- // short name plus an options object. The options-object extractors
683
- // (config, translations) wrap a `keys` map. referenceData/useExtension
684
- // take an entity reference plus payload.
685
- // =============================================================================
686
-
687
- export function extractConfig(
688
- call: CallExpression,
689
- sourceFile: SourceFile,
690
- ): ExtractOutput<ConfigPattern> {
691
- const arg = call.getArguments()[0];
692
- if (!arg) {
693
- return fail(
694
- "config",
695
- sourceLocationFromNode(call, sourceFile),
696
- "expected `{ keys: { ... } }` as first argument",
697
- );
698
- }
699
- const obj = readDataLiteralNode(arg);
700
- if (!isPlainObject(obj)) {
701
- return fail(
702
- "config",
703
- sourceLocationFromNode(call, sourceFile),
704
- "argument could not be read as a plain object",
705
- );
706
- }
707
- const keys = obj["keys"];
708
- if (!isPlainObject(keys)) {
709
- return fail(
710
- "config",
711
- sourceLocationFromNode(call, sourceFile),
712
- "missing or non-object `keys` property",
713
- );
714
- }
715
- return ok({
716
- kind: "config",
717
- source: sourceLocationFromNode(call, sourceFile),
718
- keys: keys as Readonly<Record<string, ConfigKeyDefinition<ConfigKeyType>>>,
719
- });
720
- }
721
-
722
- export function extractTranslations(
723
- call: CallExpression,
724
- sourceFile: SourceFile,
725
- ): ExtractOutput<TranslationsPattern> {
726
- const arg = call.getArguments()[0];
727
- if (!arg) {
728
- return fail(
729
- "translations",
730
- sourceLocationFromNode(call, sourceFile),
731
- "expected `{ keys: { ... } }` as first argument",
732
- );
733
- }
734
- const obj = readDataLiteralNode(arg);
735
- if (!isPlainObject(obj)) {
736
- return fail(
737
- "translations",
738
- sourceLocationFromNode(call, sourceFile),
739
- "argument could not be read as a plain object",
740
- );
741
- }
742
- const keys = obj["keys"];
743
- if (!isPlainObject(keys)) {
744
- return fail(
745
- "translations",
746
- sourceLocationFromNode(call, sourceFile),
747
- "missing or non-object `keys` property",
748
- );
749
- }
750
- return ok({
751
- kind: "translations",
752
- source: sourceLocationFromNode(call, sourceFile),
753
- keys: keys as TranslationKeys,
754
- });
755
- }
756
-
757
- // Shared shape for extractors that take a name + options bag — accepts
758
- // both positional `(name, { ...options })` and single object-form
759
- // `({ name, ...options })`. Returns the parsed name + the options bag
760
- // (options bag minus the `name` property in object-form), or routes
761
- // the failure through `fail()` so error-reason strings don't show up
762
- // as object-literal `reason:` properties (the error-reasons-guard
763
- // expects snake_case for those, and our parser-error reasons are
764
- // human-prose).
765
- type NamedOptionsResult =
766
- | { readonly kind: "ok"; readonly name: string; readonly options: Record<string, unknown> }
767
- | { readonly kind: "error"; readonly error: ParseError };
768
-
769
- function readNamedOptions(
770
- call: CallExpression,
771
- sourceFile: SourceFile,
772
- methodName: string,
773
- ): NamedOptionsResult {
774
- const args = call.getArguments();
775
- const first = args[0];
776
- if (!first) {
777
- return fail(
778
- methodName,
779
- sourceLocationFromNode(call, sourceFile),
780
- "expected at least one argument",
781
- );
782
- }
783
-
784
- // Object-Form: r.method({ name: "...", ...options })
785
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
786
- if (obj && args.length === 1) {
787
- const nameInit = obj
788
- .getProperty("name")
789
- ?.asKind(SyntaxKind.PropertyAssignment)
790
- ?.getInitializer()
791
- ?.asKind(SyntaxKind.StringLiteral);
792
- if (!nameInit) {
793
- return fail(
794
- methodName,
795
- sourceLocationFromNode(call, sourceFile),
796
- "object form requires a string-literal `name` property",
797
- );
798
- }
799
- const data = readDataLiteralNode(obj);
800
- if (!isPlainObject(data)) {
801
- return fail(
802
- methodName,
803
- sourceLocationFromNode(call, sourceFile),
804
- "argument could not be read as a plain object",
805
- );
806
- }
807
- const { name: _name, ...optionsWithoutName } = data;
808
- return { kind: "ok", name: nameInit.getLiteralValue(), options: optionsWithoutName };
809
- }
810
-
811
- // Legacy positional: r.method("name", { ...options })
812
- const nameLiteral = first.asKind(SyntaxKind.StringLiteral);
813
- if (!nameLiteral) {
814
- return fail(
815
- methodName,
816
- sourceLocationFromNode(call, sourceFile),
817
- "first argument must be a string literal name (or use the object form)",
818
- );
819
- }
820
- const optionsArg = args[1];
821
- if (!optionsArg) {
822
- return fail(
823
- methodName,
824
- sourceLocationFromNode(call, sourceFile),
825
- "expected an options object as second argument",
826
- );
827
- }
828
- const options = readDataLiteralNode(optionsArg);
829
- if (!isPlainObject(options)) {
830
- return fail(
831
- methodName,
832
- sourceLocationFromNode(call, sourceFile),
833
- "options could not be read as a plain object",
834
- );
835
- }
836
- return { kind: "ok", name: nameLiteral.getLiteralValue(), options };
837
- }
838
-
839
- export function extractMetric(
840
- call: CallExpression,
841
- sourceFile: SourceFile,
842
- ): ExtractOutput<MetricPattern> {
843
- const parsed = readNamedOptions(call, sourceFile, "metric");
844
- if (parsed.kind === "error") return parsed;
845
- return ok({
846
- kind: "metric",
847
- source: sourceLocationFromNode(call, sourceFile),
848
- shortName: parsed.name,
849
- options: parsed.options as MetricOptions,
850
- });
851
- }
852
-
853
- export function extractSecret(
854
- call: CallExpression,
855
- sourceFile: SourceFile,
856
- ): ExtractOutput<SecretPattern> {
857
- const parsed = readNamedOptions(call, sourceFile, "secret");
858
- if (parsed.kind === "error") return parsed;
859
- return ok({
860
- kind: "secret",
861
- source: sourceLocationFromNode(call, sourceFile),
862
- shortName: parsed.name,
863
- options: parsed.options as SecretOptions,
864
- });
865
- }
866
-
867
- export function extractClaimKey(
868
- call: CallExpression,
869
- sourceFile: SourceFile,
870
- ): ExtractOutput<ClaimKeyPattern> {
871
- const parsed = readNamedOptions(call, sourceFile, "claimKey");
872
- if (parsed.kind === "error") return parsed;
873
- const claimType = parsed.options["type"];
874
- if (!isClaimKeyType(claimType)) {
875
- return fail(
876
- "claimKey",
877
- sourceLocationFromNode(call, sourceFile),
878
- 'type must be one of "string" | "number" | "boolean" | "string[]" | "object"',
879
- );
880
- }
881
- return ok({
882
- kind: "claimKey",
883
- source: sourceLocationFromNode(call, sourceFile),
884
- shortName: parsed.name,
885
- claimType,
886
- });
887
- }
888
-
889
- function isClaimKeyType(value: unknown): value is ClaimKeyType {
890
- return (
891
- value === "string" ||
892
- value === "number" ||
893
- value === "boolean" ||
894
- value === "string[]" ||
895
- value === "object"
896
- );
897
- }
898
-
899
- export function extractReferenceData(
900
- call: CallExpression,
901
- sourceFile: SourceFile,
902
- ): ExtractOutput<ReferenceDataPattern> {
903
- const args = call.getArguments();
904
- const first = args[0];
905
- if (!first) {
906
- return fail(
907
- "referenceData",
908
- sourceLocationFromNode(call, sourceFile),
909
- "expected at least one argument",
910
- );
911
- }
912
-
913
- // Object-Form: r.referenceData({ entity, data, upsertKey? })
914
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
915
- if (obj && args.length === 1) {
916
- const entityInit = obj
917
- .getProperty("entity")
918
- ?.asKind(SyntaxKind.PropertyAssignment)
919
- ?.getInitializer();
920
- if (!entityInit) {
921
- return fail(
922
- "referenceData",
923
- sourceLocationFromNode(call, sourceFile),
924
- "object form requires an `entity` property",
925
- );
926
- }
927
- const entityName = readNameOrRef(entityInit);
928
- if (!entityName) {
929
- return fail(
930
- "referenceData",
931
- sourceLocationFromNode(call, sourceFile),
932
- '`entity` must be a string literal or `{ name: "..." }` ref',
933
- );
934
- }
935
- const dataInit = obj
936
- .getProperty("data")
937
- ?.asKind(SyntaxKind.PropertyAssignment)
938
- ?.getInitializer();
939
- if (!dataInit) {
940
- return fail(
941
- "referenceData",
942
- sourceLocationFromNode(call, sourceFile),
943
- "object form requires a `data` property",
944
- );
945
- }
946
- const data = readDataLiteralNode(dataInit);
947
- if (!Array.isArray(data) || !data.every(isPlainObject)) {
948
- return fail(
949
- "referenceData",
950
- sourceLocationFromNode(call, sourceFile),
951
- "data must be an array of plain objects",
952
- );
953
- }
954
- let upsertKey: string | undefined;
955
- const upsertKeyInit = obj
956
- .getProperty("upsertKey")
957
- ?.asKind(SyntaxKind.PropertyAssignment)
958
- ?.getInitializer()
959
- ?.asKind(SyntaxKind.StringLiteral);
960
- if (upsertKeyInit) {
961
- upsertKey = upsertKeyInit.getLiteralValue();
962
- }
963
- return ok({
964
- kind: "referenceData",
965
- source: sourceLocationFromNode(call, sourceFile),
966
- entityName,
967
- data: data as readonly Record<string, unknown>[],
968
- ...(upsertKey !== undefined && { upsertKey }),
969
- });
970
- }
971
-
972
- // Legacy positional: r.referenceData(entity, data, options?)
973
- const entityName = readNameOrRef(first);
974
- if (!entityName) {
975
- return fail(
976
- "referenceData",
977
- sourceLocationFromNode(call, sourceFile),
978
- 'first argument must be a string literal or an inline { name: "..." } object (or use the object form)',
979
- );
980
- }
981
- const dataArg = args[1];
982
- if (!dataArg) {
983
- return fail(
984
- "referenceData",
985
- sourceLocationFromNode(call, sourceFile),
986
- "expected a data array as second argument",
987
- );
988
- }
989
- const data = readDataLiteralNode(dataArg);
990
- if (!Array.isArray(data) || !data.every(isPlainObject)) {
991
- return fail(
992
- "referenceData",
993
- sourceLocationFromNode(call, sourceFile),
994
- "data must be an array of plain objects",
995
- );
996
- }
997
- let upsertKey: string | undefined;
998
- const optionsArg = args[2];
999
- if (optionsArg) {
1000
- const options = readDataLiteralNode(optionsArg);
1001
- if (!isPlainObject(options)) {
1002
- return fail(
1003
- "referenceData",
1004
- sourceLocationFromNode(call, sourceFile),
1005
- "options could not be read as a plain object",
1006
- );
1007
- }
1008
- if (options["upsertKey"] !== undefined) {
1009
- if (typeof options["upsertKey"] !== "string") {
1010
- return fail(
1011
- "referenceData",
1012
- sourceLocationFromNode(call, sourceFile),
1013
- "upsertKey must be a string when provided",
1014
- );
1015
- }
1016
- upsertKey = options["upsertKey"];
1017
- }
1018
- }
1019
- return ok({
1020
- kind: "referenceData",
1021
- source: sourceLocationFromNode(call, sourceFile),
1022
- entityName,
1023
- data: data as readonly Record<string, unknown>[],
1024
- ...(upsertKey !== undefined && { upsertKey }),
1025
- });
1026
- }
1027
-
1028
- export function extractUseExtension(
1029
- call: CallExpression,
1030
- sourceFile: SourceFile,
1031
- ): ExtractOutput<UseExtensionPattern> {
1032
- const args = call.getArguments();
1033
- const first = args[0];
1034
- if (!first) {
1035
- return fail(
1036
- "useExtension",
1037
- sourceLocationFromNode(call, sourceFile),
1038
- "expected at least one argument",
1039
- );
1040
- }
1041
-
1042
- // Object-Form: r.useExtension({ name, entity, options? })
1043
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
1044
- if (obj && args.length === 1) {
1045
- const nameInit = obj
1046
- .getProperty("name")
1047
- ?.asKind(SyntaxKind.PropertyAssignment)
1048
- ?.getInitializer()
1049
- ?.asKind(SyntaxKind.StringLiteral);
1050
- if (!nameInit) {
1051
- return fail(
1052
- "useExtension",
1053
- sourceLocationFromNode(call, sourceFile),
1054
- "object form requires a string-literal `name` property",
1055
- );
1056
- }
1057
- const entityInit = obj
1058
- .getProperty("entity")
1059
- ?.asKind(SyntaxKind.PropertyAssignment)
1060
- ?.getInitializer();
1061
- if (!entityInit) {
1062
- return fail(
1063
- "useExtension",
1064
- sourceLocationFromNode(call, sourceFile),
1065
- "object form requires an `entity` property",
1066
- );
1067
- }
1068
- const entityName = readNameOrRef(entityInit);
1069
- if (!entityName) {
1070
- return fail(
1071
- "useExtension",
1072
- sourceLocationFromNode(call, sourceFile),
1073
- '`entity` must be a string literal or `{ name: "..." }` ref',
1074
- );
1075
- }
1076
- let options: Readonly<Record<string, unknown>> | undefined;
1077
- const optionsInit = obj
1078
- .getProperty("options")
1079
- ?.asKind(SyntaxKind.PropertyAssignment)
1080
- ?.getInitializer();
1081
- if (optionsInit) {
1082
- const parsed = readDataLiteralNode(optionsInit);
1083
- if (!isPlainObject(parsed)) {
1084
- return fail(
1085
- "useExtension",
1086
- sourceLocationFromNode(call, sourceFile),
1087
- "options could not be read as a plain object",
1088
- );
1089
- }
1090
- options = parsed;
1091
- }
1092
- return ok({
1093
- kind: "useExtension",
1094
- source: sourceLocationFromNode(call, sourceFile),
1095
- extensionName: nameInit.getLiteralValue(),
1096
- entityName,
1097
- ...(options !== undefined && { options }),
1098
- });
1099
- }
1100
-
1101
- // Legacy positional: r.useExtension(name, entity, options?)
1102
- const nameArg = first.asKind(SyntaxKind.StringLiteral);
1103
- if (!nameArg) {
1104
- return fail(
1105
- "useExtension",
1106
- sourceLocationFromNode(call, sourceFile),
1107
- "first argument must be a string literal extension name (or use the object form)",
1108
- );
1109
- }
1110
- const entityRefArg = args[1];
1111
- if (!entityRefArg) {
1112
- return fail(
1113
- "useExtension",
1114
- sourceLocationFromNode(call, sourceFile),
1115
- "expected an entity reference as second argument",
1116
- );
1117
- }
1118
- const entityName = readNameOrRef(entityRefArg);
1119
- if (!entityName) {
1120
- return fail(
1121
- "useExtension",
1122
- sourceLocationFromNode(call, sourceFile),
1123
- 'second argument must be a string literal or an inline { name: "..." } object',
1124
- );
1125
- }
1126
- const optionsArg = args[2];
1127
- let options: Readonly<Record<string, unknown>> | undefined;
1128
- if (optionsArg) {
1129
- const parsed = readDataLiteralNode(optionsArg);
1130
- if (!isPlainObject(parsed)) {
1131
- return fail(
1132
- "useExtension",
1133
- sourceLocationFromNode(call, sourceFile),
1134
- "options could not be read as a plain object",
1135
- );
1136
- }
1137
- options = parsed;
1138
- }
1139
- return ok({
1140
- kind: "useExtension",
1141
- source: sourceLocationFromNode(call, sourceFile),
1142
- extensionName: nameArg.getLiteralValue(),
1143
- entityName,
1144
- ...(options !== undefined && { options }),
1145
- });
1146
- }
1147
-
1148
- // =============================================================================
1149
- // Round 4 — mixed patterns (header data + opaque body source)
1150
- //
1151
- // Each extractor reads the static parts (name, type, target) declaratively
1152
- // and captures any closure / Zod-schema as a SourceLocation pointing at
1153
- // the raw source span. Designer renders the body as a read-only block;
1154
- // the AI patcher overwrites the span verbatim.
1155
- //
1156
- // Closure detection: findFunctionLiteral matches an inline arrow function
1157
- // or function expression. A captured-const reference (e.g. r.hook(...,
1158
- // myHandler)) is rejected with a ParseError — those need to be inlined.
1159
- // =============================================================================
1160
-
1161
- function isHookType(value: string): value is LifecycleHookType | "validation" {
1162
- return (
1163
- value === "preSave" ||
1164
- value === "postSave" ||
1165
- value === "preDelete" ||
1166
- value === "postDelete" ||
1167
- value === "preQuery" ||
1168
- value === "validation"
1169
- );
1170
- }
1171
-
1172
- function isHttpRouteMethod(value: string): value is HttpRouteMethod {
1173
- return (
1174
- value === "GET" ||
1175
- value === "POST" ||
1176
- value === "PUT" ||
1177
- value === "PATCH" ||
1178
- value === "DELETE" ||
1179
- value === "HEAD" ||
1180
- value === "OPTIONS"
1181
- );
1182
- }
1183
-
1184
- function readOptionalPhase(node: Node | undefined): HookPhase | undefined {
1185
- if (!node) return undefined;
1186
- const obj = readDataLiteralNode(node);
1187
- if (!isPlainObject(obj)) return undefined;
1188
- const phase = obj["phase"];
1189
- if (phase === "inTransaction" || phase === "afterCommit") return phase as HookPhase;
1190
- return undefined;
1191
- }
1192
-
1193
- function readOptionalAccessRule(value: unknown): AccessRule | undefined {
1194
- if (!isPlainObject(value)) return undefined;
1195
- if (Array.isArray(value["roles"]) && value["roles"].every((r) => typeof r === "string")) {
1196
- return { roles: value["roles"] as readonly string[] };
1197
- }
1198
- if (value["openToAll"] === true) {
1199
- return { openToAll: true };
1200
- }
1201
- return undefined;
1202
- }
1203
-
1204
- function readOptionalRateLimit(value: unknown): RateLimitOption | undefined {
1205
- if (!isPlainObject(value)) return undefined;
1206
- if (typeof value["per"] !== "string") return undefined;
1207
- if (typeof value["limit"] !== "number") return undefined;
1208
- if (typeof value["windowSeconds"] !== "number") return undefined;
1209
- return value as unknown as RateLimitOption;
1210
- }
1211
-
1212
- export function extractHook(
1213
- call: CallExpression,
1214
- sourceFile: SourceFile,
1215
- ): ExtractOutput<HookPattern> {
1216
- const args = call.getArguments();
1217
- const first = args[0];
1218
- if (!first) {
1219
- return fail("hook", sourceLocationFromNode(call, sourceFile), "expected at least one argument");
1220
- }
1221
-
1222
- // Object-Form: r.hook({ type, target, handler, phase? })
1223
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
1224
- if (obj && args.length === 1) {
1225
- const typeInit = obj
1226
- .getProperty("type")
1227
- ?.asKind(SyntaxKind.PropertyAssignment)
1228
- ?.getInitializer()
1229
- ?.asKind(SyntaxKind.StringLiteral);
1230
- if (!typeInit) {
1231
- return fail(
1232
- "hook",
1233
- sourceLocationFromNode(call, sourceFile),
1234
- "object form requires a string-literal `type` property",
1235
- );
1236
- }
1237
- const hookType = typeInit.getLiteralValue();
1238
- if (!isHookType(hookType)) {
1239
- return fail(
1240
- "hook",
1241
- sourceLocationFromNode(call, sourceFile),
1242
- `hook type "${hookType}" is not one of the lifecycle types or "validation"`,
1243
- );
1244
- }
1245
- const targetInit = obj
1246
- .getProperty("target")
1247
- ?.asKind(SyntaxKind.PropertyAssignment)
1248
- ?.getInitializer();
1249
- if (!targetInit) {
1250
- return fail(
1251
- "hook",
1252
- sourceLocationFromNode(call, sourceFile),
1253
- "object form requires a `target` property",
1254
- );
1255
- }
1256
- const target = readNameOrRefOrList(targetInit);
1257
- if (!target) {
1258
- return fail(
1259
- "hook",
1260
- sourceLocationFromNode(call, sourceFile),
1261
- "target must be a string literal, an inline { name } object, or an array",
1262
- );
1263
- }
1264
- const handlerInit = obj
1265
- .getProperty("handler")
1266
- ?.asKind(SyntaxKind.PropertyAssignment)
1267
- ?.getInitializer();
1268
- if (!handlerInit) {
1269
- return fail(
1270
- "hook",
1271
- sourceLocationFromNode(call, sourceFile),
1272
- "object form requires a `handler` property",
1273
- );
1274
- }
1275
- const fn = findFunctionLiteral(handlerInit);
1276
- if (!fn) {
1277
- return fail(
1278
- "hook",
1279
- sourceLocationFromNode(call, sourceFile),
1280
- "handler must be an inline arrow function or function expression",
1281
- );
1282
- }
1283
- const phase = readOptionalPhase(obj);
1284
- return ok({
1285
- kind: "hook",
1286
- source: sourceLocationFromNode(call, sourceFile),
1287
- hookType,
1288
- target,
1289
- fnBody: sourceLocationFromNode(fn, sourceFile),
1290
- ...(phase !== undefined && { phase }),
1291
- });
1292
- }
1293
-
1294
- // Legacy positional: r.hook(type, target, fn, options?)
1295
- const typeArg = first.asKind(SyntaxKind.StringLiteral);
1296
- if (!typeArg) {
1297
- return fail(
1298
- "hook",
1299
- sourceLocationFromNode(call, sourceFile),
1300
- "first argument must be a string literal hook type (or use the object form)",
1301
- );
1302
- }
1303
- const hookType = typeArg.getLiteralValue();
1304
- if (!isHookType(hookType)) {
1305
- return fail(
1306
- "hook",
1307
- sourceLocationFromNode(call, sourceFile),
1308
- `hook type "${hookType}" is not one of the lifecycle types or "validation"`,
1309
- );
1310
- }
1311
- const targetArg = args[1];
1312
- if (!targetArg) {
1313
- return fail(
1314
- "hook",
1315
- sourceLocationFromNode(call, sourceFile),
1316
- "expected a target (NameOrRef or array) as second argument",
1317
- );
1318
- }
1319
- const target = readNameOrRefOrList(targetArg);
1320
- if (!target) {
1321
- return fail(
1322
- "hook",
1323
- sourceLocationFromNode(call, sourceFile),
1324
- "target must be a string literal, an inline { name } object, or an array",
1325
- );
1326
- }
1327
- const fnArg = args[2];
1328
- if (!fnArg) {
1329
- return fail(
1330
- "hook",
1331
- sourceLocationFromNode(call, sourceFile),
1332
- "expected a hook function as third argument",
1333
- );
1334
- }
1335
- const fn = findFunctionLiteral(fnArg);
1336
- if (!fn) {
1337
- return fail(
1338
- "hook",
1339
- sourceLocationFromNode(call, sourceFile),
1340
- "third argument must be an inline arrow function or function expression",
1341
- );
1342
- }
1343
- const phase = readOptionalPhase(args[3]);
1344
- return ok({
1345
- kind: "hook",
1346
- source: sourceLocationFromNode(call, sourceFile),
1347
- hookType,
1348
- target,
1349
- fnBody: sourceLocationFromNode(fn, sourceFile),
1350
- ...(phase !== undefined && { phase }),
1351
- });
1352
- }
1353
-
1354
- function isEntityHookType(value: string): value is "postSave" | "preDelete" | "postDelete" {
1355
- return value === "postSave" || value === "preDelete" || value === "postDelete";
1356
- }
1357
-
1358
- export function extractEntityHook(
1359
- call: CallExpression,
1360
- sourceFile: SourceFile,
1361
- ): ExtractOutput<EntityHookPattern> {
1362
- const args = call.getArguments();
1363
- const first = args[0];
1364
- if (!first) {
1365
- return fail(
1366
- "entityHook",
1367
- sourceLocationFromNode(call, sourceFile),
1368
- "expected at least one argument",
1369
- );
1370
- }
1371
-
1372
- // Object-Form: r.entityHook({ type, entity, handler, phase? })
1373
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
1374
- if (obj && args.length === 1) {
1375
- const typeInit = obj
1376
- .getProperty("type")
1377
- ?.asKind(SyntaxKind.PropertyAssignment)
1378
- ?.getInitializer()
1379
- ?.asKind(SyntaxKind.StringLiteral);
1380
- if (!typeInit) {
1381
- return fail(
1382
- "entityHook",
1383
- sourceLocationFromNode(call, sourceFile),
1384
- "object form requires a string-literal `type` property",
1385
- );
1386
- }
1387
- const hookType = typeInit.getLiteralValue();
1388
- if (!isEntityHookType(hookType)) {
1389
- return fail(
1390
- "entityHook",
1391
- sourceLocationFromNode(call, sourceFile),
1392
- `entity hook type must be postSave, preDelete, or postDelete (got "${hookType}")`,
1393
- );
1394
- }
1395
- const entityInit = obj
1396
- .getProperty("entity")
1397
- ?.asKind(SyntaxKind.PropertyAssignment)
1398
- ?.getInitializer();
1399
- if (!entityInit) {
1400
- return fail(
1401
- "entityHook",
1402
- sourceLocationFromNode(call, sourceFile),
1403
- "object form requires an `entity` property",
1404
- );
1405
- }
1406
- const entityName = readNameOrRef(entityInit);
1407
- if (!entityName) {
1408
- return fail(
1409
- "entityHook",
1410
- sourceLocationFromNode(call, sourceFile),
1411
- "`entity` must be a string literal or inline { name } object",
1412
- );
1413
- }
1414
- const handlerInit = obj
1415
- .getProperty("handler")
1416
- ?.asKind(SyntaxKind.PropertyAssignment)
1417
- ?.getInitializer();
1418
- if (!handlerInit) {
1419
- return fail(
1420
- "entityHook",
1421
- sourceLocationFromNode(call, sourceFile),
1422
- "object form requires a `handler` property",
1423
- );
1424
- }
1425
- const fn = findFunctionLiteral(handlerInit);
1426
- if (!fn) {
1427
- return fail(
1428
- "entityHook",
1429
- sourceLocationFromNode(call, sourceFile),
1430
- "handler must be an inline arrow function or function expression",
1431
- );
1432
- }
1433
- const phase = readOptionalPhase(obj);
1434
- return ok({
1435
- kind: "entityHook",
1436
- source: sourceLocationFromNode(call, sourceFile),
1437
- hookType,
1438
- entityName,
1439
- fnBody: sourceLocationFromNode(fn, sourceFile),
1440
- ...(phase !== undefined && { phase }),
1441
- });
1442
- }
1443
-
1444
- // Legacy positional: r.entityHook(type, entity, fn, options?)
1445
- const typeArg = first.asKind(SyntaxKind.StringLiteral);
1446
- if (!typeArg) {
1447
- return fail(
1448
- "entityHook",
1449
- sourceLocationFromNode(call, sourceFile),
1450
- "first argument must be a string literal hook type (or use the object form)",
1451
- );
1452
- }
1453
- const hookType = typeArg.getLiteralValue();
1454
- if (!isEntityHookType(hookType)) {
1455
- return fail(
1456
- "entityHook",
1457
- sourceLocationFromNode(call, sourceFile),
1458
- `entity hook type must be postSave, preDelete, or postDelete (got "${hookType}")`,
1459
- );
1460
- }
1461
- const entityArg = args[1];
1462
- if (!entityArg) {
1463
- return fail(
1464
- "entityHook",
1465
- sourceLocationFromNode(call, sourceFile),
1466
- "expected an entity reference as second argument",
1467
- );
1468
- }
1469
- const entityName = readNameOrRef(entityArg);
1470
- if (!entityName) {
1471
- return fail(
1472
- "entityHook",
1473
- sourceLocationFromNode(call, sourceFile),
1474
- "second argument must be a string literal or inline { name } object",
1475
- );
1476
- }
1477
- const fnArg = args[2];
1478
- if (!fnArg) {
1479
- return fail(
1480
- "entityHook",
1481
- sourceLocationFromNode(call, sourceFile),
1482
- "expected a hook function as third argument",
1483
- );
1484
- }
1485
- const fn = findFunctionLiteral(fnArg);
1486
- if (!fn) {
1487
- return fail(
1488
- "entityHook",
1489
- sourceLocationFromNode(call, sourceFile),
1490
- "third argument must be an inline arrow function or function expression",
1491
- );
1492
- }
1493
- const phase = readOptionalPhase(args[3]);
1494
- return ok({
1495
- kind: "entityHook",
1496
- source: sourceLocationFromNode(call, sourceFile),
1497
- hookType,
1498
- entityName,
1499
- fnBody: sourceLocationFromNode(fn, sourceFile),
1500
- ...(phase !== undefined && { phase }),
1501
- });
1502
- }
1503
-
1504
- export function extractAuthClaims(
1505
- call: CallExpression,
1506
- sourceFile: SourceFile,
1507
- ): ExtractOutput<AuthClaimsPattern> {
1508
- const arg = call.getArguments()[0];
1509
- if (!arg) {
1510
- return fail(
1511
- "authClaims",
1512
- sourceLocationFromNode(call, sourceFile),
1513
- "expected a function as first argument",
1514
- );
1515
- }
1516
- const fn = findFunctionLiteral(arg);
1517
- if (!fn) {
1518
- return fail(
1519
- "authClaims",
1520
- sourceLocationFromNode(call, sourceFile),
1521
- "first argument must be an inline arrow function or function expression",
1522
- );
1523
- }
1524
- return ok({
1525
- kind: "authClaims",
1526
- source: sourceLocationFromNode(call, sourceFile),
1527
- fnBody: sourceLocationFromNode(fn, sourceFile),
1528
- });
1529
- }
1530
-
1531
- // Common fields produced by parseHandlerCall — both write- and query-
1532
- // handler patterns share them. The wrapper functions below add the
1533
- // kind-discriminator and the write-only skipTransitionGuard so the
1534
- // shared helper stays unbiased.
1535
- type ParsedHandlerCall = {
1536
- readonly source: SourceLocation;
1537
- readonly handlerName: string;
1538
- readonly schemaSource: SourceLocation;
1539
- readonly handlerBody: SourceLocation;
1540
- readonly access?: AccessRule;
1541
- readonly rateLimit?: RateLimitOption;
1542
- readonly skipTransitionGuard?: boolean;
1543
- };
1544
-
1545
- // Shared parser for r.writeHandler / r.queryHandler. Accepts both
1546
- // inline form r.<method>(name, schema, handler, options?) and the
1547
- // single-arg object form r.<method>({ name, schema, handler, ... })
1548
- // (the defineWriteHandler / defineQueryHandler shape).
1549
- function parseHandlerCall(
1550
- call: CallExpression,
1551
- sourceFile: SourceFile,
1552
- methodName: "writeHandler" | "queryHandler",
1553
- ): ExtractOutput<ParsedHandlerCall> {
1554
- const args = call.getArguments();
1555
- const first = args[0];
1556
- if (!first) {
1557
- return fail(
1558
- methodName,
1559
- sourceLocationFromNode(call, sourceFile),
1560
- "expected at least one argument",
1561
- );
1562
- }
1563
-
1564
- // Object form: a single { name, schema, handler, ... } literal.
1565
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
1566
- if (obj && args.length === 1) {
1567
- const nameLiteral = obj
1568
- .getProperty("name")
1569
- ?.asKind(SyntaxKind.PropertyAssignment)
1570
- ?.getInitializer()
1571
- ?.asKind(SyntaxKind.StringLiteral);
1572
- if (!nameLiteral) {
1573
- return fail(
1574
- methodName,
1575
- sourceLocationFromNode(call, sourceFile),
1576
- "object form requires a string-literal `name` property",
1577
- );
1578
- }
1579
- const schemaInit = obj
1580
- .getProperty("schema")
1581
- ?.asKind(SyntaxKind.PropertyAssignment)
1582
- ?.getInitializer();
1583
- if (!schemaInit) {
1584
- return fail(
1585
- methodName,
1586
- sourceLocationFromNode(call, sourceFile),
1587
- "object form requires a `schema` property",
1588
- );
1589
- }
1590
- const handlerInit = obj
1591
- .getProperty("handler")
1592
- ?.asKind(SyntaxKind.PropertyAssignment)
1593
- ?.getInitializer();
1594
- if (!handlerInit) {
1595
- return fail(
1596
- methodName,
1597
- sourceLocationFromNode(call, sourceFile),
1598
- "object form requires a `handler` property",
1599
- );
1600
- }
1601
- const fn = findFunctionLiteral(handlerInit);
1602
- if (!fn) {
1603
- return fail(
1604
- methodName,
1605
- sourceLocationFromNode(call, sourceFile),
1606
- "handler must be an inline arrow function or function expression",
1607
- );
1608
- }
1609
- const accessInit = obj
1610
- .getProperty("access")
1611
- ?.asKind(SyntaxKind.PropertyAssignment)
1612
- ?.getInitializer();
1613
- const access = accessInit ? readOptionalAccessRule(readDataLiteralNode(accessInit)) : undefined;
1614
- const rateLimitInit = obj
1615
- .getProperty("rateLimit")
1616
- ?.asKind(SyntaxKind.PropertyAssignment)
1617
- ?.getInitializer();
1618
- const rateLimit = rateLimitInit
1619
- ? readOptionalRateLimit(readDataLiteralNode(rateLimitInit))
1620
- : undefined;
1621
- const skip = readBooleanProperty(obj, "skipTransitionGuard");
1622
- return ok({
1623
- source: sourceLocationFromNode(call, sourceFile),
1624
- handlerName: nameLiteral.getLiteralValue(),
1625
- schemaSource: sourceLocationFromNode(schemaInit, sourceFile),
1626
- handlerBody: sourceLocationFromNode(fn, sourceFile),
1627
- ...(access !== undefined && { access }),
1628
- ...(rateLimit !== undefined && { rateLimit }),
1629
- ...(skip === true && { skipTransitionGuard: true }),
1630
- });
1631
- }
1632
-
1633
- // Inline form: (name, schema, handler, options?).
1634
- const nameLiteral = first.asKind(SyntaxKind.StringLiteral);
1635
- if (!nameLiteral) {
1636
- return fail(
1637
- methodName,
1638
- sourceLocationFromNode(call, sourceFile),
1639
- "first argument must be a string literal handler name (or use the object form)",
1640
- );
1641
- }
1642
- const schemaArg = args[1];
1643
- if (!schemaArg) {
1644
- return fail(
1645
- methodName,
1646
- sourceLocationFromNode(call, sourceFile),
1647
- "expected a Zod schema as second argument",
1648
- );
1649
- }
1650
- const handlerArg = args[2];
1651
- if (!handlerArg) {
1652
- return fail(
1653
- methodName,
1654
- sourceLocationFromNode(call, sourceFile),
1655
- "expected a handler function as third argument",
1656
- );
1657
- }
1658
- const fn = findFunctionLiteral(handlerArg);
1659
- if (!fn) {
1660
- return fail(
1661
- methodName,
1662
- sourceLocationFromNode(call, sourceFile),
1663
- "third argument must be an inline arrow function or function expression",
1664
- );
1665
- }
1666
- const optionsArg = args[3];
1667
- let access: AccessRule | undefined;
1668
- let rateLimit: RateLimitOption | undefined;
1669
- if (optionsArg) {
1670
- const options = readDataLiteralNode(optionsArg);
1671
- if (isPlainObject(options)) {
1672
- access = readOptionalAccessRule(options["access"]);
1673
- rateLimit = readOptionalRateLimit(options["rateLimit"]);
1674
- }
1675
- }
1676
- return ok({
1677
- source: sourceLocationFromNode(call, sourceFile),
1678
- handlerName: nameLiteral.getLiteralValue(),
1679
- schemaSource: sourceLocationFromNode(schemaArg, sourceFile),
1680
- handlerBody: sourceLocationFromNode(fn, sourceFile),
1681
- ...(access !== undefined && { access }),
1682
- ...(rateLimit !== undefined && { rateLimit }),
1683
- });
1684
- }
1685
-
1686
- export function extractWriteHandler(
1687
- call: CallExpression,
1688
- sourceFile: SourceFile,
1689
- ): ExtractOutput<WriteHandlerPattern> {
1690
- const parsed = parseHandlerCall(call, sourceFile, "writeHandler");
1691
- if (parsed.kind === "error") return parsed;
1692
- return ok({
1693
- kind: "writeHandler",
1694
- source: parsed.pattern.source,
1695
- handlerName: parsed.pattern.handlerName,
1696
- schemaSource: parsed.pattern.schemaSource,
1697
- handlerBody: parsed.pattern.handlerBody,
1698
- ...(parsed.pattern.access !== undefined && { access: parsed.pattern.access }),
1699
- ...(parsed.pattern.rateLimit !== undefined && { rateLimit: parsed.pattern.rateLimit }),
1700
- ...(parsed.pattern.skipTransitionGuard === true && { skipTransitionGuard: true }),
1701
- });
1702
- }
1703
-
1704
- export function extractQueryHandler(
1705
- call: CallExpression,
1706
- sourceFile: SourceFile,
1707
- ): ExtractOutput<QueryHandlerPattern> {
1708
- const parsed = parseHandlerCall(call, sourceFile, "queryHandler");
1709
- if (parsed.kind === "error") return parsed;
1710
- // QueryHandler has no skipTransitionGuard — the field is silently
1711
- // ignored if the parser reads one (won't happen in practice because
1712
- // queryHandlers don't carry that option).
1713
- return ok({
1714
- kind: "queryHandler",
1715
- source: parsed.pattern.source,
1716
- handlerName: parsed.pattern.handlerName,
1717
- schemaSource: parsed.pattern.schemaSource,
1718
- handlerBody: parsed.pattern.handlerBody,
1719
- ...(parsed.pattern.access !== undefined && { access: parsed.pattern.access }),
1720
- ...(parsed.pattern.rateLimit !== undefined && { rateLimit: parsed.pattern.rateLimit }),
1721
- });
1722
- }
1723
-
1724
- export function extractJob(
1725
- call: CallExpression,
1726
- sourceFile: SourceFile,
1727
- ): ExtractOutput<JobPattern> {
1728
- const args = call.getArguments();
1729
- const first = args[0];
1730
- if (!first) {
1731
- return fail("job", sourceLocationFromNode(call, sourceFile), "expected at least one argument");
1732
- }
1733
-
1734
- // Object-Form: r.job({ name, ...options, handler })
1735
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
1736
- if (obj && args.length === 1) {
1737
- const nameInit = obj
1738
- .getProperty("name")
1739
- ?.asKind(SyntaxKind.PropertyAssignment)
1740
- ?.getInitializer()
1741
- ?.asKind(SyntaxKind.StringLiteral);
1742
- if (!nameInit) {
1743
- return fail(
1744
- "job",
1745
- sourceLocationFromNode(call, sourceFile),
1746
- "object form requires a string-literal `name` property",
1747
- );
1748
- }
1749
- const handlerInit = obj
1750
- .getProperty("handler")
1751
- ?.asKind(SyntaxKind.PropertyAssignment)
1752
- ?.getInitializer();
1753
- if (!handlerInit) {
1754
- return fail(
1755
- "job",
1756
- sourceLocationFromNode(call, sourceFile),
1757
- "object form requires a `handler` property",
1758
- );
1759
- }
1760
- const fn = findFunctionLiteral(handlerInit);
1761
- if (!fn) {
1762
- return fail(
1763
- "job",
1764
- sourceLocationFromNode(call, sourceFile),
1765
- "handler must be an inline arrow function or function expression",
1766
- );
1767
- }
1768
- // Read every property except `name` and `handler` as the options
1769
- // bag — `handler` is a closure (not JSON-readable) and `name` lives
1770
- // separately on the pattern. Walk properties one-by-one so handler
1771
- // doesn't crash readDataLiteralNode.
1772
- const optionsBag: Record<string, unknown> = {};
1773
- for (const prop of obj.getProperties()) {
1774
- const propAssign = prop.asKind(SyntaxKind.PropertyAssignment);
1775
- if (!propAssign) continue;
1776
- const key = readPropertyKey(propAssign);
1777
- if (key === "name" || key === "handler") continue;
1778
- const init = propAssign.getInitializer();
1779
- if (!init) continue;
1780
- const value = readDataLiteralNode(init);
1781
- if (value === undefined) {
1782
- return fail(
1783
- "job",
1784
- sourceLocationFromNode(call, sourceFile),
1785
- `option "${key}" could not be read as a plain value`,
1786
- );
1787
- }
1788
- optionsBag[key] = value;
1789
- }
1790
- return ok({
1791
- kind: "job",
1792
- source: sourceLocationFromNode(call, sourceFile),
1793
- jobName: nameInit.getLiteralValue(),
1794
- options: optionsBag as Omit<JobDefinition, "name" | "handler">,
1795
- handlerBody: sourceLocationFromNode(fn, sourceFile),
1796
- });
1797
- }
1798
-
1799
- // Legacy positional: r.job(name, options, handler)
1800
- const nameArg = first.asKind(SyntaxKind.StringLiteral);
1801
- if (!nameArg) {
1802
- return fail(
1803
- "job",
1804
- sourceLocationFromNode(call, sourceFile),
1805
- "first argument must be a string literal job name (or use the object form)",
1806
- );
1807
- }
1808
- const optionsArg = args[1];
1809
- if (!optionsArg) {
1810
- return fail(
1811
- "job",
1812
- sourceLocationFromNode(call, sourceFile),
1813
- "expected an options object as second argument",
1814
- );
1815
- }
1816
- const options = readDataLiteralNode(optionsArg);
1817
- if (!isPlainObject(options)) {
1818
- return fail(
1819
- "job",
1820
- sourceLocationFromNode(call, sourceFile),
1821
- "options could not be read as a plain object",
1822
- );
1823
- }
1824
- const handlerArg = args[2];
1825
- if (!handlerArg) {
1826
- return fail(
1827
- "job",
1828
- sourceLocationFromNode(call, sourceFile),
1829
- "expected a handler function as third argument",
1830
- );
1831
- }
1832
- const fn = findFunctionLiteral(handlerArg);
1833
- if (!fn) {
1834
- return fail(
1835
- "job",
1836
- sourceLocationFromNode(call, sourceFile),
1837
- "third argument must be an inline arrow function or function expression",
1838
- );
1839
- }
1840
- return ok({
1841
- kind: "job",
1842
- source: sourceLocationFromNode(call, sourceFile),
1843
- jobName: nameArg.getLiteralValue(),
1844
- options: options as Omit<JobDefinition, "name" | "handler">,
1845
- handlerBody: sourceLocationFromNode(fn, sourceFile),
1846
- });
1847
- }
1848
-
1849
- export function extractHttpRoute(
1850
- call: CallExpression,
1851
- sourceFile: SourceFile,
1852
- ): ExtractOutput<HttpRoutePattern> {
1853
- const arg = call.getArguments()[0]?.asKind(SyntaxKind.ObjectLiteralExpression);
1854
- if (!arg) {
1855
- return fail(
1856
- "httpRoute",
1857
- sourceLocationFromNode(call, sourceFile),
1858
- "argument must be an inline HttpRouteDefinition object",
1859
- );
1860
- }
1861
- const methodLiteral = arg
1862
- .getProperty("method")
1863
- ?.asKind(SyntaxKind.PropertyAssignment)
1864
- ?.getInitializer()
1865
- ?.asKind(SyntaxKind.StringLiteral);
1866
- if (!methodLiteral) {
1867
- return fail(
1868
- "httpRoute",
1869
- sourceLocationFromNode(call, sourceFile),
1870
- "method must be a string literal",
1871
- );
1872
- }
1873
- const methodValue = methodLiteral.getLiteralValue();
1874
- if (!isHttpRouteMethod(methodValue)) {
1875
- return fail(
1876
- "httpRoute",
1877
- sourceLocationFromNode(call, sourceFile),
1878
- `method must be one of GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS (got "${methodValue}")`,
1879
- );
1880
- }
1881
- const pathLiteral = arg
1882
- .getProperty("path")
1883
- ?.asKind(SyntaxKind.PropertyAssignment)
1884
- ?.getInitializer()
1885
- ?.asKind(SyntaxKind.StringLiteral);
1886
- if (!pathLiteral) {
1887
- return fail(
1888
- "httpRoute",
1889
- sourceLocationFromNode(call, sourceFile),
1890
- "path must be a string literal",
1891
- );
1892
- }
1893
- const handlerInit = arg
1894
- .getProperty("handler")
1895
- ?.asKind(SyntaxKind.PropertyAssignment)
1896
- ?.getInitializer();
1897
- if (!handlerInit) {
1898
- return fail(
1899
- "httpRoute",
1900
- sourceLocationFromNode(call, sourceFile),
1901
- "missing `handler` property",
1902
- );
1903
- }
1904
- const fn = findFunctionLiteral(handlerInit);
1905
- if (!fn) {
1906
- return fail(
1907
- "httpRoute",
1908
- sourceLocationFromNode(call, sourceFile),
1909
- "handler must be an inline arrow function or function expression",
1910
- );
1911
- }
1912
- const anonymous = readBooleanProperty(arg, "anonymous");
1913
- return ok({
1914
- kind: "httpRoute",
1915
- source: sourceLocationFromNode(call, sourceFile),
1916
- method: methodValue,
1917
- path: pathLiteral.getLiteralValue(),
1918
- handlerBody: sourceLocationFromNode(fn, sourceFile),
1919
- ...(anonymous === true && { anonymous: true }),
1920
- });
1921
- }
1922
-
1923
- export function extractDefineEvent(
1924
- call: CallExpression,
1925
- sourceFile: SourceFile,
1926
- ): ExtractOutput<DefineEventPattern> {
1927
- const args = call.getArguments();
1928
- const first = args[0];
1929
- if (!first) {
1930
- return fail(
1931
- "defineEvent",
1932
- sourceLocationFromNode(call, sourceFile),
1933
- "expected at least one argument",
1934
- );
1935
- }
1936
-
1937
- // Object-Form: r.defineEvent({ name, schema, version? })
1938
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
1939
- if (obj && args.length === 1) {
1940
- const nameInit = obj
1941
- .getProperty("name")
1942
- ?.asKind(SyntaxKind.PropertyAssignment)
1943
- ?.getInitializer()
1944
- ?.asKind(SyntaxKind.StringLiteral);
1945
- if (!nameInit) {
1946
- return fail(
1947
- "defineEvent",
1948
- sourceLocationFromNode(call, sourceFile),
1949
- "object form requires a string-literal `name` property",
1950
- );
1951
- }
1952
- const schemaInit = obj
1953
- .getProperty("schema")
1954
- ?.asKind(SyntaxKind.PropertyAssignment)
1955
- ?.getInitializer();
1956
- if (!schemaInit) {
1957
- return fail(
1958
- "defineEvent",
1959
- sourceLocationFromNode(call, sourceFile),
1960
- "object form requires a `schema` property",
1961
- );
1962
- }
1963
- let version: number | undefined;
1964
- const versionInit = obj
1965
- .getProperty("version")
1966
- ?.asKind(SyntaxKind.PropertyAssignment)
1967
- ?.getInitializer();
1968
- if (versionInit) {
1969
- const v = readDataLiteralNode(versionInit);
1970
- if (typeof v === "number") version = v;
1971
- }
1972
- return ok({
1973
- kind: "defineEvent",
1974
- source: sourceLocationFromNode(call, sourceFile),
1975
- eventName: nameInit.getLiteralValue(),
1976
- schemaSource: sourceLocationFromNode(schemaInit, sourceFile),
1977
- ...(version !== undefined && { version }),
1978
- });
1979
- }
1980
-
1981
- // Legacy positional: r.defineEvent(name, schema, options?)
1982
- const nameArg = first.asKind(SyntaxKind.StringLiteral);
1983
- if (!nameArg) {
1984
- return fail(
1985
- "defineEvent",
1986
- sourceLocationFromNode(call, sourceFile),
1987
- "first argument must be a string literal event name (or use the object form)",
1988
- );
1989
- }
1990
- const schemaArg = args[1];
1991
- if (!schemaArg) {
1992
- return fail(
1993
- "defineEvent",
1994
- sourceLocationFromNode(call, sourceFile),
1995
- "expected a Zod schema as second argument",
1996
- );
1997
- }
1998
- let version: number | undefined;
1999
- const optionsArg = args[2];
2000
- if (optionsArg) {
2001
- const options = readDataLiteralNode(optionsArg);
2002
- if (isPlainObject(options) && typeof options["version"] === "number") {
2003
- version = options["version"];
2004
- }
2005
- }
2006
- return ok({
2007
- kind: "defineEvent",
2008
- source: sourceLocationFromNode(call, sourceFile),
2009
- eventName: nameArg.getLiteralValue(),
2010
- schemaSource: sourceLocationFromNode(schemaArg, sourceFile),
2011
- ...(version !== undefined && { version }),
2012
- });
2013
- }
2014
-
2015
- export function extractEventMigration(
2016
- call: CallExpression,
2017
- sourceFile: SourceFile,
2018
- ): ExtractOutput<EventMigrationPattern> {
2019
- const args = call.getArguments();
2020
- const first = args[0];
2021
- if (!first) {
2022
- return fail(
2023
- "eventMigration",
2024
- sourceLocationFromNode(call, sourceFile),
2025
- "expected at least one argument",
2026
- );
2027
- }
2028
-
2029
- // Object-Form: r.eventMigration({ event, fromVersion, toVersion, transform })
2030
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
2031
- if (obj && args.length === 1) {
2032
- const eventInit = obj
2033
- .getProperty("event")
2034
- ?.asKind(SyntaxKind.PropertyAssignment)
2035
- ?.getInitializer()
2036
- ?.asKind(SyntaxKind.StringLiteral);
2037
- if (!eventInit) {
2038
- return fail(
2039
- "eventMigration",
2040
- sourceLocationFromNode(call, sourceFile),
2041
- "object form requires a string-literal `event` property",
2042
- );
2043
- }
2044
- const fromInit = obj
2045
- .getProperty("fromVersion")
2046
- ?.asKind(SyntaxKind.PropertyAssignment)
2047
- ?.getInitializer();
2048
- const fromVersion = fromInit ? readDataLiteralNode(fromInit) : undefined;
2049
- if (typeof fromVersion !== "number") {
2050
- return fail(
2051
- "eventMigration",
2052
- sourceLocationFromNode(call, sourceFile),
2053
- "fromVersion must be a numeric literal",
2054
- );
2055
- }
2056
- const toInit = obj
2057
- .getProperty("toVersion")
2058
- ?.asKind(SyntaxKind.PropertyAssignment)
2059
- ?.getInitializer();
2060
- const toVersion = toInit ? readDataLiteralNode(toInit) : undefined;
2061
- if (typeof toVersion !== "number") {
2062
- return fail(
2063
- "eventMigration",
2064
- sourceLocationFromNode(call, sourceFile),
2065
- "toVersion must be a numeric literal",
2066
- );
2067
- }
2068
- const transformInit = obj
2069
- .getProperty("transform")
2070
- ?.asKind(SyntaxKind.PropertyAssignment)
2071
- ?.getInitializer();
2072
- if (!transformInit) {
2073
- return fail(
2074
- "eventMigration",
2075
- sourceLocationFromNode(call, sourceFile),
2076
- "object form requires a `transform` property",
2077
- );
2078
- }
2079
- const fn = findFunctionLiteral(transformInit);
2080
- if (!fn) {
2081
- return fail(
2082
- "eventMigration",
2083
- sourceLocationFromNode(call, sourceFile),
2084
- "transform must be an inline arrow function or function expression",
2085
- );
2086
- }
2087
- return ok({
2088
- kind: "eventMigration",
2089
- source: sourceLocationFromNode(call, sourceFile),
2090
- eventName: eventInit.getLiteralValue(),
2091
- fromVersion,
2092
- toVersion,
2093
- transformBody: sourceLocationFromNode(fn, sourceFile),
2094
- });
2095
- }
2096
-
2097
- // Legacy positional: r.eventMigration(name, from, to, transform)
2098
- const nameArg = first.asKind(SyntaxKind.StringLiteral);
2099
- if (!nameArg) {
2100
- return fail(
2101
- "eventMigration",
2102
- sourceLocationFromNode(call, sourceFile),
2103
- "first argument must be a string literal event name (or use the object form)",
2104
- );
2105
- }
2106
- const fromArg = args[1];
2107
- const fromVersion = fromArg ? readDataLiteralNode(fromArg) : undefined;
2108
- if (typeof fromVersion !== "number") {
2109
- return fail(
2110
- "eventMigration",
2111
- sourceLocationFromNode(call, sourceFile),
2112
- "fromVersion must be a numeric literal",
2113
- );
2114
- }
2115
- const toArg = args[2];
2116
- const toVersion = toArg ? readDataLiteralNode(toArg) : undefined;
2117
- if (typeof toVersion !== "number") {
2118
- return fail(
2119
- "eventMigration",
2120
- sourceLocationFromNode(call, sourceFile),
2121
- "toVersion must be a numeric literal",
2122
- );
2123
- }
2124
- const transformArg = args[3];
2125
- if (!transformArg) {
2126
- return fail(
2127
- "eventMigration",
2128
- sourceLocationFromNode(call, sourceFile),
2129
- "expected a transform function as fourth argument",
2130
- );
2131
- }
2132
- const fn = findFunctionLiteral(transformArg);
2133
- if (!fn) {
2134
- return fail(
2135
- "eventMigration",
2136
- sourceLocationFromNode(call, sourceFile),
2137
- "transform must be an inline arrow function or function expression",
2138
- );
2139
- }
2140
- return ok({
2141
- kind: "eventMigration",
2142
- source: sourceLocationFromNode(call, sourceFile),
2143
- eventName: nameArg.getLiteralValue(),
2144
- fromVersion,
2145
- toVersion,
2146
- transformBody: sourceLocationFromNode(fn, sourceFile),
2147
- });
2148
- }
2149
-
2150
- export function extractNotification(
2151
- call: CallExpression,
2152
- sourceFile: SourceFile,
2153
- ): ExtractOutput<NotificationPattern> {
2154
- const args = call.getArguments();
2155
- const first = args[0];
2156
- if (!first) {
2157
- return fail(
2158
- "notification",
2159
- sourceLocationFromNode(call, sourceFile),
2160
- "expected at least one argument",
2161
- );
2162
- }
2163
-
2164
- // Two argument shapes accepted:
2165
- // (a) Legacy positional: r.notification("name", { trigger, recipient, data, templates? })
2166
- // (b) Canonical Object-Form: r.notification({ name, trigger, recipient, data, templates? })
2167
- // The body code below is shape-agnostic — `nameLiteral` carries the
2168
- // notification's name, `defObj` is the object literal that holds the
2169
- // trigger/recipient/data/templates.
2170
- let nameLiteral: ReturnType<typeof first.asKind<SyntaxKind.StringLiteral>>;
2171
- let defObj: ReturnType<typeof first.asKind<SyntaxKind.ObjectLiteralExpression>>;
2172
-
2173
- const firstObj = first.asKind(SyntaxKind.ObjectLiteralExpression);
2174
- if (firstObj && args.length === 1) {
2175
- // Object-Form
2176
- nameLiteral = firstObj
2177
- .getProperty("name")
2178
- ?.asKind(SyntaxKind.PropertyAssignment)
2179
- ?.getInitializer()
2180
- ?.asKind(SyntaxKind.StringLiteral);
2181
- if (!nameLiteral) {
2182
- return fail(
2183
- "notification",
2184
- sourceLocationFromNode(call, sourceFile),
2185
- "object form requires a string-literal `name` property",
2186
- );
2187
- }
2188
- defObj = firstObj;
2189
- } else {
2190
- // Legacy positional
2191
- nameLiteral = first.asKind(SyntaxKind.StringLiteral);
2192
- if (!nameLiteral) {
2193
- return fail(
2194
- "notification",
2195
- sourceLocationFromNode(call, sourceFile),
2196
- "first argument must be a string literal notification name (or use the object form)",
2197
- );
2198
- }
2199
- defObj = args[1]?.asKind(SyntaxKind.ObjectLiteralExpression);
2200
- if (!defObj) {
2201
- return fail(
2202
- "notification",
2203
- sourceLocationFromNode(call, sourceFile),
2204
- "second argument must be an inline definition object",
2205
- );
2206
- }
2207
- }
2208
- const nameArg = nameLiteral;
2209
- const triggerObj = defObj
2210
- .getProperty("trigger")
2211
- ?.asKind(SyntaxKind.PropertyAssignment)
2212
- ?.getInitializer()
2213
- ?.asKind(SyntaxKind.ObjectLiteralExpression);
2214
- if (!triggerObj) {
2215
- return fail(
2216
- "notification",
2217
- sourceLocationFromNode(call, sourceFile),
2218
- "missing or non-object `trigger` property",
2219
- );
2220
- }
2221
- const onInit = triggerObj
2222
- .getProperty("on")
2223
- ?.asKind(SyntaxKind.PropertyAssignment)
2224
- ?.getInitializer();
2225
- const onName = onInit ? readNameOrRef(onInit) : undefined;
2226
- if (!onName) {
2227
- return fail(
2228
- "notification",
2229
- sourceLocationFromNode(call, sourceFile),
2230
- "trigger.on must be a string literal or inline { name } object",
2231
- );
2232
- }
2233
- const recipientInit = defObj
2234
- .getProperty("recipient")
2235
- ?.asKind(SyntaxKind.PropertyAssignment)
2236
- ?.getInitializer();
2237
- const recipientFn = recipientInit ? findFunctionLiteral(recipientInit) : undefined;
2238
- if (!recipientFn) {
2239
- return fail(
2240
- "notification",
2241
- sourceLocationFromNode(call, sourceFile),
2242
- "recipient must be an inline arrow function or function expression",
2243
- );
2244
- }
2245
- const dataInit = defObj
2246
- .getProperty("data")
2247
- ?.asKind(SyntaxKind.PropertyAssignment)
2248
- ?.getInitializer();
2249
- const dataFn = dataInit ? findFunctionLiteral(dataInit) : undefined;
2250
- if (!dataFn) {
2251
- return fail(
2252
- "notification",
2253
- sourceLocationFromNode(call, sourceFile),
2254
- "data must be an inline arrow function or function expression",
2255
- );
2256
- }
2257
- let templates: Record<string, SourceLocation> | undefined;
2258
- const templatesObj = defObj
2259
- .getProperty("templates")
2260
- ?.asKind(SyntaxKind.PropertyAssignment)
2261
- ?.getInitializer()
2262
- ?.asKind(SyntaxKind.ObjectLiteralExpression);
2263
- if (templatesObj) {
2264
- templates = {};
2265
- for (const prop of templatesObj.getProperties()) {
2266
- const propAssign = prop.asKind(SyntaxKind.PropertyAssignment);
2267
- if (!propAssign) continue;
2268
- const init = propAssign.getInitializer();
2269
- if (!init) continue;
2270
- const tfn = findFunctionLiteral(init);
2271
- if (!tfn) continue;
2272
- templates[readPropertyKey(propAssign)] = sourceLocationFromNode(tfn, sourceFile);
2273
- }
2274
- }
2275
- return ok({
2276
- kind: "notification",
2277
- source: sourceLocationFromNode(call, sourceFile),
2278
- notificationName: nameArg.getLiteralValue(),
2279
- trigger: { on: onName },
2280
- recipientBody: sourceLocationFromNode(recipientFn, sourceFile),
2281
- dataBody: sourceLocationFromNode(dataFn, sourceFile),
2282
- ...(templates !== undefined && { templates }),
2283
- });
2284
- }
2285
-
2286
- // Read an `apply: { eventType: fn }` map from a projection-definition object.
2287
- function readApplyBodies(
2288
- defObj: ReturnType<Node["asKind"]>,
2289
- sourceFile: SourceFile,
2290
- ): Record<string, SourceLocation> | undefined {
2291
- if (!defObj) return undefined;
2292
- const obj = defObj.asKind?.(SyntaxKind.ObjectLiteralExpression);
2293
- if (!obj) return undefined;
2294
- const applyObj = obj
2295
- .getProperty("apply")
2296
- ?.asKind(SyntaxKind.PropertyAssignment)
2297
- ?.getInitializer()
2298
- ?.asKind(SyntaxKind.ObjectLiteralExpression);
2299
- if (!applyObj) return undefined;
2300
- const out: Record<string, SourceLocation> = {};
2301
- for (const prop of applyObj.getProperties()) {
2302
- const propAssign = prop.asKind(SyntaxKind.PropertyAssignment);
2303
- if (!propAssign) return undefined;
2304
- const init = propAssign.getInitializer();
2305
- if (!init) return undefined;
2306
- const fn = findFunctionLiteral(init);
2307
- if (!fn) return undefined;
2308
- out[readPropertyKey(propAssign)] = sourceLocationFromNode(fn, sourceFile);
2309
- }
2310
- return out;
2311
- }
2312
-
2313
- export function extractProjection(
2314
- call: CallExpression,
2315
- sourceFile: SourceFile,
2316
- ): ExtractOutput<ProjectionPattern> {
2317
- const arg = call.getArguments()[0]?.asKind(SyntaxKind.ObjectLiteralExpression);
2318
- if (!arg) {
2319
- return fail(
2320
- "projection",
2321
- sourceLocationFromNode(call, sourceFile),
2322
- "argument must be an inline ProjectionDefinition object",
2323
- );
2324
- }
2325
- const nameLit = arg
2326
- .getProperty("name")
2327
- ?.asKind(SyntaxKind.PropertyAssignment)
2328
- ?.getInitializer()
2329
- ?.asKind(SyntaxKind.StringLiteral);
2330
- if (!nameLit) {
2331
- return fail(
2332
- "projection",
2333
- sourceLocationFromNode(call, sourceFile),
2334
- "name must be a string literal",
2335
- );
2336
- }
2337
- const sourceInit = arg
2338
- .getProperty("source")
2339
- ?.asKind(SyntaxKind.PropertyAssignment)
2340
- ?.getInitializer();
2341
- if (!sourceInit) {
2342
- return fail(
2343
- "projection",
2344
- sourceLocationFromNode(call, sourceFile),
2345
- "missing `source` property",
2346
- );
2347
- }
2348
- const sourceEntity = readNameOrRefOrList(sourceInit);
2349
- if (!sourceEntity) {
2350
- return fail(
2351
- "projection",
2352
- sourceLocationFromNode(call, sourceFile),
2353
- "source must be a string literal or array of string literals",
2354
- );
2355
- }
2356
- const applyBodies = readApplyBodies(arg, sourceFile);
2357
- if (!applyBodies) {
2358
- return fail(
2359
- "projection",
2360
- sourceLocationFromNode(call, sourceFile),
2361
- "apply must be an inline object map of event-type → function",
2362
- );
2363
- }
2364
- return ok({
2365
- kind: "projection",
2366
- source: sourceLocationFromNode(call, sourceFile),
2367
- name: nameLit.getLiteralValue(),
2368
- sourceEntity,
2369
- applyBodies,
2370
- });
2371
- }
2372
-
2373
- export function extractMultiStreamProjection(
2374
- call: CallExpression,
2375
- sourceFile: SourceFile,
2376
- ): ExtractOutput<MultiStreamProjectionPattern> {
2377
- const arg = call.getArguments()[0]?.asKind(SyntaxKind.ObjectLiteralExpression);
2378
- if (!arg) {
2379
- return fail(
2380
- "multiStreamProjection",
2381
- sourceLocationFromNode(call, sourceFile),
2382
- "argument must be an inline MultiStreamProjectionDefinition object",
2383
- );
2384
- }
2385
- const nameLit = arg
2386
- .getProperty("name")
2387
- ?.asKind(SyntaxKind.PropertyAssignment)
2388
- ?.getInitializer()
2389
- ?.asKind(SyntaxKind.StringLiteral);
2390
- if (!nameLit) {
2391
- return fail(
2392
- "multiStreamProjection",
2393
- sourceLocationFromNode(call, sourceFile),
2394
- "name must be a string literal",
2395
- );
2396
- }
2397
- const applyBodies = readApplyBodies(arg, sourceFile);
2398
- if (!applyBodies) {
2399
- return fail(
2400
- "multiStreamProjection",
2401
- sourceLocationFromNode(call, sourceFile),
2402
- "apply must be an inline object map of event-type → function",
2403
- );
2404
- }
2405
- const errorModeInit = arg
2406
- .getProperty("errorMode")
2407
- ?.asKind(SyntaxKind.PropertyAssignment)
2408
- ?.getInitializer();
2409
- const errorMode = errorModeInit ? readDataLiteralNode(errorModeInit) : undefined;
2410
- const runInLit = arg
2411
- .getProperty("runIn")
2412
- ?.asKind(SyntaxKind.PropertyAssignment)
2413
- ?.getInitializer()
2414
- ?.asKind(SyntaxKind.StringLiteral);
2415
- const runIn = runInLit ? (runInLit.getLiteralValue() as RunIn) : undefined;
2416
- const deliveryLit = arg
2417
- .getProperty("delivery")
2418
- ?.asKind(SyntaxKind.PropertyAssignment)
2419
- ?.getInitializer()
2420
- ?.asKind(SyntaxKind.StringLiteral);
2421
- const delivery = deliveryLit
2422
- ? (deliveryLit.getLiteralValue() as "shared" | "per-instance")
2423
- : undefined;
2424
- return ok({
2425
- kind: "multiStreamProjection",
2426
- source: sourceLocationFromNode(call, sourceFile),
2427
- name: nameLit.getLiteralValue(),
2428
- applyBodies,
2429
- ...(isPlainObject(errorMode) && { errorMode: errorMode as MspErrorMode }),
2430
- ...(runIn !== undefined && { runIn }),
2431
- ...(delivery !== undefined && { delivery }),
2432
- });
2433
- }
2434
-
2435
- // Walk the screen definition and collect every closure-typed property
2436
- // as a JSON-path → SourceLocation entry. The Designer renders forms for
2437
- // the rest of the definition; the AI patcher knows it can replace the
2438
- // span at the listed paths without touching surrounding fields.
2439
- function collectScreenOpaqueProps(
2440
- node: Node,
2441
- path: string,
2442
- sourceFile: SourceFile,
2443
- out: Record<string, SourceLocation>,
2444
- ): void {
2445
- const fn = findFunctionLiteral(node);
2446
- if (fn) {
2447
- out[path] = sourceLocationFromNode(fn, sourceFile);
2448
- } else if (node.isKind(SyntaxKind.ObjectLiteralExpression)) {
2449
- for (const prop of node.getProperties()) {
2450
- const propAssign = prop.asKind(SyntaxKind.PropertyAssignment);
2451
- if (!propAssign) continue;
2452
- const init = propAssign.getInitializer();
2453
- if (!init) continue;
2454
- const key = readPropertyKey(propAssign);
2455
- const childPath = path ? `${path}.${key}` : key;
2456
- collectScreenOpaqueProps(init, childPath, sourceFile, out);
2457
- }
2458
- } else if (node.isKind(SyntaxKind.ArrayLiteralExpression)) {
2459
- node.getElements().forEach((el, idx) => {
2460
- collectScreenOpaqueProps(el, `${path}.${idx}`, sourceFile, out);
2461
- });
2462
- }
2463
- }
2464
-
2465
- // Walk a screen-definition object and produce a JSON view, replacing any
2466
- // closure-typed property with SCREEN_OPAQUE_MARKER. Identifiers and
2467
- // other non-readable nodes also become the marker so the static tree
2468
- // stays serialisable while pointing the Designer at opaqueProps for the
2469
- // real source span.
2470
- function readScreenStatic(node: Node): unknown {
2471
- if (findFunctionLiteral(node)) return SCREEN_OPAQUE_MARKER;
2472
- const obj = node.asKind(SyntaxKind.ObjectLiteralExpression);
2473
- if (obj) {
2474
- const out: Record<string, unknown> = {};
2475
- for (const prop of obj.getProperties()) {
2476
- const propAssign = prop.asKind(SyntaxKind.PropertyAssignment);
2477
- if (!propAssign) continue;
2478
- const init = propAssign.getInitializer();
2479
- if (!init) continue;
2480
- out[readPropertyKey(propAssign)] = readScreenStatic(init);
2481
- }
2482
- return out;
2483
- }
2484
- const arr = node.asKind(SyntaxKind.ArrayLiteralExpression);
2485
- if (arr) {
2486
- return arr.getElements().map(readScreenStatic);
2487
- }
2488
- const value = readDataLiteralNode(node);
2489
- if (value === undefined) return SCREEN_OPAQUE_MARKER;
2490
- return value;
2491
- }
2492
-
2493
- export function extractScreen(
2494
- call: CallExpression,
2495
- sourceFile: SourceFile,
2496
- ): ExtractOutput<ScreenPattern> {
2497
- const arg = call.getArguments()[0];
2498
- if (!arg) {
2499
- return fail(
2500
- "screen",
2501
- sourceLocationFromNode(call, sourceFile),
2502
- "expected a ScreenDefinition object as first argument",
2503
- );
2504
- }
2505
- const obj = arg.asKind(SyntaxKind.ObjectLiteralExpression);
2506
- if (!obj) {
2507
- return fail(
2508
- "screen",
2509
- sourceLocationFromNode(call, sourceFile),
2510
- "argument must be an inline object literal",
2511
- );
2512
- }
2513
- const opaqueProps: Record<string, SourceLocation> = {};
2514
- collectScreenOpaqueProps(obj, "", sourceFile, opaqueProps);
2515
- const definition = readScreenStatic(obj);
2516
- if (!isPlainObject(definition)) {
2517
- return fail(
2518
- "screen",
2519
- sourceLocationFromNode(call, sourceFile),
2520
- "definition could not be read structurally",
2521
- );
2522
- }
2523
- return ok({
2524
- kind: "screen",
2525
- source: sourceLocationFromNode(call, sourceFile),
2526
- definition: definition as ScreenDefinition,
2527
- opaqueProps: opaqueProps as OpaquePropMap,
2528
- });
2529
- }
2530
-
2531
- // =============================================================================
2532
- // Round 5 — opaque patterns (no static header beyond a name)
2533
- // =============================================================================
2534
-
2535
- export function extractExtendsRegistrar(
2536
- call: CallExpression,
2537
- sourceFile: SourceFile,
2538
- ): ExtractOutput<ExtendsRegistrarPattern> {
2539
- const args = call.getArguments();
2540
- const nameArg = args[0]?.asKind(SyntaxKind.StringLiteral);
2541
- if (!nameArg) {
2542
- return fail(
2543
- "extendsRegistrar",
2544
- sourceLocationFromNode(call, sourceFile),
2545
- "first argument must be a string literal extension name",
2546
- );
2547
- }
2548
- const defArg = args[1];
2549
- if (!defArg) {
2550
- return fail(
2551
- "extendsRegistrar",
2552
- sourceLocationFromNode(call, sourceFile),
2553
- "expected a definition argument",
2554
- );
2555
- }
2556
- return ok({
2557
- kind: "extendsRegistrar",
2558
- source: sourceLocationFromNode(call, sourceFile),
2559
- extensionName: nameArg.getLiteralValue(),
2560
- defBody: sourceLocationFromNode(defArg, sourceFile),
2561
- });
2562
- }