Skip to content

Commit 5d91c56

Browse files
committed
fix: Small tweaks around parameter passing.
1 parent aea5fcc commit 5d91c56

File tree

10 files changed

+41
-31
lines changed

10 files changed

+41
-31
lines changed

packages/@css-blocks/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"compile": "tsc --version && tsc --build",
88
"pretest": "yarn run compile",
99
"test": "yarn run test:runner",
10-
"test:runner": "mocha dist/test/opticss-test.js --opts test/mocha.opts",
10+
"test:runner": "mocha dist/test --opts test/mocha.opts",
1111
"posttest": "yarn run lint",
1212
"prepublish": "rm -rf dist && yarn run compile && yarn run lintall",
1313
"lint": "tslint -t msbuild --project . -c tslint.cli.json",

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export class Analysis<K extends keyof TemplateTypes> {
180180
if (this.currentElement) {
181181
throw new Error("Internal Error: failure to call endElement before previous call to startElement.");
182182
}
183-
this.currentElement = new ElementAnalysis<BooleanExpression, StringExpression, TernaryExpression>(this.reservedClassNames(), location, tagName, this.idGenerator.nextIdent());
183+
this.currentElement = new ElementAnalysis<BooleanExpression, StringExpression, TernaryExpression>(location, this.reservedClassNames(), tagName, this.idGenerator.nextIdent());
184184
return this.currentElement;
185185
}
186186

