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
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
@@ -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
@@ -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) => {
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

Unchanged files with check annotations Beta

import type {} from "svelte"; // FIXME: Workaround to get type information for "svelte/compiler"

Check warning on line 1 in src/parser/template.ts

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: Workaround to get type...'
import { parse } from "svelte/compiler";
import type * as Compiler from "./svelte-ast-types-for-v5.js";
import type * as SvAST from "./svelte-ast-types.js";
};
// Link declaration and declarations for backward compatibility.
// TODO Remove in v2 and later.

Check warning on line 22 in src/parser/converts/const.ts

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO Remove in v2 and later.'
Object.defineProperty(mustache, "declaration", {
get() {
return mustache.declarations[0];
(key as any).parent = sAttr;
ctx.scriptLet.addObjectShorthandProperty(attribute.key, sAttr, (es) => {
if (
// FIXME: Older parsers may use the same node. In that case, do not replace.

Check warning on line 174 in src/parser/converts/attr.ts

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: Older parsers may use the same...'
// We will drop support for ESLint v7 in the next major version and remove this branch.
es.key !== es.value
) {
/**
* @deprecated Use `declarations` instead.
*/
declaration: ESTree.VariableDeclarator; // TODO Remove in v2 and later.

Check warning on line 269 in src/ast/html.ts

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO Remove in v2 and later.'
declarations: [ESTree.VariableDeclarator];
parent:
| SvelteProgram