All files / src/utils tagged-template-literal.js

100% Statements 16/16
100% Branches 4/4
100% Functions 5/5
100% Lines 14/14
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      3558x         142x         4x 30x 59x 30x   29x                 4x 142x 28x   114x       4x 4x 4x  
/**
 * Check if a node is a tagged template literal
 */
const isTaggedTemplateLiteral = (node) => node.type === 'TaggedTemplateExpression'
 
/**
 * Check if a tagged template literal has interpolations
 */
const hasInterpolations = (node) => !node.quasi.quasis[0].tail
 
/**
 * Merges the interpolations in a parsed tagged template literals with the strings
 */
const interleave = (quasis) => (
  quasis.reduce((prev, quasi, i) => {
    if (i === 0) {
      return quasi.value.raw.replace(/(\n.*$)/, '\n')
    }
    return prev + quasi.value.raw.replace(/(\n.*$|^.+\n)/, '')
  }, '')
)
 
/**
 * Get the content of a tagged template literal
 *
 * TODO Cover edge cases
 */
const getTaggedTemplateLiteralContent = (node) => {
  if (hasInterpolations(node)) {
    return interleave(node.quasi.quasis, node.quasi.expressions)
  } else {
    return node.quasi.quasis[0].value.raw
  }
}
 
exports.isTaggedTemplateLiteral = isTaggedTemplateLiteral
exports.getTaggedTemplateLiteralContent = getTaggedTemplateLiteralContent
exports.interleave = interleave