From ff89c56caaf07b4961637dfa3062c0132812ddb4 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Tue, 21 Dec 2021 17:14:22 +0800 Subject: [PATCH] fix(mp): component with v-else (#3096) --- packages/uni-mp-compiler/__tests__/component.spec.ts | 8 ++++++++ packages/uni-mp-compiler/__tests__/vIf.spec.ts | 2 +- .../uni-mp-compiler/src/transforms/transformComponent.ts | 5 ++++- .../uni-mp-compiler/src/transforms/transformIdentifier.ts | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/uni-mp-compiler/__tests__/component.spec.ts b/packages/uni-mp-compiler/__tests__/component.spec.ts index 5b6c2049c..81d006cb0 100644 --- a/packages/uni-mp-compiler/__tests__/component.spec.ts +++ b/packages/uni-mp-compiler/__tests__/component.spec.ts @@ -104,9 +104,17 @@ describe('compiler: transform component', () => { ``, `(_ctx, _cache) => { return { a: _ctx.b, b: _ctx.d, c: _n(_ctx.f), d: _s(_ctx.h), e: _o(_ctx.i), f: _ctx.o, g: _ctx.r, h: _ctx.t, i: _o($event => _ctx.j = $event), j: _o($event => _ctx.k = $event), k: _p({ ['prop-a']: 'l', ['prop-b']: _ctx.m, first: _ctx.j, last: _ctx.k }) } +}` + ) + assert( + ``, + ``, + `(_ctx, _cache) => { + return _e({ a: _ctx.ok }, _ctx.ok ? { b: _p({ accordion: true }) } : { c: _p({ accordion: true }) }) }` ) }) + test(`mini program component`, () => { const filename = 'pages/vant/vant' addMiniProgramPageJson(filename, { diff --git a/packages/uni-mp-compiler/__tests__/vIf.spec.ts b/packages/uni-mp-compiler/__tests__/vIf.spec.ts index 52d844adb..bdfc15b44 100644 --- a/packages/uni-mp-compiler/__tests__/vIf.spec.ts +++ b/packages/uni-mp-compiler/__tests__/vIf.spec.ts @@ -179,7 +179,7 @@ describe(`compiler: v-if`, () => { test(`v-for + v-if + v-else`, () => { assert( ``, - ``, + ``, `(_ctx, _cache) => { return { a: _f(_ctx.items, (item, k0, i0) => { return _ctx.ok ? { a: '2a9ec0b0-0' + '-' + i0 } : { b: '2a9ec0b0-1' + '-' + i0, c: _p({ title: item.title }) }; }), b: _ctx.ok } }` diff --git a/packages/uni-mp-compiler/src/transforms/transformComponent.ts b/packages/uni-mp-compiler/src/transforms/transformComponent.ts index b71b89ef1..647609b10 100644 --- a/packages/uni-mp-compiler/src/transforms/transformComponent.ts +++ b/packages/uni-mp-compiler/src/transforms/transformComponent.ts @@ -3,6 +3,7 @@ import { ComponentNode, createSimpleExpression, DirectiveNode, + ElementNode, isSimpleIdentifier, isStaticExp, NodeTypes, @@ -34,6 +35,7 @@ import { } from '@babel/types' import { RENDER_PROPS } from '../runtimeHelpers' import { parseExpr } from '../ast' +import { isIfElementNode } from './vIf' export const transformComponent: NodeTransform = (node, context) => { if (!isUserComponent(node, context as any)) { @@ -229,6 +231,7 @@ export function isPropsBinding({ arg }: DirectiveNode) { export function rewritePropsBinding( dir: DirectiveNode, + node: ElementNode, context: TransformContext ) { dir.exp = createSimpleExpression( @@ -239,6 +242,6 @@ export function rewritePropsBinding( dir.loc, context )! - ) + ) + (isIfElementNode(node) && node.vIf.name === 'else' ? `||''` : '') ) } diff --git a/packages/uni-mp-compiler/src/transforms/transformIdentifier.ts b/packages/uni-mp-compiler/src/transforms/transformIdentifier.ts index 7986f3143..9467d8c18 100644 --- a/packages/uni-mp-compiler/src/transforms/transformIdentifier.ts +++ b/packages/uni-mp-compiler/src/transforms/transformIdentifier.ts @@ -81,7 +81,7 @@ export const transformIdentifier: NodeTransform = (node, context) => { hasStyleBinding = true rewriteStyle(i, dir, props, context) } else if (isPropsBinding(dir)) { - rewritePropsBinding(dir, context) + rewritePropsBinding(dir, node, context) } else { dir.exp = rewriteExpression(exp, context) } -- GitLab