Skip to content

Commit 15a41f3

Browse files
authored
feat: TypeScript 5.4 support for svelte-check (#2313)
* bump ts to 5.4.2 * assign to bound variable for ts 5.4 to widen to control flow * format * oops this doesn't work in 5.3 * revert bumping but keeps compatibility fixes * lock * Update packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts
1 parent ceb4f70 commit 15a41f3

File tree

14 files changed

+105
-68
lines changed

14 files changed

+105
-68
lines changed

packages/language-server/src/plugins/typescript/features/DiagnosticsProvider.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,17 @@ function expectedTransitionThirdArgument(
543543
return false;
544544
}
545545

546-
const callExpression = findNodeAtSpan(
547-
node,
548-
{ start: node.getStart(), length: node.getWidth() },
549-
ts.isCallExpression
550-
);
546+
// in TypeScript 5.4 the error is on the function name
547+
// in earlier versions it's on the whole call expression
548+
const callExpression =
549+
ts.isIdentifier(node) && ts.isCallExpression(node.parent)
550+
? node.parent
551+
: findNodeAtSpan(
552+
node,
553+
{ start: node.getStart(), length: node.getWidth() },
554+
ts.isCallExpression
555+
);
556+
551557
const signature =
552558
callExpression && lang.getProgram()?.getTypeChecker().getResolvedSignature(callExpression);
553559

packages/language-server/src/plugins/typescript/module-loader.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class ImpliedNodeFormatResolver {
102102
let mode = undefined;
103103
if (sourceFile) {
104104
this.cacheImpliedNodeFormat(sourceFile, compilerOptions);
105-
mode = ts.getModeForResolutionAtIndex(sourceFile, importIdxInFile);
105+
// @ts-expect-error remove when bumping to TS 5.4
106+
mode = ts.getModeForResolutionAtIndex(sourceFile, importIdxInFile, compilerOptions);
106107
}
107108
return mode;
108109
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
export let value: 'foo' | 'bar';
3+
</script>
4+
5+
<button on:click={() => value = 'foo'}>foo</button>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script lang="ts">
2+
import Component from './Component.svelte';
3+
4+
let checked = false;
5+
let value: 'foo' | 'bar' = 'foo';
6+
</script>
7+
8+
<input type="checkbox" bind:checked>
9+
10+
{#if checked === true}
11+
checked
12+
{/if}
13+
14+
{#if value === 'bar'}
15+
bar
16+
{/if}
17+
18+
<Component bind:value></Component>

packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ export function handleBinding(
9494
return;
9595
}
9696

97+
// add reassignment to force TS to widen the type of the declaration (in case it's never reassigned anywhere else)
98+
const expressionStr = str.original.substring(attr.expression.start, getEnd(attr.expression));
99+
element.appendToStartEnd([
100+
surroundWithIgnoreComments(`() => ${expressionStr} = __sveltets_2_any(null);`)
101+
]);
102+
97103
// other bindings which are transformed to normal attributes/props
98104
const isShorthand = attr.expression.start === attr.start + 'bind:'.length;
99105
const name: TransformationArray =

packages/svelte2tsx/test/htmlx2jsx/samples/binding-bare/expectedv2.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/svelte2tsx/test/htmlx2jsx/samples/binding/expectedv2.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/svelte2tsx/test/htmlx2jsx/samples/directive-quoted/expectedv2.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/svelte2tsx/test/htmlx2jsx/samples/editing-binding/expectedv2.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)