Skip to content

Commit f9600d5

Browse files
refactor to return early
1 parent acb6be2 commit f9600d5

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

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

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,39 @@ const checkForKey = (
6767
conditionalFamilies
6868
) => {
6969
if (
70-
node.parent &&
71-
node.parent.type === 'VElement' &&
72-
hasConditionalRenderedSiblings(node, componentName)
70+
!node.parent ||
71+
node.parent.type !== 'VElement' ||
72+
!hasConditionalRenderedSiblings(node, componentName)
7373
) {
74-
const conditionalFamily = conditionalFamilies.get(node.parent)
74+
return
75+
}
7576

76-
if (conditionalFamily && !utils.hasAttribute(node, 'key')) {
77-
const needsKey =
78-
conditionalFamily.if === node ||
79-
conditionalFamily.else === node ||
80-
conditionalFamily.elseIf.includes(node)
77+
const conditionalFamily = conditionalFamilies.get(node.parent)
8178

82-
if (needsKey) {
83-
context.report({
84-
node: node.startTag,
85-
loc: node.startTag.loc,
86-
messageId: 'requireKey',
87-
data: { componentName },
88-
fix(fixer) {
89-
const afterComponentNamePosition =
90-
node.startTag.range[0] + componentName.length + 1
91-
return fixer.insertTextBeforeRange(
92-
[afterComponentNamePosition, afterComponentNamePosition],
93-
` key="${uniqueKey}"`
94-
)
95-
}
96-
})
79+
if (!conditionalFamily || utils.hasAttribute(node, 'key')) {
80+
return
81+
}
82+
83+
const needsKey =
84+
conditionalFamily.if === node ||
85+
conditionalFamily.else === node ||
86+
conditionalFamily.elseIf.includes(node)
87+
88+
if (needsKey) {
89+
context.report({
90+
node: node.startTag,
91+
loc: node.startTag.loc,
92+
messageId: 'requireKey',
93+
data: { componentName },
94+
fix(fixer) {
95+
const afterComponentNamePosition =
96+
node.startTag.range[0] + componentName.length + 1
97+
return fixer.insertTextBeforeRange(
98+
[afterComponentNamePosition, afterComponentNamePosition],
99+
` key="${uniqueKey}"`
100+
)
97101
}
98-
}
102+
})
99103
}
100104
}
101105

0 commit comments

Comments
 (0)