@mmapp/react-compiler 0.1.0-alpha.1 → 0.1.0-alpha.3

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 (251) hide show
  1. package/ATOM-PIPELINE.md +144 -0
  2. package/README.md +88 -40
  3. package/dist/babel/index.js +113 -6
  4. package/dist/babel/index.mjs +2 -2
  5. package/dist/chunk-3USIFFE4.mjs +2190 -0
  6. package/dist/chunk-45YMGEVT.mjs +186 -0
  7. package/dist/chunk-4FN2AISW.mjs +148 -0
  8. package/dist/chunk-4OPI5L7G.mjs +2593 -0
  9. package/dist/chunk-4RYTKOOJ.mjs +186 -0
  10. package/dist/chunk-5RKTOVR5.mjs +244 -0
  11. package/dist/chunk-5YDMOO4X.mjs +214 -0
  12. package/dist/chunk-64ZWEMLJ.mjs +148 -0
  13. package/dist/chunk-6XP4KSWQ.mjs +2190 -0
  14. package/dist/chunk-72QWL54I.mjs +175 -0
  15. package/dist/chunk-7B4TRI7C.mjs +4835 -0
  16. package/dist/chunk-7ZKGHTNB.mjs +4952 -0
  17. package/dist/chunk-CIESM3BP.mjs +33 -0
  18. package/dist/chunk-DE3ZGQAC.mjs +148 -0
  19. package/dist/chunk-DMCY3BBG.mjs +1933 -0
  20. package/dist/chunk-DPIK3PJS.mjs +244 -0
  21. package/dist/chunk-E5IVH4RE.mjs +186 -0
  22. package/dist/chunk-E6FZNUR5.mjs +4953 -0
  23. package/dist/chunk-EJRBDQDP.mjs +2607 -0
  24. package/dist/chunk-ELO4TXJL.mjs +186 -0
  25. package/dist/chunk-FKRO52XH.mjs +3446 -0
  26. package/dist/chunk-FL4YAKU6.mjs +4941 -0
  27. package/dist/chunk-FYT47UBU.mjs +5076 -0
  28. package/dist/chunk-GCLGPOJZ.mjs +148 -0
  29. package/dist/chunk-GXB4JOP7.mjs +5072 -0
  30. package/dist/chunk-HFXOUMTD.mjs +175 -0
  31. package/dist/chunk-HWIZ47US.mjs +214 -0
  32. package/dist/chunk-IB7MNPQL.mjs +4953 -0
  33. package/dist/chunk-ICSIHQCG.mjs +148 -0
  34. package/dist/chunk-JLA5VNQ3.mjs +186 -0
  35. package/dist/chunk-JQLWFCTM.mjs +214 -0
  36. package/dist/chunk-KFJJCQAL.mjs +148 -0
  37. package/dist/chunk-KJUIIEQE.mjs +186 -0
  38. package/dist/chunk-KNWTHRVQ.mjs +175 -0
  39. package/dist/chunk-KSG4XSZF.mjs +175 -0
  40. package/dist/chunk-LF5N6DOU.mjs +175 -0
  41. package/dist/chunk-LJQCM2IM.mjs +214 -0
  42. package/dist/chunk-NW6555WJ.mjs +186 -0
  43. package/dist/chunk-OMZE6VLQ.mjs +214 -0
  44. package/dist/chunk-P4BR7WVO.mjs +2190 -0
  45. package/dist/chunk-QQHVYH2X.mjs +244 -0
  46. package/dist/chunk-S5QLWLLT.mjs +186 -0
  47. package/dist/chunk-SCWGT2FY.mjs +2190 -0
  48. package/dist/chunk-SMKJUSB3.mjs +2190 -0
  49. package/dist/chunk-VCAY2KGM.mjs +175 -0
  50. package/dist/chunk-WECAV6QB.mjs +148 -0
  51. package/dist/chunk-WMKBXUCE.mjs +3228 -0
  52. package/dist/chunk-XAJ5BKKL.mjs +4947 -0
  53. package/dist/chunk-XG2X7AEA.mjs +175 -0
  54. package/dist/chunk-XG7Z23NQ.mjs +148 -0
  55. package/dist/chunk-XWZAOCQ7.mjs +2607 -0
  56. package/dist/chunk-Y6MA7ULW.mjs +148 -0
  57. package/dist/chunk-YMS7Q7LG.mjs +214 -0
  58. package/dist/chunk-ZA37XTGA.mjs +175 -0
  59. package/dist/cli/index.js +1616 -366
  60. package/dist/cli/index.mjs +8 -8
  61. package/dist/codemod/cli.mjs +1 -1
  62. package/dist/codemod/index.mjs +1 -1
  63. package/dist/dev-server-RmGHIntF.d.mts +113 -0
  64. package/dist/dev-server-RmGHIntF.d.ts +113 -0
  65. package/dist/dev-server.d.mts +1 -1
  66. package/dist/dev-server.d.ts +1 -1
  67. package/dist/dev-server.js +982 -53
  68. package/dist/dev-server.mjs +5 -5
  69. package/dist/envelope.js +113 -6
  70. package/dist/envelope.mjs +3 -3
  71. package/dist/index.d.mts +5 -1
  72. package/dist/index.d.ts +5 -1
  73. package/dist/index.js +992 -63
  74. package/dist/index.mjs +8 -8
  75. package/{src/cli/init.ts → dist/init-7JQMAAXS.mjs} +70 -95
  76. package/dist/init-EHO4VQ22.mjs +369 -0
  77. package/dist/init-UC3FWPIW.mjs +367 -0
  78. package/dist/init-UNSMVKIK.mjs +366 -0
  79. package/dist/init-UNV5XIDE.mjs +367 -0
  80. package/dist/project-compiler-2P4N4DR7.mjs +10 -0
  81. package/dist/project-compiler-D2LCC27O.mjs +10 -0
  82. package/dist/project-compiler-EJ3GANJE.mjs +10 -0
  83. package/dist/project-compiler-LOQKVRZJ.mjs +10 -0
  84. package/dist/project-compiler-RQ6OQKRM.mjs +10 -0
  85. package/dist/project-compiler-VWNNCHGO.mjs +10 -0
  86. package/dist/project-compiler-XVAAU4C5.mjs +10 -0
  87. package/dist/project-compiler-YES5FGMD.mjs +10 -0
  88. package/dist/project-compiler-ZKMQDLGU.mjs +10 -0
  89. package/dist/project-decompiler-FLXCEJHS.mjs +7 -0
  90. package/dist/project-decompiler-VLPR22QF.mjs +7 -0
  91. package/dist/pull-FUS5QYZS.mjs +109 -0
  92. package/dist/pull-LD5ENLGY.mjs +109 -0
  93. package/dist/testing/index.js +113 -6
  94. package/dist/testing/index.mjs +2 -2
  95. package/dist/vite/index.js +113 -6
  96. package/dist/vite/index.mjs +3 -3
  97. package/examples/uber-app/app/admin/fleet.tsx +19 -19
  98. package/package.json +4 -3
  99. package/compile-blueprint-chat.mjs +0 -99
  100. package/compile-blueprint-glass-console.mjs +0 -98
  101. package/compile-chat-defs.mjs +0 -92
  102. package/examples/uber-app/tests/payment.test.tsx +0 -129
  103. package/examples/uber-app/tests/ride-flow.test.tsx +0 -123
  104. package/package.json.backup +0 -86
  105. package/scripts/decompile.ts +0 -226
  106. package/scripts/seed-auth.ts +0 -267
  107. package/scripts/seed-uber.ts +0 -248
  108. package/scripts/validate-uber.ts +0 -119
  109. package/seed-blueprint-chat.mjs +0 -444
  110. package/seed-blueprint-glass-console.mjs +0 -445
  111. package/seed-compiled.mjs +0 -318
  112. package/src/RoundTripValidator.ts +0 -400
  113. package/src/__tests__/atom-rendering-coverage.test.ts +0 -680
  114. package/src/__tests__/auth-module-compilation.test.ts +0 -247
  115. package/src/__tests__/auth-template-compilation.test.ts +0 -589
  116. package/src/__tests__/change-extractor.test.ts +0 -142
  117. package/src/__tests__/cli-pull.test.ts +0 -73
  118. package/src/__tests__/cli-test.test.ts +0 -72
  119. package/src/__tests__/component-extractor.test.ts +0 -331
  120. package/src/__tests__/context-extractor.test.ts +0 -145
  121. package/src/__tests__/decompiler.test.ts +0 -718
  122. package/src/__tests__/define-blueprint.test.ts +0 -133
  123. package/src/__tests__/definition-validator.test.ts +0 -519
  124. package/src/__tests__/during-extractor.test.ts +0 -152
  125. package/src/__tests__/effect-extractor.test.ts +0 -107
  126. package/src/__tests__/event-emission.test.ts +0 -127
  127. package/src/__tests__/examples.test.ts +0 -236
  128. package/src/__tests__/full-blueprint-coverage.test.ts +0 -1221
  129. package/src/__tests__/golden-suite.test.ts +0 -403
  130. package/src/__tests__/grammar-island-extractor.test.ts +0 -289
  131. package/src/__tests__/instance-key.test.ts +0 -82
  132. package/src/__tests__/ir-migration.test.ts +0 -255
  133. package/src/__tests__/lock-file.test.ts +0 -117
  134. package/src/__tests__/model-extractor.test.ts +0 -195
  135. package/src/__tests__/model-field-acl.test.ts +0 -237
  136. package/src/__tests__/model-hooks.test.ts +0 -130
  137. package/src/__tests__/model-ref-resolution.test.ts +0 -268
  138. package/src/__tests__/model-roundtrip.test.ts +0 -502
  139. package/src/__tests__/model-runtime.test.ts +0 -112
  140. package/src/__tests__/model-transitions.test.ts +0 -183
  141. package/src/__tests__/nrt-action-trace.test.ts +0 -391
  142. package/src/__tests__/pipeline-hardening.test.ts +0 -413
  143. package/src/__tests__/project-compiler.test.ts +0 -546
  144. package/src/__tests__/project-decompiler.test.ts +0 -343
  145. package/src/__tests__/query-compilation.test.ts +0 -145
  146. package/src/__tests__/round-trip/PLAN.md +0 -158
  147. package/src/__tests__/round-trip/README.md +0 -52
  148. package/src/__tests__/round-trip/RESULTS.md +0 -86
  149. package/src/__tests__/round-trip/fixtures/data-heavy/main.workflow.tsx +0 -55
  150. package/src/__tests__/round-trip/fixtures/data-heavy/mm.config.ts +0 -11
  151. package/src/__tests__/round-trip/fixtures/data-heavy/models/contact.ts +0 -54
  152. package/src/__tests__/round-trip/fixtures/full-workflow/main.workflow.tsx +0 -79
  153. package/src/__tests__/round-trip/fixtures/full-workflow/mm.config.ts +0 -12
  154. package/src/__tests__/round-trip/fixtures/full-workflow/models/order.ts +0 -50
  155. package/src/__tests__/round-trip/fixtures/simple-crud/main.workflow.tsx +0 -25
  156. package/src/__tests__/round-trip/fixtures/simple-crud/mm.config.ts +0 -11
  157. package/src/__tests__/round-trip/fixtures/simple-crud/models/task.ts +0 -32
  158. package/src/__tests__/round-trip/fixtures/view-heavy/main.workflow.tsx +0 -79
  159. package/src/__tests__/round-trip/fixtures/view-heavy/mm.config.ts +0 -10
  160. package/src/__tests__/round-trip/round-trip.test.ts +0 -2598
  161. package/src/__tests__/round-trip-ir.test.ts +0 -300
  162. package/src/__tests__/round-trip.test.ts +0 -1212
  163. package/src/__tests__/route-merging.test.ts +0 -372
  164. package/src/__tests__/router-composition.test.ts +0 -489
  165. package/src/__tests__/router-extractor.test.ts +0 -176
  166. package/src/__tests__/server-action-extractor.test.ts +0 -128
  167. package/src/__tests__/smart-type-inference.test.ts +0 -365
  168. package/src/__tests__/source-envelope.test.ts +0 -284
  169. package/src/__tests__/source-fidelity.test.ts +0 -516
  170. package/src/__tests__/state-extractor.test.ts +0 -115
  171. package/src/__tests__/strict-mode.test.ts +0 -227
  172. package/src/__tests__/transition-effect-extractor.test.ts +0 -119
  173. package/src/__tests__/transition-extractor.test.ts +0 -68
  174. package/src/__tests__/ts-to-expression.test.ts +0 -462
  175. package/src/__tests__/type-generator.test.ts +0 -201
  176. package/src/__tests__/uber-validation.test.ts +0 -502
  177. package/src/action-compiler.ts +0 -361
  178. package/src/babel/emitters/experience-transform.ts +0 -199
  179. package/src/babel/emitters/ir-to-tsx-emitter.ts +0 -110
  180. package/src/babel/emitters/pure-form-emitter.ts +0 -1023
  181. package/src/babel/emitters/runtime-glue-emitter.ts +0 -39
  182. package/src/babel/extractors/change-extractor.ts +0 -199
  183. package/src/babel/extractors/component-extractor.ts +0 -907
  184. package/src/babel/extractors/computed-extractor.ts +0 -262
  185. package/src/babel/extractors/context-extractor.ts +0 -277
  186. package/src/babel/extractors/during-extractor.ts +0 -295
  187. package/src/babel/extractors/effect-extractor.ts +0 -340
  188. package/src/babel/extractors/event-extractor.ts +0 -235
  189. package/src/babel/extractors/grammar-island-extractor.ts +0 -302
  190. package/src/babel/extractors/model-extractor.ts +0 -1018
  191. package/src/babel/extractors/router-extractor.ts +0 -303
  192. package/src/babel/extractors/server-action-extractor.ts +0 -173
  193. package/src/babel/extractors/server-action-hook-extractor.ts +0 -72
  194. package/src/babel/extractors/server-state-extractor.ts +0 -88
  195. package/src/babel/extractors/state-extractor.ts +0 -214
  196. package/src/babel/extractors/transition-effect-extractor.ts +0 -176
  197. package/src/babel/extractors/transition-extractor.ts +0 -143
  198. package/src/babel/index.ts +0 -24
  199. package/src/babel/transpilers/ts-to-expression.ts +0 -674
  200. package/src/babel/visitor.ts +0 -807
  201. package/src/cli/auth.ts +0 -255
  202. package/src/cli/build.ts +0 -288
  203. package/src/cli/deploy.ts +0 -206
  204. package/src/cli/index.ts +0 -328
  205. package/src/cli/installer.ts +0 -261
  206. package/src/cli/lock-file.ts +0 -94
  207. package/src/cli/mmrc.ts +0 -22
  208. package/src/cli/pull.ts +0 -172
  209. package/src/cli/registry-client.ts +0 -175
  210. package/src/cli/test.ts +0 -397
  211. package/src/cli/type-generator.ts +0 -243
  212. package/src/codemod/__tests__/forward.test.ts +0 -239
  213. package/src/codemod/__tests__/reverse.test.ts +0 -145
  214. package/src/codemod/__tests__/round-trip.test.ts +0 -137
  215. package/src/codemod/annotation.ts +0 -97
  216. package/src/codemod/classify.ts +0 -197
  217. package/src/codemod/cli.ts +0 -207
  218. package/src/codemod/control-flow.ts +0 -409
  219. package/src/codemod/forward.ts +0 -244
  220. package/src/codemod/import-manager.ts +0 -171
  221. package/src/codemod/index.ts +0 -120
  222. package/src/codemod/reverse.ts +0 -197
  223. package/src/codemod/rules.ts +0 -174
  224. package/src/codemod/state-transform.ts +0 -126
  225. package/src/decompiler/ast-builder.ts +0 -538
  226. package/src/decompiler/config-generator.ts +0 -151
  227. package/src/decompiler/index.ts +0 -315
  228. package/src/decompiler/project-decompiler.ts +0 -1776
  229. package/src/decompiler/project.ts +0 -862
  230. package/src/decompiler/split-strategy.ts +0 -140
  231. package/src/decompiler/state-emitter.ts +0 -1053
  232. package/src/decompiler/sx-emitter.ts +0 -318
  233. package/src/decompiler/workspace-hydrator.ts +0 -189
  234. package/src/dev-server.ts +0 -238
  235. package/src/envelope/fs-tree.ts +0 -217
  236. package/src/envelope/source-envelope.ts +0 -264
  237. package/src/envelope.ts +0 -315
  238. package/src/incremental-compiler.ts +0 -401
  239. package/src/index.ts +0 -99
  240. package/src/model-compiler.ts +0 -277
  241. package/src/project-compiler.ts +0 -1629
  242. package/src/route-extractor.ts +0 -333
  243. package/src/testing/index.ts +0 -32
  244. package/src/testing/snapshot.ts +0 -252
  245. package/src/testing/test-utils.ts +0 -226
  246. package/src/types.ts +0 -68
  247. package/src/vite/index.ts +0 -288
  248. package/test-compile.mjs +0 -142
  249. package/tsconfig.json +0 -25
  250. package/tsup.config.ts +0 -23
  251. package/vitest.config.ts +0 -9
