Skip to content

Commit 377d89d

Browse files
committed
fix: add migration task when there's a variable named that would conflict with a rune
1 parent 53af138 commit 377d89d

File tree

29 files changed

+259
-0
lines changed

29 files changed

+259
-0
lines changed

.changeset/chatty-gorillas-poke.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+
fix: add migration task when there's a variable named that would conflict with a rune

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,18 @@ export function migrate(source, { filename, use_ts } = {}) {
239239

240240
insertion_point = state.props_insertion_point;
241241

242+
/**
243+
* @param {"derived"|"props"|"bindable"} rune
244+
*/
245+
function check_rune_binding(rune) {
246+
const has_rune_binding = !!state.scope.get(rune);
247+
if (has_rune_binding) {
248+
throw new MigrationError(
249+
`migrating this component would require adding a \`$${rune}\` but there's already a variable named ${rune}`
250+
);
251+
}
252+
}
253+
242254
if (state.props.length > 0 || analysis.uses_rest_props || analysis.uses_props) {
243255
const has_many_props = state.props.length > 3;
244256
const newline_separator = `\n${indent}${indent}`;
@@ -253,6 +265,7 @@ export function migrate(source, { filename, use_ts } = {}) {
253265
let prop_str =
254266
prop.local === prop.exported ? prop.local : `${prop.exported}: ${prop.local}`;
255267
if (prop.bindable) {
268+
check_rune_binding('bindable');
256269
prop_str += ` = $bindable(${prop.init})`;
257270
} else if (prop.init) {
258271
prop_str += ` = ${prop.init}`;
@@ -300,11 +313,13 @@ export function migrate(source, { filename, use_ts } = {}) {
300313
if (type) {
301314
props_declaration = `${type}\n\n${indent}${props_declaration}`;
302315
}
316+
check_rune_binding('props');
303317
props_declaration = `${props_declaration}${type ? `: ${type_name}` : ''} = $props();`;
304318
} else {
305319
if (type) {
306320
props_declaration = `${state.props.length > 0 ? `${type}\n\n${indent}` : ''}/** @type {${state.props.length > 0 ? type_name : ''}${analysis.uses_props || analysis.uses_rest_props ? `${state.props.length > 0 ? ' & ' : ''}{ [key: string]: any }` : ''}} */\n${indent}${props_declaration}`;
307321
}
322+
check_rune_binding('props');
308323
props_declaration = `${props_declaration} = $props();`;
309324
}
310325

@@ -361,13 +376,15 @@ export function migrate(source, { filename, use_ts } = {}) {
361376
: insertion_point;
362377

363378
if (state.derived_components.size > 0) {
379+
check_rune_binding('derived');
364380
str.appendRight(
365381
insertion_point,
366382
`\n${indent}${[...state.derived_components.entries()].map(([init, name]) => `const ${name} = $derived(${init});`).join(`\n${indent}`)}\n`
367383
);
368384
}
369385

370386
if (state.derived_conflicting_slots.size > 0) {
387+
check_rune_binding('derived');
371388
str.appendRight(
372389
insertion_point,
373390
`\n${indent}${[...state.derived_conflicting_slots.entries()].map(([name, init]) => `const ${name} = $derived(${init});`).join(`\n${indent}`)}\n`
@@ -652,6 +669,19 @@ const instance_script = {
652669
continue;
653670
}
654671

672+
/**
673+
*
674+
* @param {"state"|"derived"} rune
675+
*/
676+
function check_rune_binding(rune) {
677+
const has_rune_binding = !!state.scope.get(rune);
678+
if (has_rune_binding) {
679+
throw new MigrationError(
680+
`can't migrate \`${state.str.original.substring(/** @type {number} */ (node.start), node.end)}\` to \`$${rune}\` because there's a variable named ${rune}`
681+
);
682+
}
683+
}
684+
655685
// state
656686
if (declarator.init) {
657687
let { start, end } = /** @type {{ start: number, end: number }} */ (declarator.init);
@@ -661,6 +691,8 @@ const instance_script = {
661691
while (state.str.original[end - 1] !== ')') end += 1;
662692
}
663693

694+
check_rune_binding('state');
695+
664696
state.str.prependLeft(start, '$state(');
665697
state.str.appendRight(end, ')');
666698
} else {
@@ -755,6 +787,8 @@ const instance_script = {
755787
}
756788
}
757789

790+
check_rune_binding('derived');
791+
758792
// Someone wrote a `$: { ... }` statement which we can turn into a `$derived`
759793
state.str.appendRight(
760794
/** @type {number} */ (declarator.id.typeAnnotation?.end ?? declarator.id.end),
@@ -795,6 +829,8 @@ const instance_script = {
795829
}
796830
}
797831
} else {
832+
check_rune_binding('state');
833+
798834
state.str.prependLeft(
799835
/** @type {number} */ (declarator.id.typeAnnotation?.end ?? declarator.id.end),
800836
' = $state('
@@ -858,6 +894,18 @@ const instance_script = {
858894

859895
next();
860896

897+
/**
898+
* @param {"state"|"derived"} rune
899+
*/
900+
function check_rune_binding(rune) {
901+
const has_rune_binding = state.scope.get(rune);
902+
if (has_rune_binding) {
903+
throw new MigrationError(
904+
`can't migrate \`$: ${state.str.original.substring(/** @type {number} */ (node.body.start), node.body.end)}\` to \`$${rune}\` because there's a variable named ${rune}`
905+
);
906+
}
907+
}
908+
861909
if (
862910
node.body.type === 'ExpressionStatement' &&
863911
node.body.expression.type === 'AssignmentExpression'
@@ -878,6 +926,8 @@ const instance_script = {
878926
node.body.expression.right
879927
);
880928

929+
check_rune_binding('derived');
930+
881931
// $derived
882932
state.str.update(
883933
/** @type {number} */ (node.start),
@@ -902,6 +952,7 @@ const instance_script = {
902952
} else {
903953
for (const binding of reassigned_bindings) {
904954
if (binding && (ids.includes(binding.node) || expression_ids.length === 0)) {
955+
check_rune_binding('state');
905956
const init =
906957
binding.kind === 'state'
907958
? ' = $state()'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
let bindable;
3+
export let something;
4+
</script>
5+
6+
<input bind:value={something} />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!-- @migration-task Error while migrating Svelte code: migrating this component would require adding a `$bindable` but there's already a variable named bindable -->
2+
<script>
3+
let bindable;
4+
export let something;
5+
</script>
6+
7+
<input bind:value={something} />
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
let name = 'world';
3+
4+
let derived;
5+
6+
$: other = name;
7+
</script>
8+
9+
<input bind:value={name} />
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!-- @migration-task Error while migrating Svelte code: can't migrate `$: other = name;` to `$derived` because there's a variable named derived -->
2+
<script>
3+
let name = 'world';
4+
5+
let derived;
6+
7+
$: other = name;
8+
</script>
9+
10+
<input bind:value={name} />
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let derived;
3+
</script>
4+
5+
<svelte:component this={derived} />

0 commit comments

Comments
 (0)