Skip to content

Commit d0642a2

Browse files
committed
test: {:else}, {:then} and {:catch} blocks
1 parent 9bbe6f8 commit d0642a2

File tree

4 files changed

+78
-6
lines changed

4 files changed

+78
-6
lines changed

src/lib/preprocessReact.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import type {
77
TemplateNode,
88
Transition,
99
} from "svelte/types/compiler/interfaces";
10+
import type IfBlock from "svelte/types/compiler/compile/nodes/IfBlock";
11+
import type EachBlock from "svelte/types/compiler/compile/nodes/EachBlock";
12+
import type AwaitBlock from "svelte/types/compiler/compile/nodes/AwaitBlock";
1013
import type {
1114
PreprocessorGroup,
1215
Processed,
@@ -218,16 +221,16 @@ function replaceReactTags(
218221
node.children?.forEach((child) => {
219222
replaceReactTags(child, content, components);
220223
});
221-
// traverse else branch of IfBlock
222-
node.else?.children?.forEach((child: TemplateNode) => {
224+
(node as IfBlock | EachBlock).else?.children?.forEach((child) => {
223225
replaceReactTags(child, content, components);
224226
});
225-
// traverse then branch of AwaitBlock
226-
node.then?.children?.forEach((child: TemplateNode) => {
227+
(node as AwaitBlock).pending?.children?.forEach((child) => {
227228
replaceReactTags(child, content, components);
228229
});
229-
// traverse catch branch of AwaitBlock
230-
node.catch?.children?.forEach((child: TemplateNode) => {
230+
(node as AwaitBlock).then?.children?.forEach((child) => {
231+
replaceReactTags(child, content, components);
232+
});
233+
(node as AwaitBlock).catch?.children?.forEach((child: TemplateNode) => {
231234
replaceReactTags(child, content, components);
232235
});
233236
return components;

src/tests/__snapshots__/preprocess.spec.ts.snap

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,36 @@ const React$button = React$$sveltify(\\"button\\", React$$createPortal, React$$R
145145
"
146146
`;
147147

148+
exports[`svelte-preprocess-react > should process {:else} {:then} and {:catch} sections 1`] = `
149+
"<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;
150+
const Component = () => null;
151+
export {};
152+
const React$Component = React$$sveltify(Component, React$$createPortal, React$$ReactDOM, React$$renderToString);</script>
153+
154+
{#if number === 1}
155+
<React$Component />
156+
{:else if number === 2}
157+
<React$Component />
158+
{:else}
159+
<React$Component />
160+
{/if}
161+
162+
{#each [] as item}
163+
<React$Component />
164+
{:else}
165+
<React$Component />
166+
{/each}
167+
168+
{#await Promise.resolve()}
169+
<React$Component />
170+
{:then}
171+
<React$Component />
172+
{:catch}
173+
<React$Component />
174+
{/await}
175+
"
176+
`;
177+
148178
exports[`svelte-preprocess-react > should support typescript when using preprocess 1`] = `
149179
"<script lang=\\"ts\\">export let title;
150180
</script>

src/tests/fixtures/Blocks.svelte

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<script lang="ts">
2+
import type React from "react";
3+
4+
const number = 1;
5+
6+
const Component: React.FC = () => null;
7+
</script>
8+
9+
{#if number === 1}
10+
<react:Component />
11+
{:else if number === 2}
12+
<react:Component />
13+
{:else}
14+
<react:Component />
15+
{/if}
16+
17+
{#each [] as item}
18+
<react:Component />
19+
{:else}
20+
<react:Component />
21+
{/each}
22+
23+
{#await Promise.resolve()}
24+
<react:Component />
25+
{:then}
26+
<react:Component />
27+
{:catch}
28+
<react:Component />
29+
{/await}

src/tests/preprocess.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ describe("svelte-preprocess-react", () => {
109109
const output = await preprocess(src, preprocessReact(), { filename });
110110
expect(output.code).toMatchSnapshot();
111111
});
112+
it("should process {:else} {:then} and {:catch} sections", async () => {
113+
const filename = resolveFilename("./fixtures/Blocks.svelte");
114+
const src = await readFile(filename, "utf8");
115+
const output = await preprocess(
116+
src,
117+
preprocessReact({ preprocess: sveltePreprocess() }),
118+
{ filename }
119+
);
120+
expect(output.code).toMatchSnapshot();
121+
});
112122
});
113123

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

0 commit comments

Comments
 (0)