closure-compiler icon indicating copy to clipboard operation
closure-compiler copied to clipboard

Concatenated template literals are not merged

Open qftlzxfz opened this issue 5 months ago • 0 comments

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?

qftlzxfz avatar Feb 02 '24 14:02 qftlzxfz