Skip to content

Commit e2daa54

Browse files
committed
fix(compiler-core): remove child node key validation for template vFor (Fix #5360)
Remove the validation that prevents child nodes of templates that contain a v-for directive from having keys. Fix #5360
1 parent 15adf25 commit e2daa54

File tree

3 files changed

+0
-49
lines changed

3 files changed

+0
-49
lines changed

packages/compiler-core/__tests__/transforms/vFor.spec.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -265,35 +265,6 @@ describe('compiler: v-for', () => {
265265
)
266266
})
267267

268-
test('<template v-for> key placement', () => {
269-
const onError = jest.fn()
270-
parseWithForTransform(
271-
`
272-
<template v-for="item in items">
273-
<div :key="item.id"/>
274-
</template>`,
275-
{ onError }
276-
)
277-
278-
expect(onError).toHaveBeenCalledTimes(1)
279-
expect(onError).toHaveBeenCalledWith(
280-
expect.objectContaining({
281-
code: ErrorCodes.X_V_FOR_TEMPLATE_KEY_PLACEMENT
282-
})
283-
)
284-
285-
// should not warn on nested v-for keys
286-
parseWithForTransform(
287-
`
288-
<template v-for="item in items">
289-
<div v-for="c in item.children" :key="c.id"/>
290-
</template>`,
291-
{ onError }
292-
)
293-
expect(onError).toHaveBeenCalledTimes(1)
294-
})
295-
})
296-
297268
describe('source location', () => {
298269
test('value & source', () => {
299270
const source = '<span v-for="item in items" />'

packages/compiler-core/src/errors.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ export const enum ErrorCodes {
7676
X_V_ELSE_NO_ADJACENT_IF,
7777
X_V_FOR_NO_EXPRESSION,
7878
X_V_FOR_MALFORMED_EXPRESSION,
79-
X_V_FOR_TEMPLATE_KEY_PLACEMENT,
8079
X_V_BIND_NO_EXPRESSION,
8180
X_V_ON_NO_EXPRESSION,
8281
X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET,
@@ -152,7 +151,6 @@ export const errorMessages: Record<ErrorCodes, string> = {
152151
[ErrorCodes.X_V_ELSE_NO_ADJACENT_IF]: `v-else/v-else-if has no adjacent v-if or v-else-if.`,
153152
[ErrorCodes.X_V_FOR_NO_EXPRESSION]: `v-for is missing expression.`,
154153
[ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression.`,
155-
[ErrorCodes.X_V_FOR_TEMPLATE_KEY_PLACEMENT]: `<template v-for> key should be placed on the <template> tag.`,
156154
[ErrorCodes.X_V_BIND_NO_EXPRESSION]: `v-bind is missing expression.`,
157155
[ErrorCodes.X_V_ON_NO_EXPRESSION]: `v-on is missing expression.`,
158156
[ErrorCodes.X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET]: `Unexpected custom directive on <slot> outlet.`,

packages/compiler-core/src/transforms/vFor.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,6 @@ export const transformFor = createStructuralDirectiveTransform(
117117
let childBlock: BlockCodegenNode
118118
const { children } = forNode
119119

120-
// check <template v-for> key placement
121-
if ((__DEV__ || !__BROWSER__) && isTemplate) {
122-
node.children.some(c => {
123-
if (c.type === NodeTypes.ELEMENT) {
124-
const key = findProp(c, 'key')
125-
if (key) {
126-
context.onError(
127-
createCompilerError(
128-
ErrorCodes.X_V_FOR_TEMPLATE_KEY_PLACEMENT,
129-
key.loc
130-
)
131-
)
132-
return true
133-
}
134-
}
135-
})
136-
}
137-
138120
const needFragmentWrapper =
139121
children.length !== 1 || children[0].type !== NodeTypes.ELEMENT
140122
const slotOutlet = isSlotOutlet(node)

0 commit comments

Comments
 (0)