ReviewOS

also looking at this

stacks/stx

fix(client): preserve client-signal {⁠{ }} + bind SVG classes via setAttribute

#1774
Closed glennmichael123 wants to merge fix/client-signal-interpolation-and-svg-bindclass into main
4 files +354 -293
packages/stx/src/expressions.tsmodified+17-6
Changes to packages/stx/src/expressions.ts
@@ -507,8 +507,15 @@ function expressionUsesOnlyContextVars(expr: string, context: Record<string, any
507507 if (!inTpl && ch === '`') { inTpl = true; tplDepth = 0; continue }
508508 if (inTpl) {
509509 if (ch === '`' && tplDepth === 0) { inTpl = false; continue }
510 if (ch === '$' && exprWithoutStrings[ti + 1] === '{') { tplDepth++; ti++; continue }
510 // Separator so adjacent interpolations `${a}${b}` (or ones split only by
511 // literal text, which we drop) don't fuse into one bogus identifier `ab`.
512 if (ch === '$' && exprWithoutStrings[ti + 1] === '{') { tplDepth++; ti++; tplResult += ' '; continue }
511513 if (ch === '}' && tplDepth > 0) { tplDepth--; continue }
514 // Inside a `${ ... }` interpolation we're looking at an EXPRESSION, not string
515 // text — keep it so its identifiers (e.g. a client-only signal `rating` in
516 // `` `${rating}/5` ``) are still detected. Only the literal text between
517 // interpolations (tplDepth === 0) is dropped.
518 if (tplDepth > 0) { tplResult += ch; continue }
512519 continue
513520 }
514521 tplResult += ch
@@ -775,11 +782,15 @@ export function processExpressions(template: string, context: Record<string, any
775782 try {
776783 const value = evaluateExpression(expr, context)
777784
778 // For signal-based templates: if evaluation returned undefined and the expression
779 // references variables NOT in the server-side context, preserve for client-side processing.
780 // evaluateExpression() internally catches ReferenceError and returns undefined,
781 // so we must check here rather than relying on the catch block below.
782 if (value === undefined && hasSignals && !expressionUsesOnlyContextVars(trimmedExpr, context)) {
785 // For signal-based templates: if the expression references variables NOT in the
786 // server-side context (i.e. client-only signals), preserve it for client-side
787 // processing REGARDLESS of the server-evaluated value. Gating on `value === undefined`
788 // only preserved BARE `{⁠{ signal }}` (which server-evaluates to undefined); a COMPOUND
789 // expression like `{⁠{ liked ? … : likes + ' people' }}` server-evaluates to a *defined*
790 // string ("undefined people…") and so was wrongly baked into static HTML instead of
791 // handed to the client — freezing it at the undefined-substituted text. Mirror the
792 // catch-block logic below, which already preserves on the same condition.
793 if (hasSignals && !expressionUsesOnlyContextVars(trimmedExpr, context)) {
783794 return match // Preserve {⁠{ expr }} for runtime evaluation
784795 }
785796