Skip to content

Commit 9f930f4

Browse files
feat: tell users of @migration-task (#13668)
1 parent 969e6aa commit 9f930f4

File tree

7 files changed

+57
-3
lines changed

7 files changed

+57
-3
lines changed

.changeset/nasty-teachers-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
feat: tell users of `@migration-task`

packages/svelte/src/compiler/migrate/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { regex_is_valid_identifier } from '../phases/patterns.js';
2323
const regex_style_tags = /(<style[^>]+>)([\S\s]*?)(<\/style>)/g;
2424
const style_placeholder = '/*$$__STYLE_CONTENT__$$*/';
2525

26+
let has_migration_task = false;
27+
2628
/**
2729
* Does a best-effort migration of Svelte code towards using runes, event attributes and render tags.
2830
* May throw an error if the code is too complex to migrate automatically.
@@ -33,6 +35,7 @@ const style_placeholder = '/*$$__STYLE_CONTENT__$$*/';
3335
*/
3436
export function migrate(source, { filename } = {}) {
3537
try {
38+
has_migration_task = false;
3639
// Blank CSS, could contain SCSS or similar that needs a preprocessor.
3740
// Since we don't care about CSS in this migration, we'll just ignore it.
3841
/** @type {Array<[number, string]>} */
@@ -303,10 +306,17 @@ export function migrate(source, { filename } = {}) {
303306
} catch (e) {
304307
// eslint-disable-next-line no-console
305308
console.error('Error while migrating Svelte code', e);
306-
309+
has_migration_task = true;
307310
return {
308311
code: `<!-- @migration-task Error while migrating Svelte code: ${/** @type {any} */ (e).message} -->\n${source}`
309312
};
313+
} finally {
314+
if (has_migration_task) {
315+
// eslint-disable-next-line no-console
316+
console.log(
317+
`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.`
318+
);
319+
}
310320
}
311321
}
312322

@@ -820,6 +830,7 @@ const template = {
820830
const source = state.str.original.substring(node.start, node.end);
821831
if (!state.filename) {
822832
const indent = guess_indent(source);
833+
has_migration_task = true;
823834
state.str.prependRight(
824835
node.start,
825836
`<!-- @migration-task: svelte:self is deprecated, import this Svelte file into itself instead -->\n${indent}`
@@ -1084,6 +1095,7 @@ function migrate_slot_usage(node, path, state) {
10841095
) {
10851096
snippet_name = attribute.value[0].data;
10861097
if (!regex_is_valid_identifier.test(snippet_name)) {
1098+
has_migration_task = true;
10871099
state.str.appendLeft(
10881100
node.start,
10891101
`<!-- @migration-task: migrate this slot by hand, \`${snippet_name}\` is an invalid identifier -->\n${state.indent}`
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
logs: [
5+
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
6+
]
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
logs: [
5+
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
6+
]
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
logs: [
5+
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
6+
]
7+
});
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { test } from '../../test';
22

33
export default test({
4-
skip_filename: true
4+
skip_filename: true,
5+
logs: [
6+
"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."
7+
]
58
});

packages/svelte/tests/migrate/test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { suite, type BaseTest } from '../suite.js';
66

77
interface ParserTest extends BaseTest {
88
skip_filename?: boolean;
9+
logs?: string[];
910
}
1011

1112
const { test, run } = suite<ParserTest>(async (config, cwd) => {
@@ -14,10 +15,22 @@ const { test, run } = suite<ParserTest>(async (config, cwd) => {
1415
.replace(/\s+$/, '')
1516
.replace(/\r/g, '');
1617

18+
const logs: any[] = [];
19+
20+
if (config.logs) {
21+
console.log = (...args) => {
22+
logs.push(...args);
23+
};
24+
}
25+
1726
const actual = migrate(input, {
18-
filename: config.skip_filename ? undefined : `${cwd}/output.svelte`
27+
filename: config.skip_filename ? undefined : `output.svelte`
1928
}).code;
2029

30+
if (config.logs) {
31+
assert.deepEqual(logs, config.logs);
32+
}
33+
2134
// run `UPDATE_SNAPSHOTS=true pnpm test migrate` to update parser tests
2235
if (process.env.UPDATE_SNAPSHOTS || !fs.existsSync(`${cwd}/output.svelte`)) {
2336
fs.writeFileSync(`${cwd}/output.svelte`, actual);

0 commit comments

Comments
 (0)