Skip to content

Commit 3f691b6

Browse files
committed
fix: harden split/move logic
#2209
1 parent a9940c1 commit 3f691b6

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

packages/svelte2tsx/src/htmlxtojsx_v2/utils/node-utils.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,14 @@ export function transform(
8282
}
8383

8484
let removeStart = start;
85-
for (const transformation of [...moves].sort((t1, t2) => t1[0] - t2[0])) {
85+
const sortedMoves = [...moves].sort((t1, t2) => t1[0] - t2[0]);
86+
for (const transformation of sortedMoves) {
8687
if (removeStart < transformation[0]) {
87-
if (deletePos !== moves.length && removeStart > deleteDest) {
88+
if (
89+
deletePos !== moves.length &&
90+
removeStart > deleteDest &&
91+
!(removeStart < end && transformation[0] >= end)
92+
) {
8893
str.move(removeStart, transformation[0], end);
8994
}
9095
if (transformation[0] < end) {
@@ -98,7 +103,8 @@ export function transform(
98103
if (removeStart > end) {
99104
// Reset the end to the last transformation before the end if there were transformations after the end
100105
// so we still delete the correct range afterwards
101-
removeStart = moves.find((m) => m[1] < end)?.[1] ?? end;
106+
let idx = sortedMoves.findIndex((m) => m[0] > end) - 1;
107+
removeStart = sortedMoves[idx]?.[1] ?? end;
102108
}
103109

104110
if (removeStart < end) {

packages/svelte2tsx/test/htmlx2jsx/samples/snippet.skip/expectedv2.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ return __sveltets_2_any(0)}
1010
return __sveltets_2_any(0)},}});
1111
{ svelteHTML.createElement("div", {});asd; }
1212

13-
Component}
13+
Component}
14+
15+
{ const $$_tsiL0C = __sveltets_2_ensureComponent(List); new $$_tsiL0C({ target: __sveltets_2_any(), props: { "data":[1, 2, 3],row:(item) => {
16+
item;
17+
return __sveltets_2_any(0)},}});
18+
19+
List}

packages/svelte2tsx/test/htmlx2jsx/samples/snippet.skip/input.svelte

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@
1010
{#snippet bar(x)}
1111
<div>asd{x}</div>
1212
{/snippet}
13-
</Component>
13+
</Component>
14+
15+
<List data={[1, 2, 3]}>
16+
{#snippet row(item)}
17+
{item}
18+
{/snippet}
19+
</List>

0 commit comments

Comments
 (0)