From 9b2a8f15fbf9c96b3cdb7af54eb72f557730a177 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Wed, 6 Nov 2024 16:48:27 +0100 Subject: [PATCH] fix: ignore `as` type expressions on property definitions (#14181) fixes #14179 --- .changeset/dull-laws-end.md | 5 +++++ .../src/compiler/phases/1-parse/remove_typescript_nodes.js | 5 +++-- .../svelte/tests/runtime-runes/samples/typescript/_config.js | 1 + .../tests/runtime-runes/samples/typescript/main.svelte | 1 + 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 .changeset/dull-laws-end.md diff --git a/.changeset/dull-laws-end.md b/.changeset/dull-laws-end.md new file mode 100644 index 0000000000..595424826e --- /dev/null +++ b/.changeset/dull-laws-end.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: ignore `as` type expressions on property definitions diff --git a/packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js b/packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js index 5febef420d..36a25f5124 100644 --- a/packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js +++ b/packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js @@ -56,13 +56,14 @@ const visitors = { if (node.exportKind === 'type') return b.empty; return node; }, - PropertyDefinition(node) { + PropertyDefinition(node, { next }) { if (node.accessor) { e.typescript_invalid_feature( node, 'accessor fields (related TSC proposal is not stage 4 yet)' ); } + return next(); }, TSAsExpression(node, context) { return context.visit(node.expression); @@ -97,7 +98,7 @@ const visitors = { if ((node.readonly || node.accessibility) && context.path.at(-2)?.kind === 'constructor') { e.typescript_invalid_feature(node, 'accessibility modifiers on constructor parameters'); } - return node.parameter; + return context.visit(node.parameter); }, FunctionExpression: remove_this_param, FunctionDeclaration: remove_this_param, diff --git a/packages/svelte/tests/runtime-runes/samples/typescript/_config.js b/packages/svelte/tests/runtime-runes/samples/typescript/_config.js index 7c9552b7b2..c15cd18225 100644 --- a/packages/svelte/tests/runtime-runes/samples/typescript/_config.js +++ b/packages/svelte/tests/runtime-runes/samples/typescript/_config.js @@ -1,6 +1,7 @@ import { flushSync } from 'svelte'; import { test } from '../../test'; +// This test mainly checks that all types are properly ignored by the compiler export default test({ html: '', diff --git a/packages/svelte/tests/runtime-runes/samples/typescript/main.svelte b/packages/svelte/tests/runtime-runes/samples/typescript/main.svelte index 6c9cacdeff..71638688f2 100644 --- a/packages/svelte/tests/runtime-runes/samples/typescript/main.svelte +++ b/packages/svelte/tests/runtime-runes/samples/typescript/main.svelte @@ -10,6 +10,7 @@ class Foo { public name: string; + x = 'x' as const; constructor(name: string) { this.name = name; }