Skip to content

Commit 6027d3a

Browse files
committed
feat: use Snippet type
1 parent 98fe60c commit 6027d3a

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

packages/svelte2tsx/src/htmlxtojsx_v2/nodes/SnippetBlock.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export function handleSnippet(
3131
element?: InlineComponent
3232
): void {
3333
const endSnippet = str.original.lastIndexOf('{', snippetBlock.end - 1);
34-
str.overwrite(endSnippet, snippetBlock.end, '}', {
34+
// Return something to silence the "snippet type not assignable to return type void" error
35+
str.overwrite(endSnippet, snippetBlock.end, 'return __sveltets_2_any(0)}', {
3536
contentOnly: true
3637
});
3738

@@ -54,22 +55,23 @@ export function handleSnippet(
5455
transforms.push([startEnd, snippetBlock.end]);
5556
element.addProp([[snippetBlock.expression.start, snippetBlock.expression.end]], transforms);
5657
} else {
58+
// slap any on to it to silence "implicit any" errors; JSDoc people can't add types to snippets
59+
let typeAnnotation = surroundWithIgnoreComments(`: import('svelte').Snippet<any>`);
60+
if (snippetBlock.context?.typeAnnotation) {
61+
typeAnnotation = surroundWithIgnoreComments(
62+
`: import('svelte').Snippet<${str.original.slice(
63+
snippetBlock.context.typeAnnotation.start,
64+
snippetBlock.context.typeAnnotation.end
65+
)}>`
66+
);
67+
}
5768
const transforms: TransformationArray = [
5869
'var ',
5970
[snippetBlock.expression.start, snippetBlock.expression.end],
60-
' = ('
71+
typeAnnotation + ' = ('
6172
];
6273
if (snippetBlock.context) {
63-
const colonIdx = str.original.indexOf(':', snippetBlock.context.end);
64-
if (colonIdx > startEnd && colonIdx !== -1) {
65-
transforms.push(
66-
[snippetBlock.context.start, snippetBlock.context.end],
67-
// slap any on to it to silence "implicit any" errors; JSDoc people can't add types to snippets
68-
surroundWithIgnoreComments(': any')
69-
);
70-
} else {
71-
transforms.push([snippetBlock.context.start, snippetBlock.context.end]);
72-
}
74+
transforms.push([snippetBlock.context.start, snippetBlock.context.end]);
7375
}
7476
transforms.push(') => {');
7577

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
var foo = (x/*Ωignore_startΩ*/: any/*Ωignore_endΩ*/) => {
1+
var foo/*Ωignore_startΩ*/: import('svelte').Snippet<any>/*Ωignore_endΩ*/ = (x) => {
22
{ svelteHTML.createElement("div", {}); x; }
3-
}
3+
return __sveltets_2_any(0)}
44

55
;foo(1);
66
;foo();
77

88
{ const $$_tnenopmoC0C = __sveltets_2_ensureComponent(Component); new $$_tnenopmoC0C({ target: __sveltets_2_any(), props: {bar:(x) => {
99
{ svelteHTML.createElement("div", {}); x; }
10-
},}});
10+
return __sveltets_2_any(0)},}});
1111
{ svelteHTML.createElement("div", {});asd; }
1212

1313
Component}

packages/svelte2tsx/test/htmlx2jsx/samples/ts-in-template.skip/expectedv2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ try { const $$_value = await (foo as Promise<void>);{ const result: any = $$_val
1616

1717
item as string;
1818

19-
const foo = (bar: string/*Ωignore_startΩ*/: any/*Ωignore_endΩ*/) => { }
19+
var foo/*Ωignore_startΩ*/: import('svelte').Snippet<string>/*Ωignore_endΩ*/ = (bar: string) => { return __sveltets_2_any(0)}
2020

2121
;foo(bar as string);
2222

packages/svelte2tsx/test/svelte2tsx/samples/snippet-instance-script.skip/expectedv2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
let foo = true;
55
bar;
6-
var bar = () => { foo;
7-
};
6+
var bar/*Ωignore_startΩ*/: import('svelte').Snippet<any>/*Ωignore_endΩ*/ = () => { foo;
7+
return __sveltets_2_any(0)};
88
async () => {
99

1010
};

0 commit comments

Comments
 (0)