@constela/compiler 0.14.2 → 0.14.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.
Files changed (2) hide show
  1. package/dist/index.js +230 -56
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2187,12 +2187,17 @@ function transformState2(state) {
2187
2187
  }
2188
2188
  return result;
2189
2189
  }
2190
- function transformExpression2(expr) {
2190
+ function transformExpression2(expr, ctx) {
2191
2191
  switch (expr.expr) {
2192
2192
  case "lit":
2193
2193
  return { expr: "lit", value: expr.value };
2194
- case "state":
2195
- return { expr: "state", name: expr.name };
2194
+ case "state": {
2195
+ const stateExpr = { expr: "state", name: expr.name };
2196
+ if (expr.path) {
2197
+ stateExpr.path = expr.path;
2198
+ }
2199
+ return stateExpr;
2200
+ }
2196
2201
  case "var": {
2197
2202
  const varExpr = { expr: "var", name: expr.name };
2198
2203
  if (expr.path) {
@@ -2204,25 +2209,25 @@ function transformExpression2(expr) {
2204
2209
  return {
2205
2210
  expr: "bin",
2206
2211
  op: expr.op,
2207
- left: transformExpression2(expr.left),
2208
- right: transformExpression2(expr.right)
2212
+ left: transformExpression2(expr.left, ctx),
2213
+ right: transformExpression2(expr.right, ctx)
2209
2214
  };
2210
2215
  case "not":
2211
2216
  return {
2212
2217
  expr: "not",
2213
- operand: transformExpression2(expr.operand)
2218
+ operand: transformExpression2(expr.operand, ctx)
2214
2219
  };
2215
2220
  case "cond":
2216
2221
  return {
2217
2222
  expr: "cond",
2218
- if: transformExpression2(expr.if),
2219
- then: transformExpression2(expr.then),
2220
- else: transformExpression2(expr.else)
2223
+ if: transformExpression2(expr.if, ctx),
2224
+ then: transformExpression2(expr.then, ctx),
2225
+ else: transformExpression2(expr.else, ctx)
2221
2226
  };
2222
2227
  case "get":
2223
2228
  return {
2224
2229
  expr: "get",
2225
- base: transformExpression2(expr.base),
2230
+ base: transformExpression2(expr.base, ctx),
2226
2231
  path: expr.path
2227
2232
  };
2228
2233
  case "route":
@@ -2246,6 +2251,60 @@ function transformExpression2(expr) {
2246
2251
  return dataExpr;
2247
2252
  }
2248
2253
  case "param": {
2254
+ if (ctx?.currentParams !== void 0) {
2255
+ const paramValue = ctx.currentParams[expr.name];
2256
+ if (paramValue !== void 0) {
2257
+ if (expr.path) {
2258
+ if (paramValue.expr === "var") {
2259
+ const varExpr = paramValue;
2260
+ const existingPath = varExpr.path;
2261
+ const resultPath = existingPath ? `${existingPath}.${expr.path}` : expr.path;
2262
+ return {
2263
+ expr: "var",
2264
+ name: varExpr.name,
2265
+ path: resultPath
2266
+ };
2267
+ }
2268
+ if (paramValue.expr === "state") {
2269
+ const stateExpr = paramValue;
2270
+ const existingPath = stateExpr.path;
2271
+ const resultPath = existingPath ? `${existingPath}.${expr.path}` : expr.path;
2272
+ return {
2273
+ expr: "state",
2274
+ name: stateExpr.name,
2275
+ path: resultPath
2276
+ };
2277
+ }
2278
+ if (paramValue.expr === "import") {
2279
+ const importExpr = paramValue;
2280
+ const existingPath = importExpr.path;
2281
+ const resultPath = existingPath ? `${existingPath}.${expr.path}` : expr.path;
2282
+ return {
2283
+ expr: "import",
2284
+ name: importExpr.name,
2285
+ path: resultPath
2286
+ };
2287
+ }
2288
+ if (paramValue.expr === "data") {
2289
+ const dataExpr = paramValue;
2290
+ const existingPath = dataExpr.path;
2291
+ const resultPath = existingPath ? `${existingPath}.${expr.path}` : expr.path;
2292
+ return {
2293
+ expr: "data",
2294
+ name: dataExpr.name,
2295
+ path: resultPath
2296
+ };
2297
+ }
2298
+ return {
2299
+ expr: "get",
2300
+ base: paramValue,
2301
+ path: expr.path
2302
+ };
2303
+ }
2304
+ return paramValue;
2305
+ }
2306
+ return { expr: "lit", value: null };
2307
+ }
2249
2308
  const paramExpr = { expr: "param", name: expr.name };
2250
2309
  if (expr.path) {
2251
2310
  paramExpr.path = expr.path;
@@ -2254,17 +2313,48 @@ function transformExpression2(expr) {
2254
2313
  }
2255
2314
  case "ref":
2256
2315
  return { expr: "ref", name: expr.name };
2316
+ case "call": {
2317
+ const callExpr = expr;
2318
+ const result = {
2319
+ expr: "call",
2320
+ target: transformExpression2(callExpr.target, ctx),
2321
+ method: callExpr.method
2322
+ };
2323
+ if (callExpr.args && callExpr.args.length > 0) {
2324
+ result.args = callExpr.args.map((arg) => transformExpression2(arg, ctx));
2325
+ }
2326
+ return result;
2327
+ }
2328
+ case "lambda": {
2329
+ const lambdaExpr = expr;
2330
+ const result = {
2331
+ expr: "lambda",
2332
+ param: lambdaExpr.param,
2333
+ body: transformExpression2(lambdaExpr.body, ctx)
2334
+ };
2335
+ if (lambdaExpr.index) {
2336
+ result.index = lambdaExpr.index;
2337
+ }
2338
+ return result;
2339
+ }
2340
+ case "array": {
2341
+ const arrayExpr = expr;
2342
+ return {
2343
+ expr: "array",
2344
+ elements: arrayExpr.elements.map((elem) => transformExpression2(elem, ctx))
2345
+ };
2346
+ }
2257
2347
  default:
2258
2348
  return { expr: "lit", value: null };
2259
2349
  }
2260
2350
  }
2261
- function transformActionStep2(step) {
2351
+ function transformActionStep2(step, ctx) {
2262
2352
  switch (step.do) {
2263
2353
  case "set":
2264
2354
  return {
2265
2355
  do: "set",
2266
2356
  target: step.target,
2267
- value: transformExpression2(step.value)
2357
+ value: transformExpression2(step.value, ctx)
2268
2358
  };
2269
2359
  case "update": {
2270
2360
  const updateStep = {
@@ -2273,35 +2363,35 @@ function transformActionStep2(step) {
2273
2363
  operation: step.operation
2274
2364
  };
2275
2365
  if (step.value) {
2276
- updateStep.value = transformExpression2(step.value);
2366
+ updateStep.value = transformExpression2(step.value, ctx);
2277
2367
  }
2278
2368
  if (step.index) {
2279
- updateStep.index = transformExpression2(step.index);
2369
+ updateStep.index = transformExpression2(step.index, ctx);
2280
2370
  }
2281
2371
  if (step.deleteCount) {
2282
- updateStep.deleteCount = transformExpression2(step.deleteCount);
2372
+ updateStep.deleteCount = transformExpression2(step.deleteCount, ctx);
2283
2373
  }
2284
2374
  return updateStep;
2285
2375
  }
2286
2376
  case "fetch": {
2287
2377
  const fetchStep = {
2288
2378
  do: "fetch",
2289
- url: transformExpression2(step.url)
2379
+ url: transformExpression2(step.url, ctx)
2290
2380
  };
2291
2381
  if (step.method) {
2292
2382
  fetchStep.method = step.method;
2293
2383
  }
2294
2384
  if (step.body) {
2295
- fetchStep.body = transformExpression2(step.body);
2385
+ fetchStep.body = transformExpression2(step.body, ctx);
2296
2386
  }
2297
2387
  if (step.result) {
2298
2388
  fetchStep.result = step.result;
2299
2389
  }
2300
2390
  if (step.onSuccess) {
2301
- fetchStep.onSuccess = step.onSuccess.map(transformActionStep2);
2391
+ fetchStep.onSuccess = step.onSuccess.map((s) => transformActionStep2(s, ctx));
2302
2392
  }
2303
2393
  if (step.onError) {
2304
- fetchStep.onError = step.onError.map(transformActionStep2);
2394
+ fetchStep.onError = step.onError.map((s) => transformActionStep2(s, ctx));
2305
2395
  }
2306
2396
  return fetchStep;
2307
2397
  }
@@ -2310,20 +2400,20 @@ function transformActionStep2(step) {
2310
2400
  const compiledStorageStep = {
2311
2401
  do: "storage",
2312
2402
  operation: storageStep.operation,
2313
- key: transformExpression2(storageStep.key),
2403
+ key: transformExpression2(storageStep.key, ctx),
2314
2404
  storage: storageStep.storage
2315
2405
  };
2316
2406
  if (storageStep.value) {
2317
- compiledStorageStep.value = transformExpression2(storageStep.value);
2407
+ compiledStorageStep.value = transformExpression2(storageStep.value, ctx);
2318
2408
  }
2319
2409
  if (storageStep.result) {
2320
2410
  compiledStorageStep.result = storageStep.result;
2321
2411
  }
2322
2412
  if (storageStep.onSuccess) {
2323
- compiledStorageStep.onSuccess = storageStep.onSuccess.map(transformActionStep2);
2413
+ compiledStorageStep.onSuccess = storageStep.onSuccess.map((s) => transformActionStep2(s, ctx));
2324
2414
  }
2325
2415
  if (storageStep.onError) {
2326
- compiledStorageStep.onError = storageStep.onError.map(transformActionStep2);
2416
+ compiledStorageStep.onError = storageStep.onError.map((s) => transformActionStep2(s, ctx));
2327
2417
  }
2328
2418
  return compiledStorageStep;
2329
2419
  }
@@ -2334,16 +2424,16 @@ function transformActionStep2(step) {
2334
2424
  operation: clipboardStep.operation
2335
2425
  };
2336
2426
  if (clipboardStep.value) {
2337
- compiledClipboardStep.value = transformExpression2(clipboardStep.value);
2427
+ compiledClipboardStep.value = transformExpression2(clipboardStep.value, ctx);
2338
2428
  }
2339
2429
  if (clipboardStep.result) {
2340
2430
  compiledClipboardStep.result = clipboardStep.result;
2341
2431
  }
2342
2432
  if (clipboardStep.onSuccess) {
2343
- compiledClipboardStep.onSuccess = clipboardStep.onSuccess.map(transformActionStep2);
2433
+ compiledClipboardStep.onSuccess = clipboardStep.onSuccess.map((s) => transformActionStep2(s, ctx));
2344
2434
  }
2345
2435
  if (clipboardStep.onError) {
2346
- compiledClipboardStep.onError = clipboardStep.onError.map(transformActionStep2);
2436
+ compiledClipboardStep.onError = clipboardStep.onError.map((s) => transformActionStep2(s, ctx));
2347
2437
  }
2348
2438
  return compiledClipboardStep;
2349
2439
  }
@@ -2351,7 +2441,7 @@ function transformActionStep2(step) {
2351
2441
  const navigateStep = step;
2352
2442
  const compiledNavigateStep = {
2353
2443
  do: "navigate",
2354
- url: transformExpression2(navigateStep.url)
2444
+ url: transformExpression2(navigateStep.url, ctx)
2355
2445
  };
2356
2446
  if (navigateStep.target) {
2357
2447
  compiledNavigateStep.target = navigateStep.target;
@@ -2369,10 +2459,10 @@ function transformActionStep2(step) {
2369
2459
  result: importStep.result
2370
2460
  };
2371
2461
  if (importStep.onSuccess) {
2372
- compiledImportStep.onSuccess = importStep.onSuccess.map(transformActionStep2);
2462
+ compiledImportStep.onSuccess = importStep.onSuccess.map((s) => transformActionStep2(s, ctx));
2373
2463
  }
2374
2464
  if (importStep.onError) {
2375
- compiledImportStep.onError = importStep.onError.map(transformActionStep2);
2465
+ compiledImportStep.onError = importStep.onError.map((s) => transformActionStep2(s, ctx));
2376
2466
  }
2377
2467
  return compiledImportStep;
2378
2468
  }
@@ -2380,19 +2470,19 @@ function transformActionStep2(step) {
2380
2470
  const callStep = step;
2381
2471
  const compiledCallStep = {
2382
2472
  do: "call",
2383
- target: transformExpression2(callStep.target)
2473
+ target: transformExpression2(callStep.target, ctx)
2384
2474
  };
2385
2475
  if (callStep.args) {
2386
- compiledCallStep.args = callStep.args.map((arg) => transformExpression2(arg));
2476
+ compiledCallStep.args = callStep.args.map((arg) => transformExpression2(arg, ctx));
2387
2477
  }
2388
2478
  if (callStep.result) {
2389
2479
  compiledCallStep.result = callStep.result;
2390
2480
  }
2391
2481
  if (callStep.onSuccess) {
2392
- compiledCallStep.onSuccess = callStep.onSuccess.map(transformActionStep2);
2482
+ compiledCallStep.onSuccess = callStep.onSuccess.map((s) => transformActionStep2(s, ctx));
2393
2483
  }
2394
2484
  if (callStep.onError) {
2395
- compiledCallStep.onError = callStep.onError.map(transformActionStep2);
2485
+ compiledCallStep.onError = callStep.onError.map((s) => transformActionStep2(s, ctx));
2396
2486
  }
2397
2487
  return compiledCallStep;
2398
2488
  }
@@ -2400,7 +2490,7 @@ function transformActionStep2(step) {
2400
2490
  const subscribeStep = step;
2401
2491
  return {
2402
2492
  do: "subscribe",
2403
- target: transformExpression2(subscribeStep.target),
2493
+ target: transformExpression2(subscribeStep.target, ctx),
2404
2494
  event: subscribeStep.event,
2405
2495
  action: subscribeStep.action
2406
2496
  };
@@ -2409,7 +2499,7 @@ function transformActionStep2(step) {
2409
2499
  const disposeStep = step;
2410
2500
  return {
2411
2501
  do: "dispose",
2412
- target: transformExpression2(disposeStep.target)
2502
+ target: transformExpression2(disposeStep.target, ctx)
2413
2503
  };
2414
2504
  }
2415
2505
  case "dom": {
@@ -2417,8 +2507,8 @@ function transformActionStep2(step) {
2417
2507
  return {
2418
2508
  do: "dom",
2419
2509
  operation: domStep.operation,
2420
- selector: transformExpression2(domStep.selector),
2421
- ...domStep.value && { value: transformExpression2(domStep.value) },
2510
+ selector: transformExpression2(domStep.selector, ctx),
2511
+ ...domStep.value && { value: transformExpression2(domStep.value, ctx) },
2422
2512
  ...domStep.attribute && { attribute: domStep.attribute }
2423
2513
  };
2424
2514
  }
@@ -2430,11 +2520,28 @@ function transformActionStep2(step) {
2430
2520
  };
2431
2521
  }
2432
2522
  }
2523
+ function transformLocalState2(localState) {
2524
+ const result = {};
2525
+ for (const [name, field] of Object.entries(localState)) {
2526
+ result[name] = { type: field.type, initial: field.initial };
2527
+ }
2528
+ return result;
2529
+ }
2530
+ function transformLocalActions2(localActions, ctx) {
2531
+ const result = {};
2532
+ for (const action of localActions) {
2533
+ result[action.name] = {
2534
+ name: action.name,
2535
+ steps: action.steps.map((s) => transformActionStep2(s, ctx))
2536
+ };
2537
+ }
2538
+ return result;
2539
+ }
2433
2540
  function transformActions2(actions) {
2434
2541
  if (!actions) return [];
2435
2542
  return actions.map((action) => ({
2436
2543
  name: action.name,
2437
- steps: action.steps.map(transformActionStep2)
2544
+ steps: action.steps.map((s) => transformActionStep2(s))
2438
2545
  }));
2439
2546
  }
2440
2547
  function transformViewNode2(node, ctx) {
@@ -2453,7 +2560,7 @@ function transformViewNode2(node, ctx) {
2453
2560
  action: value.action
2454
2561
  };
2455
2562
  } else {
2456
- result.props[key] = value;
2563
+ result.props[key] = transformExpression2(value, ctx);
2457
2564
  }
2458
2565
  }
2459
2566
  }
@@ -2467,12 +2574,12 @@ function transformViewNode2(node, ctx) {
2467
2574
  case "text":
2468
2575
  return {
2469
2576
  kind: "text",
2470
- value: node.value
2577
+ value: transformExpression2(node.value, ctx)
2471
2578
  };
2472
2579
  case "if": {
2473
2580
  const result = {
2474
2581
  kind: "if",
2475
- condition: node.condition,
2582
+ condition: transformExpression2(node.condition, ctx),
2476
2583
  then: transformViewNode2(node.then, ctx)
2477
2584
  };
2478
2585
  if (node.else) {
@@ -2483,7 +2590,7 @@ function transformViewNode2(node, ctx) {
2483
2590
  case "each":
2484
2591
  return {
2485
2592
  kind: "each",
2486
- items: node.items,
2593
+ items: transformExpression2(node.items, ctx),
2487
2594
  as: node.as,
2488
2595
  body: transformViewNode2(node.body, ctx)
2489
2596
  };
@@ -2493,11 +2600,38 @@ function transformViewNode2(node, ctx) {
2493
2600
  name: node.name
2494
2601
  };
2495
2602
  case "component": {
2496
- const def = ctx.components[node.name];
2497
- if (def) {
2498
- return transformViewNode2(def.view, ctx);
2603
+ const componentNode = node;
2604
+ const def = ctx.components[componentNode.name];
2605
+ if (!def) {
2606
+ return { kind: "element", tag: "div" };
2607
+ }
2608
+ const params = {};
2609
+ if (componentNode.props) {
2610
+ for (const [name, expr] of Object.entries(componentNode.props)) {
2611
+ params[name] = transformExpression2(expr, ctx);
2612
+ }
2499
2613
  }
2500
- return { kind: "element", tag: "div" };
2614
+ const children = [];
2615
+ if (componentNode.children && componentNode.children.length > 0) {
2616
+ for (const child of componentNode.children) {
2617
+ children.push(transformViewNode2(child, ctx));
2618
+ }
2619
+ }
2620
+ const newCtx = {
2621
+ ...ctx,
2622
+ currentParams: params,
2623
+ currentChildren: children
2624
+ };
2625
+ const expandedView = transformViewNode2(def.view, newCtx);
2626
+ if (def.localState && Object.keys(def.localState).length > 0) {
2627
+ return {
2628
+ kind: "localState",
2629
+ state: transformLocalState2(def.localState),
2630
+ actions: transformLocalActions2(def.localActions ?? [], newCtx),
2631
+ child: expandedView
2632
+ };
2633
+ }
2634
+ return expandedView;
2501
2635
  }
2502
2636
  case "markdown":
2503
2637
  return {
@@ -2676,7 +2810,44 @@ function processNamedSlotsOnly(node, namedContent) {
2676
2810
  }
2677
2811
  return node;
2678
2812
  }
2679
- function replaceSlots(node, defaultContent, namedContent) {
2813
+ function expandComponentNode(node, components, defaultContent, namedContent, parentCtx) {
2814
+ const componentNode = node;
2815
+ const def = components[componentNode.name];
2816
+ if (!def) {
2817
+ return { kind: "element", tag: "div" };
2818
+ }
2819
+ const baseCtx = parentCtx ?? { components };
2820
+ const params = {};
2821
+ if (componentNode.props) {
2822
+ for (const [name, expr] of Object.entries(componentNode.props)) {
2823
+ params[name] = transformExpression2(expr, baseCtx);
2824
+ }
2825
+ }
2826
+ const children = [];
2827
+ if (componentNode.children && componentNode.children.length > 0) {
2828
+ for (const child of componentNode.children) {
2829
+ const transformedChild = transformViewNode2(child, baseCtx);
2830
+ children.push(replaceSlots(transformedChild, defaultContent, namedContent, components));
2831
+ }
2832
+ }
2833
+ const newCtx = {
2834
+ components,
2835
+ currentParams: params,
2836
+ currentChildren: children
2837
+ };
2838
+ const expandedView = transformViewNode2(def.view, newCtx);
2839
+ const processedView = replaceSlots(expandedView, defaultContent, namedContent, components);
2840
+ if (def.localState && Object.keys(def.localState).length > 0) {
2841
+ return {
2842
+ kind: "localState",
2843
+ state: transformLocalState2(def.localState),
2844
+ actions: transformLocalActions2(def.localActions ?? [], newCtx),
2845
+ child: processedView
2846
+ };
2847
+ }
2848
+ return processedView;
2849
+ }
2850
+ function replaceSlots(node, defaultContent, namedContent, components) {
2680
2851
  if (node.kind === "slot") {
2681
2852
  const slotName = node.name;
2682
2853
  if (slotName && namedContent?.[slotName]) {
@@ -2688,10 +2859,13 @@ function replaceSlots(node, defaultContent, namedContent) {
2688
2859
  }
2689
2860
  return clonedDefault;
2690
2861
  }
2862
+ if (node.kind === "component" && components) {
2863
+ return expandComponentNode(node, components, defaultContent, namedContent);
2864
+ }
2691
2865
  if (node.kind === "element") {
2692
2866
  const children = node.children;
2693
2867
  if (children && children.length > 0) {
2694
- const newChildren = children.map((child) => replaceSlots(child, defaultContent, namedContent));
2868
+ const newChildren = children.map((child) => replaceSlots(child, defaultContent, namedContent, components));
2695
2869
  return {
2696
2870
  ...node,
2697
2871
  children: newChildren
@@ -2703,10 +2877,10 @@ function replaceSlots(node, defaultContent, namedContent) {
2703
2877
  const ifNode = node;
2704
2878
  const result = {
2705
2879
  ...node,
2706
- then: replaceSlots(ifNode.then, defaultContent, namedContent)
2880
+ then: replaceSlots(ifNode.then, defaultContent, namedContent, components)
2707
2881
  };
2708
2882
  if (ifNode.else) {
2709
- result.else = replaceSlots(ifNode.else, defaultContent, namedContent);
2883
+ result.else = replaceSlots(ifNode.else, defaultContent, namedContent, components);
2710
2884
  }
2711
2885
  return result;
2712
2886
  }
@@ -2714,7 +2888,7 @@ function replaceSlots(node, defaultContent, namedContent) {
2714
2888
  const eachNode = node;
2715
2889
  return {
2716
2890
  ...node,
2717
- body: replaceSlots(eachNode.body, defaultContent, namedContent)
2891
+ body: replaceSlots(eachNode.body, defaultContent, namedContent, components)
2718
2892
  };
2719
2893
  }
2720
2894
  return node;
@@ -2744,7 +2918,11 @@ function composeLayoutWithPage(layout, page, layoutParams, slots) {
2744
2918
  } else {
2745
2919
  namedContent = extractMdxSlotsFromImportData(page.importData);
2746
2920
  }
2747
- const composedView = replaceSlots(layoutView, page.view, namedContent);
2921
+ const mergedComponents = {
2922
+ ...layout.components || {},
2923
+ ...page.components || {}
2924
+ };
2925
+ const composedView = replaceSlots(layoutView, page.view, namedContent, mergedComponents);
2748
2926
  const mergedState = {};
2749
2927
  for (const [name, field] of Object.entries(page.state)) {
2750
2928
  mergedState[name] = field;
@@ -2778,10 +2956,6 @@ function composeLayoutWithPage(layout, page, layoutParams, slots) {
2778
2956
  mergedActions[action.name] = action;
2779
2957
  }
2780
2958
  }
2781
- const mergedComponents = {
2782
- ...layout.components || {},
2783
- ...page.components || {}
2784
- };
2785
2959
  const result = {
2786
2960
  version: "1.0",
2787
2961
  state: mergedState,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constela/compiler",
3
- "version": "0.14.2",
3
+ "version": "0.14.4",
4
4
  "description": "Compiler for Constela UI framework - AST to Program transformation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",