@sprlab/wccompiler 0.16.2 → 0.16.3

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/lib/codegen.js +22 -5
  2. package/package.json +1 -1
package/lib/codegen.js CHANGED
@@ -57,6 +57,23 @@ function slotPropRef(source, signalNames, computedNames, propNames) {
57
57
  return `'${source}'`;
58
58
  }
59
59
 
60
+ /**
61
+ * Wrap an expression in parentheses if it contains a ternary operator.
62
+ * This prevents operator precedence issues when combining with ?? (nullish coalescing).
63
+ *
64
+ * @param {string} expr - Expression to potentially wrap
65
+ * @returns {string} - Wrapped expression if it contains ternary, otherwise unchanged
66
+ */
67
+ function wrapTernaryExpr(expr) {
68
+ const trimmed = expr.trim();
69
+ // Check if expression contains ternary operator (? followed by :)
70
+ // Simple heuristic: if it has both ? and :, wrap it
71
+ if (trimmed.includes('?') && trimmed.includes(':')) {
72
+ return `(${trimmed})`;
73
+ }
74
+ return trimmed;
75
+ }
76
+
60
77
  /**
61
78
  * Transform an expression by rewriting signal/computed variable references
62
79
  * to use `this._x()` / `this._c_x()` syntax for auto-unwrapping.
@@ -444,7 +461,7 @@ function generateItemSetup(lines, forBlock, itemVar, indexVar, propNames, signal
444
461
  lines.push(`${indent} ${nodeRef}.textContent = ${b.name} ?? '';`);
445
462
  } else {
446
463
  const expr = transformForExpr(b.name, itemVar, indexVar, propNames, signalNamesSet, computedNamesSet);
447
- lines.push(`${indent} __effect(() => { ${nodeRef}.textContent = ${expr} ?? ''; });`);
464
+ lines.push(`${indent} __effect(() => { ${nodeRef}.textContent = ${wrapTernaryExpr(expr)} ?? ''; });`);
448
465
  }
449
466
  }
450
467
 
@@ -650,7 +667,7 @@ function generateItemSetup(lines, forBlock, itemVar, indexVar, propNames, signal
650
667
  lines.push(`${indent} ${nodeRef}.textContent = ${b.name} ?? '';`);
651
668
  } else {
652
669
  const expr = transformForExpr(b.name, itemVar, indexVar, propNames, signalNamesSet, computedNamesSet);
653
- lines.push(`${indent} __effect(() => { ${nodeRef}.textContent = ${expr} ?? ''; });`);
670
+ lines.push(`${indent} __effect(() => { ${nodeRef}.textContent = ${wrapTernaryExpr(expr)} ?? ''; });`);
654
671
  }
655
672
  }
656
673
 
@@ -779,7 +796,7 @@ function generateNestedItemSetup(lines, innerFor, outerItemVar, outerIndexVar, i
779
796
  lines.push(`${indent}${nodeRef}.textContent = ${b.name} ?? '';`);
780
797
  } else {
781
798
  const expr = transformNested(b.name);
782
- lines.push(`${indent}__effect(() => { ${nodeRef}.textContent = ${expr} ?? ''; });`);
799
+ lines.push(`${indent}__effect(() => { ${nodeRef}.textContent = ${wrapTernaryExpr(expr)} ?? ''; });`);
783
800
  }
784
801
  }
785
802
 
@@ -1328,7 +1345,7 @@ export function generateComponent(parseResult, options = {}) {
1328
1345
  ref = transformExpr(exprWithParens, signalNames, computedNames, propsObjectName, propNames, emitsObjectName, constantNames, methodNames, modelVarMap);
1329
1346
  }
1330
1347
  lines.push(' this.__disposers.push(__effect(() => {');
1331
- lines.push(` this.${b.varName}.textContent = ${ref} ?? '';`);
1348
+ lines.push(` this.${b.varName}.textContent = ${wrapTernaryExpr(ref)} ?? '';`);
1332
1349
  lines.push(' }));');
1333
1350
  }
1334
1351
  }
@@ -2016,7 +2033,7 @@ export function generateComponent(parseResult, options = {}) {
2016
2033
  } else {
2017
2034
  ref = transformExpr(b.name, signalNames, computedNames, propsObjectName, propNames, emitsObjectName, constantNames, methodNames, modelVarMap);
2018
2035
  }
2019
- lines.push(` __effect(() => { ${b.varName}.textContent = ${ref} ?? ''; });`);
2036
+ lines.push(` __effect(() => { ${b.varName}.textContent = ${wrapTernaryExpr(ref)} ?? ''; });`);
2020
2037
  }
2021
2038
  }
2022
2039
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sprlab/wccompiler",
3
- "version": "0.16.2",
3
+ "version": "0.16.3",
4
4
  "description": "Zero-runtime compiler that transforms .wcc single-file components into native web components with signals-based reactivity",
5
5
  "type": "module",
6
6
  "exports": {