google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.31k stars 1.15k forks source link

Concatenated template literals are not merged #4148

Open qftlzxfz opened 5 months ago

qftlzxfz commented 5 months ago

With --compilation_level ADVANCED, concatenated template literals are not merged into one.

let url = document.location.href;
console.log(
    `<h1>${url}</h1>`
    + `<p>The URL is ${url}.</p>`
);

url = '//example.com/';
console.log(
    `<h1>${url}</h1>`
    + `<p>The URL is ${url}.</p>`
);

const other_url = '//example.com/';
console.log(
    `<h1>${other_url}</h1>`
    + `<p>The URL is ${other_url}.</p>`
);

Output (with extra line breaks after ;):

let a=document.location.href;
console.log(`<h1>${a}</h1>`+`<p>The URL is ${a}.</p>`);
a="//example.com/";
console.log(`<h1>${a}</h1>`+`<p>The URL is ${a}.</p>`);
console.log("<h1>//example.com/</h1><p>The URL is //example.com/.</p>");

When the values are known, it converts the template string to a normal string and combines them.

I would expect this:

let a=document.location.href;
console.log(`<h1>${a}</h1><p>The URL is ${a}.</p>`);
a="//example.com/";
console.log(`<h1>${a}</h1><p>The URL is ${a}.</p>`);
console.log("<h1>//example.com/</h1><p>The URL is //example.com/.</p>");

Or is there a perhaps a reason not to join the template literals in the first two cases?