@cosmicdrift/kumiko-framework 0.155.0 → 0.156.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 (74) hide show
  1. package/README.md +9 -38
  2. package/package.json +2 -2
  3. package/src/__tests__/entity-permalink-open.integration.test.ts +94 -0
  4. package/src/__tests__/raw-table.integration.test.ts +17 -40
  5. package/src/crypto/__tests__/pii-field-encryption.test.ts +77 -0
  6. package/src/crypto/index.ts +1 -0
  7. package/src/crypto/pii-field-encryption.ts +19 -0
  8. package/src/db/__tests__/assert-no-unreachable-live-rows.integration.test.ts +27 -1
  9. package/src/db/__tests__/blind-index.integration.test.ts +16 -0
  10. package/src/db/__tests__/collect-table-metas.test.ts +6 -6
  11. package/src/db/__tests__/feature-table-sources.test.ts +7 -7
  12. package/src/db/__tests__/migrate-generator.test.ts +21 -0
  13. package/src/db/__tests__/number-field-fractional.integration.test.ts +1 -1
  14. package/src/db/__tests__/tenant-db-where-merge.test.ts +39 -0
  15. package/src/db/collect-table-metas.ts +6 -7
  16. package/src/db/entity-table-meta.ts +1 -1
  17. package/src/db/feature-table-sources.ts +5 -11
  18. package/src/db/migrate-generator.ts +39 -6
  19. package/src/db/queries/shadow-swap.ts +85 -3
  20. package/src/db/tenant-db.ts +8 -0
  21. package/src/engine/__tests__/boot-validator-pii-retention.test.ts +102 -0
  22. package/src/engine/__tests__/build-config-feature-schema.test.ts +16 -0
  23. package/src/engine/__tests__/feature-crud-shorthand.test.ts +28 -0
  24. package/src/engine/__tests__/field-access.test.ts +23 -1
  25. package/src/engine/__tests__/raw-table.test.ts +134 -84
  26. package/src/engine/__tests__/registry.test.ts +40 -0
  27. package/src/engine/boot-validator/__tests__/config-deps.test.ts +52 -2
  28. package/src/engine/boot-validator/action-wiring.ts +2 -2
  29. package/src/engine/boot-validator/config-deps.ts +35 -0
  30. package/src/engine/boot-validator/index.ts +2 -0
  31. package/src/engine/boot-validator/pii-retention.ts +35 -3
  32. package/src/engine/build-config-feature-schema.ts +3 -3
  33. package/src/engine/config-helpers.ts +2 -0
  34. package/src/engine/define-feature.ts +2 -6
  35. package/src/engine/entity-handlers.ts +2 -4
  36. package/src/engine/feature-ast/__tests__/parse-real-features.test.ts +1 -1
  37. package/src/engine/feature-ast/__tests__/parse.test.ts +170 -0
  38. package/src/engine/feature-ast/__tests__/patch.test.ts +31 -0
  39. package/src/engine/feature-ast/__tests__/render-roundtrip.test.ts +6 -1
  40. package/src/engine/feature-ast/extractors/events.ts +322 -0
  41. package/src/engine/feature-ast/extractors/handlers.ts +222 -0
  42. package/src/engine/feature-ast/extractors/hooks.ts +243 -0
  43. package/src/engine/feature-ast/extractors/index.ts +32 -24
  44. package/src/engine/feature-ast/extractors/jobs-routes.ts +221 -0
  45. package/src/engine/feature-ast/extractors/projections-screens.ts +269 -0
  46. package/src/engine/feature-ast/extractors/round5.ts +3 -3
  47. package/src/engine/feature-ast/parse.ts +3 -3
  48. package/src/engine/feature-ast/render.ts +16 -11
  49. package/src/engine/feature-builder-state.ts +0 -3
  50. package/src/engine/feature-config-events-jobs.ts +3 -6
  51. package/src/engine/feature-entity-handlers.ts +9 -1
  52. package/src/engine/feature-ui-extensions.ts +10 -40
  53. package/src/engine/field-access.ts +13 -2
  54. package/src/engine/object-form.ts +15 -0
  55. package/src/engine/registry-facade.ts +4 -0
  56. package/src/engine/registry-ingest.ts +19 -29
  57. package/src/engine/registry-state.ts +1 -7
  58. package/src/engine/registry-validate.ts +5 -28
  59. package/src/engine/registry.ts +0 -2
  60. package/src/engine/types/config.ts +7 -0
  61. package/src/engine/types/feature.ts +40 -68
  62. package/src/engine/types/fields.ts +6 -0
  63. package/src/engine/types/index.ts +0 -3
  64. package/src/es-ops/README.md +1 -1
  65. package/src/es-ops/__tests__/runner.integration.test.ts +74 -0
  66. package/src/es-ops/context.ts +4 -2
  67. package/src/es-ops/types.ts +8 -1
  68. package/src/pipeline/msp-rebuild.ts +4 -0
  69. package/src/pipeline/projection-rebuild.ts +35 -0
  70. package/src/search/__tests__/meilisearch-adapter.integration.test.ts +8 -1
  71. package/src/stack/test-stack.ts +17 -2
  72. package/src/ui-types/index.ts +1 -0
  73. package/src/engine/__tests__/unmanaged-table.test.ts +0 -172
  74. package/src/engine/feature-ast/extractors/round4.ts +0 -1227
