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

This commit is contained in:
Merrick Christensen 2015-08-13 13:03:04 -06:00 committed by cpojer
parent 534e277c10
commit 5395c815de
5 changed files with 68 additions and 4 deletions

View File

@ -58,6 +58,11 @@ describe('Transform Tests', () => {
});
test('class', 'class-test3');
});
it('transforms exports class', () => {
test('class', 'export-default-class-test');
});
});

View File

@ -0,0 +1,15 @@
'use strict';
import React from 'React';
export default React.createClass({
getInitialState: function() {
return {
foo: 'bar',
};
},
render: function() {
return <div />;
}
});

View File

@ -0,0 +1,18 @@
'use strict';
import React from 'React';
export default class extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
foo: 'bar',
};
}
render() {
return <div />;
}
};

View File

@ -479,7 +479,7 @@ function updateReactCreateClassToES6(file, api, options) {
if (
options['no-explicit-require'] || ReactUtils.hasReact(root)
) {
const apply = (path, isModuleExports) =>
const apply = (path, isModuleExports) =>
path
.filter(hasMixins)
.filter(callsDeprecatedAPIs)
@ -491,12 +491,14 @@ function updateReactCreateClassToES6(file, api, options) {
const didTransform = (
apply(ReactUtils.findReactCreateClass(root), false).size() +
apply(ReactUtils.findReactCreateClassModuleExports(root), true).size()
apply(ReactUtils.findReactCreateClassModuleExports(root), true).size() +
apply(ReactUtils.findReactCreateClassExportDefault(root), false).size()
) > 0;
if (didTransform) {
return root.toSource(printOptions);
}
}
return null;

View File

@ -27,7 +27,16 @@ module.exports = function(j) {
path
.findVariableDeclarators()
.filter(j.filters.VariableDeclarator.requiresModule(module))
.size() === 1;
.size() === 1 ||
path
.find(j.ImportDeclaration, {
type: 'ImportDeclaration',
source: {
type: 'Literal',
}
}).filter((importDeclarator) => {
return importDeclarator.value.source.value === module;
}).size() === 1
const hasReact = path => (
hasModule(path, 'React') ||
@ -47,6 +56,20 @@ module.exports = function(j) {
.findVariableDeclarators()
.filter(decl => findReactCreateClassCallExpression(decl).size() > 0);
const findReactCreateClassExportDefault = path => {
var collection = [];
path
.find(j.ExportDefaultDeclaration, {
type: 'ExportDefaultDeclaration',
declaration: {
type: 'CallExpression',
callee: REACT_CREATE_CLASS_MEMBER_EXPRESSION
}
})
.forEach((p) => collection.push(p.value.declaration))
return j(collection);
}
const findReactCreateClassModuleExports = path =>
path
.find(j.AssignmentExpression, {
@ -106,7 +129,7 @@ module.exports = function(j) {
// ---------------------------------------------------------------------------
// Others
const getReactCreateClassSpec = classPath => {
const spec = (classPath.value.init || classPath.value.right).arguments[0];
const spec = (classPath.value.init || classPath.value.right || classPath.value).arguments[0]
if (spec.type === 'ObjectExpression' && Array.isArray(spec.properties)) {
return spec;
}
@ -131,6 +154,7 @@ module.exports = function(j) {
findReactCreateClass,
findReactCreateClassCallExpression,
findReactCreateClassModuleExports,
findReactCreateClassExportDefault,
getComponentName,
getReactCreateClassSpec,
hasMixins,