Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-core): avoid duplicate keys with v-if #6689

Merged
merged 3 commits into from Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/compiler-core/__tests__/transforms/vIf.spec.ts
Expand Up @@ -628,6 +628,24 @@ describe('compiler: v-if', () => {
expect(branch1.props).toMatchObject(createObjectMatcher({ key: `[0]` }))
})

// #6631
test('avoid duplicate keys', () => {
const {
node: { codegenNode }
} = parseWithIfTransform(`<div v-if="ok" key="custom_key" v-bind="obj"/>`)
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 }
Expand Down
31 changes: 19 additions & 12 deletions packages/compiler-core/src/utils.ts
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -453,6 +446,20 @@ export function injectProp(
}
}

// check existing key to avoid overriding user provided keys
function isAlreadyExists(prop: Property, props: ObjectExpression) {
zhangzhonghe marked this conversation as resolved.
Show resolved Hide resolved
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'
Expand Down