@@ -75,9 +75,8 @@ export function parseUsingComponentsWithJs(
75
75
( ts . isIdentifier ( node . name ) || ts . isStringLiteral ( node . name ) ) &&
76
76
node . name . text === 'usingComponents'
77
77
) {
78
- if ( ts . isObjectLiteralExpression ( node . initializer ) ) {
79
- parseObjectLiteralProperties ( node . initializer . properties )
80
- }
78
+ // 支持 usingComponents 为对象字面量或变量等表达式
79
+ parseObjectFromExpression ( node . initializer )
81
80
}
82
81
83
82
ts . forEachChild ( node , visit )
@@ -167,6 +166,19 @@ export function parseUsingComponentsWithJs(
167
166
expression = expression . expression
168
167
}
169
168
169
+ // 处理标识符:const components = { ... }; usingComponents: components
170
+ if ( ts . isIdentifier ( expression ) ) {
171
+ const resolved = findVariableInitializerExpression (
172
+ ts ,
173
+ sourceFile ,
174
+ expression . text ,
175
+ )
176
+ if ( resolved ) {
177
+ return parseObjectFromExpression ( resolved )
178
+ }
179
+ return
180
+ }
181
+
170
182
// 处理形如 (__mpx_mode__ === 'web' && { ... }) 的逻辑与表达式
171
183
if (
172
184
ts . isBinaryExpression ( expression ) &&
@@ -194,6 +206,9 @@ export function parseUsingComponentsWithJs(
194
206
return usingComponents
195
207
}
196
208
209
+ /**
210
+ * 查找变量的初始值(用于解析字符串路径变量)
211
+ */
197
212
function findVariableValue (
198
213
ts : typeof import ( 'typescript' ) ,
199
214
sourceFile : ts . SourceFile ,
@@ -221,3 +236,31 @@ function findVariableValue(
221
236
findDeclaration ( sourceFile )
222
237
return { variableValue, variableOffset }
223
238
}
239
+
240
+ /**
241
+ * 查找对象变量的初始表达式(用于解析赋值给 usingComponents 的对象变量)
242
+ */
243
+ function findVariableInitializerExpression (
244
+ ts : typeof import ( 'typescript' ) ,
245
+ sourceFile : ts . SourceFile ,
246
+ identifierName : string ,
247
+ ) {
248
+ let initExp : ts . Expression | undefined
249
+
250
+ function findDeclaration ( node : ts . Node ) : boolean | undefined {
251
+ if (
252
+ ts . isVariableDeclaration ( node ) &&
253
+ ts . isIdentifier ( node . name ) &&
254
+ node . name . text === identifierName &&
255
+ ! ! node . initializer
256
+ ) {
257
+ initExp = node . initializer
258
+ return true
259
+ }
260
+
261
+ return ts . forEachChild ( node , findDeclaration )
262
+ }
263
+
264
+ findDeclaration ( sourceFile )
265
+ return initExp
266
+ }
0 commit comments