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

Shadowed Class Variable Causes Incorrect Minification

Open jvoccia opened this issue 3 years ago • 0 comments

With code that looks like:

class SUB_CLASS extends ALIASED_CLASS {
    SOME_METHOD(t) {
        const ALIASED_CLASS = call1();
        super.SOME_METHOD(t);
   }
}

This produces code that looks like:

var SUB_CLASS = function() {
  return ALIASED_CLASS.apply(this, arguments) || this;
};
$jscomp.inherits(SUB_CLASS, ALIASED_CLASS);
SUB_CLASS.prototype.SOME_METHOD = function(a) {
  call1().prototype.SOME_METHOD.call(this, a);   // This line is in error - it should be using ALIASED_CLASS not call1();
};

It looks like closure gets confused when a function in a class aliases the class that is extended.

You can see an example of this here: https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520SIMPLE_OPTIMIZATIONS%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540formatting%2520pretty_print%250A%252F%252F%2520%2540language_in%2520ECMASCRIPT_2015%250A%252F%252F%2520%2540language_out%2520ECMASCRIPT_5%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250A%250Aclass%2520SUB_CLASS%2520extends%2520ALIASED_CLASS%2520%257B%250A%2509%2509SOME_METHOD(t)%2520%257B%250A%2509%2509%2509const%2520ALIASED_CLASS%2520%253D%2520call1()%253B%250A%2509%2509%2509super.SOME_METHOD(t)%253B%250A%2509%2509%257D%250A%257D%250A%2509

This was noticed when some 3p code that was already minified via another tool when sent through the closure compiler. The repo steps have been simplified as much as possible.

Options: @compilation_level SIMPLE_OPTIMIZATIONS @language_in ECMASCRIPT_2015 @language_out ECMASCRIPT_5

jvoccia avatar Mar 03 '22 22:03 jvoccia