TreeWalker functions overridden by unrelated class definition
Ran into this issue, and created a minimal repro example demonstrating the problem:
To Reproduce:
Run the following code at http://closure-compiler.appspot.com/home
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @formatting pretty_print
// ==/ClosureCompiler==
var AnyClass = function() {};
AnyClass.prototype.nextNode = function() {};
// Don't strip AnyClass during advanced compilation.
console.log(AnyClass);
var walker = document.createTreeWalker(document);
// Logs undefined instead of walker.nextNode().
console.log(walker.nextNode());
Expected:
The second console.log() is passed walker.nextNode().
Actual:
The second console.log() is passed void 0.
Observations:
Explicitly typing the return value (@type {!TreeWalker}) does not change the behavior.
Any of the following changes will result in the correct compiler output:
- Removing the nextNode function definition.
- Adding any content inside the nextNode function definition (even dead code like
1;).
This issue extends to other functions like previousNode on TreeWalker as well.
This is also an issue with document.createDocumentFragment():
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @formatting pretty_print
// ==/ClosureCompiler==
var AnyClass = function() {};
AnyClass.prototype.createDocumentFragment = function() {};
// Don't strip AnyClass during advanced compilation.
console.log(AnyClass);
var fragment = document.createDocumentFragment();
console.log(fragment);
Maybe something is messed up with the TreeWalker externs? https://github.com/google/closure-compiler/blob/master/externs/browser/w3c_dom2.js
It doesn't look like there is an issue in the externs. If you rename AnyClass.prototype.nextNode to AnyClass.prototype.nextNodeX this issue disappears.
I cannot reproduce it via command line because
--compilation_level ADVANCED
--js test.js
--formatting PRETTY_PRINT
--env BROWSER
gives me the expected output:
function a() {
}
a.prototype.nextNode = function() {
};
console.log(a);
var b = document.createTreeWalker(document);
console.log(b.nextNode());
Looks like only the online version is affected when an empty method name from source collides with a method name from externs?
Looks like only the online version is affected when an empty method name from source collides with a method name from externs?
That's because the type based optimizations are on by default for the command line runner but not for the web service.
Using the latest jar (closure-compiler-v20161024.jar), I'm still seeing the issue.
Adding --new_type_inf does fix the issue, but it's not on by default.
Is that what you meant by "type based optimization"?