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

Statements: 100% (74 / 74)      Branches: 100% (32 / 32)      Functions: 100% (7 / 7)      Lines: 100% (70 / 70)      Ignored: none     

All files » 6to5/lib/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 119 120 121 122 123 124 125 126 127 128 129 1301 1   1 51     1 1506 2090 50     1456     1 49   49   49 100 37 37     49 112 112 51 51   61       49   49     1 191 191   11 11   11 5 5     11     1 1276 1276   35   35   35 35 1   34   35   35 25   10     35   35 23 23 19 19   4   23   12     35     1 39 39   4   4   4 1     4   4 2   2     4 1               3      
var t = require("../../types");
var _ = require("lodash");
 
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.identifier("undefined");
 
  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;
 
  if (t.isMemberExpression(callee)) {
    var temp = scope.generateTempBasedOnNode(callee.object, file);
    if (temp) {
      callee.object = t.assignmentExpression("=", temp, callee.object);
      contextLiteral = temp;
    } else {
      contextLiteral = callee.object;
    }
    t.appendToMemberExpression(callee, 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 nativeType = t.isIdentifier(node.callee) && _.contains(t.NATIVE_TYPE_NAMES, node.callee.name);
 
  var nodes = build(args, file);
 
  if (nativeType) {
    nodes.unshift(t.arrayExpression([t.literal(null)]));
  }
 
  var first = nodes.shift();
 
  if (nodes.length) {
    args = t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes);
  } else {
    args = first;
  }
 
  if (nativeType) {
    return t.newExpression(
      t.callExpression(
        t.memberExpression(file.addHelper("bind"), t.identifier("apply")),
        [node.callee, args]
      ),
      []
    );
  } else {
    return t.callExpression(file.addHelper("apply-constructor"), [node.callee, args]);
  }
};