diff --git a/.changeset/real-timers-complain.md b/.changeset/real-timers-complain.md new file mode 100644 index 0000000000..cad0860b05 --- /dev/null +++ b/.changeset/real-timers-complain.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: various `svelte:component` migration bugs diff --git a/packages/svelte/src/compiler/migrate/index.js b/packages/svelte/src/compiler/migrate/index.js index 95724ad807..109059f583 100644 --- a/packages/svelte/src/compiler/migrate/index.js +++ b/packages/svelte/src/compiler/migrate/index.js @@ -639,21 +639,30 @@ const template = { part.type === 'EachBlock' || part.type === 'AwaitBlock' || part.type === 'IfBlock' || - part.type === 'KeyBlock' || part.type === 'SnippetBlock' || part.type === 'Component' || part.type === 'SvelteComponent' ) { + let position = node.start; + if (i !== path.length - 1) { + for (let modifier = 1; modifier < path.length - i; modifier++) { + const path_part = path[i + modifier]; + if ('start' in path_part) { + position = /** @type {number} */ (path_part.start); + break; + } + } + } const indent = state.str.original.substring( - state.str.original.lastIndexOf('\n', node.start) + 1, - node.start + state.str.original.lastIndexOf('\n', position) + 1, + position ); state.str.prependLeft( - node.start, + position, `{@const ${expression} = ${current_expression}}\n${indent}` ); needs_derived = false; - continue; + break; } } if (needs_derived) { diff --git a/packages/svelte/tests/migrate/samples/svelte-component/input.svelte b/packages/svelte/tests/migrate/samples/svelte-component/input.svelte index 8d3cbea391..ab4e470d5b 100644 --- a/packages/svelte/tests/migrate/samples/svelte-component/input.svelte +++ b/packages/svelte/tests/migrate/samples/svelte-component/input.svelte @@ -122,4 +122,78 @@ {:catch Error} -{/await} \ No newline at end of file +{/await} + + + {@const stuff = true} +
+

+ +

+
+
+ + + {@const stuff = true} +
+

+ +

+
+
+ +{#each [] as i} + {@const stuff = true} +
  • + +
  • +{/each} + +{#await stuff} + {@const stuff = true} +
  • + +
  • +{:then x} + {@const stuff = true} +
  • + +
  • +{:catch e} + {@const stuff = true} +
  • + +
  • +{/await} + +{#await stuff then x} + {@const stuff = true} +
  • + +
  • +{:catch e} + {@const stuff = true} +
  • + +
  • +{/await} + +{#if true} + {@const stuff = true} +
  • + +
  • +{/if} + +{#snippet test()} + {@const stuff = true} +
  • + +
  • +{/snippet} + + + + + + \ No newline at end of file diff --git a/packages/svelte/tests/migrate/samples/svelte-component/output.svelte b/packages/svelte/tests/migrate/samples/svelte-component/output.svelte index de39207d3a..0fc5fff555 100644 --- a/packages/svelte/tests/migrate/samples/svelte-component/output.svelte +++ b/packages/svelte/tests/migrate/samples/svelte-component/output.svelte @@ -22,22 +22,22 @@ + {@const SvelteComponent_2 = stuff}
    - {@const SvelteComponent_2 = stuff}
    + {@const SvelteComponent_3 = stuff} - {@const SvelteComponent_3 = stuff} + {@const SvelteComponent_4 = stuff} - {@const SvelteComponent_4 = stuff} @@ -57,22 +57,22 @@ + {@const SvelteComponent_7 = stuff}
    - {@const SvelteComponent_7 = stuff}
    + {@const SvelteComponent_8 = stuff} - {@const SvelteComponent_8 = stuff} + {@const SvelteComponent_9 = stuff} - {@const SvelteComponent_9 = stuff} @@ -140,4 +140,89 @@ {:catch Error} -{/await} \ No newline at end of file +{/await} + + + {@const stuff = true} + {@const SvelteComponent_18 = stuff && Component} +
    +

    + +

    +
    +
    + + + {@const stuff = true} + {@const SvelteComponent_19 = stuff && Component} +
    +

    + +

    +
    +
    + +{#each [] as i} + {@const stuff = true} + {@const SvelteComponent_20 = stuff && Component} +
  • + +
  • +{/each} + +{#await stuff} + {@const stuff = true} + {@const SvelteComponent_21 = stuff && Component} +
  • + +
  • +{:then x} + {@const stuff = true} + {@const SvelteComponent_22 = stuff && Component} +
  • + +
  • +{:catch e} + {@const stuff = true} + {@const SvelteComponent_23 = stuff && Component} +
  • + +
  • +{/await} + +{#await stuff then x} + {@const stuff = true} + {@const SvelteComponent_24 = stuff && Component} +
  • + +
  • +{:catch e} + {@const stuff = true} + {@const SvelteComponent_25 = stuff && Component} +
  • + +
  • +{/await} + +{#if true} + {@const stuff = true} + {@const SvelteComponent_26 = stuff && Component} +
  • + +
  • +{/if} + +{#snippet test()} + {@const stuff = true} + {@const SvelteComponent_27 = stuff && Component} +
  • + +
  • +{/snippet} + + + + {@const SvelteComponent_28 = stuff && Component} + + + \ No newline at end of file