import { Rule , Declaration } from 'postcss' ; import { DeclarationsData } from '@types' ; import { COMMENT_TYPE , RTL_COMMENT_IGNORE_REGEXP } from '@constants' ; import shorthandDeclarationsJson from '@data/shorthand-declarations.json' ; import notShorthandDeclarationsJson from '@data/not-shorthand-declarations.json' ; import initialValuesJson from '@data/initial-values.json' ; const declarationsData = shorthandDeclarationsJson ; const notShorthandDeclarationsArray = notShorthandDeclarationsJson ; const initialValuesData = initialValuesJson ; const declarations = { } ; const allDeclarations = { } ; const initialValues = { } ; const getOverrideTree = ( prop ) : string [ ] => { const overridenProp = declarationsData [ prop ] . overridden ; const overridden = overridenProp ? [ overridenProp ] . concat ( getOverrideTree ( overridenProp ) ) : [ ] ; return overridden ; } ; Object . keys ( declarationsData ) . forEach ( ( prop ) : void => { const overrideTree = getOverrideTree ( prop ) ; declarations [ prop ] = overrideTree ; declarationsData [ prop ] . overrides . forEach ( ( oprop ) : void => { declarations [ oprop ] = [ prop ] . concat ( overrideTree ) ; } ) ; } ) ; Object . assign ( allDeclarations , declarations ) ; notShorthandDeclarationsArray . forEach ( ( declaration ) : void => { allDeclarations [ declaration ] = [ ] ; } ) ; Object . keys ( initialValuesData ) . forEach ( ( value ) : void => { initialValuesData [ value ] . forEach ( ( prop ) : void => { initialValues [ prop ] = value ; } ) ; } ) ; const appendDeclarationToRule = ( decl , rule ) : void => { const declClone = decl . clone ( ) ; const declPrev = decl . prev ( ) ; if ( declPrev && declPrev . type === COMMENT_TYPE ) { const commentClone = declPrev . clone ( ) ; rule . append ( commentClone ) ; declPrev . remove ( ) ; } rule . append ( declClone ) ; } ; const hasIgnoreDirectiveInRaws = ( decl ) : boolean => { const raws = ! ! decl . raws && ! ! decl . raws . value && decl . raws . value . raw ; if ( raws && RTL_COMMENT_IGNORE_REGEXP . test ( raws ) ) { return true ; } return false ; } ; export { declarations , allDeclarations , initialValues , appendDeclarationToRule , hasIgnoreDirectiveInRaws } ;