no-v-for-template-key.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * @author Yosuke Ota
  3. */
  4. 'use strict'
  5. const utils = require('../utils')
  6. // ------------------------------------------------------------------------------
  7. // Rule Definition
  8. // ------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: 'problem',
  12. docs: {
  13. description: 'disallow `key` attribute on `<template v-for>`',
  14. categories: ['essential'],
  15. url: 'https://eslint.vuejs.org/rules/no-v-for-template-key.html'
  16. },
  17. fixable: null,
  18. schema: [],
  19. messages: {
  20. disallow:
  21. "'<template v-for>' cannot be keyed. Place the key on real elements instead."
  22. }
  23. },
  24. /** @param {RuleContext} context */
  25. create(context) {
  26. return utils.defineTemplateBodyVisitor(context, {
  27. /** @param {VDirective} node */
  28. "VElement[name='template'] > VStartTag > VAttribute[directive=true][key.name.name='for']"(
  29. node
  30. ) {
  31. const element = node.parent.parent
  32. const keyNode =
  33. utils.getAttribute(element, 'key') ||
  34. utils.getDirective(element, 'bind', 'key')
  35. if (keyNode) {
  36. context.report({
  37. node: keyNode,
  38. loc: keyNode.loc,
  39. messageId: 'disallow'
  40. })
  41. }
  42. }
  43. })
  44. }
  45. }