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

Feat: add jsx spread children support #7294

Merged
merged 2 commits into from
Nov 28, 2023
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
49 changes: 24 additions & 25 deletions src/js_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14392,14 +14392,18 @@ fn NewParser_(
// Use Next() instead of NextJSXElementChild() here since the next token is an expression
try p.lexer.next();

// The "..." here is ignored (it's used to signal an array type in TypeScript)
if (p.lexer.token == .t_dot_dot_dot and is_typescript_enabled) {
const is_spread = p.lexer.token == .t_dot_dot_dot;
if (is_spread) {
try p.lexer.next();
}

// The expression is optional, and may be absent
if (p.lexer.token != .t_close_brace) {
try children.append(try p.parseExpr(.lowest));
var item = try p.parseExpr(.lowest);
if (is_spread) {
item = p.newExpr(E.Spread{ .value = item }, loc);
}
try children.append(item);
}

// Use ExpectJSXElementChild() so we parse child strings
Expand Down Expand Up @@ -15087,28 +15091,23 @@ fn NewParser_(
props = props.items[0].value.?.data.e_object.properties.list();
}

// Babel defines static jsx as children.len > 1
const is_static_jsx = e_.children.len > 1;

// if (p.options.jsx.development) {

switch (e_.children.len) {
0 => {},
1 => {
props.append(allocator, G.Property{
.key = children_key,
.value = e_.children.ptr[0],
}) catch unreachable;
},
else => {
props.append(allocator, G.Property{
.key = children_key,
.value = p.newExpr(E.Array{
.items = e_.children,
.is_single_line = e_.children.len < 2,
}, e_.close_tag_loc),
}) catch unreachable;
},
// Typescript defines static jsx as children.len > 1 or single spread
// https://github.com/microsoft/TypeScript/blob/d4fbc9b57d9aa7d02faac9b1e9bb7b37c687f6e9/src/compiler/transformers/jsx.ts#L340
const is_static_jsx = e_.children.len > 1 or (e_.children.len == 1 and e_.children.ptr[0].data == .e_spread);

if (is_static_jsx) {
props.append(allocator, G.Property{
.key = children_key,
.value = p.newExpr(E.Array{
.items = e_.children,
.is_single_line = e_.children.len < 2,
}, e_.close_tag_loc),
}) catch bun.outOfMemory();
rhyzx marked this conversation as resolved.
Show resolved Hide resolved
} else if (e_.children.len == 1) {
props.append(allocator, G.Property{
.key = children_key,
.value = e_.children.ptr[0],
}) catch bun.outOfMemory();
}
// --- These must be done in all cases --

Expand Down
25 changes: 25 additions & 0 deletions test/transpiler/transpiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,31 @@ export var ComponentThatHasSpreadCausesDeopt = <Hello {...spread} />
});
});

it("JSX spread children", () => {
var bun = new Bun.Transpiler({
loader: "jsx",
define: {
"process.env.NODE_ENV": JSON.stringify("development"),
},
});
expect(bun.transformSync("export var foo = <div>{...a}b</div>")).toBe(
`export var foo = jsxDEV("div", {
children: [
...a,
"b"
]
}, undefined, true, undefined, this);
`,
);

expect(bun.transformSync("export var foo = <div>{...a}</div>")).toBe(
`export var foo = jsxDEV("div", {
children: [...a]
}, undefined, true, undefined, this);
`,
);
});

it("require with a dynamic non-string expression", () => {
var nodeTranspiler = new Bun.Transpiler({ platform: "node" });
expect(nodeTranspiler.transformSync("require('hi' + bar)")).toBe('require("hi" + bar);\n');
Expand Down