From: Evan You Date: Wed, 28 Sep 2022 02:59:13 +0000 (+0800) Subject: refactor(compiler-core): extract props merging helper X-Git-Tag: v3.2.40~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f402d416b8883757e5d6b9d60b7a1591346657a2;p=thirdparty%2Fvuejs%2Fcore.git refactor(compiler-core): extract props merging helper --- diff --git a/packages/compiler-core/src/transforms/transformElement.ts b/packages/compiler-core/src/transforms/transformElement.ts index f52c6bf07e..7b53b24822 100644 --- a/packages/compiler-core/src/transforms/transformElement.ts +++ b/packages/compiler-core/src/transforms/transformElement.ts @@ -413,6 +413,16 @@ export function buildProps( let hasVnodeHook = false const dynamicPropNames: string[] = [] + const pushMergeArg = (arg?: PropsExpression) => { + if (properties.length) { + mergeArgs.push( + createObjectExpression(dedupeProperties(properties), elementLoc) + ) + properties = [] + } + if (arg) mergeArgs.push(arg) + } + const analyzePatchFlag = ({ key, value }: Property) => { if (isStaticExp(key)) { const name = key.content @@ -590,13 +600,9 @@ export function buildProps( if (!arg && (isVBind || isVOn)) { hasDynamicKeys = true if (exp) { - if (properties.length) { - mergeArgs.push( - createObjectExpression(dedupeProperties(properties), elementLoc) - ) - properties = [] - } if (isVBind) { + // have to merge early for compat build check + pushMergeArg() if (__COMPAT__) { // 2.x v-bind object order compat if (__DEV__) { @@ -643,7 +649,7 @@ export function buildProps( mergeArgs.push(exp) } else { // v-on="obj" -> toHandlers(obj) - mergeArgs.push({ + pushMergeArg({ type: NodeTypes.JS_CALL_EXPRESSION, loc, callee: context.helper(TO_HANDLERS), @@ -669,13 +675,7 @@ export function buildProps( const { props, needRuntime } = directiveTransform(prop, node, context) !ssr && props.forEach(analyzePatchFlag) if (isVOn && arg && !isStaticExp(arg)) { - if (properties.length) { - mergeArgs.push( - createObjectExpression(dedupeProperties(properties), elementLoc) - ) - properties = [] - } - mergeArgs.push(createObjectExpression(props, elementLoc)) + pushMergeArg(createObjectExpression(props, elementLoc)) } else { properties.push(...props) } @@ -701,11 +701,8 @@ export function buildProps( // has v-bind="object" or v-on="object", wrap with mergeProps if (mergeArgs.length) { - if (properties.length) { - mergeArgs.push( - createObjectExpression(dedupeProperties(properties), elementLoc) - ) - } + // close up any not-yet-merged props + pushMergeArg() if (mergeArgs.length > 1) { propsExpression = createCallExpression( context.helper(MERGE_PROPS),