forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid-v-for.js
189 lines (171 loc) · 5.39 KB
/
valid-v-for.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* @author Toru Nagashima
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
/**
* Check whether the given attribute is using the variables which are defined by `v-for` directives.
* @param {ASTNode} vFor The attribute node of `v-for` to check.
* @param {ASTNode} vBindKey The attribute node of `v-bind:key` to check.
* @param {boolean} isChild If checks on child in template[v-for].
* @returns {boolean} `true` if the node is using the variables which are defined by `v-for` directives.
*/
function isUsingIterationVar (vFor, vBindKey, isChild) {
if (vBindKey.value == null) {
return false
}
const references = vBindKey.value.references
const variables = vFor.parent.parent.variables
const used = references.some(reference =>
variables.some(variable =>
variable.id.name === reference.id.name &&
variable.kind === 'v-for'
)
)
if (!isChild || used) {
return used
}
const childFor = utils.getDirective(vBindKey.parent.parent, 'for')
if (!childFor) {
return false
}
const childForRefs = childFor.value.references
return childForRefs.some(cref =>
variables.some(variable =>
cref.id.name === variable.id.name &&
variable.kind === 'v-for'
)
)
}
/**
* Check the given element about `v-bind:key` attributes.
* @param {RuleContext} context The rule context to report.
* @param {ASTNode} vFor The attribute node of `v-for` to check.
* @param {ASTNode} element The element node to check.
* @param {boolean} isChild If element is a child of template[v-for].
*/
function checkKey (context, vFor, element, isChild) {
if (element.name === 'template') {
for (const child of element.children) {
if (child.type === 'VElement') {
checkKey(context, vFor, child, true)
}
}
return
}
const vBindKey = utils.getDirective(element, 'bind', 'key')
if (utils.isCustomComponent(element) && vBindKey == null) {
context.report({
node: element.startTag,
loc: element.startTag.loc,
message: "Custom elements in iteration require 'v-bind:key' directives."
})
}
if (vBindKey != null && !isUsingIterationVar(vFor, vBindKey, isChild)) {
context.report({
node: vBindKey,
loc: vBindKey.loc,
message: "Expected 'v-bind:key' directive to use the variables which are defined by the 'v-for' directive."
})
}
}
/**
* Creates AST event handlers for valid-v-for.
*
* @param {RuleContext} context - The rule context.
* @returns {Object} AST event handlers.
*/
function create (context) {
const sourceCode = context.getSourceCode()
utils.registerTemplateBodyVisitor(context, {
"VAttribute[directive=true][key.name='for']" (node) {
const element = node.parent.parent
checkKey(context, node, element)
if (node.key.argument) {
context.report({
node,
loc: node.loc,
message: "'v-for' directives require no argument."
})
}
if (node.key.modifiers.length > 0) {
context.report({
node,
loc: node.loc,
message: "'v-for' directives require no modifier."
})
}
if (!utils.hasAttributeValue(node)) {
context.report({
node,
loc: node.loc,
message: "'v-for' directives require that attribute value."
})
return
}
const expr = node.value.expression
if (expr == null) {
return
}
if (expr.type !== 'VForExpression') {
context.report({
node: node.value,
loc: node.value.loc,
message: "'v-for' directives require the special syntax '<alias> in <expression>'."
})
return
}
const lhs = expr.left
const value = lhs[0]
const key = lhs[1]
const index = lhs[2]
if (value === null) {
context.report({
node: value || expr,
loc: value && value.loc,
message: "Invalid alias ''."
})
}
if (key !== undefined && (!key || key.type !== 'Identifier')) {
context.report({
node: key || expr,
loc: key && key.loc,
message: "Invalid alias '{{text}}'.",
data: { text: key ? sourceCode.getText(key) : '' }
})
}
if (index !== undefined && (!index || index.type !== 'Identifier')) {
context.report({
node: index || expr,
loc: index && index.loc,
message: "Invalid alias '{{text}}'.",
data: { text: index ? sourceCode.getText(index) : '' }
})
}
}
})
return {}
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
create,
meta: {
docs: {
description: 'enforce valid `v-for` directives.',
category: 'Possible Errors',
recommended: true
},
fixable: false,
schema: []
}
}