@constela/compiler 0.14.3 → 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 +141 -50
  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
  }
@@ -2437,12 +2527,12 @@ function transformLocalState2(localState) {
2437
2527
  }
2438
2528
  return result;
2439
2529
  }
2440
- function transformLocalActions2(localActions) {
2530
+ function transformLocalActions2(localActions, ctx) {
2441
2531
  const result = {};
2442
2532
  for (const action of localActions) {
2443
2533
  result[action.name] = {
2444
2534
  name: action.name,
2445
- steps: action.steps.map(transformActionStep2)
2535
+ steps: action.steps.map((s) => transformActionStep2(s, ctx))
2446
2536
  };
2447
2537
  }
2448
2538
  return result;
@@ -2451,7 +2541,7 @@ function transformActions2(actions) {
2451
2541
  if (!actions) return [];
2452
2542
  return actions.map((action) => ({
2453
2543
  name: action.name,
2454
- steps: action.steps.map(transformActionStep2)
2544
+ steps: action.steps.map((s) => transformActionStep2(s))
2455
2545
  }));
2456
2546
  }
2457
2547
  function transformViewNode2(node, ctx) {
@@ -2470,7 +2560,7 @@ function transformViewNode2(node, ctx) {
2470
2560
  action: value.action
2471
2561
  };
2472
2562
  } else {
2473
- result.props[key] = value;
2563
+ result.props[key] = transformExpression2(value, ctx);
2474
2564
  }
2475
2565
  }
2476
2566
  }
@@ -2484,12 +2574,12 @@ function transformViewNode2(node, ctx) {
2484
2574
  case "text":
2485
2575
  return {
2486
2576
  kind: "text",
2487
- value: node.value
2577
+ value: transformExpression2(node.value, ctx)
2488
2578
  };
2489
2579
  case "if": {
2490
2580
  const result = {
2491
2581
  kind: "if",
2492
- condition: node.condition,
2582
+ condition: transformExpression2(node.condition, ctx),
2493
2583
  then: transformViewNode2(node.then, ctx)
2494
2584
  };
2495
2585
  if (node.else) {
@@ -2500,7 +2590,7 @@ function transformViewNode2(node, ctx) {
2500
2590
  case "each":
2501
2591
  return {
2502
2592
  kind: "each",
2503
- items: node.items,
2593
+ items: transformExpression2(node.items, ctx),
2504
2594
  as: node.as,
2505
2595
  body: transformViewNode2(node.body, ctx)
2506
2596
  };
@@ -2518,7 +2608,7 @@ function transformViewNode2(node, ctx) {
2518
2608
  const params = {};
2519
2609
  if (componentNode.props) {
2520
2610
  for (const [name, expr] of Object.entries(componentNode.props)) {
2521
- params[name] = transformExpression2(expr);
2611
+ params[name] = transformExpression2(expr, ctx);
2522
2612
  }
2523
2613
  }
2524
2614
  const children = [];
@@ -2537,7 +2627,7 @@ function transformViewNode2(node, ctx) {
2537
2627
  return {
2538
2628
  kind: "localState",
2539
2629
  state: transformLocalState2(def.localState),
2540
- actions: transformLocalActions2(def.localActions ?? []),
2630
+ actions: transformLocalActions2(def.localActions ?? [], newCtx),
2541
2631
  child: expandedView
2542
2632
  };
2543
2633
  }
@@ -2720,22 +2810,23 @@ function processNamedSlotsOnly(node, namedContent) {
2720
2810
  }
2721
2811
  return node;
2722
2812
  }
2723
- function expandComponentNode(node, components, defaultContent, namedContent) {
2813
+ function expandComponentNode(node, components, defaultContent, namedContent, parentCtx) {
2724
2814
  const componentNode = node;
2725
2815
  const def = components[componentNode.name];
2726
2816
  if (!def) {
2727
2817
  return { kind: "element", tag: "div" };
2728
2818
  }
2819
+ const baseCtx = parentCtx ?? { components };
2729
2820
  const params = {};
2730
2821
  if (componentNode.props) {
2731
2822
  for (const [name, expr] of Object.entries(componentNode.props)) {
2732
- params[name] = transformExpression2(expr);
2823
+ params[name] = transformExpression2(expr, baseCtx);
2733
2824
  }
2734
2825
  }
2735
2826
  const children = [];
2736
2827
  if (componentNode.children && componentNode.children.length > 0) {
2737
2828
  for (const child of componentNode.children) {
2738
- const transformedChild = transformViewNode2(child, { components });
2829
+ const transformedChild = transformViewNode2(child, baseCtx);
2739
2830
  children.push(replaceSlots(transformedChild, defaultContent, namedContent, components));
2740
2831
  }
2741
2832
  }
@@ -2750,7 +2841,7 @@ function expandComponentNode(node, components, defaultContent, namedContent) {
2750
2841
  return {
2751
2842
  kind: "localState",
2752
2843
  state: transformLocalState2(def.localState),
2753
- actions: transformLocalActions2(def.localActions ?? []),
2844
+ actions: transformLocalActions2(def.localActions ?? [], newCtx),
2754
2845
  child: processedView
2755
2846
  };
2756
2847
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constela/compiler",
3
- "version": "0.14.3",
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",