From 74a2c6b5807de1540f48db3875dab36d56ca738e Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Sun, 3 Nov 2024 14:31:54 +0100 Subject: [PATCH] fix: migrate multiple declarations with only some exported correctly (#14126) --- .changeset/yellow-pumas-rule.md | 5 +++ packages/svelte/src/compiler/migrate/index.js | 41 +++++++++++++++---- .../input.svelte | 6 +++ .../output.svelte | 12 ++++++ 4 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 .changeset/yellow-pumas-rule.md create mode 100644 packages/svelte/tests/migrate/samples/export-props-multiple-declarations/input.svelte create mode 100644 packages/svelte/tests/migrate/samples/export-props-multiple-declarations/output.svelte diff --git a/.changeset/yellow-pumas-rule.md b/.changeset/yellow-pumas-rule.md new file mode 100644 index 0000000000..02def3c178 --- /dev/null +++ b/.changeset/yellow-pumas-rule.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: migrate multiple declarations with only some exported correctly diff --git a/packages/svelte/src/compiler/migrate/index.js b/packages/svelte/src/compiler/migrate/index.js index ee5abc8853..011d28d61a 100644 --- a/packages/svelte/src/compiler/migrate/index.js +++ b/packages/svelte/src/compiler/migrate/index.js @@ -505,7 +505,8 @@ const instance_script = { let nr_of_props = 0; - for (const declarator of node.declarations) { + for (let i = 0; i < node.declarations.length; i++) { + const declarator = node.declarations[i]; if (state.analysis.runes) { if (get_rune(declarator.init, state.scope) === '$props') { state.props_insertion_point = /** @type {number} */ (declarator.id.start) + 1; @@ -605,12 +606,38 @@ const instance_script = { }); } - state.props_insertion_point = /** @type {number} */ (declarator.end); - state.str.update( - /** @type {number} */ (declarator.start), - /** @type {number} */ (declarator.end), - '' - ); + let start = /** @type {number} */ (declarator.start); + let end = /** @type {number} */ (declarator.end); + + // handle cases like let a,b,c; where only some are exported + if (node.declarations.length > 1) { + // move the insertion point after the node itself; + state.props_insertion_point = /** @type {number} */ (node.end); + // if it's not the first declaration remove from the , of the previous declaration + if (i !== 0) { + start = state.str.original.indexOf( + ',', + /** @type {number} */ (node.declarations[i - 1].end) + ); + } + // if it's not the last declaration remove either from up until the + // start of the next declaration (if it's the first declaration) or + // up until the last index of , from the next declaration + if (i !== node.declarations.length - 1) { + if (i === 0) { + end = /** @type {number} */ (node.declarations[i + 1].start); + } else { + end = state.str.original.lastIndexOf( + ',', + /** @type {number} */ (node.declarations[i + 1].start) + ); + } + } + } else { + state.props_insertion_point = /** @type {number} */ (declarator.end); + } + + state.str.update(start, end, ''); continue; } diff --git a/packages/svelte/tests/migrate/samples/export-props-multiple-declarations/input.svelte b/packages/svelte/tests/migrate/samples/export-props-multiple-declarations/input.svelte new file mode 100644 index 0000000000..198bd44fc0 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/export-props-multiple-declarations/input.svelte @@ -0,0 +1,6 @@ + \ No newline at end of file diff --git a/packages/svelte/tests/migrate/samples/export-props-multiple-declarations/output.svelte b/packages/svelte/tests/migrate/samples/export-props-multiple-declarations/output.svelte new file mode 100644 index 0000000000..bb16ea2ff9 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/export-props-multiple-declarations/output.svelte @@ -0,0 +1,12 @@ + \ No newline at end of file