Skip to content

Commit

Permalink
fix: allow for whitespace in snippets declaration (#2366)
Browse files Browse the repository at this point in the history
Fixes sveltejs/svelte#11478

The problem was that the generic generation was relying on the start position of typeAnnotation + 1. But if there are whytespaces this is not correct and the generate type is wrong Snippet<[: string]>. However inside the first typeAnnotation there's always a second typeAnnotation field which only includes the actual types
  • Loading branch information
paoloricciuti authored May 13, 2024
1 parent 9d7907e commit 80622df
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
7 changes: 5 additions & 2 deletions packages/svelte2tsx/src/htmlxtojsx_v2/nodes/SnippetBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ export function handleSnippet(
if (snippetBlock.parameters?.length) {
generic = `<[${snippetBlock.parameters
.map((p) =>
p.typeAnnotation
? str.original.slice(p.typeAnnotation.start + 1, p.typeAnnotation.end)
p.typeAnnotation?.typeAnnotation
? str.original.slice(
p.typeAnnotation.typeAnnotation.start,
p.typeAnnotation.typeAnnotation.end
)
: // slap any on to it to silence "implicit any" errors; JSDoc people can't add types to snippets
'any'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ try { const $$_value = await (foo as Promise<void>);{ const result: any = $$_val

item as string;

var foo/*Ωignore_startΩ*/: import('svelte').Snippet<[ string]>/*Ωignore_endΩ*/ = (bar: string) => {async () => { };return __sveltets_2_any(0)};
var foo/*Ωignore_startΩ*/: import('svelte').Snippet<[string]>/*Ωignore_endΩ*/ = (bar: string) => {async () => { };return __sveltets_2_any(0)};

var foo2/*Ωignore_startΩ*/: import('svelte').Snippet<[string]>/*Ωignore_endΩ*/ = (bar : string) => {async () => { };return __sveltets_2_any(0)};

var foo3/*Ωignore_startΩ*/: import('svelte').Snippet<[string | number]>/*Ωignore_endΩ*/ = (bar : string | number) => {async () => { };return __sveltets_2_any(0)};

var foo3/*Ωignore_startΩ*/: import('svelte').Snippet<[string | number, (str: string)=>void]>/*Ωignore_endΩ*/ = (bar : string | number, baz : (str: string)=>void) => {async () => { };return __sveltets_2_any(0)};

;__sveltets_2_ensureSnippet(foo(bar as string));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
snippet
{/snippet}

{#snippet foo2(bar : string)}
snippet
{/snippet}

{#snippet foo3(bar : string | number)}
snippet
{/snippet}

{#snippet foo3(bar : string | number, baz : (str: string)=>void)}
snippet
{/snippet}

{@render foo(bar as string)}

<button onclick={(e: Event) => {e as any}}>click</button>
Expand Down

0 comments on commit 80622df

Please sign in to comment.