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
pull/13760/head
Paolo Ricciuti 11 months ago committed by GitHub
parent ae8f50fd6a
commit d1cac703c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: don't print errors on migration errors

@ -25,6 +25,15 @@ const style_placeholder = '/*$$__STYLE_CONTENT__$$*/';
let has_migration_task = false; 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. * 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. * 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() }; return { code: str.toString() };
} catch (e) { } catch (e) {
// eslint-disable-next-line no-console if (!(e instanceof MigrationError)) {
console.error('Error while migrating Svelte code', e); // eslint-disable-next-line no-console
console.error('Error while migrating Svelte code', e);
}
has_migration_task = true; has_migration_task = true;
return { return {
code: `<!-- @migration-task Error while migrating Svelte code: ${/** @type {any} */ (e).message} -->\n${og_source}` code: `<!-- @migration-task Error while migrating Svelte code: ${/** @type {any} */ (e).message} -->\n${og_source}`
@ -398,7 +409,7 @@ const instance_script = {
state.str.remove(/** @type {number} */ (node.start), /** @type {number} */ (node.end)); state.str.remove(/** @type {number} */ (node.start), /** @type {number} */ (node.end));
} }
if (illegal_specifiers.length > 0) { if (illegal_specifiers.length > 0) {
throw new Error( throw new MigrationError(
`Can't migrate code with ${illegal_specifiers.join(' and ')}. Please migrate by hand.` `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') { if (declarator.id.type !== 'Identifier') {
// TODO invest time in this? // TODO invest time in this?
throw new Error( throw new MigrationError(
'Encountered an export declaration pattern that is not supported for automigration.' '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]} = .. // 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)); const binding = /** @type {Binding} */ (state.scope.get(name));
if (state.analysis.uses_props && (declarator.init || binding.updated)) { 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.' '$$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') { } else if (slot_name !== 'default') {
name = state.scope.generate(slot_name); name = state.scope.generate(slot_name);
if (name !== 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' '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') { } else if (name !== 'default') {
let new_name = state.scope.generate(name); let new_name = state.scope.generate(name);
if (new_name !== name) { if (new_name !== name) {
throw new Error( throw new MigrationError(
'This migration would change the name of a slot making the component unusable' 'This migration would change the name of a slot making the component unusable'
); );
} }

@ -3,5 +3,6 @@ import { test } from '../../test';
export default test({ export default test({
logs: [ logs: [
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.' 'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
] ],
errors: []
}); });

@ -3,5 +3,6 @@ import { test } from '../../test';
export default test({ export default test({
logs: [ logs: [
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.' 'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
] ],
errors: []
}); });

@ -3,5 +3,6 @@ import { test } from '../../test';
export default test({ export default test({
logs: [ logs: [
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.' 'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
] ],
errors: []
}); });

@ -3,5 +3,6 @@ import { test } from '../../test';
export default test({ export default test({
logs: [ logs: [
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.' 'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
] ],
errors: []
}); });

@ -3,5 +3,6 @@ import { test } from '../../test';
export default test({ export default test({
logs: [ logs: [
'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.' 'One or more `@migration-task` comments were added to `output.svelte`, please check them and complete the migration manually.'
] ],
errors: []
}); });

@ -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: <script
2:
3: unterminated template
^`,
message: 'Unexpected end of input',
name: 'CompileError',
position: [30, 30],
start: {
character: 30,
column: 21,
line: 3
}
}
]
});

@ -0,0 +1,4 @@
<!-- @migration-task Error while migrating Svelte code: Unexpected end of input -->
<script
unterminated template

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

@ -6,7 +6,8 @@ import { suite, type BaseTest } from '../suite.js';
interface ParserTest extends BaseTest { interface ParserTest extends BaseTest {
skip_filename?: boolean; skip_filename?: boolean;
logs?: string[]; logs?: any[];
errors?: any[];
} }
const { test, run } = suite<ParserTest>(async (config, cwd) => { const { test, run } = suite<ParserTest>(async (config, cwd) => {
@ -16,6 +17,7 @@ const { test, run } = suite<ParserTest>(async (config, cwd) => {
.replace(/\r/g, ''); .replace(/\r/g, '');
const logs: any[] = []; const logs: any[] = [];
const errors: any[] = [];
if (config.logs) { if (config.logs) {
console.log = (...args) => { console.log = (...args) => {
@ -23,6 +25,12 @@ const { test, run } = suite<ParserTest>(async (config, cwd) => {
}; };
} }
if (config.errors) {
console.error = (...args) => {
errors.push(...args);
};
}
const actual = migrate(input, { const actual = migrate(input, {
filename: config.skip_filename ? undefined : `output.svelte` filename: config.skip_filename ? undefined : `output.svelte`
}).code; }).code;
@ -31,6 +39,10 @@ const { test, run } = suite<ParserTest>(async (config, cwd) => {
assert.deepEqual(logs, config.logs); assert.deepEqual(logs, config.logs);
} }
if (config.errors) {
assert.deepEqual(errors, config.errors);
}
// run `UPDATE_SNAPSHOTS=true pnpm test migrate` to update parser tests // run `UPDATE_SNAPSHOTS=true pnpm test migrate` to update parser tests
if (process.env.UPDATE_SNAPSHOTS || !fs.existsSync(`${cwd}/output.svelte`)) { if (process.env.UPDATE_SNAPSHOTS || !fs.existsSync(`${cwd}/output.svelte`)) {
fs.writeFileSync(`${cwd}/output.svelte`, actual); fs.writeFileSync(`${cwd}/output.svelte`, actual);

Loading…
Cancel
Save