closure-compiler
closure-compiler copied to clipboard
Template string placeholders not optimized away
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`);
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.
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.
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.