valid-v-else-if.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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-else-if` directives',
  19. category: 'essential',
  20. url: 'https://eslint.vuejs.org/rules/valid-v-else-if.html'
  21. },
  22. fixable: null,
  23. schema: []
  24. },
  25. create (context) {
  26. return utils.defineTemplateBodyVisitor(context, {
  27. "VAttribute[directive=true][key.name='else-if']" (node) {
  28. const element = node.parent.parent
  29. if (!utils.prevElementHasIf(element)) {
  30. context.report({
  31. node,
  32. loc: node.loc,
  33. message: "'v-else-if' directives require being preceded by the element which has a 'v-if' or 'v-else-if' directive."
  34. })
  35. }
  36. if (utils.hasDirective(element, 'if')) {
  37. context.report({
  38. node,
  39. loc: node.loc,
  40. message: "'v-else-if' and 'v-if' directives can't exist on the same element."
  41. })
  42. }
  43. if (utils.hasDirective(element, 'else')) {
  44. context.report({
  45. node,
  46. loc: node.loc,
  47. message: "'v-else-if' and 'v-else' directives can't exist on the same element."
  48. })
  49. }
  50. if (node.key.argument) {
  51. context.report({
  52. node,
  53. loc: node.loc,
  54. message: "'v-else-if' directives require no argument."
  55. })
  56. }
  57. if (node.key.modifiers.length > 0) {
  58. context.report({
  59. node,
  60. loc: node.loc,
  61. message: "'v-else-if' directives require no modifier."
  62. })
  63. }
  64. if (!utils.hasAttributeValue(node)) {
  65. context.report({
  66. node,
  67. loc: node.loc,
  68. message: "'v-else-if' directives require that attribute value."
  69. })
  70. }
  71. }
  72. })
  73. }
  74. }