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

Statements: 97.22% (70 / 72)      Branches: 96.43% (27 / 28)      Functions: 100% (7 / 7)      Lines: 97.06% (66 / 68)      Ignored: none     

All files » 6to5/transformation/transformers/ » es6-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 114 115 116 117 118 1191   1 31     1 1247 1859 30     1217     1 29   29   29 60 25 25     29 71 71 31 31   40       29   29     1 217 217   6 6   6 3 3     6     1 1009 1009   22   22   22 22 1   21   22   22 16   6     22 22   22 14   14         14 2 2 2   12     8     22     1 21 21   2 2   2 1   1     2    
var t = require("../../types");
 
var getSpreadLiteral = function (spread, file) {
  return file.toArray(spread.argument);
};
 
var hasSpread = function (nodes) {
  for (var i in nodes) {
    if (t.isSpreadElement(nodes[i])) {
      return true;
    }
  }
  return false;
};
 
var build = function (props, file) {
  var nodes = [];
 
  var _props = [];
 
  var push = function () {
    if (!_props.length) return;
    nodes.push(t.arrayExpression(_props));
    _props = [];
  };
 
  for (var i in props) {
    var prop = props[i];
    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();
 
  if (!t.isArrayExpression(first)) {
    nodes.unshift(first);
    first = t.arrayExpression([]);
  }
 
  return t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes);
};
 
exports.CallExpression = function (node, parent, file, scope) {
  var args = node.arguments;
  if (!hasSpread(args)) return;
 
  var contextLiteral = t.literal(null);
 
  node.arguments = [];
 
  var nodes;
  if (args.length === 1 && args[0].argument.name === 'arguments') {
    nodes = [args[0].argument];
  } else {
    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;
  var temp;
 
  if (t.isMemberExpression(callee)) {
    contextLiteral = callee.object;
 
    Iif (t.isDynamic(contextLiteral)) {
      temp = contextLiteral = scope.generateTemp(file);
      callee.object = t.assignmentExpression("=", temp, 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, file);
  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]);
};