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