Skip to content

Commit 0923acc

Browse files
enhance tracking of component usage - focusing on repeated if directives
1 parent f74541d commit 0923acc

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

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

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ module.exports = {
220220
if (node.parent && node.parent.type === 'VElement') {
221221
let conditionalFamily = conditionalFamilies.get(node.parent)
222222

223-
if (conditionType === 'if' && !conditionalFamily) {
223+
if (conditionType === 'if') {
224224
conditionalFamily = createConditionalFamily(node)
225225
conditionalFamilies.set(node.parent, conditionalFamily)
226226
}
@@ -253,44 +253,55 @@ module.exports = {
253253
const currentScope = componentUsageStack[componentUsageStack.length - 1]
254254
const usageInfo = currentScope.get(componentName) || {
255255
count: 0,
256-
firstNode: null
256+
firstNode: null,
257+
hasIf: false
257258
}
258259

259-
if (hasConditionalDirective(node)) {
260-
// Store the first node if this is the first occurrence
261-
if (usageInfo.count === 0) {
262-
usageInfo.firstNode = node
263-
}
264-
265-
if (usageInfo.count > 0) {
266-
const uniqueKey = `${casing.kebabCase(componentName)}-${
267-
usageInfo.count + 1
268-
}`
269-
checkForKey(
270-
node,
271-
context,
272-
componentName,
273-
uniqueKey,
274-
conditionalFamilies
275-
)
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
276265

277-
// If this is the second occurrence, also apply a fix to the first occurrence
278-
if (usageInfo.count === 1) {
279-
const uniqueKeyForFirstInstance = `${casing.kebabCase(
280-
componentName
281-
)}-1`
266+
if (isConditional) {
267+
if (isIfDirective && usageInfo.hasIf) {
268+
// Reset if family already has an 'if' directive
269+
usageInfo.count = 1
270+
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+
}`
282279
checkForKey(
283-
usageInfo.firstNode,
280+
node,
284281
context,
285282
componentName,
286-
uniqueKeyForFirstInstance,
283+
uniqueKey,
287284
conditionalFamilies
288285
)
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+
}
289299
}
300+
usageInfo.count++
290301
}
291-
usageInfo.count += 1
292302
currentScope.set(componentName, usageInfo)
293303
}
304+
294305
componentUsageStack.push(new Map())
295306
pushedNodes.add(node)
296307
},

0 commit comments

Comments
 (0)