valid-v-else-if.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. categories: ['vue3-essential', 'essential'],
  20. url: 'https://eslint.vuejs.org/rules/valid-v-else-if.html'
  21. },
  22. fixable: null,
  23. schema: [],
  24. messages: {
  25. missingVIf:
  26. "'v-else-if' directives require being preceded by the element which has a 'v-if' or 'v-else-if' directive.",
  27. withVIf:
  28. "'v-else-if' and 'v-if' directives can't exist on the same element.",
  29. withVElse:
  30. "'v-else-if' and 'v-else' directives can't exist on the same element.",
  31. unexpectedArgument: "'v-else-if' directives require no argument.",
  32. unexpectedModifier: "'v-else-if' directives require no modifier.",
  33. expectedValue: "'v-else-if' directives require that attribute value."
  34. }
  35. },
  36. /** @param {RuleContext} context */
  37. create(context) {
  38. return utils.defineTemplateBodyVisitor(context, {
  39. /** @param {VDirective} node */
  40. "VAttribute[directive=true][key.name.name='else-if']"(node) {
  41. const element = node.parent.parent
  42. if (!utils.prevElementHasIf(element)) {
  43. context.report({
  44. node,
  45. messageId: 'missingVIf'
  46. })
  47. }
  48. if (utils.hasDirective(element, 'if')) {
  49. context.report({
  50. node,
  51. messageId: 'withVIf'
  52. })
  53. }
  54. if (utils.hasDirective(element, 'else')) {
  55. context.report({
  56. node,
  57. messageId: 'withVElse'
  58. })
  59. }
  60. if (node.key.argument) {
  61. context.report({
  62. node: node.key.argument,
  63. messageId: 'unexpectedArgument'
  64. })
  65. }
  66. if (node.key.modifiers.length > 0) {
  67. context.report({
  68. node,
  69. loc: {
  70. start: node.key.modifiers[0].loc.start,
  71. end: node.key.modifiers[node.key.modifiers.length - 1].loc.end
  72. },
  73. messageId: 'unexpectedModifier'
  74. })
  75. }
  76. if (!node.value || utils.isEmptyValueDirective(node, context)) {
  77. context.report({
  78. node,
  79. messageId: 'expectedValue'
  80. })
  81. }
  82. }
  83. })
  84. }
  85. }