From 44ed5ef818fdaaffbd97484bd7a5edbf083a333e Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Wed, 20 Nov 2024 12:02:32 +0100 Subject: [PATCH] fix: don't move appended content from previous node while hoisting interface #2592 --- .../src/svelte2tsx/nodes/HoistableInterfaces.ts | 17 +++++++++++++++-- .../ts-runes-hoistable-props-1.v5/expectedv2.ts | 3 +++ .../ts-runes-hoistable-props-2.v5/expectedv2.ts | 1 + .../ts-runes-hoistable-props-4.v5/expectedv2.ts | 2 ++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/svelte2tsx/src/svelte2tsx/nodes/HoistableInterfaces.ts b/packages/svelte2tsx/src/svelte2tsx/nodes/HoistableInterfaces.ts index 0ad8d535b..d64776d27 100644 --- a/packages/svelte2tsx/src/svelte2tsx/nodes/HoistableInterfaces.ts +++ b/packages/svelte2tsx/src/svelte2tsx/nodes/HoistableInterfaces.ts @@ -333,8 +333,21 @@ export class HoistableInterfaces { const hoistable = this.determineHoistableInterfaces(); if (hoistable.has(this.props_interface.name)) { - for (const [, node] of hoistable) { - str.move(node.pos + astOffset, node.end + astOffset, scriptStart); + for (const [name, node] of hoistable) { + let pos = node.pos + astOffset; + + // node.pos includes preceeding whitespace, which could mean we accidentally also move stuff appended to a previous node + if (name !== '$$ComponentProps') { + if (str.original[pos] === '\r') { + pos++; + } + if (/\s/.test(str.original[pos])) { + pos++; + str.prependRight(pos, '\n'); + } + } + + str.move(pos, node.end + astOffset, scriptStart); } } } diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-1.v5/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-1.v5/expectedv2.ts index c14fcdf06..36c5a9cc3 100644 --- a/packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-1.v5/expectedv2.ts +++ b/packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-1.v5/expectedv2.ts @@ -16,6 +16,9 @@ };function render() { + + + let { a, b }: Props = $props(); ; async () => { diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-2.v5/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-2.v5/expectedv2.ts index 664f10006..145efb553 100644 --- a/packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-2.v5/expectedv2.ts +++ b/packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-2.v5/expectedv2.ts @@ -8,6 +8,7 @@ };type $$ComponentProps = { a: Dependency, b: string };;function render() { + let { a, b }:/*Ωignore_startΩ*/$$ComponentProps/*Ωignore_endΩ*/ = $props(); ; async () => { diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-4.v5/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-4.v5/expectedv2.ts index 1fedd1021..f16fc5f24 100644 --- a/packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-4.v5/expectedv2.ts +++ b/packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-4.v5/expectedv2.ts @@ -9,6 +9,8 @@ };function render() { + + let { foo }: Props = $props(); ; async () => {};