From 3f691b6c8c922bd933adf09574f5ce64e9274714 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Wed, 22 Nov 2023 11:51:22 +0100 Subject: [PATCH] fix: harden split/move logic #2209 --- .../svelte2tsx/src/htmlxtojsx_v2/utils/node-utils.ts | 12 +++++++++--- .../htmlx2jsx/samples/snippet.skip/expectedv2.js | 8 +++++++- .../test/htmlx2jsx/samples/snippet.skip/input.svelte | 8 +++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/svelte2tsx/src/htmlxtojsx_v2/utils/node-utils.ts b/packages/svelte2tsx/src/htmlxtojsx_v2/utils/node-utils.ts index bef382f9b..57d6d22f7 100644 --- a/packages/svelte2tsx/src/htmlxtojsx_v2/utils/node-utils.ts +++ b/packages/svelte2tsx/src/htmlxtojsx_v2/utils/node-utils.ts @@ -82,9 +82,14 @@ export function transform( } let removeStart = start; - for (const transformation of [...moves].sort((t1, t2) => t1[0] - t2[0])) { + const sortedMoves = [...moves].sort((t1, t2) => t1[0] - t2[0]); + for (const transformation of sortedMoves) { if (removeStart < transformation[0]) { - if (deletePos !== moves.length && removeStart > deleteDest) { + if ( + deletePos !== moves.length && + removeStart > deleteDest && + !(removeStart < end && transformation[0] >= end) + ) { str.move(removeStart, transformation[0], end); } if (transformation[0] < end) { @@ -98,7 +103,8 @@ export function transform( if (removeStart > end) { // Reset the end to the last transformation before the end if there were transformations after the end // so we still delete the correct range afterwards - removeStart = moves.find((m) => m[1] < end)?.[1] ?? end; + let idx = sortedMoves.findIndex((m) => m[0] > end) - 1; + removeStart = sortedMoves[idx]?.[1] ?? end; } if (removeStart < end) { diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/snippet.skip/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/snippet.skip/expectedv2.js index 15cdf4bef..59714bd7f 100644 --- a/packages/svelte2tsx/test/htmlx2jsx/samples/snippet.skip/expectedv2.js +++ b/packages/svelte2tsx/test/htmlx2jsx/samples/snippet.skip/expectedv2.js @@ -10,4 +10,10 @@ return __sveltets_2_any(0)} return __sveltets_2_any(0)},}}); { svelteHTML.createElement("div", {});asd; } - Component} \ No newline at end of file + Component} + + { const $$_tsiL0C = __sveltets_2_ensureComponent(List); new $$_tsiL0C({ target: __sveltets_2_any(), props: { "data":[1, 2, 3],row:(item) => { + item; + return __sveltets_2_any(0)},}}); + + List} \ No newline at end of file diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/snippet.skip/input.svelte b/packages/svelte2tsx/test/htmlx2jsx/samples/snippet.skip/input.svelte index 947e2e65c..c8c6a8716 100644 --- a/packages/svelte2tsx/test/htmlx2jsx/samples/snippet.skip/input.svelte +++ b/packages/svelte2tsx/test/htmlx2jsx/samples/snippet.skip/input.svelte @@ -10,4 +10,10 @@ {#snippet bar(x)}
asd{x}
{/snippet} - \ No newline at end of file + + + + {#snippet row(item)} + {item} + {/snippet} +