From fc336dac968eb3d125ac4d4cd5e16aa27a6790c9 Mon Sep 17 00:00:00 2001 From: Rairn <958414905@qq.com> Date: Sun, 18 Sep 2022 01:34:16 +0800 Subject: [PATCH 1/3] fix(compiler-core): avoid duplicate keys --- .../__tests__/transforms/vIf.spec.ts | 18 +++++++++++ packages/compiler-core/src/utils.ts | 31 ++++++++++++------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/packages/compiler-core/__tests__/transforms/vIf.spec.ts b/packages/compiler-core/__tests__/transforms/vIf.spec.ts index 5c36885670f..9ccce811486 100644 --- a/packages/compiler-core/__tests__/transforms/vIf.spec.ts +++ b/packages/compiler-core/__tests__/transforms/vIf.spec.ts @@ -628,6 +628,24 @@ describe('compiler: v-if', () => { expect(branch1.props).toMatchObject(createObjectMatcher({ key: `[0]` })) }) + // #6631 + test('avoid duplicate keys', () => { + const { + node: { codegenNode } + } = parseWithIfTransform(`
`) + const branch1 = codegenNode.consequent as VNodeCall + expect(branch1.props).toMatchObject({ + type: NodeTypes.JS_CALL_EXPRESSION, + callee: MERGE_PROPS, + arguments: [ + createObjectMatcher({ + key: 'custom_key' + }), + { content: `obj` } + ] + }) + }) + test('with spaces between branches', () => { const { node: { codegenNode } diff --git a/packages/compiler-core/src/utils.ts b/packages/compiler-core/src/utils.ts index c9e310fe089..4599e5c4965 100644 --- a/packages/compiler-core/src/utils.ts +++ b/packages/compiler-core/src/utils.ts @@ -397,7 +397,10 @@ export function injectProp( // if doesn't override user provided keys const first = props.arguments[0] as string | JSChildNode if (!isString(first) && first.type === NodeTypes.JS_OBJECT_EXPRESSION) { - first.properties.unshift(prop) + // #6631 + if (!isAlreadyExists(prop, first)) { + first.properties.unshift(prop) + } } else { if (props.callee === TO_HANDLERS) { // #2366 @@ -411,17 +414,7 @@ export function injectProp( } !propsWithInjection && (propsWithInjection = props) } else if (props.type === NodeTypes.JS_OBJECT_EXPRESSION) { - let alreadyExists = false - // check existing key to avoid overriding user provided keys - if (prop.key.type === NodeTypes.SIMPLE_EXPRESSION) { - const propKeyName = prop.key.content - alreadyExists = props.properties.some( - p => - p.key.type === NodeTypes.SIMPLE_EXPRESSION && - p.key.content === propKeyName - ) - } - if (!alreadyExists) { + if (!isAlreadyExists(prop, props)) { props.properties.unshift(prop) } propsWithInjection = props @@ -453,6 +446,20 @@ export function injectProp( } } +// check existing key to avoid overriding user provided keys +function isAlreadyExists(prop: Property, props: ObjectExpression) { + let result = false + if (prop.key.type === NodeTypes.SIMPLE_EXPRESSION) { + const propKeyName = prop.key.content + result = props.properties.some( + p => + p.key.type === NodeTypes.SIMPLE_EXPRESSION && + p.key.content === propKeyName + ) + } + return result +} + export function toValidAssetId( name: string, type: 'component' | 'directive' | 'filter' From 0005195591d649cb20c9001725f1f8387699fe59 Mon Sep 17 00:00:00 2001 From: Rairn <958414905@qq.com> Date: Wed, 12 Oct 2022 16:05:51 +0800 Subject: [PATCH 2/3] refactor: rename --- packages/compiler-core/src/utils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/compiler-core/src/utils.ts b/packages/compiler-core/src/utils.ts index 4599e5c4965..3a4690195d8 100644 --- a/packages/compiler-core/src/utils.ts +++ b/packages/compiler-core/src/utils.ts @@ -398,7 +398,7 @@ export function injectProp( const first = props.arguments[0] as string | JSChildNode if (!isString(first) && first.type === NodeTypes.JS_OBJECT_EXPRESSION) { // #6631 - if (!isAlreadyExists(prop, first)) { + if (!isPropExists(prop, first)) { first.properties.unshift(prop) } } else { @@ -414,7 +414,7 @@ export function injectProp( } !propsWithInjection && (propsWithInjection = props) } else if (props.type === NodeTypes.JS_OBJECT_EXPRESSION) { - if (!isAlreadyExists(prop, props)) { + if (!isPropExists(prop, props)) { props.properties.unshift(prop) } propsWithInjection = props @@ -447,7 +447,7 @@ export function injectProp( } // check existing key to avoid overriding user provided keys -function isAlreadyExists(prop: Property, props: ObjectExpression) { +function isPropExists(prop: Property, props: ObjectExpression) { let result = false if (prop.key.type === NodeTypes.SIMPLE_EXPRESSION) { const propKeyName = prop.key.content From e4d81f46a768b9e6ace443c4589487fb43d04dd3 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 8 Nov 2022 10:57:54 +0800 Subject: [PATCH 3/3] refactor: naming --- packages/compiler-core/src/utils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/compiler-core/src/utils.ts b/packages/compiler-core/src/utils.ts index 3a4690195d8..6a6b03a2ead 100644 --- a/packages/compiler-core/src/utils.ts +++ b/packages/compiler-core/src/utils.ts @@ -398,7 +398,7 @@ export function injectProp( const first = props.arguments[0] as string | JSChildNode if (!isString(first) && first.type === NodeTypes.JS_OBJECT_EXPRESSION) { // #6631 - if (!isPropExists(prop, first)) { + if (!hasProp(prop, first)) { first.properties.unshift(prop) } } else { @@ -414,7 +414,7 @@ export function injectProp( } !propsWithInjection && (propsWithInjection = props) } else if (props.type === NodeTypes.JS_OBJECT_EXPRESSION) { - if (!isPropExists(prop, props)) { + if (!hasProp(prop, props)) { props.properties.unshift(prop) } propsWithInjection = props @@ -447,7 +447,7 @@ export function injectProp( } // check existing key to avoid overriding user provided keys -function isPropExists(prop: Property, props: ObjectExpression) { +function hasProp(prop: Property, props: ObjectExpression) { let result = false if (prop.key.type === NodeTypes.SIMPLE_EXPRESSION) { const propKeyName = prop.key.content