Skip to content

fix: key block can have its own block scope #2768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { BaseNode } from '../../interfaces';
import { isImplicitlyClosedBlock, withTrailingPropertyAccess } from '../utils/node-utils';

/**
* {#key expr}content{/key} ---> expr; content
* {#key expr}content{/key} ---> expr; {content}
*/
export function handleKey(str: MagicString, keyBlock: BaseNode): void {
// {#key expr} -> expr;
// {#key expr} -> expr;{
str.overwrite(keyBlock.start, keyBlock.expression.start, '', { contentOnly: true });
const expressionEnd = withTrailingPropertyAccess(str.original, keyBlock.expression.end);
const end = str.original.indexOf('}', expressionEnd);
str.overwrite(expressionEnd, end + 1, '; ');
str.overwrite(expressionEnd, end + 1, '; {');

// {/key} ->
// {/key} -> }
const endKey = str.original.lastIndexOf('{', keyBlock.end - 1);
if (!isImplicitlyClosedBlock(endKey, keyBlock)) {
str.overwrite(endKey, keyBlock.end, '', { contentOnly: true });
str.overwrite(endKey, keyBlock.end, '}', { contentOnly: true });
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
''.;
''.; {

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
value;
value; {
{ svelteHTML.createElement("p", {}); }

$store;
}
$store; {
{ svelteHTML.createElement("p", {}); }

expr.obj;
{ svelteHTML.createElement("p", {}); }
}
expr.obj; {
{ svelteHTML.createElement("p", {}); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ if(true){

}

key;
key; {
const foo/*Ωignore_positionΩ*/ = ()/*Ωignore_startΩ*/: ReturnType<import('svelte').Snippet>/*Ωignore_endΩ*/ => { async ()/*Ωignore_positionΩ*/ => {};return __sveltets_2_any(0)};;__sveltets_2_ensureSnippet(foo());


}

const snippetBlock/*Ωignore_positionΩ*/ = ()/*Ωignore_startΩ*/: ReturnType<import('svelte').Snippet>/*Ωignore_endΩ*/ => { async ()/*Ωignore_positionΩ*/ => {
const foo/*Ωignore_positionΩ*/ = ()/*Ωignore_startΩ*/: ReturnType<import('svelte').Snippet>/*Ωignore_endΩ*/ => { async ()/*Ωignore_positionΩ*/ => {};return __sveltets_2_any(0)}; const foo2/*Ωignore_positionΩ*/ = ()/*Ωignore_startΩ*/: ReturnType<import('svelte').Snippet>/*Ωignore_endΩ*/ => { async ()/*Ωignore_positionΩ*/ => {};return __sveltets_2_any(0)};;__sveltets_2_ensureSnippet(foo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ try { const $$_value = await (foo as Promise<void>);{ const result: any = $$_val

}}

item as string;
item as string; { }

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

Expand Down