Skip to content

Commit 5395c81

Browse files
iammerrickcpojer
authored andcommitted
fix(es6-import-export): initial commit actually applying
1 parent 534e277 commit 5395c81

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

packages/react-codemod/test/__tests__/transform-tests.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ describe('Transform Tests', () => {
5858
});
5959

6060
test('class', 'class-test3');
61+
62+
});
63+
64+
it('transforms exports class', () => {
65+
test('class', 'export-default-class-test');
6166
});
6267

6368
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
import React from 'React';
4+
5+
export default React.createClass({
6+
getInitialState: function() {
7+
return {
8+
foo: 'bar',
9+
};
10+
},
11+
12+
render: function() {
13+
return <div />;
14+
}
15+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
import React from 'React';
4+
5+
export default class extends React.Component {
6+
constructor(props, context) {
7+
super(props, context);
8+
9+
this.state = {
10+
foo: 'bar',
11+
};
12+
}
13+
14+
render() {
15+
return <div />;
16+
}
17+
};
18+

packages/react-codemod/transforms/class.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ function updateReactCreateClassToES6(file, api, options) {
479479
if (
480480
options['no-explicit-require'] || ReactUtils.hasReact(root)
481481
) {
482-
const apply = (path, isModuleExports) =>
482+
const apply = (path, isModuleExports) =>
483483
path
484484
.filter(hasMixins)
485485
.filter(callsDeprecatedAPIs)
@@ -491,12 +491,14 @@ function updateReactCreateClassToES6(file, api, options) {
491491

492492
const didTransform = (
493493
apply(ReactUtils.findReactCreateClass(root), false).size() +
494-
apply(ReactUtils.findReactCreateClassModuleExports(root), true).size()
494+
apply(ReactUtils.findReactCreateClassModuleExports(root), true).size() +
495+
apply(ReactUtils.findReactCreateClassExportDefault(root), false).size()
495496
) > 0;
496497

497498
if (didTransform) {
498499
return root.toSource(printOptions);
499500
}
501+
500502
}
501503

502504
return null;

packages/react-codemod/transforms/utils/ReactUtils.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@ module.exports = function(j) {
2727
path
2828
.findVariableDeclarators()
2929
.filter(j.filters.VariableDeclarator.requiresModule(module))
30-
.size() === 1;
30+
.size() === 1 ||
31+
path
32+
.find(j.ImportDeclaration, {
33+
type: 'ImportDeclaration',
34+
source: {
35+
type: 'Literal',
36+
}
37+
}).filter((importDeclarator) => {
38+
return importDeclarator.value.source.value === module;
39+
}).size() === 1
3140

3241
const hasReact = path => (
3342
hasModule(path, 'React') ||
@@ -47,6 +56,20 @@ module.exports = function(j) {
4756
.findVariableDeclarators()
4857
.filter(decl => findReactCreateClassCallExpression(decl).size() > 0);
4958

59+
const findReactCreateClassExportDefault = path => {
60+
var collection = [];
61+
path
62+
.find(j.ExportDefaultDeclaration, {
63+
type: 'ExportDefaultDeclaration',
64+
declaration: {
65+
type: 'CallExpression',
66+
callee: REACT_CREATE_CLASS_MEMBER_EXPRESSION
67+
}
68+
})
69+
.forEach((p) => collection.push(p.value.declaration))
70+
return j(collection);
71+
}
72+
5073
const findReactCreateClassModuleExports = path =>
5174
path
5275
.find(j.AssignmentExpression, {
@@ -106,7 +129,7 @@ module.exports = function(j) {
106129
// ---------------------------------------------------------------------------
107130
// Others
108131
const getReactCreateClassSpec = classPath => {
109-
const spec = (classPath.value.init || classPath.value.right).arguments[0];
132+
const spec = (classPath.value.init || classPath.value.right || classPath.value).arguments[0]
110133
if (spec.type === 'ObjectExpression' && Array.isArray(spec.properties)) {
111134
return spec;
112135
}
@@ -131,6 +154,7 @@ module.exports = function(j) {
131154
findReactCreateClass,
132155
findReactCreateClassCallExpression,
133156
findReactCreateClassModuleExports,
157+
findReactCreateClassExportDefault,
134158
getComponentName,
135159
getReactCreateClassSpec,
136160
hasMixins,

0 commit comments

Comments
 (0)