From b2071e3817625e58c3a7fe831f0e48910e98eca5 Mon Sep 17 00:00:00 2001 From: paoloricciuti Date: Fri, 4 Oct 2024 22:43:26 +0200 Subject: [PATCH] fix: special case `` --- packages/svelte/src/compiler/migrate/index.js | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/svelte/src/compiler/migrate/index.js b/packages/svelte/src/compiler/migrate/index.js index b0b1853efb..9e2ee0d6a7 100644 --- a/packages/svelte/src/compiler/migrate/index.js +++ b/packages/svelte/src/compiler/migrate/index.js @@ -740,27 +740,45 @@ const template = { next(); }, SvelteSelf(node, { state, next }) { + const source = state.str.original.substring(node.start, node.end); if (!state.filename) { - const indent = guess_indent(state.str.original.substring(node.start, node.end)); + const indent = guess_indent(source); state.str.prependRight( node.start, `\n${indent}` ); return; } + // by default we overwrite until the end of the node - 1 let end = node.end - 1; + // if it has some children we need to overwrite from start to start of the fragment if (node.fragment.nodes.length > 0) { end = node.fragment.nodes[0].start; + } else if (!source.endsWith('/>')) { + // special case `` it has no fragment but + // we still can't remove to the node end + end = node.start + source.lastIndexOf('><', node.end) + 1; } + // if at has some attributes we stop at the start of the first if (node.attributes.length > 0) { end = node.attributes[0].start; } + // overwrite the open tag state.str.overwrite(node.start + 1, end - 1, `${state.names.svelte_self}`); + // if it has a fragment we need to overwrite the closing tag too if (node.fragment.nodes.length > 0) { const start_closing = state.str.original.indexOf('<', node.fragment.nodes[node.fragment.nodes.length - 1].end) + 2; state.str.overwrite(start_closing, node.end - 1, `${state.names.svelte_self}`); + } else if (!source.endsWith('/>')) { + // special case for case `` it has no fragment but + // we still need to overwrite the end tag + state.str.overwrite( + node.start + source.lastIndexOf('