Skip to content

Commit 81ae2be

Browse files
committed
fix: wrap style-ref setup at each return to avoid const collisions
Clone the class-map setup into a block before every owned return so unbraced switch cases and @{ } statement lists do not redeclare __styleMap in the same lexical scope.
1 parent fdfcdaf commit 81ae2be

3 files changed

Lines changed: 85 additions & 11 deletions

File tree

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ function isNestedFunctionType(type) {
228228
/**
229229
* Insert a cloned setup copy immediately before every owned `return`
230230
* (if/else/switch/try/loops). Nested function bodies are left alone — those
231-
* are compiled as their own components. Braceless `return` arms become a
232-
* block so the write and the return stay a single consequent.
231+
* are compiled as their own components. Each return is wrapped in a block so
232+
* the cloned `const` bindings stay in their own lexical scope — switch cases
233+
* and statement lists share the enclosing scope, so sibling `const`s collide.
233234
*
234235
* @param {any} stmt
235236
* @param {any[]} statements
@@ -326,15 +327,7 @@ function graftReturnsInList(list, statements) {
326327
const next = [];
327328
let foundReturn = false;
328329
for (let i = 0; i < list.length; i++) {
329-
const stmt = list[i];
330-
if (stmt && stmt.type === 'ReturnStatement') {
331-
const cloned = cloneSetupStatements(statements);
332-
for (let j = 0; j < cloned.length; j++) next.push(cloned[j]);
333-
next.push(stmt);
334-
foundReturn = true;
335-
continue;
336-
}
337-
const grafted = graftReturnsInStatement(stmt, statements);
330+
const grafted = graftReturnsInStatement(list[i], statements);
338331
next.push(grafted.node);
339332
if (grafted.foundReturn) foundReturn = true;
340333
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,55 @@ export function EarlyReturnScopeStyleRef(props: { alt: boolean }) @{
167167
</style>
168168
</>
169169
}
170+
171+
export function SwitchReturnStyleRef(props: { kind: 'alt' | 'main' }) {
172+
let classes: StyleClassMap | undefined;
173+
switch (props.kind) {
174+
case 'alt':
175+
return <>
176+
<div id="switch-alt" class="card">
177+
{classes && classes.card ? classes.card as string : 'missing'}
178+
</div>
179+
<style ref={classes as never}>
180+
.card {
181+
color: rgb(71, 72, 73);
182+
}
183+
</style>
184+
</>;
185+
default:
186+
return <>
187+
<div id="switch-main" class="card">
188+
{classes && classes.card ? classes.card as string : 'missing'}
189+
</div>
190+
<style ref={classes as never}>
191+
.card {
192+
color: rgb(71, 72, 73);
193+
}
194+
</style>
195+
</>;
196+
}
197+
}
198+
199+
export function SwitchScopeStyleRef(props: { kind: 'alt' | 'main' | 'fallback' }) @{
200+
let classes: StyleClassMap | undefined;
201+
switch (props.kind) {
202+
case 'alt':
203+
return <div id="switch-scope-alt" class="card">
204+
{classes && classes.card ? classes.card as string : 'missing'}
205+
</div>;
206+
case 'main':
207+
return <div id="switch-scope-main" class="card">
208+
{classes && classes.card ? classes.card as string : 'missing'}
209+
</div>;
210+
}
211+
<>
212+
<div id="switch-scope-fallback" class="card">
213+
{classes && classes.card ? classes.card as string : 'missing'}
214+
</div>
215+
<style ref={classes as never}>
216+
.card {
217+
color: rgb(81, 82, 83);
218+
}
219+
</style>
220+
</>
221+
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
IdentifierCallbackStyleRef,
1212
NestedReturnStyleRef,
1313
ReturnedStyleRef,
14+
SwitchReturnStyleRef,
15+
SwitchScopeStyleRef,
1416
ValueStyleRef,
1517
} from './_fixtures/style-ref.tsrx';
1618

@@ -126,6 +128,33 @@ describe('style block ref class maps', function () {
126128
main.unmount();
127129
});
128130

131+
it('assigns the class map before unbraced switch returns', function () {
132+
const alt = mount(SwitchReturnStyleRef as any, { kind: 'alt' });
133+
expectClassMap(alt.find('#switch-alt') as HTMLElement, 'rgb(71, 72, 73)');
134+
alt.unmount();
135+
const main = mount(SwitchReturnStyleRef as any, { kind: 'main' });
136+
expectClassMap(main.find('#switch-main') as HTMLElement, 'rgb(71, 72, 73)');
137+
main.unmount();
138+
});
139+
140+
it('assigns the class map before switch returns in a statement-list scope', function () {
141+
const alt = mount(SwitchScopeStyleRef as any, { kind: 'alt' });
142+
const early = alt.find('#switch-scope-alt') as HTMLElement;
143+
const text = early.textContent || '';
144+
expect(text).not.toBe('missing');
145+
expect(text).toMatch(/tsrx-[a-z0-9]+ card/i);
146+
expect(early.classList.contains('card')).toBe(true);
147+
alt.unmount();
148+
const main = mount(SwitchScopeStyleRef as any, { kind: 'main' });
149+
expect((main.find('#switch-scope-main') as HTMLElement).textContent).toMatch(
150+
/tsrx-[a-z0-9]+ card/i,
151+
);
152+
main.unmount();
153+
const fallback = mount(SwitchScopeStyleRef as any, { kind: 'fallback' });
154+
expectClassMap(fallback.find('#switch-scope-fallback') as HTMLElement, 'rgb(81, 82, 83)');
155+
fallback.unmount();
156+
});
157+
129158
it('assigns the class map during SSR of a nested if/else return', function () {
130159
const alt = ServerRT.renderToString(server.NestedReturnStyleRef, { alt: true });
131160
expect(alt.html).toContain('id="nested-alt"');

0 commit comments

Comments
 (0)