Skip to content

Commit

Permalink
Add useBuiltIns option to helper-builder-react-jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism committed Oct 3, 2016
1 parent 9eb99de commit 5f232f6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/babel-helper-builder-react-jsx/src/index.js
Expand Up @@ -118,14 +118,19 @@ export default function (opts) {
/**
* The logic for this is quite terse. It's because we need to
* support spread elements. We loop over all attributes,
* breaking on spreads, we then push a new object containg
* breaking on spreads, we then push a new object containing
* all prior attributes to an array for later processing.
*/

function buildOpeningElementAttributes(attribs, file) {
let _props = [];
let objs = [];

let useBuiltIns = file.opts.useBuiltIns || false;
if (typeof useBuiltIns !== "boolean") {
throw new Error("transform-react-jsx currently only accepts a boolean option for useBuiltIns (defaults to false)");
}

function pushProps() {
if (!_props.length) return;

Expand Down Expand Up @@ -154,11 +159,12 @@ export default function (opts) {
objs.unshift(t.objectExpression([]));
}

const helper = useBuiltIns ?
t.memberExpression(t.identifier("Object"), t.identifier("assign")) :
file.addHelper("extends");

// spread it
attribs = t.callExpression(
file.addHelper("extends"),
objs
);
attribs = t.callExpression(helper, objs);
}

return attribs;
Expand Down
@@ -0,0 +1 @@
var div = <Component {...props} foo="bar" />
@@ -0,0 +1,4 @@
{
"plugins": [["transform-react-jsx", { "useBuiltIns": "invalidOption" }]],
"throws": "transform-react-jsx currently only accepts a boolean option for useBuiltIns (defaults to false)"
}
@@ -0,0 +1 @@
var div = <Component {...props} foo="bar" />
@@ -0,0 +1 @@
var div = React.createElement(Component, Object.assign({}, props, { foo: "bar" }));
@@ -0,0 +1,3 @@
{
"plugins": [["transform-react-jsx", { "useBuiltIns": true }]]
}

0 comments on commit 5f232f6

Please sign in to comment.