Skip to content

fix: $derived argument expression to apply correct type information to this #732

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 4 commits into from
Aug 2, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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/light-carrots-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: `$derived` argument expression to apply correct type information to `this`
51 changes: 38 additions & 13 deletions src/parser/typescript/analyze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,10 +953,11 @@ function transformForDollarDerived(
ctx: VirtualTypeScriptContext,
) {
const functionId = ctx.generateUniqueId("$derivedArgument");
const thisTypeId = ctx.generateUniqueId("$This");
const expression = derivedCall.arguments[0];
ctx.appendOriginal(expression.range[0]);
ctx.appendVirtualScript(
`(()=>{return ${functionId}();function ${functionId}(){return `,
`(()=>{type ${thisTypeId} = typeof this; return ${functionId}();function ${functionId}(this: ${thisTypeId}){return `,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It think we need to update comment of transformForDollarDerived function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thanks!

);
ctx.appendOriginal(expression.range[1]);
ctx.appendVirtualScript(`}})()`);
Expand All @@ -977,21 +978,40 @@ function transformForDollarDerived(
arg.arguments.length !== 0 ||
arg.callee.type !== "ArrowFunctionExpression" ||
arg.callee.body.type !== "BlockStatement" ||
arg.callee.body.body.length !== 2 ||
arg.callee.body.body[0].type !== "ReturnStatement" ||
arg.callee.body.body[0].argument?.type !== "CallExpression" ||
arg.callee.body.body[0].argument.callee.type !== "Identifier" ||
arg.callee.body.body[0].argument.callee.name !== functionId ||
arg.callee.body.body[1].type !== "FunctionDeclaration" ||
arg.callee.body.body[1].id.name !== functionId
arg.callee.body.body.length !== 3
) {
return false;
}
const fnNode = arg.callee.body.body[1];
const thisTypeNode = arg.callee.body.body[0];
if (
thisTypeNode.type !== "TSTypeAliasDeclaration" ||
thisTypeNode.id.name !== thisTypeId
) {
return false;
}
const returnNode = arg.callee.body.body[1];
if (
returnNode.type !== "ReturnStatement" ||
returnNode.argument?.type !== "CallExpression" ||
returnNode.argument.callee.type !== "Identifier" ||
returnNode.argument.callee.name !== functionId
) {
return false;
}

const fnNode = arg.callee.body.body[2];
if (
fnNode.type !== "FunctionDeclaration" ||
fnNode.id.name !== functionId ||
fnNode.body.body.length !== 1 ||
fnNode.body.body[0].type !== "ReturnStatement" ||
!fnNode.body.body[0].argument
!fnNode.body.body[0].argument ||
fnNode.params[0]?.type !== "Identifier" ||
!fnNode.params[0].typeAnnotation ||
fnNode.params[0].typeAnnotation.typeAnnotation.type !==
"TSTypeReference" ||
fnNode.params[0].typeAnnotation.typeAnnotation.typeName.type !==
"Identifier"
) {
return false;
}
Expand All @@ -1002,11 +1022,16 @@ function transformForDollarDerived(
expr.parent = node;

const scopeManager = result.scopeManager as ScopeManager;
removeFunctionScope(arg.callee.body.body[1], scopeManager);
const fnScope = scopeManager.acquire(fnNode)!;
removeIdentifierVariable(fnNode.params[0], fnScope);
removeIdentifierReference(
arg.callee.body.body[0].argument.callee,
scopeManager.acquire(arg.callee)!,
fnNode.params[0].typeAnnotation.typeAnnotation.typeName,
fnScope,
);
removeFunctionScope(fnNode, scopeManager);
const scope = scopeManager.acquire(arg.callee)!;
removeIdentifierVariable(thisTypeNode.id, scope);
removeIdentifierReference(returnNode.argument.callee, scope);
removeFunctionScope(arg.callee, scopeManager);
return true;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class Product {
x = $state(1)
y = $state(2)
result = $derived(this.x * this.y)
}
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,5 @@
{
"parse": {
"svelte": ">=5.0.0-0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { Linter } from "eslint";
import { generateParserOptions } from "../../../src/parser/test-utils.js";
import { plugin } from "typescript-eslint";
import * as parser from "../../../../src/index.js";
import globals from "globals";

export function getConfig(): Linter.Config {
return {
plugins: {
"@typescript-eslint": {
rules: plugin.rules as any,
},
},
languageOptions: {
parser,
parserOptions: {
...generateParserOptions(),
svelteFeatures: { runes: true },
},
globals: {
...globals.browser,
...globals.es2021,
},
},
rules: {
"@typescript-eslint/no-unsafe-argument": "error",
"@typescript-eslint/no-unsafe-assignment": "error",
"@typescript-eslint/no-unsafe-call": "error",
"@typescript-eslint/no-unsafe-member-access": "error",
"@typescript-eslint/no-unsafe-return": "error",
},
};
}
Loading