From 0dc6cf8fac64db9e4c879853d12eb91ea5f6894a Mon Sep 17 00:00:00 2001 From: metonym Date: Thu, 2 Jun 2022 18:17:51 -0700 Subject: [PATCH] fix: generate correct types for cut/copy/paste events (#92) * fix: generate correct types for cut/copy/paste events * Run "yarn test:snapshot" --- src/writer/writer-ts-definitions.ts | 10 +++++++++- tests/snapshots/input-events/input.svelte | 1 + tests/snapshots/input-events/output.d.ts | 14 ++++++++++++++ tests/snapshots/input-events/output.json | 23 +++++++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/snapshots/input-events/input.svelte create mode 100644 tests/snapshots/input-events/output.d.ts create mode 100644 tests/snapshots/input-events/output.json diff --git a/src/writer/writer-ts-definitions.ts b/src/writer/writer-ts-definitions.ts index 7b1496d..44d688b 100644 --- a/src/writer/writer-ts-definitions.ts +++ b/src/writer/writer-ts-definitions.ts @@ -150,6 +150,14 @@ function genSlotDef(def: Pick) { .join("\n"); } +const mapEvent = (name: string) => { + if (["cut", "copy", "paste"].includes(name)) { + return "DocumentAndElementEventHandlersEventMap"; + } + + return "WindowEventMap"; +}; + function genEventDef(def: Pick) { const createDispatchedEvent = (detail: string = ANY_TYPE) => { if (/CustomEvent/.test(detail)) return detail; @@ -158,7 +166,7 @@ function genEventDef(def: Pick) { 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"); diff --git a/tests/snapshots/input-events/input.svelte b/tests/snapshots/input-events/input.svelte new file mode 100644 index 0000000..879ee6b --- /dev/null +++ b/tests/snapshots/input-events/input.svelte @@ -0,0 +1 @@ + diff --git a/tests/snapshots/input-events/output.d.ts b/tests/snapshots/input-events/output.d.ts new file mode 100644 index 0000000..89355a9 --- /dev/null +++ b/tests/snapshots/input-events/output.d.ts @@ -0,0 +1,14 @@ +/// +import type { SvelteComponentTyped } from "svelte"; + +export interface InputProps {} + +export default class Input extends SvelteComponentTyped< + InputProps, + { + input: WindowEventMap["input"]; + change: WindowEventMap["change"]; + paste: DocumentAndElementEventHandlersEventMap["paste"]; + }, + {} +> {} diff --git a/tests/snapshots/input-events/output.json b/tests/snapshots/input-events/output.json new file mode 100644 index 0000000..8342aab --- /dev/null +++ b/tests/snapshots/input-events/output.json @@ -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": [] +} \ No newline at end of file