@@ -946,17 +946,18 @@ function transformForReactiveStatement(
946
946
}
947
947
948
948
/**
949
- * Transform for `$derived(expr)` to `$derived((()=>{ return fn(); function fn () { return expr } })())`
949
+ * Transform for `$derived(expr)` to `$derived((()=>{ type This = typeof this; return fn(); function fn (this: This ) { return expr } })())`
950
950
*/
951
951
function transformForDollarDerived (
952
952
derivedCall : TSESTree . CallExpression ,
953
953
ctx : VirtualTypeScriptContext ,
954
954
) {
955
955
const functionId = ctx . generateUniqueId ( "$derivedArgument" ) ;
956
+ const thisTypeId = ctx . generateUniqueId ( "$This" ) ;
956
957
const expression = derivedCall . arguments [ 0 ] ;
957
958
ctx . appendOriginal ( expression . range [ 0 ] ) ;
958
959
ctx . appendVirtualScript (
959
- `(()=>{return ${ functionId } ();function ${ functionId } (){return ` ,
960
+ `(()=>{type ${ thisTypeId } = typeof this; return ${ functionId } ();function ${ functionId } (this: ${ thisTypeId } ){return ` ,
960
961
) ;
961
962
ctx . appendOriginal ( expression . range [ 1 ] ) ;
962
963
ctx . appendVirtualScript ( `}})()` ) ;
@@ -977,21 +978,40 @@ function transformForDollarDerived(
977
978
arg . arguments . length !== 0 ||
978
979
arg . callee . type !== "ArrowFunctionExpression" ||
979
980
arg . callee . body . type !== "BlockStatement" ||
980
- arg . callee . body . body . length !== 2 ||
981
- arg . callee . body . body [ 0 ] . type !== "ReturnStatement" ||
982
- arg . callee . body . body [ 0 ] . argument ?. type !== "CallExpression" ||
983
- arg . callee . body . body [ 0 ] . argument . callee . type !== "Identifier" ||
984
- arg . callee . body . body [ 0 ] . argument . callee . name !== functionId ||
985
- arg . callee . body . body [ 1 ] . type !== "FunctionDeclaration" ||
986
- arg . callee . body . body [ 1 ] . id . name !== functionId
981
+ arg . callee . body . body . length !== 3
987
982
) {
988
983
return false ;
989
984
}
990
- const fnNode = arg . callee . body . body [ 1 ] ;
985
+ const thisTypeNode = arg . callee . body . body [ 0 ] ;
991
986
if (
987
+ thisTypeNode . type !== "TSTypeAliasDeclaration" ||
988
+ thisTypeNode . id . name !== thisTypeId
989
+ ) {
990
+ return false ;
991
+ }
992
+ const returnNode = arg . callee . body . body [ 1 ] ;
993
+ if (
994
+ returnNode . type !== "ReturnStatement" ||
995
+ returnNode . argument ?. type !== "CallExpression" ||
996
+ returnNode . argument . callee . type !== "Identifier" ||
997
+ returnNode . argument . callee . name !== functionId
998
+ ) {
999
+ return false ;
1000
+ }
1001
+
1002
+ const fnNode = arg . callee . body . body [ 2 ] ;
1003
+ if (
1004
+ fnNode . type !== "FunctionDeclaration" ||
1005
+ fnNode . id . name !== functionId ||
992
1006
fnNode . body . body . length !== 1 ||
993
1007
fnNode . body . body [ 0 ] . type !== "ReturnStatement" ||
994
- ! fnNode . body . body [ 0 ] . argument
1008
+ ! fnNode . body . body [ 0 ] . argument ||
1009
+ fnNode . params [ 0 ] ?. type !== "Identifier" ||
1010
+ ! fnNode . params [ 0 ] . typeAnnotation ||
1011
+ fnNode . params [ 0 ] . typeAnnotation . typeAnnotation . type !==
1012
+ "TSTypeReference" ||
1013
+ fnNode . params [ 0 ] . typeAnnotation . typeAnnotation . typeName . type !==
1014
+ "Identifier"
995
1015
) {
996
1016
return false ;
997
1017
}
@@ -1002,11 +1022,16 @@ function transformForDollarDerived(
1002
1022
expr . parent = node ;
1003
1023
1004
1024
const scopeManager = result . scopeManager as ScopeManager ;
1005
- removeFunctionScope ( arg . callee . body . body [ 1 ] , scopeManager ) ;
1025
+ const fnScope = scopeManager . acquire ( fnNode ) ! ;
1026
+ removeIdentifierVariable ( fnNode . params [ 0 ] , fnScope ) ;
1006
1027
removeIdentifierReference (
1007
- arg . callee . body . body [ 0 ] . argument . callee ,
1008
- scopeManager . acquire ( arg . callee ) ! ,
1028
+ fnNode . params [ 0 ] . typeAnnotation . typeAnnotation . typeName ,
1029
+ fnScope ,
1009
1030
) ;
1031
+ removeFunctionScope ( fnNode , scopeManager ) ;
1032
+ const scope = scopeManager . acquire ( arg . callee ) ! ;
1033
+ removeIdentifierVariable ( thisTypeNode . id , scope ) ;
1034
+ removeIdentifierReference ( returnNode . argument . callee , scope ) ;
1010
1035
removeFunctionScope ( arg . callee , scopeManager ) ;
1011
1036
return true ;
1012
1037
} ,
0 commit comments