Code coverage report for 6to5/transformation/transformers/es7-array-comprehension.js

Statements: 100% (41 / 41)      Branches: 100% (14 / 14)      Functions: 100% (5 / 5)      Lines: 100% (38 / 38)      Ignored: none     

All files » 6to5/transformation/transformers/ » es7-array-comprehension.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 831 1   1 6   6 6   6             6 6 12 12 9       6     1 2   2     2   2 2   2   2 2         2   2     1 8 8   5 5   3     3 2       5             1 9   8 6   2      
var util = require("../../util");
var t    = require("../../types");
 
var single = function (node, file) {
  var block = node.blocks[0];
 
  var templateName = "array-expression-comprehension-map";
  if (node.filter) templateName = "array-expression-comprehension-filter";
 
  var result = util.template(templateName, {
    STATEMENT: node.body,
    FILTER:    node.filter,
    ARRAY:     file.toArray(block.right),
    KEY:       block.left
  });
 
  var aliasPossibles = [result.callee.object, result];
  for (var i in aliasPossibles) {
    var call = aliasPossibles[i];
    if (t.isCallExpression(call)) {
      call.arguments[0]._aliasFunction = true;
    }
  }
 
  return result;
};
 
var multiple = function (node, file) {
  var uid = file.generateUidIdentifier("arr");
 
  var container = util.template("array-comprehension-container", {
    KEY: uid
  });
  container.callee.expression._aliasFunction = true;
 
  var block = container.callee.body;
  var body  = block.body;
 
  var returnStatement = body.pop();
 
  body.push(exports._build(node, function () {
    return util.template("array-push", {
      STATEMENT: node.body,
      KEY:       uid
    }, true);
  }));
  body.push(returnStatement);
 
  return container;
};
 
exports._build = function (node, buildBody) {
  var self = node.blocks.shift();
  if (!self) return;
 
  var child = exports._build(node, buildBody);
  if (!child) {
    // last item
    child = buildBody();
 
    // add a filter as this is our final stop
    if (node.filter) {
      child = t.ifStatement(node.filter, t.blockStatement([child]));
    }
  }
 
  return t.forOfStatement(
    t.variableDeclaration("var", [t.variableDeclarator(self.left)]),
    self.right,
    t.blockStatement([child])
  );
};
 
exports.ComprehensionExpression = function (node, parent, file) {
  if (node.generator) return;
 
  if (node.blocks.length === 1) {
    return single(node, file);
  } else {
    return multiple(node, file);
  }
};