提交 8ca9163d 编写于 作者: Q qiang

fix: 修复小程序 v-if 中复杂表达式的作用域

上级 48dfd31d
...@@ -11,7 +11,7 @@ function isMatch (name, forItem, forIndex) { ...@@ -11,7 +11,7 @@ function isMatch (name, forItem, forIndex) {
return name === forItem || name === forIndex return name === forItem || name === forIndex
} }
function findScoped (path, state) { function findScoped (path, test, state) {
if (!path) { if (!path) {
return state return state
} }
...@@ -21,9 +21,7 @@ function findScoped (path, state) { ...@@ -21,9 +21,7 @@ function findScoped (path, state) {
forIndex forIndex
} = scoped } = scoped
let match = false let match = false
path.traverse({ function Identifier (path) {
noScope: true,
Identifier (path) {
if (!match && path.key !== 'key' && (path.key !== 'property' || path.parent.computed)) { if (!match && path.key !== 'key' && (path.key !== 'property' || path.parent.computed)) {
match = isMatch(path.node.name, forItem, forIndex) match = isMatch(path.node.name, forItem, forIndex)
if (match) { if (match) {
...@@ -31,7 +29,20 @@ function findScoped (path, state) { ...@@ -31,7 +29,20 @@ function findScoped (path, state) {
} }
} }
} }
path.traverse({
noScope: true,
Identifier
}) })
if (!match) {
if (t.isIdentifier(test, { name: IDENTIFIER_METHOD }) || t.isIdentifier(test, { name: IDENTIFIER_FILTER })) {
match = scoped.declarationArray.find(({ declarations }) => declarations.find(({ id }) => id === test))
} else if (!match) {
traverse(test, {
noScope: true,
Identifier
})
}
}
return match return match
}) })
if (!scoped && state.scoped.length > 1) { if (!scoped && state.scoped.length > 1) {
...@@ -84,8 +95,8 @@ function findTest (path, state) { ...@@ -84,8 +95,8 @@ function findTest (path, state) {
} }
module.exports = function getMemberExpr (path, name, init, state, variableDeclaration = true) { module.exports = function getMemberExpr (path, name, init, state, variableDeclaration = true) {
const scoped = findScoped(path, state)
const test = findTest(path, state) const test = findTest(path, state)
const scoped = findScoped(path, test, state)
if (!variableDeclaration) { if (!variableDeclaration) {
scoped.declarationArray.push(t.expressionStatement(init)) scoped.declarationArray.push(t.expressionStatement(init))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册