From 33ee958087d5110ee4aee4b1f1cb30c7ac30a186 Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Sun, 29 Sep 2024 22:22:21 +0200 Subject: [PATCH] chore: refactor migration composition logic (#13424) the code to compose together the instance was getting a bit messy because we were trying to add stuff all in the same place if there was no script or one by one if there was one. I changed this a bit by using an insertion_point variable that we can set to then insert stuff sequentially. --- packages/svelte/src/compiler/migrate/index.js | 84 ++++++++----------- .../event-handlers-with-alias/output.svelte | 4 +- .../samples/event-handlers/output.svelte | 4 +- 3 files changed, 38 insertions(+), 54 deletions(-) diff --git a/packages/svelte/src/compiler/migrate/index.js b/packages/svelte/src/compiler/migrate/index.js index 9b1dc00e57..05d4c5db2a 100644 --- a/packages/svelte/src/compiler/migrate/index.js +++ b/packages/svelte/src/compiler/migrate/index.js @@ -102,13 +102,40 @@ export function migrate(source) { state = { ...state, scope: analysis.template.scope }; walk(parsed.fragment, state, template); + let insertion_point = parsed.instance + ? /** @type {number} */ (parsed.instance.content.start) + : 0; + + const need_script = + state.legacy_imports.size > 0 || + state.script_insertions.size > 0 || + state.props.length > 0 || + analysis.uses_rest_props || + analysis.uses_props; + + if (!parsed.instance && need_script) { + str.appendRight(0, '\n\n` - ); - added_legacy_import = true; - } + props_declaration = `\n${indent}${props_declaration}`; + str.appendRight(insertion_point, props_declaration); } } @@ -235,24 +250,9 @@ export function migrate(source) { } } - if (state.legacy_imports.size > 0 && !added_legacy_import) { - const script_insertions = - state.script_insertions.size > 0 - ? `\n${indent}${[...state.script_insertions].join(`\n${indent}`)}` - : ''; - - if (parsed.instance) { - str.appendRight( - /** @type {number} */ (parsed.instance.content.start), - `\n${indent}${legacy_import}${script_insertions}\n` - ); - } else { - str.prepend( - `\n\n` - ); - } + if (!parsed.instance && need_script) { + str.appendRight(insertion_point, '\n\n\n'); } - return { code: str.toString() }; } catch (e) { // eslint-disable-next-line no-console @@ -843,22 +843,6 @@ function get_node_range(source, node) { return { start, end }; } -/** - * @param {AST.OnDirective} last - * @param {State} state - */ -function generate_event_name(last, state) { - const scope = - (last.expression && state.analysis.template.scopes.get(last.expression)) || state.scope; - - let name = 'event'; - if (!scope.get(name)) return name; - - let i = 1; - while (scope.get(`${name}${i}`)) i += 1; - return `${name}${i}`; -} - /** * @param {Identifier} node * @param {State} state diff --git a/packages/svelte/tests/migrate/samples/event-handlers-with-alias/output.svelte b/packages/svelte/tests/migrate/samples/event-handlers-with-alias/output.svelte index 38bac2b971..131070c4b0 100644 --- a/packages/svelte/tests/migrate/samples/event-handlers-with-alias/output.svelte +++ b/packages/svelte/tests/migrate/samples/event-handlers-with-alias/output.svelte @@ -1,7 +1,7 @@ @@ -78,4 +78,4 @@ })}>click me - + \ No newline at end of file