Skip to content

Commit 0df3060

Browse files
authored
fix(rsc): preserve * for generators (#1183)
1 parent 5a9ea3d commit 0df3060

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// The hoist transform must preserve `function*` / `async function*` syntax.
2+
// Previously the `*` was dropped, making `yield` a SyntaxError.
3+
function outer() {
4+
const items = [1, 2, 3]
5+
6+
async function* stream() {
7+
'use server'
8+
for (const item of items) {
9+
yield item
10+
}
11+
}
12+
13+
return stream
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// The hoist transform must preserve `function*` / `async function*` syntax.
2+
// Previously the `*` was dropped, making `yield` a SyntaxError.
3+
function outer() {
4+
const items = [1, 2, 3]
5+
6+
const stream = /* #__PURE__ */ $$register($$hoist_0_stream, "<id>", "$$hoist_0_stream").bind(null, __enc([items]));
7+
8+
return stream
9+
}
10+
11+
;export async function* $$hoist_0_stream($$hoist_encoded) {
12+
const [items] = __dec($$hoist_encoded);
13+
'use server'
14+
for (const item of items) {
15+
yield item
16+
}
17+
};
18+
/* #__PURE__ */ Object.defineProperty($$hoist_0_stream, "name", { value: "stream" });
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// The hoist transform must preserve `function*` / `async function*` syntax.
2+
// Previously the `*` was dropped, making `yield` a SyntaxError.
3+
function outer() {
4+
const items = [1, 2, 3]
5+
6+
const stream = /* #__PURE__ */ $$register($$hoist_0_stream, "<id>", "$$hoist_0_stream").bind(null, items);
7+
8+
return stream
9+
}
10+
11+
;export async function* $$hoist_0_stream(items) {
12+
'use server'
13+
for (const item of items) {
14+
yield item
15+
}
16+
};
17+
/* #__PURE__ */ Object.defineProperty($$hoist_0_stream, "name", { value: "stream" });

packages/plugin-rsc/src/transforms/hoist.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export function transformHoistInlineDirective(
100100
node.body.start,
101101
`\n;${options.noExport ? '' : 'export '}${
102102
node.async ? 'async ' : ''
103-
}function ${newName}(${newParams}) `,
103+
}function${node.generator ? '*' : ''} ${newName}(${newParams}) `,
104104
)
105105
output.appendLeft(
106106
node.end,

0 commit comments

Comments
 (0)