function convertTemplateLiteralToArrayElements()

in packages/babel-plugin-fbt/src/FbtUtil.js [700:735]


function convertTemplateLiteralToArrayElements(
  moduleName: JSModuleNameType,
  node: BabelNodeTemplateLiteral,
): Array<
  BabelNodeStringLiteral | BabelNodeCallExpression | BabelNodeJSXElement,
> {
  const {expressions, quasis} = node;
  const nodes = [];

  let index = 0;
  // quasis items are the text literal portion of the template literal
  for (const item of quasis) {
    const text = item.value.cooked || '';
    if (text != '') {
      nodes.push(stringLiteral(text));
    }
    if (index < expressions.length) {
      const expression = expressions[index++];
      if (
        expression.type === 'StringLiteral' ||
        expression.type === 'CallExpression' ||
        expression.type === 'JSXElement'
      ) {
        nodes.push(expression);
      } else {
        throw errorAt(
          expression,
          `Unexpected node type: ${expression.type}. ${moduleName}() only supports ` +
            `the following syntax within template literals:` +
            `string literal, a construct like ${moduleName}.param() or a JSX element.`,
        );
      }
    }
  }
  return nodes;
}