Skip to content

Commit 151aef8

Browse files
committed
feat: don't print errors on migration errors
1 parent fd78385 commit 151aef8

File tree

12 files changed

+86
-14
lines changed

12 files changed

+86
-14
lines changed

.changeset/large-carrots-behave.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: don't print errors on migration errors

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ const style_placeholder = '/*$$__STYLE_CONTENT__$$*/';
2525

2626
let has_migration_task = false;
2727

28+
class MigrationError extends Error {
29+
/**
30+
* @param {string} msg
31+
*/
32+
constructor(msg) {
33+
super(msg);
34+
}
35+
}
36+
2837
/**
2938
* Does a best-effort migration of Svelte code towards using runes, event attributes and render tags.
3039
* May throw an error if the code is too complex to migrate automatically.
@@ -310,8 +319,10 @@ export function migrate(source, { filename } = {}) {
310319
}
311320
return { code: str.toString() };
312321
} catch (e) {
313-
// eslint-disable-next-line no-console
314-
console.error('Error while migrating Svelte code', e);
322+
if (!(e instanceof MigrationError)) {
323+
// eslint-disable-next-line no-console
324+
console.error('Error while migrating Svelte code', e);
325+
}
315326
has_migration_task = true;
316327
return {
317328
code: `<!-- @migration-task Error while migrating Svelte code: ${/** @type {any} */ (e).message} -->\n${og_source}`
@@ -398,7 +409,7 @@ const instance_script = {
398409
state.str.remove(/** @type {number} */ (node.start), /** @type {number} */ (node.end));
399410
}
400411
if (illegal_specifiers.length > 0) {
401-
throw new Error(
412+
throw new MigrationError(
402413
`Can't migrate code with ${illegal_specifiers.join(' and ')}. Please migrate by hand.`
403414
);
404415
}
@@ -462,7 +473,7 @@ const instance_script = {
462473

463474
if (declarator.id.type !== 'Identifier') {
464475
// TODO invest time in this?
465-
throw new Error(
476+
throw new MigrationError(
466477
'Encountered an export declaration pattern that is not supported for automigration.'
467478
);
468479
// Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = ..
@@ -493,7 +504,7 @@ const instance_script = {
493504
const binding = /** @type {Binding} */ (state.scope.get(name));
494505

495506
if (state.analysis.uses_props && (declarator.init || binding.updated)) {
496-
throw new Error(
507+
throw new MigrationError(
497508
'$$props is used together with named props in a way that cannot be automatically migrated.'
498509
);
499510
}
@@ -1065,7 +1076,7 @@ const template = {
10651076
} else if (slot_name !== 'default') {
10661077
name = state.scope.generate(slot_name);
10671078
if (name !== slot_name) {
1068-
throw new Error(
1079+
throw new MigrationError(
10691080
'This migration would change the name of a slot making the component unusable'
10701081
);
10711082
}
@@ -1520,7 +1531,7 @@ function handle_identifier(node, state, path) {
15201531
} else if (name !== 'default') {
15211532
let new_name = state.scope.generate(name);
15221533
if (new_name !== name) {
1523-
throw new Error(
1534+
throw new MigrationError(
15241535
'This migration would change the name of a slot making the component unusable'
15251536
);
15261537
}

packages/svelte/tests/migrate/samples/impossible-migrate-beforeUpdate-afterUpdate/_config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { test } from '../../test';
33
export default test({
44
logs: [
55
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
6-
]
6+
],
7+
errors: []
78
});

packages/svelte/tests/migrate/samples/impossible-migrate-prop-and-$$props/_config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { test } from '../../test';
33
export default test({
44
logs: [
55
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
6-
]
6+
],
7+
errors: []
78
});

packages/svelte/tests/migrate/samples/impossible-migrate-prop-non-identifier/_config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { test } from '../../test';
33
export default test({
44
logs: [
55
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
6-
]
6+
],
7+
errors: []
78
});

packages/svelte/tests/migrate/samples/impossible-migrate-slot-change-name/_config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { test } from '../../test';
33
export default test({
44
logs: [
55
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
6-
]
6+
],
7+
errors: []
78
});

packages/svelte/tests/migrate/samples/impossible-migrate-slot-non-identifier/_config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { test } from '../../test';
33
export default test({
44
logs: [
55
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
6-
]
6+
],
7+
errors: []
78
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
errors: [
8+
'Error while migrating Svelte code',
9+
{
10+
code: 'unexpected_eof',
11+
end: {
12+
character: 30,
13+
column: 21,
14+
line: 3
15+
},
16+
filename: 'output.svelte',
17+
frame: `1: <script
18+
2:
19+
3: unterminated template
20+
^`,
21+
message: 'Unexpected end of input',
22+
name: 'CompileError',
23+
position: [30, 30],
24+
start: {
25+
character: 30,
26+
column: 21,
27+
line: 3
28+
}
29+
}
30+
]
31+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script
2+
3+
unterminated template
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!-- @migration-task Error while migrating Svelte code: Unexpected end of input -->
2+
<script
3+
4+
unterminated template

packages/svelte/tests/migrate/samples/svelte-self-skip-filename/_config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export default test({
44
skip_filename: true,
55
logs: [
66
"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-
]
7+
],
8+
errors: []
89
});

packages/svelte/tests/migrate/test.ts

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

77
interface ParserTest extends BaseTest {
88
skip_filename?: boolean;
9-
logs?: string[];
9+
logs?: any[];
10+
errors?: any[];
1011
}
1112

1213
const { test, run } = suite<ParserTest>(async (config, cwd) => {
@@ -16,13 +17,20 @@ const { test, run } = suite<ParserTest>(async (config, cwd) => {
1617
.replace(/\r/g, '');
1718

1819
const logs: any[] = [];
20+
const errors: any[] = [];
1921

2022
if (config.logs) {
2123
console.log = (...args) => {
2224
logs.push(...args);
2325
};
2426
}
2527

28+
if (config.errors) {
29+
console.error = (...args) => {
30+
errors.push(...args);
31+
};
32+
}
33+
2634
const actual = migrate(input, {
2735
filename: config.skip_filename ? undefined : `output.svelte`
2836
}).code;
@@ -31,6 +39,10 @@ const { test, run } = suite<ParserTest>(async (config, cwd) => {
3139
assert.deepEqual(logs, config.logs);
3240
}
3341

42+
if (config.errors) {
43+
assert.deepEqual(errors, config.errors);
44+
}
45+
3446
// run `UPDATE_SNAPSHOTS=true pnpm test migrate` to update parser tests
3547
if (process.env.UPDATE_SNAPSHOTS || !fs.existsSync(`${cwd}/output.svelte`)) {
3648
fs.writeFileSync(`${cwd}/output.svelte`, actual);

0 commit comments

Comments
 (0)