Skip to content

Commit a0a31ed

Browse files
authored
fix: use analysis.name when migrating <svelte:self> (#13544)
* fix: use `analysis.name` when migrating `<svelte:self>` * simplify
1 parent 2f6d84a commit a0a31ed

File tree

5 files changed

+40
-35
lines changed

5 files changed

+40
-35
lines changed

.changeset/soft-stingrays-attend.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: use `analysis.name` when migrating `<svelte:self>`

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function migrate(source, { filename } = {}) {
4242
});
4343

4444
reset_warning_filter(() => false);
45-
reset(source, { filename: filename ?? 'migrate.svelte' });
45+
reset(source, { filename: filename ?? '(unknown)' });
4646

4747
let parsed = parse(source);
4848

@@ -52,7 +52,8 @@ export function migrate(source, { filename } = {}) {
5252
const combined_options = {
5353
...validate_component_options({}, ''),
5454
...parsed_options,
55-
customElementOptions
55+
customElementOptions,
56+
filename: filename ?? '(unknown)'
5657
};
5758

5859
const str = new MagicString(source);
@@ -92,8 +93,7 @@ export function migrate(source, { filename } = {}) {
9293
createBubbler: analysis.root.unique('createBubbler').name,
9394
bubble: analysis.root.unique('bubble').name,
9495
passive: analysis.root.unique('passive').name,
95-
nonpassive: analysis.root.unique('nonpassive').name,
96-
svelte_self: analysis.root.unique('SvelteSelf').name
96+
nonpassive: analysis.root.unique('nonpassive').name
9797
},
9898
legacy_imports: new Set(),
9999
script_insertions: new Set(),
@@ -137,7 +137,7 @@ export function migrate(source, { filename } = {}) {
137137
const file = filename.split('/').pop();
138138
str.appendRight(
139139
insertion_point,
140-
`\n${indent}import ${state.names.svelte_self} from './${file}';`
140+
`\n${indent}import ${state.analysis.name} from './${file}';`
141141
);
142142
}
143143

@@ -759,22 +759,22 @@ const template = {
759759
state.str.overwrite(
760760
node.start + 1,
761761
node.start + 1 + 'svelte:self'.length,
762-
`${state.names.svelte_self}`
762+
`${state.analysis.name}`
763763
);
764764
// if it has a fragment we need to overwrite the closing tag too
765765
if (node.fragment.nodes.length > 0) {
766766
state.str.overwrite(
767767
state.str.original.lastIndexOf('<', node.end) + 2,
768768
node.end - 1,
769-
`${state.names.svelte_self}`
769+
`${state.analysis.name}`
770770
);
771771
} else if (!source.endsWith('/>')) {
772772
// special case for case `<svelte:self></svelte:self>` it has no fragment but
773773
// we still need to overwrite the end tag
774774
state.str.overwrite(
775775
node.start + source.lastIndexOf('</', node.end) + 2,
776776
node.end - 1,
777-
`${state.names.svelte_self}`
777+
`${state.analysis.name}`
778778
);
779779
}
780780
state.has_svelte_self = true;

packages/svelte/tests/migrate/samples/svelte-self-name-conflict/input.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
let SvelteSelf;
2+
let Output;
33
</script>
44

55
{#if false}
@@ -16,4 +16,4 @@
1616
child
1717
</svelte:self>
1818
<svelte:self></svelte:self>
19-
{/if}
19+
{/if}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<script>
2-
import SvelteSelf_1 from './output.svelte';
2+
import Output_1 from './output.svelte';
33
/** @type {Record<string, any>} */
44
let { ...props } = $props();
5-
let SvelteSelf;
5+
let Output;
66
</script>
77

88
{#if false}
9-
<SvelteSelf_1 />
10-
<SvelteSelf_1 with_attributes/>
11-
<SvelteSelf_1 count={count+1}/>
12-
<SvelteSelf_1>
9+
<Output_1 />
10+
<Output_1 with_attributes/>
11+
<Output_1 count={count+1}/>
12+
<Output_1>
1313
child
14-
</SvelteSelf_1>
15-
<SvelteSelf_1 count={count+1}>
14+
</Output_1>
15+
<Output_1 count={count+1}>
1616
child
17-
</SvelteSelf_1>
18-
<SvelteSelf_1 count={props.count} >
17+
</Output_1>
18+
<Output_1 count={props.count} >
1919
child
20-
</SvelteSelf_1>
21-
<SvelteSelf_1></SvelteSelf_1>
22-
{/if}
20+
</Output_1>
21+
<Output_1></Output_1>
22+
{/if}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<script>
2-
import SvelteSelf from './output.svelte';
2+
import Output from './output.svelte';
33
/** @type {Record<string, any>} */
44
let { ...props } = $props();
55
</script>
66

77
{#if false}
8-
<SvelteSelf />
9-
<SvelteSelf with_attributes/>
10-
<SvelteSelf count={count+1}/>
11-
<SvelteSelf>
8+
<Output />
9+
<Output with_attributes/>
10+
<Output count={count+1}/>
11+
<Output>
1212
child
13-
</SvelteSelf>
14-
<SvelteSelf count={count+1}>
13+
</Output>
14+
<Output count={count+1}>
1515
child
16-
</SvelteSelf>
17-
<SvelteSelf count={props.count} >
16+
</Output>
17+
<Output count={props.count} >
1818
child
19-
</SvelteSelf>
20-
<SvelteSelf></SvelteSelf>
21-
{/if}
19+
</Output>
20+
<Output></Output>
21+
{/if}

0 commit comments

Comments
 (0)