-
Notifications
You must be signed in to change notification settings - Fork 300
Adding a codemod for transforming createElement function calls to JSX #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'); | ||
| }); | ||
|
|
||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| var React = require('React'); | ||
|
|
||
| React.createElement('div', null, 'foo'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| var React = require('React'); | ||
|
|
||
| <div>foo</div>; |
| 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' | ||
| ) | ||
| ); |
| 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>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| var React = require('foo'); | ||
|
|
||
| React.createElement(Foo, 'la'); |
| 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 }); | ||
| } |
| 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} />; | ||
| } |
| 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); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| var React = require('React'); | ||
|
|
||
| var a = <Foo />; | ||
| var b = <div />; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| var React = require('React'); | ||
|
|
||
| React.createElement(Foo, {foo: 'bar', ...someObject}); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| var React = require('React'); | ||
|
|
||
| <Foo foo="bar" {...someObject} />; |
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can also use destructuring here: const {type} = property.value; |
||
|
|
||
| let value; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following alphabetical order