google / google/closure-compiler
Tree shaker defeated by `...args` and inner function
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
[live demo](https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520ADVANCED_OPTIMIZATIONS%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540formatting%2520pretty_print%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250A%250Afunction%2520makeDecorator()%2520%257B%250A%2520%2520%2520%2520function%2520Constructor(...args)%2520%257B%250A%2520%2520%2520%2520%2520%2520%250A%2520%2520%2520%2520%2520%2520function%2520NotReferenced()%2520%257B%257D%250A%2520%2520%2520%2520%250A%2520%2520%2520%2520%257D%250A%2520%2520%2520%2520return%2520Constructor%253B%250A%257D%250A%250Aconst%2520Bar%2520%253D%2520makeDecorator()%253B%250A%250ABar()%253B%250A)
Given:
```js
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @formatting pretty_print
// ==/ClosureCompiler==
function makeDecorator() {
function Constructor(...args) {
function NotReferenced() {}
}
return Constructor;
}
const Bar = makeDecorator();
Bar();
```
Produces:
```js
(function(b) {
for (var a = 0;a < arguments.length;++a) {
}
})();
```
Interestingly if we comment out `NotReferenced` it correctly tree-shakes
Contributor guide
Assessment
This issue has not been assessed yet.