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