Skip to content

Commit c0a0297

Browse files
committed
fix(octane): skip a second scope stamp on nested style()
walkStyleValue already folds the scope chain into nested style() calls. addScopeClasses now leaves the class value alone when that expression always carries a resolved style(), so class={[style('row'), extra]} matches class={style('row')} plus the extra token instead of appending the hashes again.
1 parent 10018e4 commit c0a0297

4 files changed

Lines changed: 64 additions & 2 deletions

File tree

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,13 @@ function addScopeClasses(element, state) {
990990
} else {
991991
const expression = value.type === 'JSXExpressionContainer' ? value.expression : value;
992992
let base;
993-
if (tools.isStyleCall(expression) || expression.metadata?.tsrx_style_resolved === true) {
994-
// `class={style(expr)}` already resolves to the chain plus the value.
993+
if (
994+
tools.isStyleCall(expression) ||
995+
expression.metadata?.tsrx_style_resolved === true ||
996+
alwaysCarriesResolvedStyle(expression)
997+
) {
998+
// `class={style(expr)}` — and nested forms that always include one —
999+
// already resolve to the chain plus the value.
9951000
return element;
9961001
}
9971002
if (expression.type === 'Literal' && typeof expression.value === 'string') {
@@ -1075,3 +1080,39 @@ function markResolvedStyle(node) {
10751080
node.metadata = { ...(node.metadata ?? {}), tsrx_style_resolved: true };
10761081
return node;
10771082
}
1083+
1084+
/**
1085+
* True when every evaluation of this class-string expression already includes
1086+
* a resolved `style(expr)` (the chain is present, so the host stamp must not
1087+
* append it again). An array or template that contains one such part always
1088+
* does; a conditional or logical only does when every branch does.
1089+
*
1090+
* @param {any} node
1091+
* @returns {boolean}
1092+
*/
1093+
function alwaysCarriesResolvedStyle(node) {
1094+
if (node === null || typeof node !== 'object' || typeof node.type !== 'string') return false;
1095+
if (node.metadata?.tsrx_style_resolved === true) return true;
1096+
switch (node.type) {
1097+
case 'ArrayExpression':
1098+
return node.elements.some(
1099+
(element) => element?.type !== 'SpreadElement' && alwaysCarriesResolvedStyle(element),
1100+
);
1101+
case 'ConditionalExpression':
1102+
return (
1103+
alwaysCarriesResolvedStyle(node.consequent) && alwaysCarriesResolvedStyle(node.alternate)
1104+
);
1105+
case 'LogicalExpression':
1106+
return alwaysCarriesResolvedStyle(node.left) && alwaysCarriesResolvedStyle(node.right);
1107+
case 'TemplateLiteral':
1108+
return node.expressions.some(alwaysCarriesResolvedStyle);
1109+
case 'ParenthesizedExpression':
1110+
case 'TSAsExpression':
1111+
case 'TSSatisfiesExpression':
1112+
case 'TSNonNullExpression':
1113+
case 'TSTypeAssertion':
1114+
return alwaysCarriesResolvedStyle(node.expression);
1115+
default:
1116+
return false;
1117+
}
1118+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export function StyleCallChain(props: { cls: string }) @{
7373
</style>
7474
<p id="sc-static" class={style('sc-row')}>{'static'}</p>
7575
<p id="sc-dynamic" class={style(props.cls)}>{'dynamic'}</p>
76+
<p id="sc-nested" class={[style('sc-row'), props.cls]}>{'nested'}</p>
7677
</>
7778
}</div>
7879
</>

‎packages/octane/tests/compiler/style-call-positions.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ export function C(props) @{
104104
expect(code).not.toMatch(/\bstyle\(/);
105105
expect(code).toMatch(new RegExp(`["']${hash} a["']`));
106106
expect(code).toMatch(new RegExp(`props\\.cond && ["']${hash} b["']`));
107+
// Nested `style()` already carries the chain; the host stamp must not
108+
// wrap the array in normalizeClass and append the hash again.
109+
expect(code).not.toContain('_$normalizeClass');
110+
expect(code).not.toMatch(new RegExp(`\\} ${hash}`));
107111
});
108112

109113
it.each(MODES)(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,22 @@ describe('scoped <style> blocks', () => {
12571257
r.unmount();
12581258
});
12591259

1260+
it('nested style() in an array class does not stamp the chain twice', () => {
1261+
const r = mount(StyleCallChain, { cls: 'sc-cell' });
1262+
const host = r.find('#sc-host');
1263+
const stat = r.find('#sc-static');
1264+
const nested = r.find('#sc-nested');
1265+
const [A] = hashesOf(host);
1266+
const B = hashesOf(stat)[1];
1267+
// `class={style('sc-row')}` and `class={[style('sc-row'), extra]}` share
1268+
// one chain; the extra token is appended, the hashes are not.
1269+
expect(stat.className).toBe(`${A} ${B} sc-row`);
1270+
expect(nested.className).toBe(`${A} ${B} sc-row sc-cell`);
1271+
expect(hashesOf(nested)).toEqual([A, B]);
1272+
expect(getComputedStyle(nested).color).toBe('rgb(4, 5, 6)');
1273+
r.unmount();
1274+
});
1275+
12601276
it('every directive branch is a scope whose CSS ships even while inactive', () => {
12611277
const r = mount(ControlFlow, { ready: false, maybe: false, items: [], kind: 2 });
12621278
const [R] = hashesOf(r.find('#cf-root'));

0 commit comments

Comments
 (0)