Skip to content

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

Merged
merged 6 commits into from
Sep 1, 2017

Conversation

HerringtonDarkholme
Copy link
Member

@HerringtonDarkholme HerringtonDarkholme commented Aug 25, 2017

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

Fix vuejs#164, don't report error when nested v-for refers iterator.
Do report error when nested v-for doesn't refer iterator.
*/
function checkKey (context, vFor, element) {
function checkKey (context, vFor, element, isChild) {
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

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

labeled as optional

@mysticatea
Copy link
Member

mysticatea commented Aug 26, 2017

Thank you for contributing!

I think that this fix should be recursive. The v-for might be nested 3 times or more.
[email protected] has the relationship between variables and references, I think we can use it. For example:

/**
 * 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
  })
}

@mysticatea
Copy link
Member

mysticatea commented Aug 26, 2017

Test cases:

valid

<template v-for="x in xs">
  <template v-for="y in x.ys">
    <li v-for="z in y.zs" :key="z.id">
      123
    </li>
  </template>
</template>

https://jsfiddle.net/5s00Ln4t/1/

invalid

<template v-for="x in xs">
  <template v-for="y in a.ys">
    <li v-for="z in y.zs" :key="z.id">
      123
    </li>
  </template>
</template>
<template v-for="x in xs">
  <template v-for="y in x.ys">
    <li v-for="z in a.zs" :key="z.id">
      123
    </li>
  </template>
</template>
<template v-for="x in xs">
  <template v-for="y in x.ys">
    <li v-for="z in x.zs" :key="z.id">
      123
    </li>
  </template>
</template>

@HerringtonDarkholme
Copy link
Member Author

Hmm, recursion might be too heavy. I will try some other solution.

@HerringtonDarkholme
Copy link
Member Author

HerringtonDarkholme commented Aug 26, 2017

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.

Copy link
Member

@mysticatea mysticatea left a 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!

@mysticatea mysticatea merged commit 85137c9 into vuejs:master Sep 1, 2017
@HerringtonDarkholme
Copy link
Member Author

Thanks!

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.

3 participants