dx(compiler-core): warn on <template v-for> key misplacement

Note: the behavior is different from Vue 2. `<template v-for>` are compiled
into an array of Fragment vnodes so the key should be placed the `<template>`
for v-for to use it for diffing.
This commit is contained in:
Evan You
2020-08-04 12:20:32 -04:00
parent de0c8a7e3e
commit b0d01e9db9
3 changed files with 50 additions and 2 deletions

View File

@@ -263,6 +263,34 @@ describe('compiler: v-for', () => {
})
)
})
test('<template v-for> key placement', () => {
const onError = jest.fn()
parseWithForTransform(
`
<template v-for="item in items">
<div :key="item.id"/>
</template>`,
{ onError }
)
expect(onError).toHaveBeenCalledTimes(1)
expect(onError).toHaveBeenCalledWith(
expect.objectContaining({
code: ErrorCodes.X_V_FOR_TEMPLATE_KEY_PLACEMENT
})
)
// should not warn on nested v-for keys
parseWithForTransform(
`
<template v-for="item in items">
<div v-for="c in item.children" :key="c.id"/>
</template>`,
{ onError }
)
expect(onError).toHaveBeenCalledTimes(1)
})
})
describe('source location', () => {