Skip to content

Commit

Permalink
[Class] Fix usage of AssignmentExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Apr 13, 2016
1 parent 785410b commit 775ebea
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 30 deletions.
35 changes: 23 additions & 12 deletions 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: {
Expand Down Expand Up @@ -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();
}
}
}
},
Expand All @@ -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)) {
Expand Down
12 changes: 10 additions & 2 deletions 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
};
31 changes: 24 additions & 7 deletions test/fixtures/es-class-extend-component/expected.js
Expand Up @@ -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);
13 changes: 11 additions & 2 deletions 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
};
31 changes: 24 additions & 7 deletions test/fixtures/es-class-inheritance/expected.js
Expand Up @@ -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);

0 comments on commit 775ebea

Please sign in to comment.