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

Template string placeholders not optimized away

Open rx80 opened this issue 3 years ago • 2 comments

Compiler version: v20220405

Input code:

let x="test";
console.log(`${Math.random()}/${x}`);

Generated output with -O ADVANCED:

console.log(`${Math.random()}/${"test"}`);

Expected output:

console.log(`${Math.random()}/test`);

rx80 avatar Apr 28 '22 16:04 rx80

I don't believe we've ever tried to implement this optimization.

It'd be a good fit for https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeFoldConstants.java. We'd need to back off on tagged template literals though.

lauraharker avatar Apr 28 '22 20:04 lauraharker

Just a little bit more information:

Currently placeholders do get optimized away if all of them can be:

let x="test";
console.log(`${x}/${x}`);

becomes:

console.log("test/test");

So it seems that step of removing the placeholders is there, just not done at the right stage.

rx80 avatar May 17 '22 10:05 rx80

Encountering issues with quotation escaping and for each different case requires further investigation. Upon evaluation, the amount of characters that are saved due to inlining are not considered significant enough to pursue further.

dloyda avatar Jan 10 '24 22:01 dloyda