All files / style9/src process-references.js

100% Statements 16/16
100% Branches 8/8
100% Functions 6/6
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 538x 8x 8x     99x                                         86x   85x 11x   1x                 83x 3x         83x 86x       8x  
const { transpileCreate } = require('./transpilers/create');
const { transpileKeyframes } = require('./transpilers/keyframes');
const testASTShape = require('./utils/test-ast-shape');
 
function isPropertyCall(node, name) {
  return testASTShape(node, {
    parent: {
      type: 'MemberExpression',
      parent: {
        type: 'CallExpression',
        callee: {
          property: { name }
        },
        arguments: {
          length: 1,
          0: {
            type: 'ObjectExpression'
          }
        }
      }
    }
  });
}
 
function processReference(node, options) {
  // style9() calls are left as-is
  if (node.parentPath.isCallExpression()) return [];
 
  if (isPropertyCall(node, 'create')) return transpileCreate(node, options);
  if (isPropertyCall(node, 'keyframes')) return transpileKeyframes(node);
 
  throw node.buildCodeFrameError(
    'Unsupported use. Supported uses are: style9(), style9.create(), and style9.keyframes()'
  );
}
 
// Keyframes needs to be processed first because the result is used in create.
// The correct solution would be to process the references in the order they
// would be executed, but it's easier to just sort keyframes first
function sortReferences(references) {
  return references.sort(reference =>
    isPropertyCall(reference, 'keyframes') ? -1 : 1
  );
}
 
function processReferences(references, options) {
  return sortReferences(references).flatMap(node => {
    return processReference(node, options);
  });
}
 
module.exports = processReferences;