@@ -889,43 +889,101 @@ function transformReturnStatement(node: any, scopeManager: ScopeManager): void {
889
889
return prop ;
890
890
} ) ;
891
891
} else if ( node . argument . type === 'Identifier' ) {
892
+ transformIdentifier ( node . argument , scopeManager ) ;
893
+ if ( node . argument . type === 'Identifier' ) {
894
+ addArrayAccess ( node . argument , scopeManager ) ;
895
+ }
892
896
// Handle identifier return values
893
- const [ scopedName , kind ] = scopeManager . getVariable ( node . argument . name ) ;
894
- node . argument = {
895
- type : 'MemberExpression' ,
896
- object : {
897
- type : 'MemberExpression' ,
898
- object : {
899
- type : 'Identifier' ,
900
- name : CONTEXT_NAME ,
901
- } ,
902
- property : {
903
- type : 'Identifier' ,
904
- name : kind ,
905
- } ,
906
- computed : false ,
907
- } ,
908
- property : {
909
- type : 'Identifier' ,
910
- name : scopedName ,
911
- } ,
912
- computed : false ,
913
- } ;
914
-
915
- // Add [0] array access
916
- node . argument = {
917
- type : 'MemberExpression' ,
918
- object : node . argument ,
919
- property : {
920
- type : 'Literal' ,
921
- value : 0 ,
922
- } ,
923
- computed : true ,
924
- } ;
897
+ // const [scopedName, kind] = scopeManager.getVariable(node.argument.name);
898
+ // node.argument = {
899
+ // type: 'MemberExpression',
900
+ // object: {
901
+ // type: 'MemberExpression',
902
+ // object: {
903
+ // type: 'Identifier',
904
+ // name: CONTEXT_NAME,
905
+ // },
906
+ // property: {
907
+ // type: 'Identifier',
908
+ // name: kind,
909
+ // },
910
+ // computed: false,
911
+ // },
912
+ // property: {
913
+ // type: 'Identifier',
914
+ // name: scopedName,
915
+ // },
916
+ // computed: false,
917
+ // };
918
+
919
+ // // Add [0] array access
920
+ // node.argument = {
921
+ // type: 'MemberExpression',
922
+ // object: node.argument,
923
+ // property: {
924
+ // type: 'Literal',
925
+ // value: 0,
926
+ // },
927
+ // computed: true,
928
+ // };
925
929
}
926
930
927
931
if ( curScope === 'fn' ) {
928
932
//for nested functions : wrap the return argument in a CallExpression with math._precision(<statement>)
933
+ // Process different types of return arguments
934
+ if (
935
+ node . argument . type === 'Identifier' &&
936
+ scopeManager . isContextBound ( node . argument . name ) &&
937
+ ! scopeManager . isRootParam ( node . argument . name )
938
+ ) {
939
+ // For context-bound identifiers, add [0] array access if not already an array access
940
+ node . argument = {
941
+ type : 'MemberExpression' ,
942
+ object : node . argument ,
943
+ property : {
944
+ type : 'Literal' ,
945
+ value : 0 ,
946
+ } ,
947
+ computed : true ,
948
+ } ;
949
+ } else if ( node . argument . type === 'MemberExpression' ) {
950
+ // For member expressions, check if the object is context-bound
951
+ if (
952
+ node . argument . object . type === 'Identifier' &&
953
+ scopeManager . isContextBound ( node . argument . object . name ) &&
954
+ ! scopeManager . isRootParam ( node . argument . object . name )
955
+ ) {
956
+ // Transform array indices first if not already transformed
957
+ if ( ! node . argument . _indexTransformed ) {
958
+ transformArrayIndex ( node . argument , scopeManager ) ;
959
+ node . argument . _indexTransformed = true ;
960
+ }
961
+ }
962
+ } else if (
963
+ node . argument . type === 'BinaryExpression' ||
964
+ node . argument . type === 'LogicalExpression' ||
965
+ node . argument . type === 'ConditionalExpression' ||
966
+ node . argument . type === 'CallExpression'
967
+ ) {
968
+ // For complex expressions, walk the AST and transform all identifiers and expressions
969
+ walk . recursive ( node . argument , scopeManager , {
970
+ Identifier ( node : any , state : ScopeManager ) {
971
+ transformIdentifier ( node , state ) ;
972
+ // Add array access if needed
973
+ if ( node . type === 'Identifier' && ! node . _arrayAccessed ) {
974
+ addArrayAccess ( node , state ) ;
975
+ node . _arrayAccessed = true ;
976
+ }
977
+ } ,
978
+ MemberExpression ( node : any ) {
979
+ transformMemberExpression ( node , '' , scopeManager ) ;
980
+ } ,
981
+ CallExpression ( node : any , state : ScopeManager ) {
982
+ transformCallExpression ( node , state ) ;
983
+ } ,
984
+ } ) ;
985
+ }
986
+
929
987
node . argument = {
930
988
type : 'CallExpression' ,
931
989
callee : {
@@ -1479,9 +1537,11 @@ function transformCallExpression(node: any, scopeManager: ScopeManager, namespac
1479
1537
1480
1538
function transformFunctionDeclaration ( node : any , scopeManager : ScopeManager ) : void {
1481
1539
// Register function parameters as context-bound (but not as root params)
1540
+ const boundParamNames = [ ] ;
1482
1541
node . params . forEach ( ( param : any ) => {
1483
1542
if ( param . type === 'Identifier' ) {
1484
1543
scopeManager . addContextBoundVar ( param . name , false ) ;
1544
+ boundParamNames . push ( param . name ) ;
1485
1545
}
1486
1546
} ) ;
1487
1547
0 commit comments