Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix siblings and interleaving issues reported in #2342 #2348

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

felipemelendez
Copy link
Contributor

@felipemelendez felipemelendez commented Dec 7, 2023

This PR addresses the bugs reported in #2342

And added tests

@felipemelendez
Copy link
Contributor Author

Hi @FloEdelmann, thank you for reporting #2342, I have hopefully addressed those here, please let me know if I missed something.

@FloEdelmann FloEdelmann linked an issue Dec 7, 2023 that may be closed by this pull request
2 tasks
Copy link
Member

@FloEdelmann FloEdelmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not yet looked into your implementation, but I noticed one wrong test case.

tests/lib/rules/v-if-else-key.js Outdated Show resolved Hide resolved
Comment on lines +39 to +46
const siblings = node.parent.children.filter(
(child) => child.type === 'VElement'
)

return siblings.some(
(sibling) =>
sibling !== node &&
sibling.type === 'VElement' &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need to check the element's type anyway in the some predicate, then we can avoid filtering the list before.

Suggested change
const siblings = node.parent.children.filter(
(child) => child.type === 'VElement'
)
return siblings.some(
(sibling) =>
sibling !== node &&
sibling.type === 'VElement' &&
return node.parent.children.some(
(sibling) =>
sibling !== node &&
sibling.type === 'VElement' &&

Comment on lines +82 to 88
let needsKey = false

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}"`
)
if (node === conditionalFamily.if || node === conditionalFamily.else) {
needsKey = true
} else if (conditionalFamily.elseIf.includes(node)) {
needsKey = true
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace with a single const:

const needsKey =
  conditionalFamily.if === node ||
  conditionalFamily.else === node ||
  conditionalFamily.elseIf.includes(node)

Comment on lines +268 to +270
// Reset if family already has an 'if' directive
usageInfo.count = 1
usageInfo.firstNode = node
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work for the following case? Please also add this as a test case:

<ComponentA v-if="foo" />
<ComponentA v-else />

<ComponentA v-if="bar" />
<ComponentA v-else-if="baz" />
<ComponentA v-else />

An error should be reported for all of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vue/v-if-else-key false-positives and false-negatives
2 participants