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

Statements: 98.33% (59 / 60)      Branches: 95% (19 / 20)      Functions: 100% (8 / 8)      Lines: 100% (56 / 56)      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 991 1 1   1 27 27 26         27     1 363 363 490 25 25     363     1 25   25   25 52 24 24     25 66 27 27   39       25   25     1 78 78   4 4   4   4     1 285 285   21   21   21 21   21 16   5     21   21 14   14 2 2 2   12     7     21    
var util = require("../../util");
var t    = require("../../types");
var _    = require("lodash");
 
var getSpreadLiteral = function (spread, file) {
  var literal = spread.argument;
  if (!t.isArrayExpression(literal)) {
    literal = util.template("call", {
      OBJECT: file.addDeclaration("slice"),
      CONTEXT: 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, file) {
  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, file));
    } else {
      _props.push(prop);
    }
  });
 
  push();
 
  return nodes;
};
 
exports.ArrayExpression = function (node, parent, file) {
  var elements = node.elements;
  if (!hasSpread(elements)) return;
 
  var nodes = build(elements, file);
  var first = nodes.shift();
 
  Iif (!nodes.length) return first;
 
  return t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes);
};
 
exports.CallExpression = function (node, parent, file) {
  var args = node.arguments;
  if (!hasSpread(args)) return;
 
  var contextLiteral = t.literal(null);
 
  node.arguments = [];
 
  var nodes = build(args, file);
  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);
};