From 194570dc70d38fac14991befa09191c6c858f7a0 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 3 Sep 2024 05:01:04 -0400 Subject: [PATCH] fix: error at compile time on unsupported TypeScript language features (#12982) part of #11502 - to close it completely, we also need to look at using and possibly implement heuristics within bundler plugins to give more details --------- Co-authored-by: Simon Holthausen --- .changeset/perfect-cooks-shop.md | 5 ++ .../svelte/messages/compile-errors/script.md | 4 ++ packages/svelte/src/compiler/errors.js | 10 ++++ .../phases/1-parse/remove_typescript_nodes.js | 49 +++++++++++++++++-- .../samples/typescript/main.svelte | 13 ++++- .../ts-unsupported-accessor/errors.json | 14 ++++++ .../ts-unsupported-accessor/input.svelte | 5 ++ .../ts-unsupported-decorator/errors.json | 14 ++++++ .../ts-unsupported-decorator/input.svelte | 4 ++ .../samples/ts-unsupported-enum/errors.json | 14 ++++++ .../samples/ts-unsupported-enum/input.svelte | 5 ++ .../ts-unsupported-modifier/errors.json | 14 ++++++ .../ts-unsupported-modifier/input.svelte | 5 ++ .../ts-unsupported-namespace/errors.json | 14 ++++++ .../ts-unsupported-namespace/input.svelte | 5 ++ packages/svelte/tests/validator/test.ts | 18 +++++-- 16 files changed, 184 insertions(+), 9 deletions(-) create mode 100644 .changeset/perfect-cooks-shop.md create mode 100644 packages/svelte/tests/validator/samples/ts-unsupported-accessor/errors.json create mode 100644 packages/svelte/tests/validator/samples/ts-unsupported-accessor/input.svelte create mode 100644 packages/svelte/tests/validator/samples/ts-unsupported-decorator/errors.json create mode 100644 packages/svelte/tests/validator/samples/ts-unsupported-decorator/input.svelte create mode 100644 packages/svelte/tests/validator/samples/ts-unsupported-enum/errors.json create mode 100644 packages/svelte/tests/validator/samples/ts-unsupported-enum/input.svelte create mode 100644 packages/svelte/tests/validator/samples/ts-unsupported-modifier/errors.json create mode 100644 packages/svelte/tests/validator/samples/ts-unsupported-modifier/input.svelte create mode 100644 packages/svelte/tests/validator/samples/ts-unsupported-namespace/errors.json create mode 100644 packages/svelte/tests/validator/samples/ts-unsupported-namespace/input.svelte diff --git a/.changeset/perfect-cooks-shop.md b/.changeset/perfect-cooks-shop.md new file mode 100644 index 000000000000..b698e6a648c0 --- /dev/null +++ b/.changeset/perfect-cooks-shop.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: error at compile time on unsupported TypeScript language features diff --git a/packages/svelte/messages/compile-errors/script.md b/packages/svelte/messages/compile-errors/script.md index 47dbac6eab70..7310749f1bb4 100644 --- a/packages/svelte/messages/compile-errors/script.md +++ b/packages/svelte/messages/compile-errors/script.md @@ -159,3 +159,7 @@ > Cannot reference store value outside a `.svelte` file Using a `$` prefix to refer to the value of a store is only possible inside `.svelte` files, where Svelte can automatically create subscriptions when a component is mounted and unsubscribe when the component is unmounted. Consider migrating to runes instead. + +## typescript_invalid_feature + +> TypeScript language features like %feature% are not natively supported, and their use is generally discouraged. Outside of ` diff --git a/packages/svelte/tests/validator/samples/ts-unsupported-decorator/errors.json b/packages/svelte/tests/validator/samples/ts-unsupported-decorator/errors.json new file mode 100644 index 000000000000..427cac76f693 --- /dev/null +++ b/packages/svelte/tests/validator/samples/ts-unsupported-decorator/errors.json @@ -0,0 +1,14 @@ +[ + { + "code": "typescript_invalid_feature", + "message": "TypeScript language features like decorators (related TSC proposal is not stage 4 yet) are not natively supported, and their use is generally discouraged. Outside of ` diff --git a/packages/svelte/tests/validator/samples/ts-unsupported-enum/errors.json b/packages/svelte/tests/validator/samples/ts-unsupported-enum/errors.json new file mode 100644 index 000000000000..cd972dd86a95 --- /dev/null +++ b/packages/svelte/tests/validator/samples/ts-unsupported-enum/errors.json @@ -0,0 +1,14 @@ +[ + { + "code": "typescript_invalid_feature", + "message": "TypeScript language features like enums are not natively supported, and their use is generally discouraged. Outside of ` diff --git a/packages/svelte/tests/validator/samples/ts-unsupported-modifier/errors.json b/packages/svelte/tests/validator/samples/ts-unsupported-modifier/errors.json new file mode 100644 index 000000000000..803ebdf980a6 --- /dev/null +++ b/packages/svelte/tests/validator/samples/ts-unsupported-modifier/errors.json @@ -0,0 +1,14 @@ +[ + { + "code": "typescript_invalid_feature", + "message": "TypeScript language features like accessibility modifiers on constructor parameters are not natively supported, and their use is generally discouraged. Outside of ` diff --git a/packages/svelte/tests/validator/samples/ts-unsupported-namespace/errors.json b/packages/svelte/tests/validator/samples/ts-unsupported-namespace/errors.json new file mode 100644 index 000000000000..c9f45d3a4199 --- /dev/null +++ b/packages/svelte/tests/validator/samples/ts-unsupported-namespace/errors.json @@ -0,0 +1,14 @@ +[ + { + "code": "typescript_invalid_feature", + "message": "TypeScript language features like namespaces with non-type nodes are not natively supported, and their use is generally discouraged. Outside of ` diff --git a/packages/svelte/tests/validator/test.ts b/packages/svelte/tests/validator/test.ts index f419c6360d8f..1dcc5a58d5a5 100644 --- a/packages/svelte/tests/validator/test.ts +++ b/packages/svelte/tests/validator/test.ts @@ -53,10 +53,20 @@ const { test, run } = suite(async (config, cwd) => { const expected = expected_errors && expected_errors[0]; if (error && expected) { - assert.equal(error.code, expected.code); - assert.equal(error.message, expected.message); - assert.deepEqual({ line: error.start?.line, column: error.start?.column }, expected.start); - assert.deepEqual({ line: error.end?.line, column: error.end?.column }, expected.end); + assert.deepEqual( + { + code: error.code, + message: error.message, + start: { line: error.start?.line, column: error.start?.column }, + end: { line: error.end?.line, column: error.end?.column } + }, + { + code: expected.code, + message: expected.message, + start: expected.start, + end: expected.end + } + ); } else if (expected) { throw new Error(`Expected an error: ${expected.message}`); } else if (error) {