Skip to content

Commit

Permalink
Merge pull request reactjs#157 from jsnmoon/fix/react-import-deletion
Browse files Browse the repository at this point in the history
Class transform - Remove React import only if it should be removed
  • Loading branch information
bvaughn authored Jul 28, 2017
2 parents 0db05cb + 47c7683 commit 4b7f5bf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
19 changes: 19 additions & 0 deletions transforms/__testfixtures__/class/class-prune-react4.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

import React, {PropTypes} from 'React';

const SomeMixin = {
componentDidMount() {
console.log('did mount');
},
};

export default React.createClass({
mixins: [SomeMixin],
propTypes: {
foo: PropTypes.string,
},
render: function() {
return <div />;
},
});
24 changes: 24 additions & 0 deletions transforms/__testfixtures__/class/class-prune-react4.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

import React, {PropTypes} from 'React';

import createReactClass from 'create-react-class';

const SomeMixin = {
componentDidMount() {
console.log('did mount');
},
};

export default createReactClass({
displayName: 'class-prune-react4.input',
mixins: [SomeMixin],

propTypes: {
foo: PropTypes.string,
},

render: function() {
return <div />;
},
});
1 change: 1 addition & 0 deletions transforms/__tests__/class-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ defineTest(__dirname, 'class', {
defineTest(__dirname, 'class', null, 'class/class-prune-react');
defineTest(__dirname, 'class', null, 'class/class-prune-react2');
defineTest(__dirname, 'class', null, 'class/class-prune-react3');
defineTest(__dirname, 'class', null, 'class/class-prune-react4');
defineTest(__dirname, 'class', {
'create-class-module-name': 'createReactClass__deprecated',
'create-class-variable-name': 'createReactClass__deprecated',
Expand Down
5 changes: 4 additions & 1 deletion transforms/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,10 @@ module.exports = (file, api, options) => {
shouldReinsertComment = bodyNode.indexOf(importDeclarationNode) === 0;
removePath = path;
} else {
j(path).find(j.ImportDefaultSpecifier).remove();
const paths = j(path).find(j.ImportDefaultSpecifier);
if (paths.length) {
removePath = j(path).find(j.ImportDefaultSpecifier).paths()[0];
}
}
}

Expand Down

0 comments on commit 4b7f5bf

Please sign in to comment.