Skip to content

Commit 8188302

Browse files
authored
fix: crash with $derived() in template using ts (#698)
1 parent e82b517 commit 8188302

8 files changed

+2642
-8
lines changed

.changeset/gold-planes-retire.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": patch
3+
---
4+
5+
fix: crash with `$derived()` in template using ts

src/parser/typescript/analyze/index.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,26 @@ export function analyzeTypeScriptInSvelte(
7373

7474
analyzeRuneVariables(result, ctx, context.svelteParseContext);
7575

76-
applyTransforms(
77-
[
78-
...analyzeReactiveScopes(result),
79-
...analyzeDollarDerivedScopes(result, context.svelteParseContext),
80-
],
81-
ctx,
82-
);
76+
const scriptTransformers: TransformInfo[] = [
77+
...analyzeReactiveScopes(result),
78+
];
79+
const templateTransformers: TransformInfo[] = [];
80+
for (const transform of analyzeDollarDerivedScopes(
81+
result,
82+
context.svelteParseContext,
83+
)) {
84+
if (transform.node.range[0] < code.script.length) {
85+
scriptTransformers.push(transform);
86+
} else {
87+
templateTransformers.push(transform);
88+
}
89+
}
8390

84-
analyzeRenderScopes(code, ctx);
91+
applyTransforms(scriptTransformers, ctx);
92+
93+
analyzeRenderScopes(code, ctx, () =>
94+
applyTransforms(templateTransformers, ctx),
95+
);
8596

8697
// When performing type checking on TypeScript code that is not a module, the error `Cannot redeclare block-scoped variable 'xxx'`. occurs. To fix this, add an `export`.
8798
// see: https://github.com/sveltejs/svelte-eslint-parser/issues/557
@@ -625,10 +636,12 @@ function* analyzeDollarDerivedScopes(
625636
function analyzeRenderScopes(
626637
code: { script: string; render: string; rootScope: string },
627638
ctx: VirtualTypeScriptContext,
639+
analyzeInTemplate: () => void,
628640
) {
629641
ctx.appendOriginal(code.script.length);
630642
const renderFunctionName = ctx.generateUniqueId("render");
631643
ctx.appendVirtualScript(`export function ${renderFunctionName}(){`);
644+
analyzeInTemplate();
632645
ctx.appendOriginal(code.script.length + code.render.length);
633646
ctx.appendVirtualScript(`}`);
634647
ctx.restoreContext.addRestoreStatementProcess((node, result) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
import MyComponent from './MyComponent.svelte';
3+
</script>
4+
5+
<MyComponent :foo={() => $derived(0)} />

0 commit comments

Comments
 (0)