Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nasty-teachers-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: tell users of `@migration-task`
14 changes: 13 additions & 1 deletion packages/svelte/src/compiler/migrate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { regex_is_valid_identifier } from '../phases/patterns.js';
const regex_style_tags = /(<style[^>]+>)([\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.
Expand All @@ -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]>} */
Expand Down Expand Up @@ -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: `<!-- @migration-task Error while migrating Svelte code: ${/** @type {any} */ (e).message} -->\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.`
);
}
}
}

Expand Down Expand Up @@ -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,
`<!-- @migration-task: svelte:self is deprecated, import this Svelte file into itself instead -->\n${indent}`
Expand Down Expand Up @@ -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,
`<!-- @migration-task: migrate this slot by hand, \`${snippet_name}\` is an invalid identifier -->\n${state.indent}`
Expand Down
Original file line number Diff line number Diff line change
@@ -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.'
]
});
Original file line number Diff line number Diff line change
@@ -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.'
]
});
Original file line number Diff line number Diff line change
@@ -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.'
]
});
Original file line number Diff line number Diff line change
@@ -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."
]
});
15 changes: 14 additions & 1 deletion packages/svelte/tests/migrate/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { suite, type BaseTest } from '../suite.js';

interface ParserTest extends BaseTest {
skip_filename?: boolean;
logs?: string[];
}

const { test, run } = suite<ParserTest>(async (config, cwd) => {
Expand All @@ -14,10 +15,22 @@ const { test, run } = suite<ParserTest>(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);
Expand Down