Skip to content

Commit 47d633d

Browse files
committed
fix(tsrx): treat object-member $class reads as theme opt-in
is_class_read only recognized theme.$class on the binding itself, so a block stored as themes.dark stayed a class-map when themes.dark.$class was read. Resolve that one-level member the same way apply does.
1 parent c9ef3d8 commit 47d633d

3 files changed

Lines changed: 87 additions & 9 deletions

File tree

‎packages/tsrx/src/analyze/style-analyze.js‎

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,41 @@ function resolve_local_member(binding, expression) {
209209
}
210210

211211
/**
212-
* Whether any reference to the binding reads its `$class` property.
212+
* Whether `member` is a `$class` property access (`x.$class` or `x['$class']`).
213+
*
214+
* @param {AST.MemberExpression} member
215+
* @returns {boolean}
216+
*/
217+
function is_class_member(member) {
218+
if (member.computed) {
219+
return member.property.type === 'Literal' && member.property.value === '$class';
220+
}
221+
return member.property.type === 'Identifier' && member.property.name === '$class';
222+
}
223+
224+
/**
225+
* Whether any reference to the binding reads `$class` of this style block.
226+
* Direct assignment: `theme.$class`. Object member: `themes.dark.$class`.
213227
*
214228
* @param {Binding | null} binding
229+
* @param {AST.JSXStyleElement} style
215230
* @returns {boolean}
216231
*/
217-
function is_class_read(binding) {
232+
function is_class_read(binding, style) {
218233
if (!binding) return false;
219234
return binding.references.some(({ node, path }) => {
220235
const parent = path.at(-1);
221236
if (parent?.type !== 'MemberExpression' || parent.object !== node) return false;
222-
if (parent.computed) {
223-
return parent.property.type === 'Literal' && parent.property.value === '$class';
237+
if (is_class_member(parent)) {
238+
return binding.initial === style;
224239
}
225-
return parent.property.type === 'Identifier' && parent.property.name === '$class';
240+
const grandparent = path.at(-2);
241+
return (
242+
grandparent?.type === 'MemberExpression' &&
243+
grandparent.object === parent &&
244+
is_class_member(grandparent) &&
245+
resolve_local_member(binding, parent) === style
246+
);
226247
});
227248
}
228249

@@ -420,12 +441,13 @@ export function analyze_styles(ast, scopes, state) {
420441
parent?.type === 'ExportDefaultDeclaration' ||
421442
export_parent?.type === 'ExportNamedDeclaration' ||
422443
(declared_name !== null && exported_names.has(declared_name));
423-
// Reading `theme.$class` opts an element (or a child component's
424-
// element, through a prop) into the block's whole stylesheet, so
425-
// such a block is a theme: every selector stays, like `apply`.
444+
// Reading `theme.$class` or `themes.dark.$class` opts an element
445+
// (or a child component's element, through a prop) into the
446+
// block's whole stylesheet, so such a block is a theme: every
447+
// selector stays, like `apply`.
426448
node.metadata.styleClassRead =
427449
declared_name !== null &&
428-
is_class_read(nearest_scope(path, scopes)?.get(declared_name) ?? null);
450+
is_class_read(nearest_scope(path, scopes)?.get(declared_name) ?? null, node);
429451

430452
const stylesheet = get_style_element_stylesheet(node);
431453
if (stylesheet && get_style_class_map_names(stylesheet).includes('$class')) {

‎packages/tsrx/tests/analyze/styles.test.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,40 @@ describe('scoped style analysis', () => {
11161116
expect(styles.metadata.styleClassRead).toBe(false);
11171117
expect(styles.metadata.styleKind).toBe('class-map');
11181118
});
1119+
1120+
it('classifies an object-property block whose $class is read as a theme', () => {
1121+
for (const source of [
1122+
`function App() @{
1123+
const themes = { dark: <style>div {} .card {}</style> };
1124+
<><div class={themes.dark.$class} /><Child cls={themes.dark['$class']} /></>
1125+
}`,
1126+
`function App() @{
1127+
const themes = { 'dark': <style>div {} .card {}</style> };
1128+
<div class={themes.dark.$class} />
1129+
}`,
1130+
]) {
1131+
const result = analyze(source);
1132+
const [dark] = result.styles.assigned;
1133+
1134+
expect(dark.metadata.styleClassRead, source).toBe(true);
1135+
expect(dark.metadata.styleKind, source).toBe('theme');
1136+
}
1137+
});
1138+
1139+
it('classifies object-property $class reads individually', () => {
1140+
const result = analyze(
1141+
`function App() @{
1142+
const themes = { dark: <style>div {}</style>, light: <style>div {}</style> };
1143+
<div class={themes.dark.$class} />
1144+
}`,
1145+
);
1146+
const [dark, light] = result.styles.assigned;
1147+
1148+
expect(dark.metadata.styleClassRead).toBe(true);
1149+
expect(dark.metadata.styleKind).toBe('theme');
1150+
expect(light.metadata.styleClassRead).toBe(false);
1151+
expect(light.metadata.styleKind).toBe('class-map');
1152+
});
11191153
});
11201154

11211155
describe('assigned @{} blocks', () => {

‎packages/tsrx/tests/shared/scoped-styles.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,28 @@ export function runSharedScopedStyleTests({
803803
expect(code).toContain(`<h2 ${classAttrName}={\`\${parentClass} ${local}\`}>`);
804804
});
805805

806+
it('rfc1-class-opt-in: reading themes.dark.$class keeps every selector', () => {
807+
const { code, css, cssHash } = compile(
808+
`export function App() @{
809+
const themes = { dark: <style>
810+
div { color: blue; }
811+
.card { color: red; }
812+
</style> };
813+
<>
814+
<div ${classAttrName}={themes.dark.$class}>{'opted in'}</div>
815+
<div ${classAttrName}={themes.dark.card}>{'card'}</div>
816+
</>
817+
}`,
818+
'App.tsrx',
819+
);
820+
821+
const theme = hashes_of(cssHash)[0];
822+
expect(css).not.toContain('(unused)');
823+
expect(css).toContain(`div.${theme} { color: blue; }`);
824+
expect(code).toContain(`${classAttrName}={themes.dark.$class}`);
825+
expect(code).toContain(`${classAttrName}={themes.dark.card}`);
826+
});
827+
806828
it('keeps class-map pruning when only class entries of a local block are read', () => {
807829
const { css } = compile(
808830
`export function App() @{

0 commit comments

Comments
 (0)