Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ APIs.

### Included Scripts

`create-element-to-jsx` converts calls to `React.createElement` into JSX elements.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following alphabetical order


* `jscodeshift -t react-codemod/transforms/create-element-to-jsx.js <file>`

`findDOMNode` updates `this.getDOMNode()` or `this.refs.foo.getDOMNode()`
calls inside of `React.createClass` components to `React.findDOMNode(foo)`. Note
that it will only look at code inside of `React.createClass` calls and only
Expand Down
29 changes: 29 additions & 0 deletions test/__tests__/create-element-to-jsx-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

'use strict';

describe('create-element-to-jsx', () => {

it('transforms correctly', () => {
test('create-element-to-jsx', 'create-element-to-jsx-single-element');

test('create-element-to-jsx', 'create-element-to-jsx-props');

test('create-element-to-jsx', 'create-element-to-jsx-children-literal');

test('create-element-to-jsx', 'create-element-to-jsx-children');

test('create-element-to-jsx', 'create-element-to-jsx-spread');

test('create-element-to-jsx', 'create-element-to-jsx-no-react');
});

});
3 changes: 3 additions & 0 deletions test/create-element-to-jsx-children-literal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var React = require('React');

React.createElement('div', null, 'foo');
3 changes: 3 additions & 0 deletions test/create-element-to-jsx-children-literal.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var React = require('React');

<div>foo</div>;
12 changes: 12 additions & 0 deletions test/create-element-to-jsx-children.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var React = require('React');

var a = React.createElement(
Foo,
null,
React.createElement('div', { foo: 'bar' }),
React.createElement(
'span',
null,
'blah'
)
);
3 changes: 3 additions & 0 deletions test/create-element-to-jsx-children.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var React = require('React');

var a = <Foo><div foo="bar" /><span>blah</span></Foo>;
3 changes: 3 additions & 0 deletions test/create-element-to-jsx-no-react.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var React = require('foo');

React.createElement(Foo, 'la');
Empty file.
6 changes: 6 additions & 0 deletions test/create-element-to-jsx-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var React = require('React');

function foo() {
var a = React.createElement(Foo, { foo: 'bar', bar: this.state.baz });
var b = React.createElement('div', { foo: 'bar', bar: this.state.baz });
}
6 changes: 6 additions & 0 deletions test/create-element-to-jsx-props.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var React = require('React');

function foo() {
var a = <Foo foo="bar" bar={this.state.baz} />;
var b = <div foo="bar" bar={this.state.baz} />;
}
4 changes: 4 additions & 0 deletions test/create-element-to-jsx-single-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var React = require('React');

var a = React.createElement(Foo, null);
var b = React.createElement('div', null);
4 changes: 4 additions & 0 deletions test/create-element-to-jsx-single-element.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var React = require('React');

var a = <Foo />;
var b = <div />;
3 changes: 3 additions & 0 deletions test/create-element-to-jsx-spread.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var React = require('React');

React.createElement(Foo, {foo: 'bar', ...someObject});
3 changes: 3 additions & 0 deletions test/create-element-to-jsx-spread.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var React = require('React');

<Foo foo="bar" {...someObject} />;
86 changes: 86 additions & 0 deletions transforms/create-element-to-jsx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module.exports = function(file, api, options) {
const j = api.jscodeshift;
const root = j(file.source);
const ReactUtils = require('./utils/ReactUtils')(j);

const convertObjectExpressionToJSXAttributes = (objectExpression) => {
if (!objectExpression.properties) {
return [];
}

const attributes = objectExpression.properties.map((property) => {
if (property.type === 'SpreadProperty') {
return j.jsxSpreadAttribute(property.argument);
} else if (property.type === 'Property') {
const propertyValueType = property.value.type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also use destructuring here:

const {type} = property.value;


let value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and if you make the variable name shorter you can probably just use a ternary here :P

if (propertyValueType === 'Literal') {
value = j.literal(property.value.value);
} else if (propertyValueType === 'MemberExpression') {
value = j.jsxExpressionContainer(property.value);
}

return j.jsxAttribute(
j.jsxIdentifier(property.key.name),
value
);
}
});

return attributes;
};

const convertNodeToJSX = (node) => {
const args = node.value.arguments;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const {arguments: args} = node.value; if you wanna go nuts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, I'll resist unless you prefer it.


const elementType = args[0].type;
const elementName = elementType === 'Literal' ? args[0].value : args[0].name;
const props = args[1];

const attributes = convertObjectExpressionToJSXAttributes(props);

const children = node.value.arguments.slice(2).map((child, index) => {
if (child.type === 'Literal') {
return j.literal(child.value);
}

return convertNodeToJSX(node.get('arguments', index + 2));
});

const openingElement = j.jsxOpeningElement(j.jsxIdentifier(elementName), attributes);

if (children.length) {
return j.jsxElement(
openingElement,
j.jsxClosingElement(j.jsxIdentifier(elementName)),
children
);
} else {
openingElement.selfClosing = true;
return j.jsxElement(openingElement);
}
};

if (ReactUtils.hasReact(root)) {
const mutations = root
.find(j.CallExpression, {
callee: {
object: {
name: 'React',
},
property: {
name: 'createElement',
},
},
})
.replaceWith(convertNodeToJSX)
.size();

if (mutations) {
return root.toSource();
}
}

return null;
};