Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate correct types for cut/copy/paste events #92

Merged
merged 2 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/writer/writer-ts-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ function genSlotDef(def: Pick<ComponentDocApi, "slots">) {
.join("\n");
}

const mapEvent = (name: string) => {
if (["cut", "copy", "paste"].includes(name)) {
return "DocumentAndElementEventHandlersEventMap";
}

return "WindowEventMap";
};

function genEventDef(def: Pick<ComponentDocApi, "events">) {
const createDispatchedEvent = (detail: string = ANY_TYPE) => {
if (/CustomEvent/.test(detail)) return detail;
Expand All @@ -158,7 +166,7 @@ function genEventDef(def: Pick<ComponentDocApi, "events">) {
return def.events
.map((event) => {
return `${clampKey(event.name)}: ${
event.type === "dispatched" ? createDispatchedEvent(event.detail) : `WindowEventMap["${event.name}"]`
event.type === "dispatched" ? createDispatchedEvent(event.detail) : `${mapEvent(event.name)}["${event.name}"]`
};`;
})
.join("\n");
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/input-events/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input on:input on:change on:paste />
14 changes: 14 additions & 0 deletions tests/snapshots/input-events/output.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference types="svelte" />
import type { SvelteComponentTyped } from "svelte";

export interface InputProps {}

export default class Input extends SvelteComponentTyped<
InputProps,
{
input: WindowEventMap["input"];
change: WindowEventMap["change"];
paste: DocumentAndElementEventHandlersEventMap["paste"];
},
{}
> {}
23 changes: 23 additions & 0 deletions tests/snapshots/input-events/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"props": [],
"moduleExports": [],
"slots": [],
"events": [
{
"type": "forwarded",
"name": "input",
"element": "input"
},
{
"type": "forwarded",
"name": "change",
"element": "input"
},
{
"type": "forwarded",
"name": "paste",
"element": "input"
}
],
"typedefs": []
}