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

Statements: 100% (33 / 33)      Branches: 100% (8 / 8)      Functions: 100% (6 / 6)      Lines: 100% (31 / 31)      Ignored: none     

All files » 6to5/transformation/transformers/ » es6-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 50 51 52 53 54 55 56 57 58 591 1   1 13     1 1 1   1 1   1 3 3     1         1   1     1 8   8 17   17 17 9 9       8   6 6   6   6 7     6   2      
var t = require("../../types");
var _ = require("lodash");
 
var buildBinaryExpression = function (left, right) {
  return t.binaryExpression("+", left, right);
};
 
exports.TaggedTemplateExpression = function (node, parent, file) {
  var args = [];
  var quasi = node.quasi;
 
  var strings = [];
  var raw = [];
 
  _.each(quasi.quasis, function (elem) {
    strings.push(t.literal(elem.value.cooked));
    raw.push(t.literal(elem.value.raw));
  });
 
  args.push(t.callExpression(file.addDeclaration("tagged-template-literal"), [
    t.arrayExpression(strings),
    t.arrayExpression(raw)
  ]));
 
  args = args.concat(quasi.expressions);
 
  return t.callExpression(node.tag, args);
};
 
exports.TemplateLiteral = function (node) {
  var nodes = [];
 
  _.each(node.quasis, function (elem) {
    nodes.push(t.literal(elem.value.raw));
 
    var expr = node.expressions.shift();
    if (expr) {
      if (t.isBinary(expr)) expr = t.parenthesizedExpression(expr);
      nodes.push(expr);
    }
  });
 
  if (nodes.length > 1) {
    // remove redundant '' at the end of the expression
    var last = _.last(nodes);
    if (t.isLiteral(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];
  }
};