@@ -377,7 +377,7 @@ export class Analysis<K extends keyof TemplateTypes> {
377377
let elementNames = Object.keys(serializedAnalysis.elements);
378378
elementNames.forEach((elID) => {
379379
let data = serializedAnalysis.elements[elID];
380-
let element = new ElementAnalysis<null, null, null>(this.reservedClassNames(), data.sourceLocation || {start: POSITION_UNKNOWN}, undefined, elID);
380+
let element = new ElementAnalysis<null, null, null>(data.sourceLocation || {start: POSITION_UNKNOWN}, this.reservedClassNames(), undefined, elID);
381381
analysis.elements.set(elID, element);
382382
});
383383

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,17 @@ export abstract class Analyzer<K extends keyof TemplateTypes> {
117117
return allBlocks;
118118
}
119119

120+
/**
121+
* Iterates through all the analyses objects for all the templates and creates
122+
* a set of reservedClassNames here. This is what the block compiler calls to
123+
* get the list of reservedClassNames.
124+
*/
120125
reservedClassNames(): Set<string> {
121-
let allAliases = new Set<string>();
126+
let allReservedClassNames = new Set<string>();
122127
this.analysisMap.forEach(analysis => {
123-
allAliases = new Set<string>([...allAliases, ...analysis.reservedClassNames()]);
128+
allReservedClassNames = new Set<string>([...allReservedClassNames, ...analysis.reservedClassNames()]);
124129
});
125-
return allAliases;
130+
return allReservedClassNames;
126131
}
127132

128133
serialize(): SerializedAnalyzer<K> {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export class ElementAnalysis<BooleanExpression, StringExpression, TernaryExpress
238238
return this._sealed;
239239
}
240240

241-
constructor(reservedClassNames: Set<string>, location: SourceLocation, tagName?: string, id?: string) {
241+
constructor(location: SourceLocation, reservedClassNames: Set<string>, tagName?: string, id?: string) {
242242
this.id = id;
243243
this.tagName = tagName;
244244
this.sourceLocation = location;

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ export class Block
477477
if (result === null) {
478478
newNodes.push(node);
479479
} else {
480-
// TODO: check if this needs to be passed the global value as well
481480
newNodes.push(selectorParser.className({ value: result[0].cssClass(config, reservedClassNames)}));
482481
i += result[1];
483482
}
@@ -516,7 +515,10 @@ export class Block
516515
let result: string[] = [`Source: ${this.identifier}`];
517516

518517
// Log Root Class and all children first at root level.
519-
const classes = [...this.rootClass.cssClasses(config)].join(".");
518+
// NOTE: debug statements don't take into account the reservedClassNames as
519+
// debug happens during parse and we can only get the entire list of
520+
// reservedClassNames once block parsing is complete
521+
const classes = [...this.rootClass.cssClasses(config, new Set())].join(".");
520522
const aliases = this.rootClass.getStyleAliases();
521523

522524
result.push(`${ROOT_CLASS} (.${classes}${aliases.size ? `, aliases: .${[...aliases].join(" .")}` : ""})`, ...this.rootClass.debug(config));

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ export abstract class Style<
6161
* including inherited classes.
6262
* @returns this object's css class and all inherited classes.
6363
*/
64-
cssClasses(config: ResolvedConfiguration): string[] {
64+
cssClasses(config: ResolvedConfiguration, reservedClassNames: Set<string>): string[] {
6565
let classes: string[] = [];
6666
for (let style of this.resolveStyles()) {
67-
classes.push(style.cssClass(config, new Set()));
67+
classes.push(style.cssClass(config, reservedClassNames));
6868
}
6969
return classes;
7070
}
@@ -123,7 +123,10 @@ export abstract class Style<
123123
* @returns A debug string.
124124
*/
125125
asDebug(config: ResolvedConfiguration) {
126-
const classes = [...this.cssClasses(config)].join(".");
126+
// NOTE: debug statements don't take into account the reservedClassNames as
127+
// debug happens during parse and we can only get the entire list of
128+
// reservedClassNames once block parsing is complete
129+
const classes = [...this.cssClasses(config, new Set())].join(".");
127130
const aliases = this.getStyleAliases();
128131
return `${this.asSource()}${classes ? ` (.${classes}` : ""}${aliases.size ? `, aliases: .${[...aliases].join(" .")}` : ""}${classes || aliases ? ")" : ""}`;
129132
}

packages/@css-blocks/glimmer/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"scripts": {
1313
"test": "yarn run test:runner",
14-
"test:runner": "mocha --opts test/mocha.opts dist/cjs/test/template-rewrite-test.js",
14+
"test:runner": "mocha --opts test/mocha.opts dist/cjs/test/",
1515
"compile": "tsc --version && tsc --build && tsc --build tsconfig.amd.json",
1616
"pretest": "yarn run compile",
1717
"posttest": "yarn run lint",

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class ElementAnalyzer {
8181
private newElement(node: AnalyzableNodes, forRewrite: boolean): TemplateElement {
8282
let label = isElementNode(node) ? node.tag : node.path.original as string;
8383
if (forRewrite) {
84-
return new ElementAnalysis<BooleanExpression, StringExpression, TernaryExpression>(this.reservedClassNames, nodeLocation(node), label);
84+
return new ElementAnalysis<BooleanExpression, StringExpression, TernaryExpression>(nodeLocation(node), this.reservedClassNames, label);
8585
}
8686
else {
8787
return this.analysis.startElement<BooleanExpression, StringExpression, TernaryExpression>(nodeLocation(node), label);

packages/@css-blocks/glimmer/test/classnames-helper-test.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe("Classnames Helper", () => {
6161

6262
let inputs: Style[] = [b.rootClass, c1, c2, s1];
6363
let rewrite = new IndexedClassMapping(inputs, [], { });
64-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
64+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
6565
element.addDynamicClasses({
6666
condition: builders.boolean(true),
6767
whenTrue: [r, c2],
@@ -82,7 +82,7 @@ describe("Classnames Helper", () => {
8282

8383
let inputs = [r, c1, c2, s1];
8484
let rewrite = new IndexedClassMapping(inputs, [], { });
85-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
85+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
8686
element.addDynamicClasses({
8787
condition: builders.boolean(true),
8888
whenTrue: [r],
@@ -103,7 +103,7 @@ describe("Classnames Helper", () => {
103103

104104
let inputs = [r, c1, c2, s1];
105105
let rewrite = new IndexedClassMapping(inputs, [], { });
106-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
106+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
107107
element.addDynamicClasses({
108108
condition: builders.boolean(true),
109109
whenTrue: [r],
@@ -125,7 +125,7 @@ describe("Classnames Helper", () => {
125125

126126
let inputs = [r, red, orange, blue];
127127
let rewrite = new IndexedClassMapping(inputs, [], { });
128-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
128+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
129129
element.addDynamicGroup(r, red.attribute, builders.mustache(builders.string("blue")));
130130
element.seal();
131131
let result = print(helperGenerator(rewrite, element));
@@ -143,7 +143,7 @@ describe("Classnames Helper", () => {
143143

144144
let inputs = [r, red, orange, blue];
145145
let rewrite = new IndexedClassMapping(inputs, [], { });
146-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
146+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
147147
element.addDynamicClasses({
148148
condition: builders.boolean(true),
149149
whenTrue: [ r ],
@@ -166,7 +166,7 @@ describe("Classnames Helper", () => {
166166
let rewrite = new IndexedClassMapping(inputs, [], {
167167
a: {and: [ 0, 2 ]},
168168
});
169-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
169+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
170170
element.addDynamicClasses({
171171
condition: builders.boolean(true),
172172
whenTrue: [r, c2],
@@ -189,7 +189,7 @@ describe("Classnames Helper", () => {
189189
let rewrite = new IndexedClassMapping(inputs, [], {
190190
a: {and: [ {or: [0]}, {and: [2]} ]},
191191
});
192-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
192+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
193193
element.addDynamicClasses({
194194
condition: builders.boolean(true),
195195
whenTrue: [r, c2],
@@ -212,7 +212,7 @@ describe("Classnames Helper", () => {
212212
let rewrite = new IndexedClassMapping(inputs, [], {
213213
a: {and: [0, {not: 2}]},
214214
});
215-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
215+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
216216
element.addDynamicClasses({
217217
condition: builders.boolean(true),
218218
whenTrue: [r, c2],
@@ -235,7 +235,7 @@ describe("Classnames Helper", () => {
235235
let rewrite = new IndexedClassMapping(inputs, [], {
236236
a: {or: [0, {not: 2}]},
237237
});
238-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
238+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
239239
element.addDynamicClasses({
240240
condition: builders.boolean(true),
241241
whenTrue: [r, c2],
@@ -261,7 +261,7 @@ describe("Classnames Helper", () => {
261261
c: {and: [ 2 ]},
262262
d: {and: [ 3 ]},
263263
});
264-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
264+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
265265
element.addDynamicClasses({
266266
condition: builders.boolean(true),
267267
whenTrue: [r, c2],
@@ -287,7 +287,7 @@ describe("Classnames Helper", () => {
287287
c: {and: [ 2 ]},
288288
d: {and: [ 3 ]},
289289
});
290-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
290+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
291291
element.addDynamicClasses({
292292
condition: builders.boolean(false),
293293
whenTrue: [r, c2],
@@ -313,7 +313,7 @@ describe("Classnames Helper", () => {
313313
c: {and: [ 2 ]},
314314
d: {and: [ 3 ]},
315315
});
316-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
316+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
317317
element.addDynamicClasses({
318318
condition: builders.boolean(true),
319319
whenTrue: [ r ],
@@ -340,7 +340,7 @@ describe("Classnames Helper", () => {
340340
c: {and: [ 2 ]},
341341
d: {and: [ 3 ]},
342342
});
343-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
343+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
344344
element.addDynamicClasses({
345345
condition: builders.boolean(false),
346346
whenTrue: [ r ],
@@ -367,7 +367,7 @@ describe("Classnames Helper", () => {
367367
c: {and: [ 2 ]},
368368
d: {and: [ 3 ]},
369369
});
370-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
370+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
371371
element.addDynamicClasses({
372372
condition: builders.boolean(true),
373373
whenTrue: [ r ],
@@ -394,7 +394,7 @@ describe("Classnames Helper", () => {
394394
c: {and: [ 2 ]},
395395
d: {and: [ 3 ]},
396396
});
397-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
397+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
398398
element.addDynamicClasses({
399399
condition: builders.boolean(true),
400400
whenTrue: [ r ],
@@ -416,7 +416,7 @@ describe("Classnames Helper", () => {
416416
let orange = r.ensureAttributeValue("[state|theme=orange]");
417417
let blue = r.ensureAttributeValue("[state|theme=blue]");
418418

419-
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>(new Set(), {start: POSITION_UNKNOWN});
419+
let element = new ElementAnalysis<BooleanAST, StringAST, TernaryAST>({start: POSITION_UNKNOWN}, new Set());
420420
element.addDynamicClasses({
421421
condition: builders.boolean(true),
422422
whenTrue: [ r ],

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe("Template Rewriting", function() {
104104
let result = await pipeline(projectDir, analyzer, "with-block-aliases", templatePath);
105105
assert.deepEqual(minify(print(result.ast)), minify(`
106106
<div class="my-scope-alias b stylesheet__world">
107-
<h1 class="e my-header-alias">Hello, <span class="f stylesheet__world stylesheet__world--thick c {{-css-blocks-classnames 2 4 2 isThick 1 3 4 2 1 textStyle "bold" 1 0 "italic" 1 1 "g" 0 "f" 1 "my-alias-for-state" 2 "d" 3}}">World</span>!</h1>
107+
<h1 class="e my-header-alias">Hello, <span class="f c stylesheet__world stylesheet__world--thick {{-css-blocks-classnames 2 4 2 isThick 1 3 4 2 1 textStyle "bold" 1 0 "italic" 1 1 "g" 0 "f" 1 "my-alias-for-state" 2 "d" 3}}">World</span>!</h1>
108108
</div>
109109
`));
110110
});

0 commit comments

Comments
 (0)