Skip to content

Commit

Permalink
fix: generate correct types for cut/copy/paste events (#92)
Browse files Browse the repository at this point in the history
* fix: generate correct types for cut/copy/paste events

* Run "yarn test:snapshot"
  • Loading branch information
metonym authored Jun 3, 2022
1 parent 5b003af commit 0dc6cf8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
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": []
}

0 comments on commit 0dc6cf8

Please sign in to comment.