From 93418ff424e4bab29f7189cc2d0da7792e7161a7 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 23 Nov 2023 17:56:19 +0100 Subject: [PATCH] fix: don't add generic type when snippet has no params --- .../src/htmlxtojsx_v2/nodes/SnippetBlock.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/SnippetBlock.ts b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/SnippetBlock.ts index 2385ab5d2..e29ac3e62 100644 --- a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/SnippetBlock.ts +++ b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/SnippetBlock.ts @@ -55,26 +55,27 @@ export function handleSnippet( transforms.push([startEnd, snippetBlock.end]); element.addProp([[snippetBlock.expression.start, snippetBlock.expression.end]], transforms); } else { - // slap any on to it to silence "implicit any" errors; JSDoc people can't add types to snippets - let typeAnnotation = surroundWithIgnoreComments(`: import('svelte').Snippet`); - if (snippetBlock.context?.typeAnnotation) { - typeAnnotation = surroundWithIgnoreComments( - `: import('svelte').Snippet<${str.original.slice( - snippetBlock.context.typeAnnotation.start, - snippetBlock.context.typeAnnotation.end - )}>` - ); - } + const generic = snippetBlock.context + ? snippetBlock.context.typeAnnotation + ? `<${str.original.slice( + snippetBlock.context.typeAnnotation.start, + snippetBlock.context.typeAnnotation.end + )}>` + : // slap any on to it to silence "implicit any" errors; JSDoc people can't add types to snippets + '' + : ''; + const typeAnnotation = surroundWithIgnoreComments(`: import('svelte').Snippet${generic}`); const transforms: TransformationArray = [ 'var ', [snippetBlock.expression.start, snippetBlock.expression.end], typeAnnotation + ' = (' ]; + if (snippetBlock.context) { transforms.push([snippetBlock.context.start, snippetBlock.context.end]); } - transforms.push(') => {'); + transforms.push(') => {'); transform(str, snippetBlock.start, startEnd, startEnd, transforms); } }