feat: add `migration-task` comment after errors (#13659)

pull/13668/head
Paolo Ricciuti 3 weeks ago committed by GitHub
parent b352f08e9d
commit 0dcd4f6c6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
feat: add `migration-task` comment after errors

@ -301,8 +301,11 @@ export function migrate(source, { filename } = {}) {
return { code: str.toString() };
} catch (e) {
// eslint-disable-next-line no-console
console.error('Error while migrating Svelte code');
throw e;
console.error('Error while migrating Svelte code', e);
return {
code: `<!-- @migration-task Error while migrating Svelte code: ${/** @type {any} */ (e).message} -->\n${source}`
};
}
}
@ -348,6 +351,40 @@ const instance_script = {
},
ImportDeclaration(node, { state }) {
state.props_insertion_point = node.end ?? state.props_insertion_point;
if (node.source.value === 'svelte') {
let illegal_specifiers = [];
let removed_specifiers = 0;
for (let specifier of node.specifiers) {
if (
specifier.type === 'ImportSpecifier' &&
['beforeUpdate', 'afterUpdate'].includes(specifier.imported.name)
) {
const references = state.scope.references.get(specifier.local.name);
if (!references) {
let end = /** @type {number} */ (
state.str.original.indexOf(',', specifier.end) !== -1 &&
state.str.original.indexOf(',', specifier.end) <
state.str.original.indexOf('}', specifier.end)
? state.str.original.indexOf(',', specifier.end) + 1
: specifier.end
);
while (state.str.original[end].trim() === '') end++;
state.str.remove(/** @type {number} */ (specifier.start), end);
removed_specifiers++;
continue;
}
illegal_specifiers.push(specifier.imported.name);
}
}
if (removed_specifiers === node.specifiers.length) {
state.str.remove(/** @type {number} */ (node.start), /** @type {number} */ (node.end));
}
if (illegal_specifiers.length > 0) {
throw new Error(
`Can't migrate code with ${illegal_specifiers.join(' and ')}. Please migrate by hand.`
);
}
}
},
ExportNamedDeclaration(node, { state, next }) {
if (node.declaration) {

@ -0,0 +1,11 @@
<script>
import { beforeUpdate, afterUpdate } from "svelte";
beforeUpdate(()=>{
});
afterUpdate(()=>{
});
</script>

@ -0,0 +1,12 @@
<!-- @migration-task Error while migrating Svelte code: Can't migrate code with beforeUpdate and afterUpdate. Please migrate by hand. -->
<script>
import { beforeUpdate, afterUpdate } from "svelte";
beforeUpdate(()=>{
});
afterUpdate(()=>{
});
</script>

@ -0,0 +1,6 @@
<!-- @migration-task Error while migrating Svelte code: $$props is used together with named props in a way that cannot be automatically migrated. -->
<script>
export let value = 42;
</script>
{$$props}

@ -0,0 +1,5 @@
<script>
let props = { value: 42 };
export let { value } = props;
</script>

@ -0,0 +1,6 @@
<!-- @migration-task Error while migrating Svelte code: Encountered an export declaration pattern that is not supported for automigration. -->
<script>
let props = { value: 42 };
export let { value } = props;
</script>

@ -0,0 +1,11 @@
<script>
import { beforeUpdate, afterUpdate, onMount } from "svelte";
let count = 0;
onMount(()=>{
console.log(count);
})
</script>
<button on:click={()=> count++}></button>

@ -0,0 +1,11 @@
<script>
import { onMount } from "svelte";
let count = $state(0);
onMount(()=>{
console.log(count);
})
</script>
<button onclick={()=> count++}></button>

@ -0,0 +1,7 @@
<script>
import { beforeUpdate, afterUpdate } from "svelte";
let count = 0;
</script>
<button on:click={()=> count++}></button>

@ -0,0 +1,7 @@
<script>
let count = $state(0);
</script>
<button onclick={()=> count++}></button>
Loading…
Cancel
Save