-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathv-if-else-key.js
317 lines (286 loc) · 10.5 KB
/
v-if-else-key.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* @author Felipe Melendez
* See LICENSE file in root directory for full license.
*/
'use strict'
// =============================================================================
// Requirements
// =============================================================================
const utils = require('../utils')
const casing = require('../utils/casing')
// =============================================================================
// Rule Helpers
// =============================================================================
/**
* A conditional family is made up of a group of repeated components that are conditionally rendered
* using v-if, v-else-if, and v-else.
*
* @typedef {Object} ConditionalFamily
* @property {VElement} if - The node associated with the 'v-if' directive.
* @property {VElement[]} elseIf - An array of nodes associated with 'v-else-if' directives.
* @property {VElement | null} else - The node associated with the 'v-else' directive, or null if there isn't one.
*/
/**
* Checks if a given node has sibling nodes of the same type that are also conditionally rendered.
* This is used to determine if multiple instances of the same component are being conditionally
* rendered within the same parent scope.
*
* @param {VElement} node - The Vue component node to check for conditional rendering siblings.
* @param {string} componentName - The name of the component to check for sibling instances.
* @returns {boolean} True if there are sibling nodes of the same type and conditionally rendered, false otherwise.
*/
const hasConditionalRenderedSiblings = (node, componentName) => {
if (!node.parent || node.parent.type !== 'VElement') {
return false
}
return node.parent.children.some(
(sibling) =>
sibling !== node &&
sibling.type === 'VElement' &&
sibling.rawName === componentName &&
hasConditionalDirective(sibling)
)
}
/**
* Checks for the presence of a 'key' attribute in the given node. If the 'key' attribute is missing
* and the node is part of a conditional family a report is generated.
* The fix proposed adds a unique key based on the component's name and count,
* following the format '${kebabCase(componentName)}-${componentCount}', e.g., 'some-component-2'.
*
* @param {VElement} node - The Vue component node to check for a 'key' attribute.
* @param {RuleContext} context - The rule's context object, used for reporting.
* @param {string} componentName - Name of the component.
* @param {string} uniqueKey - A unique key for the repeated component, used for the fix.
* @param {Map<VElement, ConditionalFamily>} conditionalFamilies - Map of conditionally rendered components and their respective conditional directives.
*/
const checkForKey = (
node,
context,
componentName,
uniqueKey,
conditionalFamilies
) => {
if (
!node.parent ||
node.parent.type !== 'VElement' ||
!hasConditionalRenderedSiblings(node, componentName)
) {
return
}
const conditionalFamily = conditionalFamilies.get(node.parent)
if (!conditionalFamily || utils.hasAttribute(node, 'key')) {
return
}
const needsKey =
conditionalFamily.if === node ||
conditionalFamily.else === node ||
conditionalFamily.elseIf.includes(node)
if (needsKey) {
context.report({
node: node.startTag,
loc: node.startTag.loc,
messageId: 'requireKey',
data: { componentName },
fix(fixer) {
const afterComponentNamePosition =
node.startTag.range[0] + componentName.length + 1
return fixer.insertTextBeforeRange(
[afterComponentNamePosition, afterComponentNamePosition],
` key="${uniqueKey}"`
)
}
})
}
}
/**
* Checks for the presence of conditional directives in the given node.
*
* @param {VElement} node - The node to check for conditional directives.
* @returns {boolean} Returns true if a conditional directive is found in the node or its parents,
* false otherwise.
*/
const hasConditionalDirective = (node) =>
utils.hasDirective(node, 'if') ||
utils.hasDirective(node, 'else-if') ||
utils.hasDirective(node, 'else')
// =============================================================================
// Rule Definition
// =============================================================================
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description:
'require key attribute for conditionally rendered repeated components',
categories: null,
url: 'https://eslint.vuejs.org/rules/v-if-else-key.html'
},
// eslint-disable-next-line eslint-plugin/require-meta-fixable -- fixer is not recognized
fixable: 'code',
schema: [],
messages: {
requireKey:
"Conditionally rendered repeated component '{{componentName}}' expected to have a 'key' attribute."
}
},
/**
* Creates and returns a rule object which checks usage of repeated components. If a component
* is used more than once, it checks for the presence of a key.
*
* @param {RuleContext} context - The context object.
* @returns {Object} A dictionary of functions to be called on traversal of the template body by
* the eslint parser.
*/
create(context) {
/**
* Map to store conditionally rendered components and their respective conditional directives.
*
* @type {Map<VElement, ConditionalFamily>}
*/
const conditionalFamilies = new Map()
/**
* Array of Maps to keep track of components and their usage counts along with the first
* node instance. Each Map represents a different scope level, and maps a component name to
* an object containing the count and a reference to the first node.
*/
/** @type {Map<string, { count: number; firstNode: any }>[]} */
const componentUsageStack = [new Map()]
/**
* Checks if a given node represents a custom component without any conditional directives.
*
* @param {VElement} node - The AST node to check.
* @returns {boolean} True if the node represents a custom component without any conditional directives, false otherwise.
*/
const isCustomComponentWithoutCondition = (node) =>
node.type === 'VElement' &&
utils.isCustomComponent(node) &&
!hasConditionalDirective(node)
/** Set of built-in Vue components that are exempt from the rule. */
/** @type {Set<string>} */
const exemptTags = new Set(['component', 'slot', 'template'])
/** Set to keep track of nodes we've pushed to the stack. */
/** @type {Set<any>} */
const pushedNodes = new Set()
/**
* Creates and returns an object representing a conditional family.
*
* @param {VElement} ifNode - The VElement associated with the 'v-if' directive.
* @returns {ConditionalFamily}
*/
const createConditionalFamily = (ifNode) => ({
if: ifNode,
elseIf: [],
else: null
})
return utils.defineTemplateBodyVisitor(context, {
/**
* Callback to be executed when a Vue element is traversed. This function checks if the
* element is a component, increments the usage count of the component in the
* current scope, and checks for the key directive if the component is repeated.
*
* @param {VElement} node - The traversed Vue element.
*/
VElement(node) {
if (exemptTags.has(node.rawName)) {
return
}
const condition =
utils.getDirective(node, 'if') ||
utils.getDirective(node, 'else-if') ||
utils.getDirective(node, 'else')
if (condition) {
const conditionType = condition.key.name.name
if (node.parent && node.parent.type === 'VElement') {
let conditionalFamily = conditionalFamilies.get(node.parent)
if (!conditionalFamily) {
conditionalFamily = createConditionalFamily(node)
conditionalFamilies.set(node.parent, conditionalFamily)
}
if (conditionalFamily) {
switch (conditionType) {
case 'if': {
conditionalFamily = createConditionalFamily(node)
conditionalFamilies.set(node.parent, conditionalFamily)
break
}
case 'else-if': {
conditionalFamily.elseIf.push(node)
break
}
case 'else': {
conditionalFamily.else = node
break
}
}
}
}
}
if (isCustomComponentWithoutCondition(node)) {
componentUsageStack.push(new Map())
return
}
if (!utils.isCustomComponent(node)) {
return
}
const componentName = node.rawName
const currentScope = componentUsageStack[componentUsageStack.length - 1]
const usageInfo = currentScope.get(componentName) || {
count: 0,
firstNode: null
}
if (hasConditionalDirective(node)) {
// Store the first node if this is the first occurrence
if (usageInfo.count === 0) {
usageInfo.firstNode = node
}
if (usageInfo.count > 0) {
const uniqueKey = `${casing.kebabCase(componentName)}-${
usageInfo.count + 1
}`
checkForKey(
node,
context,
componentName,
uniqueKey,
conditionalFamilies
)
// If this is the second occurrence, also apply a fix to the first occurrence
if (usageInfo.count === 1) {
const uniqueKeyForFirstInstance = `${casing.kebabCase(
componentName
)}-1`
checkForKey(
usageInfo.firstNode,
context,
componentName,
uniqueKeyForFirstInstance,
conditionalFamilies
)
}
}
usageInfo.count += 1
currentScope.set(componentName, usageInfo)
}
componentUsageStack.push(new Map())
pushedNodes.add(node)
},
'VElement:exit'(node) {
if (exemptTags.has(node.rawName)) {
return
}
if (isCustomComponentWithoutCondition(node)) {
componentUsageStack.pop()
return
}
if (!utils.isCustomComponent(node)) {
return
}
if (pushedNodes.has(node)) {
componentUsageStack.pop()
pushedNodes.delete(node)
}
}
})
}
}