babel-plugin-transform-react-remove-prop-types icon indicating copy to clipboard operation
babel-plugin-transform-react-remove-prop-types copied to clipboard

Support static class getter propTypes

Open freaktechnik opened this issue 7 years ago • 6 comments

With classes you can also define your propTypes on a static getter property, which this plugin doesn't seem to be able to remove currently.

Example:

class MyComponent extends React.Component {
    static get propTypes() {
        return {
            children: PropTypes.node.isRequired
        };
    }

    render() {
        return this.props.children;
    }
}

freaktechnik avatar Feb 20 '18 22:02 freaktechnik

Would like this too, just adding another syntax that is almost similar for completeness:

export default class MyComponent extends React.Component {
  static propTypes = {
    children: PropTypes.node.isRequired,
  }

omichelsen avatar Feb 22 '18 22:02 omichelsen

+1. We actually write things in the latter style for stylistic reasons

jeremyong avatar Mar 08 '18 22:03 jeremyong

is there any progress on this?

koxen avatar Apr 06 '18 18:04 koxen

Static propTypes when defined as a class prop are supported I believe, not sure why the comments state otherwise.

This visitor should take care of both cases.

visitor: {
  ClassProperty(path) {
    if (path.node.static && path.node.key.name === 'propTypes')
      path.remove();
  },
  ClassMethod(path) {
    if (path.node.static && path.node.key.name === 'propTypes')
      path.remove();
  }
}

sonaye avatar Jun 08 '18 23:06 sonaye

@sonaye In this case, we miss a test to assert it :)

oliviertassinari avatar Jun 09 '18 11:06 oliviertassinari

Is there any progress on this? I am using this babel plugin but, I still have propTypes that are static fields in my components being transpiled into webpack bundle.

Lazzaro83 avatar Aug 20 '19 10:08 Lazzaro83