Skip to content

Commit

Permalink
chore: improve codegen for single line render effect with return value
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Dec 7, 2024
1 parent d9bb2fd commit 29dff1f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/compiler-vapor/src/generators/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}`,
Expand All @@ -77,7 +86,7 @@ export function genSetProp(
? 'true'
: undefined,
),
...(prevValueName ? [`)`] : []),
...(prevValueName && needWrap ? [`)`] : []),
]
}

Expand Down

0 comments on commit 29dff1f

Please sign in to comment.