import Color from '../colors/color' ; import { DocumentLine , LineExtraction , flattenLineExtractionsFlatten } from '../util/color-util' ; import { IStrategy , Extractor } from '../extractor-mixin' ; import Variable from './variable' ; export interface IVariableStrategy extends IStrategy { extractDeclarations ( fileName , fileLines ) : Promise < number > ; extractVariables ( fileName , fileLines ) : Promise < LineExtraction [ ] > ; extractVariable ( fileName , text ) : Color ; getVariableValue ( variable ) : Color | null ; deleteVariable ( fileName , line ? ) ; variablesCount ( ) : number ; } class VariablesExtractor extends Extractor { public async extractVariables ( fileName , fileLines ) { const colors = await Promise . all ( this . enabledStrategies . map ( strategy => ( < IVariableStrategy > strategy ) . extractVariables ( fileName , fileLines ) ) ) ; return flattenLineExtractionsFlatten ( colors ) ; } public deleteVariableInLine ( fileName , line ) { this . enabledStrategies . forEach ( strategy => ( < IVariableStrategy > strategy ) . deleteVariable ( fileName , line ) ) ; } public async extractDeclarations ( fileName , fileLines ) { return Promise . all ( this . enabledStrategies . map ( strategy => ( < IVariableStrategy > strategy ) . extractDeclarations ( fileName , fileLines ) ) ) ; } public getVariablesCount ( ) { return this . enabledStrategies . reduce ( ( cv , strategy ) => cv + ( < IVariableStrategy > strategy ) . variablesCount ( ) , 0 ) ; } public findVariable ( variable ) { return ( < IVariableStrategy > this . get ( variable . type ) ) . getVariableValue ( variable ) ; } public removeVariablesDeclarations ( fileName ) { this . enabledStrategies . forEach ( strategy => ( < IVariableStrategy > strategy ) . deleteVariable ( fileName ) ) ; } } const instance = new VariablesExtractor ( ) ; export default instance ;