Skip to content

Commit

Permalink
fix: error at compile time on unsupported TypeScript language features
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 22, 2024
1 parent 448f216 commit 5958b77
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/perfect-cooks-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: error at compile time on unsupported TypeScript language features
4 changes: 4 additions & 0 deletions packages/svelte/messages/compile-errors/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 feature like %feature% are not natively supported, and their use is generally discouraged. You will need to use a preprocessor to convert it to JavaScript before it gets passed to the Svelte compiler
10 changes: 10 additions & 0 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,16 @@ export function store_invalid_subscription_module(node) {
e(node, "store_invalid_subscription_module", "Cannot reference store value outside a `.svelte` file");
}

/**
* TypeScript language feature like %feature% are not natively supported, and their use is generally discouraged. You will need to use a preprocessor to convert it to JavaScript before it gets passed to the Svelte compiler
* @param {null | number | NodeLike} node
* @param {string} feature
* @returns {never}
*/
export function typescript_invalid_feature(node, feature) {
e(node, "typescript_invalid_feature", `TypeScript language feature like ${feature} are not natively supported, and their use is generally discouraged. You will need to use a preprocessor to convert it to JavaScript before it gets passed to the Svelte compiler`);
}

/**
* Declaration cannot be empty
* @param {null | number | NodeLike} node
Expand Down
6 changes: 5 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { SvelteSelf } from './visitors/SvelteSelf.js';
import { TaggedTemplateExpression } from './visitors/TaggedTemplateExpression.js';
import { Text } from './visitors/Text.js';
import { TitleElement } from './visitors/TitleElement.js';
import { TSEnumDeclaration } from './visitors/TSEnumDeclaration.js';
import { UpdateExpression } from './visitors/UpdateExpression.js';
import { UseDirective } from './visitors/UseDirective.js';
import { VariableDeclarator } from './visitors/VariableDeclarator.js';
Expand Down Expand Up @@ -168,7 +169,10 @@ const visitors = {
TitleElement,
UpdateExpression,
UseDirective,
VariableDeclarator
VariableDeclarator,

// @ts-expect-error
TSEnumDeclaration
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @import { Context } from '../types' */
import * as e from '../../../errors.js';

/**
* @param {any} node
* @param {Context} context
*/
export function TSEnumDeclaration(node, context) {
e.typescript_invalid_feature(node, 'enums');
}

0 comments on commit 5958b77

Please sign in to comment.