From 0dcd4f6c6e8db809e5fd79e65ade10617568aa2f Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Fri, 18 Oct 2024 02:59:57 +0200 Subject: [PATCH] feat: add `migration-task` comment after errors (#13659) --- .changeset/slimy-garlics-beg.md | 5 +++ packages/svelte/src/compiler/migrate/index.js | 41 ++++++++++++++++++- .../input.svelte | 11 +++++ .../output.svelte | 12 ++++++ .../input.svelte | 5 +++ .../output.svelte | 6 +++ .../input.svelte | 5 +++ .../output.svelte | 6 +++ .../input.svelte | 11 +++++ .../output.svelte | 11 +++++ .../input.svelte | 7 ++++ .../output.svelte | 7 ++++ 12 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 .changeset/slimy-garlics-beg.md create mode 100644 packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/input.svelte create mode 100644 packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/output.svelte create mode 100644 packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/input.svelte create mode 100644 packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/output.svelte create mode 100644 packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/input.svelte create mode 100644 packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/output.svelte create mode 100644 packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate-extra-imports/input.svelte create mode 100644 packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate-extra-imports/output.svelte create mode 100644 packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate/input.svelte create mode 100644 packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate/output.svelte diff --git a/.changeset/slimy-garlics-beg.md b/.changeset/slimy-garlics-beg.md new file mode 100644 index 0000000000..3add07c483 --- /dev/null +++ b/.changeset/slimy-garlics-beg.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +feat: add `migration-task` comment after errors diff --git a/packages/svelte/src/compiler/migrate/index.js b/packages/svelte/src/compiler/migrate/index.js index 66e5f2918a..2ca590ac69 100644 --- a/packages/svelte/src/compiler/migrate/index.js +++ b/packages/svelte/src/compiler/migrate/index.js @@ -301,8 +301,11 @@ export function migrate(source, { filename } = {}) { return { code: str.toString() }; } catch (e) { // eslint-disable-next-line no-console - console.error('Error while migrating Svelte code'); - throw e; + console.error('Error while migrating Svelte code', e); + + return { + code: `\n${source}` + }; } } @@ -348,6 +351,40 @@ const instance_script = { }, ImportDeclaration(node, { state }) { state.props_insertion_point = node.end ?? state.props_insertion_point; + if (node.source.value === 'svelte') { + let illegal_specifiers = []; + let removed_specifiers = 0; + for (let specifier of node.specifiers) { + if ( + specifier.type === 'ImportSpecifier' && + ['beforeUpdate', 'afterUpdate'].includes(specifier.imported.name) + ) { + const references = state.scope.references.get(specifier.local.name); + if (!references) { + let end = /** @type {number} */ ( + state.str.original.indexOf(',', specifier.end) !== -1 && + state.str.original.indexOf(',', specifier.end) < + state.str.original.indexOf('}', specifier.end) + ? state.str.original.indexOf(',', specifier.end) + 1 + : specifier.end + ); + while (state.str.original[end].trim() === '') end++; + state.str.remove(/** @type {number} */ (specifier.start), end); + removed_specifiers++; + continue; + } + illegal_specifiers.push(specifier.imported.name); + } + } + if (removed_specifiers === node.specifiers.length) { + state.str.remove(/** @type {number} */ (node.start), /** @type {number} */ (node.end)); + } + if (illegal_specifiers.length > 0) { + throw new Error( + `Can't migrate code with ${illegal_specifiers.join(' and ')}. Please migrate by hand.` + ); + } + } }, ExportNamedDeclaration(node, { state, next }) { if (node.declaration) { diff --git a/packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/input.svelte b/packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/input.svelte new file mode 100644 index 0000000000..9ee8b30c32 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/input.svelte @@ -0,0 +1,11 @@ + diff --git a/packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/output.svelte b/packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/output.svelte new file mode 100644 index 0000000000..b12d036c09 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/output.svelte @@ -0,0 +1,12 @@ + + \ No newline at end of file diff --git a/packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/input.svelte b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/input.svelte new file mode 100644 index 0000000000..c707131841 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/input.svelte @@ -0,0 +1,5 @@ + + +{$$props} \ No newline at end of file diff --git a/packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/output.svelte b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/output.svelte new file mode 100644 index 0000000000..35707ad4a1 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/output.svelte @@ -0,0 +1,6 @@ + + + +{$$props} \ No newline at end of file diff --git a/packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/input.svelte b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/input.svelte new file mode 100644 index 0000000000..f6c9903ae9 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/input.svelte @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/output.svelte b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/output.svelte new file mode 100644 index 0000000000..b267be270d --- /dev/null +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/output.svelte @@ -0,0 +1,6 @@ + + \ No newline at end of file diff --git a/packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate-extra-imports/input.svelte b/packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate-extra-imports/input.svelte new file mode 100644 index 0000000000..e1e8ebf06e --- /dev/null +++ b/packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate-extra-imports/input.svelte @@ -0,0 +1,11 @@ + + + diff --git a/packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate-extra-imports/output.svelte b/packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate-extra-imports/output.svelte new file mode 100644 index 0000000000..a5e4c15662 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate-extra-imports/output.svelte @@ -0,0 +1,11 @@ + + + \ No newline at end of file diff --git a/packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate/input.svelte b/packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate/input.svelte new file mode 100644 index 0000000000..76952f5961 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate/input.svelte @@ -0,0 +1,7 @@ + + + diff --git a/packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate/output.svelte b/packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate/output.svelte new file mode 100644 index 0000000000..e34158fe6f --- /dev/null +++ b/packages/svelte/tests/migrate/samples/unused-beforeUpdate-afterUpdate/output.svelte @@ -0,0 +1,7 @@ + + + \ No newline at end of file