Skip to content

Commit

Permalink
refactor(compiler-core): extract props merging helper
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 28, 2022
1 parent f9d43b9 commit f402d41
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions packages/compiler-core/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__) {
Expand Down Expand Up @@ -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),
Expand All @@ -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)
}
Expand All @@ -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),
Expand Down

0 comments on commit f402d41

Please sign in to comment.