import { TemplateTypes } from "@opticss/template-api" ; import { ObjectDictionary } from "@opticss/util" ; import * as errors from "../../errors" ; import { Analysis } from "../Analysis" ; import { ElementAnalysis } from "../ElementAnalysis" ; import { attributeGroupValidator } from "./attribute-group-validator" ; import { attributeParentValidator } from "./attribute-parent-validator" ; import { classPairsValidator } from "./class-pairs-validator" ; import { propertyConflictValidator } from "./property-conflict-validator" ; import { rootClassValidator } from "./root-class-validator" ; import { Validator } from "./Validator" ; export * from "./class-pairs-validator" ; export * from "./root-class-validator" ; export * from "./attribute-parent-validator" ; export * from "./property-conflict-validator" ; export interface TemplateValidators extends ObjectDictionary < Validator > { "no-root-classes" : Validator ; "no-class-pairs" : Validator ; "no-attribute-orphans" : Validator ; "no-duplicate-attribute-groups" : Validator ; "no-required-resolution" : Validator ; } export type TemplateValidatorOptions = { [ K in keyof TemplateValidators ] ? : boolean | Validator ; } ; const VALIDATORS = { "no-root-classes" : rootClassValidator , "no-class-pairs" : classPairsValidator , "no-attribute-orphans" : attributeParentValidator , "no-duplicate-attribute-groups" : attributeGroupValidator , "no-required-resolution" : propertyConflictValidator , } ; const DEFAULT_VALIDATORS = { "no-root-classes" : true , "no-class-pairs" : true , "no-attribute-orphans" : true , "no-duplicate-attribute-groups" : true , "no-required-resolution" : true , } ; param options export class TemplateValidator { private validators : Validator [ ] = [ ] ; private opts : TemplateValidatorOptions ; constructor ( options ? ) { this . opts = Object . assign ( { } , DEFAULT_VALIDATORS , options || { } ) ; let opts = this . opts ; for ( let key in opts ) { if ( opts [ key ] instanceof Function ) { this . validators . push ( opts [ key ] as Validator ) ; } else if ( ! VALIDATORS [ key ] ) { throw new errors . CssBlockError ( `Cannot find template validator "${ key }".` ) ; } else if ( opts [ key ] && VALIDATORS [ key ] ) { this . validators . push ( VALIDATORS [ key ] ) ; } } } param correlations param locInfo validate ( templateAnalysis , element ) { function err ( message , locInfo ? , details ? ) { let location = locInfo || { filename : templateAnalysis . template . identifier , start : element . sourceLocation . start , end : element . sourceLocation . end || { line : element . sourceLocation . start . line , column : ( element . sourceLocation . start . column || 0 ) + 1 } , } ; throw new errors . TemplateAnalysisError ( message , location , details ) ; } this . validators . forEach ( ( func ) => { func ( element , templateAnalysis , err ) ; } ) ; } }