All files / style9/src normalize-arguments.js

100% Statements 20/20
100% Branches 8/8
100% Functions 6/6
100% Lines 20/20

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 53 54 55 56 57 58 59 608x     4x 5x   5x               8x   8x             1x 1x   1x                     36x 42x 4x     38x 28x     10x 8x     2x 1x     1x       8x  
const t = require('@babel/types');
 
function normalizeObjectExpression(objectExpr) {
  return objectExpr.properties.map(prop => {
    t.assertIdentifier(prop.key);
 
    return {
      test: prop.value,
      value: prop.key.name
    };
  });
}
 
function normalizeLogicalExpression(logicalExpr) {
  t.assertStringLiteral(logicalExpr.right);
 
  return {
    test: logicalExpr.left,
    value: logicalExpr.right.value
  };
}
 
function normalizeConditionalExpression(conditionalExpr) {
  t.assertStringLiteral(conditionalExpr.consequent);
  t.assertStringLiteral(conditionalExpr.alternate);
 
  return [
    conditionalExpr.alternate.value,
    {
      test: conditionalExpr.test,
      value: conditionalExpr.consequent.value
    }
  ];
}
 
// Map resolver arguments to strings and logical ANDs
function normalizeArguments(callExpr) {
  return callExpr.get('arguments').flatMap(arg => {
    if (t.isObjectExpression(arg.node)) {
      return normalizeObjectExpression(arg.node);
    }
 
    if (t.isStringLiteral(arg.node)) {
      return arg.node.value;
    }
 
    if (t.isLogicalExpression(arg.node, { operator: '&&' })) {
      return normalizeLogicalExpression(arg.node);
    }
 
    if (t.isConditionalExpression(arg.node)) {
      return normalizeConditionalExpression(arg.node);
    }
 
    throw arg.buildCodeFrameError(`Unsupported type ${arg.node.type}`);
  });
}
 
module.exports = normalizeArguments;