Skip to content

Commit 5f6ef15

Browse files
HcySunYangyyx990803
authored andcommitted
polish: warn sequential index on <transition-group> (#8748)
1 parent 5624278 commit 5f6ef15

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/compiler/parser/index.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,20 @@ export function processElement (element: ASTElement, options: CompilerOptions) {
331331
function processKey (el) {
332332
const exp = getBindingAttr(el, 'key')
333333
if (exp) {
334-
if (process.env.NODE_ENV !== 'production' && el.tag === 'template') {
335-
warn(`<template> cannot be keyed. Place the key on real elements instead.`)
334+
if (process.env.NODE_ENV !== 'production') {
335+
if (el.tag === 'template') {
336+
warn(`<template> cannot be keyed. Place the key on real elements instead.`)
337+
}
338+
if (el.for) {
339+
const iterator = el.iterator2 || el.iterator1
340+
const parent = el.parent
341+
if (iterator && iterator === exp && parent && parent.tag === 'transition-group') {
342+
warn(
343+
`Do not use v-for index as key on <transtion-group> children, ` +
344+
`this is the same as not using keys.`
345+
)
346+
}
347+
}
336348
}
337349
el.key = exp
338350
}

test/unit/modules/compiler/parser.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,17 @@ describe('parser', () => {
242242
expect('<template> cannot be keyed').toHaveBeenWarned()
243243
})
244244

245+
it('warn the child of the <transition-group> component has sequential index', () => {
246+
parse(`
247+
<div>
248+
<transition-group>
249+
<i v-for="(o, i) of arr" :key="i"></i>
250+
</transition-group>
251+
</div>
252+
`, baseOptions)
253+
expect('Do not use v-for index as key on <transtion-group> children').toHaveBeenWarned()
254+
})
255+
245256
it('v-pre directive', () => {
246257
const ast = parse('<div v-pre id="message1"><p>{{msg}}</p></div>', baseOptions)
247258
expect(ast.pre).toBe(true)

0 commit comments

Comments
 (0)