Code coverage report for 6to5/transformers/template-literals.js

Statements: 100% (29 / 29)      Branches: 100% (8 / 8)      Functions: 100% (7 / 7)      Lines: 100% (27 / 27)      Ignored: none     

All files » 6to5/transformers/ » 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 41 42 43 44 45 46 47 48 49 501 1   1 13     1 1 1   1 3   1   1 2     1     1 8   8 17   17 17     8   6 6   6   6 7     6   2      
var b = require("recast").types.builders;
var _ = require("lodash");
 
var buildBinaryExpression = function (left, right) {
  return b.binaryExpression("+", left, right);
};
 
exports.TaggedTemplateExpression = function (node) {
  var args = [];
  var quasi = node.quasi;
 
  var strings = quasi.quasis.map(function (elem) {
    return b.literal(elem.value.raw);
  });
  args.push(b.arrayExpression(strings));
 
  _.each(quasi.expressions, function (expr) {
    args.push(expr);
  });
 
  return b.callExpression(node.tag, args);
};
 
exports.TemplateLiteral = function (node) {
  var nodes = [];
 
  _.each(node.quasis, function (elem) {
    nodes.push(b.literal(elem.value.raw));
 
    var expr = node.expressions.shift();
    if (expr) nodes.push(expr);
  });
 
  if (nodes.length > 1) {
    // remove redundant '' at the end of the expression
    var last = _.last(nodes);
    if (last.type === "Literal" && last.value === "") nodes.pop();
 
    var root = buildBinaryExpression(nodes.shift(), nodes.shift());
 
    _.each(nodes, function (node) {
      root = buildBinaryExpression(root, node);
    });
 
    return root;
  } else {
    return nodes[0];
  }
};