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

Statements: 93.75% (30 / 32)      Branches: 90% (9 / 10)      Functions: 100% (4 / 4)      Lines: 93.33% (28 / 30)      Ignored: none     

All files » 6to5/lib/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 631 1 1   1   1 9   9     9   9 9   9         9   9 9         9   9     1 22 22   12 12   10     10 4       12             1 10   9    
var traverse = require("../../traverse");
var util     = require("../../util");
var t        = require("../../types");
 
exports.experimental = true;
 
var build = function (node, parent, file, scope) {
  var uid = scope.generateUidBasedOnNode(parent, file);
 
  var container = util.template("array-comprehension-container", {
    KEY: uid
  });
  container.callee._aliasFunction = true;
 
  var block = container.callee.body;
  var body  = block.body;
 
  Iif (traverse.hasType(node, "YieldExpression", t.FUNCTION_TYPES)) {
    container.callee.generator = true;
    container = t.yieldExpression(container, true);
  }
 
  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("let", [t.variableDeclarator(self.left)]),
    self.right,
    t.blockStatement([child])
  );
};
 
exports.ComprehensionExpression = function (node, parent, file, scope) {
  if (node.generator) return;
 
  return build(node, parent, file, scope);
};