google / google/closure-compiler
TreeWalker functions overridden by unrelated class definition
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
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
``` js
// ==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()`:
``` js
// ==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);
```
Contributor guide
Assessment
This issue has not been assessed yet.