@@ -0,0 +1,4941 @@
1
+ // src/babel/visitor.ts
2
+ import * as t17 from "@babel/types";
3
+
4
+ // src/babel/extractors/state-extractor.ts
5
+ import * as t from "@babel/types";
6
+ var KNOWN_TYPE_REFS = {
7
+ Date: "date",
8
+ URL: "url",
9
+ Record: "json",
10
+ Map: "json",
11
+ Set: "json"
12
+ };
13
+ function typeNameToSlug(name) {
14
+ return name.replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
15
+ }
16
+ function resolveSingleType(annotation) {
17
+ if (t.isTSNumberKeyword(annotation)) return "number";
18
+ if (t.isTSStringKeyword(annotation)) return "text";
19
+ if (t.isTSBooleanKeyword(annotation)) return "boolean";
20
+ if (t.isTSTypeReference(annotation)) {
21
+ const typeName = t.isIdentifier(annotation.typeName) ? annotation.typeName.name : "";
22
+ if (typeName === "Array") return "multi_select";
23
+ if (typeName && KNOWN_TYPE_REFS[typeName]) return KNOWN_TYPE_REFS[typeName];
24
+ if (typeName) return typeNameToSlug(typeName);
25
+ }
26
+ if (t.isTSArrayType(annotation)) return "multi_select";
27
+ if (t.isTSTypeLiteral(annotation) || t.isTSTupleType(annotation)) return "json";
28
+ return null;
29
+ }
30
+ function inferFieldType(typeAnnotation, defaultValue) {
31
+ if (typeAnnotation) {
32
+ if (t.isTSUnionType(typeAnnotation)) {
33
+ const allStringLiterals = typeAnnotation.types.every(
34
+ (m) => t.isTSLiteralType(m) && t.isStringLiteral(m.literal)
35
+ );
36
+ if (allStringLiterals) return "select";
37
+ const nonNullMembers = typeAnnotation.types.filter(
38
+ (m) => !t.isTSNullKeyword(m) && !t.isTSUndefinedKeyword(m)
39
+ );
40
+ const allLiteralsExceptNull = nonNullMembers.every(
41
+ (m) => t.isTSLiteralType(m) && t.isStringLiteral(m.literal)
42
+ );
43
+ if (allLiteralsExceptNull && nonNullMembers.length > 0) return "select";
44
+ if (nonNullMembers.length === 1) {
45
+ const resolved2 = resolveSingleType(nonNullMembers[0]);
46
+ if (resolved2) return resolved2;
47
+ }
48
+ if (nonNullMembers.length > 1) return "json";
49
+ }
50
+ const resolved = resolveSingleType(typeAnnotation);
51
+ if (resolved) return resolved;
52
+ }
53
+ if (defaultValue) {
54
+ if (t.isNumericLiteral(defaultValue)) return "number";
55
+ if (t.isStringLiteral(defaultValue)) return "text";
56
+ if (t.isBooleanLiteral(defaultValue)) return "boolean";
57
+ if (t.isArrayExpression(defaultValue)) return "json";
58
+ if (t.isObjectExpression(defaultValue)) return "json";
59
+ if (t.isNewExpression(defaultValue) && t.isIdentifier(defaultValue.callee)) {
60
+ if (defaultValue.callee.name === "Date") return "date";
61
+ }
62
+ }
63
+ return "text";
64
+ }
65
+ function extractDefaultValue(arg) {
66
+ if (t.isNumericLiteral(arg)) return arg.value;
67
+ if (t.isStringLiteral(arg)) return arg.value;
68
+ if (t.isBooleanLiteral(arg)) return arg.value;
69
+ if (t.isNullLiteral(arg)) return null;
70
+ if (t.isArrayExpression(arg)) {
71
+ return arg.elements.map(
72
+ (el) => el && t.isExpression(el) ? extractDefaultValue(el) : null
73
+ );
74
+ }
75
+ if (t.isObjectExpression(arg)) {
76
+ const obj = {};
77
+ arg.properties.forEach((prop) => {
78
+ if (t.isObjectProperty(prop) && t.isIdentifier(prop.key) && t.isExpression(prop.value)) {
79
+ obj[prop.key.name] = extractDefaultValue(prop.value);
80
+ }
81
+ });
82
+ return obj;
83
+ }
84
+ return void 0;
85
+ }
86
+ function extractStates(path, state) {
87
+ const parent = path.parentPath;
88
+ if (!parent.isVariableDeclarator()) return;
89
+ const id = parent.node.id;
90
+ if (!t.isArrayPattern(id) || id.elements.length < 1) return;
91
+ const firstElement = id.elements[0];
92
+ if (!t.isIdentifier(firstElement)) return;
93
+ const fieldName = firstElement.name;
94
+ const args = path.node.arguments;
95
+ const defaultValue = args.length > 0 && t.isExpression(args[0]) ? args[0] : null;
96
+ let typeAnnotation = null;
97
+ if (path.node.typeParameters && t.isTSTypeParameterInstantiation(path.node.typeParameters)) {
98
+ const params = path.node.typeParameters.params;
99
+ if (params.length > 0) {
100
+ typeAnnotation = params[0];
101
+ }
102
+ }
103
+ const fieldType = inferFieldType(typeAnnotation, defaultValue);
104
+ const fieldDefaultValue = defaultValue ? extractDefaultValue(defaultValue) : void 0;
105
+ const field = {
106
+ name: fieldName,
107
+ type: fieldType,
108
+ default_value: fieldDefaultValue
109
+ };
110
+ if (fieldType === "select" && typeAnnotation && t.isTSUnionType(typeAnnotation)) {
111
+ const options = typeAnnotation.types.filter((m) => t.isTSLiteralType(m) && t.isStringLiteral(m.literal)).map((m) => m.literal.value);
112
+ if (options.length > 0) {
113
+ field.validation = { options };
114
+ }
115
+ }
116
+ const compilerState = state;
117
+ if (!compilerState.fields) compilerState.fields = [];
118
+ if (!compilerState.fields.some((f) => f.name === fieldName)) {
119
+ compilerState.fields.push(field);
120
+ }
121
+ }
122
+
123
+ // src/babel/extractors/effect-extractor.ts
124
+ import * as t2 from "@babel/types";
125
+ function analyzeCallbackBody(body, actionCounter) {
126
+ const actions = [];
127
+ for (const statement of body) {
128
+ if (t2.isExpressionStatement(statement)) {
129
+ const expr = statement.expression;
130
+ if (t2.isCallExpression(expr) && t2.isIdentifier(expr.callee)) {
131
+ const calleeName = expr.callee.name;
132
+ const args = expr.arguments;
133
+ if (calleeName === "setField" && args.length >= 2) {
134
+ const field = t2.isStringLiteral(args[0]) ? args[0].value : void 0;
135
+ const arg1 = args[1];
136
+ if (field !== void 0 && !t2.isArgumentPlaceholder(arg1)) {
137
+ const value = extractValue(arg1);
138
+ actions.push({
139
+ id: `auto_${++actionCounter.value}`,
140
+ type: "set_field",
141
+ mode: "auto",
142
+ config: { field, value }
143
+ });
144
+ }
145
+ } else if (calleeName === "setMemory" && args.length >= 2) {
146
+ const key = t2.isStringLiteral(args[0]) ? args[0].value : void 0;
147
+ const arg1 = args[1];
148
+ if (key !== void 0 && !t2.isArgumentPlaceholder(arg1)) {
149
+ const value = extractValue(arg1);
150
+ actions.push({
151
+ id: `auto_${++actionCounter.value}`,
152
+ type: "set_memory",
153
+ mode: "auto",
154
+ config: { key, value }
155
+ });
156
+ }
157
+ } else if (calleeName.startsWith("set") && args.length >= 1) {
158
+ const fieldName = calleeName.slice(3).charAt(0).toLowerCase() + calleeName.slice(4);
159
+ const arg0 = args[0];
160
+ if (!t2.isArgumentPlaceholder(arg0)) {
161
+ const value = extractValue(arg0);
162
+ actions.push({
163
+ id: `auto_${++actionCounter.value}`,
164
+ type: "set_field",
165
+ mode: "auto",
166
+ config: { field: fieldName, value }
167
+ });
168
+ }
169
+ }
170
+ } else if (t2.isCallExpression(expr)) {
171
+ const callee = expr.callee;
172
+ const args = expr.arguments;
173
+ if (t2.isMemberExpression(callee) && t2.isIdentifier(callee.object) && callee.object.name === "console" && t2.isIdentifier(callee.property) && callee.property.name === "log" && args.length > 0) {
174
+ const arg0 = args[0];
175
+ if (!t2.isArgumentPlaceholder(arg0)) {
176
+ const message = extractValue(arg0);
177
+ actions.push({
178
+ id: `auto_${++actionCounter.value}`,
179
+ type: "log_event",
180
+ mode: "auto",
181
+ config: { message }
182
+ });
183
+ }
184
+ } else {
185
+ actions.push({
186
+ id: `auto_${++actionCounter.value}`,
187
+ type: "custom",
188
+ mode: "auto",
189
+ config: { expression: generateCode(expr) }
190
+ });
191
+ }
192
+ }
193
+ }
194
+ }
195
+ return actions;
196
+ }
197
+ function extractValue(node) {
198
+ if (t2.isNumericLiteral(node)) return node.value;
199
+ if (t2.isStringLiteral(node)) return node.value;
200
+ if (t2.isBooleanLiteral(node)) return node.value;
201
+ if (t2.isNullLiteral(node)) return null;
202
+ if (t2.isArrayExpression(node)) {
203
+ return node.elements.map((el) => el && t2.isExpression(el) ? extractValue(el) : null);
204
+ }
205
+ if (t2.isObjectExpression(node)) {
206
+ const obj = {};
207
+ node.properties.forEach((prop) => {
208
+ if (t2.isObjectProperty(prop) && t2.isIdentifier(prop.key) && t2.isExpression(prop.value)) {
209
+ obj[prop.key.name] = extractValue(prop.value);
210
+ }
211
+ });
212
+ return obj;
213
+ }
214
+ return generateCode(node);
215
+ }
216
+ function generateCode(node) {
217
+ if (t2.isIdentifier(node)) return node.name;
218
+ if (t2.isNumericLiteral(node)) return String(node.value);
219
+ if (t2.isStringLiteral(node)) return `"${node.value}"`;
220
+ if (t2.isBooleanLiteral(node)) return String(node.value);
221
+ if (t2.isNullLiteral(node)) return "null";
222
+ if (t2.isCallExpression(node) && t2.isMemberExpression(node.callee) && t2.isIdentifier(node.callee.object) && node.callee.object.name === "Date" && t2.isIdentifier(node.callee.property) && node.callee.property.name === "now") {
223
+ return "now()";
224
+ }
225
+ if (t2.isCallExpression(node) && t2.isMemberExpression(node.callee) && t2.isNewExpression(node.callee.object) && t2.isIdentifier(node.callee.object.callee) && node.callee.object.callee.name === "Date" && t2.isIdentifier(node.callee.property) && node.callee.property.name === "toISOString") {
226
+ return "now()";
227
+ }
228
+ if (t2.isBinaryExpression(node)) {
229
+ return `(${generateCode(node.left)} ${node.operator} ${generateCode(node.right)})`;
230
+ }
231
+ if (t2.isLogicalExpression(node)) {
232
+ return `(${generateCode(node.left)} ${node.operator} ${generateCode(node.right)})`;
233
+ }
234
+ if (t2.isConditionalExpression(node)) {
235
+ return `(${generateCode(node.test)} ? ${generateCode(node.consequent)} : ${generateCode(node.alternate)})`;
236
+ }
237
+ if (t2.isUnaryExpression(node) && node.prefix) {
238
+ return `${node.operator}${generateCode(node.argument)}`;
239
+ }
240
+ if (t2.isTemplateLiteral(node)) {
241
+ return node.quasis.map((q, i) => {
242
+ const raw = q.value.raw;
243
+ if (i < node.expressions.length) return raw + "${" + generateCode(node.expressions[i]) + "}";
244
+ return raw;
245
+ }).join("");
246
+ }
247
+ if (t2.isMemberExpression(node)) {
248
+ const obj = generateCode(node.object);
249
+ if (node.computed) {
250
+ return `${obj}[${generateCode(node.property)}]`;
251
+ }
252
+ const prop = t2.isIdentifier(node.property) ? node.property.name : "[property]";
253
+ return `${obj}.${prop}`;
254
+ }
255
+ if (t2.isCallExpression(node)) {
256
+ if (t2.isIdentifier(node.callee)) {
257
+ const args = node.arguments.map((a) => generateCode(a)).join(", ");
258
+ return `${node.callee.name}(${args})`;
259
+ }
260
+ if (t2.isMemberExpression(node.callee)) {
261
+ const obj = generateCode(node.callee.object);
262
+ const prop = t2.isIdentifier(node.callee.property) ? node.callee.property.name : "[method]";
263
+ const args = node.arguments.map((a) => generateCode(a)).join(", ");
264
+ return `${obj}.${prop}(${args})`;
265
+ }
266
+ }
267
+ if (t2.isArrowFunctionExpression(node)) {
268
+ if (t2.isExpression(node.body)) {
269
+ const params = node.params;
270
+ if (params.length === 1 && t2.isIdentifier(params[0])) {
271
+ const paramName = params[0].name;
272
+ const bodyCode = generateCode(node.body);
273
+ return bodyCode.replace(new RegExp(`\\b${paramName}\\b`, "g"), "$prev");
274
+ }
275
+ }
276
+ return "[Function]";
277
+ }
278
+ if (t2.isFunctionExpression(node)) return "[Function]";
279
+ if (t2.isArrayExpression(node)) {
280
+ const elements = node.elements.map((el) => el ? generateCode(el) : "undefined").join(", ");
281
+ return `[${elements}]`;
282
+ }
283
+ if (t2.isObjectExpression(node)) {
284
+ const props = node.properties.map((p) => {
285
+ if (t2.isObjectProperty(p)) {
286
+ const key = t2.isIdentifier(p.key) ? p.key.name : t2.isStringLiteral(p.key) ? `"${p.key.value}"` : "[key]";
287
+ if (p.shorthand && t2.isIdentifier(p.value)) return key;
288
+ return `${key}: ${generateCode(p.value)}`;
289
+ }
290
+ if (t2.isSpreadElement(p)) return `...${generateCode(p.argument)}`;
291
+ return "";
292
+ }).filter(Boolean).join(", ");
293
+ return `{ ${props} }`;
294
+ }
295
+ if (t2.isTSAsExpression(node)) return generateCode(node.expression);
296
+ if (t2.isTSNonNullExpression(node)) return generateCode(node.expression);
297
+ return "[Expression]";
298
+ }
299
+ function ensureState(states, stateName) {
300
+ if (!states.has(stateName)) {
301
+ states.set(stateName, {
302
+ name: stateName,
303
+ type: "REGULAR",
304
+ on_enter: [],
305
+ during: [],
306
+ on_exit: []
307
+ });
308
+ }
309
+ return states.get(stateName);
310
+ }
311
+ function extractEffects(path, state) {
312
+ const callee = path.node.callee;
313
+ if (!t2.isIdentifier(callee)) return;
314
+ const hookName = callee.name;
315
+ const args = path.node.arguments;
316
+ if ((hookName === "useOnEnter" || hookName === "useOnExit") && args.length >= 2) {
317
+ const stateArg = args[0];
318
+ const callbackArg = args[1];
319
+ if (!t2.isStringLiteral(stateArg)) return;
320
+ const stateName = stateArg.value;
321
+ let callbackBody = [];
322
+ if (t2.isArrowFunctionExpression(callbackArg) || t2.isFunctionExpression(callbackArg)) {
323
+ const body = callbackArg.body;
324
+ if (t2.isBlockStatement(body)) {
325
+ callbackBody = body.body;
326
+ } else if (t2.isExpression(body)) {
327
+ callbackBody = [t2.expressionStatement(body)];
328
+ }
329
+ }
330
+ const compilerState = state;
331
+ if (!compilerState.states) compilerState.states = /* @__PURE__ */ new Map();
332
+ if (!compilerState.actionCounter) compilerState.actionCounter = 0;
333
+ const stateDefinition = ensureState(compilerState.states, stateName);
334
+ const actions = analyzeCallbackBody(callbackBody, { value: compilerState.actionCounter });
335
+ compilerState.actionCounter += actions.length;
336
+ if (hookName === "useOnEnter") {
337
+ stateDefinition.on_enter.push(...actions);
338
+ } else if (hookName === "useOnExit") {
339
+ stateDefinition.on_exit.push(...actions);
340
+ }
341
+ }
342
+ }
343
+
344
+ // src/babel/extractors/transition-extractor.ts
345
+ import * as t3 from "@babel/types";
346
+ function extractTransitions(path, state) {
347
+ const callee = path.node.callee;
348
+ if (!t3.isIdentifier(callee) || callee.name !== "useTransition") return;
349
+ const args = path.node.arguments;
350
+ if (args.length < 1) return;
351
+ const nameArg = args[0];
352
+ if (!t3.isStringLiteral(nameArg)) return;
353
+ const transitionName = nameArg.value;
354
+ const compilerState = state;
355
+ if (!compilerState.transitions) compilerState.transitions = [];
356
+ const existing = compilerState.transitions.find((tr) => tr.name === transitionName);
357
+ if (existing) return;
358
+ let from = [];
359
+ let to = "";
360
+ let roles;
361
+ let requiredFields;
362
+ let auto;
363
+ if (args.length >= 2 && t3.isObjectExpression(args[1])) {
364
+ const opts = args[1];
365
+ for (const prop of opts.properties) {
366
+ if (!t3.isObjectProperty(prop) || !t3.isIdentifier(prop.key)) continue;
367
+ if (prop.key.name === "from") {
368
+ if (t3.isArrayExpression(prop.value)) {
369
+ from = prop.value.elements.filter((el) => t3.isStringLiteral(el)).map((el) => el.value);
370
+ } else if (t3.isStringLiteral(prop.value)) {
371
+ from = [prop.value.value];
372
+ }
373
+ } else if (prop.key.name === "to") {
374
+ if (t3.isStringLiteral(prop.value)) {
375
+ to = prop.value.value;
376
+ }
377
+ } else if (prop.key.name === "roles") {
378
+ if (t3.isArrayExpression(prop.value)) {
379
+ roles = prop.value.elements.filter((el) => t3.isStringLiteral(el)).map((el) => el.value);
380
+ }
381
+ } else if (prop.key.name === "requiredFields") {
382
+ if (t3.isArrayExpression(prop.value)) {
383
+ requiredFields = prop.value.elements.filter((el) => t3.isStringLiteral(el)).map((el) => el.value);
384
+ }
385
+ } else if (prop.key.name === "auto") {
386
+ if (t3.isBooleanLiteral(prop.value)) {
387
+ auto = prop.value.value;
388
+ }
389
+ }
390
+ }
391
+ }
392
+ const transition = {
393
+ name: transitionName,
394
+ from,
395
+ to,
396
+ actions: [],
397
+ conditions: [],
398
+ ...roles && roles.length > 0 && { roles },
399
+ ...requiredFields && requiredFields.length > 0 && { required_fields: requiredFields },
400
+ ...auto !== void 0 && { auto }
401
+ };
402
+ compilerState.transitions.push(transition);
403
+ }
404
+ function inferTransitionStates(transitions, states) {
405
+ const stateArray = Array.from(states.values());
406
+ const startStates = stateArray.filter((s) => s.type === "START");
407
+ const regularStates = stateArray.filter((s) => s.type === "REGULAR");
408
+ const needsInference = transitions.filter((t18) => t18.from.length === 0 || !t18.to);
409
+ if (needsInference.length === 0) return;
410
+ if (startStates.length === 1 && regularStates.length > 0) {
411
+ needsInference.forEach((t18, idx) => {
412
+ if (t18.from.length === 0) {
413
+ t18.from = [startStates[0].name];
414
+ }
415
+ if (!t18.to) {
416
+ t18.to = regularStates[idx % regularStates.length]?.name || startStates[0].name;
417
+ }
418
+ });
419
+ } else {
420
+ const allStateNames = stateArray.map((s) => s.name);
421
+ needsInference.forEach((t18, idx) => {
422
+ if (t18.from.length === 0 && allStateNames.length > 0) {
423
+ t18.from = [allStateNames[0]];
424
+ }
425
+ if (!t18.to && allStateNames.length > 1) {
426
+ t18.to = allStateNames[Math.min(idx + 1, allStateNames.length - 1)];
427
+ }
428
+ });
429
+ }
430
+ }
431
+
432
+ // src/babel/extractors/event-extractor.ts
433
+ import * as t4 from "@babel/types";
434
+ function analyzeEventCallback(body) {
435
+ const actions = [];
436
+ for (const statement of body) {
437
+ if (t4.isExpressionStatement(statement)) {
438
+ const expr = statement.expression;
439
+ if (t4.isCallExpression(expr) && t4.isIdentifier(expr.callee)) {
440
+ const calleeName = expr.callee.name;
441
+ const args = expr.arguments;
442
+ if (calleeName === "setField" && args.length >= 2) {
443
+ const field = t4.isStringLiteral(args[0]) ? args[0].value : void 0;
444
+ const expression = generateExpression(args[1]);
445
+ if (field) {
446
+ actions.push({
447
+ type: "set_field",
448
+ field,
449
+ expression
450
+ });
451
+ }
452
+ } else if (calleeName === "setMemory" && args.length >= 2) {
453
+ const key = t4.isStringLiteral(args[0]) ? args[0].value : void 0;
454
+ const expression = generateExpression(args[1]);
455
+ if (key) {
456
+ actions.push({
457
+ type: "set_memory",
458
+ key,
459
+ expression
460
+ });
461
+ }
462
+ }
463
+ } else if (t4.isCallExpression(expr)) {
464
+ const callee = expr.callee;
465
+ const args = expr.arguments;
466
+ if (t4.isMemberExpression(callee) && t4.isIdentifier(callee.object) && callee.object.name === "console" && t4.isIdentifier(callee.property) && callee.property.name === "log") {
467
+ const message = args.length > 0 ? generateExpression(args[0]) : "";
468
+ actions.push({
469
+ type: "log_event",
470
+ message
471
+ });
472
+ }
473
+ }
474
+ }
475
+ }
476
+ return actions;
477
+ }
478
+ function generateExpression(node) {
479
+ if (t4.isStringLiteral(node)) return node.value;
480
+ if (t4.isNumericLiteral(node)) return String(node.value);
481
+ if (t4.isBooleanLiteral(node)) return String(node.value);
482
+ if (t4.isNullLiteral(node)) return "null";
483
+ if (t4.isIdentifier(node)) return node.name;
484
+ if (t4.isBinaryExpression(node)) {
485
+ return `(${generateExpression(node.left)} ${node.operator} ${generateExpression(node.right)})`;
486
+ }
487
+ if (t4.isLogicalExpression(node)) {
488
+ return `(${generateExpression(node.left)} ${node.operator} ${generateExpression(node.right)})`;
489
+ }
490
+ if (t4.isConditionalExpression(node)) {
491
+ return `(${generateExpression(node.test)} ? ${generateExpression(node.consequent)} : ${generateExpression(node.alternate)})`;
492
+ }
493
+ if (t4.isUnaryExpression(node) && node.prefix) {
494
+ return `${node.operator}${generateExpression(node.argument)}`;
495
+ }
496
+ if (t4.isCallExpression(node) && t4.isIdentifier(node.callee)) {
497
+ const args = node.arguments.map((arg) => generateExpression(arg)).join(", ");
498
+ return `${node.callee.name}(${args})`;
499
+ }
500
+ if (t4.isCallExpression(node) && t4.isMemberExpression(node.callee)) {
501
+ const obj = generateExpression(node.callee.object);
502
+ const prop = t4.isIdentifier(node.callee.property) ? node.callee.property.name : "[method]";
503
+ const args = node.arguments.map((arg) => generateExpression(arg)).join(", ");
504
+ return `${obj}.${prop}(${args})`;
505
+ }
506
+ if (t4.isMemberExpression(node)) {
507
+ const obj = generateExpression(node.object);
508
+ if (node.computed) {
509
+ const prop2 = generateExpression(node.property);
510
+ return `${obj}[${prop2}]`;
511
+ }
512
+ const prop = t4.isIdentifier(node.property) ? node.property.name : "[property]";
513
+ return `${obj}.${prop}`;
514
+ }
515
+ if (t4.isTemplateLiteral(node)) {
516
+ return node.quasis.map((q, i) => {
517
+ const raw = q.value.raw;
518
+ if (i < node.expressions.length) return raw + "${" + generateExpression(node.expressions[i]) + "}";
519
+ return raw;
520
+ }).join("");
521
+ }
522
+ if (t4.isArrayExpression(node)) {
523
+ const elements = node.elements.map((el) => el ? generateExpression(el) : "undefined").join(", ");
524
+ return `[${elements}]`;
525
+ }
526
+ if (t4.isObjectExpression(node)) {
527
+ const props = node.properties.map((p) => {
528
+ if (t4.isObjectProperty(p)) {
529
+ const key = t4.isIdentifier(p.key) ? p.key.name : t4.isStringLiteral(p.key) ? `"${p.key.value}"` : "[key]";
530
+ if (p.shorthand && t4.isIdentifier(p.value)) return key;
531
+ return `${key}: ${generateExpression(p.value)}`;
532
+ }
533
+ if (t4.isSpreadElement(p)) return `...${generateExpression(p.argument)}`;
534
+ return "";
535
+ }).filter(Boolean).join(", ");
536
+ return `{ ${props} }`;
537
+ }
538
+ if (t4.isTSAsExpression(node)) return generateExpression(node.expression);
539
+ if (t4.isTSNonNullExpression(node)) return generateExpression(node.expression);
540
+ return "[Expression]";
541
+ }
542
+ function ensureState2(states, stateName) {
543
+ if (!states.has(stateName)) {
544
+ states.set(stateName, {
545
+ name: stateName,
546
+ type: "REGULAR",
547
+ on_enter: [],
548
+ during: [],
549
+ on_exit: []
550
+ });
551
+ }
552
+ return states.get(stateName);
553
+ }
554
+ function extractEvents(path, state) {
555
+ const callee = path.node.callee;
556
+ if (!t4.isIdentifier(callee) || callee.name !== "useOnEvent") return;
557
+ const args = path.node.arguments;
558
+ if (args.length < 2) return;
559
+ const patternArg = args[0];
560
+ const callbackArg = args[1];
561
+ const optionsArg = args.length > 2 ? args[2] : null;
562
+ if (!t4.isStringLiteral(patternArg)) return;
563
+ const pattern = patternArg.value;
564
+ let callbackBody = [];
565
+ if (t4.isArrowFunctionExpression(callbackArg) || t4.isFunctionExpression(callbackArg)) {
566
+ const body = callbackArg.body;
567
+ if (t4.isBlockStatement(body)) {
568
+ callbackBody = body.body;
569
+ } else if (t4.isExpression(body)) {
570
+ callbackBody = [t4.expressionStatement(body)];
571
+ }
572
+ }
573
+ let stateName = null;
574
+ if (optionsArg && t4.isObjectExpression(optionsArg)) {
575
+ const whileProp = optionsArg.properties.find(
576
+ (prop) => t4.isObjectProperty(prop) && t4.isIdentifier(prop.key) && prop.key.name === "while"
577
+ );
578
+ if (whileProp && t4.isObjectProperty(whileProp) && t4.isStringLiteral(whileProp.value)) {
579
+ stateName = whileProp.value.value;
580
+ }
581
+ }
582
+ const actions = analyzeEventCallback(callbackBody);
583
+ const subscription = {
584
+ match: pattern,
585
+ conditions: [],
586
+ actions
587
+ };
588
+ const compilerState = state;
589
+ if (!compilerState.states) compilerState.states = /* @__PURE__ */ new Map();
590
+ if (stateName) {
591
+ const stateDefinition = ensureState2(compilerState.states, stateName);
592
+ if (!stateDefinition.on_event) stateDefinition.on_event = [];
593
+ stateDefinition.on_event.push(subscription);
594
+ } else {
595
+ if (!compilerState.events) compilerState.events = [];
596
+ compilerState.events.push(subscription);
597
+ }
598
+ }
599
+
600
+ // src/babel/extractors/component-extractor.ts
601
+ import * as t6 from "@babel/types";
602
+
603
+ // src/babel/transpilers/ts-to-expression.ts
604
+ import * as t5 from "@babel/types";
605
+ function transpileExpression(node, options = {}) {
606
+ const ctx = new TranspileContext(options);
607
+ const expr = ctx.visit(node);
608
+ return { expression: expr, pure: ctx.isPure };
609
+ }
610
+ function transpileBlock(node, options = {}) {
611
+ const ctx = new TranspileContext(options);
612
+ const lines = visitStatements(node.body, ctx);
613
+ return { expression: lines, pure: ctx.isPure };
614
+ }
615
+ function visitStatements(stmts, ctx) {
616
+ return stmts.map((s) => visitStatement(s, ctx)).join("\n");
617
+ }
618
+ function visitStatement(stmt, ctx) {
619
+ if (t5.isVariableDeclaration(stmt)) {
620
+ const keyword = stmt.kind === "const" ? "const" : stmt.kind === "let" ? "let" : "let";
621
+ const decls = stmt.declarations.map((d) => {
622
+ const init = d.init ? ctx.visit(d.init) : "null";
623
+ const lhs = declaratorPattern(d.id);
624
+ return `${keyword} ${lhs} = ${init}`;
625
+ });
626
+ return decls.join(";\n") + ";";
627
+ }
628
+ if (t5.isIfStatement(stmt)) {
629
+ const test = ctx.visit(stmt.test);
630
+ const consequent = visitBranchBody(stmt.consequent, ctx);
631
+ if (stmt.alternate) {
632
+ const alternate = visitBranchBody(stmt.alternate, ctx);
633
+ return `if (${test}) ${consequent} else ${alternate}`;
634
+ }
635
+ return `if (${test}) ${consequent}`;
636
+ }
637
+ if (t5.isForOfStatement(stmt)) {
638
+ const right = ctx.visit(stmt.right);
639
+ const varName = forOfPattern(stmt.left);
640
+ const body = visitBranchBody(stmt.body, ctx);
641
+ return `for (const ${varName} of ${right}) ${body}`;
642
+ }
643
+ if (t5.isWhileStatement(stmt)) {
644
+ const test = ctx.visit(stmt.test);
645
+ const body = visitBranchBody(stmt.body, ctx);
646
+ return `while (${test}) ${body}`;
647
+ }
648
+ if (t5.isReturnStatement(stmt)) {
649
+ if (!stmt.argument) return "return;";
650
+ return `return ${ctx.visit(stmt.argument)};`;
651
+ }
652
+ if (t5.isExpressionStatement(stmt)) {
653
+ return ctx.visit(stmt.expression) + ";";
654
+ }
655
+ if (t5.isBlockStatement(stmt)) {
656
+ const inner = visitStatements(stmt.body, ctx);
657
+ return `{
658
+ ${indent(inner)}
659
+ }`;
660
+ }
661
+ if (t5.isFunctionDeclaration(stmt) && stmt.id) {
662
+ const name = stmt.id.name;
663
+ const params = stmt.params.map((p) => t5.isIdentifier(p) ? p.name : "_").join(", ");
664
+ const body = visitStatements(stmt.body.body, ctx);
665
+ return `function ${name}(${params}) {
666
+ ${indent(body)}
667
+ }`;
668
+ }
669
+ return ctx.opaque(`[${stmt.type}]`) + ";";
670
+ }
671
+ function visitBranchBody(node, ctx) {
672
+ if (t5.isBlockStatement(node)) {
673
+ const inner = visitStatements(node.body, ctx);
674
+ return `{
675
+ ${indent(inner)}
676
+ }`;
677
+ }
678
+ return visitStatement(node, ctx);
679
+ }
680
+ function declaratorPattern(node) {
681
+ if (t5.isIdentifier(node)) return node.name;
682
+ if (t5.isObjectPattern(node)) {
683
+ const props = node.properties.map((p) => {
684
+ if (t5.isRestElement(p)) {
685
+ return `...${t5.isIdentifier(p.argument) ? p.argument.name : "_"}`;
686
+ }
687
+ if (t5.isObjectProperty(p)) {
688
+ const key = t5.isIdentifier(p.key) ? p.key.name : "_";
689
+ const val = t5.isIdentifier(p.value) ? p.value.name : t5.isAssignmentPattern(p.value) && t5.isIdentifier(p.value.left) ? p.value.left.name : "_";
690
+ return key === val ? key : `${key}: ${val}`;
691
+ }
692
+ return "_";
693
+ });
694
+ return `{ ${props.join(", ")} }`;
695
+ }
696
+ if (t5.isArrayPattern(node)) {
697
+ const elems = node.elements.map(
698
+ (e) => e === null ? "" : t5.isIdentifier(e) ? e.name : "_"
699
+ );
700
+ return `[${elems.join(", ")}]`;
701
+ }
702
+ return "_";
703
+ }
704
+ function forOfPattern(left) {
705
+ if (t5.isVariableDeclaration(left)) {
706
+ const d = left.declarations[0];
707
+ if (d) return declaratorPattern(d.id);
708
+ }
709
+ if (t5.isIdentifier(left)) return left.name;
710
+ return "_";
711
+ }
712
+ function indent(code) {
713
+ return code.split("\n").map((l) => " " + l).join("\n");
714
+ }
715
+ var TranspileContext = class {
716
+ constructor(options) {
717
+ this.isPure = true;
718
+ this.localFieldMap = options.localFieldMap ?? /* @__PURE__ */ new Map();
719
+ this.derivedVarMap = options.derivedVarMap ?? /* @__PURE__ */ new Map();
720
+ this.setterToFieldMap = options.setterToFieldMap ?? /* @__PURE__ */ new Map();
721
+ }
722
+ /**
723
+ * Emit an opaque JS fallback, marking the result as impure.
724
+ */
725
+ opaque(jsCode) {
726
+ this.isPure = false;
727
+ return `$expr(${jsCode})`;
728
+ }
729
+ /**
730
+ * Visit an AST node and produce an mm-compute expression string.
731
+ */
732
+ visit(node) {
733
+ if (t5.isStringLiteral(node)) return `"${node.value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
734
+ if (t5.isNumericLiteral(node)) return String(node.value);
735
+ if (t5.isBooleanLiteral(node)) return String(node.value);
736
+ if (t5.isNullLiteral(node)) return "null";
737
+ if (t5.isIdentifier(node)) {
738
+ const snakeName = this.localFieldMap.get(node.name);
739
+ if (snakeName) return `$local.${snakeName}`;
740
+ const derivedInit = this.derivedVarMap.get(node.name);
741
+ if (derivedInit) return `(${this.visit(derivedInit)})`;
742
+ if (node.name === "undefined") return "null";
743
+ if (node.name === "NaN") return "null";
744
+ if (node.name === "Infinity") return "null";
745
+ return node.name;
746
+ }
747
+ if (t5.isTSAsExpression(node)) return this.visit(node.expression);
748
+ if (t5.isTSNonNullExpression(node)) return this.visit(node.expression);
749
+ if (t5.isTSTypeAssertion(node)) return this.visit(node.expression);
750
+ if (t5.isParenthesizedExpression(node)) return `(${this.visit(node.expression)})`;
751
+ if (t5.isUnaryExpression(node) && node.prefix) {
752
+ return this.visitUnary(node);
753
+ }
754
+ if (t5.isBinaryExpression(node)) {
755
+ return this.visitBinary(node);
756
+ }
757
+ if (t5.isLogicalExpression(node)) {
758
+ return this.visitLogical(node);
759
+ }
760
+ if (t5.isConditionalExpression(node)) {
761
+ return `if(${this.visit(node.test)}, ${this.visit(node.consequent)}, ${this.visit(node.alternate)})`;
762
+ }
763
+ if (t5.isMemberExpression(node) || t5.isOptionalMemberExpression(node)) {
764
+ return this.visitMember(node);
765
+ }
766
+ if (t5.isCallExpression(node) || t5.isOptionalCallExpression(node)) {
767
+ return this.visitCall(node);
768
+ }
769
+ if (t5.isTemplateLiteral(node)) {
770
+ return this.visitTemplateLiteral(node);
771
+ }
772
+ if (t5.isArrayExpression(node)) {
773
+ const elements = node.elements.map((el) => {
774
+ if (el === null) return "null";
775
+ if (t5.isSpreadElement(el)) return this.opaque(`...${this.visit(el.argument)}`);
776
+ return t5.isExpression(el) ? this.visit(el) : "null";
777
+ });
778
+ return `[${elements.join(", ")}]`;
779
+ }
780
+ if (t5.isObjectExpression(node)) {
781
+ return this.visitObject(node);
782
+ }
783
+ if (t5.isArrowFunctionExpression(node)) {
784
+ return this.visitArrow(node);
785
+ }
786
+ if (t5.isNewExpression(node)) {
787
+ return this.visitNew(node);
788
+ }
789
+ return this.opaque(fallbackGenerate(node));
790
+ }
791
+ // ---------------------------------------------------------------------------
792
+ // Unary
793
+ // ---------------------------------------------------------------------------
794
+ visitUnary(node) {
795
+ if (node.operator === "!") {
796
+ return `not(${this.visit(node.argument)})`;
797
+ }
798
+ if (node.operator === "-") {
799
+ if (t5.isNumericLiteral(node.argument)) return String(-node.argument.value);
800
+ return `(0 - ${this.visit(node.argument)})`;
801
+ }
802
+ if (node.operator === "typeof") {
803
+ return `typeof(${this.visit(node.argument)})`;
804
+ }
805
+ return this.opaque(`${node.operator}${fallbackGenerate(node.argument)}`);
806
+ }
807
+ // ---------------------------------------------------------------------------
808
+ // Binary
809
+ // ---------------------------------------------------------------------------
810
+ visitBinary(node) {
811
+ const left = this.visit(node.left);
812
+ const right = this.visit(node.right);
813
+ switch (node.operator) {
814
+ case "===":
815
+ return `(${left} == ${right})`;
816
+ case "!==":
817
+ return `(${left} != ${right})`;
818
+ case "==":
819
+ return `(${left} == ${right})`;
820
+ case "!=":
821
+ return `(${left} != ${right})`;
822
+ case "<":
823
+ return `(${left} < ${right})`;
824
+ case ">":
825
+ return `(${left} > ${right})`;
826
+ case "<=":
827
+ return `(${left} <= ${right})`;
828
+ case ">=":
829
+ return `(${left} >= ${right})`;
830
+ case "+":
831
+ return `(${left} + ${right})`;
832
+ case "-":
833
+ return `(${left} - ${right})`;
834
+ case "*":
835
+ return `(${left} * ${right})`;
836
+ case "/":
837
+ return `(${left} / ${right})`;
838
+ case "%":
839
+ return `(${left} % ${right})`;
840
+ default:
841
+ return this.opaque(`(${left} ${node.operator} ${right})`);
842
+ }
843
+ }
844
+ // ---------------------------------------------------------------------------
845
+ // Logical
846
+ // ---------------------------------------------------------------------------
847
+ visitLogical(node) {
848
+ const left = this.visit(node.left);
849
+ const right = this.visit(node.right);
850
+ switch (node.operator) {
851
+ case "&&":
852
+ return `(${left} && ${right})`;
853
+ case "||":
854
+ return `(${left} || ${right})`;
855
+ case "??":
856
+ return `coalesce(${left}, ${right})`;
857
+ default:
858
+ return this.opaque(`(${left} ${node.operator} ${right})`);
859
+ }
860
+ }
861
+ // ---------------------------------------------------------------------------
862
+ // Member expression
863
+ // ---------------------------------------------------------------------------
864
+ visitMember(node) {
865
+ const isOptional = node.optional === true;
866
+ if (!node.computed && t5.isIdentifier(node.property) && node.property.name === "length") {
867
+ const obj2 = this.visit(node.object);
868
+ if (isOptional) {
869
+ return `if(${obj2} != null, length(${obj2}), null)`;
870
+ }
871
+ return `length(${obj2})`;
872
+ }
873
+ const obj = this.visit(node.object);
874
+ if (node.computed) {
875
+ const prop2 = t5.isExpression(node.property) ? this.visit(node.property) : "0";
876
+ if (isOptional) {
877
+ return `if(${obj} != null, get(${obj}, ${prop2}), null)`;
878
+ }
879
+ return `${obj}[${prop2}]`;
880
+ }
881
+ const prop = t5.isIdentifier(node.property) ? node.property.name : "[property]";
882
+ if (isOptional) {
883
+ return `if(${obj} != null, ${obj}.${prop}, null)`;
884
+ }
885
+ return `${obj}.${prop}`;
886
+ }
887
+ // ---------------------------------------------------------------------------
888
+ // Call expression
889
+ // ---------------------------------------------------------------------------
890
+ visitCall(node) {
891
+ if (t5.isIdentifier(node.callee)) {
892
+ const setterField = this.setterToFieldMap.get(node.callee.name);
893
+ if (setterField) {
894
+ return this.visitSetterCall(node, setterField);
895
+ }
896
+ const args = this.visitArgs(node.arguments);
897
+ return `${node.callee.name}(${args})`;
898
+ }
899
+ if (t5.isMemberExpression(node.callee) || t5.isOptionalMemberExpression(node.callee)) {
900
+ const methodName = t5.isIdentifier(node.callee.property) ? node.callee.property.name : null;
901
+ if (!methodName) {
902
+ return this.opaque(fallbackGenerate(node));
903
+ }
904
+ if (t5.isIdentifier(node.callee.object) && node.callee.object.name === "Math") {
905
+ return this.visitMathCall(methodName, node.arguments);
906
+ }
907
+ if (t5.isIdentifier(node.callee.object) && node.callee.object.name === "JSON") {
908
+ return this.visitJsonCall(methodName, node.arguments);
909
+ }
910
+ if (t5.isIdentifier(node.callee.object) && node.callee.object.name === "Date") {
911
+ if (methodName === "now") return "now_ms()";
912
+ }
913
+ const obj = this.visit(node.callee.object);
914
+ return this.visitMethodCall(obj, methodName, node.arguments);
915
+ }
916
+ return this.opaque(fallbackGenerate(node));
917
+ }
918
+ // ---------------------------------------------------------------------------
919
+ // Method call transpilation (obj.method(args))
920
+ // ---------------------------------------------------------------------------
921
+ visitMethodCall(obj, method, args) {
922
+ switch (method) {
923
+ // --- String methods ---
924
+ case "toLowerCase":
925
+ return `lower(${obj})`;
926
+ case "toUpperCase":
927
+ return `upper(${obj})`;
928
+ case "trim":
929
+ return `trim(${obj})`;
930
+ case "startsWith": {
931
+ const [arg] = this.visitArgsList(args);
932
+ return `starts_with(${obj}, ${arg})`;
933
+ }
934
+ case "endsWith": {
935
+ const [arg] = this.visitArgsList(args);
936
+ return `ends_with(${obj}, ${arg})`;
937
+ }
938
+ case "includes": {
939
+ const [arg] = this.visitArgsList(args);
940
+ return `contains(${obj}, ${arg})`;
941
+ }
942
+ case "split": {
943
+ const [sep] = this.visitArgsList(args);
944
+ return `split(${obj}, ${sep})`;
945
+ }
946
+ case "join": {
947
+ const [sep] = this.visitArgsList(args);
948
+ return `join(${obj}, ${sep})`;
949
+ }
950
+ case "replace": {
951
+ const visited = this.visitArgsList(args);
952
+ return `replace(${obj}, ${visited[0]}, ${visited[1]})`;
953
+ }
954
+ case "substring":
955
+ case "slice": {
956
+ const visited = this.visitArgsList(args);
957
+ if (visited.length === 1) return `substring(${obj}, ${visited[0]})`;
958
+ return `substring(${obj}, ${visited[0]}, ${visited[1]})`;
959
+ }
960
+ case "charAt": {
961
+ const [idx] = this.visitArgsList(args);
962
+ return `substring(${obj}, ${idx}, (${idx} + 1))`;
963
+ }
964
+ case "indexOf": {
965
+ const [arg] = this.visitArgsList(args);
966
+ return `indexof(${obj}, ${arg})`;
967
+ }
968
+ case "toString":
969
+ return `to_string(${obj})`;
970
+ case "toISOString":
971
+ return "now()";
972
+ case "concat": {
973
+ const visited = this.visitArgsList(args);
974
+ return `concat(${obj}, ${visited.join(", ")})`;
975
+ }
976
+ // --- Array higher-order methods ---
977
+ case "filter":
978
+ return this.visitArrayHigherOrder("filter", obj, args);
979
+ case "map":
980
+ return this.visitArrayHigherOrder("map", obj, args);
981
+ case "find":
982
+ return this.visitArrayHigherOrder("find", obj, args);
983
+ case "reduce":
984
+ return this.visitArrayReduce(obj, args);
985
+ case "some":
986
+ return this.visitArrayHigherOrder("some", obj, args);
987
+ case "every":
988
+ return this.visitArrayHigherOrder("every", obj, args);
989
+ // --- Array mutation-free methods ---
990
+ case "reverse":
991
+ return `reverse(${obj})`;
992
+ case "sort":
993
+ return `sort(${obj})`;
994
+ case "flat":
995
+ return `flat(${obj})`;
996
+ // --- JSON methods on values ---
997
+ case "stringify":
998
+ return `to_string(${obj})`;
999
+ default:
1000
+ return this.opaque(`${obj}.${method}(${this.visitArgs(args)})`);
1001
+ }
1002
+ }
1003
+ // ---------------------------------------------------------------------------
1004
+ // Array higher-order method transpilation
1005
+ // ---------------------------------------------------------------------------
1006
+ visitArrayHigherOrder(fn, obj, args) {
1007
+ const firstArg = args[0];
1008
+ if (!firstArg || !t5.isExpression(firstArg)) {
1009
+ return this.opaque(`${obj}.${fn}()`);
1010
+ }
1011
+ if (t5.isArrowFunctionExpression(firstArg) && t5.isExpression(firstArg.body)) {
1012
+ const paramName = firstArg.params.length > 0 && t5.isIdentifier(firstArg.params[0]) ? firstArg.params[0].name : "_";
1013
+ const lambdaBody = this.visit(firstArg.body);
1014
+ return `${fn}(${obj}, ${paramName} => ${lambdaBody})`;
1015
+ }
1016
+ const argStr = this.visit(firstArg);
1017
+ return `${fn}(${obj}, ${argStr})`;
1018
+ }
1019
+ visitArrayReduce(obj, args) {
1020
+ const [callbackArg, initArg] = args;
1021
+ if (!callbackArg || !t5.isExpression(callbackArg)) {
1022
+ return this.opaque(`${obj}.reduce()`);
1023
+ }
1024
+ if (t5.isArrowFunctionExpression(callbackArg) && t5.isExpression(callbackArg.body)) {
1025
+ const params = callbackArg.params;
1026
+ const accName = params.length > 0 && t5.isIdentifier(params[0]) ? params[0].name : "acc";
1027
+ const itemName = params.length > 1 && t5.isIdentifier(params[1]) ? params[1].name : "x";
1028
+ const lambdaBody = this.visit(callbackArg.body);
1029
+ const initStr = initArg && t5.isExpression(initArg) ? this.visit(initArg) : "0";
1030
+ return `reduce(${obj}, (${accName}, ${itemName}) => ${lambdaBody}, ${initStr})`;
1031
+ }
1032
+ return this.opaque(fallbackGenerate({ type: "CallExpression" }));
1033
+ }
1034
+ // ---------------------------------------------------------------------------
1035
+ // Math.* methods
1036
+ // ---------------------------------------------------------------------------
1037
+ visitMathCall(method, args) {
1038
+ const visited = this.visitArgsList(args);
1039
+ switch (method) {
1040
+ case "max":
1041
+ return `max(${visited.join(", ")})`;
1042
+ case "min":
1043
+ return `min(${visited.join(", ")})`;
1044
+ case "round":
1045
+ return `round(${visited.join(", ")})`;
1046
+ case "floor":
1047
+ return `floor(${visited.join(", ")})`;
1048
+ case "ceil":
1049
+ return `ceil(${visited.join(", ")})`;
1050
+ case "abs":
1051
+ return `abs(${visited.join(", ")})`;
1052
+ case "pow":
1053
+ return `power(${visited.join(", ")})`;
1054
+ case "sqrt":
1055
+ return `sqrt(${visited.join(", ")})`;
1056
+ case "log":
1057
+ return `log(${visited.join(", ")})`;
1058
+ case "log10":
1059
+ return `log10(${visited.join(", ")})`;
1060
+ case "sign":
1061
+ return `sign(${visited.join(", ")})`;
1062
+ default:
1063
+ return this.opaque(`Math.${method}(${visited.join(", ")})`);
1064
+ }
1065
+ }
1066
+ // ---------------------------------------------------------------------------
1067
+ // JSON.* methods
1068
+ // ---------------------------------------------------------------------------
1069
+ visitJsonCall(method, args) {
1070
+ const visited = this.visitArgsList(args);
1071
+ switch (method) {
1072
+ case "stringify":
1073
+ return `to_string(${visited[0]})`;
1074
+ case "parse":
1075
+ return `from_json(${visited[0]})`;
1076
+ default:
1077
+ return this.opaque(`JSON.${method}(${visited.join(", ")})`);
1078
+ }
1079
+ }
1080
+ // ---------------------------------------------------------------------------
1081
+ // Template literals
1082
+ // ---------------------------------------------------------------------------
1083
+ visitTemplateLiteral(node) {
1084
+ const parts = [];
1085
+ for (let i = 0; i < node.quasis.length; i++) {
1086
+ const raw = node.quasis[i].value.cooked ?? node.quasis[i].value.raw;
1087
+ if (raw) {
1088
+ parts.push(`"${raw.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`);
1089
+ }
1090
+ if (i < node.expressions.length) {
1091
+ const exprStr = this.visit(node.expressions[i]);
1092
+ parts.push(`to_string(${exprStr})`);
1093
+ }
1094
+ }
1095
+ const nonEmpty = parts.filter((p) => p !== '""');
1096
+ if (nonEmpty.length === 0) return '""';
1097
+ if (nonEmpty.length === 1) return nonEmpty[0];
1098
+ return `concat(${nonEmpty.join(", ")})`;
1099
+ }
1100
+ // ---------------------------------------------------------------------------
1101
+ // Object expression
1102
+ // ---------------------------------------------------------------------------
1103
+ visitObject(node) {
1104
+ const props = node.properties.map((prop) => {
1105
+ if (t5.isSpreadElement(prop)) {
1106
+ return this.opaque(`...${this.visit(prop.argument)}`);
1107
+ }
1108
+ if (t5.isObjectProperty(prop)) {
1109
+ const key = t5.isIdentifier(prop.key) ? `"${prop.key.name}"` : t5.isStringLiteral(prop.key) ? `"${prop.key.value}"` : t5.isNumericLiteral(prop.key) ? `"${prop.key.value}"` : '"[key]"';
1110
+ const value = t5.isExpression(prop.value) ? this.visit(prop.value) : "null";
1111
+ return `${key}: ${value}`;
1112
+ }
1113
+ return this.opaque("[method]");
1114
+ });
1115
+ return `{${props.join(", ")}}`;
1116
+ }
1117
+ // ---------------------------------------------------------------------------
1118
+ // Arrow function (lambda)
1119
+ // ---------------------------------------------------------------------------
1120
+ visitArrow(node) {
1121
+ if (t5.isExpression(node.body)) {
1122
+ const params = node.params.map((p) => {
1123
+ if (t5.isIdentifier(p)) return p.name;
1124
+ return "_";
1125
+ }).join(", ");
1126
+ const body = this.visit(node.body);
1127
+ if (node.params.length === 1) return `${params} => ${body}`;
1128
+ return `(${params}) => ${body}`;
1129
+ }
1130
+ return this.opaque(fallbackGenerate(node));
1131
+ }
1132
+ // ---------------------------------------------------------------------------
1133
+ // new expressions
1134
+ // ---------------------------------------------------------------------------
1135
+ visitNew(node) {
1136
+ if (t5.isIdentifier(node.callee) && node.callee.name === "Date" && node.arguments.length === 0) {
1137
+ return "now()";
1138
+ }
1139
+ return this.opaque(fallbackGenerate(node));
1140
+ }
1141
+ // ---------------------------------------------------------------------------
1142
+ // setState setter calls
1143
+ // ---------------------------------------------------------------------------
1144
+ visitSetterCall(node, fieldName) {
1145
+ if (node.arguments.length >= 1) {
1146
+ const arg = node.arguments[0];
1147
+ if ((t5.isArrowFunctionExpression(arg) || t5.isFunctionExpression(arg)) && arg.params.length >= 1 && t5.isIdentifier(arg.params[0])) {
1148
+ const paramName = arg.params[0].name;
1149
+ const bodyExpr = t5.isExpression(arg.body) ? this.visit(arg.body) : "null";
1150
+ const resolved = bodyExpr.replace(
1151
+ new RegExp(`(?<![.$\\w])${paramName}(?![\\w])`, "g"),
1152
+ `$local.${fieldName}`
1153
+ );
1154
+ return `$action.setLocal("${fieldName}")(${resolved})`;
1155
+ }
1156
+ const argStr = t5.isExpression(arg) ? this.visit(arg) : "null";
1157
+ return `$action.setLocal("${fieldName}")(${argStr})`;
1158
+ }
1159
+ return `$action.setLocal("${fieldName}")`;
1160
+ }
1161
+ // ---------------------------------------------------------------------------
1162
+ // Argument helpers
1163
+ // ---------------------------------------------------------------------------
1164
+ visitArgs(args) {
1165
+ return args.map((a) => {
1166
+ if (t5.isExpression(a)) return this.visit(a);
1167
+ if (t5.isSpreadElement(a)) return this.opaque(`...${this.visit(a.argument)}`);
1168
+ return "null";
1169
+ }).join(", ");
1170
+ }
1171
+ visitArgsList(args) {
1172
+ return args.map((a) => {
1173
+ if (t5.isExpression(a)) return this.visit(a);
1174
+ if (t5.isSpreadElement(a)) return this.opaque(`...${this.visit(a.argument)}`);
1175
+ return "null";
1176
+ });
1177
+ }
1178
+ };
1179
+ function fallbackGenerate(node) {
1180
+ if (t5.isIdentifier(node)) return node.name;
1181
+ if (t5.isStringLiteral(node)) return `"${node.value.replace(/"/g, '\\"')}"`;
1182
+ if (t5.isNumericLiteral(node)) return String(node.value);
1183
+ if (t5.isBooleanLiteral(node)) return String(node.value);
1184
+ if (t5.isNullLiteral(node)) return "null";
1185
+ return `[${node.type}]`;
1186
+ }
1187
+
1188
+ // src/babel/extractors/component-extractor.ts
1189
+ var nodeIdCounter = 0;
1190
+ var localFieldMap = /* @__PURE__ */ new Map();
1191
+ var setterToFieldMap = /* @__PURE__ */ new Map();
1192
+ var derivedVarMap = /* @__PURE__ */ new Map();
1193
+ function toSnakeCase(str) {
1194
+ return str.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "");
1195
+ }
1196
+ var STATIC_EVAL_FAIL = Symbol("STATIC_EVAL_FAIL");
1197
+ function tryStaticEval(node) {
1198
+ if (t6.isStringLiteral(node)) return node.value;
1199
+ if (t6.isNumericLiteral(node)) return node.value;
1200
+ if (t6.isBooleanLiteral(node)) return node.value;
1201
+ if (t6.isNullLiteral(node)) return null;
1202
+ if (t6.isIdentifier(node) && node.name === "undefined") return void 0;
1203
+ if (t6.isUnaryExpression(node) && node.operator === "-" && t6.isNumericLiteral(node.argument)) {
1204
+ return -node.argument.value;
1205
+ }
1206
+ if (t6.isArrayExpression(node)) {
1207
+ const result = [];
1208
+ for (const el of node.elements) {
1209
+ if (el === null) {
1210
+ result.push(null);
1211
+ continue;
1212
+ }
1213
+ if (t6.isSpreadElement(el)) return STATIC_EVAL_FAIL;
1214
+ const val = tryStaticEval(el);
1215
+ if (val === STATIC_EVAL_FAIL) return STATIC_EVAL_FAIL;
1216
+ result.push(val);
1217
+ }
1218
+ return result;
1219
+ }
1220
+ if (t6.isObjectExpression(node)) {
1221
+ const result = {};
1222
+ for (const prop of node.properties) {
1223
+ if (t6.isSpreadElement(prop) || t6.isObjectMethod(prop)) return STATIC_EVAL_FAIL;
1224
+ if (!t6.isObjectProperty(prop)) return STATIC_EVAL_FAIL;
1225
+ let key;
1226
+ if (t6.isIdentifier(prop.key)) key = prop.key.name;
1227
+ else if (t6.isStringLiteral(prop.key)) key = prop.key.value;
1228
+ else if (t6.isNumericLiteral(prop.key)) key = String(prop.key.value);
1229
+ else return STATIC_EVAL_FAIL;
1230
+ if (!t6.isExpression(prop.value)) return STATIC_EVAL_FAIL;
1231
+ const val = tryStaticEval(prop.value);
1232
+ if (val === STATIC_EVAL_FAIL) return STATIC_EVAL_FAIL;
1233
+ result[key] = val;
1234
+ }
1235
+ return result;
1236
+ }
1237
+ if (t6.isTemplateLiteral(node) && node.expressions.length === 0) {
1238
+ return node.quasis[0].value.cooked ?? node.quasis[0].value.raw;
1239
+ }
1240
+ return STATIC_EVAL_FAIL;
1241
+ }
1242
+ function initLocalFieldMaps(fields) {
1243
+ localFieldMap.clear();
1244
+ setterToFieldMap.clear();
1245
+ for (const field of fields) {
1246
+ const camelName = field.name;
1247
+ const snakeName = toSnakeCase(camelName);
1248
+ localFieldMap.set(camelName, snakeName);
1249
+ const setterName = "set" + camelName.charAt(0).toUpperCase() + camelName.slice(1);
1250
+ setterToFieldMap.set(setterName, snakeName);
1251
+ }
1252
+ }
1253
+ function registerDerivedVar(name, init) {
1254
+ derivedVarMap.set(name, init);
1255
+ }
1256
+ function resetNodeIdCounter() {
1257
+ nodeIdCounter = 0;
1258
+ derivedVarMap.clear();
1259
+ }
1260
+ function jsxToExperienceNode(node) {
1261
+ if (t6.isJSXFragment(node)) {
1262
+ return {
1263
+ id: `fragment_${++nodeIdCounter}`,
1264
+ children: extractChildren(node.children)
1265
+ };
1266
+ }
1267
+ const element = node.openingElement;
1268
+ const componentName = resolveComponentName(element.name);
1269
+ const id = generateNodeId(componentName);
1270
+ const config = {};
1271
+ const bindings = {};
1272
+ let visibleWhen;
1273
+ let layout;
1274
+ let slot;
1275
+ let className;
1276
+ let displayName;
1277
+ const layoutMap = {
1278
+ Stack: "stack",
1279
+ Row: "row",
1280
+ Grid: "grid",
1281
+ Tabs: "tabs",
1282
+ Column: "column"
1283
+ };
1284
+ if (layoutMap[componentName]) {
1285
+ layout = layoutMap[componentName];
1286
+ }
1287
+ for (const attr of element.attributes) {
1288
+ if (t6.isJSXAttribute(attr) && t6.isJSXIdentifier(attr.name)) {
1289
+ const attrName = attr.name.name;
1290
+ const attrValue = attr.value;
1291
+ if (attrName === "visible_when" && t6.isStringLiteral(attrValue)) {
1292
+ visibleWhen = attrValue.value;
1293
+ } else if (attrName === "data-slot" && t6.isStringLiteral(attrValue)) {
1294
+ slot = attrValue.value;
1295
+ } else if (attrName === "className" && t6.isStringLiteral(attrValue)) {
1296
+ className = attrValue.value;
1297
+ } else if (attrName === "displayName" && t6.isStringLiteral(attrValue)) {
1298
+ displayName = attrValue.value;
1299
+ } else if (t6.isJSXExpressionContainer(attrValue)) {
1300
+ const expr = attrValue.expression;
1301
+ if (t6.isJSXEmptyExpression(expr)) continue;
1302
+ if (t6.isNumericLiteral(expr)) {
1303
+ config[attrName] = expr.value;
1304
+ } else if (t6.isBooleanLiteral(expr)) {
1305
+ config[attrName] = expr.value;
1306
+ } else if (t6.isStringLiteral(expr)) {
1307
+ if (expr.value.includes("$instance") || expr.value.includes("$local") || /^[a-zA-Z_]+\(/.test(expr.value)) {
1308
+ bindings[attrName] = expr.value;
1309
+ } else {
1310
+ config[attrName] = expr.value;
1311
+ }
1312
+ } else if (t6.isIdentifier(expr)) {
1313
+ const snakeName = localFieldMap.get(expr.name);
1314
+ bindings[attrName] = snakeName ? `$local.${snakeName}` : `$instance.${expr.name}`;
1315
+ } else if (isEventHandlerProp(attrName) && (t6.isArrowFunctionExpression(expr) || t6.isFunctionExpression(expr))) {
1316
+ const decomposed = decomposeHandlerToSeq(expr);
1317
+ if (decomposed) {
1318
+ bindings[attrName] = decomposed;
1319
+ } else {
1320
+ bindings[attrName] = `$expr(${generateExpression2(expr)})`;
1321
+ }
1322
+ } else if (t6.isExpression(expr)) {
1323
+ const staticVal = tryStaticEval(expr);
1324
+ if (staticVal !== STATIC_EVAL_FAIL) {
1325
+ config[attrName] = staticVal;
1326
+ } else {
1327
+ bindings[attrName] = generateExpression2(expr);
1328
+ }
1329
+ }
1330
+ } else if (t6.isStringLiteral(attrValue)) {
1331
+ config[attrName] = attrValue.value;
1332
+ } else if (attrValue === null) {
1333
+ config[attrName] = true;
1334
+ }
1335
+ } else if (t6.isJSXSpreadAttribute(attr)) {
1336
+ if (t6.isIdentifier(attr.argument)) {
1337
+ bindings["..." + attr.argument.name] = `$instance.${attr.argument.name}`;
1338
+ }
1339
+ }
1340
+ }
1341
+ const children = extractChildren(node.children);
1342
+ const experienceNode = {
1343
+ id,
1344
+ ...displayName && { displayName },
1345
+ component: componentName,
1346
+ ...slot && { slot },
1347
+ ...layout && { layout },
1348
+ ...className && { className },
1349
+ ...Object.keys(bindings).length > 0 && { bindings },
1350
+ ...Object.keys(config).length > 0 && { config },
1351
+ ...visibleWhen && { visible_when: visibleWhen },
1352
+ ...children.length > 0 && { children }
1353
+ };
1354
+ return experienceNode;
1355
+ }
1356
+ function resolveComponentName(name) {
1357
+ if (t6.isJSXIdentifier(name)) return name.name;
1358
+ if (t6.isJSXMemberExpression(name)) {
1359
+ const obj = resolveComponentName(name.object);
1360
+ return `${obj}.${name.property.name}`;
1361
+ }
1362
+ if (t6.isJSXNamespacedName(name)) {
1363
+ return `${name.namespace.name}:${name.name.name}`;
1364
+ }
1365
+ return "div";
1366
+ }
1367
+ function extractChildren(children) {
1368
+ const nodes = [];
1369
+ for (const child of children) {
1370
+ if (t6.isJSXElement(child)) {
1371
+ nodes.push(jsxToExperienceNode(child));
1372
+ } else if (t6.isJSXFragment(child)) {
1373
+ nodes.push(jsxToExperienceNode(child));
1374
+ } else if (t6.isJSXText(child)) {
1375
+ const text = child.value.trim();
1376
+ if (text) {
1377
+ nodes.push({
1378
+ id: `text_${++nodeIdCounter}`,
1379
+ component: "Text",
1380
+ config: { value: text }
1381
+ });
1382
+ }
1383
+ } else if (t6.isJSXExpressionContainer(child)) {
1384
+ const expr = child.expression;
1385
+ if (t6.isJSXEmptyExpression(expr)) continue;
1386
+ if (t6.isConditionalExpression(expr)) {
1387
+ const condExpr = generateExpression2(expr.test);
1388
+ if (t6.isJSXElement(expr.consequent) || t6.isJSXFragment(expr.consequent)) {
1389
+ const consequent = jsxToExperienceNode(expr.consequent);
1390
+ consequent.visible_when = condExpr;
1391
+ nodes.push(consequent);
1392
+ }
1393
+ if (t6.isJSXElement(expr.alternate) || t6.isJSXFragment(expr.alternate)) {
1394
+ const alternate = jsxToExperienceNode(expr.alternate);
1395
+ alternate.visible_when = `not(${condExpr})`;
1396
+ nodes.push(alternate);
1397
+ }
1398
+ } else if (t6.isLogicalExpression(expr) && expr.operator === "&&") {
1399
+ const condExpr = generateExpression2(expr.left);
1400
+ if (t6.isJSXElement(expr.right) || t6.isJSXFragment(expr.right)) {
1401
+ const element = jsxToExperienceNode(expr.right);
1402
+ element.visible_when = condExpr;
1403
+ nodes.push(element);
1404
+ }
1405
+ } else if (t6.isCallExpression(expr) && t6.isMemberExpression(expr.callee) && t6.isIdentifier(expr.callee.property) && expr.callee.property.name === "map" && expr.arguments.length > 0) {
1406
+ const listSource = generateExpression2(expr.callee.object);
1407
+ const mapFn = expr.arguments[0];
1408
+ let itemTemplate = null;
1409
+ let itemAlias = "item";
1410
+ if (t6.isArrowFunctionExpression(mapFn) || t6.isFunctionExpression(mapFn)) {
1411
+ if (mapFn.params.length > 0 && t6.isIdentifier(mapFn.params[0])) {
1412
+ itemAlias = mapFn.params[0].name;
1413
+ }
1414
+ const body = mapFn.body;
1415
+ if (t6.isJSXElement(body) || t6.isJSXFragment(body)) {
1416
+ itemTemplate = jsxToExperienceNode(body);
1417
+ } else if (t6.isBlockStatement(body)) {
1418
+ for (const stmt of body.body) {
1419
+ if (t6.isReturnStatement(stmt) && stmt.argument) {
1420
+ if (t6.isJSXElement(stmt.argument) || t6.isJSXFragment(stmt.argument)) {
1421
+ itemTemplate = jsxToExperienceNode(stmt.argument);
1422
+ }
1423
+ break;
1424
+ }
1425
+ }
1426
+ }
1427
+ }
1428
+ nodes.push({
1429
+ id: `each_${++nodeIdCounter}`,
1430
+ component: "Each",
1431
+ bindings: { items: listSource.startsWith("$") ? listSource : `$instance.${listSource}` },
1432
+ config: { as: itemAlias },
1433
+ ...itemTemplate ? { children: [itemTemplate] } : {}
1434
+ });
1435
+ } else if ((t6.isArrowFunctionExpression(expr) || t6.isFunctionExpression(expr)) && extractJSXFromCallback(expr)) {
1436
+ const jsxNode = extractJSXFromCallback(expr);
1437
+ nodes.push(jsxToExperienceNode(jsxNode));
1438
+ } else if (t6.isStringLiteral(expr)) {
1439
+ nodes.push({
1440
+ id: `text_${++nodeIdCounter}`,
1441
+ component: "Text",
1442
+ config: { value: expr.value }
1443
+ });
1444
+ } else if (t6.isIdentifier(expr)) {
1445
+ const snakeName = localFieldMap.get(expr.name);
1446
+ const bindingPath = snakeName ? `$local.${snakeName}` : `$instance.${expr.name}`;
1447
+ nodes.push({
1448
+ id: `text_${++nodeIdCounter}`,
1449
+ component: "Text",
1450
+ bindings: { value: bindingPath }
1451
+ });
1452
+ } else if (t6.isExpression(expr)) {
1453
+ const exprStr = generateExpression2(expr);
1454
+ if (exprStr !== "[Expression]") {
1455
+ nodes.push({
1456
+ id: `text_${++nodeIdCounter}`,
1457
+ component: "Text",
1458
+ bindings: { value: exprStr }
1459
+ });
1460
+ }
1461
+ }
1462
+ }
1463
+ }
1464
+ return nodes;
1465
+ }
1466
+ function generateNodeId(componentName) {
1467
+ const id = componentName.replace(/\./g, "-").replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
1468
+ return `${id}_${++nodeIdCounter}`;
1469
+ }
1470
+ function generateExpression2(node) {
1471
+ if (t6.isIdentifier(node)) {
1472
+ const snakeName = localFieldMap.get(node.name);
1473
+ if (snakeName) return `$local.${snakeName}`;
1474
+ const derivedInit = derivedVarMap.get(node.name);
1475
+ if (derivedInit) return `(${generateExpression2(derivedInit)})`;
1476
+ return node.name;
1477
+ }
1478
+ if (t6.isStringLiteral(node)) return `"${node.value.replace(/"/g, '\\"')}"`;
1479
+ if (t6.isNumericLiteral(node)) return String(node.value);
1480
+ if (t6.isBooleanLiteral(node)) return String(node.value);
1481
+ if (t6.isNullLiteral(node)) return "null";
1482
+ if (t6.isMemberExpression(node) || t6.isOptionalMemberExpression(node)) {
1483
+ const obj = t6.isExpression(node.object) ? generateExpression2(node.object) : "[object]";
1484
+ const optional = node.optional ? "?." : ".";
1485
+ if (node.computed) {
1486
+ const prop2 = t6.isExpression(node.property) ? generateExpression2(node.property) : "0";
1487
+ return `${obj}[${prop2}]`;
1488
+ }
1489
+ const prop = t6.isIdentifier(node.property) ? node.property.name : "[property]";
1490
+ return `${obj}${optional}${prop}`;
1491
+ }
1492
+ if (t6.isBinaryExpression(node)) {
1493
+ return `(${generateExpression2(node.left)} ${node.operator} ${generateExpression2(node.right)})`;
1494
+ }
1495
+ if (t6.isLogicalExpression(node)) {
1496
+ return `(${generateExpression2(node.left)} ${node.operator} ${generateExpression2(node.right)})`;
1497
+ }
1498
+ if (t6.isConditionalExpression(node)) {
1499
+ return `(${generateExpression2(node.test)} ? ${generateExpression2(node.consequent)} : ${generateExpression2(node.alternate)})`;
1500
+ }
1501
+ if (t6.isUnaryExpression(node) && node.prefix) {
1502
+ const space = node.operator === "typeof" || node.operator === "void" || node.operator === "delete" ? " " : "";
1503
+ return `${node.operator}${space}${generateExpression2(node.argument)}`;
1504
+ }
1505
+ if (t6.isCallExpression(node) || t6.isOptionalCallExpression(node)) {
1506
+ if (t6.isIdentifier(node.callee)) {
1507
+ const setterFieldName = setterToFieldMap.get(node.callee.name);
1508
+ if (setterFieldName) {
1509
+ if (node.arguments.length >= 1) {
1510
+ const arg = node.arguments[0];
1511
+ if ((t6.isArrowFunctionExpression(arg) || t6.isFunctionExpression(arg)) && arg.params.length >= 1 && t6.isIdentifier(arg.params[0])) {
1512
+ const paramName = arg.params[0].name;
1513
+ const bodyExpr = t6.isExpression(arg.body) ? generateExpression2(arg.body) : "undefined";
1514
+ const resolved = bodyExpr.replace(
1515
+ new RegExp(`(?<![.$\\w])${paramName}(?![\\w])`, "g"),
1516
+ `$local.${setterFieldName}`
1517
+ );
1518
+ return `$action.setLocal("${setterFieldName}")(${resolved})`;
1519
+ }
1520
+ const argStr = t6.isExpression(arg) ? generateExpression2(arg) : "undefined";
1521
+ return `$action.setLocal("${setterFieldName}")(${argStr})`;
1522
+ }
1523
+ return `$action.setLocal("${setterFieldName}")`;
1524
+ }
1525
+ const args = node.arguments.map((a) => t6.isExpression(a) ? generateExpression2(a) : t6.isSpreadElement(a) ? `...${generateExpression2(a.argument)}` : "undefined").join(", ");
1526
+ return `${node.callee.name}(${args})`;
1527
+ }
1528
+ if (t6.isMemberExpression(node.callee) || t6.isOptionalMemberExpression(node.callee)) {
1529
+ const mmResult = transpileExpression(node, {
1530
+ localFieldMap,
1531
+ derivedVarMap,
1532
+ setterToFieldMap
1533
+ });
1534
+ if (mmResult.pure) return mmResult.expression;
1535
+ const obj = generateExpression2(node.callee.object);
1536
+ const optional = node.callee.optional ? "?." : ".";
1537
+ const prop = t6.isIdentifier(node.callee.property) ? node.callee.property.name : "[method]";
1538
+ const args = node.arguments.map((a) => t6.isExpression(a) ? generateExpression2(a) : t6.isSpreadElement(a) ? `...${generateExpression2(a.argument)}` : "undefined").join(", ");
1539
+ return `${obj}${optional}${prop}(${args})`;
1540
+ }
1541
+ if (t6.isExpression(node.callee)) {
1542
+ const callee = generateExpression2(node.callee);
1543
+ const args = node.arguments.map((a) => t6.isExpression(a) ? generateExpression2(a) : "undefined").join(", ");
1544
+ return `(${callee})(${args})`;
1545
+ }
1546
+ }
1547
+ if (t6.isTemplateLiteral(node)) {
1548
+ const parts = node.quasis.map((q, i) => {
1549
+ const raw = q.value.raw;
1550
+ if (i < node.expressions.length) {
1551
+ return raw + "${" + generateExpression2(node.expressions[i]) + "}";
1552
+ }
1553
+ return raw;
1554
+ }).join("");
1555
+ return "`" + parts + "`";
1556
+ }
1557
+ if (t6.isArrowFunctionExpression(node)) {
1558
+ const params = node.params.map((p) => {
1559
+ if (t6.isIdentifier(p)) return p.name;
1560
+ if (t6.isRestElement(p) && t6.isIdentifier(p.argument)) return `...${p.argument.name}`;
1561
+ if (t6.isAssignmentPattern(p) && t6.isIdentifier(p.left)) {
1562
+ return `${p.left.name} = ${t6.isExpression(p.right) ? generateExpression2(p.right) : "undefined"}`;
1563
+ }
1564
+ return "_";
1565
+ }).join(", ");
1566
+ if (t6.isExpression(node.body)) {
1567
+ return `(${params}) => ${generateExpression2(node.body)}`;
1568
+ }
1569
+ if (t6.isBlockStatement(node.body)) {
1570
+ const bodyStr = generateBlockStatement(node.body);
1571
+ return `(${params}) => { ${bodyStr} }`;
1572
+ }
1573
+ }
1574
+ if (t6.isArrayExpression(node)) {
1575
+ const elements = node.elements.map((el) => {
1576
+ if (el === null) return "undefined";
1577
+ if (t6.isSpreadElement(el)) return `...${generateExpression2(el.argument)}`;
1578
+ return t6.isExpression(el) ? generateExpression2(el) : "undefined";
1579
+ }).join(", ");
1580
+ return `[${elements}]`;
1581
+ }
1582
+ if (t6.isObjectExpression(node)) {
1583
+ const props = node.properties.map((prop) => {
1584
+ if (t6.isSpreadElement(prop)) return `...${generateExpression2(prop.argument)}`;
1585
+ if (t6.isObjectProperty(prop)) {
1586
+ const key = t6.isIdentifier(prop.key) ? prop.key.name : t6.isStringLiteral(prop.key) ? `"${prop.key.value}"` : t6.isNumericLiteral(prop.key) ? String(prop.key.value) : "[key]";
1587
+ if (prop.shorthand && t6.isIdentifier(prop.value)) return key;
1588
+ const value = t6.isExpression(prop.value) ? generateExpression2(prop.value) : "undefined";
1589
+ return `${key}: ${value}`;
1590
+ }
1591
+ if (t6.isObjectMethod(prop)) {
1592
+ const key = t6.isIdentifier(prop.key) ? prop.key.name : "[method]";
1593
+ const params = prop.params.map((p) => t6.isIdentifier(p) ? p.name : "_").join(", ");
1594
+ const bodyStr = generateBlockStatement(prop.body);
1595
+ return `${key}(${params}) { ${bodyStr} }`;
1596
+ }
1597
+ return "";
1598
+ }).filter(Boolean).join(", ");
1599
+ return `{ ${props} }`;
1600
+ }
1601
+ if (t6.isParenthesizedExpression(node)) {
1602
+ return `(${generateExpression2(node.expression)})`;
1603
+ }
1604
+ if (t6.isTSAsExpression(node)) return generateExpression2(node.expression);
1605
+ if (t6.isTSNonNullExpression(node)) return generateExpression2(node.expression);
1606
+ if (t6.isTSTypeAssertion(node)) return generateExpression2(node.expression);
1607
+ if (t6.isSequenceExpression(node)) {
1608
+ return `(${node.expressions.map(generateExpression2).join(", ")})`;
1609
+ }
1610
+ if (t6.isAssignmentExpression(node)) {
1611
+ const left = t6.isExpression(node.left) ? generateExpression2(node.left) : "[target]";
1612
+ return `(${left} ${node.operator} ${generateExpression2(node.right)})`;
1613
+ }
1614
+ if (t6.isUpdateExpression(node)) {
1615
+ const arg = generateExpression2(node.argument);
1616
+ return node.prefix ? `${node.operator}${arg}` : `${arg}${node.operator}`;
1617
+ }
1618
+ if (t6.isTaggedTemplateExpression(node)) {
1619
+ return generateExpression2(node.quasi);
1620
+ }
1621
+ if (t6.isSpreadElement(node)) {
1622
+ return `...${generateExpression2(node.argument)}`;
1623
+ }
1624
+ if (t6.isYieldExpression(node)) {
1625
+ return node.argument ? `yield ${generateExpression2(node.argument)}` : "yield";
1626
+ }
1627
+ if (t6.isAwaitExpression(node)) {
1628
+ return `await ${generateExpression2(node.argument)}`;
1629
+ }
1630
+ if (t6.isNewExpression(node)) {
1631
+ const callee = t6.isExpression(node.callee) ? generateExpression2(node.callee) : "Object";
1632
+ const args = node.arguments.map((a) => t6.isExpression(a) ? generateExpression2(a) : "undefined").join(", ");
1633
+ return `new ${callee}(${args})`;
1634
+ }
1635
+ if (t6.isThisExpression(node)) return "this";
1636
+ console.warn(`[component-extractor] Unhandled expression type: ${node.type}`);
1637
+ return "[Expression]";
1638
+ }
1639
+ function generateBlockStatement(block) {
1640
+ return block.body.map((stmt) => {
1641
+ if (t6.isReturnStatement(stmt)) {
1642
+ return stmt.argument ? `return ${generateExpression2(stmt.argument)};` : "return;";
1643
+ }
1644
+ if (t6.isExpressionStatement(stmt)) {
1645
+ return `${generateExpression2(stmt.expression)};`;
1646
+ }
1647
+ if (t6.isVariableDeclaration(stmt)) {
1648
+ const decls = stmt.declarations.map((d) => {
1649
+ const id = t6.isIdentifier(d.id) ? d.id.name : "_";
1650
+ const init = d.init && t6.isExpression(d.init) ? generateExpression2(d.init) : "undefined";
1651
+ return `${id} = ${init}`;
1652
+ }).join(", ");
1653
+ return `${stmt.kind} ${decls};`;
1654
+ }
1655
+ if (t6.isIfStatement(stmt)) {
1656
+ const test = generateExpression2(stmt.test);
1657
+ const consequent = t6.isBlockStatement(stmt.consequent) ? `{ ${generateBlockStatement(stmt.consequent)} }` : t6.isExpressionStatement(stmt.consequent) ? `{ ${generateExpression2(stmt.consequent.expression)}; }` : "{}";
1658
+ const alternate = stmt.alternate ? t6.isBlockStatement(stmt.alternate) ? ` else { ${generateBlockStatement(stmt.alternate)} }` : t6.isIfStatement(stmt.alternate) ? ` else ${generateBlockStatement(t6.blockStatement([stmt.alternate]))}` : "" : "";
1659
+ return `if (${test}) ${consequent}${alternate}`;
1660
+ }
1661
+ return "/* [Statement] */";
1662
+ }).join(" ");
1663
+ }
1664
+ function extractJSXFromCallback(node) {
1665
+ const body = node.body;
1666
+ if (t6.isJSXElement(body) || t6.isJSXFragment(body)) {
1667
+ return body;
1668
+ }
1669
+ if (t6.isParenthesizedExpression(body)) {
1670
+ const inner = body.expression;
1671
+ if (t6.isJSXElement(inner) || t6.isJSXFragment(inner)) {
1672
+ return inner;
1673
+ }
1674
+ }
1675
+ if (t6.isBlockStatement(body)) {
1676
+ for (const stmt of body.body) {
1677
+ if (t6.isReturnStatement(stmt) && stmt.argument) {
1678
+ if (t6.isJSXElement(stmt.argument) || t6.isJSXFragment(stmt.argument)) {
1679
+ return stmt.argument;
1680
+ }
1681
+ if (t6.isParenthesizedExpression(stmt.argument)) {
1682
+ const inner = stmt.argument.expression;
1683
+ if (t6.isJSXElement(inner) || t6.isJSXFragment(inner)) {
1684
+ return inner;
1685
+ }
1686
+ }
1687
+ }
1688
+ }
1689
+ }
1690
+ return null;
1691
+ }
1692
+ function isEventHandlerProp(name) {
1693
+ return /^on[A-Z]/.test(name);
1694
+ }
1695
+ function expressionToActionAtom(expr) {
1696
+ if (t6.isCallExpression(expr) || t6.isOptionalCallExpression(expr)) {
1697
+ if (t6.isIdentifier(expr.callee)) {
1698
+ const setterFieldName = setterToFieldMap.get(expr.callee.name);
1699
+ if (setterFieldName && expr.arguments.length >= 1) {
1700
+ const arg = expr.arguments[0];
1701
+ if ((t6.isArrowFunctionExpression(arg) || t6.isFunctionExpression(arg)) && arg.params.length >= 1 && t6.isIdentifier(arg.params[0])) {
1702
+ const paramName = arg.params[0].name;
1703
+ const bodyExpr = t6.isExpression(arg.body) ? generateExpression2(arg.body) : "undefined";
1704
+ const resolved = bodyExpr.replace(
1705
+ new RegExp(`(?<![.$\\w])${paramName}(?![\\w])`, "g"),
1706
+ `$local.${setterFieldName}`
1707
+ );
1708
+ return `$action.setLocal("${setterFieldName}", ${resolved})`;
1709
+ }
1710
+ const argStr = t6.isExpression(arg) ? generateExpression2(arg) : "undefined";
1711
+ return `$action.setLocal("${setterFieldName}", ${argStr})`;
1712
+ }
1713
+ return null;
1714
+ }
1715
+ if (t6.isMemberExpression(expr.callee) || t6.isOptionalMemberExpression(expr.callee)) {
1716
+ const generated = generateExpression2(expr);
1717
+ if (generated.startsWith("$action.")) return generated;
1718
+ }
1719
+ return null;
1720
+ }
1721
+ return null;
1722
+ }
1723
+ function statementToAction(stmt) {
1724
+ if (t6.isExpressionStatement(stmt)) {
1725
+ return expressionToActionAtom(stmt.expression);
1726
+ }
1727
+ if (t6.isIfStatement(stmt)) {
1728
+ const condition = generateExpression2(stmt.test);
1729
+ let thenAction = null;
1730
+ if (t6.isBlockStatement(stmt.consequent)) {
1731
+ const actions = stmt.consequent.body.map((s) => statementToAction(s)).filter(Boolean);
1732
+ if (actions.length === 0 || actions.length !== stmt.consequent.body.length) return null;
1733
+ thenAction = actions.length === 1 ? actions[0] : `$action.seq(${actions.join(", ")})`;
1734
+ } else if (t6.isExpressionStatement(stmt.consequent)) {
1735
+ thenAction = expressionToActionAtom(stmt.consequent.expression);
1736
+ }
1737
+ if (!thenAction) return null;
1738
+ let elseAction = null;
1739
+ if (stmt.alternate) {
1740
+ if (t6.isBlockStatement(stmt.alternate)) {
1741
+ const actions = stmt.alternate.body.map((s) => statementToAction(s)).filter(Boolean);
1742
+ if (actions.length !== stmt.alternate.body.length) return null;
1743
+ elseAction = actions.length === 1 ? actions[0] : `$action.seq(${actions.join(", ")})`;
1744
+ } else if (t6.isIfStatement(stmt.alternate)) {
1745
+ elseAction = statementToAction(stmt.alternate);
1746
+ } else if (t6.isExpressionStatement(stmt.alternate)) {
1747
+ elseAction = expressionToActionAtom(stmt.alternate.expression);
1748
+ }
1749
+ if (!elseAction) return null;
1750
+ }
1751
+ return elseAction ? `$action.when(${condition}, ${thenAction}, ${elseAction})` : `$action.when(${condition}, ${thenAction})`;
1752
+ }
1753
+ return null;
1754
+ }
1755
+ function decomposeHandlerToSeq(node) {
1756
+ if (t6.isArrowFunctionExpression(node) && t6.isExpression(node.body)) {
1757
+ return expressionToActionAtom(node.body);
1758
+ }
1759
+ const body = node.body;
1760
+ if (!t6.isBlockStatement(body)) return null;
1761
+ const actions = [];
1762
+ for (const stmt of body.body) {
1763
+ const action = statementToAction(stmt);
1764
+ if (!action) return null;
1765
+ actions.push(action);
1766
+ }
1767
+ if (actions.length === 0) return null;
1768
+ if (actions.length === 1) return actions[0];
1769
+ return `$action.seq(${actions.join(", ")})`;
1770
+ }
1771
+ function extractComponents(path, state) {
1772
+ const arg = path.node.argument;
1773
+ if (!arg) return;
1774
+ const compilerState = state;
1775
+ if (compilerState.fields && compilerState.fields.length > 0) {
1776
+ initLocalFieldMaps(compilerState.fields);
1777
+ }
1778
+ let experienceNode = null;
1779
+ if (t6.isJSXElement(arg)) {
1780
+ experienceNode = jsxToExperienceNode(arg);
1781
+ } else if (t6.isJSXFragment(arg)) {
1782
+ experienceNode = jsxToExperienceNode(arg);
1783
+ }
1784
+ if (experienceNode) {
1785
+ compilerState.experience = experienceNode;
1786
+ }
1787
+ }
1788
+
1789
+ // src/babel/extractors/during-extractor.ts
1790
+ import * as t7 from "@babel/types";
1791
+ var duringIdCounter = 0;
1792
+ function resetDuringIdCounter() {
1793
+ duringIdCounter = 0;
1794
+ }
1795
+ function analyzeCallbackBody2(body, actionCounter) {
1796
+ const actions = [];
1797
+ for (const statement of body) {
1798
+ if (!t7.isExpressionStatement(statement)) continue;
1799
+ const expr = statement.expression;
1800
+ if (t7.isCallExpression(expr) && t7.isIdentifier(expr.callee)) {
1801
+ const calleeName = expr.callee.name;
1802
+ const args = expr.arguments;
1803
+ if (calleeName === "setField" && args.length >= 2) {
1804
+ const field = t7.isStringLiteral(args[0]) ? args[0].value : void 0;
1805
+ const arg1 = args[1];
1806
+ if (field !== void 0 && !t7.isArgumentPlaceholder(arg1)) {
1807
+ actions.push({
1808
+ id: `auto_${++actionCounter.value}`,
1809
+ type: "set_field",
1810
+ mode: "auto",
1811
+ config: { field, value: extractValue2(arg1) }
1812
+ });
1813
+ }
1814
+ } else if (calleeName === "setMemory" && args.length >= 2) {
1815
+ const key = t7.isStringLiteral(args[0]) ? args[0].value : void 0;
1816
+ const arg1 = args[1];
1817
+ if (key !== void 0 && !t7.isArgumentPlaceholder(arg1)) {
1818
+ actions.push({
1819
+ id: `auto_${++actionCounter.value}`,
1820
+ type: "set_memory",
1821
+ mode: "auto",
1822
+ config: { key, value: extractValue2(arg1) }
1823
+ });
1824
+ }
1825
+ } else if (calleeName.startsWith("set") && calleeName.length > 3 && args.length >= 1) {
1826
+ const fieldName = calleeName.slice(3, 4).toLowerCase() + calleeName.slice(4);
1827
+ const arg0 = args[0];
1828
+ if (!t7.isArgumentPlaceholder(arg0)) {
1829
+ actions.push({
1830
+ id: `auto_${++actionCounter.value}`,
1831
+ type: "set_field",
1832
+ mode: "auto",
1833
+ config: { field: fieldName, value: extractValue2(arg0) }
1834
+ });
1835
+ }
1836
+ }
1837
+ } else if (t7.isCallExpression(expr)) {
1838
+ const callee = expr.callee;
1839
+ if (t7.isMemberExpression(callee) && t7.isIdentifier(callee.object) && callee.object.name === "console" && t7.isIdentifier(callee.property) && callee.property.name === "log" && expr.arguments.length > 0) {
1840
+ const arg0 = expr.arguments[0];
1841
+ if (!t7.isArgumentPlaceholder(arg0)) {
1842
+ actions.push({
1843
+ id: `auto_${++actionCounter.value}`,
1844
+ type: "log_event",
1845
+ mode: "auto",
1846
+ config: { message: extractValue2(arg0) }
1847
+ });
1848
+ }
1849
+ } else {
1850
+ actions.push({
1851
+ id: `auto_${++actionCounter.value}`,
1852
+ type: "custom",
1853
+ mode: "auto",
1854
+ config: { expression: generateCode2(expr) }
1855
+ });
1856
+ }
1857
+ }
1858
+ }
1859
+ return actions;
1860
+ }
1861
+ function extractValue2(node) {
1862
+ if (t7.isNumericLiteral(node)) return node.value;
1863
+ if (t7.isStringLiteral(node)) return node.value;
1864
+ if (t7.isBooleanLiteral(node)) return node.value;
1865
+ if (t7.isNullLiteral(node)) return null;
1866
+ if (t7.isArrayExpression(node)) {
1867
+ return node.elements.map((el) => el && t7.isExpression(el) ? extractValue2(el) : null);
1868
+ }
1869
+ if (t7.isObjectExpression(node)) {
1870
+ const obj = {};
1871
+ node.properties.forEach((prop) => {
1872
+ if (t7.isObjectProperty(prop) && t7.isIdentifier(prop.key) && t7.isExpression(prop.value)) {
1873
+ obj[prop.key.name] = extractValue2(prop.value);
1874
+ }
1875
+ });
1876
+ return obj;
1877
+ }
1878
+ return generateCode2(node);
1879
+ }
1880
+ function generateCode2(node) {
1881
+ if (t7.isIdentifier(node)) return node.name;
1882
+ if (t7.isNumericLiteral(node)) return String(node.value);
1883
+ if (t7.isStringLiteral(node)) return `"${node.value}"`;
1884
+ if (t7.isBooleanLiteral(node)) return String(node.value);
1885
+ if (t7.isNullLiteral(node)) return "null";
1886
+ if (t7.isBinaryExpression(node)) {
1887
+ return `(${generateCode2(node.left)} ${node.operator} ${generateCode2(node.right)})`;
1888
+ }
1889
+ if (t7.isLogicalExpression(node)) {
1890
+ return `(${generateCode2(node.left)} ${node.operator} ${generateCode2(node.right)})`;
1891
+ }
1892
+ if (t7.isConditionalExpression(node)) {
1893
+ return `(${generateCode2(node.test)} ? ${generateCode2(node.consequent)} : ${generateCode2(node.alternate)})`;
1894
+ }
1895
+ if (t7.isUnaryExpression(node) && node.prefix) {
1896
+ return `${node.operator}${generateCode2(node.argument)}`;
1897
+ }
1898
+ if (t7.isCallExpression(node) && t7.isMemberExpression(node.callee) && t7.isIdentifier(node.callee.object) && node.callee.object.name === "Date" && t7.isIdentifier(node.callee.property) && node.callee.property.name === "now") {
1899
+ return "now()";
1900
+ }
1901
+ if (t7.isMemberExpression(node)) {
1902
+ const obj = generateCode2(node.object);
1903
+ if (node.computed) {
1904
+ return `${obj}[${generateCode2(node.property)}]`;
1905
+ }
1906
+ const prop = t7.isIdentifier(node.property) ? node.property.name : "[property]";
1907
+ return `${obj}.${prop}`;
1908
+ }
1909
+ if (t7.isCallExpression(node)) {
1910
+ if (t7.isIdentifier(node.callee)) {
1911
+ const args = node.arguments.map((a) => generateCode2(a)).join(", ");
1912
+ return `${node.callee.name}(${args})`;
1913
+ }
1914
+ if (t7.isMemberExpression(node.callee)) {
1915
+ const obj = generateCode2(node.callee.object);
1916
+ const prop = t7.isIdentifier(node.callee.property) ? node.callee.property.name : "[method]";
1917
+ const args = node.arguments.map((a) => generateCode2(a)).join(", ");
1918
+ return `${obj}.${prop}(${args})`;
1919
+ }
1920
+ }
1921
+ if (t7.isArrowFunctionExpression(node) || t7.isFunctionExpression(node)) {
1922
+ if (t7.isArrowFunctionExpression(node) && t7.isExpression(node.body)) {
1923
+ const params = node.params;
1924
+ if (params.length === 1 && t7.isIdentifier(params[0])) {
1925
+ const paramName = params[0].name;
1926
+ const bodyCode = generateCode2(node.body);
1927
+ return bodyCode.replace(new RegExp(`\\b${paramName}\\b`, "g"), "$prev");
1928
+ }
1929
+ }
1930
+ return "[Function]";
1931
+ }
1932
+ if (t7.isTemplateLiteral(node)) {
1933
+ return node.quasis.map((q, i) => {
1934
+ const raw = q.value.raw;
1935
+ if (i < node.expressions.length) return raw + "${" + generateCode2(node.expressions[i]) + "}";
1936
+ return raw;
1937
+ }).join("");
1938
+ }
1939
+ if (t7.isArrayExpression(node)) {
1940
+ const elements = node.elements.map((el) => el ? generateCode2(el) : "undefined").join(", ");
1941
+ return `[${elements}]`;
1942
+ }
1943
+ if (t7.isObjectExpression(node)) {
1944
+ const props = node.properties.map((p) => {
1945
+ if (t7.isObjectProperty(p)) {
1946
+ const key = t7.isIdentifier(p.key) ? p.key.name : t7.isStringLiteral(p.key) ? `"${p.key.value}"` : "[key]";
1947
+ if (p.shorthand && t7.isIdentifier(p.value)) return key;
1948
+ return `${key}: ${generateCode2(p.value)}`;
1949
+ }
1950
+ if (t7.isSpreadElement(p)) return `...${generateCode2(p.argument)}`;
1951
+ return "";
1952
+ }).filter(Boolean).join(", ");
1953
+ return `{ ${props} }`;
1954
+ }
1955
+ if (t7.isTSAsExpression(node)) return generateCode2(node.expression);
1956
+ if (t7.isTSNonNullExpression(node)) return generateCode2(node.expression);
1957
+ return "[Expression]";
1958
+ }
1959
+ function ensureState3(states, stateName) {
1960
+ if (!states.has(stateName)) {
1961
+ states.set(stateName, {
1962
+ name: stateName,
1963
+ type: "REGULAR",
1964
+ on_enter: [],
1965
+ during: [],
1966
+ on_exit: []
1967
+ });
1968
+ }
1969
+ return states.get(stateName);
1970
+ }
1971
+ function extractDuring(path, state) {
1972
+ const callee = path.node.callee;
1973
+ if (!t7.isIdentifier(callee) || callee.name !== "useWhileIn") return;
1974
+ const args = path.node.arguments;
1975
+ if (args.length < 3) return;
1976
+ const stateArg = args[0];
1977
+ const intervalArg = args[1];
1978
+ const callbackArg = args[2];
1979
+ if (!t7.isStringLiteral(stateArg)) return;
1980
+ const stateName = stateArg.value;
1981
+ let intervalMs = 1e3;
1982
+ if (t7.isNumericLiteral(intervalArg)) {
1983
+ intervalMs = intervalArg.value;
1984
+ }
1985
+ let callbackBody = [];
1986
+ if (t7.isArrowFunctionExpression(callbackArg) || t7.isFunctionExpression(callbackArg)) {
1987
+ const body = callbackArg.body;
1988
+ if (t7.isBlockStatement(body)) {
1989
+ callbackBody = body.body;
1990
+ } else if (t7.isExpression(body)) {
1991
+ callbackBody = [t7.expressionStatement(body)];
1992
+ }
1993
+ }
1994
+ const compilerState = state;
1995
+ if (!compilerState.states) compilerState.states = /* @__PURE__ */ new Map();
1996
+ if (!compilerState.actionCounter) compilerState.actionCounter = 0;
1997
+ const stateDefinition = ensureState3(compilerState.states, stateName);
1998
+ const actions = analyzeCallbackBody2(callbackBody, { value: compilerState.actionCounter });
1999
+ compilerState.actionCounter += actions.length;
2000
+ const duringAction = {
2001
+ id: `during_${++duringIdCounter}`,
2002
+ type: "interval",
2003
+ interval_ms: intervalMs,
2004
+ actions
2005
+ };
2006
+ stateDefinition.during.push(duringAction);
2007
+ }
2008
+
2009
+ // src/babel/extractors/change-extractor.ts
2010
+ import * as t8 from "@babel/types";
2011
+ var watcherIdCounter = 0;
2012
+ function resetWatcherIdCounter() {
2013
+ watcherIdCounter = 0;
2014
+ }
2015
+ function analyzeCallbackBody3(body, actionCounter) {
2016
+ const actions = [];
2017
+ for (const statement of body) {
2018
+ if (!t8.isExpressionStatement(statement)) continue;
2019
+ const expr = statement.expression;
2020
+ if (t8.isCallExpression(expr) && t8.isIdentifier(expr.callee)) {
2021
+ const calleeName = expr.callee.name;
2022
+ const args = expr.arguments;
2023
+ if (calleeName === "setField" && args.length >= 2) {
2024
+ const field = t8.isStringLiteral(args[0]) ? args[0].value : void 0;
2025
+ if (field !== void 0) {
2026
+ actions.push({
2027
+ id: `auto_${++actionCounter.value}`,
2028
+ type: "set_field",
2029
+ mode: "auto",
2030
+ config: { field, value: extractValue3(args[1]) }
2031
+ });
2032
+ }
2033
+ } else if (calleeName === "setMemory" && args.length >= 2) {
2034
+ const key = t8.isStringLiteral(args[0]) ? args[0].value : void 0;
2035
+ if (key !== void 0) {
2036
+ actions.push({
2037
+ id: `auto_${++actionCounter.value}`,
2038
+ type: "set_memory",
2039
+ mode: "auto",
2040
+ config: { key, value: extractValue3(args[1]) }
2041
+ });
2042
+ }
2043
+ } else if (calleeName.startsWith("set") && calleeName.length > 3 && args.length >= 1) {
2044
+ const fieldName = calleeName.slice(3, 4).toLowerCase() + calleeName.slice(4);
2045
+ actions.push({
2046
+ id: `auto_${++actionCounter.value}`,
2047
+ type: "set_field",
2048
+ mode: "auto",
2049
+ config: { field: fieldName, value: extractValue3(args[0]) }
2050
+ });
2051
+ }
2052
+ } else if (t8.isCallExpression(expr)) {
2053
+ const callee = expr.callee;
2054
+ if (t8.isMemberExpression(callee) && t8.isIdentifier(callee.object) && callee.object.name === "console" && t8.isIdentifier(callee.property) && callee.property.name === "log" && expr.arguments.length > 0) {
2055
+ actions.push({
2056
+ id: `auto_${++actionCounter.value}`,
2057
+ type: "log_event",
2058
+ mode: "auto",
2059
+ config: { message: extractValue3(expr.arguments[0]) }
2060
+ });
2061
+ }
2062
+ }
2063
+ }
2064
+ return actions;
2065
+ }
2066
+ function extractValue3(node) {
2067
+ if (t8.isNumericLiteral(node)) return node.value;
2068
+ if (t8.isStringLiteral(node)) return node.value;
2069
+ if (t8.isBooleanLiteral(node)) return node.value;
2070
+ if (t8.isNullLiteral(node)) return null;
2071
+ if (t8.isIdentifier(node)) return node.name;
2072
+ if (t8.isTemplateLiteral(node)) {
2073
+ return node.quasis.map((q, i) => {
2074
+ const raw = q.value.raw;
2075
+ if (i < node.expressions.length) {
2076
+ const expr = node.expressions[i];
2077
+ return raw + "${" + (t8.isIdentifier(expr) ? expr.name : "[expr]") + "}";
2078
+ }
2079
+ return raw;
2080
+ }).join("");
2081
+ }
2082
+ if (t8.isArrayExpression(node)) {
2083
+ return node.elements.map((el) => el && t8.isExpression(el) ? extractValue3(el) : null);
2084
+ }
2085
+ if (t8.isObjectExpression(node)) {
2086
+ const obj = {};
2087
+ node.properties.forEach((prop) => {
2088
+ if (t8.isObjectProperty(prop) && t8.isIdentifier(prop.key) && t8.isExpression(prop.value)) {
2089
+ obj[prop.key.name] = extractValue3(prop.value);
2090
+ }
2091
+ });
2092
+ return obj;
2093
+ }
2094
+ if (t8.isMemberExpression(node)) {
2095
+ const obj = t8.isIdentifier(node.object) ? node.object.name : "[object]";
2096
+ const prop = t8.isIdentifier(node.property) ? node.property.name : "[property]";
2097
+ return `${obj}.${prop}`;
2098
+ }
2099
+ if (t8.isConditionalExpression(node)) {
2100
+ return `(${extractValue3(node.test)} ? ${extractValue3(node.consequent)} : ${extractValue3(node.alternate)})`;
2101
+ }
2102
+ if (t8.isBinaryExpression(node)) {
2103
+ return `(${extractValue3(node.left)} ${node.operator} ${extractValue3(node.right)})`;
2104
+ }
2105
+ if (t8.isLogicalExpression(node)) {
2106
+ return `(${extractValue3(node.left)} ${node.operator} ${extractValue3(node.right)})`;
2107
+ }
2108
+ if (t8.isTSAsExpression(node)) return extractValue3(node.expression);
2109
+ if (t8.isTSNonNullExpression(node)) return extractValue3(node.expression);
2110
+ return "[Expression]";
2111
+ }
2112
+ function extractChangeWatcher(path, state) {
2113
+ const callee = path.node.callee;
2114
+ if (!t8.isIdentifier(callee) || callee.name !== "useOnChange") return;
2115
+ const args = path.node.arguments;
2116
+ if (args.length < 2) return;
2117
+ const fieldArg = args[0];
2118
+ const callbackArg = args[1];
2119
+ if (!t8.isStringLiteral(fieldArg)) return;
2120
+ const field = fieldArg.value;
2121
+ let callbackBody = [];
2122
+ if (t8.isArrowFunctionExpression(callbackArg) || t8.isFunctionExpression(callbackArg)) {
2123
+ const body = callbackArg.body;
2124
+ if (t8.isBlockStatement(body)) {
2125
+ callbackBody = body.body;
2126
+ } else if (t8.isExpression(body)) {
2127
+ callbackBody = [t8.expressionStatement(body)];
2128
+ }
2129
+ }
2130
+ const compilerState = state;
2131
+ if (!compilerState.actionCounter) compilerState.actionCounter = 0;
2132
+ const actions = analyzeCallbackBody3(callbackBody, { value: compilerState.actionCounter });
2133
+ compilerState.actionCounter += actions.length;
2134
+ const watcher = {
2135
+ id: `watcher_${++watcherIdCounter}`,
2136
+ field,
2137
+ actions
2138
+ };
2139
+ if (!compilerState.metadata) compilerState.metadata = {};
2140
+ const meta = compilerState.metadata;
2141
+ if (!meta.fieldWatchers) meta.fieldWatchers = [];
2142
+ meta.fieldWatchers.push(watcher);
2143
+ }
2144
+
2145
+ // src/babel/extractors/computed-extractor.ts
2146
+ import * as t9 from "@babel/types";
2147
+ function expressionToString(node) {
2148
+ if (t9.isIdentifier(node)) return node.name;
2149
+ if (t9.isNumericLiteral(node)) return String(node.value);
2150
+ if (t9.isStringLiteral(node)) return `"${node.value}"`;
2151
+ if (t9.isBooleanLiteral(node)) return String(node.value);
2152
+ if (t9.isNullLiteral(node)) return "null";
2153
+ if (t9.isTemplateLiteral(node)) {
2154
+ return node.quasis.map((q, i) => {
2155
+ const raw = q.value.raw;
2156
+ if (i < node.expressions.length) {
2157
+ const expr = node.expressions[i];
2158
+ return raw + "${" + expressionToString(expr) + "}";
2159
+ }
2160
+ return raw;
2161
+ }).join("");
2162
+ }
2163
+ if (t9.isBinaryExpression(node)) {
2164
+ return `${expressionToString(node.left)} ${node.operator} ${expressionToString(node.right)}`;
2165
+ }
2166
+ if (t9.isLogicalExpression(node)) {
2167
+ return `${expressionToString(node.left)} ${node.operator} ${expressionToString(node.right)}`;
2168
+ }
2169
+ if (t9.isConditionalExpression(node)) {
2170
+ return `${expressionToString(node.test)} ? ${expressionToString(node.consequent)} : ${expressionToString(node.alternate)}`;
2171
+ }
2172
+ if (t9.isMemberExpression(node) && !node.computed) {
2173
+ return `${expressionToString(node.object)}.${expressionToString(node.property)}`;
2174
+ }
2175
+ if (t9.isCallExpression(node)) {
2176
+ const callee = expressionToString(node.callee);
2177
+ const args = node.arguments.map((a) => expressionToString(a)).join(", ");
2178
+ return `${callee}(${args})`;
2179
+ }
2180
+ if (t9.isUnaryExpression(node)) {
2181
+ return `${node.operator}${expressionToString(node.argument)}`;
2182
+ }
2183
+ if (t9.isParenthesizedExpression(node)) {
2184
+ return `(${expressionToString(node.expression)})`;
2185
+ }
2186
+ return "[Expression]";
2187
+ }
2188
+ function inferComputedType(body) {
2189
+ if (t9.isNumericLiteral(body) || t9.isBinaryExpression(body)) return "number";
2190
+ if (t9.isStringLiteral(body) || t9.isTemplateLiteral(body)) return "text";
2191
+ if (t9.isBooleanLiteral(body) || t9.isLogicalExpression(body)) return "boolean";
2192
+ if (t9.isArrayExpression(body)) return "json";
2193
+ if (t9.isObjectExpression(body)) return "json";
2194
+ return "text";
2195
+ }
2196
+ function extractStringArray(node) {
2197
+ if (!t9.isArrayExpression(node)) return [];
2198
+ return node.elements.filter((el) => t9.isStringLiteral(el)).map((el) => el.value);
2199
+ }
2200
+ function extractComputed(path, state) {
2201
+ const callee = path.node.callee;
2202
+ if (!t9.isIdentifier(callee) || callee.name !== "useComputed") return;
2203
+ const args = path.node.arguments;
2204
+ if (args.length < 2) return;
2205
+ const nameArg = args[0];
2206
+ if (!t9.isStringLiteral(nameArg)) return;
2207
+ const fieldName = nameArg.value;
2208
+ const computeArg = args[1];
2209
+ if (!t9.isArrowFunctionExpression(computeArg) && !t9.isFunctionExpression(computeArg)) return;
2210
+ let expression;
2211
+ let returnType = "text";
2212
+ if (t9.isExpression(computeArg.body) && !t9.isBlockStatement(computeArg.body)) {
2213
+ expression = expressionToString(computeArg.body);
2214
+ returnType = inferComputedType(computeArg.body);
2215
+ } else if (t9.isBlockStatement(computeArg.body)) {
2216
+ const returnStmt = computeArg.body.body.find(
2217
+ (s) => t9.isReturnStatement(s)
2218
+ );
2219
+ if (returnStmt?.argument) {
2220
+ expression = expressionToString(returnStmt.argument);
2221
+ returnType = inferComputedType(returnStmt.argument);
2222
+ } else {
2223
+ expression = "[Expression]";
2224
+ }
2225
+ } else {
2226
+ expression = "[Expression]";
2227
+ }
2228
+ let mode = "read-time";
2229
+ let deps = [];
2230
+ let staleness;
2231
+ let rlsStalenessOk;
2232
+ let explicitType;
2233
+ if (args.length >= 3 && t9.isObjectExpression(args[2])) {
2234
+ const optObj = args[2];
2235
+ for (const prop of optObj.properties) {
2236
+ if (!t9.isObjectProperty(prop) || !t9.isIdentifier(prop.key)) continue;
2237
+ switch (prop.key.name) {
2238
+ case "mode":
2239
+ if (t9.isStringLiteral(prop.value)) {
2240
+ const modeVal = prop.value.value;
2241
+ if (modeVal === "read-time" || modeVal === "transaction-maintained" || modeVal === "async-materialized") {
2242
+ mode = modeVal;
2243
+ }
2244
+ }
2245
+ break;
2246
+ case "deps":
2247
+ deps = extractStringArray(prop.value);
2248
+ break;
2249
+ case "staleness":
2250
+ if (t9.isStringLiteral(prop.value)) {
2251
+ staleness = prop.value.value;
2252
+ }
2253
+ break;
2254
+ case "rlsStalenessOk":
2255
+ if (t9.isBooleanLiteral(prop.value)) {
2256
+ rlsStalenessOk = prop.value.value;
2257
+ }
2258
+ break;
2259
+ case "type":
2260
+ if (t9.isStringLiteral(prop.value)) {
2261
+ explicitType = prop.value.value;
2262
+ }
2263
+ break;
2264
+ }
2265
+ }
2266
+ }
2267
+ let varName = fieldName;
2268
+ const parent = path.parentPath;
2269
+ if (parent?.isVariableDeclarator() && t9.isIdentifier(parent.node.id)) {
2270
+ varName = parent.node.id.name;
2271
+ }
2272
+ const compilerState = state;
2273
+ const fieldDef = {
2274
+ name: fieldName,
2275
+ type: explicitType || returnType,
2276
+ computed: expression,
2277
+ computed_deps: deps.length > 0 ? deps : void 0
2278
+ };
2279
+ fieldDef.computed_mode = mode;
2280
+ if (staleness !== void 0) {
2281
+ fieldDef.computed_staleness = staleness;
2282
+ }
2283
+ if (rlsStalenessOk !== void 0) {
2284
+ fieldDef.rls_staleness_tolerance = rlsStalenessOk;
2285
+ }
2286
+ compilerState.fields.push(fieldDef);
2287
+ if (!compilerState.metadata) compilerState.metadata = {};
2288
+ const meta = compilerState.metadata;
2289
+ if (!meta.computedFields) meta.computedFields = [];
2290
+ meta.computedFields.push({
2291
+ name: fieldName,
2292
+ varName,
2293
+ expression,
2294
+ deps,
2295
+ mode,
2296
+ type: explicitType || returnType,
2297
+ staleness,
2298
+ rlsStalenessOk
2299
+ });
2300
+ }
2301
+
2302
+ // src/babel/extractors/transition-effect-extractor.ts
2303
+ import * as t10 from "@babel/types";
2304
+ var transitionEffectIdCounter = 0;
2305
+ function resetTransitionEffectIdCounter() {
2306
+ transitionEffectIdCounter = 0;
2307
+ }
2308
+ function analyzeCallbackBody4(body, actionCounter) {
2309
+ const actions = [];
2310
+ for (const statement of body) {
2311
+ if (!t10.isExpressionStatement(statement)) continue;
2312
+ const expr = statement.expression;
2313
+ if (t10.isCallExpression(expr) && t10.isIdentifier(expr.callee)) {
2314
+ const calleeName = expr.callee.name;
2315
+ const args = expr.arguments;
2316
+ if (calleeName === "setField" && args.length >= 2) {
2317
+ const field = t10.isStringLiteral(args[0]) ? args[0].value : void 0;
2318
+ if (field !== void 0) {
2319
+ actions.push({
2320
+ id: `auto_${++actionCounter.value}`,
2321
+ type: "set_field",
2322
+ mode: "auto",
2323
+ config: { field, value: extractValue4(args[1]) }
2324
+ });
2325
+ }
2326
+ } else if (calleeName === "setMemory" && args.length >= 2) {
2327
+ const key = t10.isStringLiteral(args[0]) ? args[0].value : void 0;
2328
+ if (key !== void 0) {
2329
+ actions.push({
2330
+ id: `auto_${++actionCounter.value}`,
2331
+ type: "set_memory",
2332
+ mode: "auto",
2333
+ config: { key, value: extractValue4(args[1]) }
2334
+ });
2335
+ }
2336
+ } else if (calleeName.startsWith("set") && calleeName.length > 3 && args.length >= 1) {
2337
+ const fieldName = calleeName.slice(3, 4).toLowerCase() + calleeName.slice(4);
2338
+ actions.push({
2339
+ id: `auto_${++actionCounter.value}`,
2340
+ type: "set_field",
2341
+ mode: "auto",
2342
+ config: { field: fieldName, value: extractValue4(args[0]) }
2343
+ });
2344
+ }
2345
+ } else if (t10.isCallExpression(expr)) {
2346
+ const callee = expr.callee;
2347
+ if (t10.isMemberExpression(callee) && t10.isIdentifier(callee.object) && callee.object.name === "console" && t10.isIdentifier(callee.property) && callee.property.name === "log" && expr.arguments.length > 0) {
2348
+ actions.push({
2349
+ id: `auto_${++actionCounter.value}`,
2350
+ type: "log_event",
2351
+ mode: "auto",
2352
+ config: { message: extractValue4(expr.arguments[0]) }
2353
+ });
2354
+ }
2355
+ }
2356
+ }
2357
+ return actions;
2358
+ }
2359
+ function extractValue4(node) {
2360
+ if (t10.isNumericLiteral(node)) return node.value;
2361
+ if (t10.isStringLiteral(node)) return node.value;
2362
+ if (t10.isBooleanLiteral(node)) return node.value;
2363
+ if (t10.isNullLiteral(node)) return null;
2364
+ if (t10.isIdentifier(node)) return node.name;
2365
+ if (t10.isArrayExpression(node)) {
2366
+ return node.elements.map((el) => el && t10.isExpression(el) ? extractValue4(el) : null);
2367
+ }
2368
+ if (t10.isObjectExpression(node)) {
2369
+ const obj = {};
2370
+ node.properties.forEach((prop) => {
2371
+ if (t10.isObjectProperty(prop) && t10.isIdentifier(prop.key) && t10.isExpression(prop.value)) {
2372
+ obj[prop.key.name] = extractValue4(prop.value);
2373
+ }
2374
+ });
2375
+ return obj;
2376
+ }
2377
+ if (t10.isMemberExpression(node)) {
2378
+ const obj = t10.isIdentifier(node.object) ? node.object.name : "[object]";
2379
+ const prop = t10.isIdentifier(node.property) ? node.property.name : "[property]";
2380
+ return `${obj}.${prop}`;
2381
+ }
2382
+ if (t10.isConditionalExpression(node)) {
2383
+ return `(${extractValue4(node.test)} ? ${extractValue4(node.consequent)} : ${extractValue4(node.alternate)})`;
2384
+ }
2385
+ if (t10.isTSAsExpression(node)) return extractValue4(node.expression);
2386
+ if (t10.isTSNonNullExpression(node)) return extractValue4(node.expression);
2387
+ return "[Expression]";
2388
+ }
2389
+ function extractTransitionEffect(path, state) {
2390
+ const callee = path.node.callee;
2391
+ if (!t10.isIdentifier(callee) || callee.name !== "useOnTransition") return;
2392
+ const args = path.node.arguments;
2393
+ if (args.length < 1) return;
2394
+ const callbackArg = args[0];
2395
+ let callbackBody = [];
2396
+ if (t10.isArrowFunctionExpression(callbackArg) || t10.isFunctionExpression(callbackArg)) {
2397
+ const body = callbackArg.body;
2398
+ if (t10.isBlockStatement(body)) {
2399
+ callbackBody = body.body;
2400
+ } else if (t10.isExpression(body)) {
2401
+ callbackBody = [t10.expressionStatement(body)];
2402
+ }
2403
+ }
2404
+ const compilerState = state;
2405
+ if (!compilerState.actionCounter) compilerState.actionCounter = 0;
2406
+ const actions = analyzeCallbackBody4(callbackBody, { value: compilerState.actionCounter });
2407
+ compilerState.actionCounter += actions.length;
2408
+ const effect = {
2409
+ id: `transition_effect_${++transitionEffectIdCounter}`,
2410
+ actions
2411
+ };
2412
+ if (!compilerState.metadata) compilerState.metadata = {};
2413
+ const meta = compilerState.metadata;
2414
+ if (!meta.transitionEffects) meta.transitionEffects = [];
2415
+ meta.transitionEffects.push(effect);
2416
+ }
2417
+
2418
+ // src/babel/extractors/model-extractor.ts
2419
+ import * as t11 from "@babel/types";
2420
+ function tsTypeToFieldType(typeAnnotation) {
2421
+ if (!typeAnnotation) return "text";
2422
+ const annotation = typeAnnotation.typeAnnotation;
2423
+ return resolveType(annotation);
2424
+ }
2425
+ var KNOWN_TYPE_REFS2 = {
2426
+ Date: "datetime",
2427
+ Record: "json",
2428
+ Map: "json",
2429
+ Set: "json",
2430
+ URL: "url"
2431
+ };
2432
+ function typeNameToSlug2(name) {
2433
+ return name.replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
2434
+ }
2435
+ function resolveType(annotation) {
2436
+ if (t11.isTSStringKeyword(annotation)) return "text";
2437
+ if (t11.isTSNumberKeyword(annotation)) return "number";
2438
+ if (t11.isTSBooleanKeyword(annotation)) return "boolean";
2439
+ if (t11.isTSTypeReference(annotation) && t11.isIdentifier(annotation.typeName)) {
2440
+ const typeName = annotation.typeName.name;
2441
+ if (KNOWN_TYPE_REFS2[typeName]) return KNOWN_TYPE_REFS2[typeName];
2442
+ if (typeName === "Array") return "multi_select";
2443
+ return typeNameToSlug2(typeName);
2444
+ }
2445
+ if (t11.isTSArrayType(annotation)) {
2446
+ return "multi_select";
2447
+ }
2448
+ if (t11.isTSUnionType(annotation)) {
2449
+ const allLiterals = annotation.types.every(
2450
+ (t22) => t11.isTSLiteralType(t22) && (t11.isStringLiteral(t22.literal) || t11.isNumericLiteral(t22.literal))
2451
+ );
2452
+ if (allLiterals) return "select";
2453
+ return "text";
2454
+ }
2455
+ if (t11.isTSLiteralType(annotation) && t11.isStringLiteral(annotation.literal)) return "text";
2456
+ if (t11.isTSLiteralType(annotation) && t11.isNumericLiteral(annotation.literal)) return "number";
2457
+ return "text";
2458
+ }
2459
+ function extractUnionOptions(annotation) {
2460
+ if (!t11.isTSUnionType(annotation)) return void 0;
2461
+ const options = [];
2462
+ for (const member of annotation.types) {
2463
+ if (t11.isTSLiteralType(member) && t11.isStringLiteral(member.literal)) {
2464
+ options.push(member.literal.value);
2465
+ }
2466
+ }
2467
+ return options.length > 0 ? options : void 0;
2468
+ }
2469
+ function extractDefaultValue2(fieldType) {
2470
+ switch (fieldType) {
2471
+ case "text":
2472
+ return "";
2473
+ case "rich_text":
2474
+ return "";
2475
+ case "number":
2476
+ return 0;
2477
+ case "boolean":
2478
+ return false;
2479
+ case "date":
2480
+ return null;
2481
+ case "datetime":
2482
+ return null;
2483
+ case "select":
2484
+ return "";
2485
+ case "multi_select":
2486
+ return [];
2487
+ case "url":
2488
+ return "";
2489
+ case "json":
2490
+ return {};
2491
+ // Custom Element-based field types (email, currency, etc.) default to null
2492
+ default:
2493
+ return null;
2494
+ }
2495
+ }
2496
+ function extractFieldsFromInterface(declaration) {
2497
+ const fields = [];
2498
+ for (const member of declaration.body.body) {
2499
+ if (!t11.isTSPropertySignature(member)) continue;
2500
+ if (!t11.isIdentifier(member.key)) continue;
2501
+ const name = member.key.name;
2502
+ const required = !member.optional;
2503
+ const typeAnno = member.typeAnnotation;
2504
+ const fieldType = tsTypeToFieldType(typeAnno);
2505
+ const field = {
2506
+ name,
2507
+ type: fieldType,
2508
+ label: name.replace(/([A-Z])/g, " $1").replace(/^./, (c) => c.toUpperCase()),
2509
+ required,
2510
+ default_value: extractDefaultValue2(fieldType)
2511
+ };
2512
+ if (typeAnno) {
2513
+ const options = extractUnionOptions(typeAnno.typeAnnotation);
2514
+ if (options) {
2515
+ field.validation = { options };
2516
+ }
2517
+ }
2518
+ fields.push(field);
2519
+ }
2520
+ return fields;
2521
+ }
2522
+ function extractTransitionsFromObject(init, actionCounter) {
2523
+ const transitions = [];
2524
+ for (const prop of init.properties) {
2525
+ if (!t11.isObjectProperty(prop)) continue;
2526
+ if (!t11.isIdentifier(prop.key) && !t11.isStringLiteral(prop.key)) continue;
2527
+ if (!t11.isObjectExpression(prop.value)) continue;
2528
+ const name = t11.isIdentifier(prop.key) ? prop.key.name : prop.key.value;
2529
+ const config = extractObjectLiteral(prop.value);
2530
+ let from = [];
2531
+ if (typeof config.from === "string") {
2532
+ from = [config.from];
2533
+ } else if (Array.isArray(config.from)) {
2534
+ from = config.from.map(String);
2535
+ }
2536
+ const to = String(config.to || "");
2537
+ const conditions = [];
2538
+ if (config.guard) {
2539
+ conditions.push(parseGuardExpression(config.guard));
2540
+ }
2541
+ if (config.conditions && Array.isArray(config.conditions)) {
2542
+ for (const cond of config.conditions) {
2543
+ conditions.push(cond);
2544
+ }
2545
+ }
2546
+ const actions = [];
2547
+ if (config.actions && Array.isArray(config.actions)) {
2548
+ for (const action of config.actions) {
2549
+ if (typeof action === "object" && action !== null) {
2550
+ actions.push({
2551
+ id: `auto_${++actionCounter.value}`,
2552
+ type: action.type || "custom",
2553
+ mode: "auto",
2554
+ config: action
2555
+ });
2556
+ }
2557
+ }
2558
+ }
2559
+ transitions.push({
2560
+ name,
2561
+ from,
2562
+ to,
2563
+ description: config.description,
2564
+ conditions: conditions.length > 0 ? conditions : void 0,
2565
+ actions,
2566
+ roles: config.roles,
2567
+ auto: config.auto,
2568
+ required_fields: config.required_fields
2569
+ });
2570
+ }
2571
+ return transitions;
2572
+ }
2573
+ function parseGuardExpression(guard) {
2574
+ const simpleMatch = guard.match(
2575
+ /^(\$?\w+(?:\.\w+)*)\s*(not_in|is_set|is_empty|contains|>=|<=|!=|==|>|<|in)\s*(.+)?$/
2576
+ );
2577
+ if (simpleMatch) {
2578
+ const [, field, op, rawValue] = simpleMatch;
2579
+ const operatorMap = {
2580
+ "==": "eq",
2581
+ "!=": "ne",
2582
+ ">": "gt",
2583
+ ">=": "gte",
2584
+ "<": "lt",
2585
+ "<=": "lte",
2586
+ "in": "in",
2587
+ "not_in": "not_in",
2588
+ "contains": "contains",
2589
+ "is_set": "is_set",
2590
+ "is_empty": "is_empty"
2591
+ };
2592
+ const operator = operatorMap[op];
2593
+ if (operator) {
2594
+ let value = rawValue?.trim();
2595
+ if (value === void 0 || value === "") {
2596
+ return { field, operator };
2597
+ }
2598
+ if (typeof value === "string") {
2599
+ if (value.startsWith('"') && value.endsWith('"')) {
2600
+ value = value.slice(1, -1);
2601
+ } else if (value.startsWith("'") && value.endsWith("'")) {
2602
+ value = value.slice(1, -1);
2603
+ } else if (value === "true") {
2604
+ value = true;
2605
+ } else if (value === "false") {
2606
+ value = false;
2607
+ } else if (value === "null") {
2608
+ value = null;
2609
+ } else if (!isNaN(Number(value))) {
2610
+ value = Number(value);
2611
+ }
2612
+ }
2613
+ return { field, operator, value };
2614
+ }
2615
+ }
2616
+ return { expression: guard };
2617
+ }
2618
+ function extractHooksFromObject(init, states, actionCounter) {
2619
+ for (const prop of init.properties) {
2620
+ if (!t11.isObjectProperty(prop)) continue;
2621
+ if (!t11.isIdentifier(prop.key) && !t11.isStringLiteral(prop.key)) continue;
2622
+ const hookName = t11.isIdentifier(prop.key) ? prop.key.name : prop.key.value;
2623
+ const hookConfig = t11.isObjectExpression(prop.value) ? extractObjectLiteral(prop.value) : {};
2624
+ const enterMatch = hookName.match(/^on_enter_(.+)$/);
2625
+ const exitMatch = hookName.match(/^on_exit_(.+)$/);
2626
+ if (!enterMatch && !exitMatch) continue;
2627
+ const stateName = enterMatch ? enterMatch[1] : exitMatch[1];
2628
+ const phase = enterMatch ? "on_enter" : "on_exit";
2629
+ if (!states.has(stateName)) {
2630
+ states.set(stateName, {
2631
+ name: stateName,
2632
+ type: "REGULAR",
2633
+ on_enter: [],
2634
+ during: [],
2635
+ on_exit: []
2636
+ });
2637
+ }
2638
+ const state = states.get(stateName);
2639
+ if (hookConfig.emit && typeof hookConfig.emit === "string") {
2640
+ const action = {
2641
+ id: `auto_${++actionCounter.value}`,
2642
+ type: "emit_event",
2643
+ mode: "auto",
2644
+ config: { event: hookConfig.emit }
2645
+ };
2646
+ state[phase].push(action);
2647
+ }
2648
+ if (hookConfig.actions && Array.isArray(hookConfig.actions)) {
2649
+ for (const handler of hookConfig.actions) {
2650
+ if (typeof handler === "string") {
2651
+ const action = {
2652
+ id: `auto_${++actionCounter.value}`,
2653
+ type: "server_action",
2654
+ mode: "auto",
2655
+ config: { handler }
2656
+ };
2657
+ state[phase].push(action);
2658
+ }
2659
+ }
2660
+ }
2661
+ }
2662
+ }
2663
+ function applyFieldOptions(fields, init) {
2664
+ const options = extractObjectLiteral(init);
2665
+ for (const [fieldName, opts] of Object.entries(options)) {
2666
+ const field = fields.find((f) => f.name === fieldName);
2667
+ if (!field) continue;
2668
+ if (opts.scope || opts.persistence || opts.sync) {
2669
+ field.state_home = {
2670
+ scope: opts.scope || "instance",
2671
+ persistence: opts.persistence || "durable",
2672
+ sync: opts.sync || "none"
2673
+ };
2674
+ }
2675
+ if (opts.computed && typeof opts.computed === "string") {
2676
+ field.computed = opts.computed;
2677
+ }
2678
+ if (opts.computed_deps && Array.isArray(opts.computed_deps)) {
2679
+ field.computed_deps = opts.computed_deps.map(String);
2680
+ }
2681
+ if (opts.visible_in_states && Array.isArray(opts.visible_in_states)) {
2682
+ field.visible_in_states = opts.visible_in_states.map(String);
2683
+ }
2684
+ if (opts.editable_in_states && Array.isArray(opts.editable_in_states)) {
2685
+ field.editable_in_states = opts.editable_in_states.map(String);
2686
+ }
2687
+ if (opts.visible_to_roles && Array.isArray(opts.visible_to_roles)) {
2688
+ field.visible_to_roles = opts.visible_to_roles.map(String);
2689
+ }
2690
+ if (opts.editable_by_roles && Array.isArray(opts.editable_by_roles)) {
2691
+ field.editable_by_roles = opts.editable_by_roles.map(String);
2692
+ }
2693
+ if (opts.visible_when && typeof opts.visible_when === "string") {
2694
+ field.visible_when = opts.visible_when;
2695
+ }
2696
+ if (opts.editable_when && typeof opts.editable_when === "string") {
2697
+ field.editable_when = opts.editable_when;
2698
+ }
2699
+ if (opts.validation && typeof opts.validation === "object") {
2700
+ const val = opts.validation;
2701
+ const validation = field.validation || {};
2702
+ if (val.min !== void 0) validation.min = Number(val.min);
2703
+ if (val.max !== void 0) validation.max = Number(val.max);
2704
+ if (val.minLength !== void 0) validation.minLength = Number(val.minLength);
2705
+ if (val.maxLength !== void 0) validation.maxLength = Number(val.maxLength);
2706
+ if (val.options && Array.isArray(val.options)) {
2707
+ validation.options = val.options.map(String);
2708
+ }
2709
+ if (val.rules && Array.isArray(val.rules)) {
2710
+ validation.rules = val.rules.map((r) => ({
2711
+ expression: String(r.expression || ""),
2712
+ message: String(r.message || ""),
2713
+ severity: r.severity === "warning" ? "warning" : "error"
2714
+ }));
2715
+ }
2716
+ field.validation = validation;
2717
+ }
2718
+ }
2719
+ }
2720
+ function extractObjectLiteral(node) {
2721
+ const obj = {};
2722
+ for (const prop of node.properties) {
2723
+ if (!t11.isObjectProperty(prop)) continue;
2724
+ const key = t11.isIdentifier(prop.key) ? prop.key.name : t11.isStringLiteral(prop.key) ? prop.key.value : null;
2725
+ if (!key) continue;
2726
+ obj[key] = extractNodeValue(prop.value);
2727
+ }
2728
+ return obj;
2729
+ }
2730
+ function extractNodeValue(node) {
2731
+ if (t11.isStringLiteral(node)) return node.value;
2732
+ if (t11.isNumericLiteral(node)) return node.value;
2733
+ if (t11.isBooleanLiteral(node)) return node.value;
2734
+ if (t11.isNullLiteral(node)) return null;
2735
+ if (t11.isArrayExpression(node)) {
2736
+ return node.elements.map((el) => {
2737
+ if (!el) return null;
2738
+ if (t11.isSpreadElement(el)) return "[Spread]";
2739
+ return extractNodeValue(el);
2740
+ });
2741
+ }
2742
+ if (t11.isObjectExpression(node)) {
2743
+ return extractObjectLiteral(node);
2744
+ }
2745
+ if (t11.isTemplateLiteral(node)) {
2746
+ return node.quasis.map((q) => q.value.raw).join("${}");
2747
+ }
2748
+ if (t11.isIdentifier(node)) {
2749
+ if (node.name === "undefined") return void 0;
2750
+ return node.name;
2751
+ }
2752
+ if (t11.isConditionalExpression(node)) {
2753
+ return `(${extractNodeValue(node.test)} ? ${extractNodeValue(node.consequent)} : ${extractNodeValue(node.alternate)})`;
2754
+ }
2755
+ if (t11.isBinaryExpression(node)) {
2756
+ return `(${extractNodeValue(node.left)} ${node.operator} ${extractNodeValue(node.right)})`;
2757
+ }
2758
+ if (t11.isLogicalExpression(node)) {
2759
+ return `(${extractNodeValue(node.left)} ${node.operator} ${extractNodeValue(node.right)})`;
2760
+ }
2761
+ if (t11.isMemberExpression(node)) {
2762
+ const obj = t11.isIdentifier(node.object) ? node.object.name : "[object]";
2763
+ const prop = t11.isIdentifier(node.property) ? node.property.name : "[property]";
2764
+ return `${obj}.${prop}`;
2765
+ }
2766
+ if (t11.isTSAsExpression(node)) return extractNodeValue(node.expression);
2767
+ if (t11.isTSNonNullExpression(node)) return extractNodeValue(node.expression);
2768
+ return "[Expression]";
2769
+ }
2770
+ function isModelFile(path, filename) {
2771
+ if (filename && /\/models\//.test(filename)) return true;
2772
+ let hasInterface = false;
2773
+ let hasTransitions = false;
2774
+ let hasDefineModel = false;
2775
+ for (const node of path.node.body) {
2776
+ if (t11.isExportDefaultDeclaration(node)) {
2777
+ const decl = node.declaration;
2778
+ if (t11.isCallExpression(decl) && t11.isIdentifier(decl.callee) && decl.callee.name === "defineModel") {
2779
+ hasDefineModel = true;
2780
+ }
2781
+ }
2782
+ if (t11.isExportNamedDeclaration(node)) {
2783
+ if (t11.isTSInterfaceDeclaration(node.declaration)) {
2784
+ hasInterface = true;
2785
+ }
2786
+ if (t11.isVariableDeclaration(node.declaration)) {
2787
+ for (const decl of node.declaration.declarations) {
2788
+ if (t11.isIdentifier(decl.id) && decl.id.name === "transitions") {
2789
+ hasTransitions = true;
2790
+ }
2791
+ }
2792
+ }
2793
+ }
2794
+ }
2795
+ return hasDefineModel || hasInterface && hasTransitions;
2796
+ }
2797
+ function modelTypeToIRType(modelType) {
2798
+ switch (modelType) {
2799
+ case "string":
2800
+ return "text";
2801
+ case "number":
2802
+ return "number";
2803
+ case "boolean":
2804
+ return "boolean";
2805
+ case "date":
2806
+ return "date";
2807
+ case "array":
2808
+ return "multi_select";
2809
+ case "json":
2810
+ return "json";
2811
+ case "file":
2812
+ return "file";
2813
+ case "relation":
2814
+ return "relation";
2815
+ default:
2816
+ return modelType;
2817
+ }
2818
+ }
2819
+ function modelStateTypeToIR(type) {
2820
+ if (type === "initial") return "START";
2821
+ if (type === "final") return "END";
2822
+ return "REGULAR";
2823
+ }
2824
+ function extractDefineModelCall(programPath, compilerState, actionCounter) {
2825
+ for (const node of programPath.node.body) {
2826
+ if (!t11.isExportDefaultDeclaration(node)) continue;
2827
+ const decl = node.declaration;
2828
+ if (!t11.isCallExpression(decl)) continue;
2829
+ if (!t11.isIdentifier(decl.callee) || decl.callee.name !== "defineModel") continue;
2830
+ if (decl.arguments.length === 0 || !t11.isObjectExpression(decl.arguments[0])) continue;
2831
+ const config = extractObjectLiteral(decl.arguments[0]);
2832
+ if (config.slug) compilerState.metadata.slug = String(config.slug);
2833
+ if (config.version) compilerState.metadata.version = String(config.version);
2834
+ if (config.category) {
2835
+ if (Array.isArray(config.category)) {
2836
+ compilerState.metadata.category = String(config.category[0]);
2837
+ compilerState.metadata.categoryArray = config.category;
2838
+ } else {
2839
+ compilerState.metadata.category = String(config.category);
2840
+ }
2841
+ }
2842
+ if (config.description) compilerState.metadata.description = String(config.description);
2843
+ if (config.fields && typeof config.fields === "object" && !Array.isArray(config.fields)) {
2844
+ const fieldsObj = config.fields;
2845
+ for (const [name, fieldConfig] of Object.entries(fieldsObj)) {
2846
+ const irType = modelTypeToIRType(String(fieldConfig.type || "text"));
2847
+ const field = {
2848
+ name,
2849
+ type: irType,
2850
+ required: fieldConfig.required === true,
2851
+ default_value: fieldConfig.default !== void 0 ? fieldConfig.default : extractDefaultValue2(irType)
2852
+ };
2853
+ if (fieldConfig.enum && Array.isArray(fieldConfig.enum)) {
2854
+ field.validation = { options: fieldConfig.enum.map(String) };
2855
+ }
2856
+ compilerState.fields.push(field);
2857
+ }
2858
+ }
2859
+ if (config.states && typeof config.states === "object" && !Array.isArray(config.states)) {
2860
+ const statesObj = config.states;
2861
+ for (const [name, stateConfig] of Object.entries(statesObj)) {
2862
+ const stateType = modelStateTypeToIR(stateConfig.type);
2863
+ if (!compilerState.states.has(name)) {
2864
+ compilerState.states.set(name, {
2865
+ name,
2866
+ type: stateType,
2867
+ description: stateConfig.description,
2868
+ on_enter: [],
2869
+ during: [],
2870
+ on_exit: []
2871
+ });
2872
+ } else {
2873
+ compilerState.states.get(name).type = stateType;
2874
+ }
2875
+ }
2876
+ }
2877
+ if (config.transitions && typeof config.transitions === "object" && !Array.isArray(config.transitions)) {
2878
+ const transObj = config.transitions;
2879
+ for (const [name, transConfig] of Object.entries(transObj)) {
2880
+ let from = [];
2881
+ if (typeof transConfig.from === "string") {
2882
+ from = [transConfig.from];
2883
+ } else if (Array.isArray(transConfig.from)) {
2884
+ from = transConfig.from.map(String);
2885
+ }
2886
+ const to = String(transConfig.to || "");
2887
+ for (const f of from) {
2888
+ if (!compilerState.states.has(f)) {
2889
+ compilerState.states.set(f, {
2890
+ name: f,
2891
+ type: "REGULAR",
2892
+ on_enter: [],
2893
+ during: [],
2894
+ on_exit: []
2895
+ });
2896
+ }
2897
+ }
2898
+ if (to && !compilerState.states.has(to)) {
2899
+ compilerState.states.set(to, {
2900
+ name: to,
2901
+ type: "REGULAR",
2902
+ on_enter: [],
2903
+ during: [],
2904
+ on_exit: []
2905
+ });
2906
+ }
2907
+ const conditions = [];
2908
+ if (transConfig.guard) {
2909
+ conditions.push(parseGuardExpression(String(transConfig.guard)));
2910
+ }
2911
+ compilerState.transitions.push({
2912
+ name,
2913
+ from,
2914
+ to,
2915
+ description: transConfig.description,
2916
+ conditions: conditions.length > 0 ? conditions : void 0,
2917
+ actions: [],
2918
+ roles: transConfig.roles,
2919
+ auto: transConfig.auto,
2920
+ required_fields: transConfig.required_fields
2921
+ });
2922
+ }
2923
+ }
2924
+ if (config.roles && typeof config.roles === "object" && !Array.isArray(config.roles)) {
2925
+ const rolesObj = config.roles;
2926
+ if (!compilerState.metadata.roles) compilerState.metadata.roles = {};
2927
+ for (const [roleName, roleConfig] of Object.entries(rolesObj)) {
2928
+ compilerState.metadata.roles[roleName] = {
2929
+ description: roleConfig.description,
2930
+ permissions: roleConfig.permissions
2931
+ };
2932
+ }
2933
+ }
2934
+ if (config.metadata && typeof config.metadata === "object") {
2935
+ compilerState.metadata.modelMetadata = config.metadata;
2936
+ }
2937
+ return true;
2938
+ }
2939
+ return false;
2940
+ }
2941
+ function extractModelFile(path, state) {
2942
+ const compilerState = state;
2943
+ if (!compilerState.actionCounter) compilerState.actionCounter = 0;
2944
+ const actionCounter = { value: compilerState.actionCounter };
2945
+ const foundDefineModel = extractDefineModelCall(path, compilerState, actionCounter);
2946
+ if (foundDefineModel) {
2947
+ for (const node of path.node.body) {
2948
+ if (t11.isExportNamedDeclaration(node) && t11.isTSInterfaceDeclaration(node.declaration)) {
2949
+ const interfaceFields = extractFieldsFromInterface(node.declaration);
2950
+ for (const iField of interfaceFields) {
2951
+ const existing = compilerState.fields.find((f) => f.name === iField.name);
2952
+ if (existing) {
2953
+ if (iField.required) existing.required = true;
2954
+ }
2955
+ }
2956
+ if (!compilerState.metadata.name) {
2957
+ compilerState.metadata.name = node.declaration.id.name;
2958
+ }
2959
+ }
2960
+ }
2961
+ compilerState.actionCounter = actionCounter.value;
2962
+ return;
2963
+ }
2964
+ let interfaceName = "";
2965
+ for (const node of path.node.body) {
2966
+ if (!t11.isExportNamedDeclaration(node)) continue;
2967
+ const declaration = node.declaration;
2968
+ if (t11.isTSInterfaceDeclaration(declaration)) {
2969
+ interfaceName = declaration.id.name;
2970
+ const fields = extractFieldsFromInterface(declaration);
2971
+ compilerState.fields.push(...fields);
2972
+ for (const field of fields) {
2973
+ if (field.validation?.options && field.validation.options.length > 0) {
2974
+ for (const option of field.validation.options) {
2975
+ if (!compilerState.states.has(option)) {
2976
+ compilerState.states.set(option, {
2977
+ name: option,
2978
+ type: "REGULAR",
2979
+ on_enter: [],
2980
+ during: [],
2981
+ on_exit: []
2982
+ });
2983
+ }
2984
+ }
2985
+ const firstState = compilerState.states.get(field.validation.options[0]);
2986
+ if (firstState) firstState.type = "START";
2987
+ break;
2988
+ }
2989
+ }
2990
+ if (!compilerState.metadata.name) {
2991
+ compilerState.metadata.name = interfaceName;
2992
+ }
2993
+ }
2994
+ if (t11.isVariableDeclaration(declaration)) {
2995
+ for (const decl of declaration.declarations) {
2996
+ if (!t11.isIdentifier(decl.id) || !decl.init) continue;
2997
+ const varName = decl.id.name;
2998
+ if (varName === "transitions" && t11.isObjectExpression(decl.init)) {
2999
+ const transitions = extractTransitionsFromObject(decl.init, actionCounter);
3000
+ compilerState.transitions.push(...transitions);
3001
+ for (const trans of transitions) {
3002
+ for (const fromState of trans.from) {
3003
+ if (!compilerState.states.has(fromState)) {
3004
+ compilerState.states.set(fromState, {
3005
+ name: fromState,
3006
+ type: "REGULAR",
3007
+ on_enter: [],
3008
+ during: [],
3009
+ on_exit: []
3010
+ });
3011
+ }
3012
+ }
3013
+ if (trans.to && !compilerState.states.has(trans.to)) {
3014
+ compilerState.states.set(trans.to, {
3015
+ name: trans.to,
3016
+ type: "REGULAR",
3017
+ on_enter: [],
3018
+ during: [],
3019
+ on_exit: []
3020
+ });
3021
+ }
3022
+ }
3023
+ }
3024
+ if (varName === "states") {
3025
+ const arrExpr = t11.isTSAsExpression(decl.init) ? decl.init.expression : decl.init;
3026
+ if (t11.isArrayExpression(arrExpr)) {
3027
+ for (const el of arrExpr.elements) {
3028
+ if (!t11.isObjectExpression(el)) continue;
3029
+ let stateName = "";
3030
+ let stateType = "REGULAR";
3031
+ let stateDesc = "";
3032
+ for (const prop of el.properties) {
3033
+ if (!t11.isObjectProperty(prop) || !t11.isIdentifier(prop.key)) continue;
3034
+ if (prop.key.name === "name" && t11.isStringLiteral(prop.value)) {
3035
+ stateName = prop.value.value;
3036
+ } else if (prop.key.name === "type") {
3037
+ const val = t11.isTSAsExpression(prop.value) ? prop.value.expression : prop.value;
3038
+ if (t11.isStringLiteral(val)) stateType = val.value;
3039
+ } else if (prop.key.name === "description" && t11.isStringLiteral(prop.value)) {
3040
+ stateDesc = prop.value.value;
3041
+ }
3042
+ }
3043
+ if (stateName && !compilerState.states.has(stateName)) {
3044
+ compilerState.states.set(stateName, {
3045
+ name: stateName,
3046
+ type: stateType,
3047
+ description: stateDesc || void 0,
3048
+ on_enter: [],
3049
+ during: [],
3050
+ on_exit: []
3051
+ });
3052
+ } else if (stateName) {
3053
+ compilerState.states.get(stateName).type = stateType;
3054
+ }
3055
+ }
3056
+ }
3057
+ }
3058
+ if (varName === "endStates" && t11.isArrayExpression(decl.init)) {
3059
+ for (const el of decl.init.elements) {
3060
+ if (t11.isStringLiteral(el)) {
3061
+ if (!compilerState.states.has(el.value)) {
3062
+ compilerState.states.set(el.value, {
3063
+ name: el.value,
3064
+ type: "END",
3065
+ on_enter: [],
3066
+ during: [],
3067
+ on_exit: []
3068
+ });
3069
+ } else {
3070
+ compilerState.states.get(el.value).type = "END";
3071
+ }
3072
+ }
3073
+ }
3074
+ }
3075
+ if (varName === "cancelledStates" && t11.isArrayExpression(decl.init)) {
3076
+ for (const el of decl.init.elements) {
3077
+ if (t11.isStringLiteral(el)) {
3078
+ if (!compilerState.states.has(el.value)) {
3079
+ compilerState.states.set(el.value, {
3080
+ name: el.value,
3081
+ type: "CANCELLED",
3082
+ on_enter: [],
3083
+ during: [],
3084
+ on_exit: []
3085
+ });
3086
+ } else {
3087
+ compilerState.states.get(el.value).type = "CANCELLED";
3088
+ }
3089
+ }
3090
+ }
3091
+ }
3092
+ if (varName === "hooks" && t11.isObjectExpression(decl.init)) {
3093
+ extractHooksFromObject(decl.init, compilerState.states, actionCounter);
3094
+ }
3095
+ if (varName === "runtime" && t11.isStringLiteral(decl.init)) {
3096
+ compilerState.metadata.runtime = decl.init.value;
3097
+ }
3098
+ if (varName === "fieldOptions" && t11.isObjectExpression(decl.init)) {
3099
+ applyFieldOptions(compilerState.fields, decl.init);
3100
+ }
3101
+ }
3102
+ }
3103
+ }
3104
+ compilerState.actionCounter = actionCounter.value;
3105
+ if (interfaceName && compilerState.metadata.__slugAutoFallback) {
3106
+ compilerState.metadata.slug = interfaceName.replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
3107
+ delete compilerState.metadata.__slugAutoFallback;
3108
+ }
3109
+ }
3110
+
3111
+ // src/babel/extractors/server-action-extractor.ts
3112
+ import * as t12 from "@babel/types";
3113
+ function isServerActionFile(filename) {
3114
+ if (!filename) return false;
3115
+ return /\.server\.(ts|tsx|js|jsx)$/.test(filename);
3116
+ }
3117
+ function extractServerActions(path, state) {
3118
+ const compilerState = state;
3119
+ const serverActions = [];
3120
+ for (const node of path.node.body) {
3121
+ if (t12.isExportNamedDeclaration(node)) {
3122
+ const decl = node.declaration;
3123
+ if (t12.isFunctionDeclaration(decl) && decl.id) {
3124
+ const action = extractFunctionAction(decl);
3125
+ if (action) {
3126
+ const comments = node.leadingComments || decl.leadingComments || [];
3127
+ action.description = extractDescription(comments);
3128
+ serverActions.push(action);
3129
+ }
3130
+ }
3131
+ if (t12.isVariableDeclaration(decl)) {
3132
+ for (const varDecl of decl.declarations) {
3133
+ if (!t12.isIdentifier(varDecl.id)) continue;
3134
+ const init = varDecl.init;
3135
+ if (t12.isArrowFunctionExpression(init) || t12.isFunctionExpression(init)) {
3136
+ const action = {
3137
+ name: varDecl.id.name,
3138
+ async: init.async || false,
3139
+ params: init.params.filter((p) => t12.isIdentifier(p)).map((p) => p.name)
3140
+ };
3141
+ if (init.params.length > 0) {
3142
+ const firstParam = init.params[0];
3143
+ if (t12.isIdentifier(firstParam) && firstParam.typeAnnotation) {
3144
+ action.contextType = extractTypeName(firstParam.typeAnnotation);
3145
+ }
3146
+ }
3147
+ serverActions.push(action);
3148
+ }
3149
+ }
3150
+ }
3151
+ }
3152
+ }
3153
+ if (serverActions.length > 0) {
3154
+ if (!compilerState.metadata) compilerState.metadata = {};
3155
+ compilerState.metadata.serverActions = serverActions;
3156
+ }
3157
+ }
3158
+ function extractFunctionAction(decl) {
3159
+ if (!decl.id) return null;
3160
+ const action = {
3161
+ name: decl.id.name,
3162
+ async: decl.async || false,
3163
+ params: decl.params.filter((p) => t12.isIdentifier(p)).map((p) => p.name)
3164
+ };
3165
+ if (decl.params.length > 0) {
3166
+ const firstParam = decl.params[0];
3167
+ if (t12.isIdentifier(firstParam) && firstParam.typeAnnotation) {
3168
+ action.contextType = extractTypeName(firstParam.typeAnnotation);
3169
+ }
3170
+ }
3171
+ return action;
3172
+ }
3173
+ function extractTypeName(annotation) {
3174
+ if (t12.isTSTypeAnnotation(annotation)) {
3175
+ const typeNode = annotation.typeAnnotation;
3176
+ if (t12.isTSTypeReference(typeNode) && t12.isIdentifier(typeNode.typeName)) {
3177
+ return typeNode.typeName.name;
3178
+ }
3179
+ }
3180
+ return void 0;
3181
+ }
3182
+ function extractDescription(comments) {
3183
+ for (const comment of comments) {
3184
+ if (comment.type !== "CommentBlock") continue;
3185
+ const lines = comment.value.split("\n");
3186
+ for (const line of lines) {
3187
+ const trimmed = line.replace(/^\s*\*\s?/, "").trim();
3188
+ if (trimmed && !trimmed.startsWith("@")) {
3189
+ return trimmed;
3190
+ }
3191
+ }
3192
+ }
3193
+ return void 0;
3194
+ }
3195
+
3196
+ // src/babel/extractors/server-action-hook-extractor.ts
3197
+ import * as t13 from "@babel/types";
3198
+ function extractServerActionHook(path, state) {
3199
+ const args = path.node.arguments;
3200
+ if (args.length < 1) return;
3201
+ const nameArg = args[0];
3202
+ if (!t13.isStringLiteral(nameArg)) return;
3203
+ const compilerState = state;
3204
+ const entry = {
3205
+ name: nameArg.value
3206
+ };
3207
+ if (args.length > 1 && t13.isObjectExpression(args[1])) {
3208
+ for (const prop of args[1].properties) {
3209
+ if (!t13.isObjectProperty(prop) || !t13.isIdentifier(prop.key)) continue;
3210
+ if (prop.key.name === "instanceId" && t13.isStringLiteral(prop.value)) {
3211
+ entry.staticInstanceId = prop.value.value;
3212
+ }
3213
+ }
3214
+ }
3215
+ const parent = path.parentPath;
3216
+ if (parent?.isVariableDeclarator() && t13.isIdentifier(parent.node.id)) {
3217
+ entry.variableName = parent.node.id.name;
3218
+ }
3219
+ if (!compilerState.metadata) compilerState.metadata = {};
3220
+ const meta = compilerState.metadata;
3221
+ if (!meta.serverActionHooks) meta.serverActionHooks = [];
3222
+ meta.serverActionHooks.push(entry);
3223
+ }
3224
+
3225
+ // src/babel/extractors/server-state-extractor.ts
3226
+ import * as t14 from "@babel/types";
3227
+ function extractServerState(path, state) {
3228
+ const args = path.node.arguments;
3229
+ if (args.length < 1) return;
3230
+ const compilerState = state;
3231
+ const entry = {};
3232
+ const instanceArg = args[0];
3233
+ if (t14.isStringLiteral(instanceArg)) {
3234
+ entry.staticInstanceId = instanceArg.value;
3235
+ }
3236
+ if (args.length > 1 && t14.isObjectExpression(args[1])) {
3237
+ for (const prop of args[1].properties) {
3238
+ if (!t14.isObjectProperty(prop) || !t14.isIdentifier(prop.key)) continue;
3239
+ if (prop.key.name === "enabled" && t14.isBooleanLiteral(prop.value)) {
3240
+ if (!prop.value.value) entry.disabled = true;
3241
+ }
3242
+ }
3243
+ }
3244
+ const parent = path.parentPath;
3245
+ if (parent?.isVariableDeclarator()) {
3246
+ if (t14.isIdentifier(parent.node.id)) {
3247
+ entry.variableName = parent.node.id.name;
3248
+ } else if (t14.isObjectPattern(parent.node.id)) {
3249
+ const props = parent.node.id.properties.filter((p) => t14.isObjectProperty(p) && t14.isIdentifier(p.key)).map((p) => p.key.name);
3250
+ if (props.length > 0) {
3251
+ entry.variableName = props.join(", ");
3252
+ }
3253
+ }
3254
+ }
3255
+ if (!compilerState.metadata) compilerState.metadata = {};
3256
+ const meta = compilerState.metadata;
3257
+ if (!meta.serverStateSubscriptions) meta.serverStateSubscriptions = [];
3258
+ meta.serverStateSubscriptions.push(entry);
3259
+ meta.requiresSSE = true;
3260
+ }
3261
+
3262
+ // src/babel/extractors/grammar-island-extractor.ts
3263
+ import * as t15 from "@babel/types";
3264
+ var GRAMMAR_TAGS = /* @__PURE__ */ new Set(["cedar", "sql", "cron", "dmn", "graphql", "jsonpath"]);
3265
+ function parseCedar(source) {
3266
+ const policies = [];
3267
+ const policyRegex = /(permit|forbid)\s*\(([^)]*)\)(?:\s*when\s*\{([^}]*)\})?/g;
3268
+ let match;
3269
+ while ((match = policyRegex.exec(source)) !== null) {
3270
+ const [, effect, scope, condition] = match;
3271
+ const parts = scope.split(",").map((s) => s.trim());
3272
+ const policy = {
3273
+ effect,
3274
+ conditions: []
3275
+ };
3276
+ for (const part of parts) {
3277
+ if (part.startsWith("principal")) {
3278
+ policy.principal = part;
3279
+ } else if (part.startsWith("action")) {
3280
+ policy.action = part;
3281
+ } else if (part.startsWith("resource")) {
3282
+ policy.resource = part;
3283
+ }
3284
+ }
3285
+ if (condition) {
3286
+ policy.conditions.push(condition.trim());
3287
+ }
3288
+ policies.push(policy);
3289
+ }
3290
+ return { policies };
3291
+ }
3292
+ function parseCron(source) {
3293
+ const trimmed = source.trim();
3294
+ const parts = trimmed.split(/\s+/);
3295
+ const valid = parts.length === 5;
3296
+ return {
3297
+ expression: trimmed,
3298
+ fields: {
3299
+ minute: parts[0] || "*",
3300
+ hour: parts[1] || "*",
3301
+ dayOfMonth: parts[2] || "*",
3302
+ month: parts[3] || "*",
3303
+ dayOfWeek: parts[4] || "*"
3304
+ },
3305
+ valid
3306
+ };
3307
+ }
3308
+ function parseDmn(source) {
3309
+ const lines = source.split("\n").map((l) => l.trim()).filter((l) => l.length > 0);
3310
+ const rules = [];
3311
+ const headers = [];
3312
+ let hitPolicy = "first";
3313
+ if (lines.length === 0) return { hitPolicy, rules, headers };
3314
+ const headerLine = lines[0];
3315
+ const headerCells = headerLine.split("|").map((c) => c.trim()).filter((c) => c.length > 0);
3316
+ headers.push(...headerCells);
3317
+ const outputStartIndex = headers.length - 1;
3318
+ let dataStart = 1;
3319
+ if (lines.length > 1 && /^[|\s-]+$/.test(lines[1])) {
3320
+ dataStart = 2;
3321
+ }
3322
+ const hitPolicyMatch = source.match(/@hitPolicy\s*=\s*"?(\w+)"?/);
3323
+ if (hitPolicyMatch) {
3324
+ hitPolicy = hitPolicyMatch[1];
3325
+ }
3326
+ for (let i = dataStart; i < lines.length; i++) {
3327
+ const cells = lines[i].split("|").map((c) => c.trim()).filter((c) => c.length > 0);
3328
+ if (cells.length < headers.length) continue;
3329
+ const inputs = {};
3330
+ const output = {};
3331
+ for (let j = 0; j < headers.length; j++) {
3332
+ if (j >= outputStartIndex) {
3333
+ output[headers[j]] = cells[j];
3334
+ } else {
3335
+ inputs[headers[j]] = cells[j];
3336
+ }
3337
+ }
3338
+ rules.push({ inputs, output });
3339
+ }
3340
+ return { hitPolicy, rules, headers };
3341
+ }
3342
+ function extractGrammarIsland(path, state, slug) {
3343
+ const tag = path.node.tag;
3344
+ if (!t15.isIdentifier(tag)) return;
3345
+ const tagName = tag.name;
3346
+ if (!GRAMMAR_TAGS.has(tagName)) return;
3347
+ const compilerState = state;
3348
+ const quasi = path.node.quasi;
3349
+ let rawSource = "";
3350
+ for (let i = 0; i < quasi.quasis.length; i++) {
3351
+ rawSource += quasi.quasis[i].value.raw;
3352
+ if (i < quasi.expressions.length) {
3353
+ const expr = quasi.expressions[i];
3354
+ if (t15.isIdentifier(expr)) {
3355
+ rawSource += `\${${expr.name}}`;
3356
+ } else {
3357
+ rawSource += `\${expr_${i}}`;
3358
+ }
3359
+ }
3360
+ }
3361
+ rawSource = rawSource.trim();
3362
+ let parsed = void 0;
3363
+ switch (tagName) {
3364
+ case "cedar":
3365
+ parsed = parseCedar(rawSource);
3366
+ break;
3367
+ case "cron":
3368
+ parsed = parseCron(rawSource);
3369
+ break;
3370
+ case "dmn":
3371
+ parsed = parseDmn(rawSource);
3372
+ break;
3373
+ case "sql":
3374
+ parsed = { query: rawSource };
3375
+ break;
3376
+ case "graphql":
3377
+ parsed = { query: rawSource };
3378
+ break;
3379
+ case "jsonpath":
3380
+ parsed = { expression: rawSource };
3381
+ break;
3382
+ }
3383
+ const island = {
3384
+ slug,
3385
+ contextTag: tagName,
3386
+ rawSource,
3387
+ parsed
3388
+ };
3389
+ if (!compilerState.metadata) compilerState.metadata = {};
3390
+ const meta = compilerState.metadata;
3391
+ if (!meta.grammarIslands) meta.grammarIslands = [];
3392
+ meta.grammarIslands.push(island);
3393
+ }
3394
+ function extractGrammarIslands(path, state) {
3395
+ path.traverse({
3396
+ TaggedTemplateExpression(templatePath) {
3397
+ const tag = templatePath.node.tag;
3398
+ if (!t15.isIdentifier(tag) || !GRAMMAR_TAGS.has(tag.name)) return;
3399
+ let slug = "unnamed";
3400
+ const parent = templatePath.parentPath;
3401
+ if (parent && parent.isVariableDeclarator() && t15.isIdentifier(parent.node.id)) {
3402
+ slug = parent.node.id.name;
3403
+ }
3404
+ if (parent && parent.isObjectProperty() && t15.isIdentifier(parent.node.key)) {
3405
+ slug = parent.node.key.name;
3406
+ }
3407
+ if (parent && parent.isObjectProperty() && t15.isStringLiteral(parent.node.key)) {
3408
+ slug = parent.node.key.value;
3409
+ }
3410
+ extractGrammarIsland(templatePath, state, slug);
3411
+ }
3412
+ });
3413
+ }
3414
+
3415
+ // src/babel/extractors/context-extractor.ts
3416
+ import * as t16 from "@babel/types";
3417
+ function hasContextCreation(path) {
3418
+ let found = false;
3419
+ path.traverse({
3420
+ CallExpression(callPath) {
3421
+ if (t16.isIdentifier(callPath.node.callee, { name: "createContext" })) {
3422
+ found = true;
3423
+ callPath.stop();
3424
+ }
3425
+ }
3426
+ });
3427
+ return found;
3428
+ }
3429
+ function extractContextWorkflows(path, state) {
3430
+ const compilerState = state;
3431
+ const contextWorkflows = [];
3432
+ const contextNames = /* @__PURE__ */ new Map();
3433
+ path.traverse({
3434
+ // Detect: const XContext = createContext(...)
3435
+ VariableDeclarator(declPath) {
3436
+ const init = declPath.node.init;
3437
+ if (!init || !t16.isCallExpression(init)) return;
3438
+ if (!t16.isIdentifier(init.callee, { name: "createContext" })) return;
3439
+ if (!t16.isIdentifier(declPath.node.id)) return;
3440
+ const contextName = declPath.node.id.name;
3441
+ contextNames.set(contextName, contextName);
3442
+ const workflow = {
3443
+ name: contextName,
3444
+ fields: [],
3445
+ reducerActions: []
3446
+ };
3447
+ const typeParams = init.typeParameters;
3448
+ if (typeParams && t16.isTSTypeParameterInstantiation(typeParams)) {
3449
+ const typeArg = typeParams.params[0];
3450
+ if (t16.isTSTypeReference(typeArg) && t16.isIdentifier(typeArg.typeName)) {
3451
+ const interfaceName = typeArg.typeName.name;
3452
+ extractFieldsFromInterface2(path, interfaceName, workflow);
3453
+ }
3454
+ }
3455
+ if (init.arguments.length > 0 && t16.isObjectExpression(init.arguments[0])) {
3456
+ workflow.initialState = extractObjectLiteral2(init.arguments[0]);
3457
+ }
3458
+ contextWorkflows.push(workflow);
3459
+ },
3460
+ // Detect useReducer(reducer, initialState) and extract reducer cases
3461
+ CallExpression(callPath) {
3462
+ if (!t16.isIdentifier(callPath.node.callee, { name: "useReducer" })) return;
3463
+ const args = callPath.node.arguments;
3464
+ if (args.length < 1) return;
3465
+ const reducerArg = args[0];
3466
+ if (!t16.isIdentifier(reducerArg)) return;
3467
+ const reducerName = reducerArg.name;
3468
+ const actions = extractReducerActions(path, reducerName);
3469
+ if (contextWorkflows.length > 0) {
3470
+ const lastWorkflow = contextWorkflows[contextWorkflows.length - 1];
3471
+ lastWorkflow.reducerActions = actions;
3472
+ lastWorkflow.reducerName = reducerName;
3473
+ }
3474
+ }
3475
+ });
3476
+ if (contextWorkflows.length > 0) {
3477
+ if (!compilerState.metadata) compilerState.metadata = {};
3478
+ const meta = compilerState.metadata;
3479
+ meta.contextWorkflows = contextWorkflows;
3480
+ }
3481
+ }
3482
+ function extractFieldsFromInterface2(path, interfaceName, workflow) {
3483
+ path.traverse({
3484
+ TSInterfaceDeclaration(ifacePath) {
3485
+ if (!t16.isIdentifier(ifacePath.node.id, { name: interfaceName })) return;
3486
+ for (const prop of ifacePath.node.body.body) {
3487
+ if (!t16.isTSPropertySignature(prop)) continue;
3488
+ if (!t16.isIdentifier(prop.key)) continue;
3489
+ const field = {
3490
+ name: prop.key.name,
3491
+ type: extractTSType(prop.typeAnnotation),
3492
+ optional: prop.optional || false
3493
+ };
3494
+ workflow.fields.push(field);
3495
+ }
3496
+ }
3497
+ });
3498
+ }
3499
+ function extractTSType(annotation) {
3500
+ if (!annotation || !t16.isTSTypeAnnotation(annotation)) return "unknown";
3501
+ const typeNode = annotation.typeAnnotation;
3502
+ if (t16.isTSStringKeyword(typeNode)) return "string";
3503
+ if (t16.isTSNumberKeyword(typeNode)) return "number";
3504
+ if (t16.isTSBooleanKeyword(typeNode)) return "boolean";
3505
+ if (t16.isTSArrayType(typeNode)) return `${extractTSTypeNode(typeNode.elementType)}[]`;
3506
+ if (t16.isTSTypeReference(typeNode) && t16.isIdentifier(typeNode.typeName)) return typeNode.typeName.name;
3507
+ return "unknown";
3508
+ }
3509
+ function extractTSTypeNode(node) {
3510
+ if (t16.isTSStringKeyword(node)) return "string";
3511
+ if (t16.isTSNumberKeyword(node)) return "number";
3512
+ if (t16.isTSBooleanKeyword(node)) return "boolean";
3513
+ if (t16.isTSTypeReference(node) && t16.isIdentifier(node.typeName)) return node.typeName.name;
3514
+ return "unknown";
3515
+ }
3516
+ function extractReducerActions(path, reducerName) {
3517
+ const actions = [];
3518
+ path.traverse({
3519
+ FunctionDeclaration(funcPath) {
3520
+ if (!funcPath.node.id || funcPath.node.id.name !== reducerName) return;
3521
+ funcPath.traverse({
3522
+ SwitchStatement(switchPath) {
3523
+ for (const switchCase of switchPath.node.cases) {
3524
+ if (!switchCase.test) continue;
3525
+ if (t16.isStringLiteral(switchCase.test)) {
3526
+ actions.push({
3527
+ type: switchCase.test.value
3528
+ });
3529
+ }
3530
+ }
3531
+ }
3532
+ });
3533
+ }
3534
+ });
3535
+ return actions;
3536
+ }
3537
+ function extractObjectLiteral2(obj) {
3538
+ const result = {};
3539
+ for (const prop of obj.properties) {
3540
+ if (!t16.isObjectProperty(prop) || !t16.isIdentifier(prop.key)) continue;
3541
+ if (t16.isStringLiteral(prop.value)) result[prop.key.name] = prop.value.value;
3542
+ else if (t16.isNumericLiteral(prop.value)) result[prop.key.name] = prop.value.value;
3543
+ else if (t16.isBooleanLiteral(prop.value)) result[prop.key.name] = prop.value.value;
3544
+ else if (t16.isNullLiteral(prop.value)) result[prop.key.name] = null;
3545
+ else if (t16.isArrayExpression(prop.value)) result[prop.key.name] = [];
3546
+ else if (t16.isObjectExpression(prop.value)) result[prop.key.name] = extractObjectLiteral2(prop.value);
3547
+ }
3548
+ return result;
3549
+ }
3550
+
3551
+ // src/babel/emitters/pure-form-emitter.ts
3552
+ import { normalizeCategory } from "@mindmatrix/player-core";
3553
+
3554
+ // src/babel/emitters/experience-transform.ts
3555
+ function transformToFrontend(node) {
3556
+ const result = {
3557
+ id: node.id
3558
+ };
3559
+ if (node.displayName) {
3560
+ result.displayName = node.displayName;
3561
+ } else if (node.config?.displayName && typeof node.config.displayName === "string") {
3562
+ result.displayName = node.config.displayName;
3563
+ }
3564
+ if (node.experienceId) result.experienceId = node.experienceId;
3565
+ if (node.component) result.component = node.component;
3566
+ if (node.slot) {
3567
+ result.slot = node.slot;
3568
+ } else if (node.config?.slot && typeof node.config.slot === "string") {
3569
+ result.slot = node.config.slot;
3570
+ }
3571
+ if (node.className) {
3572
+ result.className = node.className;
3573
+ } else if (node.config?.className && typeof node.config.className === "string") {
3574
+ result.className = node.config.className;
3575
+ }
3576
+ if (node.style) result.style = node.style;
3577
+ if (node.overrides) result.overrides = node.overrides;
3578
+ if (node.layout) result.layout = node.layout;
3579
+ if (node.dataScope) result.dataScope = node.dataScope;
3580
+ if (node.bindings) result.bindings = node.bindings;
3581
+ if (node.visible_when) result.visible_when = node.visible_when;
3582
+ if (node.config) {
3583
+ const cleaned = { ...node.config };
3584
+ delete cleaned.slot;
3585
+ delete cleaned.className;
3586
+ delete cleaned.displayName;
3587
+ if (Object.keys(cleaned).length > 0) {
3588
+ result.config = cleaned;
3589
+ }
3590
+ }
3591
+ if (node.dataSources && node.dataSources.length > 0) {
3592
+ result.dataSources = node.dataSources.map(transformDataSource);
3593
+ }
3594
+ if (node.children && node.children.length > 0) {
3595
+ result.children = node.children.map(transformToFrontend);
3596
+ }
3597
+ return result;
3598
+ }
3599
+ function transformDataSource(ds) {
3600
+ const d = ds;
3601
+ switch (ds.type) {
3602
+ case "workflow":
3603
+ return {
3604
+ type: "workflow",
3605
+ name: ds.name,
3606
+ slug: ds.slug,
3607
+ query: ds.query,
3608
+ ...d.instanceId && { instanceId: d.instanceId },
3609
+ ...d.entity !== void 0 && { entity: d.entity },
3610
+ ...ds.paginated !== void 0 && { paginated: ds.paginated },
3611
+ ...ds.pageSize !== void 0 && { pageSize: ds.pageSize },
3612
+ ...d.filter && { filter: d.filter },
3613
+ ...d.filters && { filters: d.filters },
3614
+ ...ds.sort && { sort: ds.sort },
3615
+ ...d.search && { search: d.search },
3616
+ ...ds.searchFields && { searchFields: ds.searchFields },
3617
+ ...ds.facets && { facets: ds.facets },
3618
+ ...d.range && { range: d.range },
3619
+ ...d.aggregate && { aggregate: d.aggregate },
3620
+ ...ds.groupBy && { groupBy: ds.groupBy },
3621
+ ...ds.parentInstanceId && { parentInstanceId: ds.parentInstanceId },
3622
+ ...d.autoStart !== void 0 && { autoStart: d.autoStart },
3623
+ ...d.initialData && { initialData: d.initialData },
3624
+ ...d.includeDefinition !== void 0 && { includeDefinition: d.includeDefinition }
3625
+ };
3626
+ case "api":
3627
+ return {
3628
+ type: "api",
3629
+ name: ds.name,
3630
+ endpoint: ds.endpoint,
3631
+ ...d.method && { method: d.method },
3632
+ ...d.mapping && { mapping: d.mapping },
3633
+ ...d.staleTime !== void 0 && { staleTime: d.staleTime }
3634
+ };
3635
+ case "ref":
3636
+ return {
3637
+ type: "ref",
3638
+ name: ds.name,
3639
+ expression: ds.expression
3640
+ };
3641
+ case "static":
3642
+ return {
3643
+ type: "static",
3644
+ name: ds.name,
3645
+ data: ds.data
3646
+ };
3647
+ }
3648
+ }
3649
+
3650
+ // src/babel/emitters/pure-form-emitter.ts
3651
+ var PROP_RULES = {
3652
+ Text: {
3653
+ config: { text: "value" },
3654
+ bindings: { text: "value" }
3655
+ },
3656
+ Heading: {
3657
+ config: { text: "value" },
3658
+ bindings: { text: "value" }
3659
+ },
3660
+ Button: {
3661
+ config: { text: "label" },
3662
+ bindings: { text: "label", onPress: "onClick" }
3663
+ }
3664
+ };
3665
+ var COMPONENT_RENAMES = {
3666
+ Heading: "Text"
3667
+ };
3668
+ var RENAME_EXTRA_CONFIG = {
3669
+ Heading: { variant: "h2" }
3670
+ };
3671
+ function normalizeViewNode(node, fieldNames) {
3672
+ if (!node || typeof node !== "object") return node;
3673
+ const originalComponent = node.component;
3674
+ if (originalComponent && COMPONENT_RENAMES[originalComponent]) {
3675
+ node.component = COMPONENT_RENAMES[originalComponent];
3676
+ const extra = RENAME_EXTRA_CONFIG[originalComponent];
3677
+ if (extra) {
3678
+ node.config = { ...extra, ...node.config || {} };
3679
+ }
3680
+ }
3681
+ const rules = PROP_RULES[originalComponent || ""] || PROP_RULES[node.component || ""];
3682
+ if (rules) {
3683
+ if (node.config && rules.config) {
3684
+ for (const [from, to] of Object.entries(rules.config)) {
3685
+ if (from in node.config && !(to in node.config)) {
3686
+ node.config[to] = node.config[from];
3687
+ delete node.config[from];
3688
+ }
3689
+ }
3690
+ }
3691
+ if (node.bindings && rules.bindings) {
3692
+ for (const [from, to] of Object.entries(rules.bindings)) {
3693
+ if (from in node.bindings && !(to in node.bindings)) {
3694
+ node.bindings[to] = node.bindings[from];
3695
+ delete node.bindings[from];
3696
+ }
3697
+ }
3698
+ }
3699
+ }
3700
+ if (node.config && fieldNames.size > 0) {
3701
+ for (const [prop, val] of Object.entries(node.config)) {
3702
+ if (typeof val !== "string") continue;
3703
+ const templateRe = /\{\{(\w+)\}\}|\{(\w+)\}/g;
3704
+ let match;
3705
+ const refs = [];
3706
+ while ((match = templateRe.exec(val)) !== null) {
3707
+ const field = match[1] || match[2];
3708
+ if (fieldNames.has(field)) {
3709
+ refs.push({ full: match[0], field, index: match.index });
3710
+ }
3711
+ }
3712
+ if (refs.length > 0) {
3713
+ const parts = [];
3714
+ let lastEnd = 0;
3715
+ for (const ref of refs) {
3716
+ const before = val.slice(lastEnd, ref.index);
3717
+ if (before) parts.push(`"${before}"`);
3718
+ parts.push(`$instance.state_data.${ref.field}`);
3719
+ lastEnd = ref.index + ref.full.length;
3720
+ }
3721
+ const trailing = val.slice(lastEnd);
3722
+ if (trailing) parts.push(`"${trailing}"`);
3723
+ const expr = `$fn.concat(${parts.join(", ")})`;
3724
+ if (!node.bindings) node.bindings = {};
3725
+ node.bindings[prop] = expr;
3726
+ delete node.config[prop];
3727
+ }
3728
+ }
3729
+ }
3730
+ if (node.bindings) {
3731
+ for (const [prop, expr] of Object.entries(node.bindings)) {
3732
+ if (typeof expr !== "string") continue;
3733
+ if (expr.endsWith(".fire")) {
3734
+ const transitionName = expr.slice(0, -5);
3735
+ node.bindings[prop] = `$action.transition("${transitionName}")`;
3736
+ continue;
3737
+ }
3738
+ if (fieldNames.size > 0 && expr.startsWith("$instance.")) {
3739
+ const field = expr.slice("$instance.".length);
3740
+ if (fieldNames.has(field)) {
3741
+ node.bindings[prop] = `$instance.state_data.${field}`;
3742
+ }
3743
+ }
3744
+ }
3745
+ }
3746
+ if (node.children && Array.isArray(node.children)) {
3747
+ node.children = node.children.map((child) => normalizeViewNode(child, fieldNames));
3748
+ if (node.children.length === 1 && node.children[0].component === "Text") {
3749
+ const textChild = node.children[0];
3750
+ const configKeys = textChild.config ? Object.keys(textChild.config).filter((k) => k !== "value") : [];
3751
+ const bindingKeys = textChild.bindings ? Object.keys(textChild.bindings).filter((k) => k !== "value") : [];
3752
+ const isSimple = !textChild.style && !textChild.className && !textChild.children && !textChild.visible_when && configKeys.length === 0 && bindingKeys.length === 0;
3753
+ if (isSimple) {
3754
+ const parentRules = PROP_RULES[node.component || ""];
3755
+ const configKey = parentRules?.config?.text || "text";
3756
+ const bindingKey = parentRules?.bindings?.text || "text";
3757
+ if (textChild.config?.value && !textChild.bindings) {
3758
+ if (!node.config) node.config = {};
3759
+ node.config[configKey] = textChild.config.value;
3760
+ delete node.children;
3761
+ } else if (textChild.bindings?.value && !textChild.config?.value) {
3762
+ if (!node.bindings) node.bindings = {};
3763
+ node.bindings[bindingKey] = textChild.bindings.value;
3764
+ delete node.children;
3765
+ }
3766
+ }
3767
+ }
3768
+ }
3769
+ return node;
3770
+ }
3771
+ function toSnakeCase2(str) {
3772
+ return str.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "");
3773
+ }
3774
+ function convertAction(action) {
3775
+ return {
3776
+ id: action.id,
3777
+ action_type: action.type,
3778
+ config: action.config || {},
3779
+ ...action.condition && { condition: action.condition }
3780
+ };
3781
+ }
3782
+ function convertDuringAction(during) {
3783
+ return {
3784
+ id: during.id,
3785
+ type: during.type,
3786
+ ...during.interval_ms !== void 0 && { interval_ms: during.interval_ms },
3787
+ ...during.cron && { cron: during.cron },
3788
+ ...during.delay_ms !== void 0 && { delay_ms: during.delay_ms },
3789
+ actions: during.actions.map(convertAction),
3790
+ ...during.condition && { condition: during.condition }
3791
+ };
3792
+ }
3793
+ function convertOnEvent(sub) {
3794
+ return {
3795
+ match: sub.match,
3796
+ ...sub.description && { description: sub.description },
3797
+ conditions: sub.conditions || [],
3798
+ actions: sub.actions.map((a) => ({
3799
+ type: a.type,
3800
+ ...a.field && { field: a.field },
3801
+ ...a.expression && { expression: a.expression },
3802
+ ...a.key && { key: a.key },
3803
+ ...a.message && { message: a.message },
3804
+ ...a.config && { config: a.config },
3805
+ ...a.conditions && a.conditions.length > 0 && { conditions: a.conditions }
3806
+ }))
3807
+ };
3808
+ }
3809
+ function emitIR(extracted) {
3810
+ const { slug, name, version, description, category, fields, states, transitions } = extracted;
3811
+ const stateArray = Array.from(states.values());
3812
+ if (stateArray.length === 0) {
3813
+ stateArray.push({
3814
+ name: "draft",
3815
+ type: "START",
3816
+ on_enter: [],
3817
+ during: [],
3818
+ on_exit: []
3819
+ });
3820
+ } else {
3821
+ const hasStart = stateArray.some((s) => s.type === "START");
3822
+ if (!hasStart && stateArray.length > 0) {
3823
+ stateArray[0].type = "START";
3824
+ }
3825
+ }
3826
+ inferTransitionStates(transitions, states);
3827
+ const stateNames = new Set(stateArray.map((s) => s.name));
3828
+ for (const transition of transitions) {
3829
+ if (transition.to && !stateNames.has(transition.to)) {
3830
+ stateArray.push({
3831
+ name: transition.to,
3832
+ type: "REGULAR",
3833
+ on_enter: [],
3834
+ during: [],
3835
+ on_exit: []
3836
+ });
3837
+ stateNames.add(transition.to);
3838
+ }
3839
+ }
3840
+ const fieldNames = new Set(fields.map((f) => f.name));
3841
+ let normalizedView;
3842
+ if (extracted.experience) {
3843
+ normalizedView = normalizeViewNode(
3844
+ JSON.parse(JSON.stringify(extracted.experience)),
3845
+ fieldNames
3846
+ );
3847
+ const dataSources = extracted.extraMetadata?.dataSources;
3848
+ if (dataSources && dataSources.length > 0) {
3849
+ normalizedView.dataSources = dataSources;
3850
+ }
3851
+ if (fields.length > 0) {
3852
+ const localDefaults = {};
3853
+ for (const f of fields) {
3854
+ localDefaults[toSnakeCase2(f.name)] = f.default_value ?? null;
3855
+ }
3856
+ if (!normalizedView.config) normalizedView.config = {};
3857
+ normalizedView.config.localDefaults = localDefaults;
3858
+ }
3859
+ }
3860
+ const metadata = {
3861
+ stable_id: `def-${slug}`,
3862
+ provenance: {
3863
+ frontend: "react-compiler",
3864
+ source: `${slug}.workflow.tsx`,
3865
+ compiler_version: "1.0.0"
3866
+ }
3867
+ };
3868
+ if (extracted.fieldWatchers && extracted.fieldWatchers.length > 0) {
3869
+ metadata.fieldWatchers = extracted.fieldWatchers;
3870
+ }
3871
+ if (extracted.transitionEffects && extracted.transitionEffects.length > 0) {
3872
+ metadata.transitionEffects = extracted.transitionEffects;
3873
+ }
3874
+ if (extracted.extraMetadata) {
3875
+ for (const [key, value] of Object.entries(extracted.extraMetadata)) {
3876
+ metadata[key] = value;
3877
+ }
3878
+ }
3879
+ if (extracted.experience) {
3880
+ metadata.experience = extracted.experience;
3881
+ }
3882
+ const ir = {
3883
+ slug,
3884
+ name,
3885
+ version,
3886
+ description,
3887
+ category,
3888
+ fields,
3889
+ states: stateArray,
3890
+ transitions,
3891
+ roles: [],
3892
+ tags: [],
3893
+ metadata
3894
+ };
3895
+ if (normalizedView) {
3896
+ ir.views = { default: normalizedView };
3897
+ ir.frontendViews = { default: transformToFrontend(normalizedView) };
3898
+ }
3899
+ if (extracted.events && extracted.events.length > 0) {
3900
+ ir.on_event = extracted.events;
3901
+ }
3902
+ if (extracted.grammarIslands && extracted.grammarIslands.length > 0) {
3903
+ const extensions = {};
3904
+ for (const island of extracted.grammarIslands) {
3905
+ const key = island.contextTag;
3906
+ if (!extensions[key]) extensions[key] = [];
3907
+ extensions[key].push(island);
3908
+ }
3909
+ ir.extensions = extensions;
3910
+ }
3911
+ return ir;
3912
+ }
3913
+ var emitPureForm = emitIR;
3914
+ function liftAction(action) {
3915
+ const parts = [];
3916
+ if (action.config) {
3917
+ for (const [key, value] of Object.entries(action.config)) {
3918
+ parts.push({
3919
+ slug: key,
3920
+ category: ["atom"],
3921
+ parts: [{ slug: String(value), category: ["atom"] }]
3922
+ });
3923
+ }
3924
+ }
3925
+ if (action.condition) {
3926
+ parts.push({
3927
+ slug: "condition",
3928
+ category: ["expression", "guard"],
3929
+ parts: [{ slug: action.condition, category: ["binding"] }]
3930
+ });
3931
+ }
3932
+ return {
3933
+ slug: action.id,
3934
+ category: normalizeCategory("expression", "mutation", action.type),
3935
+ parts: parts.length > 0 ? parts : void 0
3936
+ };
3937
+ }
3938
+ function liftSchedule(during) {
3939
+ const parts = [];
3940
+ if (during.interval_ms !== void 0) {
3941
+ parts.push({
3942
+ slug: "every",
3943
+ category: ["atom"],
3944
+ parts: [{ slug: String(during.interval_ms), category: ["literal"] }]
3945
+ });
3946
+ }
3947
+ if (during.cron) {
3948
+ parts.push({
3949
+ slug: "cron",
3950
+ category: ["atom"],
3951
+ parts: [{ slug: during.cron, category: ["literal"] }]
3952
+ });
3953
+ }
3954
+ if (during.delay_ms !== void 0) {
3955
+ parts.push({
3956
+ slug: "delay",
3957
+ category: ["atom"],
3958
+ parts: [{ slug: String(during.delay_ms), category: ["literal"] }]
3959
+ });
3960
+ }
3961
+ for (const action of during.actions) {
3962
+ parts.push({
3963
+ slug: "do",
3964
+ category: normalizeCategory("expression", "mutation", action.type),
3965
+ parts: Object.entries(action.config || {}).map(([key, value]) => ({
3966
+ slug: key,
3967
+ category: ["atom"],
3968
+ parts: [{ slug: String(value), category: ["atom"] }]
3969
+ }))
3970
+ });
3971
+ }
3972
+ return {
3973
+ slug: during.id,
3974
+ category: normalizeCategory("schedule", during.type),
3975
+ parts
3976
+ };
3977
+ }
3978
+ function liftState(state) {
3979
+ const parts = [];
3980
+ const tags = [];
3981
+ if (state.type === "START") tags.push("start");
3982
+ if (state.type === "END") tags.push("end");
3983
+ if (state.type === "CANCELLED") tags.push("cancelled");
3984
+ if (state.on_enter && state.on_enter.length > 0) {
3985
+ parts.push({
3986
+ slug: "on_enter",
3987
+ category: normalizeCategory("expression", "sequence"),
3988
+ parts: state.on_enter.map(liftAction)
3989
+ });
3990
+ }
3991
+ if (state.on_exit && state.on_exit.length > 0) {
3992
+ parts.push({
3993
+ slug: "on_exit",
3994
+ category: normalizeCategory("expression", "sequence"),
3995
+ parts: state.on_exit.map(liftAction)
3996
+ });
3997
+ }
3998
+ if (state.during && state.during.length > 0) {
3999
+ for (const d of state.during) {
4000
+ parts.push(liftSchedule(d));
4001
+ }
4002
+ }
4003
+ return {
4004
+ slug: state.name,
4005
+ category: normalizeCategory("state", ...tags),
4006
+ parts: parts.length > 0 ? parts : void 0
4007
+ };
4008
+ }
4009
+ function liftTransition(transition) {
4010
+ const parts = [];
4011
+ if (transition.from && transition.from.length > 0) {
4012
+ parts.push({
4013
+ slug: "from",
4014
+ category: ["part"],
4015
+ parts: transition.from.map((s) => ({ slug: s, category: ["ref"] }))
4016
+ });
4017
+ }
4018
+ if (transition.to) {
4019
+ parts.push({
4020
+ slug: "to",
4021
+ category: ["part"],
4022
+ parts: [{ slug: transition.to, category: ["ref"] }]
4023
+ });
4024
+ }
4025
+ if (transition.actions && transition.actions.length > 0) {
4026
+ parts.push({
4027
+ slug: "actions",
4028
+ category: normalizeCategory("expression", "sequence"),
4029
+ parts: transition.actions.map(liftAction)
4030
+ });
4031
+ }
4032
+ if (transition.conditions && transition.conditions.length > 0) {
4033
+ parts.push({
4034
+ slug: "conditions",
4035
+ category: normalizeCategory("expression", "guard"),
4036
+ parts: transition.conditions.map((c, i) => ({
4037
+ slug: `condition_${i}`,
4038
+ category: normalizeCategory("expression", c.type || "condition"),
4039
+ parts: c.expression ? [{ slug: c.expression, category: ["binding"] }] : void 0
4040
+ }))
4041
+ });
4042
+ }
4043
+ return {
4044
+ slug: transition.name,
4045
+ category: ["transition"],
4046
+ parts
4047
+ };
4048
+ }
4049
+ function liftField(field) {
4050
+ const parts = [];
4051
+ if (field.label) {
4052
+ parts.push({
4053
+ slug: "label",
4054
+ category: ["atom"],
4055
+ parts: [{ slug: field.label, category: ["atom"] }]
4056
+ });
4057
+ }
4058
+ if (field.default_value !== void 0) {
4059
+ parts.push({
4060
+ slug: "default",
4061
+ category: ["atom"],
4062
+ parts: [{ slug: String(field.default_value), category: ["literal"] }]
4063
+ });
4064
+ }
4065
+ if (field.required) {
4066
+ parts.push({ slug: "required", category: ["atom"], parts: [{ slug: "true", category: ["literal"] }] });
4067
+ }
4068
+ if (field.computed) {
4069
+ parts.push({
4070
+ slug: "computed",
4071
+ category: ["expression"],
4072
+ parts: [{ slug: field.computed, category: ["binding"] }]
4073
+ });
4074
+ }
4075
+ return {
4076
+ slug: field.name,
4077
+ category: normalizeCategory("field", field.type),
4078
+ parts: parts.length > 0 ? parts : void 0
4079
+ };
4080
+ }
4081
+ function liftView(node) {
4082
+ const componentTag = (node.component || "stack").toLowerCase().replace(/\s+/g, "-");
4083
+ const parts = [];
4084
+ if (node.config) {
4085
+ for (const [key, value] of Object.entries(node.config)) {
4086
+ parts.push({
4087
+ slug: key,
4088
+ category: ["atom"],
4089
+ parts: [{ slug: String(value), category: ["atom"] }]
4090
+ });
4091
+ }
4092
+ }
4093
+ if (node.bindings) {
4094
+ for (const [key, expr] of Object.entries(node.bindings)) {
4095
+ if (key === "onClick" || key === "onPress") {
4096
+ const match = typeof expr === "string" ? expr.match(/\$action\.transition\("(.+?)"\)/) : null;
4097
+ if (match) {
4098
+ parts.push({
4099
+ slug: key,
4100
+ category: normalizeCategory("expression", "effect", "transition"),
4101
+ parts: [{ slug: match[1], category: ["ref"] }]
4102
+ });
4103
+ } else {
4104
+ parts.push({
4105
+ slug: key,
4106
+ category: normalizeCategory("expression", "binding"),
4107
+ parts: [{ slug: String(expr), category: ["binding"] }]
4108
+ });
4109
+ }
4110
+ } else {
4111
+ parts.push({
4112
+ slug: key,
4113
+ category: normalizeCategory("expression", "binding"),
4114
+ parts: [{ slug: String(expr), category: ["binding"] }]
4115
+ });
4116
+ }
4117
+ }
4118
+ }
4119
+ if (node.visible_when) {
4120
+ const eqMatch = node.visible_when.match(/\$fn\.eq\((.+?),\s*"(.+?)"\)/);
4121
+ if (eqMatch) {
4122
+ parts.push({
4123
+ slug: "visible_when",
4124
+ category: normalizeCategory("expression", "guard"),
4125
+ parts: [{
4126
+ slug: "body",
4127
+ category: normalizeCategory("expression", "eq"),
4128
+ parts: [
4129
+ { slug: "lhs", category: normalizeCategory("expression", "path"), parts: [{ slug: eqMatch[1], category: ["binding"] }] },
4130
+ { slug: "rhs", category: normalizeCategory("expression", "literal"), parts: [{ slug: eqMatch[2], category: ["literal"] }] }
4131
+ ]
4132
+ }]
4133
+ });
4134
+ } else {
4135
+ parts.push({
4136
+ slug: "visible_when",
4137
+ category: normalizeCategory("expression", "guard"),
4138
+ parts: [{ slug: node.visible_when, category: ["binding"] }]
4139
+ });
4140
+ }
4141
+ }
4142
+ if (node.children && node.children.length > 0) {
4143
+ for (const child of node.children) {
4144
+ parts.push(liftView(child));
4145
+ }
4146
+ }
4147
+ return {
4148
+ slug: node.id,
4149
+ category: normalizeCategory("view", componentTag),
4150
+ parts: parts.length > 0 ? parts : void 0
4151
+ };
4152
+ }
4153
+ function emitCanonical(extracted, sourceFilename) {
4154
+ const ir = emitIR(extracted);
4155
+ const parts = [];
4156
+ for (const state of ir.states) {
4157
+ parts.push(liftState(state));
4158
+ }
4159
+ for (const transition of ir.transitions) {
4160
+ parts.push(liftTransition(transition));
4161
+ }
4162
+ for (const field of ir.fields) {
4163
+ parts.push(liftField(field));
4164
+ }
4165
+ if (extracted.experience) {
4166
+ parts.push(liftView(extracted.experience));
4167
+ }
4168
+ parts.push({
4169
+ slug: "manifest",
4170
+ category: normalizeCategory("meta", "manifest"),
4171
+ parts: [
4172
+ {
4173
+ slug: "workflows",
4174
+ category: ["ref-list"],
4175
+ parts: [{ slug: ir.slug, category: ["ref"] }]
4176
+ },
4177
+ {
4178
+ slug: "route",
4179
+ category: ["route"],
4180
+ parts: [
4181
+ { slug: "path", category: ["atom"], parts: [{ slug: `/${ir.slug}`, category: ["atom"] }] },
4182
+ { slug: "label", category: ["atom"], parts: [{ slug: ir.name, category: ["atom"] }] }
4183
+ ]
4184
+ }
4185
+ ]
4186
+ });
4187
+ const category = extracted.category;
4188
+ let categoryArray;
4189
+ if (category.includes("/")) {
4190
+ const [primary, ...tags] = category.split("/");
4191
+ categoryArray = normalizeCategory(primary, ...tags);
4192
+ } else {
4193
+ categoryArray = [category];
4194
+ }
4195
+ return {
4196
+ slug: ir.slug,
4197
+ category: categoryArray,
4198
+ parts,
4199
+ metadata: {
4200
+ stable_id: `def-${ir.slug}`,
4201
+ provenance: {
4202
+ frontend: "react-compiler",
4203
+ source: sourceFilename || `${ir.slug}.workflow.tsx`,
4204
+ compiler_version: "1.0.0",
4205
+ compiled_at: (/* @__PURE__ */ new Date()).toISOString()
4206
+ }
4207
+ }
4208
+ };
4209
+ }
4210
+ function emitCompiledOutput(extracted, sourceFilename) {
4211
+ return {
4212
+ canonical: emitCanonical(extracted, sourceFilename),
4213
+ ir: emitIR(extracted)
4214
+ };
4215
+ }
4216
+ function foldTextContent(node) {
4217
+ if (!node || typeof node !== "object") return node;
4218
+ if (node.children && Array.isArray(node.children)) {
4219
+ node.children = node.children.map((child) => foldTextContent(child));
4220
+ if (node.children.length === 1 && node.children[0].component === "Text") {
4221
+ const textChild = node.children[0];
4222
+ const configKeys = textChild.config ? Object.keys(textChild.config).filter((k) => k !== "value") : [];
4223
+ const bindingKeys = textChild.bindings ? Object.keys(textChild.bindings).filter((k) => k !== "value") : [];
4224
+ const isSimple = !textChild.style && !textChild.className && !textChild.children && !textChild.visible_when && configKeys.length === 0 && bindingKeys.length === 0;
4225
+ if (isSimple) {
4226
+ const parentRules = PROP_RULES[node.component || ""];
4227
+ const configKey = parentRules?.config?.text || "text";
4228
+ const bindingKey = parentRules?.bindings?.text || "text";
4229
+ if (textChild.config?.value && !textChild.bindings) {
4230
+ if (!node.config) node.config = {};
4231
+ node.config[configKey] = textChild.config.value;
4232
+ node.children = void 0;
4233
+ } else if (textChild.bindings?.value && !textChild.config?.value) {
4234
+ if (!node.bindings) node.bindings = {};
4235
+ node.bindings[bindingKey] = textChild.bindings.value;
4236
+ node.children = void 0;
4237
+ }
4238
+ }
4239
+ }
4240
+ if (node.children && node.children.length === 0) {
4241
+ node.children = void 0;
4242
+ }
4243
+ }
4244
+ if (!node.children) delete node.children;
4245
+ return node;
4246
+ }
4247
+ function emitWorkflowDefinition(extracted) {
4248
+ const ir = emitIR(extracted);
4249
+ const states = ir.states.map((s) => ({
4250
+ name: s.name,
4251
+ state_type: s.type,
4252
+ description: s.description || "",
4253
+ on_enter: (s.on_enter || []).map(convertAction),
4254
+ on_exit: (s.on_exit || []).map(convertAction),
4255
+ during: (s.during || []).map(convertDuringAction),
4256
+ on_event: (s.on_event || []).map(convertOnEvent)
4257
+ }));
4258
+ const fields = ir.fields.map((f) => ({
4259
+ name: toSnakeCase2(f.name),
4260
+ field_type: f.type,
4261
+ label: f.label || f.name.replace(/([A-Z])/g, " $1").replace(/^./, (c) => c.toUpperCase()),
4262
+ required: f.required || false,
4263
+ default_value: f.default_value ?? null,
4264
+ ...f.validation && { validation: f.validation },
4265
+ ...f.computed && { computed: f.computed },
4266
+ ...f.computed_deps && { computed_deps: f.computed_deps },
4267
+ ...f.visible_in_states && { visible_in_states: f.visible_in_states },
4268
+ ...f.editable_in_states && { editable_in_states: f.editable_in_states },
4269
+ ...f.visible_to_roles && { visible_to_roles: f.visible_to_roles },
4270
+ ...f.editable_by_roles && { editable_by_roles: f.editable_by_roles },
4271
+ ...f.visible_when && { visible_when: f.visible_when },
4272
+ ...f.editable_when && { editable_when: f.editable_when },
4273
+ ...f.state_home && { state_home: f.state_home }
4274
+ }));
4275
+ const transitions = ir.transitions.map((t18) => ({
4276
+ name: t18.name,
4277
+ from: t18.from,
4278
+ to: t18.to,
4279
+ description: t18.description || "",
4280
+ roles: t18.roles || [],
4281
+ auto: t18.auto || false,
4282
+ conditions: t18.conditions || [],
4283
+ actions: (t18.actions || []).map(convertAction),
4284
+ required_fields: t18.required_fields || [],
4285
+ priority: 0
4286
+ }));
4287
+ const state_data = {};
4288
+ for (const f of fields) {
4289
+ state_data[f.name] = f.default_value;
4290
+ }
4291
+ const on_event = (ir.on_event || []).map(convertOnEvent);
4292
+ let views;
4293
+ if (extracted.experience) {
4294
+ const viewRoot = foldTextContent(JSON.parse(JSON.stringify(extracted.experience)));
4295
+ const dataSources = extracted.extraMetadata?.dataSources;
4296
+ if (dataSources && dataSources.length > 0) {
4297
+ viewRoot.dataSources = dataSources;
4298
+ }
4299
+ if (extracted.fields.length > 0) {
4300
+ const localDefaults = {};
4301
+ for (const f of extracted.fields) {
4302
+ localDefaults[toSnakeCase2(f.name)] = f.default_value ?? null;
4303
+ }
4304
+ if (!viewRoot.config) viewRoot.config = {};
4305
+ viewRoot.config.localDefaults = localDefaults;
4306
+ }
4307
+ views = { default: viewRoot };
4308
+ }
4309
+ return {
4310
+ id: `def-${ir.slug}`,
4311
+ slug: ir.slug,
4312
+ name: ir.name,
4313
+ version: ir.version,
4314
+ description: ir.description || "",
4315
+ category: ir.category,
4316
+ states,
4317
+ transitions,
4318
+ fields,
4319
+ state_data,
4320
+ roles: [],
4321
+ child_definitions: [],
4322
+ tags: ir.tags || [],
4323
+ metadata: ir.metadata || {},
4324
+ ...views && { views },
4325
+ ...on_event.length > 0 && { on_event },
4326
+ config: {},
4327
+ is_immutable: false
4328
+ };
4329
+ }
4330
+ function compilerStateToWorkflow(state, metadata) {
4331
+ const fieldWatchers = metadata.fieldWatchers || [];
4332
+ const transitionEffects = metadata.transitionEffects || [];
4333
+ const grammarIslands = metadata.grammarIslands || [];
4334
+ const knownKeys = /* @__PURE__ */ new Set(["slug", "name", "version", "description", "category", "fieldWatchers", "transitionEffects", "grammarIslands", "__modelImports", "__modelImportSlugs", "__slugAutoFallback"]);
4335
+ const extraMetadata = {};
4336
+ for (const [key, value] of Object.entries(metadata)) {
4337
+ if (!knownKeys.has(key) && value !== void 0) {
4338
+ extraMetadata[key] = value;
4339
+ }
4340
+ }
4341
+ return {
4342
+ slug: metadata.slug || "workflow",
4343
+ name: metadata.name || "Workflow",
4344
+ version: metadata.version || "0.1.0",
4345
+ description: metadata.description,
4346
+ category: metadata.category || "workflow",
4347
+ fields: state.fields || [],
4348
+ states: state.states || /* @__PURE__ */ new Map(),
4349
+ transitions: state.transitions || [],
4350
+ events: state.events || [],
4351
+ experience: state.experience,
4352
+ fieldWatchers: fieldWatchers.length > 0 ? fieldWatchers : void 0,
4353
+ transitionEffects: transitionEffects.length > 0 ? transitionEffects : void 0,
4354
+ grammarIslands: grammarIslands.length > 0 ? grammarIslands : void 0,
4355
+ errors: state.errors,
4356
+ warnings: state.warnings,
4357
+ extraMetadata: Object.keys(extraMetadata).length > 0 ? extraMetadata : void 0
4358
+ };
4359
+ }
4360
+
4361
+ // src/babel/visitor.ts
4362
+ function resolveSlugArg(args, state) {
4363
+ if (args.length < 1) return null;
4364
+ const slugArg = args[0];
4365
+ if (t17.isStringLiteral(slugArg)) return slugArg.value;
4366
+ if (t17.isIdentifier(slugArg)) {
4367
+ const compilerState = state;
4368
+ const meta = compilerState.metadata;
4369
+ const resolvedSlugs = meta.__modelImportSlugs;
4370
+ if (resolvedSlugs?.[slugArg.name]) return resolvedSlugs[slugArg.name];
4371
+ const importSources = meta.__modelImports;
4372
+ if (importSources?.[slugArg.name]) {
4373
+ const importPath = importSources[slugArg.name];
4374
+ const filename = importPath.split("/").pop() || "";
4375
+ return filename.replace(/\.(ts|tsx|js|jsx)$/, "").replace(/\.model$/, "").replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
4376
+ }
4377
+ }
4378
+ return null;
4379
+ }
4380
+ function extractQueryDataSource(path, state) {
4381
+ const args = path.node.arguments;
4382
+ if (args.length < 1) return;
4383
+ const slug = resolveSlugArg(args, state);
4384
+ if (!slug) return;
4385
+ const compilerState = state;
4386
+ const dataSource = {
4387
+ type: "workflow",
4388
+ name: slug,
4389
+ slug,
4390
+ query: "list"
4391
+ };
4392
+ if (args.length > 1 && t17.isObjectExpression(args[1])) {
4393
+ for (const prop of args[1].properties) {
4394
+ if (!t17.isObjectProperty(prop) || !t17.isIdentifier(prop.key)) continue;
4395
+ const key = prop.key.name;
4396
+ const val = prop.value;
4397
+ switch (key) {
4398
+ case "limit":
4399
+ if (t17.isNumericLiteral(val)) {
4400
+ dataSource.pageSize = val.value;
4401
+ dataSource.paginated = true;
4402
+ }
4403
+ break;
4404
+ case "orderBy":
4405
+ if (t17.isStringLiteral(val)) {
4406
+ dataSource.sort = val.value;
4407
+ }
4408
+ break;
4409
+ case "order":
4410
+ if (t17.isStringLiteral(val) && dataSource.sort) {
4411
+ dataSource.sort = `${dataSource.sort}:${val.value}`;
4412
+ }
4413
+ break;
4414
+ case "search":
4415
+ if (t17.isStringLiteral(val)) {
4416
+ dataSource.search = val.value;
4417
+ }
4418
+ break;
4419
+ case "searchFields":
4420
+ if (t17.isArrayExpression(val)) {
4421
+ dataSource.searchFields = val.elements.filter((el) => t17.isStringLiteral(el)).map((el) => el.value);
4422
+ }
4423
+ break;
4424
+ case "filter":
4425
+ if (t17.isObjectExpression(val)) {
4426
+ const filter = {};
4427
+ for (const fp of val.properties) {
4428
+ if (t17.isObjectProperty(fp) && t17.isIdentifier(fp.key) && t17.isStringLiteral(fp.value)) {
4429
+ filter[fp.key.name] = fp.value.value;
4430
+ }
4431
+ }
4432
+ if (Object.keys(filter).length > 0) {
4433
+ dataSource.filter = filter;
4434
+ }
4435
+ }
4436
+ break;
4437
+ case "state":
4438
+ if (t17.isStringLiteral(val)) {
4439
+ if (!dataSource.filter) dataSource.filter = {};
4440
+ dataSource.filter.current_state = val.value;
4441
+ }
4442
+ break;
4443
+ case "groupBy":
4444
+ if (t17.isStringLiteral(val)) {
4445
+ dataSource.groupBy = val.value;
4446
+ }
4447
+ break;
4448
+ case "facets":
4449
+ if (t17.isArrayExpression(val)) {
4450
+ dataSource.facets = val.elements.filter((el) => t17.isStringLiteral(el)).map((el) => el.value);
4451
+ }
4452
+ break;
4453
+ }
4454
+ }
4455
+ }
4456
+ if (!compilerState.metadata) compilerState.metadata = {};
4457
+ const meta = compilerState.metadata;
4458
+ if (!meta.dataSources) meta.dataSources = [];
4459
+ meta.dataSources.push(dataSource);
4460
+ }
4461
+ function extractMutationDataSource(path, state) {
4462
+ const args = path.node.arguments;
4463
+ if (args.length < 1) return;
4464
+ const slug = resolveSlugArg(args, state);
4465
+ if (!slug) return;
4466
+ const compilerState = state;
4467
+ if (!compilerState.metadata) compilerState.metadata = {};
4468
+ const meta = compilerState.metadata;
4469
+ if (!meta.mutationTargets) meta.mutationTargets = [];
4470
+ meta.mutationTargets.push(slug);
4471
+ }
4472
+ function extractDuringAction(path, state) {
4473
+ const args = path.node.arguments;
4474
+ if (args.length < 1 || !t17.isObjectExpression(args[0])) return;
4475
+ const compilerState = state;
4476
+ const config = args[0];
4477
+ let stateName;
4478
+ let intervalMs = 1e3;
4479
+ for (const prop of config.properties) {
4480
+ if (!t17.isObjectProperty(prop) || !t17.isIdentifier(prop.key)) continue;
4481
+ if (prop.key.name === "state" && t17.isStringLiteral(prop.value)) {
4482
+ stateName = prop.value.value;
4483
+ }
4484
+ if (prop.key.name === "intervalMs" && t17.isNumericLiteral(prop.value)) {
4485
+ intervalMs = prop.value.value;
4486
+ }
4487
+ }
4488
+ if (!stateName) return;
4489
+ if (!compilerState.states.has(stateName)) {
4490
+ compilerState.states.set(stateName, {
4491
+ name: stateName,
4492
+ type: "REGULAR",
4493
+ on_enter: [],
4494
+ during: [],
4495
+ on_exit: []
4496
+ });
4497
+ }
4498
+ const stateEntry = compilerState.states.get(stateName);
4499
+ if (!stateEntry.during) stateEntry.during = [];
4500
+ compilerState.actionCounter++;
4501
+ stateEntry.during.push({
4502
+ id: `during_${compilerState.actionCounter}`,
4503
+ type: "interval",
4504
+ interval_ms: intervalMs,
4505
+ actions: []
4506
+ });
4507
+ }
4508
+ function extractRoleDependency(path, state) {
4509
+ const args = path.node.arguments;
4510
+ if (args.length < 1 || !t17.isStringLiteral(args[0])) return;
4511
+ const compilerState = state;
4512
+ if (!compilerState.metadata) compilerState.metadata = {};
4513
+ const meta = compilerState.metadata;
4514
+ if (!meta.roleDependencies) meta.roleDependencies = [];
4515
+ const roles = meta.roleDependencies;
4516
+ const role = args[0].value;
4517
+ if (!roles.includes(role)) roles.push(role);
4518
+ }
4519
+ function extractViewDependency(path, state) {
4520
+ const args = path.node.arguments;
4521
+ if (args.length < 1 || !t17.isStringLiteral(args[0])) return;
4522
+ const compilerState = state;
4523
+ if (!compilerState.metadata) compilerState.metadata = {};
4524
+ const meta = compilerState.metadata;
4525
+ if (!meta.viewDependencies) meta.viewDependencies = [];
4526
+ const views = meta.viewDependencies;
4527
+ const slug = args[0].value;
4528
+ if (!views.includes(slug)) views.push(slug);
4529
+ }
4530
+ function extractParamsUsage(_path, state) {
4531
+ const compilerState = state;
4532
+ if (!compilerState.metadata) compilerState.metadata = {};
4533
+ const meta = compilerState.metadata;
4534
+ meta.acceptsParams = true;
4535
+ }
4536
+ function extractLibraryDependency(path, state) {
4537
+ const args = path.node.arguments;
4538
+ if (args.length < 1 || !t17.isStringLiteral(args[0])) return;
4539
+ const compilerState = state;
4540
+ if (!compilerState.metadata) compilerState.metadata = {};
4541
+ const meta = compilerState.metadata;
4542
+ if (!meta.libraryDependencies) meta.libraryDependencies = [];
4543
+ const libs = meta.libraryDependencies;
4544
+ const slug = args[0].value;
4545
+ if (!libs.includes(slug)) libs.push(slug);
4546
+ }
4547
+ var STRICT_BANNED_HOOKS = {
4548
+ useEffect: "STRICT_USE_EFFECT",
4549
+ useLayoutEffect: "STRICT_USE_LAYOUT_EFFECT",
4550
+ useRef: "STRICT_USE_REF",
4551
+ useMemo: "STRICT_USE_MEMO",
4552
+ useCallback: "STRICT_USE_CALLBACK"
4553
+ };
4554
+ function extractMetadataFromComments(comments, metadata) {
4555
+ for (const comment of comments) {
4556
+ if (comment.type !== "CommentBlock") continue;
4557
+ const lines = comment.value.split("\n");
4558
+ for (const line of lines) {
4559
+ const workflowMatch = line.match(/@workflow\s+(.+)/);
4560
+ if (workflowMatch) {
4561
+ const rest = workflowMatch[1].trim();
4562
+ const kvRegex = /(\w+)="([^"]+)"/g;
4563
+ let kvMatch;
4564
+ let hasKV = false;
4565
+ while ((kvMatch = kvRegex.exec(rest)) !== null) {
4566
+ hasKV = true;
4567
+ const [, key, val] = kvMatch;
4568
+ if (key === "slug") metadata.slug = val;
4569
+ else if (key === "version") metadata.version = val;
4570
+ else if (key === "category") metadata.category = val;
4571
+ else if (key === "description") metadata.description = val;
4572
+ }
4573
+ if (!hasKV) {
4574
+ metadata.slug = rest;
4575
+ }
4576
+ continue;
4577
+ }
4578
+ const match = line.match(/@(\w+)\s+(.+)/);
4579
+ if (match) {
4580
+ const [, tag, value] = match;
4581
+ if (tag === "version") metadata.version = value.trim();
4582
+ if (tag === "category") metadata.category = value.trim();
4583
+ if (tag === "description") metadata.description = value.trim();
4584
+ }
4585
+ }
4586
+ }
4587
+ }
4588
+ function createVisitor(options = {}) {
4589
+ const mode = options.mode;
4590
+ return {
4591
+ Program: {
4592
+ // Initialize compiler state at program entry
4593
+ enter(_path, state) {
4594
+ resetNodeIdCounter();
4595
+ resetDuringIdCounter();
4596
+ resetWatcherIdCounter();
4597
+ resetTransitionEffectIdCounter();
4598
+ const compilerState = {
4599
+ fields: [],
4600
+ states: /* @__PURE__ */ new Map(),
4601
+ transitions: [],
4602
+ events: [],
4603
+ actionCounter: 0,
4604
+ metadata: {},
4605
+ errors: [],
4606
+ warnings: []
4607
+ };
4608
+ const program = _path.node;
4609
+ const leadingComments = program.leadingComments || [];
4610
+ extractMetadataFromComments(leadingComments, compilerState.metadata);
4611
+ if (program.body.length > 0) {
4612
+ const firstStmt = program.body[0];
4613
+ if (firstStmt.leadingComments) {
4614
+ extractMetadataFromComments(firstStmt.leadingComments, compilerState.metadata);
4615
+ }
4616
+ }
4617
+ const exportDeclaration = program.body.find(
4618
+ (node) => t17.isExportNamedDeclaration(node) || t17.isExportDefaultDeclaration(node)
4619
+ );
4620
+ if (exportDeclaration) {
4621
+ if (exportDeclaration.leadingComments) {
4622
+ extractMetadataFromComments(exportDeclaration.leadingComments, compilerState.metadata);
4623
+ }
4624
+ if (t17.isExportNamedDeclaration(exportDeclaration)) {
4625
+ const declaration = exportDeclaration.declaration;
4626
+ if (t17.isFunctionDeclaration(declaration)) {
4627
+ if (declaration.id) {
4628
+ compilerState.metadata.name = declaration.id.name;
4629
+ }
4630
+ if (declaration.leadingComments) {
4631
+ extractMetadataFromComments(declaration.leadingComments, compilerState.metadata);
4632
+ }
4633
+ }
4634
+ } else if (t17.isExportDefaultDeclaration(exportDeclaration)) {
4635
+ const declaration = exportDeclaration.declaration;
4636
+ if (t17.isFunctionDeclaration(declaration)) {
4637
+ if (declaration.id) {
4638
+ compilerState.metadata.name = declaration.id.name;
4639
+ }
4640
+ if (declaration.leadingComments) {
4641
+ extractMetadataFromComments(declaration.leadingComments, compilerState.metadata);
4642
+ }
4643
+ } else if (t17.isIdentifier(declaration)) {
4644
+ compilerState.metadata.name = declaration.name;
4645
+ }
4646
+ }
4647
+ }
4648
+ if (!compilerState.metadata.slug && state.filename) {
4649
+ const fileName = state.filename.split("/").pop() || "";
4650
+ compilerState.metadata.slug = fileName.replace(/\.workflow\.(tsx?|jsx?)$/, "").replace(/\.(tsx?|jsx?)$/, "").replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
4651
+ compilerState.metadata.__slugAutoFallback = true;
4652
+ }
4653
+ Object.assign(state, compilerState);
4654
+ if (isModelFile(_path, state.filename)) {
4655
+ extractModelFile(_path, state);
4656
+ state.__isModelFile = true;
4657
+ if (!compilerState.metadata.category) {
4658
+ compilerState.metadata.category = "data";
4659
+ }
4660
+ }
4661
+ extractGrammarIslands(_path, state);
4662
+ if (hasContextCreation(_path)) {
4663
+ extractContextWorkflows(_path, state);
4664
+ }
4665
+ if (isServerActionFile(state.filename)) {
4666
+ extractServerActions(_path, state);
4667
+ state.__isServerActionFile = true;
4668
+ }
4669
+ },
4670
+ // After all extraction, emit Pure Form JSON
4671
+ exit(_path, state) {
4672
+ const compilerState = state;
4673
+ const extracted = compilerStateToWorkflow(compilerState, compilerState.metadata);
4674
+ const ir = emitIR(extracted);
4675
+ const definition = emitWorkflowDefinition(extracted);
4676
+ const canonical = emitCanonical(extracted, state.filename);
4677
+ if (compilerState.errors && compilerState.errors.length > 0) {
4678
+ if (!ir.metadata) ir.metadata = {};
4679
+ ir.metadata.errors = compilerState.errors;
4680
+ }
4681
+ if (compilerState.warnings && compilerState.warnings.length > 0) {
4682
+ if (!ir.metadata) ir.metadata = {};
4683
+ ir.metadata.warnings = compilerState.warnings;
4684
+ }
4685
+ if (!state.file.metadata) state.file.metadata = {};
4686
+ state.file.metadata.mindmatrixIR = ir;
4687
+ state.file.metadata.mindmatrixDefinition = definition;
4688
+ state.file.metadata.mindmatrixCanonical = canonical;
4689
+ }
4690
+ },
4691
+ // Extract metadata from function declarations with JSDoc
4692
+ FunctionDeclaration(path, state) {
4693
+ const comments = path.node.leadingComments || [];
4694
+ const compilerState = state;
4695
+ extractMetadataFromComments(comments, compilerState.metadata);
4696
+ },
4697
+ // Main hook extraction dispatcher
4698
+ CallExpression(path, state) {
4699
+ const callee = path.node.callee;
4700
+ if (!t17.isIdentifier(callee)) return;
4701
+ const compilerState = state;
4702
+ const hookName = callee.name;
4703
+ if (mode === "strict" && STRICT_BANNED_HOOKS[hookName]) {
4704
+ const error = {
4705
+ code: STRICT_BANNED_HOOKS[hookName],
4706
+ message: `${hookName}() is not allowed in strict mode. Use @mindmatrix/react effect hooks instead.`,
4707
+ line: path.node.loc?.start.line,
4708
+ column: path.node.loc?.start.column,
4709
+ severity: "error"
4710
+ };
4711
+ if (!compilerState.errors) compilerState.errors = [];
4712
+ compilerState.errors.push(error);
4713
+ return;
4714
+ }
4715
+ if (mode === "infer" && hookName === "useEffect") {
4716
+ const warning = {
4717
+ code: "INFER_RAW_JSX",
4718
+ message: `useEffect() found in infer mode \u2014 component will be treated as an opaque [raw jsx] grammar island.`,
4719
+ line: path.node.loc?.start.line,
4720
+ column: path.node.loc?.start.column,
4721
+ severity: "warning"
4722
+ };
4723
+ if (!compilerState.warnings) compilerState.warnings = [];
4724
+ compilerState.warnings.push(warning);
4725
+ return;
4726
+ }
4727
+ switch (hookName) {
4728
+ case "useState":
4729
+ extractStates(path, state);
4730
+ break;
4731
+ case "useOnEnter":
4732
+ case "useOnExit":
4733
+ extractEffects(path, state);
4734
+ break;
4735
+ case "useTransition":
4736
+ extractTransitions(path, state);
4737
+ break;
4738
+ case "useOnEvent":
4739
+ extractEvents(path, state);
4740
+ break;
4741
+ case "useWhileIn":
4742
+ extractDuring(path, state);
4743
+ break;
4744
+ case "useOnChange":
4745
+ extractChangeWatcher(path, state);
4746
+ break;
4747
+ case "useComputed":
4748
+ extractComputed(path, state);
4749
+ break;
4750
+ case "useOnTransition":
4751
+ extractTransitionEffect(path, state);
4752
+ break;
4753
+ case "useQuery":
4754
+ extractQueryDataSource(path, state);
4755
+ break;
4756
+ case "useMutation":
4757
+ extractMutationDataSource(path, state);
4758
+ break;
4759
+ case "useDuringAction":
4760
+ extractDuringAction(path, state);
4761
+ break;
4762
+ case "useRole":
4763
+ extractRoleDependency(path, state);
4764
+ break;
4765
+ case "useView":
4766
+ extractViewDependency(path, state);
4767
+ break;
4768
+ case "useParams":
4769
+ extractParamsUsage(path, state);
4770
+ break;
4771
+ case "useExpressionLibrary":
4772
+ extractLibraryDependency(path, state);
4773
+ break;
4774
+ case "useServerAction":
4775
+ extractServerActionHook(path, state);
4776
+ break;
4777
+ case "useServerState":
4778
+ extractServerState(path, state);
4779
+ break;
4780
+ }
4781
+ },
4782
+ // Track imports: model imports for useQuery(modelRef) resolution + strict mode validation
4783
+ ImportDeclaration(path, state) {
4784
+ const compilerState = state;
4785
+ const source = path.node.source.value;
4786
+ if (source.match(/\/models\/|\.model(?:\.[tj]sx?)?$/)) {
4787
+ if (!compilerState.metadata) compilerState.metadata = {};
4788
+ const meta = compilerState.metadata;
4789
+ if (!meta.__modelImports) meta.__modelImports = {};
4790
+ const imports = meta.__modelImports;
4791
+ for (const specifier of path.node.specifiers) {
4792
+ if (t17.isImportDefaultSpecifier(specifier) || t17.isImportSpecifier(specifier)) {
4793
+ imports[specifier.local.name] = source;
4794
+ }
4795
+ }
4796
+ }
4797
+ if (mode !== "strict") return;
4798
+ if (source.startsWith("@mindmatrix/") || source.startsWith("@mmapp/") || source === "react" || source.startsWith("react/") || source.startsWith(".") || source.startsWith("/")) {
4799
+ return;
4800
+ }
4801
+ const error = {
4802
+ code: "STRICT_FORBIDDEN_IMPORT",
4803
+ message: `Import from '${source}' is not allowed in strict mode. Only @mindmatrix/* and relative imports are permitted.`,
4804
+ line: path.node.loc?.start.line,
4805
+ column: path.node.loc?.start.column,
4806
+ severity: "error"
4807
+ };
4808
+ if (!compilerState.errors) compilerState.errors = [];
4809
+ compilerState.errors.push(error);
4810
+ },
4811
+ // Track derived/computed variable declarations for expression inlining.
4812
+ //
4813
+ // Three cases:
4814
+ // 1. `const hasContent = expr` → registerDerivedVar (inline the expression)
4815
+ // 2. `const users = useQuery(...)` → registerDerivedVar as $instance.{slug}
4816
+ // (data source results are available on $instance by their slug)
4817
+ // 3. `const sendMsg = useMutation(...)` → registerDerivedVar as $action.transition
4818
+ // (mutation results are action callbacks)
4819
+ // 4. `const memo = useMemo(() => expr, [])` → registerDerivedVar with body expr
4820
+ VariableDeclarator(path, state) {
4821
+ const compilerState = state;
4822
+ if (!compilerState.metadata) return;
4823
+ const id = path.node.id;
4824
+ const init = path.node.init;
4825
+ if (t17.isArrayPattern(id)) return;
4826
+ if (!t17.isIdentifier(id) || !init || !t17.isExpression(init)) return;
4827
+ const parentFn = path.getFunctionParent();
4828
+ if (!parentFn) return;
4829
+ const parentNode = parentFn.parentPath;
4830
+ const isExportedDirectly = parentNode?.isExportDefaultDeclaration() || parentNode?.isExportNamedDeclaration();
4831
+ let isExportedByName = false;
4832
+ if (!isExportedDirectly && parentNode?.isVariableDeclarator()) {
4833
+ const varId = parentNode.node.id;
4834
+ if (t17.isIdentifier(varId)) {
4835
+ isExportedByName = varId.name === compilerState.metadata?.name;
4836
+ }
4837
+ }
4838
+ if (!isExportedDirectly && !isExportedByName) return;
4839
+ if (t17.isCallExpression(init) && t17.isIdentifier(init.callee)) {
4840
+ const callee = init.callee.name;
4841
+ if (callee === "useQuery") {
4842
+ registerDerivedVar(id.name, t17.optionalMemberExpression(
4843
+ t17.identifier("$instance"),
4844
+ t17.identifier(id.name),
4845
+ false,
4846
+ // computed
4847
+ true
4848
+ // optional
4849
+ ));
4850
+ return;
4851
+ }
4852
+ if (callee === "useMutation") {
4853
+ registerDerivedVar(id.name, t17.memberExpression(
4854
+ t17.identifier("$action"),
4855
+ t17.identifier("transition")
4856
+ ));
4857
+ return;
4858
+ }
4859
+ if (callee === "useServerAction") {
4860
+ registerDerivedVar(id.name, t17.memberExpression(
4861
+ t17.identifier("$action"),
4862
+ t17.identifier("serverAction")
4863
+ ));
4864
+ return;
4865
+ }
4866
+ if (callee === "useServerState") {
4867
+ registerDerivedVar(id.name, t17.optionalMemberExpression(
4868
+ t17.identifier("$instance"),
4869
+ t17.identifier("serverState"),
4870
+ false,
4871
+ true
4872
+ ));
4873
+ return;
4874
+ }
4875
+ if (callee === "useMemo" && init.arguments.length >= 1) {
4876
+ const callback = init.arguments[0];
4877
+ if (t17.isArrowFunctionExpression(callback)) {
4878
+ if (t17.isExpression(callback.body)) {
4879
+ registerDerivedVar(id.name, callback.body);
4880
+ } else if (t17.isBlockStatement(callback.body)) {
4881
+ const retStmt = callback.body.body.find((s) => t17.isReturnStatement(s));
4882
+ if (retStmt?.argument && t17.isExpression(retStmt.argument)) {
4883
+ registerDerivedVar(id.name, retStmt.argument);
4884
+ }
4885
+ }
4886
+ }
4887
+ return;
4888
+ }
4889
+ if (callee.startsWith("use")) return;
4890
+ }
4891
+ registerDerivedVar(id.name, init);
4892
+ },
4893
+ // Extract JSX from function component return.
4894
+ // Only extract from the exported component function's direct return,
4895
+ // not from nested callbacks (Each render functions, helper components, etc.).
4896
+ ReturnStatement(path, state) {
4897
+ if (!t17.isJSXElement(path.node.argument) && !t17.isJSXFragment(path.node.argument)) return;
4898
+ const parentFn = path.getFunctionParent();
4899
+ if (!parentFn) return;
4900
+ const parentNode = parentFn.parentPath;
4901
+ const isExportedDirectly = parentNode?.isExportDefaultDeclaration() || parentNode?.isExportNamedDeclaration();
4902
+ let isExportedByName = false;
4903
+ if (!isExportedDirectly && parentNode?.isVariableDeclarator()) {
4904
+ const varId = parentNode.node.id;
4905
+ if (t17.isIdentifier(varId)) {
4906
+ isExportedByName = varId.name === state.metadata?.name;
4907
+ }
4908
+ }
4909
+ if (isExportedDirectly || isExportedByName) {
4910
+ extractComponents(path, state);
4911
+ }
4912
+ }
4913
+ };
4914
+ }
4915
+
4916
+ // src/babel/index.ts
4917
+ function babelPlugin(api, options = {}) {
4918
+ api.assertVersion(7);
4919
+ return {
4920
+ name: "mindmatrix-react",
4921
+ visitor: createVisitor(options)
4922
+ };
4923
+ }
4924
+
4925
+ export {
4926
+ extractStates,
4927
+ extractEffects,
4928
+ extractTransitions,
4929
+ extractEvents,
4930
+ transpileBlock,
4931
+ extractComponents,
4932
+ extractComputed,
4933
+ transformToFrontend,
4934
+ emitIR,
4935
+ emitPureForm,
4936
+ emitCanonical,
4937
+ emitCompiledOutput,
4938
+ compilerStateToWorkflow,
4939
+ createVisitor,
4940
+ babelPlugin
4941
+ };