Code coverage report for 6to5/transformation/transformers/spread.js

Statements: 100% (69 / 69)      Branches: 100% (24 / 24)      Functions: 100% (9 / 9)      Lines: 100% (64 / 64)      Ignored: none     

All files » 6to5/transformation/transformers/ » spread.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 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 1141 1   1 30 30 29         30     1 388 388 520 28 28     388     1 28   28   28 58 25 25     28 70 30 30   40       28   28     1 83 83   5 5   5   4     1 300 300   21   21   21 21   21 16   5     21   21 14   14 2 2 2   12     7     21     1 5 5   2 2   2 1   1     2    
var t = require("../../types");
var _ = require("lodash");
 
var getSpreadLiteral = function (spread) {
  var literal = spread.argument;
  if (!t.isArrayExpression(literal)) {
    literal = t.callExpression(
      t.memberExpression(t.identifier("Array"), t.identifier("from")),
      [literal]
    );
  }
  return literal;
};
 
var hasSpread = function (nodes) {
  var has = false;
  _.each(nodes, function (node) {
    if (t.isSpreadElement(node)) {
      has = true;
      return false;
    }
  });
  return has;
};
 
var build = function (props) {
  var nodes = [];
 
  var _props = [];
 
  var push = function () {
    if (!_props.length) return;
    nodes.push(t.arrayExpression(_props));
    _props = [];
  };
 
  _.each(props, function (prop) {
    if (t.isSpreadElement(prop)) {
      push();
      nodes.push(getSpreadLiteral(prop));
    } else {
      _props.push(prop);
    }
  });
 
  push();
 
  return nodes;
};
 
exports.ArrayExpression = function (node) {
  var elements = node.elements;
  if (!hasSpread(elements)) return;
 
  var nodes = build(elements);
  var first = nodes.shift();
 
  if (!nodes.length) return first;
 
  return t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes);
};
 
exports.CallExpression = function (node) {
  var args = node.arguments;
  if (!hasSpread(args)) return;
 
  var contextLiteral = t.literal(null);
 
  node.arguments = [];
 
  var nodes = build(args);
  var first = nodes.shift();
 
  if (nodes.length) {
    node.arguments.push(t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes));
  } else {
    node.arguments.push(first);
  }
 
  var callee = node.callee;
 
  if (t.isMemberExpression(callee)) {
    contextLiteral = callee.object;
 
    if (callee.computed) {
      callee.object = t.memberExpression(callee.object, callee.property, true);
      callee.property = t.identifier("apply");
      callee.computed = false;
    } else {
      callee.property = t.memberExpression(callee.property, t.identifier("apply"));
    }
  } else {
    node.callee = t.memberExpression(node.callee, t.identifier("apply"));
  }
 
  node.arguments.unshift(contextLiteral);
};
 
exports.NewExpression = function (node, parent, file) {
  var args = node.arguments;
  if (!hasSpread(args)) return;
 
  var nodes = build(args);
  var first = nodes.shift();
 
  if (nodes.length) {
    args = t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes);
  } else {
    args = first;
  }
 
  return t.callExpression(file.addDeclaration("apply-constructor"), [node.callee, args]);
};