Skip to content

Commit a4be4a6

Browse files
committed
feat: add migration-task comment after errors
1 parent b352f08 commit a4be4a6

File tree

12 files changed

+125
-2
lines changed

12 files changed

+125
-2
lines changed

.changeset/slimy-garlics-beg.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: add `migration-task` comment after errors

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,11 @@ export function migrate(source, { filename } = {}) {
301301
return { code: str.toString() };
302302
} catch (e) {
303303
// eslint-disable-next-line no-console
304-
console.error('Error while migrating Svelte code');
305-
throw e;
304+
console.error('Error while migrating Svelte code', e);
305+
306+
return {
307+
code: `<!-- @migration-task Error while migrating Svelte code: ${/** @type {any} */ (e).message} -->\n${source}`
308+
};
306309
}
307310
}
308311

@@ -348,6 +351,40 @@ const instance_script = {
348351
},
349352
ImportDeclaration(node, { state }) {
350353
state.props_insertion_point = node.end ?? state.props_insertion_point;
354+
if (node.source.value === 'svelte') {
355+
let illegal_specifiers = [];
356+
let removed_specifiers = 0;
357+
for (let specifier of node.specifiers) {
358+
if (
359+
specifier.type === 'ImportSpecifier' &&
360+
['beforeUpdate', 'afterUpdate'].includes(specifier.imported.name)
361+
) {
362+
const references = state.scope.references.get(specifier.local.name);
363+
if (!references) {
364+
let end = /** @type {number} */ (
365+
state.str.original.indexOf(',', specifier.end) !== -1 &&
366+
state.str.original.indexOf(',', specifier.end) <
367+
state.str.original.indexOf('}', specifier.end)
368+
? state.str.original.indexOf(',', specifier.end) + 1
369+
: specifier.end
370+
);
371+
while (state.str.original[end].trim() === '') end++;
372+
state.str.remove(/** @type {number} */ (specifier.start), end);
373+
removed_specifiers++;
374+
continue;
375+
}
376+
illegal_specifiers.push(specifier.imported.name);
377+
}
378+
}
379+
if (removed_specifiers === node.specifiers.length) {
380+
state.str.remove(/** @type {number} */ (node.start), /** @type {number} */ (node.end));
381+
}
382+
if (illegal_specifiers.length > 0) {
383+
throw new Error(
384+
`Can't migrate code with ${illegal_specifiers.join(' and ')}. Please migrate by hand.`
385+
);
386+
}
387+
}
351388
},
352389
ExportNamedDeclaration(node, { state, next }) {
353390
if (node.declaration) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { beforeUpdate, afterUpdate } from "svelte";
3+
4+
beforeUpdate(()=>{
5+
6+
});
7+
8+
afterUpdate(()=>{
9+
10+
});
11+
</script>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!-- @migration-task Error while migrating Svelte code: Can't migrate code with beforeUpdate and afterUpdate. Please migrate by hand. -->
2+
<script>
3+
import { beforeUpdate, afterUpdate } from "svelte";
4+
5+
beforeUpdate(()=>{
6+
7+
});
8+
9+
afterUpdate(()=>{
10+
11+
});
12+
</script>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let value = 42;
3+
</script>
4+
5+
{$$props}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!-- @migration-task Error while migrating Svelte code: $$props is used together with named props in a way that cannot be automatically migrated. -->
2+
<script>
3+
export let value = 42;
4+
</script>
5+
6+
{$$props}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let props = { value: 42 };
3+
4+
export let { value } = props;
5+
</script>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!-- @migration-task Error while migrating Svelte code: Encountered an export declaration pattern that is not supported for automigration. -->
2+
<script>
3+
let props = { value: 42 };
4+
5+
export let { value } = props;
6+
</script>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { beforeUpdate, afterUpdate, onMount } from "svelte";
3+
4+
let count = 0;
5+
6+
onMount(()=>{
7+
console.log(count);
8+
})
9+
</script>
10+
11+
<button on:click={()=> count++}></button>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { onMount } from "svelte";
3+
4+
let count = $state(0);
5+
6+
onMount(()=>{
7+
console.log(count);
8+
})
9+
</script>
10+
11+
<button onclick={()=> count++}></button>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import { beforeUpdate, afterUpdate } from "svelte";
3+
4+
let count = 0;
5+
</script>
6+
7+
<button on:click={()=> count++}></button>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
3+
4+
let count = $state(0);
5+
</script>
6+
7+
<button onclick={()=> count++}></button>

0 commit comments

Comments
 (0)