ES in chromium proposal: proxies

38 views
Skip to first unread message

Tibor Goldschwendt

unread,
Mar 9, 2021, 12:04:22 PM3/9/21
to chromi...@chromium.org, Demetrios Papadopoulos
Hi all,

I'm proposing to allow ES6 proxies in our JS style guide. My use case is to auto-generate mocks and spies in unit tests. ES6 proxies are supported by all of our platforms and our tooling seems to be ok with them, too (ran the snippet below through the CQ here). There is one caveat: Closure compiler sees a proxy as type Proxy<T> and not T. To effectively make use of the proxy you'll need to "cast" the proxy to T like in the snippet below:

class Foo { bar() { console.log('bar'); } } /** * @param {!T} object * @return {!T} * @template T */ function createProxy(object) { /** @type {!Proxy<T>} */ const proxy = new Proxy(object, { get: function(target, prop, receiver) { console.log(`${prop} called`); return Reflect.get(target, prop, receiver); }, }); return proxy; } /** @type {!Foo} */ const fooProxy = createProxy(new Foo()); fooProxy.bar(); // Prints 'bar called', 'bar'.

Cheers,
Tibor


Demetrios Papadopoulos

unread,
Mar 9, 2021, 2:32:31 PM3/9/21
to Tibor Goldschwendt, Chromium-dev
Hi Tibor,

Moving https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/es.md#proxy to the list of "allowed features" SGTM. Having said that, might be best to directly cc other  ui/webui/PLATFORM_OWNERS as well to ensure that this proposal gets enough visibility before moving forward with updating the JS style guide.

Thanks,
Demetrios

Tibor Goldschwendt

unread,
Mar 9, 2021, 2:59:36 PM3/9/21
to Demetrios Papadopoulos, Chromium-dev, Esmael Elmoslimany, Denis Kuznetsov, Chris Lam, John Lee, Kyle Horimoto, Michael Giuffrida, Rebekah Potter
CC'ing WebUI platform owners.

Tibor Goldschwendt

unread,
Mar 12, 2021, 12:34:20 PM3/12/21
to Demetrios Papadopoulos, Chromium-dev, Esmael Elmoslimany, Denis Kuznetsov, Chris Lam, John Lee, Kyle Horimoto, Michael Giuffrida, Rebekah Potter
Got 1 yay and 0 nays. Moving ES6 proxies to allowed features.
Reply all
Reply to author
Forward
0 new messages