-
-
Notifications
You must be signed in to change notification settings - Fork 681
polish template v-for error report #168
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
Conversation
Fix vuejs#164, don't report error when nested v-for refers iterator. Do report error when nested v-for doesn't refer iterator.
lib/rules/valid-v-for.js
Outdated
*/ | ||
function checkKey (context, vFor, element) { | ||
function checkKey (context, vFor, element, isChild) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checkKey is not always called with 4 parameters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
labeled as optional
Thank you for contributing! I think that this fix should be recursive. The /**
* Find `VElement` node in the ancestor of the given node.
* @param {Node} node The leaf node to find `VElement` node.
* @returns {VElement|null} The found `VElement` node or null.
*/
function getElement (node) {
while (node != null && node.type !== 'VElement') {
node = node.parent
}
return node
}
/**
* Check whether the given attribute is using the variables which are defined by `v-for` directives.
* @param {VDirective} vFor The attribute node of `v-for` to check.
* @param {VDirective} vBindKey The attribute node of `v-bind:key` to check. This can be a nested `v-for` directive.
* @returns {boolean} `true` if the node is using the variables which are defined by `v-for` directives.
*/
function isUsingIterationVar (vFor, vBindKey) {
if (vBindKey.value == null) {
return false
}
const variables = vFor.parent.parent.variables
return vBindKey.value.references.some(reference => {
// Check only references which are resolved to `v-for` variables.
if (reference.variable != null && reference.variable.kind === 'v-for') {
// OK if the resolved variable is defined by the given `v-for` directive.
if (variables.includes(reference.variable)) {
return true
}
// Otherwise, it might be defined by a nested `v-for` directive.
// Check whether the nested `v-for` directive is using the variable of the given `v-for` directive recursively.
const element = getElement(reference.variable.id)
const nestedVFor = element && utils.getDirective(element, 'for')
return nestedVFor != null && isUsingIterationVar(vFor, nestedVFor)
}
return false
})
} |
Test cases: valid
invalid
|
Hmm, recursion might be too heavy. I will try some other solution. |
I have pushed a new checking method. The basic idea is if child has a v-for, only check if it has used parent iterator. If yes, we can delegate other tests to child v-for. If not, we need to apply old checking logic to descendant. This checking is top-down. I think it might be clearer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Thank you!
Thanks! |
Fix #164, don't report error when nested v-for refers iterator.
Do report error when nested v-for doesn't refer iterator.
Supersede #167