google / google/closure-compiler

`sinkValue` for static methods

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

Description

Related to: https://github.com/google/closure-compiler/issues/2437

I have a class with a static method - output from TS that gets fed into CC looks like
```
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
define(["require", "exports", "mobx"], function (require, exports, mobx) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MyClass = (function () {
function MyClass() {
}
MyClass.myMethod = function () {
console.log('myMethod');
};
return MyClass;
}());
goog.reflect.sinkValue(MyClass.myMethod)
__decorate([
mobx.action
], MyClass, goog.reflect.objectProperty("myMethod", MyClass), null);
MyClass.myMethod();
});
```

This gets compiled to:
```
(function() {
function k(c) {
k[" "](c);
return c
}
k[" "] = function() {}
;
var m = this && this.b || function(c, d, e, f) {
var g = arguments.length, a = 3 > g ? d : null === f ? f = Object.getOwnPropertyDescriptor(d, e) : f, h;
if ("object" === typeof Reflect && "function" === typeof Reflect.a)
a = Reflect.a(c, d, e, f);
else
for (var l = c.length - 1; 0 <= l; l--)
if (h = c[l])
a = (3 > g ? h(a) : 3 < g ? h(d, e, a) : h(d, e)) || a;
return 3 < g && a && Object.defineProperty(d, e, a),
a
}
;
function n() {}
function p() {
console.log("myMethod")
}
k(function() {
return p
});
m([mobx.action], n, "c", null);
p();
})();
```

You'll see that I am using `sinkValue`. And while the method is not _technically_ inlined, it also is not kept on the class, so the decoration does not work.

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.