Code coverage report for src/parser.js

Statements: 100% (41 / 41)      Branches: 87.5% (14 / 16)      Functions: 100% (14 / 14)      Lines: 100% (34 / 34)      Ignored: none     

All files » src/ » parser.js
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    1     22 22   22 12 12 12   12 16 16       12     22 21   21 59 12         22 52 36       22 21 27 27 18     27       21     22 47     22 21 21 21 21      
import { plugin } from 'postcss';
 
const importRegexp = /^:import\((.+)\)$/;
 
export default plugin('parser', function parser(opts = {}) {
  const exportTokens = {};
  const translations = {};
 
  const fetchImport = (importNode, relativeTo, depNr) => {
    const file = importNode.selector.match(importRegexp)[1];
    const depTrace = opts.trace + String.fromCharCode(depNr);
    const exports = opts.fetch(file, opts.filename, depTrace);
 
    importNode.each(decl => {
      Eif (decl.type === 'decl') {
        translations[decl.prop] = exports[decl.value];
      }
    });
 
    importNode.removeSelf();
  };
 
  const fetchAllImports = css => {
    let imports = 0;
 
    css.each(node => {
      if (node.type === 'rule' && node.selector.match(importRegexp)) {
        fetchImport(node, css.source.input.from, imports++);
      }
    });
  };
 
  const linkImportedSymbols = css => css.eachDecl(decl => {
    Object.keys(translations).forEach(translation => {
      decl.value = decl.value.replace(translation, translations[translation]);
    });
  });
 
  const handleExport = exportNode => {
    exportNode.each(decl => {
      Eif (decl.type === 'decl') {
        Object.keys(translations).forEach(translation => {
          decl.value = decl.value.replace(translation, translations[translation]);
        });
 
        exportTokens[decl.prop] = decl.value;
      }
    });
 
    exportNode.removeSelf();
  };
 
  const extractExports = css => css.each(node => {
    if (node.type === 'rule' && node.selector === ':export') handleExport(node);
  });
 
  return css => {
    fetchAllImports(css);
    linkImportedSymbols(css);
    extractExports(css);
    css.tokens = exportTokens;
  };
});