From b6fccdb4f7ccbfe5e1548646f09c68f28e96783c Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Mon, 28 Oct 2024 23:17:02 +0100 Subject: [PATCH] fix: properly migrate imports types prefixed with $ (#14007) --- .changeset/wild-oranges-promise.md | 5 +++++ .../phases/2-analyze/visitors/shared/utils.js | 11 ++++++++++- packages/svelte/src/compiler/phases/scope.js | 2 +- .../migrate/samples/import-type-$-prefix/input.svelte | 5 +++++ .../samples/import-type-$-prefix/output.svelte | 9 +++++++++ 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 .changeset/wild-oranges-promise.md create mode 100644 packages/svelte/tests/migrate/samples/import-type-$-prefix/input.svelte create mode 100644 packages/svelte/tests/migrate/samples/import-type-$-prefix/output.svelte diff --git a/.changeset/wild-oranges-promise.md b/.changeset/wild-oranges-promise.md new file mode 100644 index 000000000000..d79666c47e43 --- /dev/null +++ b/.changeset/wild-oranges-promise.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: properly migrate imports types prefixed with $ diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/utils.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/utils.js index 266016fcc811..82356ea619c1 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/utils.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/utils.js @@ -220,7 +220,16 @@ export function validate_identifier_name(binding, function_depth) { if (node.name === '$') { e.dollar_binding_invalid(node); - } else if (node.name.startsWith('$')) { + } else if ( + node.name.startsWith('$') && + // import type { $Type } from "" - these are normally already filtered out, + // but for the migration they aren't, and throwing here is preventing the migration to complete + // TODO -> once migration script is gone we can remove this check + !( + binding.initial?.type === 'ImportDeclaration' && + /** @type {any} */ (binding.initial).importKind === 'type' + ) + ) { e.dollar_prefix_invalid(node); } } diff --git a/packages/svelte/src/compiler/phases/scope.js b/packages/svelte/src/compiler/phases/scope.js index 7a8afa532af9..0542a20708e9 100644 --- a/packages/svelte/src/compiler/phases/scope.js +++ b/packages/svelte/src/compiler/phases/scope.js @@ -348,7 +348,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) { is_reference(node, /** @type {Node} */ (parent)) && // TSTypeAnnotation, TSInterfaceDeclaration etc - these are normally already filtered out, // but for the migration they aren't, so we need to filter them out here - // -> once migration script is gone we can remove this check + // TODO -> once migration script is gone we can remove this check !parent.type.startsWith('TS') ) { references.push([state.scope, { node, path: path.slice() }]); diff --git a/packages/svelte/tests/migrate/samples/import-type-$-prefix/input.svelte b/packages/svelte/tests/migrate/samples/import-type-$-prefix/input.svelte new file mode 100644 index 000000000000..25eb04f3199e --- /dev/null +++ b/packages/svelte/tests/migrate/samples/import-type-$-prefix/input.svelte @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/packages/svelte/tests/migrate/samples/import-type-$-prefix/output.svelte b/packages/svelte/tests/migrate/samples/import-type-$-prefix/output.svelte new file mode 100644 index 000000000000..bcd724d8baec --- /dev/null +++ b/packages/svelte/tests/migrate/samples/import-type-$-prefix/output.svelte @@ -0,0 +1,9 @@ + \ No newline at end of file