no-use-v-if-with-v-for.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @author Yosuke Ota
  3. *
  4. * issue https://github.com/vuejs/eslint-plugin-vue/issues/403
  5. * Style guide: https://vuejs.org/v2/style-guide/#Avoid-v-if-with-v-for-essential
  6. *
  7. * I implemented it with reference to `no-confusing-v-for-v-if`
  8. */
  9. 'use strict'
  10. // ------------------------------------------------------------------------------
  11. // Requirements
  12. // ------------------------------------------------------------------------------
  13. const utils = require('../utils')
  14. // ------------------------------------------------------------------------------
  15. // Helpers
  16. // ------------------------------------------------------------------------------
  17. /**
  18. * Check whether the given `v-if` node is using the variable which is defined by the `v-for` directive.
  19. * @param {ASTNode} vIf The `v-if` attribute node to check.
  20. * @returns {boolean} `true` if the `v-if` is using the variable which is defined by the `v-for` directive.
  21. */
  22. function isUsingIterationVar (vIf) {
  23. return !!getVForUsingIterationVar(vIf)
  24. }
  25. function getVForUsingIterationVar (vIf) {
  26. const element = vIf.parent.parent
  27. for (var i = 0; i < vIf.value.references.length; i++) {
  28. const reference = vIf.value.references[i]
  29. const targetVFor = element.variables.find(variable =>
  30. variable.id.name === reference.id.name &&
  31. variable.kind === 'v-for'
  32. )
  33. if (targetVFor) {
  34. return targetVFor.id.parent
  35. }
  36. }
  37. return undefined
  38. }
  39. // ------------------------------------------------------------------------------
  40. // Rule Definition
  41. // ------------------------------------------------------------------------------
  42. module.exports = {
  43. meta: {
  44. type: 'suggestion',
  45. docs: {
  46. description: 'disallow use v-if on the same element as v-for',
  47. category: 'essential',
  48. url: 'https://eslint.vuejs.org/rules/no-use-v-if-with-v-for.html'
  49. },
  50. fixable: null,
  51. schema: [{
  52. type: 'object',
  53. properties: {
  54. allowUsingIterationVar: {
  55. type: 'boolean'
  56. }
  57. }
  58. }]
  59. },
  60. create (context) {
  61. const options = context.options[0] || {}
  62. const allowUsingIterationVar = options.allowUsingIterationVar === true // default false
  63. return utils.defineTemplateBodyVisitor(context, {
  64. "VAttribute[directive=true][key.name='if']" (node) {
  65. const element = node.parent.parent
  66. if (utils.hasDirective(element, 'for')) {
  67. if (isUsingIterationVar(node)) {
  68. if (!allowUsingIterationVar) {
  69. const vFor = getVForUsingIterationVar(node)
  70. context.report({
  71. node,
  72. loc: node.loc,
  73. message: "The '{{iteratorName}}' variable inside 'v-for' directive should be replaced with a computed property that returns filtered array instead. You should not mix 'v-for' with 'v-if'.",
  74. data: {
  75. iteratorName: vFor.right.name
  76. }
  77. })
  78. }
  79. } else {
  80. context.report({
  81. node,
  82. loc: node.loc,
  83. message: "This 'v-if' should be moved to the wrapper element."
  84. })
  85. }
  86. }
  87. }
  88. })
  89. }
  90. }