From 0b74a921d53dad109ffc68651e5ab0e67cec2141 Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 14 Aug 2020 10:34:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E4=BD=BF=E7=94=A8=E5=A4=8D=E6=9D=82=E7=9A=84?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E8=A1=A8=E8=BE=BE=E5=BC=8F=E5=9C=A8=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=BC=96=E8=AF=91=E6=8A=A5=E9=94=99=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20question/103944?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/compiler-mp-weixin.spec.js | 1 + .../lib/script/traverse/data/attrs.js | 30 ++++----- .../lib/script/traverse/member-expr.js | 62 ++++++++++--------- 3 files changed, 49 insertions(+), 44 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-mp-weixin.spec.js b/packages/uni-template-compiler/__tests__/compiler-mp-weixin.spec.js index 73ac16d5..d0d8214b 100644 --- a/packages/uni-template-compiler/__tests__/compiler-mp-weixin.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-mp-weixin.spec.js @@ -131,6 +131,7 @@ describe('mp:compiler-mp-weixin', () => { '' ) assertCodegen('', '') + assertCodegen('', '', 'with(this){var a0={"x":0};$mp.data=Object.assign({},{$root:{a0:a0}})}') }) it('generate v-show directive', () => { diff --git a/packages/uni-template-compiler/lib/script/traverse/data/attrs.js b/packages/uni-template-compiler/lib/script/traverse/data/attrs.js index 7a153581..64498687 100644 --- a/packages/uni-template-compiler/lib/script/traverse/data/attrs.js +++ b/packages/uni-template-compiler/lib/script/traverse/data/attrs.js @@ -1,19 +1,21 @@ -// const { -// IDENTIFIER_ATTR -// } = require('../../../constants') +const t = require('@babel/types') -// const getMemberExpr = require('../member-expr') +const { + IDENTIFIER_ATTR +} = require('../../../constants') + +const getMemberExpr = require('../member-expr') module.exports = function processAttrs (paths, path, state, isComponent, tagName) { - // 不再单独处理ObjectExpression,改为在转换temlplte时用()包裹(微信、QQ) - // const attrsPath = paths.attrs - // if (attrsPath) { - // attrsPath.get('value.properties').forEach(propertyPath => { - // const valuePath = propertyPath.get('value') - // if (valuePath.isObjectExpression()) { - // valuePath.replaceWith(getMemberExpr(null, IDENTIFIER_ATTR, valuePath.node, state)) - // } - // }) - // } + const attrsPath = paths.attrs + if (attrsPath) { + attrsPath.get('value.properties').forEach(propertyPath => { + const valuePath = propertyPath.get('value') + // 对于普通的ObjectExpression不再单独处理,改为在转换temlplte时用()包裹(微信、QQ) + if (valuePath.isObjectExpression() && valuePath.node.properties.find(({ key }) => !t.isIdentifier(key))) { + valuePath.replaceWith(getMemberExpr(path, IDENTIFIER_ATTR, valuePath.node, state)) + } + }) + } return [] } 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 f24b487c..a01a0f9c 100644 --- a/packages/uni-template-compiler/lib/script/traverse/member-expr.js +++ b/packages/uni-template-compiler/lib/script/traverse/member-expr.js @@ -42,41 +42,43 @@ function findScoped (path, state) { function findTest (path, state) { let tests - while (path.parentPath && path.key !== 'body') { - if (path.key === 'consequent' || path.key === 'alternate') { - const testOrig = path.container.test - let test = t.arrayExpression([t.cloneDeep(testOrig)]) - traverse(test, { - noScope: true, - MemberExpression (memberExpressionPath) { - const names = state.scoped.map(scoped => scoped.forItem) - const node = memberExpressionPath.node - const objectName = node.object.name - const property = node.property - const propertyName = property.name - if (objectName === VAR_ROOT || (names.includes(objectName) && (propertyName === IDENTIFIER_METHOD || propertyName === IDENTIFIER_FILTER))) { - let property - traverse(testOrig, { - noScope: true, - Identifier (identifierPath) { - const node = identifierPath.node - if (node.name === propertyName) { - property = node - identifierPath.stop() + if (path) { + while (path.parentPath && path.key !== 'body') { + if (path.key === 'consequent' || path.key === 'alternate') { + const testOrig = path.container.test + let test = t.arrayExpression([t.cloneDeep(testOrig)]) + traverse(test, { + noScope: true, + MemberExpression (memberExpressionPath) { + const names = state.scoped.map(scoped => scoped.forItem) + const node = memberExpressionPath.node + const objectName = node.object.name + const property = node.property + const propertyName = property.name + if (objectName === VAR_ROOT || (names.includes(objectName) && (propertyName === IDENTIFIER_METHOD || propertyName === IDENTIFIER_FILTER))) { + let property + traverse(testOrig, { + noScope: true, + Identifier (identifierPath) { + const node = identifierPath.node + if (node.name === propertyName) { + property = node + identifierPath.stop() + } } - } - }) - memberExpressionPath.replaceWith(property) + }) + memberExpressionPath.replaceWith(property) + } } + }) + test = test.elements[0] + if (path.key === 'alternate') { + test = t.unaryExpression('!', test) } - }) - test = test.elements[0] - if (path.key === 'alternate') { - test = t.unaryExpression('!', test) + tests = tests ? t.logicalExpression('&&', test, tests) : test } - tests = tests ? t.logicalExpression('&&', test, tests) : test + path = path.parentPath } - path = path.parentPath } return tests } -- GitLab