google / google/closure-compiler
Optimize / inline getters and setters
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
Currently Closure does not inline getters in advanced mode in situations where it inlines methods:
``` javascript
'use strict';
/*
class Foo {
get bar() { return 123 }
baz() { return 456 }
}
*/
'use strict';
/** @constructor */
var Foo = function() {};
Object.defineProperties(Foo.prototype, {
foo: {
/** @this {Foo} */
'get' : function() { return 123 }
}
});
console.log(new Foo().foo)
/** @constructor */
var Bar = function() {};
Bar.prototype.bar = function() { return 456 };
console.log(new Bar().bar())
```
Gives the [following output](http://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%250A%252F**%2520%2540constructor%2520*%252F%250Avar%2520Foo%2520%253D%2520function%28%29%2520%257B%257D%253B%250AObject.defineProperties%28Foo.prototype%252C%2520%257B%250A%2520%2520bar%253A%2520%257B%250A%2520%2520%2520%2520%252F**%2520%2540this%2520%257BFoo%257D%2520*%252F%250A%2520%2520%2520%2520'get'%2520%253A%2520function%28%29%2520%257B%2520return%2520123%2520%257D%250A%2520%2520%257D%250A%257D%29%253B%250AFoo.prototype.baz%2520%253D%2520function%2520%28%29%2520%257B%2520return%2520456%2520%257D%253B%250Aconsole.log%28new%2520Foo%28%29.bar%29%250Aconsole.log%28new%2520Foo%28%29.baz%28%29%29):
``` javascript
function a() {
}
Object.defineProperties(a.prototype, {a:{get:function() {
return 123;
}}});
console.log((new a).a);
console.log(456);
```
It would be nice to add full optimization support for getters/setters.
Contributor guide
Assessment
This issue has not been assessed yet.