Skip to content

Commit 8b9883c

Browse files
fix and update per review
1 parent e978c9d commit 8b9883c

File tree

1 file changed

+28
-43
lines changed

1 file changed

+28
-43
lines changed

lib/rules/v-if-else-key.js

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ const casing = require('../utils/casing')
3636
*/
3737
const hasConditionalRenderedSiblings = (node, componentName) => {
3838
if (node.parent && node.parent.type === 'VElement') {
39-
const siblings = node.parent.children.filter(
40-
(child) => child.type === 'VElement'
41-
)
42-
43-
return siblings.some(
39+
return node.parent.children.some(
4440
(sibling) =>
4541
sibling !== node &&
4642
sibling.type === 'VElement' &&
@@ -220,7 +216,7 @@ module.exports = {
220216
if (node.parent && node.parent.type === 'VElement') {
221217
let conditionalFamily = conditionalFamilies.get(node.parent)
222218

223-
if (conditionType === 'if') {
219+
if (conditionType === 'if' && !conditionalFamily) {
224220
conditionalFamily = createConditionalFamily(node)
225221
conditionalFamilies.set(node.parent, conditionalFamily)
226222
}
@@ -253,55 +249,44 @@ module.exports = {
253249
const currentScope = componentUsageStack[componentUsageStack.length - 1]
254250
const usageInfo = currentScope.get(componentName) || {
255251
count: 0,
256-
firstNode: null,
257-
hasIf: false
252+
firstNode: null
258253
}
259254

260-
const isIfDirective = utils.getDirective(node, 'if') !== null
261-
const isConditional =
262-
isIfDirective ||
263-
utils.getDirective(node, 'else-if') !== null ||
264-
utils.getDirective(node, 'else') !== null
265-
266-
if (isConditional) {
267-
if (isIfDirective && usageInfo.hasIf) {
268-
// Reset if family already has an 'if' directive
269-
usageInfo.count = 1
255+
if (hasConditionalDirective(node)) {
256+
// Store the first node if this is the first occurrence
257+
if (usageInfo.count === 0) {
270258
usageInfo.firstNode = node
271-
} else {
272-
usageInfo.hasIf = true
273-
if (usageInfo.count === 0) {
274-
usageInfo.firstNode = node
275-
} else if (usageInfo.count > 0 && usageInfo.firstNode !== node) {
276-
const uniqueKey = `${casing.kebabCase(componentName)}-${
277-
usageInfo.count + 1
278-
}`
259+
}
260+
261+
if (usageInfo.count > 0) {
262+
const uniqueKey = `${casing.kebabCase(componentName)}-${
263+
usageInfo.count + 1
264+
}`
265+
checkForKey(
266+
node,
267+
context,
268+
componentName,
269+
uniqueKey,
270+
conditionalFamilies
271+
)
272+
273+
// If this is the second occurrence, also apply a fix to the first occurrence
274+
if (usageInfo.count === 1) {
275+
const uniqueKeyForFirstInstance = `${casing.kebabCase(
276+
componentName
277+
)}-1`
279278
checkForKey(
280-
node,
279+
usageInfo.firstNode,
281280
context,
282281
componentName,
283-
uniqueKey,
282+
uniqueKeyForFirstInstance,
284283
conditionalFamilies
285284
)
286-
287-
if (usageInfo.count === 1) {
288-
const uniqueKeyForFirstInstance = `${casing.kebabCase(
289-
componentName
290-
)}-1`
291-
checkForKey(
292-
usageInfo.firstNode,
293-
context,
294-
componentName,
295-
uniqueKeyForFirstInstance,
296-
conditionalFamilies
297-
)
298-
}
299285
}
300-
usageInfo.count++
301286
}
287+
usageInfo.count += 1
302288
currentScope.set(componentName, usageInfo)
303289
}
304-
305290
componentUsageStack.push(new Map())
306291
pushedNodes.add(node)
307292
},

0 commit comments

Comments
 (0)