@@ -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 ] ) ;
0 commit comments