forked from linkedin/css-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysis.ts
552 lines (492 loc) · 19.9 KB
/
Analysis.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// tslint:disable-next-line:no-unused-variable Imported for Documentation link
import {
POSITION_UNKNOWN,
SourceLocation,
SourcePosition,
isSourcePosition,
} from "@opticss/element-analysis";
import {
SerializedTemplateInfo,
TemplateAnalysis as OptimizationTemplateAnalysis,
TemplateInfoFactory,
TemplateTypes,
} from "@opticss/template-api";
import { ObjectDictionary, objectValues } from "@opticss/util";
import { IdentGenerator } from "opticss";
import { BlockFactory } from "../BlockParser";
import { AttrValue, Block, BlockClass, Style } from "../BlockTree";
import { ResolvedConfiguration } from "../configuration";
import { allDone } from "../util";
import { Analyzer } from "./Analyzer";
import { ElementAnalysis, SerializedElementAnalysis, SerializedElementSourceAnalysis } from "./ElementAnalysis";
import { TemplateValidator, TemplateValidatorOptions } from "./validations";
/**
* This interface defines a JSON friendly serialization
* of an {Analysis}.
*/
export interface SerializedAnalysis<K extends keyof TemplateTypes> {
template: SerializedTemplateInfo<K>;
blocks: ObjectDictionary<string>;
stylesFound: string[];
// The numbers stored in each element are an index into a stylesFound;
elements: ObjectDictionary<SerializedElementAnalysis>;
}
/**
* This interface defines a JSON friendly serialization
* of an {Analysis}.
*/
export interface SerializedSourceAnalysis<K extends keyof TemplateTypes> {
template: SerializedTemplateInfo<K>;
blocks: ObjectDictionary<string>;
stylesFound: string[];
// The numbers stored in each element are an index into a stylesFound;
elements: ObjectDictionary<SerializedElementSourceAnalysis>;
}
// tslint:disable-next-line:prefer-unknown-to-any
type ElementAnalyzedCallback<BooleanExpression, StringExpression, TernaryExpression> = (element: ElementAnalysis<BooleanExpression, StringExpression, TernaryExpression>) => void;
/**
* An Analysis performs book keeping and ensures internal consistency of the block objects referenced
* within a single template. It is designed to be used as part of an AST walk over a template.
*
* 1. Call [[startElement startElement()]] at the beginning of an new html element.
* 2. Call [[addStyle addStyle(style, isDynamic)]] for all the styles used on the current html element.
* 2. Call [[addExclusiveStyle addExclusiveStyle(alwaysPresent, ...style)]] for all the styles used that are mutually exclusive on the current html element.
* 3. Call [[endElement endElement()]] when done adding styles for the current element.
*/
export class Analysis<K extends keyof TemplateTypes> {
idGenerator: IdentGenerator;
template: TemplateTypes[K];
/**
* A per-element correlation of styles used. The current correlation is added
* to this list when [[endElement]] is called.
*/
// tslint:disable-next-line:prefer-unknown-to-any
elements: Map<string, ElementAnalysis<any, any, any>>;
/**
* A map from a local name for the block to the [[Block]].
* The local name must be a legal CSS ident/class name but this is not validated here.
* See [[CLASS_NAME_IDENT]] for help validating a legal class name.
*/
private blocks: ObjectDictionary<Block>;
/**
* The current element, created when calling [[startElement]].
* The current element is unset after calling [[endElement]].
*/
// tslint:disable-next-line:prefer-unknown-to-any
private currentElement: ElementAnalysis<any, any, any> | undefined;
/**
* Template validator instance to verify blocks applied to an element.
*/
private validator: TemplateValidator;
/**
* Callback when an element is done being analyzed.
* The element analysis will be sealed.
*/
// tslint:disable-next-line:prefer-unknown-to-any
onElementAnalyzed?: ElementAnalyzedCallback<any, any, any>;
/**
* @param template The template being analyzed.
*/
// tslint:disable-next-line:prefer-unknown-to-any
constructor(template: TemplateTypes[K], options?: TemplateValidatorOptions, onElementAnalyzed?: ElementAnalyzedCallback<any, any, any>) {
this.idGenerator = new IdentGenerator();
this.template = template;
this.blocks = {};
this.elements = new Map();
this.validator = new TemplateValidator(options);
this.onElementAnalyzed = onElementAnalyzed;
}
/**
* Return the number of blocks discovered in this Template.
*/
blockCount(): number { return Object.keys(this.blocks).length; }
/**
* Convenience setter for adding a block to the template scope.
*/
addBlock(name: string, block: Block): Block { return this.blocks[name] = block; }
/**
* Convenience getter for fetching a block from the template scope.
*/
getBlock(name: string): Block | undefined { return this.blocks[name]; }
/**
* Return the number of elements discovered in this Analysis.
*/
elementCount(): number { return this.elements.size; }
/**
* Get the nth element discovered in this Analysis.
*/
getElement<BooleanExpression, StringExpression, TernaryExpression>(idx: number): ElementAnalysis<BooleanExpression, StringExpression, TernaryExpression> {
let mapIter = this.elements.entries();
let el = mapIter.next().value;
for (let i = 0; i < idx; i++) {
el = mapIter.next().value;
}
return el[1];
}
/**
* Return the number of styles discovered in this Analysis' Template.
* TODO: This is slow. We can pre-compute this as elements are added.
*/
styleCount(): number {
let c = 0;
for (let el of this.elements.values()) {
for (let _s of el.attributesFound()) {
c++;
}
for (let _s of el.classesFound()) {
c++;
}
}
return c;
}
/**
* Get an Element by ID.
*/
getElementById<BooleanExpression, StringExpression, TernaryExpression>(id: string): ElementAnalysis<BooleanExpression, StringExpression, TernaryExpression> | undefined {
return this.elements.get(id);
}
/**
* Returns the local name of the provided in this Analysis' template.
* @param block The block for which the local name should be returned.
* @return The local name of the given block.
*/
getBlockName(block: Block): string | null {
for (let name of Object.keys(this.blocks)) {
let searchBlock = this.blocks[name];
let blockName = this._searchForBlock(block, searchBlock, name);
if (blockName !== null) {
return blockName;
}
}
return null;
}
_searchForBlock(blockToFind: Block, block: Block, parentPath: string): string | null {
if (block === blockToFind) {
return parentPath;
}
// we collect these name/block pairs first, so we can early exit the next loop.
let blockRefs = new Array<[string, Block]>();
block.eachBlockReference((name, refBlock) => {
blockRefs.push([name, refBlock]);
});
for (let [name, refBlock] of blockRefs) {
let currentSearchPath = `${parentPath}>${name}`;
let rv = this._searchForBlock(blockToFind, refBlock, currentSearchPath);
if (rv !== null) {
return rv;
}
}
return null;
}
/**
* Get a new {ElementAnalysis} object to track an individual element's {Style}
* consumption in this {Analysis}' template. Every {ElementAnalysis} returned from
* `Analysis.startElement` must be passed to `Analysis.endElement` before startElement
* is called again.
* @param location The {SourceLocation} of this element in the template.
* @param tagName Optional. The tag name of the element being represented by this {ElementAnalysis}.
* @returns A new {ElementAnalysis}.
*/
startElement<BooleanExpression, StringExpression, TernaryExpression>(location: SourceLocation | SourcePosition, tagName?: string): ElementAnalysis<BooleanExpression, StringExpression, TernaryExpression> {
if (isSourcePosition(location)) { location = {start: location}; }
if (this.currentElement) {
throw new Error("Internal Error: failure to call endElement before previous call to startElement.");
}
this.currentElement = new ElementAnalysis<BooleanExpression, StringExpression, TernaryExpression>(location, this.reservedClassNames(), tagName, this.idGenerator.nextIdent());
return this.currentElement;
}
/**
* Finish an {ElementAnalysis} object returned from `Analysis.startElement` to
* the end location in source and save {Style} usage on the parent {Analysis}.
* @param element The {ElementAnalysis} we are finishing.
* @param endPosition Optional. The location in code where this element tag is closed..
*/
endElement<BooleanExpression, StringExpression, TernaryExpression>(element: ElementAnalysis<BooleanExpression, StringExpression, TernaryExpression>, endPosition?: SourcePosition): void {
if (this.currentElement !== element) {
throw new Error("Element is not the current element.");
}
if (endPosition) { element.sourceLocation.end = endPosition; }
if (!element.id) { element.id = this.idGenerator.nextIdent(); }
if (this.elements.get(element.id)) {
throw new Error(`Internal Error: an element with id = ${element.id} already exists in this analysis`);
}
this.ensureFilename(element.sourceLocation.start);
this.ensureFilename(element.sourceLocation.end);
if (!element.sealed) { element.seal(); }
this.validator.validate(this, element);
this.elements.set(element.id, element);
if (this.onElementAnalyzed) {
this.onElementAnalyzed(element);
}
this.currentElement = undefined;
}
/**
* Given a {SourcePosition}, ensure that the file name is present. If not,
* add the template identifier.
* @param post The {SourcePosition} we are validating.
*/
private ensureFilename(pos: SourcePosition | undefined) {
if (pos && !pos.filename) {
pos.filename = this.template.identifier;
}
}
/**
* @return The local name for the block object using the local prefix for the block.
*/
serializedName(o: Style): string {
let blockName = this.getBlockName(o.block);
if (blockName === null) {
throw new Error(`Block ${o.block.identifier} is not registered in the dependency graph for this analysis.`);
}
return `${blockName}${o.asSource()}`;
}
/**
* All the blocks referenced by this analysis.
*/
referencedBlocks(): Block[] {
return objectValues(this.blocks);
}
/**
* For now, returns all aliases referenced by this block and all the blocks they
* reference recursively. We can add more to this set in future
*/
reservedClassNames(): Set<string> {
let aliases = new Set<string>();
let blocks = this.transitiveBlockDependencies();
blocks.forEach(block => {
block.getAllStyleAliases().forEach(alias => aliases.add(alias));
});
return aliases;
}
/**
* All the blocks referenced by this block and all the blocks they reference recursively.
*/
transitiveBlockDependencies(): Set<Block> {
let deps = new Set<Block>();
this.referencedBlocks().forEach((block) => {
deps.add(block);
let moreDeps = block.transitiveBlockDependencies();
if (moreDeps.size > 0) {
deps = new Set([...deps, ...moreDeps]);
}
});
return deps;
}
/**
* All bhe blocks this block depends on. Same as referenced blocks except for the return type.
*/
blockDependencies(): Set<Block> {
return new Set<Block>(this.referencedBlocks());
}
*stylesFound(dynamic?: boolean): IterableIterator<Style> {
let found = new Set<Style>();
for (let el of this.elements.values()) {
for (let s of el.classesFound(dynamic)) {
if (found.has(s)) continue;
found.add(s);
yield s;
}
for (let s of el.attributesFound(dynamic)) {
if (found.has(s)) continue;
found.add(s);
yield s;
}
}
}
serializeSource(blockPaths?: Map<Block, string>): SerializedSourceAnalysis<K> {
let elements: ObjectDictionary<SerializedElementSourceAnalysis> = {};
let { template, blocks, stylesFound, styleIndexes } = this._serializeSetup(blockPaths);
// Serialize all discovered Elements.
this.elements.forEach((el, key) => {
elements[key] = el.serializeSourceAnalysis(styleIndexes);
});
// Return serialized Analysis object.
return { template, blocks, stylesFound, elements };
}
/**
* Generates a [[SerializedTemplateAnalysis]] for this analysis.
*/
serialize(blockPaths?: Map<Block, string>): SerializedAnalysis<K> {
let elements: ObjectDictionary<SerializedElementAnalysis> = {};
let { template, blocks, stylesFound, styleIndexes } = this._serializeSetup(blockPaths);
// Serialize all discovered Elements.
this.elements.forEach((el, key) => {
elements[key] = el.serialize(styleIndexes);
});
// Return serialized Analysis object.
return { template, blocks, stylesFound, elements };
}
_serializeSetup(blockPaths?: Map<Block, string>) {
let blocks = {};
let stylesFound: string[] = [];
let template = this.template.serialize() as SerializedTemplateInfo<K>;
let styleNameMap = new Map<Style, string>();
let styleIndexes = new Map<Style, number>();
let blocksUsed = new Set<Block>();
let styles = [...this.stylesFound()];
for (let found of styles) {
blocksUsed.add(found.block);
styleNameMap.set(found, this.serializedName(found));
}
// Sort our found styles into an array.
styles.sort((a, b) => {
return styleNameMap.get(a)! > styleNameMap.get(b)! ? 1 : -1;
});
styles.forEach((s, idx) => {
stylesFound.push(styleNameMap.get(s)!);
styleIndexes.set(s, idx);
});
// Serialize our blocks to a map of their local names.
Object.keys(this.blocks).forEach((localName) => {
let block = this.blocks[localName];
if (blocksUsed.has(block)) {
blocks[localName] = blockPaths && blockPaths.get(block) || block.identifier;
}
});
return { template, blocks, stylesFound, styleIndexes };
}
/**
* Creates a TemplateAnalysis from its serialized form.
* @param serializedAnalysis The analysis to be recreated.
* @param blockFactory for loading blocks referenced in the serialization.
* @param parent The analyzer this analysis will belong to.
*/
static async deserializeSource (
serializedAnalysis: SerializedSourceAnalysis<keyof TemplateTypes>,
blockFactory: BlockFactory,
parent: Analyzer<keyof TemplateTypes>,
): Promise<Analysis<keyof TemplateTypes>> {
let blockNames = Object.keys(serializedAnalysis.blocks);
let info = TemplateInfoFactory.deserialize<keyof TemplateTypes>(serializedAnalysis.template);
let analysis = parent.newAnalysis(info);
let blockPromises = new Array<Promise<{name: string; block: Block}>>();
blockNames.forEach(n => {
let blockIdentifier = serializedAnalysis.blocks[n];
let promise = blockFactory.getBlock(blockIdentifier).then(block => {
return {name: n, block: block};
});
blockPromises.push(promise);
});
let values = await allDone(blockPromises);
// Create a temporary block so we can take advantage of `Block.lookup`
// to easily resolve all BlockPaths referenced in the serialized analysis.
// TODO: We may want to abstract this so we're not making a temporary block.
let localScope = new Block("analysis-block", "tmp", "analysis-block");
values.forEach(o => {
analysis.blocks[o.name] = o.block;
localScope.addBlockReference(o.name, o.block);
});
// We lookup each style by its serialized reference.
// The index into the array is used elsewhere in this
// serialized form to reference these styles.
let styles = new Array<Style>();
serializedAnalysis.stylesFound.forEach(s => {
let style = localScope.find(s);
if (style) {
styles.push(style);
} else {
throw new Error(`Cannot resolve ${s} to a block style.`);
}
});
// These are convenience accessors into the styles array that perform
// bounds and type checking assertions.
let styleRef = (index: number) => {
let s = styles[index];
if (!s) {
throw new Error("[internal error] Style index out of bounds!");
}
return s;
};
let classRef = (index: number) => {
let s = styleRef(index);
if (!(s instanceof BlockClass)) {
throw new Error("[internal error] Block class expected.");
}
return s;
};
let attrValueRef = (index: number) => {
let s = styleRef(index);
if (!(s instanceof AttrValue)) {
throw new Error("[internal error] attribute value expected.");
}
return s;
};
let elementNames = Object.keys(serializedAnalysis.elements);
elementNames.forEach((elID) => {
let data = serializedAnalysis.elements[elID];
let element = new ElementAnalysis<null, null, null>(data.sourceLocation || {start: POSITION_UNKNOWN}, parent.reservedClassNames(), data.tagName, elID);
for (let analyzedStyle of data.analyzedStyles) {
ElementAnalysis.deserializeAnalyzedStyle(element, analyzedStyle, styleRef, classRef, attrValueRef);
}
element.seal();
analysis.elements.set(elID, element);
if (analysis.onElementAnalyzed) analysis.onElementAnalyzed(element);
});
return analysis;
}
// XXX `deserialize` doesn't actually deserialize the elements in the
// XXX serialized form. Thankfully, this method is never used.
// TODO: Get rid of this serialized form and use the "source serialization"
// TODO: as the only serialization because it's a better format for serializing
// TODO: this data.
/**
* Creates a TemplateAnalysis from its serialized form.
*
* **DO NOT USE THIS METHOD, ITS NOT FULLY IMPLEMENTED.**
* @param serializedAnalysis The analysis to be recreated.
* @param blockFactory for loading blocks referenced in the serialization.
* @param parent The analyzer this analysis will belong to.
*/
static async deserialize (
serializedAnalysis: SerializedAnalysis<keyof TemplateTypes>,
blockFactory: BlockFactory,
parent: Analyzer<keyof TemplateTypes>,
): Promise<Analysis<keyof TemplateTypes>> {
let blockNames = Object.keys(serializedAnalysis.blocks);
let info = TemplateInfoFactory.deserialize<keyof TemplateTypes>(serializedAnalysis.template);
let analysis = parent.newAnalysis(info);
let blockPromises = new Array<Promise<{name: string; block: Block}>>();
blockNames.forEach(n => {
let blockIdentifier = serializedAnalysis.blocks[n];
let promise = blockFactory.getBlock(blockIdentifier).then(block => {
return {name: n, block: block};
});
blockPromises.push(promise);
});
let values = await allDone(blockPromises);
// Create a temporary block so we can take advantage of `Block.lookup`
// to easily resolve all BlockPaths referenced in the serialized analysis.
// TODO: We may want to abstract this so we're not making a temporary block.
let localScope = new Block("analysis-block", "tmp", "analysis-block");
values.forEach(o => {
analysis.blocks[o.name] = o.block;
localScope.addBlockReference(o.name, o.block);
});
let objects = new Array<Style>();
serializedAnalysis.stylesFound.forEach(s => {
let style = localScope.find(s);
if (style) {
objects.push(style);
} else {
throw new Error(`Cannot resolve ${s} to a block style.`);
}
});
let elementNames = Object.keys(serializedAnalysis.elements);
elementNames.forEach((elID) => {
let data = serializedAnalysis.elements[elID];
let element = new ElementAnalysis<null, null, null>(data.sourceLocation || {start: POSITION_UNKNOWN}, parent.reservedClassNames(), data.tagName, elID);
element.seal();
analysis.elements.set(elID, element);
});
return analysis;
}
forOptimizer(config: ResolvedConfiguration): OptimizationTemplateAnalysis<K> {
let optAnalysis = new OptimizationTemplateAnalysis<K>(this.template);
for (let element of this.elements.values()) {
let result = element.forOptimizer(config);
optAnalysis.elements.push(result[0]);
}
return optAnalysis;
}
}
export interface IAnalysis<K extends keyof TemplateTypes> extends Analysis<K> {}