@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.
- package/dist/{chunk-NUPJYPFU.mjs → chunk-6LAII7OP.mjs} +79 -1
- package/dist/{chunk-UBDNXVL2.mjs → chunk-FPAMQXKB.mjs} +89 -3
- package/dist/cli/index.js +240 -9
- package/dist/cli/index.mjs +57 -9
- package/dist/dev-server.js +79 -1
- package/dist/dev-server.mjs +1 -1
- package/dist/index.js +79 -1
- package/dist/index.mjs +1 -1
- package/dist/{init-2CRSUGV5.mjs → init-CJCDWI33.mjs} +119 -0
- package/package.json +2 -2
- package/dist/chunk-26U577GB.mjs +0 -3465
- package/dist/chunk-2FBDFAX6.mjs +0 -2362
- package/dist/chunk-2L4QSMXG.mjs +0 -175
- package/dist/chunk-2REDFOER.mjs +0 -931
- package/dist/chunk-3USIFFE4.mjs +0 -2190
- package/dist/chunk-45YMGEVT.mjs +0 -186
- package/dist/chunk-46YKSHQR.mjs +0 -175
- package/dist/chunk-4FN2AISW.mjs +0 -148
- package/dist/chunk-4OPI5L7G.mjs +0 -2593
- package/dist/chunk-4RYTKOOJ.mjs +0 -186
- package/dist/chunk-4XHK6FWL.mjs +0 -2058
- package/dist/chunk-52XHYD2V.mjs +0 -214
- package/dist/chunk-5FTDWKHH.mjs +0 -244
- package/dist/chunk-5GUFFFGL.mjs +0 -148
- package/dist/chunk-5RKTOVR5.mjs +0 -244
- package/dist/chunk-5VNJ7C6N.mjs +0 -154
- package/dist/chunk-5YDMOO4X.mjs +0 -214
- package/dist/chunk-64ZWEMLJ.mjs +0 -148
- package/dist/chunk-6CQOAAMV.mjs +0 -1803
- package/dist/chunk-6SEVAAVT.mjs +0 -3516
- package/dist/chunk-6XP4KSWQ.mjs +0 -2190
- package/dist/chunk-6YLR5ZDA.mjs +0 -2829
- package/dist/chunk-72QWL54I.mjs +0 -175
- package/dist/chunk-7B4TRI7C.mjs +0 -4835
- package/dist/chunk-7JRAEFRB.mjs +0 -7510
- package/dist/chunk-7ZKGHTNB.mjs +0 -4952
- package/dist/chunk-AOGY2GK6.mjs +0 -3292
- package/dist/chunk-AXXUXRNA.mjs +0 -1434
- package/dist/chunk-CHLVKMQW.mjs +0 -175
- package/dist/chunk-CKGOZAB7.mjs +0 -939
- package/dist/chunk-D34RAZUX.mjs +0 -2223
- package/dist/chunk-DE3ZGQAC.mjs +0 -148
- package/dist/chunk-DMCY3BBG.mjs +0 -1933
- package/dist/chunk-DPIK3PJS.mjs +0 -244
- package/dist/chunk-E5IVH4RE.mjs +0 -186
- package/dist/chunk-E6FZNUR5.mjs +0 -4953
- package/dist/chunk-EJRBDQDP.mjs +0 -2607
- package/dist/chunk-ELO4TXJL.mjs +0 -186
- package/dist/chunk-EO6SYNCG.mjs +0 -175
- package/dist/chunk-EQGA6A6D.mjs +0 -121
- package/dist/chunk-EY2CSXYA.mjs +0 -822
- package/dist/chunk-FIQ65CDR.mjs +0 -925
- package/dist/chunk-FKRO52XH.mjs +0 -3446
- package/dist/chunk-FL4YAKU6.mjs +0 -4941
- package/dist/chunk-FOZXJFAR.mjs +0 -186
- package/dist/chunk-FX6URXWN.mjs +0 -186
- package/dist/chunk-FYT47UBU.mjs +0 -5076
- package/dist/chunk-G7SMOWOL.mjs +0 -828
- package/dist/chunk-GCLGPOJZ.mjs +0 -148
- package/dist/chunk-GGB4G5YY.mjs +0 -175
- package/dist/chunk-GXB4JOP7.mjs +0 -5072
- package/dist/chunk-HFXOUMTD.mjs +0 -175
- package/dist/chunk-HLRGCCIL.mjs +0 -4839
- package/dist/chunk-HOIUP6IF.mjs +0 -690
- package/dist/chunk-HRYR54PT.mjs +0 -175
- package/dist/chunk-HWIZ47US.mjs +0 -214
- package/dist/chunk-I3AU7GRD.mjs +0 -120
- package/dist/chunk-IB7MNPQL.mjs +0 -4953
- package/dist/chunk-ICSIHQCG.mjs +0 -148
- package/dist/chunk-ILFGMUVD.mjs +0 -1933
- package/dist/chunk-IPTX5MJU.mjs +0 -3223
- package/dist/chunk-ITGUSH2Z.mjs +0 -2783
- package/dist/chunk-IXHBCAMF.mjs +0 -3306
- package/dist/chunk-J7JUAHS4.mjs +0 -186
- package/dist/chunk-J7TWJ3TM.mjs +0 -2784
- package/dist/chunk-JDPLDGVF.mjs +0 -4810
- package/dist/chunk-JLA5VNQ3.mjs +0 -186
- package/dist/chunk-JQLWFCTM.mjs +0 -214
- package/dist/chunk-K53XP2DL.mjs +0 -148
- package/dist/chunk-K5HX2SVL.mjs +0 -1902
- package/dist/chunk-KFGYOOVS.mjs +0 -214
- package/dist/chunk-KFJJCQAL.mjs +0 -148
- package/dist/chunk-KFVVOS5N.mjs +0 -925
- package/dist/chunk-KJUIIEQE.mjs +0 -186
- package/dist/chunk-KNWTHRVQ.mjs +0 -175
- package/dist/chunk-KSG4XSZF.mjs +0 -175
- package/dist/chunk-L2OZ4CDV.mjs +0 -113
- package/dist/chunk-LF5N6DOU.mjs +0 -175
- package/dist/chunk-LJQCM2IM.mjs +0 -214
- package/dist/chunk-MIZV3TAN.mjs +0 -3293
- package/dist/chunk-NKKLQE5V.mjs +0 -148
- package/dist/chunk-NOW23XFZ.mjs +0 -186
- package/dist/chunk-NRXQKQ74.mjs +0 -148
- package/dist/chunk-NTB7OEX2.mjs +0 -2918
- package/dist/chunk-NW6555WJ.mjs +0 -186
- package/dist/chunk-O4AUS7EU.mjs +0 -148
- package/dist/chunk-OMZE6VLQ.mjs +0 -214
- package/dist/chunk-OPJKP747.mjs +0 -7506
- package/dist/chunk-OWI6XWCD.mjs +0 -3375
- package/dist/chunk-P4BR7WVO.mjs +0 -2190
- package/dist/chunk-PRUMNNDI.mjs +0 -3192
- package/dist/chunk-QQHVYH2X.mjs +0 -244
- package/dist/chunk-QTBD5B3F.mjs +0 -148
- package/dist/chunk-R57T26RR.mjs +0 -734
- package/dist/chunk-S5QLWLLT.mjs +0 -186
- package/dist/chunk-SCWGT2FY.mjs +0 -2190
- package/dist/chunk-SKSDPPNT.mjs +0 -3788
- package/dist/chunk-SMKJUSB3.mjs +0 -2190
- package/dist/chunk-SP2YUS33.mjs +0 -186
- package/dist/chunk-SU4E6E7B.mjs +0 -3153
- package/dist/chunk-SYUUKW5A.mjs +0 -3379
- package/dist/chunk-THFYE5ZX.mjs +0 -244
- package/dist/chunk-UDDTWG5J.mjs +0 -734
- package/dist/chunk-UL2XZEMA.mjs +0 -3128
- package/dist/chunk-VCAY2KGM.mjs +0 -175
- package/dist/chunk-VLTKQDJ3.mjs +0 -244
- package/dist/chunk-WBYMW4NQ.mjs +0 -3450
- package/dist/chunk-WECAV6QB.mjs +0 -148
- package/dist/chunk-WMKBXUCE.mjs +0 -3228
- package/dist/chunk-XAJ5BKKL.mjs +0 -4947
- package/dist/chunk-XG2X7AEA.mjs +0 -175
- package/dist/chunk-XG7Z23NQ.mjs +0 -148
- package/dist/chunk-XMWUHQVV.mjs +0 -939
- package/dist/chunk-XWZAOCQ7.mjs +0 -2607
- package/dist/chunk-XZNEDRGN.mjs +0 -3876
- package/dist/chunk-Y6FXYEAI.mjs +0 -10
- package/dist/chunk-Y6MA7ULW.mjs +0 -148
- package/dist/chunk-YFS6JMYO.mjs +0 -3342
- package/dist/chunk-YMS7Q7LG.mjs +0 -214
- package/dist/chunk-Z2G5RZ4H.mjs +0 -186
- package/dist/chunk-Z6AIQ4KL.mjs +0 -113
- package/dist/chunk-ZA37XTGA.mjs +0 -175
- package/dist/chunk-ZE3KCHBM.mjs +0 -2918
- package/dist/config-PL24KEWL.mjs +0 -219
- package/dist/dev-server-Bs_sz2DG.d.mts +0 -111
- package/dist/dev-server-Bs_sz2DG.d.ts +0 -111
- package/dist/dev-server-CjoufJ-u.d.mts +0 -109
- package/dist/dev-server-CjoufJ-u.d.ts +0 -109
- package/dist/dev-server-RmGHIntF.d.mts +0 -113
- package/dist/dev-server-RmGHIntF.d.ts +0 -113
- package/dist/engine-binary-QQUDACBJ.mjs +0 -455
- package/dist/envelope-DD7v0v6E.d.mts +0 -265
- package/dist/envelope-DD7v0v6E.d.ts +0 -265
- package/dist/envelope-vCVjrHlo.d.mts +0 -265
- package/dist/envelope-vCVjrHlo.d.ts +0 -265
- package/dist/index-B5gSgvnd.d.mts +0 -44
- package/dist/index-B5gSgvnd.d.ts +0 -44
- package/dist/index-Bs0MnR54.d.mts +0 -103
- package/dist/index-Bs0MnR54.d.ts +0 -103
- package/dist/index-DR0nNc_f.d.mts +0 -101
- package/dist/index-DR0nNc_f.d.ts +0 -101
- package/dist/index-revho_gS.d.mts +0 -104
- package/dist/index-revho_gS.d.ts +0 -104
- package/dist/init-7FJENUDK.mjs +0 -407
- package/dist/init-7JQMAAXS.mjs +0 -363
- package/dist/init-DQDX3QK6.mjs +0 -369
- package/dist/init-EHO4VQ22.mjs +0 -369
- package/dist/init-IXEE2RCF.mjs +0 -340
- package/dist/init-UC3FWPIW.mjs +0 -367
- package/dist/init-UNSMVKIK.mjs +0 -366
- package/dist/init-UNV5XIDE.mjs +0 -367
- package/dist/project-compiler-2P4N4DR7.mjs +0 -10
- package/dist/project-compiler-D2LCC27O.mjs +0 -10
- package/dist/project-compiler-EGJUTAJU.mjs +0 -10
- package/dist/project-compiler-EJ3GANJE.mjs +0 -10
- package/dist/project-compiler-LOQKVRZJ.mjs +0 -10
- package/dist/project-compiler-NNK32MPG.mjs +0 -10
- package/dist/project-compiler-OP2VVGJQ.mjs +0 -10
- package/dist/project-compiler-RQ6OQKRM.mjs +0 -10
- package/dist/project-compiler-VFR6CSDX.mjs +0 -10
- package/dist/project-compiler-VWNNCHGO.mjs +0 -10
- package/dist/project-compiler-XVAAU4C5.mjs +0 -10
- package/dist/project-compiler-YES5FGMD.mjs +0 -10
- package/dist/project-compiler-ZKMQDLGU.mjs +0 -10
- package/dist/project-decompiler-5GY2KSG4.mjs +0 -7
- package/dist/project-decompiler-FLXCEJHS.mjs +0 -7
- package/dist/project-decompiler-US7GAVIC.mjs +0 -7
- package/dist/project-decompiler-VLPR22QF.mjs +0 -7
- package/dist/pull-A2QUHW4K.mjs +0 -109
- package/dist/pull-FUS5QYZS.mjs +0 -109
- package/dist/pull-JBEQWVPE.mjs +0 -109
- package/dist/pull-LD5ENLGY.mjs +0 -109
- package/dist/pull-P44LDRWB.mjs +0 -109
- package/dist/verify-BYHUKARQ.mjs +0 -1833
- package/dist/verify-SEIXUGN4.mjs +0 -1833
package/dist/chunk-CKGOZAB7.mjs
DELETED
|
@@ -1,939 +0,0 @@
|
|
|
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 = "@mindmatrix/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
|
-
};
|