valid-v-else-if.js 2.4 KB

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