@samitouri / QOS-React-2 / commits / 5395c815de

fix(es6-import-export): initial commit actually applying

Merrick Christensen committed Aug 13, 2015 at 13:03 UTC 5395c815deea6bff0bb01a4e803800dc69c29886
5 files changed +68 -4
packages/react-codemod/test/__tests__/transform-tests.js
+5
@@ -58,6 +58,11 @@ describe('Transform Tests', () => {
58 });
59
60 test('class', 'class-test3');
61 +
62 + });
63 +
64 + it('transforms exports class', () => {
65 + test('class', 'export-default-class-test');
66 });
67
68 });
packages/react-codemod/test/export-default-class-test.js new
+15
@@ -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 +});
packages/react-codemod/test/export-default-class-test.output.js new
+18
@@ -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
+4 -2
@@ -479,7 +479,7 @@ function updateReactCreateClassToES6(file, api, options) {
479 if (
480 options['no-explicit-require'] || ReactUtils.hasReact(root)
481 ) {
482 - const apply = (path, isModuleExports) =>
482 + const apply = (path, isModuleExports) =>
483 path
484 .filter(hasMixins)
485 .filter(callsDeprecatedAPIs)
@@ -491,12 +491,14 @@ function updateReactCreateClassToES6(file, api, options) {
491
492 const didTransform = (
493 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()
496 ) > 0;
497
498 if (didTransform) {
499 return root.toSource(printOptions);
500 }
501 +
502 }
503
504 return null;
packages/react-codemod/transforms/utils/ReactUtils.js
+26 -2
@@ -27,7 +27,16 @@ module.exports = function(j) {
27 path
28 .findVariableDeclarators()
29 .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
40
41 const hasReact = path => (
42 hasModule(path, 'React') ||
@@ -47,6 +56,20 @@ module.exports = function(j) {
56 .findVariableDeclarators()
57 .filter(decl => findReactCreateClassCallExpression(decl).size() > 0);
58
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 +
73 const findReactCreateClassModuleExports = path =>
74 path
75 .find(j.AssignmentExpression, {
@@ -106,7 +129,7 @@ module.exports = function(j) {
129 // ---------------------------------------------------------------------------
130 // Others
131 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]
133 if (spec.type === 'ObjectExpression' && Array.isArray(spec.properties)) {
134 return spec;
135 }
@@ -131,6 +154,7 @@ module.exports = function(j) {
154 findReactCreateClass,
155 findReactCreateClassCallExpression,
156 findReactCreateClassModuleExports,
157 + findReactCreateClassExportDefault,
158 getComponentName,
159 getReactCreateClassSpec,
160 hasMixins,