Skip to content

Commit 2cf037e

Browse files
authored
fix: adjust snippet code generation for new AST shape (#2282)
Snippets can now take one or more parameters, and their AST shape has changed as a consequence. Adjust it accordingly. #2281
1 parent 38f448d commit 2cf037e

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export function handleEach(str: MagicString, eachBlock: BaseNode): void {
2727
const containsComma = str.original
2828
.substring(eachBlock.expression.start, eachBlock.expression.end)
2929
.includes(',');
30-
const expressionEnd = getEnd(eachBlock.expression, str);
31-
const contextEnd = getEnd(eachBlock.context, str);
30+
const expressionEnd = getEnd(eachBlock.expression);
31+
const contextEnd = getEnd(eachBlock.context);
3232
const arrayAndItemVarTheSame =
3333
str.original.substring(eachBlock.expression.start, expressionEnd) ===
3434
str.original.substring(eachBlock.context.start, contextEnd);
@@ -78,8 +78,6 @@ export function handleEach(str: MagicString, eachBlock: BaseNode): void {
7878
/**
7979
* Get the end of the node, excluding the type annotation
8080
*/
81-
function getEnd(node: any, str: MagicString) {
82-
return node.typeAnnotation
83-
? str.original.lastIndexOf(':', node.typeAnnotation.start)
84-
: node.end;
81+
function getEnd(node: any) {
82+
return node.typeAnnotation?.start ?? node.end;
8583
}

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,26 @@ export function handleSnippet(
4343
);
4444

4545
const startEnd =
46-
str.original.indexOf('}', snippetBlock.context?.end || snippetBlock.expression.end) + 1;
46+
str.original.indexOf(
47+
'}',
48+
// context was the first iteration in a .next release, remove at some point
49+
snippetBlock.context?.end ||
50+
snippetBlock.parameters?.at(-1)?.end ||
51+
snippetBlock.expression.end
52+
) + 1;
4753

4854
if (isImplicitProp) {
4955
str.overwrite(snippetBlock.start, snippetBlock.expression.start, '', { contentOnly: true });
5056
const transforms: TransformationArray = ['('];
51-
if (snippetBlock.context) {
52-
transforms.push([snippetBlock.context.start, snippetBlock.context.end]);
53-
str.overwrite(snippetBlock.expression.end, snippetBlock.context.start, '', {
57+
if (snippetBlock.context || snippetBlock.parameters?.length) {
58+
// context was the first iteration in a .next release, remove at some point
59+
const start = snippetBlock.context?.start || snippetBlock.parameters?.[0].start;
60+
const end = snippetBlock.context?.end || snippetBlock.parameters.at(-1).end;
61+
transforms.push([start, end]);
62+
str.overwrite(snippetBlock.expression.end, start, '', {
5463
contentOnly: true
5564
});
56-
str.overwrite(snippetBlock.context.end, startEnd, '', { contentOnly: true });
65+
str.overwrite(end, startEnd, '', { contentOnly: true });
5766
} else {
5867
str.overwrite(snippetBlock.expression.end, startEnd, '', { contentOnly: true });
5968
}
@@ -64,15 +73,27 @@ export function handleSnippet(
6473
transforms
6574
);
6675
} else {
67-
const generic = snippetBlock.context
68-
? snippetBlock.context.typeAnnotation
76+
let generic = '';
77+
// context was the first iteration in a .next release, remove at some point
78+
if (snippetBlock.context) {
79+
generic = snippetBlock.context.typeAnnotation
6980
? `<${str.original.slice(
70-
snippetBlock.context.typeAnnotation.start,
81+
snippetBlock.context.typeAnnotation.start + 1,
7182
snippetBlock.context.typeAnnotation.end
7283
)}>`
7384
: // slap any on to it to silence "implicit any" errors; JSDoc people can't add types to snippets
74-
'<any>'
75-
: '';
85+
'<any>';
86+
} else if (snippetBlock.parameters?.length) {
87+
generic = `<[${snippetBlock.parameters
88+
.map((p) =>
89+
p.typeAnnotation
90+
? str.original.slice(p.typeAnnotation.start + 1, p.typeAnnotation.end)
91+
: // slap any on to it to silence "implicit any" errors; JSDoc people can't add types to snippets
92+
'any'
93+
)
94+
.join(', ')}]>`;
95+
}
96+
7697
const typeAnnotation = surroundWithIgnoreComments(`: import('svelte').Snippet${generic}`);
7798
const transforms: TransformationArray = [
7899
'var ',

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

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

0 commit comments

Comments
 (0)