valid-v-if.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2017 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. 'use strict'
  7. // ------------------------------------------------------------------------------
  8. // Requirements
  9. // ------------------------------------------------------------------------------
  10. const utils = require('../utils')
  11. // ------------------------------------------------------------------------------
  12. // Rule Definition
  13. // ------------------------------------------------------------------------------
  14. module.exports = {
  15. meta: {
  16. type: 'problem',
  17. docs: {
  18. description: 'enforce valid `v-if` directives',
  19. category: 'essential',
  20. url: 'https://eslint.vuejs.org/rules/valid-v-if.html'
  21. },
  22. fixable: null,
  23. schema: []
  24. },
  25. create (context) {
  26. return utils.defineTemplateBodyVisitor(context, {
  27. "VAttribute[directive=true][key.name='if']" (node) {
  28. const element = node.parent.parent
  29. if (utils.hasDirective(element, 'else')) {
  30. context.report({
  31. node,
  32. loc: node.loc,
  33. message: "'v-if' and 'v-else' directives can't exist on the same element. You may want 'v-else-if' directives."
  34. })
  35. }
  36. if (utils.hasDirective(element, 'else-if')) {
  37. context.report({
  38. node,
  39. loc: node.loc,
  40. message: "'v-if' and 'v-else-if' directives can't exist on the same element."
  41. })
  42. }
  43. if (node.key.argument) {
  44. context.report({
  45. node,
  46. loc: node.loc,
  47. message: "'v-if' directives require no argument."
  48. })
  49. }
  50. if (node.key.modifiers.length > 0) {
  51. context.report({
  52. node,
  53. loc: node.loc,
  54. message: "'v-if' directives require no modifier."
  55. })
  56. }
  57. if (!utils.hasAttributeValue(node)) {
  58. context.report({
  59. node,
  60. loc: node.loc,
  61. message: "'v-if' directives require that attribute value."
  62. })
  63. }
  64. }
  65. })
  66. }
  67. }