<p dir="auto">Just noticed with <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="295335659" data-permission-text="Title is private" data-url="https://github.com/babel/babel/issues/7350" data-hovercard-type="issue" data-hovercard-url="/babel/babel/issues/7350/hovercard" href="https://github.com/babel/babel/issues/7350">#7350</a>, we're making a bunch of very long string arrays. What's more, they're (mostly) duplicated between the <code class="notranslate">strings</code> and <code class="notranslate">raw</code>.</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="tag`some template`;
// &quot;some template&quot; &quot;some template&quot;
_taggedTemplateLiteral([&quot;some template&quot;], [&quot;some template&quot;]);"><pre class="notranslate"><span class="pl-en">tag</span><span class="pl-s">`some template`</span><span class="pl-kos">;</span>
<span class="pl-c">// "some template" "some template"</span>
<span class="pl-en">_taggedTemplateLiteral</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-s">"some template"</span><span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-kos">[</span><span class="pl-s">"some template"</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">The only (I think) time that string arrays aren't duplicated is when an "invalid" escape sequence comes up (which are now valid, due to template literal revision).</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="tag`test${1}\xhello`;
// &quot;invalid&quot; string becomes undefined
_taggedTemplateLiteral([&quot;test&quot;, void 0], [&quot;test&quot;, &quot;\\xhello&quot;]);"><pre class="notranslate"><span class="pl-en">tag</span><span class="pl-s">`test<span class="pl-s1"><span class="pl-kos">${</span><span class="pl-c1">1</span><span class="pl-kos">}</span></span>\x<span class="pl-s1">hello</span>`</span><span class="pl-kos">;</span>
<span class="pl-c">// "invalid" string becomes undefined</span>
<span class="pl-en">_taggedTemplateLiteral</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-s">"test"</span><span class="pl-kos">,</span> <span class="pl-k">void</span> <span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-kos">[</span><span class="pl-s">"test"</span><span class="pl-kos">,</span> <span class="pl-s">"\\xhello"</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">At worst, we could optimize the "all valid strings" case using slice:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// default raw value to a slice of strings
function _taggedTemplateLiteral(strings, raw = strings.slice()) {
  return Object.freeze(Object.defineProperties(strings, {
    raw: { value: Object.freeze(raw) }
  }));
}

// all valid
tag`some template`;
_taggedTemplateLiteral([&quot;some template&quot;]);

// some invalid
tag`\xhello`;
// &quot;invalid&quot; string becomes undefined
_taggedTemplateLiteral([void 0], [&quot;\\xhello&quot;]);"><pre class="notranslate"><span class="pl-c">// default raw value to a slice of strings</span>
<span class="pl-k">function</span> <span class="pl-en">_taggedTemplateLiteral</span><span class="pl-kos">(</span><span class="pl-s1">strings</span><span class="pl-kos">,</span> <span class="pl-s1">raw</span> <span class="pl-c1">=</span> <span class="pl-s1">strings</span><span class="pl-kos">.</span><span class="pl-en">slice</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-k">return</span> <span class="pl-v">Object</span><span class="pl-kos">.</span><span class="pl-en">freeze</span><span class="pl-kos">(</span><span class="pl-v">Object</span><span class="pl-kos">.</span><span class="pl-en">defineProperties</span><span class="pl-kos">(</span><span class="pl-s1">strings</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>
    <span class="pl-c1">raw</span>: <span class="pl-kos">{</span> <span class="pl-c1">value</span>: <span class="pl-v">Object</span><span class="pl-kos">.</span><span class="pl-en">freeze</span><span class="pl-kos">(</span><span class="pl-s1">raw</span><span class="pl-kos">)</span> <span class="pl-kos">}</span>
  <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-c">// all valid</span>
<span class="pl-en">tag</span><span class="pl-s">`some template`</span><span class="pl-kos">;</span>
<span class="pl-en">_taggedTemplateLiteral</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-s">"some template"</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-c">// some invalid</span>
<span class="pl-en">tag</span><span class="pl-s">`\x<span class="pl-s1">hello</span>`</span><span class="pl-kos">;</span>
<span class="pl-c">// "invalid" string becomes undefined</span>
<span class="pl-en">_taggedTemplateLiteral</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-k">void</span> <span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-kos">[</span><span class="pl-s">"\\xhello"</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">But, realistically, we could just reuse the same string array (it's being frozen, anyways):</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function _taggedTemplateLiteral(strings, raw) {
  return Object.freeze(Object.defineProperties(strings, {
    raw: { value: raw ? Object.freeze(raw) : strings }
  }));
}"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">_taggedTemplateLiteral</span><span class="pl-kos">(</span><span class="pl-s1">strings</span><span class="pl-kos">,</span> <span class="pl-s1">raw</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-k">return</span> <span class="pl-v">Object</span><span class="pl-kos">.</span><span class="pl-en">freeze</span><span class="pl-kos">(</span><span class="pl-v">Object</span><span class="pl-kos">.</span><span class="pl-en">defineProperties</span><span class="pl-kos">(</span><span class="pl-s1">strings</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>
    <span class="pl-c1">raw</span>: <span class="pl-kos">{</span> <span class="pl-c1">value</span>: <span class="pl-s1">raw</span> ? <span class="pl-v">Object</span><span class="pl-kos">.</span><span class="pl-en">freeze</span><span class="pl-kos">(</span><span class="pl-s1">raw</span><span class="pl-kos">)</span> : <span class="pl-s1">strings</span> <span class="pl-kos">}</span>
  <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<h3 dir="auto">💻</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> Would you like to work on a fix?</li>
</ul>
<h3 dir="auto">How are you using Babel?</h3>
<p dir="auto">Programmatic API (<code class="notranslate">babel.transform</code>, <code class="notranslate">babel.parse</code>)</p>
<h3 dir="auto">Input code</h3>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="//@flow

declare module 'test' {
  import type { JSONSchema7 } from 'json-schema';
}

declare module 'test/submodule' {
  import type { JSONSchema7 } from 'json-schema';
}"><pre class="notranslate"><span class="pl-c">//<span class="pl-k">@flow</span></span>

<span class="pl-s1">declare</span> <span class="pl-smi">module</span> <span class="pl-s">'test'</span> <span class="pl-kos">{</span>
  <span class="pl-k">import</span> <span class="pl-s1">type</span> <span class="pl-kos">{</span> <span class="pl-v">JSONSchema7</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'json-schema'</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-s1">declare</span> <span class="pl-smi">module</span> <span class="pl-s">'test/submodule'</span> <span class="pl-kos">{</span>
  <span class="pl-k">import</span> <span class="pl-s1">type</span> <span class="pl-kos">{</span> <span class="pl-v">JSONSchema7</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'json-schema'</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto"><a href="https://babeljs.io/repl#?browsers=defaults%2C%20ie%2011%2C%20ie_mob%2011&amp;build=&amp;builtIns=false&amp;corejs=3.6&amp;spec=false&amp;loose=false&amp;code_lz=PTACDMBsHsHcCh4BMCmBjSBDATigBALbRICuk-A5AC4oDOVFeA3vHngJYEAO02VeVAJ5d8TPACkAygHkAcpLQALFAUwB2PAF884bNAJ4KAK1rQAdgFpaSlZgoBueJsSoMOfEVLlDNesFokAEaeZCiMLGycPHwCwqISMvI2qhrauvqGJuZWyXaOzvBAA&amp;debug=false&amp;forceAllTransforms=false&amp;shippedProposals=false&amp;circleciRepo=&amp;evaluate=false&amp;fileSize=false&amp;timeTravel=false&amp;sourceType=module&amp;lineWrap=true&amp;presets=env%2Cflow&amp;prettier=false&amp;targets=&amp;version=7.14.7&amp;externalPlugins=" rel="nofollow">REPL</a></p>
<h3 dir="auto">Configuration file name</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Configuration</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Current and expected behavior</h3>
<p dir="auto">There is an error about duplicate declaration:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Duplicate declaration &quot;JSONSchema7&quot;
   6 |
   7 | declare module 'test/submodule' {
&gt;  8 |   import type { JSONSchema7 } from 'json-schema';
     |                 ^^^^^^^^^^^
   9 | }
  10 |
  11 |"><pre class="notranslate"><span class="pl-v">Duplicate</span> <span class="pl-s1">declaration</span> <span class="pl-s">"JSONSchema7"</span>
   <span class="pl-c1">6</span> <span class="pl-c1">|</span>
   <span class="pl-c1">7</span> <span class="pl-c1">|</span> <span class="pl-s1">declare</span> <span class="pl-smi">module</span> <span class="pl-s">'test/submodule'</span> <span class="pl-kos">{</span>
<span class="pl-c1">&gt;</span>  <span class="pl-c1">8</span> <span class="pl-c1">|</span>   <span class="pl-k">import</span> <span class="pl-s1">type</span> <span class="pl-kos">{</span> JSONSchema7 <span class="pl-kos">}</span> <span class="pl-s1">from</span> <span class="pl-s">'json-schema'</span><span class="pl-kos">;</span>
     <span class="pl-c1">|</span>                 <span class="pl-c1">^</span><span class="pl-c1">^</span><span class="pl-c1">^</span><span class="pl-c1">^</span><span class="pl-c1">^</span><span class="pl-c1">^</span><span class="pl-c1">^</span><span class="pl-c1">^</span><span class="pl-c1">^</span><span class="pl-c1">^</span><span class="pl-c1">^</span>
   <span class="pl-c1">9</span> <span class="pl-c1">|</span> <span class="pl-kos">}</span>
  <span class="pl-c1">10</span> <span class="pl-c1">|</span>
  <span class="pl-c1">11</span> <span class="pl-c1">|</span></pre></div>
<p dir="auto">But, it should be allowed to have same imports in different modules</p>
<h3 dir="auto">Environment</h3>
<p dir="auto">not specific to the environment</p>
<h3 dir="auto">Possible solution</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Additional context</h3>
<p dir="auto">Found this when trying traverse an ATS from parsing flow definitions file (<a href="https://github.com/zxbodya/flowts/blob/master/packages/cli/src/detectOptions.ts">code where it happens</a>).</p>