Skip to content

Commit 1d18f04

Browse files
committed
fix: handle this parameter in TypeScript-annotated functions
fixes #11731
1 parent 4fef0eb commit 1d18f04

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

.changeset/fresh-walls-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: handle `this` parameter in TypeScript-annotated functions

packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import { walk } from 'zimmerframe';
22
import * as b from '../../utils/builders.js';
33

4+
/**
5+
* @param {import('estree').FunctionExpression | import('estree').FunctionDeclaration} node
6+
* @param {import('zimmerframe').Context<any, any>} context
7+
*/
8+
function remove_this_param(node, context) {
9+
if (node.params[0]?.type === 'Identifier' && node.params[0].name === 'this') {
10+
node.params.shift();
11+
}
12+
return context.next();
13+
}
14+
415
/** @type {import('zimmerframe').Visitors<any, null>} */
516
const visitors = {
617
ImportDeclaration(node) {
@@ -71,7 +82,9 @@ const visitors = {
7182
};
7283
}
7384
return node;
74-
}
85+
},
86+
FunctionExpression: remove_this_param,
87+
FunctionDeclaration: remove_this_param
7588
};
7689

7790
/**

packages/svelte/tests/runtime-runes/samples/typescript/main.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
interface Hello { message: 'hello' }
33
type Goodbye = { message: 'goodbye' };
44
5+
function this_fn(this: any) {
6+
console.log(this);
7+
}
8+
59
export type { Hello };
610
</script>
711

0 commit comments

Comments
 (0)