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

Statements: 100% (38 / 38)      Branches: 100% (12 / 12)      Functions: 100% (4 / 4)      Lines: 100% (36 / 36)      Ignored: none     

All files » 6to5/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 661 1 1   1 17 17 16         17     1 308     1 59 59   1   1       1   1     1 249 249   16   16 16   16   16 12 12 12 12   4     16   16 12 12   4     16    
var util = require("../util");
var b    = require("recast").types.builders;
var _    = require("lodash");
 
var getSpreadLiteral = function (spread, file) {
  var literal = spread.argument;
  if (literal.type !== "ArrayExpression") {
    literal = util.template("call", {
      OBJECT: file.addDeclaration("slice"),
      CONTEXT: literal
    });
  }
  return literal;
};
 
var hasSpread = function (nodes) {
  return nodes.length && _.last(nodes).type === "SpreadElement";
};
 
exports.ArrayExpression = function (node, parent, file) {
  var elements = node.elements;
  if (!hasSpread(elements)) return;
 
  var spread = elements.pop();
 
  var concat = util.template("array-concat", {
    ARGUMENT: getSpreadLiteral(spread, file)
  });
 
  concat.callee.object.elements = elements;
 
  return concat;
};
 
exports.CallExpression = function (node, parent, file) {
  var args = node.arguments;
  if (!hasSpread(args)) return;
 
  var spread = args.pop();
 
  var spreadLiteral  = getSpreadLiteral(spread, file);
  var contextLiteral = b.literal(null);
 
  node.arguments = [];
 
  if (args.length) {
    var concat = util.template("array-concat");
    concat.arguments = [spreadLiteral];
    concat.callee.object.elements = args;
    node.arguments.push(concat);
  } else {
    node.arguments.push(spreadLiteral);
  }
 
  var callee = node.callee;
 
  if (callee.type === "MemberExpression") {
    contextLiteral = callee.object;
    callee.property = b.memberExpression(callee.property, b.identifier("apply"), false);
  } else {
    node.callee = b.memberExpression(node.callee, b.identifier("apply"), false);
  }
 
  node.arguments.unshift(contextLiteral);
};