@@ -27,8 +27,8 @@ import { TemplateValidator, TemplateValidatorOptions } from "./validations";
27
27
* This interface defines a JSON friendly serialization
28
28
* of an {Analysis}.
29
29
*/
30
- export interface SerializedAnalysis {
31
- template : SerializedTemplateInfo < keyof TemplateTypes > ;
30
+ export interface SerializedAnalysis < K extends keyof TemplateTypes > {
31
+ template : SerializedTemplateInfo < K > ;
32
32
blocks : ObjectDictionary < string > ;
33
33
stylesFound : string [ ] ;
34
34
// The numbers stored in each element are an index into a stylesFound;
@@ -50,36 +50,36 @@ export class Analysis<K extends keyof TemplateTypes> {
50
50
parent ?: Analyzer < K > ;
51
51
template : TemplateTypes [ K ] ;
52
52
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
-
60
53
/**
61
54
* A per-element correlation of styles used. The current correlation is added
62
55
* to this list when [[endElement]] is called.
63
56
*/
64
57
// tslint:disable-next-line:prefer-whatever-to-any
65
58
elements : Map < string , ElementAnalysis < any , any , any > > ;
66
59
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
+
67
67
/**
68
68
* The current element, created when calling [[startElement]].
69
69
* The current element is unset after calling [[endElement]].
70
70
*/
71
71
// tslint:disable-next-line:prefer-whatever-to-any
72
- currentElement : ElementAnalysis < any , any , any > | undefined ;
72
+ private currentElement : ElementAnalysis < any , any , any > | undefined ;
73
73
74
74
/**
75
75
* Template validator instance to verify blocks applied to an element.
76
76
*/
77
- validator : TemplateValidator ;
77
+ private validator : TemplateValidator ;
78
78
79
79
/**
80
80
* @param template The template being analyzed.
81
81
*/
82
- constructor ( template : TemplateTypes [ K ] , options ?: TemplateValidatorOptions , parent ?: Analyzer < K > ) {
82
+ constructor ( parent : Analyzer < K > , template : TemplateTypes [ K ] , options ?: TemplateValidatorOptions ) {
83
83
this . idGenerator = new IdentGenerator ( ) ;
84
84
this . parent = parent ;
85
85
this . template = template ;
@@ -96,7 +96,7 @@ export class Analysis<K extends keyof TemplateTypes> {
96
96
/**
97
97
* Convenience setter for adding a block to the template scope.
98
98
*/
99
- addBlock ( name : string , block : Block ) : void { this . blocks [ name ] = block ; }
99
+ addBlock ( name : string , block : Block ) : Block { return this . blocks [ name ] = block ; }
100
100
101
101
/**
102
102
* Convenience getter for fetching a block from the template scope.
@@ -300,11 +300,11 @@ export class Analysis<K extends keyof TemplateTypes> {
300
300
/**
301
301
* Generates a [[SerializedTemplateAnalysis]] for this analysis.
302
302
*/
303
- serialize ( ) : SerializedAnalysis {
303
+ serialize ( ) : SerializedAnalysis < K > {
304
304
let blocks = { } ;
305
305
let stylesFound : string [ ] = [ ] ;
306
306
let elements : ObjectDictionary < SerializedElementAnalysis > = { } ;
307
- let template = this . template . serialize ( ) ;
307
+ let template = this . template . serialize ( ) as SerializedTemplateInfo < K > ;
308
308
let styleNameMap = new Map < Style , string > ( ) ;
309
309
let styleIndexes = new Map < Style , number > ( ) ;
310
310
@@ -325,8 +325,8 @@ export class Analysis<K extends keyof TemplateTypes> {
325
325
} ) ;
326
326
327
327
// 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 ;
330
330
} ) ;
331
331
332
332
// Serialize all discovered Elements.
@@ -345,13 +345,13 @@ export class Analysis<K extends keyof TemplateTypes> {
345
345
* @param postcssImpl The instance of postcss that should be used to parse the block's css.
346
346
*/
347
347
static async deserialize (
348
- serializedAnalysis : SerializedAnalysis ,
348
+ serializedAnalysis : SerializedAnalysis < keyof TemplateTypes > ,
349
349
blockFactory : BlockFactory ,
350
350
parent : Analyzer < keyof TemplateTypes > ,
351
351
) : Promise < Analysis < keyof TemplateTypes > > {
352
352
let blockNames = Object . keys ( serializedAnalysis . blocks ) ;
353
353
let info = TemplateInfoFactory . deserialize < keyof TemplateTypes > ( serializedAnalysis . template ) ;
354
- let analysis = new Analysis ( info , { } , parent ) ;
354
+ let analysis = parent . newAnalysis ( info ) ;
355
355
let blockPromises = new Array < Promise < { name : string ; block : Block } > > ( ) ;
356
356
blockNames . forEach ( n => {
357
357
let blockIdentifier = serializedAnalysis . blocks [ n ] ;
@@ -391,8 +391,8 @@ export class Analysis<K extends keyof TemplateTypes> {
391
391
return analysis ;
392
392
}
393
393
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 ) ;
396
396
for ( let element of this . elements . values ( ) ) {
397
397
let result = element . forOptimizer ( opts ) ;
398
398
optAnalysis . elements . push ( result [ 0 ] ) ;
0 commit comments