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