Skip to content

Commit 19b611a

Browse files
author
Keyan Zhang
committed
fixed anonymous createClass
1 parent 53730d9 commit 19b611a

File tree

5 files changed

+73
-12
lines changed

5 files changed

+73
-12
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var React = require('react');
2+
3+
const wrapper = (x) => x;
4+
5+
const Foo = wrapper(React.createClass({
6+
render() {
7+
return <div>wow so anonymous</div>;
8+
},
9+
}));
10+
11+
module.exports = wrapper(React.createClass({
12+
render() {
13+
return <div>wow so anonymous</div>;
14+
},
15+
}));
16+
17+
export default wrapper(React.createClass({
18+
render() {
19+
return <div>wow so anonymous</div>;
20+
},
21+
}));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var React = require('react');
2+
3+
const wrapper = (x) => x;
4+
5+
const Foo = wrapper(class extends React.Component {
6+
render() {
7+
return <div>wow so anonymous</div>;
8+
}
9+
});
10+
11+
module.exports = wrapper(class extends React.Component {
12+
render() {
13+
return <div>wow so anonymous</div>;
14+
}
15+
});
16+
17+
export default wrapper(class extends React.Component {
18+
render() {
19+
return <div>wow so anonymous</div>;
20+
}
21+
});

transforms/__tests__/class-test.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ const pureMixinAlternativeOption = {
1717
};
1818

1919
defineTest(__dirname, 'class');
20+
defineTest(__dirname, 'class', {flow: true}, 'class-anonymous');
2021
defineTest(__dirname, 'class', pureMixinAlternativeOption, 'class-test2');
21-
defineTest(__dirname, 'class', { flow: true }, 'export-default-class');
22+
defineTest(__dirname, 'class', {flow: true}, 'export-default-class');
2223
defineTest(__dirname, 'class', pureMixinAlternativeOption, 'class-pure-mixin1');
23-
defineTest(__dirname, 'class', { flow: true }, 'class-pure-mixin2');
24-
defineTest(__dirname, 'class', { flow: true }, 'class-initial-state');
25-
defineTest(__dirname, 'class', { flow: true }, 'class-property-field');
26-
defineTest(__dirname, 'class', { flow: true }, 'class-flow1');
27-
defineTest(__dirname, 'class', { flow: true }, 'class-flow2');
28-
defineTest(__dirname, 'class', { flow: true }, 'class-flow3');
29-
defineTest(__dirname, 'class', { flow: true }, 'class-flow4');
30-
defineTest(__dirname, 'class', { flow: true }, 'class-flow5');
24+
defineTest(__dirname, 'class', {flow: true}, 'class-pure-mixin2');
25+
defineTest(__dirname, 'class', {flow: true}, 'class-initial-state');
26+
defineTest(__dirname, 'class', {flow: true}, 'class-property-field');
27+
defineTest(__dirname, 'class', {flow: true}, 'class-flow1');
28+
defineTest(__dirname, 'class', {flow: true}, 'class-flow2');
29+
defineTest(__dirname, 'class', {flow: true}, 'class-flow3');
30+
defineTest(__dirname, 'class', {flow: true}, 'class-flow4');
31+
defineTest(__dirname, 'class', {flow: true}, 'class-flow5');

transforms/class.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,11 @@ module.exports = (file, api, options) => {
908908
const getInitialState = findGetInitialState(specPath);
909909

910910
var path;
911-
if (type == 'moduleExports' || type == 'exportDefault') {
911+
if (
912+
type == 'moduleExports' ||
913+
type == 'exportDefault' ||
914+
type == 'anonymousInCallExpression'
915+
) {
912916
path = ReactUtils.findReactCreateClassCallExpression(classPath);
913917
} else {
914918
path = j(classPath).closest(j.VariableDeclaration);
@@ -968,6 +972,8 @@ module.exports = (file, api, options) => {
968972
.forEach(classPath => updateToClass(classPath, type));
969973

970974
const didTransform = (
975+
apply(ReactUtils.findReactAnonymousCreateClassInCallExpression(root), 'anonymousInCallExpression')
976+
.size() +
971977
apply(ReactUtils.findReactCreateClass(root), 'var')
972978
.size() +
973979
apply(ReactUtils.findReactCreateClassModuleExports(root), 'moduleExports')

transforms/utils/ReactUtils.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,20 @@ module.exports = function(j) {
8686
},
8787
});
8888

89+
const findReactAnonymousCreateClassInCallExpression = path =>
90+
path.find(j.CallExpression, {
91+
arguments: [{
92+
type: 'CallExpression',
93+
callee: REACT_CREATE_CLASS_MEMBER_EXPRESSION,
94+
}],
95+
});
96+
8997
const getReactCreateClassSpec = classPath => {
90-
const {value} = classPath;
91-
const args = (value.init || value.right || value.declaration).arguments;
98+
var callCollection = findReactCreateClassCallExpression(classPath);
99+
if (callCollection.size() === 0) {
100+
return null;
101+
}
102+
const args = callCollection.get('arguments').value;
92103
if (args) {
93104
const spec = args[0];
94105
if (spec.type === 'ObjectExpression' && Array.isArray(spec.properties)) {
@@ -210,6 +221,7 @@ module.exports = function(j) {
210221

211222
return {
212223
createCreateReactClassCallExpression,
224+
findReactAnonymousCreateClassInCallExpression,
213225
findReactES6ClassDeclaration,
214226
findReactCreateClass,
215227
findReactCreateClassCallExpression,

0 commit comments

Comments
 (0)