fix: use `analysis.name` when migrating `<svelte:self>` (#13544)

* fix: use `analysis.name` when migrating `<svelte:self>`

* simplify
pull/13444/merge
Rich Harris 3 months ago committed by GitHub
parent 2f6d84ab6b
commit a0a31ed0d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: use `analysis.name` when migrating `<svelte:self>`

@ -42,7 +42,7 @@ export function migrate(source, { filename } = {}) {
}); });
reset_warning_filter(() => false); reset_warning_filter(() => false);
reset(source, { filename: filename ?? 'migrate.svelte' }); reset(source, { filename: filename ?? '(unknown)' });
let parsed = parse(source); let parsed = parse(source);
@ -52,7 +52,8 @@ export function migrate(source, { filename } = {}) {
const combined_options = { const combined_options = {
...validate_component_options({}, ''), ...validate_component_options({}, ''),
...parsed_options, ...parsed_options,
customElementOptions customElementOptions,
filename: filename ?? '(unknown)'
}; };
const str = new MagicString(source); const str = new MagicString(source);
@ -92,8 +93,7 @@ export function migrate(source, { filename } = {}) {
createBubbler: analysis.root.unique('createBubbler').name, createBubbler: analysis.root.unique('createBubbler').name,
bubble: analysis.root.unique('bubble').name, bubble: analysis.root.unique('bubble').name,
passive: analysis.root.unique('passive').name, passive: analysis.root.unique('passive').name,
nonpassive: analysis.root.unique('nonpassive').name, nonpassive: analysis.root.unique('nonpassive').name
svelte_self: analysis.root.unique('SvelteSelf').name
}, },
legacy_imports: new Set(), legacy_imports: new Set(),
script_insertions: new Set(), script_insertions: new Set(),
@ -137,7 +137,7 @@ export function migrate(source, { filename } = {}) {
const file = filename.split('/').pop(); const file = filename.split('/').pop();
str.appendRight( str.appendRight(
insertion_point, insertion_point,
`\n${indent}import ${state.names.svelte_self} from './${file}';` `\n${indent}import ${state.analysis.name} from './${file}';`
); );
} }
@ -759,14 +759,14 @@ const template = {
state.str.overwrite( state.str.overwrite(
node.start + 1, node.start + 1,
node.start + 1 + 'svelte:self'.length, node.start + 1 + 'svelte:self'.length,
`${state.names.svelte_self}` `${state.analysis.name}`
); );
// if it has a fragment we need to overwrite the closing tag too // if it has a fragment we need to overwrite the closing tag too
if (node.fragment.nodes.length > 0) { if (node.fragment.nodes.length > 0) {
state.str.overwrite( state.str.overwrite(
state.str.original.lastIndexOf('<', node.end) + 2, state.str.original.lastIndexOf('<', node.end) + 2,
node.end - 1, node.end - 1,
`${state.names.svelte_self}` `${state.analysis.name}`
); );
} else if (!source.endsWith('/>')) { } else if (!source.endsWith('/>')) {
// special case for case `<svelte:self></svelte:self>` it has no fragment but // special case for case `<svelte:self></svelte:self>` it has no fragment but
@ -774,7 +774,7 @@ const template = {
state.str.overwrite( state.str.overwrite(
node.start + source.lastIndexOf('</', node.end) + 2, node.start + source.lastIndexOf('</', node.end) + 2,
node.end - 1, node.end - 1,
`${state.names.svelte_self}` `${state.analysis.name}`
); );
} }
state.has_svelte_self = true; state.has_svelte_self = true;

@ -1,5 +1,5 @@
<script> <script>
let SvelteSelf; let Output;
</script> </script>
{#if false} {#if false}
@ -16,4 +16,4 @@
child child
</svelte:self> </svelte:self>
<svelte:self></svelte:self> <svelte:self></svelte:self>
{/if} {/if}

@ -1,22 +1,22 @@
<script> <script>
import SvelteSelf_1 from './output.svelte'; import Output_1 from './output.svelte';
/** @type {Record<string, any>} */ /** @type {Record<string, any>} */
let { ...props } = $props(); let { ...props } = $props();
let SvelteSelf; let Output;
</script> </script>
{#if false} {#if false}
<SvelteSelf_1 /> <Output_1 />
<SvelteSelf_1 with_attributes/> <Output_1 with_attributes/>
<SvelteSelf_1 count={count+1}/> <Output_1 count={count+1}/>
<SvelteSelf_1> <Output_1>
child child
</SvelteSelf_1> </Output_1>
<SvelteSelf_1 count={count+1}> <Output_1 count={count+1}>
child child
</SvelteSelf_1> </Output_1>
<SvelteSelf_1 count={props.count} > <Output_1 count={props.count} >
child child
</SvelteSelf_1> </Output_1>
<SvelteSelf_1></SvelteSelf_1> <Output_1></Output_1>
{/if} {/if}

@ -1,21 +1,21 @@
<script> <script>
import SvelteSelf from './output.svelte'; import Output from './output.svelte';
/** @type {Record<string, any>} */ /** @type {Record<string, any>} */
let { ...props } = $props(); let { ...props } = $props();
</script> </script>
{#if false} {#if false}
<SvelteSelf /> <Output />
<SvelteSelf with_attributes/> <Output with_attributes/>
<SvelteSelf count={count+1}/> <Output count={count+1}/>
<SvelteSelf> <Output>
child child
</SvelteSelf> </Output>
<SvelteSelf count={count+1}> <Output count={count+1}>
child child
</SvelteSelf> </Output>
<SvelteSelf count={props.count} > <Output count={props.count} >
child child
</SvelteSelf> </Output>
<SvelteSelf></SvelteSelf> <Output></Output>
{/if} {/if}

Loading…
Cancel
Save