@@ -19,34 +19,50 @@ const utils = require('../utils')
19
19
* Check whether the given attribute is using the variables which are defined by `v-for` directives.
20
20
* @param {ASTNode } vFor The attribute node of `v-for` to check.
21
21
* @param {ASTNode } vBindKey The attribute node of `v-bind:key` to check.
22
+ * @param {boolean } isChild If checks on child in template[v-for].
22
23
* @returns {boolean } `true` if the node is using the variables which are defined by `v-for` directives.
23
24
*/
24
- function isUsingIterationVar ( vFor , vBindKey ) {
25
+ function isUsingIterationVar ( vFor , vBindKey , isChild ) {
25
26
if ( vBindKey . value == null ) {
26
27
return false
27
28
}
28
29
const references = vBindKey . value . references
29
30
const variables = vFor . parent . parent . variables
30
-
31
- return references . some ( reference =>
31
+ const used = references . some ( reference =>
32
32
variables . some ( variable =>
33
33
variable . id . name === reference . id . name &&
34
34
variable . kind === 'v-for'
35
35
)
36
36
)
37
+
38
+ if ( ! isChild || used ) {
39
+ return used
40
+ }
41
+ const childFor = utils . getDirective ( vBindKey . parent . parent , 'for' )
42
+ if ( ! childFor ) {
43
+ return false
44
+ }
45
+ const childForRefs = childFor . value . references
46
+ return childForRefs . some ( cref =>
47
+ variables . some ( variable =>
48
+ cref . id . name === variable . id . name &&
49
+ variable . kind === 'v-for'
50
+ )
51
+ )
37
52
}
38
53
39
54
/**
40
55
* Check the given element about `v-bind:key` attributes.
41
56
* @param {RuleContext } context The rule context to report.
42
57
* @param {ASTNode } vFor The attribute node of `v-for` to check.
43
58
* @param {ASTNode } element The element node to check.
59
+ * @param {boolean } isChild If element is a child of template[v-for].
44
60
*/
45
- function checkKey ( context , vFor , element ) {
61
+ function checkKey ( context , vFor , element , isChild ) {
46
62
if ( element . name === 'template' ) {
47
63
for ( const child of element . children ) {
48
64
if ( child . type === 'VElement' ) {
49
- checkKey ( context , vFor , child )
65
+ checkKey ( context , vFor , child , true )
50
66
}
51
67
}
52
68
return
@@ -61,7 +77,7 @@ function checkKey (context, vFor, element) {
61
77
message : "Custom elements in iteration require 'v-bind:key' directives."
62
78
} )
63
79
}
64
- if ( vBindKey != null && ! isUsingIterationVar ( vFor , vBindKey ) ) {
80
+ if ( vBindKey != null && ! isUsingIterationVar ( vFor , vBindKey , isChild ) ) {
65
81
context . report ( {
66
82
node : vBindKey ,
67
83
loc : vBindKey . loc ,
0 commit comments