diff --git a/packages/uni-mp-compiler/__tests__/class.spec.ts b/packages/uni-mp-compiler/__tests__/class.spec.ts index 9bcf018ab3b8095fe01752320502d3304579d6e2..28b03a36d369a31ab080aebb5c2dfa776f3d5d96 100644 --- a/packages/uni-mp-compiler/__tests__/class.spec.ts +++ b/packages/uni-mp-compiler/__tests__/class.spec.ts @@ -61,14 +61,14 @@ describe('compiler: transform class', () => { ``, ``, `(_ctx, _cache) => { - return { a: \`\${_ctx.isRed}\` } + return { a: \`\${_ctx.isRed}\` ? 1 : 0 } }` ) assert( ``, ``, `(_ctx, _cache) => { - return { a: _ctx.ok, b: _ctx.handle(_ctx.ok), c: _ctx.ok > 1, d: _ctx.j, e: _ctx.k, f: _ctx.l, g: _ctx.m, h: _normalizeClass(_ctx.n), i: _normalizeClass({ a: true }), j: _normalizeClass({ b: _ctx.o }) } + return { a: _ctx.ok ? 1 : 0, b: _ctx.handle(_ctx.ok) ? 1 : 0, c: _ctx.ok > 1 ? 1 : 0, d: _ctx.j ? 1 : 0, e: _ctx.k, f: _ctx.l, g: _ctx.m ? 1 : 0, h: _normalizeClass(_ctx.n), i: _normalizeClass({ a: true }), j: _normalizeClass({ b: _ctx.o }) } }` ) }) @@ -77,28 +77,28 @@ describe('compiler: transform class', () => { ``, ``, `(_ctx, _cache) => { - return { a: _ctx.isRed } + return { a: _ctx.isRed ? 1 : 0 } }` ) assert( ``, ``, `(_ctx, _cache) => { - return { a: _ctx.isRed } + return { a: _ctx.isRed ? 1 : 0 } }` ) assert( ``, ``, `(_ctx, _cache) => { - return { a: _ctx.ok, b: _ctx.handle(_ctx.ok), c: _ctx.ok > 1, d: _ctx.j, e: _ctx.k, f: _ctx.l, g: _ctx.m, h: _normalizeClass(_ctx.n), i: _normalizeClass({ a: true }), j: _normalizeClass({ b: _ctx.o }) } + return { a: _ctx.ok ? 1 : 0, b: _ctx.handle(_ctx.ok) ? 1 : 0, c: _ctx.ok > 1 ? 1 : 0, d: _ctx.j ? 1 : 0, e: _ctx.k, f: _ctx.l, g: _ctx.m ? 1 : 0, h: _normalizeClass(_ctx.n), i: _normalizeClass({ a: true }), j: _normalizeClass({ b: _ctx.o }) } }` ) assert( ``, ``, `(_ctx, _cache) => { - return { a: _ctx.ok, b: _ctx.handle(_ctx.ok), c: _ctx.ok > 1, d: _ctx.j, e: _ctx.k, f: _ctx.l, g: _ctx.m, h: _normalizeClass(_ctx.n), i: _normalizeClass({ a: true }), j: _normalizeClass({ b: _ctx.o }) } + return { a: _ctx.ok ? 1 : 0, b: _ctx.handle(_ctx.ok) ? 1 : 0, c: _ctx.ok > 1 ? 1 : 0, d: _ctx.j ? 1 : 0, e: _ctx.k, f: _ctx.l, g: _ctx.m ? 1 : 0, h: _normalizeClass(_ctx.n), i: _normalizeClass({ a: true }), j: _normalizeClass({ b: _ctx.o }) } }` ) }) diff --git a/packages/uni-mp-compiler/__tests__/scope.spec.ts b/packages/uni-mp-compiler/__tests__/scope.spec.ts index 5232b715d0b1a090766850722eb9827e83b8591c..7fe5343de7473ce1b714f1a3edfc92ada6fc59bb 100644 --- a/packages/uni-mp-compiler/__tests__/scope.spec.ts +++ b/packages/uni-mp-compiler/__tests__/scope.spec.ts @@ -6,7 +6,7 @@ describe('compiler: scope', () => { `{{item.title}}`, `{{item.a}}`, `(_ctx, _cache) => { - return { a: _vFor(_ctx.items, (item, k0, i0) => { return { a: _toDisplayString(item.title), b: item.id, c: item.isRed, d: _vOn($event => _ctx.onClick(item)) }; }), b: _vOn(_ctx.longpress) } + return { a: _vFor(_ctx.items, (item, k0, i0) => { return { a: _toDisplayString(item.title), b: item.id, c: item.isRed ? 1 : 0, d: _vOn($event => _ctx.onClick(item)) }; }), b: _vOn(_ctx.longpress) } }` ) }) diff --git a/packages/uni-mp-compiler/__tests__/test.spec.ts b/packages/uni-mp-compiler/__tests__/test.spec.ts index d52b3c899c81ff1c40d34fb7ce70c7e318782f50..c4813a75a2b841531c4635dde0d1ecfdb44147f6 100644 --- a/packages/uni-mp-compiler/__tests__/test.spec.ts +++ b/packages/uni-mp-compiler/__tests__/test.spec.ts @@ -37,10 +37,10 @@ function assert( describe('compiler', () => { test('scope', () => { assert( - ``, - ``, + ``, + ``, `(_ctx, _cache) => { - return { a: \`\${_ctx.green}px\` } + return { a: _ctx.isRed ? 1 : 0 } }` ) }) diff --git a/packages/uni-mp-compiler/src/transforms/transformClass.ts b/packages/uni-mp-compiler/src/transforms/transformClass.ts index 2788cfbbc9ffd6a1a433d743d6dbc53a1807d125..38f73625f0d188623b0ec3809e1545cab1779d71 100644 --- a/packages/uni-mp-compiler/src/transforms/transformClass.ts +++ b/packages/uni-mp-compiler/src/transforms/transformClass.ts @@ -35,6 +35,7 @@ import { genBabelExpr } from '../codegen' import { TransformContext } from '../transform' import { parseExprWithRewrite, + parseExprWithRewriteClass, rewriteExpression, rewriteSpreadElement, } from './utils' @@ -160,7 +161,7 @@ function rewriteClassObjectExpression( if (isLiteral(value) && !isTemplateLiteral(value)) { return } else { - const newExpr = parseExprWithRewrite( + const newExpr = parseExprWithRewriteClass( genBabelExpr(value as Expression), loc, context, diff --git a/packages/uni-mp-compiler/src/transforms/utils.ts b/packages/uni-mp-compiler/src/transforms/utils.ts index da82ffdbab7a5a35a499aaa1c61da25a0b302c2e..30ee9f1aaf59b02e4155c770d72a5d3fb3cde1bb 100644 --- a/packages/uni-mp-compiler/src/transforms/utils.ts +++ b/packages/uni-mp-compiler/src/transforms/utils.ts @@ -1,10 +1,12 @@ import { + conditionalExpression, Expression, Identifier, identifier, isIdentifier, isReferenced, MemberExpression, + numericLiteral, objectProperty, SpreadElement, } from '@babel/types' @@ -54,6 +56,25 @@ export function parseExprWithRewrite( ) as Identifier | MemberExpression | undefined } +export function parseExprWithRewriteClass( + code: string, + loc: SourceLocation, + context: TransformContext, + node: Expression +) { + // a?1:0 + return parseExpr( + rewriteExpression( + createSimpleExpression(code, false, loc), + context, + !isUndefined(node) + ? conditionalExpression(node, numericLiteral(1), numericLiteral(0)) + : node + ), + context + ) as Identifier | MemberExpression | undefined +} + export function rewriteExpression( node: ExpressionNode, context: TransformContext, diff --git a/packages/uni-mp-vue/dist/vue.runtime.esm.js b/packages/uni-mp-vue/dist/vue.runtime.esm.js index ed4ee522cbd797ab2c5caaa1d000646c73c2b829..c61a3cfbe106cf09e0c324c626659f7073f0f18c 100644 --- a/packages/uni-mp-vue/dist/vue.runtime.esm.js +++ b/packages/uni-mp-vue/dist/vue.runtime.esm.js @@ -4548,6 +4548,13 @@ function getMPInstanceData(instance, keys) { return ret; } function patch(instance, data) { + if (!data) { + return; + } + // 序列化 + pauseTracking(); + data = JSON.parse(JSON.stringify(data)); + resetTracking(); const ctx = instance.ctx; const mpType = ctx.mpType; if (mpType === 'page' || mpType === 'component') { diff --git a/packages/uni-mp-vue/lib/vue.runtime.esm.js b/packages/uni-mp-vue/lib/vue.runtime.esm.js index 6419730549356add459ff20bb1e376659c6ecd93..8ffc847e564a7966309a6b8c9ea09b83ebf62019 100644 --- a/packages/uni-mp-vue/lib/vue.runtime.esm.js +++ b/packages/uni-mp-vue/lib/vue.runtime.esm.js @@ -4480,6 +4480,13 @@ function getMPInstanceData(instance, keys) { return ret; } function patch(instance, data) { + if (!data) { + return; + } + // 序列化 + pauseTracking(); + data = JSON.parse(JSON.stringify(data)); + resetTracking(); const ctx = instance.ctx; const mpType = ctx.mpType; if (mpType === 'page' || mpType === 'component') {