Skip to content

Commit 360a28f

Browse files
committed
fix: Switches in the rewrite didn't work with inheritance.
1 parent fafdb53 commit 360a28f

File tree

7 files changed

+74
-5
lines changed

7 files changed

+74
-5
lines changed

packages/@css-blocks/core/src/Analyzer/ElementAnalysis.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -997,11 +997,12 @@ function mapClasses(
997997
let resolvedStyles = style.resolveStyles();
998998
for (let resolvedStyle of resolvedStyles) {
999999
// TODO: update with a non empty set here
1000-
let classNames = resolvedStyle.cssClassesWithAliases(configuration, reservedClassNames);
1001-
classNames.forEach(cls => {
1000+
let classNames = [resolvedStyle.cssClass(configuration, reservedClassNames)];
1001+
classNames.push(...resolvedStyle.getStyleAliases());
1002+
for (let cls of classNames) {
10021003
map.set(cls, resolvedStyle);
10031004
classes.push(cls);
1004-
});
1005+
}
10051006
}
10061007
if (classes.length === 1) {
10071008
return attrValues.constant(classes[0]);

packages/@css-blocks/glimmer/src/ClassnamesHelperGenerator.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,11 @@ function constructSwitch(builders: Builders, stateExpr: Switch<StringAST> & HasG
241241
}
242242
}
243243
else {
244-
expr.push(builders.number(1));
245-
expr.push(builders.number(unwrap(rewrite.indexOf(obj))));
244+
let styles = obj.resolveStyles();
245+
expr.push(builders.number(styles.size));
246+
for (let s of styles) {
247+
expr.push(builders.number(unwrap(rewrite.indexOf(s))));
248+
}
246249
}
247250
}
248251
return expr;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
:scope[style=open] {
2+
padding: 2em;
3+
}
4+
5+
:scope[style=condensed] {
6+
padding: 0.5em;
7+
}
8+
9+
:scope[style=normal] {
10+
padding: 1em;
11+
}
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@block grandparent from "./grandparent.css";
2+
3+
:scope {
4+
extends: grandparent;
5+
}
6+
7+
:scope[style=open] {
8+
margin: 1em;
9+
}
10+
11+
:scope[style=normal] {
12+
margin: 0.5em;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@block parent from "./parent.css";
2+
3+
:scope {
4+
extends: parent;
5+
}
6+
7+
:scope[style=open] {
8+
line-height: 2em;
9+
}
10+
11+
:scope[style=spacious] {
12+
line-height: 3em;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div block:scope block:style={{@style}}>
2+
Inheritance Test
3+
</div>

packages/@css-blocks/glimmer/test/template-rewrite-test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,30 @@ describe("Template Rewriting", function() {
8282
);
8383
});
8484

85+
it("rewrites styles from dynamic inherited attributes", async function() {
86+
let projectDir = fixture("styled-app");
87+
let analyzer = new GlimmerAnalyzer(new BlockFactory({}), {}, moduleConfig);
88+
let templatePath = fixture("styled-app/src/ui/components/with-dynamic-inherited-states/template.hbs");
89+
let result = await pipeline(projectDir, analyzer, "with-dynamic-inherited-states", templatePath);
90+
91+
// TODO why is `f` class both static and dynamic?
92+
assert.deepEqual(minify(print(result.ast)), minify(`
93+
<div class="grandparent parent stylesheet {{-css-blocks-classnames 1 7 4 4 1 @style "open" 3 2 4 5 "condensed" 1 0 "normal" 2 1 3 "spacious" 1 6 "f" 0 "g" 1 "e" 2 "d" 3 "c" 4 "a" 5 "b" 6}}">
94+
Inheritance Test
95+
</div>
96+
`));
97+
assert.deepEqual(minify(result.css.toString()), minify(
98+
".a { line-height: 2em; } " + // block[style=open]
99+
".b { line-height: 3em; } " + // block[style=spacious]
100+
".c { margin: 1em; } " + // parent[style=open]
101+
".d { margin: 0.5em; } " + // parent[style=normal]
102+
".e { padding: 2em; } " + // grandparent[style=open]
103+
".f { padding: 0.5em; } " + // grandparent[style=condensed]
104+
".g { padding: 1em; }" // grandparent[style=normal]
105+
),
106+
);
107+
});
108+
85109
it("rewrites styles from dynamic classes", async function() {
86110
let projectDir = fixture("styled-app");
87111
let analyzer = new GlimmerAnalyzer(new BlockFactory({}), {}, moduleConfig);

0 commit comments

Comments
 (0)