@@ -18,6 +18,65 @@ function cleanFunctionParams(params: string): string {
1818 return params . replace ( / ( \w + ) \s * : \s * ( [ A - Z ] [ ^ , ) ] * | s t r i n g | n u m b e r | b o o l e a n ) / g, '$1' ) ;
1919}
2020
21+ /**
22+ * 判断字符串是否是函数字符串
23+ */
24+ function isFunctionString ( value : unknown ) : value is string {
25+ if ( typeof value !== 'string' ) return false ;
26+ const trimmed = value . trim ( ) ;
27+ // 检查是否包含箭头函数或 function 关键字
28+ // 支持格式:
29+ // - () => { ... }
30+ // - (param) => { ... }
31+ // - async () => { ... }
32+ // - function() { ... }
33+ // - async function() { ... }
34+ return / ^ \s * ( a s y n c \s + ) ? ( \( [ ^ ) ] * \) | [ a - z A - Z _ $ ] [ a - z A - Z 0 - 9 _ $ ] * ) \s * = > / . test ( trimmed )
35+ || / ^ \s * ( a s y n c \s + ) ? f u n c t i o n \s * \( / . test ( trimmed ) ;
36+ }
37+
38+ /**
39+ * 将函数字符串转换为可执行函数
40+ */
41+ function parseFunctionString ( functionCode : string ) : ( ...args : unknown [ ] ) => unknown {
42+ const Fn = Function ;
43+ // 处理箭头函数: (param: Type, param2: Type2) => 或 async (param: Type) =>
44+ const cleanedCode = functionCode . replace ( / ( a s y n c \s + ) ? \( ( [ ^ ) ] * ) \) \s * = > / g, ( _match : string , asyncKeyword : string , params : string ) => {
45+ // 去除参数中的类型标注: param: Type -> param
46+ const cleanParams = cleanFunctionParams ( params ) ;
47+ return `${ asyncKeyword || '' } (${ cleanParams } ) =>` ;
48+ } ) ;
49+ // 转换为实际函数
50+ return Fn ( `return (${ cleanedCode } )` ) ( ) ;
51+ }
52+
53+ /**
54+ * 递归处理对象和数组中的函数字符串
55+ */
56+ function deepParseFunctions ( value : unknown ) : unknown {
57+ // 如果是函数字符串,转换为函数
58+ if ( isFunctionString ( value ) ) {
59+ return parseFunctionString ( value ) ;
60+ }
61+
62+ // 如果是数组,递归处理每个元素
63+ if ( Array . isArray ( value ) ) {
64+ return value . map ( item => deepParseFunctions ( item ) ) ;
65+ }
66+
67+ // 如果是对象,递归处理每个属性
68+ if ( value !== null && typeof value === 'object' ) {
69+ const result : Record < string , unknown > = { } ;
70+ for ( const [ key , val ] of Object . entries ( value ) ) {
71+ result [ key ] = deepParseFunctions ( val ) ;
72+ }
73+ return result ;
74+ }
75+
76+ // 其他类型直接返回
77+ return value ;
78+ }
79+
2180/**
2281 * 处理 events,将事件字符串转换为可执行函数
2382 */
@@ -59,31 +118,22 @@ export function processRenderProps(
59118
60119 // 判断 prop 是否为函数类型(包含 => 或以 function 开头)
61120 const isFunctionType = prop . type && ( prop . type . includes ( '=>' ) || prop . type . startsWith ( 'function' ) ) ;
62-
63121 if ( isFunctionType ) {
64122 // 如果是函数类型且值是字符串,需要转换为可执行函数
65- const Fn = Function ;
66- // 去除 TypeScript 类型标注(只处理参数列表中的类型标注)
67- // 匹配箭头函数或普通函数的参数列表,避免影响函数体
68- let functionCode = propValue ;
69-
70- // 处理箭头函数: (param: Type, param2: Type2) => 或 async (param: Type) =>
71- if ( typeof functionCode === 'string' ) {
72- functionCode = functionCode . replace ( / ( a s y n c \s + ) ? \( ( [ ^ ) ] * ) \) \s * = > / g, ( _match : string , asyncKeyword : string , params : string ) => {
73- // 去除参数中的类型标注: param: Type -> param
74- const cleanParams = cleanFunctionParams ( params ) ;
75- return `${ asyncKeyword || '' } (${ cleanParams } ) =>` ;
76- } ) ;
77- // 转换为实际函数
78- acc [ prop . name ] = Fn ( `return (${ functionCode } )` ) ( ) ;
123+ // 先检查是否是有效的函数字符串,避免将普通字符串误解析为函数 (联合类型:string | Function)
124+ if ( typeof propValue === 'string' && isFunctionString ( propValue ) ) {
125+ acc [ prop . name ] = parseFunctionString ( propValue ) ;
79126 } else {
127+ // 如果不是函数字符串,直接使用原值(可能是普通字符串、VNode、已经是函数等)
80128 acc [ prop . name ] = propValue ;
81129 }
82130 } else {
83- // 其他类型直接赋值
84- acc [ prop . name ] = propValue ;
131+ // 对于非函数类型,也要递归处理对象和数组中的函数字符串
132+ // 例如:nav-items 数组中的 action 函数,async 对象中的 callback 函数
133+ acc [ prop . name ] = deepParseFunctions ( propValue ) ;
85134 }
86135
136+ // 处理 v-model 支持
87137 if ( prop . isSupportVModel ) {
88138 // eslint-disable-next-line no-param-reassign
89139 renderEvents [ `onUpdate:${ key } ` ] = ( value : unknown ) => {
0 commit comments