From 29dff1fc95b8d107c3b2b120e9de407c2677c265 Mon Sep 17 00:00:00 2001 From: daiwei Date: Sat, 7 Dec 2024 19:55:34 +0800 Subject: [PATCH] chore: improve codegen for single line render effect with return value --- packages/compiler-vapor/src/generators/prop.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/compiler-vapor/src/generators/prop.ts b/packages/compiler-vapor/src/generators/prop.ts index f7321ad62..1107d4489 100644 --- a/packages/compiler-vapor/src/generators/prop.ts +++ b/packages/compiler-vapor/src/generators/prop.ts @@ -43,8 +43,12 @@ export function genSetProp( oper: SetPropIRNode, context: CodegenContext, ): CodeFragment[] { - const { vaporHelper, shouldCacheRenderEffectDeps, processingRenderEffect } = - context + const { + vaporHelper, + shouldCacheRenderEffectDeps, + processingRenderEffect, + block, + } = context const { prop: { key, values, modifier }, tag, @@ -63,9 +67,14 @@ export function genSetProp( } } + // single-line render effect that needs to return a value, the expression needs to be wrapped + // in parentheses. e.g. _foo === _ctx.foo && (_foo = _setStyle(...)) + const needWrap = block.operation.length === 1 return [ NEWLINE, - ...(prevValueName ? [`(`, `${prevValueName} = `] : []), + ...(prevValueName + ? [needWrap ? `(` : undefined, `${prevValueName} = `] + : []), ...genCall( [vaporHelper(helperName), null], `n${oper.element}`, @@ -77,7 +86,7 @@ export function genSetProp( ? 'true' : undefined, ), - ...(prevValueName ? [`)`] : []), + ...(prevValueName && needWrap ? [`)`] : []), ] }