From d1cac703c05368b9a82f2a20855d01654c25a960 Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Mon, 21 Oct 2024 21:06:57 +0200 Subject: [PATCH] fix: don't print errors on migration errors (#13754) makes it look like the migration script failed when in reality you need to update some of your files by hand --- .changeset/large-carrots-behave.md | 5 +++ packages/svelte/src/compiler/migrate/index.js | 25 ++++++++++----- .../_config.js | 3 +- .../_config.js | 3 +- .../_config.js | 3 +- .../_config.js | 3 +- .../_config.js | 3 +- .../impossible-migrate-with-errors/_config.js | 31 +++++++++++++++++++ .../input.svelte | 3 ++ .../output.svelte | 4 +++ .../svelte-self-skip-filename/_config.js | 3 +- packages/svelte/tests/migrate/test.ts | 14 ++++++++- 12 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 .changeset/large-carrots-behave.md create mode 100644 packages/svelte/tests/migrate/samples/impossible-migrate-with-errors/_config.js create mode 100644 packages/svelte/tests/migrate/samples/impossible-migrate-with-errors/input.svelte create mode 100644 packages/svelte/tests/migrate/samples/impossible-migrate-with-errors/output.svelte diff --git a/.changeset/large-carrots-behave.md b/.changeset/large-carrots-behave.md new file mode 100644 index 0000000000..664a42e058 --- /dev/null +++ b/.changeset/large-carrots-behave.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: don't print errors on migration errors diff --git a/packages/svelte/src/compiler/migrate/index.js b/packages/svelte/src/compiler/migrate/index.js index 8c51b48bfc..33a369b8c8 100644 --- a/packages/svelte/src/compiler/migrate/index.js +++ b/packages/svelte/src/compiler/migrate/index.js @@ -25,6 +25,15 @@ const style_placeholder = '/*$$__STYLE_CONTENT__$$*/'; let has_migration_task = false; +class MigrationError extends Error { + /** + * @param {string} msg + */ + constructor(msg) { + super(msg); + } +} + /** * Does a best-effort migration of Svelte code towards using runes, event attributes and render tags. * May throw an error if the code is too complex to migrate automatically. @@ -310,8 +319,10 @@ export function migrate(source, { filename } = {}) { } return { code: str.toString() }; } catch (e) { - // eslint-disable-next-line no-console - console.error('Error while migrating Svelte code', e); + if (!(e instanceof MigrationError)) { + // eslint-disable-next-line no-console + console.error('Error while migrating Svelte code', e); + } has_migration_task = true; return { code: `\n${og_source}` @@ -398,7 +409,7 @@ const instance_script = { state.str.remove(/** @type {number} */ (node.start), /** @type {number} */ (node.end)); } if (illegal_specifiers.length > 0) { - throw new Error( + throw new MigrationError( `Can't migrate code with ${illegal_specifiers.join(' and ')}. Please migrate by hand.` ); } @@ -462,7 +473,7 @@ const instance_script = { if (declarator.id.type !== 'Identifier') { // TODO invest time in this? - throw new Error( + throw new MigrationError( 'Encountered an export declaration pattern that is not supported for automigration.' ); // Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = .. @@ -493,7 +504,7 @@ const instance_script = { const binding = /** @type {Binding} */ (state.scope.get(name)); if (state.analysis.uses_props && (declarator.init || binding.updated)) { - throw new Error( + throw new MigrationError( '$$props is used together with named props in a way that cannot be automatically migrated.' ); } @@ -1065,7 +1076,7 @@ const template = { } else if (slot_name !== 'default') { name = state.scope.generate(slot_name); if (name !== slot_name) { - throw new Error( + throw new MigrationError( 'This migration would change the name of a slot making the component unusable' ); } @@ -1520,7 +1531,7 @@ function handle_identifier(node, state, path) { } else if (name !== 'default') { let new_name = state.scope.generate(name); if (new_name !== name) { - throw new Error( + throw new MigrationError( 'This migration would change the name of a slot making the component unusable' ); } diff --git a/packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/_config.js b/packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/_config.js index ef889d5f23..aceae9ce69 100644 --- a/packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/_config.js +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/_config.js @@ -3,5 +3,6 @@ import { test } from '../../test'; export default test({ logs: [ 'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.' - ] + ], + errors: [] }); diff --git a/packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/_config.js b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/_config.js index ef889d5f23..aceae9ce69 100644 --- a/packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/_config.js +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/_config.js @@ -3,5 +3,6 @@ import { test } from '../../test'; export default test({ logs: [ 'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.' - ] + ], + errors: [] }); diff --git a/packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/_config.js b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/_config.js index ef889d5f23..aceae9ce69 100644 --- a/packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/_config.js +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/_config.js @@ -3,5 +3,6 @@ import { test } from '../../test'; export default test({ logs: [ 'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.' - ] + ], + errors: [] }); diff --git a/packages/svelte/tests/migrate/samples/impossible-migrate-slot-change-name/_config.js b/packages/svelte/tests/migrate/samples/impossible-migrate-slot-change-name/_config.js index ef889d5f23..aceae9ce69 100644 --- a/packages/svelte/tests/migrate/samples/impossible-migrate-slot-change-name/_config.js +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-slot-change-name/_config.js @@ -3,5 +3,6 @@ import { test } from '../../test'; export default test({ logs: [ 'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.' - ] + ], + errors: [] }); diff --git a/packages/svelte/tests/migrate/samples/impossible-migrate-slot-non-identifier/_config.js b/packages/svelte/tests/migrate/samples/impossible-migrate-slot-non-identifier/_config.js index ef889d5f23..aceae9ce69 100644 --- a/packages/svelte/tests/migrate/samples/impossible-migrate-slot-non-identifier/_config.js +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-slot-non-identifier/_config.js @@ -3,5 +3,6 @@ import { test } from '../../test'; export default test({ logs: [ 'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.' - ] + ], + errors: [] }); diff --git a/packages/svelte/tests/migrate/samples/impossible-migrate-with-errors/_config.js b/packages/svelte/tests/migrate/samples/impossible-migrate-with-errors/_config.js new file mode 100644 index 0000000000..6d25bb63b1 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-with-errors/_config.js @@ -0,0 +1,31 @@ +import { test } from '../../test'; + +export default test({ + logs: [ + 'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.' + ], + errors: [ + 'Error while migrating Svelte code', + { + code: 'unexpected_eof', + end: { + character: 30, + column: 21, + line: 3 + }, + filename: 'output.svelte', + frame: `1: