@mmapp/react-compiler 0.1.0-alpha.15 → 0.1.0-alpha.17

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 (185) hide show
  1. package/dist/{chunk-NUPJYPFU.mjs → chunk-6LAII7OP.mjs} +79 -1
  2. package/dist/{chunk-UBDNXVL2.mjs → chunk-FPAMQXKB.mjs} +89 -3
  3. package/dist/cli/index.js +240 -9
  4. package/dist/cli/index.mjs +57 -9
  5. package/dist/dev-server.js +79 -1
  6. package/dist/dev-server.mjs +1 -1
  7. package/dist/index.js +79 -1
  8. package/dist/index.mjs +1 -1
  9. package/dist/{init-2CRSUGV5.mjs → init-CJCDWI33.mjs} +119 -0
  10. package/package.json +2 -2
  11. package/dist/chunk-26U577GB.mjs +0 -3465
  12. package/dist/chunk-2FBDFAX6.mjs +0 -2362
  13. package/dist/chunk-2L4QSMXG.mjs +0 -175
  14. package/dist/chunk-2REDFOER.mjs +0 -931
  15. package/dist/chunk-3USIFFE4.mjs +0 -2190
  16. package/dist/chunk-45YMGEVT.mjs +0 -186
  17. package/dist/chunk-46YKSHQR.mjs +0 -175
  18. package/dist/chunk-4FN2AISW.mjs +0 -148
  19. package/dist/chunk-4OPI5L7G.mjs +0 -2593
  20. package/dist/chunk-4RYTKOOJ.mjs +0 -186
  21. package/dist/chunk-4XHK6FWL.mjs +0 -2058
  22. package/dist/chunk-52XHYD2V.mjs +0 -214
  23. package/dist/chunk-5FTDWKHH.mjs +0 -244
  24. package/dist/chunk-5GUFFFGL.mjs +0 -148
  25. package/dist/chunk-5RKTOVR5.mjs +0 -244
  26. package/dist/chunk-5VNJ7C6N.mjs +0 -154
  27. package/dist/chunk-5YDMOO4X.mjs +0 -214
  28. package/dist/chunk-64ZWEMLJ.mjs +0 -148
  29. package/dist/chunk-6CQOAAMV.mjs +0 -1803
  30. package/dist/chunk-6SEVAAVT.mjs +0 -3516
  31. package/dist/chunk-6XP4KSWQ.mjs +0 -2190
  32. package/dist/chunk-6YLR5ZDA.mjs +0 -2829
  33. package/dist/chunk-72QWL54I.mjs +0 -175
  34. package/dist/chunk-7B4TRI7C.mjs +0 -4835
  35. package/dist/chunk-7JRAEFRB.mjs +0 -7510
  36. package/dist/chunk-7ZKGHTNB.mjs +0 -4952
  37. package/dist/chunk-AOGY2GK6.mjs +0 -3292
  38. package/dist/chunk-AXXUXRNA.mjs +0 -1434
  39. package/dist/chunk-CHLVKMQW.mjs +0 -175
  40. package/dist/chunk-CKGOZAB7.mjs +0 -939
  41. package/dist/chunk-D34RAZUX.mjs +0 -2223
  42. package/dist/chunk-DE3ZGQAC.mjs +0 -148
  43. package/dist/chunk-DMCY3BBG.mjs +0 -1933
  44. package/dist/chunk-DPIK3PJS.mjs +0 -244
  45. package/dist/chunk-E5IVH4RE.mjs +0 -186
  46. package/dist/chunk-E6FZNUR5.mjs +0 -4953
  47. package/dist/chunk-EJRBDQDP.mjs +0 -2607
  48. package/dist/chunk-ELO4TXJL.mjs +0 -186
  49. package/dist/chunk-EO6SYNCG.mjs +0 -175
  50. package/dist/chunk-EQGA6A6D.mjs +0 -121
  51. package/dist/chunk-EY2CSXYA.mjs +0 -822
  52. package/dist/chunk-FIQ65CDR.mjs +0 -925
  53. package/dist/chunk-FKRO52XH.mjs +0 -3446
  54. package/dist/chunk-FL4YAKU6.mjs +0 -4941
  55. package/dist/chunk-FOZXJFAR.mjs +0 -186
  56. package/dist/chunk-FX6URXWN.mjs +0 -186
  57. package/dist/chunk-FYT47UBU.mjs +0 -5076
  58. package/dist/chunk-G7SMOWOL.mjs +0 -828
  59. package/dist/chunk-GCLGPOJZ.mjs +0 -148
  60. package/dist/chunk-GGB4G5YY.mjs +0 -175
  61. package/dist/chunk-GXB4JOP7.mjs +0 -5072
  62. package/dist/chunk-HFXOUMTD.mjs +0 -175
  63. package/dist/chunk-HLRGCCIL.mjs +0 -4839
  64. package/dist/chunk-HOIUP6IF.mjs +0 -690
  65. package/dist/chunk-HRYR54PT.mjs +0 -175
  66. package/dist/chunk-HWIZ47US.mjs +0 -214
  67. package/dist/chunk-I3AU7GRD.mjs +0 -120
  68. package/dist/chunk-IB7MNPQL.mjs +0 -4953
  69. package/dist/chunk-ICSIHQCG.mjs +0 -148
  70. package/dist/chunk-ILFGMUVD.mjs +0 -1933
  71. package/dist/chunk-IPTX5MJU.mjs +0 -3223
  72. package/dist/chunk-ITGUSH2Z.mjs +0 -2783
  73. package/dist/chunk-IXHBCAMF.mjs +0 -3306
  74. package/dist/chunk-J7JUAHS4.mjs +0 -186
  75. package/dist/chunk-J7TWJ3TM.mjs +0 -2784
  76. package/dist/chunk-JDPLDGVF.mjs +0 -4810
  77. package/dist/chunk-JLA5VNQ3.mjs +0 -186
  78. package/dist/chunk-JQLWFCTM.mjs +0 -214
  79. package/dist/chunk-K53XP2DL.mjs +0 -148
  80. package/dist/chunk-K5HX2SVL.mjs +0 -1902
  81. package/dist/chunk-KFGYOOVS.mjs +0 -214
  82. package/dist/chunk-KFJJCQAL.mjs +0 -148
  83. package/dist/chunk-KFVVOS5N.mjs +0 -925
  84. package/dist/chunk-KJUIIEQE.mjs +0 -186
  85. package/dist/chunk-KNWTHRVQ.mjs +0 -175
  86. package/dist/chunk-KSG4XSZF.mjs +0 -175
  87. package/dist/chunk-L2OZ4CDV.mjs +0 -113
  88. package/dist/chunk-LF5N6DOU.mjs +0 -175
  89. package/dist/chunk-LJQCM2IM.mjs +0 -214
  90. package/dist/chunk-MIZV3TAN.mjs +0 -3293
  91. package/dist/chunk-NKKLQE5V.mjs +0 -148
  92. package/dist/chunk-NOW23XFZ.mjs +0 -186
  93. package/dist/chunk-NRXQKQ74.mjs +0 -148
  94. package/dist/chunk-NTB7OEX2.mjs +0 -2918
  95. package/dist/chunk-NW6555WJ.mjs +0 -186
  96. package/dist/chunk-O4AUS7EU.mjs +0 -148
  97. package/dist/chunk-OMZE6VLQ.mjs +0 -214
  98. package/dist/chunk-OPJKP747.mjs +0 -7506
  99. package/dist/chunk-OWI6XWCD.mjs +0 -3375
  100. package/dist/chunk-P4BR7WVO.mjs +0 -2190
  101. package/dist/chunk-PRUMNNDI.mjs +0 -3192
  102. package/dist/chunk-QQHVYH2X.mjs +0 -244
  103. package/dist/chunk-QTBD5B3F.mjs +0 -148
  104. package/dist/chunk-R57T26RR.mjs +0 -734
  105. package/dist/chunk-S5QLWLLT.mjs +0 -186
  106. package/dist/chunk-SCWGT2FY.mjs +0 -2190
  107. package/dist/chunk-SKSDPPNT.mjs +0 -3788
  108. package/dist/chunk-SMKJUSB3.mjs +0 -2190
  109. package/dist/chunk-SP2YUS33.mjs +0 -186
  110. package/dist/chunk-SU4E6E7B.mjs +0 -3153
  111. package/dist/chunk-SYUUKW5A.mjs +0 -3379
  112. package/dist/chunk-THFYE5ZX.mjs +0 -244
  113. package/dist/chunk-UDDTWG5J.mjs +0 -734
  114. package/dist/chunk-UL2XZEMA.mjs +0 -3128
  115. package/dist/chunk-VCAY2KGM.mjs +0 -175
  116. package/dist/chunk-VLTKQDJ3.mjs +0 -244
  117. package/dist/chunk-WBYMW4NQ.mjs +0 -3450
  118. package/dist/chunk-WECAV6QB.mjs +0 -148
  119. package/dist/chunk-WMKBXUCE.mjs +0 -3228
  120. package/dist/chunk-XAJ5BKKL.mjs +0 -4947
  121. package/dist/chunk-XG2X7AEA.mjs +0 -175
  122. package/dist/chunk-XG7Z23NQ.mjs +0 -148
  123. package/dist/chunk-XMWUHQVV.mjs +0 -939
  124. package/dist/chunk-XWZAOCQ7.mjs +0 -2607
  125. package/dist/chunk-XZNEDRGN.mjs +0 -3876
  126. package/dist/chunk-Y6FXYEAI.mjs +0 -10
  127. package/dist/chunk-Y6MA7ULW.mjs +0 -148
  128. package/dist/chunk-YFS6JMYO.mjs +0 -3342
  129. package/dist/chunk-YMS7Q7LG.mjs +0 -214
  130. package/dist/chunk-Z2G5RZ4H.mjs +0 -186
  131. package/dist/chunk-Z6AIQ4KL.mjs +0 -113
  132. package/dist/chunk-ZA37XTGA.mjs +0 -175
  133. package/dist/chunk-ZE3KCHBM.mjs +0 -2918
  134. package/dist/config-PL24KEWL.mjs +0 -219
  135. package/dist/dev-server-Bs_sz2DG.d.mts +0 -111
  136. package/dist/dev-server-Bs_sz2DG.d.ts +0 -111
  137. package/dist/dev-server-CjoufJ-u.d.mts +0 -109
  138. package/dist/dev-server-CjoufJ-u.d.ts +0 -109
  139. package/dist/dev-server-RmGHIntF.d.mts +0 -113
  140. package/dist/dev-server-RmGHIntF.d.ts +0 -113
  141. package/dist/engine-binary-QQUDACBJ.mjs +0 -455
  142. package/dist/envelope-DD7v0v6E.d.mts +0 -265
  143. package/dist/envelope-DD7v0v6E.d.ts +0 -265
  144. package/dist/envelope-vCVjrHlo.d.mts +0 -265
  145. package/dist/envelope-vCVjrHlo.d.ts +0 -265
  146. package/dist/index-B5gSgvnd.d.mts +0 -44
  147. package/dist/index-B5gSgvnd.d.ts +0 -44
  148. package/dist/index-Bs0MnR54.d.mts +0 -103
  149. package/dist/index-Bs0MnR54.d.ts +0 -103
  150. package/dist/index-DR0nNc_f.d.mts +0 -101
  151. package/dist/index-DR0nNc_f.d.ts +0 -101
  152. package/dist/index-revho_gS.d.mts +0 -104
  153. package/dist/index-revho_gS.d.ts +0 -104
  154. package/dist/init-7FJENUDK.mjs +0 -407
  155. package/dist/init-7JQMAAXS.mjs +0 -363
  156. package/dist/init-DQDX3QK6.mjs +0 -369
  157. package/dist/init-EHO4VQ22.mjs +0 -369
  158. package/dist/init-IXEE2RCF.mjs +0 -340
  159. package/dist/init-UC3FWPIW.mjs +0 -367
  160. package/dist/init-UNSMVKIK.mjs +0 -366
  161. package/dist/init-UNV5XIDE.mjs +0 -367
  162. package/dist/project-compiler-2P4N4DR7.mjs +0 -10
  163. package/dist/project-compiler-D2LCC27O.mjs +0 -10
  164. package/dist/project-compiler-EGJUTAJU.mjs +0 -10
  165. package/dist/project-compiler-EJ3GANJE.mjs +0 -10
  166. package/dist/project-compiler-LOQKVRZJ.mjs +0 -10
  167. package/dist/project-compiler-NNK32MPG.mjs +0 -10
  168. package/dist/project-compiler-OP2VVGJQ.mjs +0 -10
  169. package/dist/project-compiler-RQ6OQKRM.mjs +0 -10
  170. package/dist/project-compiler-VFR6CSDX.mjs +0 -10
  171. package/dist/project-compiler-VWNNCHGO.mjs +0 -10
  172. package/dist/project-compiler-XVAAU4C5.mjs +0 -10
  173. package/dist/project-compiler-YES5FGMD.mjs +0 -10
  174. package/dist/project-compiler-ZKMQDLGU.mjs +0 -10
  175. package/dist/project-decompiler-5GY2KSG4.mjs +0 -7
  176. package/dist/project-decompiler-FLXCEJHS.mjs +0 -7
  177. package/dist/project-decompiler-US7GAVIC.mjs +0 -7
  178. package/dist/project-decompiler-VLPR22QF.mjs +0 -7
  179. package/dist/pull-A2QUHW4K.mjs +0 -109
  180. package/dist/pull-FUS5QYZS.mjs +0 -109
  181. package/dist/pull-JBEQWVPE.mjs +0 -109
  182. package/dist/pull-LD5ENLGY.mjs +0 -109
  183. package/dist/pull-P44LDRWB.mjs +0 -109
  184. package/dist/verify-BYHUKARQ.mjs +0 -1833
  185. package/dist/verify-SEIXUGN4.mjs +0 -1833
