Skip to content

Commit b4f4b9f

Browse files
amiller-ghchriseppstein
authored andcommitted
fix: Updated with Chris' feedback.
1 parent 57a012a commit b4f4b9f

25 files changed

+268
-204
lines changed

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

+21-21
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import { TemplateValidator, TemplateValidatorOptions } from "./validations";
2727
* This interface defines a JSON friendly serialization
2828
* of an {Analysis}.
2929
*/
30-
export interface SerializedAnalysis {
31-
template: SerializedTemplateInfo<keyof TemplateTypes>;
30+
export interface SerializedAnalysis<K extends keyof TemplateTypes> {
31+
template: SerializedTemplateInfo<K>;
3232
blocks: ObjectDictionary<string>;
3333
stylesFound: string[];
3434
// The numbers stored in each element are an index into a stylesFound;
@@ -50,36 +50,36 @@ export class Analysis<K extends keyof TemplateTypes> {
5050
parent?: Analyzer<K>;
5151
template: TemplateTypes[K];
5252

53-
/**
54-
* A map from a local name for the block to the [[Block]].
55-
* The local name must be a legal CSS ident/class name but this is not validated here.
56-
* See [[CLASS_NAME_IDENT]] for help validating a legal class name.
57-
*/
58-
blocks: ObjectDictionary<Block>;
59-
6053
/**
6154
* A per-element correlation of styles used. The current correlation is added
6255
* to this list when [[endElement]] is called.
6356
*/
6457
// tslint:disable-next-line:prefer-whatever-to-any
6558
elements: Map<string, ElementAnalysis<any, any, any>>;
6659

60+
/**
61+
* A map from a local name for the block to the [[Block]].
62+
* The local name must be a legal CSS ident/class name but this is not validated here.
63+
* See [[CLASS_NAME_IDENT]] for help validating a legal class name.
64+
*/
65+
private blocks: ObjectDictionary<Block>;
66+
6767
/**
6868
* The current element, created when calling [[startElement]].
6969
* The current element is unset after calling [[endElement]].
7070
*/
7171
// tslint:disable-next-line:prefer-whatever-to-any
72-
currentElement: ElementAnalysis<any, any, any> | undefined;
72+
private currentElement: ElementAnalysis<any, any, any> | undefined;
7373

7474
/**
7575
* Template validator instance to verify blocks applied to an element.
7676
*/
77-
validator: TemplateValidator;
77+
private validator: TemplateValidator;
7878

7979
/**
8080
* @param template The template being analyzed.
8181
*/
82-
constructor(template: TemplateTypes[K], options?: TemplateValidatorOptions, parent?: Analyzer<K>) {
82+
constructor(parent: Analyzer<K>, template: TemplateTypes[K], options?: TemplateValidatorOptions) {
8383
this.idGenerator = new IdentGenerator();
8484
this.parent = parent;
8585
this.template = template;
@@ -96,7 +96,7 @@ export class Analysis<K extends keyof TemplateTypes> {
9696
/**
9797
* Convenience setter for adding a block to the template scope.
9898
*/
99-
addBlock(name: string, block: Block): void { this.blocks[name] = block; }
99+
addBlock(name: string, block: Block): Block { return this.blocks[name] = block; }
100100

101101
/**
102102
* Convenience getter for fetching a block from the template scope.
@@ -300,11 +300,11 @@ export class Analysis<K extends keyof TemplateTypes> {
300300
/**
301301
* Generates a [[SerializedTemplateAnalysis]] for this analysis.
302302
*/
303-
serialize(): SerializedAnalysis {
303+
serialize(): SerializedAnalysis<K> {
304304
let blocks = {};
305305
let stylesFound: string[] = [];
306306
let elements: ObjectDictionary<SerializedElementAnalysis> = {};
307-
let template = this.template.serialize();
307+
let template = this.template.serialize() as SerializedTemplateInfo<K>;
308308
let styleNameMap = new Map<Style, string>();
309309
let styleIndexes = new Map<Style, number>();
310310

@@ -325,8 +325,8 @@ export class Analysis<K extends keyof TemplateTypes> {
325325
});
326326

327327
// Serialize our blocks to a map of their local names.
328-
Object.keys(this.blocks).forEach((localname) => {
329-
blocks[localname] = this.blocks[localname].identifier;
328+
Object.keys(this.blocks).forEach((localName) => {
329+
blocks[localName] = this.blocks[localName].identifier;
330330
});
331331

332332
// Serialize all discovered Elements.
@@ -345,13 +345,13 @@ export class Analysis<K extends keyof TemplateTypes> {
345345
* @param postcssImpl The instance of postcss that should be used to parse the block's css.
346346
*/
347347
static async deserialize (
348-
serializedAnalysis: SerializedAnalysis,
348+
serializedAnalysis: SerializedAnalysis<keyof TemplateTypes>,
349349
blockFactory: BlockFactory,
350350
parent: Analyzer<keyof TemplateTypes>,
351351
): Promise<Analysis<keyof TemplateTypes>> {
352352
let blockNames = Object.keys(serializedAnalysis.blocks);
353353
let info = TemplateInfoFactory.deserialize<keyof TemplateTypes>(serializedAnalysis.template);
354-
let analysis = new Analysis(info, {}, parent);
354+
let analysis = parent.newAnalysis(info);
355355
let blockPromises = new Array<Promise<{name: string; block: Block}>>();
356356
blockNames.forEach(n => {
357357
let blockIdentifier = serializedAnalysis.blocks[n];
@@ -391,8 +391,8 @@ export class Analysis<K extends keyof TemplateTypes> {
391391
return analysis;
392392
}
393393

394-
forOptimizer(opts: ResolvedConfiguration): OptimizationTemplateAnalysis<keyof TemplateTypes> {
395-
let optAnalysis = new OptimizationTemplateAnalysis<keyof TemplateTypes>(this.template);
394+
forOptimizer(opts: ResolvedConfiguration): OptimizationTemplateAnalysis<K> {
395+
let optAnalysis = new OptimizationTemplateAnalysis<K>(this.template);
396396
for (let element of this.elements.values()) {
397397
let result = element.forOptimizer(opts);
398398
optAnalysis.elements.push(result[0]);

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export interface AnalysisOptions {
3838
features?: TemplateIntegrationOptions;
3939
}
4040

41-
export interface SerializedAnalyzer {
42-
analyses: SerializedAnalysis[];
41+
export interface SerializedAnalyzer<K extends keyof TemplateTypes> {
42+
analyses: SerializedAnalysis<K>[];
4343
}
4444

4545
export abstract class Analyzer<K extends keyof TemplateTypes> {
@@ -66,7 +66,7 @@ export abstract class Analyzer<K extends keyof TemplateTypes> {
6666
this.dynamicStyles = new MultiMap();
6767
}
6868

69-
abstract analyze(...entryPoints: string[]): Promise<Analyzer<keyof TemplateTypes>>;
69+
abstract analyze(...entryPoints: string[]): Promise<Analyzer<K>>;
7070

7171
// TODO: We don't really want to burn the world here.
7272
// We need more targeted Analysis / BlockFactory invalidation.
@@ -79,7 +79,7 @@ export abstract class Analyzer<K extends keyof TemplateTypes> {
7979
}
8080

8181
newAnalysis(info: TemplateInfo<K>): Analysis<K> {
82-
let analysis = new Analysis<K>(info, this.validatorOptions, this);
82+
let analysis = new Analysis<K>(this, info, this.validatorOptions);
8383
this.analysisMap.set(info.identifier, analysis);
8484
return analysis;
8585
}
@@ -126,16 +126,16 @@ export abstract class Analyzer<K extends keyof TemplateTypes> {
126126
return allBlocks;
127127
}
128128

129-
serialize(): SerializedAnalyzer {
130-
let analyses: SerializedAnalysis[] = [];
129+
serialize(): SerializedAnalyzer<K> {
130+
let analyses: SerializedAnalysis<K>[] = [];
131131
this.eachAnalysis(a => {
132132
analyses.push(a.serialize());
133133
});
134134
return { analyses };
135135
}
136136

137-
forOptimizer(opts: ResolvedConfiguration): OptimizationAnalysis<keyof TemplateTypes>[] {
138-
let analyses = new Array<OptimizationAnalysis<keyof TemplateTypes>>();
137+
forOptimizer(opts: ResolvedConfiguration): OptimizationAnalysis<K>[] {
138+
let analyses = new Array<OptimizationAnalysis<K>>();
139139
this.eachAnalysis(a => {
140140
analyses.push(a.forOptimizer(opts));
141141
});

packages/css-blocks/src/BlockTree/RulesetContainer.ts

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ export type Resolution<S extends Styles = Styles> = {
4343
*/
4444
function expandProp(prop: string, value: string): propParser.Declarations {
4545
let expanded: propParser.Declarations = {};
46+
47+
// The PropertyParser doesn't understand CSS variables.
48+
// Replace them with something it understands.
4649
value = value.replace(/var\([^\)]+\)/gi, "inherit");
4750
if (propParser.isValidDeclaration(prop, value)) {
4851
expanded = propParser.expandShorthandProperty(prop, value, true, false);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class TemplateAnalysisTests {
3838
analysis: Analysis<"Opticss.Template">, block: Block, blockName: string,
3939
useAttrsCallback?: (container: BlockClass, element: ElementAnalysis<whatever, whatever, whatever>) => void,
4040
) {
41-
analysis.blocks[blockName] = block;
41+
analysis.addBlock(blockName, block);
4242

4343
for (let c of block.classes) {
4444
let element = analysis.startElement(POSITION_UNKNOWN);

0 commit comments

Comments
 (0)