Google closure compiler doesn't remove unreach

2019-09-12 18:01发布

问题:

Why this code doesn't result in an empty string after compilation with SIMPLE_OPTIMIZATIONS

/**
 * @define {boolean}
 */
var TEST = false;
(function() {
    if (TEST) {
        foo();
    }
})();

and instead I get the following?

var TEST=!1;(function(){TEST&&foo()})();

The if is unreachable but the closure compiler doesn't remove the code.
With "advanced optimizations" the result is what I expect (empty) but "simple optimizations" give the above result. Why this difference? The code will never be executed in both cases.

EDIT:
If I remove the closure, the if block is removed too. Why with closure this doesn't happen?

回答1:

I believe it would be removed if using "advanced optimizations"