@@ -1,2829 +0,0 @@
1
- // src/babel/visitor.ts
2
- import * as t13 from "@babel/types";
3
-
4
- // src/babel/extractors/state-extractor.ts
5
- import * as t from "@babel/types";
6
- function inferFieldType(typeAnnotation, defaultValue) {
7
- if (typeAnnotation) {
8
- if (t.isTSNumberKeyword(typeAnnotation)) return "number";
9
- if (t.isTSStringKeyword(typeAnnotation)) return "text";
10
- if (t.isTSBooleanKeyword(typeAnnotation)) return "boolean";
11
- if (t.isTSTypeReference(typeAnnotation)) {
12
- const typeName = t.isIdentifier(typeAnnotation.typeName) ? typeAnnotation.typeName.name : "";
13
- if (typeName === "Date") return "date";
14
- }
15
- if (t.isTSArrayType(typeAnnotation) || t.isTSTypeLiteral(typeAnnotation) || t.isTSTupleType(typeAnnotation)) {
16
- return "json";
17
- }
18
- }
19
- if (defaultValue) {
20
- if (t.isNumericLiteral(defaultValue)) return "number";
21
- if (t.isStringLiteral(defaultValue)) return "text";
22
- if (t.isBooleanLiteral(defaultValue)) return "boolean";
23
- if (t.isArrayExpression(defaultValue) || t.isObjectExpression(defaultValue)) {
24
- return "json";
25
- }
26
- if (t.isNewExpression(defaultValue) && t.isIdentifier(defaultValue.callee)) {
27
- if (defaultValue.callee.name === "Date") return "date";
28
- }
29
- }
30
- return "text";
31
- }
32
- function extractDefaultValue(arg) {
33
- if (t.isNumericLiteral(arg)) return arg.value;
34
- if (t.isStringLiteral(arg)) return arg.value;
35
- if (t.isBooleanLiteral(arg)) return arg.value;
36
- if (t.isNullLiteral(arg)) return null;
37
- if (t.isArrayExpression(arg)) {
38
- return arg.elements.map(
39
- (el) => el && t.isExpression(el) ? extractDefaultValue(el) : null
40
- );
41
- }
42
- if (t.isObjectExpression(arg)) {
43
- const obj = {};
44
- arg.properties.forEach((prop) => {
45
- if (t.isObjectProperty(prop) && t.isIdentifier(prop.key) && t.isExpression(prop.value)) {
46
- obj[prop.key.name] = extractDefaultValue(prop.value);
47
- }
48
- });
49
- return obj;
50
- }
51
- return void 0;
52
- }
53
- function extractStates(path, state) {
54
- const parent = path.parentPath;
55
- if (!parent.isVariableDeclarator()) return;
56
- const id = parent.node.id;
57
- if (!t.isArrayPattern(id) || id.elements.length < 1) return;
58
- const firstElement = id.elements[0];
59
- if (!t.isIdentifier(firstElement)) return;
60
- const fieldName = firstElement.name;
61
- const args = path.node.arguments;
62
- const defaultValue = args.length > 0 && t.isExpression(args[0]) ? args[0] : null;
63
- let typeAnnotation = null;
64
- if (path.node.typeParameters && t.isTSTypeParameterInstantiation(path.node.typeParameters)) {
65
- const params = path.node.typeParameters.params;
66
- if (params.length > 0) {
67
- typeAnnotation = params[0];
68
- }
69
- }
70
- const fieldType = inferFieldType(typeAnnotation, defaultValue);
71
- const fieldDefaultValue = defaultValue ? extractDefaultValue(defaultValue) : void 0;
72
- const field = {
73
- name: fieldName,
74
- type: fieldType,
75
- default_value: fieldDefaultValue
76
- };
77
- const compilerState = state;
78
- if (!compilerState.fields) compilerState.fields = [];
79
- if (!compilerState.fields.some((f) => f.name === fieldName)) {
80
- compilerState.fields.push(field);
81
- }
82
- }
83
-
84
- // src/babel/extractors/effect-extractor.ts
85
- import * as t2 from "@babel/types";
86
- function analyzeCallbackBody(body, actionCounter) {
87
- const actions = [];
88
- for (const statement of body) {
89
- if (t2.isExpressionStatement(statement)) {
90
- const expr = statement.expression;
91
- if (t2.isCallExpression(expr) && t2.isIdentifier(expr.callee)) {
92
- const calleeName = expr.callee.name;
93
- const args = expr.arguments;
94
- if (calleeName === "setField" && args.length >= 2) {
95
- const field = t2.isStringLiteral(args[0]) ? args[0].value : void 0;
96
- const arg1 = args[1];
97
- if (field !== void 0 && !t2.isArgumentPlaceholder(arg1)) {
98
- const value = extractValue(arg1);
99
- actions.push({
100
- id: `auto_${++actionCounter.value}`,
101
- type: "set_field",
102
- mode: "auto",
103
- config: { field, value }
104
- });
105
- }
106
- } else if (calleeName === "setMemory" && args.length >= 2) {
107
- const key = t2.isStringLiteral(args[0]) ? args[0].value : void 0;
108
- const arg1 = args[1];
109
- if (key !== void 0 && !t2.isArgumentPlaceholder(arg1)) {
110
- const value = extractValue(arg1);
111
- actions.push({
112
- id: `auto_${++actionCounter.value}`,
113
- type: "set_memory",
114
- mode: "auto",
115
- config: { key, value }
116
- });
117
- }
118
- } else if (calleeName.startsWith("set") && args.length >= 1) {
119
- const fieldName = calleeName.slice(3).charAt(0).toLowerCase() + calleeName.slice(4);
120
- const arg0 = args[0];
121
- if (!t2.isArgumentPlaceholder(arg0)) {
122
- const value = extractValue(arg0);
123
- actions.push({
124
- id: `auto_${++actionCounter.value}`,
125
- type: "set_field",
126
- mode: "auto",
127
- config: { field: fieldName, value }
128
- });
129
- }
130
- }
131
- } else if (t2.isCallExpression(expr)) {
132
- const callee = expr.callee;
133
- const args = expr.arguments;
134
- if (t2.isMemberExpression(callee) && t2.isIdentifier(callee.object) && callee.object.name === "console" && t2.isIdentifier(callee.property) && callee.property.name === "log" && args.length > 0) {
135
- const arg0 = args[0];
136
- if (!t2.isArgumentPlaceholder(arg0)) {
137
- const message = extractValue(arg0);
138
- actions.push({
139
- id: `auto_${++actionCounter.value}`,
140
- type: "log_event",
141
- mode: "auto",
142
- config: { message }
143
- });
144
- }
145
- } else {
146
- actions.push({
147
- id: `auto_${++actionCounter.value}`,
148
- type: "custom",
149
- mode: "auto",
150
- config: { expression: generateCode(expr) }
151
- });
152
- }
153
- }
154
- }
155
- }
156
- return actions;
157
- }
158
- function extractValue(node) {
159
- if (t2.isNumericLiteral(node)) return node.value;
160
- if (t2.isStringLiteral(node)) return node.value;
161
- if (t2.isBooleanLiteral(node)) return node.value;
162
- if (t2.isNullLiteral(node)) return null;
163
- if (t2.isArrayExpression(node)) {
164
- return node.elements.map((el) => el && t2.isExpression(el) ? extractValue(el) : null);
165
- }
166
- if (t2.isObjectExpression(node)) {
167
- const obj = {};
168
- node.properties.forEach((prop) => {
169
- if (t2.isObjectProperty(prop) && t2.isIdentifier(prop.key) && t2.isExpression(prop.value)) {
170
- obj[prop.key.name] = extractValue(prop.value);
171
- }
172
- });
173
- return obj;
174
- }
175
- return generateCode(node);
176
- }
177
- function generateCode(node) {
178
- if (t2.isIdentifier(node)) return node.name;
179
- if (t2.isNumericLiteral(node)) return String(node.value);
180
- if (t2.isStringLiteral(node)) return `"${node.value}"`;
181
- if (t2.isBooleanLiteral(node)) return String(node.value);
182
- if (t2.isNullLiteral(node)) return "null";
183
- 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") {
184
- return "now()";
185
- }
186
- 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") {
187
- return "now()";
188
- }
189
- if (t2.isTemplateLiteral(node)) {
190
- return node.quasis.map((q) => q.value.raw).join("${}");
191
- }
192
- if (t2.isMemberExpression(node) && t2.isIdentifier(node.object) && t2.isIdentifier(node.property)) {
193
- return `${node.object.name}.${node.property.name}`;
194
- }
195
- return "[Expression]";
196
- }
197
- function ensureState(states, stateName) {
198
- if (!states.has(stateName)) {
199
- states.set(stateName, {
200
- name: stateName,
201
- type: "REGULAR",
202
- on_enter: [],
203
- during: [],
204
- on_exit: []
205
- });
206
- }
207
- return states.get(stateName);
208
- }
209
- function extractEffects(path, state) {
210
- const callee = path.node.callee;
211
- if (!t2.isIdentifier(callee)) return;
212
- const hookName = callee.name;
213
- const args = path.node.arguments;
214
- if ((hookName === "useOnEnter" || hookName === "useOnExit") && args.length >= 2) {
215
- const stateArg = args[0];
216
- const callbackArg = args[1];
217
- if (!t2.isStringLiteral(stateArg)) return;
218
- const stateName = stateArg.value;
219
- let callbackBody = [];
220
- if (t2.isArrowFunctionExpression(callbackArg) || t2.isFunctionExpression(callbackArg)) {
221
- const body = callbackArg.body;
222
- if (t2.isBlockStatement(body)) {
223
- callbackBody = body.body;
224
- } else if (t2.isExpression(body)) {
225
- callbackBody = [t2.expressionStatement(body)];
226
- }
227
- }
228
- const compilerState = state;
229
- if (!compilerState.states) compilerState.states = /* @__PURE__ */ new Map();
230
- if (!compilerState.actionCounter) compilerState.actionCounter = 0;
231
- const stateDefinition = ensureState(compilerState.states, stateName);
232
- const actions = analyzeCallbackBody(callbackBody, { value: compilerState.actionCounter });
233
- compilerState.actionCounter += actions.length;
234
- if (hookName === "useOnEnter") {
235
- stateDefinition.on_enter.push(...actions);
236
- } else if (hookName === "useOnExit") {
237
- stateDefinition.on_exit.push(...actions);
238
- }
239
- }
240
- }
241
-
242
- // src/babel/extractors/transition-extractor.ts
243
- import * as t3 from "@babel/types";
244
- function extractTransitions(path, state) {
245
- const callee = path.node.callee;
246
- if (!t3.isIdentifier(callee) || callee.name !== "useTransition") return;
247
- const args = path.node.arguments;
248
- if (args.length < 1) return;
249
- const nameArg = args[0];
250
- if (!t3.isStringLiteral(nameArg)) return;
251
- const transitionName = nameArg.value;
252
- const compilerState = state;
253
- if (!compilerState.transitions) compilerState.transitions = [];
254
- const existing = compilerState.transitions.find((tr) => tr.name === transitionName);
255
- if (existing) return;
256
- let from = [];
257
- let to = "";
258
- if (args.length >= 2 && t3.isObjectExpression(args[1])) {
259
- const opts = args[1];
260
- for (const prop of opts.properties) {
261
- if (!t3.isObjectProperty(prop) || !t3.isIdentifier(prop.key)) continue;
262
- if (prop.key.name === "from") {
263
- if (t3.isArrayExpression(prop.value)) {
264
- from = prop.value.elements.filter((el) => t3.isStringLiteral(el)).map((el) => el.value);
265
- } else if (t3.isStringLiteral(prop.value)) {
266
- from = [prop.value.value];
267
- }
268
- } else if (prop.key.name === "to") {
269
- if (t3.isStringLiteral(prop.value)) {
270
- to = prop.value.value;
271
- }
272
- }
273
- }
274
- }
275
- const transition = {
276
- name: transitionName,
277
- from,
278
- to,
279
- actions: [],
280
- conditions: []
281
- };
282
- compilerState.transitions.push(transition);
283
- }
284
- function inferTransitionStates(transitions, states) {
285
- const stateArray = Array.from(states.values());
286
- const startStates = stateArray.filter((s) => s.type === "START");
287
- const regularStates = stateArray.filter((s) => s.type === "REGULAR");
288
- const needsInference = transitions.filter((t14) => t14.from.length === 0 || !t14.to);
289
- if (needsInference.length === 0) return;
290
- if (startStates.length === 1 && regularStates.length > 0) {
291
- needsInference.forEach((t14, idx) => {
292
- if (t14.from.length === 0) {
293
- t14.from = [startStates[0].name];
294
- }
295
- if (!t14.to) {
296
- t14.to = regularStates[idx % regularStates.length]?.name || startStates[0].name;
297
- }
298
- });
299
- } else {
300
- const allStateNames = stateArray.map((s) => s.name);
301
- needsInference.forEach((t14, idx) => {
302
- if (t14.from.length === 0 && allStateNames.length > 0) {
303
- t14.from = [allStateNames[0]];
304
- }
305
- if (!t14.to && allStateNames.length > 1) {
306
- t14.to = allStateNames[Math.min(idx + 1, allStateNames.length - 1)];
307
- }
308
- });
309
- }
310
- }
311
-
312
- // src/babel/extractors/event-extractor.ts
313
- import * as t4 from "@babel/types";
314
- function analyzeEventCallback(body) {
315
- const actions = [];
316
- for (const statement of body) {
317
- if (t4.isExpressionStatement(statement)) {
318
- const expr = statement.expression;
319
- if (t4.isCallExpression(expr) && t4.isIdentifier(expr.callee)) {
320
- const calleeName = expr.callee.name;
321
- const args = expr.arguments;
322
- if (calleeName === "setField" && args.length >= 2) {
323
- const field = t4.isStringLiteral(args[0]) ? args[0].value : void 0;
324
- const expression = generateExpression(args[1]);
325
- if (field) {
326
- actions.push({
327
- type: "set_field",
328
- field,
329
- expression
330
- });
331
- }
332
- } else if (calleeName === "setMemory" && args.length >= 2) {
333
- const key = t4.isStringLiteral(args[0]) ? args[0].value : void 0;
334
- const expression = generateExpression(args[1]);
335
- if (key) {
336
- actions.push({
337
- type: "set_memory",
338
- key,
339
- expression
340
- });
341
- }
342
- }
343
- } else if (t4.isCallExpression(expr)) {
344
- const callee = expr.callee;
345
- const args = expr.arguments;
346
- if (t4.isMemberExpression(callee) && t4.isIdentifier(callee.object) && callee.object.name === "console" && t4.isIdentifier(callee.property) && callee.property.name === "log") {
347
- const message = args.length > 0 ? generateExpression(args[0]) : "";
348
- actions.push({
349
- type: "log_event",
350
- message
351
- });
352
- }
353
- }
354
- }
355
- }
356
- return actions;
357
- }
358
- function generateExpression(node) {
359
- if (t4.isStringLiteral(node)) return node.value;
360
- if (t4.isNumericLiteral(node)) return String(node.value);
361
- if (t4.isBooleanLiteral(node)) return String(node.value);
362
- if (t4.isIdentifier(node)) return node.name;
363
- if (t4.isBinaryExpression(node)) {
364
- return `${generateExpression(node.left)} ${node.operator} ${generateExpression(node.right)}`;
365
- }
366
- if (t4.isCallExpression(node) && t4.isIdentifier(node.callee)) {
367
- const args = node.arguments.map((arg) => generateExpression(arg)).join(", ");
368
- return `${node.callee.name}(${args})`;
369
- }
370
- return "[Expression]";
371
- }
372
- function ensureState2(states, stateName) {
373
- if (!states.has(stateName)) {
374
- states.set(stateName, {
375
- name: stateName,
376
- type: "REGULAR",
377
- on_enter: [],
378
- during: [],
379
- on_exit: []
380
- });
381
- }
382
- return states.get(stateName);
383
- }
384
- function extractEvents(path, state) {
385
- const callee = path.node.callee;
386
- if (!t4.isIdentifier(callee) || callee.name !== "useOnEvent") return;
387
- const args = path.node.arguments;
388
- if (args.length < 2) return;
389
- const patternArg = args[0];
390
- const callbackArg = args[1];
391
- const optionsArg = args.length > 2 ? args[2] : null;
392
- if (!t4.isStringLiteral(patternArg)) return;
393
- const pattern = patternArg.value;
394
- let callbackBody = [];
395
- if (t4.isArrowFunctionExpression(callbackArg) || t4.isFunctionExpression(callbackArg)) {
396
- const body = callbackArg.body;
397
- if (t4.isBlockStatement(body)) {
398
- callbackBody = body.body;
399
- } else if (t4.isExpression(body)) {
400
- callbackBody = [t4.expressionStatement(body)];
401
- }
402
- }
403
- let stateName = null;
404
- if (optionsArg && t4.isObjectExpression(optionsArg)) {
405
- const whileProp = optionsArg.properties.find(
406
- (prop) => t4.isObjectProperty(prop) && t4.isIdentifier(prop.key) && prop.key.name === "while"
407
- );
408
- if (whileProp && t4.isObjectProperty(whileProp) && t4.isStringLiteral(whileProp.value)) {
409
- stateName = whileProp.value.value;
410
- }
411
- }
412
- const actions = analyzeEventCallback(callbackBody);
413
- const subscription = {
414
- match: pattern,
415
- conditions: [],
416
- actions
417
- };
418
- const compilerState = state;
419
- if (!compilerState.states) compilerState.states = /* @__PURE__ */ new Map();
420
- if (stateName) {
421
- const stateDefinition = ensureState2(compilerState.states, stateName);
422
- if (!stateDefinition.on_event) stateDefinition.on_event = [];
423
- stateDefinition.on_event.push(subscription);
424
- } else {
425
- if (!compilerState.events) compilerState.events = [];
426
- compilerState.events.push(subscription);
427
- }
428
- }
429
-
430
- // src/babel/extractors/component-extractor.ts
431
- import * as t5 from "@babel/types";
432
- var nodeIdCounter = 0;
433
- function resetNodeIdCounter() {
434
- nodeIdCounter = 0;
435
- }
436
- function jsxToExperienceNode(node) {
437
- if (t5.isJSXFragment(node)) {
438
- return {
439
- id: `fragment_${++nodeIdCounter}`,
440
- children: extractChildren(node.children)
441
- };
442
- }
443
- const element = node.openingElement;
444
- const componentName = resolveComponentName(element.name);
445
- const id = generateNodeId(componentName);
446
- const config = {};
447
- const bindings = {};
448
- let visibleWhen;
449
- let layout;
450
- const layoutMap = {
451
- Stack: "stack",
452
- Row: "row",
453
- Grid: "grid",
454
- Tabs: "tabs",
455
- Column: "column"
456
- };
457
- if (layoutMap[componentName]) {
458
- layout = layoutMap[componentName];
459
- }
460
- for (const attr of element.attributes) {
461
- if (t5.isJSXAttribute(attr) && t5.isJSXIdentifier(attr.name)) {
462
- const attrName = attr.name.name;
463
- const attrValue = attr.value;
464
- if (attrName === "visible_when" && t5.isStringLiteral(attrValue)) {
465
- visibleWhen = attrValue.value;
466
- } else if (attrName === "data-slot" && t5.isStringLiteral(attrValue)) {
467
- config.slot = attrValue.value;
468
- } else if (t5.isJSXExpressionContainer(attrValue)) {
469
- const expr = attrValue.expression;
470
- if (t5.isJSXEmptyExpression(expr)) continue;
471
- if (t5.isNumericLiteral(expr)) {
472
- config[attrName] = expr.value;
473
- } else if (t5.isBooleanLiteral(expr)) {
474
- config[attrName] = expr.value;
475
- } else if (t5.isStringLiteral(expr)) {
476
- config[attrName] = expr.value;
477
- } else if (t5.isIdentifier(expr)) {
478
- bindings[attrName] = `$instance.${expr.name}`;
479
- } else if (t5.isExpression(expr)) {
480
- bindings[attrName] = generateExpression2(expr);
481
- }
482
- } else if (t5.isStringLiteral(attrValue)) {
483
- config[attrName] = attrValue.value;
484
- } else if (attrValue === null) {
485
- config[attrName] = true;
486
- }
487
- } else if (t5.isJSXSpreadAttribute(attr)) {
488
- if (t5.isIdentifier(attr.argument)) {
489
- bindings["..." + attr.argument.name] = `$instance.${attr.argument.name}`;
490
- }
491
- }
492
- }
493
- const children = extractChildren(node.children);
494
- const experienceNode = {
495
- id,
496
- component: componentName,
497
- ...layout && { layout },
498
- ...Object.keys(bindings).length > 0 && { bindings },
499
- ...Object.keys(config).length > 0 && { config },
500
- ...visibleWhen && { visible_when: visibleWhen },
501
- ...children.length > 0 && { children }
502
- };
503
- return experienceNode;
504
- }
505
- function resolveComponentName(name) {
506
- if (t5.isJSXIdentifier(name)) return name.name;
507
- if (t5.isJSXMemberExpression(name)) {
508
- const obj = resolveComponentName(name.object);
509
- return `${obj}.${name.property.name}`;
510
- }
511
- if (t5.isJSXNamespacedName(name)) {
512
- return `${name.namespace.name}:${name.name.name}`;
513
- }
514
- return "div";
515
- }
516
- function extractChildren(children) {
517
- const nodes = [];
518
- for (const child of children) {
519
- if (t5.isJSXElement(child)) {
520
- nodes.push(jsxToExperienceNode(child));
521
- } else if (t5.isJSXFragment(child)) {
522
- nodes.push(jsxToExperienceNode(child));
523
- } else if (t5.isJSXText(child)) {
524
- const text = child.value.trim();
525
- if (text) {
526
- nodes.push({
527
- id: `text_${++nodeIdCounter}`,
528
- component: "Text",
529
- config: { value: text }
530
- });
531
- }
532
- } else if (t5.isJSXExpressionContainer(child)) {
533
- const expr = child.expression;
534
- if (t5.isJSXEmptyExpression(expr)) continue;
535
- if (t5.isConditionalExpression(expr)) {
536
- const condExpr = generateExpression2(expr.test);
537
- if (t5.isJSXElement(expr.consequent) || t5.isJSXFragment(expr.consequent)) {
538
- const consequent = jsxToExperienceNode(expr.consequent);
539
- consequent.visible_when = condExpr;
540
- nodes.push(consequent);
541
- }
542
- if (t5.isJSXElement(expr.alternate) || t5.isJSXFragment(expr.alternate)) {
543
- const alternate = jsxToExperienceNode(expr.alternate);
544
- alternate.visible_when = `not(${condExpr})`;
545
- nodes.push(alternate);
546
- }
547
- } else if (t5.isLogicalExpression(expr) && expr.operator === "&&") {
548
- const condExpr = generateExpression2(expr.left);
549
- if (t5.isJSXElement(expr.right) || t5.isJSXFragment(expr.right)) {
550
- const element = jsxToExperienceNode(expr.right);
551
- element.visible_when = condExpr;
552
- nodes.push(element);
553
- }
554
- } else if (t5.isCallExpression(expr) && t5.isMemberExpression(expr.callee) && t5.isIdentifier(expr.callee.property) && expr.callee.property.name === "map" && expr.arguments.length > 0) {
555
- const listSource = generateExpression2(expr.callee.object);
556
- const mapFn = expr.arguments[0];
557
- let itemTemplate = null;
558
- let itemAlias = "item";
559
- if (t5.isArrowFunctionExpression(mapFn) || t5.isFunctionExpression(mapFn)) {
560
- if (mapFn.params.length > 0 && t5.isIdentifier(mapFn.params[0])) {
561
- itemAlias = mapFn.params[0].name;
562
- }
563
- const body = mapFn.body;
564
- if (t5.isJSXElement(body) || t5.isJSXFragment(body)) {
565
- itemTemplate = jsxToExperienceNode(body);
566
- } else if (t5.isBlockStatement(body)) {
567
- for (const stmt of body.body) {
568
- if (t5.isReturnStatement(stmt) && stmt.argument) {
569
- if (t5.isJSXElement(stmt.argument) || t5.isJSXFragment(stmt.argument)) {
570
- itemTemplate = jsxToExperienceNode(stmt.argument);
571
- }
572
- break;
573
- }
574
- }
575
- }
576
- }
577
- nodes.push({
578
- id: `each_${++nodeIdCounter}`,
579
- component: "Each",
580
- bindings: { items: `$instance.${listSource}` },
581
- config: { as: itemAlias },
582
- ...itemTemplate ? { children: [itemTemplate] } : {}
583
- });
584
- } else if (t5.isIdentifier(expr)) {
585
- nodes.push({
586
- id: `text_${++nodeIdCounter}`,
587
- component: "Text",
588
- bindings: { value: `$instance.${expr.name}` }
589
- });
590
- } else if (t5.isExpression(expr)) {
591
- const exprStr = generateExpression2(expr);
592
- if (exprStr !== "[Expression]") {
593
- nodes.push({
594
- id: `text_${++nodeIdCounter}`,
595
- component: "Text",
596
- bindings: { value: exprStr }
597
- });
598
- }
599
- }
600
- }
601
- }
602
- return nodes;
603
- }
604
- function generateNodeId(componentName) {
605
- const id = componentName.replace(/\./g, "-").replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
606
- return `${id}_${++nodeIdCounter}`;
607
- }
608
- function generateExpression2(node) {
609
- if (t5.isIdentifier(node)) return node.name;
610
- if (t5.isStringLiteral(node)) return `"${node.value}"`;
611
- if (t5.isNumericLiteral(node)) return String(node.value);
612
- if (t5.isBooleanLiteral(node)) return String(node.value);
613
- if (t5.isNullLiteral(node)) return "null";
614
- if (t5.isMemberExpression(node)) {
615
- const obj = t5.isExpression(node.object) ? generateExpression2(node.object) : "[object]";
616
- const prop = t5.isIdentifier(node.property) ? node.property.name : "[property]";
617
- return `${obj}.${prop}`;
618
- }
619
- if (t5.isBinaryExpression(node)) {
620
- return `${generateExpression2(node.left)} ${node.operator} ${generateExpression2(node.right)}`;
621
- }
622
- if (t5.isUnaryExpression(node) && node.prefix) {
623
- return `${node.operator}${generateExpression2(node.argument)}`;
624
- }
625
- if (t5.isCallExpression(node) && t5.isIdentifier(node.callee)) {
626
- const args = node.arguments.map((a) => t5.isExpression(a) ? generateExpression2(a) : "[arg]").join(", ");
627
- return `${node.callee.name}(${args})`;
628
- }
629
- if (t5.isCallExpression(node) && t5.isMemberExpression(node.callee)) {
630
- const obj = generateExpression2(node.callee.object);
631
- const prop = t5.isIdentifier(node.callee.property) ? node.callee.property.name : "[method]";
632
- const args = node.arguments.map((a) => t5.isExpression(a) ? generateExpression2(a) : "[arg]").join(", ");
633
- return `${obj}.${prop}(${args})`;
634
- }
635
- if (t5.isTemplateLiteral(node)) {
636
- return node.quasis.map((q, i) => {
637
- const raw = q.value.raw;
638
- if (i < node.expressions.length) {
639
- return raw + "${" + generateExpression2(node.expressions[i]) + "}";
640
- }
641
- return raw;
642
- }).join("");
643
- }
644
- return "[Expression]";
645
- }
646
- function extractComponents(path, state) {
647
- const arg = path.node.argument;
648
- if (!arg) return;
649
- let experienceNode = null;
650
- if (t5.isJSXElement(arg)) {
651
- experienceNode = jsxToExperienceNode(arg);
652
- } else if (t5.isJSXFragment(arg)) {
653
- experienceNode = jsxToExperienceNode(arg);
654
- }
655
- if (experienceNode) {
656
- const compilerState = state;
657
- compilerState.experience = experienceNode;
658
- }
659
- }
660
-
661
- // src/babel/extractors/during-extractor.ts
662
- import * as t6 from "@babel/types";
663
- var duringIdCounter = 0;
664
- function resetDuringIdCounter() {
665
- duringIdCounter = 0;
666
- }
667
- function analyzeCallbackBody2(body, actionCounter) {
668
- const actions = [];
669
- for (const statement of body) {
670
- if (!t6.isExpressionStatement(statement)) continue;
671
- const expr = statement.expression;
672
- if (t6.isCallExpression(expr) && t6.isIdentifier(expr.callee)) {
673
- const calleeName = expr.callee.name;
674
- const args = expr.arguments;
675
- if (calleeName === "setField" && args.length >= 2) {
676
- const field = t6.isStringLiteral(args[0]) ? args[0].value : void 0;
677
- const arg1 = args[1];
678
- if (field !== void 0 && !t6.isArgumentPlaceholder(arg1)) {
679
- actions.push({
680
- id: `auto_${++actionCounter.value}`,
681
- type: "set_field",
682
- mode: "auto",
683
- config: { field, value: extractValue2(arg1) }
684
- });
685
- }
686
- } else if (calleeName === "setMemory" && args.length >= 2) {
687
- const key = t6.isStringLiteral(args[0]) ? args[0].value : void 0;
688
- const arg1 = args[1];
689
- if (key !== void 0 && !t6.isArgumentPlaceholder(arg1)) {
690
- actions.push({
691
- id: `auto_${++actionCounter.value}`,
692
- type: "set_memory",
693
- mode: "auto",
694
- config: { key, value: extractValue2(arg1) }
695
- });
696
- }
697
- } else if (calleeName.startsWith("set") && calleeName.length > 3 && args.length >= 1) {
698
- const fieldName = calleeName.slice(3, 4).toLowerCase() + calleeName.slice(4);
699
- const arg0 = args[0];
700
- if (!t6.isArgumentPlaceholder(arg0)) {
701
- actions.push({
702
- id: `auto_${++actionCounter.value}`,
703
- type: "set_field",
704
- mode: "auto",
705
- config: { field: fieldName, value: extractValue2(arg0) }
706
- });
707
- }
708
- }
709
- } else if (t6.isCallExpression(expr)) {
710
- const callee = expr.callee;
711
- if (t6.isMemberExpression(callee) && t6.isIdentifier(callee.object) && callee.object.name === "console" && t6.isIdentifier(callee.property) && callee.property.name === "log" && expr.arguments.length > 0) {
712
- const arg0 = expr.arguments[0];
713
- if (!t6.isArgumentPlaceholder(arg0)) {
714
- actions.push({
715
- id: `auto_${++actionCounter.value}`,
716
- type: "log_event",
717
- mode: "auto",
718
- config: { message: extractValue2(arg0) }
719
- });
720
- }
721
- } else {
722
- actions.push({
723
- id: `auto_${++actionCounter.value}`,
724
- type: "custom",
725
- mode: "auto",
726
- config: { expression: generateCode2(expr) }
727
- });
728
- }
729
- }
730
- }
731
- return actions;
732
- }
733
- function extractValue2(node) {
734
- if (t6.isNumericLiteral(node)) return node.value;
735
- if (t6.isStringLiteral(node)) return node.value;
736
- if (t6.isBooleanLiteral(node)) return node.value;
737
- if (t6.isNullLiteral(node)) return null;
738
- if (t6.isArrayExpression(node)) {
739
- return node.elements.map((el) => el && t6.isExpression(el) ? extractValue2(el) : null);
740
- }
741
- if (t6.isObjectExpression(node)) {
742
- const obj = {};
743
- node.properties.forEach((prop) => {
744
- if (t6.isObjectProperty(prop) && t6.isIdentifier(prop.key) && t6.isExpression(prop.value)) {
745
- obj[prop.key.name] = extractValue2(prop.value);
746
- }
747
- });
748
- return obj;
749
- }
750
- return generateCode2(node);
751
- }
752
- function generateCode2(node) {
753
- if (t6.isIdentifier(node)) return node.name;
754
- if (t6.isNumericLiteral(node)) return String(node.value);
755
- if (t6.isStringLiteral(node)) return `"${node.value}"`;
756
- if (t6.isBooleanLiteral(node)) return String(node.value);
757
- if (t6.isNullLiteral(node)) return "null";
758
- if (t6.isBinaryExpression(node)) {
759
- return `${generateCode2(node.left)} ${node.operator} ${generateCode2(node.right)}`;
760
- }
761
- if (t6.isCallExpression(node) && t6.isMemberExpression(node.callee) && t6.isIdentifier(node.callee.object) && node.callee.object.name === "Date" && t6.isIdentifier(node.callee.property) && node.callee.property.name === "now") {
762
- return "now()";
763
- }
764
- if (t6.isMemberExpression(node) && t6.isIdentifier(node.object) && t6.isIdentifier(node.property)) {
765
- return `${node.object.name}.${node.property.name}`;
766
- }
767
- if (t6.isArrowFunctionExpression(node) || t6.isFunctionExpression(node)) {
768
- if (t6.isArrowFunctionExpression(node) && t6.isExpression(node.body)) {
769
- const params = node.params;
770
- if (params.length === 1 && t6.isIdentifier(params[0])) {
771
- const paramName = params[0].name;
772
- const bodyCode = generateCode2(node.body);
773
- return bodyCode.replace(new RegExp(`\\b${paramName}\\b`, "g"), "$prev");
774
- }
775
- }
776
- return "[Function]";
777
- }
778
- return "[Expression]";
779
- }
780
- function ensureState3(states, stateName) {
781
- if (!states.has(stateName)) {
782
- states.set(stateName, {
783
- name: stateName,
784
- type: "REGULAR",
785
- on_enter: [],
786
- during: [],
787
- on_exit: []
788
- });
789
- }
790
- return states.get(stateName);
791
- }
792
- function extractDuring(path, state) {
793
- const callee = path.node.callee;
794
- if (!t6.isIdentifier(callee) || callee.name !== "useWhileIn") return;
795
- const args = path.node.arguments;
796
- if (args.length < 3) return;
797
- const stateArg = args[0];
798
- const intervalArg = args[1];
799
- const callbackArg = args[2];
800
- if (!t6.isStringLiteral(stateArg)) return;
801
- const stateName = stateArg.value;
802
- let intervalMs = 1e3;
803
- if (t6.isNumericLiteral(intervalArg)) {
804
- intervalMs = intervalArg.value;
805
- }
806
- let callbackBody = [];
807
- if (t6.isArrowFunctionExpression(callbackArg) || t6.isFunctionExpression(callbackArg)) {
808
- const body = callbackArg.body;
809
- if (t6.isBlockStatement(body)) {
810
- callbackBody = body.body;
811
- } else if (t6.isExpression(body)) {
812
- callbackBody = [t6.expressionStatement(body)];
813
- }
814
- }
815
- const compilerState = state;
816
- if (!compilerState.states) compilerState.states = /* @__PURE__ */ new Map();
817
- if (!compilerState.actionCounter) compilerState.actionCounter = 0;
818
- const stateDefinition = ensureState3(compilerState.states, stateName);
819
- const actions = analyzeCallbackBody2(callbackBody, { value: compilerState.actionCounter });
820
- compilerState.actionCounter += actions.length;
821
- const duringAction = {
822
- id: `during_${++duringIdCounter}`,
823
- type: "interval",
824
- interval_ms: intervalMs,
825
- actions
826
- };
827
- stateDefinition.during.push(duringAction);
828
- }
829
-
830
- // src/babel/extractors/change-extractor.ts
831
- import * as t7 from "@babel/types";
832
- var watcherIdCounter = 0;
833
- function resetWatcherIdCounter() {
834
- watcherIdCounter = 0;
835
- }
836
- function analyzeCallbackBody3(body, actionCounter) {
837
- const actions = [];
838
- for (const statement of body) {
839
- if (!t7.isExpressionStatement(statement)) continue;
840
- const expr = statement.expression;
841
- if (t7.isCallExpression(expr) && t7.isIdentifier(expr.callee)) {
842
- const calleeName = expr.callee.name;
843
- const args = expr.arguments;
844
- if (calleeName === "setField" && args.length >= 2) {
845
- const field = t7.isStringLiteral(args[0]) ? args[0].value : void 0;
846
- if (field !== void 0) {
847
- actions.push({
848
- id: `auto_${++actionCounter.value}`,
849
- type: "set_field",
850
- mode: "auto",
851
- config: { field, value: extractValue3(args[1]) }
852
- });
853
- }
854
- } else if (calleeName === "setMemory" && args.length >= 2) {
855
- const key = t7.isStringLiteral(args[0]) ? args[0].value : void 0;
856
- if (key !== void 0) {
857
- actions.push({
858
- id: `auto_${++actionCounter.value}`,
859
- type: "set_memory",
860
- mode: "auto",
861
- config: { key, value: extractValue3(args[1]) }
862
- });
863
- }
864
- } else if (calleeName.startsWith("set") && calleeName.length > 3 && args.length >= 1) {
865
- const fieldName = calleeName.slice(3, 4).toLowerCase() + calleeName.slice(4);
866
- actions.push({
867
- id: `auto_${++actionCounter.value}`,
868
- type: "set_field",
869
- mode: "auto",
870
- config: { field: fieldName, value: extractValue3(args[0]) }
871
- });
872
- }
873
- } else if (t7.isCallExpression(expr)) {
874
- const callee = expr.callee;
875
- if (t7.isMemberExpression(callee) && t7.isIdentifier(callee.object) && callee.object.name === "console" && t7.isIdentifier(callee.property) && callee.property.name === "log" && expr.arguments.length > 0) {
876
- actions.push({
877
- id: `auto_${++actionCounter.value}`,
878
- type: "log_event",
879
- mode: "auto",
880
- config: { message: extractValue3(expr.arguments[0]) }
881
- });
882
- }
883
- }
884
- }
885
- return actions;
886
- }
887
- function extractValue3(node) {
888
- if (t7.isNumericLiteral(node)) return node.value;
889
- if (t7.isStringLiteral(node)) return node.value;
890
- if (t7.isBooleanLiteral(node)) return node.value;
891
- if (t7.isNullLiteral(node)) return null;
892
- if (t7.isIdentifier(node)) return node.name;
893
- if (t7.isTemplateLiteral(node)) {
894
- return node.quasis.map((q) => q.value.raw).join("${}");
895
- }
896
- return "[Expression]";
897
- }
898
- function extractChangeWatcher(path, state) {
899
- const callee = path.node.callee;
900
- if (!t7.isIdentifier(callee) || callee.name !== "useOnChange") return;
901
- const args = path.node.arguments;
902
- if (args.length < 2) return;
903
- const fieldArg = args[0];
904
- const callbackArg = args[1];
905
- if (!t7.isStringLiteral(fieldArg)) return;
906
- const field = fieldArg.value;
907
- let callbackBody = [];
908
- if (t7.isArrowFunctionExpression(callbackArg) || t7.isFunctionExpression(callbackArg)) {
909
- const body = callbackArg.body;
910
- if (t7.isBlockStatement(body)) {
911
- callbackBody = body.body;
912
- } else if (t7.isExpression(body)) {
913
- callbackBody = [t7.expressionStatement(body)];
914
- }
915
- }
916
- const compilerState = state;
917
- if (!compilerState.actionCounter) compilerState.actionCounter = 0;
918
- const actions = analyzeCallbackBody3(callbackBody, { value: compilerState.actionCounter });
919
- compilerState.actionCounter += actions.length;
920
- const watcher = {
921
- id: `watcher_${++watcherIdCounter}`,
922
- field,
923
- actions
924
- };
925
- if (!compilerState.metadata) compilerState.metadata = {};
926
- const meta = compilerState.metadata;
927
- if (!meta.fieldWatchers) meta.fieldWatchers = [];
928
- meta.fieldWatchers.push(watcher);
929
- }
930
-
931
- // src/babel/extractors/transition-effect-extractor.ts
932
- import * as t8 from "@babel/types";
933
- var transitionEffectIdCounter = 0;
934
- function resetTransitionEffectIdCounter() {
935
- transitionEffectIdCounter = 0;
936
- }
937
- function analyzeCallbackBody4(body, actionCounter) {
938
- const actions = [];
939
- for (const statement of body) {
940
- if (!t8.isExpressionStatement(statement)) continue;
941
- const expr = statement.expression;
942
- if (t8.isCallExpression(expr) && t8.isIdentifier(expr.callee)) {
943
- const calleeName = expr.callee.name;
944
- const args = expr.arguments;
945
- if (calleeName === "setField" && args.length >= 2) {
946
- const field = t8.isStringLiteral(args[0]) ? args[0].value : void 0;
947
- if (field !== void 0) {
948
- actions.push({
949
- id: `auto_${++actionCounter.value}`,
950
- type: "set_field",
951
- mode: "auto",
952
- config: { field, value: extractValue4(args[1]) }
953
- });
954
- }
955
- } else if (calleeName === "setMemory" && args.length >= 2) {
956
- const key = t8.isStringLiteral(args[0]) ? args[0].value : void 0;
957
- if (key !== void 0) {
958
- actions.push({
959
- id: `auto_${++actionCounter.value}`,
960
- type: "set_memory",
961
- mode: "auto",
962
- config: { key, value: extractValue4(args[1]) }
963
- });
964
- }
965
- } else if (calleeName.startsWith("set") && calleeName.length > 3 && args.length >= 1) {
966
- const fieldName = calleeName.slice(3, 4).toLowerCase() + calleeName.slice(4);
967
- actions.push({
968
- id: `auto_${++actionCounter.value}`,
969
- type: "set_field",
970
- mode: "auto",
971
- config: { field: fieldName, value: extractValue4(args[0]) }
972
- });
973
- }
974
- } else if (t8.isCallExpression(expr)) {
975
- const callee = expr.callee;
976
- if (t8.isMemberExpression(callee) && t8.isIdentifier(callee.object) && callee.object.name === "console" && t8.isIdentifier(callee.property) && callee.property.name === "log" && expr.arguments.length > 0) {
977
- actions.push({
978
- id: `auto_${++actionCounter.value}`,
979
- type: "log_event",
980
- mode: "auto",
981
- config: { message: extractValue4(expr.arguments[0]) }
982
- });
983
- }
984
- }
985
- }
986
- return actions;
987
- }
988
- function extractValue4(node) {
989
- if (t8.isNumericLiteral(node)) return node.value;
990
- if (t8.isStringLiteral(node)) return node.value;
991
- if (t8.isBooleanLiteral(node)) return node.value;
992
- if (t8.isNullLiteral(node)) return null;
993
- if (t8.isIdentifier(node)) return node.name;
994
- return "[Expression]";
995
- }
996
- function extractTransitionEffect(path, state) {
997
- const callee = path.node.callee;
998
- if (!t8.isIdentifier(callee) || callee.name !== "useOnTransition") return;
999
- const args = path.node.arguments;
1000
- if (args.length < 1) return;
1001
- const callbackArg = args[0];
1002
- let callbackBody = [];
1003
- if (t8.isArrowFunctionExpression(callbackArg) || t8.isFunctionExpression(callbackArg)) {
1004
- const body = callbackArg.body;
1005
- if (t8.isBlockStatement(body)) {
1006
- callbackBody = body.body;
1007
- } else if (t8.isExpression(body)) {
1008
- callbackBody = [t8.expressionStatement(body)];
1009
- }
1010
- }
1011
- const compilerState = state;
1012
- if (!compilerState.actionCounter) compilerState.actionCounter = 0;
1013
- const actions = analyzeCallbackBody4(callbackBody, { value: compilerState.actionCounter });
1014
- compilerState.actionCounter += actions.length;
1015
- const effect = {
1016
- id: `transition_effect_${++transitionEffectIdCounter}`,
1017
- actions
1018
- };
1019
- if (!compilerState.metadata) compilerState.metadata = {};
1020
- const meta = compilerState.metadata;
1021
- if (!meta.transitionEffects) meta.transitionEffects = [];
1022
- meta.transitionEffects.push(effect);
1023
- }
1024
-
1025
- // src/babel/extractors/model-extractor.ts
1026
- import * as t9 from "@babel/types";
1027
- function tsTypeToFieldType(typeAnnotation) {
1028
- if (!typeAnnotation) return "text";
1029
- const annotation = typeAnnotation.typeAnnotation;
1030
- return resolveType(annotation);
1031
- }
1032
- function resolveType(annotation) {
1033
- if (t9.isTSStringKeyword(annotation)) return "text";
1034
- if (t9.isTSNumberKeyword(annotation)) return "number";
1035
- if (t9.isTSBooleanKeyword(annotation)) return "boolean";
1036
- if (t9.isTSTypeReference(annotation) && t9.isIdentifier(annotation.typeName) && annotation.typeName.name === "Date") {
1037
- return "datetime";
1038
- }
1039
- if (t9.isTSTypeReference(annotation) && t9.isIdentifier(annotation.typeName) && annotation.typeName.name === "Record") {
1040
- return "json";
1041
- }
1042
- if (t9.isTSArrayType(annotation)) {
1043
- return "multi_select";
1044
- }
1045
- if (t9.isTSTypeReference(annotation) && t9.isIdentifier(annotation.typeName) && annotation.typeName.name === "Array") {
1046
- return "multi_select";
1047
- }
1048
- if (t9.isTSUnionType(annotation)) {
1049
- const allLiterals = annotation.types.every(
1050
- (t22) => t9.isTSLiteralType(t22) && (t9.isStringLiteral(t22.literal) || t9.isNumericLiteral(t22.literal))
1051
- );
1052
- if (allLiterals) return "select";
1053
- return "text";
1054
- }
1055
- if (t9.isTSLiteralType(annotation) && t9.isStringLiteral(annotation.literal)) return "text";
1056
- if (t9.isTSLiteralType(annotation) && t9.isNumericLiteral(annotation.literal)) return "number";
1057
- return "text";
1058
- }
1059
- function extractUnionOptions(annotation) {
1060
- if (!t9.isTSUnionType(annotation)) return void 0;
1061
- const options = [];
1062
- for (const member of annotation.types) {
1063
- if (t9.isTSLiteralType(member) && t9.isStringLiteral(member.literal)) {
1064
- options.push(member.literal.value);
1065
- }
1066
- }
1067
- return options.length > 0 ? options : void 0;
1068
- }
1069
- function extractDefaultValue2(fieldType) {
1070
- switch (fieldType) {
1071
- case "text":
1072
- return "";
1073
- case "rich_text":
1074
- return "";
1075
- case "number":
1076
- return 0;
1077
- case "boolean":
1078
- return false;
1079
- case "date":
1080
- return null;
1081
- case "datetime":
1082
- return null;
1083
- case "select":
1084
- return "";
1085
- case "multi_select":
1086
- return [];
1087
- case "url":
1088
- return "";
1089
- case "json":
1090
- return {};
1091
- default:
1092
- return null;
1093
- }
1094
- }
1095
- function extractFieldsFromInterface(declaration) {
1096
- const fields = [];
1097
- for (const member of declaration.body.body) {
1098
- if (!t9.isTSPropertySignature(member)) continue;
1099
- if (!t9.isIdentifier(member.key)) continue;
1100
- const name = member.key.name;
1101
- const required = !member.optional;
1102
- const typeAnno = member.typeAnnotation;
1103
- const fieldType = tsTypeToFieldType(typeAnno);
1104
- const field = {
1105
- name,
1106
- type: fieldType,
1107
- label: name.replace(/([A-Z])/g, " $1").replace(/^./, (c) => c.toUpperCase()),
1108
- required,
1109
- default_value: extractDefaultValue2(fieldType)
1110
- };
1111
- if (typeAnno) {
1112
- const options = extractUnionOptions(typeAnno.typeAnnotation);
1113
- if (options) {
1114
- field.validation = { options };
1115
- }
1116
- }
1117
- fields.push(field);
1118
- }
1119
- return fields;
1120
- }
1121
- function extractTransitionsFromObject(init, actionCounter) {
1122
- const transitions = [];
1123
- for (const prop of init.properties) {
1124
- if (!t9.isObjectProperty(prop)) continue;
1125
- if (!t9.isIdentifier(prop.key) && !t9.isStringLiteral(prop.key)) continue;
1126
- if (!t9.isObjectExpression(prop.value)) continue;
1127
- const name = t9.isIdentifier(prop.key) ? prop.key.name : prop.key.value;
1128
- const config = extractObjectLiteral(prop.value);
1129
- let from = [];
1130
- if (typeof config.from === "string") {
1131
- from = [config.from];
1132
- } else if (Array.isArray(config.from)) {
1133
- from = config.from.map(String);
1134
- }
1135
- const to = String(config.to || "");
1136
- const conditions = [];
1137
- if (config.guard) {
1138
- conditions.push(parseGuardExpression(config.guard));
1139
- }
1140
- if (config.conditions && Array.isArray(config.conditions)) {
1141
- for (const cond of config.conditions) {
1142
- conditions.push(cond);
1143
- }
1144
- }
1145
- const actions = [];
1146
- if (config.actions && Array.isArray(config.actions)) {
1147
- for (const action of config.actions) {
1148
- if (typeof action === "object" && action !== null) {
1149
- actions.push({
1150
- id: `auto_${++actionCounter.value}`,
1151
- type: action.type || "custom",
1152
- mode: "auto",
1153
- config: action
1154
- });
1155
- }
1156
- }
1157
- }
1158
- transitions.push({
1159
- name,
1160
- from,
1161
- to,
1162
- description: config.description,
1163
- conditions: conditions.length > 0 ? conditions : void 0,
1164
- actions,
1165
- roles: config.roles,
1166
- auto: config.auto,
1167
- required_fields: config.required_fields
1168
- });
1169
- }
1170
- return transitions;
1171
- }
1172
- function parseGuardExpression(guard) {
1173
- const simpleMatch = guard.match(
1174
- /^(\$?\w+(?:\.\w+)*)\s*(not_in|is_set|is_empty|contains|>=|<=|!=|==|>|<|in)\s*(.+)?$/
1175
- );
1176
- if (simpleMatch) {
1177
- const [, field, op, rawValue] = simpleMatch;
1178
- const operatorMap = {
1179
- "==": "eq",
1180
- "!=": "ne",
1181
- ">": "gt",
1182
- ">=": "gte",
1183
- "<": "lt",
1184
- "<=": "lte",
1185
- "in": "in",
1186
- "not_in": "not_in",
1187
- "contains": "contains",
1188
- "is_set": "is_set",
1189
- "is_empty": "is_empty"
1190
- };
1191
- const operator = operatorMap[op];
1192
- if (operator) {
1193
- let value = rawValue?.trim();
1194
- if (value === void 0 || value === "") {
1195
- return { field, operator };
1196
- }
1197
- if (typeof value === "string") {
1198
- if (value.startsWith('"') && value.endsWith('"')) {
1199
- value = value.slice(1, -1);
1200
- } else if (value.startsWith("'") && value.endsWith("'")) {
1201
- value = value.slice(1, -1);
1202
- } else if (value === "true") {
1203
- value = true;
1204
- } else if (value === "false") {
1205
- value = false;
1206
- } else if (value === "null") {
1207
- value = null;
1208
- } else if (!isNaN(Number(value))) {
1209
- value = Number(value);
1210
- }
1211
- }
1212
- return { field, operator, value };
1213
- }
1214
- }
1215
- return { expression: guard };
1216
- }
1217
- function extractHooksFromObject(init, states, actionCounter) {
1218
- for (const prop of init.properties) {
1219
- if (!t9.isObjectProperty(prop)) continue;
1220
- if (!t9.isIdentifier(prop.key) && !t9.isStringLiteral(prop.key)) continue;
1221
- const hookName = t9.isIdentifier(prop.key) ? prop.key.name : prop.key.value;
1222
- const hookConfig = t9.isObjectExpression(prop.value) ? extractObjectLiteral(prop.value) : {};
1223
- const enterMatch = hookName.match(/^on_enter_(.+)$/);
1224
- const exitMatch = hookName.match(/^on_exit_(.+)$/);
1225
- if (!enterMatch && !exitMatch) continue;
1226
- const stateName = enterMatch ? enterMatch[1] : exitMatch[1];
1227
- const phase = enterMatch ? "on_enter" : "on_exit";
1228
- if (!states.has(stateName)) {
1229
- states.set(stateName, {
1230
- name: stateName,
1231
- type: "REGULAR",
1232
- on_enter: [],
1233
- during: [],
1234
- on_exit: []
1235
- });
1236
- }
1237
- const state = states.get(stateName);
1238
- if (hookConfig.emit && typeof hookConfig.emit === "string") {
1239
- const action = {
1240
- id: `auto_${++actionCounter.value}`,
1241
- type: "emit_event",
1242
- mode: "auto",
1243
- config: { event: hookConfig.emit }
1244
- };
1245
- state[phase].push(action);
1246
- }
1247
- if (hookConfig.actions && Array.isArray(hookConfig.actions)) {
1248
- for (const handler of hookConfig.actions) {
1249
- if (typeof handler === "string") {
1250
- const action = {
1251
- id: `auto_${++actionCounter.value}`,
1252
- type: "server_action",
1253
- mode: "auto",
1254
- config: { handler }
1255
- };
1256
- state[phase].push(action);
1257
- }
1258
- }
1259
- }
1260
- }
1261
- }
1262
- function applyFieldOptions(fields, init) {
1263
- const options = extractObjectLiteral(init);
1264
- for (const [fieldName, opts] of Object.entries(options)) {
1265
- const field = fields.find((f) => f.name === fieldName);
1266
- if (!field) continue;
1267
- if (opts.scope || opts.persistence || opts.sync) {
1268
- field.state_home = {
1269
- scope: opts.scope || "instance",
1270
- persistence: opts.persistence || "durable",
1271
- sync: opts.sync || "none"
1272
- };
1273
- }
1274
- if (opts.computed && typeof opts.computed === "string") {
1275
- field.computed = opts.computed;
1276
- }
1277
- if (opts.computed_deps && Array.isArray(opts.computed_deps)) {
1278
- field.computed_deps = opts.computed_deps.map(String);
1279
- }
1280
- if (opts.visible_in_states && Array.isArray(opts.visible_in_states)) {
1281
- field.visible_in_states = opts.visible_in_states.map(String);
1282
- }
1283
- if (opts.editable_in_states && Array.isArray(opts.editable_in_states)) {
1284
- field.editable_in_states = opts.editable_in_states.map(String);
1285
- }
1286
- if (opts.visible_to_roles && Array.isArray(opts.visible_to_roles)) {
1287
- field.visible_to_roles = opts.visible_to_roles.map(String);
1288
- }
1289
- if (opts.editable_by_roles && Array.isArray(opts.editable_by_roles)) {
1290
- field.editable_by_roles = opts.editable_by_roles.map(String);
1291
- }
1292
- if (opts.visible_when && typeof opts.visible_when === "string") {
1293
- field.visible_when = opts.visible_when;
1294
- }
1295
- if (opts.editable_when && typeof opts.editable_when === "string") {
1296
- field.editable_when = opts.editable_when;
1297
- }
1298
- if (opts.validation && typeof opts.validation === "object") {
1299
- const val = opts.validation;
1300
- const validation = field.validation || {};
1301
- if (val.min !== void 0) validation.min = Number(val.min);
1302
- if (val.max !== void 0) validation.max = Number(val.max);
1303
- if (val.minLength !== void 0) validation.minLength = Number(val.minLength);
1304
- if (val.maxLength !== void 0) validation.maxLength = Number(val.maxLength);
1305
- if (val.options && Array.isArray(val.options)) {
1306
- validation.options = val.options.map(String);
1307
- }
1308
- if (val.rules && Array.isArray(val.rules)) {
1309
- validation.rules = val.rules.map((r) => ({
1310
- expression: String(r.expression || ""),
1311
- message: String(r.message || ""),
1312
- severity: r.severity === "warning" ? "warning" : "error"
1313
- }));
1314
- }
1315
- field.validation = validation;
1316
- }
1317
- }
1318
- }
1319
- function extractObjectLiteral(node) {
1320
- const obj = {};
1321
- for (const prop of node.properties) {
1322
- if (!t9.isObjectProperty(prop)) continue;
1323
- const key = t9.isIdentifier(prop.key) ? prop.key.name : t9.isStringLiteral(prop.key) ? prop.key.value : null;
1324
- if (!key) continue;
1325
- obj[key] = extractNodeValue(prop.value);
1326
- }
1327
- return obj;
1328
- }
1329
- function extractNodeValue(node) {
1330
- if (t9.isStringLiteral(node)) return node.value;
1331
- if (t9.isNumericLiteral(node)) return node.value;
1332
- if (t9.isBooleanLiteral(node)) return node.value;
1333
- if (t9.isNullLiteral(node)) return null;
1334
- if (t9.isArrayExpression(node)) {
1335
- return node.elements.map((el) => {
1336
- if (!el) return null;
1337
- if (t9.isSpreadElement(el)) return "[Spread]";
1338
- return extractNodeValue(el);
1339
- });
1340
- }
1341
- if (t9.isObjectExpression(node)) {
1342
- return extractObjectLiteral(node);
1343
- }
1344
- if (t9.isTemplateLiteral(node)) {
1345
- return node.quasis.map((q) => q.value.raw).join("${}");
1346
- }
1347
- if (t9.isIdentifier(node)) {
1348
- if (node.name === "undefined") return void 0;
1349
- return node.name;
1350
- }
1351
- return "[Expression]";
1352
- }
1353
- function isModelFile(path, filename) {
1354
- if (filename && /\/models\//.test(filename)) return true;
1355
- let hasInterface = false;
1356
- let hasTransitions = false;
1357
- for (const node of path.node.body) {
1358
- if (t9.isExportNamedDeclaration(node)) {
1359
- if (t9.isTSInterfaceDeclaration(node.declaration)) {
1360
- hasInterface = true;
1361
- }
1362
- if (t9.isVariableDeclaration(node.declaration)) {
1363
- for (const decl of node.declaration.declarations) {
1364
- if (t9.isIdentifier(decl.id) && decl.id.name === "transitions") {
1365
- hasTransitions = true;
1366
- }
1367
- }
1368
- }
1369
- }
1370
- }
1371
- return hasInterface && hasTransitions;
1372
- }
1373
- function extractModelFile(path, state) {
1374
- const compilerState = state;
1375
- if (!compilerState.actionCounter) compilerState.actionCounter = 0;
1376
- const actionCounter = { value: compilerState.actionCounter };
1377
- let interfaceName = "";
1378
- for (const node of path.node.body) {
1379
- if (!t9.isExportNamedDeclaration(node)) continue;
1380
- const declaration = node.declaration;
1381
- if (t9.isTSInterfaceDeclaration(declaration)) {
1382
- interfaceName = declaration.id.name;
1383
- const fields = extractFieldsFromInterface(declaration);
1384
- compilerState.fields.push(...fields);
1385
- for (const field of fields) {
1386
- if (field.validation?.options && field.validation.options.length > 0) {
1387
- for (const option of field.validation.options) {
1388
- if (!compilerState.states.has(option)) {
1389
- compilerState.states.set(option, {
1390
- name: option,
1391
- type: "REGULAR",
1392
- on_enter: [],
1393
- during: [],
1394
- on_exit: []
1395
- });
1396
- }
1397
- }
1398
- const firstState = compilerState.states.get(field.validation.options[0]);
1399
- if (firstState) firstState.type = "START";
1400
- break;
1401
- }
1402
- }
1403
- if (!compilerState.metadata.name) {
1404
- compilerState.metadata.name = interfaceName;
1405
- }
1406
- }
1407
- if (t9.isVariableDeclaration(declaration)) {
1408
- for (const decl of declaration.declarations) {
1409
- if (!t9.isIdentifier(decl.id) || !decl.init) continue;
1410
- const varName = decl.id.name;
1411
- if (varName === "transitions" && t9.isObjectExpression(decl.init)) {
1412
- const transitions = extractTransitionsFromObject(decl.init, actionCounter);
1413
- compilerState.transitions.push(...transitions);
1414
- for (const trans of transitions) {
1415
- for (const fromState of trans.from) {
1416
- if (!compilerState.states.has(fromState)) {
1417
- compilerState.states.set(fromState, {
1418
- name: fromState,
1419
- type: "REGULAR",
1420
- on_enter: [],
1421
- during: [],
1422
- on_exit: []
1423
- });
1424
- }
1425
- }
1426
- if (trans.to && !compilerState.states.has(trans.to)) {
1427
- compilerState.states.set(trans.to, {
1428
- name: trans.to,
1429
- type: "REGULAR",
1430
- on_enter: [],
1431
- during: [],
1432
- on_exit: []
1433
- });
1434
- }
1435
- }
1436
- }
1437
- if (varName === "endStates" && t9.isArrayExpression(decl.init)) {
1438
- for (const el of decl.init.elements) {
1439
- if (t9.isStringLiteral(el)) {
1440
- if (!compilerState.states.has(el.value)) {
1441
- compilerState.states.set(el.value, {
1442
- name: el.value,
1443
- type: "END",
1444
- on_enter: [],
1445
- during: [],
1446
- on_exit: []
1447
- });
1448
- } else {
1449
- compilerState.states.get(el.value).type = "END";
1450
- }
1451
- }
1452
- }
1453
- }
1454
- if (varName === "cancelledStates" && t9.isArrayExpression(decl.init)) {
1455
- for (const el of decl.init.elements) {
1456
- if (t9.isStringLiteral(el)) {
1457
- if (!compilerState.states.has(el.value)) {
1458
- compilerState.states.set(el.value, {
1459
- name: el.value,
1460
- type: "CANCELLED",
1461
- on_enter: [],
1462
- during: [],
1463
- on_exit: []
1464
- });
1465
- } else {
1466
- compilerState.states.get(el.value).type = "CANCELLED";
1467
- }
1468
- }
1469
- }
1470
- }
1471
- if (varName === "hooks" && t9.isObjectExpression(decl.init)) {
1472
- extractHooksFromObject(decl.init, compilerState.states, actionCounter);
1473
- }
1474
- if (varName === "runtime" && t9.isStringLiteral(decl.init)) {
1475
- compilerState.metadata.runtime = decl.init.value;
1476
- }
1477
- if (varName === "fieldOptions" && t9.isObjectExpression(decl.init)) {
1478
- applyFieldOptions(compilerState.fields, decl.init);
1479
- }
1480
- }
1481
- }
1482
- }
1483
- compilerState.actionCounter = actionCounter.value;
1484
- if (interfaceName && compilerState.metadata.__slugAutoFallback) {
1485
- compilerState.metadata.slug = interfaceName.replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
1486
- delete compilerState.metadata.__slugAutoFallback;
1487
- }
1488
- }
1489
-
1490
- // src/babel/extractors/server-action-extractor.ts
1491
- import * as t10 from "@babel/types";
1492
- function isServerActionFile(filename) {
1493
- if (!filename) return false;
1494
- return /\.server\.(ts|tsx|js|jsx)$/.test(filename);
1495
- }
1496
- function extractServerActions(path, state) {
1497
- const compilerState = state;
1498
- const serverActions = [];
1499
- for (const node of path.node.body) {
1500
- if (t10.isExportNamedDeclaration(node)) {
1501
- const decl = node.declaration;
1502
- if (t10.isFunctionDeclaration(decl) && decl.id) {
1503
- const action = extractFunctionAction(decl);
1504
- if (action) {
1505
- const comments = node.leadingComments || decl.leadingComments || [];
1506
- action.description = extractDescription(comments);
1507
- serverActions.push(action);
1508
- }
1509
- }
1510
- if (t10.isVariableDeclaration(decl)) {
1511
- for (const varDecl of decl.declarations) {
1512
- if (!t10.isIdentifier(varDecl.id)) continue;
1513
- const init = varDecl.init;
1514
- if (t10.isArrowFunctionExpression(init) || t10.isFunctionExpression(init)) {
1515
- const action = {
1516
- name: varDecl.id.name,
1517
- async: init.async || false,
1518
- params: init.params.filter((p) => t10.isIdentifier(p)).map((p) => p.name)
1519
- };
1520
- if (init.params.length > 0) {
1521
- const firstParam = init.params[0];
1522
- if (t10.isIdentifier(firstParam) && firstParam.typeAnnotation) {
1523
- action.contextType = extractTypeName(firstParam.typeAnnotation);
1524
- }
1525
- }
1526
- serverActions.push(action);
1527
- }
1528
- }
1529
- }
1530
- }
1531
- }
1532
- if (serverActions.length > 0) {
1533
- if (!compilerState.metadata) compilerState.metadata = {};
1534
- compilerState.metadata.serverActions = serverActions;
1535
- }
1536
- }
1537
- function extractFunctionAction(decl) {
1538
- if (!decl.id) return null;
1539
- const action = {
1540
- name: decl.id.name,
1541
- async: decl.async || false,
1542
- params: decl.params.filter((p) => t10.isIdentifier(p)).map((p) => p.name)
1543
- };
1544
- if (decl.params.length > 0) {
1545
- const firstParam = decl.params[0];
1546
- if (t10.isIdentifier(firstParam) && firstParam.typeAnnotation) {
1547
- action.contextType = extractTypeName(firstParam.typeAnnotation);
1548
- }
1549
- }
1550
- return action;
1551
- }
1552
- function extractTypeName(annotation) {
1553
- if (t10.isTSTypeAnnotation(annotation)) {
1554
- const typeNode = annotation.typeAnnotation;
1555
- if (t10.isTSTypeReference(typeNode) && t10.isIdentifier(typeNode.typeName)) {
1556
- return typeNode.typeName.name;
1557
- }
1558
- }
1559
- return void 0;
1560
- }
1561
- function extractDescription(comments) {
1562
- for (const comment of comments) {
1563
- if (comment.type !== "CommentBlock") continue;
1564
- const lines = comment.value.split("\n");
1565
- for (const line of lines) {
1566
- const trimmed = line.replace(/^\s*\*\s?/, "").trim();
1567
- if (trimmed && !trimmed.startsWith("@")) {
1568
- return trimmed;
1569
- }
1570
- }
1571
- }
1572
- return void 0;
1573
- }
1574
-
1575
- // src/babel/extractors/grammar-island-extractor.ts
1576
- import * as t11 from "@babel/types";
1577
- var GRAMMAR_TAGS = /* @__PURE__ */ new Set(["cedar", "sql", "cron", "dmn", "graphql", "jsonpath"]);
1578
- function parseCedar(source) {
1579
- const policies = [];
1580
- const policyRegex = /(permit|forbid)\s*\(([^)]*)\)(?:\s*when\s*\{([^}]*)\})?/g;
1581
- let match;
1582
- while ((match = policyRegex.exec(source)) !== null) {
1583
- const [, effect, scope, condition] = match;
1584
- const parts = scope.split(",").map((s) => s.trim());
1585
- const policy = {
1586
- effect,
1587
- conditions: []
1588
- };
1589
- for (const part of parts) {
1590
- if (part.startsWith("principal")) {
1591
- policy.principal = part;
1592
- } else if (part.startsWith("action")) {
1593
- policy.action = part;
1594
- } else if (part.startsWith("resource")) {
1595
- policy.resource = part;
1596
- }
1597
- }
1598
- if (condition) {
1599
- policy.conditions.push(condition.trim());
1600
- }
1601
- policies.push(policy);
1602
- }
1603
- return { policies };
1604
- }
1605
- function parseCron(source) {
1606
- const trimmed = source.trim();
1607
- const parts = trimmed.split(/\s+/);
1608
- const valid = parts.length === 5;
1609
- return {
1610
- expression: trimmed,
1611
- fields: {
1612
- minute: parts[0] || "*",
1613
- hour: parts[1] || "*",
1614
- dayOfMonth: parts[2] || "*",
1615
- month: parts[3] || "*",
1616
- dayOfWeek: parts[4] || "*"
1617
- },
1618
- valid
1619
- };
1620
- }
1621
- function parseDmn(source) {
1622
- const lines = source.split("\n").map((l) => l.trim()).filter((l) => l.length > 0);
1623
- const rules = [];
1624
- const headers = [];
1625
- let hitPolicy = "first";
1626
- if (lines.length === 0) return { hitPolicy, rules, headers };
1627
- const headerLine = lines[0];
1628
- const headerCells = headerLine.split("|").map((c) => c.trim()).filter((c) => c.length > 0);
1629
- headers.push(...headerCells);
1630
- const outputStartIndex = headers.length - 1;
1631
- let dataStart = 1;
1632
- if (lines.length > 1 && /^[|\s-]+$/.test(lines[1])) {
1633
- dataStart = 2;
1634
- }
1635
- const hitPolicyMatch = source.match(/@hitPolicy\s*=\s*"?(\w+)"?/);
1636
- if (hitPolicyMatch) {
1637
- hitPolicy = hitPolicyMatch[1];
1638
- }
1639
- for (let i = dataStart; i < lines.length; i++) {
1640
- const cells = lines[i].split("|").map((c) => c.trim()).filter((c) => c.length > 0);
1641
- if (cells.length < headers.length) continue;
1642
- const inputs = {};
1643
- const output = {};
1644
- for (let j = 0; j < headers.length; j++) {
1645
- if (j >= outputStartIndex) {
1646
- output[headers[j]] = cells[j];
1647
- } else {
1648
- inputs[headers[j]] = cells[j];
1649
- }
1650
- }
1651
- rules.push({ inputs, output });
1652
- }
1653
- return { hitPolicy, rules, headers };
1654
- }
1655
- function extractGrammarIsland(path, state, slug) {
1656
- const tag = path.node.tag;
1657
- if (!t11.isIdentifier(tag)) return;
1658
- const tagName = tag.name;
1659
- if (!GRAMMAR_TAGS.has(tagName)) return;
1660
- const compilerState = state;
1661
- const quasi = path.node.quasi;
1662
- let rawSource = "";
1663
- for (let i = 0; i < quasi.quasis.length; i++) {
1664
- rawSource += quasi.quasis[i].value.raw;
1665
- if (i < quasi.expressions.length) {
1666
- const expr = quasi.expressions[i];
1667
- if (t11.isIdentifier(expr)) {
1668
- rawSource += `\${${expr.name}}`;
1669
- } else {
1670
- rawSource += `\${expr_${i}}`;
1671
- }
1672
- }
1673
- }
1674
- rawSource = rawSource.trim();
1675
- let parsed = void 0;
1676
- switch (tagName) {
1677
- case "cedar":
1678
- parsed = parseCedar(rawSource);
1679
- break;
1680
- case "cron":
1681
- parsed = parseCron(rawSource);
1682
- break;
1683
- case "dmn":
1684
- parsed = parseDmn(rawSource);
1685
- break;
1686
- case "sql":
1687
- parsed = { query: rawSource };
1688
- break;
1689
- case "graphql":
1690
- parsed = { query: rawSource };
1691
- break;
1692
- case "jsonpath":
1693
- parsed = { expression: rawSource };
1694
- break;
1695
- }
1696
- const island = {
1697
- slug,
1698
- contextTag: tagName,
1699
- rawSource,
1700
- parsed
1701
- };
1702
- if (!compilerState.metadata) compilerState.metadata = {};
1703
- const meta = compilerState.metadata;
1704
- if (!meta.grammarIslands) meta.grammarIslands = [];
1705
- meta.grammarIslands.push(island);
1706
- }
1707
- function extractGrammarIslands(path, state) {
1708
- path.traverse({
1709
- TaggedTemplateExpression(templatePath) {
1710
- const tag = templatePath.node.tag;
1711
- if (!t11.isIdentifier(tag) || !GRAMMAR_TAGS.has(tag.name)) return;
1712
- let slug = "unnamed";
1713
- const parent = templatePath.parentPath;
1714
- if (parent && parent.isVariableDeclarator() && t11.isIdentifier(parent.node.id)) {
1715
- slug = parent.node.id.name;
1716
- }
1717
- if (parent && parent.isObjectProperty() && t11.isIdentifier(parent.node.key)) {
1718
- slug = parent.node.key.name;
1719
- }
1720
- if (parent && parent.isObjectProperty() && t11.isStringLiteral(parent.node.key)) {
1721
- slug = parent.node.key.value;
1722
- }
1723
- extractGrammarIsland(templatePath, state, slug);
1724
- }
1725
- });
1726
- }
1727
-
1728
- // src/babel/extractors/context-extractor.ts
1729
- import * as t12 from "@babel/types";
1730
- function hasContextCreation(path) {
1731
- let found = false;
1732
- path.traverse({
1733
- CallExpression(callPath) {
1734
- if (t12.isIdentifier(callPath.node.callee, { name: "createContext" })) {
1735
- found = true;
1736
- callPath.stop();
1737
- }
1738
- }
1739
- });
1740
- return found;
1741
- }
1742
- function extractContextWorkflows(path, state) {
1743
- const compilerState = state;
1744
- const contextWorkflows = [];
1745
- const contextNames = /* @__PURE__ */ new Map();
1746
- path.traverse({
1747
- // Detect: const XContext = createContext(...)
1748
- VariableDeclarator(declPath) {
1749
- const init = declPath.node.init;
1750
- if (!init || !t12.isCallExpression(init)) return;
1751
- if (!t12.isIdentifier(init.callee, { name: "createContext" })) return;
1752
- if (!t12.isIdentifier(declPath.node.id)) return;
1753
- const contextName = declPath.node.id.name;
1754
- contextNames.set(contextName, contextName);
1755
- const workflow = {
1756
- name: contextName,
1757
- fields: [],
1758
- reducerActions: []
1759
- };
1760
- const typeParams = init.typeParameters;
1761
- if (typeParams && t12.isTSTypeParameterInstantiation(typeParams)) {
1762
- const typeArg = typeParams.params[0];
1763
- if (t12.isTSTypeReference(typeArg) && t12.isIdentifier(typeArg.typeName)) {
1764
- const interfaceName = typeArg.typeName.name;
1765
- extractFieldsFromInterface2(path, interfaceName, workflow);
1766
- }
1767
- }
1768
- if (init.arguments.length > 0 && t12.isObjectExpression(init.arguments[0])) {
1769
- workflow.initialState = extractObjectLiteral2(init.arguments[0]);
1770
- }
1771
- contextWorkflows.push(workflow);
1772
- },
1773
- // Detect useReducer(reducer, initialState) and extract reducer cases
1774
- CallExpression(callPath) {
1775
- if (!t12.isIdentifier(callPath.node.callee, { name: "useReducer" })) return;
1776
- const args = callPath.node.arguments;
1777
- if (args.length < 1) return;
1778
- const reducerArg = args[0];
1779
- if (!t12.isIdentifier(reducerArg)) return;
1780
- const reducerName = reducerArg.name;
1781
- const actions = extractReducerActions(path, reducerName);
1782
- if (contextWorkflows.length > 0) {
1783
- const lastWorkflow = contextWorkflows[contextWorkflows.length - 1];
1784
- lastWorkflow.reducerActions = actions;
1785
- lastWorkflow.reducerName = reducerName;
1786
- }
1787
- }
1788
- });
1789
- if (contextWorkflows.length > 0) {
1790
- if (!compilerState.metadata) compilerState.metadata = {};
1791
- const meta = compilerState.metadata;
1792
- meta.contextWorkflows = contextWorkflows;
1793
- }
1794
- }
1795
- function extractFieldsFromInterface2(path, interfaceName, workflow) {
1796
- path.traverse({
1797
- TSInterfaceDeclaration(ifacePath) {
1798
- if (!t12.isIdentifier(ifacePath.node.id, { name: interfaceName })) return;
1799
- for (const prop of ifacePath.node.body.body) {
1800
- if (!t12.isTSPropertySignature(prop)) continue;
1801
- if (!t12.isIdentifier(prop.key)) continue;
1802
- const field = {
1803
- name: prop.key.name,
1804
- type: extractTSType(prop.typeAnnotation),
1805
- optional: prop.optional || false
1806
- };
1807
- workflow.fields.push(field);
1808
- }
1809
- }
1810
- });
1811
- }
1812
- function extractTSType(annotation) {
1813
- if (!annotation || !t12.isTSTypeAnnotation(annotation)) return "unknown";
1814
- const typeNode = annotation.typeAnnotation;
1815
- if (t12.isTSStringKeyword(typeNode)) return "string";
1816
- if (t12.isTSNumberKeyword(typeNode)) return "number";
1817
- if (t12.isTSBooleanKeyword(typeNode)) return "boolean";
1818
- if (t12.isTSArrayType(typeNode)) return `${extractTSTypeNode(typeNode.elementType)}[]`;
1819
- if (t12.isTSTypeReference(typeNode) && t12.isIdentifier(typeNode.typeName)) return typeNode.typeName.name;
1820
- return "unknown";
1821
- }
1822
- function extractTSTypeNode(node) {
1823
- if (t12.isTSStringKeyword(node)) return "string";
1824
- if (t12.isTSNumberKeyword(node)) return "number";
1825
- if (t12.isTSBooleanKeyword(node)) return "boolean";
1826
- if (t12.isTSTypeReference(node) && t12.isIdentifier(node.typeName)) return node.typeName.name;
1827
- return "unknown";
1828
- }
1829
- function extractReducerActions(path, reducerName) {
1830
- const actions = [];
1831
- path.traverse({
1832
- FunctionDeclaration(funcPath) {
1833
- if (!funcPath.node.id || funcPath.node.id.name !== reducerName) return;
1834
- funcPath.traverse({
1835
- SwitchStatement(switchPath) {
1836
- for (const switchCase of switchPath.node.cases) {
1837
- if (!switchCase.test) continue;
1838
- if (t12.isStringLiteral(switchCase.test)) {
1839
- actions.push({
1840
- type: switchCase.test.value
1841
- });
1842
- }
1843
- }
1844
- }
1845
- });
1846
- }
1847
- });
1848
- return actions;
1849
- }
1850
- function extractObjectLiteral2(obj) {
1851
- const result = {};
1852
- for (const prop of obj.properties) {
1853
- if (!t12.isObjectProperty(prop) || !t12.isIdentifier(prop.key)) continue;
1854
- if (t12.isStringLiteral(prop.value)) result[prop.key.name] = prop.value.value;
1855
- else if (t12.isNumericLiteral(prop.value)) result[prop.key.name] = prop.value.value;
1856
- else if (t12.isBooleanLiteral(prop.value)) result[prop.key.name] = prop.value.value;
1857
- else if (t12.isNullLiteral(prop.value)) result[prop.key.name] = null;
1858
- else if (t12.isArrayExpression(prop.value)) result[prop.key.name] = [];
1859
- else if (t12.isObjectExpression(prop.value)) result[prop.key.name] = extractObjectLiteral2(prop.value);
1860
- }
1861
- return result;
1862
- }
1863
-
1864
- // src/babel/emitters/pure-form-emitter.ts
1865
- import { normalizeCategory } from "@mindmatrix/player-core";
1866
- var PROP_RULES = {
1867
- Text: {
1868
- config: { text: "value" },
1869
- bindings: { text: "value" }
1870
- },
1871
- Heading: {
1872
- config: { text: "value" },
1873
- bindings: { text: "value" }
1874
- },
1875
- Button: {
1876
- config: { text: "label" },
1877
- bindings: { text: "label", onPress: "onClick" }
1878
- }
1879
- };
1880
- var COMPONENT_RENAMES = {
1881
- Heading: "Text"
1882
- };
1883
- var RENAME_EXTRA_CONFIG = {
1884
- Heading: { variant: "h2" }
1885
- };
1886
- function normalizeViewNode(node, fieldNames) {
1887
- if (!node || typeof node !== "object") return node;
1888
- const originalComponent = node.component;
1889
- if (originalComponent && COMPONENT_RENAMES[originalComponent]) {
1890
- node.component = COMPONENT_RENAMES[originalComponent];
1891
- const extra = RENAME_EXTRA_CONFIG[originalComponent];
1892
- if (extra) {
1893
- node.config = { ...extra, ...node.config || {} };
1894
- }
1895
- }
1896
- const rules = PROP_RULES[originalComponent || ""] || PROP_RULES[node.component || ""];
1897
- if (rules) {
1898
- if (node.config && rules.config) {
1899
- for (const [from, to] of Object.entries(rules.config)) {
1900
- if (from in node.config && !(to in node.config)) {
1901
- node.config[to] = node.config[from];
1902
- delete node.config[from];
1903
- }
1904
- }
1905
- }
1906
- if (node.bindings && rules.bindings) {
1907
- for (const [from, to] of Object.entries(rules.bindings)) {
1908
- if (from in node.bindings && !(to in node.bindings)) {
1909
- node.bindings[to] = node.bindings[from];
1910
- delete node.bindings[from];
1911
- }
1912
- }
1913
- }
1914
- }
1915
- if (node.config && fieldNames.size > 0) {
1916
- for (const [prop, val] of Object.entries(node.config)) {
1917
- if (typeof val !== "string") continue;
1918
- const templateRe = /\{\{(\w+)\}\}|\{(\w+)\}/g;
1919
- let match;
1920
- const refs = [];
1921
- while ((match = templateRe.exec(val)) !== null) {
1922
- const field = match[1] || match[2];
1923
- if (fieldNames.has(field)) {
1924
- refs.push({ full: match[0], field, index: match.index });
1925
- }
1926
- }
1927
- if (refs.length > 0) {
1928
- const parts = [];
1929
- let lastEnd = 0;
1930
- for (const ref of refs) {
1931
- const before = val.slice(lastEnd, ref.index);
1932
- if (before) parts.push(`"${before}"`);
1933
- parts.push(`$instance.state_data.${ref.field}`);
1934
- lastEnd = ref.index + ref.full.length;
1935
- }
1936
- const trailing = val.slice(lastEnd);
1937
- if (trailing) parts.push(`"${trailing}"`);
1938
- const expr = `$fn.concat(${parts.join(", ")})`;
1939
- if (!node.bindings) node.bindings = {};
1940
- node.bindings[prop] = expr;
1941
- delete node.config[prop];
1942
- }
1943
- }
1944
- }
1945
- if (node.bindings) {
1946
- for (const [prop, expr] of Object.entries(node.bindings)) {
1947
- if (typeof expr !== "string") continue;
1948
- if (expr.endsWith(".fire")) {
1949
- const transitionName = expr.slice(0, -5);
1950
- node.bindings[prop] = `$action.transition("${transitionName}")`;
1951
- continue;
1952
- }
1953
- if (fieldNames.size > 0 && expr.startsWith("$instance.")) {
1954
- const field = expr.slice("$instance.".length);
1955
- if (fieldNames.has(field)) {
1956
- node.bindings[prop] = `$instance.state_data.${field}`;
1957
- }
1958
- }
1959
- }
1960
- }
1961
- if (node.children && Array.isArray(node.children)) {
1962
- node.children = node.children.map((child) => normalizeViewNode(child, fieldNames));
1963
- if (node.children.length === 1 && node.children[0].component === "Text") {
1964
- const textChild = node.children[0];
1965
- if (textChild.config?.value && !textChild.bindings) {
1966
- if (!node.config) node.config = {};
1967
- node.config.text = textChild.config.value;
1968
- delete node.children;
1969
- } else if (textChild.bindings?.value && !textChild.config?.value) {
1970
- if (!node.bindings) node.bindings = {};
1971
- node.bindings.text = textChild.bindings.value;
1972
- delete node.children;
1973
- }
1974
- }
1975
- }
1976
- return node;
1977
- }
1978
- function toSnakeCase(str) {
1979
- return str.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "");
1980
- }
1981
- function convertAction(action) {
1982
- return {
1983
- id: action.id,
1984
- action_type: action.type,
1985
- config: action.config || {},
1986
- ...action.condition && { condition: action.condition }
1987
- };
1988
- }
1989
- function convertDuringAction(during) {
1990
- return {
1991
- id: during.id,
1992
- type: during.type,
1993
- ...during.interval_ms !== void 0 && { interval_ms: during.interval_ms },
1994
- ...during.cron && { cron: during.cron },
1995
- ...during.delay_ms !== void 0 && { delay_ms: during.delay_ms },
1996
- actions: during.actions.map(convertAction),
1997
- ...during.condition && { condition: during.condition }
1998
- };
1999
- }
2000
- function convertOnEvent(sub) {
2001
- return {
2002
- match: sub.match,
2003
- ...sub.description && { description: sub.description },
2004
- conditions: sub.conditions || [],
2005
- actions: sub.actions.map((a) => ({
2006
- type: a.type,
2007
- ...a.field && { field: a.field },
2008
- ...a.expression && { expression: a.expression },
2009
- ...a.key && { key: a.key },
2010
- ...a.message && { message: a.message },
2011
- ...a.config && { config: a.config },
2012
- ...a.conditions && a.conditions.length > 0 && { conditions: a.conditions }
2013
- }))
2014
- };
2015
- }
2016
- function emitIR(extracted) {
2017
- const { slug, name, version, description, category, fields, states, transitions } = extracted;
2018
- const stateArray = Array.from(states.values());
2019
- if (stateArray.length === 0) {
2020
- stateArray.push({
2021
- name: "draft",
2022
- type: "START",
2023
- on_enter: [],
2024
- during: [],
2025
- on_exit: []
2026
- });
2027
- } else {
2028
- const hasStart = stateArray.some((s) => s.type === "START");
2029
- if (!hasStart && stateArray.length > 0) {
2030
- stateArray[0].type = "START";
2031
- }
2032
- }
2033
- inferTransitionStates(transitions, states);
2034
- const stateNames = new Set(stateArray.map((s) => s.name));
2035
- for (const transition of transitions) {
2036
- if (transition.to && !stateNames.has(transition.to)) {
2037
- stateArray.push({
2038
- name: transition.to,
2039
- type: "REGULAR",
2040
- on_enter: [],
2041
- during: [],
2042
- on_exit: []
2043
- });
2044
- stateNames.add(transition.to);
2045
- }
2046
- }
2047
- const fieldNames = new Set(fields.map((f) => f.name));
2048
- let normalizedView;
2049
- if (extracted.experience) {
2050
- normalizedView = normalizeViewNode(
2051
- JSON.parse(JSON.stringify(extracted.experience)),
2052
- fieldNames
2053
- );
2054
- }
2055
- const metadata = {
2056
- stable_id: `def-${slug}`,
2057
- provenance: {
2058
- frontend: "react-compiler",
2059
- source: `${slug}.workflow.tsx`,
2060
- compiler_version: "1.0.0"
2061
- }
2062
- };
2063
- if (extracted.fieldWatchers && extracted.fieldWatchers.length > 0) {
2064
- metadata.fieldWatchers = extracted.fieldWatchers;
2065
- }
2066
- if (extracted.transitionEffects && extracted.transitionEffects.length > 0) {
2067
- metadata.transitionEffects = extracted.transitionEffects;
2068
- }
2069
- if (extracted.extraMetadata) {
2070
- for (const [key, value] of Object.entries(extracted.extraMetadata)) {
2071
- metadata[key] = value;
2072
- }
2073
- }
2074
- if (extracted.experience) {
2075
- metadata.experience = extracted.experience;
2076
- }
2077
- const ir = {
2078
- slug,
2079
- name,
2080
- version,
2081
- description,
2082
- category,
2083
- fields,
2084
- states: stateArray,
2085
- transitions,
2086
- roles: [],
2087
- tags: [],
2088
- metadata
2089
- };
2090
- if (normalizedView) {
2091
- ir.views = { default: normalizedView };
2092
- }
2093
- if (extracted.events && extracted.events.length > 0) {
2094
- ir.on_event = extracted.events;
2095
- }
2096
- if (extracted.grammarIslands && extracted.grammarIslands.length > 0) {
2097
- const extensions = {};
2098
- for (const island of extracted.grammarIslands) {
2099
- const key = island.contextTag;
2100
- if (!extensions[key]) extensions[key] = [];
2101
- extensions[key].push(island);
2102
- }
2103
- ir.extensions = extensions;
2104
- }
2105
- return ir;
2106
- }
2107
- var emitPureForm = emitIR;
2108
- function liftAction(action) {
2109
- const parts = [];
2110
- if (action.config) {
2111
- for (const [key, value] of Object.entries(action.config)) {
2112
- parts.push({
2113
- slug: key,
2114
- category: ["atom"],
2115
- parts: [{ slug: String(value), category: ["atom"] }]
2116
- });
2117
- }
2118
- }
2119
- if (action.condition) {
2120
- parts.push({
2121
- slug: "condition",
2122
- category: ["expression", "guard"],
2123
- parts: [{ slug: action.condition, category: ["binding"] }]
2124
- });
2125
- }
2126
- return {
2127
- slug: action.id,
2128
- category: normalizeCategory("expression", "mutation", action.type),
2129
- parts: parts.length > 0 ? parts : void 0
2130
- };
2131
- }
2132
- function liftSchedule(during) {
2133
- const parts = [];
2134
- if (during.interval_ms !== void 0) {
2135
- parts.push({
2136
- slug: "every",
2137
- category: ["atom"],
2138
- parts: [{ slug: String(during.interval_ms), category: ["literal"] }]
2139
- });
2140
- }
2141
- if (during.cron) {
2142
- parts.push({
2143
- slug: "cron",
2144
- category: ["atom"],
2145
- parts: [{ slug: during.cron, category: ["literal"] }]
2146
- });
2147
- }
2148
- if (during.delay_ms !== void 0) {
2149
- parts.push({
2150
- slug: "delay",
2151
- category: ["atom"],
2152
- parts: [{ slug: String(during.delay_ms), category: ["literal"] }]
2153
- });
2154
- }
2155
- for (const action of during.actions) {
2156
- parts.push({
2157
- slug: "do",
2158
- category: normalizeCategory("expression", "mutation", action.type),
2159
- parts: Object.entries(action.config || {}).map(([key, value]) => ({
2160
- slug: key,
2161
- category: ["atom"],
2162
- parts: [{ slug: String(value), category: ["atom"] }]
2163
- }))
2164
- });
2165
- }
2166
- return {
2167
- slug: during.id,
2168
- category: normalizeCategory("schedule", during.type),
2169
- parts
2170
- };
2171
- }
2172
- function liftState(state) {
2173
- const parts = [];
2174
- const tags = [];
2175
- if (state.type === "START") tags.push("start");
2176
- if (state.type === "END") tags.push("end");
2177
- if (state.type === "CANCELLED") tags.push("cancelled");
2178
- if (state.on_enter && state.on_enter.length > 0) {
2179
- parts.push({
2180
- slug: "on_enter",
2181
- category: normalizeCategory("expression", "sequence"),
2182
- parts: state.on_enter.map(liftAction)
2183
- });
2184
- }
2185
- if (state.on_exit && state.on_exit.length > 0) {
2186
- parts.push({
2187
- slug: "on_exit",
2188
- category: normalizeCategory("expression", "sequence"),
2189
- parts: state.on_exit.map(liftAction)
2190
- });
2191
- }
2192
- if (state.during && state.during.length > 0) {
2193
- for (const d of state.during) {
2194
- parts.push(liftSchedule(d));
2195
- }
2196
- }
2197
- return {
2198
- slug: state.name,
2199
- category: normalizeCategory("state", ...tags),
2200
- parts: parts.length > 0 ? parts : void 0
2201
- };
2202
- }
2203
- function liftTransition(transition) {
2204
- const parts = [];
2205
- if (transition.from && transition.from.length > 0) {
2206
- parts.push({
2207
- slug: "from",
2208
- category: ["part"],
2209
- parts: transition.from.map((s) => ({ slug: s, category: ["ref"] }))
2210
- });
2211
- }
2212
- if (transition.to) {
2213
- parts.push({
2214
- slug: "to",
2215
- category: ["part"],
2216
- parts: [{ slug: transition.to, category: ["ref"] }]
2217
- });
2218
- }
2219
- if (transition.actions && transition.actions.length > 0) {
2220
- parts.push({
2221
- slug: "actions",
2222
- category: normalizeCategory("expression", "sequence"),
2223
- parts: transition.actions.map(liftAction)
2224
- });
2225
- }
2226
- if (transition.conditions && transition.conditions.length > 0) {
2227
- parts.push({
2228
- slug: "conditions",
2229
- category: normalizeCategory("expression", "guard"),
2230
- parts: transition.conditions.map((c, i) => ({
2231
- slug: `condition_${i}`,
2232
- category: normalizeCategory("expression", c.type || "condition"),
2233
- parts: c.expression ? [{ slug: c.expression, category: ["binding"] }] : void 0
2234
- }))
2235
- });
2236
- }
2237
- return {
2238
- slug: transition.name,
2239
- category: ["transition"],
2240
- parts
2241
- };
2242
- }
2243
- function liftField(field) {
2244
- const parts = [];
2245
- if (field.label) {
2246
- parts.push({
2247
- slug: "label",
2248
- category: ["atom"],
2249
- parts: [{ slug: field.label, category: ["atom"] }]
2250
- });
2251
- }
2252
- if (field.default_value !== void 0) {
2253
- parts.push({
2254
- slug: "default",
2255
- category: ["atom"],
2256
- parts: [{ slug: String(field.default_value), category: ["literal"] }]
2257
- });
2258
- }
2259
- if (field.required) {
2260
- parts.push({ slug: "required", category: ["atom"], parts: [{ slug: "true", category: ["literal"] }] });
2261
- }
2262
- if (field.computed) {
2263
- parts.push({
2264
- slug: "computed",
2265
- category: ["expression"],
2266
- parts: [{ slug: field.computed, category: ["binding"] }]
2267
- });
2268
- }
2269
- return {
2270
- slug: field.name,
2271
- category: normalizeCategory("field", field.type),
2272
- parts: parts.length > 0 ? parts : void 0
2273
- };
2274
- }
2275
- function liftView(node) {
2276
- const componentTag = (node.component || "stack").toLowerCase().replace(/\s+/g, "-");
2277
- const parts = [];
2278
- if (node.config) {
2279
- for (const [key, value] of Object.entries(node.config)) {
2280
- parts.push({
2281
- slug: key,
2282
- category: ["atom"],
2283
- parts: [{ slug: String(value), category: ["atom"] }]
2284
- });
2285
- }
2286
- }
2287
- if (node.bindings) {
2288
- for (const [key, expr] of Object.entries(node.bindings)) {
2289
- if (key === "onClick" || key === "onPress") {
2290
- const match = typeof expr === "string" ? expr.match(/\$action\.transition\("(.+?)"\)/) : null;
2291
- if (match) {
2292
- parts.push({
2293
- slug: key,
2294
- category: normalizeCategory("expression", "effect", "transition"),
2295
- parts: [{ slug: match[1], category: ["ref"] }]
2296
- });
2297
- } else {
2298
- parts.push({
2299
- slug: key,
2300
- category: normalizeCategory("expression", "binding"),
2301
- parts: [{ slug: String(expr), category: ["binding"] }]
2302
- });
2303
- }
2304
- } else {
2305
- parts.push({
2306
- slug: key,
2307
- category: normalizeCategory("expression", "binding"),
2308
- parts: [{ slug: String(expr), category: ["binding"] }]
2309
- });
2310
- }
2311
- }
2312
- }
2313
- if (node.visible_when) {
2314
- const eqMatch = node.visible_when.match(/\$fn\.eq\((.+?),\s*"(.+?)"\)/);
2315
- if (eqMatch) {
2316
- parts.push({
2317
- slug: "visible_when",
2318
- category: normalizeCategory("expression", "guard"),
2319
- parts: [{
2320
- slug: "body",
2321
- category: normalizeCategory("expression", "eq"),
2322
- parts: [
2323
- { slug: "lhs", category: normalizeCategory("expression", "path"), parts: [{ slug: eqMatch[1], category: ["binding"] }] },
2324
- { slug: "rhs", category: normalizeCategory("expression", "literal"), parts: [{ slug: eqMatch[2], category: ["literal"] }] }
2325
- ]
2326
- }]
2327
- });
2328
- } else {
2329
- parts.push({
2330
- slug: "visible_when",
2331
- category: normalizeCategory("expression", "guard"),
2332
- parts: [{ slug: node.visible_when, category: ["binding"] }]
2333
- });
2334
- }
2335
- }
2336
- if (node.children && node.children.length > 0) {
2337
- for (const child of node.children) {
2338
- parts.push(liftView(child));
2339
- }
2340
- }
2341
- return {
2342
- slug: node.id,
2343
- category: normalizeCategory("view", componentTag),
2344
- parts: parts.length > 0 ? parts : void 0
2345
- };
2346
- }
2347
- function emitCanonical(extracted, sourceFilename) {
2348
- const ir = emitIR(extracted);
2349
- const parts = [];
2350
- for (const state of ir.states) {
2351
- parts.push(liftState(state));
2352
- }
2353
- for (const transition of ir.transitions) {
2354
- parts.push(liftTransition(transition));
2355
- }
2356
- for (const field of ir.fields) {
2357
- parts.push(liftField(field));
2358
- }
2359
- if (extracted.experience) {
2360
- parts.push(liftView(extracted.experience));
2361
- }
2362
- parts.push({
2363
- slug: "manifest",
2364
- category: normalizeCategory("meta", "manifest"),
2365
- parts: [
2366
- {
2367
- slug: "workflows",
2368
- category: ["ref-list"],
2369
- parts: [{ slug: ir.slug, category: ["ref"] }]
2370
- },
2371
- {
2372
- slug: "route",
2373
- category: ["route"],
2374
- parts: [
2375
- { slug: "path", category: ["atom"], parts: [{ slug: `/${ir.slug}`, category: ["atom"] }] },
2376
- { slug: "label", category: ["atom"], parts: [{ slug: ir.name, category: ["atom"] }] }
2377
- ]
2378
- }
2379
- ]
2380
- });
2381
- const category = extracted.category;
2382
- const categoryArray = category === "workflow" ? ["workflow"] : normalizeCategory("workflow", category);
2383
- return {
2384
- slug: ir.slug,
2385
- category: categoryArray,
2386
- parts,
2387
- metadata: {
2388
- stable_id: `def-${ir.slug}`,
2389
- provenance: {
2390
- frontend: "react-compiler",
2391
- source: sourceFilename || `${ir.slug}.workflow.tsx`,
2392
- compiler_version: "1.0.0",
2393
- compiled_at: (/* @__PURE__ */ new Date()).toISOString()
2394
- }
2395
- }
2396
- };
2397
- }
2398
- function emitCompiledOutput(extracted, sourceFilename) {
2399
- return {
2400
- canonical: emitCanonical(extracted, sourceFilename),
2401
- ir: emitIR(extracted)
2402
- };
2403
- }
2404
- function foldTextContent(node) {
2405
- if (!node || typeof node !== "object") return node;
2406
- if (node.children && Array.isArray(node.children)) {
2407
- node.children = node.children.map((child) => foldTextContent(child));
2408
- if (node.children.length === 1 && node.children[0].component === "Text") {
2409
- const textChild = node.children[0];
2410
- if (textChild.config?.value && !textChild.bindings) {
2411
- if (!node.config) node.config = {};
2412
- node.config.text = textChild.config.value;
2413
- node.children = void 0;
2414
- } else if (textChild.bindings?.value && !textChild.config?.value) {
2415
- if (!node.bindings) node.bindings = {};
2416
- node.bindings.text = textChild.bindings.value;
2417
- node.children = void 0;
2418
- }
2419
- }
2420
- if (node.children && node.children.length === 0) {
2421
- node.children = void 0;
2422
- }
2423
- }
2424
- if (!node.children) delete node.children;
2425
- return node;
2426
- }
2427
- function emitWorkflowDefinition(extracted) {
2428
- const ir = emitIR(extracted);
2429
- const states = ir.states.map((s) => ({
2430
- name: s.name,
2431
- state_type: s.type,
2432
- description: s.description || "",
2433
- on_enter: (s.on_enter || []).map(convertAction),
2434
- on_exit: (s.on_exit || []).map(convertAction),
2435
- during: (s.during || []).map(convertDuringAction),
2436
- on_event: (s.on_event || []).map(convertOnEvent)
2437
- }));
2438
- const fields = ir.fields.map((f) => ({
2439
- name: toSnakeCase(f.name),
2440
- field_type: f.type,
2441
- label: f.label || f.name.replace(/([A-Z])/g, " $1").replace(/^./, (c) => c.toUpperCase()),
2442
- required: f.required || false,
2443
- default_value: f.default_value ?? null,
2444
- ...f.validation && { validation: f.validation },
2445
- ...f.computed && { computed: f.computed },
2446
- ...f.computed_deps && { computed_deps: f.computed_deps },
2447
- ...f.visible_in_states && { visible_in_states: f.visible_in_states },
2448
- ...f.editable_in_states && { editable_in_states: f.editable_in_states },
2449
- ...f.visible_to_roles && { visible_to_roles: f.visible_to_roles },
2450
- ...f.editable_by_roles && { editable_by_roles: f.editable_by_roles },
2451
- ...f.visible_when && { visible_when: f.visible_when },
2452
- ...f.editable_when && { editable_when: f.editable_when },
2453
- ...f.state_home && { state_home: f.state_home }
2454
- }));
2455
- const transitions = ir.transitions.map((t14) => ({
2456
- name: t14.name,
2457
- from: t14.from,
2458
- to: t14.to,
2459
- description: t14.description || "",
2460
- roles: t14.roles || [],
2461
- auto: t14.auto || false,
2462
- conditions: t14.conditions || [],
2463
- actions: (t14.actions || []).map(convertAction),
2464
- required_fields: t14.required_fields || [],
2465
- priority: 0
2466
- }));
2467
- const state_data = {};
2468
- for (const f of fields) {
2469
- state_data[f.name] = f.default_value;
2470
- }
2471
- const on_event = (ir.on_event || []).map(convertOnEvent);
2472
- let views;
2473
- if (extracted.experience) {
2474
- views = { default: foldTextContent(JSON.parse(JSON.stringify(extracted.experience))) };
2475
- }
2476
- return {
2477
- id: `def-${ir.slug}`,
2478
- slug: ir.slug,
2479
- name: ir.name,
2480
- version: ir.version,
2481
- description: ir.description || "",
2482
- category: ir.category,
2483
- states,
2484
- transitions,
2485
- fields,
2486
- state_data,
2487
- roles: [],
2488
- child_definitions: [],
2489
- tags: ir.tags || [],
2490
- metadata: ir.metadata || {},
2491
- ...views && { views },
2492
- ...on_event.length > 0 && { on_event },
2493
- config: {},
2494
- is_immutable: false
2495
- };
2496
- }
2497
- function compilerStateToWorkflow(state, metadata) {
2498
- const fieldWatchers = metadata.fieldWatchers || [];
2499
- const transitionEffects = metadata.transitionEffects || [];
2500
- const grammarIslands = metadata.grammarIslands || [];
2501
- const knownKeys = /* @__PURE__ */ new Set(["slug", "name", "version", "description", "category", "fieldWatchers", "transitionEffects", "grammarIslands"]);
2502
- const extraMetadata = {};
2503
- for (const [key, value] of Object.entries(metadata)) {
2504
- if (!knownKeys.has(key) && value !== void 0) {
2505
- extraMetadata[key] = value;
2506
- }
2507
- }
2508
- return {
2509
- slug: metadata.slug || "workflow",
2510
- name: metadata.name || "Workflow",
2511
- version: metadata.version || "0.1.0",
2512
- description: metadata.description,
2513
- category: metadata.category || "workflow",
2514
- fields: state.fields || [],
2515
- states: state.states || /* @__PURE__ */ new Map(),
2516
- transitions: state.transitions || [],
2517
- events: state.events || [],
2518
- experience: state.experience,
2519
- fieldWatchers: fieldWatchers.length > 0 ? fieldWatchers : void 0,
2520
- transitionEffects: transitionEffects.length > 0 ? transitionEffects : void 0,
2521
- grammarIslands: grammarIslands.length > 0 ? grammarIslands : void 0,
2522
- errors: state.errors,
2523
- warnings: state.warnings,
2524
- extraMetadata: Object.keys(extraMetadata).length > 0 ? extraMetadata : void 0
2525
- };
2526
- }
2527
-
2528
- // src/babel/visitor.ts
2529
- function extractQueryDataSource(path, state) {
2530
- const args = path.node.arguments;
2531
- if (args.length < 1) return;
2532
- const slugArg = args[0];
2533
- if (!t13.isStringLiteral(slugArg)) return;
2534
- const slug = slugArg.value;
2535
- const compilerState = state;
2536
- const dataSource = {
2537
- type: "workflow",
2538
- name: slug,
2539
- slug,
2540
- query: "list"
2541
- };
2542
- if (args.length > 1 && t13.isObjectExpression(args[1])) {
2543
- for (const prop of args[1].properties) {
2544
- if (!t13.isObjectProperty(prop) || !t13.isIdentifier(prop.key)) continue;
2545
- const key = prop.key.name;
2546
- if (key === "limit" && t13.isNumericLiteral(prop.value)) {
2547
- dataSource.pageSize = prop.value.value;
2548
- dataSource.paginated = true;
2549
- }
2550
- if (key === "search" && t13.isStringLiteral(prop.value)) {
2551
- dataSource.searchFields = [prop.value.value];
2552
- }
2553
- if (key === "orderBy" && t13.isStringLiteral(prop.value)) {
2554
- dataSource.sort = prop.value.value;
2555
- }
2556
- }
2557
- }
2558
- if (!compilerState.metadata) compilerState.metadata = {};
2559
- const meta = compilerState.metadata;
2560
- if (!meta.dataSources) meta.dataSources = [];
2561
- meta.dataSources.push(dataSource);
2562
- }
2563
- function extractMutationDataSource(path, state) {
2564
- const args = path.node.arguments;
2565
- if (args.length < 1) return;
2566
- const slugArg = args[0];
2567
- if (!t13.isStringLiteral(slugArg)) return;
2568
- const slug = slugArg.value;
2569
- const compilerState = state;
2570
- if (!compilerState.metadata) compilerState.metadata = {};
2571
- const meta = compilerState.metadata;
2572
- if (!meta.mutationTargets) meta.mutationTargets = [];
2573
- meta.mutationTargets.push(slug);
2574
- }
2575
- var STRICT_BANNED_HOOKS = {
2576
- useEffect: "STRICT_USE_EFFECT",
2577
- useLayoutEffect: "STRICT_USE_LAYOUT_EFFECT",
2578
- useRef: "STRICT_USE_REF",
2579
- useMemo: "STRICT_USE_MEMO",
2580
- useCallback: "STRICT_USE_CALLBACK"
2581
- };
2582
- function extractMetadataFromComments(comments, metadata) {
2583
- for (const comment of comments) {
2584
- if (comment.type !== "CommentBlock") continue;
2585
- const lines = comment.value.split("\n");
2586
- for (const line of lines) {
2587
- const workflowMatch = line.match(/@workflow\s+(.+)/);
2588
- if (workflowMatch) {
2589
- const rest = workflowMatch[1].trim();
2590
- const kvRegex = /(\w+)="([^"]+)"/g;
2591
- let kvMatch;
2592
- let hasKV = false;
2593
- while ((kvMatch = kvRegex.exec(rest)) !== null) {
2594
- hasKV = true;
2595
- const [, key, val] = kvMatch;
2596
- if (key === "slug") metadata.slug = val;
2597
- else if (key === "version") metadata.version = val;
2598
- else if (key === "category") metadata.category = val;
2599
- else if (key === "description") metadata.description = val;
2600
- }
2601
- if (!hasKV) {
2602
- metadata.slug = rest;
2603
- }
2604
- continue;
2605
- }
2606
- const match = line.match(/@(\w+)\s+(.+)/);
2607
- if (match) {
2608
- const [, tag, value] = match;
2609
- if (tag === "version") metadata.version = value.trim();
2610
- if (tag === "category") metadata.category = value.trim();
2611
- if (tag === "description") metadata.description = value.trim();
2612
- }
2613
- }
2614
- }
2615
- }
2616
- function createVisitor(options = {}) {
2617
- const mode = options.mode;
2618
- return {
2619
- Program: {
2620
- // Initialize compiler state at program entry
2621
- enter(_path, state) {
2622
- resetNodeIdCounter();
2623
- resetDuringIdCounter();
2624
- resetWatcherIdCounter();
2625
- resetTransitionEffectIdCounter();
2626
- const compilerState = {
2627
- fields: [],
2628
- states: /* @__PURE__ */ new Map(),
2629
- transitions: [],
2630
- events: [],
2631
- actionCounter: 0,
2632
- metadata: {},
2633
- errors: [],
2634
- warnings: []
2635
- };
2636
- const program = _path.node;
2637
- const leadingComments = program.leadingComments || [];
2638
- extractMetadataFromComments(leadingComments, compilerState.metadata);
2639
- if (program.body.length > 0) {
2640
- const firstStmt = program.body[0];
2641
- if (firstStmt.leadingComments) {
2642
- extractMetadataFromComments(firstStmt.leadingComments, compilerState.metadata);
2643
- }
2644
- }
2645
- const exportDeclaration = program.body.find(
2646
- (node) => t13.isExportNamedDeclaration(node) || t13.isExportDefaultDeclaration(node)
2647
- );
2648
- if (exportDeclaration) {
2649
- if (t13.isExportNamedDeclaration(exportDeclaration)) {
2650
- const declaration = exportDeclaration.declaration;
2651
- if (t13.isFunctionDeclaration(declaration)) {
2652
- if (declaration.id) {
2653
- compilerState.metadata.name = declaration.id.name;
2654
- }
2655
- if (declaration.leadingComments) {
2656
- extractMetadataFromComments(declaration.leadingComments, compilerState.metadata);
2657
- }
2658
- }
2659
- } else if (t13.isExportDefaultDeclaration(exportDeclaration)) {
2660
- const declaration = exportDeclaration.declaration;
2661
- if (t13.isFunctionDeclaration(declaration)) {
2662
- if (declaration.id) {
2663
- compilerState.metadata.name = declaration.id.name;
2664
- }
2665
- if (declaration.leadingComments) {
2666
- extractMetadataFromComments(declaration.leadingComments, compilerState.metadata);
2667
- }
2668
- } else if (t13.isIdentifier(declaration)) {
2669
- compilerState.metadata.name = declaration.name;
2670
- }
2671
- }
2672
- }
2673
- if (!compilerState.metadata.slug && state.filename) {
2674
- const fileName = state.filename.split("/").pop() || "";
2675
- compilerState.metadata.slug = fileName.replace(/\.workflow\.(tsx?|jsx?)$/, "").replace(/\.(tsx?|jsx?)$/, "").replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
2676
- compilerState.metadata.__slugAutoFallback = true;
2677
- }
2678
- Object.assign(state, compilerState);
2679
- if (isModelFile(_path, state.filename)) {
2680
- extractModelFile(_path, state);
2681
- state.__isModelFile = true;
2682
- }
2683
- extractGrammarIslands(_path, state);
2684
- if (hasContextCreation(_path)) {
2685
- extractContextWorkflows(_path, state);
2686
- }
2687
- if (isServerActionFile(state.filename)) {
2688
- extractServerActions(_path, state);
2689
- state.__isServerActionFile = true;
2690
- }
2691
- },
2692
- // After all extraction, emit Pure Form JSON
2693
- exit(_path, state) {
2694
- const compilerState = state;
2695
- const extracted = compilerStateToWorkflow(compilerState, compilerState.metadata);
2696
- const ir = emitIR(extracted);
2697
- const definition = emitWorkflowDefinition(extracted);
2698
- const canonical = emitCanonical(extracted, state.filename);
2699
- if (compilerState.errors && compilerState.errors.length > 0) {
2700
- if (!ir.metadata) ir.metadata = {};
2701
- ir.metadata.errors = compilerState.errors;
2702
- }
2703
- if (compilerState.warnings && compilerState.warnings.length > 0) {
2704
- if (!ir.metadata) ir.metadata = {};
2705
- ir.metadata.warnings = compilerState.warnings;
2706
- }
2707
- if (!state.file.metadata) state.file.metadata = {};
2708
- state.file.metadata.mindmatrixIR = ir;
2709
- state.file.metadata.mindmatrixDefinition = definition;
2710
- state.file.metadata.mindmatrixCanonical = canonical;
2711
- }
2712
- },
2713
- // Extract metadata from function declarations with JSDoc
2714
- FunctionDeclaration(path, state) {
2715
- const comments = path.node.leadingComments || [];
2716
- const compilerState = state;
2717
- extractMetadataFromComments(comments, compilerState.metadata);
2718
- },
2719
- // Main hook extraction dispatcher
2720
- CallExpression(path, state) {
2721
- const callee = path.node.callee;
2722
- if (!t13.isIdentifier(callee)) return;
2723
- const compilerState = state;
2724
- const hookName = callee.name;
2725
- if (mode === "strict" && STRICT_BANNED_HOOKS[hookName]) {
2726
- const error = {
2727
- code: STRICT_BANNED_HOOKS[hookName],
2728
- message: `${hookName}() is not allowed in strict mode. Use @mindmatrix/react effect hooks instead.`,
2729
- line: path.node.loc?.start.line,
2730
- column: path.node.loc?.start.column,
2731
- severity: "error"
2732
- };
2733
- if (!compilerState.errors) compilerState.errors = [];
2734
- compilerState.errors.push(error);
2735
- return;
2736
- }
2737
- if (mode === "infer" && hookName === "useEffect") {
2738
- const warning = {
2739
- code: "INFER_RAW_JSX",
2740
- message: `useEffect() found in infer mode \u2014 component will be treated as an opaque [raw jsx] grammar island.`,
2741
- line: path.node.loc?.start.line,
2742
- column: path.node.loc?.start.column,
2743
- severity: "warning"
2744
- };
2745
- if (!compilerState.warnings) compilerState.warnings = [];
2746
- compilerState.warnings.push(warning);
2747
- return;
2748
- }
2749
- switch (hookName) {
2750
- case "useState":
2751
- extractStates(path, state);
2752
- break;
2753
- case "useOnEnter":
2754
- case "useOnExit":
2755
- extractEffects(path, state);
2756
- break;
2757
- case "useTransition":
2758
- extractTransitions(path, state);
2759
- break;
2760
- case "useOnEvent":
2761
- extractEvents(path, state);
2762
- break;
2763
- case "useWhileIn":
2764
- extractDuring(path, state);
2765
- break;
2766
- case "useOnChange":
2767
- extractChangeWatcher(path, state);
2768
- break;
2769
- case "useOnTransition":
2770
- extractTransitionEffect(path, state);
2771
- break;
2772
- case "useQuery":
2773
- extractQueryDataSource(path, state);
2774
- break;
2775
- case "useMutation":
2776
- extractMutationDataSource(path, state);
2777
- break;
2778
- }
2779
- },
2780
- // Strict mode: validate imports
2781
- ImportDeclaration(path, state) {
2782
- if (mode !== "strict") return;
2783
- const compilerState = state;
2784
- const source = path.node.source.value;
2785
- if (source.startsWith("@mindmatrix/") || source === "react" || source.startsWith("react/") || source.startsWith(".") || source.startsWith("/")) {
2786
- return;
2787
- }
2788
- const error = {
2789
- code: "STRICT_FORBIDDEN_IMPORT",
2790
- message: `Import from '${source}' is not allowed in strict mode. Only @mindmatrix/* and relative imports are permitted.`,
2791
- line: path.node.loc?.start.line,
2792
- column: path.node.loc?.start.column,
2793
- severity: "error"
2794
- };
2795
- if (!compilerState.errors) compilerState.errors = [];
2796
- compilerState.errors.push(error);
2797
- },
2798
- // Extract JSX from function component return
2799
- ReturnStatement(path, state) {
2800
- if (t13.isJSXElement(path.node.argument) || t13.isJSXFragment(path.node.argument)) {
2801
- extractComponents(path, state);
2802
- }
2803
- }
2804
- };
2805
- }
2806
-
2807
- // src/babel/index.ts
2808
- function babelPlugin(api, options = {}) {
2809
- api.assertVersion(7);
2810
- return {
2811
- name: "mindmatrix-react",
2812
- visitor: createVisitor(options)
2813
- };
2814
- }
2815
-
2816
- export {
2817
- extractStates,
2818
- extractEffects,
2819
- extractTransitions,
2820
- extractEvents,
2821
- extractComponents,
2822
- emitIR,
2823
- emitPureForm,
2824
- emitCanonical,
2825
- emitCompiledOutput,
2826
- compilerStateToWorkflow,
2827
- createVisitor,
2828
- babelPlugin
2829
- };