fix: incorrect island child validation (#1740)

This commit is contained in:
Marvin Hagemeister
2023-09-06 11:11:52 +02:00
committed by GitHub
parent 96d8eb4ffc
commit 227cefbf50
4 changed files with 40 additions and 4 deletions
+6 -3
View File
@@ -237,14 +237,17 @@ options.__b = (vnode: VNode<Record<string, unknown>>) => {
// Guard against passing objects as children to JSX
if (
children !== null && !Array.isArray(children) &&
!isValidElement(children)
typeof children === "function" || (
children !== null && typeof children === "object" &&
!Array.isArray(children) &&
!isValidElement(children)
)
) {
const name = originalType.displayName || originalType.name ||
"Anonymous";
throw new Error(
`Invalid JSX child passed to island <${name} />. Only JSX elements can be passed as children to an island. To resolve this error, pass the data as a standard prop instead.`,
`Invalid JSX child passed to island <${name} />. To resolve this error, pass the data as a standard prop instead.`,
);
}
@@ -18,6 +18,7 @@ import * as $12 from "./routes/island_jsx_text.tsx";
import * as $13 from "./routes/island_nested_props.tsx";
import * as $14 from "./routes/island_order.tsx";
import * as $15 from "./routes/island_siblings.tsx";
import * as $16 from "./routes/island_valid_children.tsx";
import * as $$0 from "./islands/BooleanButton.tsx";
import * as $$1 from "./islands/Counter.tsx";
import * as $$2 from "./islands/FragmentIsland.tsx";
@@ -47,6 +48,7 @@ const manifest = {
"./routes/island_nested_props.tsx": $13,
"./routes/island_order.tsx": $14,
"./routes/island_siblings.tsx": $15,
"./routes/island_valid_children.tsx": $16,
},
islands: {
"./islands/BooleanButton.tsx": $$0,
@@ -0,0 +1,24 @@
import { PassThrough } from "../islands/PassThrough.tsx";
function Foo() {
return <h1>foo</h1>;
}
export default function Page() {
return (
<div id="page">
<PassThrough>hello</PassThrough>
<PassThrough>{2}</PassThrough>
<PassThrough>{null}</PassThrough>
<PassThrough>{true}</PassThrough>
<PassThrough>{false}</PassThrough>
<PassThrough>{undefined}</PassThrough>
<PassThrough>
<h1>hello</h1>
</PassThrough>
<PassThrough>
<Foo />
</PassThrough>
</div>
);
}
+8 -1
View File
@@ -6,6 +6,7 @@ import {
Page,
} from "./deps.ts";
import {
assertNotSelector,
assertSelector,
assertTextMatch,
clickWhenListenerReady,
@@ -564,7 +565,7 @@ Deno.test({
sanitizeResources: false,
});
Deno.test("throws when passing non-jsx children to an island", async () => {
Deno.test("throws when passing non-jsx children to an island", async (t) => {
await withFresh(
"./tests/fixture_island_nesting/dev.ts",
async (address) => {
@@ -577,6 +578,12 @@ Deno.test("throws when passing non-jsx children to an island", async () => {
assertSelector(doc2, ".frsh-error-page");
assertTextMatch(doc2, "pre", /Invalid JSX child passed to island/);
await t.step("should not throw on valid children", async () => {
const doc2 = await fetchHtml(`${address}/island_valid_children`);
assertNotSelector(doc2, ".frsh-error-page");
});
},
);
});