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

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 (265) hide show
  1. package/ATOM-PIPELINE.md +144 -0
  2. package/README.md +88 -40
  3. package/dist/babel/index.js +2814 -277
  4. package/dist/babel/index.mjs +2 -2
  5. package/dist/chunk-3USIFFE4.mjs +2190 -0
  6. package/dist/chunk-45YMGEVT.mjs +186 -0
  7. package/dist/chunk-4FN2AISW.mjs +148 -0
  8. package/dist/chunk-4OPI5L7G.mjs +2593 -0
  9. package/dist/chunk-4RYTKOOJ.mjs +186 -0
  10. package/dist/chunk-52XHYD2V.mjs +214 -0
  11. package/dist/chunk-5GUFFFGL.mjs +148 -0
  12. package/dist/chunk-5RKTOVR5.mjs +244 -0
  13. package/dist/chunk-5YDMOO4X.mjs +214 -0
  14. package/dist/chunk-64ZWEMLJ.mjs +148 -0
  15. package/dist/chunk-6XP4KSWQ.mjs +2190 -0
  16. package/dist/chunk-72QWL54I.mjs +175 -0
  17. package/dist/chunk-7B4TRI7C.mjs +4835 -0
  18. package/dist/chunk-7ZKGHTNB.mjs +4952 -0
  19. package/dist/chunk-CIESM3BP.mjs +33 -0
  20. package/dist/chunk-DE3ZGQAC.mjs +148 -0
  21. package/dist/chunk-DMCY3BBG.mjs +1933 -0
  22. package/dist/chunk-DPIK3PJS.mjs +244 -0
  23. package/dist/chunk-E5IVH4RE.mjs +186 -0
  24. package/dist/chunk-E6FZNUR5.mjs +4953 -0
  25. package/dist/chunk-EJRBDQDP.mjs +2607 -0
  26. package/dist/chunk-ELO4TXJL.mjs +186 -0
  27. package/dist/chunk-EO6SYNCG.mjs +175 -0
  28. package/dist/chunk-FKRO52XH.mjs +3446 -0
  29. package/dist/chunk-FL4YAKU6.mjs +4941 -0
  30. package/dist/chunk-FYT47UBU.mjs +5076 -0
  31. package/dist/chunk-GCLGPOJZ.mjs +148 -0
  32. package/dist/chunk-GXB4JOP7.mjs +5072 -0
  33. package/dist/chunk-HFXOUMTD.mjs +175 -0
  34. package/dist/chunk-HWIZ47US.mjs +214 -0
  35. package/dist/chunk-IB7MNPQL.mjs +4953 -0
  36. package/dist/chunk-ICSIHQCG.mjs +148 -0
  37. package/dist/chunk-J7JUAHS4.mjs +186 -0
  38. package/dist/chunk-JLA5VNQ3.mjs +186 -0
  39. package/dist/chunk-JQLWFCTM.mjs +214 -0
  40. package/dist/chunk-KFJJCQAL.mjs +148 -0
  41. package/dist/chunk-KJUIIEQE.mjs +186 -0
  42. package/dist/chunk-KNWTHRVQ.mjs +175 -0
  43. package/dist/chunk-KSG4XSZF.mjs +175 -0
  44. package/dist/chunk-LF5N6DOU.mjs +175 -0
  45. package/dist/chunk-LJQCM2IM.mjs +214 -0
  46. package/dist/chunk-NTB7OEX2.mjs +2918 -0
  47. package/dist/chunk-NW6555WJ.mjs +186 -0
  48. package/dist/chunk-OMZE6VLQ.mjs +214 -0
  49. package/dist/chunk-OPJKP747.mjs +7506 -0
  50. package/dist/chunk-P4BR7WVO.mjs +2190 -0
  51. package/dist/chunk-QQHVYH2X.mjs +244 -0
  52. package/dist/chunk-S5QLWLLT.mjs +186 -0
  53. package/dist/chunk-SCWGT2FY.mjs +2190 -0
  54. package/dist/chunk-SMKJUSB3.mjs +2190 -0
  55. package/dist/chunk-THFYE5ZX.mjs +244 -0
  56. package/dist/chunk-VCAY2KGM.mjs +175 -0
  57. package/dist/chunk-WBYMW4NQ.mjs +3450 -0
  58. package/dist/chunk-WECAV6QB.mjs +148 -0
  59. package/dist/chunk-WMKBXUCE.mjs +3228 -0
  60. package/dist/chunk-XAJ5BKKL.mjs +4947 -0
  61. package/dist/chunk-XG2X7AEA.mjs +175 -0
  62. package/dist/chunk-XG7Z23NQ.mjs +148 -0
  63. package/dist/chunk-XWZAOCQ7.mjs +2607 -0
  64. package/dist/chunk-Y6MA7ULW.mjs +148 -0
  65. package/dist/chunk-YMS7Q7LG.mjs +214 -0
  66. package/dist/chunk-ZA37XTGA.mjs +175 -0
  67. package/dist/cli/index.js +13189 -6838
  68. package/dist/cli/index.mjs +140 -22
  69. package/dist/codemod/cli.mjs +1 -1
  70. package/dist/codemod/index.mjs +1 -1
  71. package/dist/config-PL24KEWL.mjs +219 -0
  72. package/dist/dev-server-RmGHIntF.d.mts +113 -0
  73. package/dist/dev-server-RmGHIntF.d.ts +113 -0
  74. package/dist/dev-server.d.mts +1 -1
  75. package/dist/dev-server.d.ts +1 -1
  76. package/dist/dev-server.js +4135 -440
  77. package/dist/dev-server.mjs +5 -5
  78. package/dist/envelope.js +2812 -275
  79. package/dist/envelope.mjs +3 -3
  80. package/dist/index.d.mts +161 -2
  81. package/dist/index.d.ts +161 -2
  82. package/dist/index.js +4429 -428
  83. package/dist/index.mjs +217 -9
  84. package/{src/cli/init.ts → dist/init-7JQMAAXS.mjs} +70 -95
  85. package/dist/init-DQDX3QK6.mjs +369 -0
  86. package/dist/init-EHO4VQ22.mjs +369 -0
  87. package/dist/init-UC3FWPIW.mjs +367 -0
  88. package/dist/init-UNSMVKIK.mjs +366 -0
  89. package/dist/init-UNV5XIDE.mjs +367 -0
  90. package/dist/project-compiler-2P4N4DR7.mjs +10 -0
  91. package/dist/project-compiler-D2LCC27O.mjs +10 -0
  92. package/dist/project-compiler-EJ3GANJE.mjs +10 -0
  93. package/dist/project-compiler-LOQKVRZJ.mjs +10 -0
  94. package/dist/project-compiler-OP2VVGJQ.mjs +10 -0
  95. package/dist/project-compiler-RQ6OQKRM.mjs +10 -0
  96. package/dist/project-compiler-VWNNCHGO.mjs +10 -0
  97. package/dist/project-compiler-XVAAU4C5.mjs +10 -0
  98. package/dist/project-compiler-YES5FGMD.mjs +10 -0
  99. package/dist/project-compiler-ZKMQDLGU.mjs +10 -0
  100. package/dist/project-decompiler-FLXCEJHS.mjs +7 -0
  101. package/dist/project-decompiler-US7GAVIC.mjs +7 -0
  102. package/dist/project-decompiler-VLPR22QF.mjs +7 -0
  103. package/dist/pull-FUS5QYZS.mjs +109 -0
  104. package/dist/pull-LD5ENLGY.mjs +109 -0
  105. package/dist/pull-P44LDRWB.mjs +109 -0
  106. package/dist/testing/index.js +2822 -285
  107. package/dist/testing/index.mjs +2 -2
  108. package/dist/verify-SEIXUGN4.mjs +1833 -0
  109. package/dist/vite/index.js +2815 -278
  110. package/dist/vite/index.mjs +3 -3
  111. package/examples/uber-app/app/admin/fleet.tsx +19 -19
  112. package/package.json +16 -6
  113. package/compile-blueprint-chat.mjs +0 -99
  114. package/compile-blueprint-glass-console.mjs +0 -98
  115. package/compile-chat-defs.mjs +0 -92
  116. package/examples/uber-app/tests/payment.test.tsx +0 -129
  117. package/examples/uber-app/tests/ride-flow.test.tsx +0 -123
  118. package/package.json.backup +0 -86
  119. package/scripts/decompile.ts +0 -226
  120. package/scripts/seed-auth.ts +0 -267
  121. package/scripts/seed-uber.ts +0 -248
  122. package/scripts/validate-uber.ts +0 -119
  123. package/seed-blueprint-chat.mjs +0 -444
  124. package/seed-blueprint-glass-console.mjs +0 -445
  125. package/seed-compiled.mjs +0 -318
  126. package/src/RoundTripValidator.ts +0 -400
  127. package/src/__tests__/atom-rendering-coverage.test.ts +0 -680
  128. package/src/__tests__/auth-module-compilation.test.ts +0 -247
  129. package/src/__tests__/auth-template-compilation.test.ts +0 -589
  130. package/src/__tests__/change-extractor.test.ts +0 -142
  131. package/src/__tests__/cli-pull.test.ts +0 -73
  132. package/src/__tests__/cli-test.test.ts +0 -72
  133. package/src/__tests__/component-extractor.test.ts +0 -331
  134. package/src/__tests__/context-extractor.test.ts +0 -145
  135. package/src/__tests__/decompiler.test.ts +0 -718
  136. package/src/__tests__/define-blueprint.test.ts +0 -133
  137. package/src/__tests__/definition-validator.test.ts +0 -519
  138. package/src/__tests__/during-extractor.test.ts +0 -152
  139. package/src/__tests__/effect-extractor.test.ts +0 -107
  140. package/src/__tests__/event-emission.test.ts +0 -127
  141. package/src/__tests__/examples.test.ts +0 -236
  142. package/src/__tests__/full-blueprint-coverage.test.ts +0 -1221
  143. package/src/__tests__/golden-suite.test.ts +0 -403
  144. package/src/__tests__/grammar-island-extractor.test.ts +0 -289
  145. package/src/__tests__/instance-key.test.ts +0 -82
  146. package/src/__tests__/ir-migration.test.ts +0 -255
  147. package/src/__tests__/lock-file.test.ts +0 -117
  148. package/src/__tests__/model-extractor.test.ts +0 -195
  149. package/src/__tests__/model-field-acl.test.ts +0 -237
  150. package/src/__tests__/model-hooks.test.ts +0 -130
  151. package/src/__tests__/model-ref-resolution.test.ts +0 -268
  152. package/src/__tests__/model-roundtrip.test.ts +0 -502
  153. package/src/__tests__/model-runtime.test.ts +0 -112
  154. package/src/__tests__/model-transitions.test.ts +0 -183
  155. package/src/__tests__/nrt-action-trace.test.ts +0 -391
  156. package/src/__tests__/pipeline-hardening.test.ts +0 -413
  157. package/src/__tests__/project-compiler.test.ts +0 -546
  158. package/src/__tests__/project-decompiler.test.ts +0 -343
  159. package/src/__tests__/query-compilation.test.ts +0 -145
  160. package/src/__tests__/round-trip/PLAN.md +0 -158
  161. package/src/__tests__/round-trip/README.md +0 -52
  162. package/src/__tests__/round-trip/RESULTS.md +0 -86
  163. package/src/__tests__/round-trip/fixtures/data-heavy/main.workflow.tsx +0 -55
  164. package/src/__tests__/round-trip/fixtures/data-heavy/mm.config.ts +0 -11
  165. package/src/__tests__/round-trip/fixtures/data-heavy/models/contact.ts +0 -54
  166. package/src/__tests__/round-trip/fixtures/full-workflow/main.workflow.tsx +0 -79
  167. package/src/__tests__/round-trip/fixtures/full-workflow/mm.config.ts +0 -12
  168. package/src/__tests__/round-trip/fixtures/full-workflow/models/order.ts +0 -50
  169. package/src/__tests__/round-trip/fixtures/simple-crud/main.workflow.tsx +0 -25
  170. package/src/__tests__/round-trip/fixtures/simple-crud/mm.config.ts +0 -11
  171. package/src/__tests__/round-trip/fixtures/simple-crud/models/task.ts +0 -32
  172. package/src/__tests__/round-trip/fixtures/view-heavy/main.workflow.tsx +0 -79
  173. package/src/__tests__/round-trip/fixtures/view-heavy/mm.config.ts +0 -10
  174. package/src/__tests__/round-trip/round-trip.test.ts +0 -2598
  175. package/src/__tests__/round-trip-ir.test.ts +0 -300
  176. package/src/__tests__/round-trip.test.ts +0 -1212
  177. package/src/__tests__/route-merging.test.ts +0 -372
  178. package/src/__tests__/router-composition.test.ts +0 -489
  179. package/src/__tests__/router-extractor.test.ts +0 -176
  180. package/src/__tests__/server-action-extractor.test.ts +0 -128
  181. package/src/__tests__/smart-type-inference.test.ts +0 -365
  182. package/src/__tests__/source-envelope.test.ts +0 -284
  183. package/src/__tests__/source-fidelity.test.ts +0 -516
  184. package/src/__tests__/state-extractor.test.ts +0 -115
  185. package/src/__tests__/strict-mode.test.ts +0 -227
  186. package/src/__tests__/transition-effect-extractor.test.ts +0 -119
  187. package/src/__tests__/transition-extractor.test.ts +0 -68
  188. package/src/__tests__/ts-to-expression.test.ts +0 -462
  189. package/src/__tests__/type-generator.test.ts +0 -201
  190. package/src/__tests__/uber-validation.test.ts +0 -502
  191. package/src/action-compiler.ts +0 -361
  192. package/src/babel/emitters/experience-transform.ts +0 -199
  193. package/src/babel/emitters/ir-to-tsx-emitter.ts +0 -110
  194. package/src/babel/emitters/pure-form-emitter.ts +0 -1023
  195. package/src/babel/emitters/runtime-glue-emitter.ts +0 -39
  196. package/src/babel/extractors/change-extractor.ts +0 -199
  197. package/src/babel/extractors/component-extractor.ts +0 -907
  198. package/src/babel/extractors/computed-extractor.ts +0 -262
  199. package/src/babel/extractors/context-extractor.ts +0 -277
  200. package/src/babel/extractors/during-extractor.ts +0 -295
  201. package/src/babel/extractors/effect-extractor.ts +0 -340
  202. package/src/babel/extractors/event-extractor.ts +0 -235
  203. package/src/babel/extractors/grammar-island-extractor.ts +0 -302
  204. package/src/babel/extractors/model-extractor.ts +0 -1018
  205. package/src/babel/extractors/router-extractor.ts +0 -303
  206. package/src/babel/extractors/server-action-extractor.ts +0 -173
  207. package/src/babel/extractors/server-action-hook-extractor.ts +0 -72
  208. package/src/babel/extractors/server-state-extractor.ts +0 -88
  209. package/src/babel/extractors/state-extractor.ts +0 -214
  210. package/src/babel/extractors/transition-effect-extractor.ts +0 -176
  211. package/src/babel/extractors/transition-extractor.ts +0 -143
  212. package/src/babel/index.ts +0 -24
  213. package/src/babel/transpilers/ts-to-expression.ts +0 -674
  214. package/src/babel/visitor.ts +0 -807
  215. package/src/cli/auth.ts +0 -255
  216. package/src/cli/build.ts +0 -288
  217. package/src/cli/deploy.ts +0 -206
  218. package/src/cli/index.ts +0 -328
  219. package/src/cli/installer.ts +0 -261
  220. package/src/cli/lock-file.ts +0 -94
  221. package/src/cli/mmrc.ts +0 -22
  222. package/src/cli/pull.ts +0 -172
  223. package/src/cli/registry-client.ts +0 -175
  224. package/src/cli/test.ts +0 -397
  225. package/src/cli/type-generator.ts +0 -243
  226. package/src/codemod/__tests__/forward.test.ts +0 -239
  227. package/src/codemod/__tests__/reverse.test.ts +0 -145
  228. package/src/codemod/__tests__/round-trip.test.ts +0 -137
  229. package/src/codemod/annotation.ts +0 -97
  230. package/src/codemod/classify.ts +0 -197
  231. package/src/codemod/cli.ts +0 -207
  232. package/src/codemod/control-flow.ts +0 -409
  233. package/src/codemod/forward.ts +0 -244
  234. package/src/codemod/import-manager.ts +0 -171
  235. package/src/codemod/index.ts +0 -120
  236. package/src/codemod/reverse.ts +0 -197
  237. package/src/codemod/rules.ts +0 -174
  238. package/src/codemod/state-transform.ts +0 -126
  239. package/src/decompiler/ast-builder.ts +0 -538
  240. package/src/decompiler/config-generator.ts +0 -151
  241. package/src/decompiler/index.ts +0 -315
  242. package/src/decompiler/project-decompiler.ts +0 -1776
  243. package/src/decompiler/project.ts +0 -862
  244. package/src/decompiler/split-strategy.ts +0 -140
  245. package/src/decompiler/state-emitter.ts +0 -1053
  246. package/src/decompiler/sx-emitter.ts +0 -318
  247. package/src/decompiler/workspace-hydrator.ts +0 -189
  248. package/src/dev-server.ts +0 -238
  249. package/src/envelope/fs-tree.ts +0 -217
  250. package/src/envelope/source-envelope.ts +0 -264
  251. package/src/envelope.ts +0 -315
  252. package/src/incremental-compiler.ts +0 -401
  253. package/src/index.ts +0 -99
  254. package/src/model-compiler.ts +0 -277
  255. package/src/project-compiler.ts +0 -1629
  256. package/src/route-extractor.ts +0 -333
  257. package/src/testing/index.ts +0 -32
  258. package/src/testing/snapshot.ts +0 -252
  259. package/src/testing/test-utils.ts +0 -226
  260. package/src/types.ts +0 -68
  261. package/src/vite/index.ts +0 -288
  262. package/test-compile.mjs +0 -142
  263. package/tsconfig.json +0 -25
  264. package/tsup.config.ts +0 -23
  265. package/vitest.config.ts +0 -9
