-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improved typing + error detection
- Loading branch information
Bob Fanger
committed
Jun 10, 2022
1 parent
a0f70b3
commit 1123d07
Showing
18 changed files
with
210 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export interface ConstructorOf<T> { | ||
new (): T; | ||
} | ||
|
||
type Uppercase = | ||
| "A" | ||
| "B" | ||
| "C" | ||
| "D" | ||
| "E" | ||
| "F" | ||
| "G" | ||
| "H" | ||
| "I" | ||
| "J" | ||
| "K" | ||
| "L" | ||
| "M" | ||
| "N" | ||
| "S" | ||
| "T" | ||
| "U" | ||
| "V" | ||
| "W" | ||
| "X" | ||
| "Y" | ||
| "Z"; | ||
|
||
type EventKey = `on${Uppercase}${string}`; | ||
type ExcludeProps<T> = T extends EventKey ? T : never; | ||
type ExcludeEvents<T> = T extends EventKey ? never : T; | ||
|
||
export type PropsOf<T> = Omit<T, ExcludeProps<keyof T>>; | ||
export type EventsOf<T> = Omit<T, ExcludeEvents<keyof T>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,4 @@ | ||
<script lang="ts"> | ||
import Counter from "../demo/react-components/Counter"; | ||
import sveltify from "$lib/sveltifyReact18"; | ||
let toggle = true; | ||
let initialValue = 10; | ||
const ReactCounter = sveltify(Counter); | ||
</script> | ||
|
||
<label> | ||
<input type="checkbox" bind:checked={toggle} /> | ||
{toggle} | ||
</label> | ||
{#if toggle} | ||
<ReactCounter {initialValue} /> | ||
{/if} | ||
|
||
<div> | ||
<button | ||
on:click={() => { | ||
initialValue = Math.random(); | ||
}}>Change</button | ||
> | ||
</div> | ||
|
||
<style> | ||
label { | ||
display: block; | ||
user-select: none; | ||
cursor: pointer; | ||
} | ||
</style> | ||
<ul> | ||
<li><a href="/via-preprocessor">via preprocessor</a></li> | ||
<li><a href="/via-lib">via lib</a></li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script lang="ts"> | ||
import sveltifyReact18 from "$lib/sveltifyReact18"; | ||
import Counter from "../demo/react-components/Counter"; | ||
const ReactCounter = sveltifyReact18(Counter); | ||
</script> | ||
|
||
<ReactCounter count={10} onCount={console.info} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script lang="ts"> | ||
import Counter from "../demo/react-components/Counter"; | ||
</script> | ||
|
||
<react:Counter initial={10} on:count={console.info} /> |
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
src/tests/__snapshots__/svelte-preprocess-react.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<script> | ||
/** eslint-disable ValidationError */ | ||
import Counter from "./Counter"; | ||
let count = 1; | ||
</script> | ||
|
||
<react:Counter bind:count /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<script> | ||
// @ts-nocheck | ||
import Counter from "./Counter"; | ||
let count = 1; | ||
</script> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<!-- Counter could be a global variable --> | ||
<react:Counter /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<script> | ||
// @ts-nocheck | ||
import Counter from "./Counter"; | ||
const count = 1; | ||
</script> | ||
|
||
<react:Counter {count}> | ||
<div>Hello World</div> | ||
</react:Counter> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,57 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { preprocess } from "svelte/compiler"; | ||
import { readFile } from "fs/promises"; | ||
import { dirname, resolve } from "path"; | ||
import preprocessReact from "../lib/svelte-preprocess-react"; | ||
import source from "./fixtures/Container.svelte?raw"; | ||
|
||
describe("preprocess-react", () => { | ||
it("should process <react:component> tags", async () => { | ||
const output = await preprocess(source, preprocessReact(), { | ||
filename: "Counter.svelte", | ||
}); | ||
const filename = resolveFilename("./fixtures/Container.svelte"); | ||
const src = await readFile(filename, "utf8"); | ||
const output = await preprocess(src, preprocessReact(), { filename }); | ||
expect(output.code).toMatchSnapshot(); | ||
}); | ||
|
||
it("should fail on bindings", async () => { | ||
const filename = resolveFilename("./fixtures/Binding.svelte"); | ||
const src = await readFile(filename, "utf8"); | ||
let failed: boolean; | ||
try { | ||
await preprocess(src, preprocessReact(), { filename }); | ||
failed = false; | ||
} catch (err: any) { | ||
expect(err.message).toMatchSnapshot(); | ||
failed = true; | ||
} | ||
expect(failed).toBe(true); | ||
}); | ||
|
||
it("should fail on slots (for now)", async () => { | ||
const filename = resolveFilename("./fixtures/Slots.svelte"); | ||
const src = await readFile(filename, "utf8"); | ||
let failed: boolean; | ||
try { | ||
await preprocess(src, preprocessReact(), { filename }); | ||
failed = false; | ||
} catch (err: any) { | ||
expect(err.message).toBe( | ||
"Nested components are not (yet) supported in svelte-preprocess-react" | ||
); | ||
failed = true; | ||
} | ||
expect(failed).toBe(true); | ||
}); | ||
|
||
it("should inject a script tag", async () => { | ||
const filename = resolveFilename("./fixtures/NoScript.svelte"); | ||
const src = await readFile(filename, "utf8"); | ||
const output = await preprocess(src, preprocessReact(), { filename }); | ||
expect(output.code).toContain("<script>"); | ||
expect(output.code).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
const base = dirname(import.meta.url).replace("file://", ""); | ||
function resolveFilename(filename: string) { | ||
return resolve(base, filename); | ||
} |
Oops, something went wrong.