From f7142657d61bf68ef8eb65bd25d9c13bd3a533c4 Mon Sep 17 00:00:00 2001 From: Eli White Date: Sun, 8 Nov 2015 12:33:38 -0800 Subject: [PATCH] create-element-to-jsx-children: Convert .map calls into a JSXExpressionContainer --- test/__tests__/create-element-to-jsx-test.js | 2 ++ test/create-element-to-jsx-children-map.js | 7 +++++++ test/create-element-to-jsx-children-map.output.js | 3 +++ transforms/create-element-to-jsx.js | 8 ++++++-- 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 test/create-element-to-jsx-children-map.js create mode 100644 test/create-element-to-jsx-children-map.output.js diff --git a/test/__tests__/create-element-to-jsx-test.js b/test/__tests__/create-element-to-jsx-test.js index 6fb53724..084ffb1a 100644 --- a/test/__tests__/create-element-to-jsx-test.js +++ b/test/__tests__/create-element-to-jsx-test.js @@ -21,6 +21,8 @@ describe('create-element-to-jsx', () => { test('create-element-to-jsx', 'create-element-to-jsx-children'); + test('create-element-to-jsx', 'create-element-to-jsx-children-map'); + test('create-element-to-jsx', 'create-element-to-jsx-spread'); test('create-element-to-jsx', 'create-element-to-jsx-no-react'); diff --git a/test/create-element-to-jsx-children-map.js b/test/create-element-to-jsx-children-map.js new file mode 100644 index 00000000..ae04dc97 --- /dev/null +++ b/test/create-element-to-jsx-children-map.js @@ -0,0 +1,7 @@ +var React = require('React'); + +React.createElement( + 'div', + null, + foo.map(function() {}) +); diff --git a/test/create-element-to-jsx-children-map.output.js b/test/create-element-to-jsx-children-map.output.js new file mode 100644 index 00000000..ef73b3bd --- /dev/null +++ b/test/create-element-to-jsx-children-map.output.js @@ -0,0 +1,3 @@ +var React = require('React'); + +
{foo.map(function() {})}
; diff --git a/transforms/create-element-to-jsx.js b/transforms/create-element-to-jsx.js index ad833470..dc1df51e 100644 --- a/transforms/create-element-to-jsx.js +++ b/transforms/create-element-to-jsx.js @@ -43,9 +43,13 @@ module.exports = function(file, api, options) { const children = node.value.arguments.slice(2).map((child, index) => { if (child.type === 'Literal') { return j.literal(child.value); + } else if (child.type === 'CallExpression' && + child.callee.object.name === 'React' && + child.callee.property.name === 'createElement') { + return convertNodeToJSX(node.get('arguments', index + 2)); + } else { + return j.jsxExpressionContainer(child); } - - return convertNodeToJSX(node.get('arguments', index + 2)); }); const openingElement = j.jsxOpeningElement(j.jsxIdentifier(elementName), attributes);