Skip to content

Commit d325cac

Browse files
authored
Merge pull request #4 from babel/jsx-object-assign
Use Object.assign() for JSX spread
2 parents dff636d + 12ff9d7 commit d325cac

File tree

26 files changed

+219
-33
lines changed

26 files changed

+219
-33
lines changed

src/plugins/transform-jsx-spread/index.js

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,60 @@ import esutils from "esutils";
1010
* h("div", { a: "1", ...b })
1111
*/
1212
export default ({ types: t }) => {
13-
function convertAttribute(node) {
13+
// converts a set of JSXAttributes to an Object.assign() call
14+
function convertAttributesAssign(attributes) {
15+
const args = [];
16+
for (let i = 0, current; i < attributes.length; i++) {
17+
const node = attributes[i];
18+
if (t.isJSXSpreadAttribute(node)) {
19+
// the first attribute is a spread, avoid copying all other attributes onto it
20+
if (i === 0) {
21+
args.push(t.objectExpression([]));
22+
}
23+
current = null;
24+
args.push(node.argument);
25+
} else {
26+
const name = getAttributeName(node);
27+
const value = getAttributeValue(node);
28+
if (!current) {
29+
current = t.objectExpression([]);
30+
args.push(current);
31+
}
32+
current.properties.push(t.objectProperty(name, value));
33+
}
34+
}
35+
return t.callExpression(
36+
t.memberExpression(t.identifier("Object"), t.identifier("assign")),
37+
args
38+
);
39+
}
40+
41+
// Converts a JSXAttribute to the equivalent ObjectExpression property
42+
function convertAttributeSpread(node) {
1443
if (t.isJSXSpreadAttribute(node)) {
1544
return t.spreadElement(node.argument);
1645
}
1746

47+
const name = getAttributeName(node);
48+
const value = getAttributeValue(node);
49+
return t.inherits(t.objectProperty(name, value), node);
50+
}
51+
52+
// Convert a JSX attribute name to an Object expression property name
53+
function getAttributeName(node) {
54+
if (t.isJSXNamespacedName(node.name)) {
55+
return t.stringLiteral(
56+
node.name.namespace.name + ":" + node.name.name.name
57+
);
58+
}
59+
if (esutils.keyword.isIdentifierNameES6(node.name.name)) {
60+
return t.identifier(node.name.name);
61+
}
62+
return t.stringLiteral(node.name.name);
63+
}
64+
65+
// Convert a JSX attribute value to a JavaScript expression value
66+
function getAttributeValue(node) {
1867
let value = node.value || t.booleanLiteral(true);
1968

2069
if (t.isJSXExpressionContainer(value)) {
@@ -28,33 +77,34 @@ export default ({ types: t }) => {
2877
}
2978
}
3079

31-
if (t.isJSXNamespacedName(node.name)) {
32-
node.name = t.stringLiteral(
33-
node.name.namespace.name + ":" + node.name.name.name
34-
);
35-
} else if (esutils.keyword.isIdentifierNameES6(node.name.name)) {
36-
node.name.type = "Identifier";
37-
} else {
38-
node.name = t.stringLiteral(node.name.name);
39-
}
40-
41-
return t.inherits(t.objectProperty(node.name, value), node);
80+
return value;
4281
}
4382

4483
return {
45-
name: "transform-hoist-tagged-templates",
84+
name: "transform-jsx-spread",
4685
visitor: {
47-
JSXOpeningElement(path) {
48-
const hasSpread = path.node.attributes.some(t.isJSXSpreadAttribute);
86+
JSXOpeningElement(path, state) {
87+
const useSpread = state.opts.useSpread === true;
88+
const hasSpread = path.node.attributes.some(attr =>
89+
t.isJSXSpreadAttribute(attr)
90+
);
4991

5092
// ignore JSX Elements without spread or with lone spread:
5193
if (!hasSpread || path.node.attributes.length === 1) return;
5294

53-
path.node.attributes = [
54-
t.jsxSpreadAttribute(
55-
t.objectExpression(path.node.attributes.map(convertAttribute))
56-
),
57-
];
95+
if (useSpread) {
96+
path.node.attributes = [
97+
t.jsxSpreadAttribute(
98+
t.objectExpression(
99+
path.node.attributes.map(convertAttributeSpread)
100+
)
101+
),
102+
];
103+
} else {
104+
path.node.attributes = [
105+
t.jsxSpreadAttribute(convertAttributesAssign(path.node.attributes)),
106+
];
107+
}
58108
},
59109
},
60110
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div {...props} b="hi">a</div>;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../../lib/plugins/transform-jsx-spread",
5+
{
6+
"useSpread": true
7+
}
8+
],
9+
[
10+
"@babel/plugin-transform-react-jsx",
11+
{
12+
"pragma": "h"
13+
}
14+
]
15+
]
16+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h("div", { ...props,
2+
b: "hi"
3+
}, "a");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div {...props} />;
2+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../../lib/plugins/transform-jsx-spread",
5+
{
6+
"useSpread": true
7+
}
8+
],
9+
[
10+
"@babel/plugin-transform-react-jsx",
11+
{
12+
"pragma": "h"
13+
}
14+
]
15+
]
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
h("div", props);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div a={1} {...props} b />;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../../lib/plugins/transform-jsx-spread",
5+
{
6+
"useSpread": true
7+
}
8+
],
9+
[
10+
"@babel/plugin-transform-react-jsx",
11+
{
12+
"pragma": "h"
13+
}
14+
]
15+
]
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
h("div", {
2+
a: 1,
3+
...props,
4+
b: true
5+
});

0 commit comments

Comments
 (0)