Skip to content

Commit 7496309

Browse files
authored
Merge pull request #54 from mpx-ecology/fix-parse-using-components
fix(core): enhance usingComponents parsing
2 parents 52c2c74 + f8c061a commit 7496309

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

inspect-extension/components/script-json/json-js/index.mpx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ console.log('---> debug')
3232
<script name="json">
3333
const listPath = '../components/list.mpx'
3434

35-
const wxConfigL = {
35+
const wxConfig = {
3636
component: true,
3737
usingComponents: {
3838
list: '../components/list.mpx',
@@ -56,14 +56,16 @@ const wxConfigL = {
5656
},
5757
}
5858

59+
const comps = {
60+
list: '../components/list1.mpx',
61+
'list-error-path': '../components/list-error.mpx',
62+
'list-alias': '@/script-json/components/list.mpx',
63+
'list-from-dir': '@/script-json/components',
64+
}
65+
5966
const aliConfig = {
6067
component: true,
61-
usingComponents: {
62-
list: '../components/list1.mpx',
63-
'list-error-path': '../components/list-error.mpx',
64-
'list-alias': '@/script-json/components/list.mpx',
65-
'list-from-dir': '@/script-json/components',
66-
},
68+
usingComponents: comps,
6769
}
6870

6971
let finalConfig = null

packages/language-core/src/utils/parseJsonUsingComponents.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ export function parseUsingComponentsWithJs(
7575
(ts.isIdentifier(node.name) || ts.isStringLiteral(node.name)) &&
7676
node.name.text === 'usingComponents'
7777
) {
78-
if (ts.isObjectLiteralExpression(node.initializer)) {
79-
parseObjectLiteralProperties(node.initializer.properties)
80-
}
78+
// 支持 usingComponents 为对象字面量或变量等表达式
79+
parseObjectFromExpression(node.initializer)
8180
}
8281

8382
ts.forEachChild(node, visit)
@@ -167,6 +166,19 @@ export function parseUsingComponentsWithJs(
167166
expression = expression.expression
168167
}
169168

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+
170182
// 处理形如 (__mpx_mode__ === 'web' && { ... }) 的逻辑与表达式
171183
if (
172184
ts.isBinaryExpression(expression) &&
@@ -194,6 +206,9 @@ export function parseUsingComponentsWithJs(
194206
return usingComponents
195207
}
196208

209+
/**
210+
* 查找变量的初始值(用于解析字符串路径变量)
211+
*/
197212
function findVariableValue(
198213
ts: typeof import('typescript'),
199214
sourceFile: ts.SourceFile,
@@ -221,3 +236,31 @@ function findVariableValue(
221236
findDeclaration(sourceFile)
222237
return { variableValue, variableOffset }
223238
}
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

Comments
 (0)