Skip to content

Commit 55367b3

Browse files
committed
Update for TS7
I am testing Typescript 7's JS support, which I've largely rewritten during the switch to Go. That means Javascript code will need to change much more than Typescript code. Fortunately, most of the changes are for the better: Javascript semantics are now nearly identical to Typescript semantics. It's much stricter and no longer has some persistent bugs that arose from shady JS handling I wrote years ago. This PR changes Svelte so that it compiles both with TS5.* and TS7, which means that occasionally there are duplicative or non-obvious changes. I'll annotate the interesting changes to explain why I made them. Because TS7 is quite a way off, I don't know whether you'll want to take this PR. Most of the changes are for the better, because they're due to stricter TS-aligned checking. But some are neutral and there is the previously mentioned duplication in a few places.
1 parent 6837246 commit 55367b3

File tree

28 files changed

+71
-43
lines changed

28 files changed

+71
-43
lines changed

packages/svelte/scripts/generate-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ await createBundle({
2626
// so that types/properties with `@internal` (and its dependencies) are removed from the output
2727
stripInternal: true,
2828
paths: Object.fromEntries(
29-
Object.entries(pkg.imports).map(([key, value]) => {
29+
Object.entries(pkg.imports).map(/** @param {[string,any]} import */([key, value]) => {
3030
return [key, [value.types ?? value.default ?? value]];
3131
})
3232
)

packages/svelte/src/compiler/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export function compileModule(source, options) {
8282
* @returns {AST.Root}
8383
*/
8484

85+
// TODO 6.0 remove unused `filename`
8586
/**
8687
* The parse function parses a component, returning only its abstract syntax tree.
8788
*
@@ -94,7 +95,6 @@ export function compileModule(source, options) {
9495
* @returns {Record<string, any>}
9596
*/
9697

97-
// TODO 6.0 remove unused `filename`
9898
/**
9999
* The parse function parses a component, returning only its abstract syntax tree.
100100
*
@@ -123,6 +123,8 @@ export function parse(source, { modern, loose } = {}) {
123123
* @param {boolean | undefined} modern
124124
*/
125125
function to_public_ast(source, ast, modern) {
126+
/** @type {AST.Root} */
127+
const rrrrr = parse('hi', { modern: true }); // ensure that the modern AST is available
126128
if (modern) {
127129
const clean = (/** @type {any} */ node) => {
128130
delete node.metadata;

packages/svelte/src/compiler/legacy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function convert(source, ast) {
5555

5656
// Insert svelte:options back into the root nodes
5757
if (/** @type {any} */ (options)?.__raw__) {
58-
let idx = node.fragment.nodes.findIndex((node) => options.end <= node.start);
58+
let idx = node.fragment.nodes.findIndex((node) => /** @type {any} */ (options).end <= node.start);
5959
if (idx === -1) {
6060
idx = node.fragment.nodes.length;
6161
}

packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
import { regex_ends_with_whitespace, regex_starts_with_whitespace } from '../../patterns.js';
1010
import { get_attribute_chunks, is_text_attribute } from '../../../utils/ast.js';
1111

12-
/** @typedef {NODE_PROBABLY_EXISTS | NODE_DEFINITELY_EXISTS} NodeExistsValue */
13-
/** @typedef {FORWARD | BACKWARD} Direction */
12+
/** @typedef {typeof NODE_PROBABLY_EXISTS | typeof NODE_DEFINITELY_EXISTS} NodeExistsValue */
13+
/** @typedef {typeof FORWARD | typeof BACKWARD} Direction */
1414

1515
const NODE_PROBABLY_EXISTS = 0;
1616
const NODE_DEFINITELY_EXISTS = 1;

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ export function analyze_module(source, options) {
295295
// TODO the following are not needed for modules, but we have to pass them in order to avoid type error,
296296
// and reducing the type would result in a lot of tedious type casts elsewhere - find a good solution one day
297297
ast_type: /** @type {any} */ (null),
298-
component_slots: new Set(),
298+
component_slots: /** @type {Set<string>} */ (new Set()),
299299
expression: null,
300300
function_depth: 0,
301301
has_props_rune: false,

packages/svelte/src/compiler/phases/2-analyze/utils/check_graph_for_cycles.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default function check_graph_for_cycles(edges) {
1414
}, new Map());
1515

1616
const visited = new Set();
17+
/** @type {Set<T>} */
1718
const on_stack = new Set();
1819
/** @type {Array<Array<T>>} */
1920
const cycles = [];

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/a11y/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ function has_disabled_attribute(attribute_map) {
599599
/**
600600
* @param {string} tag_name
601601
* @param {Map<string, AST.Attribute>} attribute_map
602-
* @returns {ElementInteractivity[keyof ElementInteractivity]}
602+
* @returns {typeof ElementInteractivity[keyof typeof ElementInteractivity]}
603603
*/
604604
function element_interactivity(tag_name, attribute_map) {
605605
if (

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/component.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export function visit_component(node, context) {
145145
if (slot_name !== 'default') comments = [];
146146
}
147147

148+
/** @type {Set<string>} */
148149
const component_slots = new Set();
149150

150151
for (const slot_name in nodes) {

packages/svelte/src/compiler/phases/scope.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const NUMBER = Symbol('number');
2222
const STRING = Symbol('string');
2323
const FUNCTION = Symbol('string');
2424

25-
/** @type {Record<string, [type: NUMBER | STRING | UNKNOWN, fn?: Function]>} */
25+
/** @type {Record<string, [type: typeof NUMBER | typeof STRING | typeof UNKNOWN, fn?: Function]>} */
2626
const globals = {
2727
BigInt: [NUMBER],
2828
'Math.min': [NUMBER, Math.min],

packages/svelte/src/compiler/state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function pop_ignore() {
8787

8888
/**
8989
* @param {AST.SvelteNode | NodeLike} node
90-
* @param {import('../constants.js').IGNORABLE_RUNTIME_WARNINGS[number]} code
90+
* @param {typeof import('../constants.js').IGNORABLE_RUNTIME_WARNINGS[number]} code
9191
* @returns
9292
*/
9393
export function is_ignored(node, code) {

0 commit comments

Comments
 (0)