Skip to content

fix: crash with $derived() in template using ts #698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gold-planes-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: crash with `$derived()` in template using ts
29 changes: 21 additions & 8 deletions src/parser/typescript/analyze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,26 @@ export function analyzeTypeScriptInSvelte(

analyzeRuneVariables(result, ctx, context.svelteParseContext);

applyTransforms(
[
...analyzeReactiveScopes(result),
...analyzeDollarDerivedScopes(result, context.svelteParseContext),
],
ctx,
);
const scriptTransformers: TransformInfo[] = [
...analyzeReactiveScopes(result),
];
const templateTransformers: TransformInfo[] = [];
for (const transform of analyzeDollarDerivedScopes(
result,
context.svelteParseContext,
)) {
if (transform.node.range[0] < code.script.length) {
scriptTransformers.push(transform);
} else {
templateTransformers.push(transform);
}
}

analyzeRenderScopes(code, ctx);
applyTransforms(scriptTransformers, ctx);

analyzeRenderScopes(code, ctx, () =>
applyTransforms(templateTransformers, ctx),
);

// 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`.
// see: https://github.com/sveltejs/svelte-eslint-parser/issues/557
Expand Down Expand Up @@ -625,10 +636,12 @@ function* analyzeDollarDerivedScopes(
function analyzeRenderScopes(
code: { script: string; render: string; rootScope: string },
ctx: VirtualTypeScriptContext,
analyzeInTemplate: () => void,
) {
ctx.appendOriginal(code.script.length);
const renderFunctionName = ctx.generateUniqueId("render");
ctx.appendVirtualScript(`export function ${renderFunctionName}(){`);
analyzeInTemplate();
ctx.appendOriginal(code.script.length + code.render.length);
ctx.appendVirtualScript(`}`);
ctx.restoreContext.addRestoreStatementProcess((node, result) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script lang="ts">
import MyComponent from './MyComponent.svelte';
</script>

<MyComponent :foo={() => $derived(0)} />
Loading
Loading