Code coverage report for harmonizer/transform/comprehensions.js

Statements: 100% (45 / 45)      Branches: 100% (4 / 4)      Functions: 100% (5 / 5)      Lines: 100% (45 / 45)      Ignored: none     

All files » harmonizer/transform/ » comprehensions.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  1 1 1 1 1 1 1 1 1 10 9 9 9 9 9 9 9           9 9 15 15 15 15 15 6   9   15   9 9 9 9 2       2   7   9 9 9 9 9 9 27   9 3 3       1
'use strict';
var objectPatternC = require('nodes');
var nodes = objectPatternC.nodes;
var objectPatternB = require('../util/self');
var getSelfId = objectPatternB.getSelfId;
var objectPatternA = require('../util/string');
var express = objectPatternA.express;
var objectPattern = require('../util/id');
var getUniqueName = objectPattern.getUniqueName;
function comprehendify(program) {
  program.search('#ComprehensionExpression').forEach(function (node) {
    var parentNode = node.parentNode;
    var blocks = node.blocks;
    var wrapper = express('(function(){})()').expression;
    var body = wrapper.callee.body.body;
    var comprehensionId = new nodes.Identifier({ name: '$' });
    var identifiers = [comprehensionId];
    var comprehensionDeclaration = new nodes.VariableDeclaration({
        declarations: [new nodes.VariableDeclarator({
            id: comprehensionId,
            init: new nodes.ArrayExpression()
          })]
      });
    var forOfRoot, forOfInnermost;
    blocks.forEach(function (block) {
      var forOfStatement = new nodes.ForOfStatement();
      forOfStatement.left = new nodes.VariableDeclaration({ declarations: [new nodes.VariableDeclarator({ id: block.left })] });
      forOfStatement.right = block.right;
      forOfStatement.body = new nodes.BlockStatement();
      if (forOfInnermost) {
        forOfInnermost.body.body.push(forOfStatement);
      } else {
        forOfRoot = forOfStatement;
      }
      forOfInnermost = forOfStatement;
    });
    var pushCallExpression = express(comprehensionId.name + '.push()');
    pushCallExpression.expression.arguments.push(node.body);
    identifiers.push(pushCallExpression.expression.callee.object);
    if (node.filter) {
      var ifStatement = new nodes.IfStatement({
          test: node.filter,
          consequent: pushCallExpression
        });
      forOfInnermost.body.body.push(ifStatement);
    } else {
      forOfInnermost.body.body.push(pushCallExpression);
    }
    var returnStatement = new nodes.ReturnStatement({ argument: comprehensionId.clone() });
    identifiers.push(returnStatement.argument);
    body.push(comprehensionDeclaration, forOfRoot, returnStatement);
    parentNode.replaceChild(node, wrapper);
    var comprehensionName = getUniqueName(wrapper.callee, 'comprehension');
    identifiers.forEach(function (id) {
      id.name = comprehensionName;
    });
    body.search('=> #ThisExpression').forEach(function (node) {
      var selfId = getSelfId(wrapper.scope());
      node.parentNode.replaceChild(node, selfId.clone());
    });
  });
}
exports.transform = comprehendify;