Code coverage report for harmonizer/transform/template-literals.js

Statements: 100% (23 / 23)      Branches: 100% (8 / 8)      Functions: 100% (4 / 4)      Lines: 100% (23 / 23)      Ignored: none     

All files » harmonizer/transform/ » template-literals.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  1 1 1 10 4 4 4 11 11 5 5   11 7     4 1   4 1 1   3         3 6           3     1
'use strict';
var objectPattern = require('nodes');
var nodes = objectPattern.nodes;
function templateify(program) {
  program.search('#TemplateLiteral').forEach(function (node) {
    var parts = [];
    var stringFound;
    node.quasis.forEach(function (quasi, i) {
      var cooked = quasi.value.cooked;
      if (cooked) {
        stringFound = true;
        parts.push(new nodes.Literal({ value: quasi.value.cooked }));
      }
      if (i in node.expressions) {
        parts.push(node.expressions[i]);
      }
    });
    if (!stringFound) {
      parts.push(new nodes.Literal({ value: '' }));
    }
    if (parts.length === 1) {
      node.parentNode.replaceChild(node, parts[0]);
      return;
    }
    var bin = new nodes.BinaryExpression({
        operator: '+',
        left: parts.shift(),
        right: parts.pop()
      });
    parts.reduceRight(function (bin, part) {
      return bin.left = new nodes.BinaryExpression({
        operator: '+',
        left: bin.left,
        right: part
      });
    }, bin);
    node.parentNode.replaceChild(node, bin);
  });
}
exports.transform = templateify;