Skip to content

Commit 2b93a58

Browse files
committed
fix: keep style-ref grafts from hiding return-JSX lowering
Insert class-map setup as siblings of list-position returns so compileReturnJsxFunction and ssrCompileBody still see them. Wrap only unbraced arms, switch cases, and @{ } fall-through, where a shared scope would redeclare const __styleMap.
1 parent 7ca982f commit 2b93a58

3 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎packages/octane/src/compiler/style-scopes.js‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,11 @@ function isNestedFunctionType(type) {
226226
}
227227

228228
/**
229-
* Each graft owns a block so `const __styleMap` is not redeclared in a
230-
* shared scope (unbraced `switch` cases, or a `@{ }` return plus the
231-
* fall-through append).
229+
* Wrap setup in a block so `const __styleMap` is block-scoped. Needed when
230+
* the graft is a single statement (unbraced if/for/while) or shares a scope
231+
* with another copy (unbraced `switch` cases, `@{ }` fall-through append).
232+
* List-position function-body returns stay unwrapped so later return-JSX
233+
* lowering still sees them.
232234
*
233235
* @param {any[]} statements
234236
* @param {any[]} [extra]
@@ -245,8 +247,8 @@ function wrapSetupBlock(statements, extra) {
245247
/**
246248
* Insert a cloned setup copy immediately before every owned `return`
247249
* (if/else/switch/try/loops). Nested function bodies are left alone — those
248-
* are compiled as their own components. Each write is a block so the
249-
* `const __styleMap` binding stays local to that site.
250+
* are compiled as their own components. Braceless `return` arms become a
251+
* block so the write and the return stay a single consequent.
250252
*
251253
* @param {any} stmt
252254
* @param {any[]} statements
@@ -289,7 +291,9 @@ function graftReturnsInStatement(stmt, statements) {
289291
const switchCase = authored[i];
290292
const grafted = graftReturnsInList(switchCase.consequent || [], statements);
291293
if (grafted.foundReturn) foundReturn = true;
292-
cases.push(grafted.foundReturn ? { ...switchCase, consequent: grafted.list } : switchCase);
294+
cases.push(
295+
grafted.foundReturn ? { ...switchCase, consequent: [b.block(grafted.list)] } : switchCase,
296+
);
293297
}
294298
return {
295299
node: foundReturn ? { ...stmt, cases } : stmt,
@@ -335,6 +339,10 @@ function graftReturnsInStatement(stmt, statements) {
335339
}
336340

337341
/**
342+
* Insert a cloned setup copy immediately before every list-position
343+
* `return`, leaving the `ReturnStatement` in place so
344+
* `compileReturnJsxFunction` and `ssrCompileBody` still see it.
345+
*
338346
* @param {any[]} list
339347
* @param {any[]} statements
340348
* @returns {{ list: any[], foundReturn: boolean }}
@@ -345,7 +353,9 @@ function graftReturnsInList(list, statements) {
345353
for (let i = 0; i < list.length; i++) {
346354
const stmt = list[i];
347355
if (stmt && stmt.type === 'ReturnStatement') {
348-
next.push(wrapSetupBlock(statements, [stmt]));
356+
const cloned = cloneSetupStatements(statements);
357+
for (let j = 0; j < cloned.length; j++) next.push(cloned[j]);
358+
next.push(stmt);
349359
foundReturn = true;
350360
continue;
351361
}

‎packages/octane/tests/_fixtures/style-ref.tsrx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ export function ReturnedStyleRef() {
9898
</>;
9999
}
100100

101+
export function ReturnedHostStyleRef() {
102+
let classes: StyleClassMap | undefined;
103+
return <div id="returned-host" class="card">
104+
{classes && classes.card ? classes.card as string : 'missing'}
105+
<style ref={classes as never}>
106+
.card {
107+
color: rgb(91, 92, 93);
108+
}
109+
</style>
110+
</div>;
111+
}
112+
101113
export function NestedReturnStyleRef(props: { alt: boolean }) {
102114
let classes: StyleClassMap | undefined;
103115
if (props.alt) {

‎packages/octane/tests/style-ref.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { readFileSync } from 'node:fs';
12
import { describe, expect, it } from 'vitest';
3+
import { compile } from 'octane/compiler';
24
import * as ServerRT from 'octane/server';
35
import { mount } from './_helpers';
46
import { loadServerFixture } from './_server-fixture';
@@ -10,6 +12,7 @@ import {
1012
EarlyReturnScopeStyleRef,
1113
IdentifierCallbackStyleRef,
1214
NestedReturnStyleRef,
15+
ReturnedHostStyleRef,
1316
ReturnedStyleRef,
1417
SwitchReturnStyleRef,
1518
SwitchScopeStyleRef,
@@ -95,6 +98,31 @@ describe('style block ref class maps', function () {
9598
expect(css).toMatch(/\.card\.tsrx-[a-z0-9]+/i);
9699
});
97100

101+
it('assigns the class map in a returned host element', function () {
102+
const r = mount(ReturnedHostStyleRef as any);
103+
expectClassMap(r.find('#returned-host') as HTMLElement, 'rgb(91, 92, 93)');
104+
r.unmount();
105+
});
106+
107+
it('assigns the class map during SSR of a returned host element', function () {
108+
const { html, css } = ServerRT.renderToString(server.ReturnedHostStyleRef);
109+
expect(html).toContain('id="returned-host"');
110+
expect(html).not.toContain('missing');
111+
expect(html).toMatch(/tsrx-[a-z0-9]+ card/i);
112+
expect(css).toMatch(/\.card\.tsrx-[a-z0-9]+/i);
113+
});
114+
115+
it('compiles a returned host with a style ref as a static fragment', function () {
116+
const source = readFileSync(FIXTURE, 'utf8');
117+
const client = compile(source, FIXTURE, { hmr: false, dev: false });
118+
const server = compile(source, FIXTURE, { mode: 'server', hmr: false, dev: false });
119+
// lowerReturnJsx / ssrCompileBody only see top-level ReturnStatements.
120+
// Wrapping that return in a block deopts to value lowering, so the host
121+
// never becomes a compiled template string (printed with escaped quotes).
122+
expect(client.code).toMatch(/<div id=\\?"returned-host\\?"/);
123+
expect(server.code).toMatch(/<div id=\\?"returned-host\\?"/);
124+
});
125+
98126
it('assigns the class map before nested if/else returns', function () {
99127
const alt = mount(NestedReturnStyleRef as any, { alt: true });
100128
expectClassMap(alt.find('#nested-alt') as HTMLElement, 'rgb(41, 42, 43)');

0 commit comments

Comments
 (0)