Skip to content

Commit

Permalink
feat: TypeScript 5.4 support for svelte-check (#2313)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jasonlyu123 authored Mar 19, 2024
1 parent ceb4f70 commit 15a41f3
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,17 @@ function expectedTransitionThirdArgument(
return false;
}

const callExpression = findNodeAtSpan(
node,
{ start: node.getStart(), length: node.getWidth() },
ts.isCallExpression
);
// in TypeScript 5.4 the error is on the function name
// in earlier versions it's on the whole call expression
const callExpression =
ts.isIdentifier(node) && ts.isCallExpression(node.parent)
? node.parent
: findNodeAtSpan(
node,
{ start: node.getStart(), length: node.getWidth() },
ts.isCallExpression
);

const signature =
callExpression && lang.getProgram()?.getTypeChecker().getResolvedSignature(callExpression);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class ImpliedNodeFormatResolver {
let mode = undefined;
if (sourceFile) {
this.cacheImpliedNodeFormat(sourceFile, compilerOptions);
mode = ts.getModeForResolutionAtIndex(sourceFile, importIdxInFile);
// @ts-expect-error remove when bumping to TS 5.4
mode = ts.getModeForResolutionAtIndex(sourceFile, importIdxInFile, compilerOptions);
}
return mode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script lang="ts">
export let value: 'foo' | 'bar';
</script>

<button on:click={() => value = 'foo'}>foo</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script lang="ts">
import Component from './Component.svelte';
let checked = false;
let value: 'foo' | 'bar' = 'foo';
</script>

<input type="checkbox" bind:checked>

{#if checked === true}
checked
{/if}

{#if value === 'bar'}
bar
{/if}

<Component bind:value></Component>
6 changes: 6 additions & 0 deletions packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export function handleBinding(
return;
}

// add reassignment to force TS to widen the type of the declaration (in case it's never reassigned anywhere else)
const expressionStr = str.original.substring(attr.expression.start, getEnd(attr.expression));
element.appendToStartEnd([
surroundWithIgnoreComments(`() => ${expressionStr} = __sveltets_2_any(null);`)
]);

// other bindings which are transformed to normal attributes/props
const isShorthand = attr.expression.start === attr.start + 'bind:'.length;
const name: TransformationArray =
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@ async•()•=>•{••{•const•$$_tnenopmoC0C•=•__sveltets_2_ensureCom
{/**
------------------------------------------------------------------------------------------------------------------------------------------------------ */}
{ const $$_tnenopmoC0C = __sveltets_2_ensureComponent(Component); new $$_tnenopmoC0C({ target: __sveltets_2_any(), props: { foo:bar,}});} {/**
•{•const•$$_tnenopmoC0C•=•__sveltets_2_ensureComponent(Component);•new•$$_tnenopmoC0C({•target:•__sveltets_2_any(),•props:•{•••foo:bar,}});}↲ [generated] line 11
•{•const•$$_tnenopmoC0C•=•__sveltets_2_ensureComponent(Component);•new•$$_tnenopmoC0C({•target:•__sveltets_2_any(),•props:•{ [generated] subset
< Component
<Component
<Component↲ [original] line 11
•{•const•$$_tnenopmoC0C•=•__sveltets_2_ensureComponent(Component);•new•$$_tnenopmoC0C({•target:•__sveltets_2_any(),•props:•{•••foo:bar,}});}↲ [generated] line 11
•• foo:bar,}});} [generated] subset
b{ foo=bar}
#= Order-breaking mappings
b foo={bar}
╚bind:foo={bar}↲ [original] line 12
•{•const•$$_tnenopmoC0C•=•__sveltets_2_ensureComponent(Component);•new•$$_tnenopmoC0C({•target:•__sveltets_2_any(),•props:•{•••foo:bar,}});}↲ [generated] line 11
• ↲ [generated] subset
/
/ ↲
/>↲ [original] line 13
{ const $$_tnenopmoC0C = __sveltets_2_ensureComponent(Component); new $$_tnenopmoC0C({ target: __sveltets_2_any(), props: { foo:bar,}});/*Ωignore_startΩ*/() => bar = __sveltets_2_any(null);/*Ωignore_endΩ*/}{/**
•{•const•$$_tnenopmoC0C•=•__sveltets_2_ensureComponent(Component);•new•$$_tnenopmoC0C({•target:•__sveltets_2_any(),•props:•{•••foo:bar,}});/*Ωignore_startΩ*/()=>•bar•=•__sveltets_2_any(null);/*Ωignore_endΩ*/} [generated] line 11
{•const•$$_tnenopmoC0C•=•__sveltets_2_ensureComponent(Component);•new•$$_tnenopmoC0C({•target:•__sveltets_2_any(),•props:{ [generated] subset
< Component
<Component
<Component↲ [original] line 11
{•const•$$_tnenopmoC0C•=•__sveltets_2_ensureComponent(Component);•new•$$_tnenopmoC0C({•target:•__sveltets_2_any(),•props:{•••foo:bar,}});/*Ωignore_startΩ*/()•=>•bar•=•__sveltets_2_any(null);/*Ωignore_endΩ*/}↲ [generated] line 11
•• foo:bar,}});/*Ωignore_startΩ*/()•=>•bar•=•__sveltets_2_any(null);/*Ωignore_endΩ*/} [generated] subset
b{ foo=bar}
#= Order-breaking mappings
b foo={bar}
╚bind:foo={bar} [original] line 12
{•const•$$_tnenopmoC0C•=•__sveltets_2_ensureComponent(Component);•new•$$_tnenopmoC0C({•target:•__sveltets_2_any(),•props:{•••foo:bar,}});/*Ωignore_startΩ*/()•=>•bar•=•__sveltets_2_any(null);/*Ωignore_endΩ*/}↲ [generated] line 11
[generated] subset
/
/
/> [original] line 13
------------------------------------------------------------------------------------------------------------------------------------------------------ */}
{/**
Expand Down
Loading

0 comments on commit 15a41f3

Please sign in to comment.