Skip to content

Commit a2f86e1

Browse files
committed
fix: snippet cleanup
- adjust snippet function to handle no params through function overload - move top level snippets to start of instance script to prevent "cannot be used before declaration" errors - append `;` to non-prop-snippets to ensure code afterwards is a new statement - fix snippet prop for js version (`?` isn't valid there)
1 parent 5aa950a commit a2f86e1

File tree

30 files changed

+50
-46
lines changed

30 files changed

+50
-46
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,22 @@ export function handleSnippet(
3030
snippetBlock: BaseNode,
3131
component?: InlineComponent
3232
): void {
33+
const isImplicitProp = component !== undefined;
3334
const endSnippet = str.original.lastIndexOf('{', snippetBlock.end - 1);
3435
// 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)}', {
36-
contentOnly: true
37-
});
36+
str.overwrite(
37+
endSnippet,
38+
snippetBlock.end,
39+
`return __sveltets_2_any(0)}${isImplicitProp ? '' : ';'}`,
40+
{
41+
contentOnly: true
42+
}
43+
);
3844

3945
const startEnd =
4046
str.original.indexOf('}', snippetBlock.context?.end || snippetBlock.expression.end) + 1;
4147

42-
if (component !== undefined) {
48+
if (isImplicitProp) {
4349
str.overwrite(snippetBlock.start, snippetBlock.expression.start, '', { contentOnly: true });
4450
const transforms: TransformationArray = ['('];
4551
if (snippetBlock.context) {

packages/svelte2tsx/src/svelte2tsx/createRenderFunction.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ export function createRenderFunction({
7474
}
7575
str.overwrite(scriptTag.start + 1, start - 1, `function render`);
7676
str.overwrite(start - 1, start, `<`); // if the generics are unused, only this char is colored opaque
77-
if (end < scriptTagEnd) {
78-
str.overwrite(end, scriptTagEnd, `>() {${propsDecl}\n`);
79-
} else {
80-
str.prependRight(end, `>() {${propsDecl}\n`);
81-
}
77+
str.overwrite(end, scriptTagEnd, `>() {${propsDecl}\n`);
8278
} else {
8379
str.overwrite(
8480
scriptTag.start + 1,
@@ -87,10 +83,11 @@ export function createRenderFunction({
8783
);
8884
}
8985

90-
const scriptEndTagStart = htmlx.lastIndexOf('<', scriptTag.end - 1);
9186
for (const rootSnippet of rootSnippets) {
92-
str.move(rootSnippet[0], rootSnippet[1], scriptEndTagStart);
87+
str.move(rootSnippet[0], rootSnippet[1], scriptTagEnd);
9388
}
89+
90+
const scriptEndTagStart = htmlx.lastIndexOf('<', scriptTag.end - 1);
9491
// wrap template with callback
9592
str.overwrite(scriptEndTagStart, scriptTag.end, `${slotsDeclaration};\nasync () => {`, {
9693
contentOnly: true

packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ export class ExportedNames {
594594
): string[] {
595595
return names.map(([key, value]) => {
596596
if (value.implicitChildren) {
597-
return `children?: ${
597+
return `children: ${
598598
value.implicitChildren === 'empty'
599599
? '__sveltets_2_snippet()'
600600
: '$$implicit_children'

packages/svelte2tsx/svelte-shims-v4.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ declare function __sveltets_2_cssProp(prop: Record<string, any>): {};
148148
// @ts-ignore Svelte v3/v4 don't have this
149149
declare function __sveltets_2_ensureSnippet(val: ReturnType<import('svelte').Snippet>): any;
150150
// @ts-ignore Svelte v3/v4 don't have this
151-
declare function __sveltets_2_snippet<T = void>(t: T): import('svelte').Snippet<T>;
151+
declare function __sveltets_2_snippet(): import('svelte').Snippet<void>;
152+
declare function __sveltets_2_snippet<T>(t: T): import('svelte').Snippet<T>;
152153

153154
/** @internal PRIVATE API, DO NOT USE */
154155
type __sveltets_2_SvelteAnimationReturnType = {

packages/svelte2tsx/test/htmlx2jsx/samples/snippet.v5/expected-svelte5.js

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

55
var bar/*Ωignore_startΩ*/: import('svelte').Snippet/*Ωignore_endΩ*/ = () => {
66
{ svelteHTML.createElement("div", {}); }
7-
return __sveltets_2_any(0)}
7+
return __sveltets_2_any(0)};
88

99
;__sveltets_2_ensureSnippet(foo(1));
1010
;__sveltets_2_ensureSnippet(bar());

packages/svelte2tsx/test/htmlx2jsx/samples/ts-in-template.v5/expected-svelte5.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-
var foo/*Ωignore_startΩ*/: import('svelte').Snippet<string>/*Ωignore_endΩ*/ = (bar: string) => { return __sveltets_2_any(0)}
19+
var foo/*Ωignore_startΩ*/: import('svelte').Snippet<string>/*Ωignore_endΩ*/ = (bar: string) => { return __sveltets_2_any(0)};
2020

2121
;__sveltets_2_ensureSnippet(foo(bar as string));
2222

packages/svelte2tsx/test/svelte2tsx/samples/component-default-slot/expected-svelte5.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ async () => {
88
{ svelteHTML.createElement("div", {});
99
{ __sveltets_createSlot("default", { "a":b,}); }
1010
}};
11-
let $$implicit_children = {a:b};
11+
let $$implicit_children = __sveltets_2_snippet({a:b});
1212
return { props: {children: $$implicit_children}, slots: {'default': {a:b}}, events: {} }}
1313

1414
export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(['children'], __sveltets_2_with_any_event(render()))) {

packages/svelte2tsx/test/svelte2tsx/samples/component-multiple-slots/expected-svelte5.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async () => {
1212
{ __sveltets_createSlot("test", { "c":d,e,}); }
1313
{ __sveltets_createSlot("abc-cde.113", { }); }
1414
}};
15-
let $$implicit_children = {a:b};
15+
let $$implicit_children = __sveltets_2_snippet({a:b});
1616
return { props: {children: $$implicit_children}, slots: {'default': {a:b}, 'test': {c:d, e:e}, 'abc-cde.113': {}}, events: {} }}
1717

1818
export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(['children'], __sveltets_2_with_any_event(render()))) {

packages/svelte2tsx/test/svelte2tsx/samples/component-slot-$$slot-interface/expected-svelte5.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async () => {
1818
{ __sveltets_createSlot("default", { "a":b,});}
1919
{ __sveltets_createSlot("foo", { b,});}
2020
}};
21-
let $$implicit_children = {a:b};
21+
let $$implicit_children = __sveltets_2_snippet({a:b});
2222
return { props: {children: $$implicit_children}, slots: {} as unknown as $$Slots, events: {} }}
2323

2424
export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(['children'], __sveltets_2_with_any_event(render()))) {

packages/svelte2tsx/test/svelte2tsx/samples/component-slot-$$slot-type/expected-svelte5.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async () => {
1818
{ __sveltets_createSlot("default", { "a":b,});}
1919
{ __sveltets_createSlot("foo", { b,});}
2020
}};
21-
let $$implicit_children = {a:b};
21+
let $$implicit_children = __sveltets_2_snippet({a:b});
2222
return { props: {children: $$implicit_children}, slots: {} as unknown as $$Slots, events: {} }}
2323

2424
export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(['children'], __sveltets_2_with_any_event(render()))) {

0 commit comments

Comments
 (0)