Skip to content

Commit

Permalink
test: {:else}, {:then} and {:catch} blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
bfanger committed Dec 2, 2022
1 parent 9bbe6f8 commit d0642a2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/lib/preprocessReact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import type {
TemplateNode,
Transition,
} from "svelte/types/compiler/interfaces";
import type IfBlock from "svelte/types/compiler/compile/nodes/IfBlock";
import type EachBlock from "svelte/types/compiler/compile/nodes/EachBlock";
import type AwaitBlock from "svelte/types/compiler/compile/nodes/AwaitBlock";
import type {
PreprocessorGroup,
Processed,
Expand Down Expand Up @@ -218,16 +221,16 @@ function replaceReactTags(
node.children?.forEach((child) => {
replaceReactTags(child, content, components);
});
// traverse else branch of IfBlock
node.else?.children?.forEach((child: TemplateNode) => {
(node as IfBlock | EachBlock).else?.children?.forEach((child) => {
replaceReactTags(child, content, components);
});
// traverse then branch of AwaitBlock
node.then?.children?.forEach((child: TemplateNode) => {
(node as AwaitBlock).pending?.children?.forEach((child) => {
replaceReactTags(child, content, components);
});
// traverse catch branch of AwaitBlock
node.catch?.children?.forEach((child: TemplateNode) => {
(node as AwaitBlock).then?.children?.forEach((child) => {
replaceReactTags(child, content, components);
});
(node as AwaitBlock).catch?.children?.forEach((child: TemplateNode) => {
replaceReactTags(child, content, components);
});
return components;
Expand Down
30 changes: 30 additions & 0 deletions src/tests/__snapshots__/preprocess.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,36 @@ const React$button = React$$sveltify(\\"button\\", React$$createPortal, React$$R
"
`;

exports[`svelte-preprocess-react > should process {:else} {:then} and {:catch} sections 1`] = `
"<script lang=\\"ts\\">import React$$sveltify from \\"svelte-preprocess-react/sveltify\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\"; const number = 1;
const Component = () => null;
export {};
const React$Component = React$$sveltify(Component, React$$createPortal, React$$ReactDOM, React$$renderToString);</script>
{#if number === 1}
<React$Component />
{:else if number === 2}
<React$Component />
{:else}
<React$Component />
{/if}
{#each [] as item}
<React$Component />
{:else}
<React$Component />
{/each}
{#await Promise.resolve()}
<React$Component />
{:then}
<React$Component />
{:catch}
<React$Component />
{/await}
"
`;

exports[`svelte-preprocess-react > should support typescript when using preprocess 1`] = `
"<script lang=\\"ts\\">export let title;
</script>
Expand Down
29 changes: 29 additions & 0 deletions src/tests/fixtures/Blocks.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script lang="ts">
import type React from "react";
const number = 1;
const Component: React.FC = () => null;
</script>

{#if number === 1}
<react:Component />
{:else if number === 2}
<react:Component />
{:else}
<react:Component />
{/if}

{#each [] as item}
<react:Component />
{:else}
<react:Component />
{/each}

{#await Promise.resolve()}
<react:Component />
{:then}
<react:Component />
{:catch}
<react:Component />
{/await}
10 changes: 10 additions & 0 deletions src/tests/preprocess.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ describe("svelte-preprocess-react", () => {
const output = await preprocess(src, preprocessReact(), { filename });
expect(output.code).toMatchSnapshot();
});
it("should process {:else} {:then} and {:catch} sections", async () => {
const filename = resolveFilename("./fixtures/Blocks.svelte");
const src = await readFile(filename, "utf8");
const output = await preprocess(
src,
preprocessReact({ preprocess: sveltePreprocess() }),
{ filename }
);
expect(output.code).toMatchSnapshot();
});
});

const base = dirname(import.meta.url).replace("file://", "");
Expand Down

0 comments on commit d0642a2

Please sign in to comment.