diff --git a/src/index.js b/src/index.js index f1ecb3d..5036363 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,25 @@ import isStatelessComponent from './isStatelessComponent'; +function isReactClass(superClass, scope) { + let isReactClass = false; + + if (superClass.matchesPattern('React.Component') || + superClass.node.name === 'Component') { + isReactClass = true; + } else if (superClass.node.name) { // Check for inheritance + const className = superClass.node.name; + const binding = scope.getBinding(className); + superClass = binding.path.get('superClass'); + + if (superClass.matchesPattern('React.Component') || + (superClass.node && superClass.node.name === 'Component')) { + isReactClass = true; + } + } + + return isReactClass; +} + export default function ({ Plugin, types: t }) { return { visitor: { @@ -38,18 +58,8 @@ export default function ({ Plugin, types: t }) { let superClass = scope.path.get('superClass'); - if (superClass.matchesPattern('React.Component') || - superClass.node.name === 'Component') { + if (isReactClass(superClass, scope)) { path.remove(); - } else if (superClass.node.name) { // Check for inheritance - const className = superClass.node.name; - const binding = scope.getBinding(className); - superClass = binding.path.get('superClass'); - - if (superClass.matchesPattern('React.Component') || - (superClass.node && superClass.node.name === 'Component')) { - path.remove(); - } } } }, @@ -72,7 +82,8 @@ export default function ({ Plugin, types: t }) { if (binding.path.isClassDeclaration()) { const superClass = binding.path.get('superClass'); - if (superClass.matchesPattern('React.Component') || superClass.matchesPattern('Component')) { + + if (isReactClass(superClass, scope)) { path.remove(); } } else if (isStatelessComponent(binding.path)) { diff --git a/test/fixtures/es-class-extend-component/actual.js b/test/fixtures/es-class-extend-component/actual.js index 3e177d9..bff28b2 100644 --- a/test/fixtures/es-class-extend-component/actual.js +++ b/test/fixtures/es-class-extend-component/actual.js @@ -1,7 +1,15 @@ -class Foo extends Component { +class Foo1 extends Component { static propTypes = { - foo: React.PropTypes.string + foo1: React.PropTypes.string }; render() {} } + +class Foo2 extends Component { + render() {} +} + +Foo2.propTypes = { + foo2: React.PropTypes.string +}; diff --git a/test/fixtures/es-class-extend-component/expected.js b/test/fixtures/es-class-extend-component/expected.js index da58a0a..deb6c73 100644 --- a/test/fixtures/es-class-extend-component/expected.js +++ b/test/fixtures/es-class-extend-component/expected.js @@ -8,19 +8,36 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } -var Foo = function (_Component) { - _inherits(Foo, _Component); +var Foo1 = function (_Component) { + _inherits(Foo1, _Component); - function Foo() { - _classCallCheck(this, Foo); + function Foo1() { + _classCallCheck(this, Foo1); - return _possibleConstructorReturn(this, Object.getPrototypeOf(Foo).apply(this, arguments)); + return _possibleConstructorReturn(this, Object.getPrototypeOf(Foo1).apply(this, arguments)); } - _createClass(Foo, [{ + _createClass(Foo1, [{ key: "render", value: function render() {} }]); - return Foo; + return Foo1; +}(Component); + +var Foo2 = function (_Component2) { + _inherits(Foo2, _Component2); + + function Foo2() { + _classCallCheck(this, Foo2); + + return _possibleConstructorReturn(this, Object.getPrototypeOf(Foo2).apply(this, arguments)); + } + + _createClass(Foo2, [{ + key: "render", + value: function render() {} + }]); + + return Foo2; }(Component); diff --git a/test/fixtures/es-class-inheritance/actual.js b/test/fixtures/es-class-inheritance/actual.js index 80b9178..9810068 100644 --- a/test/fixtures/es-class-inheritance/actual.js +++ b/test/fixtures/es-class-inheritance/actual.js @@ -1,10 +1,19 @@ class PureRenderComponent extends Component { } -class Foo extends PureRenderComponent { +class Foo1 extends PureRenderComponent { static propTypes = { - foo: PropTypes.string.isRequired + foo1: PropTypes.string.isRequired }; render () { } } + +class Foo2 extends PureRenderComponent { + render () { + } +} + +Foo2.propTypes = { + foo2: PropTypes.string.isRequired +}; diff --git a/test/fixtures/es-class-inheritance/expected.js b/test/fixtures/es-class-inheritance/expected.js index dd83928..39450a4 100644 --- a/test/fixtures/es-class-inheritance/expected.js +++ b/test/fixtures/es-class-inheritance/expected.js @@ -20,19 +20,36 @@ var PureRenderComponent = function (_Component) { return PureRenderComponent; }(Component); -var Foo = function (_PureRenderComponent) { - _inherits(Foo, _PureRenderComponent); +var Foo1 = function (_PureRenderComponent) { + _inherits(Foo1, _PureRenderComponent); - function Foo() { - _classCallCheck(this, Foo); + function Foo1() { + _classCallCheck(this, Foo1); - return _possibleConstructorReturn(this, Object.getPrototypeOf(Foo).apply(this, arguments)); + return _possibleConstructorReturn(this, Object.getPrototypeOf(Foo1).apply(this, arguments)); } - _createClass(Foo, [{ + _createClass(Foo1, [{ key: "render", value: function render() {} }]); - return Foo; + return Foo1; +}(PureRenderComponent); + +var Foo2 = function (_PureRenderComponent2) { + _inherits(Foo2, _PureRenderComponent2); + + function Foo2() { + _classCallCheck(this, Foo2); + + return _possibleConstructorReturn(this, Object.getPrototypeOf(Foo2).apply(this, arguments)); + } + + _createClass(Foo2, [{ + key: "render", + value: function render() {} + }]); + + return Foo2; }(PureRenderComponent);