From 8ca9163d6dbe32e780a85e40759dcead1d8a84a3 Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 14 Aug 2020 20:55:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=20v-if=20=E4=B8=AD=E5=A4=8D=E6=9D=82=E8=A1=A8?= =?UTF-8?q?=E8=BE=BE=E5=BC=8F=E7=9A=84=E4=BD=9C=E7=94=A8=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/script/traverse/member-expr.js | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/packages/uni-template-compiler/lib/script/traverse/member-expr.js b/packages/uni-template-compiler/lib/script/traverse/member-expr.js index a01a0f9cc..a180e1976 100644 --- a/packages/uni-template-compiler/lib/script/traverse/member-expr.js +++ b/packages/uni-template-compiler/lib/script/traverse/member-expr.js @@ -11,7 +11,7 @@ function isMatch (name, forItem, forIndex) { return name === forItem || name === forIndex } -function findScoped (path, state) { +function findScoped (path, test, state) { if (!path) { return state } @@ -21,17 +21,28 @@ function findScoped (path, state) { forIndex } = scoped let match = false - path.traverse({ - noScope: true, - Identifier (path) { - if (!match && path.key !== 'key' && (path.key !== 'property' || path.parent.computed)) { - match = isMatch(path.node.name, forItem, forIndex) - if (match) { - path.stop() - } + function Identifier (path) { + if (!match && path.key !== 'key' && (path.key !== 'property' || path.parent.computed)) { + match = isMatch(path.node.name, forItem, forIndex) + if (match) { + path.stop() } } + } + 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 }) if (!scoped && state.scoped.length > 1) { @@ -84,8 +95,8 @@ function findTest (path, state) { } module.exports = function getMemberExpr (path, name, init, state, variableDeclaration = true) { - const scoped = findScoped(path, state) const test = findTest(path, state) + const scoped = findScoped(path, test, state) if (!variableDeclaration) { scoped.declarationArray.push(t.expressionStatement(init)) -- GitLab