Skip to content

Commit

Permalink
test: add generics fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym committed Apr 20, 2024
1 parent 039e9d7 commit 97e833a
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 3 deletions.
128 changes: 125 additions & 3 deletions tests/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1141,9 +1141,31 @@ exports[`fixtures (JSON) "typed-props/input.svelte" 1`] = `
}"
`;

exports[`fixtures (JSON) "generics/Test.svelte" 1`] = `
"{
"props": [],
"moduleExports": [],
"slots": [],
"events": [],
"typedefs": [],
"generics": null
}"
`;

exports[`fixtures (JSON) "generics/input.svelte" 1`] = `
"{
"props": [
{
"name": "headers",
"kind": "let",
"type": "ReadonlyArray<DataTableHeader<Row>>",
"value": "[]",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
"constant": false,
"reactive": false
},
{
"name": "rows",
"kind": "let",
Expand All @@ -1167,9 +1189,19 @@ exports[`fixtures (JSON) "generics/input.svelte" 1`] = `
"events": [],
"typedefs": [
{
"type": "Record<string, any>",
"type": "{ id: string | number; [key: string]: any; }",
"name": "DataTableRow",
"ts": "type DataTableRow = Record<string, any>"
"ts": "interface DataTableRow { id: string | number; [key: string]: any; }"
},
{
"type": "Exclude<keyof Row, \"id\">",
"name": "DataTableKey<Row>",
"ts": "type DataTableKey<Row> = Exclude<keyof Row, \"id\">"
},
{
"type": "{ key: DataTableKey<Row>; value: string; }",
"name": "DataTableHeader<Row>",
"ts": "interface DataTableHeader<Row> { key: DataTableKey<Row>; value: string; }"
}
],
"generics": [
Expand Down Expand Up @@ -1796,12 +1828,36 @@ export default class TypedProps extends SvelteComponentTyped<TypedPropsProps, Re
"
`;
exports[`fixtures (TypeScript) "generics/Test.svelte" 1`] = `
"import type { SvelteComponentTyped } from "svelte";
export interface GenericsProps {}
export default class Generics extends SvelteComponentTyped<GenericsProps, Record<string, any>, {}> {}
"
`;
exports[`fixtures (TypeScript) "generics/input.svelte" 1`] = `
"import type { SvelteComponentTyped } from "svelte";
export type DataTableRow = Record<string, any>;
export interface DataTableRow {
id: string | number;
[key: string]: any;
}
export type DataTableKey<Row> = Exclude<keyof Row, "id">;
export interface DataTableHeader<Row> {
key: DataTableKey<Row>;
value: string;
}
export interface GenericsProps<Row> {
/**
* @default []
*/
headers?: ReadonlyArray<DataTableHeader<Row>>;
/**
* @default []
*/
Expand All @@ -1815,3 +1871,69 @@ export default class Generics<Row extends DataTableRow = DataTableRow> extends S
> {}
"
`;
exports[`fixtures (TypeScript) "svg-props/input.svelte" 1`] = `
"import type { SvelteComponentTyped } from "svelte";
import type { SvelteHTMLElements } from "svelte/elements";
type RestProps = SvelteHTMLElements["svg"];
export interface SvgPropsProps extends RestProps {
[key: \`data-${string}\`]: any;
}
export default class SvgProps extends SvelteComponentTyped<SvgPropsProps, Record<string, any>, {}> {}
"
`;
exports[`fixtures (TypeScript) "rest-props/input.svelte" 1`] = `
"import type { SvelteComponentTyped } from "svelte";
import type { SvelteHTMLElements } from "svelte/elements";
type RestProps = SvelteHTMLElements["h1"];
export interface RestPropsProps extends RestProps {
[key: \`data-${string}\`]: any;
}
export default class RestProps extends SvelteComponentTyped<RestPropsProps, Record<string, any>, {}> {}
"
`;
exports[`fixtures (TypeScript) "rest-props-multiple/input.svelte" 1`] = `
"import type { SvelteComponentTyped } from "svelte";
import type { SvelteHTMLElements } from "svelte/elements";
type RestProps = SvelteHTMLElements["h1"] & SvelteHTMLElements["span"];
export interface RestPropsMultipleProps extends RestProps {
/**
* @default false
*/
edit?: boolean;
/**
* @default false
*/
heading?: boolean;
[key: \`data-${string}\`]: any;
}
export default class RestPropsMultiple extends SvelteComponentTyped<RestPropsMultipleProps, Record<string, any>, {}> {}
"
`;
exports[`fixtures (TypeScript) "anchor-props/input.svelte" 1`] = `
"import type { SvelteComponentTyped } from "svelte";
import type { SvelteHTMLElements } from "svelte/elements";
type RestProps = SvelteHTMLElements["a"];
export interface AnchorPropsProps extends RestProps {
[key: \`data-${string}\`]: any;
}
export default class AnchorProps extends SvelteComponentTyped<AnchorPropsProps, Record<string, any>, { default: {} }> {}
"
`;
26 changes: 26 additions & 0 deletions tests/fixtures/generics/Test.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script lang="ts">
import Generics from "./output";
</script>

<Generics
headers={[
{
key: "name",
value: "Name",
},
{
key: "port",
value: "Port",
},
]}
rows={[
{ id: 1, name: "Name 1", port: 3000 },
{ id: 2, name: "Name 2", port: 3000 },
{ id: 3, name: "Name 3", port: 3000 },
]}
let:rows
>
{#each rows as row}
{row.name}
{/each}
</Generics>
17 changes: 17 additions & 0 deletions tests/fixtures/generics/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
/**
* @typedef {{ id: string | number; [key: string]: any; }} DataTableRow
* @typedef {Exclude<keyof Row, "id">} DataTableKey<Row>
* @typedef {{ key: DataTableKey<Row>; value: string; }} DataTableHeader<Row>
* @template {DataTableRow} Row
* @generics {Row extends DataTableRow = DataTableRow} Row
*/
/** @type {ReadonlyArray<DataTableHeader<Row>>} */
export let headers = [];
/** @type {ReadonlyArray<Row>} */
export let rows = [];
</script>

<slot {rows} />
31 changes: 31 additions & 0 deletions tests/fixtures/generics/output.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { SvelteComponentTyped } from "svelte";

export interface DataTableRow {
id: string | number;
[key: string]: any;
}

export type DataTableKey<Row> = Exclude<keyof Row, "id">;

export interface DataTableHeader<Row> {
key: DataTableKey<Row>;
value: string;
}

export interface GenericsProps<Row> {
/**
* @default []
*/
headers?: ReadonlyArray<DataTableHeader<Row>>;

/**
* @default []
*/
rows?: ReadonlyArray<Row>;
}

export default class Generics<Row extends DataTableRow = DataTableRow> extends SvelteComponentTyped<
GenericsProps<Row>,
Record<string, any>,
{ default: { rows: ReadonlyArray<Row> } }
> {}
56 changes: 56 additions & 0 deletions tests/fixtures/generics/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"props": [
{
"name": "headers",
"kind": "let",
"type": "ReadonlyArray<DataTableHeader<Row>>",
"value": "[]",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
"constant": false,
"reactive": false
},
{
"name": "rows",
"kind": "let",
"type": "ReadonlyArray<Row>",
"value": "[]",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
"constant": false,
"reactive": false
}
],
"moduleExports": [],
"slots": [
{
"name": "__default__",
"default": true,
"slot_props": "{ rows: ReadonlyArray<Row> }"
}
],
"events": [],
"typedefs": [
{
"type": "{ id: string | number; [key: string]: any; }",
"name": "DataTableRow",
"ts": "interface DataTableRow { id: string | number; [key: string]: any; }"
},
{
"type": "Exclude<keyof Row, \"id\">",
"name": "DataTableKey<Row>",
"ts": "type DataTableKey<Row> = Exclude<keyof Row, \"id\">"
},
{
"type": "{ key: DataTableKey<Row>; value: string; }",
"name": "DataTableHeader<Row>",
"ts": "interface DataTableHeader<Row> { key: DataTableKey<Row>; value: string; }"
}
],
"generics": [
"Row",
"Row extends DataTableRow = DataTableRow"
]
}

0 comments on commit 97e833a

Please sign in to comment.