Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #86 from nfcampos/master
Browse files Browse the repository at this point in the history
Only wrap descendants with render method, closes #84
  • Loading branch information
gaearon committed Mar 4, 2016
2 parents 63cafc9 + b9d30fc commit 84878e0
Show file tree
Hide file tree
Showing 56 changed files with 549 additions and 384 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default function({ types: t, template }) {
Class(path) {
if (
path.node[VISITED_KEY] ||
!matchesPatterns(path.get('superClass'), this.superClasses) &&
!matchesPatterns(path.get('superClass'), this.superClasses) ||
!isReactLikeClass(path.node)
) {
return;
Expand Down Expand Up @@ -119,7 +119,7 @@ export default function({ types: t, template }) {
CallExpression(path) {
if (
path.node[VISITED_KEY] ||
!matchesPatterns(path.get('callee'), this.factoryMethods) &&
!matchesPatterns(path.get('callee'), this.factoryMethods) ||
!isReactLikeComponentObject(path.node.arguments[0])
) {
return;
Expand Down Expand Up @@ -171,7 +171,7 @@ export default function({ types: t, template }) {
normalizeOptions(options) {
return {
factoryMethods: options.factoryMethods || ['React.createClass'],
superClasses: options.superClasses || ['React.Component'],
superClasses: options.superClasses || ['React.Component', 'Component'],
transforms: options.transforms.map(opts => {
return {
transform: opts.transform,
Expand Down
32 changes: 6 additions & 26 deletions test/fixtures/code-call-expression-with-render-method/expected.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
import _transformLib from 'transform-lib';
const _components = {
_component: {},
_component2: {},
_component3: {}
};

const _transformLib2 = _transformLib({
filename: '%FIXTURE_PATH%',
components: _components,
locals: [],
imports: []
});

function _wrapComponent(id) {
return function (Component) {
return _transformLib2(Component, id);
};
}

_wrapComponent('_component')(factory({
factory({
render() {}
}));
});

_wrapComponent('_component2')(factory({
factory({
render: function () {}
}));
});

_wrapComponent('_component3')(factory({
factory({
'render': function () {}
}));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
["../../../src", {
"transforms": [{
"transform": "transform-lib"
}]
}]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
foo(class Foo extends React.Component {
render() {}
});
foo(class extends React.Component {
render() {}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import _transformLib from "transform-lib";
const _components = {
Foo: {
displayName: "Foo"
},
_component: {}
};

const _transformLib2 = _transformLib({
filename: "%FIXTURE_PATH%",
components: _components,
locals: [],
imports: []
});

function _wrapComponent(id) {
return function (Component) {
return _transformLib2(Component, id);
};
}

foo(_wrapComponent("Foo")(class Foo extends React.Component {
render() {}
}));
foo(_wrapComponent("_component")(class extends React.Component {
render() {}
}));
25 changes: 2 additions & 23 deletions test/fixtures/code-class-expression/expected.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,2 @@
import _transformLib from "transform-lib";
const _components = {
Foo: {
displayName: "Foo"
},
_component: {}
};

const _transformLib2 = _transformLib({
filename: "%FIXTURE_PATH%",
components: _components,
locals: [],
imports: []
});

function _wrapComponent(id) {
return function (Component) {
return _transformLib2(Component, id);
};
}

foo(_wrapComponent("Foo")(class Foo extends React.Component {}));
foo(_wrapComponent("_component")(class extends React.Component {}));
foo(class Foo extends React.Component {});
foo(class extends React.Component {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
["../../../src", {
"transforms": [{
"transform": "transform-lib"
}]
}]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React, { Component } from 'react';
class Foo extends Component {
render() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import _transformLib from 'transform-lib';
const _components = {
Foo: {
displayName: 'Foo'
}
};

const _transformLib2 = _transformLib({
filename: '%FIXTURE_PATH%',
components: _components,
locals: [],
imports: []
});

function _wrapComponent(id) {
return function (Component) {
return _transformLib2(Component, id);
};
}

import React, { Component } from 'react';

const Foo = _wrapComponent('Foo')(class Foo extends Component {
render() {}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
["../../../src", {
"transforms": [{
"transform": "transform-lib"
}]
}]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Foo extends React.Component {
render() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import _transformLib from "transform-lib";
const _components = {
Foo: {
displayName: "Foo"
}
};

const _transformLib2 = _transformLib({
filename: "%FIXTURE_PATH%",
components: _components,
locals: [],
imports: []
});

function _wrapComponent(id) {
return function (Component) {
return _transformLib2(Component, id);
};
}

const Foo = _wrapComponent("Foo")(class Foo extends React.Component {
render() {}
});
22 changes: 1 addition & 21 deletions test/fixtures/code-class-extends-react-component/expected.js
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
import _transformLib from "transform-lib";
const _components = {
Foo: {
displayName: "Foo"
}
};

const _transformLib2 = _transformLib({
filename: "%FIXTURE_PATH%",
components: _components,
locals: [],
imports: []
});

function _wrapComponent(id) {
return function (Component) {
return _transformLib2(Component, id);
};
}

const Foo = _wrapComponent("Foo")(class Foo extends React.Component {});
class Foo extends React.Component {}
24 changes: 2 additions & 22 deletions test/fixtures/code-class-with-render-method/expected.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
import _transformLib from "transform-lib";
const _components = {
Foo: {
displayName: "Foo"
}
};

const _transformLib2 = _transformLib({
filename: "%FIXTURE_PATH%",
components: _components,
locals: [],
imports: []
});

function _wrapComponent(id) {
return function (Component) {
return _transformLib2(Component, id);
};
}

const Foo = _wrapComponent("Foo")(class Foo {
class Foo {
render() {}
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
["../../../src", {
"transforms": [{
"transform": "transform-lib"
}]
}]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function factory() {
return class Foo extends React.Component {
render() {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import _transformLib from "transform-lib";
const _components = {
Foo: {
displayName: "Foo",
isInFunction: true
}
};

const _transformLib2 = _transformLib({
filename: "%FIXTURE_PATH%",
components: _components,
locals: [],
imports: []
});

function _wrapComponent(id) {
return function (Component) {
return _transformLib2(Component, id);
};
}

function factory() {
return _wrapComponent("Foo")(class Foo extends React.Component {
render() {}
});
}
23 changes: 1 addition & 22 deletions test/fixtures/code-class-within-function/expected.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
import _transformLib from "transform-lib";
const _components = {
Foo: {
displayName: "Foo",
isInFunction: true
}
};

const _transformLib2 = _transformLib({
filename: "%FIXTURE_PATH%",
components: _components,
locals: [],
imports: []
});

function _wrapComponent(id) {
return function (Component) {
return _transformLib2(Component, id);
};
}

function factory() {
return _wrapComponent("Foo")(class Foo extends React.Component {});
return class Foo extends React.Component {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
["../../../src", {
"transforms": [{
"transform": "transform-lib"
}]
}]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const Foo = class extends React.Component {
render() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import _transformLib from "transform-lib";
const _components = {
_component: {}
};

const _transformLib2 = _transformLib({
filename: "%FIXTURE_PATH%",
components: _components,
locals: [],
imports: []
});

function _wrapComponent(id) {
return function (Component) {
return _transformLib2(Component, id);
};
}

const Foo = _wrapComponent("_component")(class extends React.Component {
render() {}
});
20 changes: 1 addition & 19 deletions test/fixtures/code-class-without-name/expected.js
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
import _transformLib from "transform-lib";
const _components = {
_component: {}
};

const _transformLib2 = _transformLib({
filename: "%FIXTURE_PATH%",
components: _components,
locals: [],
imports: []
});

function _wrapComponent(id) {
return function (Component) {
return _transformLib2(Component, id);
};
}

const Foo = _wrapComponent("_component")(class extends React.Component {});
const Foo = class extends React.Component {};
3 changes: 3 additions & 0 deletions test/fixtures/code-exports/actual.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ export default class extends React.Component {}
export default React.createClass({});
export class Bar extends React.Component {}
export const bar = React.createClass({});
export default class Baz { render() {} }
export default class { render() {} }
export class Boo { render() {} }
Loading

0 comments on commit 84878e0

Please sign in to comment.