All files / src index.js

83.91% Statements 73/87
68.52% Branches 37/54
97.14% Functions 34/35
82.28% Lines 65/79
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256    1x                       1x     3x   2x 2x 2x 2x   2x           4x   2x     4x       2x       4x           20x 10x     10x 2x     8x         8x 4x     4x         4x   2x 4x   4x 6x   6x         6x       6x       4x         20x 20x   6x           6x 4x 4x       2x               4x                     6x       6x 16x 12x           1x 1x 1x     12x                 6x         12x                 4x   2x 4x   4x 4x         20x 20x   6x 16x 4x         6x   4x 4x       2x           2x                       4x   6x                                                                        
import adapters from './adapters'
 
const validPseudoClasses = [
  'link',
  'hover',
  'focus',
  'active',
  'visited',
  'disabled',
  'focusWithin',
  'firstChild',
  'lastChild',
]
 
const validMediaQueries = ['viewportWidth', 'viewportHeight']
 
export default function createGenerator({ adapter = 'fela' } = {}) {
  const usedAdapter = adapters.find(adapt => adapt.name === adapter)
 
  return function generate(ast, fileName) {
    const css = generateCSS(ast)
    const js = generateJS(ast, usedAdapter)
    const root = generateRoot(ast)
 
    return { _root: root, ...css, ...js }
  }
}
 
function generateRoot(ast) {
  // TODO: include fragments
  const styles = ast.body.filter(node => node.type === 'Style')
 
  const imports = styles
    .map(
      module =>
        'import { ' + module.name + " } from './" + module.name + ".elodin.js'"
    )
    .join('\n')
 
  return (
    imports +
    '\n\n' +
    'export {\n  ' +
    styles.map(module => module.name).join(',\n  ') +
    '\n}'
  )
}
 
function generateCSSValue(value, unit = true) {
  if (value.type === 'NumericLiteral') {
    return value.value + (unit ? 'px' : '')
  }
 
  if (value.type === 'FunctionExpression') {
    return (
      value.callee +
      '(' +
      value.params.map(param => generateCSSValue(param, false)).join(',') +
      ')'
    )
  }
 
  if (value.type === 'Float') {
    return value.integer + '.' + value.fractional + (unit ? 'px' : '')
  }
 
  return value.value
}
 
function generateCSS(ast) {
  // TODO: include fragments
  const styles = ast.body.filter(node => node.type === 'Style')
 
  return styles.reduce((files, module) => {
    const classes = generateClasses(module.body)
 
    files[module.name + '.elodin.css'] = classes
      .filter(selector => selector.declarations.length > 0)
      .map(selector => {
        const css = stringifyCSS(
          selector.declarations,
          module.name + selector.pseudo
        )
 
        Iif (selector.media) {
          return '@media ' + selector.media + ' {\n' + css + '\n}'
        }
 
        return css
      })
      .join('\n\n')
 
    return files
  }, {})
}
 
function generateClasses(nodes, classes = [], pseudo = '', media = '') {
  const base = nodes.filter(node => node.type === 'Declaration')
  const nesting = nodes.filter(node => node.type !== 'Declaration')
 
  classes.push({
    media,
    pseudo,
    declarations: getStaticDeclarations(base),
  })
 
  nesting.forEach(nest => {
    Eif (nest.property.type === 'Variable' && nest.property.environment) {
      if (
        nest.boolean &&
        validPseudoClasses.indexOf(nest.property.value) !== -1
      ) {
        generateClasses(
          nest.body,
          classes,
          pseudo + ':' + nest.property.value,
          media
        )
      }
 
      Iif (validMediaQueries.indexOf(nest.property.value) !== -1) {
        generateClasses(
          nest.body,
          classes,
          pseudo,
          getMediaQuery(nest.value.value, nest.property.value, nest.operator)
        )
      }
    }
  })
 
  return classes
}
 
function getStaticDeclarations(declarations) {
  return declarations
    .filter(decl => decl.value.type !== 'Variable')
    .map(declaration => ({
      property: declaration.property,
      value: generateCSSValue(declaration.value),
    }))
}
 
var uppercasePattern = /[A-Z]/g
var msPattern = /^ms-/
var cache = {}
 
function hyphenateProperty(string) {
  return string in cache
    ? cache[string]
    : (cache[string] = string
        .replace(uppercasePattern, '-$&')
        .toLowerCase()
        .replace(msPattern, '-ms-'))
}
 
function stringifyCSS(declarations, name) {
  return (
    '.' +
    name +
    ' {\n  ' +
    declarations
      .map(decl => hyphenateProperty(decl.property) + ': ' + decl.value)
      .join(';\n  ') +
    '\n' +
    '}'
  )
}
 
function generateJS(ast, adapter) {
  // TODO: include fragments
  const styles = ast.body.filter(node => node.type === 'Style')
 
  return styles.reduce((files, module) => {
    const style = generateStyle(module.body)
 
    files[module.name + '.elodin.js'] = adapter.stringify(style, module.name)
    return files
  }, {})
}
 
function generateStyle(nodes) {
  const base = nodes.filter(node => node.type === 'Declaration')
  const nestings = nodes.filter(node => node.type !== 'Declaration')
 
  const declarations = base
    .filter(decl => decl.value.type === 'Variable')
    .map(declaration => ({
      property: declaration.property,
      value: declaration.value.value,
    }))
 
  const nests = nestings
    .map(nest => {
      Eif (nest.property.type === 'Variable' && nest.property.environment) {
        if (
          nest.boolean &&
          validPseudoClasses.indexOf(nest.property.value) !== -1
        ) {
          return {
            property: ':' + nest.property.value,
            value: generateStyle(nest.body),
          }
        }
 
        Iif (validMediaQueries.indexOf(nest.property.value) !== -1) {
          return {
            property: getMediaQuery(
              nest.value.value,
              nest.property.value,
              nest.operator
            ),
            value: generateStyle(nest.body),
          }
        }
      }
    })
    .filter(nesting => nesting && nesting.value.length > 0)
 
  return [...declarations, ...nests]
}
 
function getMediaQuery(value, property, operator) {
  const dimension = property.indexOf('Height') !== -1 ? 'height' : 'width'
 
  if (operator === '=') {
    return (
      '(min-' +
      dimension +
      ': ' +
      value +
      'px) and (max-' +
      dimension +
      ': ' +
      value +
      'px)'
    )
  }
 
  if (operator === '>') {
    return '(min-' + dimension + ': ' + (value + 1) + 'px)'
  }
 
  if (operator === '>=') {
    return '(min-' + dimension + ': ' + value + 'px)'
  }
 
  if (operator === '<=') {
    return '(max-' + dimension + ': ' + value + 'px)'
  }
 
  if (operator === '<') {
    return '(max-' + dimension + ': ' + (value - 1) + 'px)'
  }
}