Skip to content

Commit afedab9

Browse files
committed
fix: Addressing comments from Chris.
1 parent bc86451 commit afedab9

File tree

6 files changed

+28
-37
lines changed

6 files changed

+28
-37
lines changed

packages/@css-blocks/core/src/BlockCompiler/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { postcss } from "opticss";
33

44
import { Analyzer } from "../Analyzer";
55
import {
6+
BLOCK_ALIAS,
67
BLOCK_AT_RULES,
78
BLOCK_DEBUG,
89
BLOCK_PROP_NAMES_RE,
@@ -31,7 +32,6 @@ export class BlockCompiler {
3132
}
3233

3334
compile(block: Block, root: postcss.Root, analyzer?: Analyzer<keyof TemplateTypes>): postcss.Root {
34-
3535
let resolver = new ConflictResolver(this.config);
3636
let filename = this.config.importer.debugIdentifier(block.identifier, this.config);
3737

@@ -54,6 +54,9 @@ export class BlockCompiler {
5454
}
5555
});
5656

57+
// Clean up block aliases across all styles (the above only cleans the root).
58+
root.walkDecls(BLOCK_ALIAS, (decl) => decl.remove());
59+
5760
// Resolve inheritance based conflicts
5861
resolver.resolveInheritance(root, block);
5962
root.walkRules((rule) => {

packages/@css-blocks/core/src/BlockParser/features/construct-block.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CompoundSelector, ParsedSelector, postcss, postcssSelectorParser as selectorParser } from "opticss";
22

3-
import { BLOCK_ALIAS } from "../../BlockSyntax";
3+
import { BLOCK_ALIAS, CLASS_NAME_IDENT } from "../../BlockSyntax";
44
import { AttrValue, Block, BlockClass, Style } from "../../BlockTree";
55
import { Configuration } from "../../configuration";
66
import * as errors from "../../errors";
@@ -87,9 +87,9 @@ export async function constructBlock(configuration: Configuration, root: postcss
8787
// If this is the key selector, save this ruleset on the created style.
8888
if (isKey) {
8989
if (foundStyles.blockAttrs.length) {
90-
foundStyles.blockAttrs.map(s => addStyleRules(s, rule, styleRuleTuples));
90+
foundStyles.blockAttrs.map(s => addStyleRules(configuration, block, rule, file, s, styleRuleTuples));
9191
} else {
92-
foundStyles.blockClasses.map(s => addStyleRules(s, rule, styleRuleTuples));
92+
foundStyles.blockClasses.map(s => addStyleRules(configuration, block, rule, file, s, styleRuleTuples));
9393
}
9494
}
9595

@@ -107,10 +107,20 @@ export async function constructBlock(configuration: Configuration, root: postcss
107107
return block;
108108
}
109109

110-
function addStyleRules(style: AttrValue | BlockClass, rule: postcss.Rule, tuple: Set<[Style, postcss.Rule]>): void {
110+
function addStyleRules(configuration: Configuration, block: Block, rule: postcss.Rule, file: string, style: AttrValue | BlockClass, tuple: Set<[Style, postcss.Rule]>): void {
111111
rule.walkDecls(BLOCK_ALIAS, decl => {
112-
style.setStyleAliases(new Set(decl.value.split(/\s+/).map(stripQuotes)));
113-
decl.remove();
112+
let aliases: Set<string> = new Set();
113+
decl.value.split(/\s+/).map(alias => {
114+
let cleanedAlias = stripQuotes(alias);
115+
if (!CLASS_NAME_IDENT.test(cleanedAlias)) {
116+
throw new errors.InvalidBlockSyntax(
117+
`Illegal block-alias in export. "${alias}" is not a legal CSS identifier.`,
118+
sourceRange(configuration, block.stylesheet, file, rule),
119+
);
120+
}
121+
aliases.add(cleanedAlias);
122+
});
123+
style.setStyleAliases(aliases);
114124
});
115125
tuple.add([style, rule]);
116126
}

packages/@css-blocks/core/src/BlockTree/Block.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -395,16 +395,12 @@ export class Block
395395

396396
/**
397397
* Return all the style aliases defined on the block.
398-
* @param shallow Pass true to not include inherited objects.
399398
* @returns Array of style aliases.
400399
*/
401-
getAllStyleAliases(shallow?: boolean): Set<string> {
400+
getAllStyleAliases(): Set<string> {
402401
let result = new Set<string>();
403402
for (let blockClass of this.classes) {
404-
result = new Set([...result, ...blockClass.getStyleAliases()]);
405-
}
406-
if (!shallow && this.base) {
407-
result = new Set([...result, ...this.base.getAllStyleAliases(shallow)]);
403+
blockClass.getStyleAliases().forEach(alias => result.add(alias));
408404
}
409405
return result;
410406
}

packages/@css-blocks/core/src/BlockTree/Style.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,7 @@ export abstract class Style<
8888
* Adds a set of aliases to the to this object
8989
*/
9090
public setStyleAliases(aliases: Set<string>): void {
91-
if (aliases.size) {
92-
// clear the existing styleAliases if they exist
93-
if (this._styleAliases) {
94-
this._styleAliases.clear();
95-
} else {
96-
this._styleAliases = aliases;
97-
}
98-
}
91+
this._styleAliases = aliases;
9992
}
10093

10194
/**

packages/@css-blocks/core/test/Block/style-alias-test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class StyleAlias extends BEMProcessor {
8080
let { config } = setupImporting();
8181

8282
let filename = "foo/bar/a-block.css";
83-
let inputCSS = `:scope { block-alias: a-block-alias1 "a-block-alias2-;-with"; color: red; }
83+
let inputCSS = `:scope { block-alias: a-block-alias1 "a-block-alias2--with"; color: red; }
8484
.foo { block-alias: my-class-alias1 my-class-alias-2; clear: both; }
8585
.b[state|small] {block-alias: my-state-alias1 my-state-alias2; color: blue;}
8686
@block-debug self to comment;`;
@@ -93,7 +93,7 @@ export class StyleAlias extends BEMProcessor {
9393
.a-block__foo { clear: both; }
9494
.a-block__b--small { color: blue; }
9595
/* Source: foo/bar/a-block.css
96-
* :scope (.a-block, aliases: .a-block-alias1 .a-block-alias2-;-with)
96+
* :scope (.a-block, aliases: .a-block-alias1 .a-block-alias2--with)
9797
* ├── .b (.a-block__b)
9898
* | states:
9999
* | └── .b[state|small] (.a-block__b--small, aliases: .my-state-alias1 .my-state-alias2)

packages/@css-blocks/core/test/opticss-test.ts

+3-14
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class TemplateAnalysisTests {
5656
let self = this;
5757
let filename = "blocks/foo.block.css";
5858
let css = clean`
59-
:scope { block-alias: my-foo-alias "somethingwith,splcharacters;"; color: blue; font-size: 20px; }
59+
:scope { block-alias: my-foo-alias "namewith-quotes"; color: blue; font-size: 20px; }
6060
:scope[state|foo] { block-alias: my-foo-alias-red; color: red; }
6161
.asdf { font-size: 20px; }
6262
.asdf[state|larger] { font-size: 26px; color: red; }
@@ -96,18 +96,7 @@ export class TemplateAnalysisTests {
9696
let analyzer = new TestAnalyzer();
9797
return analyzer.analyze().then(async (analyzer: TestAnalyzer) => {
9898
let compiler = new BlockCompiler(postcss, config);
99-
let optimizer = new Optimizer({}, {
100-
rewriteIdents: {
101-
id: false,
102-
class: true,
103-
omitIdents: {
104-
id: [],
105-
class: [],
106-
},
107-
},
108-
analyzedAttributes: [],
109-
analyzedTagnames: false,
110-
});
99+
let optimizer = new Optimizer({}, { rewriteIdents: { id: false, class: true} });
111100
let block = await analyzer.blockFactory.getBlock(filename);
112101
let compiled = compiler.compile(block, block.stylesheet!, analyzer);
113102
for (let analysis of analyzer.analyses()) {
@@ -129,7 +118,7 @@ export class TemplateAnalysisTests {
129118
let it = analyses[0].elements.values();
130119
let element1 = it.next().value;
131120
let rewrite1 = blockMapping.rewriteMapping(element1);
132-
assert.deepEqual([...rewrite1.staticClasses].sort(), ["c", "d", "e", "my-foo-alias", "my-foo-alias-red", "somethingwith,splcharacters;"]);
121+
assert.deepEqual([...rewrite1.staticClasses].sort(), ["c", "d", "e", "my-foo-alias", "my-foo-alias-red", "namewith-quotes"]);
133122
assert.deepEqual(rewrite1.dynamicClasses, []);
134123
let element2 = it.next().value;
135124
let rewrite2 = blockMapping.rewriteMapping(element2);

0 commit comments

Comments
 (0)