google / google/closure-compiler

Use flow analysis to remove unused prototype methods

Open
#666 1 comment 0 reactions 0 assignees View on GitHub
enhancement P3
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

If you compile the following example in advanced mode, Child1#method123 is not removed, even though it is never used. The reason is that the compiler doesn't know that x in x.method123(0) can only ever be a Child2. It would be nice if we can do some simple flow analysis (eg, 0CFA) to catch things like that and remove more dead code.

``` js
/** @interface */
function Parent() {}
/** @return {number} */
Parent.prototype.method123 = function(x) {};

/** @constructor @implements {Parent} */
function Child1() { this.prop = 123; }
/** @override */
Child1.prototype.method123 = function(x) { return x+1; };

/** @constructor @implements {Parent} */
function Child2() {}
/** @override */
Child2.prototype.method123 = function(x) { return x-1; };

/** @param {!Parent} x */
function f(x) {
alert(x.method123(0));
}

alert((new Child1).prop);
f(new Child2);
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.