ReviewOS

also looking at this

stacks/stx

fix(includes): preserve {⁠{ }} in @include partials that define their own signals (#1758)

#1759
Merged glennmichael123 wants to merge fix/1758-mustache-in-include-partials into main
3 files +76 -6
packages/stx/test/includes/signal-partial-mustache.test.tsadded+56-0
Changes to packages/stx/test/includes/signal-partial-mustache.test.ts
@@ -0,0 +1,56 @@
1import { afterAll, beforeAll, describe, expect, it } from 'bun:test'
2import { processDirectives } from '../../src/process'
3import { cleanupTestDirs, createPartialFile, PARTIALS_DIR, setupTestDirs } from '../utils'
4
5// Regression: stacksjs/stx#1758
6//
7// An @include partial's <script> is stripped (includes.ts) BEFORE its body
8// expressions are processed, so processExpressions' usesSignalsInScript gate
9// can't see the partial's own state()/derived(). Without recovering that gate
10// from the partial's full content, `{⁠{ signalCall() }}` is evaluated
11// server-side (undefined → emptied) and the marker is consumed — the partial
12// renders permanently blank instead of binding on the client.
13//
14// The fix computes usesSignalsInScript on the FULL partial content and passes
15// it to processExpressions as { forceSignals }. These tests pin both the fix
16// (signal partials preserve {⁠{ }}) and that it does NOT over-preserve
17// (server-var / signal-free partials still evaluate server-side).
18//
19// Rendered via src `processDirectives` rather than Bun.build+stxPlugin so the
20// assertions exercise src, not a possibly-stale dist (see test-realm memory).
21describe('stx#1758: {⁠{ }} in @include partials with their own signals', () => {
22 beforeAll(setupTestDirs)
23 afterAll(cleanupTestDirs)
24
25 const opts = { debug: false, partialsDir: PARTIALS_DIR, componentsDir: PARTIALS_DIR } as any
26 const render = (tmpl: string): Promise<string> =>
27 processDirectives(tmpl, {}, 'page.stx', opts, new Set())
28
29 it('preserves {⁠{ signalCall() }} when the partial defines its own signals', async () => {
30 await createPartialFile(
31 'signal-counter.stx',
32 `<script client>\n const n = derived(() => 42)\n</script>\n<p class="counter">{⁠{ n() }}</p>`,
33 )
34 const out = await render(`<div>@include('signal-counter')</div>`)
35 // Must survive as a runtime marker, not be emptied server-side.
36 expect(out).toMatch(/\{\{\s*n\(\)\s*\}\}/)
37 })
38
39 it('still evaluates <script server> vars in a partial (no over-preservation)', async () => {
40 await createPartialFile(
41 'server-card.stx',
42 `<script server>\n const who = 'Alice'\n</script>\n<p class="server">{⁠{ who }}</p>`,
43 )
44 const out = await render(`<div>@include('server-card')</div>`)
45 expect(out).toContain('Alice')
46 expect(out).not.toMatch(/\{\{\s*who\s*\}\}/)
47 })
48
49 it('does not over-preserve {⁠{ }} in a signal-free partial', async () => {
50 await createPartialFile('dumb.stx', `<p class="dumb">{⁠{ mystery }}</p>`)
51 const out = await render(`<div>@include('dumb')</div>`)
52 // No signals anywhere → evaluated server-side (undefined → empty), not shipped
53 // to the client as a literal it cannot resolve.
54 expect(out).not.toMatch(/\{\{\s*mystery\s*\}\}/)
55 })
56})