Skip to content
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

Add useBuiltIns option to helper-builder-react-jsx #4655

Merged
merged 1 commit into from Oct 14, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 }]]
}