mihaip / react-closure-compiler

Tooling to teach Closure Compiler about React
Apache License 2.0
100 stars 14 forks source link

Change strategy for optional abstract mixin methods #53

Closed arv closed 5 years ago

arv commented 5 years ago

Given:

class Mixin extends React.Component {
  method() {
    if (this.optionalAbstract) {
      this.optionalAbstract(42);
    }
}
ReactSupport.declareMixin(Mixin);
/**
 * @param {number} x
 */
Mixin.optionalAbstract;

class Comp extends React.Component {}
ReactSupport.mixin(Comp, Mixin);

We now generate something along the lines of:

/** @interface */
function MixinInterface() {}
MixinInterface.prototype {
    method: function() {},
};
/**
 * @param {number} x
 */
Mixin$$optionalAbstract;
/** @type {typeof Mixin$$optionalAbstract|undefined} */
MixinInterface.prototype.optionalAbstract;

class Mixin extends React.Component {
  method() {
    if (this.optionalAbstract) {
      this.optionalAbstract(42);
    }
}
/** @type {typeof Mixin$$optionalAbstract|undefined} */
Mixin.prototype.optionalAbstract;
ReactSupport.declareMixin(Mixin);
/**
 * @param {number} x
 */
Mixin.optionalAbstract;

/** @implements {MixinInterface} */
class Comp extends React.Component {}
ReactSupport.mixin(Comp, Mixin);
/** @type {typeof MixinInterface.prototype.optionalAbstract} */
Comp.prototype.optionalAbstract;
arv commented 5 years ago

This one needs more work. It is only clearing the duplicate property when there is a mixin target.

They problem is that we need to keep the info around in memory but we don't want to output the code for it. I will need to refactor to keep the state and the prototype literal separate.

arv commented 5 years ago

TBR