@@ -1,1227 +0,0 @@
1
- import type { CallExpression, Node, SourceFile } from "ts-morph";
2
- import { SyntaxKind } from "ts-morph";
3
- import type { LifecycleHookType } from "../../constants";
4
- import type { JobDefinition, RunIn } from "../../types/config";
5
- import type { AccessRule, RateLimitOption } from "../../types/handlers";
6
- import type { HookPhase } from "../../types/hooks";
7
- import type { HttpRouteMethod } from "../../types/http-route";
8
- import type { MspErrorMode } from "../../types/projection";
9
- import type { ScreenDefinition } from "../../types/screen";
10
- import type {
11
- AuthClaimsPattern,
12
- DefineEventPattern,
13
- HookPattern,
14
- HttpRoutePattern,
15
- JobPattern,
16
- MultiStreamProjectionPattern,
17
- NotificationPattern,
18
- OpaquePropMap,
19
- ProjectionPattern,
20
- QueryHandlerPattern,
21
- ScreenPattern,
22
- WriteHandlerPattern,
23
- } from "../patterns";
24
- import { SCREEN_OPAQUE_MARKER } from "../patterns";
25
- import type { SourceLocation } from "../source-location";
26
- import { sourceLocationFromNode } from "../source-location";
27
- import {
28
- type ExtractOutput,
29
- fail,
30
- findFunctionLiteral,
31
- isPlainObject,
32
- isRawRefSentinel,
33
- ok,
34
- readBooleanProperty,
35
- readDataLiteralNode,
36
- readNameOrRef,
37
- readNameOrRefOrList,
38
- readPropertyKey,
39
- } from "./shared";
40
-
41
- export function isHookType(value: string): value is LifecycleHookType | "validation" {
42
- return (
43
- value === "preSave" ||
44
- value === "postSave" ||
45
- value === "preDelete" ||
46
- value === "postDelete" ||
47
- value === "preQuery" ||
48
- value === "validation"
49
- );
50
- }
51
-
52
- export function isHttpRouteMethod(value: string): value is HttpRouteMethod {
53
- return (
54
- value === "GET" ||
55
- value === "POST" ||
56
- value === "PUT" ||
57
- value === "PATCH" ||
58
- value === "DELETE" ||
59
- value === "HEAD" ||
60
- value === "OPTIONS"
61
- );
62
- }
63
-
64
- export function readOptionalPhase(node: Node | undefined): HookPhase | undefined {
65
- if (!node) return undefined;
66
- const obj = readDataLiteralNode(node);
67
- if (!isPlainObject(obj)) return undefined;
68
- const phase = obj["phase"];
69
- if (phase === "inTransaction" || phase === "afterCommit") return phase as HookPhase;
70
- return undefined;
71
- }
72
-
73
- export function readOptionalAccessRule(value: unknown): AccessRule | undefined {
74
- if (!isPlainObject(value)) return undefined;
75
- if (Array.isArray(value["roles"]) && value["roles"].every((r) => typeof r === "string")) {
76
- return { roles: value["roles"] as readonly string[] };
77
- }
78
- if (value["openToAll"] === true) {
79
- return { openToAll: true };
80
- }
81
- return undefined;
82
- }
83
-
84
- export function readOptionalRateLimit(value: unknown): RateLimitOption | undefined {
85
- if (!isPlainObject(value)) return undefined;
86
- if (typeof value["per"] !== "string") return undefined;
87
- if (typeof value["limit"] !== "number") return undefined;
88
- if (typeof value["windowSeconds"] !== "number") return undefined;
89
- return value as unknown as RateLimitOption;
90
- }
91
-
92
- // r.hook's target: a NameOrRef, a list of them, or an entity-wide
93
- // `{ allOf: entityRef }` (replaces the old r.entityHook(type, entity, fn)).
94
- // Checked first so a malformed `{ allOf }` doesn't silently fall through
95
- // to being read as some other object shape.
96
- function readHookTarget(
97
- node: Node,
98
- ): string | readonly string[] | { readonly allOf: string } | undefined {
99
- const obj = node.asKind(SyntaxKind.ObjectLiteralExpression);
100
- if (obj) {
101
- const allOfProp = obj.getProperty("allOf")?.asKind(SyntaxKind.PropertyAssignment);
102
- if (allOfProp) {
103
- const initializer = allOfProp.getInitializer();
104
- const entityName = initializer && readNameOrRef(initializer);
105
- return entityName ? { allOf: entityName } : undefined;
106
- }
107
- }
108
- return readNameOrRefOrList(node);
109
- }
110
-
111
- export function extractHook(
112
- call: CallExpression,
113
- sourceFile: SourceFile,
114
- ): ExtractOutput<HookPattern> {
115
- const args = call.getArguments();
116
- const first = args[0];
117
- if (!first) {
118
- return fail("hook", sourceLocationFromNode(call, sourceFile), "expected at least one argument");
119
- }
120
-
121
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
122
- if (obj && args.length === 1) {
123
- const typeInit = obj
124
- .getProperty("type")
125
- ?.asKind(SyntaxKind.PropertyAssignment)
126
- ?.getInitializer()
127
- ?.asKind(SyntaxKind.StringLiteral);
128
- if (!typeInit) {
129
- return fail(
130
- "hook",
131
- sourceLocationFromNode(call, sourceFile),
132
- "object form requires a string-literal `type` property",
133
- );
134
- }
135
- const hookType = typeInit.getLiteralValue();
136
- if (!isHookType(hookType)) {
137
- return fail(
138
- "hook",
139
- sourceLocationFromNode(call, sourceFile),
140
- `hook type "${hookType}" is not one of the lifecycle types or "validation"`,
141
- );
142
- }
143
- const targetInit = obj
144
- .getProperty("target")
145
- ?.asKind(SyntaxKind.PropertyAssignment)
146
- ?.getInitializer();
147
- if (!targetInit) {
148
- return fail(
149
- "hook",
150
- sourceLocationFromNode(call, sourceFile),
151
- "object form requires a `target` property",
152
- );
153
- }
154
- const target = readHookTarget(targetInit);
155
- if (!target) {
156
- return fail(
157
- "hook",
158
- sourceLocationFromNode(call, sourceFile),
159
- "target must be a string literal, an inline { name } object, or an array",
160
- );
161
- }
162
- const handlerInit = obj
163
- .getProperty("handler")
164
- ?.asKind(SyntaxKind.PropertyAssignment)
165
- ?.getInitializer();
166
- if (!handlerInit) {
167
- return fail(
168
- "hook",
169
- sourceLocationFromNode(call, sourceFile),
170
- "object form requires a `handler` property",
171
- );
172
- }
173
- const fn = findFunctionLiteral(handlerInit);
174
- if (!fn) {
175
- return fail(
176
- "hook",
177
- sourceLocationFromNode(call, sourceFile),
178
- "handler must be an inline arrow function or function expression",
179
- );
180
- }
181
- const phase = readOptionalPhase(obj);
182
- return ok({
183
- kind: "hook",
184
- source: sourceLocationFromNode(call, sourceFile),
185
- hookType,
186
- target,
187
- fnBody: sourceLocationFromNode(fn, sourceFile),
188
- ...(phase !== undefined && { phase }),
189
- });
190
- }
191
-
192
- const typeArg = first.asKind(SyntaxKind.StringLiteral);
193
- if (!typeArg) {
194
- return fail(
195
- "hook",
196
- sourceLocationFromNode(call, sourceFile),
197
- "first argument must be a string literal hook type (or use the object form)",
198
- );
199
- }
200
- const hookType = typeArg.getLiteralValue();
201
- if (!isHookType(hookType)) {
202
- return fail(
203
- "hook",
204
- sourceLocationFromNode(call, sourceFile),
205
- `hook type "${hookType}" is not one of the lifecycle types or "validation"`,
206
- );
207
- }
208
- const targetArg = args[1];
209
- if (!targetArg) {
210
- return fail(
211
- "hook",
212
- sourceLocationFromNode(call, sourceFile),
213
- "expected a target (NameOrRef or array) as second argument",
214
- );
215
- }
216
- const target = readHookTarget(targetArg);
217
- if (!target) {
218
- return fail(
219
- "hook",
220
- sourceLocationFromNode(call, sourceFile),
221
- "target must be a string literal, an inline { name } object, or an array",
222
- );
223
- }
224
- const fnArg = args[2];
225
- if (!fnArg) {
226
- return fail(
227
- "hook",
228
- sourceLocationFromNode(call, sourceFile),
229
- "expected a hook function as third argument",
230
- );
231
- }
232
- const fn = findFunctionLiteral(fnArg);
233
- if (!fn) {
234
- return fail(
235
- "hook",
236
- sourceLocationFromNode(call, sourceFile),
237
- "third argument must be an inline arrow function or function expression",
238
- );
239
- }
240
- const phase = readOptionalPhase(args[3]);
241
- return ok({
242
- kind: "hook",
243
- source: sourceLocationFromNode(call, sourceFile),
244
- hookType,
245
- target,
246
- fnBody: sourceLocationFromNode(fn, sourceFile),
247
- ...(phase !== undefined && { phase }),
248
- });
249
- }
250
-
251
- // guard:dup-ok — intentionale Parallele zu extractTree (round6); verschiedene Feature-AST-Extraktoren by design
252
- export function extractAuthClaims(
253
- call: CallExpression,
254
- sourceFile: SourceFile,
255
- ): ExtractOutput<AuthClaimsPattern> {
256
- const arg = call.getArguments()[0];
257
- if (!arg) {
258
- return fail(
259
- "authClaims",
260
- sourceLocationFromNode(call, sourceFile),
261
- "expected a function as first argument",
262
- );
263
- }
264
- const fn = findFunctionLiteral(arg);
265
- if (!fn) {
266
- return fail(
267
- "authClaims",
268
- sourceLocationFromNode(call, sourceFile),
269
- "first argument must be an inline arrow function or function expression",
270
- );
271
- }
272
- return ok({
273
- kind: "authClaims",
274
- source: sourceLocationFromNode(call, sourceFile),
275
- fnBody: sourceLocationFromNode(fn, sourceFile),
276
- });
277
- }
278
-
279
- export type ParsedHandlerCall = {
280
- readonly source: SourceLocation;
281
- readonly handlerName?: string;
282
- readonly schemaSource?: SourceLocation;
283
- readonly handlerBody?: SourceLocation;
284
- readonly access?: AccessRule;
285
- readonly rateLimit?: RateLimitOption;
286
- readonly unsafeSkipTransitionGuard?: boolean;
287
- };
288
-
289
- /**
290
- * Resolves an argument standing in for a handler-call's object-form body:
291
- * either it's already an object literal, or a bare identifier declared
292
- * locally (same file only) with one as its initializer. Anything else
293
- * (imported binding, factory call, ...) is not resolvable here.
294
- */
295
- function resolveObjectLiteralArg(node: Node) {
296
- const direct = node.asKind(SyntaxKind.ObjectLiteralExpression);
297
- if (direct) return direct;
298
- const identifier = node.asKind(SyntaxKind.Identifier);
299
- if (!identifier) return undefined;
300
- const varDecl = node.getSourceFile().getVariableDeclaration(identifier.getText());
301
- return varDecl?.getInitializer()?.asKind(SyntaxKind.ObjectLiteralExpression);
302
- }
303
-
304
- export function parseHandlerCall(
305
- call: CallExpression,
306
- sourceFile: SourceFile,
307
- methodName: "writeHandler" | "queryHandler",
308
- ): ExtractOutput<ParsedHandlerCall> {
309
- const args = call.getArguments();
310
- const first = args[0];
311
- if (!first) {
312
- return fail(
313
- methodName,
314
- sourceLocationFromNode(call, sourceFile),
315
- "expected at least one argument",
316
- );
317
- }
318
-
319
- const obj = args.length === 1 ? resolveObjectLiteralArg(first) : undefined;
320
- if (obj) {
321
- const nameLiteral = obj
322
- .getProperty("name")
323
- ?.asKind(SyntaxKind.PropertyAssignment)
324
- ?.getInitializer()
325
- ?.asKind(SyntaxKind.StringLiteral);
326
- if (!nameLiteral) {
327
- return fail(
328
- methodName,
329
- sourceLocationFromNode(call, sourceFile),
330
- "object form requires a string-literal `name` property",
331
- );
332
- }
333
- const schemaInit = obj
334
- .getProperty("schema")
335
- ?.asKind(SyntaxKind.PropertyAssignment)
336
- ?.getInitializer();
337
- if (!schemaInit) {
338
- return fail(
339
- methodName,
340
- sourceLocationFromNode(call, sourceFile),
341
- "object form requires a `schema` property",
342
- );
343
- }
344
- const handlerInit = obj
345
- .getProperty("handler")
346
- ?.asKind(SyntaxKind.PropertyAssignment)
347
- ?.getInitializer();
348
- if (!handlerInit) {
349
- return fail(
350
- methodName,
351
- sourceLocationFromNode(call, sourceFile),
352
- "object form requires a `handler` property",
353
- );
354
- }
355
- const fn = findFunctionLiteral(handlerInit);
356
- if (!fn) {
357
- return fail(
358
- methodName,
359
- sourceLocationFromNode(call, sourceFile),
360
- "handler must be an inline arrow function or function expression",
361
- );
362
- }
363
- const accessInit = obj
364
- .getProperty("access")
365
- ?.asKind(SyntaxKind.PropertyAssignment)
366
- ?.getInitializer();
367
- const access = accessInit ? readOptionalAccessRule(readDataLiteralNode(accessInit)) : undefined;
368
- const rateLimitInit = obj
369
- .getProperty("rateLimit")
370
- ?.asKind(SyntaxKind.PropertyAssignment)
371
- ?.getInitializer();
372
- const rateLimit = rateLimitInit
373
- ? readOptionalRateLimit(readDataLiteralNode(rateLimitInit))
374
- : undefined;
375
- const skip = readBooleanProperty(obj, "unsafeSkipTransitionGuard");
376
- return ok({
377
- source: sourceLocationFromNode(call, sourceFile),
378
- handlerName: nameLiteral.getLiteralValue(),
379
- schemaSource: sourceLocationFromNode(schemaInit, sourceFile),
380
- handlerBody: sourceLocationFromNode(fn, sourceFile),
381
- ...(access !== undefined && { access }),
382
- ...(rateLimit !== undefined && { rateLimit }),
383
- ...(skip === true && { unsafeSkipTransitionGuard: true }),
384
- });
385
- }
386
-
387
- // A single reference standing in for the whole handler argument set
388
- // (`r.writeHandler(eventCreateHandler)`, `r.queryHandler(someQuery())`)
389
- // that resolveObjectLiteralArg above couldn't turn into an object literal
390
- // (imported binding, factory call, ...). Keep it recognised as this kind
391
- // instead of ParseErroring — see #1007. Opaque: renderWriteHandler/
392
- // renderQueryHandler re-emit `source.raw` verbatim when handlerName is
393
- // undefined.
394
- if (args.length === 1 && isRawRefSentinel(readDataLiteralNode(first))) {
395
- return ok({ source: sourceLocationFromNode(call, sourceFile) });
396
- }
397
- const nameLiteral = first.asKind(SyntaxKind.StringLiteral);
398
- if (!nameLiteral) {
399
- return fail(
400
- methodName,
401
- sourceLocationFromNode(call, sourceFile),
402
- "first argument must be a string literal handler name (or use the object form)",
403
- );
404
- }
405
- const schemaArg = args[1];
406
- if (!schemaArg) {
407
- return fail(
408
- methodName,
409
- sourceLocationFromNode(call, sourceFile),
410
- "expected a Zod schema as second argument",
411
- );
412
- }
413
- const handlerArg = args[2];
414
- if (!handlerArg) {
415
- return fail(
416
- methodName,
417
- sourceLocationFromNode(call, sourceFile),
418
- "expected a handler function as third argument",
419
- );
420
- }
421
- const fn = findFunctionLiteral(handlerArg);
422
- if (!fn) {
423
- return fail(
424
- methodName,
425
- sourceLocationFromNode(call, sourceFile),
426
- "third argument must be an inline arrow function or function expression",
427
- );
428
- }
429
- const optionsArg = args[3];
430
- let access: AccessRule | undefined;
431
- let rateLimit: RateLimitOption | undefined;
432
- if (optionsArg) {
433
- const options = readDataLiteralNode(optionsArg);
434
- if (isPlainObject(options)) {
435
- access = readOptionalAccessRule(options["access"]);
436
- rateLimit = readOptionalRateLimit(options["rateLimit"]);
437
- }
438
- }
439
- return ok({
440
- source: sourceLocationFromNode(call, sourceFile),
441
- handlerName: nameLiteral.getLiteralValue(),
442
- schemaSource: sourceLocationFromNode(schemaArg, sourceFile),
443
- handlerBody: sourceLocationFromNode(fn, sourceFile),
444
- ...(access !== undefined && { access }),
445
- ...(rateLimit !== undefined && { rateLimit }),
446
- });
447
- }
448
-
449
- export function extractWriteHandler(
450
- call: CallExpression,
451
- sourceFile: SourceFile,
452
- ): ExtractOutput<WriteHandlerPattern> {
453
- const parsed = parseHandlerCall(call, sourceFile, "writeHandler");
454
- if (parsed.kind === "error") return parsed;
455
- return ok({
456
- kind: "writeHandler",
457
- source: parsed.pattern.source,
458
- handlerName: parsed.pattern.handlerName,
459
- schemaSource: parsed.pattern.schemaSource,
460
- handlerBody: parsed.pattern.handlerBody,
461
- ...(parsed.pattern.access !== undefined && { access: parsed.pattern.access }),
462
- ...(parsed.pattern.rateLimit !== undefined && { rateLimit: parsed.pattern.rateLimit }),
463
- ...(parsed.pattern.unsafeSkipTransitionGuard === true && { unsafeSkipTransitionGuard: true }),
464
- });
465
- }
466
-
467
- export function extractQueryHandler(
468
- call: CallExpression,
469
- sourceFile: SourceFile,
470
- ): ExtractOutput<QueryHandlerPattern> {
471
- const parsed = parseHandlerCall(call, sourceFile, "queryHandler");
472
- if (parsed.kind === "error") return parsed;
473
- return ok({
474
- kind: "queryHandler",
475
- source: parsed.pattern.source,
476
- handlerName: parsed.pattern.handlerName,
477
- schemaSource: parsed.pattern.schemaSource,
478
- handlerBody: parsed.pattern.handlerBody,
479
- ...(parsed.pattern.access !== undefined && { access: parsed.pattern.access }),
480
- ...(parsed.pattern.rateLimit !== undefined && { rateLimit: parsed.pattern.rateLimit }),
481
- });
482
- }
483
-
484
- export function extractJob(
485
- call: CallExpression,
486
- sourceFile: SourceFile,
487
- ): ExtractOutput<JobPattern> {
488
- const args = call.getArguments();
489
- const first = args[0];
490
- if (!first) {
491
- return fail("job", sourceLocationFromNode(call, sourceFile), "expected at least one argument");
492
- }
493
-
494
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
495
- if (obj && args.length === 1) {
496
- const nameInit = obj
497
- .getProperty("name")
498
- ?.asKind(SyntaxKind.PropertyAssignment)
499
- ?.getInitializer()
500
- ?.asKind(SyntaxKind.StringLiteral);
501
- if (!nameInit) {
502
- return fail(
503
- "job",
504
- sourceLocationFromNode(call, sourceFile),
505
- "object form requires a string-literal `name` property",
506
- );
507
- }
508
- const handlerInit = obj
509
- .getProperty("handler")
510
- ?.asKind(SyntaxKind.PropertyAssignment)
511
- ?.getInitializer();
512
- if (!handlerInit) {
513
- return fail(
514
- "job",
515
- sourceLocationFromNode(call, sourceFile),
516
- "object form requires a `handler` property",
517
- );
518
- }
519
- const fn = findFunctionLiteral(handlerInit);
520
- if (!fn) {
521
- return fail(
522
- "job",
523
- sourceLocationFromNode(call, sourceFile),
524
- "handler must be an inline arrow function or function expression",
525
- );
526
- }
527
- const optionsBag: Record<string, unknown> = {};
528
- for (const prop of obj.getProperties()) {
529
- const propAssign = prop.asKind(SyntaxKind.PropertyAssignment);
530
- if (!propAssign) continue;
531
- const key = readPropertyKey(propAssign);
532
- if (key === "name" || key === "handler") continue;
533
- const init = propAssign.getInitializer();
534
- if (!init) continue;
535
- const value = readDataLiteralNode(init);
536
- if (value === undefined) {
537
- return fail(
538
- "job",
539
- sourceLocationFromNode(call, sourceFile),
540
- `option "${key}" could not be read as a plain value`,
541
- );
542
- }
543
- optionsBag[key] = value;
544
- }
545
- return ok({
546
- kind: "job",
547
- source: sourceLocationFromNode(call, sourceFile),
548
- jobName: nameInit.getLiteralValue(),
549
- options: optionsBag as Omit<JobDefinition, "name" | "handler">,
550
- handlerBody: sourceLocationFromNode(fn, sourceFile),
551
- });
552
- }
553
-
554
- const nameArg = first.asKind(SyntaxKind.StringLiteral);
555
- if (!nameArg) {
556
- return fail(
557
- "job",
558
- sourceLocationFromNode(call, sourceFile),
559
- "first argument must be a string literal job name (or use the object form)",
560
- );
561
- }
562
- const optionsArg = args[1];
563
- if (!optionsArg) {
564
- return fail(
565
- "job",
566
- sourceLocationFromNode(call, sourceFile),
567
- "expected an options object as second argument",
568
- );
569
- }
570
- const options = readDataLiteralNode(optionsArg);
571
- if (!isPlainObject(options)) {
572
- return fail(
573
- "job",
574
- sourceLocationFromNode(call, sourceFile),
575
- "options could not be read as a plain object",
576
- );
577
- }
578
- const handlerArg = args[2];
579
- if (!handlerArg) {
580
- return fail(
581
- "job",
582
- sourceLocationFromNode(call, sourceFile),
583
- "expected a handler function as third argument",
584
- );
585
- }
586
- const fn = findFunctionLiteral(handlerArg);
587
- if (!fn) {
588
- return fail(
589
- "job",
590
- sourceLocationFromNode(call, sourceFile),
591
- "third argument must be an inline arrow function or function expression",
592
- );
593
- }
594
- return ok({
595
- kind: "job",
596
- source: sourceLocationFromNode(call, sourceFile),
597
- jobName: nameArg.getLiteralValue(),
598
- options: options as Omit<JobDefinition, "name" | "handler">,
599
- handlerBody: sourceLocationFromNode(fn, sourceFile),
600
- });
601
- }
602
-
603
- export function extractHttpRoute(
604
- call: CallExpression,
605
- sourceFile: SourceFile,
606
- ): ExtractOutput<HttpRoutePattern> {
607
- const arg = call.getArguments()[0]?.asKind(SyntaxKind.ObjectLiteralExpression);
608
- if (!arg) {
609
- return fail(
610
- "httpRoute",
611
- sourceLocationFromNode(call, sourceFile),
612
- "argument must be an inline HttpRouteDefinition object",
613
- );
614
- }
615
- const methodLiteral = arg
616
- .getProperty("method")
617
- ?.asKind(SyntaxKind.PropertyAssignment)
618
- ?.getInitializer()
619
- ?.asKind(SyntaxKind.StringLiteral);
620
- if (!methodLiteral) {
621
- return fail(
622
- "httpRoute",
623
- sourceLocationFromNode(call, sourceFile),
624
- "method must be a string literal",
625
- );
626
- }
627
- const methodValue = methodLiteral.getLiteralValue();
628
- if (!isHttpRouteMethod(methodValue)) {
629
- return fail(
630
- "httpRoute",
631
- sourceLocationFromNode(call, sourceFile),
632
- `method must be one of GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS (got "${methodValue}")`,
633
- );
634
- }
635
- const pathLiteral = arg
636
- .getProperty("path")
637
- ?.asKind(SyntaxKind.PropertyAssignment)
638
- ?.getInitializer()
639
- ?.asKind(SyntaxKind.StringLiteral);
640
- if (!pathLiteral) {
641
- return fail(
642
- "httpRoute",
643
- sourceLocationFromNode(call, sourceFile),
644
- "path must be a string literal",
645
- );
646
- }
647
- const handlerInit = arg
648
- .getProperty("handler")
649
- ?.asKind(SyntaxKind.PropertyAssignment)
650
- ?.getInitializer();
651
- if (!handlerInit) {
652
- return fail(
653
- "httpRoute",
654
- sourceLocationFromNode(call, sourceFile),
655
- "missing `handler` property",
656
- );
657
- }
658
- const fn = findFunctionLiteral(handlerInit);
659
- if (!fn) {
660
- return fail(
661
- "httpRoute",
662
- sourceLocationFromNode(call, sourceFile),
663
- "handler must be an inline arrow function or function expression",
664
- );
665
- }
666
- const anonymous = readBooleanProperty(arg, "anonymous");
667
- return ok({
668
- kind: "httpRoute",
669
- source: sourceLocationFromNode(call, sourceFile),
670
- method: methodValue,
671
- path: pathLiteral.getLiteralValue(),
672
- handlerBody: sourceLocationFromNode(fn, sourceFile),
673
- ...(anonymous === true && { anonymous: true }),
674
- });
675
- }
676
-
677
- // Reads defineEvent's `migrations` option — either the array-of-steps shape
678
- // used by hand-authored calls (`[{ fromVersion, toVersion, transform }]`,
679
- // toVersion is redundant on-disk since it is always fromVersion + 1) or the
680
- // keyed-object canonical shape the Designer renders (`{ "1": transform }`).
681
- // Keyed by fromVersion (as a string) → the transform closure's location.
682
- // Skips malformed entries rather than failing the whole defineEvent extract
683
- // — same "best-effort, degrade gracefully" posture as readDataLiteralNode.
684
- function extractEventMigrationsFromArray(
685
- arr: Node,
686
- sourceFile: SourceFile,
687
- ): Record<string, SourceLocation> {
688
- const result: Record<string, SourceLocation> = {};
689
- const arrLit = arr.asKindOrThrow(SyntaxKind.ArrayLiteralExpression);
690
- for (const el of arrLit.getElements()) {
691
- const obj = el.asKind(SyntaxKind.ObjectLiteralExpression);
692
- if (!obj) continue;
693
- const fromInit = obj
694
- .getProperty("fromVersion")
695
- ?.asKind(SyntaxKind.PropertyAssignment)
696
- ?.getInitializer();
697
- const fromVersion = fromInit ? readDataLiteralNode(fromInit) : undefined;
698
- if (typeof fromVersion !== "number") continue;
699
- const transformInit = obj
700
- .getProperty("transform")
701
- ?.asKind(SyntaxKind.PropertyAssignment)
702
- ?.getInitializer();
703
- const fn = transformInit ? findFunctionLiteral(transformInit) : undefined;
704
- if (!fn) continue;
705
- result[String(fromVersion)] = sourceLocationFromNode(fn, sourceFile);
706
- }
707
- return result;
708
- }
709
-
710
- function extractEventMigrationsFromKeyedObject(
711
- objLit: Node,
712
- sourceFile: SourceFile,
713
- ): Record<string, SourceLocation> {
714
- const result: Record<string, SourceLocation> = {};
715
- const obj = objLit.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);
716
- for (const prop of obj.getProperties()) {
717
- const propAssign = prop.asKind(SyntaxKind.PropertyAssignment);
718
- const initializer = propAssign?.getInitializer();
719
- const fn = initializer ? findFunctionLiteral(initializer) : undefined;
720
- if (!propAssign || !fn) continue;
721
- result[readPropertyKey(propAssign)] = sourceLocationFromNode(fn, sourceFile);
722
- }
723
- return result;
724
- }
725
-
726
- // Reads defineEvent's `migrations` option — either the array-of-steps shape
727
- // used by hand-authored calls (`[{ fromVersion, toVersion, transform }]`,
728
- // toVersion is redundant on-disk since it is always fromVersion + 1) or the
729
- // keyed-object canonical shape the Designer renders (`{ "1": transform }`).
730
- // Keyed by fromVersion (as a string) → the transform closure's location.
731
- // Skips malformed entries rather than failing the whole defineEvent extract
732
- // — same "best-effort, degrade gracefully" posture as readDataLiteralNode.
733
- function extractEventMigrationsField(
734
- node: Node,
735
- sourceFile: SourceFile,
736
- ): Readonly<Record<string, SourceLocation>> | undefined {
737
- const result = node.asKind(SyntaxKind.ArrayLiteralExpression)
738
- ? extractEventMigrationsFromArray(node, sourceFile)
739
- : node.asKind(SyntaxKind.ObjectLiteralExpression)
740
- ? extractEventMigrationsFromKeyedObject(node, sourceFile)
741
- : undefined;
742
- return result && Object.keys(result).length > 0 ? result : undefined;
743
- }
744
-
745
- export function extractDefineEvent(
746
- call: CallExpression,
747
- sourceFile: SourceFile,
748
- ): ExtractOutput<DefineEventPattern> {
749
- const args = call.getArguments();
750
- const first = args[0];
751
- if (!first) {
752
- return fail(
753
- "defineEvent",
754
- sourceLocationFromNode(call, sourceFile),
755
- "expected at least one argument",
756
- );
757
- }
758
-
759
- const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
760
- if (obj && args.length === 1) {
761
- const nameInit = obj
762
- .getProperty("name")
763
- ?.asKind(SyntaxKind.PropertyAssignment)
764
- ?.getInitializer()
765
- ?.asKind(SyntaxKind.StringLiteral);
766
- if (!nameInit) {
767
- return fail(
768
- "defineEvent",
769
- sourceLocationFromNode(call, sourceFile),
770
- "object form requires a string-literal `name` property",
771
- );
772
- }
773
- const schemaInit = obj
774
- .getProperty("schema")
775
- ?.asKind(SyntaxKind.PropertyAssignment)
776
- ?.getInitializer();
777
- if (!schemaInit) {
778
- return fail(
779
- "defineEvent",
780
- sourceLocationFromNode(call, sourceFile),
781
- "object form requires a `schema` property",
782
- );
783
- }
784
- let version: number | undefined;
785
- const versionInit = obj
786
- .getProperty("version")
787
- ?.asKind(SyntaxKind.PropertyAssignment)
788
- ?.getInitializer();
789
- if (versionInit) {
790
- const v = readDataLiteralNode(versionInit);
791
- if (typeof v === "number") version = v;
792
- }
793
- const migrationsInit = obj
794
- .getProperty("migrations")
795
- ?.asKind(SyntaxKind.PropertyAssignment)
796
- ?.getInitializer();
797
- const migrations = migrationsInit
798
- ? extractEventMigrationsField(migrationsInit, sourceFile)
799
- : undefined;
800
- return ok({
801
- kind: "defineEvent",
802
- source: sourceLocationFromNode(call, sourceFile),
803
- eventName: nameInit.getLiteralValue(),
804
- schemaSource: sourceLocationFromNode(schemaInit, sourceFile),
805
- ...(version !== undefined && { version }),
806
- ...(migrations !== undefined && { migrations }),
807
- });
808
- }
809
-
810
- const nameArg = first.asKind(SyntaxKind.StringLiteral);
811
- if (!nameArg) {
812
- return fail(
813
- "defineEvent",
814
- sourceLocationFromNode(call, sourceFile),
815
- "first argument must be a string literal event name (or use the object form)",
816
- );
817
- }
818
- const schemaArg = args[1];
819
- if (!schemaArg) {
820
- return fail(
821
- "defineEvent",
822
- sourceLocationFromNode(call, sourceFile),
823
- "expected a Zod schema as second argument",
824
- );
825
- }
826
- let version: number | undefined;
827
- let migrations: Readonly<Record<string, SourceLocation>> | undefined;
828
- const optionsArg = args[2];
829
- const optionsObj = optionsArg?.asKind(SyntaxKind.ObjectLiteralExpression);
830
- if (optionsObj) {
831
- const versionInit = optionsObj
832
- .getProperty("version")
833
- ?.asKind(SyntaxKind.PropertyAssignment)
834
- ?.getInitializer();
835
- if (versionInit) {
836
- const v = readDataLiteralNode(versionInit);
837
- if (typeof v === "number") version = v;
838
- }
839
- const migrationsInit = optionsObj
840
- .getProperty("migrations")
841
- ?.asKind(SyntaxKind.PropertyAssignment)
842
- ?.getInitializer();
843
- if (migrationsInit) {
844
- migrations = extractEventMigrationsField(migrationsInit, sourceFile);
845
- }
846
- }
847
- return ok({
848
- kind: "defineEvent",
849
- source: sourceLocationFromNode(call, sourceFile),
850
- eventName: nameArg.getLiteralValue(),
851
- schemaSource: sourceLocationFromNode(schemaArg, sourceFile),
852
- ...(version !== undefined && { version }),
853
- ...(migrations !== undefined && { migrations }),
854
- });
855
- }
856
-
857
- export function extractNotification(
858
- call: CallExpression,
859
- sourceFile: SourceFile,
860
- ): ExtractOutput<NotificationPattern> {
861
- const args = call.getArguments();
862
- const first = args[0];
863
- if (!first) {
864
- return fail(
865
- "notification",
866
- sourceLocationFromNode(call, sourceFile),
867
- "expected at least one argument",
868
- );
869
- }
870
-
871
- let nameLiteral: ReturnType<typeof first.asKind<SyntaxKind.StringLiteral>>;
872
- let defObj: ReturnType<typeof first.asKind<SyntaxKind.ObjectLiteralExpression>>;
873
-
874
- const firstObj = first.asKind(SyntaxKind.ObjectLiteralExpression);
875
- if (firstObj && args.length === 1) {
876
- nameLiteral = firstObj
877
- .getProperty("name")
878
- ?.asKind(SyntaxKind.PropertyAssignment)
879
- ?.getInitializer()
880
- ?.asKind(SyntaxKind.StringLiteral);
881
- if (!nameLiteral) {
882
- return fail(
883
- "notification",
884
- sourceLocationFromNode(call, sourceFile),
885
- "object form requires a string-literal `name` property",
886
- );
887
- }
888
- defObj = firstObj;
889
- } else {
890
- nameLiteral = first.asKind(SyntaxKind.StringLiteral);
891
- if (!nameLiteral) {
892
- return fail(
893
- "notification",
894
- sourceLocationFromNode(call, sourceFile),
895
- "first argument must be a string literal notification name (or use the object form)",
896
- );
897
- }
898
- defObj = args[1]?.asKind(SyntaxKind.ObjectLiteralExpression);
899
- if (!defObj) {
900
- return fail(
901
- "notification",
902
- sourceLocationFromNode(call, sourceFile),
903
- "second argument must be an inline definition object",
904
- );
905
- }
906
- }
907
- const nameArg = nameLiteral;
908
- const triggerObj = defObj
909
- .getProperty("trigger")
910
- ?.asKind(SyntaxKind.PropertyAssignment)
911
- ?.getInitializer()
912
- ?.asKind(SyntaxKind.ObjectLiteralExpression);
913
- if (!triggerObj) {
914
- return fail(
915
- "notification",
916
- sourceLocationFromNode(call, sourceFile),
917
- "missing or non-object `trigger` property",
918
- );
919
- }
920
- const onInit = triggerObj
921
- .getProperty("on")
922
- ?.asKind(SyntaxKind.PropertyAssignment)
923
- ?.getInitializer();
924
- const onName = onInit ? readNameOrRef(onInit) : undefined;
925
- if (!onName) {
926
- return fail(
927
- "notification",
928
- sourceLocationFromNode(call, sourceFile),
929
- "trigger.on must be a string literal or inline { name } object",
930
- );
931
- }
932
- const recipientInit = defObj
933
- .getProperty("recipient")
934
- ?.asKind(SyntaxKind.PropertyAssignment)
935
- ?.getInitializer();
936
- const recipientFn = recipientInit ? findFunctionLiteral(recipientInit) : undefined;
937
- if (!recipientFn) {
938
- return fail(
939
- "notification",
940
- sourceLocationFromNode(call, sourceFile),
941
- "recipient must be an inline arrow function or function expression",
942
- );
943
- }
944
- const dataInit = defObj
945
- .getProperty("data")
946
- ?.asKind(SyntaxKind.PropertyAssignment)
947
- ?.getInitializer();
948
- const dataFn = dataInit ? findFunctionLiteral(dataInit) : undefined;
949
- if (!dataFn) {
950
- return fail(
951
- "notification",
952
- sourceLocationFromNode(call, sourceFile),
953
- "data must be an inline arrow function or function expression",
954
- );
955
- }
956
- let templates: Record<string, SourceLocation> | undefined;
957
- const templatesObj = defObj
958
- .getProperty("templates")
959
- ?.asKind(SyntaxKind.PropertyAssignment)
960
- ?.getInitializer()
961
- ?.asKind(SyntaxKind.ObjectLiteralExpression);
962
- if (templatesObj) {
963
- templates = {};
964
- for (const prop of templatesObj.getProperties()) {
965
- const propAssign = prop.asKind(SyntaxKind.PropertyAssignment);
966
- if (!propAssign) continue;
967
- const init = propAssign.getInitializer();
968
- if (!init) continue;
969
- const tfn = findFunctionLiteral(init);
970
- if (!tfn) continue;
971
- templates[readPropertyKey(propAssign)] = sourceLocationFromNode(tfn, sourceFile);
972
- }
973
- }
974
- return ok({
975
- kind: "notification",
976
- source: sourceLocationFromNode(call, sourceFile),
977
- notificationName: nameArg.getLiteralValue(),
978
- trigger: { on: onName },
979
- recipientBody: sourceLocationFromNode(recipientFn, sourceFile),
980
- dataBody: sourceLocationFromNode(dataFn, sourceFile),
981
- ...(templates !== undefined && { templates }),
982
- });
983
- }
984
-
985
- export function readApplyBodies(
986
- defObj: ReturnType<Node["asKind"]>,
987
- sourceFile: SourceFile,
988
- ): Record<string, SourceLocation> | undefined {
989
- if (!defObj) return undefined;
990
- const obj = defObj.asKind?.(SyntaxKind.ObjectLiteralExpression);
991
- if (!obj) return undefined;
992
- const applyObj = obj
993
- .getProperty("apply")
994
- ?.asKind(SyntaxKind.PropertyAssignment)
995
- ?.getInitializer()
996
- ?.asKind(SyntaxKind.ObjectLiteralExpression);
997
- if (!applyObj) return undefined;
998
- const out: Record<string, SourceLocation> = {};
999
- for (const prop of applyObj.getProperties()) {
1000
- const propAssign = prop.asKind(SyntaxKind.PropertyAssignment);
1001
- if (!propAssign) return undefined;
1002
- const init = propAssign.getInitializer();
1003
- if (!init) return undefined;
1004
- const fn = findFunctionLiteral(init);
1005
- if (!fn) return undefined;
1006
- out[readPropertyKey(propAssign)] = sourceLocationFromNode(fn, sourceFile);
1007
- }
1008
- return out;
1009
- }
1010
-
1011
- export function extractProjection(
1012
- call: CallExpression,
1013
- sourceFile: SourceFile,
1014
- ): ExtractOutput<ProjectionPattern> {
1015
- const arg = call.getArguments()[0]?.asKind(SyntaxKind.ObjectLiteralExpression);
1016
- if (!arg) {
1017
- return fail(
1018
- "projection",
1019
- sourceLocationFromNode(call, sourceFile),
1020
- "argument must be an inline ProjectionDefinition object",
1021
- );
1022
- }
1023
- const nameLit = arg
1024
- .getProperty("name")
1025
- ?.asKind(SyntaxKind.PropertyAssignment)
1026
- ?.getInitializer()
1027
- ?.asKind(SyntaxKind.StringLiteral);
1028
- if (!nameLit) {
1029
- return fail(
1030
- "projection",
1031
- sourceLocationFromNode(call, sourceFile),
1032
- "name must be a string literal",
1033
- );
1034
- }
1035
- const sourceInit = arg
1036
- .getProperty("source")
1037
- ?.asKind(SyntaxKind.PropertyAssignment)
1038
- ?.getInitializer();
1039
- if (!sourceInit) {
1040
- return fail(
1041
- "projection",
1042
- sourceLocationFromNode(call, sourceFile),
1043
- "missing `source` property",
1044
- );
1045
- }
1046
- const sourceEntity = readNameOrRefOrList(sourceInit);
1047
- if (!sourceEntity) {
1048
- return fail(
1049
- "projection",
1050
- sourceLocationFromNode(call, sourceFile),
1051
- "source must be a string literal or array of string literals",
1052
- );
1053
- }
1054
- const applyBodies = readApplyBodies(arg, sourceFile);
1055
- if (!applyBodies) {
1056
- return fail(
1057
- "projection",
1058
- sourceLocationFromNode(call, sourceFile),
1059
- "apply must be an inline object map of event-type → function",
1060
- );
1061
- }
1062
- return ok({
1063
- kind: "projection",
1064
- source: sourceLocationFromNode(call, sourceFile),
1065
- name: nameLit.getLiteralValue(),
1066
- sourceEntity,
1067
- applyBodies,
1068
- });
1069
- }
1070
-
1071
- export function extractMultiStreamProjection(
1072
- call: CallExpression,
1073
- sourceFile: SourceFile,
1074
- ): ExtractOutput<MultiStreamProjectionPattern> {
1075
- const arg = call.getArguments()[0]?.asKind(SyntaxKind.ObjectLiteralExpression);
1076
- if (!arg) {
1077
- return fail(
1078
- "multiStreamProjection",
1079
- sourceLocationFromNode(call, sourceFile),
1080
- "argument must be an inline MultiStreamProjectionDefinition object",
1081
- );
1082
- }
1083
- const nameLit = arg
1084
- .getProperty("name")
1085
- ?.asKind(SyntaxKind.PropertyAssignment)
1086
- ?.getInitializer()
1087
- ?.asKind(SyntaxKind.StringLiteral);
1088
- if (!nameLit) {
1089
- return fail(
1090
- "multiStreamProjection",
1091
- sourceLocationFromNode(call, sourceFile),
1092
- "name must be a string literal",
1093
- );
1094
- }
1095
- const applyBodies = readApplyBodies(arg, sourceFile);
1096
- if (!applyBodies) {
1097
- return fail(
1098
- "multiStreamProjection",
1099
- sourceLocationFromNode(call, sourceFile),
1100
- "apply must be an inline object map of event-type → function",
1101
- );
1102
- }
1103
- const errorModeInit = arg
1104
- .getProperty("errorMode")
1105
- ?.asKind(SyntaxKind.PropertyAssignment)
1106
- ?.getInitializer();
1107
- const errorMode = errorModeInit ? readDataLiteralNode(errorModeInit) : undefined;
1108
- const runInLit = arg
1109
- .getProperty("runIn")
1110
- ?.asKind(SyntaxKind.PropertyAssignment)
1111
- ?.getInitializer()
1112
- ?.asKind(SyntaxKind.StringLiteral);
1113
- const runIn = runInLit ? (runInLit.getLiteralValue() as RunIn) : undefined;
1114
- const deliveryLit = arg
1115
- .getProperty("delivery")
1116
- ?.asKind(SyntaxKind.PropertyAssignment)
1117
- ?.getInitializer()
1118
- ?.asKind(SyntaxKind.StringLiteral);
1119
- const delivery = deliveryLit
1120
- ? (deliveryLit.getLiteralValue() as "shared" | "per-instance")
1121
- : undefined;
1122
- return ok({
1123
- kind: "multiStreamProjection",
1124
- source: sourceLocationFromNode(call, sourceFile),
1125
- name: nameLit.getLiteralValue(),
1126
- applyBodies,
1127
- ...(isPlainObject(errorMode) && { errorMode: errorMode as MspErrorMode }),
1128
- ...(runIn !== undefined && { runIn }),
1129
- ...(delivery !== undefined && { delivery }),
1130
- });
1131
- }
1132
-
1133
- export function collectScreenOpaqueProps(
1134
- node: Node,
1135
- path: string,
1136
- sourceFile: SourceFile,
1137
- out: Record<string, SourceLocation>,
1138
- ): void {
1139
- const fn = findFunctionLiteral(node);
1140
- if (fn) {
1141
- out[path] = sourceLocationFromNode(fn, sourceFile);
1142
- } else if (node.isKind(SyntaxKind.ObjectLiteralExpression)) {
1143
- for (const prop of node.getProperties()) {
1144
- const propAssign = prop.asKind(SyntaxKind.PropertyAssignment);
1145
- if (!propAssign) continue;
1146
- const init = propAssign.getInitializer();
1147
- if (!init) continue;
1148
- const key = readPropertyKey(propAssign);
1149
- const childPath = path ? `${path}.${key}` : key;
1150
- collectScreenOpaqueProps(init, childPath, sourceFile, out);
1151
- }
1152
- } else if (node.isKind(SyntaxKind.ArrayLiteralExpression)) {
1153
- node.getElements().forEach((el, idx) => {
1154
- collectScreenOpaqueProps(el, `${path}.${idx}`, sourceFile, out);
1155
- });
1156
- }
1157
- }
1158
-
1159
- export function readScreenStatic(node: Node): unknown {
1160
- if (findFunctionLiteral(node)) return SCREEN_OPAQUE_MARKER;
1161
- const obj = node.asKind(SyntaxKind.ObjectLiteralExpression);
1162
- if (obj) {
1163
- const out: Record<string, unknown> = {};
1164
- for (const prop of obj.getProperties()) {
1165
- const propAssign = prop.asKind(SyntaxKind.PropertyAssignment);
1166
- if (!propAssign) continue;
1167
- const init = propAssign.getInitializer();
1168
- if (!init) continue;
1169
- out[readPropertyKey(propAssign)] = readScreenStatic(init);
1170
- }
1171
- return out;
1172
- }
1173
- const arr = node.asKind(SyntaxKind.ArrayLiteralExpression);
1174
- if (arr) {
1175
- return arr.getElements().map(readScreenStatic);
1176
- }
1177
- const value = readDataLiteralNode(node);
1178
- if (value === undefined) return SCREEN_OPAQUE_MARKER;
1179
- return value;
1180
- }
1181
-
1182
- export function extractScreen(
1183
- call: CallExpression,
1184
- sourceFile: SourceFile,
1185
- ): ExtractOutput<ScreenPattern> {
1186
- const arg = call.getArguments()[0];
1187
- if (!arg) {
1188
- return fail(
1189
- "screen",
1190
- sourceLocationFromNode(call, sourceFile),
1191
- "expected a ScreenDefinition object as first argument",
1192
- );
1193
- }
1194
- const obj = arg.asKind(SyntaxKind.ObjectLiteralExpression);
1195
- if (!obj) {
1196
- const raw = readDataLiteralNode(arg);
1197
- if (!isRawRefSentinel(raw)) {
1198
- return fail(
1199
- "screen",
1200
- sourceLocationFromNode(call, sourceFile),
1201
- "argument must be an inline object literal",
1202
- );
1203
- }
1204
- return ok({
1205
- kind: "screen",
1206
- source: sourceLocationFromNode(call, sourceFile),
1207
- definition: raw as unknown as ScreenDefinition,
1208
- opaqueProps: {} as OpaquePropMap,
1209
- });
1210
- }
1211
- const opaqueProps: Record<string, SourceLocation> = {};
1212
- collectScreenOpaqueProps(obj, "", sourceFile, opaqueProps);
1213
- const definition = readScreenStatic(obj);
1214
- if (!isPlainObject(definition)) {
1215
- return fail(
1216
- "screen",
1217
- sourceLocationFromNode(call, sourceFile),
1218
- "definition could not be read structurally",
1219
- );
1220
- }
1221
- return ok({
1222
- kind: "screen",
1223
- source: sourceLocationFromNode(call, sourceFile),
1224
- definition: definition as ScreenDefinition,
1225
- opaqueProps: opaqueProps as OpaquePropMap,
1226
- });
1227
- }