Skip to content

Commit

Permalink
polish: warn sequential index on <transition-group> (vuejs#8748)
Browse files Browse the repository at this point in the history
  • Loading branch information
HcySunYang authored and hefeng committed Jan 25, 2019
1 parent e9ba169 commit abb7969
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/compiler/parser/index.js
Expand Up @@ -331,8 +331,20 @@ export function processElement (element: ASTElement, options: CompilerOptions) {
function processKey (el) {
const exp = getBindingAttr(el, 'key')
if (exp) {
if (process.env.NODE_ENV !== 'production' && el.tag === 'template') {
warn(`<template> cannot be keyed. Place the key on real elements instead.`)
if (process.env.NODE_ENV !== 'production') {
if (el.tag === 'template') {
warn(`<template> cannot be keyed. Place the key on real elements instead.`)
}
if (el.for) {
const iterator = el.iterator2 || el.iterator1
const parent = el.parent
if (iterator && iterator === exp && parent && parent.tag === 'transition-group') {
warn(
`Do not use v-for index as key on <transtion-group> children, ` +
`this is the same as not using keys.`
)
}
}
}
el.key = exp
}
Expand Down
11 changes: 11 additions & 0 deletions test/unit/modules/compiler/parser.spec.js
Expand Up @@ -242,6 +242,17 @@ describe('parser', () => {
expect('<template> cannot be keyed').toHaveBeenWarned()
})

it('warn the child of the <transition-group> component has sequential index', () => {
parse(`
<div>
<transition-group>
<i v-for="(o, i) of arr" :key="i"></i>
</transition-group>
</div>
`, baseOptions)
expect('Do not use v-for index as key on <transtion-group> children').toHaveBeenWarned()
})

it('v-pre directive', () => {
const ast = parse('<div v-pre id="message1"><p>{{msg}}</p></div>', baseOptions)
expect(ast.pre).toBe(true)
Expand Down

0 comments on commit abb7969

Please sign in to comment.