@@ -11,16 +11,16 @@ import {
11
11
TemplateInfoFactory ,
12
12
TemplateTypes ,
13
13
} from "@opticss/template-api" ;
14
- import { ObjectDictionary , assertNever , objectValues } from "@opticss/util" ;
14
+ import { ObjectDictionary , objectValues } from "@opticss/util" ;
15
15
import { IdentGenerator } from "opticss" ;
16
16
17
17
import { BlockFactory } from "../BlockParser" ;
18
- import { AttrValue , Attribute , Block , BlockClass , Style } from "../BlockTree" ;
18
+ import { AttrValue , Block , BlockClass , Style } from "../BlockTree" ;
19
19
import { ResolvedConfiguration } from "../configuration" ;
20
20
import { allDone } from "../util" ;
21
21
22
22
import { Analyzer } from "./Analyzer" ;
23
- import { DynamicClasses , ElementAnalysis , FalseCondition , SerializedElementAnalysis , SerializedElementSourceAnalysis , TrueCondition , hasAttrValue , hasDependency , isAttrGroup , isConditional , isFalseCondition , isStaticClass , isSwitch , isTrueCondition } from "./ElementAnalysis" ;
23
+ import { ElementAnalysis , SerializedElementAnalysis , SerializedElementSourceAnalysis } from "./ElementAnalysis" ;
24
24
import { TemplateValidator , TemplateValidatorOptions } from "./validations" ;
25
25
26
26
/**
@@ -183,7 +183,7 @@ export class Analysis<K extends keyof TemplateTypes> {
183
183
}
184
184
185
185
_searchForBlock ( blockToFind : Block , block : Block , parentPath : string ) : string | null {
186
- if ( block === blockToFind || block . isAncestorOf ( blockToFind ) ) {
186
+ if ( block === blockToFind ) {
187
187
return parentPath ;
188
188
}
189
189
@@ -391,8 +391,8 @@ export class Analysis<K extends keyof TemplateTypes> {
391
391
/**
392
392
* Creates a TemplateAnalysis from its serialized form.
393
393
* @param serializedAnalysis The analysis to be recreated.
394
- * @param options The plugin options that are used to parse the blocks .
395
- * @param postcssImpl The instance of postcss that should be used to parse the block's css .
394
+ * @param blockFactory for loading blocks referenced in the serialization .
395
+ * @param parent The analyzer this analysis will belong to .
396
396
*/
397
397
static async deserializeSource (
398
398
serializedAnalysis : SerializedSourceAnalysis < keyof TemplateTypes > ,
@@ -420,30 +420,38 @@ export class Analysis<K extends keyof TemplateTypes> {
420
420
analysis . blocks [ o . name ] = o . block ;
421
421
localScope . addBlockReference ( o . name , o . block ) ;
422
422
} ) ;
423
- let objects = new Array < Style > ( ) ;
423
+
424
+ // We lookup each style by its serialized reference.
425
+ // The index into the array is used elsewhere in this
426
+ // serialized form to reference these styles.
427
+ let styles = new Array < Style > ( ) ;
424
428
serializedAnalysis . stylesFound . forEach ( s => {
425
429
let style = localScope . find ( s ) ;
426
430
if ( style ) {
427
- objects . push ( style ) ;
431
+ styles . push ( style ) ;
428
432
} else {
429
433
throw new Error ( `Cannot resolve ${ s } to a block style.` ) ;
430
434
}
431
435
} ) ;
432
436
437
+ // These are convenience accessors into the styles array that perform
438
+ // bounds and type checking assertions.
433
439
let styleRef = ( index : number ) => {
434
- let s = objects [ index ] ;
440
+ let s = styles [ index ] ;
435
441
if ( ! s ) {
436
442
throw new Error ( "[internal error] Style index out of bounds!" ) ;
437
443
}
438
444
return s ;
439
445
} ;
446
+
440
447
let classRef = ( index : number ) => {
441
448
let s = styleRef ( index ) ;
442
449
if ( ! ( s instanceof BlockClass ) ) {
443
450
throw new Error ( "[internal error] Block class expected." ) ;
444
451
}
445
452
return s ;
446
453
} ;
454
+
447
455
let attrValueRef = ( index : number ) => {
448
456
let s = styleRef ( index ) ;
449
457
if ( ! ( s instanceof AttrValue ) ) {
@@ -457,57 +465,27 @@ export class Analysis<K extends keyof TemplateTypes> {
457
465
let data = serializedAnalysis . elements [ elID ] ;
458
466
let element = new ElementAnalysis < null , null , null > ( data . sourceLocation || { start : POSITION_UNKNOWN } , parent . reservedClassNames ( ) , data . tagName , elID ) ;
459
467
for ( let analyzedStyle of data . analyzedStyles ) {
460
- if ( isStaticClass ( analyzedStyle ) ) {
461
- element . addStaticClass ( < BlockClass > styleRef ( analyzedStyle . klass ) ) ;
462
- } else if ( isConditional ( analyzedStyle ) && ( isTrueCondition ( analyzedStyle ) || isFalseCondition ( analyzedStyle ) ) ) {
463
- let dynClasses : Partial < DynamicClasses < null > > = { condition : null } ;
464
- if ( isTrueCondition ( analyzedStyle ) ) {
465
- ( < TrueCondition < BlockClass > > dynClasses ) . whenTrue = analyzedStyle . whenTrue . map ( c => classRef ( c ) ) ;
466
- }
467
- if ( isFalseCondition ( analyzedStyle ) ) {
468
- ( < FalseCondition < BlockClass > > dynClasses ) . whenFalse = analyzedStyle . whenFalse . map ( c => classRef ( c ) ) ;
469
- }
470
- element . addDynamicClasses ( < Required < DynamicClasses < null > > > dynClasses ) ;
471
- } else if ( hasDependency ( analyzedStyle ) && hasAttrValue ( analyzedStyle ) ) {
472
- let value = attrValueRef ( analyzedStyle . value [ 0 ] ) ;
473
- let container = classRef ( analyzedStyle . container ) ;
474
- if ( isConditional ( analyzedStyle ) ) {
475
- element . addDynamicAttr ( container , value , null ) ;
476
- } else {
477
- element . addStaticAttr ( container , value ) ;
478
- }
479
- } else if ( hasDependency ( analyzedStyle ) && isAttrGroup ( analyzedStyle ) && isSwitch ( analyzedStyle ) ) {
480
- let container = classRef ( analyzedStyle . container ) ;
481
- let group : Attribute | undefined ;
482
- // Because the attribute is resolved into styles for serialization
483
- // we have to find the attribute that is in the most specific sub-block
484
- // of this attribute group.
485
- for ( let attrValueIdx of Object . values ( analyzedStyle . group ) ) {
486
- let attrValue = attrValueRef ( attrValueIdx ) ;
487
- if ( ! group ) {
488
- group = attrValue . attribute ;
489
- } else if ( group . block . isAncestorOf ( attrValue . block ) ) {
490
- group = attrValue . attribute ;
491
- }
492
- }
493
- element . addDynamicGroup ( container , group ! , null , analyzedStyle . disallowFalsy ) ;
494
- } else {
495
- assertNever ( analyzedStyle ) ;
496
- }
468
+ ElementAnalysis . deserializeAnalyzedStyle ( element , analyzedStyle , styleRef , classRef , attrValueRef ) ;
497
469
}
498
470
element . seal ( ) ;
499
471
analysis . elements . set ( elID , element ) ;
500
472
} ) ;
501
473
502
- // tslint:disable-next-line:prefer-unknown-to-any
503
474
return analysis ;
504
475
}
505
476
477
+ // XXX `deserialize` doesn't actually deserialize the elements in the
478
+ // XXX serialized form. Thankfully, this method is never used.
479
+ // TODO: Get rid of this serialized form and use the "source serialization"
480
+ // TODO: as the only serialization because it's a better format for serializing
481
+ // TODO: this data.
506
482
/**
507
483
* Creates a TemplateAnalysis from its serialized form.
484
+ *
485
+ * **DO NOT USE THIS METHOD, ITS NOT FULLY IMPLEMENTED.**
508
486
* @param serializedAnalysis The analysis to be recreated.
509
- * @param options The plugin options that are used to parse the blocks .
510
- * @param postcssImpl The instance of postcss that should be used to parse the block's css .
487
+ * @param blockFactory for loading blocks referenced in the serialization .
488
+ * @param parent The analyzer this analysis will belong to .
511
489
*/
512
490
static async deserialize (
513
491
serializedAnalysis : SerializedAnalysis < keyof TemplateTypes > ,
@@ -553,7 +531,6 @@ export class Analysis<K extends keyof TemplateTypes> {
553
531
analysis . elements . set ( elID , element ) ;
554
532
} ) ;
555
533
556
- // tslint:disable-next-line:prefer-unknown-to-any
557
534
return analysis ;
558
535
}
559
536
0 commit comments