Search code examples
javascriptgoogle-closure-compiler

Prevent to change some string from Google Closure Compiler?


Have this code:

var a = 'Start';
var b = ' here';
return (document.querySelectorAll + "").toString().toLowerCase().indexOf(a + b) == -1;

After Google Closure Compiler, this code will be:

return (document.querySelectorAll + "").toString().toLowerCase().indexOf('Start here') == -1;

How to prevent changing this string because I don't need in parameter of indexOf 'Start here', very important that will be exactly 'a + b'? Do I have specific keys above this code that will explain GCC to not compile this code/string?


Solution

  • You can use the experimental @noinline annotation which:

    Denotes a function or variable that should not be inlined by the optimizations.

    To keep both a and b preserved, use:

    function x() {
      /** @noinline */
      var a = 'Start';
      /** @noinline */
      var b = ' here';
      return (document.querySelectorAll + "").toString().toLowerCase().indexOf(a + b) == -1;
    }
    

    Result:

    function x(){var a="Start",b=" here";return-1==(document.querySelectorAll+"").toString().toLowerCase().indexOf(a+b)};
    

    Demo

    (Note that since document.querySelectorAll + "" evaluates to a string already, you don't need to call toString on it again - you can leave that part off if you want)