From a0a31ed0d00eac7ff014eb4b254149b65b0f6a0b Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 9 Oct 2024 12:25:26 -0400 Subject: [PATCH] fix: use `analysis.name` when migrating `` (#13544) * fix: use `analysis.name` when migrating `` * simplify --- .changeset/soft-stingrays-attend.md | 5 ++++ packages/svelte/src/compiler/migrate/index.js | 16 ++++++------ .../svelte-self-name-conflict/input.svelte | 4 +-- .../svelte-self-name-conflict/output.svelte | 26 +++++++++---------- .../migrate/samples/svelte-self/output.svelte | 24 ++++++++--------- 5 files changed, 40 insertions(+), 35 deletions(-) create mode 100644 .changeset/soft-stingrays-attend.md diff --git a/.changeset/soft-stingrays-attend.md b/.changeset/soft-stingrays-attend.md new file mode 100644 index 0000000000..d6bb949d81 --- /dev/null +++ b/.changeset/soft-stingrays-attend.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: use `analysis.name` when migrating `` diff --git a/packages/svelte/src/compiler/migrate/index.js b/packages/svelte/src/compiler/migrate/index.js index e0d8cd55df..5c2c1fb484 100644 --- a/packages/svelte/src/compiler/migrate/index.js +++ b/packages/svelte/src/compiler/migrate/index.js @@ -42,7 +42,7 @@ export function migrate(source, { filename } = {}) { }); reset_warning_filter(() => false); - reset(source, { filename: filename ?? 'migrate.svelte' }); + reset(source, { filename: filename ?? '(unknown)' }); let parsed = parse(source); @@ -52,7 +52,8 @@ export function migrate(source, { filename } = {}) { const combined_options = { ...validate_component_options({}, ''), ...parsed_options, - customElementOptions + customElementOptions, + filename: filename ?? '(unknown)' }; const str = new MagicString(source); @@ -92,8 +93,7 @@ export function migrate(source, { filename } = {}) { createBubbler: analysis.root.unique('createBubbler').name, bubble: analysis.root.unique('bubble').name, passive: analysis.root.unique('passive').name, - nonpassive: analysis.root.unique('nonpassive').name, - svelte_self: analysis.root.unique('SvelteSelf').name + nonpassive: analysis.root.unique('nonpassive').name }, legacy_imports: new Set(), script_insertions: new Set(), @@ -137,7 +137,7 @@ export function migrate(source, { filename } = {}) { const file = filename.split('/').pop(); str.appendRight( 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( node.start + 1, 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 (node.fragment.nodes.length > 0) { state.str.overwrite( state.str.original.lastIndexOf('<', node.end) + 2, node.end - 1, - `${state.names.svelte_self}` + `${state.analysis.name}` ); } else if (!source.endsWith('/>')) { // special case for case `` it has no fragment but @@ -774,7 +774,7 @@ const template = { state.str.overwrite( node.start + source.lastIndexOf(' - let SvelteSelf; + let Output; {#if false} @@ -16,4 +16,4 @@ child -{/if} \ No newline at end of file +{/if} diff --git a/packages/svelte/tests/migrate/samples/svelte-self-name-conflict/output.svelte b/packages/svelte/tests/migrate/samples/svelte-self-name-conflict/output.svelte index 827d06c3a3..50f7e42112 100644 --- a/packages/svelte/tests/migrate/samples/svelte-self-name-conflict/output.svelte +++ b/packages/svelte/tests/migrate/samples/svelte-self-name-conflict/output.svelte @@ -1,22 +1,22 @@ {#if false} - - - - + + + + child - - + + child - - + + child - - -{/if} \ No newline at end of file + + +{/if} diff --git a/packages/svelte/tests/migrate/samples/svelte-self/output.svelte b/packages/svelte/tests/migrate/samples/svelte-self/output.svelte index 7e7ebae215..8a01edce69 100644 --- a/packages/svelte/tests/migrate/samples/svelte-self/output.svelte +++ b/packages/svelte/tests/migrate/samples/svelte-self/output.svelte @@ -1,21 +1,21 @@ {#if false} - - - - + + + + child - - + + child - - + + child - - -{/if} \ No newline at end of file + + +{/if}