@sprlab/wccompiler 0.16.1 → 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 +27 -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
  }
@@ -1548,6 +1565,11 @@ export function generateComponent(parseResult, options = {}) {
1548
1565
  lines.push(` __val ? this.${ab.varName}.classList.add(__k) : this.${ab.varName}.classList.remove(__k);`);
1549
1566
  lines.push(' }');
1550
1567
  lines.push(' }));');
1568
+ } else if (ab.expression.trimStart().startsWith('[')) {
1569
+ // Array expression: join with spaces
1570
+ lines.push(' this.__disposers.push(__effect(() => {');
1571
+ lines.push(` this.${ab.varName}.className = (${expr}).join(' ');`);
1572
+ lines.push(' }));');
1551
1573
  } else {
1552
1574
  // String expression: set className
1553
1575
  lines.push(' this.__disposers.push(__effect(() => {');
@@ -2011,7 +2033,7 @@ export function generateComponent(parseResult, options = {}) {
2011
2033
  } else {
2012
2034
  ref = transformExpr(b.name, signalNames, computedNames, propsObjectName, propNames, emitsObjectName, constantNames, methodNames, modelVarMap);
2013
2035
  }
2014
- lines.push(` __effect(() => { ${b.varName}.textContent = ${ref} ?? ''; });`);
2036
+ lines.push(` __effect(() => { ${b.varName}.textContent = ${wrapTernaryExpr(ref)} ?? ''; });`);
2015
2037
  }
2016
2038
  }
2017
2039
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sprlab/wccompiler",
3
- "version": "0.16.1",
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": {