From 9f930f4e9b3c39adcddf3efce6faea92377542b1 Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Fri, 18 Oct 2024 12:09:08 +0200 Subject: [PATCH] feat: tell users of `@migration-task` (#13668) --- .changeset/nasty-teachers-end.md | 5 +++++ packages/svelte/src/compiler/migrate/index.js | 14 +++++++++++++- .../_config.js | 7 +++++++ .../_config.js | 7 +++++++ .../_config.js | 7 +++++++ .../samples/svelte-self-skip-filename/_config.js | 5 ++++- packages/svelte/tests/migrate/test.ts | 15 ++++++++++++++- 7 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 .changeset/nasty-teachers-end.md create mode 100644 packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/_config.js create mode 100644 packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/_config.js create mode 100644 packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/_config.js diff --git a/.changeset/nasty-teachers-end.md b/.changeset/nasty-teachers-end.md new file mode 100644 index 0000000000..ee84cce019 --- /dev/null +++ b/.changeset/nasty-teachers-end.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +feat: tell users of `@migration-task` diff --git a/packages/svelte/src/compiler/migrate/index.js b/packages/svelte/src/compiler/migrate/index.js index a554d0f0bb..69c840aedb 100644 --- a/packages/svelte/src/compiler/migrate/index.js +++ b/packages/svelte/src/compiler/migrate/index.js @@ -23,6 +23,8 @@ import { regex_is_valid_identifier } from '../phases/patterns.js'; const regex_style_tags = /(]+>)([\S\s]*?)(<\/style>)/g; const style_placeholder = '/*$$__STYLE_CONTENT__$$*/'; +let has_migration_task = false; + /** * 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. @@ -33,6 +35,7 @@ const style_placeholder = '/*$$__STYLE_CONTENT__$$*/'; */ export function migrate(source, { filename } = {}) { try { + has_migration_task = false; // Blank CSS, could contain SCSS or similar that needs a preprocessor. // Since we don't care about CSS in this migration, we'll just ignore it. /** @type {Array<[number, string]>} */ @@ -303,10 +306,17 @@ export function migrate(source, { filename } = {}) { } catch (e) { // eslint-disable-next-line no-console console.error('Error while migrating Svelte code', e); - + has_migration_task = true; return { code: `\n${source}` }; + } finally { + if (has_migration_task) { + // eslint-disable-next-line no-console + console.log( + `One or more \`@migration-task\` comments were added to ${filename ? `\`${filename}\`` : "a file (unfortunately we don't know the name)"}, please check them and complete the migration manually.` + ); + } } } @@ -820,6 +830,7 @@ const template = { const source = state.str.original.substring(node.start, node.end); if (!state.filename) { const indent = guess_indent(source); + has_migration_task = true; state.str.prependRight( node.start, `\n${indent}` @@ -1084,6 +1095,7 @@ function migrate_slot_usage(node, path, state) { ) { snippet_name = attribute.value[0].data; if (!regex_is_valid_identifier.test(snippet_name)) { + has_migration_task = true; state.str.appendLeft( node.start, `\n${state.indent}` 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 new file mode 100644 index 0000000000..ef889d5f23 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/_config.js @@ -0,0 +1,7 @@ +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.' + ] +}); 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 new file mode 100644 index 0000000000..ef889d5f23 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/_config.js @@ -0,0 +1,7 @@ +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.' + ] +}); 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 new file mode 100644 index 0000000000..ef889d5f23 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/_config.js @@ -0,0 +1,7 @@ +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.' + ] +}); diff --git a/packages/svelte/tests/migrate/samples/svelte-self-skip-filename/_config.js b/packages/svelte/tests/migrate/samples/svelte-self-skip-filename/_config.js index 9c8f6f9c78..8771de3560 100644 --- a/packages/svelte/tests/migrate/samples/svelte-self-skip-filename/_config.js +++ b/packages/svelte/tests/migrate/samples/svelte-self-skip-filename/_config.js @@ -1,5 +1,8 @@ import { test } from '../../test'; export default test({ - skip_filename: true + skip_filename: true, + logs: [ + "One or more `@migration-task` comments were added to a file (unfortunately we don't know the name), please check them and complete the migration manually." + ] }); diff --git a/packages/svelte/tests/migrate/test.ts b/packages/svelte/tests/migrate/test.ts index e26e8e376b..940eecb344 100644 --- a/packages/svelte/tests/migrate/test.ts +++ b/packages/svelte/tests/migrate/test.ts @@ -6,6 +6,7 @@ import { suite, type BaseTest } from '../suite.js'; interface ParserTest extends BaseTest { skip_filename?: boolean; + logs?: string[]; } const { test, run } = suite(async (config, cwd) => { @@ -14,10 +15,22 @@ const { test, run } = suite(async (config, cwd) => { .replace(/\s+$/, '') .replace(/\r/g, ''); + const logs: any[] = []; + + if (config.logs) { + console.log = (...args) => { + logs.push(...args); + }; + } + const actual = migrate(input, { - filename: config.skip_filename ? undefined : `${cwd}/output.svelte` + filename: config.skip_filename ? undefined : `output.svelte` }).code; + if (config.logs) { + assert.deepEqual(logs, config.logs); + } + // run `UPDATE_SNAPSHOTS=true pnpm test migrate` to update parser tests if (process.env.UPDATE_SNAPSHOTS || !fs.existsSync(`${cwd}/output.svelte`)) { fs.writeFileSync(`${cwd}/output.svelte`, actual);