Skip to content

Commit 74a2c6b

Browse files
fix: migrate multiple declarations with only some exported correctly (#14126)
1 parent a952860 commit 74a2c6b

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

.changeset/yellow-pumas-rule.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: migrate multiple declarations with only some exported correctly

packages/svelte/src/compiler/migrate/index.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,8 @@ const instance_script = {
505505

506506
let nr_of_props = 0;
507507

508-
for (const declarator of node.declarations) {
508+
for (let i = 0; i < node.declarations.length; i++) {
509+
const declarator = node.declarations[i];
509510
if (state.analysis.runes) {
510511
if (get_rune(declarator.init, state.scope) === '$props') {
511512
state.props_insertion_point = /** @type {number} */ (declarator.id.start) + 1;
@@ -605,12 +606,38 @@ const instance_script = {
605606
});
606607
}
607608

608-
state.props_insertion_point = /** @type {number} */ (declarator.end);
609-
state.str.update(
610-
/** @type {number} */ (declarator.start),
611-
/** @type {number} */ (declarator.end),
612-
''
613-
);
609+
let start = /** @type {number} */ (declarator.start);
610+
let end = /** @type {number} */ (declarator.end);
611+
612+
// handle cases like let a,b,c; where only some are exported
613+
if (node.declarations.length > 1) {
614+
// move the insertion point after the node itself;
615+
state.props_insertion_point = /** @type {number} */ (node.end);
616+
// if it's not the first declaration remove from the , of the previous declaration
617+
if (i !== 0) {
618+
start = state.str.original.indexOf(
619+
',',
620+
/** @type {number} */ (node.declarations[i - 1].end)
621+
);
622+
}
623+
// if it's not the last declaration remove either from up until the
624+
// start of the next declaration (if it's the first declaration) or
625+
// up until the last index of , from the next declaration
626+
if (i !== node.declarations.length - 1) {
627+
if (i === 0) {
628+
end = /** @type {number} */ (node.declarations[i + 1].start);
629+
} else {
630+
end = state.str.original.lastIndexOf(
631+
',',
632+
/** @type {number} */ (node.declarations[i + 1].start)
633+
);
634+
}
635+
}
636+
} else {
637+
state.props_insertion_point = /** @type {number} */ (declarator.end);
638+
}
639+
640+
state.str.update(start, end, '');
614641

615642
continue;
616643
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
let a, b, c, d;
3+
let e, f, g, h;
4+
5+
export {a, c , f, h}
6+
</script>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
let b, d;
3+
let e, g;
4+
let {
5+
a,
6+
c,
7+
f,
8+
h
9+
} = $props();
10+
11+
12+
</script>

0 commit comments

Comments
 (0)