title: How to convert a TemplateLiteral AST node back into template string source code?
body: <p>So I am working through <a href="https://github.com/lancejpollard/normalize-ast.js/blob/040dcbfb584e25f1fb58106956f06e0c899000d9/index.js" rel="nofollow noreferrer">&quot;normalizing&quot; a JS AST</a> in certain ways to make transpilation easier into a particular target language. So I am having to learn the inner details of the AST. Then for testing, I am converting it back into JS source code, using <a href="https://github.com/lancejpollard/normalize-ast.js/blob/af39cb30471e66a6bd4a3b0d14f6f0072186813d/print.js" rel="nofollow noreferrer">print functions</a>. I have handled pretty much every case so far, but the <code>TemplateLiteral</code> seems like it's a strange structure.</p>
  <p>For example, you have this template string:</p>
  <pre><code>const str = 'const b83 = `foo${a100} bar ${b82}`'
  </code></pre>
  <p>Parse that <code>str</code> into an AST and you get this structure:</p>
  <pre><code>{
    &quot;type&quot;: &quot;TemplateLiteral&quot;,
    &quot;start&quot;: 2692,
    &quot;end&quot;: 2715,
    &quot;expressions&quot;: [
      {
        &quot;type&quot;: &quot;Identifier&quot;,
        &quot;start&quot;: 2698,
        &quot;end&quot;: 2702,
        &quot;name&quot;: &quot;a100&quot;
      },
      {
        &quot;type&quot;: &quot;Identifier&quot;,
        &quot;start&quot;: 2710,
        &quot;end&quot;: 2713,
        &quot;name&quot;: &quot;b82&quot;
      }
    ],
    &quot;quasis&quot;: [
      {
        &quot;type&quot;: &quot;TemplateElement&quot;,
        &quot;start&quot;: 2693,
        &quot;end&quot;: 2696,
        &quot;value&quot;: {
          &quot;raw&quot;: &quot;foo&quot;,
          &quot;cooked&quot;: &quot;foo&quot;
        },
        &quot;tail&quot;: false
      },
      {
        &quot;type&quot;: &quot;TemplateElement&quot;,
        &quot;start&quot;: 2703,
        &quot;end&quot;: 2708,
        &quot;value&quot;: {
          &quot;raw&quot;: &quot; bar &quot;,
          &quot;cooked&quot;: &quot; bar &quot;
        },
        &quot;tail&quot;: false
      },
      {
        &quot;type&quot;: &quot;TemplateElement&quot;,
        &quot;start&quot;: 2714,
        &quot;end&quot;: 2714,
        &quot;value&quot;: {
          &quot;raw&quot;: &quot;&quot;,
          &quot;cooked&quot;: &quot;&quot;
        },
        &quot;tail&quot;: true
      }
    ]
  }
  </code></pre>
  <p>Notice, the &quot;expressions&quot; and &quot;quasis&quot; are not interlaced, they are made into separate arrays. How am I to go back and properly put them in the right order to regenerate the template string source code? Assume this AST node might be part of a larger AST, and the tree might be rewritten, so any information you use regarding the start and end text positions will only be useful from a relative, not absolute, perspective. I am just not quite sure the best way to read the start and end to re-interlace the expressions and quasis.</p>