@mmapp/react-compiler 0.1.0-alpha.7 → 0.1.0-alpha.9
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.
- package/dist/auth-3UK75242.mjs +17 -0
- package/dist/babel/index.d.mts +2 -2
- package/dist/babel/index.d.ts +2 -2
- package/dist/babel/index.js +27 -31
- package/dist/babel/index.mjs +1 -1
- package/dist/chunk-7T6Q5KAA.mjs +7506 -0
- package/dist/chunk-ABYPKRSB.mjs +215 -0
- package/dist/chunk-BZEXUPDH.mjs +175 -0
- package/dist/chunk-J3M4GUS7.mjs +161 -0
- package/dist/chunk-JRGFBWTN.mjs +2918 -0
- package/dist/chunk-R2DD5GTY.mjs +186 -0
- package/dist/chunk-UDDTWG5J.mjs +734 -0
- package/dist/chunk-VLTKQDJ3.mjs +244 -0
- package/dist/chunk-WVYY32LD.mjs +939 -0
- package/dist/chunk-XDVM4YHX.mjs +3450 -0
- package/dist/cli/index.js +4372 -3240
- package/dist/cli/index.mjs +77 -179
- package/dist/codemod/cli.js +1 -1
- package/dist/codemod/cli.mjs +1 -1
- package/dist/codemod/index.d.mts +3 -3
- package/dist/codemod/index.d.ts +3 -3
- package/dist/codemod/index.js +1 -1
- package/dist/codemod/index.mjs +1 -1
- package/dist/deploy-YAJGW6II.mjs +9 -0
- package/dist/dev-server-CrQ041KP.d.mts +79 -0
- package/dist/dev-server-CrQ041KP.d.ts +79 -0
- package/dist/dev-server.d.mts +2 -2
- package/dist/dev-server.d.ts +2 -2
- package/dist/dev-server.js +1240 -120
- package/dist/dev-server.mjs +4 -4
- package/dist/envelope-ChEkuHij.d.mts +265 -0
- package/dist/envelope-ChEkuHij.d.ts +265 -0
- package/dist/envelope.d.mts +2 -2
- package/dist/envelope.d.ts +2 -2
- package/dist/envelope.js +27 -31
- package/dist/envelope.mjs +2 -2
- package/dist/index-CEKyyazf.d.mts +104 -0
- package/dist/index-CEKyyazf.d.ts +104 -0
- package/dist/index.d.mts +8 -8
- package/dist/index.d.ts +8 -8
- package/dist/index.js +1512 -600
- package/dist/index.mjs +8 -8
- package/dist/init-7FJENUDK.mjs +407 -0
- package/dist/project-compiler-ZB4RUYVL.mjs +10 -0
- package/dist/project-decompiler-U55HQUHW.mjs +7 -0
- package/dist/pull-KOL2QAYQ.mjs +109 -0
- package/dist/seed-KOGEPGOJ.mjs +154 -0
- package/dist/server-VW6UPCHO.mjs +277 -0
- package/dist/testing/index.d.mts +8 -8
- package/dist/testing/index.d.ts +8 -8
- package/dist/testing/index.js +27 -31
- package/dist/testing/index.mjs +1 -1
- package/dist/verify-OQDEQYMS.mjs +1833 -0
- package/dist/vite/index.d.mts +1 -1
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +27 -31
- package/dist/vite/index.mjs +2 -2
- package/examples/authentication/main.workflow.tsx +1 -1
- package/examples/authentication/mm.config.ts +1 -1
- package/examples/authentication/pages/LoginPage.tsx +2 -2
- package/examples/authentication/pages/SignupPage.tsx +2 -2
- package/examples/counter.workflow.tsx +1 -1
- package/examples/dashboard.workflow.tsx +1 -1
- package/examples/invoice-approval/actions/invoice.server.ts +1 -1
- package/examples/invoice-approval/main.workflow.tsx +1 -1
- package/examples/invoice-approval/mm.config.ts +1 -1
- package/examples/invoice-approval/pages/InvoiceDetailPage.tsx +1 -1
- package/examples/invoice-approval/pages/InvoiceFormPage.tsx +1 -1
- package/examples/invoice-approval/pages/InvoiceListPage.tsx +1 -1
- package/examples/todo-app.workflow.tsx +1 -1
- package/examples/uber-app/actions/matching.server.ts +1 -1
- package/examples/uber-app/actions/notifications.server.ts +1 -1
- package/examples/uber-app/actions/payments.server.ts +1 -1
- package/examples/uber-app/actions/pricing.server.ts +1 -1
- package/examples/uber-app/app/admin/analytics.tsx +2 -2
- package/examples/uber-app/app/admin/fleet.tsx +2 -2
- package/examples/uber-app/app/admin/surge-pricing.tsx +2 -2
- package/examples/uber-app/app/driver/dashboard.tsx +2 -2
- package/examples/uber-app/app/driver/earnings.tsx +2 -2
- package/examples/uber-app/app/driver/navigation.tsx +2 -2
- package/examples/uber-app/app/driver/ride-acceptance.tsx +2 -2
- package/examples/uber-app/app/rider/home.tsx +2 -2
- package/examples/uber-app/app/rider/payment-methods.tsx +2 -2
- package/examples/uber-app/app/rider/ride-history.tsx +2 -2
- package/examples/uber-app/app/rider/ride-tracking.tsx +2 -2
- package/examples/uber-app/components/DriverCard.tsx +1 -1
- package/examples/uber-app/components/MapView.tsx +3 -3
- package/examples/uber-app/components/RatingStars.tsx +2 -2
- package/examples/uber-app/components/RideCard.tsx +1 -1
- package/examples/uber-app/mm.config.ts +1 -1
- package/examples/uber-app/workflows/dispute-resolution.workflow.tsx +2 -2
- package/examples/uber-app/workflows/driver-onboarding.workflow.tsx +2 -2
- package/examples/uber-app/workflows/payment-processing.workflow.tsx +2 -2
- package/examples/uber-app/workflows/ride-request.workflow.tsx +2 -2
- package/package.json +7 -14
|
@@ -0,0 +1,939 @@
|
|
|
1
|
+
// src/codemod/index.ts
|
|
2
|
+
import { transformSync } from "@babel/core";
|
|
3
|
+
import { declare } from "@babel/helper-plugin-utils";
|
|
4
|
+
|
|
5
|
+
// src/codemod/forward.ts
|
|
6
|
+
import * as t2 from "@babel/types";
|
|
7
|
+
|
|
8
|
+
// src/codemod/rules.ts
|
|
9
|
+
var FORWARD_RULES = [
|
|
10
|
+
// div with flex-col → Stack
|
|
11
|
+
{
|
|
12
|
+
htmlTag: "div",
|
|
13
|
+
atom: "Stack",
|
|
14
|
+
classSignal: ["flex-col"],
|
|
15
|
+
implicitClasses: ["flex", "flex-col"],
|
|
16
|
+
priority: 20
|
|
17
|
+
},
|
|
18
|
+
// div with grid → Grid
|
|
19
|
+
{
|
|
20
|
+
htmlTag: "div",
|
|
21
|
+
atom: "Grid",
|
|
22
|
+
classSignal: ["grid"],
|
|
23
|
+
implicitClasses: ["grid"],
|
|
24
|
+
priority: 20
|
|
25
|
+
},
|
|
26
|
+
// div with flex (no flex-col) → Row
|
|
27
|
+
{
|
|
28
|
+
htmlTag: "div",
|
|
29
|
+
atom: "Row",
|
|
30
|
+
classSignal: ["flex"],
|
|
31
|
+
implicitClasses: ["flex"],
|
|
32
|
+
priority: 10
|
|
33
|
+
},
|
|
34
|
+
// div with no layout signal → Stack (default container)
|
|
35
|
+
{
|
|
36
|
+
htmlTag: "div",
|
|
37
|
+
atom: "Stack",
|
|
38
|
+
classSignal: [],
|
|
39
|
+
implicitClasses: [],
|
|
40
|
+
priority: 0
|
|
41
|
+
},
|
|
42
|
+
// Semantic HTML
|
|
43
|
+
{ htmlTag: "main", atom: "Stack", implicitClasses: [] },
|
|
44
|
+
{ htmlTag: "aside", atom: "Stack", implicitClasses: [] },
|
|
45
|
+
{ htmlTag: "nav", atom: "Stack", implicitClasses: [] },
|
|
46
|
+
{ htmlTag: "section", atom: "Section", implicitClasses: [] },
|
|
47
|
+
{ htmlTag: "header", atom: "Row", implicitClasses: [] },
|
|
48
|
+
{ htmlTag: "footer", atom: "Row", implicitClasses: [] },
|
|
49
|
+
// Text elements
|
|
50
|
+
{ htmlTag: "span", atom: "Text", implicitClasses: [] },
|
|
51
|
+
{ htmlTag: "p", atom: "Text", implicitClasses: [] },
|
|
52
|
+
{ htmlTag: "label", atom: "Text", implicitClasses: [] },
|
|
53
|
+
{ htmlTag: "h1", atom: "Text", implicitClasses: [], props: { variant: "h1" } },
|
|
54
|
+
{ htmlTag: "h2", atom: "Text", implicitClasses: [], props: { variant: "h2" } },
|
|
55
|
+
{ htmlTag: "h3", atom: "Text", implicitClasses: [], props: { variant: "h3" } },
|
|
56
|
+
{ htmlTag: "h4", atom: "Text", implicitClasses: [], props: { variant: "h4" } },
|
|
57
|
+
{ htmlTag: "h5", atom: "Text", implicitClasses: [], props: { variant: "h5" } },
|
|
58
|
+
{ htmlTag: "h6", atom: "Text", implicitClasses: [], props: { variant: "h6" } },
|
|
59
|
+
// Interactive elements
|
|
60
|
+
{ htmlTag: "button", atom: "Button", implicitClasses: [] },
|
|
61
|
+
{ htmlTag: "a", atom: "Link", implicitClasses: [] },
|
|
62
|
+
// Form elements
|
|
63
|
+
{ htmlTag: "input", atom: "TextInput", implicitClasses: [] },
|
|
64
|
+
{
|
|
65
|
+
htmlTag: "textarea",
|
|
66
|
+
atom: "TextInput",
|
|
67
|
+
implicitClasses: [],
|
|
68
|
+
props: { multiline: true }
|
|
69
|
+
},
|
|
70
|
+
// Media
|
|
71
|
+
{ htmlTag: "img", atom: "Image", implicitClasses: [] }
|
|
72
|
+
];
|
|
73
|
+
var REVERSE_RULES = [
|
|
74
|
+
{ atom: "Stack", htmlTag: "div", injectClasses: ["flex", "flex-col"] },
|
|
75
|
+
{ atom: "Row", htmlTag: "div", injectClasses: ["flex"] },
|
|
76
|
+
{ atom: "Grid", htmlTag: "div", injectClasses: ["grid"] },
|
|
77
|
+
{ atom: "Text", htmlTag: "span" },
|
|
78
|
+
{ atom: "Button", htmlTag: "button" },
|
|
79
|
+
{ atom: "Link", htmlTag: "a" },
|
|
80
|
+
{ atom: "TextInput", htmlTag: "input" },
|
|
81
|
+
{ atom: "Image", htmlTag: "img" },
|
|
82
|
+
{ atom: "Section", htmlTag: "section" }
|
|
83
|
+
];
|
|
84
|
+
var CONTROL_FLOW_ATOMS = /* @__PURE__ */ new Set([
|
|
85
|
+
"Show",
|
|
86
|
+
"Each",
|
|
87
|
+
"Modal",
|
|
88
|
+
"Slot",
|
|
89
|
+
"Divider"
|
|
90
|
+
]);
|
|
91
|
+
var ATOM_IMPORT_SOURCE = "@mmapp/react/atoms";
|
|
92
|
+
var TEXT_VARIANT_TO_TAG = {
|
|
93
|
+
h1: "h1",
|
|
94
|
+
h2: "h2",
|
|
95
|
+
h3: "h3",
|
|
96
|
+
h4: "h4",
|
|
97
|
+
h5: "h5",
|
|
98
|
+
h6: "h6"
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// src/codemod/classify.ts
|
|
102
|
+
function extractStaticClasses(attrValue) {
|
|
103
|
+
if (!attrValue) return [];
|
|
104
|
+
if (attrValue.type === "StringLiteral") {
|
|
105
|
+
return attrValue.value.split(/\s+/).filter(Boolean);
|
|
106
|
+
}
|
|
107
|
+
if (attrValue.type === "JSXExpressionContainer") {
|
|
108
|
+
return extractStaticClasses(attrValue.expression);
|
|
109
|
+
}
|
|
110
|
+
if (attrValue.type === "TemplateLiteral") {
|
|
111
|
+
const staticParts = attrValue.quasis.map((q) => q.value.raw).join(" ");
|
|
112
|
+
return staticParts.split(/\s+/).filter(Boolean);
|
|
113
|
+
}
|
|
114
|
+
if (attrValue.type === "CallExpression" && attrValue.callee.type === "Identifier" && attrValue.callee.name === "cn" && attrValue.arguments.length > 0) {
|
|
115
|
+
const firstArg = attrValue.arguments[0];
|
|
116
|
+
if (firstArg.type === "StringLiteral") {
|
|
117
|
+
return firstArg.value.split(/\s+/).filter(Boolean);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return [];
|
|
121
|
+
}
|
|
122
|
+
function classifyElement(tagName, classNames) {
|
|
123
|
+
if (tagName[0] === tagName[0].toUpperCase() && tagName[0] !== tagName[0].toLowerCase()) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
if (tagName.startsWith("motion.")) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
const candidates = FORWARD_RULES.filter((r) => r.htmlTag === tagName).sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
130
|
+
if (candidates.length === 0) return null;
|
|
131
|
+
if (tagName === "div") {
|
|
132
|
+
for (const rule2 of candidates) {
|
|
133
|
+
if (!rule2.classSignal || rule2.classSignal.length === 0) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const hasAllSignals = rule2.classSignal.every((s) => classNames.includes(s));
|
|
137
|
+
if (hasAllSignals) {
|
|
138
|
+
return makeResult(rule2, tagName);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const defaultRule = candidates.find((r) => (r.priority ?? 0) === 0);
|
|
142
|
+
if (defaultRule) {
|
|
143
|
+
return makeResult(defaultRule, tagName);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const rule = candidates[0];
|
|
147
|
+
return makeResult(rule, tagName);
|
|
148
|
+
}
|
|
149
|
+
function makeResult(rule, tagName) {
|
|
150
|
+
return {
|
|
151
|
+
atom: rule.atom,
|
|
152
|
+
removeClasses: rule.implicitClasses ?? [],
|
|
153
|
+
addProps: rule.props ?? {},
|
|
154
|
+
originalTag: tagName
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function reverseClassifyAtom(atomName, props) {
|
|
158
|
+
if (CONTROL_FLOW_ATOMS.has(atomName)) return null;
|
|
159
|
+
if (atomName === "Text") {
|
|
160
|
+
const variantNode = props.get("variant");
|
|
161
|
+
if (variantNode && variantNode.type === "StringLiteral") {
|
|
162
|
+
const tag = TEXT_VARIANT_TO_TAG[variantNode.value];
|
|
163
|
+
if (tag) {
|
|
164
|
+
return {
|
|
165
|
+
htmlTag: tag,
|
|
166
|
+
injectClasses: [],
|
|
167
|
+
removeProps: ["variant"]
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return { htmlTag: "span", injectClasses: [], removeProps: [] };
|
|
172
|
+
}
|
|
173
|
+
if (atomName === "TextInput") {
|
|
174
|
+
const multilineNode = props.get("multiline");
|
|
175
|
+
if (multilineNode) {
|
|
176
|
+
const isMultiline = multilineNode.type === "BooleanLiteral" && multilineNode.value || multilineNode.type === "JSXExpressionContainer" && multilineNode.expression?.type === "BooleanLiteral" && multilineNode.expression?.value;
|
|
177
|
+
if (isMultiline) {
|
|
178
|
+
return { htmlTag: "textarea", injectClasses: [], removeProps: ["multiline"] };
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return { htmlTag: "input", injectClasses: [], removeProps: [] };
|
|
182
|
+
}
|
|
183
|
+
const rule = REVERSE_RULES.find((r) => r.atom === atomName);
|
|
184
|
+
if (!rule) return null;
|
|
185
|
+
return {
|
|
186
|
+
htmlTag: rule.htmlTag,
|
|
187
|
+
injectClasses: rule.injectClasses ?? [],
|
|
188
|
+
removeProps: rule.removeProps ?? []
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function removeClassesFromString(className, toRemove) {
|
|
192
|
+
if (toRemove.length === 0) return className;
|
|
193
|
+
const removeSet = new Set(toRemove);
|
|
194
|
+
return className.split(/\s+/).filter((c) => c && !removeSet.has(c)).join(" ");
|
|
195
|
+
}
|
|
196
|
+
function addClassesToString(className, toAdd) {
|
|
197
|
+
if (toAdd.length === 0) return className;
|
|
198
|
+
const existing = new Set(className.split(/\s+/).filter(Boolean));
|
|
199
|
+
const newClasses = toAdd.filter((c) => !existing.has(c));
|
|
200
|
+
if (newClasses.length === 0) return className;
|
|
201
|
+
return [...newClasses, ...className.split(/\s+/).filter(Boolean)].join(" ");
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// src/codemod/control-flow.ts
|
|
205
|
+
import * as t from "@babel/types";
|
|
206
|
+
function matchConditionalRender(expr) {
|
|
207
|
+
if (expr.type !== "LogicalExpression" || expr.operator !== "&&") return null;
|
|
208
|
+
if (!isJSXLike(expr.right)) return null;
|
|
209
|
+
return { condition: expr.left, consequent: expr.right };
|
|
210
|
+
}
|
|
211
|
+
function matchTernaryRender(expr) {
|
|
212
|
+
if (expr.type !== "ConditionalExpression") return null;
|
|
213
|
+
if (!isJSXLike(expr.consequent) && !isJSXLike(expr.alternate)) return null;
|
|
214
|
+
return {
|
|
215
|
+
condition: expr.test,
|
|
216
|
+
consequent: expr.consequent,
|
|
217
|
+
alternate: expr.alternate
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
function matchMapRender(expr) {
|
|
221
|
+
if (expr.type !== "CallExpression") return null;
|
|
222
|
+
if (expr.callee.type !== "MemberExpression") return null;
|
|
223
|
+
const prop = expr.callee.property;
|
|
224
|
+
if (!(prop.type === "Identifier" && prop.name === "map")) return null;
|
|
225
|
+
const array = expr.callee.object;
|
|
226
|
+
const callback = expr.arguments[0];
|
|
227
|
+
if (!callback) return null;
|
|
228
|
+
if (callback.type !== "ArrowFunctionExpression" && callback.type !== "FunctionExpression")
|
|
229
|
+
return null;
|
|
230
|
+
const param = callback.params[0] || t.identifier("item");
|
|
231
|
+
const indexParam = callback.params[1] && callback.params[1].type === "Identifier" ? callback.params[1] : null;
|
|
232
|
+
let keyExpression = null;
|
|
233
|
+
const returnedJSX = getReturnedJSX(callback.body);
|
|
234
|
+
if (returnedJSX && returnedJSX.type === "JSXElement") {
|
|
235
|
+
const keyAttr = returnedJSX.openingElement.attributes.find(
|
|
236
|
+
(a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "key"
|
|
237
|
+
);
|
|
238
|
+
if (keyAttr && keyAttr.value) {
|
|
239
|
+
if (keyAttr.value.type === "JSXExpressionContainer") {
|
|
240
|
+
keyExpression = keyAttr.value.expression;
|
|
241
|
+
} else if (keyAttr.value.type === "StringLiteral") {
|
|
242
|
+
keyExpression = keyAttr.value;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
array,
|
|
248
|
+
param,
|
|
249
|
+
indexParam,
|
|
250
|
+
body: callback.body,
|
|
251
|
+
keyExpression
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function createShowElement(condition, children) {
|
|
255
|
+
const whenAttr = t.jsxAttribute(
|
|
256
|
+
t.jsxIdentifier("when"),
|
|
257
|
+
t.jsxExpressionContainer(condition)
|
|
258
|
+
);
|
|
259
|
+
const childNodes = [];
|
|
260
|
+
if (t.isJSXElement(children) || t.isJSXFragment(children)) {
|
|
261
|
+
childNodes.push(children);
|
|
262
|
+
} else {
|
|
263
|
+
childNodes.push(t.jsxExpressionContainer(children));
|
|
264
|
+
}
|
|
265
|
+
return t.jsxElement(
|
|
266
|
+
t.jsxOpeningElement(t.jsxIdentifier("Show"), [whenAttr]),
|
|
267
|
+
t.jsxClosingElement(t.jsxIdentifier("Show")),
|
|
268
|
+
childNodes,
|
|
269
|
+
false
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
function createEachElement(items, paramName, body, keyExpression, indexParam) {
|
|
273
|
+
const attrs = [
|
|
274
|
+
t.jsxAttribute(
|
|
275
|
+
t.jsxIdentifier("items"),
|
|
276
|
+
t.jsxExpressionContainer(items)
|
|
277
|
+
),
|
|
278
|
+
t.jsxAttribute(t.jsxIdentifier("as"), t.stringLiteral(paramName))
|
|
279
|
+
];
|
|
280
|
+
if (keyExpression) {
|
|
281
|
+
attrs.push(
|
|
282
|
+
t.jsxAttribute(
|
|
283
|
+
t.jsxIdentifier("keyField"),
|
|
284
|
+
t.jsxExpressionContainer(keyExpression)
|
|
285
|
+
)
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
const params = [t.identifier(paramName)];
|
|
289
|
+
if (indexParam) params.push(indexParam);
|
|
290
|
+
const arrow = t.arrowFunctionExpression(params, body);
|
|
291
|
+
const child = t.jsxExpressionContainer(arrow);
|
|
292
|
+
removeKeyFromBody(body);
|
|
293
|
+
return t.jsxElement(
|
|
294
|
+
t.jsxOpeningElement(t.jsxIdentifier("Each"), attrs),
|
|
295
|
+
t.jsxClosingElement(t.jsxIdentifier("Each")),
|
|
296
|
+
[child],
|
|
297
|
+
false
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
function matchShowElement(node) {
|
|
301
|
+
const tag = node.openingElement.name;
|
|
302
|
+
if (tag.type !== "JSXIdentifier" || tag.name !== "Show") return null;
|
|
303
|
+
const whenAttr = node.openingElement.attributes.find(
|
|
304
|
+
(a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "when"
|
|
305
|
+
);
|
|
306
|
+
if (!whenAttr || !whenAttr.value) return null;
|
|
307
|
+
let condition;
|
|
308
|
+
if (whenAttr.value.type === "JSXExpressionContainer") {
|
|
309
|
+
condition = whenAttr.value.expression;
|
|
310
|
+
} else if (whenAttr.value.type === "StringLiteral") {
|
|
311
|
+
condition = whenAttr.value;
|
|
312
|
+
} else {
|
|
313
|
+
return null;
|
|
314
|
+
}
|
|
315
|
+
return { condition, children: node.children };
|
|
316
|
+
}
|
|
317
|
+
function matchEachElement(node) {
|
|
318
|
+
const tag = node.openingElement.name;
|
|
319
|
+
if (tag.type !== "JSXIdentifier" || tag.name !== "Each") return null;
|
|
320
|
+
const itemsAttr = node.openingElement.attributes.find(
|
|
321
|
+
(a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "items"
|
|
322
|
+
);
|
|
323
|
+
if (!itemsAttr?.value) return null;
|
|
324
|
+
const asAttr = node.openingElement.attributes.find(
|
|
325
|
+
(a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "as"
|
|
326
|
+
);
|
|
327
|
+
const keyAttr = node.openingElement.attributes.find(
|
|
328
|
+
(a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "keyField"
|
|
329
|
+
);
|
|
330
|
+
let items;
|
|
331
|
+
if (itemsAttr.value.type === "JSXExpressionContainer") {
|
|
332
|
+
items = itemsAttr.value.expression;
|
|
333
|
+
} else {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
const paramName = asAttr?.value?.type === "StringLiteral" ? asAttr.value.value : "item";
|
|
337
|
+
let keyExpression = null;
|
|
338
|
+
if (keyAttr?.value?.type === "JSXExpressionContainer") {
|
|
339
|
+
keyExpression = keyAttr.value.expression;
|
|
340
|
+
}
|
|
341
|
+
return { items, paramName, keyExpression, children: node.children };
|
|
342
|
+
}
|
|
343
|
+
function showToConditional(condition, children) {
|
|
344
|
+
const content = unwrapChildren(children);
|
|
345
|
+
const logical = t.logicalExpression("&&", condition, content);
|
|
346
|
+
return t.jsxExpressionContainer(logical);
|
|
347
|
+
}
|
|
348
|
+
function eachToMap(items, paramName, keyExpression, children) {
|
|
349
|
+
let callback = null;
|
|
350
|
+
for (const child of children) {
|
|
351
|
+
if (child.type === "JSXExpressionContainer" && child.expression.type !== "JSXEmptyExpression") {
|
|
352
|
+
const expr = child.expression;
|
|
353
|
+
if (expr.type === "ArrowFunctionExpression" || expr.type === "FunctionExpression") {
|
|
354
|
+
callback = expr;
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (!callback) {
|
|
360
|
+
const content = unwrapChildren(children);
|
|
361
|
+
callback = t.arrowFunctionExpression([t.identifier(paramName)], content);
|
|
362
|
+
}
|
|
363
|
+
if (keyExpression) {
|
|
364
|
+
addKeyToCallback(callback, keyExpression);
|
|
365
|
+
}
|
|
366
|
+
const mapCall = t.callExpression(
|
|
367
|
+
t.memberExpression(items, t.identifier("map")),
|
|
368
|
+
[callback]
|
|
369
|
+
);
|
|
370
|
+
return t.jsxExpressionContainer(mapCall);
|
|
371
|
+
}
|
|
372
|
+
function isJSXLike(node) {
|
|
373
|
+
return node.type === "JSXElement" || node.type === "JSXFragment" || node.type === "ParenthesizedExpression" && isJSXLike(node.expression);
|
|
374
|
+
}
|
|
375
|
+
function getReturnedJSX(body) {
|
|
376
|
+
if (body.type === "JSXElement" || body.type === "JSXFragment") return body;
|
|
377
|
+
if (body.type === "ParenthesizedExpression") {
|
|
378
|
+
return getReturnedJSX(body.expression);
|
|
379
|
+
}
|
|
380
|
+
if (body.type === "BlockStatement") {
|
|
381
|
+
for (const stmt of body.body) {
|
|
382
|
+
if (stmt.type === "ReturnStatement" && stmt.argument) {
|
|
383
|
+
if (stmt.argument.type === "JSXElement" || stmt.argument.type === "JSXFragment") {
|
|
384
|
+
return stmt.argument;
|
|
385
|
+
}
|
|
386
|
+
if (stmt.argument.type === "ParenthesizedExpression") {
|
|
387
|
+
return getReturnedJSX(stmt.argument.expression);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return null;
|
|
393
|
+
}
|
|
394
|
+
function removeKeyFromBody(body) {
|
|
395
|
+
const jsx = getReturnedJSX(body);
|
|
396
|
+
if (jsx && jsx.type === "JSXElement") {
|
|
397
|
+
jsx.openingElement.attributes = jsx.openingElement.attributes.filter(
|
|
398
|
+
(a) => !(a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "key")
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
function addKeyToCallback(callback, keyExpr) {
|
|
403
|
+
const jsx = getReturnedJSX(callback.body);
|
|
404
|
+
if (jsx && jsx.type === "JSXElement") {
|
|
405
|
+
const hasKey = jsx.openingElement.attributes.some(
|
|
406
|
+
(a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "key"
|
|
407
|
+
);
|
|
408
|
+
if (!hasKey) {
|
|
409
|
+
jsx.openingElement.attributes.unshift(
|
|
410
|
+
t.jsxAttribute(
|
|
411
|
+
t.jsxIdentifier("key"),
|
|
412
|
+
t.jsxExpressionContainer(keyExpr)
|
|
413
|
+
)
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
function unwrapChildren(children) {
|
|
419
|
+
const meaningful = children.filter((c) => {
|
|
420
|
+
if (c.type === "JSXText") return c.value.trim().length > 0;
|
|
421
|
+
return true;
|
|
422
|
+
});
|
|
423
|
+
if (meaningful.length === 1) {
|
|
424
|
+
const child = meaningful[0];
|
|
425
|
+
if (child.type === "JSXElement" || child.type === "JSXFragment") return child;
|
|
426
|
+
if (child.type === "JSXExpressionContainer") {
|
|
427
|
+
return child.expression;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return t.jsxFragment(t.jsxOpeningFragment(), t.jsxClosingFragment(), meaningful);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// src/codemod/annotation.ts
|
|
434
|
+
var ANNOTATION_PREFIX = "@mm-original";
|
|
435
|
+
function createAnnotation(originalTag, classNames) {
|
|
436
|
+
const parts = [originalTag, ...classNames.slice(0, 10)];
|
|
437
|
+
return `${ANNOTATION_PREFIX} ${parts.join(".")}`;
|
|
438
|
+
}
|
|
439
|
+
function parseAnnotation(comment) {
|
|
440
|
+
const trimmed = comment.trim();
|
|
441
|
+
if (!trimmed.startsWith(ANNOTATION_PREFIX)) return null;
|
|
442
|
+
const rest = trimmed.slice(ANNOTATION_PREFIX.length).trim();
|
|
443
|
+
if (!rest) return null;
|
|
444
|
+
const parts = rest.split(".");
|
|
445
|
+
const tag = parts[0];
|
|
446
|
+
const classes = parts.slice(1);
|
|
447
|
+
return { tag, classes };
|
|
448
|
+
}
|
|
449
|
+
function addAnnotationComment(node, originalTag, classNames) {
|
|
450
|
+
const text = ` ${createAnnotation(originalTag, classNames)} `;
|
|
451
|
+
if (!node.leadingComments) {
|
|
452
|
+
node.leadingComments = [];
|
|
453
|
+
}
|
|
454
|
+
node.leadingComments.push({
|
|
455
|
+
type: "CommentBlock",
|
|
456
|
+
value: text
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
function readAnnotationComment(node) {
|
|
460
|
+
if (!node.leadingComments) return null;
|
|
461
|
+
for (let i = node.leadingComments.length - 1; i >= 0; i--) {
|
|
462
|
+
const comment = node.leadingComments[i];
|
|
463
|
+
const parsed = parseAnnotation(comment.value);
|
|
464
|
+
if (parsed) {
|
|
465
|
+
node.leadingComments.splice(i, 1);
|
|
466
|
+
return parsed;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return null;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// src/codemod/forward.ts
|
|
473
|
+
function createForwardVisitor(imports, options = {}) {
|
|
474
|
+
const { annotate = true } = options;
|
|
475
|
+
return {
|
|
476
|
+
JSXElement(path) {
|
|
477
|
+
const opening = path.node.openingElement;
|
|
478
|
+
const tag = opening.name;
|
|
479
|
+
if (tag.type !== "JSXIdentifier") return;
|
|
480
|
+
const tagName = tag.name;
|
|
481
|
+
const classAttr = opening.attributes.find(
|
|
482
|
+
(a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "className"
|
|
483
|
+
);
|
|
484
|
+
const staticClasses = extractStaticClasses(classAttr?.value ?? null);
|
|
485
|
+
const result = classifyElement(tagName, staticClasses);
|
|
486
|
+
if (!result) return;
|
|
487
|
+
if (annotate) {
|
|
488
|
+
addAnnotationComment(path.node, result.originalTag, staticClasses);
|
|
489
|
+
}
|
|
490
|
+
tag.name = result.atom;
|
|
491
|
+
if (path.node.closingElement) {
|
|
492
|
+
path.node.closingElement.name.name = result.atom;
|
|
493
|
+
}
|
|
494
|
+
imports.addAtom(result.atom);
|
|
495
|
+
if (classAttr && result.removeClasses.length > 0) {
|
|
496
|
+
updateClassName(classAttr, result.removeClasses, "remove");
|
|
497
|
+
}
|
|
498
|
+
for (const [key, value] of Object.entries(result.addProps)) {
|
|
499
|
+
const existing = opening.attributes.find(
|
|
500
|
+
(a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === key
|
|
501
|
+
);
|
|
502
|
+
if (!existing) {
|
|
503
|
+
if (typeof value === "boolean") {
|
|
504
|
+
opening.attributes.push(
|
|
505
|
+
t2.jsxAttribute(
|
|
506
|
+
t2.jsxIdentifier(key),
|
|
507
|
+
value ? null : t2.jsxExpressionContainer(t2.booleanLiteral(false))
|
|
508
|
+
)
|
|
509
|
+
);
|
|
510
|
+
} else {
|
|
511
|
+
opening.attributes.push(
|
|
512
|
+
t2.jsxAttribute(t2.jsxIdentifier(key), t2.stringLiteral(value))
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
renameEventHandlers(opening, "forward");
|
|
518
|
+
},
|
|
519
|
+
JSXExpressionContainer(path) {
|
|
520
|
+
if (!path.parentPath.isJSXElement() && !path.parentPath.isJSXFragment()) return;
|
|
521
|
+
const expr = path.node.expression;
|
|
522
|
+
if (expr.type === "JSXEmptyExpression") return;
|
|
523
|
+
const conditional = matchConditionalRender(expr);
|
|
524
|
+
if (conditional) {
|
|
525
|
+
const showEl = createShowElement(conditional.condition, conditional.consequent);
|
|
526
|
+
imports.addAtom("Show");
|
|
527
|
+
path.replaceWith(showEl);
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
const ternary = matchTernaryRender(expr);
|
|
531
|
+
if (ternary) {
|
|
532
|
+
const show1 = createShowElement(ternary.condition, ternary.consequent);
|
|
533
|
+
const negated = t2.unaryExpression("!", ternary.condition);
|
|
534
|
+
const show2 = createShowElement(negated, ternary.alternate);
|
|
535
|
+
imports.addAtom("Show");
|
|
536
|
+
path.replaceWithMultiple([show1, show2]);
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
const mapRender = matchMapRender(expr);
|
|
540
|
+
if (mapRender) {
|
|
541
|
+
const paramName = mapRender.param.type === "Identifier" ? mapRender.param.name : "item";
|
|
542
|
+
const eachEl = createEachElement(
|
|
543
|
+
mapRender.array,
|
|
544
|
+
paramName,
|
|
545
|
+
mapRender.body,
|
|
546
|
+
mapRender.keyExpression,
|
|
547
|
+
mapRender.indexParam
|
|
548
|
+
);
|
|
549
|
+
imports.addAtom("Each");
|
|
550
|
+
path.replaceWith(eachEl);
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
function updateClassName(attr, classes, operation) {
|
|
557
|
+
if (!attr.value) return;
|
|
558
|
+
if (attr.value.type === "StringLiteral") {
|
|
559
|
+
const result = operation === "remove" ? removeClassesFromString(attr.value.value, classes) : attr.value.value;
|
|
560
|
+
if (result.trim()) {
|
|
561
|
+
attr.value = t2.stringLiteral(result);
|
|
562
|
+
} else {
|
|
563
|
+
attr.value = t2.stringLiteral("");
|
|
564
|
+
}
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
if (attr.value.type === "JSXExpressionContainer") {
|
|
568
|
+
const expr = attr.value.expression;
|
|
569
|
+
if (expr.type === "TemplateLiteral") {
|
|
570
|
+
for (const quasi of expr.quasis) {
|
|
571
|
+
if (operation === "remove") {
|
|
572
|
+
quasi.value.raw = removeClassesFromString(quasi.value.raw, classes);
|
|
573
|
+
quasi.value.cooked = removeClassesFromString(quasi.value.cooked ?? quasi.value.raw, classes);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
if (expr.type === "CallExpression" && expr.callee.type === "Identifier" && expr.callee.name === "cn" && expr.arguments.length > 0) {
|
|
579
|
+
const firstArg = expr.arguments[0];
|
|
580
|
+
if (firstArg.type === "StringLiteral" && operation === "remove") {
|
|
581
|
+
firstArg.value = removeClassesFromString(firstArg.value, classes);
|
|
582
|
+
}
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
function renameEventHandlers(opening, direction) {
|
|
588
|
+
const map = direction === "forward" ? { onClick: "onPress", onChange: "onChange", onSubmit: "onSubmit" } : { onPress: "onClick" };
|
|
589
|
+
for (const attr of opening.attributes) {
|
|
590
|
+
if (attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name in map) {
|
|
591
|
+
attr.name.name = map[attr.name.name];
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// src/codemod/reverse.ts
|
|
597
|
+
import * as t3 from "@babel/types";
|
|
598
|
+
function createReverseVisitor(imports, _options = {}) {
|
|
599
|
+
return {
|
|
600
|
+
JSXElement: {
|
|
601
|
+
// Use exit so children are processed first (bottom-up)
|
|
602
|
+
exit(path) {
|
|
603
|
+
const opening = path.node.openingElement;
|
|
604
|
+
const tag = opening.name;
|
|
605
|
+
if (tag.type !== "JSXIdentifier") return;
|
|
606
|
+
const atomName = tag.name;
|
|
607
|
+
const showMatch = matchShowElement(path.node);
|
|
608
|
+
if (showMatch) {
|
|
609
|
+
const replacement = showToConditional(showMatch.condition, showMatch.children);
|
|
610
|
+
imports.removeAtom("Show");
|
|
611
|
+
path.replaceWith(replacement);
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
const eachMatch = matchEachElement(path.node);
|
|
615
|
+
if (eachMatch) {
|
|
616
|
+
const replacement = eachToMap(
|
|
617
|
+
eachMatch.items,
|
|
618
|
+
eachMatch.paramName,
|
|
619
|
+
eachMatch.keyExpression,
|
|
620
|
+
eachMatch.children
|
|
621
|
+
);
|
|
622
|
+
imports.removeAtom("Each");
|
|
623
|
+
path.replaceWith(replacement);
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
const annotation = readAnnotationComment(path.node);
|
|
627
|
+
const propsMap = /* @__PURE__ */ new Map();
|
|
628
|
+
for (const attr of opening.attributes) {
|
|
629
|
+
if (attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier") {
|
|
630
|
+
propsMap.set(attr.name.name, attr.value ?? t3.booleanLiteral(true));
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
const result = reverseClassifyAtom(atomName, propsMap);
|
|
634
|
+
if (!result) return;
|
|
635
|
+
const targetTag = annotation?.tag ?? result.htmlTag;
|
|
636
|
+
tag.name = targetTag;
|
|
637
|
+
if (path.node.closingElement) {
|
|
638
|
+
path.node.closingElement.name.name = targetTag;
|
|
639
|
+
}
|
|
640
|
+
imports.removeAtom(atomName);
|
|
641
|
+
if (result.removeProps.length > 0) {
|
|
642
|
+
opening.attributes = opening.attributes.filter(
|
|
643
|
+
(attr) => !(attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && result.removeProps.includes(attr.name.name))
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
if (result.injectClasses.length > 0) {
|
|
647
|
+
injectClasses(opening, result.injectClasses, annotation?.classes);
|
|
648
|
+
}
|
|
649
|
+
renameEventHandlers2(opening);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
function injectClasses(opening, injectClasses2, _annotationClasses) {
|
|
655
|
+
const classAttr = opening.attributes.find(
|
|
656
|
+
(a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "className"
|
|
657
|
+
);
|
|
658
|
+
if (classAttr && classAttr.value) {
|
|
659
|
+
if (classAttr.value.type === "StringLiteral") {
|
|
660
|
+
classAttr.value = t3.stringLiteral(
|
|
661
|
+
addClassesToString(classAttr.value.value, injectClasses2)
|
|
662
|
+
);
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
if (classAttr.value.type === "JSXExpressionContainer") {
|
|
666
|
+
const expr = classAttr.value.expression;
|
|
667
|
+
if (expr.type === "TemplateLiteral" && expr.quasis.length > 0) {
|
|
668
|
+
const first = expr.quasis[0];
|
|
669
|
+
first.value.raw = addClassesToString(first.value.raw, injectClasses2);
|
|
670
|
+
first.value.cooked = addClassesToString(first.value.cooked ?? first.value.raw, injectClasses2);
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
if (expr.type === "CallExpression" && expr.callee.type === "Identifier" && expr.callee.name === "cn" && expr.arguments.length > 0 && expr.arguments[0].type === "StringLiteral") {
|
|
674
|
+
expr.arguments[0] = t3.stringLiteral(
|
|
675
|
+
addClassesToString(expr.arguments[0].value, injectClasses2)
|
|
676
|
+
);
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
} else {
|
|
681
|
+
const classValue = injectClasses2.join(" ");
|
|
682
|
+
if (classValue) {
|
|
683
|
+
opening.attributes.push(
|
|
684
|
+
t3.jsxAttribute(
|
|
685
|
+
t3.jsxIdentifier("className"),
|
|
686
|
+
t3.stringLiteral(classValue)
|
|
687
|
+
)
|
|
688
|
+
);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
function renameEventHandlers2(opening) {
|
|
693
|
+
const map = { onPress: "onClick" };
|
|
694
|
+
for (const attr of opening.attributes) {
|
|
695
|
+
if (attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name in map) {
|
|
696
|
+
attr.name.name = map[attr.name.name];
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// src/codemod/state-transform.ts
|
|
702
|
+
import * as t4 from "@babel/types";
|
|
703
|
+
function createStateVisitor(mode) {
|
|
704
|
+
if (mode === "preserve") return {};
|
|
705
|
+
if (mode === "annotate") {
|
|
706
|
+
return {
|
|
707
|
+
VariableDeclaration(path) {
|
|
708
|
+
for (const decl of path.node.declarations) {
|
|
709
|
+
if (!isUseStateCall(decl.init)) continue;
|
|
710
|
+
if (hasFieldAnnotation(path.node)) continue;
|
|
711
|
+
const fieldType = inferFieldType(decl.init);
|
|
712
|
+
const comment = ` @mm-field type=${fieldType} `;
|
|
713
|
+
if (!path.node.trailingComments) {
|
|
714
|
+
path.node.trailingComments = [];
|
|
715
|
+
}
|
|
716
|
+
path.node.trailingComments.push({
|
|
717
|
+
type: "CommentLine",
|
|
718
|
+
value: comment
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
return {
|
|
725
|
+
VariableDeclaration(path) {
|
|
726
|
+
for (const decl of path.node.declarations) {
|
|
727
|
+
if (!isUseStateCall(decl.init)) continue;
|
|
728
|
+
const migrateAnnotation = getMigrateAnnotation(path.node);
|
|
729
|
+
if (!migrateAnnotation) continue;
|
|
730
|
+
if (migrateAnnotation.type === "query") {
|
|
731
|
+
if (decl.id.type !== "ArrayPattern" || decl.id.elements.length < 1)
|
|
732
|
+
continue;
|
|
733
|
+
const nameNode = decl.id.elements[0];
|
|
734
|
+
if (!nameNode || nameNode.type !== "Identifier") continue;
|
|
735
|
+
decl.id = nameNode;
|
|
736
|
+
decl.init = t4.callExpression(t4.identifier("useQuery"), [
|
|
737
|
+
t4.stringLiteral(migrateAnnotation.model || nameNode.name),
|
|
738
|
+
t4.objectExpression([
|
|
739
|
+
t4.objectProperty(
|
|
740
|
+
t4.identifier("where"),
|
|
741
|
+
t4.objectExpression([])
|
|
742
|
+
)
|
|
743
|
+
])
|
|
744
|
+
]);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
function isUseStateCall(node) {
|
|
751
|
+
return !!node && node.type === "CallExpression" && node.callee.type === "Identifier" && node.callee.name === "useState";
|
|
752
|
+
}
|
|
753
|
+
function hasFieldAnnotation(node) {
|
|
754
|
+
return !!node.trailingComments && node.trailingComments.some((c) => c.value.includes("@mm-field"));
|
|
755
|
+
}
|
|
756
|
+
function inferFieldType(call) {
|
|
757
|
+
if (call.arguments.length === 0) return "any";
|
|
758
|
+
const arg = call.arguments[0];
|
|
759
|
+
if (arg.type === "StringLiteral") return "text";
|
|
760
|
+
if (arg.type === "NumericLiteral") return "number";
|
|
761
|
+
if (arg.type === "BooleanLiteral") return "boolean";
|
|
762
|
+
if (arg.type === "ArrayExpression") return "list";
|
|
763
|
+
if (arg.type === "ObjectExpression") return "object";
|
|
764
|
+
if (arg.type === "NullLiteral") return "any";
|
|
765
|
+
return "any";
|
|
766
|
+
}
|
|
767
|
+
function getMigrateAnnotation(node) {
|
|
768
|
+
const comments = [
|
|
769
|
+
...node.leadingComments || [],
|
|
770
|
+
...node.trailingComments || []
|
|
771
|
+
];
|
|
772
|
+
for (const c of comments) {
|
|
773
|
+
const match = c.value.match(/@mm-migrate:\s*(query|mutation)(?:\s+(\w+))?/);
|
|
774
|
+
if (match) {
|
|
775
|
+
return {
|
|
776
|
+
type: match[1],
|
|
777
|
+
model: match[2]
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
return null;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
// src/codemod/import-manager.ts
|
|
785
|
+
import * as t5 from "@babel/types";
|
|
786
|
+
function createImportTracker() {
|
|
787
|
+
const used = /* @__PURE__ */ new Set();
|
|
788
|
+
const removed = /* @__PURE__ */ new Set();
|
|
789
|
+
return {
|
|
790
|
+
addAtom(name) {
|
|
791
|
+
used.add(name);
|
|
792
|
+
removed.delete(name);
|
|
793
|
+
},
|
|
794
|
+
removeAtom(name) {
|
|
795
|
+
removed.add(name);
|
|
796
|
+
},
|
|
797
|
+
getUsedAtoms() {
|
|
798
|
+
return used;
|
|
799
|
+
},
|
|
800
|
+
getRemovedAtoms() {
|
|
801
|
+
return removed;
|
|
802
|
+
}
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
function rewriteImportsForward(programPath, tracker) {
|
|
806
|
+
const usedAtoms = tracker.getUsedAtoms();
|
|
807
|
+
if (usedAtoms.size === 0) return;
|
|
808
|
+
let existingImport = null;
|
|
809
|
+
programPath.traverse({
|
|
810
|
+
ImportDeclaration(path) {
|
|
811
|
+
if (path.node.source.value === ATOM_IMPORT_SOURCE) {
|
|
812
|
+
existingImport = path;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
});
|
|
816
|
+
if (existingImport) {
|
|
817
|
+
const existing = new Set(
|
|
818
|
+
existingImport.node.specifiers.filter((s) => s.type === "ImportSpecifier").map((s) => s.imported.name)
|
|
819
|
+
);
|
|
820
|
+
for (const atom of usedAtoms) {
|
|
821
|
+
if (!existing.has(atom)) {
|
|
822
|
+
existingImport.node.specifiers.push(
|
|
823
|
+
t5.importSpecifier(t5.identifier(atom), t5.identifier(atom))
|
|
824
|
+
);
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
} else {
|
|
828
|
+
const specifiers = Array.from(usedAtoms).sort().map((name) => t5.importSpecifier(t5.identifier(name), t5.identifier(name)));
|
|
829
|
+
const importDecl = t5.importDeclaration(
|
|
830
|
+
specifiers,
|
|
831
|
+
t5.stringLiteral(ATOM_IMPORT_SOURCE)
|
|
832
|
+
);
|
|
833
|
+
const lastImport = programPath.get("body").filter((p) => p.isImportDeclaration()).pop();
|
|
834
|
+
if (lastImport) {
|
|
835
|
+
lastImport.insertAfter(importDecl);
|
|
836
|
+
} else {
|
|
837
|
+
programPath.get("body")[0].insertBefore(importDecl);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
function rewriteImportsReverse(programPath, tracker) {
|
|
842
|
+
const removed = tracker.getRemovedAtoms();
|
|
843
|
+
if (removed.size === 0) return;
|
|
844
|
+
programPath.traverse({
|
|
845
|
+
ImportDeclaration(path) {
|
|
846
|
+
if (path.node.source.value !== ATOM_IMPORT_SOURCE) return;
|
|
847
|
+
path.node.specifiers = path.node.specifiers.filter((spec) => {
|
|
848
|
+
if (spec.type !== "ImportSpecifier") return true;
|
|
849
|
+
const name = spec.imported.name;
|
|
850
|
+
return !removed.has(name);
|
|
851
|
+
});
|
|
852
|
+
if (path.node.specifiers.length === 0) {
|
|
853
|
+
path.remove();
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
});
|
|
857
|
+
let hasReactImport = false;
|
|
858
|
+
programPath.traverse({
|
|
859
|
+
ImportDeclaration(path) {
|
|
860
|
+
if (path.node.source.value === "react") {
|
|
861
|
+
hasReactImport = true;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
});
|
|
865
|
+
if (!hasReactImport) {
|
|
866
|
+
const reactImport = t5.importDeclaration(
|
|
867
|
+
[t5.importDefaultSpecifier(t5.identifier("React"))],
|
|
868
|
+
t5.stringLiteral("react")
|
|
869
|
+
);
|
|
870
|
+
programPath.get("body")[0].insertBefore(reactImport);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// src/codemod/index.ts
|
|
875
|
+
var codemodPlugin = declare(
|
|
876
|
+
(api, options) => {
|
|
877
|
+
api.assertVersion(7);
|
|
878
|
+
const direction = options.direction || "forward";
|
|
879
|
+
const stateMode = options.stateMode || "preserve";
|
|
880
|
+
return {
|
|
881
|
+
name: "mm-codemod",
|
|
882
|
+
visitor: {
|
|
883
|
+
Program: {
|
|
884
|
+
enter(programPath) {
|
|
885
|
+
const imports = createImportTracker();
|
|
886
|
+
const stateVisitor = createStateVisitor(stateMode);
|
|
887
|
+
if (direction === "forward") {
|
|
888
|
+
const forwardVisitor = createForwardVisitor(imports, {
|
|
889
|
+
annotate: options.annotate ?? true,
|
|
890
|
+
stateMode
|
|
891
|
+
});
|
|
892
|
+
programPath.traverse(stateVisitor);
|
|
893
|
+
programPath.traverse(forwardVisitor);
|
|
894
|
+
rewriteImportsForward(programPath, imports);
|
|
895
|
+
} else {
|
|
896
|
+
const reverseVisitor = createReverseVisitor(imports, {
|
|
897
|
+
stripAnnotations: !options.annotate
|
|
898
|
+
});
|
|
899
|
+
programPath.traverse(reverseVisitor);
|
|
900
|
+
rewriteImportsReverse(programPath, imports);
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
);
|
|
908
|
+
var codemod_default = codemodPlugin;
|
|
909
|
+
function transform(source, options) {
|
|
910
|
+
const babelOptions = {
|
|
911
|
+
filename: "file.tsx",
|
|
912
|
+
plugins: [
|
|
913
|
+
["@babel/plugin-syntax-typescript", { isTSX: true }],
|
|
914
|
+
[codemodPlugin, options]
|
|
915
|
+
],
|
|
916
|
+
// Preserve formatting as much as possible
|
|
917
|
+
retainLines: true,
|
|
918
|
+
compact: false,
|
|
919
|
+
sourceMaps: true
|
|
920
|
+
};
|
|
921
|
+
const result = transformSync(source, babelOptions);
|
|
922
|
+
if (!result || !result.code) {
|
|
923
|
+
throw new Error("Codemod transform produced no output");
|
|
924
|
+
}
|
|
925
|
+
return { code: result.code, map: result.map };
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
export {
|
|
929
|
+
FORWARD_RULES,
|
|
930
|
+
REVERSE_RULES,
|
|
931
|
+
ATOM_IMPORT_SOURCE,
|
|
932
|
+
extractStaticClasses,
|
|
933
|
+
classifyElement,
|
|
934
|
+
reverseClassifyAtom,
|
|
935
|
+
createImportTracker,
|
|
936
|
+
codemodPlugin,
|
|
937
|
+
codemod_default,
|
|
938
|
+
transform
|
|
939
|
+
};
|