@@ -1,409 +0,0 @@
1
- /**
2
- * Control flow transforms: Show/Each ↔ &&/.map()
3
- */
4
-
5
- import * as t from '@babel/types';
6
-
7
- // ─── Forward: React patterns → Atom control flow ───
8
-
9
- /**
10
- * Check if a JSX expression container holds a `{cond && <Element/>}` pattern.
11
- * Returns { condition, consequent } or null.
12
- */
13
- export function matchConditionalRender(
14
- expr: t.Expression,
15
- ): { condition: t.Expression; consequent: t.Expression } | null {
16
- if (expr.type !== 'LogicalExpression' || expr.operator !== '&&') return null;
17
- // The right side should be JSX or a parenthesized JSX
18
- if (!isJSXLike(expr.right)) return null;
19
- return { condition: expr.left, consequent: expr.right };
20
- }
21
-
22
- /**
23
- * Check if a JSX expression container holds a ternary `{cond ? <A/> : <B/>}`.
24
- * Returns { condition, consequent, alternate } or null.
25
- */
26
- export function matchTernaryRender(
27
- expr: t.Expression,
28
- ): { condition: t.Expression; consequent: t.Expression; alternate: t.Expression } | null {
29
- if (expr.type !== 'ConditionalExpression') return null;
30
- if (!isJSXLike(expr.consequent) && !isJSXLike(expr.alternate)) return null;
31
- return {
32
- condition: expr.test,
33
- consequent: expr.consequent,
34
- alternate: expr.alternate,
35
- };
36
- }
37
-
38
- /**
39
- * Check if a JSX expression container holds `{arr.map((item) => <Element/>)}`.
40
- * Returns { array, param, body, keyProp } or null.
41
- */
42
- export function matchMapRender(
43
- expr: t.Expression,
44
- ): {
45
- array: t.Expression;
46
- param: t.Identifier | t.Pattern;
47
- indexParam: t.Identifier | null;
48
- body: t.Expression | t.BlockStatement;
49
- keyExpression: t.Expression | null;
50
- } | null {
51
- if (expr.type !== 'CallExpression') return null;
52
- if (expr.callee.type !== 'MemberExpression') return null;
53
- const prop = expr.callee.property;
54
- if (!((prop.type === 'Identifier' && prop.name === 'map'))) return null;
55
-
56
- const array = expr.callee.object;
57
- const callback = expr.arguments[0];
58
- if (!callback) return null;
59
-
60
- if (
61
- callback.type !== 'ArrowFunctionExpression' &&
62
- callback.type !== 'FunctionExpression'
63
- )
64
- return null;
65
-
66
- const param = callback.params[0] || t.identifier('item');
67
- const indexParam =
68
- callback.params[1] && callback.params[1].type === 'Identifier'
69
- ? callback.params[1]
70
- : null;
71
-
72
- // Extract key prop from the returned JSX
73
- let keyExpression: t.Expression | null = null;
74
- const returnedJSX = getReturnedJSX(callback.body);
75
- if (returnedJSX && returnedJSX.type === 'JSXElement') {
76
- const keyAttr = returnedJSX.openingElement.attributes.find(
77
- (a): a is t.JSXAttribute =>
78
- a.type === 'JSXAttribute' &&
79
- a.name.type === 'JSXIdentifier' &&
80
- a.name.name === 'key',
81
- );
82
- if (keyAttr && keyAttr.value) {
83
- if (keyAttr.value.type === 'JSXExpressionContainer') {
84
- keyExpression = keyAttr.value.expression as t.Expression;
85
- } else if (keyAttr.value.type === 'StringLiteral') {
86
- keyExpression = keyAttr.value;
87
- }
88
- }
89
- }
90
-
91
- return {
92
- array,
93
- param: param as t.Identifier | t.Pattern,
94
- indexParam,
95
- body: callback.body,
96
- keyExpression,
97
- };
98
- }
99
-
100
- // ─── Create atom JSX for forward ───
101
-
102
- /**
103
- * Create <Show when={condition}>{children}</Show>
104
- */
105
- export function createShowElement(
106
- condition: t.Expression,
107
- children: t.JSXElement | t.JSXFragment | t.Expression,
108
- ): t.JSXElement {
109
- const whenAttr = t.jsxAttribute(
110
- t.jsxIdentifier('when'),
111
- t.jsxExpressionContainer(condition),
112
- );
113
-
114
- const childNodes: (t.JSXElement | t.JSXExpressionContainer | t.JSXText | t.JSXFragment)[] = [];
115
-
116
- if (t.isJSXElement(children) || t.isJSXFragment(children)) {
117
- childNodes.push(children);
118
- } else {
119
- childNodes.push(t.jsxExpressionContainer(children));
120
- }
121
-
122
- return t.jsxElement(
123
- t.jsxOpeningElement(t.jsxIdentifier('Show'), [whenAttr]),
124
- t.jsxClosingElement(t.jsxIdentifier('Show')),
125
- childNodes,
126
- false,
127
- );
128
- }
129
-
130
- /**
131
- * Create <Each items={array} as="param">{(param) => ...}</Each>
132
- */
133
- export function createEachElement(
134
- items: t.Expression,
135
- paramName: string,
136
- body: t.Expression | t.BlockStatement,
137
- keyExpression: t.Expression | null,
138
- indexParam: t.Identifier | null,
139
- ): t.JSXElement {
140
- const attrs: t.JSXAttribute[] = [
141
- t.jsxAttribute(
142
- t.jsxIdentifier('items'),
143
- t.jsxExpressionContainer(items),
144
- ),
145
- t.jsxAttribute(t.jsxIdentifier('as'), t.stringLiteral(paramName)),
146
- ];
147
-
148
- if (keyExpression) {
149
- attrs.push(
150
- t.jsxAttribute(
151
- t.jsxIdentifier('keyField'),
152
- t.jsxExpressionContainer(keyExpression),
153
- ),
154
- );
155
- }
156
-
157
- // Build callback: {(param, index) => ...}
158
- const params: t.Identifier[] = [t.identifier(paramName)];
159
- if (indexParam) params.push(indexParam);
160
-
161
- // If body is a block statement returning JSX, keep it; if expression, keep as arrow
162
- const arrow = t.arrowFunctionExpression(params, body);
163
- const child = t.jsxExpressionContainer(arrow);
164
-
165
- // Remove key prop from the returned JSX (it's now on Each's keyField)
166
- removeKeyFromBody(body);
167
-
168
- return t.jsxElement(
169
- t.jsxOpeningElement(t.jsxIdentifier('Each'), attrs),
170
- t.jsxClosingElement(t.jsxIdentifier('Each')),
171
- [child],
172
- false,
173
- );
174
- }
175
-
176
- // ─── Reverse: Atom control flow → React patterns ───
177
-
178
- /**
179
- * Check if a JSXElement is <Show when={expr}>.
180
- */
181
- export function matchShowElement(
182
- node: t.JSXElement,
183
- ): { condition: t.Expression; children: t.JSXElement['children'] } | null {
184
- const tag = node.openingElement.name;
185
- if (tag.type !== 'JSXIdentifier' || tag.name !== 'Show') return null;
186
-
187
- const whenAttr = node.openingElement.attributes.find(
188
- (a): a is t.JSXAttribute =>
189
- a.type === 'JSXAttribute' &&
190
- a.name.type === 'JSXIdentifier' &&
191
- a.name.name === 'when',
192
- );
193
- if (!whenAttr || !whenAttr.value) return null;
194
-
195
- let condition: t.Expression;
196
- if (whenAttr.value.type === 'JSXExpressionContainer') {
197
- condition = whenAttr.value.expression as t.Expression;
198
- } else if (whenAttr.value.type === 'StringLiteral') {
199
- condition = whenAttr.value;
200
- } else {
201
- return null;
202
- }
203
-
204
- return { condition, children: node.children };
205
- }
206
-
207
- /**
208
- * Check if a JSXElement is <Each items={expr} as="param">.
209
- */
210
- export function matchEachElement(
211
- node: t.JSXElement,
212
- ): {
213
- items: t.Expression;
214
- paramName: string;
215
- keyExpression: t.Expression | null;
216
- children: t.JSXElement['children'];
217
- } | null {
218
- const tag = node.openingElement.name;
219
- if (tag.type !== 'JSXIdentifier' || tag.name !== 'Each') return null;
220
-
221
- const itemsAttr = node.openingElement.attributes.find(
222
- (a): a is t.JSXAttribute =>
223
- a.type === 'JSXAttribute' &&
224
- a.name.type === 'JSXIdentifier' &&
225
- a.name.name === 'items',
226
- );
227
- if (!itemsAttr?.value) return null;
228
-
229
- const asAttr = node.openingElement.attributes.find(
230
- (a): a is t.JSXAttribute =>
231
- a.type === 'JSXAttribute' &&
232
- a.name.type === 'JSXIdentifier' &&
233
- a.name.name === 'as',
234
- );
235
-
236
- const keyAttr = node.openingElement.attributes.find(
237
- (a): a is t.JSXAttribute =>
238
- a.type === 'JSXAttribute' &&
239
- a.name.type === 'JSXIdentifier' &&
240
- a.name.name === 'keyField',
241
- );
242
-
243
- let items: t.Expression;
244
- if (itemsAttr.value.type === 'JSXExpressionContainer') {
245
- items = itemsAttr.value.expression as t.Expression;
246
- } else {
247
- return null;
248
- }
249
-
250
- const paramName =
251
- asAttr?.value?.type === 'StringLiteral' ? asAttr.value.value : 'item';
252
-
253
- let keyExpression: t.Expression | null = null;
254
- if (keyAttr?.value?.type === 'JSXExpressionContainer') {
255
- keyExpression = keyAttr.value.expression as t.Expression;
256
- }
257
-
258
- return { items, paramName, keyExpression, children: node.children };
259
- }
260
-
261
- /**
262
- * Convert <Show> back to {condition && children}
263
- */
264
- export function showToConditional(
265
- condition: t.Expression,
266
- children: t.JSXElement['children'],
267
- ): t.JSXExpressionContainer {
268
- const content = unwrapChildren(children);
269
- const logical = t.logicalExpression('&&', condition, content);
270
- return t.jsxExpressionContainer(logical);
271
- }
272
-
273
- /**
274
- * Convert <Each> back to {items.map((param) => children)}
275
- */
276
- export function eachToMap(
277
- items: t.Expression,
278
- paramName: string,
279
- keyExpression: t.Expression | null,
280
- children: t.JSXElement['children'],
281
- ): t.JSXExpressionContainer {
282
- // Find the callback in children (should be a JSXExpressionContainer with arrow)
283
- let callback: t.ArrowFunctionExpression | t.FunctionExpression | null = null;
284
-
285
- for (const child of children) {
286
- if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression') {
287
- const expr = child.expression;
288
- if (expr.type === 'ArrowFunctionExpression' || expr.type === 'FunctionExpression') {
289
- callback = expr;
290
- break;
291
- }
292
- }
293
- }
294
-
295
- if (!callback) {
296
- // Fallback: wrap children in arrow function
297
- const content = unwrapChildren(children);
298
- callback = t.arrowFunctionExpression([t.identifier(paramName)], content);
299
- }
300
-
301
- // If keyExpression, add key prop to the returned JSX
302
- if (keyExpression) {
303
- addKeyToCallback(callback, keyExpression);
304
- }
305
-
306
- const mapCall = t.callExpression(
307
- t.memberExpression(items, t.identifier('map')),
308
- [callback],
309
- );
310
-
311
- return t.jsxExpressionContainer(mapCall);
312
- }
313
-
314
- // ─── Helpers ───
315
-
316
- function isJSXLike(node: t.Node): boolean {
317
- return (
318
- node.type === 'JSXElement' ||
319
- node.type === 'JSXFragment' ||
320
- (node.type === 'ParenthesizedExpression' && isJSXLike((node as any).expression))
321
- );
322
- }
323
-
324
- function getReturnedJSX(
325
- body: t.Expression | t.BlockStatement,
326
- ): t.JSXElement | t.JSXFragment | null {
327
- if (body.type === 'JSXElement' || body.type === 'JSXFragment') return body;
328
- if (body.type === 'ParenthesizedExpression') {
329
- return getReturnedJSX((body as any).expression);
330
- }
331
- if (body.type === 'BlockStatement') {
332
- for (const stmt of body.body) {
333
- if (stmt.type === 'ReturnStatement' && stmt.argument) {
334
- if (
335
- stmt.argument.type === 'JSXElement' ||
336
- stmt.argument.type === 'JSXFragment'
337
- ) {
338
- return stmt.argument;
339
- }
340
- if (stmt.argument.type === 'ParenthesizedExpression') {
341
- return getReturnedJSX((stmt.argument as any).expression);
342
- }
343
- }
344
- }
345
- }
346
- return null;
347
- }
348
-
349
- function removeKeyFromBody(body: t.Expression | t.BlockStatement): void {
350
- const jsx = getReturnedJSX(body);
351
- if (jsx && jsx.type === 'JSXElement') {
352
- jsx.openingElement.attributes = jsx.openingElement.attributes.filter(
353
- a =>
354
- !(
355
- a.type === 'JSXAttribute' &&
356
- a.name.type === 'JSXIdentifier' &&
357
- a.name.name === 'key'
358
- ),
359
- );
360
- }
361
- }
362
-
363
- function addKeyToCallback(
364
- callback: t.ArrowFunctionExpression | t.FunctionExpression,
365
- keyExpr: t.Expression,
366
- ): void {
367
- const jsx = getReturnedJSX(callback.body);
368
- if (jsx && jsx.type === 'JSXElement') {
369
- // Check if key already exists
370
- const hasKey = jsx.openingElement.attributes.some(
371
- a =>
372
- a.type === 'JSXAttribute' &&
373
- a.name.type === 'JSXIdentifier' &&
374
- a.name.name === 'key',
375
- );
376
- if (!hasKey) {
377
- jsx.openingElement.attributes.unshift(
378
- t.jsxAttribute(
379
- t.jsxIdentifier('key'),
380
- t.jsxExpressionContainer(keyExpr),
381
- ),
382
- );
383
- }
384
- }
385
- }
386
-
387
- /**
388
- * Unwrap JSX children into a single expression.
389
- * If single JSX child, return it. If multiple, wrap in fragment.
390
- */
391
- function unwrapChildren(
392
- children: t.JSXElement['children'],
393
- ): t.JSXElement | t.JSXFragment | t.Expression {
394
- const meaningful = children.filter(c => {
395
- if (c.type === 'JSXText') return c.value.trim().length > 0;
396
- return true;
397
- });
398
-
399
- if (meaningful.length === 1) {
400
- const child = meaningful[0];
401
- if (child.type === 'JSXElement' || child.type === 'JSXFragment') return child;
402
- if (child.type === 'JSXExpressionContainer') {
403
- return child.expression as t.Expression;
404
- }
405
- }
406
-
407
- // Wrap in fragment
408
- return t.jsxFragment(t.jsxOpeningFragment(), t.jsxClosingFragment(), meaningful);
409
- }
@@ -1,244 +0,0 @@
1
- /**
2
- * Forward transform: Raw React → Workflow Atoms
3
- *
4
- * Babel visitor that:
5
- * 1. Renames HTML elements to atoms (div→Stack/Row, span→Text, etc.)
6
- * 2. Transforms control flow ({cond && <X/>} → <Show>, {arr.map()} → <Each>)
7
- * 3. Cleans up implicit className classes (flex, flex-col)
8
- * 4. Adds @mm-original annotations for round-trip fidelity
9
- * 5. Updates imports
10
- */
11
-
12
- import type { NodePath, Visitor } from '@babel/traverse';
13
- import * as t from '@babel/types';
14
- import {
15
- classifyElement,
16
- extractStaticClasses,
17
- removeClassesFromString,
18
- } from './classify';
19
- import {
20
- matchConditionalRender,
21
- matchTernaryRender,
22
- matchMapRender,
23
- createShowElement,
24
- createEachElement,
25
- } from './control-flow';
26
- import { addAnnotationComment } from './annotation';
27
- import type { ImportTracker } from './import-manager';
28
-
29
- export interface ForwardOptions {
30
- /** Add @mm-original annotations for round-trip fidelity */
31
- annotate?: boolean;
32
- /** State transform mode */
33
- stateMode?: 'preserve' | 'migrate' | 'annotate';
34
- }
35
-
36
- /**
37
- * Create the forward visitor (Raw React → Atoms).
38
- */
39
- export function createForwardVisitor(
40
- imports: ImportTracker,
41
- options: ForwardOptions = {},
42
- ): Visitor {
43
- const { annotate = true } = options;
44
-
45
- return {
46
- JSXElement(path: NodePath<t.JSXElement>) {
47
- const opening = path.node.openingElement;
48
- const tag = opening.name;
49
-
50
- // Only transform simple identifiers (div, span, button, etc.)
51
- // Skip namespaced (xml:foo) and member expressions (motion.div)
52
- if (tag.type !== 'JSXIdentifier') return;
53
-
54
- const tagName = tag.name;
55
-
56
- // Extract className for classification
57
- const classAttr = opening.attributes.find(
58
- (a): a is t.JSXAttribute =>
59
- a.type === 'JSXAttribute' &&
60
- a.name.type === 'JSXIdentifier' &&
61
- a.name.name === 'className',
62
- );
63
- const staticClasses = extractStaticClasses(classAttr?.value ?? null);
64
-
65
- // Classify the element
66
- const result = classifyElement(tagName, staticClasses);
67
- if (!result) return;
68
-
69
- // Add annotation before transforming
70
- if (annotate) {
71
- addAnnotationComment(path.node, result.originalTag, staticClasses);
72
- }
73
-
74
- // Rename the element
75
- tag.name = result.atom;
76
- if (path.node.closingElement) {
77
- (path.node.closingElement.name as t.JSXIdentifier).name = result.atom;
78
- }
79
-
80
- // Track atom usage for imports
81
- imports.addAtom(result.atom);
82
-
83
- // Clean up className: remove implicit classes
84
- if (classAttr && result.removeClasses.length > 0) {
85
- updateClassName(classAttr, result.removeClasses, 'remove');
86
- }
87
-
88
- // Add extra props (e.g., variant="h1" for headings, multiline for textarea)
89
- for (const [key, value] of Object.entries(result.addProps)) {
90
- const existing = opening.attributes.find(
91
- (a): a is t.JSXAttribute =>
92
- a.type === 'JSXAttribute' &&
93
- a.name.type === 'JSXIdentifier' &&
94
- a.name.name === key,
95
- );
96
- if (!existing) {
97
- if (typeof value === 'boolean') {
98
- opening.attributes.push(
99
- t.jsxAttribute(
100
- t.jsxIdentifier(key),
101
- value ? null : t.jsxExpressionContainer(t.booleanLiteral(false)),
102
- ),
103
- );
104
- } else {
105
- opening.attributes.push(
106
- t.jsxAttribute(t.jsxIdentifier(key), t.stringLiteral(value)),
107
- );
108
- }
109
- }
110
- }
111
-
112
- // Rename HTML event handlers to atom conventions
113
- renameEventHandlers(opening, 'forward');
114
- },
115
-
116
- JSXExpressionContainer(path: NodePath<t.JSXExpressionContainer>) {
117
- // Only transform expression containers that are direct children of JSX elements
118
- if (!path.parentPath.isJSXElement() && !path.parentPath.isJSXFragment()) return;
119
-
120
- const expr = path.node.expression;
121
- if (expr.type === 'JSXEmptyExpression') return;
122
-
123
- // Pattern: {condition && <Element />} → <Show when={condition}>
124
- const conditional = matchConditionalRender(expr as t.Expression);
125
- if (conditional) {
126
- const showEl = createShowElement(conditional.condition, conditional.consequent);
127
- imports.addAtom('Show');
128
- path.replaceWith(showEl);
129
- return;
130
- }
131
-
132
- // Pattern: {condition ? <A /> : <B />} → <Show when={cond}><A/></Show><Show when={!cond}><B/></Show>
133
- const ternary = matchTernaryRender(expr as t.Expression);
134
- if (ternary) {
135
- const show1 = createShowElement(ternary.condition, ternary.consequent);
136
- const negated = t.unaryExpression('!', ternary.condition);
137
- const show2 = createShowElement(negated, ternary.alternate);
138
- imports.addAtom('Show');
139
-
140
- // Replace with both Show elements
141
- path.replaceWithMultiple([show1, show2]);
142
- return;
143
- }
144
-
145
- // Pattern: {array.map((item) => <Element />)} → <Each items={array}>
146
- const mapRender = matchMapRender(expr as t.Expression);
147
- if (mapRender) {
148
- const paramName =
149
- mapRender.param.type === 'Identifier'
150
- ? mapRender.param.name
151
- : 'item';
152
-
153
- const eachEl = createEachElement(
154
- mapRender.array,
155
- paramName,
156
- mapRender.body,
157
- mapRender.keyExpression,
158
- mapRender.indexParam,
159
- );
160
- imports.addAtom('Each');
161
- path.replaceWith(eachEl);
162
- return;
163
- }
164
- },
165
- };
166
- }
167
-
168
- /**
169
- * Update a className attribute: remove or add classes.
170
- */
171
- function updateClassName(
172
- attr: t.JSXAttribute,
173
- classes: string[],
174
- operation: 'remove' | 'add',
175
- ): void {
176
- if (!attr.value) return;
177
-
178
- if (attr.value.type === 'StringLiteral') {
179
- const result =
180
- operation === 'remove'
181
- ? removeClassesFromString(attr.value.value, classes)
182
- : attr.value.value;
183
-
184
- if (result.trim()) {
185
- attr.value = t.stringLiteral(result);
186
- } else {
187
- // Empty className — could remove the attribute, but keep it for round-trip
188
- attr.value = t.stringLiteral('');
189
- }
190
- return;
191
- }
192
-
193
- if (attr.value.type === 'JSXExpressionContainer') {
194
- const expr = attr.value.expression;
195
-
196
- // Template literal: remove from static quasis
197
- if (expr.type === 'TemplateLiteral') {
198
- for (const quasi of expr.quasis) {
199
- if (operation === 'remove') {
200
- quasi.value.raw = removeClassesFromString(quasi.value.raw, classes);
201
- quasi.value.cooked = removeClassesFromString(quasi.value.cooked ?? quasi.value.raw, classes);
202
- }
203
- }
204
- return;
205
- }
206
-
207
- // cn("flex items-center", dynamic) → cn("items-center", dynamic)
208
- if (
209
- expr.type === 'CallExpression' &&
210
- expr.callee.type === 'Identifier' &&
211
- expr.callee.name === 'cn' &&
212
- expr.arguments.length > 0
213
- ) {
214
- const firstArg = expr.arguments[0];
215
- if (firstArg.type === 'StringLiteral' && operation === 'remove') {
216
- firstArg.value = removeClassesFromString(firstArg.value, classes);
217
- }
218
- return;
219
- }
220
- }
221
- }
222
-
223
- /**
224
- * Rename HTML event handler props to atom conventions and vice versa.
225
- */
226
- function renameEventHandlers(
227
- opening: t.JSXOpeningElement,
228
- direction: 'forward' | 'reverse',
229
- ): void {
230
- const map: Record<string, string> =
231
- direction === 'forward'
232
- ? { onClick: 'onPress', onChange: 'onChange', onSubmit: 'onSubmit' }
233
- : { onPress: 'onClick' };
234
-
235
- for (const attr of opening.attributes) {
236
- if (
237
- attr.type === 'JSXAttribute' &&
238
- attr.name.type === 'JSXIdentifier' &&
239
- attr.name.name in map
240
- ) {
241
- attr.name.name = map[attr.name.name];
242
- }
243
- }
244
- }